Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1 | //===--- RewriteObjC.cpp - Playground for the code rewriter ---------------===// |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Hacks and fun related to the code rewriter. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Ted Kremenek | cdf8149 | 2012-09-01 05:09:24 +0000 | [diff] [blame] | 14 | #include "clang/Rewrite/Frontend/ASTConsumers.h" |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 15 | #include "clang/AST/AST.h" |
| 16 | #include "clang/AST/ASTConsumer.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 17 | #include "clang/AST/Attr.h" |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 18 | #include "clang/AST/ParentMap.h" |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 19 | #include "clang/Basic/CharInfo.h" |
Chris Lattner | 4431a1b | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 20 | #include "clang/Basic/Diagnostic.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 21 | #include "clang/Basic/IdentifierTable.h" |
| 22 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | f3a59a1 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 23 | #include "clang/Lex/Lexer.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "clang/Rewrite/Core/Rewriter.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/DenseSet.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallPtrSet.h" |
| 27 | #include "llvm/ADT/StringExtras.h" |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 28 | #include "llvm/Support/MemoryBuffer.h" |
| 29 | #include "llvm/Support/raw_ostream.h" |
Ahmed Charles | dfca6f9 | 2014-03-09 11:36:40 +0000 | [diff] [blame] | 30 | #include <memory> |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 31 | |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 32 | using namespace clang; |
Chris Lattner | 211f8b8 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 33 | using llvm::utostr; |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 34 | |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 35 | namespace { |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 36 | class RewriteObjC : public ASTConsumer { |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 37 | protected: |
| 38 | |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 39 | enum { |
Nico Weber | 1879bca | 2010-11-22 10:26:41 +0000 | [diff] [blame] | 40 | BLOCK_FIELD_IS_OBJECT = 3, /* id, NSObject, __attribute__((NSObject)), |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 41 | block, ... */ |
| 42 | BLOCK_FIELD_IS_BLOCK = 7, /* a block variable */ |
| 43 | BLOCK_FIELD_IS_BYREF = 8, /* the on stack structure holding the |
| 44 | __block variable */ |
Nico Weber | 1879bca | 2010-11-22 10:26:41 +0000 | [diff] [blame] | 45 | BLOCK_FIELD_IS_WEAK = 16, /* declared __weak, only used in byref copy |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 46 | helpers */ |
Nico Weber | 1879bca | 2010-11-22 10:26:41 +0000 | [diff] [blame] | 47 | BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 48 | support routines */ |
| 49 | BLOCK_BYREF_CURRENT_MAX = 256 |
| 50 | }; |
| 51 | |
| 52 | enum { |
| 53 | BLOCK_NEEDS_FREE = (1 << 24), |
| 54 | BLOCK_HAS_COPY_DISPOSE = (1 << 25), |
| 55 | BLOCK_HAS_CXX_OBJ = (1 << 26), |
| 56 | BLOCK_IS_GC = (1 << 27), |
| 57 | BLOCK_IS_GLOBAL = (1 << 28), |
| 58 | BLOCK_HAS_DESCRIPTOR = (1 << 29) |
| 59 | }; |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 60 | static const int OBJC_ABI_VERSION = 7; |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 61 | |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 62 | Rewriter Rewrite; |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 63 | DiagnosticsEngine &Diags; |
Steve Naroff | 945a3b1 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 64 | const LangOptions &LangOpts; |
Chris Lattner | c6d91c0 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 65 | ASTContext *Context; |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 66 | SourceManager *SM; |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 67 | TranslationUnitDecl *TUDecl; |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 68 | FileID MainFileID; |
Chris Lattner | f3a59a1 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 69 | const char *MainFileStart, *MainFileEnd; |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 70 | Stmt *CurrentBody; |
| 71 | ParentMap *PropParentMap; // created lazily. |
Fariborz Jahanian | 8efef60 | 2011-12-05 19:50:04 +0000 | [diff] [blame] | 72 | std::string InFileName; |
| 73 | raw_ostream* OutFile; |
| 74 | std::string Preamble; |
| 75 | |
| 76 | TypeDecl *ProtocolTypeDecl; |
| 77 | VarDecl *GlobalVarDecl; |
| 78 | unsigned RewriteFailedDiag; |
Fariborz Jahanian | 8efef60 | 2011-12-05 19:50:04 +0000 | [diff] [blame] | 79 | // ObjC string constant support. |
| 80 | unsigned NumObjCStringLiterals; |
| 81 | VarDecl *ConstantStringClassReference; |
| 82 | RecordDecl *NSStringRecord; |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 83 | |
Fariborz Jahanian | 8efef60 | 2011-12-05 19:50:04 +0000 | [diff] [blame] | 84 | // ObjC foreach break/continue generation support. |
| 85 | int BcLabelCount; |
| 86 | |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 87 | unsigned TryFinallyContainsReturnDiag; |
Fariborz Jahanian | 8efef60 | 2011-12-05 19:50:04 +0000 | [diff] [blame] | 88 | // Needed for super. |
| 89 | ObjCMethodDecl *CurMethodDef; |
| 90 | RecordDecl *SuperStructDecl; |
| 91 | RecordDecl *ConstantStringDecl; |
| 92 | |
| 93 | FunctionDecl *MsgSendFunctionDecl; |
| 94 | FunctionDecl *MsgSendSuperFunctionDecl; |
| 95 | FunctionDecl *MsgSendStretFunctionDecl; |
| 96 | FunctionDecl *MsgSendSuperStretFunctionDecl; |
| 97 | FunctionDecl *MsgSendFpretFunctionDecl; |
| 98 | FunctionDecl *GetClassFunctionDecl; |
| 99 | FunctionDecl *GetMetaClassFunctionDecl; |
| 100 | FunctionDecl *GetSuperClassFunctionDecl; |
| 101 | FunctionDecl *SelGetUidFunctionDecl; |
| 102 | FunctionDecl *CFStringFunctionDecl; |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 103 | FunctionDecl *SuperConstructorFunctionDecl; |
Fariborz Jahanian | 8efef60 | 2011-12-05 19:50:04 +0000 | [diff] [blame] | 104 | FunctionDecl *CurFunctionDef; |
| 105 | FunctionDecl *CurFunctionDeclToDeclareForBlock; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | |
Fariborz Jahanian | 8efef60 | 2011-12-05 19:50:04 +0000 | [diff] [blame] | 107 | /* Misc. containers needed for meta-data rewrite. */ |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 108 | SmallVector<ObjCImplementationDecl *, 8> ClassImplementation; |
| 109 | SmallVector<ObjCCategoryImplDecl *, 8> CategoryImplementation; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 110 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCSynthesizedStructs; |
Steve Naroff | 13e7487 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 111 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> ObjCSynthesizedProtocols; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 112 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCForwardDecls; |
| 113 | llvm::DenseMap<ObjCMethodDecl*, std::string> MethodInternalNames; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 114 | SmallVector<Stmt *, 32> Stmts; |
| 115 | SmallVector<int, 8> ObjCBcLabelNo; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 116 | // Remember all the @protocol(<expr>) expressions. |
| 117 | llvm::SmallPtrSet<ObjCProtocolDecl *, 32> ProtocolExprDecls; |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 118 | |
| 119 | llvm::DenseSet<uint64_t> CopyDestroyCache; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 120 | |
| 121 | // Block expressions. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 122 | SmallVector<BlockExpr *, 32> Blocks; |
| 123 | SmallVector<int, 32> InnerDeclRefsCount; |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 124 | SmallVector<DeclRefExpr *, 32> InnerDeclRefs; |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 125 | |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 126 | SmallVector<DeclRefExpr *, 32> BlockDeclRefs; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 127 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 128 | // Block related declarations. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 129 | SmallVector<ValueDecl *, 8> BlockByCopyDecls; |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 130 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByCopyDeclsPtrSet; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 131 | SmallVector<ValueDecl *, 8> BlockByRefDecls; |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 132 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByRefDeclsPtrSet; |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 133 | llvm::DenseMap<ValueDecl *, unsigned> BlockByRefDeclNo; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 134 | llvm::SmallPtrSet<ValueDecl *, 8> ImportedBlockDecls; |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 135 | llvm::SmallPtrSet<VarDecl *, 8> ImportedLocalExternalDecls; |
| 136 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 137 | llvm::DenseMap<BlockExpr *, std::string> RewrittenBlockExprs; |
| 138 | |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 139 | // This maps an original source AST to it's rewritten form. This allows |
| 140 | // us to avoid rewriting the same node twice (which is very uncommon). |
| 141 | // This is needed to support some of the exotic property rewriting. |
| 142 | llvm::DenseMap<Stmt *, Stmt *> ReplacedNodes; |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 143 | |
Fariborz Jahanian | 8efef60 | 2011-12-05 19:50:04 +0000 | [diff] [blame] | 144 | // Needed for header files being rewritten |
| 145 | bool IsHeader; |
| 146 | bool SilenceRewriteMacroWarning; |
| 147 | bool objc_impl_method; |
| 148 | |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 149 | bool DisableReplaceStmt; |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 150 | class DisableReplaceStmtScope { |
| 151 | RewriteObjC &R; |
| 152 | bool SavedValue; |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 153 | |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 154 | public: |
| 155 | DisableReplaceStmtScope(RewriteObjC &R) |
| 156 | : R(R), SavedValue(R.DisableReplaceStmt) { |
| 157 | R.DisableReplaceStmt = true; |
| 158 | } |
| 159 | ~DisableReplaceStmtScope() { |
| 160 | R.DisableReplaceStmt = SavedValue; |
| 161 | } |
| 162 | }; |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 163 | void InitializeCommon(ASTContext &context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 164 | |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 165 | public: |
Ted Kremenek | 380df93 | 2008-05-31 20:11:04 +0000 | [diff] [blame] | 166 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 167 | // Top Level Driver code. |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 168 | bool HandleTopLevelDecl(DeclGroupRef D) override { |
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 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 196 | void HandleTranslationUnit(ASTContext &C) override; |
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); |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 331 | |
| 332 | virtual void Initialize(ASTContext &context) override = 0; |
| 333 | |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 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() {} |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 524 | virtual void Initialize(ASTContext &context) override; |
| 525 | |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 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); |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 534 | void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol, |
| 535 | StringRef prefix, StringRef ClassName, |
| 536 | std::string &Result) override; |
| 537 | void RewriteObjCProtocolListMetaData( |
| 538 | const ObjCList<ObjCProtocolDecl> &Prots, |
| 539 | StringRef prefix, StringRef ClassName, std::string &Result) override; |
| 540 | void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
| 541 | std::string &Result) override; |
| 542 | void RewriteMetaDataIntoBuffer(std::string &Result) override; |
| 543 | void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
| 544 | std::string &Result) override; |
| 545 | |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 546 | // Rewriting ivar |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 547 | void RewriteIvarOffsetComputation(ObjCIvarDecl *ivar, |
| 548 | std::string &Result) override; |
| 549 | Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV) override; |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 550 | }; |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 553 | void RewriteObjC::RewriteBlocksInFunctionProtoType(QualType funcType, |
| 554 | NamedDecl *D) { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 555 | if (const FunctionProtoType *fproto |
Abramo Bagnara | 6d81063 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 556 | = dyn_cast<FunctionProtoType>(funcType.IgnoreParens())) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 557 | for (FunctionProtoType::param_type_iterator I = fproto->param_type_begin(), |
| 558 | E = fproto->param_type_end(); |
| 559 | I && (I != E); ++I) |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 560 | if (isTopLevelBlockPointerType(*I)) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 561 | // All the args are checked/rewritten. Don't call twice! |
| 562 | RewriteBlockPointerDecl(D); |
| 563 | break; |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | void RewriteObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 569 | const PointerType *PT = funcType->getAs<PointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 570 | if (PT && PointerTypeTakesAnyBlockArguments(funcType)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 571 | RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 574 | static bool IsHeaderFile(const std::string &Filename) { |
| 575 | std::string::size_type DotPos = Filename.rfind('.'); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 576 | |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 577 | if (DotPos == std::string::npos) { |
| 578 | // no file extension |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 579 | return false; |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 580 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 581 | |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 582 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 583 | // C header: .h |
| 584 | // C++ header: .hh or .H; |
| 585 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 586 | } |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 587 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 588 | RewriteObjC::RewriteObjC(std::string inFile, raw_ostream* OS, |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 589 | DiagnosticsEngine &D, const LangOptions &LOpts, |
Eli Friedman | f22439a | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 590 | bool silenceMacroWarn) |
| 591 | : Diags(D), LangOpts(LOpts), InFileName(inFile), OutFile(OS), |
| 592 | SilenceRewriteMacroWarning(silenceMacroWarn) { |
Steve Naroff | f9e7c90 | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 593 | IsHeader = IsHeaderFile(inFile); |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 594 | RewriteFailedDiag = Diags.getCustomDiagID(DiagnosticsEngine::Warning, |
Steve Naroff | f9e7c90 | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 595 | "rewriting sub-expression within a macro (may not be correct)"); |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 596 | TryFinallyContainsReturnDiag = Diags.getCustomDiagID( |
| 597 | DiagnosticsEngine::Warning, |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 598 | "rewriter doesn't support user-specified control flow semantics " |
| 599 | "for @try/@finally (code may not execute properly)"); |
Steve Naroff | f9e7c90 | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Eli Friedman | a63ab2d | 2009-05-18 22:29:17 +0000 | [diff] [blame] | 602 | ASTConsumer *clang::CreateObjCRewriter(const std::string& InFile, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 603 | raw_ostream* OS, |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 604 | DiagnosticsEngine &Diags, |
Eli Friedman | f22439a | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 605 | const LangOptions &LOpts, |
| 606 | bool SilenceRewriteMacroWarning) { |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 607 | return new RewriteObjCFragileABI(InFile, OS, Diags, LOpts, SilenceRewriteMacroWarning); |
Chris Lattner | e9c810c | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 608 | } |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 609 | |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 610 | void RewriteObjC::InitializeCommon(ASTContext &context) { |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 611 | Context = &context; |
| 612 | SM = &Context->getSourceManager(); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 613 | TUDecl = Context->getTranslationUnitDecl(); |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 614 | MsgSendFunctionDecl = 0; |
| 615 | MsgSendSuperFunctionDecl = 0; |
| 616 | MsgSendStretFunctionDecl = 0; |
| 617 | MsgSendSuperStretFunctionDecl = 0; |
| 618 | MsgSendFpretFunctionDecl = 0; |
| 619 | GetClassFunctionDecl = 0; |
| 620 | GetMetaClassFunctionDecl = 0; |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 621 | GetSuperClassFunctionDecl = 0; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 622 | SelGetUidFunctionDecl = 0; |
| 623 | CFStringFunctionDecl = 0; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 624 | ConstantStringClassReference = 0; |
| 625 | NSStringRecord = 0; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 626 | CurMethodDef = 0; |
| 627 | CurFunctionDef = 0; |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 628 | CurFunctionDeclToDeclareForBlock = 0; |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 629 | GlobalVarDecl = 0; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 630 | SuperStructDecl = 0; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 631 | ProtocolTypeDecl = 0; |
Steve Naroff | 60a9ef6 | 2008-03-27 22:59:54 +0000 | [diff] [blame] | 632 | ConstantStringDecl = 0; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 633 | BcLabelCount = 0; |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 634 | SuperConstructorFunctionDecl = 0; |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 635 | NumObjCStringLiterals = 0; |
Steve Naroff | f1ab600 | 2008-12-08 20:01:41 +0000 | [diff] [blame] | 636 | PropParentMap = 0; |
| 637 | CurrentBody = 0; |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 638 | DisableReplaceStmt = false; |
Fariborz Jahanian | bc6811c | 2010-01-07 22:51:18 +0000 | [diff] [blame] | 639 | objc_impl_method = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 640 | |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 641 | // Get the ID and start/end of the main file. |
| 642 | MainFileID = SM->getMainFileID(); |
| 643 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 644 | MainFileStart = MainBuf->getBufferStart(); |
| 645 | MainFileEnd = MainBuf->getBufferEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 646 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 647 | Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOpts()); |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 648 | } |
| 649 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 650 | //===----------------------------------------------------------------------===// |
| 651 | // Top Level Driver Code |
| 652 | //===----------------------------------------------------------------------===// |
| 653 | |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 654 | void RewriteObjC::HandleTopLevelSingleDecl(Decl *D) { |
Ted Kremenek | 31e7f0f | 2010-02-05 21:28:51 +0000 | [diff] [blame] | 655 | if (Diags.hasErrorOccurred()) |
| 656 | return; |
| 657 | |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 658 | // Two cases: either the decl could be in the main file, or it could be in a |
| 659 | // #included file. If the former, rewrite it now. If the later, check to see |
| 660 | // if we rewrote the #include/#import. |
| 661 | SourceLocation Loc = D->getLocation(); |
Chandler Carruth | 35f5320 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 662 | Loc = SM->getExpansionLoc(Loc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 663 | |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 664 | // If this is for a builtin, ignore it. |
| 665 | if (Loc.isInvalid()) return; |
| 666 | |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 667 | // Look for built-in declarations that we need to refer during the rewrite. |
| 668 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 669 | RewriteFunctionDecl(FD); |
Steve Naroff | 08899ff | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 670 | } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) { |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 671 | // declared in <Foundation/NSString.h> |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 672 | if (FVD->getName() == "_NSConstantStringClassReference") { |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 673 | ConstantStringClassReference = FVD; |
| 674 | return; |
| 675 | } |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 676 | } else if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) { |
| 677 | if (ID->isThisDeclarationADefinition()) |
| 678 | RewriteInterfaceDecl(ID); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 679 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 680 | RewriteCategoryDecl(CD); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 681 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 682 | if (PD->isThisDeclarationADefinition()) |
| 683 | RewriteProtocolDecl(PD); |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 684 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
| 685 | // Recurse into linkage specifications |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 686 | for (DeclContext::decl_iterator DI = LSD->decls_begin(), |
| 687 | DIEnd = LSD->decls_end(); |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 688 | DI != DIEnd; ) { |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 689 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>((*DI))) { |
| 690 | if (!IFace->isThisDeclarationADefinition()) { |
| 691 | SmallVector<Decl *, 8> DG; |
| 692 | SourceLocation StartLoc = IFace->getLocStart(); |
| 693 | do { |
| 694 | if (isa<ObjCInterfaceDecl>(*DI) && |
| 695 | !cast<ObjCInterfaceDecl>(*DI)->isThisDeclarationADefinition() && |
| 696 | StartLoc == (*DI)->getLocStart()) |
| 697 | DG.push_back(*DI); |
| 698 | else |
| 699 | break; |
| 700 | |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 701 | ++DI; |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 702 | } while (DI != DIEnd); |
| 703 | RewriteForwardClassDecl(DG); |
| 704 | continue; |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 705 | } |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 706 | } |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 707 | |
| 708 | if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>((*DI))) { |
| 709 | if (!Proto->isThisDeclarationADefinition()) { |
| 710 | SmallVector<Decl *, 8> DG; |
| 711 | SourceLocation StartLoc = Proto->getLocStart(); |
| 712 | do { |
| 713 | if (isa<ObjCProtocolDecl>(*DI) && |
| 714 | !cast<ObjCProtocolDecl>(*DI)->isThisDeclarationADefinition() && |
| 715 | StartLoc == (*DI)->getLocStart()) |
| 716 | DG.push_back(*DI); |
| 717 | else |
| 718 | break; |
| 719 | |
| 720 | ++DI; |
| 721 | } while (DI != DIEnd); |
| 722 | RewriteForwardProtocolDecl(DG); |
| 723 | continue; |
| 724 | } |
| 725 | } |
| 726 | |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 727 | HandleTopLevelSingleDecl(*DI); |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 728 | ++DI; |
| 729 | } |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 730 | } |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 731 | // 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] | 732 | if (SM->isWrittenInMainFile(Loc)) |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 733 | return HandleDeclInMainFile(D); |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 734 | } |
| 735 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 736 | //===----------------------------------------------------------------------===// |
| 737 | // Syntactic (non-AST) Rewriting Code |
| 738 | //===----------------------------------------------------------------------===// |
| 739 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 740 | void RewriteObjC::RewriteInclude() { |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 741 | SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 742 | StringRef MainBuf = SM->getBufferData(MainFileID); |
Benjamin Kramer | eb92dc0 | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 743 | const char *MainBufStart = MainBuf.begin(); |
| 744 | const char *MainBufEnd = MainBuf.end(); |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 745 | size_t ImportLen = strlen("import"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 746 | |
Fariborz Jahanian | 137d693 | 2008-01-19 01:03:17 +0000 | [diff] [blame] | 747 | // Loop over the whole file, looking for includes. |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 748 | for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { |
| 749 | if (*BufPtr == '#') { |
| 750 | if (++BufPtr == MainBufEnd) |
| 751 | return; |
| 752 | while (*BufPtr == ' ' || *BufPtr == '\t') |
| 753 | if (++BufPtr == MainBufEnd) |
| 754 | return; |
| 755 | if (!strncmp(BufPtr, "import", ImportLen)) { |
| 756 | // replace import with include |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 757 | SourceLocation ImportLoc = |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 758 | LocStart.getLocWithOffset(BufPtr-MainBufStart); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 759 | ReplaceText(ImportLoc, ImportLen, "include"); |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 760 | BufPtr += ImportLen; |
| 761 | } |
| 762 | } |
| 763 | } |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 766 | static std::string getIvarAccessString(ObjCIvarDecl *OID) { |
| 767 | const ObjCInterfaceDecl *ClassDecl = OID->getContainingInterface(); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 768 | std::string S; |
| 769 | S = "((struct "; |
| 770 | S += ClassDecl->getIdentifier()->getName(); |
| 771 | S += "_IMPL *)self)->"; |
Daniel Dunbar | 70e7ead | 2009-10-18 20:26:27 +0000 | [diff] [blame] | 772 | S += OID->getName(); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 773 | return S; |
| 774 | } |
| 775 | |
Steve Naroff | c038b3a | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 776 | void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 777 | ObjCImplementationDecl *IMD, |
| 778 | ObjCCategoryImplDecl *CID) { |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 779 | static bool objcGetPropertyDefined = false; |
| 780 | static bool objcSetPropertyDefined = false; |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 781 | SourceLocation startLoc = PID->getLocStart(); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 782 | InsertText(startLoc, "// "); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 783 | const char *startBuf = SM->getCharacterData(startLoc); |
| 784 | assert((*startBuf == '@') && "bogus @synthesize location"); |
| 785 | const char *semiBuf = strchr(startBuf, ';'); |
| 786 | assert((*semiBuf == ';') && "@synthesize: can't find ';'"); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 787 | SourceLocation onePastSemiLoc = |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 788 | startLoc.getLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 789 | |
| 790 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 791 | return; // FIXME: is this correct? |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 792 | |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 793 | // Generate the 'getter' function. |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 794 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 795 | ObjCIvarDecl *OID = PID->getPropertyIvarDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 796 | |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 797 | if (!OID) |
| 798 | return; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 799 | unsigned Attributes = PD->getPropertyAttributes(); |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 800 | if (!PD->getGetterMethodDecl()->isDefined()) { |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 801 | bool GenGetProperty = !(Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) && |
| 802 | (Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 803 | ObjCPropertyDecl::OBJC_PR_copy)); |
| 804 | std::string Getr; |
| 805 | if (GenGetProperty && !objcGetPropertyDefined) { |
| 806 | objcGetPropertyDefined = true; |
| 807 | // FIXME. Is this attribute correct in all cases? |
| 808 | Getr = "\nextern \"C\" __declspec(dllimport) " |
| 809 | "id objc_getProperty(id, SEL, long, bool);\n"; |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 810 | } |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 811 | RewriteObjCMethodDecl(OID->getContainingInterface(), |
| 812 | PD->getGetterMethodDecl(), Getr); |
| 813 | Getr += "{ "; |
| 814 | // Synthesize an explicit cast to gain access to the ivar. |
| 815 | // See objc-act.c:objc_synthesize_new_getter() for details. |
| 816 | if (GenGetProperty) { |
| 817 | // return objc_getProperty(self, _cmd, offsetof(ClassDecl, OID), 1) |
| 818 | Getr += "typedef "; |
| 819 | const FunctionType *FPRetType = 0; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 820 | RewriteTypeIntoString(PD->getGetterMethodDecl()->getReturnType(), Getr, |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 821 | FPRetType); |
| 822 | Getr += " _TYPE"; |
| 823 | if (FPRetType) { |
| 824 | Getr += ")"; // close the precedence "scope" for "*". |
| 825 | |
| 826 | // Now, emit the argument types (if any). |
| 827 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)){ |
| 828 | Getr += "("; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 829 | for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) { |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 830 | if (i) Getr += ", "; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 831 | std::string ParamStr = |
| 832 | FT->getParamType(i).getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 833 | Getr += ParamStr; |
| 834 | } |
| 835 | if (FT->isVariadic()) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 836 | if (FT->getNumParams()) |
| 837 | Getr += ", "; |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 838 | Getr += "..."; |
| 839 | } |
| 840 | Getr += ")"; |
| 841 | } else |
| 842 | Getr += "()"; |
| 843 | } |
| 844 | Getr += ";\n"; |
| 845 | Getr += "return (_TYPE)"; |
| 846 | Getr += "objc_getProperty(self, _cmd, "; |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 847 | RewriteIvarOffsetComputation(OID, Getr); |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 848 | Getr += ", 1)"; |
| 849 | } |
| 850 | else |
| 851 | Getr += "return " + getIvarAccessString(OID); |
| 852 | Getr += "; }"; |
| 853 | InsertText(onePastSemiLoc, Getr); |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 854 | } |
Fariborz Jahanian | 7c299bc | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 855 | |
| 856 | if (PD->isReadOnly() || PD->getSetterMethodDecl()->isDefined()) |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 857 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 858 | |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 859 | // Generate the 'setter' function. |
| 860 | std::string Setr; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 861 | bool GenSetProperty = Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 862 | ObjCPropertyDecl::OBJC_PR_copy); |
| 863 | if (GenSetProperty && !objcSetPropertyDefined) { |
| 864 | objcSetPropertyDefined = true; |
| 865 | // FIXME. Is this attribute correct in all cases? |
| 866 | Setr = "\nextern \"C\" __declspec(dllimport) " |
| 867 | "void objc_setProperty (id, SEL, long, id, bool, bool);\n"; |
| 868 | } |
| 869 | |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 870 | RewriteObjCMethodDecl(OID->getContainingInterface(), |
| 871 | PD->getSetterMethodDecl(), Setr); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 872 | Setr += "{ "; |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 873 | // Synthesize an explicit cast to initialize the ivar. |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 874 | // See objc-act.c:objc_synthesize_new_setter() for details. |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 875 | if (GenSetProperty) { |
| 876 | Setr += "objc_setProperty (self, _cmd, "; |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 877 | RewriteIvarOffsetComputation(OID, Setr); |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 878 | Setr += ", (id)"; |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 879 | Setr += PD->getName(); |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 880 | Setr += ", "; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 881 | if (Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 882 | Setr += "0, "; |
| 883 | else |
| 884 | Setr += "1, "; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 885 | if (Attributes & ObjCPropertyDecl::OBJC_PR_copy) |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 886 | Setr += "1)"; |
| 887 | else |
| 888 | Setr += "0)"; |
| 889 | } |
| 890 | else { |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 891 | Setr += getIvarAccessString(OID) + " = "; |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 892 | Setr += PD->getName(); |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 893 | } |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 894 | Setr += "; }"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 895 | InsertText(onePastSemiLoc, Setr); |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 896 | } |
Chris Lattner | 16a0de4 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 897 | |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 898 | static void RewriteOneForwardClassDecl(ObjCInterfaceDecl *ForwardDecl, |
| 899 | std::string &typedefString) { |
| 900 | typedefString += "#ifndef _REWRITER_typedef_"; |
| 901 | typedefString += ForwardDecl->getNameAsString(); |
| 902 | typedefString += "\n"; |
| 903 | typedefString += "#define _REWRITER_typedef_"; |
| 904 | typedefString += ForwardDecl->getNameAsString(); |
| 905 | typedefString += "\n"; |
| 906 | typedefString += "typedef struct objc_object "; |
| 907 | typedefString += ForwardDecl->getNameAsString(); |
| 908 | typedefString += ";\n#endif\n"; |
| 909 | } |
| 910 | |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 911 | void RewriteObjC::RewriteForwardClassEpilogue(ObjCInterfaceDecl *ClassDecl, |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 912 | const std::string &typedefString) { |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 913 | SourceLocation startLoc = ClassDecl->getLocStart(); |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 914 | const char *startBuf = SM->getCharacterData(startLoc); |
| 915 | const char *semiPtr = strchr(startBuf, ';'); |
| 916 | // Replace the @class with typedefs corresponding to the classes. |
| 917 | ReplaceText(startLoc, semiPtr-startBuf+1, typedefString); |
| 918 | } |
| 919 | |
Fariborz Jahanian | 3a039e3 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 920 | void RewriteObjC::RewriteForwardClassDecl(DeclGroupRef D) { |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 921 | std::string typedefString; |
Fariborz Jahanian | 3a039e3 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 922 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 923 | ObjCInterfaceDecl *ForwardDecl = cast<ObjCInterfaceDecl>(*I); |
Fariborz Jahanian | 3a039e3 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 924 | if (I == D.begin()) { |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 925 | // Translate to typedef's that forward reference structs with the same name |
| 926 | // as the class. As a convenience, we include the original declaration |
| 927 | // as a comment. |
Fariborz Jahanian | 3a039e3 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 928 | typedefString += "// @class "; |
| 929 | typedefString += ForwardDecl->getNameAsString(); |
Fariborz Jahanian | 1c2cb6d | 2010-01-11 22:48:40 +0000 | [diff] [blame] | 930 | typedefString += ";\n"; |
Fariborz Jahanian | 3a039e3 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 931 | } |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 932 | RewriteOneForwardClassDecl(ForwardDecl, typedefString); |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 933 | } |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 934 | DeclGroupRef::iterator I = D.begin(); |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 935 | RewriteForwardClassEpilogue(cast<ObjCInterfaceDecl>(*I), typedefString); |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 936 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 937 | |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 938 | void RewriteObjC::RewriteForwardClassDecl(const SmallVectorImpl<Decl *> &D) { |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 939 | std::string typedefString; |
| 940 | for (unsigned i = 0; i < D.size(); i++) { |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 941 | ObjCInterfaceDecl *ForwardDecl = cast<ObjCInterfaceDecl>(D[i]); |
Fariborz Jahanian | abc11aa | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 942 | if (i == 0) { |
| 943 | typedefString += "// @class "; |
| 944 | typedefString += ForwardDecl->getNameAsString(); |
| 945 | typedefString += ";\n"; |
| 946 | } |
| 947 | RewriteOneForwardClassDecl(ForwardDecl, typedefString); |
| 948 | } |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 949 | RewriteForwardClassEpilogue(cast<ObjCInterfaceDecl>(D[0]), typedefString); |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 950 | } |
| 951 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 952 | void RewriteObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) { |
Fariborz Jahanian | da8ec2b | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 953 | // When method is a synthesized one, such as a getter/setter there is |
| 954 | // nothing to rewrite. |
Fariborz Jahanian | e1378a4 | 2011-09-09 20:35:22 +0000 | [diff] [blame] | 955 | if (Method->isImplicit()) |
Fariborz Jahanian | da8ec2b | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 956 | return; |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 957 | SourceLocation LocStart = Method->getLocStart(); |
| 958 | SourceLocation LocEnd = Method->getLocEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 959 | |
Chandler Carruth | d48db21 | 2011-07-25 21:09:52 +0000 | [diff] [blame] | 960 | if (SM->getExpansionLineNumber(LocEnd) > |
| 961 | SM->getExpansionLineNumber(LocStart)) { |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 962 | InsertText(LocStart, "#if 0\n"); |
| 963 | ReplaceText(LocEnd, 1, ";\n#endif\n"); |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 964 | } else { |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 965 | InsertText(LocStart, "// "); |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 966 | } |
| 967 | } |
| 968 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 969 | void RewriteObjC::RewriteProperty(ObjCPropertyDecl *prop) { |
Fariborz Jahanian | da8ec2b | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 970 | SourceLocation Loc = prop->getAtLoc(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 971 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 972 | ReplaceText(Loc, 0, "// "); |
Steve Naroff | 0c0f5ba | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 973 | // FIXME: handle properties that are declared across multiple lines. |
Fariborz Jahanian | e8a3016 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 974 | } |
| 975 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 976 | void RewriteObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 977 | SourceLocation LocStart = CatDecl->getLocStart(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 978 | |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 979 | // FIXME: handle category headers that are declared across multiple lines. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 980 | ReplaceText(LocStart, 0, "// "); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 981 | |
Aaron Ballman | d174edf | 2014-03-13 19:11:50 +0000 | [diff] [blame] | 982 | for (auto *I : CatDecl->properties()) |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 983 | RewriteProperty(I); |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 984 | for (auto *I : CatDecl->instance_methods()) |
| 985 | RewriteMethodDeclaration(I); |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 986 | for (auto *I : CatDecl->class_methods()) |
| 987 | RewriteMethodDeclaration(I); |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 988 | |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 989 | // Lastly, comment out the @end. |
Fariborz Jahanian | 427ee8b | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 990 | ReplaceText(CatDecl->getAtEndRange().getBegin(), |
| 991 | strlen("@end"), "/* @end */"); |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 992 | } |
| 993 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 994 | void RewriteObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 995 | SourceLocation LocStart = PDecl->getLocStart(); |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 996 | assert(PDecl->isThisDeclarationADefinition()); |
| 997 | |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 998 | // FIXME: handle protocol headers that are declared across multiple lines. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 999 | ReplaceText(LocStart, 0, "// "); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1000 | |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 1001 | for (auto *I : PDecl->instance_methods()) |
| 1002 | RewriteMethodDeclaration(I); |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 1003 | for (auto *I : PDecl->class_methods()) |
| 1004 | RewriteMethodDeclaration(I); |
Aaron Ballman | d174edf | 2014-03-13 19:11:50 +0000 | [diff] [blame] | 1005 | for (auto *I : PDecl->properties()) |
Aaron Ballman | dc4bea4 | 2014-03-13 18:47:37 +0000 | [diff] [blame] | 1006 | RewriteProperty(I); |
Fariborz Jahanian | aa0f2b3 | 2010-09-24 18:36:58 +0000 | [diff] [blame] | 1007 | |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 1008 | // Lastly, comment out the @end. |
Ted Kremenek | c7c6431 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1009 | SourceLocation LocEnd = PDecl->getAtEndRange().getBegin(); |
Fariborz Jahanian | 427ee8b | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 1010 | ReplaceText(LocEnd, strlen("@end"), "/* @end */"); |
Steve Naroff | a509f04 | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 1011 | |
Fariborz Jahanian | fe38ba2 | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 1012 | // Must comment out @optional/@required |
| 1013 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1014 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 1015 | for (const char *p = startBuf; p < endBuf; p++) { |
| 1016 | if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1017 | SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1018 | ReplaceText(OptionalLoc, strlen("@optional"), "/* @optional */"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1019 | |
Fariborz Jahanian | fe38ba2 | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 1020 | } |
| 1021 | else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1022 | SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1023 | ReplaceText(OptionalLoc, strlen("@required"), "/* @required */"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1024 | |
Fariborz Jahanian | fe38ba2 | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 1025 | } |
| 1026 | } |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1029 | void RewriteObjC::RewriteForwardProtocolDecl(DeclGroupRef D) { |
| 1030 | SourceLocation LocStart = (*D.begin())->getLocStart(); |
| 1031 | if (LocStart.isInvalid()) |
| 1032 | llvm_unreachable("Invalid SourceLocation"); |
| 1033 | // FIXME: handle forward protocol that are declared across multiple lines. |
| 1034 | ReplaceText(LocStart, 0, "// "); |
| 1035 | } |
| 1036 | |
| 1037 | void |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 1038 | RewriteObjC::RewriteForwardProtocolDecl(const SmallVectorImpl<Decl *> &DG) { |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1039 | SourceLocation LocStart = DG[0]->getLocStart(); |
Steve Naroff | c17b056 | 2007-11-14 03:37:28 +0000 | [diff] [blame] | 1040 | if (LocStart.isInvalid()) |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1041 | llvm_unreachable("Invalid SourceLocation"); |
Fariborz Jahanian | da6165c | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 1042 | // FIXME: handle forward protocol that are declared across multiple lines. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1043 | ReplaceText(LocStart, 0, "// "); |
Fariborz Jahanian | da6165c | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1046 | void RewriteObjC::RewriteTypeIntoString(QualType T, std::string &ResultStr, |
| 1047 | const FunctionType *&FPRetType) { |
| 1048 | if (T->isObjCQualifiedIdType()) |
Fariborz Jahanian | 24cb52c | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1049 | ResultStr += "id"; |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1050 | else if (T->isFunctionPointerType() || |
| 1051 | T->isBlockPointerType()) { |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1052 | // needs special handling, since pointer-to-functions have special |
| 1053 | // syntax (where a decaration models use). |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1054 | QualType retType = T; |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1055 | QualType PointeeTy; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1056 | if (const PointerType* PT = retType->getAs<PointerType>()) |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1057 | PointeeTy = PT->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1058 | else if (const BlockPointerType *BPT = retType->getAs<BlockPointerType>()) |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1059 | PointeeTy = BPT->getPointeeType(); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1060 | if ((FPRetType = PointeeTy->getAs<FunctionType>())) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1061 | ResultStr += |
| 1062 | FPRetType->getReturnType().getAsString(Context->getPrintingPolicy()); |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1063 | ResultStr += "(*"; |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1064 | } |
| 1065 | } else |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1066 | ResultStr += T.getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1069 | void RewriteObjC::RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl, |
| 1070 | ObjCMethodDecl *OMD, |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1071 | std::string &ResultStr) { |
| 1072 | //fprintf(stderr,"In RewriteObjCMethodDecl\n"); |
| 1073 | const FunctionType *FPRetType = 0; |
| 1074 | ResultStr += "\nstatic "; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1075 | RewriteTypeIntoString(OMD->getReturnType(), ResultStr, FPRetType); |
Fariborz Jahanian | 7262fca | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 1076 | ResultStr += " "; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1077 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1078 | // Unique method name |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1079 | std::string NameStr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1080 | |
Douglas Gregor | ffca3a2 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1081 | if (OMD->isInstanceMethod()) |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1082 | NameStr += "_I_"; |
| 1083 | else |
| 1084 | NameStr += "_C_"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1085 | |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1086 | NameStr += IDecl->getNameAsString(); |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1087 | NameStr += "_"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1088 | |
| 1089 | if (ObjCCategoryImplDecl *CID = |
Steve Naroff | 11b387f | 2009-01-08 19:41:02 +0000 | [diff] [blame] | 1090 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1091 | NameStr += CID->getNameAsString(); |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1092 | NameStr += "_"; |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1093 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1094 | // Append selector names, replacing ':' with '_' |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1095 | { |
| 1096 | std::string selString = OMD->getSelector().getAsString(); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1097 | int len = selString.size(); |
| 1098 | for (int i = 0; i < len; i++) |
| 1099 | if (selString[i] == ':') |
| 1100 | selString[i] = '_'; |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1101 | NameStr += selString; |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1102 | } |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1103 | // Remember this name for metadata emission |
| 1104 | MethodInternalNames[OMD] = NameStr; |
| 1105 | ResultStr += NameStr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1106 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1107 | // Rewrite arguments |
| 1108 | ResultStr += "("; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1109 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1110 | // invisible arguments |
Douglas Gregor | ffca3a2 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1111 | if (OMD->isInstanceMethod()) { |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1112 | QualType selfTy = Context->getObjCInterfaceType(IDecl); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1113 | selfTy = Context->getPointerType(selfTy); |
Francois Pichet | 0706d20 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 1114 | if (!LangOpts.MicrosoftExt) { |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1115 | if (ObjCSynthesizedStructs.count(const_cast<ObjCInterfaceDecl*>(IDecl))) |
Steve Naroff | dc5b6b2 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 1116 | ResultStr += "struct "; |
| 1117 | } |
| 1118 | // When rewriting for Microsoft, explicitly omit the structure name. |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1119 | ResultStr += IDecl->getNameAsString(); |
Steve Naroff | a1e115e | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1120 | ResultStr += " *"; |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1121 | } |
| 1122 | else |
Daniel Dunbar | 8ab6c54 | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1123 | ResultStr += Context->getObjCClassType().getAsString( |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1124 | Context->getPrintingPolicy()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1125 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1126 | ResultStr += " self, "; |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1127 | ResultStr += Context->getObjCSelType().getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1128 | ResultStr += " _cmd"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1129 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1130 | // Method arguments. |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 1131 | for (const auto *PDecl : OMD->params()) { |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1132 | ResultStr += ", "; |
Steve Naroff | dcdcdcd | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1133 | if (PDecl->getType()->isObjCQualifiedIdType()) { |
| 1134 | ResultStr += "id "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1135 | ResultStr += PDecl->getNameAsString(); |
Steve Naroff | dcdcdcd | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1136 | } else { |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1137 | std::string Name = PDecl->getNameAsString(); |
Fariborz Jahanian | 31b8a9d | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 1138 | QualType QT = PDecl->getType(); |
| 1139 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 835cabe | 2012-03-27 16:42:20 +0000 | [diff] [blame] | 1140 | (void)convertBlockPointerToFunctionPointer(QT); |
| 1141 | QT.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Steve Naroff | dcdcdcd | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1142 | ResultStr += Name; |
| 1143 | } |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1144 | } |
Fariborz Jahanian | eab81cd | 2008-01-21 20:14:23 +0000 | [diff] [blame] | 1145 | if (OMD->isVariadic()) |
| 1146 | ResultStr += ", ..."; |
Fariborz Jahanian | 7262fca | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 1147 | ResultStr += ") "; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1148 | |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1149 | if (FPRetType) { |
| 1150 | ResultStr += ")"; // close the precedence "scope" for "*". |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1151 | |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1152 | // Now, emit the argument types (if any). |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1153 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) { |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1154 | ResultStr += "("; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1155 | for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) { |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1156 | if (i) ResultStr += ", "; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1157 | std::string ParamStr = |
| 1158 | FT->getParamType(i).getAsString(Context->getPrintingPolicy()); |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1159 | ResultStr += ParamStr; |
| 1160 | } |
| 1161 | if (FT->isVariadic()) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1162 | if (FT->getNumParams()) |
| 1163 | ResultStr += ", "; |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1164 | ResultStr += "..."; |
| 1165 | } |
| 1166 | ResultStr += ")"; |
| 1167 | } else { |
| 1168 | ResultStr += "()"; |
| 1169 | } |
| 1170 | } |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1171 | } |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1172 | void RewriteObjC::RewriteImplementationDecl(Decl *OID) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1173 | ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID); |
| 1174 | ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1175 | |
Fariborz Jahanian | 02d964b | 2010-02-15 21:11:41 +0000 | [diff] [blame] | 1176 | InsertText(IMD ? IMD->getLocStart() : CID->getLocStart(), "// "); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1177 | |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 1178 | for (auto *OMD : IMD ? IMD->instance_methods() : CID->instance_methods()) { |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1179 | std::string ResultStr; |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1180 | RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1181 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1182 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1183 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1184 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1185 | const char *endBuf = SM->getCharacterData(LocEnd); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1186 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1187 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1188 | |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 1189 | for (auto *OMD : IMD ? IMD->class_methods() : CID->class_methods()) { |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1190 | std::string ResultStr; |
Fariborz Jahanian | 1cee0ad | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1191 | RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1192 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1193 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1194 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1195 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1196 | const char *endBuf = SM->getCharacterData(LocEnd); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1197 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1198 | } |
Aaron Ballman | d85eff4 | 2014-03-14 15:02:45 +0000 | [diff] [blame] | 1199 | for (auto *I : IMD ? IMD->property_impls() : CID->property_impls()) |
| 1200 | RewritePropertyImplDecl(I, IMD, CID); |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1201 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1202 | InsertText(IMD ? IMD->getLocEnd() : CID->getLocEnd(), "// "); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1205 | void RewriteObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { |
Steve Naroff | c548404 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 1206 | std::string ResultStr; |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1207 | if (!ObjCForwardDecls.count(ClassDecl->getCanonicalDecl())) { |
Steve Naroff | 2f55b98 | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1208 | // we haven't seen a forward decl - generate a typedef. |
Steve Naroff | 03f2767 | 2007-11-14 23:02:56 +0000 | [diff] [blame] | 1209 | ResultStr = "#ifndef _REWRITER_typedef_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1210 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1211 | ResultStr += "\n"; |
| 1212 | ResultStr += "#define _REWRITER_typedef_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1213 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1214 | ResultStr += "\n"; |
Steve Naroff | a1e115e | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1215 | ResultStr += "typedef struct objc_object "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1216 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1217 | ResultStr += ";\n#endif\n"; |
Steve Naroff | 2f55b98 | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1218 | // Mark this typedef as having been generated. |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1219 | ObjCForwardDecls.insert(ClassDecl->getCanonicalDecl()); |
Steve Naroff | 2f55b98 | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1220 | } |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 1221 | RewriteObjCInternalStruct(ClassDecl, ResultStr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1222 | |
Aaron Ballman | d174edf | 2014-03-13 19:11:50 +0000 | [diff] [blame] | 1223 | for (auto *I : ClassDecl->properties()) |
Aaron Ballman | dc4bea4 | 2014-03-13 18:47:37 +0000 | [diff] [blame] | 1224 | RewriteProperty(I); |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 1225 | for (auto *I : ClassDecl->instance_methods()) |
| 1226 | RewriteMethodDeclaration(I); |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 1227 | for (auto *I : ClassDecl->class_methods()) |
| 1228 | RewriteMethodDeclaration(I); |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1229 | |
Steve Naroff | 4cd61ac | 2007-10-30 03:43:13 +0000 | [diff] [blame] | 1230 | // Lastly, comment out the @end. |
Fariborz Jahanian | 427ee8b | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 1231 | ReplaceText(ClassDecl->getAtEndRange().getBegin(), strlen("@end"), |
| 1232 | "/* @end */"); |
Steve Naroff | 161a92b | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1235 | Stmt *RewriteObjC::RewritePropertyOrImplicitSetter(PseudoObjectExpr *PseudoOp) { |
| 1236 | SourceRange OldRange = PseudoOp->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1237 | |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1238 | // We just magically know some things about the structure of this |
| 1239 | // expression. |
| 1240 | ObjCMessageExpr *OldMsg = |
| 1241 | cast<ObjCMessageExpr>(PseudoOp->getSemanticExpr( |
| 1242 | PseudoOp->getNumSemanticExprs() - 1)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1243 | |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1244 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 1245 | // we need to suppress rewriting the sub-statements. |
| 1246 | Expr *Base, *RHS; |
| 1247 | { |
| 1248 | DisableReplaceStmtScope S(*this); |
| 1249 | |
| 1250 | // Rebuild the base expression if we have one. |
| 1251 | Base = 0; |
| 1252 | if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) { |
| 1253 | Base = OldMsg->getInstanceReceiver(); |
| 1254 | Base = cast<OpaqueValueExpr>(Base)->getSourceExpr(); |
| 1255 | Base = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Base)); |
| 1256 | } |
| 1257 | |
| 1258 | // Rebuild the RHS. |
| 1259 | RHS = cast<BinaryOperator>(PseudoOp->getSyntacticForm())->getRHS(); |
| 1260 | RHS = cast<OpaqueValueExpr>(RHS)->getSourceExpr(); |
| 1261 | RHS = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(RHS)); |
| 1262 | } |
| 1263 | |
| 1264 | // TODO: avoid this copy. |
| 1265 | SmallVector<SourceLocation, 1> SelLocs; |
| 1266 | OldMsg->getSelectorLocs(SelLocs); |
| 1267 | |
Chad Rosier | 598f3d2 | 2012-01-06 20:05:14 +0000 | [diff] [blame] | 1268 | ObjCMessageExpr *NewMsg = 0; |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1269 | switch (OldMsg->getReceiverKind()) { |
| 1270 | case ObjCMessageExpr::Class: |
| 1271 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1272 | OldMsg->getValueKind(), |
| 1273 | OldMsg->getLeftLoc(), |
| 1274 | OldMsg->getClassReceiverTypeInfo(), |
| 1275 | OldMsg->getSelector(), |
| 1276 | SelLocs, |
| 1277 | OldMsg->getMethodDecl(), |
| 1278 | RHS, |
Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1279 | OldMsg->getRightLoc(), |
| 1280 | OldMsg->isImplicit()); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1281 | break; |
| 1282 | |
| 1283 | case ObjCMessageExpr::Instance: |
| 1284 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1285 | OldMsg->getValueKind(), |
| 1286 | OldMsg->getLeftLoc(), |
| 1287 | Base, |
| 1288 | OldMsg->getSelector(), |
| 1289 | SelLocs, |
| 1290 | OldMsg->getMethodDecl(), |
| 1291 | RHS, |
Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1292 | OldMsg->getRightLoc(), |
| 1293 | OldMsg->isImplicit()); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1294 | break; |
| 1295 | |
| 1296 | case ObjCMessageExpr::SuperClass: |
| 1297 | case ObjCMessageExpr::SuperInstance: |
| 1298 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1299 | OldMsg->getValueKind(), |
| 1300 | OldMsg->getLeftLoc(), |
| 1301 | OldMsg->getSuperLoc(), |
| 1302 | OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance, |
| 1303 | OldMsg->getSuperType(), |
| 1304 | OldMsg->getSelector(), |
| 1305 | SelLocs, |
| 1306 | OldMsg->getMethodDecl(), |
| 1307 | RHS, |
Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1308 | OldMsg->getRightLoc(), |
| 1309 | OldMsg->isImplicit()); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1310 | break; |
| 1311 | } |
| 1312 | |
| 1313 | Stmt *Replacement = SynthMessageExpr(NewMsg); |
| 1314 | ReplaceStmtWithRange(PseudoOp, Replacement, OldRange); |
| 1315 | return Replacement; |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1318 | Stmt *RewriteObjC::RewritePropertyOrImplicitGetter(PseudoObjectExpr *PseudoOp) { |
| 1319 | SourceRange OldRange = PseudoOp->getSourceRange(); |
| 1320 | |
| 1321 | // We just magically know some things about the structure of this |
| 1322 | // expression. |
| 1323 | ObjCMessageExpr *OldMsg = |
| 1324 | cast<ObjCMessageExpr>(PseudoOp->getResultExpr()->IgnoreImplicit()); |
| 1325 | |
| 1326 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 1327 | // we need to suppress rewriting the sub-statements. |
| 1328 | Expr *Base = 0; |
| 1329 | { |
| 1330 | DisableReplaceStmtScope S(*this); |
| 1331 | |
| 1332 | // Rebuild the base expression if we have one. |
| 1333 | if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) { |
| 1334 | Base = OldMsg->getInstanceReceiver(); |
| 1335 | Base = cast<OpaqueValueExpr>(Base)->getSourceExpr(); |
| 1336 | Base = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Base)); |
John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 1337 | } |
Fariborz Jahanian | bb40ea4 | 2010-10-20 16:07:20 +0000 | [diff] [blame] | 1338 | } |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1339 | |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1340 | // Intentionally empty. |
| 1341 | SmallVector<SourceLocation, 1> SelLocs; |
| 1342 | SmallVector<Expr*, 1> Args; |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1343 | |
Chad Rosier | 598f3d2 | 2012-01-06 20:05:14 +0000 | [diff] [blame] | 1344 | ObjCMessageExpr *NewMsg = 0; |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1345 | switch (OldMsg->getReceiverKind()) { |
| 1346 | case ObjCMessageExpr::Class: |
| 1347 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1348 | OldMsg->getValueKind(), |
| 1349 | OldMsg->getLeftLoc(), |
| 1350 | OldMsg->getClassReceiverTypeInfo(), |
| 1351 | OldMsg->getSelector(), |
| 1352 | SelLocs, |
| 1353 | OldMsg->getMethodDecl(), |
| 1354 | Args, |
Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1355 | OldMsg->getRightLoc(), |
| 1356 | OldMsg->isImplicit()); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1357 | break; |
| 1358 | |
| 1359 | case ObjCMessageExpr::Instance: |
| 1360 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1361 | OldMsg->getValueKind(), |
| 1362 | OldMsg->getLeftLoc(), |
| 1363 | Base, |
| 1364 | OldMsg->getSelector(), |
| 1365 | SelLocs, |
| 1366 | OldMsg->getMethodDecl(), |
| 1367 | Args, |
Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1368 | OldMsg->getRightLoc(), |
| 1369 | OldMsg->isImplicit()); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1370 | break; |
| 1371 | |
| 1372 | case ObjCMessageExpr::SuperClass: |
| 1373 | case ObjCMessageExpr::SuperInstance: |
| 1374 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1375 | OldMsg->getValueKind(), |
| 1376 | OldMsg->getLeftLoc(), |
| 1377 | OldMsg->getSuperLoc(), |
| 1378 | OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance, |
| 1379 | OldMsg->getSuperType(), |
| 1380 | OldMsg->getSelector(), |
| 1381 | SelLocs, |
| 1382 | OldMsg->getMethodDecl(), |
| 1383 | Args, |
Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1384 | OldMsg->getRightLoc(), |
| 1385 | OldMsg->isImplicit()); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1386 | break; |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1387 | } |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1388 | |
| 1389 | Stmt *Replacement = SynthMessageExpr(NewMsg); |
| 1390 | ReplaceStmtWithRange(PseudoOp, Replacement, OldRange); |
| 1391 | return Replacement; |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1394 | /// SynthCountByEnumWithState - To print: |
| 1395 | /// ((unsigned int (*) |
| 1396 | /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1397 | /// (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1398 | /// sel_registerName( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1399 | /// "countByEnumeratingWithState:objects:count:"), |
| 1400 | /// &enumState, |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1401 | /// (id *)__rw_items, (unsigned int)16) |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1402 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1403 | void RewriteObjC::SynthCountByEnumWithState(std::string &buf) { |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1404 | buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, " |
| 1405 | "id *, unsigned int))(void *)objc_msgSend)"; |
| 1406 | buf += "\n\t\t"; |
| 1407 | buf += "((id)l_collection,\n\t\t"; |
| 1408 | buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; |
| 1409 | buf += "\n\t\t"; |
| 1410 | buf += "&enumState, " |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1411 | "(id *)__rw_items, (unsigned int)16)"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1412 | } |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1413 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1414 | /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach |
| 1415 | /// statement to exit to its outer synthesized loop. |
| 1416 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1417 | Stmt *RewriteObjC::RewriteBreakStmt(BreakStmt *S) { |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1418 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1419 | return S; |
| 1420 | // replace break with goto __break_label |
| 1421 | std::string buf; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1422 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1423 | SourceLocation startLoc = S->getLocStart(); |
| 1424 | buf = "goto __break_label_"; |
| 1425 | buf += utostr(ObjCBcLabelNo.back()); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1426 | ReplaceText(startLoc, strlen("break"), buf); |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1427 | |
| 1428 | return 0; |
| 1429 | } |
| 1430 | |
| 1431 | /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach |
| 1432 | /// statement to continue with its inner synthesized loop. |
| 1433 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1434 | Stmt *RewriteObjC::RewriteContinueStmt(ContinueStmt *S) { |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1435 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1436 | return S; |
| 1437 | // replace continue with goto __continue_label |
| 1438 | std::string buf; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1439 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1440 | SourceLocation startLoc = S->getLocStart(); |
| 1441 | buf = "goto __continue_label_"; |
| 1442 | buf += utostr(ObjCBcLabelNo.back()); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1443 | ReplaceText(startLoc, strlen("continue"), buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1444 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1445 | return 0; |
| 1446 | } |
| 1447 | |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1448 | /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1449 | /// It rewrites: |
| 1450 | /// for ( type elem in collection) { stmts; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1451 | |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1452 | /// Into: |
| 1453 | /// { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1454 | /// type elem; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1455 | /// struct __objcFastEnumerationState enumState = { 0 }; |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1456 | /// id __rw_items[16]; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1457 | /// id l_collection = (id)collection; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1458 | /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1459 | /// objects:__rw_items count:16]; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1460 | /// if (limit) { |
| 1461 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1462 | /// do { |
| 1463 | /// unsigned long counter = 0; |
| 1464 | /// do { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1465 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1466 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1467 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1468 | /// stmts; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1469 | /// __continue_label: ; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1470 | /// } while (counter < limit); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1471 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1472 | /// objects:__rw_items count:16]); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1473 | /// elem = nil; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1474 | /// __break_label: ; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1475 | /// } |
| 1476 | /// else |
| 1477 | /// elem = nil; |
| 1478 | /// } |
| 1479 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1480 | Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
Chris Lattner | a779d69 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 1481 | SourceLocation OrigEnd) { |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1482 | assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1483 | assert(isa<ObjCForCollectionStmt>(Stmts.back()) && |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1484 | "ObjCForCollectionStmt Statement stack mismatch"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1485 | assert(!ObjCBcLabelNo.empty() && |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1486 | "ObjCForCollectionStmt - Label No stack empty"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1487 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1488 | SourceLocation startLoc = S->getLocStart(); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1489 | const char *startBuf = SM->getCharacterData(startLoc); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1490 | StringRef elementName; |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1491 | std::string elementTypeAsString; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1492 | std::string buf; |
| 1493 | buf = "\n{\n\t"; |
| 1494 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { |
| 1495 | // type elem; |
Chris Lattner | 529efc7 | 2009-03-28 06:33:19 +0000 | [diff] [blame] | 1496 | NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl()); |
Ted Kremenek | 292b384 | 2008-10-06 22:16:13 +0000 | [diff] [blame] | 1497 | QualType ElementType = cast<ValueDecl>(D)->getType(); |
Steve Naroff | 3ce3af2 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1498 | if (ElementType->isObjCQualifiedIdType() || |
| 1499 | ElementType->isObjCQualifiedInterfaceType()) |
| 1500 | // Simply use 'id' for all qualified types. |
| 1501 | elementTypeAsString = "id"; |
| 1502 | else |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1503 | elementTypeAsString = ElementType.getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1504 | buf += elementTypeAsString; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1505 | buf += " "; |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 1506 | elementName = D->getName(); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1507 | buf += elementName; |
| 1508 | buf += ";\n\t"; |
| 1509 | } |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1510 | else { |
| 1511 | DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 1512 | elementName = DR->getDecl()->getName(); |
Steve Naroff | 3ce3af2 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1513 | ValueDecl *VD = cast<ValueDecl>(DR->getDecl()); |
| 1514 | if (VD->getType()->isObjCQualifiedIdType() || |
| 1515 | VD->getType()->isObjCQualifiedInterfaceType()) |
| 1516 | // Simply use 'id' for all qualified types. |
| 1517 | elementTypeAsString = "id"; |
| 1518 | else |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1519 | elementTypeAsString = VD->getType().getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1520 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1521 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1522 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1523 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1524 | // id __rw_items[16]; |
| 1525 | buf += "id __rw_items[16];\n\t"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1526 | // id l_collection = (id) |
| 1527 | buf += "id l_collection = (id)"; |
Fariborz Jahanian | 82ae015 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1528 | // Find start location of 'collection' the hard way! |
| 1529 | const char *startCollectionBuf = startBuf; |
| 1530 | startCollectionBuf += 3; // skip 'for' |
| 1531 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1532 | startCollectionBuf++; // skip '(' |
| 1533 | // find 'in' and skip it. |
| 1534 | while (*startCollectionBuf != ' ' || |
| 1535 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1536 | (*(startCollectionBuf+3) != ' ' && |
| 1537 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1538 | startCollectionBuf++; |
| 1539 | startCollectionBuf += 3; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1540 | |
| 1541 | // Replace: "for (type element in" with string constructed thus far. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1542 | ReplaceText(startLoc, startCollectionBuf - startBuf, buf); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1543 | // Replace ')' in for '(' type elem in collection ')' with ';' |
Fariborz Jahanian | 82ae015 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1544 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1545 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1546 | SourceLocation lparenLoc = startLoc.getLocWithOffset(rparenBuf-startBuf); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1547 | buf = ";\n\t"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1548 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1549 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1550 | // objects:__rw_items count:16]; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1551 | // which is synthesized into: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1552 | // unsigned int limit = |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1553 | // ((unsigned int (*) |
| 1554 | // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1555 | // (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1556 | // sel_registerName( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1557 | // "countByEnumeratingWithState:objects:count:"), |
| 1558 | // (struct __objcFastEnumerationState *)&state, |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1559 | // (id *)__rw_items, (unsigned int)16); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1560 | buf += "unsigned long limit =\n\t\t"; |
| 1561 | SynthCountByEnumWithState(buf); |
| 1562 | buf += ";\n\t"; |
| 1563 | /// if (limit) { |
| 1564 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1565 | /// do { |
| 1566 | /// unsigned long counter = 0; |
| 1567 | /// do { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1568 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1569 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1570 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1571 | buf += "if (limit) {\n\t"; |
| 1572 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1573 | buf += "do {\n\t\t"; |
| 1574 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1575 | buf += "do {\n\t\t\t"; |
| 1576 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1577 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1578 | buf += elementName; |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1579 | buf += " = ("; |
| 1580 | buf += elementTypeAsString; |
| 1581 | buf += ")enumState.itemsPtr[counter++];"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1582 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1583 | ReplaceText(lparenLoc, 1, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1584 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1585 | /// __continue_label: ; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1586 | /// } while (counter < limit); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1587 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | e0e6505 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1588 | /// objects:__rw_items count:16]); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1589 | /// elem = nil; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1590 | /// __break_label: ; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1591 | /// } |
| 1592 | /// else |
| 1593 | /// elem = nil; |
| 1594 | /// } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1595 | /// |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1596 | buf = ";\n\t"; |
| 1597 | buf += "__continue_label_"; |
| 1598 | buf += utostr(ObjCBcLabelNo.back()); |
| 1599 | buf += ": ;"; |
| 1600 | buf += "\n\t\t"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1601 | buf += "} while (counter < limit);\n\t"; |
| 1602 | buf += "} while (limit = "; |
| 1603 | SynthCountByEnumWithState(buf); |
| 1604 | buf += ");\n\t"; |
| 1605 | buf += elementName; |
Fariborz Jahanian | 39d7094 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1606 | buf += " = (("; |
| 1607 | buf += elementTypeAsString; |
| 1608 | buf += ")0);\n\t"; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1609 | buf += "__break_label_"; |
| 1610 | buf += utostr(ObjCBcLabelNo.back()); |
| 1611 | buf += ": ;\n\t"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1612 | buf += "}\n\t"; |
| 1613 | buf += "else\n\t\t"; |
| 1614 | buf += elementName; |
Fariborz Jahanian | 39d7094 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1615 | buf += " = (("; |
| 1616 | buf += elementTypeAsString; |
| 1617 | buf += ")0);\n\t"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1618 | buf += "}\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1619 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1620 | // Insert all these *after* the statement body. |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1621 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Steve Naroff | f0ff879 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1622 | if (isa<CompoundStmt>(S->getBody())) { |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1623 | SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1624 | InsertText(endBodyLoc, buf); |
Steve Naroff | f0ff879 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1625 | } else { |
| 1626 | /* Need to treat single statements specially. For example: |
| 1627 | * |
| 1628 | * for (A *a in b) if (stuff()) break; |
| 1629 | * for (A *a in b) xxxyy; |
| 1630 | * |
| 1631 | * The following code simply scans ahead to the semi to find the actual end. |
| 1632 | */ |
| 1633 | const char *stmtBuf = SM->getCharacterData(OrigEnd); |
| 1634 | const char *semiBuf = strchr(stmtBuf, ';'); |
| 1635 | assert(semiBuf && "Can't find ';'"); |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1636 | SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(semiBuf-stmtBuf+1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1637 | InsertText(endBodyLoc, buf); |
Steve Naroff | f0ff879 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1638 | } |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1639 | Stmts.pop_back(); |
| 1640 | ObjCBcLabelNo.pop_back(); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1641 | return 0; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1642 | } |
| 1643 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1644 | /// RewriteObjCSynchronizedStmt - |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1645 | /// This routine rewrites @synchronized(expr) stmt; |
| 1646 | /// into: |
| 1647 | /// objc_sync_enter(expr); |
| 1648 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1649 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1650 | Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1651 | // Get the start location and compute the semi location. |
| 1652 | SourceLocation startLoc = S->getLocStart(); |
| 1653 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1654 | |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1655 | assert((*startBuf == '@') && "bogus @synchronized location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1656 | |
| 1657 | std::string buf; |
Steve Naroff | b2fc052 | 2008-08-21 13:03:03 +0000 | [diff] [blame] | 1658 | buf = "objc_sync_enter((id)"; |
| 1659 | const char *lparenBuf = startBuf; |
| 1660 | while (*lparenBuf != '(') lparenBuf++; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1661 | ReplaceText(startLoc, lparenBuf-startBuf+1, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1662 | // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since |
| 1663 | // the sync expression is typically a message expression that's already |
Steve Naroff | ad7013b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1664 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1665 | SourceLocation endLoc = S->getSynchBody()->getLocStart(); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1666 | const char *endBuf = SM->getCharacterData(endLoc); |
Steve Naroff | ad7013b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1667 | while (*endBuf != ')') endBuf--; |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1668 | SourceLocation rparenLoc = startLoc.getLocWithOffset(endBuf-startBuf); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1669 | buf = ");\n"; |
| 1670 | // declare a new scope with two variables, _stack and _rethrow. |
| 1671 | buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n"; |
| 1672 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1673 | buf += "char *pointers[4];} _stack;\n"; |
| 1674 | buf += "id volatile _rethrow = 0;\n"; |
| 1675 | buf += "objc_exception_try_enter(&_stack);\n"; |
| 1676 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1677 | ReplaceText(rparenLoc, 1, buf); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1678 | startLoc = S->getSynchBody()->getLocEnd(); |
| 1679 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1680 | |
Steve Naroff | ad7013b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1681 | assert((*startBuf == '}') && "bogus @synchronized block"); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1682 | SourceLocation lastCurlyLoc = startLoc; |
| 1683 | buf = "}\nelse {\n"; |
| 1684 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 1685 | buf += "}\n"; |
| 1686 | buf += "{ /* implicit finally clause */\n"; |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1687 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1688 | |
| 1689 | std::string syncBuf; |
| 1690 | syncBuf += " objc_sync_exit("; |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 1691 | |
| 1692 | Expr *syncExpr = S->getSynchExpr(); |
| 1693 | CastKind CK = syncExpr->getType()->isObjCObjectPointerType() |
| 1694 | ? CK_BitCast : |
| 1695 | syncExpr->getType()->isBlockPointerType() |
| 1696 | ? CK_BlockPointerToObjCPointerCast |
| 1697 | : CK_CPointerToObjCPointerCast; |
| 1698 | syncExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 1699 | CK, syncExpr); |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1700 | std::string syncExprBufS; |
| 1701 | llvm::raw_string_ostream syncExprBuf(syncExprBufS); |
Richard Smith | 235341b | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 1702 | syncExpr->printPretty(syncExprBuf, 0, PrintingPolicy(LangOpts)); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1703 | syncBuf += syncExprBuf.str(); |
| 1704 | syncBuf += ");"; |
| 1705 | |
| 1706 | buf += syncBuf; |
| 1707 | buf += "\n if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1708 | buf += "}\n"; |
| 1709 | buf += "}"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1710 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1711 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1712 | |
| 1713 | bool hasReturns = false; |
| 1714 | HasReturnStmts(S->getSynchBody(), hasReturns); |
| 1715 | if (hasReturns) |
| 1716 | RewriteSyncReturnStmts(S->getSynchBody(), syncBuf); |
| 1717 | |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1718 | return 0; |
| 1719 | } |
| 1720 | |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1721 | void RewriteObjC::WarnAboutReturnGotoStmts(Stmt *S) |
| 1722 | { |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1723 | // Perform a bottom up traversal of all children. |
John McCall | 8322c3a | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1724 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1725 | if (*CI) |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1726 | WarnAboutReturnGotoStmts(*CI); |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1727 | |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1728 | if (isa<ReturnStmt>(S) || isa<GotoStmt>(S)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1729 | Diags.Report(Context->getFullLoc(S->getLocStart()), |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1730 | TryFinallyContainsReturnDiag); |
| 1731 | } |
| 1732 | return; |
| 1733 | } |
| 1734 | |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1735 | void RewriteObjC::HasReturnStmts(Stmt *S, bool &hasReturns) |
| 1736 | { |
| 1737 | // Perform a bottom up traversal of all children. |
John McCall | 8322c3a | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1738 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1739 | if (*CI) |
| 1740 | HasReturnStmts(*CI, hasReturns); |
| 1741 | |
| 1742 | if (isa<ReturnStmt>(S)) |
| 1743 | hasReturns = true; |
| 1744 | return; |
| 1745 | } |
| 1746 | |
| 1747 | void RewriteObjC::RewriteTryReturnStmts(Stmt *S) { |
| 1748 | // Perform a bottom up traversal of all children. |
John McCall | 8322c3a | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1749 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1750 | if (*CI) { |
| 1751 | RewriteTryReturnStmts(*CI); |
| 1752 | } |
| 1753 | if (isa<ReturnStmt>(S)) { |
| 1754 | SourceLocation startLoc = S->getLocStart(); |
| 1755 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1756 | |
| 1757 | const char *semiBuf = strchr(startBuf, ';'); |
| 1758 | assert((*semiBuf == ';') && "RewriteTryReturnStmts: can't find ';'"); |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1759 | SourceLocation onePastSemiLoc = startLoc.getLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1760 | |
| 1761 | std::string buf; |
| 1762 | buf = "{ objc_exception_try_exit(&_stack); return"; |
| 1763 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1764 | ReplaceText(startLoc, 6, buf); |
| 1765 | InsertText(onePastSemiLoc, "}"); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1766 | } |
| 1767 | return; |
| 1768 | } |
| 1769 | |
| 1770 | void RewriteObjC::RewriteSyncReturnStmts(Stmt *S, std::string syncExitBuf) { |
| 1771 | // Perform a bottom up traversal of all children. |
John McCall | 8322c3a | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1772 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1773 | if (*CI) { |
| 1774 | RewriteSyncReturnStmts(*CI, syncExitBuf); |
| 1775 | } |
| 1776 | if (isa<ReturnStmt>(S)) { |
| 1777 | SourceLocation startLoc = S->getLocStart(); |
| 1778 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1779 | |
| 1780 | const char *semiBuf = strchr(startBuf, ';'); |
| 1781 | assert((*semiBuf == ';') && "RewriteSyncReturnStmts: can't find ';'"); |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1782 | SourceLocation onePastSemiLoc = startLoc.getLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1783 | |
| 1784 | std::string buf; |
| 1785 | buf = "{ objc_exception_try_exit(&_stack);"; |
| 1786 | buf += syncExitBuf; |
| 1787 | buf += " return"; |
| 1788 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1789 | ReplaceText(startLoc, 6, buf); |
| 1790 | InsertText(onePastSemiLoc, "}"); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1791 | } |
| 1792 | return; |
| 1793 | } |
| 1794 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1795 | Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1796 | // Get the start location and compute the semi location. |
| 1797 | SourceLocation startLoc = S->getLocStart(); |
| 1798 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1799 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1800 | assert((*startBuf == '@') && "bogus @try location"); |
| 1801 | |
| 1802 | std::string buf; |
| 1803 | // declare a new scope with two variables, _stack and _rethrow. |
| 1804 | buf = "/* @try scope begin */ { struct _objc_exception_data {\n"; |
| 1805 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1806 | buf += "char *pointers[4];} _stack;\n"; |
| 1807 | buf += "id volatile _rethrow = 0;\n"; |
| 1808 | buf += "objc_exception_try_enter(&_stack);\n"; |
Steve Naroff | 1601858 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1809 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1810 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1811 | ReplaceText(startLoc, 4, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1812 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1813 | startLoc = S->getTryBody()->getLocEnd(); |
| 1814 | startBuf = SM->getCharacterData(startLoc); |
| 1815 | |
| 1816 | assert((*startBuf == '}') && "bogus @try block"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1817 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1818 | SourceLocation lastCurlyLoc = startLoc; |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1819 | if (S->getNumCatchStmts()) { |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1820 | startLoc = startLoc.getLocWithOffset(1); |
Steve Naroff | ce2dca1 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1821 | buf = " /* @catch begin */ else {\n"; |
| 1822 | buf += " id _caught = objc_exception_extract(&_stack);\n"; |
| 1823 | buf += " objc_exception_try_enter (&_stack);\n"; |
| 1824 | buf += " if (_setjmp(_stack.buf))\n"; |
| 1825 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1826 | buf += " else { /* @catch continue */"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1827 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1828 | InsertText(startLoc, buf); |
Steve Naroff | fac18fe | 2008-09-09 19:59:12 +0000 | [diff] [blame] | 1829 | } else { /* no catch list */ |
| 1830 | buf = "}\nelse {\n"; |
| 1831 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1832 | buf += "}"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1833 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | ce2dca1 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1834 | } |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1835 | Stmt *lastCatchBody = 0; |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1836 | for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I) { |
| 1837 | ObjCAtCatchStmt *Catch = S->getCatchStmt(I); |
Douglas Gregor | 46a572b | 2010-04-26 16:46:50 +0000 | [diff] [blame] | 1838 | VarDecl *catchDecl = Catch->getCatchParamDecl(); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1839 | |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1840 | if (I == 0) |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1841 | buf = "if ("; // we are generating code for the first catch clause |
| 1842 | else |
| 1843 | buf = "else if ("; |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1844 | startLoc = Catch->getLocStart(); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1845 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1846 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1847 | assert((*startBuf == '@') && "bogus @catch location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1848 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1849 | const char *lParenLoc = strchr(startBuf, '('); |
| 1850 | |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1851 | if (Catch->hasEllipsis()) { |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1852 | // Now rewrite the body... |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1853 | lastCatchBody = Catch->getCatchBody(); |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1854 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1855 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1856 | assert(*SM->getCharacterData(Catch->getRParenLoc()) == ')' && |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1857 | "bogus @catch paren location"); |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1858 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1859 | |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1860 | buf += "1) { id _tmp = _caught;"; |
Daniel Dunbar | dec484a | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 1861 | Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, buf); |
Steve Naroff | 371b8fb | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1862 | } else if (catchDecl) { |
| 1863 | QualType t = catchDecl->getType(); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1864 | if (t == Context->getObjCIdType()) { |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1865 | buf += "1) { "; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1866 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf); |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 1867 | } else if (const ObjCObjectPointerType *Ptr = |
| 1868 | t->getAs<ObjCObjectPointerType>()) { |
| 1869 | // Should be a pointer to a class. |
| 1870 | ObjCInterfaceDecl *IDecl = Ptr->getObjectType()->getInterface(); |
| 1871 | if (IDecl) { |
Steve Naroff | 1601858 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1872 | buf += "objc_exception_match((struct objc_class *)objc_getClass(\""; |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 1873 | buf += IDecl->getNameAsString(); |
Steve Naroff | 1601858 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1874 | buf += "\"), (struct objc_object *)_caught)) { "; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1875 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1876 | } |
| 1877 | } |
| 1878 | // Now rewrite the body... |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1879 | lastCatchBody = Catch->getCatchBody(); |
| 1880 | SourceLocation rParenLoc = Catch->getRParenLoc(); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1881 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1882 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 1883 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 1884 | assert((*rParenBuf == ')') && "bogus @catch paren location"); |
| 1885 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1886 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1887 | // Here we replace ") {" with "= _caught;" (which initializes and |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1888 | // declares the @catch parameter). |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1889 | ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, " = _caught;"); |
Steve Naroff | 371b8fb | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1890 | } else { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1891 | llvm_unreachable("@catch rewrite bug"); |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1892 | } |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1893 | } |
| 1894 | // Complete the catch list... |
| 1895 | if (lastCatchBody) { |
| 1896 | SourceLocation bodyLoc = lastCatchBody->getLocEnd(); |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1897 | assert(*SM->getCharacterData(bodyLoc) == '}' && |
| 1898 | "bogus @catch body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1899 | |
Steve Naroff | 4adbe31 | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1900 | // Insert the last (implicit) else clause *before* the right curly brace. |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1901 | bodyLoc = bodyLoc.getLocWithOffset(-1); |
Steve Naroff | 4adbe31 | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1902 | buf = "} /* last catch end */\n"; |
| 1903 | buf += "else {\n"; |
| 1904 | buf += " _rethrow = _caught;\n"; |
| 1905 | buf += " objc_exception_try_exit(&_stack);\n"; |
| 1906 | buf += "} } /* @catch end */\n"; |
| 1907 | if (!S->getFinallyStmt()) |
| 1908 | buf += "}\n"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1909 | InsertText(bodyLoc, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1910 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1911 | // Set lastCurlyLoc |
| 1912 | lastCurlyLoc = lastCatchBody->getLocEnd(); |
| 1913 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1914 | if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) { |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1915 | startLoc = finalStmt->getLocStart(); |
| 1916 | startBuf = SM->getCharacterData(startLoc); |
| 1917 | assert((*startBuf == '@') && "bogus @finally start"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1918 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1919 | ReplaceText(startLoc, 8, "/* @finally */"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1920 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1921 | Stmt *body = finalStmt->getFinallyBody(); |
| 1922 | SourceLocation startLoc = body->getLocStart(); |
| 1923 | SourceLocation endLoc = body->getLocEnd(); |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1924 | assert(*SM->getCharacterData(startLoc) == '{' && |
| 1925 | "bogus @finally body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1926 | assert(*SM->getCharacterData(endLoc) == '}' && |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1927 | "bogus @finally body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1928 | |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1929 | startLoc = startLoc.getLocWithOffset(1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1930 | InsertText(startLoc, " if (!_rethrow) objc_exception_try_exit(&_stack);\n"); |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1931 | endLoc = endLoc.getLocWithOffset(-1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1932 | InsertText(endLoc, " if (_rethrow) objc_exception_throw(_rethrow);\n"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1933 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1934 | // Set lastCurlyLoc |
| 1935 | lastCurlyLoc = body->getLocEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1936 | |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1937 | // Now check for any return/continue/go statements within the @try. |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1938 | WarnAboutReturnGotoStmts(S->getTryBody()); |
Steve Naroff | 4adbe31 | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1939 | } else { /* no finally clause - make sure we synthesize an implicit one */ |
| 1940 | buf = "{ /* implicit finally clause */\n"; |
| 1941 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
| 1942 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 1943 | buf += "}"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1944 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1945 | |
| 1946 | // Now check for any return/continue/go statements within the @try. |
| 1947 | // The implicit finally clause won't called if the @try contains any |
| 1948 | // jump statements. |
| 1949 | bool hasReturns = false; |
| 1950 | HasReturnStmts(S->getTryBody(), hasReturns); |
| 1951 | if (hasReturns) |
| 1952 | RewriteTryReturnStmts(S->getTryBody()); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1953 | } |
| 1954 | // Now emit the final closing curly brace... |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1955 | lastCurlyLoc = lastCurlyLoc.getLocWithOffset(1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1956 | InsertText(lastCurlyLoc, " } /* @try scope end */\n"); |
Fariborz Jahanian | 63ac80e | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1957 | return 0; |
| 1958 | } |
| 1959 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1960 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
| 1961 | // the throw expression is typically a message expression that's already |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1962 | // been rewritten! (which implies the SourceLocation's are invalid). |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1963 | Stmt *RewriteObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1964 | // Get the start location and compute the semi location. |
| 1965 | SourceLocation startLoc = S->getLocStart(); |
| 1966 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1967 | |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1968 | assert((*startBuf == '@') && "bogus @throw location"); |
| 1969 | |
| 1970 | std::string buf; |
| 1971 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
Steve Naroff | c7d2df2 | 2008-01-19 00:42:38 +0000 | [diff] [blame] | 1972 | if (S->getThrowExpr()) |
| 1973 | buf = "objc_exception_throw("; |
| 1974 | else // add an implicit argument |
| 1975 | buf = "objc_exception_throw(_caught"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1976 | |
Steve Naroff | 2978834 | 2008-07-25 15:41:30 +0000 | [diff] [blame] | 1977 | // handle "@ throw" correctly. |
| 1978 | const char *wBuf = strchr(startBuf, 'w'); |
| 1979 | assert((*wBuf == 'w') && "@throw: can't find 'w'"); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1980 | ReplaceText(startLoc, wBuf-startBuf+1, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1981 | |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1982 | const char *semiBuf = strchr(startBuf, ';'); |
| 1983 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1984 | SourceLocation semiLoc = startLoc.getLocWithOffset(semiBuf-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1985 | ReplaceText(semiLoc, 1, ");"); |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1986 | return 0; |
| 1987 | } |
Fariborz Jahanian | 63ac80e | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1988 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1989 | Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
Chris Lattner | c6d91c0 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 1990 | // Create a new string expression. |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1991 | std::string StrEncoding; |
Daniel Dunbar | fc1066d | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1992 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); |
Benjamin Kramer | fc18842 | 2014-02-25 12:26:11 +0000 | [diff] [blame] | 1993 | Expr *Replacement = getStringLiteral(StrEncoding); |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1994 | ReplaceStmt(Exp, Replacement); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1995 | |
Chris Lattner | 4431a1b | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 1996 | // Replace this subexpr in the parent. |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1997 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Chris Lattner | 6953469 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 1998 | return Replacement; |
Chris Lattner | a7c19fe | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 1999 | } |
| 2000 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2001 | Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
Steve Naroff | 2654e18 | 2008-12-22 22:16:07 +0000 | [diff] [blame] | 2002 | if (!SelGetUidFunctionDecl) |
| 2003 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | e4f9b23 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2004 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 2005 | // Create a call to sel_registerName("selName"). |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2006 | SmallVector<Expr*, 8> SelExprs; |
Benjamin Kramer | fc18842 | 2014-02-25 12:26:11 +0000 | [diff] [blame] | 2007 | SelExprs.push_back(getStringLiteral(Exp->getSelector().getAsString())); |
Steve Naroff | e4f9b23 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2008 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2009 | &SelExprs[0], SelExprs.size()); |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2010 | ReplaceStmt(Exp, SelExp); |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2011 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | e4f9b23 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2012 | return SelExp; |
| 2013 | } |
| 2014 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2015 | CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl( |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2016 | FunctionDecl *FD, Expr **args, unsigned nargs, SourceLocation StartLoc, |
| 2017 | SourceLocation EndLoc) { |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2018 | // 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] | 2019 | QualType msgSendType = FD->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2020 | |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2021 | // Create a reference to the objc_msgSend() declaration. |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2022 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, false, msgSendType, |
| 2023 | VK_LValue, SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2024 | |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2025 | // 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] | 2026 | QualType pToFunc = Context->getPointerType(msgSendType); |
Anders Carlsson | 97597938 | 2010-04-23 22:18:37 +0000 | [diff] [blame] | 2027 | ImplicitCastExpr *ICE = |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2028 | ImplicitCastExpr::Create(*Context, pToFunc, CK_FunctionToPointerDecay, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2029 | DRE, 0, VK_RValue); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2030 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2031 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2032 | |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2033 | CallExpr *Exp = |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2034 | new (Context) CallExpr(*Context, ICE, llvm::makeArrayRef(args, nargs), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2035 | FT->getCallResultType(*Context), |
| 2036 | VK_RValue, EndLoc); |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2037 | return Exp; |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2038 | } |
| 2039 | |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2040 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 2041 | const char *&startRef, const char *&endRef) { |
| 2042 | while (startBuf < endBuf) { |
| 2043 | if (*startBuf == '<') |
| 2044 | startRef = startBuf; // mark the start. |
| 2045 | if (*startBuf == '>') { |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 2046 | if (startRef && *startRef == '<') { |
| 2047 | endRef = startBuf; // mark the end. |
| 2048 | return true; |
| 2049 | } |
| 2050 | return false; |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2051 | } |
| 2052 | startBuf++; |
| 2053 | } |
| 2054 | return false; |
| 2055 | } |
| 2056 | |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2057 | static void scanToNextArgument(const char *&argRef) { |
| 2058 | int angle = 0; |
| 2059 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 2060 | if (*argRef == '<') |
| 2061 | angle++; |
| 2062 | else if (*argRef == '>') |
| 2063 | angle--; |
| 2064 | argRef++; |
| 2065 | } |
| 2066 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 2067 | } |
Fariborz Jahanian | 16e703a | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2068 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2069 | bool RewriteObjC::needToScanForQualifiers(QualType T) { |
Fariborz Jahanian | 80c54b0 | 2010-02-03 21:29:28 +0000 | [diff] [blame] | 2070 | if (T->isObjCQualifiedIdType()) |
| 2071 | return true; |
Fariborz Jahanian | 06769f9 | 2010-02-02 18:35:07 +0000 | [diff] [blame] | 2072 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 2073 | if (PT->getPointeeType()->isObjCQualifiedIdType()) |
| 2074 | return true; |
| 2075 | } |
| 2076 | if (T->isObjCObjectPointerType()) { |
| 2077 | T = T->getPointeeType(); |
| 2078 | return T->isObjCQualifiedInterfaceType(); |
| 2079 | } |
Fariborz Jahanian | 7bf13c4 | 2010-09-30 20:41:32 +0000 | [diff] [blame] | 2080 | if (T->isArrayType()) { |
| 2081 | QualType ElemTy = Context->getBaseElementType(T); |
| 2082 | return needToScanForQualifiers(ElemTy); |
| 2083 | } |
Fariborz Jahanian | 06769f9 | 2010-02-02 18:35:07 +0000 | [diff] [blame] | 2084 | return false; |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2085 | } |
| 2086 | |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2087 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) { |
| 2088 | QualType Type = E->getType(); |
| 2089 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | dbfc693 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2090 | SourceLocation Loc, EndLoc; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2091 | |
Steve Naroff | dbfc693 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2092 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) { |
| 2093 | Loc = ECE->getLParenLoc(); |
| 2094 | EndLoc = ECE->getRParenLoc(); |
| 2095 | } else { |
| 2096 | Loc = E->getLocStart(); |
| 2097 | EndLoc = E->getLocEnd(); |
| 2098 | } |
| 2099 | // This will defend against trying to rewrite synthesized expressions. |
| 2100 | if (Loc.isInvalid() || EndLoc.isInvalid()) |
| 2101 | return; |
| 2102 | |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2103 | const char *startBuf = SM->getCharacterData(Loc); |
Steve Naroff | dbfc693 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2104 | const char *endBuf = SM->getCharacterData(EndLoc); |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2105 | const char *startRef = 0, *endRef = 0; |
| 2106 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2107 | // Get the locations of the startRef, endRef. |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2108 | SourceLocation LessLoc = Loc.getLocWithOffset(startRef-startBuf); |
| 2109 | SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-startBuf+1); |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2110 | // Comment out the protocol references. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2111 | InsertText(LessLoc, "/*"); |
| 2112 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2113 | } |
| 2114 | } |
| 2115 | } |
| 2116 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2117 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2118 | SourceLocation Loc; |
| 2119 | QualType Type; |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2120 | const FunctionProtoType *proto = 0; |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2121 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 2122 | Loc = VD->getLocation(); |
| 2123 | Type = VD->getType(); |
| 2124 | } |
| 2125 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 2126 | Loc = FD->getLocation(); |
| 2127 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 2128 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2129 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2130 | assert(funcType && "missing function type"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2131 | proto = dyn_cast<FunctionProtoType>(funcType); |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2132 | if (!proto) |
| 2133 | return; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2134 | Type = proto->getReturnType(); |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2135 | } |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 2136 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(Dcl)) { |
| 2137 | Loc = FD->getLocation(); |
| 2138 | Type = FD->getType(); |
| 2139 | } |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2140 | else |
| 2141 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2142 | |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2143 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2144 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2145 | |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2146 | const char *endBuf = SM->getCharacterData(Loc); |
| 2147 | const char *startBuf = endBuf; |
Steve Naroff | 930e099 | 2008-05-31 05:02:17 +0000 | [diff] [blame] | 2148 | while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2149 | startBuf--; // scan backward (from the decl location) for return type. |
| 2150 | const char *startRef = 0, *endRef = 0; |
| 2151 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2152 | // Get the locations of the startRef, endRef. |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2153 | SourceLocation LessLoc = Loc.getLocWithOffset(startRef-endBuf); |
| 2154 | SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-endBuf+1); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2155 | // Comment out the protocol references. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2156 | InsertText(LessLoc, "/*"); |
| 2157 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 37e011c | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2158 | } |
| 2159 | } |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2160 | if (!proto) |
| 2161 | return; // most likely, was a variable |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2162 | // Now check arguments. |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2163 | const char *startBuf = SM->getCharacterData(Loc); |
| 2164 | const char *startFuncBuf = startBuf; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2165 | for (unsigned i = 0; i < proto->getNumParams(); i++) { |
| 2166 | if (needToScanForQualifiers(proto->getParamType(i))) { |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2167 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2168 | |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2169 | const char *endBuf = startBuf; |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2170 | // scan forward (from the decl location) for argument types. |
| 2171 | scanToNextArgument(endBuf); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2172 | const char *startRef = 0, *endRef = 0; |
| 2173 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2174 | // Get the locations of the startRef, endRef. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2175 | SourceLocation LessLoc = |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2176 | Loc.getLocWithOffset(startRef-startFuncBuf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2177 | SourceLocation GreaterLoc = |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2178 | Loc.getLocWithOffset(endRef-startFuncBuf+1); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2179 | // Comment out the protocol references. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2180 | InsertText(LessLoc, "/*"); |
| 2181 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2182 | } |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2183 | startBuf = ++endBuf; |
| 2184 | } |
| 2185 | else { |
Steve Naroff | c884aa8 | 2008-08-06 15:58:23 +0000 | [diff] [blame] | 2186 | // If the function name is derived from a macro expansion, then the |
| 2187 | // argument buffer will not follow the name. Need to speak with Chris. |
| 2188 | while (*startBuf && *startBuf != ')' && *startBuf != ',') |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2189 | startBuf++; // scan forward (from the decl location) for argument types. |
| 2190 | startBuf++; |
| 2191 | } |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2192 | } |
Steve Naroff | 37e011c | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2193 | } |
| 2194 | |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2195 | void RewriteObjC::RewriteTypeOfDecl(VarDecl *ND) { |
| 2196 | QualType QT = ND->getType(); |
| 2197 | const Type* TypePtr = QT->getAs<Type>(); |
| 2198 | if (!isa<TypeOfExprType>(TypePtr)) |
| 2199 | return; |
| 2200 | while (isa<TypeOfExprType>(TypePtr)) { |
| 2201 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 2202 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 2203 | TypePtr = QT->getAs<Type>(); |
| 2204 | } |
| 2205 | // FIXME. This will not work for multiple declarators; as in: |
| 2206 | // __typeof__(a) b,c,d; |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2207 | std::string TypeAsString(QT.getAsString(Context->getPrintingPolicy())); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2208 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
| 2209 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 2210 | if (ND->getInit()) { |
| 2211 | std::string Name(ND->getNameAsString()); |
| 2212 | TypeAsString += " " + Name + " = "; |
| 2213 | Expr *E = ND->getInit(); |
| 2214 | SourceLocation startLoc; |
| 2215 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 2216 | startLoc = ECE->getLParenLoc(); |
| 2217 | else |
| 2218 | startLoc = E->getLocStart(); |
Chandler Carruth | 35f5320 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 2219 | startLoc = SM->getExpansionLoc(startLoc); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2220 | const char *endBuf = SM->getCharacterData(startLoc); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2221 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2222 | } |
| 2223 | else { |
| 2224 | SourceLocation X = ND->getLocEnd(); |
Chandler Carruth | 35f5320 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 2225 | X = SM->getExpansionLoc(X); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2226 | const char *endBuf = SM->getCharacterData(X); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2227 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2228 | } |
| 2229 | } |
| 2230 | |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2231 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2232 | void RewriteObjC::SynthSelGetUidFunctionDecl() { |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2233 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2234 | SmallVector<QualType, 16> ArgTys; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2235 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2236 | QualType getFuncType = |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2237 | getSimpleFunctionType(Context->getObjCSelType(), ArgTys); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2238 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2239 | SourceLocation(), |
| 2240 | SourceLocation(), |
| 2241 | SelGetUidIdent, getFuncType, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2242 | SC_Extern); |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2243 | } |
| 2244 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2245 | void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2246 | // declared in <objc/objc.h> |
Douglas Gregor | 1e21c19 | 2009-01-09 01:47:02 +0000 | [diff] [blame] | 2247 | if (FD->getIdentifier() && |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 2248 | FD->getName() == "sel_registerName") { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2249 | SelGetUidFunctionDecl = FD; |
Steve Naroff | 37e011c | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2250 | return; |
| 2251 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2252 | RewriteObjCQualifiedInterfaceTypes(FD); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
Daniel Dunbar | 8ab6c54 | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2255 | void RewriteObjC::RewriteBlockPointerType(std::string& Str, QualType Type) { |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2256 | std::string TypeString(Type.getAsString(Context->getPrintingPolicy())); |
Fariborz Jahanian | a459c44 | 2010-02-12 17:52:31 +0000 | [diff] [blame] | 2257 | const char *argPtr = TypeString.c_str(); |
| 2258 | if (!strchr(argPtr, '^')) { |
| 2259 | Str += TypeString; |
| 2260 | return; |
| 2261 | } |
| 2262 | while (*argPtr) { |
| 2263 | Str += (*argPtr == '^' ? '*' : *argPtr); |
| 2264 | argPtr++; |
| 2265 | } |
| 2266 | } |
| 2267 | |
Fariborz Jahanian | e1ff123 | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2268 | // FIXME. Consolidate this routine with RewriteBlockPointerType. |
Daniel Dunbar | 8ab6c54 | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2269 | void RewriteObjC::RewriteBlockPointerTypeVariable(std::string& Str, |
| 2270 | ValueDecl *VD) { |
Fariborz Jahanian | e1ff123 | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2271 | QualType Type = VD->getType(); |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2272 | std::string TypeString(Type.getAsString(Context->getPrintingPolicy())); |
Fariborz Jahanian | e1ff123 | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2273 | const char *argPtr = TypeString.c_str(); |
| 2274 | int paren = 0; |
| 2275 | while (*argPtr) { |
| 2276 | switch (*argPtr) { |
| 2277 | case '(': |
| 2278 | Str += *argPtr; |
| 2279 | paren++; |
| 2280 | break; |
| 2281 | case ')': |
| 2282 | Str += *argPtr; |
| 2283 | paren--; |
| 2284 | break; |
| 2285 | case '^': |
| 2286 | Str += '*'; |
| 2287 | if (paren == 1) |
| 2288 | Str += VD->getNameAsString(); |
| 2289 | break; |
| 2290 | default: |
| 2291 | Str += *argPtr; |
| 2292 | break; |
| 2293 | } |
| 2294 | argPtr++; |
| 2295 | } |
| 2296 | } |
| 2297 | |
| 2298 | |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2299 | void RewriteObjC::RewriteBlockLiteralFunctionDecl(FunctionDecl *FD) { |
| 2300 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
| 2301 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
| 2302 | const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(funcType); |
| 2303 | if (!proto) |
| 2304 | return; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2305 | QualType Type = proto->getReturnType(); |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2306 | std::string FdStr = Type.getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2307 | FdStr += " "; |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 2308 | FdStr += FD->getName(); |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2309 | FdStr += "("; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2310 | unsigned numArgs = proto->getNumParams(); |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2311 | for (unsigned i = 0; i < numArgs; i++) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2312 | QualType ArgType = proto->getParamType(i); |
Fariborz Jahanian | a459c44 | 2010-02-12 17:52:31 +0000 | [diff] [blame] | 2313 | RewriteBlockPointerType(FdStr, ArgType); |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2314 | if (i+1 < numArgs) |
| 2315 | FdStr += ", "; |
| 2316 | } |
| 2317 | FdStr += ");\n"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2318 | InsertText(FunLocStart, FdStr); |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2319 | CurFunctionDeclToDeclareForBlock = 0; |
| 2320 | } |
| 2321 | |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 2322 | // SynthSuperConstructorFunctionDecl - id objc_super(id obj, id super); |
| 2323 | void RewriteObjC::SynthSuperConstructorFunctionDecl() { |
| 2324 | if (SuperConstructorFunctionDecl) |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2325 | return; |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2326 | IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2327 | SmallVector<QualType, 16> ArgTys; |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2328 | QualType argT = Context->getObjCIdType(); |
| 2329 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2330 | ArgTys.push_back(argT); |
| 2331 | ArgTys.push_back(argT); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2332 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2333 | ArgTys); |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 2334 | SuperConstructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2335 | SourceLocation(), |
| 2336 | SourceLocation(), |
| 2337 | msgSendIdent, msgSendType, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2338 | 0, SC_Extern); |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2339 | } |
| 2340 | |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2341 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2342 | void RewriteObjC::SynthMsgSendFunctionDecl() { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2343 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2344 | SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2345 | QualType argT = Context->getObjCIdType(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2346 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2347 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2348 | argT = Context->getObjCSelType(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2349 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2350 | ArgTys.push_back(argT); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2351 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2352 | ArgTys, /*isVariadic=*/true); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2353 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2354 | SourceLocation(), |
| 2355 | SourceLocation(), |
| 2356 | msgSendIdent, msgSendType, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2357 | SC_Extern); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2358 | } |
| 2359 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2360 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2361 | void RewriteObjC::SynthMsgSendSuperFunctionDecl() { |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2362 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2363 | SmallVector<QualType, 16> ArgTys; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2364 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2365 | SourceLocation(), SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2366 | &Context->Idents.get("objc_super")); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2367 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2368 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2369 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2370 | argT = Context->getObjCSelType(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2371 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2372 | ArgTys.push_back(argT); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2373 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2374 | ArgTys, /*isVariadic=*/true); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2375 | MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2376 | SourceLocation(), |
| 2377 | SourceLocation(), |
| 2378 | msgSendIdent, msgSendType, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2379 | SC_Extern); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2380 | } |
| 2381 | |
Fariborz Jahanian | ff4d5e4 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2382 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2383 | void RewriteObjC::SynthMsgSendStretFunctionDecl() { |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2384 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2385 | SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2386 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2387 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2388 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2389 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2390 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2391 | ArgTys.push_back(argT); |
Fariborz Jahanian | ff4d5e4 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2392 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2393 | ArgTys, /*isVariadic=*/true); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2394 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2395 | SourceLocation(), |
| 2396 | SourceLocation(), |
| 2397 | msgSendIdent, msgSendType, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2398 | SC_Extern); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2399 | } |
| 2400 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2401 | // SynthMsgSendSuperStretFunctionDecl - |
Fariborz Jahanian | ff4d5e4 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2402 | // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2403 | void RewriteObjC::SynthMsgSendSuperStretFunctionDecl() { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2404 | IdentifierInfo *msgSendIdent = |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2405 | &Context->Idents.get("objc_msgSendSuper_stret"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2406 | SmallVector<QualType, 16> ArgTys; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2407 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2408 | SourceLocation(), SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2409 | &Context->Idents.get("objc_super")); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2410 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2411 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2412 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2413 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2414 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2415 | ArgTys.push_back(argT); |
Fariborz Jahanian | ff4d5e4 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2416 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2417 | ArgTys, /*isVariadic=*/true); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2418 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2419 | SourceLocation(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2420 | SourceLocation(), |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2421 | msgSendIdent, |
| 2422 | msgSendType, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2423 | SC_Extern); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2424 | } |
| 2425 | |
Steve Naroff | 2e4e385 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2426 | // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2427 | void RewriteObjC::SynthMsgSendFpretFunctionDecl() { |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2428 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2429 | SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2430 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2431 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2432 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2433 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2434 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2435 | ArgTys.push_back(argT); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2436 | QualType msgSendType = getSimpleFunctionType(Context->DoubleTy, |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2437 | ArgTys, /*isVariadic=*/true); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2438 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2439 | SourceLocation(), |
| 2440 | SourceLocation(), |
| 2441 | msgSendIdent, msgSendType, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2442 | SC_Extern); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2443 | } |
| 2444 | |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2445 | // SynthGetClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2446 | void RewriteObjC::SynthGetClassFunctionDecl() { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2447 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2448 | SmallVector<QualType, 16> ArgTys; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2449 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2450 | QualType getClassType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2451 | ArgTys); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2452 | GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2453 | SourceLocation(), |
| 2454 | SourceLocation(), |
| 2455 | getClassIdent, getClassType, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2456 | SC_Extern); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2457 | } |
| 2458 | |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2459 | // SynthGetSuperClassFunctionDecl - Class class_getSuperclass(Class cls); |
| 2460 | void RewriteObjC::SynthGetSuperClassFunctionDecl() { |
| 2461 | IdentifierInfo *getSuperClassIdent = |
| 2462 | &Context->Idents.get("class_getSuperclass"); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2463 | SmallVector<QualType, 16> ArgTys; |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2464 | ArgTys.push_back(Context->getObjCClassType()); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2465 | QualType getClassType = getSimpleFunctionType(Context->getObjCClassType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2466 | ArgTys); |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2467 | GetSuperClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2468 | SourceLocation(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2469 | SourceLocation(), |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2470 | getSuperClassIdent, |
| 2471 | getClassType, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2472 | SC_Extern); |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2473 | } |
| 2474 | |
Fariborz Jahanian | a4b2a86 | 2011-12-21 19:48:07 +0000 | [diff] [blame] | 2475 | // SynthGetMetaClassFunctionDecl - id objc_getMetaClass(const char *name); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2476 | void RewriteObjC::SynthGetMetaClassFunctionDecl() { |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2477 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
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 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2483 | SourceLocation(), |
| 2484 | SourceLocation(), |
| 2485 | getClassIdent, getClassType, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2486 | 0, SC_Extern); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2487 | } |
| 2488 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2489 | Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2490 | QualType strType = getConstantStringStructType(); |
| 2491 | |
| 2492 | std::string S = "__NSConstantStringImpl_"; |
Steve Naroff | a6141f0 | 2008-05-31 03:35:42 +0000 | [diff] [blame] | 2493 | |
| 2494 | std::string tmpName = InFileName; |
| 2495 | unsigned i; |
| 2496 | for (i=0; i < tmpName.length(); i++) { |
| 2497 | char c = tmpName.at(i); |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 2498 | // replace any non-alphanumeric characters with '_'. |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 2499 | if (!isAlphanumeric(c)) |
Steve Naroff | a6141f0 | 2008-05-31 03:35:42 +0000 | [diff] [blame] | 2500 | tmpName[i] = '_'; |
| 2501 | } |
| 2502 | S += tmpName; |
| 2503 | S += "_"; |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2504 | S += utostr(NumObjCStringLiterals++); |
| 2505 | |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2506 | Preamble += "static __NSConstantStringImpl " + S; |
| 2507 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 2508 | Preamble += "0x000007c8,"; // utf8_str |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2509 | // The pretty printer for StringLiteral handles escape characters properly. |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2510 | std::string prettyBufS; |
| 2511 | llvm::raw_string_ostream prettyBuf(prettyBufS); |
Richard Smith | 235341b | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 2512 | Exp->getString()->printPretty(prettyBuf, 0, PrintingPolicy(LangOpts)); |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2513 | Preamble += prettyBuf.str(); |
| 2514 | Preamble += ","; |
Steve Naroff | 94ed6dc | 2009-12-06 01:48:44 +0000 | [diff] [blame] | 2515 | Preamble += utostr(Exp->getString()->getByteLength()) + "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2516 | |
| 2517 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2518 | SourceLocation(), &Context->Idents.get(S), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2519 | strType, 0, SC_Static); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2520 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, false, strType, VK_LValue, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2521 | SourceLocation()); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2522 | Expr *Unop = new (Context) UnaryOperator(DRE, UO_AddrOf, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2523 | Context->getPointerType(DRE->getType()), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2524 | VK_RValue, OK_Ordinary, |
| 2525 | SourceLocation()); |
Steve Naroff | 265a6b9 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2526 | // cast to NSConstantString * |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2527 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Exp->getType(), |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2528 | CK_CPointerToObjCPointerCast, Unop); |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2529 | ReplaceStmt(Exp, cast); |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2530 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | 265a6b9 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2531 | return cast; |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2534 | // struct objc_super { struct objc_object *receiver; struct objc_class *super; }; |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2535 | QualType RewriteObjC::getSuperStructType() { |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2536 | if (!SuperStructDecl) { |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2537 | SuperStructDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2538 | SourceLocation(), SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2539 | &Context->Idents.get("objc_super")); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2540 | QualType FieldTypes[2]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2541 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2542 | // struct objc_object *receiver; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2543 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2544 | // struct objc_class *super; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2545 | FieldTypes[1] = Context->getObjCClassType(); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2546 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2547 | // Create fields |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2548 | for (unsigned i = 0; i < 2; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2549 | SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2550 | SourceLocation(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2551 | SourceLocation(), 0, |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2552 | FieldTypes[i], 0, |
| 2553 | /*BitWidth=*/0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2554 | /*Mutable=*/false, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2555 | ICIS_NoInit)); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2556 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2557 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2558 | SuperStructDecl->completeDefinition(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2559 | } |
| 2560 | return Context->getTagDeclType(SuperStructDecl); |
| 2561 | } |
| 2562 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2563 | QualType RewriteObjC::getConstantStringStructType() { |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2564 | if (!ConstantStringDecl) { |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2565 | ConstantStringDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2566 | SourceLocation(), SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2567 | &Context->Idents.get("__NSConstantStringImpl")); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2568 | QualType FieldTypes[4]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2569 | |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2570 | // struct objc_object *receiver; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2571 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2572 | // int flags; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2573 | FieldTypes[1] = Context->IntTy; |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2574 | // char *str; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2575 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2576 | // long length; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2577 | FieldTypes[3] = Context->LongTy; |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2578 | |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2579 | // Create fields |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2580 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2581 | ConstantStringDecl->addDecl(FieldDecl::Create(*Context, |
| 2582 | ConstantStringDecl, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2583 | SourceLocation(), |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2584 | SourceLocation(), 0, |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2585 | FieldTypes[i], 0, |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2586 | /*BitWidth=*/0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2587 | /*Mutable=*/true, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2588 | ICIS_NoInit)); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2589 | } |
| 2590 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2591 | ConstantStringDecl->completeDefinition(); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2592 | } |
| 2593 | return Context->getTagDeclType(ConstantStringDecl); |
| 2594 | } |
| 2595 | |
Fariborz Jahanian | d9c8aac | 2012-06-28 21:20:35 +0000 | [diff] [blame] | 2596 | CallExpr *RewriteObjC::SynthMsgSendStretCallExpr(FunctionDecl *MsgSendStretFlavor, |
| 2597 | QualType msgSendType, |
| 2598 | QualType returnType, |
| 2599 | SmallVectorImpl<QualType> &ArgTypes, |
| 2600 | SmallVectorImpl<Expr*> &MsgExprs, |
| 2601 | ObjCMethodDecl *Method) { |
| 2602 | // Create a reference to the objc_msgSend_stret() declaration. |
| 2603 | DeclRefExpr *STDRE = new (Context) DeclRefExpr(MsgSendStretFlavor, |
| 2604 | false, msgSendType, |
| 2605 | VK_LValue, SourceLocation()); |
| 2606 | // Need to cast objc_msgSend_stret to "void *" (see above comment). |
| 2607 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, |
| 2608 | Context->getPointerType(Context->VoidTy), |
| 2609 | CK_BitCast, STDRE); |
| 2610 | // Now do the "normal" pointer to function cast. |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2611 | QualType castType = getSimpleFunctionType(returnType, ArgTypes, |
| 2612 | Method ? Method->isVariadic() |
| 2613 | : false); |
Fariborz Jahanian | d9c8aac | 2012-06-28 21:20:35 +0000 | [diff] [blame] | 2614 | castType = Context->getPointerType(castType); |
| 2615 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
| 2616 | cast); |
| 2617 | |
| 2618 | // Don't forget the parens to enforce the proper binding. |
| 2619 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
| 2620 | |
| 2621 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2622 | CallExpr *STCE = new (Context) CallExpr( |
| 2623 | *Context, PE, MsgExprs, FT->getReturnType(), VK_RValue, SourceLocation()); |
Fariborz Jahanian | d9c8aac | 2012-06-28 21:20:35 +0000 | [diff] [blame] | 2624 | return STCE; |
| 2625 | |
| 2626 | } |
| 2627 | |
| 2628 | |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2629 | Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp, |
| 2630 | SourceLocation StartLoc, |
| 2631 | SourceLocation EndLoc) { |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2632 | if (!SelGetUidFunctionDecl) |
| 2633 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2634 | if (!MsgSendFunctionDecl) |
| 2635 | SynthMsgSendFunctionDecl(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2636 | if (!MsgSendSuperFunctionDecl) |
| 2637 | SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2638 | if (!MsgSendStretFunctionDecl) |
| 2639 | SynthMsgSendStretFunctionDecl(); |
| 2640 | if (!MsgSendSuperStretFunctionDecl) |
| 2641 | SynthMsgSendSuperStretFunctionDecl(); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2642 | if (!MsgSendFpretFunctionDecl) |
| 2643 | SynthMsgSendFpretFunctionDecl(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2644 | if (!GetClassFunctionDecl) |
| 2645 | SynthGetClassFunctionDecl(); |
Fariborz Jahanian | a4a925f | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2646 | if (!GetSuperClassFunctionDecl) |
| 2647 | SynthGetSuperClassFunctionDecl(); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2648 | if (!GetMetaClassFunctionDecl) |
| 2649 | SynthGetMetaClassFunctionDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2650 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2651 | // default to objc_msgSend(). |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2652 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2653 | // May need to use objc_msgSend_stret() as well. |
| 2654 | FunctionDecl *MsgSendStretFlavor = 0; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2655 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2656 | QualType resultType = mDecl->getReturnType(); |
Douglas Gregor | 8385a06 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 2657 | if (resultType->isRecordType()) |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2658 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
Chris Lattner | 3f6cd0b | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2659 | else if (resultType->isRealFloatingType()) |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2660 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2661 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2662 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2663 | // Synthesize a call to objc_msgSend(). |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2664 | SmallVector<Expr*, 8> MsgExprs; |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2665 | switch (Exp->getReceiverKind()) { |
| 2666 | case ObjCMessageExpr::SuperClass: { |
| 2667 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2668 | if (MsgSendStretFlavor) |
| 2669 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2670 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2671 | |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2672 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2673 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2674 | SmallVector<Expr*, 4> InitExprs; |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2675 | |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2676 | // set the receiver to self, the first argument to all methods. |
| 2677 | InitExprs.push_back( |
| 2678 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2679 | CK_BitCast, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2680 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2681 | false, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2682 | Context->getObjCIdType(), |
| 2683 | VK_RValue, |
| 2684 | SourceLocation())) |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2685 | ); // set the 'receiver'. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2686 | |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2687 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2688 | SmallVector<Expr*, 8> ClsExprs; |
Benjamin Kramer | fc18842 | 2014-02-25 12:26:11 +0000 | [diff] [blame] | 2689 | ClsExprs.push_back(getStringLiteral(ClassDecl->getIdentifier()->getName())); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2690 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
| 2691 | &ClsExprs[0], |
| 2692 | ClsExprs.size(), |
| 2693 | StartLoc, |
| 2694 | EndLoc); |
| 2695 | // (Class)objc_getClass("CurrentClass") |
| 2696 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 2697 | Context->getObjCClassType(), |
Fariborz Jahanian | a4b2a86 | 2011-12-21 19:48:07 +0000 | [diff] [blame] | 2698 | CK_BitCast, Cls); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2699 | ClsExprs.clear(); |
| 2700 | ClsExprs.push_back(ArgExpr); |
| 2701 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 2702 | &ClsExprs[0], ClsExprs.size(), |
| 2703 | StartLoc, EndLoc); |
| 2704 | |
| 2705 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2706 | // To turn off a warning, type-cast to 'id' |
| 2707 | InitExprs.push_back( // set 'super class', using class_getSuperclass(). |
| 2708 | NoTypeInfoCStyleCastExpr(Context, |
| 2709 | Context->getObjCIdType(), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2710 | CK_BitCast, Cls)); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2711 | // struct objc_super |
| 2712 | QualType superType = getSuperStructType(); |
| 2713 | Expr *SuperRep; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2714 | |
Francois Pichet | 0706d20 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 2715 | if (LangOpts.MicrosoftExt) { |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 2716 | SynthSuperConstructorFunctionDecl(); |
| 2717 | // Simulate a constructor call... |
| 2718 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperConstructorFunctionDecl, |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2719 | false, superType, VK_LValue, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2720 | SourceLocation()); |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2721 | SuperRep = new (Context) CallExpr(*Context, DRE, InitExprs, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2722 | superType, VK_LValue, |
| 2723 | SourceLocation()); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2724 | // The code for super is a little tricky to prevent collision with |
| 2725 | // the structure definition in the header. The rewriter has it's own |
| 2726 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2727 | // we need the cast below. For example: |
| 2728 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2729 | // |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2730 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2731 | Context->getPointerType(SuperRep->getType()), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2732 | VK_RValue, OK_Ordinary, |
| 2733 | SourceLocation()); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2734 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2735 | Context->getPointerType(superType), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2736 | CK_BitCast, SuperRep); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2737 | } else { |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2738 | // (struct objc_super) { <exprs from above> } |
| 2739 | InitListExpr *ILE = |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2740 | new (Context) InitListExpr(*Context, SourceLocation(), InitExprs, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2741 | SourceLocation()); |
| 2742 | TypeSourceInfo *superTInfo |
| 2743 | = Context->getTrivialTypeSourceInfo(superType); |
| 2744 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2745 | superType, VK_LValue, |
| 2746 | ILE, false); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2747 | // struct objc_super * |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2748 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2749 | Context->getPointerType(SuperRep->getType()), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2750 | VK_RValue, OK_Ordinary, |
| 2751 | SourceLocation()); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2752 | } |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2753 | MsgExprs.push_back(SuperRep); |
| 2754 | break; |
Steve Naroff | e7f1819 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2755 | } |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2756 | |
| 2757 | case ObjCMessageExpr::Class: { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2758 | SmallVector<Expr*, 8> ClsExprs; |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2759 | ObjCInterfaceDecl *Class |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 2760 | = Exp->getClassReceiver()->getAs<ObjCObjectType>()->getInterface(); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2761 | IdentifierInfo *clsName = Class->getIdentifier(); |
Benjamin Kramer | fc18842 | 2014-02-25 12:26:11 +0000 | [diff] [blame] | 2762 | ClsExprs.push_back(getStringLiteral(clsName->getName())); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2763 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2764 | &ClsExprs[0], |
| 2765 | ClsExprs.size(), |
| 2766 | StartLoc, EndLoc); |
| 2767 | MsgExprs.push_back(Cls); |
| 2768 | break; |
| 2769 | } |
| 2770 | |
| 2771 | case ObjCMessageExpr::SuperInstance:{ |
| 2772 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2773 | if (MsgSendStretFlavor) |
| 2774 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2775 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 2776 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2777 | SmallVector<Expr*, 4> InitExprs; |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2778 | |
| 2779 | InitExprs.push_back( |
| 2780 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2781 | CK_BitCast, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2782 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2783 | false, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2784 | Context->getObjCIdType(), |
| 2785 | VK_RValue, SourceLocation())) |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2786 | ); // set the 'receiver'. |
| 2787 | |
| 2788 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2789 | SmallVector<Expr*, 8> ClsExprs; |
Benjamin Kramer | fc18842 | 2014-02-25 12:26:11 +0000 | [diff] [blame] | 2790 | ClsExprs.push_back(getStringLiteral(ClassDecl->getIdentifier()->getName())); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2791 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2792 | &ClsExprs[0], |
| 2793 | ClsExprs.size(), |
| 2794 | StartLoc, EndLoc); |
| 2795 | // (Class)objc_getClass("CurrentClass") |
| 2796 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 2797 | Context->getObjCClassType(), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2798 | CK_BitCast, Cls); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2799 | ClsExprs.clear(); |
| 2800 | ClsExprs.push_back(ArgExpr); |
| 2801 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 2802 | &ClsExprs[0], ClsExprs.size(), |
| 2803 | StartLoc, EndLoc); |
| 2804 | |
| 2805 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2806 | // To turn off a warning, type-cast to 'id' |
| 2807 | InitExprs.push_back( |
| 2808 | // set 'super class', using class_getSuperclass(). |
| 2809 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2810 | CK_BitCast, Cls)); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2811 | // struct objc_super |
| 2812 | QualType superType = getSuperStructType(); |
| 2813 | Expr *SuperRep; |
| 2814 | |
Francois Pichet | 0706d20 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 2815 | if (LangOpts.MicrosoftExt) { |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 2816 | SynthSuperConstructorFunctionDecl(); |
| 2817 | // Simulate a constructor call... |
| 2818 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperConstructorFunctionDecl, |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2819 | false, superType, VK_LValue, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2820 | SourceLocation()); |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2821 | SuperRep = new (Context) CallExpr(*Context, DRE, InitExprs, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2822 | superType, VK_LValue, SourceLocation()); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2823 | // The code for super is a little tricky to prevent collision with |
| 2824 | // the structure definition in the header. The rewriter has it's own |
| 2825 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2826 | // we need the cast below. For example: |
| 2827 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2828 | // |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2829 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2830 | Context->getPointerType(SuperRep->getType()), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2831 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2832 | SourceLocation()); |
| 2833 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2834 | Context->getPointerType(superType), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2835 | CK_BitCast, SuperRep); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2836 | } else { |
| 2837 | // (struct objc_super) { <exprs from above> } |
| 2838 | InitListExpr *ILE = |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2839 | new (Context) InitListExpr(*Context, SourceLocation(), InitExprs, |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2840 | SourceLocation()); |
| 2841 | TypeSourceInfo *superTInfo |
| 2842 | = Context->getTrivialTypeSourceInfo(superType); |
| 2843 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2844 | superType, VK_RValue, ILE, |
| 2845 | false); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2846 | } |
| 2847 | MsgExprs.push_back(SuperRep); |
| 2848 | break; |
| 2849 | } |
| 2850 | |
| 2851 | case ObjCMessageExpr::Instance: { |
| 2852 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 2853 | // Foo<Proto> *. |
| 2854 | Expr *recExpr = Exp->getInstanceReceiver(); |
| 2855 | while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr)) |
| 2856 | recExpr = CE->getSubExpr(); |
Fariborz Jahanian | 942bbce | 2011-10-07 17:17:45 +0000 | [diff] [blame] | 2857 | CastKind CK = recExpr->getType()->isObjCObjectPointerType() |
| 2858 | ? CK_BitCast : recExpr->getType()->isBlockPointerType() |
| 2859 | ? CK_BlockPointerToObjCPointerCast |
| 2860 | : CK_CPointerToObjCPointerCast; |
| 2861 | |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2862 | recExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
Fariborz Jahanian | 942bbce | 2011-10-07 17:17:45 +0000 | [diff] [blame] | 2863 | CK, recExpr); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2864 | MsgExprs.push_back(recExpr); |
| 2865 | break; |
| 2866 | } |
| 2867 | } |
| 2868 | |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2869 | // 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] | 2870 | SmallVector<Expr*, 8> SelExprs; |
Benjamin Kramer | fc18842 | 2014-02-25 12:26:11 +0000 | [diff] [blame] | 2871 | SelExprs.push_back(getStringLiteral(Exp->getSelector().getAsString())); |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2872 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2873 | &SelExprs[0], SelExprs.size(), |
| 2874 | StartLoc, |
| 2875 | EndLoc); |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2876 | MsgExprs.push_back(SelExp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2877 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2878 | // Now push any user supplied arguments. |
| 2879 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
Steve Naroff | e7f1819 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2880 | Expr *userExpr = Exp->getArg(i); |
Steve Naroff | f60782b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2881 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 2882 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 2883 | // Reuse the ICE type, it is exactly what the doctor ordered. |
Fariborz Jahanian | 8f49033 | 2011-02-26 01:31:36 +0000 | [diff] [blame] | 2884 | QualType type = ICE->getType(); |
| 2885 | if (needToScanForQualifiers(type)) |
| 2886 | type = Context->getObjCIdType(); |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 2887 | // Make sure we convert "type (^)(...)" to "type (*)(...)". |
Fariborz Jahanian | 31b8a9d | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 2888 | (void)convertBlockPointerToFunctionPointer(type); |
Fariborz Jahanian | 9e7dbd1 | 2011-08-04 23:58:03 +0000 | [diff] [blame] | 2889 | const Expr *SubExpr = ICE->IgnoreParenImpCasts(); |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2890 | CastKind CK; |
| 2891 | if (SubExpr->getType()->isIntegralType(*Context) && |
| 2892 | type->isBooleanType()) { |
| 2893 | CK = CK_IntegralToBoolean; |
| 2894 | } else if (type->isObjCObjectPointerType()) { |
| 2895 | if (SubExpr->getType()->isBlockPointerType()) { |
| 2896 | CK = CK_BlockPointerToObjCPointerCast; |
| 2897 | } else if (SubExpr->getType()->isPointerType()) { |
| 2898 | CK = CK_CPointerToObjCPointerCast; |
| 2899 | } else { |
| 2900 | CK = CK_BitCast; |
| 2901 | } |
| 2902 | } else { |
| 2903 | CK = CK_BitCast; |
| 2904 | } |
| 2905 | |
| 2906 | userExpr = NoTypeInfoCStyleCastExpr(Context, type, CK, userExpr); |
Fariborz Jahanian | 9f0e310 | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2907 | } |
| 2908 | // Make id<P...> cast into an 'id' cast. |
Douglas Gregor | f19b231 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2909 | else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2910 | if (CE->getType()->isObjCQualifiedIdType()) { |
Douglas Gregor | f19b231 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2911 | while ((CE = dyn_cast<CStyleCastExpr>(userExpr))) |
Fariborz Jahanian | 9f0e310 | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2912 | userExpr = CE->getSubExpr(); |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2913 | CastKind CK; |
| 2914 | if (userExpr->getType()->isIntegralType(*Context)) { |
| 2915 | CK = CK_IntegralToPointer; |
| 2916 | } else if (userExpr->getType()->isBlockPointerType()) { |
| 2917 | CK = CK_BlockPointerToObjCPointerCast; |
| 2918 | } else if (userExpr->getType()->isPointerType()) { |
| 2919 | CK = CK_CPointerToObjCPointerCast; |
| 2920 | } else { |
| 2921 | CK = CK_BitCast; |
| 2922 | } |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2923 | userExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2924 | CK, userExpr); |
Fariborz Jahanian | 9f0e310 | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2925 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2926 | } |
Steve Naroff | e7f1819 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2927 | MsgExprs.push_back(userExpr); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2928 | // We've transferred the ownership to MsgExprs. For now, we *don't* null |
| 2929 | // out the argument in the original expression (since we aren't deleting |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2930 | // the ObjCMessageExpr). See RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2931 | //Exp->setArg(i, 0); |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2932 | } |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2933 | // Generate the funky cast. |
| 2934 | CastExpr *cast; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2935 | SmallVector<QualType, 8> ArgTypes; |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2936 | QualType returnType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2937 | |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2938 | // Push 'id' and 'SEL', the 2 implicit arguments. |
Steve Naroff | 44864e4 | 2007-11-15 10:43:57 +0000 | [diff] [blame] | 2939 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 2940 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 2941 | else |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2942 | ArgTypes.push_back(Context->getObjCIdType()); |
| 2943 | ArgTypes.push_back(Context->getObjCSelType()); |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2944 | if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) { |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2945 | // Push any user argument types. |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 2946 | for (const auto *PI : OMD->params()) { |
| 2947 | QualType t = PI->getType()->isObjCQualifiedIdType() |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2948 | ? Context->getObjCIdType() |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 2949 | : PI->getType(); |
Steve Naroff | 52c65fa | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 2950 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 31b8a9d | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 2951 | (void)convertBlockPointerToFunctionPointer(t); |
Steve Naroff | 98eb8d1 | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 2952 | ArgTypes.push_back(t); |
| 2953 | } |
Fariborz Jahanian | 9f0bc57 | 2011-09-10 17:01:56 +0000 | [diff] [blame] | 2954 | returnType = Exp->getType(); |
| 2955 | convertToUnqualifiedObjCType(returnType); |
Fariborz Jahanian | 31b8a9d | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 2956 | (void)convertBlockPointerToFunctionPointer(returnType); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2957 | } else { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2958 | returnType = Context->getObjCIdType(); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2959 | } |
| 2960 | // 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] | 2961 | QualType msgSendType = MsgSendFlavor->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2962 | |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2963 | // Create a reference to the objc_msgSend() declaration. |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2964 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, false, msgSendType, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2965 | VK_LValue, SourceLocation()); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2966 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2967 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2968 | // If we don't do this cast, we get the following bizarre warning/note: |
| 2969 | // xx.m:13: warning: function called through a non-compatible type |
| 2970 | // 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] | 2971 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 2972 | Context->getPointerType(Context->VoidTy), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2973 | CK_BitCast, DRE); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2974 | |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2975 | // Now do the "normal" pointer to function cast. |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2976 | // If we don't have a method decl, force a variadic cast. |
| 2977 | const ObjCMethodDecl *MD = Exp->getMethodDecl(); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2978 | QualType castType = |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2979 | getSimpleFunctionType(returnType, ArgTypes, MD ? MD->isVariadic() : true); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2980 | castType = Context->getPointerType(castType); |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2981 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2982 | cast); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2983 | |
| 2984 | // Don't forget the parens to enforce the proper binding. |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2985 | ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2986 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2987 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2988 | CallExpr *CE = new (Context) |
| 2989 | CallExpr(*Context, PE, MsgExprs, FT->getReturnType(), VK_RValue, EndLoc); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2990 | Stmt *ReplacingStmt = CE; |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2991 | if (MsgSendStretFlavor) { |
| 2992 | // We have the method which returns a struct/union. Must also generate |
| 2993 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 2994 | // expression which dictate which one to envoke depending on size of |
| 2995 | // method's return type. |
Fariborz Jahanian | d9c8aac | 2012-06-28 21:20:35 +0000 | [diff] [blame] | 2996 | |
| 2997 | CallExpr *STCE = SynthMsgSendStretCallExpr(MsgSendStretFlavor, |
| 2998 | msgSendType, returnType, |
| 2999 | ArgTypes, MsgExprs, |
| 3000 | Exp->getMethodDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3001 | |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3002 | // Build sizeof(returnType) |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3003 | UnaryExprOrTypeTraitExpr *sizeofExpr = |
| 3004 | new (Context) UnaryExprOrTypeTraitExpr(UETT_SizeOf, |
| 3005 | Context->getTrivialTypeSourceInfo(returnType), |
| 3006 | Context->getSizeType(), SourceLocation(), |
| 3007 | SourceLocation()); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3008 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 3009 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 3010 | // For X86 it is more complicated and some kind of target specific routine |
| 3011 | // is needed to decide what to do. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3012 | unsigned IntSize = |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3013 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 3014 | IntegerLiteral *limit = IntegerLiteral::Create(*Context, |
| 3015 | llvm::APInt(IntSize, 8), |
| 3016 | Context->IntTy, |
| 3017 | SourceLocation()); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3018 | BinaryOperator *lessThanExpr = |
| 3019 | new (Context) BinaryOperator(sizeofExpr, limit, BO_LE, Context->IntTy, |
Lang Hames | 5de91cc | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 3020 | VK_RValue, OK_Ordinary, SourceLocation(), |
| 3021 | false); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3022 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3023 | ConditionalOperator *CondExpr = |
Douglas Gregor | 7e112b0 | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 3024 | new (Context) ConditionalOperator(lessThanExpr, |
| 3025 | SourceLocation(), CE, |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3026 | SourceLocation(), STCE, |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3027 | returnType, VK_RValue, OK_Ordinary); |
Fariborz Jahanian | c6bf0bd | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 3028 | ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3029 | CondExpr); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3030 | } |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3031 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3032 | return ReplacingStmt; |
| 3033 | } |
| 3034 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3035 | Stmt *RewriteObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 3036 | Stmt *ReplacingStmt = SynthMessageExpr(Exp, Exp->getLocStart(), |
| 3037 | Exp->getLocEnd()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3038 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 3039 | // Now do the actual rewrite. |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 3040 | ReplaceStmt(Exp, ReplacingStmt); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3041 | |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3042 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3043 | return ReplacingStmt; |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 3044 | } |
| 3045 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3046 | // typedef struct objc_object Protocol; |
| 3047 | QualType RewriteObjC::getProtocolType() { |
| 3048 | if (!ProtocolTypeDecl) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3049 | TypeSourceInfo *TInfo |
| 3050 | = Context->getTrivialTypeSourceInfo(Context->getObjCIdType()); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3051 | ProtocolTypeDecl = TypedefDecl::Create(*Context, TUDecl, |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 3052 | SourceLocation(), SourceLocation(), |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3053 | &Context->Idents.get("Protocol"), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3054 | TInfo); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3055 | } |
| 3056 | return Context->getTypeDeclType(ProtocolTypeDecl); |
| 3057 | } |
| 3058 | |
Fariborz Jahanian | 33c0e81 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 3059 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3060 | /// a synthesized/forward data reference (to the protocol's metadata). |
| 3061 | /// The forward references (and metadata) are generated in |
| 3062 | /// RewriteObjC::HandleTranslationUnit(). |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3063 | Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3064 | std::string Name = "_OBJC_PROTOCOL_" + Exp->getProtocol()->getNameAsString(); |
| 3065 | IdentifierInfo *ID = &Context->Idents.get(Name); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3066 | VarDecl *VD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3067 | SourceLocation(), ID, getProtocolType(), 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3068 | SC_Extern); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3069 | DeclRefExpr *DRE = new (Context) DeclRefExpr(VD, false, getProtocolType(), |
| 3070 | VK_LValue, SourceLocation()); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3071 | Expr *DerefExpr = new (Context) UnaryOperator(DRE, UO_AddrOf, |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3072 | Context->getPointerType(DRE->getType()), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3073 | VK_RValue, OK_Ordinary, SourceLocation()); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3074 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, DerefExpr->getType(), |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3075 | CK_BitCast, |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3076 | DerefExpr); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3077 | ReplaceStmt(Exp, castExpr); |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 3078 | ProtocolExprDecls.insert(Exp->getProtocol()->getCanonicalDecl()); |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3079 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3080 | return castExpr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3081 | |
Fariborz Jahanian | 33c0e81 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 3082 | } |
| 3083 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3084 | bool RewriteObjC::BufferContainsPPDirectives(const char *startBuf, |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3085 | const char *endBuf) { |
| 3086 | while (startBuf < endBuf) { |
| 3087 | if (*startBuf == '#') { |
| 3088 | // Skip whitespace. |
| 3089 | for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf) |
| 3090 | ; |
| 3091 | if (!strncmp(startBuf, "if", strlen("if")) || |
| 3092 | !strncmp(startBuf, "ifdef", strlen("ifdef")) || |
| 3093 | !strncmp(startBuf, "ifndef", strlen("ifndef")) || |
| 3094 | !strncmp(startBuf, "define", strlen("define")) || |
| 3095 | !strncmp(startBuf, "undef", strlen("undef")) || |
| 3096 | !strncmp(startBuf, "else", strlen("else")) || |
| 3097 | !strncmp(startBuf, "elif", strlen("elif")) || |
| 3098 | !strncmp(startBuf, "endif", strlen("endif")) || |
| 3099 | !strncmp(startBuf, "pragma", strlen("pragma")) || |
| 3100 | !strncmp(startBuf, "include", strlen("include")) || |
| 3101 | !strncmp(startBuf, "import", strlen("import")) || |
| 3102 | !strncmp(startBuf, "include_next", strlen("include_next"))) |
| 3103 | return true; |
| 3104 | } |
| 3105 | startBuf++; |
| 3106 | } |
| 3107 | return false; |
| 3108 | } |
| 3109 | |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 3110 | /// RewriteObjCInternalStruct - Rewrite one internal struct corresponding to |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3111 | /// an objective-c class with ivars. |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 3112 | void RewriteObjC::RewriteObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3113 | std::string &Result) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3114 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3115 | assert(CDecl->getName() != "" && |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 3116 | "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | c3cda76 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 3117 | // Do not synthesize more than once. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3118 | if (ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | c3cda76 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 3119 | return; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3120 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Chris Lattner | 8d1c04f | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3121 | int NumIvars = CDecl->ivar_size(); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3122 | SourceLocation LocStart = CDecl->getLocStart(); |
Douglas Gregor | 1640832 | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 3123 | SourceLocation LocEnd = CDecl->getEndOfDefinitionLoc(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3124 | |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3125 | const char *startBuf = SM->getCharacterData(LocStart); |
| 3126 | const char *endBuf = SM->getCharacterData(LocEnd); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3127 | |
Fariborz Jahanian | a883d6e | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3128 | // If no ivars and no root or if its root, directly or indirectly, |
| 3129 | // 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] | 3130 | if ((!CDecl->isThisDeclarationADefinition() || NumIvars == 0) && |
Chris Lattner | 8d1c04f | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3131 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3132 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3133 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | a883d6e | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3134 | return; |
| 3135 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3136 | |
| 3137 | // FIXME: This has potential of causing problem. If |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3138 | // SynthesizeObjCInternalStruct is ever called recursively. |
Fariborz Jahanian | a883d6e | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3139 | Result += "\nstruct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3140 | Result += CDecl->getNameAsString(); |
Francois Pichet | 0706d20 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 3141 | if (LangOpts.MicrosoftExt) |
Steve Naroff | a1e115e | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 3142 | Result += "_IMPL"; |
Steve Naroff | dc5b6b2 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 3143 | |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3144 | if (NumIvars > 0) { |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3145 | const char *cursor = strchr(startBuf, '{'); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3146 | assert((cursor && endBuf) |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3147 | && "SynthesizeObjCInternalStruct - malformed @interface"); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3148 | // If the buffer contains preprocessor directives, we do more fine-grained |
| 3149 | // rewrites. This is intended to fix code that looks like (which occurs in |
| 3150 | // NSURL.h, for example): |
| 3151 | // |
| 3152 | // #ifdef XYZ |
| 3153 | // @interface Foo : NSObject |
| 3154 | // #else |
| 3155 | // @interface FooBar : NSObject |
| 3156 | // #endif |
| 3157 | // { |
| 3158 | // int i; |
| 3159 | // } |
| 3160 | // @end |
| 3161 | // |
| 3162 | // This clause is segregated to avoid breaking the common case. |
| 3163 | if (BufferContainsPPDirectives(startBuf, cursor)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3164 | SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() : |
Argyrios Kyrtzidis | 52f53fb | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 3165 | CDecl->getAtStartLoc(); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3166 | const char *endHeader = SM->getCharacterData(L); |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3167 | endHeader += Lexer::MeasureTokenLength(L, *SM, LangOpts); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3168 | |
Chris Lattner | f5b7751 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3169 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3170 | // advance to the end of the referenced protocols. |
| 3171 | while (endHeader < cursor && *endHeader != '>') endHeader++; |
| 3172 | endHeader++; |
| 3173 | } |
| 3174 | // rewrite the original header |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3175 | ReplaceText(LocStart, endHeader-startBuf, Result); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3176 | } else { |
| 3177 | // rewrite the original header *without* disturbing the '{' |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3178 | ReplaceText(LocStart, cursor-startBuf, Result); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3179 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3180 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3181 | Result = "\n struct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3182 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 9f33bd2 | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 3183 | Result += "_IMPL "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3184 | Result += RCDecl->getNameAsString(); |
Steve Naroff | ffb5f9a | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 3185 | Result += "_IVARS;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3186 | |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3187 | // insert the super class structure definition. |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 3188 | SourceLocation OnePastCurly = |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3189 | LocStart.getLocWithOffset(cursor-startBuf+1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3190 | InsertText(OnePastCurly, Result); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3191 | } |
| 3192 | cursor++; // past '{' |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3193 | |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3194 | // Now comment out any visibility specifiers. |
| 3195 | while (cursor < endBuf) { |
| 3196 | if (*cursor == '@') { |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3197 | SourceLocation atLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Chris Lattner | 174a825 | 2007-11-14 22:57:51 +0000 | [diff] [blame] | 3198 | // Skip whitespace. |
| 3199 | for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor) |
| 3200 | /*scan*/; |
| 3201 | |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3202 | // FIXME: presence of @public, etc. inside comment results in |
| 3203 | // this transformation as well, which is still correct c-code. |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3204 | if (!strncmp(cursor, "public", strlen("public")) || |
| 3205 | !strncmp(cursor, "private", strlen("private")) || |
Steve Naroff | af91b9a | 2008-04-04 22:34:24 +0000 | [diff] [blame] | 3206 | !strncmp(cursor, "package", strlen("package")) || |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3207 | !strncmp(cursor, "protected", strlen("protected"))) |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3208 | InsertText(atLoc, "// "); |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3209 | } |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3210 | // FIXME: If there are cases where '<' is used in ivar declaration part |
| 3211 | // of user code, then scan the ivar list and use needToScanForQualifiers |
| 3212 | // for type checking. |
| 3213 | else if (*cursor == '<') { |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3214 | SourceLocation atLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3215 | InsertText(atLoc, "/* "); |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3216 | cursor = strchr(cursor, '>'); |
| 3217 | cursor++; |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3218 | atLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3219 | InsertText(atLoc, " */"); |
Steve Naroff | 295570a | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3220 | } else if (*cursor == '^') { // rewrite block specifier. |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3221 | SourceLocation caretLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3222 | ReplaceText(caretLoc, 1, "*"); |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3223 | } |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3224 | cursor++; |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3225 | } |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3226 | // Don't forget to add a ';'!! |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3227 | InsertText(LocEnd.getLocWithOffset(1), ";"); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3228 | } else { // we don't have any instance variables - insert super struct. |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3229 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3230 | Result += " {\n struct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3231 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 9f33bd2 | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 3232 | Result += "_IMPL "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3233 | Result += RCDecl->getNameAsString(); |
Steve Naroff | ffb5f9a | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 3234 | Result += "_IVARS;\n};\n"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3235 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3236 | } |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3237 | // Mark this struct as having been generated. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3238 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3239 | llvm_unreachable("struct already synthesize- SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3240 | } |
| 3241 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3242 | //===----------------------------------------------------------------------===// |
| 3243 | // Meta Data Emission |
| 3244 | //===----------------------------------------------------------------------===// |
| 3245 | |
Fariborz Jahanian | c34409c | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3246 | |
Fariborz Jahanian | 98ba6cd | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3247 | /// RewriteImplementations - This routine rewrites all method implementations |
| 3248 | /// and emits meta-data. |
| 3249 | |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3250 | void RewriteObjC::RewriteImplementations() { |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3251 | int ClsDefCount = ClassImplementation.size(); |
| 3252 | int CatDefCount = CategoryImplementation.size(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3253 | |
Fariborz Jahanian | 98ba6cd | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3254 | // Rewrite implemented methods |
| 3255 | for (int i = 0; i < ClsDefCount; i++) |
| 3256 | RewriteImplementationDecl(ClassImplementation[i]); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3257 | |
Fariborz Jahanian | c54d846 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 3258 | for (int i = 0; i < CatDefCount; i++) |
| 3259 | RewriteImplementationDecl(CategoryImplementation[i]); |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3260 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3261 | |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3262 | void RewriteObjC::RewriteByRefString(std::string &ResultStr, |
| 3263 | const std::string &Name, |
Fariborz Jahanian | ee504a0 | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 3264 | ValueDecl *VD, bool def) { |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3265 | assert(BlockByRefDeclNo.count(VD) && |
| 3266 | "RewriteByRefString: ByRef decl missing"); |
Fariborz Jahanian | ee504a0 | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 3267 | if (def) |
| 3268 | ResultStr += "struct "; |
| 3269 | ResultStr += "__Block_byref_" + Name + |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3270 | "_" + utostr(BlockByRefDeclNo[VD]) ; |
| 3271 | } |
| 3272 | |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3273 | static bool HasLocalVariableExternalStorage(ValueDecl *VD) { |
| 3274 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 3275 | return (Var->isFunctionOrMethodVarDecl() && !Var->hasLocalStorage()); |
| 3276 | return false; |
| 3277 | } |
| 3278 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3279 | std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3280 | StringRef funcName, |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3281 | std::string Tag) { |
| 3282 | const FunctionType *AFT = CE->getFunctionType(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3283 | QualType RT = AFT->getReturnType(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3284 | std::string StructRef = "struct " + Tag; |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 3285 | std::string S = "static " + RT.getAsString(Context->getPrintingPolicy()) + " __" + |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3286 | funcName.str() + "_" + "block_func_" + utostr(i); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 3287 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3288 | BlockDecl *BD = CE->getBlockDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3289 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3290 | if (isa<FunctionNoProtoType>(AFT)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3291 | // 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] | 3292 | // block (to reference imported block decl refs). |
| 3293 | S += "(" + StructRef + " *__cself)"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3294 | } else if (BD->param_empty()) { |
| 3295 | S += "(" + StructRef + " *__cself)"; |
| 3296 | } else { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3297 | const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3298 | assert(FT && "SynthesizeBlockFunc: No function proto"); |
| 3299 | S += '('; |
| 3300 | // first add the implicit argument. |
| 3301 | S += StructRef + " *__cself, "; |
| 3302 | std::string ParamStr; |
| 3303 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 3304 | E = BD->param_end(); AI != E; ++AI) { |
| 3305 | if (AI != BD->param_begin()) S += ", "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3306 | ParamStr = (*AI)->getNameAsString(); |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3307 | QualType QT = (*AI)->getType(); |
Fariborz Jahanian | 835cabe | 2012-03-27 16:42:20 +0000 | [diff] [blame] | 3308 | (void)convertBlockPointerToFunctionPointer(QT); |
| 3309 | QT.getAsStringInternal(ParamStr, Context->getPrintingPolicy()); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3310 | S += ParamStr; |
| 3311 | } |
| 3312 | if (FT->isVariadic()) { |
| 3313 | if (!BD->param_empty()) S += ", "; |
| 3314 | S += "..."; |
| 3315 | } |
| 3316 | S += ')'; |
| 3317 | } |
| 3318 | S += " {\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3319 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3320 | // Create local declarations to avoid rewriting all closure decl ref exprs. |
| 3321 | // First, emit a declaration for all "by ref" decls. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 3322 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3323 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3324 | S += " "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3325 | std::string Name = (*I)->getNameAsString(); |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3326 | std::string TypeString; |
| 3327 | RewriteByRefString(TypeString, Name, (*I)); |
| 3328 | TypeString += " *"; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 3329 | Name = TypeString + Name; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3330 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3331 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3332 | // Next, emit a declaration for all "by copy" declarations. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 3333 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3334 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3335 | S += " "; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3336 | // Handle nested closure invocation. For example: |
| 3337 | // |
| 3338 | // void (^myImportedClosure)(void); |
| 3339 | // myImportedClosure = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3340 | // |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3341 | // void (^anotherClosure)(void); |
| 3342 | // anotherClosure = ^(void) { |
| 3343 | // myImportedClosure(); // import and invoke the closure |
| 3344 | // }; |
| 3345 | // |
Fariborz Jahanian | e1ff123 | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 3346 | if (isTopLevelBlockPointerType((*I)->getType())) { |
| 3347 | RewriteBlockPointerTypeVariable(S, (*I)); |
| 3348 | S += " = ("; |
| 3349 | RewriteBlockPointerType(S, (*I)->getType()); |
| 3350 | S += ")"; |
| 3351 | S += "__cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; |
| 3352 | } |
| 3353 | else { |
Fariborz Jahanian | b6a68c0 | 2010-02-16 17:26:03 +0000 | [diff] [blame] | 3354 | std::string Name = (*I)->getNameAsString(); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3355 | QualType QT = (*I)->getType(); |
| 3356 | if (HasLocalVariableExternalStorage(*I)) |
| 3357 | QT = Context->getPointerType(QT); |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 3358 | QT.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Fariborz Jahanian | e1ff123 | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 3359 | S += Name + " = __cself->" + |
| 3360 | (*I)->getNameAsString() + "; // bound by copy\n"; |
| 3361 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3362 | } |
| 3363 | std::string RewrittenStr = RewrittenBlockExprs[CE]; |
| 3364 | const char *cstr = RewrittenStr.c_str(); |
| 3365 | while (*cstr++ != '{') ; |
| 3366 | S += cstr; |
| 3367 | S += "\n"; |
| 3368 | return S; |
| 3369 | } |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 3370 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3371 | std::string RewriteObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3372 | StringRef funcName, |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3373 | std::string Tag) { |
| 3374 | std::string StructRef = "struct " + Tag; |
| 3375 | std::string S = "static void __"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3376 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3377 | S += funcName; |
| 3378 | S += "_block_copy_" + utostr(i); |
| 3379 | S += "(" + StructRef; |
| 3380 | S += "*dst, " + StructRef; |
| 3381 | S += "*src) {"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3382 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3383 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Fariborz Jahanian | d560ed7 | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3384 | ValueDecl *VD = (*I); |
Steve Naroff | 61d879e | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3385 | S += "_Block_object_assign((void*)&dst->"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3386 | S += (*I)->getNameAsString(); |
Steve Naroff | 5ac4eac | 2008-12-11 20:51:38 +0000 | [diff] [blame] | 3387 | S += ", (void*)src->"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3388 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 3389 | if (BlockByRefDeclsPtrSet.count((*I))) |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 3390 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | d560ed7 | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3391 | else if (VD->getType()->isBlockPointerType()) |
Fariborz Jahanian | bce9ee2 | 2011-07-30 01:07:55 +0000 | [diff] [blame] | 3392 | S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);"; |
Fariborz Jahanian | d560ed7 | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3393 | else |
| 3394 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3395 | } |
Fariborz Jahanian | cbdcfe8 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 3396 | S += "}\n"; |
| 3397 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3398 | S += "\nstatic void __"; |
| 3399 | S += funcName; |
| 3400 | S += "_block_dispose_" + utostr(i); |
| 3401 | S += "(" + StructRef; |
| 3402 | S += "*src) {"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3403 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3404 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Fariborz Jahanian | d560ed7 | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3405 | ValueDecl *VD = (*I); |
Steve Naroff | 61d879e | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3406 | S += "_Block_object_dispose((void*)src->"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3407 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 3408 | if (BlockByRefDeclsPtrSet.count((*I))) |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 3409 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | d560ed7 | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3410 | else if (VD->getType()->isBlockPointerType()) |
Fariborz Jahanian | bce9ee2 | 2011-07-30 01:07:55 +0000 | [diff] [blame] | 3411 | S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);"; |
Fariborz Jahanian | d560ed7 | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3412 | else |
| 3413 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3414 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3415 | S += "}\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3416 | return S; |
| 3417 | } |
| 3418 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3419 | std::string RewriteObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 3420 | std::string Desc) { |
Steve Naroff | 295570a | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3421 | std::string S = "\nstruct " + Tag; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3422 | std::string Constructor = " " + Tag; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3423 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3424 | S += " {\n struct __block_impl impl;\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3425 | S += " struct " + Desc; |
| 3426 | S += "* Desc;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3427 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3428 | Constructor += "(void *fp, "; // Invoke function pointer. |
| 3429 | Constructor += "struct " + Desc; // Descriptor pointer. |
| 3430 | Constructor += " *desc"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3431 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3432 | if (BlockDeclRefs.size()) { |
| 3433 | // Output all "by copy" declarations. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 3434 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3435 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3436 | S += " "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3437 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3438 | std::string ArgName = "_" + FieldName; |
| 3439 | // Handle nested closure invocation. For example: |
| 3440 | // |
| 3441 | // void (^myImportedBlock)(void); |
| 3442 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3443 | // |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3444 | // void (^anotherBlock)(void); |
| 3445 | // anotherBlock = ^(void) { |
| 3446 | // myImportedBlock(); // import and invoke the closure |
| 3447 | // }; |
| 3448 | // |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3449 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3450 | S += "struct __block_impl *"; |
| 3451 | Constructor += ", void *" + ArgName; |
| 3452 | } else { |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3453 | QualType QT = (*I)->getType(); |
| 3454 | if (HasLocalVariableExternalStorage(*I)) |
| 3455 | QT = Context->getPointerType(QT); |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 3456 | QT.getAsStringInternal(FieldName, Context->getPrintingPolicy()); |
| 3457 | QT.getAsStringInternal(ArgName, Context->getPrintingPolicy()); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3458 | Constructor += ", " + ArgName; |
| 3459 | } |
| 3460 | S += FieldName + ";\n"; |
| 3461 | } |
| 3462 | // Output all "by ref" declarations. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 3463 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3464 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3465 | S += " "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3466 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3467 | std::string ArgName = "_" + FieldName; |
Fariborz Jahanian | c6078c8 | 2011-04-01 23:08:13 +0000 | [diff] [blame] | 3468 | { |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3469 | std::string TypeString; |
| 3470 | RewriteByRefString(TypeString, FieldName, (*I)); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 3471 | TypeString += " *"; |
| 3472 | FieldName = TypeString + FieldName; |
| 3473 | ArgName = TypeString + ArgName; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3474 | Constructor += ", " + ArgName; |
| 3475 | } |
| 3476 | S += FieldName + "; // by ref\n"; |
| 3477 | } |
| 3478 | // Finish writing the constructor. |
Fariborz Jahanian | e6a4e39 | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 3479 | Constructor += ", int flags=0)"; |
| 3480 | // Initialize all "by copy" arguments. |
| 3481 | bool firsTime = true; |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 3482 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByCopyDecls.begin(), |
Fariborz Jahanian | e6a4e39 | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 3483 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3484 | std::string Name = (*I)->getNameAsString(); |
| 3485 | if (firsTime) { |
| 3486 | Constructor += " : "; |
| 3487 | firsTime = false; |
| 3488 | } |
| 3489 | else |
| 3490 | Constructor += ", "; |
| 3491 | if (isTopLevelBlockPointerType((*I)->getType())) |
| 3492 | Constructor += Name + "((struct __block_impl *)_" + Name + ")"; |
| 3493 | else |
| 3494 | Constructor += Name + "(_" + Name + ")"; |
| 3495 | } |
| 3496 | // Initialize all "by ref" arguments. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 3497 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByRefDecls.begin(), |
Fariborz Jahanian | e6a4e39 | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 3498 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3499 | std::string Name = (*I)->getNameAsString(); |
| 3500 | if (firsTime) { |
| 3501 | Constructor += " : "; |
| 3502 | firsTime = false; |
| 3503 | } |
| 3504 | else |
| 3505 | Constructor += ", "; |
Fariborz Jahanian | c6078c8 | 2011-04-01 23:08:13 +0000 | [diff] [blame] | 3506 | Constructor += Name + "(_" + Name + "->__forwarding)"; |
Fariborz Jahanian | e6a4e39 | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 3507 | } |
| 3508 | |
| 3509 | Constructor += " {\n"; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3510 | if (GlobalVarDecl) |
| 3511 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 3512 | else |
| 3513 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3514 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3515 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3516 | Constructor += " Desc = desc;\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3517 | } else { |
| 3518 | // Finish writing the constructor. |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3519 | Constructor += ", int flags=0) {\n"; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3520 | if (GlobalVarDecl) |
| 3521 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 3522 | else |
| 3523 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3524 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 3525 | Constructor += " Desc = desc;\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3526 | } |
| 3527 | Constructor += " "; |
| 3528 | Constructor += "}\n"; |
| 3529 | S += Constructor; |
| 3530 | S += "};\n"; |
| 3531 | return S; |
| 3532 | } |
| 3533 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3534 | std::string RewriteObjC::SynthesizeBlockDescriptor(std::string DescTag, |
| 3535 | std::string ImplTag, int i, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3536 | StringRef FunName, |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3537 | unsigned hasCopy) { |
| 3538 | std::string S = "\nstatic struct " + DescTag; |
| 3539 | |
| 3540 | S += " {\n unsigned long reserved;\n"; |
| 3541 | S += " unsigned long Block_size;\n"; |
| 3542 | if (hasCopy) { |
Fariborz Jahanian | e175eeb | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 3543 | S += " void (*copy)(struct "; |
| 3544 | S += ImplTag; S += "*, struct "; |
| 3545 | S += ImplTag; S += "*);\n"; |
| 3546 | |
| 3547 | S += " void (*dispose)(struct "; |
| 3548 | S += ImplTag; S += "*);\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3549 | } |
| 3550 | S += "} "; |
| 3551 | |
| 3552 | S += DescTag + "_DATA = { 0, sizeof(struct "; |
| 3553 | S += ImplTag + ")"; |
| 3554 | if (hasCopy) { |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3555 | S += ", __" + FunName.str() + "_block_copy_" + utostr(i); |
| 3556 | S += ", __" + FunName.str() + "_block_dispose_" + utostr(i); |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3557 | } |
| 3558 | S += "};\n"; |
| 3559 | return S; |
| 3560 | } |
| 3561 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3562 | void RewriteObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3563 | StringRef FunName) { |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 3564 | // Insert declaration for the function in which block literal is used. |
Fariborz Jahanian | 5c26eee | 2010-01-15 18:14:52 +0000 | [diff] [blame] | 3565 | if (CurFunctionDeclToDeclareForBlock && !Blocks.empty()) |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 3566 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Fariborz Jahanian | 535c9c0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 3567 | bool RewriteSC = (GlobalVarDecl && |
| 3568 | !Blocks.empty() && |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3569 | GlobalVarDecl->getStorageClass() == SC_Static && |
Fariborz Jahanian | 535c9c0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 3570 | GlobalVarDecl->getType().getCVRQualifiers()); |
| 3571 | if (RewriteSC) { |
| 3572 | std::string SC(" void __"); |
| 3573 | SC += GlobalVarDecl->getNameAsString(); |
| 3574 | SC += "() {}"; |
| 3575 | InsertText(FunLocStart, SC); |
| 3576 | } |
| 3577 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3578 | // Insert closures that were part of the function. |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3579 | for (unsigned i = 0, count=0; i < Blocks.size(); i++) { |
| 3580 | CollectBlockDeclRefInfo(Blocks[i]); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3581 | // Need to copy-in the inner copied-in variables not actually used in this |
| 3582 | // block. |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3583 | for (int j = 0; j < InnerDeclRefsCount[i]; j++) { |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3584 | DeclRefExpr *Exp = InnerDeclRefs[count++]; |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3585 | ValueDecl *VD = Exp->getDecl(); |
| 3586 | BlockDeclRefs.push_back(Exp); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3587 | if (!VD->hasAttr<BlocksAttr>() && !BlockByCopyDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3588 | BlockByCopyDeclsPtrSet.insert(VD); |
| 3589 | BlockByCopyDecls.push_back(VD); |
| 3590 | } |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3591 | if (VD->hasAttr<BlocksAttr>() && !BlockByRefDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3592 | BlockByRefDeclsPtrSet.insert(VD); |
| 3593 | BlockByRefDecls.push_back(VD); |
| 3594 | } |
Fariborz Jahanian | fc8315f | 2010-10-05 18:05:06 +0000 | [diff] [blame] | 3595 | // imported objects in the inner blocks not used in the outer |
| 3596 | // blocks must be copied/disposed in the outer block as well. |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3597 | if (VD->hasAttr<BlocksAttr>() || |
Fariborz Jahanian | fc8315f | 2010-10-05 18:05:06 +0000 | [diff] [blame] | 3598 | VD->getType()->isObjCObjectPointerType() || |
| 3599 | VD->getType()->isBlockPointerType()) |
| 3600 | ImportedBlockDecls.insert(VD); |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3601 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3602 | |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3603 | std::string ImplTag = "__" + FunName.str() + "_block_impl_" + utostr(i); |
| 3604 | std::string DescTag = "__" + FunName.str() + "_block_desc_" + utostr(i); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3605 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3606 | std::string CI = SynthesizeBlockImpl(Blocks[i], ImplTag, DescTag); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3607 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3608 | InsertText(FunLocStart, CI); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3609 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3610 | std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, ImplTag); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3611 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3612 | InsertText(FunLocStart, CF); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3613 | |
| 3614 | if (ImportedBlockDecls.size()) { |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3615 | std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, ImplTag); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3616 | InsertText(FunLocStart, HF); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3617 | } |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3618 | std::string BD = SynthesizeBlockDescriptor(DescTag, ImplTag, i, FunName, |
| 3619 | ImportedBlockDecls.size() > 0); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3620 | InsertText(FunLocStart, BD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3621 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3622 | BlockDeclRefs.clear(); |
| 3623 | BlockByRefDecls.clear(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 3624 | BlockByRefDeclsPtrSet.clear(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3625 | BlockByCopyDecls.clear(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 3626 | BlockByCopyDeclsPtrSet.clear(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3627 | ImportedBlockDecls.clear(); |
| 3628 | } |
Fariborz Jahanian | 535c9c0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 3629 | if (RewriteSC) { |
Fariborz Jahanian | 8bb35c4 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3630 | // Must insert any 'const/volatile/static here. Since it has been |
| 3631 | // removed as result of rewriting of block literals. |
Fariborz Jahanian | 8bb35c4 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3632 | std::string SC; |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3633 | if (GlobalVarDecl->getStorageClass() == SC_Static) |
Fariborz Jahanian | 8bb35c4 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3634 | SC = "static "; |
Fariborz Jahanian | 8bb35c4 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3635 | if (GlobalVarDecl->getType().isConstQualified()) |
| 3636 | SC += "const "; |
| 3637 | if (GlobalVarDecl->getType().isVolatileQualified()) |
| 3638 | SC += "volatile "; |
Fariborz Jahanian | 535c9c0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 3639 | if (GlobalVarDecl->getType().isRestrictQualified()) |
| 3640 | SC += "restrict "; |
| 3641 | InsertText(FunLocStart, SC); |
Fariborz Jahanian | 8bb35c4 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3642 | } |
| 3643 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3644 | Blocks.clear(); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3645 | InnerDeclRefsCount.clear(); |
| 3646 | InnerDeclRefs.clear(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3647 | RewrittenBlockExprs.clear(); |
| 3648 | } |
| 3649 | |
| 3650 | void RewriteObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { |
| 3651 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3652 | StringRef FuncName = FD->getName(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3653 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3654 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 3655 | } |
| 3656 | |
Fariborz Jahanian | c3bdefa | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 3657 | static void BuildUniqueMethodName(std::string &Name, |
| 3658 | ObjCMethodDecl *MD) { |
| 3659 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3660 | Name = IFace->getName(); |
Fariborz Jahanian | c3bdefa | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 3661 | Name += "__" + MD->getSelector().getAsString(); |
| 3662 | // Convert colons to underscores. |
| 3663 | std::string::size_type loc = 0; |
| 3664 | while ((loc = Name.find(":", loc)) != std::string::npos) |
| 3665 | Name.replace(loc, 1, "_"); |
| 3666 | } |
| 3667 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3668 | void RewriteObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { |
Steve Naroff | 295570a | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3669 | //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n"); |
| 3670 | //SourceLocation FunLocStart = MD->getLocStart(); |
Fariborz Jahanian | b5f99c3 | 2010-01-29 01:55:49 +0000 | [diff] [blame] | 3671 | SourceLocation FunLocStart = MD->getLocStart(); |
Fariborz Jahanian | c3bdefa | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 3672 | std::string FuncName; |
| 3673 | BuildUniqueMethodName(FuncName, MD); |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3674 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3675 | } |
| 3676 | |
| 3677 | void RewriteObjC::GetBlockDeclRefExprs(Stmt *S) { |
John McCall | 8322c3a | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 3678 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3679 | if (*CI) { |
| 3680 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 3681 | GetBlockDeclRefExprs(CBE->getBody()); |
| 3682 | else |
| 3683 | GetBlockDeclRefExprs(*CI); |
| 3684 | } |
| 3685 | // Handle specific things. |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3686 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 3687 | if (DRE->refersToEnclosingLocal()) { |
| 3688 | // FIXME: Handle enums. |
| 3689 | if (!isa<FunctionDecl>(DRE->getDecl())) |
| 3690 | BlockDeclRefs.push_back(DRE); |
| 3691 | if (HasLocalVariableExternalStorage(DRE->getDecl())) |
| 3692 | BlockDeclRefs.push_back(DRE); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3693 | } |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3694 | } |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3695 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3696 | return; |
| 3697 | } |
| 3698 | |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 3699 | void RewriteObjC::GetInnerBlockDeclRefExprs(Stmt *S, |
| 3700 | SmallVectorImpl<DeclRefExpr *> &InnerBlockDeclRefs, |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3701 | llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts) { |
John McCall | 8322c3a | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 3702 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3703 | if (*CI) { |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3704 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) { |
| 3705 | InnerContexts.insert(cast<DeclContext>(CBE->getBlockDecl())); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3706 | GetInnerBlockDeclRefExprs(CBE->getBody(), |
| 3707 | InnerBlockDeclRefs, |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3708 | InnerContexts); |
| 3709 | } |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3710 | else |
| 3711 | GetInnerBlockDeclRefExprs(*CI, |
| 3712 | InnerBlockDeclRefs, |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3713 | InnerContexts); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3714 | |
| 3715 | } |
| 3716 | // Handle specific things. |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3717 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 3718 | if (DRE->refersToEnclosingLocal()) { |
| 3719 | if (!isa<FunctionDecl>(DRE->getDecl()) && |
| 3720 | !InnerContexts.count(DRE->getDecl()->getDeclContext())) |
| 3721 | InnerBlockDeclRefs.push_back(DRE); |
| 3722 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) |
| 3723 | if (Var->isFunctionOrMethodVarDecl()) |
| 3724 | ImportedLocalExternalDecls.insert(Var); |
| 3725 | } |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3726 | } |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3727 | |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3728 | return; |
| 3729 | } |
| 3730 | |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3731 | /// convertFunctionTypeOfBlocks - This routine converts a function type |
| 3732 | /// 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] | 3733 | /// might be block pointers to an equivalent function type replacing |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3734 | /// all block pointers to function pointers. |
| 3735 | QualType RewriteObjC::convertFunctionTypeOfBlocks(const FunctionType *FT) { |
| 3736 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
| 3737 | // FTP will be null for closures that don't take arguments. |
| 3738 | // Generate a funky cast. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3739 | SmallVector<QualType, 8> ArgTypes; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3740 | QualType Res = FT->getReturnType(); |
Fariborz Jahanian | 31b8a9d | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3741 | bool HasBlockType = convertBlockPointerToFunctionPointer(Res); |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3742 | |
| 3743 | if (FTP) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 3744 | for (FunctionProtoType::param_type_iterator I = FTP->param_type_begin(), |
| 3745 | E = FTP->param_type_end(); |
| 3746 | I && (I != E); ++I) { |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3747 | QualType t = *I; |
| 3748 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 31b8a9d | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3749 | if (convertBlockPointerToFunctionPointer(t)) |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3750 | HasBlockType = true; |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3751 | ArgTypes.push_back(t); |
| 3752 | } |
| 3753 | } |
| 3754 | QualType FuncType; |
| 3755 | // FIXME. Does this work if block takes no argument but has a return type |
| 3756 | // which is of block type? |
| 3757 | if (HasBlockType) |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 3758 | FuncType = getSimpleFunctionType(Res, ArgTypes); |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3759 | else FuncType = QualType(FT, 0); |
| 3760 | return FuncType; |
| 3761 | } |
| 3762 | |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3763 | Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp, const Expr *BlockExp) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3764 | // Navigate to relevant type information. |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3765 | const BlockPointerType *CPT = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3766 | |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3767 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BlockExp)) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3768 | CPT = DRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3769 | } else if (const MemberExpr *MExpr = dyn_cast<MemberExpr>(BlockExp)) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3770 | CPT = MExpr->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3771 | } |
| 3772 | else if (const ParenExpr *PRE = dyn_cast<ParenExpr>(BlockExp)) { |
| 3773 | return SynthesizeBlockCall(Exp, PRE->getSubExpr()); |
| 3774 | } |
| 3775 | else if (const ImplicitCastExpr *IEXPR = dyn_cast<ImplicitCastExpr>(BlockExp)) |
| 3776 | CPT = IEXPR->getType()->getAs<BlockPointerType>(); |
| 3777 | else if (const ConditionalOperator *CEXPR = |
| 3778 | dyn_cast<ConditionalOperator>(BlockExp)) { |
| 3779 | Expr *LHSExp = CEXPR->getLHS(); |
| 3780 | Stmt *LHSStmt = SynthesizeBlockCall(Exp, LHSExp); |
| 3781 | Expr *RHSExp = CEXPR->getRHS(); |
| 3782 | Stmt *RHSStmt = SynthesizeBlockCall(Exp, RHSExp); |
| 3783 | Expr *CONDExp = CEXPR->getCond(); |
| 3784 | ConditionalOperator *CondExpr = |
| 3785 | new (Context) ConditionalOperator(CONDExp, |
| 3786 | SourceLocation(), cast<Expr>(LHSStmt), |
Fariborz Jahanian | c6bf0bd | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 3787 | SourceLocation(), cast<Expr>(RHSStmt), |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3788 | Exp->getType(), VK_RValue, OK_Ordinary); |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3789 | return CondExpr; |
Fariborz Jahanian | 6ab7ed4 | 2009-12-18 01:15:21 +0000 | [diff] [blame] | 3790 | } else if (const ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(BlockExp)) { |
| 3791 | CPT = IRE->getType()->getAs<BlockPointerType>(); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 3792 | } else if (const PseudoObjectExpr *POE |
| 3793 | = dyn_cast<PseudoObjectExpr>(BlockExp)) { |
| 3794 | CPT = POE->getType()->castAs<BlockPointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3795 | } else { |
| 3796 | assert(1 && "RewriteBlockClass: Bad type"); |
| 3797 | } |
| 3798 | assert(CPT && "RewriteBlockClass: Bad type"); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3799 | const FunctionType *FT = CPT->getPointeeType()->getAs<FunctionType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3800 | assert(FT && "RewriteBlockClass: Bad type"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3801 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3802 | // FTP will be null for closures that don't take arguments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3803 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3804 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3805 | SourceLocation(), SourceLocation(), |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3806 | &Context->Idents.get("__block_impl")); |
| 3807 | QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD)); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3808 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3809 | // Generate a funky cast. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3810 | SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3811 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3812 | // Push the block argument type. |
| 3813 | ArgTypes.push_back(PtrBlock); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3814 | if (FTP) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 3815 | for (FunctionProtoType::param_type_iterator I = FTP->param_type_begin(), |
| 3816 | E = FTP->param_type_end(); |
| 3817 | I && (I != E); ++I) { |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3818 | QualType t = *I; |
| 3819 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 90d2e57 | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 3820 | if (!convertBlockPointerToFunctionPointer(t)) |
| 3821 | convertToUnqualifiedObjCType(t); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3822 | ArgTypes.push_back(t); |
| 3823 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3824 | } |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3825 | // Now do the pointer to function cast. |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 3826 | QualType PtrToFuncCastType = getSimpleFunctionType(Exp->getType(), ArgTypes); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3827 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3828 | PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3829 | |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3830 | CastExpr *BlkCast = NoTypeInfoCStyleCastExpr(Context, PtrBlock, |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3831 | CK_BitCast, |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3832 | const_cast<Expr*>(BlockExp)); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3833 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3834 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3835 | BlkCast); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3836 | //PE->dump(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3837 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3838 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3839 | SourceLocation(), |
| 3840 | &Context->Idents.get("FuncPtr"), |
| 3841 | Context->VoidPtrTy, 0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3842 | /*BitWidth=*/0, /*Mutable=*/true, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3843 | ICIS_NoInit); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3844 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3845 | FD->getType(), VK_LValue, |
| 3846 | OK_Ordinary); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3847 | |
Fariborz Jahanian | 90d2e57 | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 3848 | |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3849 | CastExpr *FunkCast = NoTypeInfoCStyleCastExpr(Context, PtrToFuncCastType, |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3850 | CK_BitCast, ME); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3851 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3852 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3853 | SmallVector<Expr*, 8> BlkExprs; |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3854 | // Add the implicit argument. |
| 3855 | BlkExprs.push_back(BlkCast); |
| 3856 | // Add the user arguments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3857 | for (CallExpr::arg_iterator I = Exp->arg_begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3858 | E = Exp->arg_end(); I != E; ++I) { |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3859 | BlkExprs.push_back(*I); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3860 | } |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3861 | CallExpr *CE = new (Context) CallExpr(*Context, PE, BlkExprs, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3862 | Exp->getType(), VK_RValue, |
| 3863 | SourceLocation()); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3864 | return CE; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3865 | } |
| 3866 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3867 | // We need to return the rewritten expression to handle cases where the |
| 3868 | // BlockDeclRefExpr is embedded in another expression being rewritten. |
| 3869 | // For example: |
| 3870 | // |
| 3871 | // int main() { |
| 3872 | // __block Foo *f; |
| 3873 | // __block int i; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3874 | // |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3875 | // void (^myblock)() = ^() { |
| 3876 | // [f test]; // f is a BlockDeclRefExpr embedded in a message (which is being rewritten). |
| 3877 | // i = 77; |
| 3878 | // }; |
| 3879 | //} |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3880 | Stmt *RewriteObjC::RewriteBlockDeclRefExpr(DeclRefExpr *DeclRefExp) { |
Fariborz Jahanian | 25c07fa | 2009-12-23 19:26:34 +0000 | [diff] [blame] | 3881 | // Rewrite the byref variable into BYREFVAR->__forwarding->BYREFVAR |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3882 | // for each DeclRefExp where BYREFVAR is name of the variable. |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3883 | ValueDecl *VD = DeclRefExp->getDecl(); |
| 3884 | bool isArrow = DeclRefExp->refersToEnclosingLocal(); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3885 | |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3886 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3887 | SourceLocation(), |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3888 | &Context->Idents.get("__forwarding"), |
| 3889 | Context->VoidPtrTy, 0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3890 | /*BitWidth=*/0, /*Mutable=*/true, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3891 | ICIS_NoInit); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3892 | MemberExpr *ME = new (Context) MemberExpr(DeclRefExp, isArrow, |
| 3893 | FD, SourceLocation(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3894 | FD->getType(), VK_LValue, |
| 3895 | OK_Ordinary); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3896 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3897 | StringRef Name = VD->getName(); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3898 | FD = FieldDecl::Create(*Context, 0, SourceLocation(), SourceLocation(), |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3899 | &Context->Idents.get(Name), |
| 3900 | Context->VoidPtrTy, 0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3901 | /*BitWidth=*/0, /*Mutable=*/true, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3902 | ICIS_NoInit); |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3903 | ME = new (Context) MemberExpr(ME, true, FD, SourceLocation(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3904 | DeclRefExp->getType(), VK_LValue, OK_Ordinary); |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3905 | |
| 3906 | |
| 3907 | |
Steve Naroff | f26a1d4 | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 3908 | // Need parens to enforce precedence. |
Fariborz Jahanian | 5bba75f | 2011-04-01 19:19:28 +0000 | [diff] [blame] | 3909 | ParenExpr *PE = new (Context) ParenExpr(DeclRefExp->getExprLoc(), |
| 3910 | DeclRefExp->getExprLoc(), |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3911 | ME); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3912 | ReplaceStmt(DeclRefExp, PE); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3913 | return PE; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3914 | } |
| 3915 | |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3916 | // Rewrites the imported local variable V with external storage |
| 3917 | // (static, extern, etc.) as *V |
| 3918 | // |
| 3919 | Stmt *RewriteObjC::RewriteLocalVariableExternalStorage(DeclRefExpr *DRE) { |
| 3920 | ValueDecl *VD = DRE->getDecl(); |
| 3921 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 3922 | if (!ImportedLocalExternalDecls.count(Var)) |
| 3923 | return DRE; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3924 | Expr *Exp = new (Context) UnaryOperator(DRE, UO_Deref, DRE->getType(), |
| 3925 | VK_LValue, OK_Ordinary, |
| 3926 | DRE->getLocation()); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3927 | // Need parens to enforce precedence. |
| 3928 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3929 | Exp); |
| 3930 | ReplaceStmt(DRE, PE); |
| 3931 | return PE; |
| 3932 | } |
| 3933 | |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 3934 | void RewriteObjC::RewriteCastExpr(CStyleCastExpr *CE) { |
| 3935 | SourceLocation LocStart = CE->getLParenLoc(); |
| 3936 | SourceLocation LocEnd = CE->getRParenLoc(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3937 | |
| 3938 | // Need to avoid trying to rewrite synthesized casts. |
| 3939 | if (LocStart.isInvalid()) |
| 3940 | return; |
Steve Naroff | 3e7ced1 | 2008-11-03 11:20:24 +0000 | [diff] [blame] | 3941 | // Need to avoid trying to rewrite casts contained in macros. |
| 3942 | if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) |
| 3943 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3944 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3945 | const char *startBuf = SM->getCharacterData(LocStart); |
| 3946 | const char *endBuf = SM->getCharacterData(LocEnd); |
Fariborz Jahanian | f3b9b95 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 3947 | QualType QT = CE->getType(); |
| 3948 | const Type* TypePtr = QT->getAs<Type>(); |
| 3949 | if (isa<TypeOfExprType>(TypePtr)) { |
| 3950 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 3951 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 3952 | std::string TypeAsString = "("; |
Fariborz Jahanian | f506791 | 2010-02-18 01:20:22 +0000 | [diff] [blame] | 3953 | RewriteBlockPointerType(TypeAsString, QT); |
Fariborz Jahanian | f3b9b95 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 3954 | TypeAsString += ")"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3955 | ReplaceText(LocStart, endBuf-startBuf+1, TypeAsString); |
Fariborz Jahanian | f3b9b95 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 3956 | return; |
| 3957 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3958 | // advance the location to startArgList. |
| 3959 | const char *argPtr = startBuf; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3960 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3961 | while (*argPtr++ && (argPtr < endBuf)) { |
| 3962 | switch (*argPtr) { |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 3963 | case '^': |
| 3964 | // Replace the '^' with '*'. |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3965 | LocStart = LocStart.getLocWithOffset(argPtr-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3966 | ReplaceText(LocStart, 1, "*"); |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 3967 | break; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3968 | } |
| 3969 | } |
| 3970 | return; |
| 3971 | } |
| 3972 | |
| 3973 | void RewriteObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { |
| 3974 | SourceLocation DeclLoc = FD->getLocation(); |
| 3975 | unsigned parenCount = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3976 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3977 | // We have 1 or more arguments that have closure pointers. |
| 3978 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 3979 | const char *startArgList = strchr(startBuf, '('); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3980 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3981 | assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3982 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3983 | parenCount++; |
| 3984 | // advance the location to startArgList. |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3985 | DeclLoc = DeclLoc.getLocWithOffset(startArgList-startBuf); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3986 | assert((DeclLoc.isValid()) && "Invalid DeclLoc"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3987 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3988 | const char *argPtr = startArgList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3989 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3990 | while (*argPtr++ && parenCount) { |
| 3991 | switch (*argPtr) { |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 3992 | case '^': |
| 3993 | // Replace the '^' with '*'. |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3994 | DeclLoc = DeclLoc.getLocWithOffset(argPtr-startArgList); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3995 | ReplaceText(DeclLoc, 1, "*"); |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 3996 | break; |
| 3997 | case '(': |
| 3998 | parenCount++; |
| 3999 | break; |
| 4000 | case ')': |
| 4001 | parenCount--; |
| 4002 | break; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4003 | } |
| 4004 | } |
| 4005 | return; |
| 4006 | } |
| 4007 | |
| 4008 | bool RewriteObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4009 | const FunctionProtoType *FTP; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4010 | const PointerType *PT = QT->getAs<PointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4011 | if (PT) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4012 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4013 | } else { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4014 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4015 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4016 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4017 | } |
| 4018 | if (FTP) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4019 | for (FunctionProtoType::param_type_iterator I = FTP->param_type_begin(), |
| 4020 | E = FTP->param_type_end(); |
| 4021 | I != E; ++I) |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4022 | if (isTopLevelBlockPointerType(*I)) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4023 | return true; |
| 4024 | } |
| 4025 | return false; |
| 4026 | } |
| 4027 | |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4028 | bool RewriteObjC::PointerTypeTakesAnyObjCQualifiedType(QualType QT) { |
| 4029 | const FunctionProtoType *FTP; |
| 4030 | const PointerType *PT = QT->getAs<PointerType>(); |
| 4031 | if (PT) { |
| 4032 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4033 | } else { |
| 4034 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
| 4035 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
| 4036 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4037 | } |
| 4038 | if (FTP) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4039 | for (FunctionProtoType::param_type_iterator I = FTP->param_type_begin(), |
| 4040 | E = FTP->param_type_end(); |
| 4041 | I != E; ++I) { |
Fariborz Jahanian | 733dde6 | 2010-11-03 23:50:34 +0000 | [diff] [blame] | 4042 | if ((*I)->isObjCQualifiedIdType()) |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4043 | return true; |
Fariborz Jahanian | 733dde6 | 2010-11-03 23:50:34 +0000 | [diff] [blame] | 4044 | if ((*I)->isObjCObjectPointerType() && |
| 4045 | (*I)->getPointeeType()->isObjCQualifiedInterfaceType()) |
| 4046 | return true; |
| 4047 | } |
| 4048 | |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4049 | } |
| 4050 | return false; |
| 4051 | } |
| 4052 | |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4053 | void RewriteObjC::GetExtentOfArgList(const char *Name, const char *&LParen, |
| 4054 | const char *&RParen) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4055 | const char *argPtr = strchr(Name, '('); |
| 4056 | assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4057 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4058 | LParen = argPtr; // output the start. |
| 4059 | argPtr++; // skip past the left paren. |
| 4060 | unsigned parenCount = 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4061 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4062 | while (*argPtr && parenCount) { |
| 4063 | switch (*argPtr) { |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4064 | case '(': parenCount++; break; |
| 4065 | case ')': parenCount--; break; |
| 4066 | default: break; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4067 | } |
| 4068 | if (parenCount) argPtr++; |
| 4069 | } |
| 4070 | assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); |
| 4071 | RParen = argPtr; // output the end |
| 4072 | } |
| 4073 | |
| 4074 | void RewriteObjC::RewriteBlockPointerDecl(NamedDecl *ND) { |
| 4075 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 4076 | RewriteBlockPointerFunctionArgs(FD); |
| 4077 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4078 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4079 | // Handle Variables and Typedefs. |
| 4080 | SourceLocation DeclLoc = ND->getLocation(); |
| 4081 | QualType DeclT; |
| 4082 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4083 | DeclT = VD->getType(); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4084 | else if (TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(ND)) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4085 | DeclT = TDD->getUnderlyingType(); |
| 4086 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) |
| 4087 | DeclT = FD->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4088 | else |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4089 | llvm_unreachable("RewriteBlockPointerDecl(): Decl type not yet handled"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4090 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4091 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4092 | const char *endBuf = startBuf; |
| 4093 | // scan backward (from the decl location) for the end of the previous decl. |
| 4094 | while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) |
| 4095 | startBuf--; |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4096 | SourceLocation Start = DeclLoc.getLocWithOffset(startBuf-endBuf); |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4097 | std::string buf; |
| 4098 | unsigned OrigLength=0; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4099 | // *startBuf != '^' if we are dealing with a pointer to function that |
| 4100 | // may take block argument types (which will be handled below). |
| 4101 | if (*startBuf == '^') { |
| 4102 | // Replace the '^' with '*', computing a negative offset. |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4103 | buf = '*'; |
| 4104 | startBuf++; |
| 4105 | OrigLength++; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4106 | } |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4107 | while (*startBuf != ')') { |
| 4108 | buf += *startBuf; |
| 4109 | startBuf++; |
| 4110 | OrigLength++; |
| 4111 | } |
| 4112 | buf += ')'; |
| 4113 | OrigLength++; |
| 4114 | |
| 4115 | if (PointerTypeTakesAnyBlockArguments(DeclT) || |
| 4116 | PointerTypeTakesAnyObjCQualifiedType(DeclT)) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4117 | // Replace the '^' with '*' for arguments. |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4118 | // Replace id<P> with id/*<>*/ |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4119 | DeclLoc = ND->getLocation(); |
| 4120 | startBuf = SM->getCharacterData(DeclLoc); |
| 4121 | const char *argListBegin, *argListEnd; |
| 4122 | GetExtentOfArgList(startBuf, argListBegin, argListEnd); |
| 4123 | while (argListBegin < argListEnd) { |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4124 | if (*argListBegin == '^') |
| 4125 | buf += '*'; |
| 4126 | else if (*argListBegin == '<') { |
| 4127 | buf += "/*"; |
| 4128 | buf += *argListBegin++; |
Dmitri Gribenko | 76bb5cabfa | 2012-09-10 21:20:09 +0000 | [diff] [blame] | 4129 | OrigLength++; |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4130 | while (*argListBegin != '>') { |
| 4131 | buf += *argListBegin++; |
| 4132 | OrigLength++; |
| 4133 | } |
| 4134 | buf += *argListBegin; |
| 4135 | buf += "*/"; |
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 | else |
| 4138 | buf += *argListBegin; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4139 | argListBegin++; |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4140 | OrigLength++; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4141 | } |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4142 | buf += ')'; |
| 4143 | OrigLength++; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4144 | } |
Fariborz Jahanian | 147e1cb | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4145 | ReplaceText(Start, OrigLength, buf); |
| 4146 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4147 | return; |
| 4148 | } |
| 4149 | |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4150 | |
| 4151 | /// SynthesizeByrefCopyDestroyHelper - This routine synthesizes: |
| 4152 | /// void __Block_byref_id_object_copy(struct Block_byref_id_object *dst, |
| 4153 | /// struct Block_byref_id_object *src) { |
| 4154 | /// _Block_object_assign (&_dest->object, _src->object, |
| 4155 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4156 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4157 | /// _Block_object_assign(&_dest->object, _src->object, |
| 4158 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4159 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4160 | /// } |
| 4161 | /// And: |
| 4162 | /// void __Block_byref_id_object_dispose(struct Block_byref_id_object *_src) { |
| 4163 | /// _Block_object_dispose(_src->object, |
| 4164 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4165 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4166 | /// _Block_object_dispose(_src->object, |
| 4167 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4168 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4169 | /// } |
| 4170 | |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4171 | std::string RewriteObjC::SynthesizeByrefCopyDestroyHelper(VarDecl *VD, |
| 4172 | int flag) { |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4173 | std::string S; |
Benjamin Kramer | e056cea | 2010-01-10 19:57:50 +0000 | [diff] [blame] | 4174 | if (CopyDestroyCache.count(flag)) |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4175 | return S; |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4176 | CopyDestroyCache.insert(flag); |
| 4177 | S = "static void __Block_byref_id_object_copy_"; |
| 4178 | S += utostr(flag); |
| 4179 | S += "(void *dst, void *src) {\n"; |
| 4180 | |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4181 | // offset into the object pointer is computed as: |
| 4182 | // void * + void* + int + int + void* + void * |
| 4183 | unsigned IntSize = |
| 4184 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 4185 | unsigned VoidPtrSize = |
| 4186 | static_cast<unsigned>(Context->getTypeSize(Context->VoidPtrTy)); |
| 4187 | |
Ken Dyck | d9c83e6 | 2011-04-30 16:08:27 +0000 | [diff] [blame] | 4188 | unsigned offset = (VoidPtrSize*4 + IntSize + IntSize)/Context->getCharWidth(); |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4189 | S += " _Block_object_assign((char*)dst + "; |
| 4190 | S += utostr(offset); |
| 4191 | S += ", *(void * *) ((char*)src + "; |
| 4192 | S += utostr(offset); |
| 4193 | S += "), "; |
| 4194 | S += utostr(flag); |
| 4195 | S += ");\n}\n"; |
| 4196 | |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4197 | S += "static void __Block_byref_id_object_dispose_"; |
| 4198 | S += utostr(flag); |
| 4199 | S += "(void *src) {\n"; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4200 | S += " _Block_object_dispose(*(void * *) ((char*)src + "; |
| 4201 | S += utostr(offset); |
| 4202 | S += "), "; |
| 4203 | S += utostr(flag); |
| 4204 | S += ");\n}\n"; |
| 4205 | return S; |
| 4206 | } |
| 4207 | |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4208 | /// RewriteByRefVar - For each __block typex ND variable this routine transforms |
| 4209 | /// the declaration into: |
| 4210 | /// struct __Block_byref_ND { |
| 4211 | /// void *__isa; // NULL for everything except __weak pointers |
| 4212 | /// struct __Block_byref_ND *__forwarding; |
| 4213 | /// int32_t __flags; |
| 4214 | /// int32_t __size; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4215 | /// void *__Block_byref_id_object_copy; // If variable is __block ObjC object |
| 4216 | /// void *__Block_byref_id_object_dispose; // If variable is __block ObjC object |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4217 | /// typex ND; |
| 4218 | /// }; |
| 4219 | /// |
| 4220 | /// It then replaces declaration of ND variable with: |
| 4221 | /// struct __Block_byref_ND ND = {__isa=0B, __forwarding=&ND, __flags=some_flag, |
| 4222 | /// __size=sizeof(struct __Block_byref_ND), |
| 4223 | /// ND=initializer-if-any}; |
| 4224 | /// |
| 4225 | /// |
| 4226 | void RewriteObjC::RewriteByRefVar(VarDecl *ND) { |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 4227 | // Insert declaration for the function in which block literal is |
| 4228 | // used. |
| 4229 | if (CurFunctionDeclToDeclareForBlock) |
| 4230 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4231 | int flag = 0; |
| 4232 | int isa = 0; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4233 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
Fariborz Jahanian | 6005bd8 | 2010-02-26 22:49:11 +0000 | [diff] [blame] | 4234 | if (DeclLoc.isInvalid()) |
| 4235 | // If type location is missing, it is because of missing type (a warning). |
| 4236 | // Use variable's location which is good for this case. |
| 4237 | DeclLoc = ND->getLocation(); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4238 | const char *startBuf = SM->getCharacterData(DeclLoc); |
Fariborz Jahanian | 92368a1 | 2009-12-30 20:38:08 +0000 | [diff] [blame] | 4239 | SourceLocation X = ND->getLocEnd(); |
Chandler Carruth | 35f5320 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 4240 | X = SM->getExpansionLoc(X); |
Fariborz Jahanian | 92368a1 | 2009-12-30 20:38:08 +0000 | [diff] [blame] | 4241 | const char *endBuf = SM->getCharacterData(X); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4242 | std::string Name(ND->getNameAsString()); |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4243 | std::string ByrefType; |
Fariborz Jahanian | ee504a0 | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 4244 | RewriteByRefString(ByrefType, Name, ND, true); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4245 | ByrefType += " {\n"; |
| 4246 | ByrefType += " void *__isa;\n"; |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4247 | RewriteByRefString(ByrefType, Name, ND); |
| 4248 | ByrefType += " *__forwarding;\n"; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4249 | ByrefType += " int __flags;\n"; |
| 4250 | ByrefType += " int __size;\n"; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4251 | // Add void *__Block_byref_id_object_copy; |
| 4252 | // void *__Block_byref_id_object_dispose; if needed. |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4253 | QualType Ty = ND->getType(); |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 4254 | bool HasCopyAndDispose = Context->BlockRequiresCopying(Ty, ND); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4255 | if (HasCopyAndDispose) { |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4256 | ByrefType += " void (*__Block_byref_id_object_copy)(void*, void*);\n"; |
| 4257 | ByrefType += " void (*__Block_byref_id_object_dispose)(void*);\n"; |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4258 | } |
Fariborz Jahanian | ff51d4e | 2011-03-31 22:49:32 +0000 | [diff] [blame] | 4259 | |
| 4260 | QualType T = Ty; |
| 4261 | (void)convertBlockPointerToFunctionPointer(T); |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 4262 | T.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Fariborz Jahanian | ff51d4e | 2011-03-31 22:49:32 +0000 | [diff] [blame] | 4263 | |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4264 | ByrefType += " " + Name + ";\n"; |
| 4265 | ByrefType += "};\n"; |
| 4266 | // Insert this type in global scope. It is needed by helper function. |
Fariborz Jahanian | faf85c0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4267 | SourceLocation FunLocStart; |
| 4268 | if (CurFunctionDef) |
| 4269 | FunLocStart = CurFunctionDef->getTypeSpecStartLoc(); |
| 4270 | else { |
| 4271 | assert(CurMethodDef && "RewriteByRefVar - CurMethodDef is null"); |
| 4272 | FunLocStart = CurMethodDef->getLocStart(); |
| 4273 | } |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4274 | InsertText(FunLocStart, ByrefType); |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4275 | if (Ty.isObjCGCWeak()) { |
| 4276 | flag |= BLOCK_FIELD_IS_WEAK; |
| 4277 | isa = 1; |
| 4278 | } |
| 4279 | |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4280 | if (HasCopyAndDispose) { |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4281 | flag = BLOCK_BYREF_CALLER; |
| 4282 | QualType Ty = ND->getType(); |
| 4283 | // FIXME. Handle __weak variable (BLOCK_FIELD_IS_WEAK) as well. |
| 4284 | if (Ty->isBlockPointerType()) |
| 4285 | flag |= BLOCK_FIELD_IS_BLOCK; |
| 4286 | else |
| 4287 | flag |= BLOCK_FIELD_IS_OBJECT; |
| 4288 | std::string HF = SynthesizeByrefCopyDestroyHelper(ND, flag); |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4289 | if (!HF.empty()) |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4290 | InsertText(FunLocStart, HF); |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4291 | } |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4292 | |
| 4293 | // struct __Block_byref_ND ND = |
| 4294 | // {0, &ND, some_flag, __size=sizeof(struct __Block_byref_ND), |
| 4295 | // initializer-if-any}; |
| 4296 | bool hasInit = (ND->getInit() != 0); |
Fariborz Jahanian | f794543 | 2010-01-05 18:15:57 +0000 | [diff] [blame] | 4297 | unsigned flags = 0; |
| 4298 | if (HasCopyAndDispose) |
| 4299 | flags |= BLOCK_HAS_COPY_DISPOSE; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4300 | Name = ND->getNameAsString(); |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4301 | ByrefType.clear(); |
| 4302 | RewriteByRefString(ByrefType, Name, ND); |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4303 | std::string ForwardingCastType("("); |
| 4304 | ForwardingCastType += ByrefType + " *)"; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4305 | if (!hasInit) { |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4306 | ByrefType += " " + Name + " = {(void*)"; |
| 4307 | ByrefType += utostr(isa); |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4308 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4309 | ByrefType += utostr(flags); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4310 | ByrefType += ", "; |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4311 | ByrefType += "sizeof("; |
| 4312 | RewriteByRefString(ByrefType, Name, ND); |
| 4313 | ByrefType += ")"; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4314 | if (HasCopyAndDispose) { |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4315 | ByrefType += ", __Block_byref_id_object_copy_"; |
| 4316 | ByrefType += utostr(flag); |
| 4317 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 4318 | ByrefType += utostr(flag); |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4319 | } |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4320 | ByrefType += "};\n"; |
Fariborz Jahanian | ff51d4e | 2011-03-31 22:49:32 +0000 | [diff] [blame] | 4321 | unsigned nameSize = Name.size(); |
| 4322 | // for block or function pointer declaration. Name is aleady |
| 4323 | // part of the declaration. |
| 4324 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) |
| 4325 | nameSize = 1; |
| 4326 | ReplaceText(DeclLoc, endBuf-startBuf+nameSize, ByrefType); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4327 | } |
| 4328 | else { |
Fariborz Jahanian | faf85c0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4329 | SourceLocation startLoc; |
| 4330 | Expr *E = ND->getInit(); |
| 4331 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 4332 | startLoc = ECE->getLParenLoc(); |
| 4333 | else |
| 4334 | startLoc = E->getLocStart(); |
Chandler Carruth | 35f5320 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 4335 | startLoc = SM->getExpansionLoc(startLoc); |
Fariborz Jahanian | faf85c0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4336 | endBuf = SM->getCharacterData(startLoc); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4337 | ByrefType += " " + Name; |
Fariborz Jahanian | faf85c0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4338 | ByrefType += " = {(void*)"; |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4339 | ByrefType += utostr(isa); |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4340 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4341 | ByrefType += utostr(flags); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4342 | ByrefType += ", "; |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4343 | ByrefType += "sizeof("; |
| 4344 | RewriteByRefString(ByrefType, Name, ND); |
| 4345 | ByrefType += "), "; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4346 | if (HasCopyAndDispose) { |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4347 | ByrefType += "__Block_byref_id_object_copy_"; |
| 4348 | ByrefType += utostr(flag); |
| 4349 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 4350 | ByrefType += utostr(flag); |
| 4351 | ByrefType += ", "; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4352 | } |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4353 | ReplaceText(DeclLoc, endBuf-startBuf, ByrefType); |
Steve Naroff | 1346837 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 4354 | |
| 4355 | // Complete the newly synthesized compound expression by inserting a right |
| 4356 | // curly brace before the end of the declaration. |
| 4357 | // FIXME: This approach avoids rewriting the initializer expression. It |
| 4358 | // also assumes there is only one declarator. For example, the following |
| 4359 | // isn't currently supported by this routine (in general): |
| 4360 | // |
| 4361 | // double __block BYREFVAR = 1.34, BYREFVAR2 = 1.37; |
| 4362 | // |
Fariborz Jahanian | 34c8598 | 2010-07-21 17:36:39 +0000 | [diff] [blame] | 4363 | const char *startInitializerBuf = SM->getCharacterData(startLoc); |
| 4364 | const char *semiBuf = strchr(startInitializerBuf, ';'); |
Steve Naroff | 1346837 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 4365 | assert((*semiBuf == ';') && "RewriteByRefVar: can't find ';'"); |
| 4366 | SourceLocation semiLoc = |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4367 | startLoc.getLocWithOffset(semiBuf-startInitializerBuf); |
Steve Naroff | 1346837 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 4368 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4369 | InsertText(semiLoc, "}"); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4370 | } |
Fariborz Jahanian | 8120346 | 2009-12-22 00:48:54 +0000 | [diff] [blame] | 4371 | return; |
| 4372 | } |
| 4373 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4374 | void RewriteObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4375 | // Add initializers for any closure decl refs. |
| 4376 | GetBlockDeclRefExprs(Exp->getBody()); |
| 4377 | if (BlockDeclRefs.size()) { |
| 4378 | // Unique all "by copy" declarations. |
| 4379 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4380 | if (!BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>()) { |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4381 | if (!BlockByCopyDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 4382 | BlockByCopyDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 4383 | BlockByCopyDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 4384 | } |
| 4385 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4386 | // Unique all "by ref" declarations. |
| 4387 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4388 | if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>()) { |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4389 | if (!BlockByRefDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 4390 | BlockByRefDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 4391 | BlockByRefDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 4392 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4393 | } |
| 4394 | // Find any imported blocks...they will need special attention. |
| 4395 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4396 | if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>() || |
Fariborz Jahanian | e175eeb | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 4397 | BlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
Fariborz Jahanian | 9bbc148 | 2010-02-26 21:46:27 +0000 | [diff] [blame] | 4398 | BlockDeclRefs[i]->getType()->isBlockPointerType()) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4399 | ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4400 | } |
| 4401 | } |
| 4402 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4403 | FunctionDecl *RewriteObjC::SynthBlockInitFunctionDecl(StringRef name) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4404 | IdentifierInfo *ID = &Context->Idents.get(name); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4405 | QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4406 | return FunctionDecl::Create(*Context, TUDecl, SourceLocation(), |
| 4407 | SourceLocation(), ID, FType, 0, SC_Extern, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4408 | false, false); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4409 | } |
| 4410 | |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4411 | Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 4412 | const SmallVectorImpl<DeclRefExpr *> &InnerBlockDeclRefs) { |
Fariborz Jahanian | 9cd649d | 2011-02-16 22:37:10 +0000 | [diff] [blame] | 4413 | const BlockDecl *block = Exp->getBlockDecl(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4414 | Blocks.push_back(Exp); |
| 4415 | |
| 4416 | CollectBlockDeclRefInfo(Exp); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4417 | |
| 4418 | // Add inner imported variables now used in current block. |
| 4419 | int countOfInnerDecls = 0; |
Fariborz Jahanian | be730c9 | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4420 | if (!InnerBlockDeclRefs.empty()) { |
| 4421 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) { |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4422 | DeclRefExpr *Exp = InnerBlockDeclRefs[i]; |
Fariborz Jahanian | be730c9 | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4423 | ValueDecl *VD = Exp->getDecl(); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4424 | if (!VD->hasAttr<BlocksAttr>() && !BlockByCopyDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4425 | // We need to save the copied-in variables in nested |
| 4426 | // blocks because it is needed at the end for some of the API generations. |
| 4427 | // See SynthesizeBlockLiterals routine. |
Fariborz Jahanian | be730c9 | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4428 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 4429 | BlockDeclRefs.push_back(Exp); |
| 4430 | BlockByCopyDeclsPtrSet.insert(VD); |
| 4431 | BlockByCopyDecls.push_back(VD); |
| 4432 | } |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4433 | if (VD->hasAttr<BlocksAttr>() && !BlockByRefDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | be730c9 | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4434 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 4435 | BlockDeclRefs.push_back(Exp); |
| 4436 | BlockByRefDeclsPtrSet.insert(VD); |
| 4437 | BlockByRefDecls.push_back(VD); |
| 4438 | } |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4439 | } |
Fariborz Jahanian | be730c9 | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4440 | // Find any imported blocks...they will need special attention. |
| 4441 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4442 | if (InnerBlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>() || |
Fariborz Jahanian | be730c9 | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4443 | InnerBlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
| 4444 | InnerBlockDeclRefs[i]->getType()->isBlockPointerType()) |
| 4445 | ImportedBlockDecls.insert(InnerBlockDeclRefs[i]->getDecl()); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4446 | } |
| 4447 | InnerDeclRefsCount.push_back(countOfInnerDecls); |
| 4448 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4449 | std::string FuncName; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4450 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4451 | if (CurFunctionDef) |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4452 | FuncName = CurFunctionDef->getNameAsString(); |
Fariborz Jahanian | c3bdefa | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 4453 | else if (CurMethodDef) |
| 4454 | BuildUniqueMethodName(FuncName, CurMethodDef); |
| 4455 | else if (GlobalVarDecl) |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4456 | FuncName = std::string(GlobalVarDecl->getNameAsString()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4457 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4458 | std::string BlockNumber = utostr(Blocks.size()-1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4459 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4460 | std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber; |
| 4461 | std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4462 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4463 | // Get a pointer to the function type so we can cast appropriately. |
Fariborz Jahanian | 19c6240 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4464 | QualType BFT = convertFunctionTypeOfBlocks(Exp->getFunctionType()); |
| 4465 | QualType FType = Context->getPointerType(BFT); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4466 | |
| 4467 | FunctionDecl *FD; |
| 4468 | Expr *NewRep; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4469 | |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 4470 | // Simulate a constructor call... |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4471 | FD = SynthBlockInitFunctionDecl(Tag); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4472 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, false, FType, VK_RValue, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4473 | SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4474 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4475 | SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4476 | |
Steve Naroff | e251423 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4477 | // Initialize the block function. |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4478 | FD = SynthBlockInitFunctionDecl(Func); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4479 | DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, false, FD->getType(), |
| 4480 | VK_LValue, SourceLocation()); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4481 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4482 | CK_BitCast, Arg); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4483 | InitExprs.push_back(castExpr); |
| 4484 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4485 | // Initialize the block descriptor. |
| 4486 | std::string DescData = "__" + FuncName + "_block_desc_" + BlockNumber + "_DATA"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4487 | |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4488 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, |
| 4489 | SourceLocation(), SourceLocation(), |
| 4490 | &Context->Idents.get(DescData.c_str()), |
| 4491 | Context->VoidPtrTy, 0, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4492 | SC_Static); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4493 | UnaryOperator *DescRefExpr = |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4494 | new (Context) UnaryOperator(new (Context) DeclRefExpr(NewVD, false, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4495 | Context->VoidPtrTy, |
| 4496 | VK_LValue, |
| 4497 | SourceLocation()), |
| 4498 | UO_AddrOf, |
| 4499 | Context->getPointerType(Context->VoidPtrTy), |
| 4500 | VK_RValue, OK_Ordinary, |
| 4501 | SourceLocation()); |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4502 | InitExprs.push_back(DescRefExpr); |
| 4503 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4504 | // Add initializers for any closure decl refs. |
| 4505 | if (BlockDeclRefs.size()) { |
Steve Naroff | e251423 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4506 | Expr *Exp; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4507 | // Output all "by copy" declarations. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 4508 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4509 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4510 | if (isObjCType((*I)->getType())) { |
Steve Naroff | e251423 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4511 | // FIXME: Conform to ABI ([[obj retain] autorelease]). |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4512 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4513 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4514 | SourceLocation()); |
Fariborz Jahanian | 36680dd | 2010-05-24 18:32:56 +0000 | [diff] [blame] | 4515 | if (HasLocalVariableExternalStorage(*I)) { |
| 4516 | QualType QT = (*I)->getType(); |
| 4517 | QT = Context->getPointerType(QT); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4518 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue, |
| 4519 | OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | 36680dd | 2010-05-24 18:32:56 +0000 | [diff] [blame] | 4520 | } |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4521 | } else if (isTopLevelBlockPointerType((*I)->getType())) { |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4522 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4523 | Arg = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4524 | SourceLocation()); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4525 | Exp = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4526 | CK_BitCast, Arg); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4527 | } else { |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4528 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4529 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4530 | SourceLocation()); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4531 | if (HasLocalVariableExternalStorage(*I)) { |
| 4532 | QualType QT = (*I)->getType(); |
| 4533 | QT = Context->getPointerType(QT); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4534 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue, |
| 4535 | OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4536 | } |
| 4537 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4538 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4539 | InitExprs.push_back(Exp); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4540 | } |
| 4541 | // Output all "by ref" declarations. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 4542 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4543 | E = BlockByRefDecls.end(); I != E; ++I) { |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4544 | ValueDecl *ND = (*I); |
| 4545 | std::string Name(ND->getNameAsString()); |
| 4546 | std::string RecName; |
Fariborz Jahanian | ee504a0 | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 4547 | RewriteByRefString(RecName, Name, ND, true); |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4548 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str() |
| 4549 | + sizeof("struct")); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4550 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4551 | SourceLocation(), SourceLocation(), |
| 4552 | II); |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4553 | assert(RD && "SynthBlockInitExpr(): Can't find RecordDecl"); |
| 4554 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 4555 | |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4556 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4557 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4558 | SourceLocation()); |
Fariborz Jahanian | 9cd649d | 2011-02-16 22:37:10 +0000 | [diff] [blame] | 4559 | bool isNestedCapturedVar = false; |
| 4560 | if (block) |
Aaron Ballman | 9371dd2 | 2014-03-14 18:34:04 +0000 | [diff] [blame] | 4561 | for (const auto &CI : block->captures()) { |
| 4562 | const VarDecl *variable = CI.getVariable(); |
| 4563 | if (variable == ND && CI.isNested()) { |
| 4564 | assert (CI.isByRef() && |
Fariborz Jahanian | 9cd649d | 2011-02-16 22:37:10 +0000 | [diff] [blame] | 4565 | "SynthBlockInitExpr - captured block variable is not byref"); |
| 4566 | isNestedCapturedVar = true; |
| 4567 | break; |
| 4568 | } |
| 4569 | } |
| 4570 | // captured nested byref variable has its address passed. Do not take |
| 4571 | // its address again. |
| 4572 | if (!isNestedCapturedVar) |
| 4573 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4574 | Context->getPointerType(Exp->getType()), |
| 4575 | VK_RValue, OK_Ordinary, SourceLocation()); |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4576 | Exp = NoTypeInfoCStyleCastExpr(Context, castT, CK_BitCast, Exp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4577 | InitExprs.push_back(Exp); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4578 | } |
| 4579 | } |
Fariborz Jahanian | 65e6bd6 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 4580 | if (ImportedBlockDecls.size()) { |
| 4581 | // generate BLOCK_HAS_COPY_DISPOSE(have helper funcs) | BLOCK_HAS_DESCRIPTOR |
| 4582 | int flag = (BLOCK_HAS_COPY_DISPOSE | BLOCK_HAS_DESCRIPTOR); |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4583 | unsigned IntSize = |
| 4584 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 4585 | Expr *FlagExp = IntegerLiteral::Create(*Context, llvm::APInt(IntSize, flag), |
| 4586 | Context->IntTy, SourceLocation()); |
Fariborz Jahanian | 65e6bd6 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 4587 | InitExprs.push_back(FlagExp); |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4588 | } |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 4589 | NewRep = new (Context) CallExpr(*Context, DRE, InitExprs, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4590 | FType, VK_LValue, SourceLocation()); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4591 | NewRep = new (Context) UnaryOperator(NewRep, UO_AddrOf, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4592 | Context->getPointerType(NewRep->getType()), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4593 | VK_RValue, OK_Ordinary, SourceLocation()); |
John McCall | f608deb | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4594 | NewRep = NoTypeInfoCStyleCastExpr(Context, FType, CK_BitCast, |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4595 | NewRep); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4596 | BlockDeclRefs.clear(); |
| 4597 | BlockByRefDecls.clear(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4598 | BlockByRefDeclsPtrSet.clear(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4599 | BlockByCopyDecls.clear(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4600 | BlockByCopyDeclsPtrSet.clear(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4601 | ImportedBlockDecls.clear(); |
| 4602 | return NewRep; |
| 4603 | } |
| 4604 | |
Fariborz Jahanian | 74405b0 | 2011-02-24 21:29:21 +0000 | [diff] [blame] | 4605 | bool RewriteObjC::IsDeclStmtInForeachHeader(DeclStmt *DS) { |
| 4606 | if (const ObjCForCollectionStmt * CS = |
| 4607 | dyn_cast<ObjCForCollectionStmt>(Stmts.back())) |
| 4608 | return CS->getElement() == DS; |
| 4609 | return false; |
| 4610 | } |
| 4611 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4612 | //===----------------------------------------------------------------------===// |
| 4613 | // Function Body / Expression rewriting |
| 4614 | //===----------------------------------------------------------------------===// |
| 4615 | |
| 4616 | Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4617 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4618 | isa<DoStmt>(S) || isa<ForStmt>(S)) |
| 4619 | Stmts.push_back(S); |
| 4620 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 4621 | Stmts.push_back(S); |
Chris Lattner | b71980f | 2010-01-09 21:45:57 +0000 | [diff] [blame] | 4622 | ObjCBcLabelNo.push_back(++BcLabelCount); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4623 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4624 | |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4625 | // Pseudo-object operations and ivar references need special |
| 4626 | // treatment because we're going to recursively rewrite them. |
| 4627 | if (PseudoObjectExpr *PseudoOp = dyn_cast<PseudoObjectExpr>(S)) { |
| 4628 | if (isa<BinaryOperator>(PseudoOp->getSyntacticForm())) { |
| 4629 | return RewritePropertyOrImplicitSetter(PseudoOp); |
| 4630 | } else { |
| 4631 | return RewritePropertyOrImplicitGetter(PseudoOp); |
| 4632 | } |
| 4633 | } else if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) { |
| 4634 | return RewriteObjCIvarRefExpr(IvarRefExpr); |
| 4635 | } |
| 4636 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4637 | SourceRange OrigStmtRange = S->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4638 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4639 | // Perform a bottom up rewrite of all children. |
John McCall | 8322c3a | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 4640 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4641 | if (*CI) { |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4642 | Stmt *childStmt = (*CI); |
| 4643 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(childStmt); |
Fariborz Jahanian | fae2e8d | 2011-04-11 21:17:02 +0000 | [diff] [blame] | 4644 | if (newStmt) { |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4645 | *CI = newStmt; |
Fariborz Jahanian | fae2e8d | 2011-04-11 21:17:02 +0000 | [diff] [blame] | 4646 | } |
Nick Lewycky | 508ef2c | 2010-10-31 21:07:24 +0000 | [diff] [blame] | 4647 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4648 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4649 | if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4650 | SmallVector<DeclRefExpr *, 8> InnerBlockDeclRefs; |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4651 | llvm::SmallPtrSet<const DeclContext *, 8> InnerContexts; |
| 4652 | InnerContexts.insert(BE->getBlockDecl()); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4653 | ImportedLocalExternalDecls.clear(); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4654 | GetInnerBlockDeclRefExprs(BE->getBody(), |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4655 | InnerBlockDeclRefs, InnerContexts); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4656 | // Rewrite the block body in place. |
Fariborz Jahanian | 086a24a | 2010-11-08 18:37:50 +0000 | [diff] [blame] | 4657 | Stmt *SaveCurrentBody = CurrentBody; |
| 4658 | CurrentBody = BE->getBody(); |
| 4659 | PropParentMap = 0; |
Fariborz Jahanian | c7c346f | 2011-08-02 20:28:46 +0000 | [diff] [blame] | 4660 | // block literal on rhs of a property-dot-sytax assignment |
| 4661 | // must be replaced by its synthesize ast so getRewrittenText |
| 4662 | // works as expected. In this case, what actually ends up on RHS |
| 4663 | // is the blockTranscribed which is the helper function for the |
| 4664 | // block literal; as in: self.c = ^() {[ace ARR];}; |
| 4665 | bool saveDisableReplaceStmt = DisableReplaceStmt; |
| 4666 | DisableReplaceStmt = false; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4667 | RewriteFunctionBodyOrGlobalInitializer(BE->getBody()); |
Fariborz Jahanian | c7c346f | 2011-08-02 20:28:46 +0000 | [diff] [blame] | 4668 | DisableReplaceStmt = saveDisableReplaceStmt; |
Fariborz Jahanian | 086a24a | 2010-11-08 18:37:50 +0000 | [diff] [blame] | 4669 | CurrentBody = SaveCurrentBody; |
| 4670 | PropParentMap = 0; |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4671 | ImportedLocalExternalDecls.clear(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4672 | // 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] | 4673 | std::string Str = Rewrite.getRewrittenText(BE->getSourceRange()); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4674 | RewrittenBlockExprs[BE] = Str; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4675 | |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4676 | Stmt *blockTranscribed = SynthBlockInitExpr(BE, InnerBlockDeclRefs); |
| 4677 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4678 | //blockTranscribed->dump(); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4679 | ReplaceStmt(S, blockTranscribed); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4680 | return blockTranscribed; |
| 4681 | } |
| 4682 | // Handle specific things. |
| 4683 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 4684 | return RewriteAtEncode(AtEncode); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4685 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4686 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 4687 | return RewriteAtSelector(AtSelector); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4688 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4689 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 4690 | return RewriteObjCStringLiteral(AtString); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4691 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4692 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4693 | #if 0 |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4694 | // Before we rewrite it, put the original message expression in a comment. |
| 4695 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 4696 | SourceLocation endLoc = MessExpr->getLocEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4697 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4698 | const char *startBuf = SM->getCharacterData(startLoc); |
| 4699 | const char *endBuf = SM->getCharacterData(endLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4700 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4701 | std::string messString; |
| 4702 | messString += "// "; |
| 4703 | messString.append(startBuf, endBuf-startBuf+1); |
| 4704 | messString += "\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4705 | |
| 4706 | // FIXME: Missing definition of |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4707 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
| 4708 | // InsertText(startLoc, messString.c_str(), messString.size()); |
| 4709 | // Tried this, but it didn't work either... |
| 4710 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4711 | #endif |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4712 | return RewriteMessageExpr(MessExpr); |
| 4713 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4714 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4715 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 4716 | return RewriteObjCTryStmt(StmtTry); |
| 4717 | |
| 4718 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 4719 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 4720 | |
| 4721 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 4722 | return RewriteObjCThrowStmt(StmtThrow); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4723 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4724 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 4725 | return RewriteObjCProtocolExpr(ProtocolExp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4726 | |
| 4727 | if (ObjCForCollectionStmt *StmtForCollection = |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4728 | dyn_cast<ObjCForCollectionStmt>(S)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4729 | return RewriteObjCForCollectionStmt(StmtForCollection, |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4730 | OrigStmtRange.getEnd()); |
| 4731 | if (BreakStmt *StmtBreakStmt = |
| 4732 | dyn_cast<BreakStmt>(S)) |
| 4733 | return RewriteBreakStmt(StmtBreakStmt); |
| 4734 | if (ContinueStmt *StmtContinueStmt = |
| 4735 | dyn_cast<ContinueStmt>(S)) |
| 4736 | return RewriteContinueStmt(StmtContinueStmt); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4737 | |
| 4738 | // 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] | 4739 | // and cast exprs. |
| 4740 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
| 4741 | // FIXME: What we're doing here is modifying the type-specifier that |
| 4742 | // precedes the first Decl. In the future the DeclGroup should have |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4743 | // a separate type-specifier that we can rewrite. |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4744 | // NOTE: We need to avoid rewriting the DeclStmt if it is within |
| 4745 | // the context of an ObjCForCollectionStmt. For example: |
| 4746 | // NSArray *someArray; |
| 4747 | // for (id <FooProtocol> index in someArray) ; |
| 4748 | // This is because RewriteObjCForCollectionStmt() does textual rewriting |
| 4749 | // and it depends on the original text locations/positions. |
Fariborz Jahanian | 74405b0 | 2011-02-24 21:29:21 +0000 | [diff] [blame] | 4750 | if (Stmts.empty() || !IsDeclStmtInForeachHeader(DS)) |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4751 | RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4752 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4753 | // Blocks rewrite rules. |
Aaron Ballman | 535bbcc | 2014-03-14 17:01:24 +0000 | [diff] [blame] | 4754 | for (auto *SD : DS->decls()) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4755 | if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4756 | if (isTopLevelBlockPointerType(ND->getType())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4757 | RewriteBlockPointerDecl(ND); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4758 | else if (ND->getType()->isFunctionPointerType()) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4759 | CheckFunctionPointerDecl(ND->getType(), ND); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 4760 | if (VarDecl *VD = dyn_cast<VarDecl>(SD)) { |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4761 | if (VD->hasAttr<BlocksAttr>()) { |
| 4762 | static unsigned uniqueByrefDeclCount = 0; |
| 4763 | assert(!BlockByRefDeclNo.count(ND) && |
| 4764 | "RewriteFunctionBodyOrGlobalInitializer: Duplicate byref decl"); |
| 4765 | BlockByRefDeclNo[ND] = uniqueByrefDeclCount++; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4766 | RewriteByRefVar(VD); |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4767 | } |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 4768 | else |
| 4769 | RewriteTypeOfDecl(VD); |
| 4770 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4771 | } |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4772 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) { |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4773 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4774 | RewriteBlockPointerDecl(TD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4775 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4776 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 4777 | } |
| 4778 | } |
| 4779 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4780 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4781 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) |
| 4782 | RewriteObjCQualifiedInterfaceTypes(CE); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4783 | |
| 4784 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4785 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 4786 | assert(!Stmts.empty() && "Statement stack is empty"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4787 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 4788 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4789 | && "Statement stack mismatch"); |
| 4790 | Stmts.pop_back(); |
| 4791 | } |
| 4792 | // Handle blocks rewriting. |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4793 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 4794 | ValueDecl *VD = DRE->getDecl(); |
| 4795 | if (VD->hasAttr<BlocksAttr>()) |
| 4796 | return RewriteBlockDeclRefExpr(DRE); |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4797 | if (HasLocalVariableExternalStorage(VD)) |
| 4798 | return RewriteLocalVariableExternalStorage(DRE); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4799 | } |
| 4800 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4801 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4802 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4803 | Stmt *BlockCall = SynthesizeBlockCall(CE, CE->getCallee()); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4804 | ReplaceStmt(S, BlockCall); |
| 4805 | return BlockCall; |
| 4806 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4807 | } |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4808 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4809 | RewriteCastExpr(CE); |
| 4810 | } |
| 4811 | #if 0 |
| 4812 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4813 | CastExpr *Replacement = new (Context) CastExpr(ICE->getType(), |
| 4814 | ICE->getSubExpr(), |
| 4815 | SourceLocation()); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4816 | // Get the new text. |
| 4817 | std::string SStr; |
| 4818 | llvm::raw_string_ostream Buf(SStr); |
Richard Smith | 235341b | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 4819 | Replacement->printPretty(Buf); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4820 | const std::string &Str = Buf.str(); |
| 4821 | |
| 4822 | printf("CAST = %s\n", &Str[0]); |
| 4823 | InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 4824 | delete S; |
| 4825 | return Replacement; |
| 4826 | } |
| 4827 | #endif |
| 4828 | // Return this stmt unmodified. |
| 4829 | return S; |
| 4830 | } |
| 4831 | |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4832 | void RewriteObjC::RewriteRecordBody(RecordDecl *RD) { |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 4833 | for (auto *FD : RD->fields()) { |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4834 | if (isTopLevelBlockPointerType(FD->getType())) |
| 4835 | RewriteBlockPointerDecl(FD); |
| 4836 | if (FD->getType()->isObjCQualifiedIdType() || |
| 4837 | FD->getType()->isObjCQualifiedInterfaceType()) |
| 4838 | RewriteObjCQualifiedInterfaceTypes(FD); |
| 4839 | } |
| 4840 | } |
| 4841 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4842 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 4843 | /// main file of the input. |
| 4844 | void RewriteObjC::HandleDeclInMainFile(Decl *D) { |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4845 | switch (D->getKind()) { |
| 4846 | case Decl::Function: { |
| 4847 | FunctionDecl *FD = cast<FunctionDecl>(D); |
| 4848 | if (FD->isOverloadedOperator()) |
| 4849 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4850 | |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4851 | // Since function prototypes don't have ParmDecl's, we check the function |
| 4852 | // prototype. This enables us to rewrite function declarations and |
| 4853 | // definitions using the same code. |
| 4854 | RewriteBlocksInFunctionProtoType(FD->getType(), FD); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4855 | |
Argyrios Kyrtzidis | 75627ad | 2012-02-12 04:48:45 +0000 | [diff] [blame] | 4856 | if (!FD->isThisDeclarationADefinition()) |
| 4857 | break; |
| 4858 | |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4859 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
| 4860 | if (CompoundStmt *Body = dyn_cast_or_null<CompoundStmt>(FD->getBody())) { |
| 4861 | CurFunctionDef = FD; |
| 4862 | CurFunctionDeclToDeclareForBlock = FD; |
| 4863 | CurrentBody = Body; |
| 4864 | Body = |
| 4865 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 4866 | FD->setBody(Body); |
| 4867 | CurrentBody = 0; |
| 4868 | if (PropParentMap) { |
| 4869 | delete PropParentMap; |
| 4870 | PropParentMap = 0; |
| 4871 | } |
| 4872 | // This synthesizes and inserts the block "impl" struct, invoke function, |
| 4873 | // and any copy/dispose helper functions. |
| 4874 | InsertBlockLiteralsWithinFunction(FD); |
| 4875 | CurFunctionDef = 0; |
| 4876 | CurFunctionDeclToDeclareForBlock = 0; |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4877 | } |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4878 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4879 | } |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4880 | case Decl::ObjCMethod: { |
| 4881 | ObjCMethodDecl *MD = cast<ObjCMethodDecl>(D); |
| 4882 | if (CompoundStmt *Body = MD->getCompoundBody()) { |
| 4883 | CurMethodDef = MD; |
| 4884 | CurrentBody = Body; |
| 4885 | Body = |
| 4886 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 4887 | MD->setBody(Body); |
| 4888 | CurrentBody = 0; |
| 4889 | if (PropParentMap) { |
| 4890 | delete PropParentMap; |
| 4891 | PropParentMap = 0; |
| 4892 | } |
| 4893 | InsertBlockLiteralsWithinMethod(MD); |
| 4894 | CurMethodDef = 0; |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4895 | } |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4896 | break; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4897 | } |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4898 | case Decl::ObjCImplementation: { |
| 4899 | ObjCImplementationDecl *CI = cast<ObjCImplementationDecl>(D); |
| 4900 | ClassImplementation.push_back(CI); |
| 4901 | break; |
| 4902 | } |
| 4903 | case Decl::ObjCCategoryImpl: { |
| 4904 | ObjCCategoryImplDecl *CI = cast<ObjCCategoryImplDecl>(D); |
| 4905 | CategoryImplementation.push_back(CI); |
| 4906 | break; |
| 4907 | } |
| 4908 | case Decl::Var: { |
| 4909 | VarDecl *VD = cast<VarDecl>(D); |
| 4910 | RewriteObjCQualifiedInterfaceTypes(VD); |
| 4911 | if (isTopLevelBlockPointerType(VD->getType())) |
| 4912 | RewriteBlockPointerDecl(VD); |
| 4913 | else if (VD->getType()->isFunctionPointerType()) { |
| 4914 | CheckFunctionPointerDecl(VD->getType(), VD); |
| 4915 | if (VD->getInit()) { |
| 4916 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
| 4917 | RewriteCastExpr(CE); |
| 4918 | } |
| 4919 | } |
| 4920 | } else if (VD->getType()->isRecordType()) { |
| 4921 | RecordDecl *RD = VD->getType()->getAs<RecordType>()->getDecl(); |
| 4922 | if (RD->isCompleteDefinition()) |
| 4923 | RewriteRecordBody(RD); |
| 4924 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4925 | if (VD->getInit()) { |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4926 | GlobalVarDecl = VD; |
| 4927 | CurrentBody = VD->getInit(); |
| 4928 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
| 4929 | CurrentBody = 0; |
| 4930 | if (PropParentMap) { |
| 4931 | delete PropParentMap; |
| 4932 | PropParentMap = 0; |
| 4933 | } |
| 4934 | SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), VD->getName()); |
| 4935 | GlobalVarDecl = 0; |
| 4936 | |
| 4937 | // This is needed for blocks. |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4938 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4939 | RewriteCastExpr(CE); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4940 | } |
| 4941 | } |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4942 | break; |
| 4943 | } |
| 4944 | case Decl::TypeAlias: |
| 4945 | case Decl::Typedef: { |
| 4946 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
| 4947 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
| 4948 | RewriteBlockPointerDecl(TD); |
| 4949 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
| 4950 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 4951 | } |
| 4952 | break; |
| 4953 | } |
| 4954 | case Decl::CXXRecord: |
| 4955 | case Decl::Record: { |
| 4956 | RecordDecl *RD = cast<RecordDecl>(D); |
| 4957 | if (RD->isCompleteDefinition()) |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4958 | RewriteRecordBody(RD); |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4959 | break; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4960 | } |
Fariborz Jahanian | 7b18669 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4961 | default: |
| 4962 | break; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4963 | } |
| 4964 | // Nothing yet. |
| 4965 | } |
| 4966 | |
Chris Lattner | cf16983 | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 4967 | void RewriteObjC::HandleTranslationUnit(ASTContext &C) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4968 | if (Diags.hasErrorOccurred()) |
| 4969 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4970 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4971 | RewriteInclude(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4972 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4973 | // Here's a great place to add any extra declarations that may be needed. |
| 4974 | // Write out meta data for each @protocol(<expr>). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4975 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4976 | E = ProtocolExprDecls.end(); I != E; ++I) |
| 4977 | RewriteObjCProtocolMetaData(*I, "", "", Preamble); |
| 4978 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4979 | InsertText(SM->getLocForStartOfFile(MainFileID), Preamble, false); |
Steve Naroff | 2a2a41f | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 4980 | if (ClassImplementation.size() || CategoryImplementation.size()) |
| 4981 | RewriteImplementations(); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4982 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4983 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 4984 | // we are done. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4985 | if (const RewriteBuffer *RewriteBuf = |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4986 | Rewrite.getRewriteBufferFor(MainFileID)) { |
| 4987 | //printf("Changed:\n"); |
| 4988 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 4989 | } else { |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4990 | llvm::errs() << "No changes\n"; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4991 | } |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 4992 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4993 | if (ClassImplementation.size() || CategoryImplementation.size() || |
| 4994 | ProtocolExprDecls.size()) { |
Steve Naroff | 2a2a41f | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 4995 | // Rewrite Objective-c meta data* |
| 4996 | std::string ResultStr; |
Fariborz Jahanian | 68e628e | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 4997 | RewriteMetaDataIntoBuffer(ResultStr); |
Steve Naroff | 2a2a41f | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 4998 | // Emit metadata. |
| 4999 | *OutFile << ResultStr; |
| 5000 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5001 | OutFile->flush(); |
| 5002 | } |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5003 | |
| 5004 | void RewriteObjCFragileABI::Initialize(ASTContext &context) { |
| 5005 | InitializeCommon(context); |
| 5006 | |
| 5007 | // declaring objc_selector outside the parameter list removes a silly |
| 5008 | // scope related warning... |
| 5009 | if (IsHeader) |
| 5010 | Preamble = "#pragma once\n"; |
| 5011 | Preamble += "struct objc_selector; struct objc_class;\n"; |
| 5012 | Preamble += "struct __rw_objc_super { struct objc_object *object; "; |
| 5013 | Preamble += "struct objc_object *superClass; "; |
| 5014 | if (LangOpts.MicrosoftExt) { |
| 5015 | // Add a constructor for creating temporary objects. |
| 5016 | Preamble += "__rw_objc_super(struct objc_object *o, struct objc_object *s) " |
| 5017 | ": "; |
| 5018 | Preamble += "object(o), superClass(s) {} "; |
| 5019 | } |
| 5020 | Preamble += "};\n"; |
| 5021 | Preamble += "#ifndef _REWRITER_typedef_Protocol\n"; |
| 5022 | Preamble += "typedef struct objc_object Protocol;\n"; |
| 5023 | Preamble += "#define _REWRITER_typedef_Protocol\n"; |
| 5024 | Preamble += "#endif\n"; |
| 5025 | if (LangOpts.MicrosoftExt) { |
| 5026 | Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n"; |
| 5027 | Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n"; |
| 5028 | } else |
| 5029 | Preamble += "#define __OBJC_RW_DLLIMPORT extern\n"; |
| 5030 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend"; |
| 5031 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
| 5032 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper"; |
| 5033 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
| 5034 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object* objc_msgSend_stret"; |
| 5035 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
| 5036 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object* objc_msgSendSuper_stret"; |
| 5037 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
| 5038 | Preamble += "__OBJC_RW_DLLIMPORT double objc_msgSend_fpret"; |
| 5039 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
| 5040 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getClass"; |
| 5041 | Preamble += "(const char *);\n"; |
| 5042 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_class *class_getSuperclass"; |
| 5043 | Preamble += "(struct objc_class *);\n"; |
| 5044 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getMetaClass"; |
| 5045 | Preamble += "(const char *);\n"; |
| 5046 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw(struct objc_object *);\n"; |
| 5047 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_enter(void *);\n"; |
| 5048 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_exit(void *);\n"; |
| 5049 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_exception_extract(void *);\n"; |
| 5050 | Preamble += "__OBJC_RW_DLLIMPORT int objc_exception_match"; |
| 5051 | Preamble += "(struct objc_class *, struct objc_object *);\n"; |
| 5052 | // @synchronized hooks. |
Aaron Ballman | 9c00446 | 2012-09-06 16:44:16 +0000 | [diff] [blame] | 5053 | Preamble += "__OBJC_RW_DLLIMPORT int objc_sync_enter(struct objc_object *);\n"; |
| 5054 | Preamble += "__OBJC_RW_DLLIMPORT int objc_sync_exit(struct objc_object *);\n"; |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5055 | Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n"; |
| 5056 | Preamble += "#ifndef __FASTENUMERATIONSTATE\n"; |
| 5057 | Preamble += "struct __objcFastEnumerationState {\n\t"; |
| 5058 | Preamble += "unsigned long state;\n\t"; |
| 5059 | Preamble += "void **itemsPtr;\n\t"; |
| 5060 | Preamble += "unsigned long *mutationsPtr;\n\t"; |
| 5061 | Preamble += "unsigned long extra[5];\n};\n"; |
| 5062 | Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n"; |
| 5063 | Preamble += "#define __FASTENUMERATIONSTATE\n"; |
| 5064 | Preamble += "#endif\n"; |
| 5065 | Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n"; |
| 5066 | Preamble += "struct __NSConstantStringImpl {\n"; |
| 5067 | Preamble += " int *isa;\n"; |
| 5068 | Preamble += " int flags;\n"; |
| 5069 | Preamble += " char *str;\n"; |
| 5070 | Preamble += " long length;\n"; |
| 5071 | Preamble += "};\n"; |
| 5072 | Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n"; |
| 5073 | Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n"; |
| 5074 | Preamble += "#else\n"; |
| 5075 | Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n"; |
| 5076 | Preamble += "#endif\n"; |
| 5077 | Preamble += "#define __NSCONSTANTSTRINGIMPL\n"; |
| 5078 | Preamble += "#endif\n"; |
| 5079 | // Blocks preamble. |
| 5080 | Preamble += "#ifndef BLOCK_IMPL\n"; |
| 5081 | Preamble += "#define BLOCK_IMPL\n"; |
| 5082 | Preamble += "struct __block_impl {\n"; |
| 5083 | Preamble += " void *isa;\n"; |
| 5084 | Preamble += " int Flags;\n"; |
| 5085 | Preamble += " int Reserved;\n"; |
| 5086 | Preamble += " void *FuncPtr;\n"; |
| 5087 | Preamble += "};\n"; |
| 5088 | Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n"; |
| 5089 | Preamble += "#ifdef __OBJC_EXPORT_BLOCKS\n"; |
| 5090 | Preamble += "extern \"C\" __declspec(dllexport) " |
| 5091 | "void _Block_object_assign(void *, const void *, const int);\n"; |
| 5092 | Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_dispose(const void *, const int);\n"; |
| 5093 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteGlobalBlock[32];\n"; |
| 5094 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteStackBlock[32];\n"; |
| 5095 | Preamble += "#else\n"; |
| 5096 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_assign(void *, const void *, const int);\n"; |
| 5097 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_dispose(const void *, const int);\n"; |
| 5098 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteGlobalBlock[32];\n"; |
| 5099 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteStackBlock[32];\n"; |
| 5100 | Preamble += "#endif\n"; |
| 5101 | Preamble += "#endif\n"; |
| 5102 | if (LangOpts.MicrosoftExt) { |
| 5103 | Preamble += "#undef __OBJC_RW_DLLIMPORT\n"; |
| 5104 | Preamble += "#undef __OBJC_RW_STATICIMPORT\n"; |
| 5105 | Preamble += "#ifndef KEEP_ATTRIBUTES\n"; // We use this for clang tests. |
| 5106 | Preamble += "#define __attribute__(X)\n"; |
| 5107 | Preamble += "#endif\n"; |
| 5108 | Preamble += "#define __weak\n"; |
| 5109 | } |
| 5110 | else { |
| 5111 | Preamble += "#define __block\n"; |
| 5112 | Preamble += "#define __weak\n"; |
| 5113 | } |
| 5114 | // NOTE! Windows uses LLP64 for 64bit mode. So, cast pointer to long long |
| 5115 | // as this avoids warning in any 64bit/32bit compilation model. |
| 5116 | Preamble += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((long long) &((TYPE *)0)->MEMBER)\n"; |
| 5117 | } |
| 5118 | |
| 5119 | /// RewriteIvarOffsetComputation - This rutine synthesizes computation of |
| 5120 | /// ivar offset. |
| 5121 | void RewriteObjCFragileABI::RewriteIvarOffsetComputation(ObjCIvarDecl *ivar, |
| 5122 | std::string &Result) { |
| 5123 | if (ivar->isBitField()) { |
| 5124 | // FIXME: The hack below doesn't work for bitfields. For now, we simply |
| 5125 | // place all bitfields at offset 0. |
| 5126 | Result += "0"; |
| 5127 | } else { |
| 5128 | Result += "__OFFSETOFIVAR__(struct "; |
| 5129 | Result += ivar->getContainingInterface()->getNameAsString(); |
| 5130 | if (LangOpts.MicrosoftExt) |
| 5131 | Result += "_IMPL"; |
| 5132 | Result += ", "; |
| 5133 | Result += ivar->getNameAsString(); |
| 5134 | Result += ")"; |
| 5135 | } |
| 5136 | } |
| 5137 | |
| 5138 | /// RewriteObjCProtocolMetaData - Rewrite protocols meta-data. |
| 5139 | void RewriteObjCFragileABI::RewriteObjCProtocolMetaData( |
| 5140 | ObjCProtocolDecl *PDecl, StringRef prefix, |
| 5141 | StringRef ClassName, std::string &Result) { |
| 5142 | static bool objc_protocol_methods = false; |
| 5143 | |
| 5144 | // Output struct protocol_methods holder of method selector and type. |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 5145 | if (!objc_protocol_methods && PDecl->hasDefinition()) { |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5146 | /* struct protocol_methods { |
| 5147 | SEL _cmd; |
| 5148 | char *method_types; |
| 5149 | } |
| 5150 | */ |
| 5151 | Result += "\nstruct _protocol_methods {\n"; |
| 5152 | Result += "\tstruct objc_selector *_cmd;\n"; |
| 5153 | Result += "\tchar *method_types;\n"; |
| 5154 | Result += "};\n"; |
| 5155 | |
| 5156 | objc_protocol_methods = true; |
| 5157 | } |
| 5158 | // Do not synthesize the protocol more than once. |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 5159 | if (ObjCSynthesizedProtocols.count(PDecl->getCanonicalDecl())) |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5160 | return; |
| 5161 | |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 5162 | if (ObjCProtocolDecl *Def = PDecl->getDefinition()) |
| 5163 | PDecl = Def; |
| 5164 | |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5165 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 5166 | unsigned NumMethods = std::distance(PDecl->instmeth_begin(), |
| 5167 | PDecl->instmeth_end()); |
| 5168 | /* struct _objc_protocol_method_list { |
| 5169 | int protocol_method_count; |
| 5170 | struct protocol_methods protocols[]; |
| 5171 | } |
| 5172 | */ |
| 5173 | Result += "\nstatic struct {\n"; |
| 5174 | Result += "\tint protocol_method_count;\n"; |
| 5175 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 5176 | Result += utostr(NumMethods); |
| 5177 | Result += "];\n} _OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 5178 | Result += PDecl->getNameAsString(); |
| 5179 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= " |
| 5180 | "{\n\t" + utostr(NumMethods) + "\n"; |
| 5181 | |
| 5182 | // Output instance methods declared in this protocol. |
| 5183 | for (ObjCProtocolDecl::instmeth_iterator |
| 5184 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
| 5185 | I != E; ++I) { |
| 5186 | if (I == PDecl->instmeth_begin()) |
| 5187 | Result += "\t ,{{(struct objc_selector *)\""; |
| 5188 | else |
| 5189 | Result += "\t ,{(struct objc_selector *)\""; |
| 5190 | Result += (*I)->getSelector().getAsString(); |
| 5191 | std::string MethodTypeString; |
| 5192 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 5193 | Result += "\", \""; |
| 5194 | Result += MethodTypeString; |
| 5195 | Result += "\"}\n"; |
| 5196 | } |
| 5197 | Result += "\t }\n};\n"; |
| 5198 | } |
| 5199 | |
| 5200 | // Output class methods declared in this protocol. |
| 5201 | unsigned NumMethods = std::distance(PDecl->classmeth_begin(), |
| 5202 | PDecl->classmeth_end()); |
| 5203 | if (NumMethods > 0) { |
| 5204 | /* struct _objc_protocol_method_list { |
| 5205 | int protocol_method_count; |
| 5206 | struct protocol_methods protocols[]; |
| 5207 | } |
| 5208 | */ |
| 5209 | Result += "\nstatic struct {\n"; |
| 5210 | Result += "\tint protocol_method_count;\n"; |
| 5211 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 5212 | Result += utostr(NumMethods); |
| 5213 | Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_"; |
| 5214 | Result += PDecl->getNameAsString(); |
| 5215 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 5216 | "{\n\t"; |
| 5217 | Result += utostr(NumMethods); |
| 5218 | Result += "\n"; |
| 5219 | |
| 5220 | // Output instance methods declared in this protocol. |
| 5221 | for (ObjCProtocolDecl::classmeth_iterator |
| 5222 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
| 5223 | I != E; ++I) { |
| 5224 | if (I == PDecl->classmeth_begin()) |
| 5225 | Result += "\t ,{{(struct objc_selector *)\""; |
| 5226 | else |
| 5227 | Result += "\t ,{(struct objc_selector *)\""; |
| 5228 | Result += (*I)->getSelector().getAsString(); |
| 5229 | std::string MethodTypeString; |
| 5230 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 5231 | Result += "\", \""; |
| 5232 | Result += MethodTypeString; |
| 5233 | Result += "\"}\n"; |
| 5234 | } |
| 5235 | Result += "\t }\n};\n"; |
| 5236 | } |
| 5237 | |
| 5238 | // Output: |
| 5239 | /* struct _objc_protocol { |
| 5240 | // Objective-C 1.0 extensions |
| 5241 | struct _objc_protocol_extension *isa; |
| 5242 | char *protocol_name; |
| 5243 | struct _objc_protocol **protocol_list; |
| 5244 | struct _objc_protocol_method_list *instance_methods; |
| 5245 | struct _objc_protocol_method_list *class_methods; |
| 5246 | }; |
| 5247 | */ |
| 5248 | static bool objc_protocol = false; |
| 5249 | if (!objc_protocol) { |
| 5250 | Result += "\nstruct _objc_protocol {\n"; |
| 5251 | Result += "\tstruct _objc_protocol_extension *isa;\n"; |
| 5252 | Result += "\tchar *protocol_name;\n"; |
| 5253 | Result += "\tstruct _objc_protocol **protocol_list;\n"; |
| 5254 | Result += "\tstruct _objc_protocol_method_list *instance_methods;\n"; |
| 5255 | Result += "\tstruct _objc_protocol_method_list *class_methods;\n"; |
| 5256 | Result += "};\n"; |
| 5257 | |
| 5258 | objc_protocol = true; |
| 5259 | } |
| 5260 | |
| 5261 | Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_"; |
| 5262 | Result += PDecl->getNameAsString(); |
| 5263 | Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= " |
| 5264 | "{\n\t0, \""; |
| 5265 | Result += PDecl->getNameAsString(); |
| 5266 | Result += "\", 0, "; |
| 5267 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 5268 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 5269 | Result += PDecl->getNameAsString(); |
| 5270 | Result += ", "; |
| 5271 | } |
| 5272 | else |
| 5273 | Result += "0, "; |
| 5274 | if (PDecl->classmeth_begin() != PDecl->classmeth_end()) { |
| 5275 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
| 5276 | Result += PDecl->getNameAsString(); |
| 5277 | Result += "\n"; |
| 5278 | } |
| 5279 | else |
| 5280 | Result += "0\n"; |
| 5281 | Result += "};\n"; |
| 5282 | |
| 5283 | // Mark this protocol as having been generated. |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 5284 | if (!ObjCSynthesizedProtocols.insert(PDecl->getCanonicalDecl())) |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5285 | llvm_unreachable("protocol already synthesized"); |
| 5286 | |
| 5287 | } |
| 5288 | |
| 5289 | void RewriteObjCFragileABI::RewriteObjCProtocolListMetaData( |
| 5290 | const ObjCList<ObjCProtocolDecl> &Protocols, |
| 5291 | StringRef prefix, StringRef ClassName, |
| 5292 | std::string &Result) { |
| 5293 | if (Protocols.empty()) return; |
| 5294 | |
| 5295 | for (unsigned i = 0; i != Protocols.size(); i++) |
| 5296 | RewriteObjCProtocolMetaData(Protocols[i], prefix, ClassName, Result); |
| 5297 | |
| 5298 | // Output the top lovel protocol meta-data for the class. |
| 5299 | /* struct _objc_protocol_list { |
| 5300 | struct _objc_protocol_list *next; |
| 5301 | int protocol_count; |
| 5302 | struct _objc_protocol *class_protocols[]; |
| 5303 | } |
| 5304 | */ |
| 5305 | Result += "\nstatic struct {\n"; |
| 5306 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 5307 | Result += "\tint protocol_count;\n"; |
| 5308 | Result += "\tstruct _objc_protocol *class_protocols["; |
| 5309 | Result += utostr(Protocols.size()); |
| 5310 | Result += "];\n} _OBJC_"; |
| 5311 | Result += prefix; |
| 5312 | Result += "_PROTOCOLS_"; |
| 5313 | Result += ClassName; |
| 5314 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 5315 | "{\n\t0, "; |
| 5316 | Result += utostr(Protocols.size()); |
| 5317 | Result += "\n"; |
| 5318 | |
| 5319 | Result += "\t,{&_OBJC_PROTOCOL_"; |
| 5320 | Result += Protocols[0]->getNameAsString(); |
| 5321 | Result += " \n"; |
| 5322 | |
| 5323 | for (unsigned i = 1; i != Protocols.size(); i++) { |
| 5324 | Result += "\t ,&_OBJC_PROTOCOL_"; |
| 5325 | Result += Protocols[i]->getNameAsString(); |
| 5326 | Result += "\n"; |
| 5327 | } |
| 5328 | Result += "\t }\n};\n"; |
| 5329 | } |
| 5330 | |
| 5331 | void RewriteObjCFragileABI::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
| 5332 | std::string &Result) { |
| 5333 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
| 5334 | |
| 5335 | // Explicitly declared @interface's are already synthesized. |
| 5336 | if (CDecl->isImplicitInterfaceDecl()) { |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 5337 | // FIXME: Implementation of a class with no @interface (legacy) does not |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5338 | // produce correct synthesis as yet. |
| 5339 | RewriteObjCInternalStruct(CDecl, Result); |
| 5340 | } |
| 5341 | |
| 5342 | // Build _objc_ivar_list metadata for classes ivars if needed |
| 5343 | unsigned NumIvars = !IDecl->ivar_empty() |
| 5344 | ? IDecl->ivar_size() |
| 5345 | : (CDecl ? CDecl->ivar_size() : 0); |
| 5346 | if (NumIvars > 0) { |
| 5347 | static bool objc_ivar = false; |
| 5348 | if (!objc_ivar) { |
| 5349 | /* struct _objc_ivar { |
| 5350 | char *ivar_name; |
| 5351 | char *ivar_type; |
| 5352 | int ivar_offset; |
| 5353 | }; |
| 5354 | */ |
| 5355 | Result += "\nstruct _objc_ivar {\n"; |
| 5356 | Result += "\tchar *ivar_name;\n"; |
| 5357 | Result += "\tchar *ivar_type;\n"; |
| 5358 | Result += "\tint ivar_offset;\n"; |
| 5359 | Result += "};\n"; |
| 5360 | |
| 5361 | objc_ivar = true; |
| 5362 | } |
| 5363 | |
| 5364 | /* struct { |
| 5365 | int ivar_count; |
| 5366 | struct _objc_ivar ivar_list[nIvars]; |
| 5367 | }; |
| 5368 | */ |
| 5369 | Result += "\nstatic struct {\n"; |
| 5370 | Result += "\tint ivar_count;\n"; |
| 5371 | Result += "\tstruct _objc_ivar ivar_list["; |
| 5372 | Result += utostr(NumIvars); |
| 5373 | Result += "];\n} _OBJC_INSTANCE_VARIABLES_"; |
| 5374 | Result += IDecl->getNameAsString(); |
| 5375 | Result += " __attribute__ ((used, section (\"__OBJC, __instance_vars\")))= " |
| 5376 | "{\n\t"; |
| 5377 | Result += utostr(NumIvars); |
| 5378 | Result += "\n"; |
| 5379 | |
| 5380 | ObjCInterfaceDecl::ivar_iterator IVI, IVE; |
| 5381 | SmallVector<ObjCIvarDecl *, 8> IVars; |
| 5382 | if (!IDecl->ivar_empty()) { |
Aaron Ballman | d6d25de | 2014-03-14 15:16:45 +0000 | [diff] [blame] | 5383 | for (auto *IV : IDecl->ivars()) |
| 5384 | IVars.push_back(IV); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5385 | IVI = IDecl->ivar_begin(); |
| 5386 | IVE = IDecl->ivar_end(); |
| 5387 | } else { |
| 5388 | IVI = CDecl->ivar_begin(); |
| 5389 | IVE = CDecl->ivar_end(); |
| 5390 | } |
| 5391 | Result += "\t,{{\""; |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5392 | Result += IVI->getNameAsString(); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5393 | Result += "\", \""; |
| 5394 | std::string TmpString, StrEncoding; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5395 | Context->getObjCEncodingForType(IVI->getType(), TmpString, *IVI); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5396 | QuoteDoublequotes(TmpString, StrEncoding); |
| 5397 | Result += StrEncoding; |
| 5398 | Result += "\", "; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5399 | RewriteIvarOffsetComputation(*IVI, Result); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5400 | Result += "}\n"; |
| 5401 | for (++IVI; IVI != IVE; ++IVI) { |
| 5402 | Result += "\t ,{\""; |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5403 | Result += IVI->getNameAsString(); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5404 | Result += "\", \""; |
| 5405 | std::string TmpString, StrEncoding; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5406 | Context->getObjCEncodingForType(IVI->getType(), TmpString, *IVI); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5407 | QuoteDoublequotes(TmpString, StrEncoding); |
| 5408 | Result += StrEncoding; |
| 5409 | Result += "\", "; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5410 | RewriteIvarOffsetComputation(*IVI, Result); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5411 | Result += "}\n"; |
| 5412 | } |
| 5413 | |
| 5414 | Result += "\t }\n};\n"; |
| 5415 | } |
| 5416 | |
| 5417 | // Build _objc_method_list for class's instance methods if needed |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 5418 | SmallVector<ObjCMethodDecl *, 32> InstanceMethods(IDecl->instance_methods()); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5419 | |
| 5420 | // If any of our property implementations have associated getters or |
| 5421 | // setters, produce metadata for them as well. |
Aaron Ballman | d85eff4 | 2014-03-14 15:02:45 +0000 | [diff] [blame] | 5422 | for (const auto *Prop : IDecl->property_impls()) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5423 | if (Prop->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5424 | continue; |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5425 | if (!Prop->getPropertyIvarDecl()) |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5426 | continue; |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5427 | ObjCPropertyDecl *PD = Prop->getPropertyDecl(); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5428 | if (!PD) |
| 5429 | continue; |
| 5430 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 5431 | if (!Getter->isDefined()) |
| 5432 | InstanceMethods.push_back(Getter); |
| 5433 | if (PD->isReadOnly()) |
| 5434 | continue; |
| 5435 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 5436 | if (!Setter->isDefined()) |
| 5437 | InstanceMethods.push_back(Setter); |
| 5438 | } |
| 5439 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
| 5440 | true, "", IDecl->getName(), Result); |
| 5441 | |
| 5442 | // Build _objc_method_list for class's class methods if needed |
| 5443 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
| 5444 | false, "", IDecl->getName(), Result); |
| 5445 | |
| 5446 | // Protocols referenced in class declaration? |
| 5447 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), |
| 5448 | "CLASS", CDecl->getName(), Result); |
| 5449 | |
| 5450 | // Declaration of class/meta-class metadata |
| 5451 | /* struct _objc_class { |
| 5452 | struct _objc_class *isa; // or const char *root_class_name when metadata |
| 5453 | const char *super_class_name; |
| 5454 | char *name; |
| 5455 | long version; |
| 5456 | long info; |
| 5457 | long instance_size; |
| 5458 | struct _objc_ivar_list *ivars; |
| 5459 | struct _objc_method_list *methods; |
| 5460 | struct objc_cache *cache; |
| 5461 | struct objc_protocol_list *protocols; |
| 5462 | const char *ivar_layout; |
| 5463 | struct _objc_class_ext *ext; |
| 5464 | }; |
| 5465 | */ |
| 5466 | static bool objc_class = false; |
| 5467 | if (!objc_class) { |
| 5468 | Result += "\nstruct _objc_class {\n"; |
| 5469 | Result += "\tstruct _objc_class *isa;\n"; |
| 5470 | Result += "\tconst char *super_class_name;\n"; |
| 5471 | Result += "\tchar *name;\n"; |
| 5472 | Result += "\tlong version;\n"; |
| 5473 | Result += "\tlong info;\n"; |
| 5474 | Result += "\tlong instance_size;\n"; |
| 5475 | Result += "\tstruct _objc_ivar_list *ivars;\n"; |
| 5476 | Result += "\tstruct _objc_method_list *methods;\n"; |
| 5477 | Result += "\tstruct objc_cache *cache;\n"; |
| 5478 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 5479 | Result += "\tconst char *ivar_layout;\n"; |
| 5480 | Result += "\tstruct _objc_class_ext *ext;\n"; |
| 5481 | Result += "};\n"; |
| 5482 | objc_class = true; |
| 5483 | } |
| 5484 | |
| 5485 | // Meta-class metadata generation. |
| 5486 | ObjCInterfaceDecl *RootClass = 0; |
| 5487 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
| 5488 | while (SuperClass) { |
| 5489 | RootClass = SuperClass; |
| 5490 | SuperClass = SuperClass->getSuperClass(); |
| 5491 | } |
| 5492 | SuperClass = CDecl->getSuperClass(); |
| 5493 | |
| 5494 | Result += "\nstatic struct _objc_class _OBJC_METACLASS_"; |
| 5495 | Result += CDecl->getNameAsString(); |
| 5496 | Result += " __attribute__ ((used, section (\"__OBJC, __meta_class\")))= " |
| 5497 | "{\n\t(struct _objc_class *)\""; |
| 5498 | Result += (RootClass ? RootClass->getNameAsString() : CDecl->getNameAsString()); |
| 5499 | Result += "\""; |
| 5500 | |
| 5501 | if (SuperClass) { |
| 5502 | Result += ", \""; |
| 5503 | Result += SuperClass->getNameAsString(); |
| 5504 | Result += "\", \""; |
| 5505 | Result += CDecl->getNameAsString(); |
| 5506 | Result += "\""; |
| 5507 | } |
| 5508 | else { |
| 5509 | Result += ", 0, \""; |
| 5510 | Result += CDecl->getNameAsString(); |
| 5511 | Result += "\""; |
| 5512 | } |
| 5513 | // Set 'ivars' field for root class to 0. ObjC1 runtime does not use it. |
| 5514 | // 'info' field is initialized to CLS_META(2) for metaclass |
| 5515 | Result += ", 0,2, sizeof(struct _objc_class), 0"; |
| 5516 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
| 5517 | Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_"; |
| 5518 | Result += IDecl->getNameAsString(); |
| 5519 | Result += "\n"; |
| 5520 | } |
| 5521 | else |
| 5522 | Result += ", 0\n"; |
| 5523 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
| 5524 | Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_"; |
| 5525 | Result += CDecl->getNameAsString(); |
| 5526 | Result += ",0,0\n"; |
| 5527 | } |
| 5528 | else |
| 5529 | Result += "\t,0,0,0,0\n"; |
| 5530 | Result += "};\n"; |
| 5531 | |
| 5532 | // class metadata generation. |
| 5533 | Result += "\nstatic struct _objc_class _OBJC_CLASS_"; |
| 5534 | Result += CDecl->getNameAsString(); |
| 5535 | Result += " __attribute__ ((used, section (\"__OBJC, __class\")))= " |
| 5536 | "{\n\t&_OBJC_METACLASS_"; |
| 5537 | Result += CDecl->getNameAsString(); |
| 5538 | if (SuperClass) { |
| 5539 | Result += ", \""; |
| 5540 | Result += SuperClass->getNameAsString(); |
| 5541 | Result += "\", \""; |
| 5542 | Result += CDecl->getNameAsString(); |
| 5543 | Result += "\""; |
| 5544 | } |
| 5545 | else { |
| 5546 | Result += ", 0, \""; |
| 5547 | Result += CDecl->getNameAsString(); |
| 5548 | Result += "\""; |
| 5549 | } |
| 5550 | // 'info' field is initialized to CLS_CLASS(1) for class |
| 5551 | Result += ", 0,1"; |
| 5552 | if (!ObjCSynthesizedStructs.count(CDecl)) |
| 5553 | Result += ",0"; |
| 5554 | else { |
| 5555 | // class has size. Must synthesize its size. |
| 5556 | Result += ",sizeof(struct "; |
| 5557 | Result += CDecl->getNameAsString(); |
| 5558 | if (LangOpts.MicrosoftExt) |
| 5559 | Result += "_IMPL"; |
| 5560 | Result += ")"; |
| 5561 | } |
| 5562 | if (NumIvars > 0) { |
| 5563 | Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_"; |
| 5564 | Result += CDecl->getNameAsString(); |
| 5565 | Result += "\n\t"; |
| 5566 | } |
| 5567 | else |
| 5568 | Result += ",0"; |
| 5569 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
| 5570 | Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_"; |
| 5571 | Result += CDecl->getNameAsString(); |
| 5572 | Result += ", 0\n\t"; |
| 5573 | } |
| 5574 | else |
| 5575 | Result += ",0,0"; |
| 5576 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
| 5577 | Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_"; |
| 5578 | Result += CDecl->getNameAsString(); |
| 5579 | Result += ", 0,0\n"; |
| 5580 | } |
| 5581 | else |
| 5582 | Result += ",0,0,0\n"; |
| 5583 | Result += "};\n"; |
| 5584 | } |
| 5585 | |
| 5586 | void RewriteObjCFragileABI::RewriteMetaDataIntoBuffer(std::string &Result) { |
| 5587 | int ClsDefCount = ClassImplementation.size(); |
| 5588 | int CatDefCount = CategoryImplementation.size(); |
| 5589 | |
| 5590 | // For each implemented class, write out all its meta data. |
| 5591 | for (int i = 0; i < ClsDefCount; i++) |
| 5592 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
| 5593 | |
| 5594 | // For each implemented category, write out all its meta data. |
| 5595 | for (int i = 0; i < CatDefCount; i++) |
| 5596 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
| 5597 | |
| 5598 | // Write objc_symtab metadata |
| 5599 | /* |
| 5600 | struct _objc_symtab |
| 5601 | { |
| 5602 | long sel_ref_cnt; |
| 5603 | SEL *refs; |
| 5604 | short cls_def_cnt; |
| 5605 | short cat_def_cnt; |
| 5606 | void *defs[cls_def_cnt + cat_def_cnt]; |
| 5607 | }; |
| 5608 | */ |
| 5609 | |
| 5610 | Result += "\nstruct _objc_symtab {\n"; |
| 5611 | Result += "\tlong sel_ref_cnt;\n"; |
| 5612 | Result += "\tSEL *refs;\n"; |
| 5613 | Result += "\tshort cls_def_cnt;\n"; |
| 5614 | Result += "\tshort cat_def_cnt;\n"; |
| 5615 | Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n"; |
| 5616 | Result += "};\n\n"; |
| 5617 | |
| 5618 | Result += "static struct _objc_symtab " |
| 5619 | "_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n"; |
| 5620 | Result += "\t0, 0, " + utostr(ClsDefCount) |
| 5621 | + ", " + utostr(CatDefCount) + "\n"; |
| 5622 | for (int i = 0; i < ClsDefCount; i++) { |
| 5623 | Result += "\t,&_OBJC_CLASS_"; |
| 5624 | Result += ClassImplementation[i]->getNameAsString(); |
| 5625 | Result += "\n"; |
| 5626 | } |
| 5627 | |
| 5628 | for (int i = 0; i < CatDefCount; i++) { |
| 5629 | Result += "\t,&_OBJC_CATEGORY_"; |
| 5630 | Result += CategoryImplementation[i]->getClassInterface()->getNameAsString(); |
| 5631 | Result += "_"; |
| 5632 | Result += CategoryImplementation[i]->getNameAsString(); |
| 5633 | Result += "\n"; |
| 5634 | } |
| 5635 | |
| 5636 | Result += "};\n\n"; |
| 5637 | |
| 5638 | // Write objc_module metadata |
| 5639 | |
| 5640 | /* |
| 5641 | struct _objc_module { |
| 5642 | long version; |
| 5643 | long size; |
| 5644 | const char *name; |
| 5645 | struct _objc_symtab *symtab; |
| 5646 | } |
| 5647 | */ |
| 5648 | |
| 5649 | Result += "\nstruct _objc_module {\n"; |
| 5650 | Result += "\tlong version;\n"; |
| 5651 | Result += "\tlong size;\n"; |
| 5652 | Result += "\tconst char *name;\n"; |
| 5653 | Result += "\tstruct _objc_symtab *symtab;\n"; |
| 5654 | Result += "};\n\n"; |
| 5655 | Result += "static struct _objc_module " |
| 5656 | "_OBJC_MODULES __attribute__ ((used, section (\"__OBJC, __module_info\")))= {\n"; |
| 5657 | Result += "\t" + utostr(OBJC_ABI_VERSION) + |
| 5658 | ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n"; |
| 5659 | Result += "};\n\n"; |
| 5660 | |
| 5661 | if (LangOpts.MicrosoftExt) { |
| 5662 | if (ProtocolExprDecls.size()) { |
| 5663 | Result += "#pragma section(\".objc_protocol$B\",long,read,write)\n"; |
| 5664 | Result += "#pragma data_seg(push, \".objc_protocol$B\")\n"; |
| 5665 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
| 5666 | E = ProtocolExprDecls.end(); I != E; ++I) { |
| 5667 | Result += "static struct _objc_protocol *_POINTER_OBJC_PROTOCOL_"; |
| 5668 | Result += (*I)->getNameAsString(); |
| 5669 | Result += " = &_OBJC_PROTOCOL_"; |
| 5670 | Result += (*I)->getNameAsString(); |
| 5671 | Result += ";\n"; |
| 5672 | } |
| 5673 | Result += "#pragma data_seg(pop)\n\n"; |
| 5674 | } |
| 5675 | Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n"; |
| 5676 | Result += "#pragma data_seg(push, \".objc_module_info$B\")\n"; |
| 5677 | Result += "static struct _objc_module *_POINTER_OBJC_MODULES = "; |
| 5678 | Result += "&_OBJC_MODULES;\n"; |
| 5679 | Result += "#pragma data_seg(pop)\n\n"; |
| 5680 | } |
| 5681 | } |
| 5682 | |
| 5683 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
| 5684 | /// implementation. |
| 5685 | void RewriteObjCFragileABI::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
| 5686 | std::string &Result) { |
| 5687 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
| 5688 | // Find category declaration for this implementation. |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 5689 | ObjCCategoryDecl *CDecl |
| 5690 | = ClassDecl->FindCategoryDeclaration(IDecl->getIdentifier()); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5691 | |
| 5692 | std::string FullCategoryName = ClassDecl->getNameAsString(); |
| 5693 | FullCategoryName += '_'; |
| 5694 | FullCategoryName += IDecl->getNameAsString(); |
| 5695 | |
| 5696 | // Build _objc_method_list for class's instance methods if needed |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 5697 | SmallVector<ObjCMethodDecl *, 32> InstanceMethods(IDecl->instance_methods()); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5698 | |
| 5699 | // If any of our property implementations have associated getters or |
| 5700 | // setters, produce metadata for them as well. |
Aaron Ballman | d85eff4 | 2014-03-14 15:02:45 +0000 | [diff] [blame] | 5701 | for (const auto *Prop : IDecl->property_impls()) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5702 | if (Prop->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5703 | continue; |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5704 | if (!Prop->getPropertyIvarDecl()) |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5705 | continue; |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5706 | ObjCPropertyDecl *PD = Prop->getPropertyDecl(); |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5707 | if (!PD) |
| 5708 | continue; |
| 5709 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 5710 | InstanceMethods.push_back(Getter); |
| 5711 | if (PD->isReadOnly()) |
| 5712 | continue; |
| 5713 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 5714 | InstanceMethods.push_back(Setter); |
| 5715 | } |
| 5716 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
| 5717 | true, "CATEGORY_", FullCategoryName.c_str(), |
| 5718 | Result); |
| 5719 | |
| 5720 | // Build _objc_method_list for class's class methods if needed |
| 5721 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
| 5722 | false, "CATEGORY_", FullCategoryName.c_str(), |
| 5723 | Result); |
| 5724 | |
| 5725 | // Protocols referenced in class declaration? |
| 5726 | // Null CDecl is case of a category implementation with no category interface |
| 5727 | if (CDecl) |
| 5728 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), "CATEGORY", |
| 5729 | FullCategoryName, Result); |
| 5730 | /* struct _objc_category { |
| 5731 | char *category_name; |
| 5732 | char *class_name; |
| 5733 | struct _objc_method_list *instance_methods; |
| 5734 | struct _objc_method_list *class_methods; |
| 5735 | struct _objc_protocol_list *protocols; |
| 5736 | // Objective-C 1.0 extensions |
| 5737 | uint32_t size; // sizeof (struct _objc_category) |
| 5738 | struct _objc_property_list *instance_properties; // category's own |
| 5739 | // @property decl. |
| 5740 | }; |
| 5741 | */ |
| 5742 | |
| 5743 | static bool objc_category = false; |
| 5744 | if (!objc_category) { |
| 5745 | Result += "\nstruct _objc_category {\n"; |
| 5746 | Result += "\tchar *category_name;\n"; |
| 5747 | Result += "\tchar *class_name;\n"; |
| 5748 | Result += "\tstruct _objc_method_list *instance_methods;\n"; |
| 5749 | Result += "\tstruct _objc_method_list *class_methods;\n"; |
| 5750 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 5751 | Result += "\tunsigned int size;\n"; |
| 5752 | Result += "\tstruct _objc_property_list *instance_properties;\n"; |
| 5753 | Result += "};\n"; |
| 5754 | objc_category = true; |
| 5755 | } |
| 5756 | Result += "\nstatic struct _objc_category _OBJC_CATEGORY_"; |
| 5757 | Result += FullCategoryName; |
| 5758 | Result += " __attribute__ ((used, section (\"__OBJC, __category\")))= {\n\t\""; |
| 5759 | Result += IDecl->getNameAsString(); |
| 5760 | Result += "\"\n\t, \""; |
| 5761 | Result += ClassDecl->getNameAsString(); |
| 5762 | Result += "\"\n"; |
| 5763 | |
| 5764 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
| 5765 | Result += "\t, (struct _objc_method_list *)" |
| 5766 | "&_OBJC_CATEGORY_INSTANCE_METHODS_"; |
| 5767 | Result += FullCategoryName; |
| 5768 | Result += "\n"; |
| 5769 | } |
| 5770 | else |
| 5771 | Result += "\t, 0\n"; |
| 5772 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
| 5773 | Result += "\t, (struct _objc_method_list *)" |
| 5774 | "&_OBJC_CATEGORY_CLASS_METHODS_"; |
| 5775 | Result += FullCategoryName; |
| 5776 | Result += "\n"; |
| 5777 | } |
| 5778 | else |
| 5779 | Result += "\t, 0\n"; |
| 5780 | |
| 5781 | if (CDecl && CDecl->protocol_begin() != CDecl->protocol_end()) { |
| 5782 | Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_"; |
| 5783 | Result += FullCategoryName; |
| 5784 | Result += "\n"; |
| 5785 | } |
| 5786 | else |
| 5787 | Result += "\t, 0\n"; |
| 5788 | Result += "\t, sizeof(struct _objc_category), 0\n};\n"; |
| 5789 | } |
| 5790 | |
| 5791 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
| 5792 | /// class methods. |
| 5793 | template<typename MethodIterator> |
| 5794 | void RewriteObjCFragileABI::RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 5795 | MethodIterator MethodEnd, |
| 5796 | bool IsInstanceMethod, |
| 5797 | StringRef prefix, |
| 5798 | StringRef ClassName, |
| 5799 | std::string &Result) { |
| 5800 | if (MethodBegin == MethodEnd) return; |
| 5801 | |
| 5802 | if (!objc_impl_method) { |
| 5803 | /* struct _objc_method { |
| 5804 | SEL _cmd; |
| 5805 | char *method_types; |
| 5806 | void *_imp; |
| 5807 | } |
| 5808 | */ |
| 5809 | Result += "\nstruct _objc_method {\n"; |
| 5810 | Result += "\tSEL _cmd;\n"; |
| 5811 | Result += "\tchar *method_types;\n"; |
| 5812 | Result += "\tvoid *_imp;\n"; |
| 5813 | Result += "};\n"; |
| 5814 | |
| 5815 | objc_impl_method = true; |
| 5816 | } |
| 5817 | |
| 5818 | // Build _objc_method_list for class's methods if needed |
| 5819 | |
| 5820 | /* struct { |
| 5821 | struct _objc_method_list *next_method; |
| 5822 | int method_count; |
| 5823 | struct _objc_method method_list[]; |
| 5824 | } |
| 5825 | */ |
| 5826 | unsigned NumMethods = std::distance(MethodBegin, MethodEnd); |
| 5827 | Result += "\nstatic struct {\n"; |
| 5828 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 5829 | Result += "\tint method_count;\n"; |
| 5830 | Result += "\tstruct _objc_method method_list["; |
| 5831 | Result += utostr(NumMethods); |
| 5832 | Result += "];\n} _OBJC_"; |
| 5833 | Result += prefix; |
| 5834 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 5835 | Result += "_METHODS_"; |
| 5836 | Result += ClassName; |
| 5837 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
| 5838 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 5839 | Result += "_meth\")))= "; |
| 5840 | Result += "{\n\t0, " + utostr(NumMethods) + "\n"; |
| 5841 | |
| 5842 | Result += "\t,{{(SEL)\""; |
| 5843 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
| 5844 | std::string MethodTypeString; |
| 5845 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
| 5846 | Result += "\", \""; |
| 5847 | Result += MethodTypeString; |
| 5848 | Result += "\", (void *)"; |
| 5849 | Result += MethodInternalNames[*MethodBegin]; |
| 5850 | Result += "}\n"; |
| 5851 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 5852 | Result += "\t ,{(SEL)\""; |
| 5853 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
| 5854 | std::string MethodTypeString; |
| 5855 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
| 5856 | Result += "\", \""; |
| 5857 | Result += MethodTypeString; |
| 5858 | Result += "\", (void *)"; |
| 5859 | Result += MethodInternalNames[*MethodBegin]; |
| 5860 | Result += "}\n"; |
| 5861 | } |
| 5862 | Result += "\t }\n};\n"; |
| 5863 | } |
| 5864 | |
| 5865 | Stmt *RewriteObjCFragileABI::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV) { |
| 5866 | SourceRange OldRange = IV->getSourceRange(); |
| 5867 | Expr *BaseExpr = IV->getBase(); |
| 5868 | |
| 5869 | // Rewrite the base, but without actually doing replaces. |
| 5870 | { |
| 5871 | DisableReplaceStmtScope S(*this); |
| 5872 | BaseExpr = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(BaseExpr)); |
| 5873 | IV->setBase(BaseExpr); |
| 5874 | } |
| 5875 | |
| 5876 | ObjCIvarDecl *D = IV->getDecl(); |
| 5877 | |
| 5878 | Expr *Replacement = IV; |
| 5879 | if (CurMethodDef) { |
| 5880 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
| 5881 | const ObjCInterfaceType *iFaceDecl = |
| 5882 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
| 5883 | assert(iFaceDecl && "RewriteObjCIvarRefExpr - iFaceDecl is null"); |
| 5884 | // lookup which class implements the instance variable. |
| 5885 | ObjCInterfaceDecl *clsDeclared = 0; |
| 5886 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
| 5887 | clsDeclared); |
| 5888 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
| 5889 | |
| 5890 | // Synthesize an explicit cast to gain access to the ivar. |
| 5891 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 5892 | RecName += "_IMPL"; |
| 5893 | IdentifierInfo *II = &Context->Idents.get(RecName); |
| 5894 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 5895 | SourceLocation(), SourceLocation(), |
| 5896 | II); |
| 5897 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 5898 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 5899 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, |
| 5900 | CK_BitCast, |
| 5901 | IV->getBase()); |
| 5902 | // Don't forget the parens to enforce the proper binding. |
| 5903 | ParenExpr *PE = new (Context) ParenExpr(OldRange.getBegin(), |
| 5904 | OldRange.getEnd(), |
| 5905 | castExpr); |
| 5906 | if (IV->isFreeIvar() && |
Douglas Gregor | 0b144e1 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 5907 | declaresSameEntity(CurMethodDef->getClassInterface(), iFaceDecl->getDecl())) { |
Fariborz Jahanian | 8307742 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5908 | MemberExpr *ME = new (Context) MemberExpr(PE, true, D, |
| 5909 | IV->getLocation(), |
| 5910 | D->getType(), |
| 5911 | VK_LValue, OK_Ordinary); |
| 5912 | Replacement = ME; |
| 5913 | } else { |
| 5914 | IV->setBase(PE); |
| 5915 | } |
| 5916 | } |
| 5917 | } else { // we are outside a method. |
| 5918 | assert(!IV->isFreeIvar() && "Cannot have a free standing ivar outside a method"); |
| 5919 | |
| 5920 | // Explicit ivar refs need to have a cast inserted. |
| 5921 | // FIXME: consider sharing some of this code with the code above. |
| 5922 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
| 5923 | const ObjCInterfaceType *iFaceDecl = |
| 5924 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
| 5925 | // lookup which class implements the instance variable. |
| 5926 | ObjCInterfaceDecl *clsDeclared = 0; |
| 5927 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
| 5928 | clsDeclared); |
| 5929 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
| 5930 | |
| 5931 | // Synthesize an explicit cast to gain access to the ivar. |
| 5932 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 5933 | RecName += "_IMPL"; |
| 5934 | IdentifierInfo *II = &Context->Idents.get(RecName); |
| 5935 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 5936 | SourceLocation(), SourceLocation(), |
| 5937 | II); |
| 5938 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 5939 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 5940 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, |
| 5941 | CK_BitCast, |
| 5942 | IV->getBase()); |
| 5943 | // Don't forget the parens to enforce the proper binding. |
| 5944 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
| 5945 | IV->getBase()->getLocEnd(), castExpr); |
| 5946 | // Cannot delete IV->getBase(), since PE points to it. |
| 5947 | // Replace the old base with the cast. This is important when doing |
| 5948 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
| 5949 | IV->setBase(PE); |
| 5950 | } |
| 5951 | } |
| 5952 | |
| 5953 | ReplaceStmtWithRange(IV, Replacement, OldRange); |
| 5954 | return Replacement; |
| 5955 | } |