Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1 | //===--- RewriteObjC.cpp - Playground for the code rewriter ---------------===// |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 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 | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Hacks and fun related to the code rewriter. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Daniel Dunbar | 9b414d3 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 14 | #include "clang/Rewrite/ASTConsumers.h" |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 15 | #include "clang/Rewrite/Rewriter.h" |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 16 | #include "clang/AST/AST.h" |
| 17 | #include "clang/AST/ASTConsumer.h" |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 18 | #include "clang/AST/ParentMap.h" |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 19 | #include "clang/Basic/SourceManager.h" |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 20 | #include "clang/Basic/IdentifierTable.h" |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 21 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | 26de465 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 22 | #include "clang/Lex/Lexer.h" |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MemoryBuffer.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringExtras.h" |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallPtrSet.h" |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/OwningPtr.h" |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/DenseSet.h" |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 30 | using namespace clang; |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 31 | using llvm::utostr; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 32 | |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 33 | namespace { |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 34 | class RewriteObjC : public ASTConsumer { |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 35 | protected: |
| 36 | |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 37 | enum { |
Nico Weber | 59b173d | 2010-11-22 10:26:41 +0000 | [diff] [blame] | 38 | BLOCK_FIELD_IS_OBJECT = 3, /* id, NSObject, __attribute__((NSObject)), |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 39 | block, ... */ |
| 40 | BLOCK_FIELD_IS_BLOCK = 7, /* a block variable */ |
| 41 | BLOCK_FIELD_IS_BYREF = 8, /* the on stack structure holding the |
| 42 | __block variable */ |
Nico Weber | 59b173d | 2010-11-22 10:26:41 +0000 | [diff] [blame] | 43 | BLOCK_FIELD_IS_WEAK = 16, /* declared __weak, only used in byref copy |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 44 | helpers */ |
Nico Weber | 59b173d | 2010-11-22 10:26:41 +0000 | [diff] [blame] | 45 | BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 46 | support routines */ |
| 47 | BLOCK_BYREF_CURRENT_MAX = 256 |
| 48 | }; |
| 49 | |
| 50 | enum { |
| 51 | BLOCK_NEEDS_FREE = (1 << 24), |
| 52 | BLOCK_HAS_COPY_DISPOSE = (1 << 25), |
| 53 | BLOCK_HAS_CXX_OBJ = (1 << 26), |
| 54 | BLOCK_IS_GC = (1 << 27), |
| 55 | BLOCK_IS_GLOBAL = (1 << 28), |
| 56 | BLOCK_HAS_DESCRIPTOR = (1 << 29) |
| 57 | }; |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 58 | static const int OBJC_ABI_VERSION = 7; |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 60 | Rewriter Rewrite; |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 61 | DiagnosticsEngine &Diags; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 62 | const LangOptions &LangOpts; |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 63 | ASTContext *Context; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 64 | SourceManager *SM; |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 65 | TranslationUnitDecl *TUDecl; |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 66 | FileID MainFileID; |
Chris Lattner | 26de465 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 67 | const char *MainFileStart, *MainFileEnd; |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 68 | Stmt *CurrentBody; |
| 69 | ParentMap *PropParentMap; // created lazily. |
Fariborz Jahanian | dd8079c | 2011-12-05 19:50:04 +0000 | [diff] [blame] | 70 | std::string InFileName; |
| 71 | raw_ostream* OutFile; |
| 72 | std::string Preamble; |
| 73 | |
| 74 | TypeDecl *ProtocolTypeDecl; |
| 75 | VarDecl *GlobalVarDecl; |
| 76 | unsigned RewriteFailedDiag; |
Fariborz Jahanian | dd8079c | 2011-12-05 19:50:04 +0000 | [diff] [blame] | 77 | // ObjC string constant support. |
| 78 | unsigned NumObjCStringLiterals; |
| 79 | VarDecl *ConstantStringClassReference; |
| 80 | RecordDecl *NSStringRecord; |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 81 | |
Fariborz Jahanian | dd8079c | 2011-12-05 19:50:04 +0000 | [diff] [blame] | 82 | // ObjC foreach break/continue generation support. |
| 83 | int BcLabelCount; |
| 84 | |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 85 | unsigned TryFinallyContainsReturnDiag; |
Fariborz Jahanian | dd8079c | 2011-12-05 19:50:04 +0000 | [diff] [blame] | 86 | // Needed for super. |
| 87 | ObjCMethodDecl *CurMethodDef; |
| 88 | RecordDecl *SuperStructDecl; |
| 89 | RecordDecl *ConstantStringDecl; |
| 90 | |
| 91 | FunctionDecl *MsgSendFunctionDecl; |
| 92 | FunctionDecl *MsgSendSuperFunctionDecl; |
| 93 | FunctionDecl *MsgSendStretFunctionDecl; |
| 94 | FunctionDecl *MsgSendSuperStretFunctionDecl; |
| 95 | FunctionDecl *MsgSendFpretFunctionDecl; |
| 96 | FunctionDecl *GetClassFunctionDecl; |
| 97 | FunctionDecl *GetMetaClassFunctionDecl; |
| 98 | FunctionDecl *GetSuperClassFunctionDecl; |
| 99 | FunctionDecl *SelGetUidFunctionDecl; |
| 100 | FunctionDecl *CFStringFunctionDecl; |
| 101 | FunctionDecl *SuperContructorFunctionDecl; |
| 102 | FunctionDecl *CurFunctionDef; |
| 103 | FunctionDecl *CurFunctionDeclToDeclareForBlock; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 104 | |
Fariborz Jahanian | dd8079c | 2011-12-05 19:50:04 +0000 | [diff] [blame] | 105 | /* Misc. containers needed for meta-data rewrite. */ |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 106 | SmallVector<ObjCImplementationDecl *, 8> ClassImplementation; |
| 107 | SmallVector<ObjCCategoryImplDecl *, 8> CategoryImplementation; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 108 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCSynthesizedStructs; |
Steve Naroff | fbfe825 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 109 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> ObjCSynthesizedProtocols; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 110 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCForwardDecls; |
| 111 | llvm::DenseMap<ObjCMethodDecl*, std::string> MethodInternalNames; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 112 | SmallVector<Stmt *, 32> Stmts; |
| 113 | SmallVector<int, 8> ObjCBcLabelNo; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 114 | // Remember all the @protocol(<expr>) expressions. |
| 115 | llvm::SmallPtrSet<ObjCProtocolDecl *, 32> ProtocolExprDecls; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 116 | |
| 117 | llvm::DenseSet<uint64_t> CopyDestroyCache; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 118 | |
| 119 | // Block expressions. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 120 | SmallVector<BlockExpr *, 32> Blocks; |
| 121 | SmallVector<int, 32> InnerDeclRefsCount; |
| 122 | SmallVector<BlockDeclRefExpr *, 32> InnerDeclRefs; |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 123 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 124 | SmallVector<BlockDeclRefExpr *, 32> BlockDeclRefs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 125 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 126 | // Block related declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 127 | SmallVector<ValueDecl *, 8> BlockByCopyDecls; |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 128 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByCopyDeclsPtrSet; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 129 | SmallVector<ValueDecl *, 8> BlockByRefDecls; |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 130 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByRefDeclsPtrSet; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 131 | llvm::DenseMap<ValueDecl *, unsigned> BlockByRefDeclNo; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 132 | llvm::SmallPtrSet<ValueDecl *, 8> ImportedBlockDecls; |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 133 | llvm::SmallPtrSet<VarDecl *, 8> ImportedLocalExternalDecls; |
| 134 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 135 | llvm::DenseMap<BlockExpr *, std::string> RewrittenBlockExprs; |
| 136 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 137 | // This maps an original source AST to it's rewritten form. This allows |
| 138 | // us to avoid rewriting the same node twice (which is very uncommon). |
| 139 | // This is needed to support some of the exotic property rewriting. |
| 140 | llvm::DenseMap<Stmt *, Stmt *> ReplacedNodes; |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 141 | |
Fariborz Jahanian | dd8079c | 2011-12-05 19:50:04 +0000 | [diff] [blame] | 142 | // Needed for header files being rewritten |
| 143 | bool IsHeader; |
| 144 | bool SilenceRewriteMacroWarning; |
| 145 | bool objc_impl_method; |
| 146 | |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 147 | bool DisableReplaceStmt; |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 148 | class DisableReplaceStmtScope { |
| 149 | RewriteObjC &R; |
| 150 | bool SavedValue; |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 151 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 152 | public: |
| 153 | DisableReplaceStmtScope(RewriteObjC &R) |
| 154 | : R(R), SavedValue(R.DisableReplaceStmt) { |
| 155 | R.DisableReplaceStmt = true; |
| 156 | } |
| 157 | ~DisableReplaceStmtScope() { |
| 158 | R.DisableReplaceStmt = SavedValue; |
| 159 | } |
| 160 | }; |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 161 | void InitializeCommon(ASTContext &context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 162 | |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 163 | public: |
Ted Kremenek | e3a6198 | 2008-05-31 20:11:04 +0000 | [diff] [blame] | 164 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 165 | // Top Level Driver code. |
Argyrios Kyrtzidis | 88c2596 | 2011-11-18 00:26:59 +0000 | [diff] [blame] | 166 | virtual bool HandleTopLevelDecl(DeclGroupRef D) { |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 167 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 168 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(*I)) { |
| 169 | if (!Class->isThisDeclarationADefinition()) { |
| 170 | RewriteForwardClassDecl(D); |
| 171 | break; |
| 172 | } |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 173 | } |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 174 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 175 | HandleTopLevelSingleDecl(*I); |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 176 | } |
Argyrios Kyrtzidis | 88c2596 | 2011-11-18 00:26:59 +0000 | [diff] [blame] | 177 | return true; |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 178 | } |
| 179 | void HandleTopLevelSingleDecl(Decl *D); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 180 | void HandleDeclInMainFile(Decl *D); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 181 | RewriteObjC(std::string inFile, raw_ostream *OS, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 182 | DiagnosticsEngine &D, const LangOptions &LOpts, |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 183 | bool silenceMacroWarn); |
Ted Kremenek | e452e0f | 2008-08-08 04:15:52 +0000 | [diff] [blame] | 184 | |
| 185 | ~RewriteObjC() {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 186 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 187 | virtual void HandleTranslationUnit(ASTContext &C); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 188 | |
Fariborz Jahanian | 88906cd | 2010-02-05 16:43:40 +0000 | [diff] [blame] | 189 | void ReplaceStmt(Stmt *Old, Stmt *New) { |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 190 | Stmt *ReplacingStmt = ReplacedNodes[Old]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 191 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 192 | if (ReplacingStmt) |
| 193 | return; // We can't rewrite the same node twice. |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 194 | |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 195 | if (DisableReplaceStmt) |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 196 | return; |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 197 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 198 | // If replacement succeeded or warning disabled return with no warning. |
Fariborz Jahanian | 88906cd | 2010-02-05 16:43:40 +0000 | [diff] [blame] | 199 | if (!Rewrite.ReplaceStmt(Old, New)) { |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 200 | ReplacedNodes[Old] = New; |
| 201 | return; |
| 202 | } |
| 203 | if (SilenceRewriteMacroWarning) |
| 204 | return; |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 205 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 206 | << Old->getSourceRange(); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 207 | } |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 208 | |
| 209 | void ReplaceStmtWithRange(Stmt *Old, Stmt *New, SourceRange SrcRange) { |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 210 | if (DisableReplaceStmt) |
| 211 | return; |
| 212 | |
Nick Lewycky | 7e74924 | 2010-10-31 21:07:24 +0000 | [diff] [blame] | 213 | // Measure the old text. |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 214 | int Size = Rewrite.getRangeSize(SrcRange); |
| 215 | if (Size == -1) { |
| 216 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 217 | << Old->getSourceRange(); |
| 218 | return; |
| 219 | } |
| 220 | // Get the new text. |
| 221 | std::string SStr; |
| 222 | llvm::raw_string_ostream S(SStr); |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 223 | New->printPretty(S, *Context, 0, PrintingPolicy(LangOpts)); |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 224 | const std::string &Str = S.str(); |
| 225 | |
| 226 | // If replacement succeeded or warning disabled return with no warning. |
Daniel Dunbar | d7407dc | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 227 | if (!Rewrite.ReplaceText(SrcRange.getBegin(), Size, Str)) { |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 228 | ReplacedNodes[Old] = New; |
| 229 | return; |
| 230 | } |
| 231 | if (SilenceRewriteMacroWarning) |
| 232 | return; |
| 233 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 234 | << Old->getSourceRange(); |
| 235 | } |
| 236 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 237 | void InsertText(SourceLocation Loc, StringRef Str, |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 238 | bool InsertAfter = true) { |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 239 | // If insertion succeeded or warning disabled return with no warning. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 240 | if (!Rewrite.InsertText(Loc, Str, InsertAfter) || |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 241 | SilenceRewriteMacroWarning) |
| 242 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 243 | |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 244 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 245 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 246 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 247 | void ReplaceText(SourceLocation Start, unsigned OrigLength, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 248 | StringRef Str) { |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 249 | // If removal succeeded or warning disabled return with no warning. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 250 | if (!Rewrite.ReplaceText(Start, OrigLength, Str) || |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 251 | SilenceRewriteMacroWarning) |
| 252 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 253 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 254 | Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag); |
| 255 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 256 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 257 | // Syntactic Rewriting. |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 258 | void RewriteRecordBody(RecordDecl *RD); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 259 | void RewriteInclude(); |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 260 | void RewriteForwardClassDecl(DeclGroupRef D); |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 261 | void RewriteForwardClassDecl(const llvm::SmallVector<Decl*, 8> &DG); |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 262 | void RewriteForwardClassEpilogue(ObjCInterfaceDecl *ClassDecl, |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 263 | const std::string &typedefString); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 264 | void RewriteImplementations(); |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 265 | void RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 266 | ObjCImplementationDecl *IMD, |
| 267 | ObjCCategoryImplDecl *CID); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 268 | void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 269 | void RewriteImplementationDecl(Decl *Dcl); |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 270 | void RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl, |
| 271 | ObjCMethodDecl *MDecl, std::string &ResultStr); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 272 | void RewriteTypeIntoString(QualType T, std::string &ResultStr, |
| 273 | const FunctionType *&FPRetType); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 274 | void RewriteByRefString(std::string &ResultStr, const std::string &Name, |
Fariborz Jahanian | 1e8011e | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 275 | ValueDecl *VD, bool def=false); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 276 | void RewriteCategoryDecl(ObjCCategoryDecl *Dcl); |
| 277 | void RewriteProtocolDecl(ObjCProtocolDecl *Dcl); |
| 278 | void RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *Dcl); |
| 279 | void RewriteMethodDeclaration(ObjCMethodDecl *Method); |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 280 | void RewriteProperty(ObjCPropertyDecl *prop); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 281 | void RewriteFunctionDecl(FunctionDecl *FD); |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 282 | void RewriteBlockPointerType(std::string& Str, QualType Type); |
| 283 | void RewriteBlockPointerTypeVariable(std::string& Str, ValueDecl *VD); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 284 | void RewriteBlockLiteralFunctionDecl(FunctionDecl *FD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 285 | void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 286 | void RewriteTypeOfDecl(VarDecl *VD); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 287 | void RewriteObjCQualifiedInterfaceTypes(Expr *E); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 288 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 289 | // Expression Rewriting. |
Steve Naroff | f3473a7 | 2007-11-09 15:20:18 +0000 | [diff] [blame] | 290 | Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S); |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 291 | Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 292 | Stmt *RewritePropertyOrImplicitGetter(PseudoObjectExpr *Pseudo); |
| 293 | Stmt *RewritePropertyOrImplicitSetter(PseudoObjectExpr *Pseudo); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 294 | Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp); |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 295 | Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp); |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 296 | Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp); |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 297 | Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 298 | void RewriteTryReturnStmts(Stmt *S); |
| 299 | void RewriteSyncReturnStmts(Stmt *S, std::string buf); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 300 | Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 301 | Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 302 | Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S); |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 303 | Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
| 304 | SourceLocation OrigEnd); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 305 | Stmt *RewriteBreakStmt(BreakStmt *S); |
| 306 | Stmt *RewriteContinueStmt(ContinueStmt *S); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 307 | void RewriteCastExpr(CStyleCastExpr *CE); |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 308 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 309 | // Block rewriting. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 310 | void RewriteBlocksInFunctionProtoType(QualType funcType, NamedDecl *D); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 311 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 312 | // Block specific rewrite rules. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 313 | void RewriteBlockPointerDecl(NamedDecl *VD); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 314 | void RewriteByRefVar(VarDecl *VD); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 315 | Stmt *RewriteBlockDeclRefExpr(Expr *VD); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 316 | Stmt *RewriteLocalVariableExternalStorage(DeclRefExpr *DRE); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 317 | void RewriteBlockPointerFunctionArgs(FunctionDecl *FD); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 318 | |
| 319 | void RewriteObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
| 320 | std::string &Result); |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 321 | |
| 322 | virtual void Initialize(ASTContext &context) = 0; |
| 323 | |
| 324 | // Metadata Rewriting. |
| 325 | virtual void RewriteMetaDataIntoBuffer(std::string &Result) = 0; |
| 326 | virtual void RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Prots, |
| 327 | StringRef prefix, |
| 328 | StringRef ClassName, |
| 329 | std::string &Result) = 0; |
| 330 | virtual void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
| 331 | std::string &Result) = 0; |
| 332 | virtual void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol, |
| 333 | StringRef prefix, |
| 334 | StringRef ClassName, |
| 335 | std::string &Result) = 0; |
| 336 | virtual void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
| 337 | std::string &Result) = 0; |
| 338 | |
| 339 | // Rewriting ivar access |
| 340 | virtual Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV) = 0; |
| 341 | virtual void RewriteIvarOffsetComputation(ObjCIvarDecl *ivar, |
| 342 | std::string &Result) = 0; |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 343 | |
| 344 | // Misc. AST transformation routines. Somtimes they end up calling |
| 345 | // rewriting routines on the new ASTs. |
| 346 | CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD, |
| 347 | Expr **args, unsigned nargs, |
| 348 | SourceLocation StartLoc=SourceLocation(), |
| 349 | SourceLocation EndLoc=SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 350 | |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 351 | Stmt *SynthMessageExpr(ObjCMessageExpr *Exp, |
| 352 | SourceLocation StartLoc=SourceLocation(), |
| 353 | SourceLocation EndLoc=SourceLocation()); |
| 354 | |
| 355 | void SynthCountByEnumWithState(std::string &buf); |
| 356 | void SynthMsgSendFunctionDecl(); |
| 357 | void SynthMsgSendSuperFunctionDecl(); |
| 358 | void SynthMsgSendStretFunctionDecl(); |
| 359 | void SynthMsgSendFpretFunctionDecl(); |
| 360 | void SynthMsgSendSuperStretFunctionDecl(); |
| 361 | void SynthGetClassFunctionDecl(); |
| 362 | void SynthGetMetaClassFunctionDecl(); |
| 363 | void SynthGetSuperClassFunctionDecl(); |
| 364 | void SynthSelGetUidFunctionDecl(); |
| 365 | void SynthSuperContructorFunctionDecl(); |
| 366 | |
| 367 | std::string SynthesizeByrefCopyDestroyHelper(VarDecl *VD, int flag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 368 | std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 369 | StringRef funcName, std::string Tag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 370 | std::string SynthesizeBlockFunc(BlockExpr *CE, int i, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 371 | StringRef funcName, std::string Tag); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 372 | std::string SynthesizeBlockImpl(BlockExpr *CE, |
| 373 | std::string Tag, std::string Desc); |
| 374 | std::string SynthesizeBlockDescriptor(std::string DescTag, |
| 375 | std::string ImplTag, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 376 | int i, StringRef funcName, |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 377 | unsigned hasCopy); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 378 | Stmt *SynthesizeBlockCall(CallExpr *Exp, const Expr* BlockExp); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 379 | void SynthesizeBlockLiterals(SourceLocation FunLocStart, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 380 | StringRef FunName); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 381 | FunctionDecl *SynthBlockInitFunctionDecl(StringRef name); |
| 382 | Stmt *SynthBlockInitExpr(BlockExpr *Exp, |
| 383 | const SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 384 | |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 385 | // Misc. helper routines. |
Fariborz Jahanian | dd8079c | 2011-12-05 19:50:04 +0000 | [diff] [blame] | 386 | QualType getProtocolType(); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 387 | void WarnAboutReturnGotoStmts(Stmt *S); |
| 388 | void HasReturnStmts(Stmt *S, bool &hasReturns); |
| 389 | void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND); |
| 390 | void InsertBlockLiteralsWithinFunction(FunctionDecl *FD); |
| 391 | void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD); |
| 392 | |
| 393 | bool IsDeclStmtInForeachHeader(DeclStmt *DS); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 394 | void CollectBlockDeclRefInfo(BlockExpr *Exp); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 395 | void GetBlockDeclRefExprs(Stmt *S); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 396 | void GetInnerBlockDeclRefExprs(Stmt *S, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 397 | SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs, |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 398 | llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 399 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 400 | // We avoid calling Type::isBlockPointerType(), since it operates on the |
| 401 | // canonical type. We only care if the top-level type is a closure pointer. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 402 | bool isTopLevelBlockPointerType(QualType T) { |
| 403 | return isa<BlockPointerType>(T); |
| 404 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 405 | |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 406 | /// convertBlockPointerToFunctionPointer - Converts a block-pointer type |
| 407 | /// to a function pointer type and upon success, returns true; false |
| 408 | /// otherwise. |
| 409 | bool convertBlockPointerToFunctionPointer(QualType &T) { |
| 410 | if (isTopLevelBlockPointerType(T)) { |
| 411 | const BlockPointerType *BPT = T->getAs<BlockPointerType>(); |
| 412 | T = Context->getPointerType(BPT->getPointeeType()); |
| 413 | return true; |
| 414 | } |
| 415 | return false; |
| 416 | } |
| 417 | |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 418 | bool needToScanForQualifiers(QualType T); |
| 419 | QualType getSuperStructType(); |
| 420 | QualType getConstantStringStructType(); |
| 421 | QualType convertFunctionTypeOfBlocks(const FunctionType *FT); |
| 422 | bool BufferContainsPPDirectives(const char *startBuf, const char *endBuf); |
| 423 | |
Fariborz Jahanian | 8188e5f | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 424 | void convertToUnqualifiedObjCType(QualType &T) { |
| 425 | if (T->isObjCQualifiedIdType()) |
| 426 | T = Context->getObjCIdType(); |
| 427 | else if (T->isObjCQualifiedClassType()) |
| 428 | T = Context->getObjCClassType(); |
| 429 | else if (T->isObjCObjectPointerType() && |
Fariborz Jahanian | 3a448fb | 2011-09-10 17:01:56 +0000 | [diff] [blame] | 430 | T->getPointeeType()->isObjCQualifiedInterfaceType()) { |
| 431 | if (const ObjCObjectPointerType * OBJPT = |
| 432 | T->getAsObjCInterfacePointerType()) { |
| 433 | const ObjCInterfaceType *IFaceT = OBJPT->getInterfaceType(); |
| 434 | T = QualType(IFaceT, 0); |
| 435 | T = Context->getPointerType(T); |
| 436 | } |
| 437 | } |
Fariborz Jahanian | 8188e5f | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 440 | // FIXME: This predicate seems like it would be useful to add to ASTContext. |
| 441 | bool isObjCType(QualType T) { |
| 442 | if (!LangOpts.ObjC1 && !LangOpts.ObjC2) |
| 443 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 444 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 445 | QualType OCT = Context->getCanonicalType(T).getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 446 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 447 | if (OCT == Context->getCanonicalType(Context->getObjCIdType()) || |
| 448 | OCT == Context->getCanonicalType(Context->getObjCClassType())) |
| 449 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 450 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 451 | if (const PointerType *PT = OCT->getAs<PointerType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 452 | if (isa<ObjCInterfaceType>(PT->getPointeeType()) || |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 453 | PT->getPointeeType()->isObjCQualifiedIdType()) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 454 | return true; |
| 455 | } |
| 456 | return false; |
| 457 | } |
| 458 | bool PointerTypeTakesAnyBlockArguments(QualType QT); |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 459 | bool PointerTypeTakesAnyObjCQualifiedType(QualType QT); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 460 | void GetExtentOfArgList(const char *Name, const char *&LParen, |
| 461 | const char *&RParen); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 462 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 463 | void QuoteDoublequotes(std::string &From, std::string &To) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 464 | for (unsigned i = 0; i < From.length(); i++) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 465 | if (From[i] == '"') |
| 466 | To += "\\\""; |
| 467 | else |
| 468 | To += From[i]; |
| 469 | } |
| 470 | } |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 471 | |
| 472 | QualType getSimpleFunctionType(QualType result, |
| 473 | const QualType *args, |
| 474 | unsigned numArgs, |
| 475 | bool variadic = false) { |
Fariborz Jahanian | 8891480 | 2011-09-09 20:35:22 +0000 | [diff] [blame] | 476 | if (result == Context->getObjCInstanceType()) |
| 477 | result = Context->getObjCIdType(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 478 | FunctionProtoType::ExtProtoInfo fpi; |
| 479 | fpi.Variadic = variadic; |
| 480 | return Context->getFunctionType(result, args, numArgs, fpi); |
| 481 | } |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 482 | |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 483 | // Helper function: create a CStyleCastExpr with trivial type source info. |
| 484 | CStyleCastExpr* NoTypeInfoCStyleCastExpr(ASTContext *Ctx, QualType Ty, |
| 485 | CastKind Kind, Expr *E) { |
| 486 | TypeSourceInfo *TInfo = Ctx->getTrivialTypeSourceInfo(Ty, SourceLocation()); |
| 487 | return CStyleCastExpr::Create(*Ctx, Ty, VK_RValue, Kind, E, 0, TInfo, |
| 488 | SourceLocation(), SourceLocation()); |
| 489 | } |
| 490 | }; |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 491 | |
| 492 | class RewriteObjCFragileABI : public RewriteObjC { |
| 493 | public: |
| 494 | |
| 495 | RewriteObjCFragileABI(std::string inFile, raw_ostream *OS, |
| 496 | DiagnosticsEngine &D, const LangOptions &LOpts, |
| 497 | bool silenceMacroWarn) : RewriteObjC(inFile, OS, |
| 498 | D, LOpts, |
| 499 | silenceMacroWarn) {} |
| 500 | |
| 501 | ~RewriteObjCFragileABI() {} |
| 502 | virtual void Initialize(ASTContext &context); |
| 503 | |
| 504 | // Rewriting metadata |
| 505 | template<typename MethodIterator> |
| 506 | void RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 507 | MethodIterator MethodEnd, |
| 508 | bool IsInstanceMethod, |
| 509 | StringRef prefix, |
| 510 | StringRef ClassName, |
| 511 | std::string &Result); |
| 512 | virtual void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol, |
| 513 | StringRef prefix, |
| 514 | StringRef ClassName, |
| 515 | std::string &Result); |
| 516 | virtual void RewriteObjCProtocolListMetaData( |
| 517 | const ObjCList<ObjCProtocolDecl> &Prots, |
| 518 | StringRef prefix, StringRef ClassName, std::string &Result); |
| 519 | virtual void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
| 520 | std::string &Result); |
| 521 | virtual void RewriteMetaDataIntoBuffer(std::string &Result); |
| 522 | virtual void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
| 523 | std::string &Result); |
| 524 | |
| 525 | // Rewriting ivar |
| 526 | virtual void RewriteIvarOffsetComputation(ObjCIvarDecl *ivar, |
| 527 | std::string &Result); |
| 528 | virtual Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV); |
| 529 | }; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 532 | void RewriteObjC::RewriteBlocksInFunctionProtoType(QualType funcType, |
| 533 | NamedDecl *D) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 534 | if (const FunctionProtoType *fproto |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 535 | = dyn_cast<FunctionProtoType>(funcType.IgnoreParens())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 536 | for (FunctionProtoType::arg_type_iterator I = fproto->arg_type_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 537 | E = fproto->arg_type_end(); I && (I != E); ++I) |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 538 | if (isTopLevelBlockPointerType(*I)) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 539 | // All the args are checked/rewritten. Don't call twice! |
| 540 | RewriteBlockPointerDecl(D); |
| 541 | break; |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | void RewriteObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 547 | const PointerType *PT = funcType->getAs<PointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 548 | if (PT && PointerTypeTakesAnyBlockArguments(funcType)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 549 | RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 552 | static bool IsHeaderFile(const std::string &Filename) { |
| 553 | std::string::size_type DotPos = Filename.rfind('.'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 554 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 555 | if (DotPos == std::string::npos) { |
| 556 | // no file extension |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 557 | return false; |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 558 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 559 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 560 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 561 | // C header: .h |
| 562 | // C++ header: .hh or .H; |
| 563 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 564 | } |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 565 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 566 | RewriteObjC::RewriteObjC(std::string inFile, raw_ostream* OS, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 567 | DiagnosticsEngine &D, const LangOptions &LOpts, |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 568 | bool silenceMacroWarn) |
| 569 | : Diags(D), LangOpts(LOpts), InFileName(inFile), OutFile(OS), |
| 570 | SilenceRewriteMacroWarning(silenceMacroWarn) { |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 571 | IsHeader = IsHeaderFile(inFile); |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 572 | RewriteFailedDiag = Diags.getCustomDiagID(DiagnosticsEngine::Warning, |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 573 | "rewriting sub-expression within a macro (may not be correct)"); |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 574 | TryFinallyContainsReturnDiag = Diags.getCustomDiagID( |
| 575 | DiagnosticsEngine::Warning, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 576 | "rewriter doesn't support user-specified control flow semantics " |
| 577 | "for @try/@finally (code may not execute properly)"); |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Eli Friedman | bce831b | 2009-05-18 22:29:17 +0000 | [diff] [blame] | 580 | ASTConsumer *clang::CreateObjCRewriter(const std::string& InFile, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 581 | raw_ostream* OS, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 582 | DiagnosticsEngine &Diags, |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 583 | const LangOptions &LOpts, |
| 584 | bool SilenceRewriteMacroWarning) { |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 585 | if (true /*!LOpts.ObjCNonFragileABI*/) |
| 586 | return new RewriteObjCFragileABI(InFile, OS, Diags, LOpts, SilenceRewriteMacroWarning); |
| 587 | else { |
| 588 | assert(false && "objective-C rewriter for nonfragile ABI = NYI"); |
| 589 | return 0; |
| 590 | } |
Chris Lattner | e365c50 | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 591 | } |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 592 | |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 593 | void RewriteObjC::InitializeCommon(ASTContext &context) { |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 594 | Context = &context; |
| 595 | SM = &Context->getSourceManager(); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 596 | TUDecl = Context->getTranslationUnitDecl(); |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 597 | MsgSendFunctionDecl = 0; |
| 598 | MsgSendSuperFunctionDecl = 0; |
| 599 | MsgSendStretFunctionDecl = 0; |
| 600 | MsgSendSuperStretFunctionDecl = 0; |
| 601 | MsgSendFpretFunctionDecl = 0; |
| 602 | GetClassFunctionDecl = 0; |
| 603 | GetMetaClassFunctionDecl = 0; |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 604 | GetSuperClassFunctionDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 605 | SelGetUidFunctionDecl = 0; |
| 606 | CFStringFunctionDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 607 | ConstantStringClassReference = 0; |
| 608 | NSStringRecord = 0; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 609 | CurMethodDef = 0; |
| 610 | CurFunctionDef = 0; |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 611 | CurFunctionDeclToDeclareForBlock = 0; |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 612 | GlobalVarDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 613 | SuperStructDecl = 0; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 614 | ProtocolTypeDecl = 0; |
Steve Naroff | 9630ec5 | 2008-03-27 22:59:54 +0000 | [diff] [blame] | 615 | ConstantStringDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 616 | BcLabelCount = 0; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 617 | SuperContructorFunctionDecl = 0; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 618 | NumObjCStringLiterals = 0; |
Steve Naroff | 68272b8 | 2008-12-08 20:01:41 +0000 | [diff] [blame] | 619 | PropParentMap = 0; |
| 620 | CurrentBody = 0; |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 621 | DisableReplaceStmt = false; |
Fariborz Jahanian | f292fcf | 2010-01-07 22:51:18 +0000 | [diff] [blame] | 622 | objc_impl_method = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 623 | |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 624 | // Get the ID and start/end of the main file. |
| 625 | MainFileID = SM->getMainFileID(); |
| 626 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 627 | MainFileStart = MainBuf->getBufferStart(); |
| 628 | MainFileEnd = MainBuf->getBufferEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 629 | |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 630 | Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOptions()); |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 633 | //===----------------------------------------------------------------------===// |
| 634 | // Top Level Driver Code |
| 635 | //===----------------------------------------------------------------------===// |
| 636 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 637 | void RewriteObjC::HandleTopLevelSingleDecl(Decl *D) { |
Ted Kremenek | e50187a | 2010-02-05 21:28:51 +0000 | [diff] [blame] | 638 | if (Diags.hasErrorOccurred()) |
| 639 | return; |
| 640 | |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 641 | // Two cases: either the decl could be in the main file, or it could be in a |
| 642 | // #included file. If the former, rewrite it now. If the later, check to see |
| 643 | // if we rewrote the #include/#import. |
| 644 | SourceLocation Loc = D->getLocation(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 645 | Loc = SM->getExpansionLoc(Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 646 | |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 647 | // If this is for a builtin, ignore it. |
| 648 | if (Loc.isInvalid()) return; |
| 649 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 650 | // Look for built-in declarations that we need to refer during the rewrite. |
| 651 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 652 | RewriteFunctionDecl(FD); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 653 | } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) { |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 654 | // declared in <Foundation/NSString.h> |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 655 | if (FVD->getName() == "_NSConstantStringClassReference") { |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 656 | ConstantStringClassReference = FVD; |
| 657 | return; |
| 658 | } |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 659 | } else if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) { |
| 660 | if (ID->isThisDeclarationADefinition()) |
| 661 | RewriteInterfaceDecl(ID); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 662 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 663 | RewriteCategoryDecl(CD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 664 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 665 | RewriteProtocolDecl(PD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 666 | } else if (ObjCForwardProtocolDecl *FP = |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 667 | dyn_cast<ObjCForwardProtocolDecl>(D)){ |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 668 | RewriteForwardProtocolDecl(FP); |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 669 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
| 670 | // Recurse into linkage specifications |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 671 | for (DeclContext::decl_iterator DI = LSD->decls_begin(), |
| 672 | DIEnd = LSD->decls_end(); |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 673 | DI != DIEnd; ) { |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 674 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>((*DI))) { |
| 675 | if (!IFace->isThisDeclarationADefinition()) { |
| 676 | SmallVector<Decl *, 8> DG; |
| 677 | SourceLocation StartLoc = IFace->getLocStart(); |
| 678 | do { |
| 679 | if (isa<ObjCInterfaceDecl>(*DI) && |
| 680 | !cast<ObjCInterfaceDecl>(*DI)->isThisDeclarationADefinition() && |
| 681 | StartLoc == (*DI)->getLocStart()) |
| 682 | DG.push_back(*DI); |
| 683 | else |
| 684 | break; |
| 685 | |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 686 | ++DI; |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 687 | } while (DI != DIEnd); |
| 688 | RewriteForwardClassDecl(DG); |
| 689 | continue; |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 690 | } |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 691 | } |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 692 | HandleTopLevelSingleDecl(*DI); |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 693 | ++DI; |
| 694 | } |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 695 | } |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 696 | // If we have a decl in the main file, see if we should rewrite it. |
Ted Kremenek | cf7e958 | 2008-04-14 21:24:13 +0000 | [diff] [blame] | 697 | if (SM->isFromMainFile(Loc)) |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 698 | return HandleDeclInMainFile(D); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 701 | //===----------------------------------------------------------------------===// |
| 702 | // Syntactic (non-AST) Rewriting Code |
| 703 | //===----------------------------------------------------------------------===// |
| 704 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 705 | void RewriteObjC::RewriteInclude() { |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 706 | SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 707 | StringRef MainBuf = SM->getBufferData(MainFileID); |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 708 | const char *MainBufStart = MainBuf.begin(); |
| 709 | const char *MainBufEnd = MainBuf.end(); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 710 | size_t ImportLen = strlen("import"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 711 | |
Fariborz Jahanian | af57b46 | 2008-01-19 01:03:17 +0000 | [diff] [blame] | 712 | // Loop over the whole file, looking for includes. |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 713 | for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { |
| 714 | if (*BufPtr == '#') { |
| 715 | if (++BufPtr == MainBufEnd) |
| 716 | return; |
| 717 | while (*BufPtr == ' ' || *BufPtr == '\t') |
| 718 | if (++BufPtr == MainBufEnd) |
| 719 | return; |
| 720 | if (!strncmp(BufPtr, "import", ImportLen)) { |
| 721 | // replace import with include |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 722 | SourceLocation ImportLoc = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 723 | LocStart.getLocWithOffset(BufPtr-MainBufStart); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 724 | ReplaceText(ImportLoc, ImportLen, "include"); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 725 | BufPtr += ImportLen; |
| 726 | } |
| 727 | } |
| 728 | } |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 731 | static std::string getIvarAccessString(ObjCIvarDecl *OID) { |
| 732 | const ObjCInterfaceDecl *ClassDecl = OID->getContainingInterface(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 733 | std::string S; |
| 734 | S = "((struct "; |
| 735 | S += ClassDecl->getIdentifier()->getName(); |
| 736 | S += "_IMPL *)self)->"; |
Daniel Dunbar | 5ffe14c | 2009-10-18 20:26:27 +0000 | [diff] [blame] | 737 | S += OID->getName(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 738 | return S; |
| 739 | } |
| 740 | |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 741 | void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 742 | ObjCImplementationDecl *IMD, |
| 743 | ObjCCategoryImplDecl *CID) { |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 744 | static bool objcGetPropertyDefined = false; |
| 745 | static bool objcSetPropertyDefined = false; |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 746 | SourceLocation startLoc = PID->getLocStart(); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 747 | InsertText(startLoc, "// "); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 748 | const char *startBuf = SM->getCharacterData(startLoc); |
| 749 | assert((*startBuf == '@') && "bogus @synthesize location"); |
| 750 | const char *semiBuf = strchr(startBuf, ';'); |
| 751 | assert((*semiBuf == ';') && "@synthesize: can't find ';'"); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 752 | SourceLocation onePastSemiLoc = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 753 | startLoc.getLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 754 | |
| 755 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 756 | return; // FIXME: is this correct? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 757 | |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 758 | // Generate the 'getter' function. |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 759 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 760 | ObjCIvarDecl *OID = PID->getPropertyIvarDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 761 | |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 762 | if (!OID) |
| 763 | return; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 764 | unsigned Attributes = PD->getPropertyAttributes(); |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 765 | if (!PD->getGetterMethodDecl()->isDefined()) { |
| 766 | bool GenGetProperty = !(Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) && |
| 767 | (Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
| 768 | ObjCPropertyDecl::OBJC_PR_copy)); |
| 769 | std::string Getr; |
| 770 | if (GenGetProperty && !objcGetPropertyDefined) { |
| 771 | objcGetPropertyDefined = true; |
| 772 | // FIXME. Is this attribute correct in all cases? |
| 773 | Getr = "\nextern \"C\" __declspec(dllimport) " |
| 774 | "id objc_getProperty(id, SEL, long, bool);\n"; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 775 | } |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 776 | RewriteObjCMethodDecl(OID->getContainingInterface(), |
| 777 | PD->getGetterMethodDecl(), Getr); |
| 778 | Getr += "{ "; |
| 779 | // Synthesize an explicit cast to gain access to the ivar. |
| 780 | // See objc-act.c:objc_synthesize_new_getter() for details. |
| 781 | if (GenGetProperty) { |
| 782 | // return objc_getProperty(self, _cmd, offsetof(ClassDecl, OID), 1) |
| 783 | Getr += "typedef "; |
| 784 | const FunctionType *FPRetType = 0; |
| 785 | RewriteTypeIntoString(PD->getGetterMethodDecl()->getResultType(), Getr, |
| 786 | FPRetType); |
| 787 | Getr += " _TYPE"; |
| 788 | if (FPRetType) { |
| 789 | Getr += ")"; // close the precedence "scope" for "*". |
| 790 | |
| 791 | // Now, emit the argument types (if any). |
| 792 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)){ |
| 793 | Getr += "("; |
| 794 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 795 | if (i) Getr += ", "; |
| 796 | std::string ParamStr = FT->getArgType(i).getAsString( |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 797 | Context->getPrintingPolicy()); |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 798 | Getr += ParamStr; |
| 799 | } |
| 800 | if (FT->isVariadic()) { |
| 801 | if (FT->getNumArgs()) Getr += ", "; |
| 802 | Getr += "..."; |
| 803 | } |
| 804 | Getr += ")"; |
| 805 | } else |
| 806 | Getr += "()"; |
| 807 | } |
| 808 | Getr += ";\n"; |
| 809 | Getr += "return (_TYPE)"; |
| 810 | Getr += "objc_getProperty(self, _cmd, "; |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 811 | RewriteIvarOffsetComputation(OID, Getr); |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 812 | Getr += ", 1)"; |
| 813 | } |
| 814 | else |
| 815 | Getr += "return " + getIvarAccessString(OID); |
| 816 | Getr += "; }"; |
| 817 | InsertText(onePastSemiLoc, Getr); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 818 | } |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 819 | |
| 820 | if (PD->isReadOnly() || PD->getSetterMethodDecl()->isDefined()) |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 821 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 822 | |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 823 | // Generate the 'setter' function. |
| 824 | std::string Setr; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 825 | bool GenSetProperty = Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
| 826 | ObjCPropertyDecl::OBJC_PR_copy); |
| 827 | if (GenSetProperty && !objcSetPropertyDefined) { |
| 828 | objcSetPropertyDefined = true; |
| 829 | // FIXME. Is this attribute correct in all cases? |
| 830 | Setr = "\nextern \"C\" __declspec(dllimport) " |
| 831 | "void objc_setProperty (id, SEL, long, id, bool, bool);\n"; |
| 832 | } |
| 833 | |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 834 | RewriteObjCMethodDecl(OID->getContainingInterface(), |
| 835 | PD->getSetterMethodDecl(), Setr); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 836 | Setr += "{ "; |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 837 | // Synthesize an explicit cast to initialize the ivar. |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 838 | // See objc-act.c:objc_synthesize_new_setter() for details. |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 839 | if (GenSetProperty) { |
| 840 | Setr += "objc_setProperty (self, _cmd, "; |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 841 | RewriteIvarOffsetComputation(OID, Setr); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 842 | Setr += ", (id)"; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 843 | Setr += PD->getName(); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 844 | Setr += ", "; |
| 845 | if (Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 846 | Setr += "0, "; |
| 847 | else |
| 848 | Setr += "1, "; |
| 849 | if (Attributes & ObjCPropertyDecl::OBJC_PR_copy) |
| 850 | Setr += "1)"; |
| 851 | else |
| 852 | Setr += "0)"; |
| 853 | } |
| 854 | else { |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 855 | Setr += getIvarAccessString(OID) + " = "; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 856 | Setr += PD->getName(); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 857 | } |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 858 | Setr += "; }"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 859 | InsertText(onePastSemiLoc, Setr); |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 860 | } |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 861 | |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 862 | static void RewriteOneForwardClassDecl(ObjCInterfaceDecl *ForwardDecl, |
| 863 | std::string &typedefString) { |
| 864 | typedefString += "#ifndef _REWRITER_typedef_"; |
| 865 | typedefString += ForwardDecl->getNameAsString(); |
| 866 | typedefString += "\n"; |
| 867 | typedefString += "#define _REWRITER_typedef_"; |
| 868 | typedefString += ForwardDecl->getNameAsString(); |
| 869 | typedefString += "\n"; |
| 870 | typedefString += "typedef struct objc_object "; |
| 871 | typedefString += ForwardDecl->getNameAsString(); |
| 872 | typedefString += ";\n#endif\n"; |
| 873 | } |
| 874 | |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 875 | void RewriteObjC::RewriteForwardClassEpilogue(ObjCInterfaceDecl *ClassDecl, |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 876 | const std::string &typedefString) { |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 877 | SourceLocation startLoc = ClassDecl->getLocStart(); |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 878 | const char *startBuf = SM->getCharacterData(startLoc); |
| 879 | const char *semiPtr = strchr(startBuf, ';'); |
| 880 | // Replace the @class with typedefs corresponding to the classes. |
| 881 | ReplaceText(startLoc, semiPtr-startBuf+1, typedefString); |
| 882 | } |
| 883 | |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 884 | void RewriteObjC::RewriteForwardClassDecl(DeclGroupRef D) { |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 885 | std::string typedefString; |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 886 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 887 | ObjCInterfaceDecl *ForwardDecl = cast<ObjCInterfaceDecl>(*I); |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 888 | if (I == D.begin()) { |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 889 | // Translate to typedef's that forward reference structs with the same name |
| 890 | // as the class. As a convenience, we include the original declaration |
| 891 | // as a comment. |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 892 | typedefString += "// @class "; |
| 893 | typedefString += ForwardDecl->getNameAsString(); |
Fariborz Jahanian | 91fbd12 | 2010-01-11 22:48:40 +0000 | [diff] [blame] | 894 | typedefString += ";\n"; |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 895 | } |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 896 | RewriteOneForwardClassDecl(ForwardDecl, typedefString); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 897 | } |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 898 | DeclGroupRef::iterator I = D.begin(); |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 899 | RewriteForwardClassEpilogue(cast<ObjCInterfaceDecl>(*I), typedefString); |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 900 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 901 | |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 902 | void RewriteObjC::RewriteForwardClassDecl( |
| 903 | const llvm::SmallVector<Decl*, 8> &D) { |
| 904 | std::string typedefString; |
| 905 | for (unsigned i = 0; i < D.size(); i++) { |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 906 | ObjCInterfaceDecl *ForwardDecl = cast<ObjCInterfaceDecl>(D[i]); |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 907 | if (i == 0) { |
| 908 | typedefString += "// @class "; |
| 909 | typedefString += ForwardDecl->getNameAsString(); |
| 910 | typedefString += ";\n"; |
| 911 | } |
| 912 | RewriteOneForwardClassDecl(ForwardDecl, typedefString); |
| 913 | } |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 914 | RewriteForwardClassEpilogue(cast<ObjCInterfaceDecl>(D[0]), typedefString); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 917 | void RewriteObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) { |
Fariborz Jahanian | d050240 | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 918 | // When method is a synthesized one, such as a getter/setter there is |
| 919 | // nothing to rewrite. |
Fariborz Jahanian | 8891480 | 2011-09-09 20:35:22 +0000 | [diff] [blame] | 920 | if (Method->isImplicit()) |
Fariborz Jahanian | d050240 | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 921 | return; |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 922 | SourceLocation LocStart = Method->getLocStart(); |
| 923 | SourceLocation LocEnd = Method->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 924 | |
Chandler Carruth | 6421162 | 2011-07-25 21:09:52 +0000 | [diff] [blame] | 925 | if (SM->getExpansionLineNumber(LocEnd) > |
| 926 | SM->getExpansionLineNumber(LocStart)) { |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 927 | InsertText(LocStart, "#if 0\n"); |
| 928 | ReplaceText(LocEnd, 1, ";\n#endif\n"); |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 929 | } else { |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 930 | InsertText(LocStart, "// "); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 931 | } |
| 932 | } |
| 933 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 934 | void RewriteObjC::RewriteProperty(ObjCPropertyDecl *prop) { |
Fariborz Jahanian | d050240 | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 935 | SourceLocation Loc = prop->getAtLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 936 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 937 | ReplaceText(Loc, 0, "// "); |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 938 | // FIXME: handle properties that are declared across multiple lines. |
Fariborz Jahanian | 957cf65 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 939 | } |
| 940 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 941 | void RewriteObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 942 | SourceLocation LocStart = CatDecl->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 943 | |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 944 | // FIXME: handle category headers that are declared across multiple lines. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 945 | ReplaceText(LocStart, 0, "// "); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 946 | |
Fariborz Jahanian | 13751e3 | 2010-02-10 01:15:09 +0000 | [diff] [blame] | 947 | for (ObjCCategoryDecl::prop_iterator I = CatDecl->prop_begin(), |
| 948 | E = CatDecl->prop_end(); I != E; ++I) |
| 949 | RewriteProperty(*I); |
| 950 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 951 | for (ObjCCategoryDecl::instmeth_iterator |
| 952 | I = CatDecl->instmeth_begin(), E = CatDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 953 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 954 | RewriteMethodDeclaration(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 955 | for (ObjCCategoryDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 956 | I = CatDecl->classmeth_begin(), E = CatDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 957 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 958 | RewriteMethodDeclaration(*I); |
| 959 | |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 960 | // Lastly, comment out the @end. |
Fariborz Jahanian | 73d1eb0 | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 961 | ReplaceText(CatDecl->getAtEndRange().getBegin(), |
| 962 | strlen("@end"), "/* @end */"); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 963 | } |
| 964 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 965 | void RewriteObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 966 | SourceLocation LocStart = PDecl->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 967 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 968 | // FIXME: handle protocol headers that are declared across multiple lines. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 969 | ReplaceText(LocStart, 0, "// "); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 970 | |
| 971 | for (ObjCProtocolDecl::instmeth_iterator |
| 972 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 973 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 974 | RewriteMethodDeclaration(*I); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 975 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 976 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 977 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 978 | RewriteMethodDeclaration(*I); |
| 979 | |
Fariborz Jahanian | 07acdf4 | 2010-09-24 18:36:58 +0000 | [diff] [blame] | 980 | for (ObjCInterfaceDecl::prop_iterator I = PDecl->prop_begin(), |
| 981 | E = PDecl->prop_end(); I != E; ++I) |
| 982 | RewriteProperty(*I); |
| 983 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 984 | // Lastly, comment out the @end. |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 985 | SourceLocation LocEnd = PDecl->getAtEndRange().getBegin(); |
Fariborz Jahanian | 73d1eb0 | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 986 | ReplaceText(LocEnd, strlen("@end"), "/* @end */"); |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 987 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 988 | // Must comment out @optional/@required |
| 989 | const char *startBuf = SM->getCharacterData(LocStart); |
| 990 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 991 | for (const char *p = startBuf; p < endBuf; p++) { |
| 992 | if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 993 | SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 994 | ReplaceText(OptionalLoc, strlen("@optional"), "/* @optional */"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 995 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 996 | } |
| 997 | else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 998 | SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 999 | ReplaceText(OptionalLoc, strlen("@required"), "/* @required */"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1000 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 1001 | } |
| 1002 | } |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1005 | void RewriteObjC::RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *PDecl) { |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 1006 | SourceLocation LocStart = PDecl->getLocation(); |
Steve Naroff | b7fa992 | 2007-11-14 03:37:28 +0000 | [diff] [blame] | 1007 | if (LocStart.isInvalid()) |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1008 | llvm_unreachable("Invalid SourceLocation"); |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 1009 | // FIXME: handle forward protocol that are declared across multiple lines. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1010 | ReplaceText(LocStart, 0, "// "); |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1013 | void RewriteObjC::RewriteTypeIntoString(QualType T, std::string &ResultStr, |
| 1014 | const FunctionType *&FPRetType) { |
| 1015 | if (T->isObjCQualifiedIdType()) |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1016 | ResultStr += "id"; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1017 | else if (T->isFunctionPointerType() || |
| 1018 | T->isBlockPointerType()) { |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1019 | // needs special handling, since pointer-to-functions have special |
| 1020 | // syntax (where a decaration models use). |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1021 | QualType retType = T; |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1022 | QualType PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1023 | if (const PointerType* PT = retType->getAs<PointerType>()) |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1024 | PointeeTy = PT->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1025 | else if (const BlockPointerType *BPT = retType->getAs<BlockPointerType>()) |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1026 | PointeeTy = BPT->getPointeeType(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1027 | if ((FPRetType = PointeeTy->getAs<FunctionType>())) { |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1028 | ResultStr += FPRetType->getResultType().getAsString( |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1029 | Context->getPrintingPolicy()); |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1030 | ResultStr += "(*"; |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1031 | } |
| 1032 | } else |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1033 | ResultStr += T.getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1036 | void RewriteObjC::RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl, |
| 1037 | ObjCMethodDecl *OMD, |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1038 | std::string &ResultStr) { |
| 1039 | //fprintf(stderr,"In RewriteObjCMethodDecl\n"); |
| 1040 | const FunctionType *FPRetType = 0; |
| 1041 | ResultStr += "\nstatic "; |
| 1042 | RewriteTypeIntoString(OMD->getResultType(), ResultStr, FPRetType); |
Fariborz Jahanian | 531a1ea | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 1043 | ResultStr += " "; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1044 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1045 | // Unique method name |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1046 | std::string NameStr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1047 | |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1048 | if (OMD->isInstanceMethod()) |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1049 | NameStr += "_I_"; |
| 1050 | else |
| 1051 | NameStr += "_C_"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1052 | |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1053 | NameStr += IDecl->getNameAsString(); |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1054 | NameStr += "_"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1055 | |
| 1056 | if (ObjCCategoryImplDecl *CID = |
Steve Naroff | 3e0a540 | 2009-01-08 19:41:02 +0000 | [diff] [blame] | 1057 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1058 | NameStr += CID->getNameAsString(); |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1059 | NameStr += "_"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1060 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1061 | // Append selector names, replacing ':' with '_' |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1062 | { |
| 1063 | std::string selString = OMD->getSelector().getAsString(); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1064 | int len = selString.size(); |
| 1065 | for (int i = 0; i < len; i++) |
| 1066 | if (selString[i] == ':') |
| 1067 | selString[i] = '_'; |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1068 | NameStr += selString; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1069 | } |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1070 | // Remember this name for metadata emission |
| 1071 | MethodInternalNames[OMD] = NameStr; |
| 1072 | ResultStr += NameStr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1073 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1074 | // Rewrite arguments |
| 1075 | ResultStr += "("; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1076 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1077 | // invisible arguments |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1078 | if (OMD->isInstanceMethod()) { |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1079 | QualType selfTy = Context->getObjCInterfaceType(IDecl); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1080 | selfTy = Context->getPointerType(selfTy); |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 1081 | if (!LangOpts.MicrosoftExt) { |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1082 | if (ObjCSynthesizedStructs.count(const_cast<ObjCInterfaceDecl*>(IDecl))) |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 1083 | ResultStr += "struct "; |
| 1084 | } |
| 1085 | // When rewriting for Microsoft, explicitly omit the structure name. |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1086 | ResultStr += IDecl->getNameAsString(); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1087 | ResultStr += " *"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1088 | } |
| 1089 | else |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1090 | ResultStr += Context->getObjCClassType().getAsString( |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1091 | Context->getPrintingPolicy()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1092 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1093 | ResultStr += " self, "; |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1094 | ResultStr += Context->getObjCSelType().getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1095 | ResultStr += " _cmd"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1096 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1097 | // Method arguments. |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1098 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 1099 | E = OMD->param_end(); PI != E; ++PI) { |
| 1100 | ParmVarDecl *PDecl = *PI; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1101 | ResultStr += ", "; |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1102 | if (PDecl->getType()->isObjCQualifiedIdType()) { |
| 1103 | ResultStr += "id "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1104 | ResultStr += PDecl->getNameAsString(); |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1105 | } else { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1106 | std::string Name = PDecl->getNameAsString(); |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 1107 | QualType QT = PDecl->getType(); |
| 1108 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
| 1109 | if (convertBlockPointerToFunctionPointer(QT)) |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1110 | QT.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 1111 | else |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1112 | PDecl->getType().getAsStringInternal(Name, Context->getPrintingPolicy()); |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1113 | ResultStr += Name; |
| 1114 | } |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1115 | } |
Fariborz Jahanian | 7c39ff7 | 2008-01-21 20:14:23 +0000 | [diff] [blame] | 1116 | if (OMD->isVariadic()) |
| 1117 | ResultStr += ", ..."; |
Fariborz Jahanian | 531a1ea | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 1118 | ResultStr += ") "; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1119 | |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1120 | if (FPRetType) { |
| 1121 | ResultStr += ")"; // close the precedence "scope" for "*". |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1122 | |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1123 | // Now, emit the argument types (if any). |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1124 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) { |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1125 | ResultStr += "("; |
| 1126 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 1127 | if (i) ResultStr += ", "; |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1128 | std::string ParamStr = FT->getArgType(i).getAsString( |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1129 | Context->getPrintingPolicy()); |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1130 | ResultStr += ParamStr; |
| 1131 | } |
| 1132 | if (FT->isVariadic()) { |
| 1133 | if (FT->getNumArgs()) ResultStr += ", "; |
| 1134 | ResultStr += "..."; |
| 1135 | } |
| 1136 | ResultStr += ")"; |
| 1137 | } else { |
| 1138 | ResultStr += "()"; |
| 1139 | } |
| 1140 | } |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1141 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1142 | void RewriteObjC::RewriteImplementationDecl(Decl *OID) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1143 | ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID); |
| 1144 | ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1145 | |
Fariborz Jahanian | a135216 | 2010-02-15 21:11:41 +0000 | [diff] [blame] | 1146 | InsertText(IMD ? IMD->getLocStart() : CID->getLocStart(), "// "); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1147 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1148 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1149 | I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(), |
| 1150 | E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1151 | I != E; ++I) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1152 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1153 | ObjCMethodDecl *OMD = *I; |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1154 | RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1155 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1156 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1157 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1158 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1159 | const char *endBuf = SM->getCharacterData(LocEnd); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1160 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1161 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1162 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1163 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1164 | I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(), |
| 1165 | E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1166 | I != E; ++I) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1167 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1168 | ObjCMethodDecl *OMD = *I; |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1169 | RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1170 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1171 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1172 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1173 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1174 | const char *endBuf = SM->getCharacterData(LocEnd); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1175 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1176 | } |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1177 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1178 | I = IMD ? IMD->propimpl_begin() : CID->propimpl_begin(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1179 | E = IMD ? IMD->propimpl_end() : CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1180 | I != E; ++I) { |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 1181 | RewritePropertyImplDecl(*I, IMD, CID); |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1184 | InsertText(IMD ? IMD->getLocEnd() : CID->getLocEnd(), "// "); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1187 | void RewriteObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { |
Steve Naroff | f908a87 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 1188 | std::string ResultStr; |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1189 | if (!ObjCForwardDecls.count(ClassDecl->getCanonicalDecl())) { |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1190 | // we haven't seen a forward decl - generate a typedef. |
Steve Naroff | 5086a8d | 2007-11-14 23:02:56 +0000 | [diff] [blame] | 1191 | ResultStr = "#ifndef _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1192 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1193 | ResultStr += "\n"; |
| 1194 | ResultStr += "#define _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1195 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1196 | ResultStr += "\n"; |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1197 | ResultStr += "typedef struct objc_object "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1198 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1199 | ResultStr += ";\n#endif\n"; |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1200 | // Mark this typedef as having been generated. |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1201 | ObjCForwardDecls.insert(ClassDecl->getCanonicalDecl()); |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1202 | } |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 1203 | RewriteObjCInternalStruct(ClassDecl, ResultStr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1204 | |
| 1205 | for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1206 | E = ClassDecl->prop_end(); I != E; ++I) |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 1207 | RewriteProperty(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1208 | for (ObjCInterfaceDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1209 | I = ClassDecl->instmeth_begin(), E = ClassDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1210 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1211 | RewriteMethodDeclaration(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1212 | for (ObjCInterfaceDecl::classmeth_iterator |
| 1213 | I = ClassDecl->classmeth_begin(), E = ClassDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1214 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1215 | RewriteMethodDeclaration(*I); |
| 1216 | |
Steve Naroff | 2feac5e | 2007-10-30 03:43:13 +0000 | [diff] [blame] | 1217 | // Lastly, comment out the @end. |
Fariborz Jahanian | 73d1eb0 | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 1218 | ReplaceText(ClassDecl->getAtEndRange().getBegin(), strlen("@end"), |
| 1219 | "/* @end */"); |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1222 | Stmt *RewriteObjC::RewritePropertyOrImplicitSetter(PseudoObjectExpr *PseudoOp) { |
| 1223 | SourceRange OldRange = PseudoOp->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1224 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1225 | // We just magically know some things about the structure of this |
| 1226 | // expression. |
| 1227 | ObjCMessageExpr *OldMsg = |
| 1228 | cast<ObjCMessageExpr>(PseudoOp->getSemanticExpr( |
| 1229 | PseudoOp->getNumSemanticExprs() - 1)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1230 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1231 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 1232 | // we need to suppress rewriting the sub-statements. |
| 1233 | Expr *Base, *RHS; |
| 1234 | { |
| 1235 | DisableReplaceStmtScope S(*this); |
| 1236 | |
| 1237 | // Rebuild the base expression if we have one. |
| 1238 | Base = 0; |
| 1239 | if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) { |
| 1240 | Base = OldMsg->getInstanceReceiver(); |
| 1241 | Base = cast<OpaqueValueExpr>(Base)->getSourceExpr(); |
| 1242 | Base = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Base)); |
| 1243 | } |
| 1244 | |
| 1245 | // Rebuild the RHS. |
| 1246 | RHS = cast<BinaryOperator>(PseudoOp->getSyntacticForm())->getRHS(); |
| 1247 | RHS = cast<OpaqueValueExpr>(RHS)->getSourceExpr(); |
| 1248 | RHS = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(RHS)); |
| 1249 | } |
| 1250 | |
| 1251 | // TODO: avoid this copy. |
| 1252 | SmallVector<SourceLocation, 1> SelLocs; |
| 1253 | OldMsg->getSelectorLocs(SelLocs); |
| 1254 | |
| 1255 | ObjCMessageExpr *NewMsg; |
| 1256 | switch (OldMsg->getReceiverKind()) { |
| 1257 | case ObjCMessageExpr::Class: |
| 1258 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1259 | OldMsg->getValueKind(), |
| 1260 | OldMsg->getLeftLoc(), |
| 1261 | OldMsg->getClassReceiverTypeInfo(), |
| 1262 | OldMsg->getSelector(), |
| 1263 | SelLocs, |
| 1264 | OldMsg->getMethodDecl(), |
| 1265 | RHS, |
| 1266 | OldMsg->getRightLoc()); |
| 1267 | break; |
| 1268 | |
| 1269 | case ObjCMessageExpr::Instance: |
| 1270 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1271 | OldMsg->getValueKind(), |
| 1272 | OldMsg->getLeftLoc(), |
| 1273 | Base, |
| 1274 | OldMsg->getSelector(), |
| 1275 | SelLocs, |
| 1276 | OldMsg->getMethodDecl(), |
| 1277 | RHS, |
| 1278 | OldMsg->getRightLoc()); |
| 1279 | break; |
| 1280 | |
| 1281 | case ObjCMessageExpr::SuperClass: |
| 1282 | case ObjCMessageExpr::SuperInstance: |
| 1283 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1284 | OldMsg->getValueKind(), |
| 1285 | OldMsg->getLeftLoc(), |
| 1286 | OldMsg->getSuperLoc(), |
| 1287 | OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance, |
| 1288 | OldMsg->getSuperType(), |
| 1289 | OldMsg->getSelector(), |
| 1290 | SelLocs, |
| 1291 | OldMsg->getMethodDecl(), |
| 1292 | RHS, |
| 1293 | OldMsg->getRightLoc()); |
| 1294 | break; |
| 1295 | } |
| 1296 | |
| 1297 | Stmt *Replacement = SynthMessageExpr(NewMsg); |
| 1298 | ReplaceStmtWithRange(PseudoOp, Replacement, OldRange); |
| 1299 | return Replacement; |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1302 | Stmt *RewriteObjC::RewritePropertyOrImplicitGetter(PseudoObjectExpr *PseudoOp) { |
| 1303 | SourceRange OldRange = PseudoOp->getSourceRange(); |
| 1304 | |
| 1305 | // We just magically know some things about the structure of this |
| 1306 | // expression. |
| 1307 | ObjCMessageExpr *OldMsg = |
| 1308 | cast<ObjCMessageExpr>(PseudoOp->getResultExpr()->IgnoreImplicit()); |
| 1309 | |
| 1310 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 1311 | // we need to suppress rewriting the sub-statements. |
| 1312 | Expr *Base = 0; |
| 1313 | { |
| 1314 | DisableReplaceStmtScope S(*this); |
| 1315 | |
| 1316 | // Rebuild the base expression if we have one. |
| 1317 | if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) { |
| 1318 | Base = OldMsg->getInstanceReceiver(); |
| 1319 | Base = cast<OpaqueValueExpr>(Base)->getSourceExpr(); |
| 1320 | Base = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Base)); |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 1321 | } |
Fariborz Jahanian | e0f8386 | 2010-10-20 16:07:20 +0000 | [diff] [blame] | 1322 | } |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1323 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1324 | // Intentionally empty. |
| 1325 | SmallVector<SourceLocation, 1> SelLocs; |
| 1326 | SmallVector<Expr*, 1> Args; |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1327 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1328 | ObjCMessageExpr *NewMsg; |
| 1329 | switch (OldMsg->getReceiverKind()) { |
| 1330 | case ObjCMessageExpr::Class: |
| 1331 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1332 | OldMsg->getValueKind(), |
| 1333 | OldMsg->getLeftLoc(), |
| 1334 | OldMsg->getClassReceiverTypeInfo(), |
| 1335 | OldMsg->getSelector(), |
| 1336 | SelLocs, |
| 1337 | OldMsg->getMethodDecl(), |
| 1338 | Args, |
| 1339 | OldMsg->getRightLoc()); |
| 1340 | break; |
| 1341 | |
| 1342 | case ObjCMessageExpr::Instance: |
| 1343 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1344 | OldMsg->getValueKind(), |
| 1345 | OldMsg->getLeftLoc(), |
| 1346 | Base, |
| 1347 | OldMsg->getSelector(), |
| 1348 | SelLocs, |
| 1349 | OldMsg->getMethodDecl(), |
| 1350 | Args, |
| 1351 | OldMsg->getRightLoc()); |
| 1352 | break; |
| 1353 | |
| 1354 | case ObjCMessageExpr::SuperClass: |
| 1355 | case ObjCMessageExpr::SuperInstance: |
| 1356 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1357 | OldMsg->getValueKind(), |
| 1358 | OldMsg->getLeftLoc(), |
| 1359 | OldMsg->getSuperLoc(), |
| 1360 | OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance, |
| 1361 | OldMsg->getSuperType(), |
| 1362 | OldMsg->getSelector(), |
| 1363 | SelLocs, |
| 1364 | OldMsg->getMethodDecl(), |
| 1365 | Args, |
| 1366 | OldMsg->getRightLoc()); |
| 1367 | break; |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1368 | } |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1369 | |
| 1370 | Stmt *Replacement = SynthMessageExpr(NewMsg); |
| 1371 | ReplaceStmtWithRange(PseudoOp, Replacement, OldRange); |
| 1372 | return Replacement; |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1375 | /// SynthCountByEnumWithState - To print: |
| 1376 | /// ((unsigned int (*) |
| 1377 | /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1378 | /// (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1379 | /// sel_registerName( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1380 | /// "countByEnumeratingWithState:objects:count:"), |
| 1381 | /// &enumState, |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1382 | /// (id *)__rw_items, (unsigned int)16) |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1383 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1384 | void RewriteObjC::SynthCountByEnumWithState(std::string &buf) { |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1385 | buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, " |
| 1386 | "id *, unsigned int))(void *)objc_msgSend)"; |
| 1387 | buf += "\n\t\t"; |
| 1388 | buf += "((id)l_collection,\n\t\t"; |
| 1389 | buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; |
| 1390 | buf += "\n\t\t"; |
| 1391 | buf += "&enumState, " |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1392 | "(id *)__rw_items, (unsigned int)16)"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1393 | } |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1394 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1395 | /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach |
| 1396 | /// statement to exit to its outer synthesized loop. |
| 1397 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1398 | Stmt *RewriteObjC::RewriteBreakStmt(BreakStmt *S) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1399 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1400 | return S; |
| 1401 | // replace break with goto __break_label |
| 1402 | std::string buf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1403 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1404 | SourceLocation startLoc = S->getLocStart(); |
| 1405 | buf = "goto __break_label_"; |
| 1406 | buf += utostr(ObjCBcLabelNo.back()); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1407 | ReplaceText(startLoc, strlen("break"), buf); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1408 | |
| 1409 | return 0; |
| 1410 | } |
| 1411 | |
| 1412 | /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach |
| 1413 | /// statement to continue with its inner synthesized loop. |
| 1414 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1415 | Stmt *RewriteObjC::RewriteContinueStmt(ContinueStmt *S) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1416 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1417 | return S; |
| 1418 | // replace continue with goto __continue_label |
| 1419 | std::string buf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1420 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1421 | SourceLocation startLoc = S->getLocStart(); |
| 1422 | buf = "goto __continue_label_"; |
| 1423 | buf += utostr(ObjCBcLabelNo.back()); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1424 | ReplaceText(startLoc, strlen("continue"), buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1425 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1426 | return 0; |
| 1427 | } |
| 1428 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1429 | /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1430 | /// It rewrites: |
| 1431 | /// for ( type elem in collection) { stmts; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1432 | |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1433 | /// Into: |
| 1434 | /// { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1435 | /// type elem; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1436 | /// struct __objcFastEnumerationState enumState = { 0 }; |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1437 | /// id __rw_items[16]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1438 | /// id l_collection = (id)collection; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1439 | /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1440 | /// objects:__rw_items count:16]; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1441 | /// if (limit) { |
| 1442 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1443 | /// do { |
| 1444 | /// unsigned long counter = 0; |
| 1445 | /// do { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1446 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1447 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1448 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1449 | /// stmts; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1450 | /// __continue_label: ; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1451 | /// } while (counter < limit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1452 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1453 | /// objects:__rw_items count:16]); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1454 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1455 | /// __break_label: ; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1456 | /// } |
| 1457 | /// else |
| 1458 | /// elem = nil; |
| 1459 | /// } |
| 1460 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1461 | Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 1462 | SourceLocation OrigEnd) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1463 | assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1464 | assert(isa<ObjCForCollectionStmt>(Stmts.back()) && |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1465 | "ObjCForCollectionStmt Statement stack mismatch"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1466 | assert(!ObjCBcLabelNo.empty() && |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1467 | "ObjCForCollectionStmt - Label No stack empty"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1468 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1469 | SourceLocation startLoc = S->getLocStart(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1470 | const char *startBuf = SM->getCharacterData(startLoc); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1471 | StringRef elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1472 | std::string elementTypeAsString; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1473 | std::string buf; |
| 1474 | buf = "\n{\n\t"; |
| 1475 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { |
| 1476 | // type elem; |
Chris Lattner | 7e24e82 | 2009-03-28 06:33:19 +0000 | [diff] [blame] | 1477 | NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl()); |
Ted Kremenek | 1ed8e2a | 2008-10-06 22:16:13 +0000 | [diff] [blame] | 1478 | QualType ElementType = cast<ValueDecl>(D)->getType(); |
Steve Naroff | e89b8e7 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1479 | if (ElementType->isObjCQualifiedIdType() || |
| 1480 | ElementType->isObjCQualifiedInterfaceType()) |
| 1481 | // Simply use 'id' for all qualified types. |
| 1482 | elementTypeAsString = "id"; |
| 1483 | else |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1484 | elementTypeAsString = ElementType.getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1485 | buf += elementTypeAsString; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1486 | buf += " "; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 1487 | elementName = D->getName(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1488 | buf += elementName; |
| 1489 | buf += ";\n\t"; |
| 1490 | } |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1491 | else { |
| 1492 | DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 1493 | elementName = DR->getDecl()->getName(); |
Steve Naroff | e89b8e7 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1494 | ValueDecl *VD = cast<ValueDecl>(DR->getDecl()); |
| 1495 | if (VD->getType()->isObjCQualifiedIdType() || |
| 1496 | VD->getType()->isObjCQualifiedInterfaceType()) |
| 1497 | // Simply use 'id' for all qualified types. |
| 1498 | elementTypeAsString = "id"; |
| 1499 | else |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1500 | elementTypeAsString = VD->getType().getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1501 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1502 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1503 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1504 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1505 | // id __rw_items[16]; |
| 1506 | buf += "id __rw_items[16];\n\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1507 | // id l_collection = (id) |
| 1508 | buf += "id l_collection = (id)"; |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1509 | // Find start location of 'collection' the hard way! |
| 1510 | const char *startCollectionBuf = startBuf; |
| 1511 | startCollectionBuf += 3; // skip 'for' |
| 1512 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1513 | startCollectionBuf++; // skip '(' |
| 1514 | // find 'in' and skip it. |
| 1515 | while (*startCollectionBuf != ' ' || |
| 1516 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1517 | (*(startCollectionBuf+3) != ' ' && |
| 1518 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1519 | startCollectionBuf++; |
| 1520 | startCollectionBuf += 3; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1521 | |
| 1522 | // Replace: "for (type element in" with string constructed thus far. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1523 | ReplaceText(startLoc, startCollectionBuf - startBuf, buf); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1524 | // Replace ')' in for '(' type elem in collection ')' with ';' |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1525 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1526 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1527 | SourceLocation lparenLoc = startLoc.getLocWithOffset(rparenBuf-startBuf); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1528 | buf = ";\n\t"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1529 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1530 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1531 | // objects:__rw_items count:16]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1532 | // which is synthesized into: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1533 | // unsigned int limit = |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1534 | // ((unsigned int (*) |
| 1535 | // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1536 | // (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1537 | // sel_registerName( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1538 | // "countByEnumeratingWithState:objects:count:"), |
| 1539 | // (struct __objcFastEnumerationState *)&state, |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1540 | // (id *)__rw_items, (unsigned int)16); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1541 | buf += "unsigned long limit =\n\t\t"; |
| 1542 | SynthCountByEnumWithState(buf); |
| 1543 | buf += ";\n\t"; |
| 1544 | /// if (limit) { |
| 1545 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1546 | /// do { |
| 1547 | /// unsigned long counter = 0; |
| 1548 | /// do { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1549 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1550 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1551 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1552 | buf += "if (limit) {\n\t"; |
| 1553 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1554 | buf += "do {\n\t\t"; |
| 1555 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1556 | buf += "do {\n\t\t\t"; |
| 1557 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1558 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1559 | buf += elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1560 | buf += " = ("; |
| 1561 | buf += elementTypeAsString; |
| 1562 | buf += ")enumState.itemsPtr[counter++];"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1563 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1564 | ReplaceText(lparenLoc, 1, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1565 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1566 | /// __continue_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1567 | /// } while (counter < limit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1568 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1569 | /// objects:__rw_items count:16]); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1570 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1571 | /// __break_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1572 | /// } |
| 1573 | /// else |
| 1574 | /// elem = nil; |
| 1575 | /// } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1576 | /// |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1577 | buf = ";\n\t"; |
| 1578 | buf += "__continue_label_"; |
| 1579 | buf += utostr(ObjCBcLabelNo.back()); |
| 1580 | buf += ": ;"; |
| 1581 | buf += "\n\t\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1582 | buf += "} while (counter < limit);\n\t"; |
| 1583 | buf += "} while (limit = "; |
| 1584 | SynthCountByEnumWithState(buf); |
| 1585 | buf += ");\n\t"; |
| 1586 | buf += elementName; |
Fariborz Jahanian | 65b0aa5 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1587 | buf += " = (("; |
| 1588 | buf += elementTypeAsString; |
| 1589 | buf += ")0);\n\t"; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1590 | buf += "__break_label_"; |
| 1591 | buf += utostr(ObjCBcLabelNo.back()); |
| 1592 | buf += ": ;\n\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1593 | buf += "}\n\t"; |
| 1594 | buf += "else\n\t\t"; |
| 1595 | buf += elementName; |
Fariborz Jahanian | 65b0aa5 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1596 | buf += " = (("; |
| 1597 | buf += elementTypeAsString; |
| 1598 | buf += ")0);\n\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1599 | buf += "}\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1600 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1601 | // Insert all these *after* the statement body. |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1602 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1603 | if (isa<CompoundStmt>(S->getBody())) { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1604 | SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1605 | InsertText(endBodyLoc, buf); |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1606 | } else { |
| 1607 | /* Need to treat single statements specially. For example: |
| 1608 | * |
| 1609 | * for (A *a in b) if (stuff()) break; |
| 1610 | * for (A *a in b) xxxyy; |
| 1611 | * |
| 1612 | * The following code simply scans ahead to the semi to find the actual end. |
| 1613 | */ |
| 1614 | const char *stmtBuf = SM->getCharacterData(OrigEnd); |
| 1615 | const char *semiBuf = strchr(stmtBuf, ';'); |
| 1616 | assert(semiBuf && "Can't find ';'"); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1617 | SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(semiBuf-stmtBuf+1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1618 | InsertText(endBodyLoc, buf); |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1619 | } |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1620 | Stmts.pop_back(); |
| 1621 | ObjCBcLabelNo.pop_back(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1622 | return 0; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1623 | } |
| 1624 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1625 | /// RewriteObjCSynchronizedStmt - |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1626 | /// This routine rewrites @synchronized(expr) stmt; |
| 1627 | /// into: |
| 1628 | /// objc_sync_enter(expr); |
| 1629 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1630 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1631 | Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1632 | // Get the start location and compute the semi location. |
| 1633 | SourceLocation startLoc = S->getLocStart(); |
| 1634 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1635 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1636 | assert((*startBuf == '@') && "bogus @synchronized location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1637 | |
| 1638 | std::string buf; |
Steve Naroff | 3498cc9 | 2008-08-21 13:03:03 +0000 | [diff] [blame] | 1639 | buf = "objc_sync_enter((id)"; |
| 1640 | const char *lparenBuf = startBuf; |
| 1641 | while (*lparenBuf != '(') lparenBuf++; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1642 | ReplaceText(startLoc, lparenBuf-startBuf+1, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1643 | // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since |
| 1644 | // the sync expression is typically a message expression that's already |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1645 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1646 | SourceLocation endLoc = S->getSynchBody()->getLocStart(); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1647 | const char *endBuf = SM->getCharacterData(endLoc); |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1648 | while (*endBuf != ')') endBuf--; |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1649 | SourceLocation rparenLoc = startLoc.getLocWithOffset(endBuf-startBuf); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1650 | buf = ");\n"; |
| 1651 | // declare a new scope with two variables, _stack and _rethrow. |
| 1652 | buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n"; |
| 1653 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1654 | buf += "char *pointers[4];} _stack;\n"; |
| 1655 | buf += "id volatile _rethrow = 0;\n"; |
| 1656 | buf += "objc_exception_try_enter(&_stack);\n"; |
| 1657 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1658 | ReplaceText(rparenLoc, 1, buf); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1659 | startLoc = S->getSynchBody()->getLocEnd(); |
| 1660 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1661 | |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1662 | assert((*startBuf == '}') && "bogus @synchronized block"); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1663 | SourceLocation lastCurlyLoc = startLoc; |
| 1664 | buf = "}\nelse {\n"; |
| 1665 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 1666 | buf += "}\n"; |
| 1667 | buf += "{ /* implicit finally clause */\n"; |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1668 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1669 | |
| 1670 | std::string syncBuf; |
| 1671 | syncBuf += " objc_sync_exit("; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 1672 | |
| 1673 | Expr *syncExpr = S->getSynchExpr(); |
| 1674 | CastKind CK = syncExpr->getType()->isObjCObjectPointerType() |
| 1675 | ? CK_BitCast : |
| 1676 | syncExpr->getType()->isBlockPointerType() |
| 1677 | ? CK_BlockPointerToObjCPointerCast |
| 1678 | : CK_CPointerToObjCPointerCast; |
| 1679 | syncExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 1680 | CK, syncExpr); |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1681 | std::string syncExprBufS; |
| 1682 | llvm::raw_string_ostream syncExprBuf(syncExprBufS); |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1683 | syncExpr->printPretty(syncExprBuf, *Context, 0, |
| 1684 | PrintingPolicy(LangOpts)); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1685 | syncBuf += syncExprBuf.str(); |
| 1686 | syncBuf += ");"; |
| 1687 | |
| 1688 | buf += syncBuf; |
| 1689 | buf += "\n if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1690 | buf += "}\n"; |
| 1691 | buf += "}"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1692 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1693 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1694 | |
| 1695 | bool hasReturns = false; |
| 1696 | HasReturnStmts(S->getSynchBody(), hasReturns); |
| 1697 | if (hasReturns) |
| 1698 | RewriteSyncReturnStmts(S->getSynchBody(), syncBuf); |
| 1699 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1700 | return 0; |
| 1701 | } |
| 1702 | |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1703 | void RewriteObjC::WarnAboutReturnGotoStmts(Stmt *S) |
| 1704 | { |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1705 | // Perform a bottom up traversal of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1706 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1707 | if (*CI) |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1708 | WarnAboutReturnGotoStmts(*CI); |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1709 | |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1710 | if (isa<ReturnStmt>(S) || isa<GotoStmt>(S)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1711 | Diags.Report(Context->getFullLoc(S->getLocStart()), |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1712 | TryFinallyContainsReturnDiag); |
| 1713 | } |
| 1714 | return; |
| 1715 | } |
| 1716 | |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1717 | void RewriteObjC::HasReturnStmts(Stmt *S, bool &hasReturns) |
| 1718 | { |
| 1719 | // Perform a bottom up traversal of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1720 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1721 | if (*CI) |
| 1722 | HasReturnStmts(*CI, hasReturns); |
| 1723 | |
| 1724 | if (isa<ReturnStmt>(S)) |
| 1725 | hasReturns = true; |
| 1726 | return; |
| 1727 | } |
| 1728 | |
| 1729 | void RewriteObjC::RewriteTryReturnStmts(Stmt *S) { |
| 1730 | // Perform a bottom up traversal of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1731 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1732 | if (*CI) { |
| 1733 | RewriteTryReturnStmts(*CI); |
| 1734 | } |
| 1735 | if (isa<ReturnStmt>(S)) { |
| 1736 | SourceLocation startLoc = S->getLocStart(); |
| 1737 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1738 | |
| 1739 | const char *semiBuf = strchr(startBuf, ';'); |
| 1740 | assert((*semiBuf == ';') && "RewriteTryReturnStmts: can't find ';'"); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1741 | SourceLocation onePastSemiLoc = startLoc.getLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1742 | |
| 1743 | std::string buf; |
| 1744 | buf = "{ objc_exception_try_exit(&_stack); return"; |
| 1745 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1746 | ReplaceText(startLoc, 6, buf); |
| 1747 | InsertText(onePastSemiLoc, "}"); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1748 | } |
| 1749 | return; |
| 1750 | } |
| 1751 | |
| 1752 | void RewriteObjC::RewriteSyncReturnStmts(Stmt *S, std::string syncExitBuf) { |
| 1753 | // Perform a bottom up traversal of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1754 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1755 | if (*CI) { |
| 1756 | RewriteSyncReturnStmts(*CI, syncExitBuf); |
| 1757 | } |
| 1758 | if (isa<ReturnStmt>(S)) { |
| 1759 | SourceLocation startLoc = S->getLocStart(); |
| 1760 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1761 | |
| 1762 | const char *semiBuf = strchr(startBuf, ';'); |
| 1763 | assert((*semiBuf == ';') && "RewriteSyncReturnStmts: can't find ';'"); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1764 | SourceLocation onePastSemiLoc = startLoc.getLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1765 | |
| 1766 | std::string buf; |
| 1767 | buf = "{ objc_exception_try_exit(&_stack);"; |
| 1768 | buf += syncExitBuf; |
| 1769 | buf += " return"; |
| 1770 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1771 | ReplaceText(startLoc, 6, buf); |
| 1772 | InsertText(onePastSemiLoc, "}"); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1773 | } |
| 1774 | return; |
| 1775 | } |
| 1776 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1777 | Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1778 | // Get the start location and compute the semi location. |
| 1779 | SourceLocation startLoc = S->getLocStart(); |
| 1780 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1781 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1782 | assert((*startBuf == '@') && "bogus @try location"); |
| 1783 | |
| 1784 | std::string buf; |
| 1785 | // declare a new scope with two variables, _stack and _rethrow. |
| 1786 | buf = "/* @try scope begin */ { struct _objc_exception_data {\n"; |
| 1787 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1788 | buf += "char *pointers[4];} _stack;\n"; |
| 1789 | buf += "id volatile _rethrow = 0;\n"; |
| 1790 | buf += "objc_exception_try_enter(&_stack);\n"; |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1791 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1792 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1793 | ReplaceText(startLoc, 4, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1794 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1795 | startLoc = S->getTryBody()->getLocEnd(); |
| 1796 | startBuf = SM->getCharacterData(startLoc); |
| 1797 | |
| 1798 | assert((*startBuf == '}') && "bogus @try block"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1799 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1800 | SourceLocation lastCurlyLoc = startLoc; |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1801 | if (S->getNumCatchStmts()) { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1802 | startLoc = startLoc.getLocWithOffset(1); |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1803 | buf = " /* @catch begin */ else {\n"; |
| 1804 | buf += " id _caught = objc_exception_extract(&_stack);\n"; |
| 1805 | buf += " objc_exception_try_enter (&_stack);\n"; |
| 1806 | buf += " if (_setjmp(_stack.buf))\n"; |
| 1807 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1808 | buf += " else { /* @catch continue */"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1809 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1810 | InsertText(startLoc, buf); |
Steve Naroff | 8bd3dc6 | 2008-09-09 19:59:12 +0000 | [diff] [blame] | 1811 | } else { /* no catch list */ |
| 1812 | buf = "}\nelse {\n"; |
| 1813 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1814 | buf += "}"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1815 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1816 | } |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1817 | Stmt *lastCatchBody = 0; |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1818 | for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I) { |
| 1819 | ObjCAtCatchStmt *Catch = S->getCatchStmt(I); |
Douglas Gregor | c00d8e1 | 2010-04-26 16:46:50 +0000 | [diff] [blame] | 1820 | VarDecl *catchDecl = Catch->getCatchParamDecl(); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1821 | |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1822 | if (I == 0) |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1823 | buf = "if ("; // we are generating code for the first catch clause |
| 1824 | else |
| 1825 | buf = "else if ("; |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1826 | startLoc = Catch->getLocStart(); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1827 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1828 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1829 | assert((*startBuf == '@') && "bogus @catch location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1830 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1831 | const char *lParenLoc = strchr(startBuf, '('); |
| 1832 | |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1833 | if (Catch->hasEllipsis()) { |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1834 | // Now rewrite the body... |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1835 | lastCatchBody = Catch->getCatchBody(); |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1836 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1837 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1838 | assert(*SM->getCharacterData(Catch->getRParenLoc()) == ')' && |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1839 | "bogus @catch paren location"); |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1840 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1841 | |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1842 | buf += "1) { id _tmp = _caught;"; |
Daniel Dunbar | d7407dc | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 1843 | Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, buf); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1844 | } else if (catchDecl) { |
| 1845 | QualType t = catchDecl->getType(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1846 | if (t == Context->getObjCIdType()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1847 | buf += "1) { "; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1848 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf); |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 1849 | } else if (const ObjCObjectPointerType *Ptr = |
| 1850 | t->getAs<ObjCObjectPointerType>()) { |
| 1851 | // Should be a pointer to a class. |
| 1852 | ObjCInterfaceDecl *IDecl = Ptr->getObjectType()->getInterface(); |
| 1853 | if (IDecl) { |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1854 | buf += "objc_exception_match((struct objc_class *)objc_getClass(\""; |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 1855 | buf += IDecl->getNameAsString(); |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1856 | buf += "\"), (struct objc_object *)_caught)) { "; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1857 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1858 | } |
| 1859 | } |
| 1860 | // Now rewrite the body... |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1861 | lastCatchBody = Catch->getCatchBody(); |
| 1862 | SourceLocation rParenLoc = Catch->getRParenLoc(); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1863 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1864 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 1865 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 1866 | assert((*rParenBuf == ')') && "bogus @catch paren location"); |
| 1867 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1868 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1869 | // Here we replace ") {" with "= _caught;" (which initializes and |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1870 | // declares the @catch parameter). |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1871 | ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, " = _caught;"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1872 | } else { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1873 | llvm_unreachable("@catch rewrite bug"); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1874 | } |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1875 | } |
| 1876 | // Complete the catch list... |
| 1877 | if (lastCatchBody) { |
| 1878 | SourceLocation bodyLoc = lastCatchBody->getLocEnd(); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1879 | assert(*SM->getCharacterData(bodyLoc) == '}' && |
| 1880 | "bogus @catch body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1881 | |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1882 | // Insert the last (implicit) else clause *before* the right curly brace. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1883 | bodyLoc = bodyLoc.getLocWithOffset(-1); |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1884 | buf = "} /* last catch end */\n"; |
| 1885 | buf += "else {\n"; |
| 1886 | buf += " _rethrow = _caught;\n"; |
| 1887 | buf += " objc_exception_try_exit(&_stack);\n"; |
| 1888 | buf += "} } /* @catch end */\n"; |
| 1889 | if (!S->getFinallyStmt()) |
| 1890 | buf += "}\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1891 | InsertText(bodyLoc, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1892 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1893 | // Set lastCurlyLoc |
| 1894 | lastCurlyLoc = lastCatchBody->getLocEnd(); |
| 1895 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1896 | if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1897 | startLoc = finalStmt->getLocStart(); |
| 1898 | startBuf = SM->getCharacterData(startLoc); |
| 1899 | assert((*startBuf == '@') && "bogus @finally start"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1900 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1901 | ReplaceText(startLoc, 8, "/* @finally */"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1902 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1903 | Stmt *body = finalStmt->getFinallyBody(); |
| 1904 | SourceLocation startLoc = body->getLocStart(); |
| 1905 | SourceLocation endLoc = body->getLocEnd(); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1906 | assert(*SM->getCharacterData(startLoc) == '{' && |
| 1907 | "bogus @finally body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1908 | assert(*SM->getCharacterData(endLoc) == '}' && |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1909 | "bogus @finally body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1910 | |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1911 | startLoc = startLoc.getLocWithOffset(1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1912 | InsertText(startLoc, " if (!_rethrow) objc_exception_try_exit(&_stack);\n"); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1913 | endLoc = endLoc.getLocWithOffset(-1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1914 | InsertText(endLoc, " if (_rethrow) objc_exception_throw(_rethrow);\n"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1915 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1916 | // Set lastCurlyLoc |
| 1917 | lastCurlyLoc = body->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1918 | |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1919 | // Now check for any return/continue/go statements within the @try. |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1920 | WarnAboutReturnGotoStmts(S->getTryBody()); |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1921 | } else { /* no finally clause - make sure we synthesize an implicit one */ |
| 1922 | buf = "{ /* implicit finally clause */\n"; |
| 1923 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
| 1924 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 1925 | buf += "}"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1926 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1927 | |
| 1928 | // Now check for any return/continue/go statements within the @try. |
| 1929 | // The implicit finally clause won't called if the @try contains any |
| 1930 | // jump statements. |
| 1931 | bool hasReturns = false; |
| 1932 | HasReturnStmts(S->getTryBody(), hasReturns); |
| 1933 | if (hasReturns) |
| 1934 | RewriteTryReturnStmts(S->getTryBody()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1935 | } |
| 1936 | // Now emit the final closing curly brace... |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1937 | lastCurlyLoc = lastCurlyLoc.getLocWithOffset(1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1938 | InsertText(lastCurlyLoc, " } /* @try scope end */\n"); |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1939 | return 0; |
| 1940 | } |
| 1941 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1942 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
| 1943 | // the throw expression is typically a message expression that's already |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1944 | // been rewritten! (which implies the SourceLocation's are invalid). |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1945 | Stmt *RewriteObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1946 | // Get the start location and compute the semi location. |
| 1947 | SourceLocation startLoc = S->getLocStart(); |
| 1948 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1949 | |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1950 | assert((*startBuf == '@') && "bogus @throw location"); |
| 1951 | |
| 1952 | std::string buf; |
| 1953 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
Steve Naroff | 20ebf8f | 2008-01-19 00:42:38 +0000 | [diff] [blame] | 1954 | if (S->getThrowExpr()) |
| 1955 | buf = "objc_exception_throw("; |
| 1956 | else // add an implicit argument |
| 1957 | buf = "objc_exception_throw(_caught"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1958 | |
Steve Naroff | 4ba0acb | 2008-07-25 15:41:30 +0000 | [diff] [blame] | 1959 | // handle "@ throw" correctly. |
| 1960 | const char *wBuf = strchr(startBuf, 'w'); |
| 1961 | assert((*wBuf == 'w') && "@throw: can't find 'w'"); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1962 | ReplaceText(startLoc, wBuf-startBuf+1, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1963 | |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1964 | const char *semiBuf = strchr(startBuf, ';'); |
| 1965 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1966 | SourceLocation semiLoc = startLoc.getLocWithOffset(semiBuf-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1967 | ReplaceText(semiLoc, 1, ");"); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1968 | return 0; |
| 1969 | } |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1970 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1971 | Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 1972 | // Create a new string expression. |
| 1973 | QualType StrType = Context->getPointerType(Context->CharTy); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1974 | std::string StrEncoding; |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1975 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 1976 | Expr *Replacement = StringLiteral::Create(*Context, StrEncoding, |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1977 | StringLiteral::Ascii, false, |
| 1978 | StrType, SourceLocation()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1979 | ReplaceStmt(Exp, Replacement); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1980 | |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 1981 | // Replace this subexpr in the parent. |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 1982 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 1983 | return Replacement; |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 1984 | } |
| 1985 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1986 | Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
Steve Naroff | 1a93764 | 2008-12-22 22:16:07 +0000 | [diff] [blame] | 1987 | if (!SelGetUidFunctionDecl) |
| 1988 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1989 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 1990 | // Create a call to sel_registerName("selName"). |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1991 | SmallVector<Expr*, 8> SelExprs; |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1992 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1993 | SelExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 1994 | Exp->getSelector().getAsString(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1995 | StringLiteral::Ascii, false, |
| 1996 | argType, SourceLocation())); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1997 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 1998 | &SelExprs[0], SelExprs.size()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1999 | ReplaceStmt(Exp, SelExp); |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2000 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2001 | return SelExp; |
| 2002 | } |
| 2003 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2004 | CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl( |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2005 | FunctionDecl *FD, Expr **args, unsigned nargs, SourceLocation StartLoc, |
| 2006 | SourceLocation EndLoc) { |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2007 | // Get the type, we will need to reference it in a couple spots. |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2008 | QualType msgSendType = FD->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2009 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2010 | // Create a reference to the objc_msgSend() declaration. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2011 | DeclRefExpr *DRE = |
| 2012 | new (Context) DeclRefExpr(FD, msgSendType, VK_LValue, SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2013 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2014 | // Now, we cast the reference to a pointer to the objc_msgSend type. |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 2015 | QualType pToFunc = Context->getPointerType(msgSendType); |
Anders Carlsson | 88465d3 | 2010-04-23 22:18:37 +0000 | [diff] [blame] | 2016 | ImplicitCastExpr *ICE = |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2017 | ImplicitCastExpr::Create(*Context, pToFunc, CK_FunctionToPointerDecay, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2018 | DRE, 0, VK_RValue); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2019 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2020 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2021 | |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2022 | CallExpr *Exp = |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 2023 | new (Context) CallExpr(*Context, ICE, args, nargs, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2024 | FT->getCallResultType(*Context), |
| 2025 | VK_RValue, EndLoc); |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2026 | return Exp; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2027 | } |
| 2028 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2029 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 2030 | const char *&startRef, const char *&endRef) { |
| 2031 | while (startBuf < endBuf) { |
| 2032 | if (*startBuf == '<') |
| 2033 | startRef = startBuf; // mark the start. |
| 2034 | if (*startBuf == '>') { |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 2035 | if (startRef && *startRef == '<') { |
| 2036 | endRef = startBuf; // mark the end. |
| 2037 | return true; |
| 2038 | } |
| 2039 | return false; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2040 | } |
| 2041 | startBuf++; |
| 2042 | } |
| 2043 | return false; |
| 2044 | } |
| 2045 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2046 | static void scanToNextArgument(const char *&argRef) { |
| 2047 | int angle = 0; |
| 2048 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 2049 | if (*argRef == '<') |
| 2050 | angle++; |
| 2051 | else if (*argRef == '>') |
| 2052 | angle--; |
| 2053 | argRef++; |
| 2054 | } |
| 2055 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 2056 | } |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2057 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2058 | bool RewriteObjC::needToScanForQualifiers(QualType T) { |
Fariborz Jahanian | 32132a0 | 2010-02-03 21:29:28 +0000 | [diff] [blame] | 2059 | if (T->isObjCQualifiedIdType()) |
| 2060 | return true; |
Fariborz Jahanian | 84aa946 | 2010-02-02 18:35:07 +0000 | [diff] [blame] | 2061 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 2062 | if (PT->getPointeeType()->isObjCQualifiedIdType()) |
| 2063 | return true; |
| 2064 | } |
| 2065 | if (T->isObjCObjectPointerType()) { |
| 2066 | T = T->getPointeeType(); |
| 2067 | return T->isObjCQualifiedInterfaceType(); |
| 2068 | } |
Fariborz Jahanian | 24f9cab | 2010-09-30 20:41:32 +0000 | [diff] [blame] | 2069 | if (T->isArrayType()) { |
| 2070 | QualType ElemTy = Context->getBaseElementType(T); |
| 2071 | return needToScanForQualifiers(ElemTy); |
| 2072 | } |
Fariborz Jahanian | 84aa946 | 2010-02-02 18:35:07 +0000 | [diff] [blame] | 2073 | return false; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2074 | } |
| 2075 | |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2076 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) { |
| 2077 | QualType Type = E->getType(); |
| 2078 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2079 | SourceLocation Loc, EndLoc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2080 | |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2081 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) { |
| 2082 | Loc = ECE->getLParenLoc(); |
| 2083 | EndLoc = ECE->getRParenLoc(); |
| 2084 | } else { |
| 2085 | Loc = E->getLocStart(); |
| 2086 | EndLoc = E->getLocEnd(); |
| 2087 | } |
| 2088 | // This will defend against trying to rewrite synthesized expressions. |
| 2089 | if (Loc.isInvalid() || EndLoc.isInvalid()) |
| 2090 | return; |
| 2091 | |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2092 | const char *startBuf = SM->getCharacterData(Loc); |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2093 | const char *endBuf = SM->getCharacterData(EndLoc); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2094 | const char *startRef = 0, *endRef = 0; |
| 2095 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2096 | // Get the locations of the startRef, endRef. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2097 | SourceLocation LessLoc = Loc.getLocWithOffset(startRef-startBuf); |
| 2098 | SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-startBuf+1); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2099 | // Comment out the protocol references. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2100 | InsertText(LessLoc, "/*"); |
| 2101 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2102 | } |
| 2103 | } |
| 2104 | } |
| 2105 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2106 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2107 | SourceLocation Loc; |
| 2108 | QualType Type; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2109 | const FunctionProtoType *proto = 0; |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2110 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 2111 | Loc = VD->getLocation(); |
| 2112 | Type = VD->getType(); |
| 2113 | } |
| 2114 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 2115 | Loc = FD->getLocation(); |
| 2116 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 2117 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2118 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2119 | assert(funcType && "missing function type"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2120 | proto = dyn_cast<FunctionProtoType>(funcType); |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2121 | if (!proto) |
| 2122 | return; |
| 2123 | Type = proto->getResultType(); |
| 2124 | } |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 2125 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(Dcl)) { |
| 2126 | Loc = FD->getLocation(); |
| 2127 | Type = FD->getType(); |
| 2128 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2129 | else |
| 2130 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2131 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2132 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2133 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2134 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2135 | const char *endBuf = SM->getCharacterData(Loc); |
| 2136 | const char *startBuf = endBuf; |
Steve Naroff | 6cafbf2 | 2008-05-31 05:02:17 +0000 | [diff] [blame] | 2137 | while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2138 | startBuf--; // scan backward (from the decl location) for return type. |
| 2139 | const char *startRef = 0, *endRef = 0; |
| 2140 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2141 | // Get the locations of the startRef, endRef. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2142 | SourceLocation LessLoc = Loc.getLocWithOffset(startRef-endBuf); |
| 2143 | SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-endBuf+1); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2144 | // Comment out the protocol references. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2145 | InsertText(LessLoc, "/*"); |
| 2146 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2147 | } |
| 2148 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2149 | if (!proto) |
| 2150 | return; // most likely, was a variable |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2151 | // Now check arguments. |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2152 | const char *startBuf = SM->getCharacterData(Loc); |
| 2153 | const char *startFuncBuf = startBuf; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2154 | for (unsigned i = 0; i < proto->getNumArgs(); i++) { |
| 2155 | if (needToScanForQualifiers(proto->getArgType(i))) { |
| 2156 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2157 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2158 | const char *endBuf = startBuf; |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2159 | // scan forward (from the decl location) for argument types. |
| 2160 | scanToNextArgument(endBuf); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2161 | const char *startRef = 0, *endRef = 0; |
| 2162 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2163 | // Get the locations of the startRef, endRef. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2164 | SourceLocation LessLoc = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2165 | Loc.getLocWithOffset(startRef-startFuncBuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2166 | SourceLocation GreaterLoc = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2167 | Loc.getLocWithOffset(endRef-startFuncBuf+1); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2168 | // Comment out the protocol references. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2169 | InsertText(LessLoc, "/*"); |
| 2170 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2171 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2172 | startBuf = ++endBuf; |
| 2173 | } |
| 2174 | else { |
Steve Naroff | aba49d1 | 2008-08-06 15:58:23 +0000 | [diff] [blame] | 2175 | // If the function name is derived from a macro expansion, then the |
| 2176 | // argument buffer will not follow the name. Need to speak with Chris. |
| 2177 | while (*startBuf && *startBuf != ')' && *startBuf != ',') |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2178 | startBuf++; // scan forward (from the decl location) for argument types. |
| 2179 | startBuf++; |
| 2180 | } |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2181 | } |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2182 | } |
| 2183 | |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2184 | void RewriteObjC::RewriteTypeOfDecl(VarDecl *ND) { |
| 2185 | QualType QT = ND->getType(); |
| 2186 | const Type* TypePtr = QT->getAs<Type>(); |
| 2187 | if (!isa<TypeOfExprType>(TypePtr)) |
| 2188 | return; |
| 2189 | while (isa<TypeOfExprType>(TypePtr)) { |
| 2190 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 2191 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 2192 | TypePtr = QT->getAs<Type>(); |
| 2193 | } |
| 2194 | // FIXME. This will not work for multiple declarators; as in: |
| 2195 | // __typeof__(a) b,c,d; |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2196 | std::string TypeAsString(QT.getAsString(Context->getPrintingPolicy())); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2197 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
| 2198 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 2199 | if (ND->getInit()) { |
| 2200 | std::string Name(ND->getNameAsString()); |
| 2201 | TypeAsString += " " + Name + " = "; |
| 2202 | Expr *E = ND->getInit(); |
| 2203 | SourceLocation startLoc; |
| 2204 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 2205 | startLoc = ECE->getLParenLoc(); |
| 2206 | else |
| 2207 | startLoc = E->getLocStart(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 2208 | startLoc = SM->getExpansionLoc(startLoc); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2209 | const char *endBuf = SM->getCharacterData(startLoc); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2210 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2211 | } |
| 2212 | else { |
| 2213 | SourceLocation X = ND->getLocEnd(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 2214 | X = SM->getExpansionLoc(X); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2215 | const char *endBuf = SM->getCharacterData(X); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2216 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2217 | } |
| 2218 | } |
| 2219 | |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2220 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2221 | void RewriteObjC::SynthSelGetUidFunctionDecl() { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2222 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2223 | SmallVector<QualType, 16> ArgTys; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2224 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2225 | QualType getFuncType = |
| 2226 | getSimpleFunctionType(Context->getObjCSelType(), &ArgTys[0], ArgTys.size()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2227 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2228 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2229 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2230 | SelGetUidIdent, getFuncType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2231 | SC_Extern, |
| 2232 | SC_None, false); |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2233 | } |
| 2234 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2235 | void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2236 | // declared in <objc/objc.h> |
Douglas Gregor | 51efe56 | 2009-01-09 01:47:02 +0000 | [diff] [blame] | 2237 | if (FD->getIdentifier() && |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 2238 | FD->getName() == "sel_registerName") { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2239 | SelGetUidFunctionDecl = FD; |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2240 | return; |
| 2241 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2242 | RewriteObjCQualifiedInterfaceTypes(FD); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2243 | } |
| 2244 | |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2245 | void RewriteObjC::RewriteBlockPointerType(std::string& Str, QualType Type) { |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2246 | std::string TypeString(Type.getAsString(Context->getPrintingPolicy())); |
Fariborz Jahanian | 52b2e1e | 2010-02-12 17:52:31 +0000 | [diff] [blame] | 2247 | const char *argPtr = TypeString.c_str(); |
| 2248 | if (!strchr(argPtr, '^')) { |
| 2249 | Str += TypeString; |
| 2250 | return; |
| 2251 | } |
| 2252 | while (*argPtr) { |
| 2253 | Str += (*argPtr == '^' ? '*' : *argPtr); |
| 2254 | argPtr++; |
| 2255 | } |
| 2256 | } |
| 2257 | |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2258 | // FIXME. Consolidate this routine with RewriteBlockPointerType. |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2259 | void RewriteObjC::RewriteBlockPointerTypeVariable(std::string& Str, |
| 2260 | ValueDecl *VD) { |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2261 | QualType Type = VD->getType(); |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2262 | std::string TypeString(Type.getAsString(Context->getPrintingPolicy())); |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2263 | const char *argPtr = TypeString.c_str(); |
| 2264 | int paren = 0; |
| 2265 | while (*argPtr) { |
| 2266 | switch (*argPtr) { |
| 2267 | case '(': |
| 2268 | Str += *argPtr; |
| 2269 | paren++; |
| 2270 | break; |
| 2271 | case ')': |
| 2272 | Str += *argPtr; |
| 2273 | paren--; |
| 2274 | break; |
| 2275 | case '^': |
| 2276 | Str += '*'; |
| 2277 | if (paren == 1) |
| 2278 | Str += VD->getNameAsString(); |
| 2279 | break; |
| 2280 | default: |
| 2281 | Str += *argPtr; |
| 2282 | break; |
| 2283 | } |
| 2284 | argPtr++; |
| 2285 | } |
| 2286 | } |
| 2287 | |
| 2288 | |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2289 | void RewriteObjC::RewriteBlockLiteralFunctionDecl(FunctionDecl *FD) { |
| 2290 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
| 2291 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
| 2292 | const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(funcType); |
| 2293 | if (!proto) |
| 2294 | return; |
| 2295 | QualType Type = proto->getResultType(); |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2296 | std::string FdStr = Type.getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2297 | FdStr += " "; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 2298 | FdStr += FD->getName(); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2299 | FdStr += "("; |
| 2300 | unsigned numArgs = proto->getNumArgs(); |
| 2301 | for (unsigned i = 0; i < numArgs; i++) { |
| 2302 | QualType ArgType = proto->getArgType(i); |
Fariborz Jahanian | 52b2e1e | 2010-02-12 17:52:31 +0000 | [diff] [blame] | 2303 | RewriteBlockPointerType(FdStr, ArgType); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2304 | if (i+1 < numArgs) |
| 2305 | FdStr += ", "; |
| 2306 | } |
| 2307 | FdStr += ");\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2308 | InsertText(FunLocStart, FdStr); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2309 | CurFunctionDeclToDeclareForBlock = 0; |
| 2310 | } |
| 2311 | |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2312 | // SynthSuperContructorFunctionDecl - id objc_super(id obj, id super); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2313 | void RewriteObjC::SynthSuperContructorFunctionDecl() { |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2314 | if (SuperContructorFunctionDecl) |
| 2315 | return; |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2316 | IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2317 | SmallVector<QualType, 16> ArgTys; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2318 | QualType argT = Context->getObjCIdType(); |
| 2319 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2320 | ArgTys.push_back(argT); |
| 2321 | ArgTys.push_back(argT); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2322 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2323 | &ArgTys[0], ArgTys.size()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2324 | SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2325 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2326 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2327 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2328 | SC_Extern, |
| 2329 | SC_None, false); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2330 | } |
| 2331 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2332 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2333 | void RewriteObjC::SynthMsgSendFunctionDecl() { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2334 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2335 | SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2336 | QualType argT = Context->getObjCIdType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2337 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2338 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2339 | argT = Context->getObjCSelType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2340 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2341 | ArgTys.push_back(argT); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2342 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2343 | &ArgTys[0], ArgTys.size(), |
| 2344 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2345 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2346 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2347 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2348 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2349 | SC_Extern, |
| 2350 | SC_None, false); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2351 | } |
| 2352 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2353 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2354 | void RewriteObjC::SynthMsgSendSuperFunctionDecl() { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2355 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2356 | SmallVector<QualType, 16> ArgTys; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2357 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2358 | SourceLocation(), SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2359 | &Context->Idents.get("objc_super")); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2360 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2361 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2362 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2363 | argT = Context->getObjCSelType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2364 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2365 | ArgTys.push_back(argT); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2366 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2367 | &ArgTys[0], ArgTys.size(), |
| 2368 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2369 | MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2370 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2371 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2372 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2373 | SC_Extern, |
| 2374 | SC_None, false); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2375 | } |
| 2376 | |
Fariborz Jahanian | 336c8f7 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2377 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2378 | void RewriteObjC::SynthMsgSendStretFunctionDecl() { |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2379 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2380 | SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2381 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2382 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2383 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2384 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2385 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2386 | ArgTys.push_back(argT); |
Fariborz Jahanian | 336c8f7 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2387 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2388 | &ArgTys[0], ArgTys.size(), |
| 2389 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2390 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2391 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2392 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2393 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2394 | SC_Extern, |
| 2395 | SC_None, false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2396 | } |
| 2397 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2398 | // SynthMsgSendSuperStretFunctionDecl - |
Fariborz Jahanian | 336c8f7 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2399 | // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2400 | void RewriteObjC::SynthMsgSendSuperStretFunctionDecl() { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2401 | IdentifierInfo *msgSendIdent = |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2402 | &Context->Idents.get("objc_msgSendSuper_stret"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2403 | SmallVector<QualType, 16> ArgTys; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2404 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2405 | SourceLocation(), SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2406 | &Context->Idents.get("objc_super")); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2407 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2408 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2409 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2410 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2411 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2412 | ArgTys.push_back(argT); |
Fariborz Jahanian | 336c8f7 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2413 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2414 | &ArgTys[0], ArgTys.size(), |
| 2415 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2416 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2417 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2418 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2419 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2420 | SC_Extern, |
| 2421 | SC_None, false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2422 | } |
| 2423 | |
Steve Naroff | 1284db8 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2424 | // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2425 | void RewriteObjC::SynthMsgSendFpretFunctionDecl() { |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2426 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2427 | SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2428 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2429 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2430 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2431 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2432 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2433 | ArgTys.push_back(argT); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2434 | QualType msgSendType = getSimpleFunctionType(Context->DoubleTy, |
| 2435 | &ArgTys[0], ArgTys.size(), |
| 2436 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2437 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2438 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2439 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2440 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2441 | SC_Extern, |
| 2442 | SC_None, false); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2443 | } |
| 2444 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2445 | // SynthGetClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2446 | void RewriteObjC::SynthGetClassFunctionDecl() { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2447 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2448 | SmallVector<QualType, 16> ArgTys; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2449 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2450 | QualType getClassType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2451 | &ArgTys[0], ArgTys.size()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2452 | GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2453 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2454 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2455 | getClassIdent, getClassType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2456 | SC_Extern, |
| 2457 | SC_None, false); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2458 | } |
| 2459 | |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2460 | // SynthGetSuperClassFunctionDecl - Class class_getSuperclass(Class cls); |
| 2461 | void RewriteObjC::SynthGetSuperClassFunctionDecl() { |
| 2462 | IdentifierInfo *getSuperClassIdent = |
| 2463 | &Context->Idents.get("class_getSuperclass"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2464 | SmallVector<QualType, 16> ArgTys; |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2465 | ArgTys.push_back(Context->getObjCClassType()); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2466 | QualType getClassType = getSimpleFunctionType(Context->getObjCClassType(), |
| 2467 | &ArgTys[0], ArgTys.size()); |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2468 | GetSuperClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2469 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2470 | SourceLocation(), |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2471 | getSuperClassIdent, |
| 2472 | getClassType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2473 | SC_Extern, |
| 2474 | SC_None, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2475 | false); |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2476 | } |
| 2477 | |
Fariborz Jahanian | 97bbab2 | 2011-12-21 19:48:07 +0000 | [diff] [blame] | 2478 | // SynthGetMetaClassFunctionDecl - id objc_getMetaClass(const char *name); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2479 | void RewriteObjC::SynthGetMetaClassFunctionDecl() { |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2480 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2481 | SmallVector<QualType, 16> ArgTys; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2482 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2483 | QualType getClassType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2484 | &ArgTys[0], ArgTys.size()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2485 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2486 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2487 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2488 | getClassIdent, getClassType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2489 | SC_Extern, |
| 2490 | SC_None, false); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2491 | } |
| 2492 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2493 | Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2494 | QualType strType = getConstantStringStructType(); |
| 2495 | |
| 2496 | std::string S = "__NSConstantStringImpl_"; |
Steve Naroff | 7691d9b | 2008-05-31 03:35:42 +0000 | [diff] [blame] | 2497 | |
| 2498 | std::string tmpName = InFileName; |
| 2499 | unsigned i; |
| 2500 | for (i=0; i < tmpName.length(); i++) { |
| 2501 | char c = tmpName.at(i); |
| 2502 | // replace any non alphanumeric characters with '_'. |
| 2503 | if (!isalpha(c) && (c < '0' || c > '9')) |
| 2504 | tmpName[i] = '_'; |
| 2505 | } |
| 2506 | S += tmpName; |
| 2507 | S += "_"; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2508 | S += utostr(NumObjCStringLiterals++); |
| 2509 | |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2510 | Preamble += "static __NSConstantStringImpl " + S; |
| 2511 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 2512 | Preamble += "0x000007c8,"; // utf8_str |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2513 | // The pretty printer for StringLiteral handles escape characters properly. |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2514 | std::string prettyBufS; |
| 2515 | llvm::raw_string_ostream prettyBuf(prettyBufS); |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2516 | Exp->getString()->printPretty(prettyBuf, *Context, 0, |
| 2517 | PrintingPolicy(LangOpts)); |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2518 | Preamble += prettyBuf.str(); |
| 2519 | Preamble += ","; |
Steve Naroff | fd5b76f | 2009-12-06 01:48:44 +0000 | [diff] [blame] | 2520 | Preamble += utostr(Exp->getString()->getByteLength()) + "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2521 | |
| 2522 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2523 | SourceLocation(), &Context->Idents.get(S), |
| 2524 | strType, 0, SC_Static, SC_None); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2525 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, strType, VK_LValue, |
| 2526 | SourceLocation()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2527 | Expr *Unop = new (Context) UnaryOperator(DRE, UO_AddrOf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2528 | Context->getPointerType(DRE->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2529 | VK_RValue, OK_Ordinary, |
| 2530 | SourceLocation()); |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2531 | // cast to NSConstantString * |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2532 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Exp->getType(), |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2533 | CK_CPointerToObjCPointerCast, Unop); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2534 | ReplaceStmt(Exp, cast); |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2535 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2536 | return cast; |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2537 | } |
| 2538 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2539 | // struct objc_super { struct objc_object *receiver; struct objc_class *super; }; |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2540 | QualType RewriteObjC::getSuperStructType() { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2541 | if (!SuperStructDecl) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2542 | SuperStructDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2543 | SourceLocation(), SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2544 | &Context->Idents.get("objc_super")); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2545 | QualType FieldTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2546 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2547 | // struct objc_object *receiver; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2548 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2549 | // struct objc_class *super; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2550 | FieldTypes[1] = Context->getObjCClassType(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2551 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2552 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2553 | for (unsigned i = 0; i < 2; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2554 | SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2555 | SourceLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2556 | SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2557 | FieldTypes[i], 0, |
| 2558 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2559 | /*Mutable=*/false, |
| 2560 | /*HasInit=*/false)); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2561 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2562 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2563 | SuperStructDecl->completeDefinition(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2564 | } |
| 2565 | return Context->getTagDeclType(SuperStructDecl); |
| 2566 | } |
| 2567 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2568 | QualType RewriteObjC::getConstantStringStructType() { |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2569 | if (!ConstantStringDecl) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2570 | ConstantStringDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2571 | SourceLocation(), SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2572 | &Context->Idents.get("__NSConstantStringImpl")); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2573 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2574 | |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2575 | // struct objc_object *receiver; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2576 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2577 | // int flags; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2578 | FieldTypes[1] = Context->IntTy; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2579 | // char *str; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2580 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2581 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2582 | FieldTypes[3] = Context->LongTy; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2583 | |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2584 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2585 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2586 | ConstantStringDecl->addDecl(FieldDecl::Create(*Context, |
| 2587 | ConstantStringDecl, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2588 | SourceLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2589 | SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2590 | FieldTypes[i], 0, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2591 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2592 | /*Mutable=*/true, |
| 2593 | /*HasInit=*/false)); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2594 | } |
| 2595 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2596 | ConstantStringDecl->completeDefinition(); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2597 | } |
| 2598 | return Context->getTagDeclType(ConstantStringDecl); |
| 2599 | } |
| 2600 | |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2601 | Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp, |
| 2602 | SourceLocation StartLoc, |
| 2603 | SourceLocation EndLoc) { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2604 | if (!SelGetUidFunctionDecl) |
| 2605 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2606 | if (!MsgSendFunctionDecl) |
| 2607 | SynthMsgSendFunctionDecl(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2608 | if (!MsgSendSuperFunctionDecl) |
| 2609 | SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2610 | if (!MsgSendStretFunctionDecl) |
| 2611 | SynthMsgSendStretFunctionDecl(); |
| 2612 | if (!MsgSendSuperStretFunctionDecl) |
| 2613 | SynthMsgSendSuperStretFunctionDecl(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2614 | if (!MsgSendFpretFunctionDecl) |
| 2615 | SynthMsgSendFpretFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2616 | if (!GetClassFunctionDecl) |
| 2617 | SynthGetClassFunctionDecl(); |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2618 | if (!GetSuperClassFunctionDecl) |
| 2619 | SynthGetSuperClassFunctionDecl(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2620 | if (!GetMetaClassFunctionDecl) |
| 2621 | SynthGetMetaClassFunctionDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2622 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2623 | // default to objc_msgSend(). |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2624 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2625 | // May need to use objc_msgSend_stret() as well. |
| 2626 | FunctionDecl *MsgSendStretFlavor = 0; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2627 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
| 2628 | QualType resultType = mDecl->getResultType(); |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 2629 | if (resultType->isRecordType()) |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2630 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
Chris Lattner | 8b51fd7 | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2631 | else if (resultType->isRealFloatingType()) |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2632 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2633 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2634 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2635 | // Synthesize a call to objc_msgSend(). |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2636 | SmallVector<Expr*, 8> MsgExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2637 | switch (Exp->getReceiverKind()) { |
| 2638 | case ObjCMessageExpr::SuperClass: { |
| 2639 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2640 | if (MsgSendStretFlavor) |
| 2641 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2642 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2643 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2644 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2645 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2646 | SmallVector<Expr*, 4> InitExprs; |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2647 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2648 | // set the receiver to self, the first argument to all methods. |
| 2649 | InitExprs.push_back( |
| 2650 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2651 | CK_BitCast, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2652 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2653 | Context->getObjCIdType(), |
| 2654 | VK_RValue, |
| 2655 | SourceLocation())) |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2656 | ); // set the 'receiver'. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2657 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2658 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2659 | SmallVector<Expr*, 8> ClsExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2660 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2661 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2662 | ClassDecl->getIdentifier()->getName(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2663 | StringLiteral::Ascii, false, |
| 2664 | argType, SourceLocation())); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2665 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
| 2666 | &ClsExprs[0], |
| 2667 | ClsExprs.size(), |
| 2668 | StartLoc, |
| 2669 | EndLoc); |
| 2670 | // (Class)objc_getClass("CurrentClass") |
| 2671 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 2672 | Context->getObjCClassType(), |
Fariborz Jahanian | 97bbab2 | 2011-12-21 19:48:07 +0000 | [diff] [blame] | 2673 | CK_BitCast, Cls); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2674 | ClsExprs.clear(); |
| 2675 | ClsExprs.push_back(ArgExpr); |
| 2676 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 2677 | &ClsExprs[0], ClsExprs.size(), |
| 2678 | StartLoc, EndLoc); |
| 2679 | |
| 2680 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2681 | // To turn off a warning, type-cast to 'id' |
| 2682 | InitExprs.push_back( // set 'super class', using class_getSuperclass(). |
| 2683 | NoTypeInfoCStyleCastExpr(Context, |
| 2684 | Context->getObjCIdType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2685 | CK_BitCast, Cls)); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2686 | // struct objc_super |
| 2687 | QualType superType = getSuperStructType(); |
| 2688 | Expr *SuperRep; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2689 | |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 2690 | if (LangOpts.MicrosoftExt) { |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2691 | SynthSuperContructorFunctionDecl(); |
| 2692 | // Simulate a contructor call... |
| 2693 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2694 | superType, VK_LValue, |
| 2695 | SourceLocation()); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2696 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
| 2697 | InitExprs.size(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2698 | superType, VK_LValue, |
| 2699 | SourceLocation()); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2700 | // The code for super is a little tricky to prevent collision with |
| 2701 | // the structure definition in the header. The rewriter has it's own |
| 2702 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2703 | // we need the cast below. For example: |
| 2704 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2705 | // |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2706 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2707 | Context->getPointerType(SuperRep->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2708 | VK_RValue, OK_Ordinary, |
| 2709 | SourceLocation()); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2710 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2711 | Context->getPointerType(superType), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2712 | CK_BitCast, SuperRep); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2713 | } else { |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2714 | // (struct objc_super) { <exprs from above> } |
| 2715 | InitListExpr *ILE = |
| 2716 | new (Context) InitListExpr(*Context, SourceLocation(), |
| 2717 | &InitExprs[0], InitExprs.size(), |
| 2718 | SourceLocation()); |
| 2719 | TypeSourceInfo *superTInfo |
| 2720 | = Context->getTrivialTypeSourceInfo(superType); |
| 2721 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2722 | superType, VK_LValue, |
| 2723 | ILE, false); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2724 | // struct objc_super * |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2725 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2726 | Context->getPointerType(SuperRep->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2727 | VK_RValue, OK_Ordinary, |
| 2728 | SourceLocation()); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2729 | } |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2730 | MsgExprs.push_back(SuperRep); |
| 2731 | break; |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2732 | } |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2733 | |
| 2734 | case ObjCMessageExpr::Class: { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2735 | SmallVector<Expr*, 8> ClsExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2736 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2737 | ObjCInterfaceDecl *Class |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 2738 | = Exp->getClassReceiver()->getAs<ObjCObjectType>()->getInterface(); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2739 | IdentifierInfo *clsName = Class->getIdentifier(); |
| 2740 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2741 | clsName->getName(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2742 | StringLiteral::Ascii, false, |
Anders Carlsson | 3e2193c | 2011-04-14 00:40:03 +0000 | [diff] [blame] | 2743 | argType, SourceLocation())); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2744 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2745 | &ClsExprs[0], |
| 2746 | ClsExprs.size(), |
| 2747 | StartLoc, EndLoc); |
| 2748 | MsgExprs.push_back(Cls); |
| 2749 | break; |
| 2750 | } |
| 2751 | |
| 2752 | case ObjCMessageExpr::SuperInstance:{ |
| 2753 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2754 | if (MsgSendStretFlavor) |
| 2755 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2756 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 2757 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2758 | SmallVector<Expr*, 4> InitExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2759 | |
| 2760 | InitExprs.push_back( |
| 2761 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2762 | CK_BitCast, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2763 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2764 | Context->getObjCIdType(), |
| 2765 | VK_RValue, SourceLocation())) |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2766 | ); // set the 'receiver'. |
| 2767 | |
| 2768 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2769 | SmallVector<Expr*, 8> ClsExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2770 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2771 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2772 | ClassDecl->getIdentifier()->getName(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2773 | StringLiteral::Ascii, false, argType, |
| 2774 | SourceLocation())); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2775 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2776 | &ClsExprs[0], |
| 2777 | ClsExprs.size(), |
| 2778 | StartLoc, EndLoc); |
| 2779 | // (Class)objc_getClass("CurrentClass") |
| 2780 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 2781 | Context->getObjCClassType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2782 | CK_BitCast, Cls); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2783 | ClsExprs.clear(); |
| 2784 | ClsExprs.push_back(ArgExpr); |
| 2785 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 2786 | &ClsExprs[0], ClsExprs.size(), |
| 2787 | StartLoc, EndLoc); |
| 2788 | |
| 2789 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2790 | // To turn off a warning, type-cast to 'id' |
| 2791 | InitExprs.push_back( |
| 2792 | // set 'super class', using class_getSuperclass(). |
| 2793 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2794 | CK_BitCast, Cls)); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2795 | // struct objc_super |
| 2796 | QualType superType = getSuperStructType(); |
| 2797 | Expr *SuperRep; |
| 2798 | |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 2799 | if (LangOpts.MicrosoftExt) { |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2800 | SynthSuperContructorFunctionDecl(); |
| 2801 | // Simulate a contructor call... |
| 2802 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2803 | superType, VK_LValue, |
| 2804 | SourceLocation()); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2805 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
| 2806 | InitExprs.size(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2807 | superType, VK_LValue, SourceLocation()); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2808 | // The code for super is a little tricky to prevent collision with |
| 2809 | // the structure definition in the header. The rewriter has it's own |
| 2810 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2811 | // we need the cast below. For example: |
| 2812 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2813 | // |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2814 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2815 | Context->getPointerType(SuperRep->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2816 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2817 | SourceLocation()); |
| 2818 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2819 | Context->getPointerType(superType), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2820 | CK_BitCast, SuperRep); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2821 | } else { |
| 2822 | // (struct objc_super) { <exprs from above> } |
| 2823 | InitListExpr *ILE = |
| 2824 | new (Context) InitListExpr(*Context, SourceLocation(), |
| 2825 | &InitExprs[0], InitExprs.size(), |
| 2826 | SourceLocation()); |
| 2827 | TypeSourceInfo *superTInfo |
| 2828 | = Context->getTrivialTypeSourceInfo(superType); |
| 2829 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2830 | superType, VK_RValue, ILE, |
| 2831 | false); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2832 | } |
| 2833 | MsgExprs.push_back(SuperRep); |
| 2834 | break; |
| 2835 | } |
| 2836 | |
| 2837 | case ObjCMessageExpr::Instance: { |
| 2838 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 2839 | // Foo<Proto> *. |
| 2840 | Expr *recExpr = Exp->getInstanceReceiver(); |
| 2841 | while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr)) |
| 2842 | recExpr = CE->getSubExpr(); |
Fariborz Jahanian | baac1ea | 2011-10-07 17:17:45 +0000 | [diff] [blame] | 2843 | CastKind CK = recExpr->getType()->isObjCObjectPointerType() |
| 2844 | ? CK_BitCast : recExpr->getType()->isBlockPointerType() |
| 2845 | ? CK_BlockPointerToObjCPointerCast |
| 2846 | : CK_CPointerToObjCPointerCast; |
| 2847 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2848 | recExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
Fariborz Jahanian | baac1ea | 2011-10-07 17:17:45 +0000 | [diff] [blame] | 2849 | CK, recExpr); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2850 | MsgExprs.push_back(recExpr); |
| 2851 | break; |
| 2852 | } |
| 2853 | } |
| 2854 | |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2855 | // Create a call to sel_registerName("selName"), it will be the 2nd argument. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2856 | SmallVector<Expr*, 8> SelExprs; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2857 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2858 | SelExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2859 | Exp->getSelector().getAsString(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2860 | StringLiteral::Ascii, false, |
| 2861 | argType, SourceLocation())); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2862 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2863 | &SelExprs[0], SelExprs.size(), |
| 2864 | StartLoc, |
| 2865 | EndLoc); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2866 | MsgExprs.push_back(SelExp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2867 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2868 | // Now push any user supplied arguments. |
| 2869 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2870 | Expr *userExpr = Exp->getArg(i); |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2871 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 2872 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 2873 | // Reuse the ICE type, it is exactly what the doctor ordered. |
Fariborz Jahanian | dff3f01 | 2011-02-26 01:31:36 +0000 | [diff] [blame] | 2874 | QualType type = ICE->getType(); |
| 2875 | if (needToScanForQualifiers(type)) |
| 2876 | type = Context->getObjCIdType(); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 2877 | // Make sure we convert "type (^)(...)" to "type (*)(...)". |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 2878 | (void)convertBlockPointerToFunctionPointer(type); |
Fariborz Jahanian | 1a38b46 | 2011-08-04 23:58:03 +0000 | [diff] [blame] | 2879 | const Expr *SubExpr = ICE->IgnoreParenImpCasts(); |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2880 | CastKind CK; |
| 2881 | if (SubExpr->getType()->isIntegralType(*Context) && |
| 2882 | type->isBooleanType()) { |
| 2883 | CK = CK_IntegralToBoolean; |
| 2884 | } else if (type->isObjCObjectPointerType()) { |
| 2885 | if (SubExpr->getType()->isBlockPointerType()) { |
| 2886 | CK = CK_BlockPointerToObjCPointerCast; |
| 2887 | } else if (SubExpr->getType()->isPointerType()) { |
| 2888 | CK = CK_CPointerToObjCPointerCast; |
| 2889 | } else { |
| 2890 | CK = CK_BitCast; |
| 2891 | } |
| 2892 | } else { |
| 2893 | CK = CK_BitCast; |
| 2894 | } |
| 2895 | |
| 2896 | userExpr = NoTypeInfoCStyleCastExpr(Context, type, CK, userExpr); |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2897 | } |
| 2898 | // Make id<P...> cast into an 'id' cast. |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2899 | else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2900 | if (CE->getType()->isObjCQualifiedIdType()) { |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2901 | while ((CE = dyn_cast<CStyleCastExpr>(userExpr))) |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2902 | userExpr = CE->getSubExpr(); |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2903 | CastKind CK; |
| 2904 | if (userExpr->getType()->isIntegralType(*Context)) { |
| 2905 | CK = CK_IntegralToPointer; |
| 2906 | } else if (userExpr->getType()->isBlockPointerType()) { |
| 2907 | CK = CK_BlockPointerToObjCPointerCast; |
| 2908 | } else if (userExpr->getType()->isPointerType()) { |
| 2909 | CK = CK_CPointerToObjCPointerCast; |
| 2910 | } else { |
| 2911 | CK = CK_BitCast; |
| 2912 | } |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2913 | userExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2914 | CK, userExpr); |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2915 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2916 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2917 | MsgExprs.push_back(userExpr); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2918 | // We've transferred the ownership to MsgExprs. For now, we *don't* null |
| 2919 | // out the argument in the original expression (since we aren't deleting |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2920 | // the ObjCMessageExpr). See RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2921 | //Exp->setArg(i, 0); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2922 | } |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2923 | // Generate the funky cast. |
| 2924 | CastExpr *cast; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2925 | SmallVector<QualType, 8> ArgTypes; |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2926 | QualType returnType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2927 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2928 | // Push 'id' and 'SEL', the 2 implicit arguments. |
Steve Naroff | c3a438c | 2007-11-15 10:43:57 +0000 | [diff] [blame] | 2929 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 2930 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 2931 | else |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2932 | ArgTypes.push_back(Context->getObjCIdType()); |
| 2933 | ArgTypes.push_back(Context->getObjCSelType()); |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2934 | if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) { |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2935 | // Push any user argument types. |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2936 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 2937 | E = OMD->param_end(); PI != E; ++PI) { |
| 2938 | QualType t = (*PI)->getType()->isObjCQualifiedIdType() |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2939 | ? Context->getObjCIdType() |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2940 | : (*PI)->getType(); |
Steve Naroff | a206b06 | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 2941 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 2942 | (void)convertBlockPointerToFunctionPointer(t); |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 2943 | ArgTypes.push_back(t); |
| 2944 | } |
Fariborz Jahanian | 3a448fb | 2011-09-10 17:01:56 +0000 | [diff] [blame] | 2945 | returnType = Exp->getType(); |
| 2946 | convertToUnqualifiedObjCType(returnType); |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 2947 | (void)convertBlockPointerToFunctionPointer(returnType); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2948 | } else { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2949 | returnType = Context->getObjCIdType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2950 | } |
| 2951 | // Get the type, we will need to reference it in a couple spots. |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2952 | QualType msgSendType = MsgSendFlavor->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2953 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2954 | // Create a reference to the objc_msgSend() declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2955 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, msgSendType, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2956 | VK_LValue, SourceLocation()); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2957 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2958 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2959 | // If we don't do this cast, we get the following bizarre warning/note: |
| 2960 | // xx.m:13: warning: function called through a non-compatible type |
| 2961 | // xx.m:13: note: if this code is reached, the program will abort |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2962 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 2963 | Context->getPointerType(Context->VoidTy), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2964 | CK_BitCast, DRE); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2965 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2966 | // Now do the "normal" pointer to function cast. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2967 | QualType castType = |
| 2968 | getSimpleFunctionType(returnType, &ArgTypes[0], ArgTypes.size(), |
| 2969 | // If we don't have a method decl, force a variadic cast. |
| 2970 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2971 | castType = Context->getPointerType(castType); |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2972 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2973 | cast); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2974 | |
| 2975 | // Don't forget the parens to enforce the proper binding. |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2976 | ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2977 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2978 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2979 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2980 | MsgExprs.size(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2981 | FT->getResultType(), VK_RValue, |
| 2982 | EndLoc); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2983 | Stmt *ReplacingStmt = CE; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2984 | if (MsgSendStretFlavor) { |
| 2985 | // We have the method which returns a struct/union. Must also generate |
| 2986 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 2987 | // expression which dictate which one to envoke depending on size of |
| 2988 | // method's return type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2989 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2990 | // Create a reference to the objc_msgSend_stret() declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2991 | DeclRefExpr *STDRE = new (Context) DeclRefExpr(MsgSendStretFlavor, msgSendType, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2992 | VK_LValue, SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2993 | // Need to cast objc_msgSend_stret to "void *" (see above comment). |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2994 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 2995 | Context->getPointerType(Context->VoidTy), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2996 | CK_BitCast, STDRE); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2997 | // Now do the "normal" pointer to function cast. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2998 | castType = getSimpleFunctionType(returnType, &ArgTypes[0], ArgTypes.size(), |
| 2999 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3000 | castType = Context->getPointerType(castType); |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3001 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3002 | cast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3003 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3004 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3005 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3006 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3007 | FT = msgSendType->getAs<FunctionType>(); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3008 | CallExpr *STCE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3009 | MsgExprs.size(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3010 | FT->getResultType(), VK_RValue, |
| 3011 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3012 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3013 | // Build sizeof(returnType) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3014 | UnaryExprOrTypeTraitExpr *sizeofExpr = |
| 3015 | new (Context) UnaryExprOrTypeTraitExpr(UETT_SizeOf, |
| 3016 | Context->getTrivialTypeSourceInfo(returnType), |
| 3017 | Context->getSizeType(), SourceLocation(), |
| 3018 | SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3019 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 3020 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 3021 | // For X86 it is more complicated and some kind of target specific routine |
| 3022 | // is needed to decide what to do. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3023 | unsigned IntSize = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3024 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 3025 | IntegerLiteral *limit = IntegerLiteral::Create(*Context, |
| 3026 | llvm::APInt(IntSize, 8), |
| 3027 | Context->IntTy, |
| 3028 | SourceLocation()); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3029 | BinaryOperator *lessThanExpr = |
| 3030 | new (Context) BinaryOperator(sizeofExpr, limit, BO_LE, Context->IntTy, |
| 3031 | VK_RValue, OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3032 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3033 | ConditionalOperator *CondExpr = |
Douglas Gregor | 47e1f7c | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 3034 | new (Context) ConditionalOperator(lessThanExpr, |
| 3035 | SourceLocation(), CE, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3036 | SourceLocation(), STCE, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3037 | returnType, VK_RValue, OK_Ordinary); |
Fariborz Jahanian | f9b949f | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 3038 | ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3039 | CondExpr); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3040 | } |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3041 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3042 | return ReplacingStmt; |
| 3043 | } |
| 3044 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3045 | Stmt *RewriteObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 3046 | Stmt *ReplacingStmt = SynthMessageExpr(Exp, Exp->getLocStart(), |
| 3047 | Exp->getLocEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3048 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 3049 | // Now do the actual rewrite. |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 3050 | ReplaceStmt(Exp, ReplacingStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3051 | |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3052 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3053 | return ReplacingStmt; |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 3054 | } |
| 3055 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3056 | // typedef struct objc_object Protocol; |
| 3057 | QualType RewriteObjC::getProtocolType() { |
| 3058 | if (!ProtocolTypeDecl) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3059 | TypeSourceInfo *TInfo |
| 3060 | = Context->getTrivialTypeSourceInfo(Context->getObjCIdType()); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3061 | ProtocolTypeDecl = TypedefDecl::Create(*Context, TUDecl, |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 3062 | SourceLocation(), SourceLocation(), |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3063 | &Context->Idents.get("Protocol"), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3064 | TInfo); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3065 | } |
| 3066 | return Context->getTypeDeclType(ProtocolTypeDecl); |
| 3067 | } |
| 3068 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 3069 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3070 | /// a synthesized/forward data reference (to the protocol's metadata). |
| 3071 | /// The forward references (and metadata) are generated in |
| 3072 | /// RewriteObjC::HandleTranslationUnit(). |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3073 | Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3074 | std::string Name = "_OBJC_PROTOCOL_" + Exp->getProtocol()->getNameAsString(); |
| 3075 | IdentifierInfo *ID = &Context->Idents.get(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3076 | VarDecl *VD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3077 | SourceLocation(), ID, getProtocolType(), 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3078 | SC_Extern, SC_None); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3079 | DeclRefExpr *DRE = new (Context) DeclRefExpr(VD, getProtocolType(), VK_LValue, |
| 3080 | SourceLocation()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3081 | Expr *DerefExpr = new (Context) UnaryOperator(DRE, UO_AddrOf, |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3082 | Context->getPointerType(DRE->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3083 | VK_RValue, OK_Ordinary, SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3084 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, DerefExpr->getType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3085 | CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3086 | DerefExpr); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3087 | ReplaceStmt(Exp, castExpr); |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 3088 | ProtocolExprDecls.insert(Exp->getProtocol()->getCanonicalDecl()); |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3089 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3090 | return castExpr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3091 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 3092 | } |
| 3093 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3094 | bool RewriteObjC::BufferContainsPPDirectives(const char *startBuf, |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3095 | const char *endBuf) { |
| 3096 | while (startBuf < endBuf) { |
| 3097 | if (*startBuf == '#') { |
| 3098 | // Skip whitespace. |
| 3099 | for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf) |
| 3100 | ; |
| 3101 | if (!strncmp(startBuf, "if", strlen("if")) || |
| 3102 | !strncmp(startBuf, "ifdef", strlen("ifdef")) || |
| 3103 | !strncmp(startBuf, "ifndef", strlen("ifndef")) || |
| 3104 | !strncmp(startBuf, "define", strlen("define")) || |
| 3105 | !strncmp(startBuf, "undef", strlen("undef")) || |
| 3106 | !strncmp(startBuf, "else", strlen("else")) || |
| 3107 | !strncmp(startBuf, "elif", strlen("elif")) || |
| 3108 | !strncmp(startBuf, "endif", strlen("endif")) || |
| 3109 | !strncmp(startBuf, "pragma", strlen("pragma")) || |
| 3110 | !strncmp(startBuf, "include", strlen("include")) || |
| 3111 | !strncmp(startBuf, "import", strlen("import")) || |
| 3112 | !strncmp(startBuf, "include_next", strlen("include_next"))) |
| 3113 | return true; |
| 3114 | } |
| 3115 | startBuf++; |
| 3116 | } |
| 3117 | return false; |
| 3118 | } |
| 3119 | |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 3120 | /// RewriteObjCInternalStruct - Rewrite one internal struct corresponding to |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3121 | /// an objective-c class with ivars. |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 3122 | void RewriteObjC::RewriteObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3123 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3124 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3125 | assert(CDecl->getName() != "" && |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 3126 | "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 3127 | // Do not synthesize more than once. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3128 | if (ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 3129 | return; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3130 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3131 | int NumIvars = CDecl->ivar_size(); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3132 | SourceLocation LocStart = CDecl->getLocStart(); |
Douglas Gregor | 05c272f | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 3133 | SourceLocation LocEnd = CDecl->getEndOfDefinitionLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3134 | |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3135 | const char *startBuf = SM->getCharacterData(LocStart); |
| 3136 | const char *endBuf = SM->getCharacterData(LocEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3137 | |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3138 | // If no ivars and no root or if its root, directly or indirectly, |
| 3139 | // have no ivars (thus not synthesized) then no need to synthesize this class. |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 3140 | if ((!CDecl->isThisDeclarationADefinition() || NumIvars == 0) && |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3141 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3142 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3143 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3144 | return; |
| 3145 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3146 | |
| 3147 | // FIXME: This has potential of causing problem. If |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3148 | // SynthesizeObjCInternalStruct is ever called recursively. |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3149 | Result += "\nstruct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3150 | Result += CDecl->getNameAsString(); |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 3151 | if (LangOpts.MicrosoftExt) |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 3152 | Result += "_IMPL"; |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 3153 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3154 | if (NumIvars > 0) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3155 | const char *cursor = strchr(startBuf, '{'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3156 | assert((cursor && endBuf) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3157 | && "SynthesizeObjCInternalStruct - malformed @interface"); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3158 | // If the buffer contains preprocessor directives, we do more fine-grained |
| 3159 | // rewrites. This is intended to fix code that looks like (which occurs in |
| 3160 | // NSURL.h, for example): |
| 3161 | // |
| 3162 | // #ifdef XYZ |
| 3163 | // @interface Foo : NSObject |
| 3164 | // #else |
| 3165 | // @interface FooBar : NSObject |
| 3166 | // #endif |
| 3167 | // { |
| 3168 | // int i; |
| 3169 | // } |
| 3170 | // @end |
| 3171 | // |
| 3172 | // This clause is segregated to avoid breaking the common case. |
| 3173 | if (BufferContainsPPDirectives(startBuf, cursor)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3174 | SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() : |
Argyrios Kyrtzidis | 1711fc9 | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 3175 | CDecl->getAtStartLoc(); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3176 | const char *endHeader = SM->getCharacterData(L); |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3177 | endHeader += Lexer::MeasureTokenLength(L, *SM, LangOpts); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3178 | |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3179 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3180 | // advance to the end of the referenced protocols. |
| 3181 | while (endHeader < cursor && *endHeader != '>') endHeader++; |
| 3182 | endHeader++; |
| 3183 | } |
| 3184 | // rewrite the original header |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3185 | ReplaceText(LocStart, endHeader-startBuf, Result); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3186 | } else { |
| 3187 | // rewrite the original header *without* disturbing the '{' |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3188 | ReplaceText(LocStart, cursor-startBuf, Result); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3189 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3190 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3191 | Result = "\n struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3192 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 39bbd9f | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 3193 | Result += "_IMPL "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3194 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 819173c | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 3195 | Result += "_IVARS;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3196 | |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3197 | // insert the super class structure definition. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 3198 | SourceLocation OnePastCurly = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3199 | LocStart.getLocWithOffset(cursor-startBuf+1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3200 | InsertText(OnePastCurly, Result); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3201 | } |
| 3202 | cursor++; // past '{' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3203 | |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3204 | // Now comment out any visibility specifiers. |
| 3205 | while (cursor < endBuf) { |
| 3206 | if (*cursor == '@') { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3207 | SourceLocation atLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Chris Lattner | df6a51b | 2007-11-14 22:57:51 +0000 | [diff] [blame] | 3208 | // Skip whitespace. |
| 3209 | for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor) |
| 3210 | /*scan*/; |
| 3211 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3212 | // FIXME: presence of @public, etc. inside comment results in |
| 3213 | // this transformation as well, which is still correct c-code. |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3214 | if (!strncmp(cursor, "public", strlen("public")) || |
| 3215 | !strncmp(cursor, "private", strlen("private")) || |
Steve Naroff | c5e3277 | 2008-04-04 22:34:24 +0000 | [diff] [blame] | 3216 | !strncmp(cursor, "package", strlen("package")) || |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3217 | !strncmp(cursor, "protected", strlen("protected"))) |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3218 | InsertText(atLoc, "// "); |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3219 | } |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3220 | // FIXME: If there are cases where '<' is used in ivar declaration part |
| 3221 | // of user code, then scan the ivar list and use needToScanForQualifiers |
| 3222 | // for type checking. |
| 3223 | else if (*cursor == '<') { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3224 | SourceLocation atLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3225 | InsertText(atLoc, "/* "); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3226 | cursor = strchr(cursor, '>'); |
| 3227 | cursor++; |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3228 | atLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3229 | InsertText(atLoc, " */"); |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3230 | } else if (*cursor == '^') { // rewrite block specifier. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3231 | SourceLocation caretLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3232 | ReplaceText(caretLoc, 1, "*"); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3233 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3234 | cursor++; |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3235 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3236 | // Don't forget to add a ';'!! |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3237 | InsertText(LocEnd.getLocWithOffset(1), ";"); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3238 | } else { // we don't have any instance variables - insert super struct. |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3239 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3240 | Result += " {\n struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3241 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 39bbd9f | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 3242 | Result += "_IMPL "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3243 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 819173c | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 3244 | Result += "_IVARS;\n};\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3245 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3246 | } |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3247 | // Mark this struct as having been generated. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3248 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3249 | llvm_unreachable("struct already synthesize- SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3250 | } |
| 3251 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3252 | //===----------------------------------------------------------------------===// |
| 3253 | // Meta Data Emission |
| 3254 | //===----------------------------------------------------------------------===// |
| 3255 | |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3256 | |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3257 | /// RewriteImplementations - This routine rewrites all method implementations |
| 3258 | /// and emits meta-data. |
| 3259 | |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3260 | void RewriteObjC::RewriteImplementations() { |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3261 | int ClsDefCount = ClassImplementation.size(); |
| 3262 | int CatDefCount = CategoryImplementation.size(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3263 | |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3264 | // Rewrite implemented methods |
| 3265 | for (int i = 0; i < ClsDefCount; i++) |
| 3266 | RewriteImplementationDecl(ClassImplementation[i]); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3267 | |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 3268 | for (int i = 0; i < CatDefCount; i++) |
| 3269 | RewriteImplementationDecl(CategoryImplementation[i]); |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3270 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3271 | |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3272 | void RewriteObjC::RewriteByRefString(std::string &ResultStr, |
| 3273 | const std::string &Name, |
Fariborz Jahanian | 1e8011e | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 3274 | ValueDecl *VD, bool def) { |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3275 | assert(BlockByRefDeclNo.count(VD) && |
| 3276 | "RewriteByRefString: ByRef decl missing"); |
Fariborz Jahanian | 1e8011e | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 3277 | if (def) |
| 3278 | ResultStr += "struct "; |
| 3279 | ResultStr += "__Block_byref_" + Name + |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3280 | "_" + utostr(BlockByRefDeclNo[VD]) ; |
| 3281 | } |
| 3282 | |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3283 | static bool HasLocalVariableExternalStorage(ValueDecl *VD) { |
| 3284 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 3285 | return (Var->isFunctionOrMethodVarDecl() && !Var->hasLocalStorage()); |
| 3286 | return false; |
| 3287 | } |
| 3288 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3289 | std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3290 | StringRef funcName, |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3291 | std::string Tag) { |
| 3292 | const FunctionType *AFT = CE->getFunctionType(); |
| 3293 | QualType RT = AFT->getResultType(); |
| 3294 | std::string StructRef = "struct " + Tag; |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 3295 | std::string S = "static " + RT.getAsString(Context->getPrintingPolicy()) + " __" + |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3296 | funcName.str() + "_" + "block_func_" + utostr(i); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 3297 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3298 | BlockDecl *BD = CE->getBlockDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3299 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3300 | if (isa<FunctionNoProtoType>(AFT)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3301 | // No user-supplied arguments. Still need to pass in a pointer to the |
Steve Naroff | df8570d | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 3302 | // block (to reference imported block decl refs). |
| 3303 | S += "(" + StructRef + " *__cself)"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3304 | } else if (BD->param_empty()) { |
| 3305 | S += "(" + StructRef + " *__cself)"; |
| 3306 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3307 | const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3308 | assert(FT && "SynthesizeBlockFunc: No function proto"); |
| 3309 | S += '('; |
| 3310 | // first add the implicit argument. |
| 3311 | S += StructRef + " *__cself, "; |
| 3312 | std::string ParamStr; |
| 3313 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 3314 | E = BD->param_end(); AI != E; ++AI) { |
| 3315 | if (AI != BD->param_begin()) S += ", "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3316 | ParamStr = (*AI)->getNameAsString(); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3317 | QualType QT = (*AI)->getType(); |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3318 | if (convertBlockPointerToFunctionPointer(QT)) |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 3319 | QT.getAsStringInternal(ParamStr, Context->getPrintingPolicy()); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3320 | else |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 3321 | QT.getAsStringInternal(ParamStr, Context->getPrintingPolicy()); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3322 | S += ParamStr; |
| 3323 | } |
| 3324 | if (FT->isVariadic()) { |
| 3325 | if (!BD->param_empty()) S += ", "; |
| 3326 | S += "..."; |
| 3327 | } |
| 3328 | S += ')'; |
| 3329 | } |
| 3330 | S += " {\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3331 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3332 | // Create local declarations to avoid rewriting all closure decl ref exprs. |
| 3333 | // First, emit a declaration for all "by ref" decls. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3334 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3335 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3336 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3337 | std::string Name = (*I)->getNameAsString(); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3338 | std::string TypeString; |
| 3339 | RewriteByRefString(TypeString, Name, (*I)); |
| 3340 | TypeString += " *"; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 3341 | Name = TypeString + Name; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3342 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3343 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3344 | // Next, emit a declaration for all "by copy" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3345 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3346 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3347 | S += " "; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3348 | // Handle nested closure invocation. For example: |
| 3349 | // |
| 3350 | // void (^myImportedClosure)(void); |
| 3351 | // myImportedClosure = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3352 | // |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3353 | // void (^anotherClosure)(void); |
| 3354 | // anotherClosure = ^(void) { |
| 3355 | // myImportedClosure(); // import and invoke the closure |
| 3356 | // }; |
| 3357 | // |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 3358 | if (isTopLevelBlockPointerType((*I)->getType())) { |
| 3359 | RewriteBlockPointerTypeVariable(S, (*I)); |
| 3360 | S += " = ("; |
| 3361 | RewriteBlockPointerType(S, (*I)->getType()); |
| 3362 | S += ")"; |
| 3363 | S += "__cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; |
| 3364 | } |
| 3365 | else { |
Fariborz Jahanian | 210c248 | 2010-02-16 17:26:03 +0000 | [diff] [blame] | 3366 | std::string Name = (*I)->getNameAsString(); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3367 | QualType QT = (*I)->getType(); |
| 3368 | if (HasLocalVariableExternalStorage(*I)) |
| 3369 | QT = Context->getPointerType(QT); |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 3370 | QT.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 3371 | S += Name + " = __cself->" + |
| 3372 | (*I)->getNameAsString() + "; // bound by copy\n"; |
| 3373 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3374 | } |
| 3375 | std::string RewrittenStr = RewrittenBlockExprs[CE]; |
| 3376 | const char *cstr = RewrittenStr.c_str(); |
| 3377 | while (*cstr++ != '{') ; |
| 3378 | S += cstr; |
| 3379 | S += "\n"; |
| 3380 | return S; |
| 3381 | } |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 3382 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3383 | std::string RewriteObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3384 | StringRef funcName, |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3385 | std::string Tag) { |
| 3386 | std::string StructRef = "struct " + Tag; |
| 3387 | std::string S = "static void __"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3388 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3389 | S += funcName; |
| 3390 | S += "_block_copy_" + utostr(i); |
| 3391 | S += "(" + StructRef; |
| 3392 | S += "*dst, " + StructRef; |
| 3393 | S += "*src) {"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3394 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3395 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3396 | ValueDecl *VD = (*I); |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3397 | S += "_Block_object_assign((void*)&dst->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3398 | S += (*I)->getNameAsString(); |
Steve Naroff | 47a2422 | 2008-12-11 20:51:38 +0000 | [diff] [blame] | 3399 | S += ", (void*)src->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3400 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 3401 | if (BlockByRefDeclsPtrSet.count((*I))) |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 3402 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3403 | else if (VD->getType()->isBlockPointerType()) |
Fariborz Jahanian | 06433c6 | 2011-07-30 01:07:55 +0000 | [diff] [blame] | 3404 | S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);"; |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3405 | else |
| 3406 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3407 | } |
Fariborz Jahanian | d25d1b5 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 3408 | S += "}\n"; |
| 3409 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3410 | S += "\nstatic void __"; |
| 3411 | S += funcName; |
| 3412 | S += "_block_dispose_" + utostr(i); |
| 3413 | S += "(" + StructRef; |
| 3414 | S += "*src) {"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3415 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3416 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3417 | ValueDecl *VD = (*I); |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3418 | S += "_Block_object_dispose((void*)src->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3419 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 3420 | if (BlockByRefDeclsPtrSet.count((*I))) |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 3421 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3422 | else if (VD->getType()->isBlockPointerType()) |
Fariborz Jahanian | 06433c6 | 2011-07-30 01:07:55 +0000 | [diff] [blame] | 3423 | S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);"; |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3424 | else |
| 3425 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3426 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3427 | S += "}\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3428 | return S; |
| 3429 | } |
| 3430 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3431 | std::string RewriteObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 3432 | std::string Desc) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3433 | std::string S = "\nstruct " + Tag; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3434 | std::string Constructor = " " + Tag; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3435 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3436 | S += " {\n struct __block_impl impl;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3437 | S += " struct " + Desc; |
| 3438 | S += "* Desc;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3439 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3440 | Constructor += "(void *fp, "; // Invoke function pointer. |
| 3441 | Constructor += "struct " + Desc; // Descriptor pointer. |
| 3442 | Constructor += " *desc"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3443 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3444 | if (BlockDeclRefs.size()) { |
| 3445 | // Output all "by copy" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3446 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3447 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3448 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3449 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3450 | std::string ArgName = "_" + FieldName; |
| 3451 | // Handle nested closure invocation. For example: |
| 3452 | // |
| 3453 | // void (^myImportedBlock)(void); |
| 3454 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3455 | // |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3456 | // void (^anotherBlock)(void); |
| 3457 | // anotherBlock = ^(void) { |
| 3458 | // myImportedBlock(); // import and invoke the closure |
| 3459 | // }; |
| 3460 | // |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3461 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3462 | S += "struct __block_impl *"; |
| 3463 | Constructor += ", void *" + ArgName; |
| 3464 | } else { |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3465 | QualType QT = (*I)->getType(); |
| 3466 | if (HasLocalVariableExternalStorage(*I)) |
| 3467 | QT = Context->getPointerType(QT); |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 3468 | QT.getAsStringInternal(FieldName, Context->getPrintingPolicy()); |
| 3469 | QT.getAsStringInternal(ArgName, Context->getPrintingPolicy()); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3470 | Constructor += ", " + ArgName; |
| 3471 | } |
| 3472 | S += FieldName + ";\n"; |
| 3473 | } |
| 3474 | // Output all "by ref" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3475 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3476 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3477 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3478 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3479 | std::string ArgName = "_" + FieldName; |
Fariborz Jahanian | 651ba52 | 2011-04-01 23:08:13 +0000 | [diff] [blame] | 3480 | { |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3481 | std::string TypeString; |
| 3482 | RewriteByRefString(TypeString, FieldName, (*I)); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 3483 | TypeString += " *"; |
| 3484 | FieldName = TypeString + FieldName; |
| 3485 | ArgName = TypeString + ArgName; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3486 | Constructor += ", " + ArgName; |
| 3487 | } |
| 3488 | S += FieldName + "; // by ref\n"; |
| 3489 | } |
| 3490 | // Finish writing the constructor. |
Fariborz Jahanian | 20432ef | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 3491 | Constructor += ", int flags=0)"; |
| 3492 | // Initialize all "by copy" arguments. |
| 3493 | bool firsTime = true; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3494 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Fariborz Jahanian | 20432ef | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 3495 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3496 | std::string Name = (*I)->getNameAsString(); |
| 3497 | if (firsTime) { |
| 3498 | Constructor += " : "; |
| 3499 | firsTime = false; |
| 3500 | } |
| 3501 | else |
| 3502 | Constructor += ", "; |
| 3503 | if (isTopLevelBlockPointerType((*I)->getType())) |
| 3504 | Constructor += Name + "((struct __block_impl *)_" + Name + ")"; |
| 3505 | else |
| 3506 | Constructor += Name + "(_" + Name + ")"; |
| 3507 | } |
| 3508 | // Initialize all "by ref" arguments. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3509 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Fariborz Jahanian | 20432ef | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 3510 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3511 | std::string Name = (*I)->getNameAsString(); |
| 3512 | if (firsTime) { |
| 3513 | Constructor += " : "; |
| 3514 | firsTime = false; |
| 3515 | } |
| 3516 | else |
| 3517 | Constructor += ", "; |
Fariborz Jahanian | 651ba52 | 2011-04-01 23:08:13 +0000 | [diff] [blame] | 3518 | Constructor += Name + "(_" + Name + "->__forwarding)"; |
Fariborz Jahanian | 20432ef | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 3519 | } |
| 3520 | |
| 3521 | Constructor += " {\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3522 | if (GlobalVarDecl) |
| 3523 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 3524 | else |
| 3525 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3526 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3527 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3528 | Constructor += " Desc = desc;\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3529 | } else { |
| 3530 | // Finish writing the constructor. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3531 | Constructor += ", int flags=0) {\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3532 | if (GlobalVarDecl) |
| 3533 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 3534 | else |
| 3535 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3536 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 3537 | Constructor += " Desc = desc;\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3538 | } |
| 3539 | Constructor += " "; |
| 3540 | Constructor += "}\n"; |
| 3541 | S += Constructor; |
| 3542 | S += "};\n"; |
| 3543 | return S; |
| 3544 | } |
| 3545 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3546 | std::string RewriteObjC::SynthesizeBlockDescriptor(std::string DescTag, |
| 3547 | std::string ImplTag, int i, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3548 | StringRef FunName, |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3549 | unsigned hasCopy) { |
| 3550 | std::string S = "\nstatic struct " + DescTag; |
| 3551 | |
| 3552 | S += " {\n unsigned long reserved;\n"; |
| 3553 | S += " unsigned long Block_size;\n"; |
| 3554 | if (hasCopy) { |
Fariborz Jahanian | 4fcc4fd | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 3555 | S += " void (*copy)(struct "; |
| 3556 | S += ImplTag; S += "*, struct "; |
| 3557 | S += ImplTag; S += "*);\n"; |
| 3558 | |
| 3559 | S += " void (*dispose)(struct "; |
| 3560 | S += ImplTag; S += "*);\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3561 | } |
| 3562 | S += "} "; |
| 3563 | |
| 3564 | S += DescTag + "_DATA = { 0, sizeof(struct "; |
| 3565 | S += ImplTag + ")"; |
| 3566 | if (hasCopy) { |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3567 | S += ", __" + FunName.str() + "_block_copy_" + utostr(i); |
| 3568 | S += ", __" + FunName.str() + "_block_dispose_" + utostr(i); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3569 | } |
| 3570 | S += "};\n"; |
| 3571 | return S; |
| 3572 | } |
| 3573 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3574 | void RewriteObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3575 | StringRef FunName) { |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 3576 | // Insert declaration for the function in which block literal is used. |
Fariborz Jahanian | bf07012 | 2010-01-15 18:14:52 +0000 | [diff] [blame] | 3577 | if (CurFunctionDeclToDeclareForBlock && !Blocks.empty()) |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 3578 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Fariborz Jahanian | 8611eb0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 3579 | bool RewriteSC = (GlobalVarDecl && |
| 3580 | !Blocks.empty() && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3581 | GlobalVarDecl->getStorageClass() == SC_Static && |
Fariborz Jahanian | 8611eb0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 3582 | GlobalVarDecl->getType().getCVRQualifiers()); |
| 3583 | if (RewriteSC) { |
| 3584 | std::string SC(" void __"); |
| 3585 | SC += GlobalVarDecl->getNameAsString(); |
| 3586 | SC += "() {}"; |
| 3587 | InsertText(FunLocStart, SC); |
| 3588 | } |
| 3589 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3590 | // Insert closures that were part of the function. |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3591 | for (unsigned i = 0, count=0; i < Blocks.size(); i++) { |
| 3592 | CollectBlockDeclRefInfo(Blocks[i]); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3593 | // Need to copy-in the inner copied-in variables not actually used in this |
| 3594 | // block. |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3595 | for (int j = 0; j < InnerDeclRefsCount[i]; j++) { |
| 3596 | BlockDeclRefExpr *Exp = InnerDeclRefs[count++]; |
| 3597 | ValueDecl *VD = Exp->getDecl(); |
| 3598 | BlockDeclRefs.push_back(Exp); |
| 3599 | if (!Exp->isByRef() && !BlockByCopyDeclsPtrSet.count(VD)) { |
| 3600 | BlockByCopyDeclsPtrSet.insert(VD); |
| 3601 | BlockByCopyDecls.push_back(VD); |
| 3602 | } |
| 3603 | if (Exp->isByRef() && !BlockByRefDeclsPtrSet.count(VD)) { |
| 3604 | BlockByRefDeclsPtrSet.insert(VD); |
| 3605 | BlockByRefDecls.push_back(VD); |
| 3606 | } |
Fariborz Jahanian | 92c8568 | 2010-10-05 18:05:06 +0000 | [diff] [blame] | 3607 | // imported objects in the inner blocks not used in the outer |
| 3608 | // blocks must be copied/disposed in the outer block as well. |
| 3609 | if (Exp->isByRef() || |
| 3610 | VD->getType()->isObjCObjectPointerType() || |
| 3611 | VD->getType()->isBlockPointerType()) |
| 3612 | ImportedBlockDecls.insert(VD); |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3613 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3614 | |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3615 | std::string ImplTag = "__" + FunName.str() + "_block_impl_" + utostr(i); |
| 3616 | std::string DescTag = "__" + FunName.str() + "_block_desc_" + utostr(i); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3617 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3618 | std::string CI = SynthesizeBlockImpl(Blocks[i], ImplTag, DescTag); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3619 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3620 | InsertText(FunLocStart, CI); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3621 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3622 | std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, ImplTag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3623 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3624 | InsertText(FunLocStart, CF); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3625 | |
| 3626 | if (ImportedBlockDecls.size()) { |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3627 | std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, ImplTag); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3628 | InsertText(FunLocStart, HF); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3629 | } |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3630 | std::string BD = SynthesizeBlockDescriptor(DescTag, ImplTag, i, FunName, |
| 3631 | ImportedBlockDecls.size() > 0); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3632 | InsertText(FunLocStart, BD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3633 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3634 | BlockDeclRefs.clear(); |
| 3635 | BlockByRefDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 3636 | BlockByRefDeclsPtrSet.clear(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3637 | BlockByCopyDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 3638 | BlockByCopyDeclsPtrSet.clear(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3639 | ImportedBlockDecls.clear(); |
| 3640 | } |
Fariborz Jahanian | 8611eb0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 3641 | if (RewriteSC) { |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3642 | // Must insert any 'const/volatile/static here. Since it has been |
| 3643 | // removed as result of rewriting of block literals. |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3644 | std::string SC; |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3645 | if (GlobalVarDecl->getStorageClass() == SC_Static) |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3646 | SC = "static "; |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3647 | if (GlobalVarDecl->getType().isConstQualified()) |
| 3648 | SC += "const "; |
| 3649 | if (GlobalVarDecl->getType().isVolatileQualified()) |
| 3650 | SC += "volatile "; |
Fariborz Jahanian | 8611eb0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 3651 | if (GlobalVarDecl->getType().isRestrictQualified()) |
| 3652 | SC += "restrict "; |
| 3653 | InsertText(FunLocStart, SC); |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3654 | } |
| 3655 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3656 | Blocks.clear(); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3657 | InnerDeclRefsCount.clear(); |
| 3658 | InnerDeclRefs.clear(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3659 | RewrittenBlockExprs.clear(); |
| 3660 | } |
| 3661 | |
| 3662 | void RewriteObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { |
| 3663 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3664 | StringRef FuncName = FD->getName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3665 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3666 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 3667 | } |
| 3668 | |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 3669 | static void BuildUniqueMethodName(std::string &Name, |
| 3670 | ObjCMethodDecl *MD) { |
| 3671 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3672 | Name = IFace->getName(); |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 3673 | Name += "__" + MD->getSelector().getAsString(); |
| 3674 | // Convert colons to underscores. |
| 3675 | std::string::size_type loc = 0; |
| 3676 | while ((loc = Name.find(":", loc)) != std::string::npos) |
| 3677 | Name.replace(loc, 1, "_"); |
| 3678 | } |
| 3679 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3680 | void RewriteObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3681 | //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n"); |
| 3682 | //SourceLocation FunLocStart = MD->getLocStart(); |
Fariborz Jahanian | 0e1c99a | 2010-01-29 01:55:49 +0000 | [diff] [blame] | 3683 | SourceLocation FunLocStart = MD->getLocStart(); |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 3684 | std::string FuncName; |
| 3685 | BuildUniqueMethodName(FuncName, MD); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3686 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3687 | } |
| 3688 | |
| 3689 | void RewriteObjC::GetBlockDeclRefExprs(Stmt *S) { |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 3690 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3691 | if (*CI) { |
| 3692 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 3693 | GetBlockDeclRefExprs(CBE->getBody()); |
| 3694 | else |
| 3695 | GetBlockDeclRefExprs(*CI); |
| 3696 | } |
| 3697 | // Handle specific things. |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3698 | if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3699 | // FIXME: Handle enums. |
| 3700 | if (!isa<FunctionDecl>(CDRE->getDecl())) |
| 3701 | BlockDeclRefs.push_back(CDRE); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3702 | } |
| 3703 | else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) |
| 3704 | if (HasLocalVariableExternalStorage(DRE->getDecl())) { |
| 3705 | BlockDeclRefExpr *BDRE = |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 3706 | new (Context)BlockDeclRefExpr(cast<VarDecl>(DRE->getDecl()), |
| 3707 | DRE->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3708 | VK_LValue, DRE->getLocation(), false); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3709 | BlockDeclRefs.push_back(BDRE); |
| 3710 | } |
| 3711 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3712 | return; |
| 3713 | } |
| 3714 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3715 | void RewriteObjC::GetInnerBlockDeclRefExprs(Stmt *S, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3716 | SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs, |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3717 | llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts) { |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 3718 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3719 | if (*CI) { |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3720 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) { |
| 3721 | InnerContexts.insert(cast<DeclContext>(CBE->getBlockDecl())); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3722 | GetInnerBlockDeclRefExprs(CBE->getBody(), |
| 3723 | InnerBlockDeclRefs, |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3724 | InnerContexts); |
| 3725 | } |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3726 | else |
| 3727 | GetInnerBlockDeclRefExprs(*CI, |
| 3728 | InnerBlockDeclRefs, |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3729 | InnerContexts); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3730 | |
| 3731 | } |
| 3732 | // Handle specific things. |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3733 | if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3734 | if (!isa<FunctionDecl>(CDRE->getDecl()) && |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3735 | !InnerContexts.count(CDRE->getDecl()->getDeclContext())) |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3736 | InnerBlockDeclRefs.push_back(CDRE); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3737 | } |
| 3738 | else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 3739 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) |
| 3740 | if (Var->isFunctionOrMethodVarDecl()) |
| 3741 | ImportedLocalExternalDecls.insert(Var); |
| 3742 | } |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3743 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3744 | return; |
| 3745 | } |
| 3746 | |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3747 | /// convertFunctionTypeOfBlocks - This routine converts a function type |
| 3748 | /// whose result type may be a block pointer or whose argument type(s) |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3749 | /// might be block pointers to an equivalent function type replacing |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3750 | /// all block pointers to function pointers. |
| 3751 | QualType RewriteObjC::convertFunctionTypeOfBlocks(const FunctionType *FT) { |
| 3752 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
| 3753 | // FTP will be null for closures that don't take arguments. |
| 3754 | // Generate a funky cast. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3755 | SmallVector<QualType, 8> ArgTypes; |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3756 | QualType Res = FT->getResultType(); |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3757 | bool HasBlockType = convertBlockPointerToFunctionPointer(Res); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3758 | |
| 3759 | if (FTP) { |
| 3760 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
| 3761 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 3762 | QualType t = *I; |
| 3763 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3764 | if (convertBlockPointerToFunctionPointer(t)) |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3765 | HasBlockType = true; |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3766 | ArgTypes.push_back(t); |
| 3767 | } |
| 3768 | } |
| 3769 | QualType FuncType; |
| 3770 | // FIXME. Does this work if block takes no argument but has a return type |
| 3771 | // which is of block type? |
| 3772 | if (HasBlockType) |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3773 | FuncType = getSimpleFunctionType(Res, &ArgTypes[0], ArgTypes.size()); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3774 | else FuncType = QualType(FT, 0); |
| 3775 | return FuncType; |
| 3776 | } |
| 3777 | |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3778 | Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp, const Expr *BlockExp) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3779 | // Navigate to relevant type information. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3780 | const BlockPointerType *CPT = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3781 | |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3782 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BlockExp)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3783 | CPT = DRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3784 | } else if (const BlockDeclRefExpr *CDRE = |
| 3785 | dyn_cast<BlockDeclRefExpr>(BlockExp)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3786 | CPT = CDRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3787 | } else if (const MemberExpr *MExpr = dyn_cast<MemberExpr>(BlockExp)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3788 | CPT = MExpr->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3789 | } |
| 3790 | else if (const ParenExpr *PRE = dyn_cast<ParenExpr>(BlockExp)) { |
| 3791 | return SynthesizeBlockCall(Exp, PRE->getSubExpr()); |
| 3792 | } |
| 3793 | else if (const ImplicitCastExpr *IEXPR = dyn_cast<ImplicitCastExpr>(BlockExp)) |
| 3794 | CPT = IEXPR->getType()->getAs<BlockPointerType>(); |
| 3795 | else if (const ConditionalOperator *CEXPR = |
| 3796 | dyn_cast<ConditionalOperator>(BlockExp)) { |
| 3797 | Expr *LHSExp = CEXPR->getLHS(); |
| 3798 | Stmt *LHSStmt = SynthesizeBlockCall(Exp, LHSExp); |
| 3799 | Expr *RHSExp = CEXPR->getRHS(); |
| 3800 | Stmt *RHSStmt = SynthesizeBlockCall(Exp, RHSExp); |
| 3801 | Expr *CONDExp = CEXPR->getCond(); |
| 3802 | ConditionalOperator *CondExpr = |
| 3803 | new (Context) ConditionalOperator(CONDExp, |
| 3804 | SourceLocation(), cast<Expr>(LHSStmt), |
Fariborz Jahanian | f9b949f | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 3805 | SourceLocation(), cast<Expr>(RHSStmt), |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3806 | Exp->getType(), VK_RValue, OK_Ordinary); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3807 | return CondExpr; |
Fariborz Jahanian | e24b22b | 2009-12-18 01:15:21 +0000 | [diff] [blame] | 3808 | } else if (const ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(BlockExp)) { |
| 3809 | CPT = IRE->getType()->getAs<BlockPointerType>(); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 3810 | } else if (const PseudoObjectExpr *POE |
| 3811 | = dyn_cast<PseudoObjectExpr>(BlockExp)) { |
| 3812 | CPT = POE->getType()->castAs<BlockPointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3813 | } else { |
| 3814 | assert(1 && "RewriteBlockClass: Bad type"); |
| 3815 | } |
| 3816 | assert(CPT && "RewriteBlockClass: Bad type"); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3817 | const FunctionType *FT = CPT->getPointeeType()->getAs<FunctionType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3818 | assert(FT && "RewriteBlockClass: Bad type"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3819 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3820 | // FTP will be null for closures that don't take arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3821 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3822 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3823 | SourceLocation(), SourceLocation(), |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3824 | &Context->Idents.get("__block_impl")); |
| 3825 | QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD)); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3826 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3827 | // Generate a funky cast. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3828 | SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3829 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3830 | // Push the block argument type. |
| 3831 | ArgTypes.push_back(PtrBlock); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3832 | if (FTP) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3833 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3834 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 3835 | QualType t = *I; |
| 3836 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 8188e5f | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 3837 | if (!convertBlockPointerToFunctionPointer(t)) |
| 3838 | convertToUnqualifiedObjCType(t); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3839 | ArgTypes.push_back(t); |
| 3840 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3841 | } |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3842 | // Now do the pointer to function cast. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3843 | QualType PtrToFuncCastType |
| 3844 | = getSimpleFunctionType(Exp->getType(), &ArgTypes[0], ArgTypes.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3845 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3846 | PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3847 | |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3848 | CastExpr *BlkCast = NoTypeInfoCStyleCastExpr(Context, PtrBlock, |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3849 | CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3850 | const_cast<Expr*>(BlockExp)); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3851 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3852 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3853 | BlkCast); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3854 | //PE->dump(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3855 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3856 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3857 | SourceLocation(), |
| 3858 | &Context->Idents.get("FuncPtr"), |
| 3859 | Context->VoidPtrTy, 0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3860 | /*BitWidth=*/0, /*Mutable=*/true, |
| 3861 | /*HasInit=*/false); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3862 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3863 | FD->getType(), VK_LValue, |
| 3864 | OK_Ordinary); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3865 | |
Fariborz Jahanian | 8188e5f | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 3866 | |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3867 | CastExpr *FunkCast = NoTypeInfoCStyleCastExpr(Context, PtrToFuncCastType, |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3868 | CK_BitCast, ME); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3869 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3870 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3871 | SmallVector<Expr*, 8> BlkExprs; |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3872 | // Add the implicit argument. |
| 3873 | BlkExprs.push_back(BlkCast); |
| 3874 | // Add the user arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3875 | for (CallExpr::arg_iterator I = Exp->arg_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3876 | E = Exp->arg_end(); I != E; ++I) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3877 | BlkExprs.push_back(*I); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3878 | } |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3879 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &BlkExprs[0], |
| 3880 | BlkExprs.size(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3881 | Exp->getType(), VK_RValue, |
| 3882 | SourceLocation()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3883 | return CE; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3884 | } |
| 3885 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3886 | // We need to return the rewritten expression to handle cases where the |
| 3887 | // BlockDeclRefExpr is embedded in another expression being rewritten. |
| 3888 | // For example: |
| 3889 | // |
| 3890 | // int main() { |
| 3891 | // __block Foo *f; |
| 3892 | // __block int i; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3893 | // |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3894 | // void (^myblock)() = ^() { |
| 3895 | // [f test]; // f is a BlockDeclRefExpr embedded in a message (which is being rewritten). |
| 3896 | // i = 77; |
| 3897 | // }; |
| 3898 | //} |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3899 | Stmt *RewriteObjC::RewriteBlockDeclRefExpr(Expr *DeclRefExp) { |
Fariborz Jahanian | bbf37e2 | 2009-12-23 19:26:34 +0000 | [diff] [blame] | 3900 | // Rewrite the byref variable into BYREFVAR->__forwarding->BYREFVAR |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3901 | // for each DeclRefExp where BYREFVAR is name of the variable. |
| 3902 | ValueDecl *VD; |
| 3903 | bool isArrow = true; |
| 3904 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(DeclRefExp)) |
| 3905 | VD = BDRE->getDecl(); |
| 3906 | else { |
| 3907 | VD = cast<DeclRefExpr>(DeclRefExp)->getDecl(); |
| 3908 | isArrow = false; |
| 3909 | } |
| 3910 | |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3911 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3912 | SourceLocation(), |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3913 | &Context->Idents.get("__forwarding"), |
| 3914 | Context->VoidPtrTy, 0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3915 | /*BitWidth=*/0, /*Mutable=*/true, |
| 3916 | /*HasInit=*/false); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3917 | MemberExpr *ME = new (Context) MemberExpr(DeclRefExp, isArrow, |
| 3918 | FD, SourceLocation(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3919 | FD->getType(), VK_LValue, |
| 3920 | OK_Ordinary); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3921 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3922 | StringRef Name = VD->getName(); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3923 | FD = FieldDecl::Create(*Context, 0, SourceLocation(), SourceLocation(), |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3924 | &Context->Idents.get(Name), |
| 3925 | Context->VoidPtrTy, 0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3926 | /*BitWidth=*/0, /*Mutable=*/true, |
| 3927 | /*HasInit=*/false); |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3928 | ME = new (Context) MemberExpr(ME, true, FD, SourceLocation(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3929 | DeclRefExp->getType(), VK_LValue, OK_Ordinary); |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3930 | |
| 3931 | |
| 3932 | |
Steve Naroff | df8570d | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 3933 | // Need parens to enforce precedence. |
Fariborz Jahanian | 380ee50 | 2011-04-01 19:19:28 +0000 | [diff] [blame] | 3934 | ParenExpr *PE = new (Context) ParenExpr(DeclRefExp->getExprLoc(), |
| 3935 | DeclRefExp->getExprLoc(), |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3936 | ME); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3937 | ReplaceStmt(DeclRefExp, PE); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3938 | return PE; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3939 | } |
| 3940 | |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3941 | // Rewrites the imported local variable V with external storage |
| 3942 | // (static, extern, etc.) as *V |
| 3943 | // |
| 3944 | Stmt *RewriteObjC::RewriteLocalVariableExternalStorage(DeclRefExpr *DRE) { |
| 3945 | ValueDecl *VD = DRE->getDecl(); |
| 3946 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 3947 | if (!ImportedLocalExternalDecls.count(Var)) |
| 3948 | return DRE; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3949 | Expr *Exp = new (Context) UnaryOperator(DRE, UO_Deref, DRE->getType(), |
| 3950 | VK_LValue, OK_Ordinary, |
| 3951 | DRE->getLocation()); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3952 | // Need parens to enforce precedence. |
| 3953 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3954 | Exp); |
| 3955 | ReplaceStmt(DRE, PE); |
| 3956 | return PE; |
| 3957 | } |
| 3958 | |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 3959 | void RewriteObjC::RewriteCastExpr(CStyleCastExpr *CE) { |
| 3960 | SourceLocation LocStart = CE->getLParenLoc(); |
| 3961 | SourceLocation LocEnd = CE->getRParenLoc(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3962 | |
| 3963 | // Need to avoid trying to rewrite synthesized casts. |
| 3964 | if (LocStart.isInvalid()) |
| 3965 | return; |
Steve Naroff | 8f6ce57 | 2008-11-03 11:20:24 +0000 | [diff] [blame] | 3966 | // Need to avoid trying to rewrite casts contained in macros. |
| 3967 | if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) |
| 3968 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3969 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3970 | const char *startBuf = SM->getCharacterData(LocStart); |
| 3971 | const char *endBuf = SM->getCharacterData(LocEnd); |
Fariborz Jahanian | 1d4fca2 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 3972 | QualType QT = CE->getType(); |
| 3973 | const Type* TypePtr = QT->getAs<Type>(); |
| 3974 | if (isa<TypeOfExprType>(TypePtr)) { |
| 3975 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 3976 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 3977 | std::string TypeAsString = "("; |
Fariborz Jahanian | afad76f | 2010-02-18 01:20:22 +0000 | [diff] [blame] | 3978 | RewriteBlockPointerType(TypeAsString, QT); |
Fariborz Jahanian | 1d4fca2 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 3979 | TypeAsString += ")"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3980 | ReplaceText(LocStart, endBuf-startBuf+1, TypeAsString); |
Fariborz Jahanian | 1d4fca2 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 3981 | return; |
| 3982 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3983 | // advance the location to startArgList. |
| 3984 | const char *argPtr = startBuf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3985 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3986 | while (*argPtr++ && (argPtr < endBuf)) { |
| 3987 | switch (*argPtr) { |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 3988 | case '^': |
| 3989 | // Replace the '^' with '*'. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3990 | LocStart = LocStart.getLocWithOffset(argPtr-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3991 | ReplaceText(LocStart, 1, "*"); |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 3992 | break; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3993 | } |
| 3994 | } |
| 3995 | return; |
| 3996 | } |
| 3997 | |
| 3998 | void RewriteObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { |
| 3999 | SourceLocation DeclLoc = FD->getLocation(); |
| 4000 | unsigned parenCount = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4001 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4002 | // We have 1 or more arguments that have closure pointers. |
| 4003 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4004 | const char *startArgList = strchr(startBuf, '('); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4005 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4006 | assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4007 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4008 | parenCount++; |
| 4009 | // advance the location to startArgList. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4010 | DeclLoc = DeclLoc.getLocWithOffset(startArgList-startBuf); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4011 | assert((DeclLoc.isValid()) && "Invalid DeclLoc"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4012 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4013 | const char *argPtr = startArgList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4014 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4015 | while (*argPtr++ && parenCount) { |
| 4016 | switch (*argPtr) { |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4017 | case '^': |
| 4018 | // Replace the '^' with '*'. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4019 | DeclLoc = DeclLoc.getLocWithOffset(argPtr-startArgList); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4020 | ReplaceText(DeclLoc, 1, "*"); |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4021 | break; |
| 4022 | case '(': |
| 4023 | parenCount++; |
| 4024 | break; |
| 4025 | case ')': |
| 4026 | parenCount--; |
| 4027 | break; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4028 | } |
| 4029 | } |
| 4030 | return; |
| 4031 | } |
| 4032 | |
| 4033 | bool RewriteObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4034 | const FunctionProtoType *FTP; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4035 | const PointerType *PT = QT->getAs<PointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4036 | if (PT) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4037 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4038 | } else { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4039 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4040 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4041 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4042 | } |
| 4043 | if (FTP) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4044 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4045 | E = FTP->arg_type_end(); I != E; ++I) |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4046 | if (isTopLevelBlockPointerType(*I)) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4047 | return true; |
| 4048 | } |
| 4049 | return false; |
| 4050 | } |
| 4051 | |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4052 | bool RewriteObjC::PointerTypeTakesAnyObjCQualifiedType(QualType QT) { |
| 4053 | const FunctionProtoType *FTP; |
| 4054 | const PointerType *PT = QT->getAs<PointerType>(); |
| 4055 | if (PT) { |
| 4056 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4057 | } else { |
| 4058 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
| 4059 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
| 4060 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4061 | } |
| 4062 | if (FTP) { |
| 4063 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Fariborz Jahanian | 06de2cf | 2010-11-03 23:50:34 +0000 | [diff] [blame] | 4064 | E = FTP->arg_type_end(); I != E; ++I) { |
| 4065 | if ((*I)->isObjCQualifiedIdType()) |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4066 | return true; |
Fariborz Jahanian | 06de2cf | 2010-11-03 23:50:34 +0000 | [diff] [blame] | 4067 | if ((*I)->isObjCObjectPointerType() && |
| 4068 | (*I)->getPointeeType()->isObjCQualifiedInterfaceType()) |
| 4069 | return true; |
| 4070 | } |
| 4071 | |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4072 | } |
| 4073 | return false; |
| 4074 | } |
| 4075 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4076 | void RewriteObjC::GetExtentOfArgList(const char *Name, const char *&LParen, |
| 4077 | const char *&RParen) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4078 | const char *argPtr = strchr(Name, '('); |
| 4079 | assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4080 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4081 | LParen = argPtr; // output the start. |
| 4082 | argPtr++; // skip past the left paren. |
| 4083 | unsigned parenCount = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4084 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4085 | while (*argPtr && parenCount) { |
| 4086 | switch (*argPtr) { |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4087 | case '(': parenCount++; break; |
| 4088 | case ')': parenCount--; break; |
| 4089 | default: break; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4090 | } |
| 4091 | if (parenCount) argPtr++; |
| 4092 | } |
| 4093 | assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); |
| 4094 | RParen = argPtr; // output the end |
| 4095 | } |
| 4096 | |
| 4097 | void RewriteObjC::RewriteBlockPointerDecl(NamedDecl *ND) { |
| 4098 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 4099 | RewriteBlockPointerFunctionArgs(FD); |
| 4100 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4101 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4102 | // Handle Variables and Typedefs. |
| 4103 | SourceLocation DeclLoc = ND->getLocation(); |
| 4104 | QualType DeclT; |
| 4105 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4106 | DeclT = VD->getType(); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4107 | else if (TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(ND)) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4108 | DeclT = TDD->getUnderlyingType(); |
| 4109 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) |
| 4110 | DeclT = FD->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4111 | else |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4112 | llvm_unreachable("RewriteBlockPointerDecl(): Decl type not yet handled"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4113 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4114 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4115 | const char *endBuf = startBuf; |
| 4116 | // scan backward (from the decl location) for the end of the previous decl. |
| 4117 | while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) |
| 4118 | startBuf--; |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4119 | SourceLocation Start = DeclLoc.getLocWithOffset(startBuf-endBuf); |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4120 | std::string buf; |
| 4121 | unsigned OrigLength=0; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4122 | // *startBuf != '^' if we are dealing with a pointer to function that |
| 4123 | // may take block argument types (which will be handled below). |
| 4124 | if (*startBuf == '^') { |
| 4125 | // Replace the '^' with '*', computing a negative offset. |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4126 | buf = '*'; |
| 4127 | startBuf++; |
| 4128 | OrigLength++; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4129 | } |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4130 | while (*startBuf != ')') { |
| 4131 | buf += *startBuf; |
| 4132 | startBuf++; |
| 4133 | OrigLength++; |
| 4134 | } |
| 4135 | buf += ')'; |
| 4136 | OrigLength++; |
| 4137 | |
| 4138 | if (PointerTypeTakesAnyBlockArguments(DeclT) || |
| 4139 | PointerTypeTakesAnyObjCQualifiedType(DeclT)) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4140 | // Replace the '^' with '*' for arguments. |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4141 | // Replace id<P> with id/*<>*/ |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4142 | DeclLoc = ND->getLocation(); |
| 4143 | startBuf = SM->getCharacterData(DeclLoc); |
| 4144 | const char *argListBegin, *argListEnd; |
| 4145 | GetExtentOfArgList(startBuf, argListBegin, argListEnd); |
| 4146 | while (argListBegin < argListEnd) { |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4147 | if (*argListBegin == '^') |
| 4148 | buf += '*'; |
| 4149 | else if (*argListBegin == '<') { |
| 4150 | buf += "/*"; |
| 4151 | buf += *argListBegin++; |
| 4152 | OrigLength++;; |
| 4153 | while (*argListBegin != '>') { |
| 4154 | buf += *argListBegin++; |
| 4155 | OrigLength++; |
| 4156 | } |
| 4157 | buf += *argListBegin; |
| 4158 | buf += "*/"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4159 | } |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4160 | else |
| 4161 | buf += *argListBegin; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4162 | argListBegin++; |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4163 | OrigLength++; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4164 | } |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4165 | buf += ')'; |
| 4166 | OrigLength++; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4167 | } |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4168 | ReplaceText(Start, OrigLength, buf); |
| 4169 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4170 | return; |
| 4171 | } |
| 4172 | |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4173 | |
| 4174 | /// SynthesizeByrefCopyDestroyHelper - This routine synthesizes: |
| 4175 | /// void __Block_byref_id_object_copy(struct Block_byref_id_object *dst, |
| 4176 | /// struct Block_byref_id_object *src) { |
| 4177 | /// _Block_object_assign (&_dest->object, _src->object, |
| 4178 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4179 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4180 | /// _Block_object_assign(&_dest->object, _src->object, |
| 4181 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4182 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4183 | /// } |
| 4184 | /// And: |
| 4185 | /// void __Block_byref_id_object_dispose(struct Block_byref_id_object *_src) { |
| 4186 | /// _Block_object_dispose(_src->object, |
| 4187 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4188 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4189 | /// _Block_object_dispose(_src->object, |
| 4190 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4191 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4192 | /// } |
| 4193 | |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4194 | std::string RewriteObjC::SynthesizeByrefCopyDestroyHelper(VarDecl *VD, |
| 4195 | int flag) { |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4196 | std::string S; |
Benjamin Kramer | 1211a71 | 2010-01-10 19:57:50 +0000 | [diff] [blame] | 4197 | if (CopyDestroyCache.count(flag)) |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4198 | return S; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4199 | CopyDestroyCache.insert(flag); |
| 4200 | S = "static void __Block_byref_id_object_copy_"; |
| 4201 | S += utostr(flag); |
| 4202 | S += "(void *dst, void *src) {\n"; |
| 4203 | |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4204 | // offset into the object pointer is computed as: |
| 4205 | // void * + void* + int + int + void* + void * |
| 4206 | unsigned IntSize = |
| 4207 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 4208 | unsigned VoidPtrSize = |
| 4209 | static_cast<unsigned>(Context->getTypeSize(Context->VoidPtrTy)); |
| 4210 | |
Ken Dyck | 0c4e5d6 | 2011-04-30 16:08:27 +0000 | [diff] [blame] | 4211 | unsigned offset = (VoidPtrSize*4 + IntSize + IntSize)/Context->getCharWidth(); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4212 | S += " _Block_object_assign((char*)dst + "; |
| 4213 | S += utostr(offset); |
| 4214 | S += ", *(void * *) ((char*)src + "; |
| 4215 | S += utostr(offset); |
| 4216 | S += "), "; |
| 4217 | S += utostr(flag); |
| 4218 | S += ");\n}\n"; |
| 4219 | |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4220 | S += "static void __Block_byref_id_object_dispose_"; |
| 4221 | S += utostr(flag); |
| 4222 | S += "(void *src) {\n"; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4223 | S += " _Block_object_dispose(*(void * *) ((char*)src + "; |
| 4224 | S += utostr(offset); |
| 4225 | S += "), "; |
| 4226 | S += utostr(flag); |
| 4227 | S += ");\n}\n"; |
| 4228 | return S; |
| 4229 | } |
| 4230 | |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4231 | /// RewriteByRefVar - For each __block typex ND variable this routine transforms |
| 4232 | /// the declaration into: |
| 4233 | /// struct __Block_byref_ND { |
| 4234 | /// void *__isa; // NULL for everything except __weak pointers |
| 4235 | /// struct __Block_byref_ND *__forwarding; |
| 4236 | /// int32_t __flags; |
| 4237 | /// int32_t __size; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4238 | /// void *__Block_byref_id_object_copy; // If variable is __block ObjC object |
| 4239 | /// void *__Block_byref_id_object_dispose; // If variable is __block ObjC object |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4240 | /// typex ND; |
| 4241 | /// }; |
| 4242 | /// |
| 4243 | /// It then replaces declaration of ND variable with: |
| 4244 | /// struct __Block_byref_ND ND = {__isa=0B, __forwarding=&ND, __flags=some_flag, |
| 4245 | /// __size=sizeof(struct __Block_byref_ND), |
| 4246 | /// ND=initializer-if-any}; |
| 4247 | /// |
| 4248 | /// |
| 4249 | void RewriteObjC::RewriteByRefVar(VarDecl *ND) { |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 4250 | // Insert declaration for the function in which block literal is |
| 4251 | // used. |
| 4252 | if (CurFunctionDeclToDeclareForBlock) |
| 4253 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4254 | int flag = 0; |
| 4255 | int isa = 0; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4256 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
Fariborz Jahanian | d64a4f4 | 2010-02-26 22:49:11 +0000 | [diff] [blame] | 4257 | if (DeclLoc.isInvalid()) |
| 4258 | // If type location is missing, it is because of missing type (a warning). |
| 4259 | // Use variable's location which is good for this case. |
| 4260 | DeclLoc = ND->getLocation(); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4261 | const char *startBuf = SM->getCharacterData(DeclLoc); |
Fariborz Jahanian | 6f0a0a9 | 2009-12-30 20:38:08 +0000 | [diff] [blame] | 4262 | SourceLocation X = ND->getLocEnd(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 4263 | X = SM->getExpansionLoc(X); |
Fariborz Jahanian | 6f0a0a9 | 2009-12-30 20:38:08 +0000 | [diff] [blame] | 4264 | const char *endBuf = SM->getCharacterData(X); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4265 | std::string Name(ND->getNameAsString()); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4266 | std::string ByrefType; |
Fariborz Jahanian | 1e8011e | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 4267 | RewriteByRefString(ByrefType, Name, ND, true); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4268 | ByrefType += " {\n"; |
| 4269 | ByrefType += " void *__isa;\n"; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4270 | RewriteByRefString(ByrefType, Name, ND); |
| 4271 | ByrefType += " *__forwarding;\n"; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4272 | ByrefType += " int __flags;\n"; |
| 4273 | ByrefType += " int __size;\n"; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4274 | // Add void *__Block_byref_id_object_copy; |
| 4275 | // void *__Block_byref_id_object_dispose; if needed. |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4276 | QualType Ty = ND->getType(); |
| 4277 | bool HasCopyAndDispose = Context->BlockRequiresCopying(Ty); |
| 4278 | if (HasCopyAndDispose) { |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4279 | ByrefType += " void (*__Block_byref_id_object_copy)(void*, void*);\n"; |
| 4280 | ByrefType += " void (*__Block_byref_id_object_dispose)(void*);\n"; |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4281 | } |
Fariborz Jahanian | 822ac87 | 2011-03-31 22:49:32 +0000 | [diff] [blame] | 4282 | |
| 4283 | QualType T = Ty; |
| 4284 | (void)convertBlockPointerToFunctionPointer(T); |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 4285 | T.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Fariborz Jahanian | 822ac87 | 2011-03-31 22:49:32 +0000 | [diff] [blame] | 4286 | |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4287 | ByrefType += " " + Name + ";\n"; |
| 4288 | ByrefType += "};\n"; |
| 4289 | // Insert this type in global scope. It is needed by helper function. |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4290 | SourceLocation FunLocStart; |
| 4291 | if (CurFunctionDef) |
| 4292 | FunLocStart = CurFunctionDef->getTypeSpecStartLoc(); |
| 4293 | else { |
| 4294 | assert(CurMethodDef && "RewriteByRefVar - CurMethodDef is null"); |
| 4295 | FunLocStart = CurMethodDef->getLocStart(); |
| 4296 | } |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4297 | InsertText(FunLocStart, ByrefType); |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4298 | if (Ty.isObjCGCWeak()) { |
| 4299 | flag |= BLOCK_FIELD_IS_WEAK; |
| 4300 | isa = 1; |
| 4301 | } |
| 4302 | |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4303 | if (HasCopyAndDispose) { |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4304 | flag = BLOCK_BYREF_CALLER; |
| 4305 | QualType Ty = ND->getType(); |
| 4306 | // FIXME. Handle __weak variable (BLOCK_FIELD_IS_WEAK) as well. |
| 4307 | if (Ty->isBlockPointerType()) |
| 4308 | flag |= BLOCK_FIELD_IS_BLOCK; |
| 4309 | else |
| 4310 | flag |= BLOCK_FIELD_IS_OBJECT; |
| 4311 | std::string HF = SynthesizeByrefCopyDestroyHelper(ND, flag); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4312 | if (!HF.empty()) |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4313 | InsertText(FunLocStart, HF); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4314 | } |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4315 | |
| 4316 | // struct __Block_byref_ND ND = |
| 4317 | // {0, &ND, some_flag, __size=sizeof(struct __Block_byref_ND), |
| 4318 | // initializer-if-any}; |
| 4319 | bool hasInit = (ND->getInit() != 0); |
Fariborz Jahanian | e1f84f8 | 2010-01-05 18:15:57 +0000 | [diff] [blame] | 4320 | unsigned flags = 0; |
| 4321 | if (HasCopyAndDispose) |
| 4322 | flags |= BLOCK_HAS_COPY_DISPOSE; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4323 | Name = ND->getNameAsString(); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4324 | ByrefType.clear(); |
| 4325 | RewriteByRefString(ByrefType, Name, ND); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4326 | std::string ForwardingCastType("("); |
| 4327 | ForwardingCastType += ByrefType + " *)"; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4328 | if (!hasInit) { |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4329 | ByrefType += " " + Name + " = {(void*)"; |
| 4330 | ByrefType += utostr(isa); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4331 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4332 | ByrefType += utostr(flags); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4333 | ByrefType += ", "; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4334 | ByrefType += "sizeof("; |
| 4335 | RewriteByRefString(ByrefType, Name, ND); |
| 4336 | ByrefType += ")"; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4337 | if (HasCopyAndDispose) { |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4338 | ByrefType += ", __Block_byref_id_object_copy_"; |
| 4339 | ByrefType += utostr(flag); |
| 4340 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 4341 | ByrefType += utostr(flag); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4342 | } |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4343 | ByrefType += "};\n"; |
Fariborz Jahanian | 822ac87 | 2011-03-31 22:49:32 +0000 | [diff] [blame] | 4344 | unsigned nameSize = Name.size(); |
| 4345 | // for block or function pointer declaration. Name is aleady |
| 4346 | // part of the declaration. |
| 4347 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) |
| 4348 | nameSize = 1; |
| 4349 | ReplaceText(DeclLoc, endBuf-startBuf+nameSize, ByrefType); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4350 | } |
| 4351 | else { |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4352 | SourceLocation startLoc; |
| 4353 | Expr *E = ND->getInit(); |
| 4354 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 4355 | startLoc = ECE->getLParenLoc(); |
| 4356 | else |
| 4357 | startLoc = E->getLocStart(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 4358 | startLoc = SM->getExpansionLoc(startLoc); |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4359 | endBuf = SM->getCharacterData(startLoc); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4360 | ByrefType += " " + Name; |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4361 | ByrefType += " = {(void*)"; |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4362 | ByrefType += utostr(isa); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4363 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4364 | ByrefType += utostr(flags); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4365 | ByrefType += ", "; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4366 | ByrefType += "sizeof("; |
| 4367 | RewriteByRefString(ByrefType, Name, ND); |
| 4368 | ByrefType += "), "; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4369 | if (HasCopyAndDispose) { |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4370 | ByrefType += "__Block_byref_id_object_copy_"; |
| 4371 | ByrefType += utostr(flag); |
| 4372 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 4373 | ByrefType += utostr(flag); |
| 4374 | ByrefType += ", "; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4375 | } |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4376 | ReplaceText(DeclLoc, endBuf-startBuf, ByrefType); |
Steve Naroff | c5143c5 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 4377 | |
| 4378 | // Complete the newly synthesized compound expression by inserting a right |
| 4379 | // curly brace before the end of the declaration. |
| 4380 | // FIXME: This approach avoids rewriting the initializer expression. It |
| 4381 | // also assumes there is only one declarator. For example, the following |
| 4382 | // isn't currently supported by this routine (in general): |
| 4383 | // |
| 4384 | // double __block BYREFVAR = 1.34, BYREFVAR2 = 1.37; |
| 4385 | // |
Fariborz Jahanian | 5f371ee | 2010-07-21 17:36:39 +0000 | [diff] [blame] | 4386 | const char *startInitializerBuf = SM->getCharacterData(startLoc); |
| 4387 | const char *semiBuf = strchr(startInitializerBuf, ';'); |
Steve Naroff | c5143c5 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 4388 | assert((*semiBuf == ';') && "RewriteByRefVar: can't find ';'"); |
| 4389 | SourceLocation semiLoc = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4390 | startLoc.getLocWithOffset(semiBuf-startInitializerBuf); |
Steve Naroff | c5143c5 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 4391 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4392 | InsertText(semiLoc, "}"); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4393 | } |
Fariborz Jahanian | 1be6b46 | 2009-12-22 00:48:54 +0000 | [diff] [blame] | 4394 | return; |
| 4395 | } |
| 4396 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4397 | void RewriteObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4398 | // Add initializers for any closure decl refs. |
| 4399 | GetBlockDeclRefExprs(Exp->getBody()); |
| 4400 | if (BlockDeclRefs.size()) { |
| 4401 | // Unique all "by copy" declarations. |
| 4402 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4403 | if (!BlockDeclRefs[i]->isByRef()) { |
| 4404 | if (!BlockByCopyDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 4405 | BlockByCopyDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 4406 | BlockByCopyDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 4407 | } |
| 4408 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4409 | // Unique all "by ref" declarations. |
| 4410 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 4411 | if (BlockDeclRefs[i]->isByRef()) { |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4412 | if (!BlockByRefDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 4413 | BlockByRefDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 4414 | BlockByRefDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 4415 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4416 | } |
| 4417 | // Find any imported blocks...they will need special attention. |
| 4418 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
Fariborz Jahanian | 4fcc4fd | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 4419 | if (BlockDeclRefs[i]->isByRef() || |
| 4420 | BlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
Fariborz Jahanian | 5b011b0 | 2010-02-26 21:46:27 +0000 | [diff] [blame] | 4421 | BlockDeclRefs[i]->getType()->isBlockPointerType()) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4422 | ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4423 | } |
| 4424 | } |
| 4425 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4426 | FunctionDecl *RewriteObjC::SynthBlockInitFunctionDecl(StringRef name) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4427 | IdentifierInfo *ID = &Context->Idents.get(name); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4428 | QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4429 | return FunctionDecl::Create(*Context, TUDecl, SourceLocation(), |
| 4430 | SourceLocation(), ID, FType, 0, SC_Extern, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4431 | SC_None, false, false); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4432 | } |
| 4433 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4434 | Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4435 | const SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs) { |
Fariborz Jahanian | 05318d8 | 2011-02-16 22:37:10 +0000 | [diff] [blame] | 4436 | const BlockDecl *block = Exp->getBlockDecl(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4437 | Blocks.push_back(Exp); |
| 4438 | |
| 4439 | CollectBlockDeclRefInfo(Exp); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4440 | |
| 4441 | // Add inner imported variables now used in current block. |
| 4442 | int countOfInnerDecls = 0; |
Fariborz Jahanian | 1276bfe | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4443 | if (!InnerBlockDeclRefs.empty()) { |
| 4444 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) { |
| 4445 | BlockDeclRefExpr *Exp = InnerBlockDeclRefs[i]; |
| 4446 | ValueDecl *VD = Exp->getDecl(); |
| 4447 | if (!Exp->isByRef() && !BlockByCopyDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4448 | // We need to save the copied-in variables in nested |
| 4449 | // blocks because it is needed at the end for some of the API generations. |
| 4450 | // See SynthesizeBlockLiterals routine. |
Fariborz Jahanian | 1276bfe | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4451 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 4452 | BlockDeclRefs.push_back(Exp); |
| 4453 | BlockByCopyDeclsPtrSet.insert(VD); |
| 4454 | BlockByCopyDecls.push_back(VD); |
| 4455 | } |
| 4456 | if (Exp->isByRef() && !BlockByRefDeclsPtrSet.count(VD)) { |
| 4457 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 4458 | BlockDeclRefs.push_back(Exp); |
| 4459 | BlockByRefDeclsPtrSet.insert(VD); |
| 4460 | BlockByRefDecls.push_back(VD); |
| 4461 | } |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4462 | } |
Fariborz Jahanian | 1276bfe | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4463 | // Find any imported blocks...they will need special attention. |
| 4464 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) |
| 4465 | if (InnerBlockDeclRefs[i]->isByRef() || |
| 4466 | InnerBlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
| 4467 | InnerBlockDeclRefs[i]->getType()->isBlockPointerType()) |
| 4468 | ImportedBlockDecls.insert(InnerBlockDeclRefs[i]->getDecl()); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4469 | } |
| 4470 | InnerDeclRefsCount.push_back(countOfInnerDecls); |
| 4471 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4472 | std::string FuncName; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4473 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4474 | if (CurFunctionDef) |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4475 | FuncName = CurFunctionDef->getNameAsString(); |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 4476 | else if (CurMethodDef) |
| 4477 | BuildUniqueMethodName(FuncName, CurMethodDef); |
| 4478 | else if (GlobalVarDecl) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4479 | FuncName = std::string(GlobalVarDecl->getNameAsString()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4480 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4481 | std::string BlockNumber = utostr(Blocks.size()-1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4482 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4483 | std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber; |
| 4484 | std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4485 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4486 | // Get a pointer to the function type so we can cast appropriately. |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4487 | QualType BFT = convertFunctionTypeOfBlocks(Exp->getFunctionType()); |
| 4488 | QualType FType = Context->getPointerType(BFT); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4489 | |
| 4490 | FunctionDecl *FD; |
| 4491 | Expr *NewRep; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4492 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4493 | // Simulate a contructor call... |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4494 | FD = SynthBlockInitFunctionDecl(Tag); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4495 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, FType, VK_RValue, |
| 4496 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4497 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4498 | SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4499 | |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4500 | // Initialize the block function. |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4501 | FD = SynthBlockInitFunctionDecl(Func); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4502 | DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, FD->getType(), VK_LValue, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4503 | SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4504 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4505 | CK_BitCast, Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4506 | InitExprs.push_back(castExpr); |
| 4507 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4508 | // Initialize the block descriptor. |
| 4509 | std::string DescData = "__" + FuncName + "_block_desc_" + BlockNumber + "_DATA"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4510 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4511 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, |
| 4512 | SourceLocation(), SourceLocation(), |
| 4513 | &Context->Idents.get(DescData.c_str()), |
| 4514 | Context->VoidPtrTy, 0, |
| 4515 | SC_Static, SC_None); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4516 | UnaryOperator *DescRefExpr = |
| 4517 | new (Context) UnaryOperator(new (Context) DeclRefExpr(NewVD, |
| 4518 | Context->VoidPtrTy, |
| 4519 | VK_LValue, |
| 4520 | SourceLocation()), |
| 4521 | UO_AddrOf, |
| 4522 | Context->getPointerType(Context->VoidPtrTy), |
| 4523 | VK_RValue, OK_Ordinary, |
| 4524 | SourceLocation()); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4525 | InitExprs.push_back(DescRefExpr); |
| 4526 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4527 | // Add initializers for any closure decl refs. |
| 4528 | if (BlockDeclRefs.size()) { |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4529 | Expr *Exp; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4530 | // Output all "by copy" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4531 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4532 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4533 | if (isObjCType((*I)->getType())) { |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4534 | // FIXME: Conform to ABI ([[obj retain] autorelease]). |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4535 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4536 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), VK_LValue, |
| 4537 | SourceLocation()); |
Fariborz Jahanian | a5a7987 | 2010-05-24 18:32:56 +0000 | [diff] [blame] | 4538 | if (HasLocalVariableExternalStorage(*I)) { |
| 4539 | QualType QT = (*I)->getType(); |
| 4540 | QT = Context->getPointerType(QT); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4541 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue, |
| 4542 | OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | a5a7987 | 2010-05-24 18:32:56 +0000 | [diff] [blame] | 4543 | } |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4544 | } else if (isTopLevelBlockPointerType((*I)->getType())) { |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4545 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4546 | Arg = new (Context) DeclRefExpr(FD, FD->getType(), VK_LValue, |
| 4547 | SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4548 | Exp = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4549 | CK_BitCast, Arg); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4550 | } else { |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4551 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4552 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), VK_LValue, |
| 4553 | SourceLocation()); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4554 | if (HasLocalVariableExternalStorage(*I)) { |
| 4555 | QualType QT = (*I)->getType(); |
| 4556 | QT = Context->getPointerType(QT); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4557 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue, |
| 4558 | OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4559 | } |
| 4560 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4561 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4562 | InitExprs.push_back(Exp); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4563 | } |
| 4564 | // Output all "by ref" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4565 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4566 | E = BlockByRefDecls.end(); I != E; ++I) { |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4567 | ValueDecl *ND = (*I); |
| 4568 | std::string Name(ND->getNameAsString()); |
| 4569 | std::string RecName; |
Fariborz Jahanian | 1e8011e | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 4570 | RewriteByRefString(RecName, Name, ND, true); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4571 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str() |
| 4572 | + sizeof("struct")); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4573 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4574 | SourceLocation(), SourceLocation(), |
| 4575 | II); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4576 | assert(RD && "SynthBlockInitExpr(): Can't find RecordDecl"); |
| 4577 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 4578 | |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4579 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4580 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), VK_LValue, |
| 4581 | SourceLocation()); |
Fariborz Jahanian | 05318d8 | 2011-02-16 22:37:10 +0000 | [diff] [blame] | 4582 | bool isNestedCapturedVar = false; |
| 4583 | if (block) |
| 4584 | for (BlockDecl::capture_const_iterator ci = block->capture_begin(), |
| 4585 | ce = block->capture_end(); ci != ce; ++ci) { |
| 4586 | const VarDecl *variable = ci->getVariable(); |
| 4587 | if (variable == ND && ci->isNested()) { |
| 4588 | assert (ci->isByRef() && |
| 4589 | "SynthBlockInitExpr - captured block variable is not byref"); |
| 4590 | isNestedCapturedVar = true; |
| 4591 | break; |
| 4592 | } |
| 4593 | } |
| 4594 | // captured nested byref variable has its address passed. Do not take |
| 4595 | // its address again. |
| 4596 | if (!isNestedCapturedVar) |
| 4597 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4598 | Context->getPointerType(Exp->getType()), |
| 4599 | VK_RValue, OK_Ordinary, SourceLocation()); |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4600 | Exp = NoTypeInfoCStyleCastExpr(Context, castT, CK_BitCast, Exp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4601 | InitExprs.push_back(Exp); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4602 | } |
| 4603 | } |
Fariborz Jahanian | ff12788 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 4604 | if (ImportedBlockDecls.size()) { |
| 4605 | // generate BLOCK_HAS_COPY_DISPOSE(have helper funcs) | BLOCK_HAS_DESCRIPTOR |
| 4606 | int flag = (BLOCK_HAS_COPY_DISPOSE | BLOCK_HAS_DESCRIPTOR); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4607 | unsigned IntSize = |
| 4608 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 4609 | Expr *FlagExp = IntegerLiteral::Create(*Context, llvm::APInt(IntSize, flag), |
| 4610 | Context->IntTy, SourceLocation()); |
Fariborz Jahanian | ff12788 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 4611 | InitExprs.push_back(FlagExp); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4612 | } |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 4613 | NewRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], InitExprs.size(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4614 | FType, VK_LValue, SourceLocation()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4615 | NewRep = new (Context) UnaryOperator(NewRep, UO_AddrOf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4616 | Context->getPointerType(NewRep->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4617 | VK_RValue, OK_Ordinary, SourceLocation()); |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4618 | NewRep = NoTypeInfoCStyleCastExpr(Context, FType, CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4619 | NewRep); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4620 | BlockDeclRefs.clear(); |
| 4621 | BlockByRefDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4622 | BlockByRefDeclsPtrSet.clear(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4623 | BlockByCopyDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4624 | BlockByCopyDeclsPtrSet.clear(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4625 | ImportedBlockDecls.clear(); |
| 4626 | return NewRep; |
| 4627 | } |
| 4628 | |
Fariborz Jahanian | 42f1e65 | 2011-02-24 21:29:21 +0000 | [diff] [blame] | 4629 | bool RewriteObjC::IsDeclStmtInForeachHeader(DeclStmt *DS) { |
| 4630 | if (const ObjCForCollectionStmt * CS = |
| 4631 | dyn_cast<ObjCForCollectionStmt>(Stmts.back())) |
| 4632 | return CS->getElement() == DS; |
| 4633 | return false; |
| 4634 | } |
| 4635 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4636 | //===----------------------------------------------------------------------===// |
| 4637 | // Function Body / Expression rewriting |
| 4638 | //===----------------------------------------------------------------------===// |
| 4639 | |
| 4640 | Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4641 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4642 | isa<DoStmt>(S) || isa<ForStmt>(S)) |
| 4643 | Stmts.push_back(S); |
| 4644 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 4645 | Stmts.push_back(S); |
Chris Lattner | 4824fcd | 2010-01-09 21:45:57 +0000 | [diff] [blame] | 4646 | ObjCBcLabelNo.push_back(++BcLabelCount); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4647 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4648 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4649 | // Pseudo-object operations and ivar references need special |
| 4650 | // treatment because we're going to recursively rewrite them. |
| 4651 | if (PseudoObjectExpr *PseudoOp = dyn_cast<PseudoObjectExpr>(S)) { |
| 4652 | if (isa<BinaryOperator>(PseudoOp->getSyntacticForm())) { |
| 4653 | return RewritePropertyOrImplicitSetter(PseudoOp); |
| 4654 | } else { |
| 4655 | return RewritePropertyOrImplicitGetter(PseudoOp); |
| 4656 | } |
| 4657 | } else if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) { |
| 4658 | return RewriteObjCIvarRefExpr(IvarRefExpr); |
| 4659 | } |
| 4660 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4661 | SourceRange OrigStmtRange = S->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4662 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4663 | // Perform a bottom up rewrite of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 4664 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4665 | if (*CI) { |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4666 | Stmt *childStmt = (*CI); |
| 4667 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(childStmt); |
Fariborz Jahanian | 1d01531 | 2011-04-11 21:17:02 +0000 | [diff] [blame] | 4668 | if (newStmt) { |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4669 | *CI = newStmt; |
Fariborz Jahanian | 1d01531 | 2011-04-11 21:17:02 +0000 | [diff] [blame] | 4670 | } |
Nick Lewycky | 7e74924 | 2010-10-31 21:07:24 +0000 | [diff] [blame] | 4671 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4672 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4673 | if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4674 | SmallVector<BlockDeclRefExpr *, 8> InnerBlockDeclRefs; |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4675 | llvm::SmallPtrSet<const DeclContext *, 8> InnerContexts; |
| 4676 | InnerContexts.insert(BE->getBlockDecl()); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4677 | ImportedLocalExternalDecls.clear(); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4678 | GetInnerBlockDeclRefExprs(BE->getBody(), |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4679 | InnerBlockDeclRefs, InnerContexts); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4680 | // Rewrite the block body in place. |
Fariborz Jahanian | da4ad9f | 2010-11-08 18:37:50 +0000 | [diff] [blame] | 4681 | Stmt *SaveCurrentBody = CurrentBody; |
| 4682 | CurrentBody = BE->getBody(); |
| 4683 | PropParentMap = 0; |
Fariborz Jahanian | f23a0ff | 2011-08-02 20:28:46 +0000 | [diff] [blame] | 4684 | // block literal on rhs of a property-dot-sytax assignment |
| 4685 | // must be replaced by its synthesize ast so getRewrittenText |
| 4686 | // works as expected. In this case, what actually ends up on RHS |
| 4687 | // is the blockTranscribed which is the helper function for the |
| 4688 | // block literal; as in: self.c = ^() {[ace ARR];}; |
| 4689 | bool saveDisableReplaceStmt = DisableReplaceStmt; |
| 4690 | DisableReplaceStmt = false; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4691 | RewriteFunctionBodyOrGlobalInitializer(BE->getBody()); |
Fariborz Jahanian | f23a0ff | 2011-08-02 20:28:46 +0000 | [diff] [blame] | 4692 | DisableReplaceStmt = saveDisableReplaceStmt; |
Fariborz Jahanian | da4ad9f | 2010-11-08 18:37:50 +0000 | [diff] [blame] | 4693 | CurrentBody = SaveCurrentBody; |
| 4694 | PropParentMap = 0; |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4695 | ImportedLocalExternalDecls.clear(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4696 | // Now we snarf the rewritten text and stash it away for later use. |
Fariborz Jahanian | f23a0ff | 2011-08-02 20:28:46 +0000 | [diff] [blame] | 4697 | std::string Str = Rewrite.getRewrittenText(BE->getSourceRange()); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4698 | RewrittenBlockExprs[BE] = Str; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4699 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4700 | Stmt *blockTranscribed = SynthBlockInitExpr(BE, InnerBlockDeclRefs); |
| 4701 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4702 | //blockTranscribed->dump(); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4703 | ReplaceStmt(S, blockTranscribed); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4704 | return blockTranscribed; |
| 4705 | } |
| 4706 | // Handle specific things. |
| 4707 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 4708 | return RewriteAtEncode(AtEncode); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4709 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4710 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 4711 | return RewriteAtSelector(AtSelector); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4712 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4713 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 4714 | return RewriteObjCStringLiteral(AtString); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4715 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4716 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4717 | #if 0 |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4718 | // Before we rewrite it, put the original message expression in a comment. |
| 4719 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 4720 | SourceLocation endLoc = MessExpr->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4721 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4722 | const char *startBuf = SM->getCharacterData(startLoc); |
| 4723 | const char *endBuf = SM->getCharacterData(endLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4724 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4725 | std::string messString; |
| 4726 | messString += "// "; |
| 4727 | messString.append(startBuf, endBuf-startBuf+1); |
| 4728 | messString += "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4729 | |
| 4730 | // FIXME: Missing definition of |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4731 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
| 4732 | // InsertText(startLoc, messString.c_str(), messString.size()); |
| 4733 | // Tried this, but it didn't work either... |
| 4734 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4735 | #endif |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4736 | return RewriteMessageExpr(MessExpr); |
| 4737 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4738 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4739 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 4740 | return RewriteObjCTryStmt(StmtTry); |
| 4741 | |
| 4742 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 4743 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 4744 | |
| 4745 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 4746 | return RewriteObjCThrowStmt(StmtThrow); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4747 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4748 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 4749 | return RewriteObjCProtocolExpr(ProtocolExp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4750 | |
| 4751 | if (ObjCForCollectionStmt *StmtForCollection = |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4752 | dyn_cast<ObjCForCollectionStmt>(S)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4753 | return RewriteObjCForCollectionStmt(StmtForCollection, |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4754 | OrigStmtRange.getEnd()); |
| 4755 | if (BreakStmt *StmtBreakStmt = |
| 4756 | dyn_cast<BreakStmt>(S)) |
| 4757 | return RewriteBreakStmt(StmtBreakStmt); |
| 4758 | if (ContinueStmt *StmtContinueStmt = |
| 4759 | dyn_cast<ContinueStmt>(S)) |
| 4760 | return RewriteContinueStmt(StmtContinueStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4761 | |
| 4762 | // Need to check for protocol refs (id <P>, Foo <P> *) in variable decls |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4763 | // and cast exprs. |
| 4764 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
| 4765 | // FIXME: What we're doing here is modifying the type-specifier that |
| 4766 | // precedes the first Decl. In the future the DeclGroup should have |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4767 | // a separate type-specifier that we can rewrite. |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4768 | // NOTE: We need to avoid rewriting the DeclStmt if it is within |
| 4769 | // the context of an ObjCForCollectionStmt. For example: |
| 4770 | // NSArray *someArray; |
| 4771 | // for (id <FooProtocol> index in someArray) ; |
| 4772 | // This is because RewriteObjCForCollectionStmt() does textual rewriting |
| 4773 | // and it depends on the original text locations/positions. |
Fariborz Jahanian | 42f1e65 | 2011-02-24 21:29:21 +0000 | [diff] [blame] | 4774 | if (Stmts.empty() || !IsDeclStmtInForeachHeader(DS)) |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4775 | RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4776 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4777 | // Blocks rewrite rules. |
| 4778 | for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); |
| 4779 | DI != DE; ++DI) { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 4780 | Decl *SD = *DI; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4781 | if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4782 | if (isTopLevelBlockPointerType(ND->getType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4783 | RewriteBlockPointerDecl(ND); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4784 | else if (ND->getType()->isFunctionPointerType()) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4785 | CheckFunctionPointerDecl(ND->getType(), ND); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 4786 | if (VarDecl *VD = dyn_cast<VarDecl>(SD)) { |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4787 | if (VD->hasAttr<BlocksAttr>()) { |
| 4788 | static unsigned uniqueByrefDeclCount = 0; |
| 4789 | assert(!BlockByRefDeclNo.count(ND) && |
| 4790 | "RewriteFunctionBodyOrGlobalInitializer: Duplicate byref decl"); |
| 4791 | BlockByRefDeclNo[ND] = uniqueByrefDeclCount++; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4792 | RewriteByRefVar(VD); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4793 | } |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 4794 | else |
| 4795 | RewriteTypeOfDecl(VD); |
| 4796 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4797 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4798 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4799 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4800 | RewriteBlockPointerDecl(TD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4801 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4802 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 4803 | } |
| 4804 | } |
| 4805 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4806 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4807 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) |
| 4808 | RewriteObjCQualifiedInterfaceTypes(CE); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4809 | |
| 4810 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4811 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 4812 | assert(!Stmts.empty() && "Statement stack is empty"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4813 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 4814 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4815 | && "Statement stack mismatch"); |
| 4816 | Stmts.pop_back(); |
| 4817 | } |
| 4818 | // Handle blocks rewriting. |
| 4819 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
| 4820 | if (BDRE->isByRef()) |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4821 | return RewriteBlockDeclRefExpr(BDRE); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4822 | } |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4823 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 4824 | ValueDecl *VD = DRE->getDecl(); |
| 4825 | if (VD->hasAttr<BlocksAttr>()) |
| 4826 | return RewriteBlockDeclRefExpr(DRE); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4827 | if (HasLocalVariableExternalStorage(VD)) |
| 4828 | return RewriteLocalVariableExternalStorage(DRE); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4829 | } |
| 4830 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4831 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4832 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4833 | Stmt *BlockCall = SynthesizeBlockCall(CE, CE->getCallee()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4834 | ReplaceStmt(S, BlockCall); |
| 4835 | return BlockCall; |
| 4836 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4837 | } |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4838 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4839 | RewriteCastExpr(CE); |
| 4840 | } |
| 4841 | #if 0 |
| 4842 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4843 | CastExpr *Replacement = new (Context) CastExpr(ICE->getType(), |
| 4844 | ICE->getSubExpr(), |
| 4845 | SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4846 | // Get the new text. |
| 4847 | std::string SStr; |
| 4848 | llvm::raw_string_ostream Buf(SStr); |
Eli Friedman | 3a9eb44 | 2009-05-30 05:19:26 +0000 | [diff] [blame] | 4849 | Replacement->printPretty(Buf, *Context); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4850 | const std::string &Str = Buf.str(); |
| 4851 | |
| 4852 | printf("CAST = %s\n", &Str[0]); |
| 4853 | InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 4854 | delete S; |
| 4855 | return Replacement; |
| 4856 | } |
| 4857 | #endif |
| 4858 | // Return this stmt unmodified. |
| 4859 | return S; |
| 4860 | } |
| 4861 | |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4862 | void RewriteObjC::RewriteRecordBody(RecordDecl *RD) { |
| 4863 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 4864 | e = RD->field_end(); i != e; ++i) { |
| 4865 | FieldDecl *FD = *i; |
| 4866 | if (isTopLevelBlockPointerType(FD->getType())) |
| 4867 | RewriteBlockPointerDecl(FD); |
| 4868 | if (FD->getType()->isObjCQualifiedIdType() || |
| 4869 | FD->getType()->isObjCQualifiedInterfaceType()) |
| 4870 | RewriteObjCQualifiedInterfaceTypes(FD); |
| 4871 | } |
| 4872 | } |
| 4873 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4874 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 4875 | /// main file of the input. |
| 4876 | void RewriteObjC::HandleDeclInMainFile(Decl *D) { |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4877 | switch (D->getKind()) { |
| 4878 | case Decl::Function: { |
| 4879 | FunctionDecl *FD = cast<FunctionDecl>(D); |
| 4880 | if (FD->isOverloadedOperator()) |
| 4881 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4882 | |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4883 | // Since function prototypes don't have ParmDecl's, we check the function |
| 4884 | // prototype. This enables us to rewrite function declarations and |
| 4885 | // definitions using the same code. |
| 4886 | RewriteBlocksInFunctionProtoType(FD->getType(), FD); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4887 | |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4888 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
| 4889 | if (CompoundStmt *Body = dyn_cast_or_null<CompoundStmt>(FD->getBody())) { |
| 4890 | CurFunctionDef = FD; |
| 4891 | CurFunctionDeclToDeclareForBlock = FD; |
| 4892 | CurrentBody = Body; |
| 4893 | Body = |
| 4894 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 4895 | FD->setBody(Body); |
| 4896 | CurrentBody = 0; |
| 4897 | if (PropParentMap) { |
| 4898 | delete PropParentMap; |
| 4899 | PropParentMap = 0; |
| 4900 | } |
| 4901 | // This synthesizes and inserts the block "impl" struct, invoke function, |
| 4902 | // and any copy/dispose helper functions. |
| 4903 | InsertBlockLiteralsWithinFunction(FD); |
| 4904 | CurFunctionDef = 0; |
| 4905 | CurFunctionDeclToDeclareForBlock = 0; |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4906 | } |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4907 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4908 | } |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4909 | case Decl::ObjCMethod: { |
| 4910 | ObjCMethodDecl *MD = cast<ObjCMethodDecl>(D); |
| 4911 | if (CompoundStmt *Body = MD->getCompoundBody()) { |
| 4912 | CurMethodDef = MD; |
| 4913 | CurrentBody = Body; |
| 4914 | Body = |
| 4915 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 4916 | MD->setBody(Body); |
| 4917 | CurrentBody = 0; |
| 4918 | if (PropParentMap) { |
| 4919 | delete PropParentMap; |
| 4920 | PropParentMap = 0; |
| 4921 | } |
| 4922 | InsertBlockLiteralsWithinMethod(MD); |
| 4923 | CurMethodDef = 0; |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4924 | } |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4925 | break; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4926 | } |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4927 | case Decl::ObjCImplementation: { |
| 4928 | ObjCImplementationDecl *CI = cast<ObjCImplementationDecl>(D); |
| 4929 | ClassImplementation.push_back(CI); |
| 4930 | break; |
| 4931 | } |
| 4932 | case Decl::ObjCCategoryImpl: { |
| 4933 | ObjCCategoryImplDecl *CI = cast<ObjCCategoryImplDecl>(D); |
| 4934 | CategoryImplementation.push_back(CI); |
| 4935 | break; |
| 4936 | } |
| 4937 | case Decl::Var: { |
| 4938 | VarDecl *VD = cast<VarDecl>(D); |
| 4939 | RewriteObjCQualifiedInterfaceTypes(VD); |
| 4940 | if (isTopLevelBlockPointerType(VD->getType())) |
| 4941 | RewriteBlockPointerDecl(VD); |
| 4942 | else if (VD->getType()->isFunctionPointerType()) { |
| 4943 | CheckFunctionPointerDecl(VD->getType(), VD); |
| 4944 | if (VD->getInit()) { |
| 4945 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
| 4946 | RewriteCastExpr(CE); |
| 4947 | } |
| 4948 | } |
| 4949 | } else if (VD->getType()->isRecordType()) { |
| 4950 | RecordDecl *RD = VD->getType()->getAs<RecordType>()->getDecl(); |
| 4951 | if (RD->isCompleteDefinition()) |
| 4952 | RewriteRecordBody(RD); |
| 4953 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4954 | if (VD->getInit()) { |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4955 | GlobalVarDecl = VD; |
| 4956 | CurrentBody = VD->getInit(); |
| 4957 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
| 4958 | CurrentBody = 0; |
| 4959 | if (PropParentMap) { |
| 4960 | delete PropParentMap; |
| 4961 | PropParentMap = 0; |
| 4962 | } |
| 4963 | SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), VD->getName()); |
| 4964 | GlobalVarDecl = 0; |
| 4965 | |
| 4966 | // This is needed for blocks. |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4967 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4968 | RewriteCastExpr(CE); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4969 | } |
| 4970 | } |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4971 | break; |
| 4972 | } |
| 4973 | case Decl::TypeAlias: |
| 4974 | case Decl::Typedef: { |
| 4975 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
| 4976 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
| 4977 | RewriteBlockPointerDecl(TD); |
| 4978 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
| 4979 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 4980 | } |
| 4981 | break; |
| 4982 | } |
| 4983 | case Decl::CXXRecord: |
| 4984 | case Decl::Record: { |
| 4985 | RecordDecl *RD = cast<RecordDecl>(D); |
| 4986 | if (RD->isCompleteDefinition()) |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4987 | RewriteRecordBody(RD); |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4988 | break; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4989 | } |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4990 | default: |
| 4991 | break; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4992 | } |
| 4993 | // Nothing yet. |
| 4994 | } |
| 4995 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 4996 | void RewriteObjC::HandleTranslationUnit(ASTContext &C) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4997 | if (Diags.hasErrorOccurred()) |
| 4998 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4999 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5000 | RewriteInclude(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5001 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5002 | // Here's a great place to add any extra declarations that may be needed. |
| 5003 | // Write out meta data for each @protocol(<expr>). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5004 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5005 | E = ProtocolExprDecls.end(); I != E; ++I) |
| 5006 | RewriteObjCProtocolMetaData(*I, "", "", Preamble); |
| 5007 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5008 | InsertText(SM->getLocForStartOfFile(MainFileID), Preamble, false); |
Steve Naroff | 0aab796 | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5009 | if (ClassImplementation.size() || CategoryImplementation.size()) |
| 5010 | RewriteImplementations(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5011 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5012 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 5013 | // we are done. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5014 | if (const RewriteBuffer *RewriteBuf = |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5015 | Rewrite.getRewriteBufferFor(MainFileID)) { |
| 5016 | //printf("Changed:\n"); |
| 5017 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 5018 | } else { |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5019 | llvm::errs() << "No changes\n"; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5020 | } |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 5021 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5022 | if (ClassImplementation.size() || CategoryImplementation.size() || |
| 5023 | ProtocolExprDecls.size()) { |
Steve Naroff | 0aab796 | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5024 | // Rewrite Objective-c meta data* |
| 5025 | std::string ResultStr; |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 5026 | RewriteMetaDataIntoBuffer(ResultStr); |
Steve Naroff | 0aab796 | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5027 | // Emit metadata. |
| 5028 | *OutFile << ResultStr; |
| 5029 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5030 | OutFile->flush(); |
| 5031 | } |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5032 | |
| 5033 | void RewriteObjCFragileABI::Initialize(ASTContext &context) { |
| 5034 | InitializeCommon(context); |
| 5035 | |
| 5036 | // declaring objc_selector outside the parameter list removes a silly |
| 5037 | // scope related warning... |
| 5038 | if (IsHeader) |
| 5039 | Preamble = "#pragma once\n"; |
| 5040 | Preamble += "struct objc_selector; struct objc_class;\n"; |
| 5041 | Preamble += "struct __rw_objc_super { struct objc_object *object; "; |
| 5042 | Preamble += "struct objc_object *superClass; "; |
| 5043 | if (LangOpts.MicrosoftExt) { |
| 5044 | // Add a constructor for creating temporary objects. |
| 5045 | Preamble += "__rw_objc_super(struct objc_object *o, struct objc_object *s) " |
| 5046 | ": "; |
| 5047 | Preamble += "object(o), superClass(s) {} "; |
| 5048 | } |
| 5049 | Preamble += "};\n"; |
| 5050 | Preamble += "#ifndef _REWRITER_typedef_Protocol\n"; |
| 5051 | Preamble += "typedef struct objc_object Protocol;\n"; |
| 5052 | Preamble += "#define _REWRITER_typedef_Protocol\n"; |
| 5053 | Preamble += "#endif\n"; |
| 5054 | if (LangOpts.MicrosoftExt) { |
| 5055 | Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n"; |
| 5056 | Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n"; |
| 5057 | } else |
| 5058 | Preamble += "#define __OBJC_RW_DLLIMPORT extern\n"; |
| 5059 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend"; |
| 5060 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
| 5061 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper"; |
| 5062 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
| 5063 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object* objc_msgSend_stret"; |
| 5064 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
| 5065 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object* objc_msgSendSuper_stret"; |
| 5066 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
| 5067 | Preamble += "__OBJC_RW_DLLIMPORT double objc_msgSend_fpret"; |
| 5068 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
| 5069 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getClass"; |
| 5070 | Preamble += "(const char *);\n"; |
| 5071 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_class *class_getSuperclass"; |
| 5072 | Preamble += "(struct objc_class *);\n"; |
| 5073 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getMetaClass"; |
| 5074 | Preamble += "(const char *);\n"; |
| 5075 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw(struct objc_object *);\n"; |
| 5076 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_enter(void *);\n"; |
| 5077 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_exit(void *);\n"; |
| 5078 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_exception_extract(void *);\n"; |
| 5079 | Preamble += "__OBJC_RW_DLLIMPORT int objc_exception_match"; |
| 5080 | Preamble += "(struct objc_class *, struct objc_object *);\n"; |
| 5081 | // @synchronized hooks. |
| 5082 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_enter(struct objc_object *);\n"; |
| 5083 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_exit(struct objc_object *);\n"; |
| 5084 | Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n"; |
| 5085 | Preamble += "#ifndef __FASTENUMERATIONSTATE\n"; |
| 5086 | Preamble += "struct __objcFastEnumerationState {\n\t"; |
| 5087 | Preamble += "unsigned long state;\n\t"; |
| 5088 | Preamble += "void **itemsPtr;\n\t"; |
| 5089 | Preamble += "unsigned long *mutationsPtr;\n\t"; |
| 5090 | Preamble += "unsigned long extra[5];\n};\n"; |
| 5091 | Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n"; |
| 5092 | Preamble += "#define __FASTENUMERATIONSTATE\n"; |
| 5093 | Preamble += "#endif\n"; |
| 5094 | Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n"; |
| 5095 | Preamble += "struct __NSConstantStringImpl {\n"; |
| 5096 | Preamble += " int *isa;\n"; |
| 5097 | Preamble += " int flags;\n"; |
| 5098 | Preamble += " char *str;\n"; |
| 5099 | Preamble += " long length;\n"; |
| 5100 | Preamble += "};\n"; |
| 5101 | Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n"; |
| 5102 | Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n"; |
| 5103 | Preamble += "#else\n"; |
| 5104 | Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n"; |
| 5105 | Preamble += "#endif\n"; |
| 5106 | Preamble += "#define __NSCONSTANTSTRINGIMPL\n"; |
| 5107 | Preamble += "#endif\n"; |
| 5108 | // Blocks preamble. |
| 5109 | Preamble += "#ifndef BLOCK_IMPL\n"; |
| 5110 | Preamble += "#define BLOCK_IMPL\n"; |
| 5111 | Preamble += "struct __block_impl {\n"; |
| 5112 | Preamble += " void *isa;\n"; |
| 5113 | Preamble += " int Flags;\n"; |
| 5114 | Preamble += " int Reserved;\n"; |
| 5115 | Preamble += " void *FuncPtr;\n"; |
| 5116 | Preamble += "};\n"; |
| 5117 | Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n"; |
| 5118 | Preamble += "#ifdef __OBJC_EXPORT_BLOCKS\n"; |
| 5119 | Preamble += "extern \"C\" __declspec(dllexport) " |
| 5120 | "void _Block_object_assign(void *, const void *, const int);\n"; |
| 5121 | Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_dispose(const void *, const int);\n"; |
| 5122 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteGlobalBlock[32];\n"; |
| 5123 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteStackBlock[32];\n"; |
| 5124 | Preamble += "#else\n"; |
| 5125 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_assign(void *, const void *, const int);\n"; |
| 5126 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_dispose(const void *, const int);\n"; |
| 5127 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteGlobalBlock[32];\n"; |
| 5128 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteStackBlock[32];\n"; |
| 5129 | Preamble += "#endif\n"; |
| 5130 | Preamble += "#endif\n"; |
| 5131 | if (LangOpts.MicrosoftExt) { |
| 5132 | Preamble += "#undef __OBJC_RW_DLLIMPORT\n"; |
| 5133 | Preamble += "#undef __OBJC_RW_STATICIMPORT\n"; |
| 5134 | Preamble += "#ifndef KEEP_ATTRIBUTES\n"; // We use this for clang tests. |
| 5135 | Preamble += "#define __attribute__(X)\n"; |
| 5136 | Preamble += "#endif\n"; |
| 5137 | Preamble += "#define __weak\n"; |
| 5138 | } |
| 5139 | else { |
| 5140 | Preamble += "#define __block\n"; |
| 5141 | Preamble += "#define __weak\n"; |
| 5142 | } |
| 5143 | // NOTE! Windows uses LLP64 for 64bit mode. So, cast pointer to long long |
| 5144 | // as this avoids warning in any 64bit/32bit compilation model. |
| 5145 | Preamble += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((long long) &((TYPE *)0)->MEMBER)\n"; |
| 5146 | } |
| 5147 | |
| 5148 | /// RewriteIvarOffsetComputation - This rutine synthesizes computation of |
| 5149 | /// ivar offset. |
| 5150 | void RewriteObjCFragileABI::RewriteIvarOffsetComputation(ObjCIvarDecl *ivar, |
| 5151 | std::string &Result) { |
| 5152 | if (ivar->isBitField()) { |
| 5153 | // FIXME: The hack below doesn't work for bitfields. For now, we simply |
| 5154 | // place all bitfields at offset 0. |
| 5155 | Result += "0"; |
| 5156 | } else { |
| 5157 | Result += "__OFFSETOFIVAR__(struct "; |
| 5158 | Result += ivar->getContainingInterface()->getNameAsString(); |
| 5159 | if (LangOpts.MicrosoftExt) |
| 5160 | Result += "_IMPL"; |
| 5161 | Result += ", "; |
| 5162 | Result += ivar->getNameAsString(); |
| 5163 | Result += ")"; |
| 5164 | } |
| 5165 | } |
| 5166 | |
| 5167 | /// RewriteObjCProtocolMetaData - Rewrite protocols meta-data. |
| 5168 | void RewriteObjCFragileABI::RewriteObjCProtocolMetaData( |
| 5169 | ObjCProtocolDecl *PDecl, StringRef prefix, |
| 5170 | StringRef ClassName, std::string &Result) { |
| 5171 | static bool objc_protocol_methods = false; |
| 5172 | |
| 5173 | // Output struct protocol_methods holder of method selector and type. |
Douglas Gregor | 5e2a1ff | 2012-01-01 19:29:29 +0000 | [diff] [blame^] | 5174 | if (!objc_protocol_methods && PDecl->hasDefinition()) { |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5175 | /* struct protocol_methods { |
| 5176 | SEL _cmd; |
| 5177 | char *method_types; |
| 5178 | } |
| 5179 | */ |
| 5180 | Result += "\nstruct _protocol_methods {\n"; |
| 5181 | Result += "\tstruct objc_selector *_cmd;\n"; |
| 5182 | Result += "\tchar *method_types;\n"; |
| 5183 | Result += "};\n"; |
| 5184 | |
| 5185 | objc_protocol_methods = true; |
| 5186 | } |
| 5187 | // Do not synthesize the protocol more than once. |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 5188 | if (ObjCSynthesizedProtocols.count(PDecl->getCanonicalDecl())) |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5189 | return; |
| 5190 | |
| 5191 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 5192 | unsigned NumMethods = std::distance(PDecl->instmeth_begin(), |
| 5193 | PDecl->instmeth_end()); |
| 5194 | /* struct _objc_protocol_method_list { |
| 5195 | int protocol_method_count; |
| 5196 | struct protocol_methods protocols[]; |
| 5197 | } |
| 5198 | */ |
| 5199 | Result += "\nstatic struct {\n"; |
| 5200 | Result += "\tint protocol_method_count;\n"; |
| 5201 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 5202 | Result += utostr(NumMethods); |
| 5203 | Result += "];\n} _OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 5204 | Result += PDecl->getNameAsString(); |
| 5205 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= " |
| 5206 | "{\n\t" + utostr(NumMethods) + "\n"; |
| 5207 | |
| 5208 | // Output instance methods declared in this protocol. |
| 5209 | for (ObjCProtocolDecl::instmeth_iterator |
| 5210 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
| 5211 | I != E; ++I) { |
| 5212 | if (I == PDecl->instmeth_begin()) |
| 5213 | Result += "\t ,{{(struct objc_selector *)\""; |
| 5214 | else |
| 5215 | Result += "\t ,{(struct objc_selector *)\""; |
| 5216 | Result += (*I)->getSelector().getAsString(); |
| 5217 | std::string MethodTypeString; |
| 5218 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 5219 | Result += "\", \""; |
| 5220 | Result += MethodTypeString; |
| 5221 | Result += "\"}\n"; |
| 5222 | } |
| 5223 | Result += "\t }\n};\n"; |
| 5224 | } |
| 5225 | |
| 5226 | // Output class methods declared in this protocol. |
| 5227 | unsigned NumMethods = std::distance(PDecl->classmeth_begin(), |
| 5228 | PDecl->classmeth_end()); |
| 5229 | if (NumMethods > 0) { |
| 5230 | /* struct _objc_protocol_method_list { |
| 5231 | int protocol_method_count; |
| 5232 | struct protocol_methods protocols[]; |
| 5233 | } |
| 5234 | */ |
| 5235 | Result += "\nstatic struct {\n"; |
| 5236 | Result += "\tint protocol_method_count;\n"; |
| 5237 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 5238 | Result += utostr(NumMethods); |
| 5239 | Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_"; |
| 5240 | Result += PDecl->getNameAsString(); |
| 5241 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 5242 | "{\n\t"; |
| 5243 | Result += utostr(NumMethods); |
| 5244 | Result += "\n"; |
| 5245 | |
| 5246 | // Output instance methods declared in this protocol. |
| 5247 | for (ObjCProtocolDecl::classmeth_iterator |
| 5248 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
| 5249 | I != E; ++I) { |
| 5250 | if (I == PDecl->classmeth_begin()) |
| 5251 | Result += "\t ,{{(struct objc_selector *)\""; |
| 5252 | else |
| 5253 | Result += "\t ,{(struct objc_selector *)\""; |
| 5254 | Result += (*I)->getSelector().getAsString(); |
| 5255 | std::string MethodTypeString; |
| 5256 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 5257 | Result += "\", \""; |
| 5258 | Result += MethodTypeString; |
| 5259 | Result += "\"}\n"; |
| 5260 | } |
| 5261 | Result += "\t }\n};\n"; |
| 5262 | } |
| 5263 | |
| 5264 | // Output: |
| 5265 | /* struct _objc_protocol { |
| 5266 | // Objective-C 1.0 extensions |
| 5267 | struct _objc_protocol_extension *isa; |
| 5268 | char *protocol_name; |
| 5269 | struct _objc_protocol **protocol_list; |
| 5270 | struct _objc_protocol_method_list *instance_methods; |
| 5271 | struct _objc_protocol_method_list *class_methods; |
| 5272 | }; |
| 5273 | */ |
| 5274 | static bool objc_protocol = false; |
| 5275 | if (!objc_protocol) { |
| 5276 | Result += "\nstruct _objc_protocol {\n"; |
| 5277 | Result += "\tstruct _objc_protocol_extension *isa;\n"; |
| 5278 | Result += "\tchar *protocol_name;\n"; |
| 5279 | Result += "\tstruct _objc_protocol **protocol_list;\n"; |
| 5280 | Result += "\tstruct _objc_protocol_method_list *instance_methods;\n"; |
| 5281 | Result += "\tstruct _objc_protocol_method_list *class_methods;\n"; |
| 5282 | Result += "};\n"; |
| 5283 | |
| 5284 | objc_protocol = true; |
| 5285 | } |
| 5286 | |
| 5287 | Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_"; |
| 5288 | Result += PDecl->getNameAsString(); |
| 5289 | Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= " |
| 5290 | "{\n\t0, \""; |
| 5291 | Result += PDecl->getNameAsString(); |
| 5292 | Result += "\", 0, "; |
| 5293 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 5294 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 5295 | Result += PDecl->getNameAsString(); |
| 5296 | Result += ", "; |
| 5297 | } |
| 5298 | else |
| 5299 | Result += "0, "; |
| 5300 | if (PDecl->classmeth_begin() != PDecl->classmeth_end()) { |
| 5301 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
| 5302 | Result += PDecl->getNameAsString(); |
| 5303 | Result += "\n"; |
| 5304 | } |
| 5305 | else |
| 5306 | Result += "0\n"; |
| 5307 | Result += "};\n"; |
| 5308 | |
| 5309 | // Mark this protocol as having been generated. |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 5310 | if (!ObjCSynthesizedProtocols.insert(PDecl->getCanonicalDecl())) |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5311 | llvm_unreachable("protocol already synthesized"); |
| 5312 | |
| 5313 | } |
| 5314 | |
| 5315 | void RewriteObjCFragileABI::RewriteObjCProtocolListMetaData( |
| 5316 | const ObjCList<ObjCProtocolDecl> &Protocols, |
| 5317 | StringRef prefix, StringRef ClassName, |
| 5318 | std::string &Result) { |
| 5319 | if (Protocols.empty()) return; |
| 5320 | |
| 5321 | for (unsigned i = 0; i != Protocols.size(); i++) |
| 5322 | RewriteObjCProtocolMetaData(Protocols[i], prefix, ClassName, Result); |
| 5323 | |
| 5324 | // Output the top lovel protocol meta-data for the class. |
| 5325 | /* struct _objc_protocol_list { |
| 5326 | struct _objc_protocol_list *next; |
| 5327 | int protocol_count; |
| 5328 | struct _objc_protocol *class_protocols[]; |
| 5329 | } |
| 5330 | */ |
| 5331 | Result += "\nstatic struct {\n"; |
| 5332 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 5333 | Result += "\tint protocol_count;\n"; |
| 5334 | Result += "\tstruct _objc_protocol *class_protocols["; |
| 5335 | Result += utostr(Protocols.size()); |
| 5336 | Result += "];\n} _OBJC_"; |
| 5337 | Result += prefix; |
| 5338 | Result += "_PROTOCOLS_"; |
| 5339 | Result += ClassName; |
| 5340 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 5341 | "{\n\t0, "; |
| 5342 | Result += utostr(Protocols.size()); |
| 5343 | Result += "\n"; |
| 5344 | |
| 5345 | Result += "\t,{&_OBJC_PROTOCOL_"; |
| 5346 | Result += Protocols[0]->getNameAsString(); |
| 5347 | Result += " \n"; |
| 5348 | |
| 5349 | for (unsigned i = 1; i != Protocols.size(); i++) { |
| 5350 | Result += "\t ,&_OBJC_PROTOCOL_"; |
| 5351 | Result += Protocols[i]->getNameAsString(); |
| 5352 | Result += "\n"; |
| 5353 | } |
| 5354 | Result += "\t }\n};\n"; |
| 5355 | } |
| 5356 | |
| 5357 | void RewriteObjCFragileABI::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
| 5358 | std::string &Result) { |
| 5359 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
| 5360 | |
| 5361 | // Explicitly declared @interface's are already synthesized. |
| 5362 | if (CDecl->isImplicitInterfaceDecl()) { |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 5363 | // FIXME: Implementation of a class with no @interface (legacy) does not |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5364 | // produce correct synthesis as yet. |
| 5365 | RewriteObjCInternalStruct(CDecl, Result); |
| 5366 | } |
| 5367 | |
| 5368 | // Build _objc_ivar_list metadata for classes ivars if needed |
| 5369 | unsigned NumIvars = !IDecl->ivar_empty() |
| 5370 | ? IDecl->ivar_size() |
| 5371 | : (CDecl ? CDecl->ivar_size() : 0); |
| 5372 | if (NumIvars > 0) { |
| 5373 | static bool objc_ivar = false; |
| 5374 | if (!objc_ivar) { |
| 5375 | /* struct _objc_ivar { |
| 5376 | char *ivar_name; |
| 5377 | char *ivar_type; |
| 5378 | int ivar_offset; |
| 5379 | }; |
| 5380 | */ |
| 5381 | Result += "\nstruct _objc_ivar {\n"; |
| 5382 | Result += "\tchar *ivar_name;\n"; |
| 5383 | Result += "\tchar *ivar_type;\n"; |
| 5384 | Result += "\tint ivar_offset;\n"; |
| 5385 | Result += "};\n"; |
| 5386 | |
| 5387 | objc_ivar = true; |
| 5388 | } |
| 5389 | |
| 5390 | /* struct { |
| 5391 | int ivar_count; |
| 5392 | struct _objc_ivar ivar_list[nIvars]; |
| 5393 | }; |
| 5394 | */ |
| 5395 | Result += "\nstatic struct {\n"; |
| 5396 | Result += "\tint ivar_count;\n"; |
| 5397 | Result += "\tstruct _objc_ivar ivar_list["; |
| 5398 | Result += utostr(NumIvars); |
| 5399 | Result += "];\n} _OBJC_INSTANCE_VARIABLES_"; |
| 5400 | Result += IDecl->getNameAsString(); |
| 5401 | Result += " __attribute__ ((used, section (\"__OBJC, __instance_vars\")))= " |
| 5402 | "{\n\t"; |
| 5403 | Result += utostr(NumIvars); |
| 5404 | Result += "\n"; |
| 5405 | |
| 5406 | ObjCInterfaceDecl::ivar_iterator IVI, IVE; |
| 5407 | SmallVector<ObjCIvarDecl *, 8> IVars; |
| 5408 | if (!IDecl->ivar_empty()) { |
| 5409 | for (ObjCInterfaceDecl::ivar_iterator |
| 5410 | IV = IDecl->ivar_begin(), IVEnd = IDecl->ivar_end(); |
| 5411 | IV != IVEnd; ++IV) |
| 5412 | IVars.push_back(*IV); |
| 5413 | IVI = IDecl->ivar_begin(); |
| 5414 | IVE = IDecl->ivar_end(); |
| 5415 | } else { |
| 5416 | IVI = CDecl->ivar_begin(); |
| 5417 | IVE = CDecl->ivar_end(); |
| 5418 | } |
| 5419 | Result += "\t,{{\""; |
| 5420 | Result += (*IVI)->getNameAsString(); |
| 5421 | Result += "\", \""; |
| 5422 | std::string TmpString, StrEncoding; |
| 5423 | Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI); |
| 5424 | QuoteDoublequotes(TmpString, StrEncoding); |
| 5425 | Result += StrEncoding; |
| 5426 | Result += "\", "; |
| 5427 | RewriteIvarOffsetComputation(*IVI, Result); |
| 5428 | Result += "}\n"; |
| 5429 | for (++IVI; IVI != IVE; ++IVI) { |
| 5430 | Result += "\t ,{\""; |
| 5431 | Result += (*IVI)->getNameAsString(); |
| 5432 | Result += "\", \""; |
| 5433 | std::string TmpString, StrEncoding; |
| 5434 | Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI); |
| 5435 | QuoteDoublequotes(TmpString, StrEncoding); |
| 5436 | Result += StrEncoding; |
| 5437 | Result += "\", "; |
| 5438 | RewriteIvarOffsetComputation((*IVI), Result); |
| 5439 | Result += "}\n"; |
| 5440 | } |
| 5441 | |
| 5442 | Result += "\t }\n};\n"; |
| 5443 | } |
| 5444 | |
| 5445 | // Build _objc_method_list for class's instance methods if needed |
| 5446 | SmallVector<ObjCMethodDecl *, 32> |
| 5447 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
| 5448 | |
| 5449 | // If any of our property implementations have associated getters or |
| 5450 | // setters, produce metadata for them as well. |
| 5451 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 5452 | PropEnd = IDecl->propimpl_end(); |
| 5453 | Prop != PropEnd; ++Prop) { |
| 5454 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 5455 | continue; |
| 5456 | if (!(*Prop)->getPropertyIvarDecl()) |
| 5457 | continue; |
| 5458 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 5459 | if (!PD) |
| 5460 | continue; |
| 5461 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 5462 | if (!Getter->isDefined()) |
| 5463 | InstanceMethods.push_back(Getter); |
| 5464 | if (PD->isReadOnly()) |
| 5465 | continue; |
| 5466 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 5467 | if (!Setter->isDefined()) |
| 5468 | InstanceMethods.push_back(Setter); |
| 5469 | } |
| 5470 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
| 5471 | true, "", IDecl->getName(), Result); |
| 5472 | |
| 5473 | // Build _objc_method_list for class's class methods if needed |
| 5474 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
| 5475 | false, "", IDecl->getName(), Result); |
| 5476 | |
| 5477 | // Protocols referenced in class declaration? |
| 5478 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), |
| 5479 | "CLASS", CDecl->getName(), Result); |
| 5480 | |
| 5481 | // Declaration of class/meta-class metadata |
| 5482 | /* struct _objc_class { |
| 5483 | struct _objc_class *isa; // or const char *root_class_name when metadata |
| 5484 | const char *super_class_name; |
| 5485 | char *name; |
| 5486 | long version; |
| 5487 | long info; |
| 5488 | long instance_size; |
| 5489 | struct _objc_ivar_list *ivars; |
| 5490 | struct _objc_method_list *methods; |
| 5491 | struct objc_cache *cache; |
| 5492 | struct objc_protocol_list *protocols; |
| 5493 | const char *ivar_layout; |
| 5494 | struct _objc_class_ext *ext; |
| 5495 | }; |
| 5496 | */ |
| 5497 | static bool objc_class = false; |
| 5498 | if (!objc_class) { |
| 5499 | Result += "\nstruct _objc_class {\n"; |
| 5500 | Result += "\tstruct _objc_class *isa;\n"; |
| 5501 | Result += "\tconst char *super_class_name;\n"; |
| 5502 | Result += "\tchar *name;\n"; |
| 5503 | Result += "\tlong version;\n"; |
| 5504 | Result += "\tlong info;\n"; |
| 5505 | Result += "\tlong instance_size;\n"; |
| 5506 | Result += "\tstruct _objc_ivar_list *ivars;\n"; |
| 5507 | Result += "\tstruct _objc_method_list *methods;\n"; |
| 5508 | Result += "\tstruct objc_cache *cache;\n"; |
| 5509 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 5510 | Result += "\tconst char *ivar_layout;\n"; |
| 5511 | Result += "\tstruct _objc_class_ext *ext;\n"; |
| 5512 | Result += "};\n"; |
| 5513 | objc_class = true; |
| 5514 | } |
| 5515 | |
| 5516 | // Meta-class metadata generation. |
| 5517 | ObjCInterfaceDecl *RootClass = 0; |
| 5518 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
| 5519 | while (SuperClass) { |
| 5520 | RootClass = SuperClass; |
| 5521 | SuperClass = SuperClass->getSuperClass(); |
| 5522 | } |
| 5523 | SuperClass = CDecl->getSuperClass(); |
| 5524 | |
| 5525 | Result += "\nstatic struct _objc_class _OBJC_METACLASS_"; |
| 5526 | Result += CDecl->getNameAsString(); |
| 5527 | Result += " __attribute__ ((used, section (\"__OBJC, __meta_class\")))= " |
| 5528 | "{\n\t(struct _objc_class *)\""; |
| 5529 | Result += (RootClass ? RootClass->getNameAsString() : CDecl->getNameAsString()); |
| 5530 | Result += "\""; |
| 5531 | |
| 5532 | if (SuperClass) { |
| 5533 | Result += ", \""; |
| 5534 | Result += SuperClass->getNameAsString(); |
| 5535 | Result += "\", \""; |
| 5536 | Result += CDecl->getNameAsString(); |
| 5537 | Result += "\""; |
| 5538 | } |
| 5539 | else { |
| 5540 | Result += ", 0, \""; |
| 5541 | Result += CDecl->getNameAsString(); |
| 5542 | Result += "\""; |
| 5543 | } |
| 5544 | // Set 'ivars' field for root class to 0. ObjC1 runtime does not use it. |
| 5545 | // 'info' field is initialized to CLS_META(2) for metaclass |
| 5546 | Result += ", 0,2, sizeof(struct _objc_class), 0"; |
| 5547 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
| 5548 | Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_"; |
| 5549 | Result += IDecl->getNameAsString(); |
| 5550 | Result += "\n"; |
| 5551 | } |
| 5552 | else |
| 5553 | Result += ", 0\n"; |
| 5554 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
| 5555 | Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_"; |
| 5556 | Result += CDecl->getNameAsString(); |
| 5557 | Result += ",0,0\n"; |
| 5558 | } |
| 5559 | else |
| 5560 | Result += "\t,0,0,0,0\n"; |
| 5561 | Result += "};\n"; |
| 5562 | |
| 5563 | // class metadata generation. |
| 5564 | Result += "\nstatic struct _objc_class _OBJC_CLASS_"; |
| 5565 | Result += CDecl->getNameAsString(); |
| 5566 | Result += " __attribute__ ((used, section (\"__OBJC, __class\")))= " |
| 5567 | "{\n\t&_OBJC_METACLASS_"; |
| 5568 | Result += CDecl->getNameAsString(); |
| 5569 | if (SuperClass) { |
| 5570 | Result += ", \""; |
| 5571 | Result += SuperClass->getNameAsString(); |
| 5572 | Result += "\", \""; |
| 5573 | Result += CDecl->getNameAsString(); |
| 5574 | Result += "\""; |
| 5575 | } |
| 5576 | else { |
| 5577 | Result += ", 0, \""; |
| 5578 | Result += CDecl->getNameAsString(); |
| 5579 | Result += "\""; |
| 5580 | } |
| 5581 | // 'info' field is initialized to CLS_CLASS(1) for class |
| 5582 | Result += ", 0,1"; |
| 5583 | if (!ObjCSynthesizedStructs.count(CDecl)) |
| 5584 | Result += ",0"; |
| 5585 | else { |
| 5586 | // class has size. Must synthesize its size. |
| 5587 | Result += ",sizeof(struct "; |
| 5588 | Result += CDecl->getNameAsString(); |
| 5589 | if (LangOpts.MicrosoftExt) |
| 5590 | Result += "_IMPL"; |
| 5591 | Result += ")"; |
| 5592 | } |
| 5593 | if (NumIvars > 0) { |
| 5594 | Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_"; |
| 5595 | Result += CDecl->getNameAsString(); |
| 5596 | Result += "\n\t"; |
| 5597 | } |
| 5598 | else |
| 5599 | Result += ",0"; |
| 5600 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
| 5601 | Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_"; |
| 5602 | Result += CDecl->getNameAsString(); |
| 5603 | Result += ", 0\n\t"; |
| 5604 | } |
| 5605 | else |
| 5606 | Result += ",0,0"; |
| 5607 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
| 5608 | Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_"; |
| 5609 | Result += CDecl->getNameAsString(); |
| 5610 | Result += ", 0,0\n"; |
| 5611 | } |
| 5612 | else |
| 5613 | Result += ",0,0,0\n"; |
| 5614 | Result += "};\n"; |
| 5615 | } |
| 5616 | |
| 5617 | void RewriteObjCFragileABI::RewriteMetaDataIntoBuffer(std::string &Result) { |
| 5618 | int ClsDefCount = ClassImplementation.size(); |
| 5619 | int CatDefCount = CategoryImplementation.size(); |
| 5620 | |
| 5621 | // For each implemented class, write out all its meta data. |
| 5622 | for (int i = 0; i < ClsDefCount; i++) |
| 5623 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
| 5624 | |
| 5625 | // For each implemented category, write out all its meta data. |
| 5626 | for (int i = 0; i < CatDefCount; i++) |
| 5627 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
| 5628 | |
| 5629 | // Write objc_symtab metadata |
| 5630 | /* |
| 5631 | struct _objc_symtab |
| 5632 | { |
| 5633 | long sel_ref_cnt; |
| 5634 | SEL *refs; |
| 5635 | short cls_def_cnt; |
| 5636 | short cat_def_cnt; |
| 5637 | void *defs[cls_def_cnt + cat_def_cnt]; |
| 5638 | }; |
| 5639 | */ |
| 5640 | |
| 5641 | Result += "\nstruct _objc_symtab {\n"; |
| 5642 | Result += "\tlong sel_ref_cnt;\n"; |
| 5643 | Result += "\tSEL *refs;\n"; |
| 5644 | Result += "\tshort cls_def_cnt;\n"; |
| 5645 | Result += "\tshort cat_def_cnt;\n"; |
| 5646 | Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n"; |
| 5647 | Result += "};\n\n"; |
| 5648 | |
| 5649 | Result += "static struct _objc_symtab " |
| 5650 | "_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n"; |
| 5651 | Result += "\t0, 0, " + utostr(ClsDefCount) |
| 5652 | + ", " + utostr(CatDefCount) + "\n"; |
| 5653 | for (int i = 0; i < ClsDefCount; i++) { |
| 5654 | Result += "\t,&_OBJC_CLASS_"; |
| 5655 | Result += ClassImplementation[i]->getNameAsString(); |
| 5656 | Result += "\n"; |
| 5657 | } |
| 5658 | |
| 5659 | for (int i = 0; i < CatDefCount; i++) { |
| 5660 | Result += "\t,&_OBJC_CATEGORY_"; |
| 5661 | Result += CategoryImplementation[i]->getClassInterface()->getNameAsString(); |
| 5662 | Result += "_"; |
| 5663 | Result += CategoryImplementation[i]->getNameAsString(); |
| 5664 | Result += "\n"; |
| 5665 | } |
| 5666 | |
| 5667 | Result += "};\n\n"; |
| 5668 | |
| 5669 | // Write objc_module metadata |
| 5670 | |
| 5671 | /* |
| 5672 | struct _objc_module { |
| 5673 | long version; |
| 5674 | long size; |
| 5675 | const char *name; |
| 5676 | struct _objc_symtab *symtab; |
| 5677 | } |
| 5678 | */ |
| 5679 | |
| 5680 | Result += "\nstruct _objc_module {\n"; |
| 5681 | Result += "\tlong version;\n"; |
| 5682 | Result += "\tlong size;\n"; |
| 5683 | Result += "\tconst char *name;\n"; |
| 5684 | Result += "\tstruct _objc_symtab *symtab;\n"; |
| 5685 | Result += "};\n\n"; |
| 5686 | Result += "static struct _objc_module " |
| 5687 | "_OBJC_MODULES __attribute__ ((used, section (\"__OBJC, __module_info\")))= {\n"; |
| 5688 | Result += "\t" + utostr(OBJC_ABI_VERSION) + |
| 5689 | ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n"; |
| 5690 | Result += "};\n\n"; |
| 5691 | |
| 5692 | if (LangOpts.MicrosoftExt) { |
| 5693 | if (ProtocolExprDecls.size()) { |
| 5694 | Result += "#pragma section(\".objc_protocol$B\",long,read,write)\n"; |
| 5695 | Result += "#pragma data_seg(push, \".objc_protocol$B\")\n"; |
| 5696 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
| 5697 | E = ProtocolExprDecls.end(); I != E; ++I) { |
| 5698 | Result += "static struct _objc_protocol *_POINTER_OBJC_PROTOCOL_"; |
| 5699 | Result += (*I)->getNameAsString(); |
| 5700 | Result += " = &_OBJC_PROTOCOL_"; |
| 5701 | Result += (*I)->getNameAsString(); |
| 5702 | Result += ";\n"; |
| 5703 | } |
| 5704 | Result += "#pragma data_seg(pop)\n\n"; |
| 5705 | } |
| 5706 | Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n"; |
| 5707 | Result += "#pragma data_seg(push, \".objc_module_info$B\")\n"; |
| 5708 | Result += "static struct _objc_module *_POINTER_OBJC_MODULES = "; |
| 5709 | Result += "&_OBJC_MODULES;\n"; |
| 5710 | Result += "#pragma data_seg(pop)\n\n"; |
| 5711 | } |
| 5712 | } |
| 5713 | |
| 5714 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
| 5715 | /// implementation. |
| 5716 | void RewriteObjCFragileABI::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
| 5717 | std::string &Result) { |
| 5718 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
| 5719 | // Find category declaration for this implementation. |
| 5720 | ObjCCategoryDecl *CDecl; |
| 5721 | for (CDecl = ClassDecl->getCategoryList(); CDecl; |
| 5722 | CDecl = CDecl->getNextClassCategory()) |
| 5723 | if (CDecl->getIdentifier() == IDecl->getIdentifier()) |
| 5724 | break; |
| 5725 | |
| 5726 | std::string FullCategoryName = ClassDecl->getNameAsString(); |
| 5727 | FullCategoryName += '_'; |
| 5728 | FullCategoryName += IDecl->getNameAsString(); |
| 5729 | |
| 5730 | // Build _objc_method_list for class's instance methods if needed |
| 5731 | SmallVector<ObjCMethodDecl *, 32> |
| 5732 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
| 5733 | |
| 5734 | // If any of our property implementations have associated getters or |
| 5735 | // setters, produce metadata for them as well. |
| 5736 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 5737 | PropEnd = IDecl->propimpl_end(); |
| 5738 | Prop != PropEnd; ++Prop) { |
| 5739 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 5740 | continue; |
| 5741 | if (!(*Prop)->getPropertyIvarDecl()) |
| 5742 | continue; |
| 5743 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 5744 | if (!PD) |
| 5745 | continue; |
| 5746 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 5747 | InstanceMethods.push_back(Getter); |
| 5748 | if (PD->isReadOnly()) |
| 5749 | continue; |
| 5750 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 5751 | InstanceMethods.push_back(Setter); |
| 5752 | } |
| 5753 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
| 5754 | true, "CATEGORY_", FullCategoryName.c_str(), |
| 5755 | Result); |
| 5756 | |
| 5757 | // Build _objc_method_list for class's class methods if needed |
| 5758 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
| 5759 | false, "CATEGORY_", FullCategoryName.c_str(), |
| 5760 | Result); |
| 5761 | |
| 5762 | // Protocols referenced in class declaration? |
| 5763 | // Null CDecl is case of a category implementation with no category interface |
| 5764 | if (CDecl) |
| 5765 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), "CATEGORY", |
| 5766 | FullCategoryName, Result); |
| 5767 | /* struct _objc_category { |
| 5768 | char *category_name; |
| 5769 | char *class_name; |
| 5770 | struct _objc_method_list *instance_methods; |
| 5771 | struct _objc_method_list *class_methods; |
| 5772 | struct _objc_protocol_list *protocols; |
| 5773 | // Objective-C 1.0 extensions |
| 5774 | uint32_t size; // sizeof (struct _objc_category) |
| 5775 | struct _objc_property_list *instance_properties; // category's own |
| 5776 | // @property decl. |
| 5777 | }; |
| 5778 | */ |
| 5779 | |
| 5780 | static bool objc_category = false; |
| 5781 | if (!objc_category) { |
| 5782 | Result += "\nstruct _objc_category {\n"; |
| 5783 | Result += "\tchar *category_name;\n"; |
| 5784 | Result += "\tchar *class_name;\n"; |
| 5785 | Result += "\tstruct _objc_method_list *instance_methods;\n"; |
| 5786 | Result += "\tstruct _objc_method_list *class_methods;\n"; |
| 5787 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 5788 | Result += "\tunsigned int size;\n"; |
| 5789 | Result += "\tstruct _objc_property_list *instance_properties;\n"; |
| 5790 | Result += "};\n"; |
| 5791 | objc_category = true; |
| 5792 | } |
| 5793 | Result += "\nstatic struct _objc_category _OBJC_CATEGORY_"; |
| 5794 | Result += FullCategoryName; |
| 5795 | Result += " __attribute__ ((used, section (\"__OBJC, __category\")))= {\n\t\""; |
| 5796 | Result += IDecl->getNameAsString(); |
| 5797 | Result += "\"\n\t, \""; |
| 5798 | Result += ClassDecl->getNameAsString(); |
| 5799 | Result += "\"\n"; |
| 5800 | |
| 5801 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
| 5802 | Result += "\t, (struct _objc_method_list *)" |
| 5803 | "&_OBJC_CATEGORY_INSTANCE_METHODS_"; |
| 5804 | Result += FullCategoryName; |
| 5805 | Result += "\n"; |
| 5806 | } |
| 5807 | else |
| 5808 | Result += "\t, 0\n"; |
| 5809 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
| 5810 | Result += "\t, (struct _objc_method_list *)" |
| 5811 | "&_OBJC_CATEGORY_CLASS_METHODS_"; |
| 5812 | Result += FullCategoryName; |
| 5813 | Result += "\n"; |
| 5814 | } |
| 5815 | else |
| 5816 | Result += "\t, 0\n"; |
| 5817 | |
| 5818 | if (CDecl && CDecl->protocol_begin() != CDecl->protocol_end()) { |
| 5819 | Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_"; |
| 5820 | Result += FullCategoryName; |
| 5821 | Result += "\n"; |
| 5822 | } |
| 5823 | else |
| 5824 | Result += "\t, 0\n"; |
| 5825 | Result += "\t, sizeof(struct _objc_category), 0\n};\n"; |
| 5826 | } |
| 5827 | |
| 5828 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
| 5829 | /// class methods. |
| 5830 | template<typename MethodIterator> |
| 5831 | void RewriteObjCFragileABI::RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 5832 | MethodIterator MethodEnd, |
| 5833 | bool IsInstanceMethod, |
| 5834 | StringRef prefix, |
| 5835 | StringRef ClassName, |
| 5836 | std::string &Result) { |
| 5837 | if (MethodBegin == MethodEnd) return; |
| 5838 | |
| 5839 | if (!objc_impl_method) { |
| 5840 | /* struct _objc_method { |
| 5841 | SEL _cmd; |
| 5842 | char *method_types; |
| 5843 | void *_imp; |
| 5844 | } |
| 5845 | */ |
| 5846 | Result += "\nstruct _objc_method {\n"; |
| 5847 | Result += "\tSEL _cmd;\n"; |
| 5848 | Result += "\tchar *method_types;\n"; |
| 5849 | Result += "\tvoid *_imp;\n"; |
| 5850 | Result += "};\n"; |
| 5851 | |
| 5852 | objc_impl_method = true; |
| 5853 | } |
| 5854 | |
| 5855 | // Build _objc_method_list for class's methods if needed |
| 5856 | |
| 5857 | /* struct { |
| 5858 | struct _objc_method_list *next_method; |
| 5859 | int method_count; |
| 5860 | struct _objc_method method_list[]; |
| 5861 | } |
| 5862 | */ |
| 5863 | unsigned NumMethods = std::distance(MethodBegin, MethodEnd); |
| 5864 | Result += "\nstatic struct {\n"; |
| 5865 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 5866 | Result += "\tint method_count;\n"; |
| 5867 | Result += "\tstruct _objc_method method_list["; |
| 5868 | Result += utostr(NumMethods); |
| 5869 | Result += "];\n} _OBJC_"; |
| 5870 | Result += prefix; |
| 5871 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 5872 | Result += "_METHODS_"; |
| 5873 | Result += ClassName; |
| 5874 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
| 5875 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 5876 | Result += "_meth\")))= "; |
| 5877 | Result += "{\n\t0, " + utostr(NumMethods) + "\n"; |
| 5878 | |
| 5879 | Result += "\t,{{(SEL)\""; |
| 5880 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
| 5881 | std::string MethodTypeString; |
| 5882 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
| 5883 | Result += "\", \""; |
| 5884 | Result += MethodTypeString; |
| 5885 | Result += "\", (void *)"; |
| 5886 | Result += MethodInternalNames[*MethodBegin]; |
| 5887 | Result += "}\n"; |
| 5888 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 5889 | Result += "\t ,{(SEL)\""; |
| 5890 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
| 5891 | std::string MethodTypeString; |
| 5892 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
| 5893 | Result += "\", \""; |
| 5894 | Result += MethodTypeString; |
| 5895 | Result += "\", (void *)"; |
| 5896 | Result += MethodInternalNames[*MethodBegin]; |
| 5897 | Result += "}\n"; |
| 5898 | } |
| 5899 | Result += "\t }\n};\n"; |
| 5900 | } |
| 5901 | |
| 5902 | Stmt *RewriteObjCFragileABI::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV) { |
| 5903 | SourceRange OldRange = IV->getSourceRange(); |
| 5904 | Expr *BaseExpr = IV->getBase(); |
| 5905 | |
| 5906 | // Rewrite the base, but without actually doing replaces. |
| 5907 | { |
| 5908 | DisableReplaceStmtScope S(*this); |
| 5909 | BaseExpr = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(BaseExpr)); |
| 5910 | IV->setBase(BaseExpr); |
| 5911 | } |
| 5912 | |
| 5913 | ObjCIvarDecl *D = IV->getDecl(); |
| 5914 | |
| 5915 | Expr *Replacement = IV; |
| 5916 | if (CurMethodDef) { |
| 5917 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
| 5918 | const ObjCInterfaceType *iFaceDecl = |
| 5919 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
| 5920 | assert(iFaceDecl && "RewriteObjCIvarRefExpr - iFaceDecl is null"); |
| 5921 | // lookup which class implements the instance variable. |
| 5922 | ObjCInterfaceDecl *clsDeclared = 0; |
| 5923 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
| 5924 | clsDeclared); |
| 5925 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
| 5926 | |
| 5927 | // Synthesize an explicit cast to gain access to the ivar. |
| 5928 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 5929 | RecName += "_IMPL"; |
| 5930 | IdentifierInfo *II = &Context->Idents.get(RecName); |
| 5931 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 5932 | SourceLocation(), SourceLocation(), |
| 5933 | II); |
| 5934 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 5935 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 5936 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, |
| 5937 | CK_BitCast, |
| 5938 | IV->getBase()); |
| 5939 | // Don't forget the parens to enforce the proper binding. |
| 5940 | ParenExpr *PE = new (Context) ParenExpr(OldRange.getBegin(), |
| 5941 | OldRange.getEnd(), |
| 5942 | castExpr); |
| 5943 | if (IV->isFreeIvar() && |
Douglas Gregor | 60ef308 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 5944 | declaresSameEntity(CurMethodDef->getClassInterface(), iFaceDecl->getDecl())) { |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5945 | MemberExpr *ME = new (Context) MemberExpr(PE, true, D, |
| 5946 | IV->getLocation(), |
| 5947 | D->getType(), |
| 5948 | VK_LValue, OK_Ordinary); |
| 5949 | Replacement = ME; |
| 5950 | } else { |
| 5951 | IV->setBase(PE); |
| 5952 | } |
| 5953 | } |
| 5954 | } else { // we are outside a method. |
| 5955 | assert(!IV->isFreeIvar() && "Cannot have a free standing ivar outside a method"); |
| 5956 | |
| 5957 | // Explicit ivar refs need to have a cast inserted. |
| 5958 | // FIXME: consider sharing some of this code with the code above. |
| 5959 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
| 5960 | const ObjCInterfaceType *iFaceDecl = |
| 5961 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
| 5962 | // lookup which class implements the instance variable. |
| 5963 | ObjCInterfaceDecl *clsDeclared = 0; |
| 5964 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
| 5965 | clsDeclared); |
| 5966 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
| 5967 | |
| 5968 | // Synthesize an explicit cast to gain access to the ivar. |
| 5969 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 5970 | RecName += "_IMPL"; |
| 5971 | IdentifierInfo *II = &Context->Idents.get(RecName); |
| 5972 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 5973 | SourceLocation(), SourceLocation(), |
| 5974 | II); |
| 5975 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 5976 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 5977 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, |
| 5978 | CK_BitCast, |
| 5979 | IV->getBase()); |
| 5980 | // Don't forget the parens to enforce the proper binding. |
| 5981 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
| 5982 | IV->getBase()->getLocEnd(), castExpr); |
| 5983 | // Cannot delete IV->getBase(), since PE points to it. |
| 5984 | // Replace the old base with the cast. This is important when doing |
| 5985 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
| 5986 | IV->setBase(PE); |
| 5987 | } |
| 5988 | } |
| 5989 | |
| 5990 | ReplaceStmtWithRange(IV, Replacement, OldRange); |
| 5991 | return Replacement; |
| 5992 | } |
| 5993 | |