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; |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 122 | SmallVector<DeclRefExpr *, 32> InnerDeclRefs; |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 123 | |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 124 | SmallVector<DeclRefExpr *, 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 | |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 175 | if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(*I)) { |
| 176 | if (!Proto->isThisDeclarationADefinition()) { |
| 177 | RewriteForwardProtocolDecl(D); |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 182 | HandleTopLevelSingleDecl(*I); |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 183 | } |
Argyrios Kyrtzidis | 88c2596 | 2011-11-18 00:26:59 +0000 | [diff] [blame] | 184 | return true; |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 185 | } |
| 186 | void HandleTopLevelSingleDecl(Decl *D); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 187 | void HandleDeclInMainFile(Decl *D); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 188 | RewriteObjC(std::string inFile, raw_ostream *OS, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 189 | DiagnosticsEngine &D, const LangOptions &LOpts, |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 190 | bool silenceMacroWarn); |
Ted Kremenek | e452e0f | 2008-08-08 04:15:52 +0000 | [diff] [blame] | 191 | |
| 192 | ~RewriteObjC() {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 193 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 194 | virtual void HandleTranslationUnit(ASTContext &C); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | |
Fariborz Jahanian | 88906cd | 2010-02-05 16:43:40 +0000 | [diff] [blame] | 196 | void ReplaceStmt(Stmt *Old, Stmt *New) { |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 197 | Stmt *ReplacingStmt = ReplacedNodes[Old]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 198 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 199 | if (ReplacingStmt) |
| 200 | return; // We can't rewrite the same node twice. |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 201 | |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 202 | if (DisableReplaceStmt) |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 203 | return; |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 204 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 205 | // If replacement succeeded or warning disabled return with no warning. |
Fariborz Jahanian | 88906cd | 2010-02-05 16:43:40 +0000 | [diff] [blame] | 206 | if (!Rewrite.ReplaceStmt(Old, New)) { |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 207 | ReplacedNodes[Old] = New; |
| 208 | return; |
| 209 | } |
| 210 | if (SilenceRewriteMacroWarning) |
| 211 | return; |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 212 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 213 | << Old->getSourceRange(); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 214 | } |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 215 | |
| 216 | void ReplaceStmtWithRange(Stmt *Old, Stmt *New, SourceRange SrcRange) { |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 217 | if (DisableReplaceStmt) |
| 218 | return; |
| 219 | |
Nick Lewycky | 7e74924 | 2010-10-31 21:07:24 +0000 | [diff] [blame] | 220 | // Measure the old text. |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 221 | int Size = Rewrite.getRangeSize(SrcRange); |
| 222 | if (Size == -1) { |
| 223 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 224 | << Old->getSourceRange(); |
| 225 | return; |
| 226 | } |
| 227 | // Get the new text. |
| 228 | std::string SStr; |
| 229 | llvm::raw_string_ostream S(SStr); |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 230 | New->printPretty(S, *Context, 0, PrintingPolicy(LangOpts)); |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 231 | const std::string &Str = S.str(); |
| 232 | |
| 233 | // If replacement succeeded or warning disabled return with no warning. |
Daniel Dunbar | d7407dc | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 234 | if (!Rewrite.ReplaceText(SrcRange.getBegin(), Size, Str)) { |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 235 | ReplacedNodes[Old] = New; |
| 236 | return; |
| 237 | } |
| 238 | if (SilenceRewriteMacroWarning) |
| 239 | return; |
| 240 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 241 | << Old->getSourceRange(); |
| 242 | } |
| 243 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 244 | void InsertText(SourceLocation Loc, StringRef Str, |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 245 | bool InsertAfter = true) { |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 246 | // If insertion succeeded or warning disabled return with no warning. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 247 | if (!Rewrite.InsertText(Loc, Str, InsertAfter) || |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 248 | SilenceRewriteMacroWarning) |
| 249 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 250 | |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 251 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 252 | } |
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 | void ReplaceText(SourceLocation Start, unsigned OrigLength, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 255 | StringRef Str) { |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 256 | // If removal succeeded or warning disabled return with no warning. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 257 | if (!Rewrite.ReplaceText(Start, OrigLength, Str) || |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 258 | SilenceRewriteMacroWarning) |
| 259 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 260 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 261 | Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag); |
| 262 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 263 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 264 | // Syntactic Rewriting. |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 265 | void RewriteRecordBody(RecordDecl *RD); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 266 | void RewriteInclude(); |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 267 | void RewriteForwardClassDecl(DeclGroupRef D); |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 268 | void RewriteForwardClassDecl(const llvm::SmallVector<Decl*, 8> &DG); |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 269 | void RewriteForwardClassEpilogue(ObjCInterfaceDecl *ClassDecl, |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 270 | const std::string &typedefString); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 271 | void RewriteImplementations(); |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 272 | void RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 273 | ObjCImplementationDecl *IMD, |
| 274 | ObjCCategoryImplDecl *CID); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 275 | void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 276 | void RewriteImplementationDecl(Decl *Dcl); |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 277 | void RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl, |
| 278 | ObjCMethodDecl *MDecl, std::string &ResultStr); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 279 | void RewriteTypeIntoString(QualType T, std::string &ResultStr, |
| 280 | const FunctionType *&FPRetType); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 281 | void RewriteByRefString(std::string &ResultStr, const std::string &Name, |
Fariborz Jahanian | 1e8011e | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 282 | ValueDecl *VD, bool def=false); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 283 | void RewriteCategoryDecl(ObjCCategoryDecl *Dcl); |
| 284 | void RewriteProtocolDecl(ObjCProtocolDecl *Dcl); |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 285 | void RewriteForwardProtocolDecl(DeclGroupRef D); |
| 286 | void RewriteForwardProtocolDecl(const llvm::SmallVector<Decl*, 8> &DG); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 287 | void RewriteMethodDeclaration(ObjCMethodDecl *Method); |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 288 | void RewriteProperty(ObjCPropertyDecl *prop); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 289 | void RewriteFunctionDecl(FunctionDecl *FD); |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 290 | void RewriteBlockPointerType(std::string& Str, QualType Type); |
| 291 | void RewriteBlockPointerTypeVariable(std::string& Str, ValueDecl *VD); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 292 | void RewriteBlockLiteralFunctionDecl(FunctionDecl *FD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 293 | void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 294 | void RewriteTypeOfDecl(VarDecl *VD); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 295 | void RewriteObjCQualifiedInterfaceTypes(Expr *E); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 296 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 297 | // Expression Rewriting. |
Steve Naroff | f3473a7 | 2007-11-09 15:20:18 +0000 | [diff] [blame] | 298 | Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S); |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 299 | Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 300 | Stmt *RewritePropertyOrImplicitGetter(PseudoObjectExpr *Pseudo); |
| 301 | Stmt *RewritePropertyOrImplicitSetter(PseudoObjectExpr *Pseudo); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 302 | Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp); |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 303 | Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp); |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 304 | Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp); |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 305 | Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 306 | void RewriteTryReturnStmts(Stmt *S); |
| 307 | void RewriteSyncReturnStmts(Stmt *S, std::string buf); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 308 | Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 309 | Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 310 | Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S); |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 311 | Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
| 312 | SourceLocation OrigEnd); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 313 | Stmt *RewriteBreakStmt(BreakStmt *S); |
| 314 | Stmt *RewriteContinueStmt(ContinueStmt *S); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 315 | void RewriteCastExpr(CStyleCastExpr *CE); |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 316 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 317 | // Block rewriting. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 318 | void RewriteBlocksInFunctionProtoType(QualType funcType, NamedDecl *D); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 319 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 320 | // Block specific rewrite rules. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 321 | void RewriteBlockPointerDecl(NamedDecl *VD); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 322 | void RewriteByRefVar(VarDecl *VD); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 323 | Stmt *RewriteBlockDeclRefExpr(DeclRefExpr *VD); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 324 | Stmt *RewriteLocalVariableExternalStorage(DeclRefExpr *DRE); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 325 | void RewriteBlockPointerFunctionArgs(FunctionDecl *FD); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 326 | |
| 327 | void RewriteObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
| 328 | std::string &Result); |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 329 | |
| 330 | virtual void Initialize(ASTContext &context) = 0; |
| 331 | |
| 332 | // Metadata Rewriting. |
| 333 | virtual void RewriteMetaDataIntoBuffer(std::string &Result) = 0; |
| 334 | virtual void RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Prots, |
| 335 | StringRef prefix, |
| 336 | StringRef ClassName, |
| 337 | std::string &Result) = 0; |
| 338 | virtual void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
| 339 | std::string &Result) = 0; |
| 340 | virtual void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol, |
| 341 | StringRef prefix, |
| 342 | StringRef ClassName, |
| 343 | std::string &Result) = 0; |
| 344 | virtual void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
| 345 | std::string &Result) = 0; |
| 346 | |
| 347 | // Rewriting ivar access |
| 348 | virtual Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV) = 0; |
| 349 | virtual void RewriteIvarOffsetComputation(ObjCIvarDecl *ivar, |
| 350 | std::string &Result) = 0; |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 351 | |
| 352 | // Misc. AST transformation routines. Somtimes they end up calling |
| 353 | // rewriting routines on the new ASTs. |
| 354 | CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD, |
| 355 | Expr **args, unsigned nargs, |
| 356 | SourceLocation StartLoc=SourceLocation(), |
| 357 | SourceLocation EndLoc=SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 358 | |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 359 | Stmt *SynthMessageExpr(ObjCMessageExpr *Exp, |
| 360 | SourceLocation StartLoc=SourceLocation(), |
| 361 | SourceLocation EndLoc=SourceLocation()); |
| 362 | |
| 363 | void SynthCountByEnumWithState(std::string &buf); |
| 364 | void SynthMsgSendFunctionDecl(); |
| 365 | void SynthMsgSendSuperFunctionDecl(); |
| 366 | void SynthMsgSendStretFunctionDecl(); |
| 367 | void SynthMsgSendFpretFunctionDecl(); |
| 368 | void SynthMsgSendSuperStretFunctionDecl(); |
| 369 | void SynthGetClassFunctionDecl(); |
| 370 | void SynthGetMetaClassFunctionDecl(); |
| 371 | void SynthGetSuperClassFunctionDecl(); |
| 372 | void SynthSelGetUidFunctionDecl(); |
| 373 | void SynthSuperContructorFunctionDecl(); |
| 374 | |
| 375 | std::string SynthesizeByrefCopyDestroyHelper(VarDecl *VD, int flag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 376 | std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 377 | StringRef funcName, std::string Tag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 378 | std::string SynthesizeBlockFunc(BlockExpr *CE, int i, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 379 | StringRef funcName, std::string Tag); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 380 | std::string SynthesizeBlockImpl(BlockExpr *CE, |
| 381 | std::string Tag, std::string Desc); |
| 382 | std::string SynthesizeBlockDescriptor(std::string DescTag, |
| 383 | std::string ImplTag, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 384 | int i, StringRef funcName, |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 385 | unsigned hasCopy); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 386 | Stmt *SynthesizeBlockCall(CallExpr *Exp, const Expr* BlockExp); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 387 | void SynthesizeBlockLiterals(SourceLocation FunLocStart, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 388 | StringRef FunName); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 389 | FunctionDecl *SynthBlockInitFunctionDecl(StringRef name); |
| 390 | Stmt *SynthBlockInitExpr(BlockExpr *Exp, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 391 | const SmallVector<DeclRefExpr *, 8> &InnerBlockDeclRefs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 392 | |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 393 | // Misc. helper routines. |
Fariborz Jahanian | dd8079c | 2011-12-05 19:50:04 +0000 | [diff] [blame] | 394 | QualType getProtocolType(); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 395 | void WarnAboutReturnGotoStmts(Stmt *S); |
| 396 | void HasReturnStmts(Stmt *S, bool &hasReturns); |
| 397 | void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND); |
| 398 | void InsertBlockLiteralsWithinFunction(FunctionDecl *FD); |
| 399 | void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD); |
| 400 | |
| 401 | bool IsDeclStmtInForeachHeader(DeclStmt *DS); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 402 | void CollectBlockDeclRefInfo(BlockExpr *Exp); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 403 | void GetBlockDeclRefExprs(Stmt *S); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 404 | void GetInnerBlockDeclRefExprs(Stmt *S, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 405 | SmallVector<DeclRefExpr *, 8> &InnerBlockDeclRefs, |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 406 | llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 407 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 408 | // We avoid calling Type::isBlockPointerType(), since it operates on the |
| 409 | // 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] | 410 | bool isTopLevelBlockPointerType(QualType T) { |
| 411 | return isa<BlockPointerType>(T); |
| 412 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 413 | |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 414 | /// convertBlockPointerToFunctionPointer - Converts a block-pointer type |
| 415 | /// to a function pointer type and upon success, returns true; false |
| 416 | /// otherwise. |
| 417 | bool convertBlockPointerToFunctionPointer(QualType &T) { |
| 418 | if (isTopLevelBlockPointerType(T)) { |
| 419 | const BlockPointerType *BPT = T->getAs<BlockPointerType>(); |
| 420 | T = Context->getPointerType(BPT->getPointeeType()); |
| 421 | return true; |
| 422 | } |
| 423 | return false; |
| 424 | } |
| 425 | |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 426 | bool needToScanForQualifiers(QualType T); |
| 427 | QualType getSuperStructType(); |
| 428 | QualType getConstantStringStructType(); |
| 429 | QualType convertFunctionTypeOfBlocks(const FunctionType *FT); |
| 430 | bool BufferContainsPPDirectives(const char *startBuf, const char *endBuf); |
| 431 | |
Fariborz Jahanian | 8188e5f | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 432 | void convertToUnqualifiedObjCType(QualType &T) { |
| 433 | if (T->isObjCQualifiedIdType()) |
| 434 | T = Context->getObjCIdType(); |
| 435 | else if (T->isObjCQualifiedClassType()) |
| 436 | T = Context->getObjCClassType(); |
| 437 | else if (T->isObjCObjectPointerType() && |
Fariborz Jahanian | 3a448fb | 2011-09-10 17:01:56 +0000 | [diff] [blame] | 438 | T->getPointeeType()->isObjCQualifiedInterfaceType()) { |
| 439 | if (const ObjCObjectPointerType * OBJPT = |
| 440 | T->getAsObjCInterfacePointerType()) { |
| 441 | const ObjCInterfaceType *IFaceT = OBJPT->getInterfaceType(); |
| 442 | T = QualType(IFaceT, 0); |
| 443 | T = Context->getPointerType(T); |
| 444 | } |
| 445 | } |
Fariborz Jahanian | 8188e5f | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 448 | // FIXME: This predicate seems like it would be useful to add to ASTContext. |
| 449 | bool isObjCType(QualType T) { |
| 450 | if (!LangOpts.ObjC1 && !LangOpts.ObjC2) |
| 451 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 452 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 453 | QualType OCT = Context->getCanonicalType(T).getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 454 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 455 | if (OCT == Context->getCanonicalType(Context->getObjCIdType()) || |
| 456 | OCT == Context->getCanonicalType(Context->getObjCClassType())) |
| 457 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 458 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 459 | if (const PointerType *PT = OCT->getAs<PointerType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 460 | if (isa<ObjCInterfaceType>(PT->getPointeeType()) || |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 461 | PT->getPointeeType()->isObjCQualifiedIdType()) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 462 | return true; |
| 463 | } |
| 464 | return false; |
| 465 | } |
| 466 | bool PointerTypeTakesAnyBlockArguments(QualType QT); |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 467 | bool PointerTypeTakesAnyObjCQualifiedType(QualType QT); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 468 | void GetExtentOfArgList(const char *Name, const char *&LParen, |
| 469 | const char *&RParen); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 470 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 471 | void QuoteDoublequotes(std::string &From, std::string &To) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 472 | for (unsigned i = 0; i < From.length(); i++) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 473 | if (From[i] == '"') |
| 474 | To += "\\\""; |
| 475 | else |
| 476 | To += From[i]; |
| 477 | } |
| 478 | } |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 479 | |
| 480 | QualType getSimpleFunctionType(QualType result, |
| 481 | const QualType *args, |
| 482 | unsigned numArgs, |
| 483 | bool variadic = false) { |
Fariborz Jahanian | 8891480 | 2011-09-09 20:35:22 +0000 | [diff] [blame] | 484 | if (result == Context->getObjCInstanceType()) |
| 485 | result = Context->getObjCIdType(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 486 | FunctionProtoType::ExtProtoInfo fpi; |
| 487 | fpi.Variadic = variadic; |
| 488 | return Context->getFunctionType(result, args, numArgs, fpi); |
| 489 | } |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 490 | |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 491 | // Helper function: create a CStyleCastExpr with trivial type source info. |
| 492 | CStyleCastExpr* NoTypeInfoCStyleCastExpr(ASTContext *Ctx, QualType Ty, |
| 493 | CastKind Kind, Expr *E) { |
| 494 | TypeSourceInfo *TInfo = Ctx->getTrivialTypeSourceInfo(Ty, SourceLocation()); |
| 495 | return CStyleCastExpr::Create(*Ctx, Ty, VK_RValue, Kind, E, 0, TInfo, |
| 496 | SourceLocation(), SourceLocation()); |
| 497 | } |
| 498 | }; |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 499 | |
| 500 | class RewriteObjCFragileABI : public RewriteObjC { |
| 501 | public: |
| 502 | |
| 503 | RewriteObjCFragileABI(std::string inFile, raw_ostream *OS, |
| 504 | DiagnosticsEngine &D, const LangOptions &LOpts, |
| 505 | bool silenceMacroWarn) : RewriteObjC(inFile, OS, |
| 506 | D, LOpts, |
| 507 | silenceMacroWarn) {} |
| 508 | |
| 509 | ~RewriteObjCFragileABI() {} |
| 510 | virtual void Initialize(ASTContext &context); |
| 511 | |
| 512 | // Rewriting metadata |
| 513 | template<typename MethodIterator> |
| 514 | void RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 515 | MethodIterator MethodEnd, |
| 516 | bool IsInstanceMethod, |
| 517 | StringRef prefix, |
| 518 | StringRef ClassName, |
| 519 | std::string &Result); |
| 520 | virtual void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol, |
| 521 | StringRef prefix, |
| 522 | StringRef ClassName, |
| 523 | std::string &Result); |
| 524 | virtual void RewriteObjCProtocolListMetaData( |
| 525 | const ObjCList<ObjCProtocolDecl> &Prots, |
| 526 | StringRef prefix, StringRef ClassName, std::string &Result); |
| 527 | virtual void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
| 528 | std::string &Result); |
| 529 | virtual void RewriteMetaDataIntoBuffer(std::string &Result); |
| 530 | virtual void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
| 531 | std::string &Result); |
| 532 | |
| 533 | // Rewriting ivar |
| 534 | virtual void RewriteIvarOffsetComputation(ObjCIvarDecl *ivar, |
| 535 | std::string &Result); |
| 536 | virtual Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV); |
| 537 | }; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 540 | void RewriteObjC::RewriteBlocksInFunctionProtoType(QualType funcType, |
| 541 | NamedDecl *D) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 542 | if (const FunctionProtoType *fproto |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 543 | = dyn_cast<FunctionProtoType>(funcType.IgnoreParens())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 544 | for (FunctionProtoType::arg_type_iterator I = fproto->arg_type_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 545 | E = fproto->arg_type_end(); I && (I != E); ++I) |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 546 | if (isTopLevelBlockPointerType(*I)) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 547 | // All the args are checked/rewritten. Don't call twice! |
| 548 | RewriteBlockPointerDecl(D); |
| 549 | break; |
| 550 | } |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | void RewriteObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 555 | const PointerType *PT = funcType->getAs<PointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 556 | if (PT && PointerTypeTakesAnyBlockArguments(funcType)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 557 | RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 560 | static bool IsHeaderFile(const std::string &Filename) { |
| 561 | std::string::size_type DotPos = Filename.rfind('.'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 562 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 563 | if (DotPos == std::string::npos) { |
| 564 | // no file extension |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 565 | return false; |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 566 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 567 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 568 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 569 | // C header: .h |
| 570 | // C++ header: .hh or .H; |
| 571 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 572 | } |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 573 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 574 | RewriteObjC::RewriteObjC(std::string inFile, raw_ostream* OS, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 575 | DiagnosticsEngine &D, const LangOptions &LOpts, |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 576 | bool silenceMacroWarn) |
| 577 | : Diags(D), LangOpts(LOpts), InFileName(inFile), OutFile(OS), |
| 578 | SilenceRewriteMacroWarning(silenceMacroWarn) { |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 579 | IsHeader = IsHeaderFile(inFile); |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 580 | RewriteFailedDiag = Diags.getCustomDiagID(DiagnosticsEngine::Warning, |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 581 | "rewriting sub-expression within a macro (may not be correct)"); |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 582 | TryFinallyContainsReturnDiag = Diags.getCustomDiagID( |
| 583 | DiagnosticsEngine::Warning, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 584 | "rewriter doesn't support user-specified control flow semantics " |
| 585 | "for @try/@finally (code may not execute properly)"); |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Eli Friedman | bce831b | 2009-05-18 22:29:17 +0000 | [diff] [blame] | 588 | ASTConsumer *clang::CreateObjCRewriter(const std::string& InFile, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 589 | raw_ostream* OS, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 590 | DiagnosticsEngine &Diags, |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 591 | const LangOptions &LOpts, |
| 592 | bool SilenceRewriteMacroWarning) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 593 | return new RewriteObjCFragileABI(InFile, OS, Diags, LOpts, SilenceRewriteMacroWarning); |
Chris Lattner | e365c50 | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 594 | } |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 595 | |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 596 | void RewriteObjC::InitializeCommon(ASTContext &context) { |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 597 | Context = &context; |
| 598 | SM = &Context->getSourceManager(); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 599 | TUDecl = Context->getTranslationUnitDecl(); |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 600 | MsgSendFunctionDecl = 0; |
| 601 | MsgSendSuperFunctionDecl = 0; |
| 602 | MsgSendStretFunctionDecl = 0; |
| 603 | MsgSendSuperStretFunctionDecl = 0; |
| 604 | MsgSendFpretFunctionDecl = 0; |
| 605 | GetClassFunctionDecl = 0; |
| 606 | GetMetaClassFunctionDecl = 0; |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 607 | GetSuperClassFunctionDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 608 | SelGetUidFunctionDecl = 0; |
| 609 | CFStringFunctionDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 610 | ConstantStringClassReference = 0; |
| 611 | NSStringRecord = 0; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 612 | CurMethodDef = 0; |
| 613 | CurFunctionDef = 0; |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 614 | CurFunctionDeclToDeclareForBlock = 0; |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 615 | GlobalVarDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 616 | SuperStructDecl = 0; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 617 | ProtocolTypeDecl = 0; |
Steve Naroff | 9630ec5 | 2008-03-27 22:59:54 +0000 | [diff] [blame] | 618 | ConstantStringDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 619 | BcLabelCount = 0; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 620 | SuperContructorFunctionDecl = 0; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 621 | NumObjCStringLiterals = 0; |
Steve Naroff | 68272b8 | 2008-12-08 20:01:41 +0000 | [diff] [blame] | 622 | PropParentMap = 0; |
| 623 | CurrentBody = 0; |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 624 | DisableReplaceStmt = false; |
Fariborz Jahanian | f292fcf | 2010-01-07 22:51:18 +0000 | [diff] [blame] | 625 | objc_impl_method = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 626 | |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 627 | // Get the ID and start/end of the main file. |
| 628 | MainFileID = SM->getMainFileID(); |
| 629 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 630 | MainFileStart = MainBuf->getBufferStart(); |
| 631 | MainFileEnd = MainBuf->getBufferEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 632 | |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 633 | Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOptions()); |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 636 | //===----------------------------------------------------------------------===// |
| 637 | // Top Level Driver Code |
| 638 | //===----------------------------------------------------------------------===// |
| 639 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 640 | void RewriteObjC::HandleTopLevelSingleDecl(Decl *D) { |
Ted Kremenek | e50187a | 2010-02-05 21:28:51 +0000 | [diff] [blame] | 641 | if (Diags.hasErrorOccurred()) |
| 642 | return; |
| 643 | |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 644 | // Two cases: either the decl could be in the main file, or it could be in a |
| 645 | // #included file. If the former, rewrite it now. If the later, check to see |
| 646 | // if we rewrote the #include/#import. |
| 647 | SourceLocation Loc = D->getLocation(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 648 | Loc = SM->getExpansionLoc(Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 649 | |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 650 | // If this is for a builtin, ignore it. |
| 651 | if (Loc.isInvalid()) return; |
| 652 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 653 | // Look for built-in declarations that we need to refer during the rewrite. |
| 654 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 655 | RewriteFunctionDecl(FD); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 656 | } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) { |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 657 | // declared in <Foundation/NSString.h> |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 658 | if (FVD->getName() == "_NSConstantStringClassReference") { |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 659 | ConstantStringClassReference = FVD; |
| 660 | return; |
| 661 | } |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 662 | } else if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) { |
| 663 | if (ID->isThisDeclarationADefinition()) |
| 664 | RewriteInterfaceDecl(ID); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 665 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 666 | RewriteCategoryDecl(CD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 667 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 668 | if (PD->isThisDeclarationADefinition()) |
| 669 | RewriteProtocolDecl(PD); |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 670 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
| 671 | // Recurse into linkage specifications |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 672 | for (DeclContext::decl_iterator DI = LSD->decls_begin(), |
| 673 | DIEnd = LSD->decls_end(); |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 674 | DI != DIEnd; ) { |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 675 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>((*DI))) { |
| 676 | if (!IFace->isThisDeclarationADefinition()) { |
| 677 | SmallVector<Decl *, 8> DG; |
| 678 | SourceLocation StartLoc = IFace->getLocStart(); |
| 679 | do { |
| 680 | if (isa<ObjCInterfaceDecl>(*DI) && |
| 681 | !cast<ObjCInterfaceDecl>(*DI)->isThisDeclarationADefinition() && |
| 682 | StartLoc == (*DI)->getLocStart()) |
| 683 | DG.push_back(*DI); |
| 684 | else |
| 685 | break; |
| 686 | |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 687 | ++DI; |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 688 | } while (DI != DIEnd); |
| 689 | RewriteForwardClassDecl(DG); |
| 690 | continue; |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 691 | } |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 692 | } |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 693 | |
| 694 | if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>((*DI))) { |
| 695 | if (!Proto->isThisDeclarationADefinition()) { |
| 696 | SmallVector<Decl *, 8> DG; |
| 697 | SourceLocation StartLoc = Proto->getLocStart(); |
| 698 | do { |
| 699 | if (isa<ObjCProtocolDecl>(*DI) && |
| 700 | !cast<ObjCProtocolDecl>(*DI)->isThisDeclarationADefinition() && |
| 701 | StartLoc == (*DI)->getLocStart()) |
| 702 | DG.push_back(*DI); |
| 703 | else |
| 704 | break; |
| 705 | |
| 706 | ++DI; |
| 707 | } while (DI != DIEnd); |
| 708 | RewriteForwardProtocolDecl(DG); |
| 709 | continue; |
| 710 | } |
| 711 | } |
| 712 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 713 | HandleTopLevelSingleDecl(*DI); |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 714 | ++DI; |
| 715 | } |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 716 | } |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 717 | // 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] | 718 | if (SM->isFromMainFile(Loc)) |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 719 | return HandleDeclInMainFile(D); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 722 | //===----------------------------------------------------------------------===// |
| 723 | // Syntactic (non-AST) Rewriting Code |
| 724 | //===----------------------------------------------------------------------===// |
| 725 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 726 | void RewriteObjC::RewriteInclude() { |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 727 | SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 728 | StringRef MainBuf = SM->getBufferData(MainFileID); |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 729 | const char *MainBufStart = MainBuf.begin(); |
| 730 | const char *MainBufEnd = MainBuf.end(); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 731 | size_t ImportLen = strlen("import"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 732 | |
Fariborz Jahanian | af57b46 | 2008-01-19 01:03:17 +0000 | [diff] [blame] | 733 | // Loop over the whole file, looking for includes. |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 734 | for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { |
| 735 | if (*BufPtr == '#') { |
| 736 | if (++BufPtr == MainBufEnd) |
| 737 | return; |
| 738 | while (*BufPtr == ' ' || *BufPtr == '\t') |
| 739 | if (++BufPtr == MainBufEnd) |
| 740 | return; |
| 741 | if (!strncmp(BufPtr, "import", ImportLen)) { |
| 742 | // replace import with include |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 743 | SourceLocation ImportLoc = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 744 | LocStart.getLocWithOffset(BufPtr-MainBufStart); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 745 | ReplaceText(ImportLoc, ImportLen, "include"); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 746 | BufPtr += ImportLen; |
| 747 | } |
| 748 | } |
| 749 | } |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 752 | static std::string getIvarAccessString(ObjCIvarDecl *OID) { |
| 753 | const ObjCInterfaceDecl *ClassDecl = OID->getContainingInterface(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 754 | std::string S; |
| 755 | S = "((struct "; |
| 756 | S += ClassDecl->getIdentifier()->getName(); |
| 757 | S += "_IMPL *)self)->"; |
Daniel Dunbar | 5ffe14c | 2009-10-18 20:26:27 +0000 | [diff] [blame] | 758 | S += OID->getName(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 759 | return S; |
| 760 | } |
| 761 | |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 762 | void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 763 | ObjCImplementationDecl *IMD, |
| 764 | ObjCCategoryImplDecl *CID) { |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 765 | static bool objcGetPropertyDefined = false; |
| 766 | static bool objcSetPropertyDefined = false; |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 767 | SourceLocation startLoc = PID->getLocStart(); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 768 | InsertText(startLoc, "// "); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 769 | const char *startBuf = SM->getCharacterData(startLoc); |
| 770 | assert((*startBuf == '@') && "bogus @synthesize location"); |
| 771 | const char *semiBuf = strchr(startBuf, ';'); |
| 772 | assert((*semiBuf == ';') && "@synthesize: can't find ';'"); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 773 | SourceLocation onePastSemiLoc = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 774 | startLoc.getLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 775 | |
| 776 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 777 | return; // FIXME: is this correct? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 778 | |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 779 | // Generate the 'getter' function. |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 780 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 781 | ObjCIvarDecl *OID = PID->getPropertyIvarDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 782 | |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 783 | if (!OID) |
| 784 | return; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 785 | unsigned Attributes = PD->getPropertyAttributes(); |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 786 | if (!PD->getGetterMethodDecl()->isDefined()) { |
| 787 | bool GenGetProperty = !(Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) && |
| 788 | (Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
| 789 | ObjCPropertyDecl::OBJC_PR_copy)); |
| 790 | std::string Getr; |
| 791 | if (GenGetProperty && !objcGetPropertyDefined) { |
| 792 | objcGetPropertyDefined = true; |
| 793 | // FIXME. Is this attribute correct in all cases? |
| 794 | Getr = "\nextern \"C\" __declspec(dllimport) " |
| 795 | "id objc_getProperty(id, SEL, long, bool);\n"; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 796 | } |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 797 | RewriteObjCMethodDecl(OID->getContainingInterface(), |
| 798 | PD->getGetterMethodDecl(), Getr); |
| 799 | Getr += "{ "; |
| 800 | // Synthesize an explicit cast to gain access to the ivar. |
| 801 | // See objc-act.c:objc_synthesize_new_getter() for details. |
| 802 | if (GenGetProperty) { |
| 803 | // return objc_getProperty(self, _cmd, offsetof(ClassDecl, OID), 1) |
| 804 | Getr += "typedef "; |
| 805 | const FunctionType *FPRetType = 0; |
| 806 | RewriteTypeIntoString(PD->getGetterMethodDecl()->getResultType(), Getr, |
| 807 | FPRetType); |
| 808 | Getr += " _TYPE"; |
| 809 | if (FPRetType) { |
| 810 | Getr += ")"; // close the precedence "scope" for "*". |
| 811 | |
| 812 | // Now, emit the argument types (if any). |
| 813 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)){ |
| 814 | Getr += "("; |
| 815 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 816 | if (i) Getr += ", "; |
| 817 | std::string ParamStr = FT->getArgType(i).getAsString( |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 818 | Context->getPrintingPolicy()); |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 819 | Getr += ParamStr; |
| 820 | } |
| 821 | if (FT->isVariadic()) { |
| 822 | if (FT->getNumArgs()) Getr += ", "; |
| 823 | Getr += "..."; |
| 824 | } |
| 825 | Getr += ")"; |
| 826 | } else |
| 827 | Getr += "()"; |
| 828 | } |
| 829 | Getr += ";\n"; |
| 830 | Getr += "return (_TYPE)"; |
| 831 | Getr += "objc_getProperty(self, _cmd, "; |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 832 | RewriteIvarOffsetComputation(OID, Getr); |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 833 | Getr += ", 1)"; |
| 834 | } |
| 835 | else |
| 836 | Getr += "return " + getIvarAccessString(OID); |
| 837 | Getr += "; }"; |
| 838 | InsertText(onePastSemiLoc, Getr); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 839 | } |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 840 | |
| 841 | if (PD->isReadOnly() || PD->getSetterMethodDecl()->isDefined()) |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 842 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 843 | |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 844 | // Generate the 'setter' function. |
| 845 | std::string Setr; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 846 | bool GenSetProperty = Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
| 847 | ObjCPropertyDecl::OBJC_PR_copy); |
| 848 | if (GenSetProperty && !objcSetPropertyDefined) { |
| 849 | objcSetPropertyDefined = true; |
| 850 | // FIXME. Is this attribute correct in all cases? |
| 851 | Setr = "\nextern \"C\" __declspec(dllimport) " |
| 852 | "void objc_setProperty (id, SEL, long, id, bool, bool);\n"; |
| 853 | } |
| 854 | |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 855 | RewriteObjCMethodDecl(OID->getContainingInterface(), |
| 856 | PD->getSetterMethodDecl(), Setr); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 857 | Setr += "{ "; |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 858 | // Synthesize an explicit cast to initialize the ivar. |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 859 | // See objc-act.c:objc_synthesize_new_setter() for details. |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 860 | if (GenSetProperty) { |
| 861 | Setr += "objc_setProperty (self, _cmd, "; |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 862 | RewriteIvarOffsetComputation(OID, Setr); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 863 | Setr += ", (id)"; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 864 | Setr += PD->getName(); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 865 | Setr += ", "; |
| 866 | if (Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 867 | Setr += "0, "; |
| 868 | else |
| 869 | Setr += "1, "; |
| 870 | if (Attributes & ObjCPropertyDecl::OBJC_PR_copy) |
| 871 | Setr += "1)"; |
| 872 | else |
| 873 | Setr += "0)"; |
| 874 | } |
| 875 | else { |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 876 | Setr += getIvarAccessString(OID) + " = "; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 877 | Setr += PD->getName(); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 878 | } |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 879 | Setr += "; }"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 880 | InsertText(onePastSemiLoc, Setr); |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 881 | } |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 882 | |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 883 | static void RewriteOneForwardClassDecl(ObjCInterfaceDecl *ForwardDecl, |
| 884 | std::string &typedefString) { |
| 885 | typedefString += "#ifndef _REWRITER_typedef_"; |
| 886 | typedefString += ForwardDecl->getNameAsString(); |
| 887 | typedefString += "\n"; |
| 888 | typedefString += "#define _REWRITER_typedef_"; |
| 889 | typedefString += ForwardDecl->getNameAsString(); |
| 890 | typedefString += "\n"; |
| 891 | typedefString += "typedef struct objc_object "; |
| 892 | typedefString += ForwardDecl->getNameAsString(); |
| 893 | typedefString += ";\n#endif\n"; |
| 894 | } |
| 895 | |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 896 | void RewriteObjC::RewriteForwardClassEpilogue(ObjCInterfaceDecl *ClassDecl, |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 897 | const std::string &typedefString) { |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 898 | SourceLocation startLoc = ClassDecl->getLocStart(); |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 899 | const char *startBuf = SM->getCharacterData(startLoc); |
| 900 | const char *semiPtr = strchr(startBuf, ';'); |
| 901 | // Replace the @class with typedefs corresponding to the classes. |
| 902 | ReplaceText(startLoc, semiPtr-startBuf+1, typedefString); |
| 903 | } |
| 904 | |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 905 | void RewriteObjC::RewriteForwardClassDecl(DeclGroupRef D) { |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 906 | std::string typedefString; |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 907 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 908 | ObjCInterfaceDecl *ForwardDecl = cast<ObjCInterfaceDecl>(*I); |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 909 | if (I == D.begin()) { |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 910 | // Translate to typedef's that forward reference structs with the same name |
| 911 | // as the class. As a convenience, we include the original declaration |
| 912 | // as a comment. |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 913 | typedefString += "// @class "; |
| 914 | typedefString += ForwardDecl->getNameAsString(); |
Fariborz Jahanian | 91fbd12 | 2010-01-11 22:48:40 +0000 | [diff] [blame] | 915 | typedefString += ";\n"; |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 916 | } |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 917 | RewriteOneForwardClassDecl(ForwardDecl, typedefString); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 918 | } |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 919 | DeclGroupRef::iterator I = D.begin(); |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 920 | RewriteForwardClassEpilogue(cast<ObjCInterfaceDecl>(*I), typedefString); |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 921 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 922 | |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 923 | void RewriteObjC::RewriteForwardClassDecl( |
| 924 | const llvm::SmallVector<Decl*, 8> &D) { |
| 925 | std::string typedefString; |
| 926 | for (unsigned i = 0; i < D.size(); i++) { |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 927 | ObjCInterfaceDecl *ForwardDecl = cast<ObjCInterfaceDecl>(D[i]); |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 928 | if (i == 0) { |
| 929 | typedefString += "// @class "; |
| 930 | typedefString += ForwardDecl->getNameAsString(); |
| 931 | typedefString += ";\n"; |
| 932 | } |
| 933 | RewriteOneForwardClassDecl(ForwardDecl, typedefString); |
| 934 | } |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 935 | RewriteForwardClassEpilogue(cast<ObjCInterfaceDecl>(D[0]), typedefString); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 936 | } |
| 937 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 938 | void RewriteObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) { |
Fariborz Jahanian | d050240 | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 939 | // When method is a synthesized one, such as a getter/setter there is |
| 940 | // nothing to rewrite. |
Fariborz Jahanian | 8891480 | 2011-09-09 20:35:22 +0000 | [diff] [blame] | 941 | if (Method->isImplicit()) |
Fariborz Jahanian | d050240 | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 942 | return; |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 943 | SourceLocation LocStart = Method->getLocStart(); |
| 944 | SourceLocation LocEnd = Method->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 945 | |
Chandler Carruth | 6421162 | 2011-07-25 21:09:52 +0000 | [diff] [blame] | 946 | if (SM->getExpansionLineNumber(LocEnd) > |
| 947 | SM->getExpansionLineNumber(LocStart)) { |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 948 | InsertText(LocStart, "#if 0\n"); |
| 949 | ReplaceText(LocEnd, 1, ";\n#endif\n"); |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 950 | } else { |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 951 | InsertText(LocStart, "// "); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 952 | } |
| 953 | } |
| 954 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 955 | void RewriteObjC::RewriteProperty(ObjCPropertyDecl *prop) { |
Fariborz Jahanian | d050240 | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 956 | SourceLocation Loc = prop->getAtLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 957 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 958 | ReplaceText(Loc, 0, "// "); |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 959 | // FIXME: handle properties that are declared across multiple lines. |
Fariborz Jahanian | 957cf65 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 960 | } |
| 961 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 962 | void RewriteObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 963 | SourceLocation LocStart = CatDecl->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 964 | |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 965 | // FIXME: handle category headers that are declared across multiple lines. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 966 | ReplaceText(LocStart, 0, "// "); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 967 | |
Fariborz Jahanian | 13751e3 | 2010-02-10 01:15:09 +0000 | [diff] [blame] | 968 | for (ObjCCategoryDecl::prop_iterator I = CatDecl->prop_begin(), |
| 969 | E = CatDecl->prop_end(); I != E; ++I) |
| 970 | RewriteProperty(*I); |
| 971 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 972 | for (ObjCCategoryDecl::instmeth_iterator |
| 973 | I = CatDecl->instmeth_begin(), E = CatDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 974 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 975 | RewriteMethodDeclaration(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 976 | for (ObjCCategoryDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 977 | I = CatDecl->classmeth_begin(), E = CatDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 978 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 979 | RewriteMethodDeclaration(*I); |
| 980 | |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 981 | // Lastly, comment out the @end. |
Fariborz Jahanian | 73d1eb0 | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 982 | ReplaceText(CatDecl->getAtEndRange().getBegin(), |
| 983 | strlen("@end"), "/* @end */"); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 984 | } |
| 985 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 986 | void RewriteObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 987 | SourceLocation LocStart = PDecl->getLocStart(); |
Douglas Gregor | 61cc296 | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 988 | assert(PDecl->isThisDeclarationADefinition()); |
| 989 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 990 | // FIXME: handle protocol headers that are declared across multiple lines. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 991 | ReplaceText(LocStart, 0, "// "); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 992 | |
| 993 | for (ObjCProtocolDecl::instmeth_iterator |
| 994 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 995 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 996 | RewriteMethodDeclaration(*I); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 997 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 998 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 999 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1000 | RewriteMethodDeclaration(*I); |
| 1001 | |
Fariborz Jahanian | 07acdf4 | 2010-09-24 18:36:58 +0000 | [diff] [blame] | 1002 | for (ObjCInterfaceDecl::prop_iterator I = PDecl->prop_begin(), |
| 1003 | E = PDecl->prop_end(); I != E; ++I) |
| 1004 | RewriteProperty(*I); |
| 1005 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 1006 | // Lastly, comment out the @end. |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1007 | SourceLocation LocEnd = PDecl->getAtEndRange().getBegin(); |
Fariborz Jahanian | 73d1eb0 | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 1008 | ReplaceText(LocEnd, strlen("@end"), "/* @end */"); |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 1009 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 1010 | // Must comment out @optional/@required |
| 1011 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1012 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 1013 | for (const char *p = startBuf; p < endBuf; p++) { |
| 1014 | if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1015 | SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1016 | ReplaceText(OptionalLoc, strlen("@optional"), "/* @optional */"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1017 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 1018 | } |
| 1019 | else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1020 | SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1021 | ReplaceText(OptionalLoc, strlen("@required"), "/* @required */"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1022 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 1023 | } |
| 1024 | } |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1027 | void RewriteObjC::RewriteForwardProtocolDecl(DeclGroupRef D) { |
| 1028 | SourceLocation LocStart = (*D.begin())->getLocStart(); |
| 1029 | if (LocStart.isInvalid()) |
| 1030 | llvm_unreachable("Invalid SourceLocation"); |
| 1031 | // FIXME: handle forward protocol that are declared across multiple lines. |
| 1032 | ReplaceText(LocStart, 0, "// "); |
| 1033 | } |
| 1034 | |
| 1035 | void |
| 1036 | RewriteObjC::RewriteForwardProtocolDecl(const llvm::SmallVector<Decl*, 8> &DG) { |
| 1037 | SourceLocation LocStart = DG[0]->getLocStart(); |
Steve Naroff | b7fa992 | 2007-11-14 03:37:28 +0000 | [diff] [blame] | 1038 | if (LocStart.isInvalid()) |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1039 | llvm_unreachable("Invalid SourceLocation"); |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 1040 | // FIXME: handle forward protocol that are declared across multiple lines. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1041 | ReplaceText(LocStart, 0, "// "); |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1044 | void RewriteObjC::RewriteTypeIntoString(QualType T, std::string &ResultStr, |
| 1045 | const FunctionType *&FPRetType) { |
| 1046 | if (T->isObjCQualifiedIdType()) |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1047 | ResultStr += "id"; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1048 | else if (T->isFunctionPointerType() || |
| 1049 | T->isBlockPointerType()) { |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1050 | // needs special handling, since pointer-to-functions have special |
| 1051 | // syntax (where a decaration models use). |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1052 | QualType retType = T; |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1053 | QualType PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1054 | if (const PointerType* PT = retType->getAs<PointerType>()) |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1055 | PointeeTy = PT->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1056 | else if (const BlockPointerType *BPT = retType->getAs<BlockPointerType>()) |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1057 | PointeeTy = BPT->getPointeeType(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1058 | if ((FPRetType = PointeeTy->getAs<FunctionType>())) { |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1059 | ResultStr += FPRetType->getResultType().getAsString( |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1060 | Context->getPrintingPolicy()); |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1061 | ResultStr += "(*"; |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1062 | } |
| 1063 | } else |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1064 | ResultStr += T.getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1067 | void RewriteObjC::RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl, |
| 1068 | ObjCMethodDecl *OMD, |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1069 | std::string &ResultStr) { |
| 1070 | //fprintf(stderr,"In RewriteObjCMethodDecl\n"); |
| 1071 | const FunctionType *FPRetType = 0; |
| 1072 | ResultStr += "\nstatic "; |
| 1073 | RewriteTypeIntoString(OMD->getResultType(), ResultStr, FPRetType); |
Fariborz Jahanian | 531a1ea | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 1074 | ResultStr += " "; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1075 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1076 | // Unique method name |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1077 | std::string NameStr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1078 | |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1079 | if (OMD->isInstanceMethod()) |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1080 | NameStr += "_I_"; |
| 1081 | else |
| 1082 | NameStr += "_C_"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1083 | |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1084 | NameStr += IDecl->getNameAsString(); |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1085 | NameStr += "_"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1086 | |
| 1087 | if (ObjCCategoryImplDecl *CID = |
Steve Naroff | 3e0a540 | 2009-01-08 19:41:02 +0000 | [diff] [blame] | 1088 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1089 | NameStr += CID->getNameAsString(); |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1090 | NameStr += "_"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1091 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1092 | // Append selector names, replacing ':' with '_' |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1093 | { |
| 1094 | std::string selString = OMD->getSelector().getAsString(); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1095 | int len = selString.size(); |
| 1096 | for (int i = 0; i < len; i++) |
| 1097 | if (selString[i] == ':') |
| 1098 | selString[i] = '_'; |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1099 | NameStr += selString; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1100 | } |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1101 | // Remember this name for metadata emission |
| 1102 | MethodInternalNames[OMD] = NameStr; |
| 1103 | ResultStr += NameStr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1104 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1105 | // Rewrite arguments |
| 1106 | ResultStr += "("; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1107 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1108 | // invisible arguments |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1109 | if (OMD->isInstanceMethod()) { |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1110 | QualType selfTy = Context->getObjCInterfaceType(IDecl); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1111 | selfTy = Context->getPointerType(selfTy); |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 1112 | if (!LangOpts.MicrosoftExt) { |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1113 | if (ObjCSynthesizedStructs.count(const_cast<ObjCInterfaceDecl*>(IDecl))) |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 1114 | ResultStr += "struct "; |
| 1115 | } |
| 1116 | // When rewriting for Microsoft, explicitly omit the structure name. |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1117 | ResultStr += IDecl->getNameAsString(); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1118 | ResultStr += " *"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1119 | } |
| 1120 | else |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1121 | ResultStr += Context->getObjCClassType().getAsString( |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1122 | Context->getPrintingPolicy()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1123 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1124 | ResultStr += " self, "; |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1125 | ResultStr += Context->getObjCSelType().getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1126 | ResultStr += " _cmd"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1127 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1128 | // Method arguments. |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1129 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 1130 | E = OMD->param_end(); PI != E; ++PI) { |
| 1131 | ParmVarDecl *PDecl = *PI; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1132 | ResultStr += ", "; |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1133 | if (PDecl->getType()->isObjCQualifiedIdType()) { |
| 1134 | ResultStr += "id "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1135 | ResultStr += PDecl->getNameAsString(); |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1136 | } else { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1137 | std::string Name = PDecl->getNameAsString(); |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 1138 | QualType QT = PDecl->getType(); |
| 1139 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
| 1140 | if (convertBlockPointerToFunctionPointer(QT)) |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1141 | QT.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 1142 | else |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1143 | PDecl->getType().getAsStringInternal(Name, Context->getPrintingPolicy()); |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1144 | ResultStr += Name; |
| 1145 | } |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1146 | } |
Fariborz Jahanian | 7c39ff7 | 2008-01-21 20:14:23 +0000 | [diff] [blame] | 1147 | if (OMD->isVariadic()) |
| 1148 | ResultStr += ", ..."; |
Fariborz Jahanian | 531a1ea | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 1149 | ResultStr += ") "; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1150 | |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1151 | if (FPRetType) { |
| 1152 | ResultStr += ")"; // close the precedence "scope" for "*". |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1153 | |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1154 | // Now, emit the argument types (if any). |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1155 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) { |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1156 | ResultStr += "("; |
| 1157 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 1158 | if (i) ResultStr += ", "; |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1159 | std::string ParamStr = FT->getArgType(i).getAsString( |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1160 | Context->getPrintingPolicy()); |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1161 | ResultStr += ParamStr; |
| 1162 | } |
| 1163 | if (FT->isVariadic()) { |
| 1164 | if (FT->getNumArgs()) ResultStr += ", "; |
| 1165 | ResultStr += "..."; |
| 1166 | } |
| 1167 | ResultStr += ")"; |
| 1168 | } else { |
| 1169 | ResultStr += "()"; |
| 1170 | } |
| 1171 | } |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1172 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1173 | void RewriteObjC::RewriteImplementationDecl(Decl *OID) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1174 | ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID); |
| 1175 | ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1176 | |
Fariborz Jahanian | a135216 | 2010-02-15 21:11:41 +0000 | [diff] [blame] | 1177 | InsertText(IMD ? IMD->getLocStart() : CID->getLocStart(), "// "); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1178 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1179 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1180 | I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(), |
| 1181 | E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1182 | I != E; ++I) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1183 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1184 | ObjCMethodDecl *OMD = *I; |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1185 | RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1186 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1187 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1188 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1189 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1190 | const char *endBuf = SM->getCharacterData(LocEnd); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1191 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1192 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1193 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1194 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1195 | I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(), |
| 1196 | E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1197 | I != E; ++I) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1198 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1199 | ObjCMethodDecl *OMD = *I; |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1200 | RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1201 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1202 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1203 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1204 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1205 | const char *endBuf = SM->getCharacterData(LocEnd); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1206 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1207 | } |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1208 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1209 | I = IMD ? IMD->propimpl_begin() : CID->propimpl_begin(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1210 | E = IMD ? IMD->propimpl_end() : CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1211 | I != E; ++I) { |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 1212 | RewritePropertyImplDecl(*I, IMD, CID); |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1215 | InsertText(IMD ? IMD->getLocEnd() : CID->getLocEnd(), "// "); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1218 | void RewriteObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { |
Steve Naroff | f908a87 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 1219 | std::string ResultStr; |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1220 | if (!ObjCForwardDecls.count(ClassDecl->getCanonicalDecl())) { |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1221 | // we haven't seen a forward decl - generate a typedef. |
Steve Naroff | 5086a8d | 2007-11-14 23:02:56 +0000 | [diff] [blame] | 1222 | ResultStr = "#ifndef _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1223 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1224 | ResultStr += "\n"; |
| 1225 | ResultStr += "#define _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1226 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1227 | ResultStr += "\n"; |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1228 | ResultStr += "typedef struct objc_object "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1229 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1230 | ResultStr += ";\n#endif\n"; |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1231 | // Mark this typedef as having been generated. |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1232 | ObjCForwardDecls.insert(ClassDecl->getCanonicalDecl()); |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1233 | } |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 1234 | RewriteObjCInternalStruct(ClassDecl, ResultStr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1235 | |
| 1236 | for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1237 | E = ClassDecl->prop_end(); I != E; ++I) |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 1238 | RewriteProperty(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1239 | for (ObjCInterfaceDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1240 | I = ClassDecl->instmeth_begin(), E = ClassDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1241 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1242 | RewriteMethodDeclaration(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1243 | for (ObjCInterfaceDecl::classmeth_iterator |
| 1244 | I = ClassDecl->classmeth_begin(), E = ClassDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1245 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1246 | RewriteMethodDeclaration(*I); |
| 1247 | |
Steve Naroff | 2feac5e | 2007-10-30 03:43:13 +0000 | [diff] [blame] | 1248 | // Lastly, comment out the @end. |
Fariborz Jahanian | 73d1eb0 | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 1249 | ReplaceText(ClassDecl->getAtEndRange().getBegin(), strlen("@end"), |
| 1250 | "/* @end */"); |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1253 | Stmt *RewriteObjC::RewritePropertyOrImplicitSetter(PseudoObjectExpr *PseudoOp) { |
| 1254 | SourceRange OldRange = PseudoOp->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1255 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1256 | // We just magically know some things about the structure of this |
| 1257 | // expression. |
| 1258 | ObjCMessageExpr *OldMsg = |
| 1259 | cast<ObjCMessageExpr>(PseudoOp->getSemanticExpr( |
| 1260 | PseudoOp->getNumSemanticExprs() - 1)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1261 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1262 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 1263 | // we need to suppress rewriting the sub-statements. |
| 1264 | Expr *Base, *RHS; |
| 1265 | { |
| 1266 | DisableReplaceStmtScope S(*this); |
| 1267 | |
| 1268 | // Rebuild the base expression if we have one. |
| 1269 | Base = 0; |
| 1270 | if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) { |
| 1271 | Base = OldMsg->getInstanceReceiver(); |
| 1272 | Base = cast<OpaqueValueExpr>(Base)->getSourceExpr(); |
| 1273 | Base = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Base)); |
| 1274 | } |
| 1275 | |
| 1276 | // Rebuild the RHS. |
| 1277 | RHS = cast<BinaryOperator>(PseudoOp->getSyntacticForm())->getRHS(); |
| 1278 | RHS = cast<OpaqueValueExpr>(RHS)->getSourceExpr(); |
| 1279 | RHS = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(RHS)); |
| 1280 | } |
| 1281 | |
| 1282 | // TODO: avoid this copy. |
| 1283 | SmallVector<SourceLocation, 1> SelLocs; |
| 1284 | OldMsg->getSelectorLocs(SelLocs); |
| 1285 | |
Chad Rosier | 0774ba7 | 2012-01-06 20:05:14 +0000 | [diff] [blame] | 1286 | ObjCMessageExpr *NewMsg = 0; |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1287 | switch (OldMsg->getReceiverKind()) { |
| 1288 | case ObjCMessageExpr::Class: |
| 1289 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1290 | OldMsg->getValueKind(), |
| 1291 | OldMsg->getLeftLoc(), |
| 1292 | OldMsg->getClassReceiverTypeInfo(), |
| 1293 | OldMsg->getSelector(), |
| 1294 | SelLocs, |
| 1295 | OldMsg->getMethodDecl(), |
| 1296 | RHS, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1297 | OldMsg->getRightLoc(), |
| 1298 | OldMsg->isImplicit()); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1299 | break; |
| 1300 | |
| 1301 | case ObjCMessageExpr::Instance: |
| 1302 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1303 | OldMsg->getValueKind(), |
| 1304 | OldMsg->getLeftLoc(), |
| 1305 | Base, |
| 1306 | OldMsg->getSelector(), |
| 1307 | SelLocs, |
| 1308 | OldMsg->getMethodDecl(), |
| 1309 | RHS, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1310 | OldMsg->getRightLoc(), |
| 1311 | OldMsg->isImplicit()); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1312 | break; |
| 1313 | |
| 1314 | case ObjCMessageExpr::SuperClass: |
| 1315 | case ObjCMessageExpr::SuperInstance: |
| 1316 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1317 | OldMsg->getValueKind(), |
| 1318 | OldMsg->getLeftLoc(), |
| 1319 | OldMsg->getSuperLoc(), |
| 1320 | OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance, |
| 1321 | OldMsg->getSuperType(), |
| 1322 | OldMsg->getSelector(), |
| 1323 | SelLocs, |
| 1324 | OldMsg->getMethodDecl(), |
| 1325 | RHS, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1326 | OldMsg->getRightLoc(), |
| 1327 | OldMsg->isImplicit()); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1328 | break; |
| 1329 | } |
| 1330 | |
| 1331 | Stmt *Replacement = SynthMessageExpr(NewMsg); |
| 1332 | ReplaceStmtWithRange(PseudoOp, Replacement, OldRange); |
| 1333 | return Replacement; |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1336 | Stmt *RewriteObjC::RewritePropertyOrImplicitGetter(PseudoObjectExpr *PseudoOp) { |
| 1337 | SourceRange OldRange = PseudoOp->getSourceRange(); |
| 1338 | |
| 1339 | // We just magically know some things about the structure of this |
| 1340 | // expression. |
| 1341 | ObjCMessageExpr *OldMsg = |
| 1342 | cast<ObjCMessageExpr>(PseudoOp->getResultExpr()->IgnoreImplicit()); |
| 1343 | |
| 1344 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 1345 | // we need to suppress rewriting the sub-statements. |
| 1346 | Expr *Base = 0; |
| 1347 | { |
| 1348 | DisableReplaceStmtScope S(*this); |
| 1349 | |
| 1350 | // Rebuild the base expression if we have one. |
| 1351 | if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) { |
| 1352 | Base = OldMsg->getInstanceReceiver(); |
| 1353 | Base = cast<OpaqueValueExpr>(Base)->getSourceExpr(); |
| 1354 | Base = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Base)); |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 1355 | } |
Fariborz Jahanian | e0f8386 | 2010-10-20 16:07:20 +0000 | [diff] [blame] | 1356 | } |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1357 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1358 | // Intentionally empty. |
| 1359 | SmallVector<SourceLocation, 1> SelLocs; |
| 1360 | SmallVector<Expr*, 1> Args; |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1361 | |
Chad Rosier | 0774ba7 | 2012-01-06 20:05:14 +0000 | [diff] [blame] | 1362 | ObjCMessageExpr *NewMsg = 0; |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1363 | switch (OldMsg->getReceiverKind()) { |
| 1364 | case ObjCMessageExpr::Class: |
| 1365 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1366 | OldMsg->getValueKind(), |
| 1367 | OldMsg->getLeftLoc(), |
| 1368 | OldMsg->getClassReceiverTypeInfo(), |
| 1369 | OldMsg->getSelector(), |
| 1370 | SelLocs, |
| 1371 | OldMsg->getMethodDecl(), |
| 1372 | Args, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1373 | OldMsg->getRightLoc(), |
| 1374 | OldMsg->isImplicit()); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1375 | break; |
| 1376 | |
| 1377 | case ObjCMessageExpr::Instance: |
| 1378 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1379 | OldMsg->getValueKind(), |
| 1380 | OldMsg->getLeftLoc(), |
| 1381 | Base, |
| 1382 | OldMsg->getSelector(), |
| 1383 | SelLocs, |
| 1384 | OldMsg->getMethodDecl(), |
| 1385 | Args, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1386 | OldMsg->getRightLoc(), |
| 1387 | OldMsg->isImplicit()); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1388 | break; |
| 1389 | |
| 1390 | case ObjCMessageExpr::SuperClass: |
| 1391 | case ObjCMessageExpr::SuperInstance: |
| 1392 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1393 | OldMsg->getValueKind(), |
| 1394 | OldMsg->getLeftLoc(), |
| 1395 | OldMsg->getSuperLoc(), |
| 1396 | OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance, |
| 1397 | OldMsg->getSuperType(), |
| 1398 | OldMsg->getSelector(), |
| 1399 | SelLocs, |
| 1400 | OldMsg->getMethodDecl(), |
| 1401 | Args, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1402 | OldMsg->getRightLoc(), |
| 1403 | OldMsg->isImplicit()); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1404 | break; |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1405 | } |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1406 | |
| 1407 | Stmt *Replacement = SynthMessageExpr(NewMsg); |
| 1408 | ReplaceStmtWithRange(PseudoOp, Replacement, OldRange); |
| 1409 | return Replacement; |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1412 | /// SynthCountByEnumWithState - To print: |
| 1413 | /// ((unsigned int (*) |
| 1414 | /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1415 | /// (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1416 | /// sel_registerName( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1417 | /// "countByEnumeratingWithState:objects:count:"), |
| 1418 | /// &enumState, |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1419 | /// (id *)__rw_items, (unsigned int)16) |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1420 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1421 | void RewriteObjC::SynthCountByEnumWithState(std::string &buf) { |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1422 | buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, " |
| 1423 | "id *, unsigned int))(void *)objc_msgSend)"; |
| 1424 | buf += "\n\t\t"; |
| 1425 | buf += "((id)l_collection,\n\t\t"; |
| 1426 | buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; |
| 1427 | buf += "\n\t\t"; |
| 1428 | buf += "&enumState, " |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1429 | "(id *)__rw_items, (unsigned int)16)"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1430 | } |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1431 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1432 | /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach |
| 1433 | /// statement to exit to its outer synthesized loop. |
| 1434 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1435 | Stmt *RewriteObjC::RewriteBreakStmt(BreakStmt *S) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1436 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1437 | return S; |
| 1438 | // replace break with goto __break_label |
| 1439 | std::string buf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1440 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1441 | SourceLocation startLoc = S->getLocStart(); |
| 1442 | buf = "goto __break_label_"; |
| 1443 | buf += utostr(ObjCBcLabelNo.back()); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1444 | ReplaceText(startLoc, strlen("break"), buf); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1445 | |
| 1446 | return 0; |
| 1447 | } |
| 1448 | |
| 1449 | /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach |
| 1450 | /// statement to continue with its inner synthesized loop. |
| 1451 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1452 | Stmt *RewriteObjC::RewriteContinueStmt(ContinueStmt *S) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1453 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1454 | return S; |
| 1455 | // replace continue with goto __continue_label |
| 1456 | std::string buf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1457 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1458 | SourceLocation startLoc = S->getLocStart(); |
| 1459 | buf = "goto __continue_label_"; |
| 1460 | buf += utostr(ObjCBcLabelNo.back()); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1461 | ReplaceText(startLoc, strlen("continue"), buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1462 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1463 | return 0; |
| 1464 | } |
| 1465 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1466 | /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1467 | /// It rewrites: |
| 1468 | /// for ( type elem in collection) { stmts; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1469 | |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1470 | /// Into: |
| 1471 | /// { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1472 | /// type elem; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1473 | /// struct __objcFastEnumerationState enumState = { 0 }; |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1474 | /// id __rw_items[16]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1475 | /// id l_collection = (id)collection; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1476 | /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1477 | /// objects:__rw_items count:16]; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1478 | /// if (limit) { |
| 1479 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1480 | /// do { |
| 1481 | /// unsigned long counter = 0; |
| 1482 | /// do { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1483 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1484 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1485 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1486 | /// stmts; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1487 | /// __continue_label: ; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1488 | /// } while (counter < limit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1489 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1490 | /// objects:__rw_items count:16]); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1491 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1492 | /// __break_label: ; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1493 | /// } |
| 1494 | /// else |
| 1495 | /// elem = nil; |
| 1496 | /// } |
| 1497 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1498 | Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 1499 | SourceLocation OrigEnd) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1500 | assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1501 | assert(isa<ObjCForCollectionStmt>(Stmts.back()) && |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1502 | "ObjCForCollectionStmt Statement stack mismatch"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1503 | assert(!ObjCBcLabelNo.empty() && |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1504 | "ObjCForCollectionStmt - Label No stack empty"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1505 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1506 | SourceLocation startLoc = S->getLocStart(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1507 | const char *startBuf = SM->getCharacterData(startLoc); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1508 | StringRef elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1509 | std::string elementTypeAsString; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1510 | std::string buf; |
| 1511 | buf = "\n{\n\t"; |
| 1512 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { |
| 1513 | // type elem; |
Chris Lattner | 7e24e82 | 2009-03-28 06:33:19 +0000 | [diff] [blame] | 1514 | NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl()); |
Ted Kremenek | 1ed8e2a | 2008-10-06 22:16:13 +0000 | [diff] [blame] | 1515 | QualType ElementType = cast<ValueDecl>(D)->getType(); |
Steve Naroff | e89b8e7 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1516 | if (ElementType->isObjCQualifiedIdType() || |
| 1517 | ElementType->isObjCQualifiedInterfaceType()) |
| 1518 | // Simply use 'id' for all qualified types. |
| 1519 | elementTypeAsString = "id"; |
| 1520 | else |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1521 | elementTypeAsString = ElementType.getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1522 | buf += elementTypeAsString; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1523 | buf += " "; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 1524 | elementName = D->getName(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1525 | buf += elementName; |
| 1526 | buf += ";\n\t"; |
| 1527 | } |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1528 | else { |
| 1529 | DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 1530 | elementName = DR->getDecl()->getName(); |
Steve Naroff | e89b8e7 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1531 | ValueDecl *VD = cast<ValueDecl>(DR->getDecl()); |
| 1532 | if (VD->getType()->isObjCQualifiedIdType() || |
| 1533 | VD->getType()->isObjCQualifiedInterfaceType()) |
| 1534 | // Simply use 'id' for all qualified types. |
| 1535 | elementTypeAsString = "id"; |
| 1536 | else |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1537 | elementTypeAsString = VD->getType().getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1538 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1539 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1540 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1541 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1542 | // id __rw_items[16]; |
| 1543 | buf += "id __rw_items[16];\n\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1544 | // id l_collection = (id) |
| 1545 | buf += "id l_collection = (id)"; |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1546 | // Find start location of 'collection' the hard way! |
| 1547 | const char *startCollectionBuf = startBuf; |
| 1548 | startCollectionBuf += 3; // skip 'for' |
| 1549 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1550 | startCollectionBuf++; // skip '(' |
| 1551 | // find 'in' and skip it. |
| 1552 | while (*startCollectionBuf != ' ' || |
| 1553 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1554 | (*(startCollectionBuf+3) != ' ' && |
| 1555 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1556 | startCollectionBuf++; |
| 1557 | startCollectionBuf += 3; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1558 | |
| 1559 | // Replace: "for (type element in" with string constructed thus far. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1560 | ReplaceText(startLoc, startCollectionBuf - startBuf, buf); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1561 | // Replace ')' in for '(' type elem in collection ')' with ';' |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1562 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1563 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1564 | SourceLocation lparenLoc = startLoc.getLocWithOffset(rparenBuf-startBuf); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1565 | buf = ";\n\t"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1566 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1567 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1568 | // objects:__rw_items count:16]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1569 | // which is synthesized into: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1570 | // unsigned int limit = |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1571 | // ((unsigned int (*) |
| 1572 | // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1573 | // (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1574 | // sel_registerName( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1575 | // "countByEnumeratingWithState:objects:count:"), |
| 1576 | // (struct __objcFastEnumerationState *)&state, |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1577 | // (id *)__rw_items, (unsigned int)16); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1578 | buf += "unsigned long limit =\n\t\t"; |
| 1579 | SynthCountByEnumWithState(buf); |
| 1580 | buf += ";\n\t"; |
| 1581 | /// if (limit) { |
| 1582 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1583 | /// do { |
| 1584 | /// unsigned long counter = 0; |
| 1585 | /// do { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1586 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1587 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1588 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1589 | buf += "if (limit) {\n\t"; |
| 1590 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1591 | buf += "do {\n\t\t"; |
| 1592 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1593 | buf += "do {\n\t\t\t"; |
| 1594 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1595 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1596 | buf += elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1597 | buf += " = ("; |
| 1598 | buf += elementTypeAsString; |
| 1599 | buf += ")enumState.itemsPtr[counter++];"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1600 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1601 | ReplaceText(lparenLoc, 1, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1602 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1603 | /// __continue_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1604 | /// } while (counter < limit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1605 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1606 | /// objects:__rw_items count:16]); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1607 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1608 | /// __break_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1609 | /// } |
| 1610 | /// else |
| 1611 | /// elem = nil; |
| 1612 | /// } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1613 | /// |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1614 | buf = ";\n\t"; |
| 1615 | buf += "__continue_label_"; |
| 1616 | buf += utostr(ObjCBcLabelNo.back()); |
| 1617 | buf += ": ;"; |
| 1618 | buf += "\n\t\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1619 | buf += "} while (counter < limit);\n\t"; |
| 1620 | buf += "} while (limit = "; |
| 1621 | SynthCountByEnumWithState(buf); |
| 1622 | buf += ");\n\t"; |
| 1623 | buf += elementName; |
Fariborz Jahanian | 65b0aa5 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1624 | buf += " = (("; |
| 1625 | buf += elementTypeAsString; |
| 1626 | buf += ")0);\n\t"; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1627 | buf += "__break_label_"; |
| 1628 | buf += utostr(ObjCBcLabelNo.back()); |
| 1629 | buf += ": ;\n\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1630 | buf += "}\n\t"; |
| 1631 | buf += "else\n\t\t"; |
| 1632 | buf += elementName; |
Fariborz Jahanian | 65b0aa5 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1633 | buf += " = (("; |
| 1634 | buf += elementTypeAsString; |
| 1635 | buf += ")0);\n\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1636 | buf += "}\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1637 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1638 | // Insert all these *after* the statement body. |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1639 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1640 | if (isa<CompoundStmt>(S->getBody())) { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1641 | SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1642 | InsertText(endBodyLoc, buf); |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1643 | } else { |
| 1644 | /* Need to treat single statements specially. For example: |
| 1645 | * |
| 1646 | * for (A *a in b) if (stuff()) break; |
| 1647 | * for (A *a in b) xxxyy; |
| 1648 | * |
| 1649 | * The following code simply scans ahead to the semi to find the actual end. |
| 1650 | */ |
| 1651 | const char *stmtBuf = SM->getCharacterData(OrigEnd); |
| 1652 | const char *semiBuf = strchr(stmtBuf, ';'); |
| 1653 | assert(semiBuf && "Can't find ';'"); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1654 | SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(semiBuf-stmtBuf+1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1655 | InsertText(endBodyLoc, buf); |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1656 | } |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1657 | Stmts.pop_back(); |
| 1658 | ObjCBcLabelNo.pop_back(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1659 | return 0; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1662 | /// RewriteObjCSynchronizedStmt - |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1663 | /// This routine rewrites @synchronized(expr) stmt; |
| 1664 | /// into: |
| 1665 | /// objc_sync_enter(expr); |
| 1666 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1667 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1668 | Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1669 | // Get the start location and compute the semi location. |
| 1670 | SourceLocation startLoc = S->getLocStart(); |
| 1671 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1672 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1673 | assert((*startBuf == '@') && "bogus @synchronized location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1674 | |
| 1675 | std::string buf; |
Steve Naroff | 3498cc9 | 2008-08-21 13:03:03 +0000 | [diff] [blame] | 1676 | buf = "objc_sync_enter((id)"; |
| 1677 | const char *lparenBuf = startBuf; |
| 1678 | while (*lparenBuf != '(') lparenBuf++; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1679 | ReplaceText(startLoc, lparenBuf-startBuf+1, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1680 | // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since |
| 1681 | // the sync expression is typically a message expression that's already |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1682 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1683 | SourceLocation endLoc = S->getSynchBody()->getLocStart(); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1684 | const char *endBuf = SM->getCharacterData(endLoc); |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1685 | while (*endBuf != ')') endBuf--; |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1686 | SourceLocation rparenLoc = startLoc.getLocWithOffset(endBuf-startBuf); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1687 | buf = ");\n"; |
| 1688 | // declare a new scope with two variables, _stack and _rethrow. |
| 1689 | buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n"; |
| 1690 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1691 | buf += "char *pointers[4];} _stack;\n"; |
| 1692 | buf += "id volatile _rethrow = 0;\n"; |
| 1693 | buf += "objc_exception_try_enter(&_stack);\n"; |
| 1694 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1695 | ReplaceText(rparenLoc, 1, buf); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1696 | startLoc = S->getSynchBody()->getLocEnd(); |
| 1697 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1698 | |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1699 | assert((*startBuf == '}') && "bogus @synchronized block"); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1700 | SourceLocation lastCurlyLoc = startLoc; |
| 1701 | buf = "}\nelse {\n"; |
| 1702 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 1703 | buf += "}\n"; |
| 1704 | buf += "{ /* implicit finally clause */\n"; |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1705 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1706 | |
| 1707 | std::string syncBuf; |
| 1708 | syncBuf += " objc_sync_exit("; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 1709 | |
| 1710 | Expr *syncExpr = S->getSynchExpr(); |
| 1711 | CastKind CK = syncExpr->getType()->isObjCObjectPointerType() |
| 1712 | ? CK_BitCast : |
| 1713 | syncExpr->getType()->isBlockPointerType() |
| 1714 | ? CK_BlockPointerToObjCPointerCast |
| 1715 | : CK_CPointerToObjCPointerCast; |
| 1716 | syncExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 1717 | CK, syncExpr); |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1718 | std::string syncExprBufS; |
| 1719 | llvm::raw_string_ostream syncExprBuf(syncExprBufS); |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1720 | syncExpr->printPretty(syncExprBuf, *Context, 0, |
| 1721 | PrintingPolicy(LangOpts)); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1722 | syncBuf += syncExprBuf.str(); |
| 1723 | syncBuf += ");"; |
| 1724 | |
| 1725 | buf += syncBuf; |
| 1726 | buf += "\n if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1727 | buf += "}\n"; |
| 1728 | buf += "}"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1729 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1730 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1731 | |
| 1732 | bool hasReturns = false; |
| 1733 | HasReturnStmts(S->getSynchBody(), hasReturns); |
| 1734 | if (hasReturns) |
| 1735 | RewriteSyncReturnStmts(S->getSynchBody(), syncBuf); |
| 1736 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1737 | return 0; |
| 1738 | } |
| 1739 | |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1740 | void RewriteObjC::WarnAboutReturnGotoStmts(Stmt *S) |
| 1741 | { |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1742 | // Perform a bottom up traversal of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1743 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1744 | if (*CI) |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1745 | WarnAboutReturnGotoStmts(*CI); |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1746 | |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1747 | if (isa<ReturnStmt>(S) || isa<GotoStmt>(S)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1748 | Diags.Report(Context->getFullLoc(S->getLocStart()), |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1749 | TryFinallyContainsReturnDiag); |
| 1750 | } |
| 1751 | return; |
| 1752 | } |
| 1753 | |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1754 | void RewriteObjC::HasReturnStmts(Stmt *S, bool &hasReturns) |
| 1755 | { |
| 1756 | // Perform a bottom up traversal of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1757 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1758 | if (*CI) |
| 1759 | HasReturnStmts(*CI, hasReturns); |
| 1760 | |
| 1761 | if (isa<ReturnStmt>(S)) |
| 1762 | hasReturns = true; |
| 1763 | return; |
| 1764 | } |
| 1765 | |
| 1766 | void RewriteObjC::RewriteTryReturnStmts(Stmt *S) { |
| 1767 | // Perform a bottom up traversal of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1768 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1769 | if (*CI) { |
| 1770 | RewriteTryReturnStmts(*CI); |
| 1771 | } |
| 1772 | if (isa<ReturnStmt>(S)) { |
| 1773 | SourceLocation startLoc = S->getLocStart(); |
| 1774 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1775 | |
| 1776 | const char *semiBuf = strchr(startBuf, ';'); |
| 1777 | assert((*semiBuf == ';') && "RewriteTryReturnStmts: can't find ';'"); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1778 | SourceLocation onePastSemiLoc = startLoc.getLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1779 | |
| 1780 | std::string buf; |
| 1781 | buf = "{ objc_exception_try_exit(&_stack); return"; |
| 1782 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1783 | ReplaceText(startLoc, 6, buf); |
| 1784 | InsertText(onePastSemiLoc, "}"); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1785 | } |
| 1786 | return; |
| 1787 | } |
| 1788 | |
| 1789 | void RewriteObjC::RewriteSyncReturnStmts(Stmt *S, std::string syncExitBuf) { |
| 1790 | // Perform a bottom up traversal of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1791 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1792 | if (*CI) { |
| 1793 | RewriteSyncReturnStmts(*CI, syncExitBuf); |
| 1794 | } |
| 1795 | if (isa<ReturnStmt>(S)) { |
| 1796 | SourceLocation startLoc = S->getLocStart(); |
| 1797 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1798 | |
| 1799 | const char *semiBuf = strchr(startBuf, ';'); |
| 1800 | assert((*semiBuf == ';') && "RewriteSyncReturnStmts: can't find ';'"); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1801 | SourceLocation onePastSemiLoc = startLoc.getLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1802 | |
| 1803 | std::string buf; |
| 1804 | buf = "{ objc_exception_try_exit(&_stack);"; |
| 1805 | buf += syncExitBuf; |
| 1806 | buf += " return"; |
| 1807 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1808 | ReplaceText(startLoc, 6, buf); |
| 1809 | InsertText(onePastSemiLoc, "}"); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1810 | } |
| 1811 | return; |
| 1812 | } |
| 1813 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1814 | Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1815 | // Get the start location and compute the semi location. |
| 1816 | SourceLocation startLoc = S->getLocStart(); |
| 1817 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1818 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1819 | assert((*startBuf == '@') && "bogus @try location"); |
| 1820 | |
| 1821 | std::string buf; |
| 1822 | // declare a new scope with two variables, _stack and _rethrow. |
| 1823 | buf = "/* @try scope begin */ { struct _objc_exception_data {\n"; |
| 1824 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1825 | buf += "char *pointers[4];} _stack;\n"; |
| 1826 | buf += "id volatile _rethrow = 0;\n"; |
| 1827 | buf += "objc_exception_try_enter(&_stack);\n"; |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1828 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1829 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1830 | ReplaceText(startLoc, 4, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1831 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1832 | startLoc = S->getTryBody()->getLocEnd(); |
| 1833 | startBuf = SM->getCharacterData(startLoc); |
| 1834 | |
| 1835 | assert((*startBuf == '}') && "bogus @try block"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1836 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1837 | SourceLocation lastCurlyLoc = startLoc; |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1838 | if (S->getNumCatchStmts()) { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1839 | startLoc = startLoc.getLocWithOffset(1); |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1840 | buf = " /* @catch begin */ else {\n"; |
| 1841 | buf += " id _caught = objc_exception_extract(&_stack);\n"; |
| 1842 | buf += " objc_exception_try_enter (&_stack);\n"; |
| 1843 | buf += " if (_setjmp(_stack.buf))\n"; |
| 1844 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1845 | buf += " else { /* @catch continue */"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1846 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1847 | InsertText(startLoc, buf); |
Steve Naroff | 8bd3dc6 | 2008-09-09 19:59:12 +0000 | [diff] [blame] | 1848 | } else { /* no catch list */ |
| 1849 | buf = "}\nelse {\n"; |
| 1850 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1851 | buf += "}"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1852 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1853 | } |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1854 | Stmt *lastCatchBody = 0; |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1855 | for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I) { |
| 1856 | ObjCAtCatchStmt *Catch = S->getCatchStmt(I); |
Douglas Gregor | c00d8e1 | 2010-04-26 16:46:50 +0000 | [diff] [blame] | 1857 | VarDecl *catchDecl = Catch->getCatchParamDecl(); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1858 | |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1859 | if (I == 0) |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1860 | buf = "if ("; // we are generating code for the first catch clause |
| 1861 | else |
| 1862 | buf = "else if ("; |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1863 | startLoc = Catch->getLocStart(); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1864 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1865 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1866 | assert((*startBuf == '@') && "bogus @catch location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1867 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1868 | const char *lParenLoc = strchr(startBuf, '('); |
| 1869 | |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1870 | if (Catch->hasEllipsis()) { |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1871 | // Now rewrite the body... |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1872 | lastCatchBody = Catch->getCatchBody(); |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1873 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1874 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1875 | assert(*SM->getCharacterData(Catch->getRParenLoc()) == ')' && |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1876 | "bogus @catch paren location"); |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1877 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1878 | |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1879 | buf += "1) { id _tmp = _caught;"; |
Daniel Dunbar | d7407dc | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 1880 | Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, buf); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1881 | } else if (catchDecl) { |
| 1882 | QualType t = catchDecl->getType(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1883 | if (t == Context->getObjCIdType()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1884 | buf += "1) { "; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1885 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf); |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 1886 | } else if (const ObjCObjectPointerType *Ptr = |
| 1887 | t->getAs<ObjCObjectPointerType>()) { |
| 1888 | // Should be a pointer to a class. |
| 1889 | ObjCInterfaceDecl *IDecl = Ptr->getObjectType()->getInterface(); |
| 1890 | if (IDecl) { |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1891 | buf += "objc_exception_match((struct objc_class *)objc_getClass(\""; |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 1892 | buf += IDecl->getNameAsString(); |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1893 | buf += "\"), (struct objc_object *)_caught)) { "; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1894 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1895 | } |
| 1896 | } |
| 1897 | // Now rewrite the body... |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1898 | lastCatchBody = Catch->getCatchBody(); |
| 1899 | SourceLocation rParenLoc = Catch->getRParenLoc(); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1900 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1901 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 1902 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 1903 | assert((*rParenBuf == ')') && "bogus @catch paren location"); |
| 1904 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1905 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1906 | // Here we replace ") {" with "= _caught;" (which initializes and |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1907 | // declares the @catch parameter). |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1908 | ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, " = _caught;"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1909 | } else { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1910 | llvm_unreachable("@catch rewrite bug"); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1911 | } |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1912 | } |
| 1913 | // Complete the catch list... |
| 1914 | if (lastCatchBody) { |
| 1915 | SourceLocation bodyLoc = lastCatchBody->getLocEnd(); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1916 | assert(*SM->getCharacterData(bodyLoc) == '}' && |
| 1917 | "bogus @catch body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1918 | |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1919 | // Insert the last (implicit) else clause *before* the right curly brace. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1920 | bodyLoc = bodyLoc.getLocWithOffset(-1); |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1921 | buf = "} /* last catch end */\n"; |
| 1922 | buf += "else {\n"; |
| 1923 | buf += " _rethrow = _caught;\n"; |
| 1924 | buf += " objc_exception_try_exit(&_stack);\n"; |
| 1925 | buf += "} } /* @catch end */\n"; |
| 1926 | if (!S->getFinallyStmt()) |
| 1927 | buf += "}\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1928 | InsertText(bodyLoc, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1929 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1930 | // Set lastCurlyLoc |
| 1931 | lastCurlyLoc = lastCatchBody->getLocEnd(); |
| 1932 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1933 | if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1934 | startLoc = finalStmt->getLocStart(); |
| 1935 | startBuf = SM->getCharacterData(startLoc); |
| 1936 | assert((*startBuf == '@') && "bogus @finally start"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1937 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1938 | ReplaceText(startLoc, 8, "/* @finally */"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1939 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1940 | Stmt *body = finalStmt->getFinallyBody(); |
| 1941 | SourceLocation startLoc = body->getLocStart(); |
| 1942 | SourceLocation endLoc = body->getLocEnd(); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1943 | assert(*SM->getCharacterData(startLoc) == '{' && |
| 1944 | "bogus @finally body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1945 | assert(*SM->getCharacterData(endLoc) == '}' && |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1946 | "bogus @finally body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1947 | |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1948 | startLoc = startLoc.getLocWithOffset(1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1949 | InsertText(startLoc, " if (!_rethrow) objc_exception_try_exit(&_stack);\n"); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1950 | endLoc = endLoc.getLocWithOffset(-1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1951 | InsertText(endLoc, " if (_rethrow) objc_exception_throw(_rethrow);\n"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1952 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1953 | // Set lastCurlyLoc |
| 1954 | lastCurlyLoc = body->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1955 | |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1956 | // Now check for any return/continue/go statements within the @try. |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1957 | WarnAboutReturnGotoStmts(S->getTryBody()); |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1958 | } else { /* no finally clause - make sure we synthesize an implicit one */ |
| 1959 | buf = "{ /* implicit finally clause */\n"; |
| 1960 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
| 1961 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 1962 | buf += "}"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1963 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1964 | |
| 1965 | // Now check for any return/continue/go statements within the @try. |
| 1966 | // The implicit finally clause won't called if the @try contains any |
| 1967 | // jump statements. |
| 1968 | bool hasReturns = false; |
| 1969 | HasReturnStmts(S->getTryBody(), hasReturns); |
| 1970 | if (hasReturns) |
| 1971 | RewriteTryReturnStmts(S->getTryBody()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1972 | } |
| 1973 | // Now emit the final closing curly brace... |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1974 | lastCurlyLoc = lastCurlyLoc.getLocWithOffset(1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1975 | InsertText(lastCurlyLoc, " } /* @try scope end */\n"); |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1976 | return 0; |
| 1977 | } |
| 1978 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1979 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
| 1980 | // the throw expression is typically a message expression that's already |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1981 | // been rewritten! (which implies the SourceLocation's are invalid). |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1982 | Stmt *RewriteObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1983 | // Get the start location and compute the semi location. |
| 1984 | SourceLocation startLoc = S->getLocStart(); |
| 1985 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1986 | |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1987 | assert((*startBuf == '@') && "bogus @throw location"); |
| 1988 | |
| 1989 | std::string buf; |
| 1990 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
Steve Naroff | 20ebf8f | 2008-01-19 00:42:38 +0000 | [diff] [blame] | 1991 | if (S->getThrowExpr()) |
| 1992 | buf = "objc_exception_throw("; |
| 1993 | else // add an implicit argument |
| 1994 | buf = "objc_exception_throw(_caught"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1995 | |
Steve Naroff | 4ba0acb | 2008-07-25 15:41:30 +0000 | [diff] [blame] | 1996 | // handle "@ throw" correctly. |
| 1997 | const char *wBuf = strchr(startBuf, 'w'); |
| 1998 | assert((*wBuf == 'w') && "@throw: can't find 'w'"); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1999 | ReplaceText(startLoc, wBuf-startBuf+1, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2000 | |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2001 | const char *semiBuf = strchr(startBuf, ';'); |
| 2002 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2003 | SourceLocation semiLoc = startLoc.getLocWithOffset(semiBuf-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2004 | ReplaceText(semiLoc, 1, ");"); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2005 | return 0; |
| 2006 | } |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 2007 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2008 | Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 2009 | // Create a new string expression. |
| 2010 | QualType StrType = Context->getPointerType(Context->CharTy); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2011 | std::string StrEncoding; |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 2012 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2013 | Expr *Replacement = StringLiteral::Create(*Context, StrEncoding, |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2014 | StringLiteral::Ascii, false, |
| 2015 | StrType, SourceLocation()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2016 | ReplaceStmt(Exp, Replacement); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2017 | |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 2018 | // Replace this subexpr in the parent. |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2019 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 2020 | return Replacement; |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 2021 | } |
| 2022 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2023 | Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
Steve Naroff | 1a93764 | 2008-12-22 22:16:07 +0000 | [diff] [blame] | 2024 | if (!SelGetUidFunctionDecl) |
| 2025 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2026 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 2027 | // Create a call to sel_registerName("selName"). |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2028 | SmallVector<Expr*, 8> SelExprs; |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2029 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2030 | SelExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2031 | Exp->getSelector().getAsString(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2032 | StringLiteral::Ascii, false, |
| 2033 | argType, SourceLocation())); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2034 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2035 | &SelExprs[0], SelExprs.size()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2036 | ReplaceStmt(Exp, SelExp); |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2037 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2038 | return SelExp; |
| 2039 | } |
| 2040 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2041 | CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl( |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2042 | FunctionDecl *FD, Expr **args, unsigned nargs, SourceLocation StartLoc, |
| 2043 | SourceLocation EndLoc) { |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2044 | // 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] | 2045 | QualType msgSendType = FD->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2046 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2047 | // Create a reference to the objc_msgSend() declaration. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 2048 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, false, msgSendType, |
| 2049 | VK_LValue, SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2050 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2051 | // 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] | 2052 | QualType pToFunc = Context->getPointerType(msgSendType); |
Anders Carlsson | 88465d3 | 2010-04-23 22:18:37 +0000 | [diff] [blame] | 2053 | ImplicitCastExpr *ICE = |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2054 | ImplicitCastExpr::Create(*Context, pToFunc, CK_FunctionToPointerDecay, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2055 | DRE, 0, VK_RValue); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2056 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2057 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2058 | |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2059 | CallExpr *Exp = |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 2060 | new (Context) CallExpr(*Context, ICE, args, nargs, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2061 | FT->getCallResultType(*Context), |
| 2062 | VK_RValue, EndLoc); |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2063 | return Exp; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2064 | } |
| 2065 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2066 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 2067 | const char *&startRef, const char *&endRef) { |
| 2068 | while (startBuf < endBuf) { |
| 2069 | if (*startBuf == '<') |
| 2070 | startRef = startBuf; // mark the start. |
| 2071 | if (*startBuf == '>') { |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 2072 | if (startRef && *startRef == '<') { |
| 2073 | endRef = startBuf; // mark the end. |
| 2074 | return true; |
| 2075 | } |
| 2076 | return false; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2077 | } |
| 2078 | startBuf++; |
| 2079 | } |
| 2080 | return false; |
| 2081 | } |
| 2082 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2083 | static void scanToNextArgument(const char *&argRef) { |
| 2084 | int angle = 0; |
| 2085 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 2086 | if (*argRef == '<') |
| 2087 | angle++; |
| 2088 | else if (*argRef == '>') |
| 2089 | angle--; |
| 2090 | argRef++; |
| 2091 | } |
| 2092 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 2093 | } |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2094 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2095 | bool RewriteObjC::needToScanForQualifiers(QualType T) { |
Fariborz Jahanian | 32132a0 | 2010-02-03 21:29:28 +0000 | [diff] [blame] | 2096 | if (T->isObjCQualifiedIdType()) |
| 2097 | return true; |
Fariborz Jahanian | 84aa946 | 2010-02-02 18:35:07 +0000 | [diff] [blame] | 2098 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 2099 | if (PT->getPointeeType()->isObjCQualifiedIdType()) |
| 2100 | return true; |
| 2101 | } |
| 2102 | if (T->isObjCObjectPointerType()) { |
| 2103 | T = T->getPointeeType(); |
| 2104 | return T->isObjCQualifiedInterfaceType(); |
| 2105 | } |
Fariborz Jahanian | 24f9cab | 2010-09-30 20:41:32 +0000 | [diff] [blame] | 2106 | if (T->isArrayType()) { |
| 2107 | QualType ElemTy = Context->getBaseElementType(T); |
| 2108 | return needToScanForQualifiers(ElemTy); |
| 2109 | } |
Fariborz Jahanian | 84aa946 | 2010-02-02 18:35:07 +0000 | [diff] [blame] | 2110 | return false; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2111 | } |
| 2112 | |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2113 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) { |
| 2114 | QualType Type = E->getType(); |
| 2115 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2116 | SourceLocation Loc, EndLoc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2117 | |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2118 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) { |
| 2119 | Loc = ECE->getLParenLoc(); |
| 2120 | EndLoc = ECE->getRParenLoc(); |
| 2121 | } else { |
| 2122 | Loc = E->getLocStart(); |
| 2123 | EndLoc = E->getLocEnd(); |
| 2124 | } |
| 2125 | // This will defend against trying to rewrite synthesized expressions. |
| 2126 | if (Loc.isInvalid() || EndLoc.isInvalid()) |
| 2127 | return; |
| 2128 | |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2129 | const char *startBuf = SM->getCharacterData(Loc); |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2130 | const char *endBuf = SM->getCharacterData(EndLoc); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2131 | const char *startRef = 0, *endRef = 0; |
| 2132 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2133 | // Get the locations of the startRef, endRef. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2134 | SourceLocation LessLoc = Loc.getLocWithOffset(startRef-startBuf); |
| 2135 | SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-startBuf+1); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2136 | // Comment out the protocol references. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2137 | InsertText(LessLoc, "/*"); |
| 2138 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2139 | } |
| 2140 | } |
| 2141 | } |
| 2142 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2143 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2144 | SourceLocation Loc; |
| 2145 | QualType Type; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2146 | const FunctionProtoType *proto = 0; |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2147 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 2148 | Loc = VD->getLocation(); |
| 2149 | Type = VD->getType(); |
| 2150 | } |
| 2151 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 2152 | Loc = FD->getLocation(); |
| 2153 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 2154 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2155 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2156 | assert(funcType && "missing function type"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2157 | proto = dyn_cast<FunctionProtoType>(funcType); |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2158 | if (!proto) |
| 2159 | return; |
| 2160 | Type = proto->getResultType(); |
| 2161 | } |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 2162 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(Dcl)) { |
| 2163 | Loc = FD->getLocation(); |
| 2164 | Type = FD->getType(); |
| 2165 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2166 | else |
| 2167 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2168 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2169 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2170 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2171 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2172 | const char *endBuf = SM->getCharacterData(Loc); |
| 2173 | const char *startBuf = endBuf; |
Steve Naroff | 6cafbf2 | 2008-05-31 05:02:17 +0000 | [diff] [blame] | 2174 | while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2175 | startBuf--; // scan backward (from the decl location) for return type. |
| 2176 | const char *startRef = 0, *endRef = 0; |
| 2177 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2178 | // Get the locations of the startRef, endRef. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2179 | SourceLocation LessLoc = Loc.getLocWithOffset(startRef-endBuf); |
| 2180 | SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-endBuf+1); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2181 | // Comment out the protocol references. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2182 | InsertText(LessLoc, "/*"); |
| 2183 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2184 | } |
| 2185 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2186 | if (!proto) |
| 2187 | return; // most likely, was a variable |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2188 | // Now check arguments. |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2189 | const char *startBuf = SM->getCharacterData(Loc); |
| 2190 | const char *startFuncBuf = startBuf; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2191 | for (unsigned i = 0; i < proto->getNumArgs(); i++) { |
| 2192 | if (needToScanForQualifiers(proto->getArgType(i))) { |
| 2193 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2194 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2195 | const char *endBuf = startBuf; |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2196 | // scan forward (from the decl location) for argument types. |
| 2197 | scanToNextArgument(endBuf); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2198 | const char *startRef = 0, *endRef = 0; |
| 2199 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2200 | // Get the locations of the startRef, endRef. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2201 | SourceLocation LessLoc = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2202 | Loc.getLocWithOffset(startRef-startFuncBuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2203 | SourceLocation GreaterLoc = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2204 | Loc.getLocWithOffset(endRef-startFuncBuf+1); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2205 | // Comment out the protocol references. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2206 | InsertText(LessLoc, "/*"); |
| 2207 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2208 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2209 | startBuf = ++endBuf; |
| 2210 | } |
| 2211 | else { |
Steve Naroff | aba49d1 | 2008-08-06 15:58:23 +0000 | [diff] [blame] | 2212 | // If the function name is derived from a macro expansion, then the |
| 2213 | // argument buffer will not follow the name. Need to speak with Chris. |
| 2214 | while (*startBuf && *startBuf != ')' && *startBuf != ',') |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2215 | startBuf++; // scan forward (from the decl location) for argument types. |
| 2216 | startBuf++; |
| 2217 | } |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2218 | } |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2219 | } |
| 2220 | |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2221 | void RewriteObjC::RewriteTypeOfDecl(VarDecl *ND) { |
| 2222 | QualType QT = ND->getType(); |
| 2223 | const Type* TypePtr = QT->getAs<Type>(); |
| 2224 | if (!isa<TypeOfExprType>(TypePtr)) |
| 2225 | return; |
| 2226 | while (isa<TypeOfExprType>(TypePtr)) { |
| 2227 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 2228 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 2229 | TypePtr = QT->getAs<Type>(); |
| 2230 | } |
| 2231 | // FIXME. This will not work for multiple declarators; as in: |
| 2232 | // __typeof__(a) b,c,d; |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2233 | std::string TypeAsString(QT.getAsString(Context->getPrintingPolicy())); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2234 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
| 2235 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 2236 | if (ND->getInit()) { |
| 2237 | std::string Name(ND->getNameAsString()); |
| 2238 | TypeAsString += " " + Name + " = "; |
| 2239 | Expr *E = ND->getInit(); |
| 2240 | SourceLocation startLoc; |
| 2241 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 2242 | startLoc = ECE->getLParenLoc(); |
| 2243 | else |
| 2244 | startLoc = E->getLocStart(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 2245 | startLoc = SM->getExpansionLoc(startLoc); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2246 | const char *endBuf = SM->getCharacterData(startLoc); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2247 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2248 | } |
| 2249 | else { |
| 2250 | SourceLocation X = ND->getLocEnd(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 2251 | X = SM->getExpansionLoc(X); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2252 | const char *endBuf = SM->getCharacterData(X); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2253 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2254 | } |
| 2255 | } |
| 2256 | |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2257 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2258 | void RewriteObjC::SynthSelGetUidFunctionDecl() { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2259 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2260 | SmallVector<QualType, 16> ArgTys; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2261 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2262 | QualType getFuncType = |
| 2263 | getSimpleFunctionType(Context->getObjCSelType(), &ArgTys[0], ArgTys.size()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2264 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2265 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2266 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2267 | SelGetUidIdent, getFuncType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2268 | SC_Extern, |
| 2269 | SC_None, false); |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2270 | } |
| 2271 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2272 | void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2273 | // declared in <objc/objc.h> |
Douglas Gregor | 51efe56 | 2009-01-09 01:47:02 +0000 | [diff] [blame] | 2274 | if (FD->getIdentifier() && |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 2275 | FD->getName() == "sel_registerName") { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2276 | SelGetUidFunctionDecl = FD; |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2277 | return; |
| 2278 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2279 | RewriteObjCQualifiedInterfaceTypes(FD); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2280 | } |
| 2281 | |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2282 | void RewriteObjC::RewriteBlockPointerType(std::string& Str, QualType Type) { |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2283 | std::string TypeString(Type.getAsString(Context->getPrintingPolicy())); |
Fariborz Jahanian | 52b2e1e | 2010-02-12 17:52:31 +0000 | [diff] [blame] | 2284 | const char *argPtr = TypeString.c_str(); |
| 2285 | if (!strchr(argPtr, '^')) { |
| 2286 | Str += TypeString; |
| 2287 | return; |
| 2288 | } |
| 2289 | while (*argPtr) { |
| 2290 | Str += (*argPtr == '^' ? '*' : *argPtr); |
| 2291 | argPtr++; |
| 2292 | } |
| 2293 | } |
| 2294 | |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2295 | // FIXME. Consolidate this routine with RewriteBlockPointerType. |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2296 | void RewriteObjC::RewriteBlockPointerTypeVariable(std::string& Str, |
| 2297 | ValueDecl *VD) { |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2298 | QualType Type = VD->getType(); |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2299 | std::string TypeString(Type.getAsString(Context->getPrintingPolicy())); |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2300 | const char *argPtr = TypeString.c_str(); |
| 2301 | int paren = 0; |
| 2302 | while (*argPtr) { |
| 2303 | switch (*argPtr) { |
| 2304 | case '(': |
| 2305 | Str += *argPtr; |
| 2306 | paren++; |
| 2307 | break; |
| 2308 | case ')': |
| 2309 | Str += *argPtr; |
| 2310 | paren--; |
| 2311 | break; |
| 2312 | case '^': |
| 2313 | Str += '*'; |
| 2314 | if (paren == 1) |
| 2315 | Str += VD->getNameAsString(); |
| 2316 | break; |
| 2317 | default: |
| 2318 | Str += *argPtr; |
| 2319 | break; |
| 2320 | } |
| 2321 | argPtr++; |
| 2322 | } |
| 2323 | } |
| 2324 | |
| 2325 | |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2326 | void RewriteObjC::RewriteBlockLiteralFunctionDecl(FunctionDecl *FD) { |
| 2327 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
| 2328 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
| 2329 | const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(funcType); |
| 2330 | if (!proto) |
| 2331 | return; |
| 2332 | QualType Type = proto->getResultType(); |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2333 | std::string FdStr = Type.getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2334 | FdStr += " "; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 2335 | FdStr += FD->getName(); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2336 | FdStr += "("; |
| 2337 | unsigned numArgs = proto->getNumArgs(); |
| 2338 | for (unsigned i = 0; i < numArgs; i++) { |
| 2339 | QualType ArgType = proto->getArgType(i); |
Fariborz Jahanian | 52b2e1e | 2010-02-12 17:52:31 +0000 | [diff] [blame] | 2340 | RewriteBlockPointerType(FdStr, ArgType); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2341 | if (i+1 < numArgs) |
| 2342 | FdStr += ", "; |
| 2343 | } |
| 2344 | FdStr += ");\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2345 | InsertText(FunLocStart, FdStr); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2346 | CurFunctionDeclToDeclareForBlock = 0; |
| 2347 | } |
| 2348 | |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2349 | // SynthSuperContructorFunctionDecl - id objc_super(id obj, id super); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2350 | void RewriteObjC::SynthSuperContructorFunctionDecl() { |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2351 | if (SuperContructorFunctionDecl) |
| 2352 | return; |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2353 | IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2354 | SmallVector<QualType, 16> ArgTys; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2355 | QualType argT = Context->getObjCIdType(); |
| 2356 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2357 | ArgTys.push_back(argT); |
| 2358 | ArgTys.push_back(argT); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2359 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2360 | &ArgTys[0], ArgTys.size()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2361 | SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2362 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2363 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2364 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2365 | SC_Extern, |
| 2366 | SC_None, false); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2367 | } |
| 2368 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2369 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2370 | void RewriteObjC::SynthMsgSendFunctionDecl() { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2371 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2372 | SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2373 | QualType argT = Context->getObjCIdType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2374 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2375 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2376 | argT = Context->getObjCSelType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2377 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2378 | ArgTys.push_back(argT); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2379 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2380 | &ArgTys[0], ArgTys.size(), |
| 2381 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2382 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2383 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2384 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2385 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2386 | SC_Extern, |
| 2387 | SC_None, false); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2388 | } |
| 2389 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2390 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2391 | void RewriteObjC::SynthMsgSendSuperFunctionDecl() { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2392 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2393 | SmallVector<QualType, 16> ArgTys; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2394 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2395 | SourceLocation(), SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2396 | &Context->Idents.get("objc_super")); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2397 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2398 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2399 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2400 | argT = Context->getObjCSelType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2401 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2402 | ArgTys.push_back(argT); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2403 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2404 | &ArgTys[0], ArgTys.size(), |
| 2405 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2406 | MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2407 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2408 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2409 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2410 | SC_Extern, |
| 2411 | SC_None, false); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2412 | } |
| 2413 | |
Fariborz Jahanian | 336c8f7 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2414 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2415 | void RewriteObjC::SynthMsgSendStretFunctionDecl() { |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2416 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2417 | SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2418 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2419 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2420 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2421 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2422 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2423 | ArgTys.push_back(argT); |
Fariborz Jahanian | 336c8f7 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2424 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2425 | &ArgTys[0], ArgTys.size(), |
| 2426 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2427 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2428 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2429 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2430 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2431 | SC_Extern, |
| 2432 | SC_None, false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2433 | } |
| 2434 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2435 | // SynthMsgSendSuperStretFunctionDecl - |
Fariborz Jahanian | 336c8f7 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2436 | // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2437 | void RewriteObjC::SynthMsgSendSuperStretFunctionDecl() { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2438 | IdentifierInfo *msgSendIdent = |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2439 | &Context->Idents.get("objc_msgSendSuper_stret"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2440 | SmallVector<QualType, 16> ArgTys; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2441 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2442 | SourceLocation(), SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2443 | &Context->Idents.get("objc_super")); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2444 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2445 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2446 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2447 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2448 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2449 | ArgTys.push_back(argT); |
Fariborz Jahanian | 336c8f7 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2450 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2451 | &ArgTys[0], ArgTys.size(), |
| 2452 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2453 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2454 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2455 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2456 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2457 | SC_Extern, |
| 2458 | SC_None, false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2459 | } |
| 2460 | |
Steve Naroff | 1284db8 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2461 | // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2462 | void RewriteObjC::SynthMsgSendFpretFunctionDecl() { |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2463 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2464 | SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2465 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2466 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2467 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2468 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2469 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2470 | ArgTys.push_back(argT); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2471 | QualType msgSendType = getSimpleFunctionType(Context->DoubleTy, |
| 2472 | &ArgTys[0], ArgTys.size(), |
| 2473 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2474 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2475 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2476 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2477 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2478 | SC_Extern, |
| 2479 | SC_None, false); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2482 | // SynthGetClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2483 | void RewriteObjC::SynthGetClassFunctionDecl() { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2484 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2485 | SmallVector<QualType, 16> ArgTys; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2486 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2487 | QualType getClassType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2488 | &ArgTys[0], ArgTys.size()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2489 | GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2490 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2491 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2492 | getClassIdent, getClassType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2493 | SC_Extern, |
| 2494 | SC_None, false); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2495 | } |
| 2496 | |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2497 | // SynthGetSuperClassFunctionDecl - Class class_getSuperclass(Class cls); |
| 2498 | void RewriteObjC::SynthGetSuperClassFunctionDecl() { |
| 2499 | IdentifierInfo *getSuperClassIdent = |
| 2500 | &Context->Idents.get("class_getSuperclass"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2501 | SmallVector<QualType, 16> ArgTys; |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2502 | ArgTys.push_back(Context->getObjCClassType()); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2503 | QualType getClassType = getSimpleFunctionType(Context->getObjCClassType(), |
| 2504 | &ArgTys[0], ArgTys.size()); |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2505 | GetSuperClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2506 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2507 | SourceLocation(), |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2508 | getSuperClassIdent, |
| 2509 | getClassType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2510 | SC_Extern, |
| 2511 | SC_None, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2512 | false); |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2513 | } |
| 2514 | |
Fariborz Jahanian | 97bbab2 | 2011-12-21 19:48:07 +0000 | [diff] [blame] | 2515 | // SynthGetMetaClassFunctionDecl - id objc_getMetaClass(const char *name); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2516 | void RewriteObjC::SynthGetMetaClassFunctionDecl() { |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2517 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2518 | SmallVector<QualType, 16> ArgTys; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2519 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2520 | QualType getClassType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2521 | &ArgTys[0], ArgTys.size()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2522 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2523 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2524 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2525 | getClassIdent, getClassType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2526 | SC_Extern, |
| 2527 | SC_None, false); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2528 | } |
| 2529 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2530 | Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2531 | QualType strType = getConstantStringStructType(); |
| 2532 | |
| 2533 | std::string S = "__NSConstantStringImpl_"; |
Steve Naroff | 7691d9b | 2008-05-31 03:35:42 +0000 | [diff] [blame] | 2534 | |
| 2535 | std::string tmpName = InFileName; |
| 2536 | unsigned i; |
| 2537 | for (i=0; i < tmpName.length(); i++) { |
| 2538 | char c = tmpName.at(i); |
| 2539 | // replace any non alphanumeric characters with '_'. |
| 2540 | if (!isalpha(c) && (c < '0' || c > '9')) |
| 2541 | tmpName[i] = '_'; |
| 2542 | } |
| 2543 | S += tmpName; |
| 2544 | S += "_"; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2545 | S += utostr(NumObjCStringLiterals++); |
| 2546 | |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2547 | Preamble += "static __NSConstantStringImpl " + S; |
| 2548 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 2549 | Preamble += "0x000007c8,"; // utf8_str |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2550 | // The pretty printer for StringLiteral handles escape characters properly. |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2551 | std::string prettyBufS; |
| 2552 | llvm::raw_string_ostream prettyBuf(prettyBufS); |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2553 | Exp->getString()->printPretty(prettyBuf, *Context, 0, |
| 2554 | PrintingPolicy(LangOpts)); |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2555 | Preamble += prettyBuf.str(); |
| 2556 | Preamble += ","; |
Steve Naroff | fd5b76f | 2009-12-06 01:48:44 +0000 | [diff] [blame] | 2557 | Preamble += utostr(Exp->getString()->getByteLength()) + "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2558 | |
| 2559 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2560 | SourceLocation(), &Context->Idents.get(S), |
| 2561 | strType, 0, SC_Static, SC_None); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 2562 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, false, strType, VK_LValue, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2563 | SourceLocation()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2564 | Expr *Unop = new (Context) UnaryOperator(DRE, UO_AddrOf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2565 | Context->getPointerType(DRE->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2566 | VK_RValue, OK_Ordinary, |
| 2567 | SourceLocation()); |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2568 | // cast to NSConstantString * |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2569 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Exp->getType(), |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2570 | CK_CPointerToObjCPointerCast, Unop); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2571 | ReplaceStmt(Exp, cast); |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2572 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2573 | return cast; |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2574 | } |
| 2575 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2576 | // struct objc_super { struct objc_object *receiver; struct objc_class *super; }; |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2577 | QualType RewriteObjC::getSuperStructType() { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2578 | if (!SuperStructDecl) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2579 | SuperStructDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2580 | SourceLocation(), SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2581 | &Context->Idents.get("objc_super")); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2582 | QualType FieldTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2583 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2584 | // struct objc_object *receiver; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2585 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2586 | // struct objc_class *super; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2587 | FieldTypes[1] = Context->getObjCClassType(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2588 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2589 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2590 | for (unsigned i = 0; i < 2; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2591 | SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2592 | SourceLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2593 | SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2594 | FieldTypes[i], 0, |
| 2595 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2596 | /*Mutable=*/false, |
| 2597 | /*HasInit=*/false)); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2598 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2599 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2600 | SuperStructDecl->completeDefinition(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2601 | } |
| 2602 | return Context->getTagDeclType(SuperStructDecl); |
| 2603 | } |
| 2604 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2605 | QualType RewriteObjC::getConstantStringStructType() { |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2606 | if (!ConstantStringDecl) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2607 | ConstantStringDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2608 | SourceLocation(), SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2609 | &Context->Idents.get("__NSConstantStringImpl")); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2610 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2611 | |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2612 | // struct objc_object *receiver; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2613 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2614 | // int flags; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2615 | FieldTypes[1] = Context->IntTy; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2616 | // char *str; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2617 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2618 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2619 | FieldTypes[3] = Context->LongTy; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2620 | |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2621 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2622 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2623 | ConstantStringDecl->addDecl(FieldDecl::Create(*Context, |
| 2624 | ConstantStringDecl, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2625 | SourceLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2626 | SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2627 | FieldTypes[i], 0, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2628 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2629 | /*Mutable=*/true, |
| 2630 | /*HasInit=*/false)); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2631 | } |
| 2632 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2633 | ConstantStringDecl->completeDefinition(); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2634 | } |
| 2635 | return Context->getTagDeclType(ConstantStringDecl); |
| 2636 | } |
| 2637 | |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2638 | Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp, |
| 2639 | SourceLocation StartLoc, |
| 2640 | SourceLocation EndLoc) { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2641 | if (!SelGetUidFunctionDecl) |
| 2642 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2643 | if (!MsgSendFunctionDecl) |
| 2644 | SynthMsgSendFunctionDecl(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2645 | if (!MsgSendSuperFunctionDecl) |
| 2646 | SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2647 | if (!MsgSendStretFunctionDecl) |
| 2648 | SynthMsgSendStretFunctionDecl(); |
| 2649 | if (!MsgSendSuperStretFunctionDecl) |
| 2650 | SynthMsgSendSuperStretFunctionDecl(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2651 | if (!MsgSendFpretFunctionDecl) |
| 2652 | SynthMsgSendFpretFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2653 | if (!GetClassFunctionDecl) |
| 2654 | SynthGetClassFunctionDecl(); |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2655 | if (!GetSuperClassFunctionDecl) |
| 2656 | SynthGetSuperClassFunctionDecl(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2657 | if (!GetMetaClassFunctionDecl) |
| 2658 | SynthGetMetaClassFunctionDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2659 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2660 | // default to objc_msgSend(). |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2661 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2662 | // May need to use objc_msgSend_stret() as well. |
| 2663 | FunctionDecl *MsgSendStretFlavor = 0; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2664 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
| 2665 | QualType resultType = mDecl->getResultType(); |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 2666 | if (resultType->isRecordType()) |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2667 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
Chris Lattner | 8b51fd7 | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2668 | else if (resultType->isRealFloatingType()) |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2669 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2670 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2671 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2672 | // Synthesize a call to objc_msgSend(). |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2673 | SmallVector<Expr*, 8> MsgExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2674 | switch (Exp->getReceiverKind()) { |
| 2675 | case ObjCMessageExpr::SuperClass: { |
| 2676 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2677 | if (MsgSendStretFlavor) |
| 2678 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2679 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2680 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2681 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2682 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2683 | SmallVector<Expr*, 4> InitExprs; |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2684 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2685 | // set the receiver to self, the first argument to all methods. |
| 2686 | InitExprs.push_back( |
| 2687 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2688 | CK_BitCast, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2689 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 2690 | false, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2691 | Context->getObjCIdType(), |
| 2692 | VK_RValue, |
| 2693 | SourceLocation())) |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2694 | ); // set the 'receiver'. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2695 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2696 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2697 | SmallVector<Expr*, 8> ClsExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2698 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2699 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2700 | ClassDecl->getIdentifier()->getName(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2701 | StringLiteral::Ascii, false, |
| 2702 | argType, SourceLocation())); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2703 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
| 2704 | &ClsExprs[0], |
| 2705 | ClsExprs.size(), |
| 2706 | StartLoc, |
| 2707 | EndLoc); |
| 2708 | // (Class)objc_getClass("CurrentClass") |
| 2709 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 2710 | Context->getObjCClassType(), |
Fariborz Jahanian | 97bbab2 | 2011-12-21 19:48:07 +0000 | [diff] [blame] | 2711 | CK_BitCast, Cls); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2712 | ClsExprs.clear(); |
| 2713 | ClsExprs.push_back(ArgExpr); |
| 2714 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 2715 | &ClsExprs[0], ClsExprs.size(), |
| 2716 | StartLoc, EndLoc); |
| 2717 | |
| 2718 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2719 | // To turn off a warning, type-cast to 'id' |
| 2720 | InitExprs.push_back( // set 'super class', using class_getSuperclass(). |
| 2721 | NoTypeInfoCStyleCastExpr(Context, |
| 2722 | Context->getObjCIdType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2723 | CK_BitCast, Cls)); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2724 | // struct objc_super |
| 2725 | QualType superType = getSuperStructType(); |
| 2726 | Expr *SuperRep; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2727 | |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 2728 | if (LangOpts.MicrosoftExt) { |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2729 | SynthSuperContructorFunctionDecl(); |
| 2730 | // Simulate a contructor call... |
| 2731 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 2732 | false, superType, VK_LValue, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2733 | SourceLocation()); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2734 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
| 2735 | InitExprs.size(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2736 | superType, VK_LValue, |
| 2737 | SourceLocation()); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2738 | // The code for super is a little tricky to prevent collision with |
| 2739 | // the structure definition in the header. The rewriter has it's own |
| 2740 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2741 | // we need the cast below. For example: |
| 2742 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2743 | // |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2744 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2745 | Context->getPointerType(SuperRep->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2746 | VK_RValue, OK_Ordinary, |
| 2747 | SourceLocation()); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2748 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2749 | Context->getPointerType(superType), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2750 | CK_BitCast, SuperRep); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2751 | } else { |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2752 | // (struct objc_super) { <exprs from above> } |
| 2753 | InitListExpr *ILE = |
| 2754 | new (Context) InitListExpr(*Context, SourceLocation(), |
| 2755 | &InitExprs[0], InitExprs.size(), |
| 2756 | SourceLocation()); |
| 2757 | TypeSourceInfo *superTInfo |
| 2758 | = Context->getTrivialTypeSourceInfo(superType); |
| 2759 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2760 | superType, VK_LValue, |
| 2761 | ILE, false); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2762 | // struct objc_super * |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2763 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2764 | Context->getPointerType(SuperRep->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2765 | VK_RValue, OK_Ordinary, |
| 2766 | SourceLocation()); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2767 | } |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2768 | MsgExprs.push_back(SuperRep); |
| 2769 | break; |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2770 | } |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2771 | |
| 2772 | case ObjCMessageExpr::Class: { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2773 | SmallVector<Expr*, 8> ClsExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2774 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2775 | ObjCInterfaceDecl *Class |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 2776 | = Exp->getClassReceiver()->getAs<ObjCObjectType>()->getInterface(); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2777 | IdentifierInfo *clsName = Class->getIdentifier(); |
| 2778 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2779 | clsName->getName(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2780 | StringLiteral::Ascii, false, |
Anders Carlsson | 3e2193c | 2011-04-14 00:40:03 +0000 | [diff] [blame] | 2781 | argType, SourceLocation())); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2782 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2783 | &ClsExprs[0], |
| 2784 | ClsExprs.size(), |
| 2785 | StartLoc, EndLoc); |
| 2786 | MsgExprs.push_back(Cls); |
| 2787 | break; |
| 2788 | } |
| 2789 | |
| 2790 | case ObjCMessageExpr::SuperInstance:{ |
| 2791 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2792 | if (MsgSendStretFlavor) |
| 2793 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2794 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 2795 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2796 | SmallVector<Expr*, 4> InitExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2797 | |
| 2798 | InitExprs.push_back( |
| 2799 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2800 | CK_BitCast, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2801 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 2802 | false, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2803 | Context->getObjCIdType(), |
| 2804 | VK_RValue, SourceLocation())) |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2805 | ); // set the 'receiver'. |
| 2806 | |
| 2807 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2808 | SmallVector<Expr*, 8> ClsExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2809 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2810 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2811 | ClassDecl->getIdentifier()->getName(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2812 | StringLiteral::Ascii, false, argType, |
| 2813 | SourceLocation())); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2814 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2815 | &ClsExprs[0], |
| 2816 | ClsExprs.size(), |
| 2817 | StartLoc, EndLoc); |
| 2818 | // (Class)objc_getClass("CurrentClass") |
| 2819 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 2820 | Context->getObjCClassType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2821 | CK_BitCast, Cls); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2822 | ClsExprs.clear(); |
| 2823 | ClsExprs.push_back(ArgExpr); |
| 2824 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 2825 | &ClsExprs[0], ClsExprs.size(), |
| 2826 | StartLoc, EndLoc); |
| 2827 | |
| 2828 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2829 | // To turn off a warning, type-cast to 'id' |
| 2830 | InitExprs.push_back( |
| 2831 | // set 'super class', using class_getSuperclass(). |
| 2832 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2833 | CK_BitCast, Cls)); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2834 | // struct objc_super |
| 2835 | QualType superType = getSuperStructType(); |
| 2836 | Expr *SuperRep; |
| 2837 | |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 2838 | if (LangOpts.MicrosoftExt) { |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2839 | SynthSuperContructorFunctionDecl(); |
| 2840 | // Simulate a contructor call... |
| 2841 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 2842 | false, superType, VK_LValue, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2843 | SourceLocation()); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2844 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
| 2845 | InitExprs.size(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2846 | superType, VK_LValue, SourceLocation()); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2847 | // The code for super is a little tricky to prevent collision with |
| 2848 | // the structure definition in the header. The rewriter has it's own |
| 2849 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2850 | // we need the cast below. For example: |
| 2851 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2852 | // |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2853 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2854 | Context->getPointerType(SuperRep->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2855 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2856 | SourceLocation()); |
| 2857 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2858 | Context->getPointerType(superType), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2859 | CK_BitCast, SuperRep); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2860 | } else { |
| 2861 | // (struct objc_super) { <exprs from above> } |
| 2862 | InitListExpr *ILE = |
| 2863 | new (Context) InitListExpr(*Context, SourceLocation(), |
| 2864 | &InitExprs[0], InitExprs.size(), |
| 2865 | SourceLocation()); |
| 2866 | TypeSourceInfo *superTInfo |
| 2867 | = Context->getTrivialTypeSourceInfo(superType); |
| 2868 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2869 | superType, VK_RValue, ILE, |
| 2870 | false); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2871 | } |
| 2872 | MsgExprs.push_back(SuperRep); |
| 2873 | break; |
| 2874 | } |
| 2875 | |
| 2876 | case ObjCMessageExpr::Instance: { |
| 2877 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 2878 | // Foo<Proto> *. |
| 2879 | Expr *recExpr = Exp->getInstanceReceiver(); |
| 2880 | while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr)) |
| 2881 | recExpr = CE->getSubExpr(); |
Fariborz Jahanian | baac1ea | 2011-10-07 17:17:45 +0000 | [diff] [blame] | 2882 | CastKind CK = recExpr->getType()->isObjCObjectPointerType() |
| 2883 | ? CK_BitCast : recExpr->getType()->isBlockPointerType() |
| 2884 | ? CK_BlockPointerToObjCPointerCast |
| 2885 | : CK_CPointerToObjCPointerCast; |
| 2886 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2887 | recExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
Fariborz Jahanian | baac1ea | 2011-10-07 17:17:45 +0000 | [diff] [blame] | 2888 | CK, recExpr); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2889 | MsgExprs.push_back(recExpr); |
| 2890 | break; |
| 2891 | } |
| 2892 | } |
| 2893 | |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2894 | // 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] | 2895 | SmallVector<Expr*, 8> SelExprs; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2896 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2897 | SelExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2898 | Exp->getSelector().getAsString(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2899 | StringLiteral::Ascii, false, |
| 2900 | argType, SourceLocation())); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2901 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2902 | &SelExprs[0], SelExprs.size(), |
| 2903 | StartLoc, |
| 2904 | EndLoc); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2905 | MsgExprs.push_back(SelExp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2906 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2907 | // Now push any user supplied arguments. |
| 2908 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2909 | Expr *userExpr = Exp->getArg(i); |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2910 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 2911 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 2912 | // Reuse the ICE type, it is exactly what the doctor ordered. |
Fariborz Jahanian | dff3f01 | 2011-02-26 01:31:36 +0000 | [diff] [blame] | 2913 | QualType type = ICE->getType(); |
| 2914 | if (needToScanForQualifiers(type)) |
| 2915 | type = Context->getObjCIdType(); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 2916 | // Make sure we convert "type (^)(...)" to "type (*)(...)". |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 2917 | (void)convertBlockPointerToFunctionPointer(type); |
Fariborz Jahanian | 1a38b46 | 2011-08-04 23:58:03 +0000 | [diff] [blame] | 2918 | const Expr *SubExpr = ICE->IgnoreParenImpCasts(); |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2919 | CastKind CK; |
| 2920 | if (SubExpr->getType()->isIntegralType(*Context) && |
| 2921 | type->isBooleanType()) { |
| 2922 | CK = CK_IntegralToBoolean; |
| 2923 | } else if (type->isObjCObjectPointerType()) { |
| 2924 | if (SubExpr->getType()->isBlockPointerType()) { |
| 2925 | CK = CK_BlockPointerToObjCPointerCast; |
| 2926 | } else if (SubExpr->getType()->isPointerType()) { |
| 2927 | CK = CK_CPointerToObjCPointerCast; |
| 2928 | } else { |
| 2929 | CK = CK_BitCast; |
| 2930 | } |
| 2931 | } else { |
| 2932 | CK = CK_BitCast; |
| 2933 | } |
| 2934 | |
| 2935 | userExpr = NoTypeInfoCStyleCastExpr(Context, type, CK, userExpr); |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2936 | } |
| 2937 | // Make id<P...> cast into an 'id' cast. |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2938 | else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2939 | if (CE->getType()->isObjCQualifiedIdType()) { |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2940 | while ((CE = dyn_cast<CStyleCastExpr>(userExpr))) |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2941 | userExpr = CE->getSubExpr(); |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2942 | CastKind CK; |
| 2943 | if (userExpr->getType()->isIntegralType(*Context)) { |
| 2944 | CK = CK_IntegralToPointer; |
| 2945 | } else if (userExpr->getType()->isBlockPointerType()) { |
| 2946 | CK = CK_BlockPointerToObjCPointerCast; |
| 2947 | } else if (userExpr->getType()->isPointerType()) { |
| 2948 | CK = CK_CPointerToObjCPointerCast; |
| 2949 | } else { |
| 2950 | CK = CK_BitCast; |
| 2951 | } |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2952 | userExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2953 | CK, userExpr); |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2954 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2955 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2956 | MsgExprs.push_back(userExpr); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2957 | // We've transferred the ownership to MsgExprs. For now, we *don't* null |
| 2958 | // out the argument in the original expression (since we aren't deleting |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2959 | // the ObjCMessageExpr). See RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2960 | //Exp->setArg(i, 0); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2961 | } |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2962 | // Generate the funky cast. |
| 2963 | CastExpr *cast; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2964 | SmallVector<QualType, 8> ArgTypes; |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2965 | QualType returnType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2966 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2967 | // Push 'id' and 'SEL', the 2 implicit arguments. |
Steve Naroff | c3a438c | 2007-11-15 10:43:57 +0000 | [diff] [blame] | 2968 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 2969 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 2970 | else |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2971 | ArgTypes.push_back(Context->getObjCIdType()); |
| 2972 | ArgTypes.push_back(Context->getObjCSelType()); |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2973 | if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) { |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2974 | // Push any user argument types. |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2975 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 2976 | E = OMD->param_end(); PI != E; ++PI) { |
| 2977 | QualType t = (*PI)->getType()->isObjCQualifiedIdType() |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2978 | ? Context->getObjCIdType() |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2979 | : (*PI)->getType(); |
Steve Naroff | a206b06 | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 2980 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 2981 | (void)convertBlockPointerToFunctionPointer(t); |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 2982 | ArgTypes.push_back(t); |
| 2983 | } |
Fariborz Jahanian | 3a448fb | 2011-09-10 17:01:56 +0000 | [diff] [blame] | 2984 | returnType = Exp->getType(); |
| 2985 | convertToUnqualifiedObjCType(returnType); |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 2986 | (void)convertBlockPointerToFunctionPointer(returnType); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2987 | } else { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2988 | returnType = Context->getObjCIdType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2989 | } |
| 2990 | // Get the type, we will need to reference it in a couple spots. |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2991 | QualType msgSendType = MsgSendFlavor->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2992 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2993 | // Create a reference to the objc_msgSend() declaration. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 2994 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, false, msgSendType, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2995 | VK_LValue, SourceLocation()); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2996 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2997 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2998 | // If we don't do this cast, we get the following bizarre warning/note: |
| 2999 | // xx.m:13: warning: function called through a non-compatible type |
| 3000 | // xx.m:13: note: if this code is reached, the program will abort |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3001 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 3002 | Context->getPointerType(Context->VoidTy), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3003 | CK_BitCast, DRE); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3004 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3005 | // Now do the "normal" pointer to function cast. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3006 | QualType castType = |
| 3007 | getSimpleFunctionType(returnType, &ArgTypes[0], ArgTypes.size(), |
| 3008 | // If we don't have a method decl, force a variadic cast. |
| 3009 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3010 | castType = Context->getPointerType(castType); |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3011 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3012 | cast); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3013 | |
| 3014 | // Don't forget the parens to enforce the proper binding. |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 3015 | ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3016 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3017 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3018 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3019 | MsgExprs.size(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3020 | FT->getResultType(), VK_RValue, |
| 3021 | EndLoc); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3022 | Stmt *ReplacingStmt = CE; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3023 | if (MsgSendStretFlavor) { |
| 3024 | // We have the method which returns a struct/union. Must also generate |
| 3025 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 3026 | // expression which dictate which one to envoke depending on size of |
| 3027 | // method's return type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3028 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3029 | // Create a reference to the objc_msgSend_stret() declaration. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 3030 | DeclRefExpr *STDRE = new (Context) DeclRefExpr(MsgSendStretFlavor, |
| 3031 | false, msgSendType, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3032 | VK_LValue, SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3033 | // Need to cast objc_msgSend_stret to "void *" (see above comment). |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3034 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 3035 | Context->getPointerType(Context->VoidTy), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3036 | CK_BitCast, STDRE); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3037 | // Now do the "normal" pointer to function cast. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3038 | castType = getSimpleFunctionType(returnType, &ArgTypes[0], ArgTypes.size(), |
| 3039 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3040 | castType = Context->getPointerType(castType); |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3041 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3042 | cast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3043 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3044 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3045 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3046 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3047 | FT = msgSendType->getAs<FunctionType>(); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3048 | CallExpr *STCE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3049 | MsgExprs.size(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3050 | FT->getResultType(), VK_RValue, |
| 3051 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3052 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3053 | // Build sizeof(returnType) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3054 | UnaryExprOrTypeTraitExpr *sizeofExpr = |
| 3055 | new (Context) UnaryExprOrTypeTraitExpr(UETT_SizeOf, |
| 3056 | Context->getTrivialTypeSourceInfo(returnType), |
| 3057 | Context->getSizeType(), SourceLocation(), |
| 3058 | SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3059 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 3060 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 3061 | // For X86 it is more complicated and some kind of target specific routine |
| 3062 | // is needed to decide what to do. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3063 | unsigned IntSize = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3064 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 3065 | IntegerLiteral *limit = IntegerLiteral::Create(*Context, |
| 3066 | llvm::APInt(IntSize, 8), |
| 3067 | Context->IntTy, |
| 3068 | SourceLocation()); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3069 | BinaryOperator *lessThanExpr = |
| 3070 | new (Context) BinaryOperator(sizeofExpr, limit, BO_LE, Context->IntTy, |
| 3071 | VK_RValue, OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3072 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3073 | ConditionalOperator *CondExpr = |
Douglas Gregor | 47e1f7c | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 3074 | new (Context) ConditionalOperator(lessThanExpr, |
| 3075 | SourceLocation(), CE, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3076 | SourceLocation(), STCE, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3077 | returnType, VK_RValue, OK_Ordinary); |
Fariborz Jahanian | f9b949f | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 3078 | ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3079 | CondExpr); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3080 | } |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3081 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3082 | return ReplacingStmt; |
| 3083 | } |
| 3084 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3085 | Stmt *RewriteObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 3086 | Stmt *ReplacingStmt = SynthMessageExpr(Exp, Exp->getLocStart(), |
| 3087 | Exp->getLocEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3088 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 3089 | // Now do the actual rewrite. |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 3090 | ReplaceStmt(Exp, ReplacingStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3091 | |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3092 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3093 | return ReplacingStmt; |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 3094 | } |
| 3095 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3096 | // typedef struct objc_object Protocol; |
| 3097 | QualType RewriteObjC::getProtocolType() { |
| 3098 | if (!ProtocolTypeDecl) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3099 | TypeSourceInfo *TInfo |
| 3100 | = Context->getTrivialTypeSourceInfo(Context->getObjCIdType()); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3101 | ProtocolTypeDecl = TypedefDecl::Create(*Context, TUDecl, |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 3102 | SourceLocation(), SourceLocation(), |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3103 | &Context->Idents.get("Protocol"), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3104 | TInfo); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3105 | } |
| 3106 | return Context->getTypeDeclType(ProtocolTypeDecl); |
| 3107 | } |
| 3108 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 3109 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3110 | /// a synthesized/forward data reference (to the protocol's metadata). |
| 3111 | /// The forward references (and metadata) are generated in |
| 3112 | /// RewriteObjC::HandleTranslationUnit(). |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3113 | Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3114 | std::string Name = "_OBJC_PROTOCOL_" + Exp->getProtocol()->getNameAsString(); |
| 3115 | IdentifierInfo *ID = &Context->Idents.get(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3116 | VarDecl *VD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3117 | SourceLocation(), ID, getProtocolType(), 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3118 | SC_Extern, SC_None); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 3119 | DeclRefExpr *DRE = new (Context) DeclRefExpr(VD, false, getProtocolType(), |
| 3120 | VK_LValue, SourceLocation()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3121 | Expr *DerefExpr = new (Context) UnaryOperator(DRE, UO_AddrOf, |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3122 | Context->getPointerType(DRE->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3123 | VK_RValue, OK_Ordinary, SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3124 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, DerefExpr->getType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3125 | CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3126 | DerefExpr); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3127 | ReplaceStmt(Exp, castExpr); |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 3128 | ProtocolExprDecls.insert(Exp->getProtocol()->getCanonicalDecl()); |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3129 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3130 | return castExpr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3131 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 3132 | } |
| 3133 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3134 | bool RewriteObjC::BufferContainsPPDirectives(const char *startBuf, |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3135 | const char *endBuf) { |
| 3136 | while (startBuf < endBuf) { |
| 3137 | if (*startBuf == '#') { |
| 3138 | // Skip whitespace. |
| 3139 | for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf) |
| 3140 | ; |
| 3141 | if (!strncmp(startBuf, "if", strlen("if")) || |
| 3142 | !strncmp(startBuf, "ifdef", strlen("ifdef")) || |
| 3143 | !strncmp(startBuf, "ifndef", strlen("ifndef")) || |
| 3144 | !strncmp(startBuf, "define", strlen("define")) || |
| 3145 | !strncmp(startBuf, "undef", strlen("undef")) || |
| 3146 | !strncmp(startBuf, "else", strlen("else")) || |
| 3147 | !strncmp(startBuf, "elif", strlen("elif")) || |
| 3148 | !strncmp(startBuf, "endif", strlen("endif")) || |
| 3149 | !strncmp(startBuf, "pragma", strlen("pragma")) || |
| 3150 | !strncmp(startBuf, "include", strlen("include")) || |
| 3151 | !strncmp(startBuf, "import", strlen("import")) || |
| 3152 | !strncmp(startBuf, "include_next", strlen("include_next"))) |
| 3153 | return true; |
| 3154 | } |
| 3155 | startBuf++; |
| 3156 | } |
| 3157 | return false; |
| 3158 | } |
| 3159 | |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 3160 | /// RewriteObjCInternalStruct - Rewrite one internal struct corresponding to |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3161 | /// an objective-c class with ivars. |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 3162 | void RewriteObjC::RewriteObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3163 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3164 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3165 | assert(CDecl->getName() != "" && |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 3166 | "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 3167 | // Do not synthesize more than once. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3168 | if (ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 3169 | return; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3170 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3171 | int NumIvars = CDecl->ivar_size(); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3172 | SourceLocation LocStart = CDecl->getLocStart(); |
Douglas Gregor | 05c272f | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 3173 | SourceLocation LocEnd = CDecl->getEndOfDefinitionLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3174 | |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3175 | const char *startBuf = SM->getCharacterData(LocStart); |
| 3176 | const char *endBuf = SM->getCharacterData(LocEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3177 | |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3178 | // If no ivars and no root or if its root, directly or indirectly, |
| 3179 | // 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] | 3180 | if ((!CDecl->isThisDeclarationADefinition() || NumIvars == 0) && |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3181 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3182 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3183 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3184 | return; |
| 3185 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3186 | |
| 3187 | // FIXME: This has potential of causing problem. If |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3188 | // SynthesizeObjCInternalStruct is ever called recursively. |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3189 | Result += "\nstruct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3190 | Result += CDecl->getNameAsString(); |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 3191 | if (LangOpts.MicrosoftExt) |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 3192 | Result += "_IMPL"; |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 3193 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3194 | if (NumIvars > 0) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3195 | const char *cursor = strchr(startBuf, '{'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3196 | assert((cursor && endBuf) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3197 | && "SynthesizeObjCInternalStruct - malformed @interface"); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3198 | // If the buffer contains preprocessor directives, we do more fine-grained |
| 3199 | // rewrites. This is intended to fix code that looks like (which occurs in |
| 3200 | // NSURL.h, for example): |
| 3201 | // |
| 3202 | // #ifdef XYZ |
| 3203 | // @interface Foo : NSObject |
| 3204 | // #else |
| 3205 | // @interface FooBar : NSObject |
| 3206 | // #endif |
| 3207 | // { |
| 3208 | // int i; |
| 3209 | // } |
| 3210 | // @end |
| 3211 | // |
| 3212 | // This clause is segregated to avoid breaking the common case. |
| 3213 | if (BufferContainsPPDirectives(startBuf, cursor)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3214 | SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() : |
Argyrios Kyrtzidis | 1711fc9 | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 3215 | CDecl->getAtStartLoc(); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3216 | const char *endHeader = SM->getCharacterData(L); |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3217 | endHeader += Lexer::MeasureTokenLength(L, *SM, LangOpts); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3218 | |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3219 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3220 | // advance to the end of the referenced protocols. |
| 3221 | while (endHeader < cursor && *endHeader != '>') endHeader++; |
| 3222 | endHeader++; |
| 3223 | } |
| 3224 | // rewrite the original header |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3225 | ReplaceText(LocStart, endHeader-startBuf, Result); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3226 | } else { |
| 3227 | // rewrite the original header *without* disturbing the '{' |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3228 | ReplaceText(LocStart, cursor-startBuf, Result); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3229 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3230 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3231 | Result = "\n struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3232 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 39bbd9f | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 3233 | Result += "_IMPL "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3234 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 819173c | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 3235 | Result += "_IVARS;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3236 | |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3237 | // insert the super class structure definition. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 3238 | SourceLocation OnePastCurly = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3239 | LocStart.getLocWithOffset(cursor-startBuf+1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3240 | InsertText(OnePastCurly, Result); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3241 | } |
| 3242 | cursor++; // past '{' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3243 | |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3244 | // Now comment out any visibility specifiers. |
| 3245 | while (cursor < endBuf) { |
| 3246 | if (*cursor == '@') { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3247 | SourceLocation atLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Chris Lattner | df6a51b | 2007-11-14 22:57:51 +0000 | [diff] [blame] | 3248 | // Skip whitespace. |
| 3249 | for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor) |
| 3250 | /*scan*/; |
| 3251 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3252 | // FIXME: presence of @public, etc. inside comment results in |
| 3253 | // this transformation as well, which is still correct c-code. |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3254 | if (!strncmp(cursor, "public", strlen("public")) || |
| 3255 | !strncmp(cursor, "private", strlen("private")) || |
Steve Naroff | c5e3277 | 2008-04-04 22:34:24 +0000 | [diff] [blame] | 3256 | !strncmp(cursor, "package", strlen("package")) || |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3257 | !strncmp(cursor, "protected", strlen("protected"))) |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3258 | InsertText(atLoc, "// "); |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3259 | } |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3260 | // FIXME: If there are cases where '<' is used in ivar declaration part |
| 3261 | // of user code, then scan the ivar list and use needToScanForQualifiers |
| 3262 | // for type checking. |
| 3263 | else if (*cursor == '<') { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3264 | SourceLocation atLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3265 | InsertText(atLoc, "/* "); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3266 | cursor = strchr(cursor, '>'); |
| 3267 | cursor++; |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3268 | atLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3269 | InsertText(atLoc, " */"); |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3270 | } else if (*cursor == '^') { // rewrite block specifier. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3271 | SourceLocation caretLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3272 | ReplaceText(caretLoc, 1, "*"); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3273 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3274 | cursor++; |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3275 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3276 | // Don't forget to add a ';'!! |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3277 | InsertText(LocEnd.getLocWithOffset(1), ";"); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3278 | } else { // we don't have any instance variables - insert super struct. |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3279 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3280 | Result += " {\n struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3281 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 39bbd9f | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 3282 | Result += "_IMPL "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3283 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 819173c | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 3284 | Result += "_IVARS;\n};\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3285 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3286 | } |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3287 | // Mark this struct as having been generated. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3288 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3289 | llvm_unreachable("struct already synthesize- SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3290 | } |
| 3291 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3292 | //===----------------------------------------------------------------------===// |
| 3293 | // Meta Data Emission |
| 3294 | //===----------------------------------------------------------------------===// |
| 3295 | |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3296 | |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3297 | /// RewriteImplementations - This routine rewrites all method implementations |
| 3298 | /// and emits meta-data. |
| 3299 | |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3300 | void RewriteObjC::RewriteImplementations() { |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3301 | int ClsDefCount = ClassImplementation.size(); |
| 3302 | int CatDefCount = CategoryImplementation.size(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3303 | |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3304 | // Rewrite implemented methods |
| 3305 | for (int i = 0; i < ClsDefCount; i++) |
| 3306 | RewriteImplementationDecl(ClassImplementation[i]); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3307 | |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 3308 | for (int i = 0; i < CatDefCount; i++) |
| 3309 | RewriteImplementationDecl(CategoryImplementation[i]); |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3310 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3311 | |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3312 | void RewriteObjC::RewriteByRefString(std::string &ResultStr, |
| 3313 | const std::string &Name, |
Fariborz Jahanian | 1e8011e | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 3314 | ValueDecl *VD, bool def) { |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3315 | assert(BlockByRefDeclNo.count(VD) && |
| 3316 | "RewriteByRefString: ByRef decl missing"); |
Fariborz Jahanian | 1e8011e | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 3317 | if (def) |
| 3318 | ResultStr += "struct "; |
| 3319 | ResultStr += "__Block_byref_" + Name + |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3320 | "_" + utostr(BlockByRefDeclNo[VD]) ; |
| 3321 | } |
| 3322 | |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3323 | static bool HasLocalVariableExternalStorage(ValueDecl *VD) { |
| 3324 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 3325 | return (Var->isFunctionOrMethodVarDecl() && !Var->hasLocalStorage()); |
| 3326 | return false; |
| 3327 | } |
| 3328 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3329 | std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3330 | StringRef funcName, |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3331 | std::string Tag) { |
| 3332 | const FunctionType *AFT = CE->getFunctionType(); |
| 3333 | QualType RT = AFT->getResultType(); |
| 3334 | std::string StructRef = "struct " + Tag; |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 3335 | std::string S = "static " + RT.getAsString(Context->getPrintingPolicy()) + " __" + |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3336 | funcName.str() + "_" + "block_func_" + utostr(i); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 3337 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3338 | BlockDecl *BD = CE->getBlockDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3339 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3340 | if (isa<FunctionNoProtoType>(AFT)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3341 | // 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] | 3342 | // block (to reference imported block decl refs). |
| 3343 | S += "(" + StructRef + " *__cself)"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3344 | } else if (BD->param_empty()) { |
| 3345 | S += "(" + StructRef + " *__cself)"; |
| 3346 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3347 | const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3348 | assert(FT && "SynthesizeBlockFunc: No function proto"); |
| 3349 | S += '('; |
| 3350 | // first add the implicit argument. |
| 3351 | S += StructRef + " *__cself, "; |
| 3352 | std::string ParamStr; |
| 3353 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 3354 | E = BD->param_end(); AI != E; ++AI) { |
| 3355 | if (AI != BD->param_begin()) S += ", "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3356 | ParamStr = (*AI)->getNameAsString(); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3357 | QualType QT = (*AI)->getType(); |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3358 | if (convertBlockPointerToFunctionPointer(QT)) |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 3359 | QT.getAsStringInternal(ParamStr, Context->getPrintingPolicy()); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3360 | else |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 3361 | QT.getAsStringInternal(ParamStr, Context->getPrintingPolicy()); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3362 | S += ParamStr; |
| 3363 | } |
| 3364 | if (FT->isVariadic()) { |
| 3365 | if (!BD->param_empty()) S += ", "; |
| 3366 | S += "..."; |
| 3367 | } |
| 3368 | S += ')'; |
| 3369 | } |
| 3370 | S += " {\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3371 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3372 | // Create local declarations to avoid rewriting all closure decl ref exprs. |
| 3373 | // First, emit a declaration for all "by ref" decls. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3374 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3375 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3376 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3377 | std::string Name = (*I)->getNameAsString(); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3378 | std::string TypeString; |
| 3379 | RewriteByRefString(TypeString, Name, (*I)); |
| 3380 | TypeString += " *"; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 3381 | Name = TypeString + Name; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3382 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3383 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3384 | // Next, emit a declaration for all "by copy" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3385 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3386 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3387 | S += " "; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3388 | // Handle nested closure invocation. For example: |
| 3389 | // |
| 3390 | // void (^myImportedClosure)(void); |
| 3391 | // myImportedClosure = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3392 | // |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3393 | // void (^anotherClosure)(void); |
| 3394 | // anotherClosure = ^(void) { |
| 3395 | // myImportedClosure(); // import and invoke the closure |
| 3396 | // }; |
| 3397 | // |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 3398 | if (isTopLevelBlockPointerType((*I)->getType())) { |
| 3399 | RewriteBlockPointerTypeVariable(S, (*I)); |
| 3400 | S += " = ("; |
| 3401 | RewriteBlockPointerType(S, (*I)->getType()); |
| 3402 | S += ")"; |
| 3403 | S += "__cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; |
| 3404 | } |
| 3405 | else { |
Fariborz Jahanian | 210c248 | 2010-02-16 17:26:03 +0000 | [diff] [blame] | 3406 | std::string Name = (*I)->getNameAsString(); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3407 | QualType QT = (*I)->getType(); |
| 3408 | if (HasLocalVariableExternalStorage(*I)) |
| 3409 | QT = Context->getPointerType(QT); |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 3410 | QT.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 3411 | S += Name + " = __cself->" + |
| 3412 | (*I)->getNameAsString() + "; // bound by copy\n"; |
| 3413 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3414 | } |
| 3415 | std::string RewrittenStr = RewrittenBlockExprs[CE]; |
| 3416 | const char *cstr = RewrittenStr.c_str(); |
| 3417 | while (*cstr++ != '{') ; |
| 3418 | S += cstr; |
| 3419 | S += "\n"; |
| 3420 | return S; |
| 3421 | } |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 3422 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3423 | std::string RewriteObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3424 | StringRef funcName, |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3425 | std::string Tag) { |
| 3426 | std::string StructRef = "struct " + Tag; |
| 3427 | std::string S = "static void __"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3428 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3429 | S += funcName; |
| 3430 | S += "_block_copy_" + utostr(i); |
| 3431 | S += "(" + StructRef; |
| 3432 | S += "*dst, " + StructRef; |
| 3433 | S += "*src) {"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3434 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3435 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3436 | ValueDecl *VD = (*I); |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3437 | S += "_Block_object_assign((void*)&dst->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3438 | S += (*I)->getNameAsString(); |
Steve Naroff | 47a2422 | 2008-12-11 20:51:38 +0000 | [diff] [blame] | 3439 | S += ", (void*)src->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3440 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 3441 | if (BlockByRefDeclsPtrSet.count((*I))) |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 3442 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3443 | else if (VD->getType()->isBlockPointerType()) |
Fariborz Jahanian | 06433c6 | 2011-07-30 01:07:55 +0000 | [diff] [blame] | 3444 | S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);"; |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3445 | else |
| 3446 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3447 | } |
Fariborz Jahanian | d25d1b5 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 3448 | S += "}\n"; |
| 3449 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3450 | S += "\nstatic void __"; |
| 3451 | S += funcName; |
| 3452 | S += "_block_dispose_" + utostr(i); |
| 3453 | S += "(" + StructRef; |
| 3454 | S += "*src) {"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3455 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3456 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3457 | ValueDecl *VD = (*I); |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3458 | S += "_Block_object_dispose((void*)src->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3459 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 3460 | if (BlockByRefDeclsPtrSet.count((*I))) |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 3461 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3462 | else if (VD->getType()->isBlockPointerType()) |
Fariborz Jahanian | 06433c6 | 2011-07-30 01:07:55 +0000 | [diff] [blame] | 3463 | S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);"; |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3464 | else |
| 3465 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3466 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3467 | S += "}\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3468 | return S; |
| 3469 | } |
| 3470 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3471 | std::string RewriteObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 3472 | std::string Desc) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3473 | std::string S = "\nstruct " + Tag; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3474 | std::string Constructor = " " + Tag; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3475 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3476 | S += " {\n struct __block_impl impl;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3477 | S += " struct " + Desc; |
| 3478 | S += "* Desc;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3479 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3480 | Constructor += "(void *fp, "; // Invoke function pointer. |
| 3481 | Constructor += "struct " + Desc; // Descriptor pointer. |
| 3482 | Constructor += " *desc"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3483 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3484 | if (BlockDeclRefs.size()) { |
| 3485 | // Output all "by copy" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3486 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3487 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3488 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3489 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3490 | std::string ArgName = "_" + FieldName; |
| 3491 | // Handle nested closure invocation. For example: |
| 3492 | // |
| 3493 | // void (^myImportedBlock)(void); |
| 3494 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3495 | // |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3496 | // void (^anotherBlock)(void); |
| 3497 | // anotherBlock = ^(void) { |
| 3498 | // myImportedBlock(); // import and invoke the closure |
| 3499 | // }; |
| 3500 | // |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3501 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3502 | S += "struct __block_impl *"; |
| 3503 | Constructor += ", void *" + ArgName; |
| 3504 | } else { |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3505 | QualType QT = (*I)->getType(); |
| 3506 | if (HasLocalVariableExternalStorage(*I)) |
| 3507 | QT = Context->getPointerType(QT); |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 3508 | QT.getAsStringInternal(FieldName, Context->getPrintingPolicy()); |
| 3509 | QT.getAsStringInternal(ArgName, Context->getPrintingPolicy()); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3510 | Constructor += ", " + ArgName; |
| 3511 | } |
| 3512 | S += FieldName + ";\n"; |
| 3513 | } |
| 3514 | // Output all "by ref" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3515 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3516 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3517 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3518 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3519 | std::string ArgName = "_" + FieldName; |
Fariborz Jahanian | 651ba52 | 2011-04-01 23:08:13 +0000 | [diff] [blame] | 3520 | { |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3521 | std::string TypeString; |
| 3522 | RewriteByRefString(TypeString, FieldName, (*I)); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 3523 | TypeString += " *"; |
| 3524 | FieldName = TypeString + FieldName; |
| 3525 | ArgName = TypeString + ArgName; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3526 | Constructor += ", " + ArgName; |
| 3527 | } |
| 3528 | S += FieldName + "; // by ref\n"; |
| 3529 | } |
| 3530 | // Finish writing the constructor. |
Fariborz Jahanian | 20432ef | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 3531 | Constructor += ", int flags=0)"; |
| 3532 | // Initialize all "by copy" arguments. |
| 3533 | bool firsTime = true; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3534 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Fariborz Jahanian | 20432ef | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 3535 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3536 | std::string Name = (*I)->getNameAsString(); |
| 3537 | if (firsTime) { |
| 3538 | Constructor += " : "; |
| 3539 | firsTime = false; |
| 3540 | } |
| 3541 | else |
| 3542 | Constructor += ", "; |
| 3543 | if (isTopLevelBlockPointerType((*I)->getType())) |
| 3544 | Constructor += Name + "((struct __block_impl *)_" + Name + ")"; |
| 3545 | else |
| 3546 | Constructor += Name + "(_" + Name + ")"; |
| 3547 | } |
| 3548 | // Initialize all "by ref" arguments. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3549 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Fariborz Jahanian | 20432ef | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 3550 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3551 | std::string Name = (*I)->getNameAsString(); |
| 3552 | if (firsTime) { |
| 3553 | Constructor += " : "; |
| 3554 | firsTime = false; |
| 3555 | } |
| 3556 | else |
| 3557 | Constructor += ", "; |
Fariborz Jahanian | 651ba52 | 2011-04-01 23:08:13 +0000 | [diff] [blame] | 3558 | Constructor += Name + "(_" + Name + "->__forwarding)"; |
Fariborz Jahanian | 20432ef | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 3559 | } |
| 3560 | |
| 3561 | Constructor += " {\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3562 | if (GlobalVarDecl) |
| 3563 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 3564 | else |
| 3565 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3566 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3567 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3568 | Constructor += " Desc = desc;\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3569 | } else { |
| 3570 | // Finish writing the constructor. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3571 | Constructor += ", int flags=0) {\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3572 | if (GlobalVarDecl) |
| 3573 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 3574 | else |
| 3575 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3576 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 3577 | Constructor += " Desc = desc;\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3578 | } |
| 3579 | Constructor += " "; |
| 3580 | Constructor += "}\n"; |
| 3581 | S += Constructor; |
| 3582 | S += "};\n"; |
| 3583 | return S; |
| 3584 | } |
| 3585 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3586 | std::string RewriteObjC::SynthesizeBlockDescriptor(std::string DescTag, |
| 3587 | std::string ImplTag, int i, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3588 | StringRef FunName, |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3589 | unsigned hasCopy) { |
| 3590 | std::string S = "\nstatic struct " + DescTag; |
| 3591 | |
| 3592 | S += " {\n unsigned long reserved;\n"; |
| 3593 | S += " unsigned long Block_size;\n"; |
| 3594 | if (hasCopy) { |
Fariborz Jahanian | 4fcc4fd | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 3595 | S += " void (*copy)(struct "; |
| 3596 | S += ImplTag; S += "*, struct "; |
| 3597 | S += ImplTag; S += "*);\n"; |
| 3598 | |
| 3599 | S += " void (*dispose)(struct "; |
| 3600 | S += ImplTag; S += "*);\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3601 | } |
| 3602 | S += "} "; |
| 3603 | |
| 3604 | S += DescTag + "_DATA = { 0, sizeof(struct "; |
| 3605 | S += ImplTag + ")"; |
| 3606 | if (hasCopy) { |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3607 | S += ", __" + FunName.str() + "_block_copy_" + utostr(i); |
| 3608 | S += ", __" + FunName.str() + "_block_dispose_" + utostr(i); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3609 | } |
| 3610 | S += "};\n"; |
| 3611 | return S; |
| 3612 | } |
| 3613 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3614 | void RewriteObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3615 | StringRef FunName) { |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 3616 | // Insert declaration for the function in which block literal is used. |
Fariborz Jahanian | bf07012 | 2010-01-15 18:14:52 +0000 | [diff] [blame] | 3617 | if (CurFunctionDeclToDeclareForBlock && !Blocks.empty()) |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 3618 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Fariborz Jahanian | 8611eb0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 3619 | bool RewriteSC = (GlobalVarDecl && |
| 3620 | !Blocks.empty() && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3621 | GlobalVarDecl->getStorageClass() == SC_Static && |
Fariborz Jahanian | 8611eb0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 3622 | GlobalVarDecl->getType().getCVRQualifiers()); |
| 3623 | if (RewriteSC) { |
| 3624 | std::string SC(" void __"); |
| 3625 | SC += GlobalVarDecl->getNameAsString(); |
| 3626 | SC += "() {}"; |
| 3627 | InsertText(FunLocStart, SC); |
| 3628 | } |
| 3629 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3630 | // Insert closures that were part of the function. |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3631 | for (unsigned i = 0, count=0; i < Blocks.size(); i++) { |
| 3632 | CollectBlockDeclRefInfo(Blocks[i]); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3633 | // Need to copy-in the inner copied-in variables not actually used in this |
| 3634 | // block. |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3635 | for (int j = 0; j < InnerDeclRefsCount[i]; j++) { |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 3636 | DeclRefExpr *Exp = InnerDeclRefs[count++]; |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3637 | ValueDecl *VD = Exp->getDecl(); |
| 3638 | BlockDeclRefs.push_back(Exp); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 3639 | if (!VD->hasAttr<BlocksAttr>() && !BlockByCopyDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3640 | BlockByCopyDeclsPtrSet.insert(VD); |
| 3641 | BlockByCopyDecls.push_back(VD); |
| 3642 | } |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 3643 | if (VD->hasAttr<BlocksAttr>() && !BlockByRefDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3644 | BlockByRefDeclsPtrSet.insert(VD); |
| 3645 | BlockByRefDecls.push_back(VD); |
| 3646 | } |
Fariborz Jahanian | 92c8568 | 2010-10-05 18:05:06 +0000 | [diff] [blame] | 3647 | // imported objects in the inner blocks not used in the outer |
| 3648 | // blocks must be copied/disposed in the outer block as well. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 3649 | if (VD->hasAttr<BlocksAttr>() || |
Fariborz Jahanian | 92c8568 | 2010-10-05 18:05:06 +0000 | [diff] [blame] | 3650 | VD->getType()->isObjCObjectPointerType() || |
| 3651 | VD->getType()->isBlockPointerType()) |
| 3652 | ImportedBlockDecls.insert(VD); |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3653 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3654 | |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3655 | std::string ImplTag = "__" + FunName.str() + "_block_impl_" + utostr(i); |
| 3656 | std::string DescTag = "__" + FunName.str() + "_block_desc_" + utostr(i); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3657 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3658 | std::string CI = SynthesizeBlockImpl(Blocks[i], ImplTag, DescTag); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3659 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3660 | InsertText(FunLocStart, CI); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3661 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3662 | std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, ImplTag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3663 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3664 | InsertText(FunLocStart, CF); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3665 | |
| 3666 | if (ImportedBlockDecls.size()) { |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3667 | std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, ImplTag); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3668 | InsertText(FunLocStart, HF); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3669 | } |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3670 | std::string BD = SynthesizeBlockDescriptor(DescTag, ImplTag, i, FunName, |
| 3671 | ImportedBlockDecls.size() > 0); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3672 | InsertText(FunLocStart, BD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3673 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3674 | BlockDeclRefs.clear(); |
| 3675 | BlockByRefDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 3676 | BlockByRefDeclsPtrSet.clear(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3677 | BlockByCopyDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 3678 | BlockByCopyDeclsPtrSet.clear(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3679 | ImportedBlockDecls.clear(); |
| 3680 | } |
Fariborz Jahanian | 8611eb0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 3681 | if (RewriteSC) { |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3682 | // Must insert any 'const/volatile/static here. Since it has been |
| 3683 | // removed as result of rewriting of block literals. |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3684 | std::string SC; |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3685 | if (GlobalVarDecl->getStorageClass() == SC_Static) |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3686 | SC = "static "; |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3687 | if (GlobalVarDecl->getType().isConstQualified()) |
| 3688 | SC += "const "; |
| 3689 | if (GlobalVarDecl->getType().isVolatileQualified()) |
| 3690 | SC += "volatile "; |
Fariborz Jahanian | 8611eb0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 3691 | if (GlobalVarDecl->getType().isRestrictQualified()) |
| 3692 | SC += "restrict "; |
| 3693 | InsertText(FunLocStart, SC); |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3694 | } |
| 3695 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3696 | Blocks.clear(); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3697 | InnerDeclRefsCount.clear(); |
| 3698 | InnerDeclRefs.clear(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3699 | RewrittenBlockExprs.clear(); |
| 3700 | } |
| 3701 | |
| 3702 | void RewriteObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { |
| 3703 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3704 | StringRef FuncName = FD->getName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3705 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3706 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 3707 | } |
| 3708 | |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 3709 | static void BuildUniqueMethodName(std::string &Name, |
| 3710 | ObjCMethodDecl *MD) { |
| 3711 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3712 | Name = IFace->getName(); |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 3713 | Name += "__" + MD->getSelector().getAsString(); |
| 3714 | // Convert colons to underscores. |
| 3715 | std::string::size_type loc = 0; |
| 3716 | while ((loc = Name.find(":", loc)) != std::string::npos) |
| 3717 | Name.replace(loc, 1, "_"); |
| 3718 | } |
| 3719 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3720 | void RewriteObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3721 | //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n"); |
| 3722 | //SourceLocation FunLocStart = MD->getLocStart(); |
Fariborz Jahanian | 0e1c99a | 2010-01-29 01:55:49 +0000 | [diff] [blame] | 3723 | SourceLocation FunLocStart = MD->getLocStart(); |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 3724 | std::string FuncName; |
| 3725 | BuildUniqueMethodName(FuncName, MD); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3726 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3727 | } |
| 3728 | |
| 3729 | void RewriteObjC::GetBlockDeclRefExprs(Stmt *S) { |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 3730 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3731 | if (*CI) { |
| 3732 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 3733 | GetBlockDeclRefExprs(CBE->getBody()); |
| 3734 | else |
| 3735 | GetBlockDeclRefExprs(*CI); |
| 3736 | } |
| 3737 | // Handle specific things. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 3738 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 3739 | if (DRE->refersToEnclosingLocal()) { |
| 3740 | // FIXME: Handle enums. |
| 3741 | if (!isa<FunctionDecl>(DRE->getDecl())) |
| 3742 | BlockDeclRefs.push_back(DRE); |
| 3743 | if (HasLocalVariableExternalStorage(DRE->getDecl())) |
| 3744 | BlockDeclRefs.push_back(DRE); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3745 | } |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 3746 | } |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3747 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3748 | return; |
| 3749 | } |
| 3750 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3751 | void RewriteObjC::GetInnerBlockDeclRefExprs(Stmt *S, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 3752 | SmallVector<DeclRefExpr *, 8> &InnerBlockDeclRefs, |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3753 | llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts) { |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 3754 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3755 | if (*CI) { |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3756 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) { |
| 3757 | InnerContexts.insert(cast<DeclContext>(CBE->getBlockDecl())); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3758 | GetInnerBlockDeclRefExprs(CBE->getBody(), |
| 3759 | InnerBlockDeclRefs, |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3760 | InnerContexts); |
| 3761 | } |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3762 | else |
| 3763 | GetInnerBlockDeclRefExprs(*CI, |
| 3764 | InnerBlockDeclRefs, |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3765 | InnerContexts); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3766 | |
| 3767 | } |
| 3768 | // Handle specific things. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 3769 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 3770 | if (DRE->refersToEnclosingLocal()) { |
| 3771 | if (!isa<FunctionDecl>(DRE->getDecl()) && |
| 3772 | !InnerContexts.count(DRE->getDecl()->getDeclContext())) |
| 3773 | InnerBlockDeclRefs.push_back(DRE); |
| 3774 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) |
| 3775 | if (Var->isFunctionOrMethodVarDecl()) |
| 3776 | ImportedLocalExternalDecls.insert(Var); |
| 3777 | } |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3778 | } |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3779 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3780 | return; |
| 3781 | } |
| 3782 | |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3783 | /// convertFunctionTypeOfBlocks - This routine converts a function type |
| 3784 | /// 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] | 3785 | /// might be block pointers to an equivalent function type replacing |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3786 | /// all block pointers to function pointers. |
| 3787 | QualType RewriteObjC::convertFunctionTypeOfBlocks(const FunctionType *FT) { |
| 3788 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
| 3789 | // FTP will be null for closures that don't take arguments. |
| 3790 | // Generate a funky cast. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3791 | SmallVector<QualType, 8> ArgTypes; |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3792 | QualType Res = FT->getResultType(); |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3793 | bool HasBlockType = convertBlockPointerToFunctionPointer(Res); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3794 | |
| 3795 | if (FTP) { |
| 3796 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
| 3797 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 3798 | QualType t = *I; |
| 3799 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3800 | if (convertBlockPointerToFunctionPointer(t)) |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3801 | HasBlockType = true; |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3802 | ArgTypes.push_back(t); |
| 3803 | } |
| 3804 | } |
| 3805 | QualType FuncType; |
| 3806 | // FIXME. Does this work if block takes no argument but has a return type |
| 3807 | // which is of block type? |
| 3808 | if (HasBlockType) |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3809 | FuncType = getSimpleFunctionType(Res, &ArgTypes[0], ArgTypes.size()); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3810 | else FuncType = QualType(FT, 0); |
| 3811 | return FuncType; |
| 3812 | } |
| 3813 | |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3814 | Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp, const Expr *BlockExp) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3815 | // Navigate to relevant type information. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3816 | const BlockPointerType *CPT = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3817 | |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3818 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BlockExp)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3819 | CPT = DRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3820 | } else if (const MemberExpr *MExpr = dyn_cast<MemberExpr>(BlockExp)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3821 | CPT = MExpr->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3822 | } |
| 3823 | else if (const ParenExpr *PRE = dyn_cast<ParenExpr>(BlockExp)) { |
| 3824 | return SynthesizeBlockCall(Exp, PRE->getSubExpr()); |
| 3825 | } |
| 3826 | else if (const ImplicitCastExpr *IEXPR = dyn_cast<ImplicitCastExpr>(BlockExp)) |
| 3827 | CPT = IEXPR->getType()->getAs<BlockPointerType>(); |
| 3828 | else if (const ConditionalOperator *CEXPR = |
| 3829 | dyn_cast<ConditionalOperator>(BlockExp)) { |
| 3830 | Expr *LHSExp = CEXPR->getLHS(); |
| 3831 | Stmt *LHSStmt = SynthesizeBlockCall(Exp, LHSExp); |
| 3832 | Expr *RHSExp = CEXPR->getRHS(); |
| 3833 | Stmt *RHSStmt = SynthesizeBlockCall(Exp, RHSExp); |
| 3834 | Expr *CONDExp = CEXPR->getCond(); |
| 3835 | ConditionalOperator *CondExpr = |
| 3836 | new (Context) ConditionalOperator(CONDExp, |
| 3837 | SourceLocation(), cast<Expr>(LHSStmt), |
Fariborz Jahanian | f9b949f | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 3838 | SourceLocation(), cast<Expr>(RHSStmt), |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3839 | Exp->getType(), VK_RValue, OK_Ordinary); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3840 | return CondExpr; |
Fariborz Jahanian | e24b22b | 2009-12-18 01:15:21 +0000 | [diff] [blame] | 3841 | } else if (const ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(BlockExp)) { |
| 3842 | CPT = IRE->getType()->getAs<BlockPointerType>(); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 3843 | } else if (const PseudoObjectExpr *POE |
| 3844 | = dyn_cast<PseudoObjectExpr>(BlockExp)) { |
| 3845 | CPT = POE->getType()->castAs<BlockPointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3846 | } else { |
| 3847 | assert(1 && "RewriteBlockClass: Bad type"); |
| 3848 | } |
| 3849 | assert(CPT && "RewriteBlockClass: Bad type"); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3850 | const FunctionType *FT = CPT->getPointeeType()->getAs<FunctionType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3851 | assert(FT && "RewriteBlockClass: Bad type"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3852 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3853 | // FTP will be null for closures that don't take arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3854 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3855 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3856 | SourceLocation(), SourceLocation(), |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3857 | &Context->Idents.get("__block_impl")); |
| 3858 | QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD)); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3859 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3860 | // Generate a funky cast. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3861 | SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3862 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3863 | // Push the block argument type. |
| 3864 | ArgTypes.push_back(PtrBlock); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3865 | if (FTP) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3866 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3867 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 3868 | QualType t = *I; |
| 3869 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 8188e5f | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 3870 | if (!convertBlockPointerToFunctionPointer(t)) |
| 3871 | convertToUnqualifiedObjCType(t); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3872 | ArgTypes.push_back(t); |
| 3873 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3874 | } |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3875 | // Now do the pointer to function cast. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3876 | QualType PtrToFuncCastType |
| 3877 | = getSimpleFunctionType(Exp->getType(), &ArgTypes[0], ArgTypes.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3878 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3879 | PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3880 | |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3881 | CastExpr *BlkCast = NoTypeInfoCStyleCastExpr(Context, PtrBlock, |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3882 | CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3883 | const_cast<Expr*>(BlockExp)); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3884 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3885 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3886 | BlkCast); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3887 | //PE->dump(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3888 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3889 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3890 | SourceLocation(), |
| 3891 | &Context->Idents.get("FuncPtr"), |
| 3892 | Context->VoidPtrTy, 0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3893 | /*BitWidth=*/0, /*Mutable=*/true, |
| 3894 | /*HasInit=*/false); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3895 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3896 | FD->getType(), VK_LValue, |
| 3897 | OK_Ordinary); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3898 | |
Fariborz Jahanian | 8188e5f | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 3899 | |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3900 | CastExpr *FunkCast = NoTypeInfoCStyleCastExpr(Context, PtrToFuncCastType, |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3901 | CK_BitCast, ME); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3902 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3903 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3904 | SmallVector<Expr*, 8> BlkExprs; |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3905 | // Add the implicit argument. |
| 3906 | BlkExprs.push_back(BlkCast); |
| 3907 | // Add the user arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3908 | for (CallExpr::arg_iterator I = Exp->arg_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3909 | E = Exp->arg_end(); I != E; ++I) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3910 | BlkExprs.push_back(*I); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3911 | } |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3912 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &BlkExprs[0], |
| 3913 | BlkExprs.size(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3914 | Exp->getType(), VK_RValue, |
| 3915 | SourceLocation()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3916 | return CE; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3917 | } |
| 3918 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3919 | // We need to return the rewritten expression to handle cases where the |
| 3920 | // BlockDeclRefExpr is embedded in another expression being rewritten. |
| 3921 | // For example: |
| 3922 | // |
| 3923 | // int main() { |
| 3924 | // __block Foo *f; |
| 3925 | // __block int i; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3926 | // |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3927 | // void (^myblock)() = ^() { |
| 3928 | // [f test]; // f is a BlockDeclRefExpr embedded in a message (which is being rewritten). |
| 3929 | // i = 77; |
| 3930 | // }; |
| 3931 | //} |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 3932 | Stmt *RewriteObjC::RewriteBlockDeclRefExpr(DeclRefExpr *DeclRefExp) { |
Fariborz Jahanian | bbf37e2 | 2009-12-23 19:26:34 +0000 | [diff] [blame] | 3933 | // Rewrite the byref variable into BYREFVAR->__forwarding->BYREFVAR |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3934 | // for each DeclRefExp where BYREFVAR is name of the variable. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 3935 | ValueDecl *VD = DeclRefExp->getDecl(); |
| 3936 | bool isArrow = DeclRefExp->refersToEnclosingLocal(); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3937 | |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3938 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3939 | SourceLocation(), |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3940 | &Context->Idents.get("__forwarding"), |
| 3941 | Context->VoidPtrTy, 0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3942 | /*BitWidth=*/0, /*Mutable=*/true, |
| 3943 | /*HasInit=*/false); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3944 | MemberExpr *ME = new (Context) MemberExpr(DeclRefExp, isArrow, |
| 3945 | FD, SourceLocation(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3946 | FD->getType(), VK_LValue, |
| 3947 | OK_Ordinary); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3948 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3949 | StringRef Name = VD->getName(); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3950 | FD = FieldDecl::Create(*Context, 0, SourceLocation(), SourceLocation(), |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3951 | &Context->Idents.get(Name), |
| 3952 | Context->VoidPtrTy, 0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3953 | /*BitWidth=*/0, /*Mutable=*/true, |
| 3954 | /*HasInit=*/false); |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3955 | ME = new (Context) MemberExpr(ME, true, FD, SourceLocation(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3956 | DeclRefExp->getType(), VK_LValue, OK_Ordinary); |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3957 | |
| 3958 | |
| 3959 | |
Steve Naroff | df8570d | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 3960 | // Need parens to enforce precedence. |
Fariborz Jahanian | 380ee50 | 2011-04-01 19:19:28 +0000 | [diff] [blame] | 3961 | ParenExpr *PE = new (Context) ParenExpr(DeclRefExp->getExprLoc(), |
| 3962 | DeclRefExp->getExprLoc(), |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3963 | ME); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3964 | ReplaceStmt(DeclRefExp, PE); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3965 | return PE; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3966 | } |
| 3967 | |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3968 | // Rewrites the imported local variable V with external storage |
| 3969 | // (static, extern, etc.) as *V |
| 3970 | // |
| 3971 | Stmt *RewriteObjC::RewriteLocalVariableExternalStorage(DeclRefExpr *DRE) { |
| 3972 | ValueDecl *VD = DRE->getDecl(); |
| 3973 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 3974 | if (!ImportedLocalExternalDecls.count(Var)) |
| 3975 | return DRE; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3976 | Expr *Exp = new (Context) UnaryOperator(DRE, UO_Deref, DRE->getType(), |
| 3977 | VK_LValue, OK_Ordinary, |
| 3978 | DRE->getLocation()); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3979 | // Need parens to enforce precedence. |
| 3980 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3981 | Exp); |
| 3982 | ReplaceStmt(DRE, PE); |
| 3983 | return PE; |
| 3984 | } |
| 3985 | |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 3986 | void RewriteObjC::RewriteCastExpr(CStyleCastExpr *CE) { |
| 3987 | SourceLocation LocStart = CE->getLParenLoc(); |
| 3988 | SourceLocation LocEnd = CE->getRParenLoc(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3989 | |
| 3990 | // Need to avoid trying to rewrite synthesized casts. |
| 3991 | if (LocStart.isInvalid()) |
| 3992 | return; |
Steve Naroff | 8f6ce57 | 2008-11-03 11:20:24 +0000 | [diff] [blame] | 3993 | // Need to avoid trying to rewrite casts contained in macros. |
| 3994 | if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) |
| 3995 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3996 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3997 | const char *startBuf = SM->getCharacterData(LocStart); |
| 3998 | const char *endBuf = SM->getCharacterData(LocEnd); |
Fariborz Jahanian | 1d4fca2 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 3999 | QualType QT = CE->getType(); |
| 4000 | const Type* TypePtr = QT->getAs<Type>(); |
| 4001 | if (isa<TypeOfExprType>(TypePtr)) { |
| 4002 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 4003 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 4004 | std::string TypeAsString = "("; |
Fariborz Jahanian | afad76f | 2010-02-18 01:20:22 +0000 | [diff] [blame] | 4005 | RewriteBlockPointerType(TypeAsString, QT); |
Fariborz Jahanian | 1d4fca2 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4006 | TypeAsString += ")"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4007 | ReplaceText(LocStart, endBuf-startBuf+1, TypeAsString); |
Fariborz Jahanian | 1d4fca2 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4008 | return; |
| 4009 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4010 | // advance the location to startArgList. |
| 4011 | const char *argPtr = startBuf; |
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 | while (*argPtr++ && (argPtr < endBuf)) { |
| 4014 | switch (*argPtr) { |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4015 | case '^': |
| 4016 | // Replace the '^' with '*'. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4017 | LocStart = LocStart.getLocWithOffset(argPtr-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4018 | ReplaceText(LocStart, 1, "*"); |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4019 | break; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4020 | } |
| 4021 | } |
| 4022 | return; |
| 4023 | } |
| 4024 | |
| 4025 | void RewriteObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { |
| 4026 | SourceLocation DeclLoc = FD->getLocation(); |
| 4027 | unsigned parenCount = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4028 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4029 | // We have 1 or more arguments that have closure pointers. |
| 4030 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4031 | const char *startArgList = strchr(startBuf, '('); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4032 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4033 | assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4034 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4035 | parenCount++; |
| 4036 | // advance the location to startArgList. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4037 | DeclLoc = DeclLoc.getLocWithOffset(startArgList-startBuf); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4038 | assert((DeclLoc.isValid()) && "Invalid DeclLoc"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4039 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4040 | const char *argPtr = startArgList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4041 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4042 | while (*argPtr++ && parenCount) { |
| 4043 | switch (*argPtr) { |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4044 | case '^': |
| 4045 | // Replace the '^' with '*'. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4046 | DeclLoc = DeclLoc.getLocWithOffset(argPtr-startArgList); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4047 | ReplaceText(DeclLoc, 1, "*"); |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4048 | break; |
| 4049 | case '(': |
| 4050 | parenCount++; |
| 4051 | break; |
| 4052 | case ')': |
| 4053 | parenCount--; |
| 4054 | break; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4055 | } |
| 4056 | } |
| 4057 | return; |
| 4058 | } |
| 4059 | |
| 4060 | bool RewriteObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4061 | const FunctionProtoType *FTP; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4062 | const PointerType *PT = QT->getAs<PointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4063 | if (PT) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4064 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4065 | } else { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4066 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4067 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4068 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4069 | } |
| 4070 | if (FTP) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4071 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4072 | E = FTP->arg_type_end(); I != E; ++I) |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4073 | if (isTopLevelBlockPointerType(*I)) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4074 | return true; |
| 4075 | } |
| 4076 | return false; |
| 4077 | } |
| 4078 | |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4079 | bool RewriteObjC::PointerTypeTakesAnyObjCQualifiedType(QualType QT) { |
| 4080 | const FunctionProtoType *FTP; |
| 4081 | const PointerType *PT = QT->getAs<PointerType>(); |
| 4082 | if (PT) { |
| 4083 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4084 | } else { |
| 4085 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
| 4086 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
| 4087 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4088 | } |
| 4089 | if (FTP) { |
| 4090 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Fariborz Jahanian | 06de2cf | 2010-11-03 23:50:34 +0000 | [diff] [blame] | 4091 | E = FTP->arg_type_end(); I != E; ++I) { |
| 4092 | if ((*I)->isObjCQualifiedIdType()) |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4093 | return true; |
Fariborz Jahanian | 06de2cf | 2010-11-03 23:50:34 +0000 | [diff] [blame] | 4094 | if ((*I)->isObjCObjectPointerType() && |
| 4095 | (*I)->getPointeeType()->isObjCQualifiedInterfaceType()) |
| 4096 | return true; |
| 4097 | } |
| 4098 | |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4099 | } |
| 4100 | return false; |
| 4101 | } |
| 4102 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4103 | void RewriteObjC::GetExtentOfArgList(const char *Name, const char *&LParen, |
| 4104 | const char *&RParen) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4105 | const char *argPtr = strchr(Name, '('); |
| 4106 | assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4107 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4108 | LParen = argPtr; // output the start. |
| 4109 | argPtr++; // skip past the left paren. |
| 4110 | unsigned parenCount = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4111 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4112 | while (*argPtr && parenCount) { |
| 4113 | switch (*argPtr) { |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4114 | case '(': parenCount++; break; |
| 4115 | case ')': parenCount--; break; |
| 4116 | default: break; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4117 | } |
| 4118 | if (parenCount) argPtr++; |
| 4119 | } |
| 4120 | assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); |
| 4121 | RParen = argPtr; // output the end |
| 4122 | } |
| 4123 | |
| 4124 | void RewriteObjC::RewriteBlockPointerDecl(NamedDecl *ND) { |
| 4125 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 4126 | RewriteBlockPointerFunctionArgs(FD); |
| 4127 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4128 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4129 | // Handle Variables and Typedefs. |
| 4130 | SourceLocation DeclLoc = ND->getLocation(); |
| 4131 | QualType DeclT; |
| 4132 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4133 | DeclT = VD->getType(); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4134 | else if (TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(ND)) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4135 | DeclT = TDD->getUnderlyingType(); |
| 4136 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) |
| 4137 | DeclT = FD->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4138 | else |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4139 | llvm_unreachable("RewriteBlockPointerDecl(): Decl type not yet handled"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4140 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4141 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4142 | const char *endBuf = startBuf; |
| 4143 | // scan backward (from the decl location) for the end of the previous decl. |
| 4144 | while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) |
| 4145 | startBuf--; |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4146 | SourceLocation Start = DeclLoc.getLocWithOffset(startBuf-endBuf); |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4147 | std::string buf; |
| 4148 | unsigned OrigLength=0; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4149 | // *startBuf != '^' if we are dealing with a pointer to function that |
| 4150 | // may take block argument types (which will be handled below). |
| 4151 | if (*startBuf == '^') { |
| 4152 | // Replace the '^' with '*', computing a negative offset. |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4153 | buf = '*'; |
| 4154 | startBuf++; |
| 4155 | OrigLength++; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4156 | } |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4157 | while (*startBuf != ')') { |
| 4158 | buf += *startBuf; |
| 4159 | startBuf++; |
| 4160 | OrigLength++; |
| 4161 | } |
| 4162 | buf += ')'; |
| 4163 | OrigLength++; |
| 4164 | |
| 4165 | if (PointerTypeTakesAnyBlockArguments(DeclT) || |
| 4166 | PointerTypeTakesAnyObjCQualifiedType(DeclT)) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4167 | // Replace the '^' with '*' for arguments. |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4168 | // Replace id<P> with id/*<>*/ |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4169 | DeclLoc = ND->getLocation(); |
| 4170 | startBuf = SM->getCharacterData(DeclLoc); |
| 4171 | const char *argListBegin, *argListEnd; |
| 4172 | GetExtentOfArgList(startBuf, argListBegin, argListEnd); |
| 4173 | while (argListBegin < argListEnd) { |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4174 | if (*argListBegin == '^') |
| 4175 | buf += '*'; |
| 4176 | else if (*argListBegin == '<') { |
| 4177 | buf += "/*"; |
| 4178 | buf += *argListBegin++; |
| 4179 | OrigLength++;; |
| 4180 | while (*argListBegin != '>') { |
| 4181 | buf += *argListBegin++; |
| 4182 | OrigLength++; |
| 4183 | } |
| 4184 | buf += *argListBegin; |
| 4185 | buf += "*/"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4186 | } |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4187 | else |
| 4188 | buf += *argListBegin; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4189 | argListBegin++; |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4190 | OrigLength++; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4191 | } |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4192 | buf += ')'; |
| 4193 | OrigLength++; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4194 | } |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4195 | ReplaceText(Start, OrigLength, buf); |
| 4196 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4197 | return; |
| 4198 | } |
| 4199 | |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4200 | |
| 4201 | /// SynthesizeByrefCopyDestroyHelper - This routine synthesizes: |
| 4202 | /// void __Block_byref_id_object_copy(struct Block_byref_id_object *dst, |
| 4203 | /// struct Block_byref_id_object *src) { |
| 4204 | /// _Block_object_assign (&_dest->object, _src->object, |
| 4205 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4206 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4207 | /// _Block_object_assign(&_dest->object, _src->object, |
| 4208 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4209 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4210 | /// } |
| 4211 | /// And: |
| 4212 | /// void __Block_byref_id_object_dispose(struct Block_byref_id_object *_src) { |
| 4213 | /// _Block_object_dispose(_src->object, |
| 4214 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4215 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4216 | /// _Block_object_dispose(_src->object, |
| 4217 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4218 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4219 | /// } |
| 4220 | |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4221 | std::string RewriteObjC::SynthesizeByrefCopyDestroyHelper(VarDecl *VD, |
| 4222 | int flag) { |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4223 | std::string S; |
Benjamin Kramer | 1211a71 | 2010-01-10 19:57:50 +0000 | [diff] [blame] | 4224 | if (CopyDestroyCache.count(flag)) |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4225 | return S; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4226 | CopyDestroyCache.insert(flag); |
| 4227 | S = "static void __Block_byref_id_object_copy_"; |
| 4228 | S += utostr(flag); |
| 4229 | S += "(void *dst, void *src) {\n"; |
| 4230 | |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4231 | // offset into the object pointer is computed as: |
| 4232 | // void * + void* + int + int + void* + void * |
| 4233 | unsigned IntSize = |
| 4234 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 4235 | unsigned VoidPtrSize = |
| 4236 | static_cast<unsigned>(Context->getTypeSize(Context->VoidPtrTy)); |
| 4237 | |
Ken Dyck | 0c4e5d6 | 2011-04-30 16:08:27 +0000 | [diff] [blame] | 4238 | unsigned offset = (VoidPtrSize*4 + IntSize + IntSize)/Context->getCharWidth(); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4239 | S += " _Block_object_assign((char*)dst + "; |
| 4240 | S += utostr(offset); |
| 4241 | S += ", *(void * *) ((char*)src + "; |
| 4242 | S += utostr(offset); |
| 4243 | S += "), "; |
| 4244 | S += utostr(flag); |
| 4245 | S += ");\n}\n"; |
| 4246 | |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4247 | S += "static void __Block_byref_id_object_dispose_"; |
| 4248 | S += utostr(flag); |
| 4249 | S += "(void *src) {\n"; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4250 | S += " _Block_object_dispose(*(void * *) ((char*)src + "; |
| 4251 | S += utostr(offset); |
| 4252 | S += "), "; |
| 4253 | S += utostr(flag); |
| 4254 | S += ");\n}\n"; |
| 4255 | return S; |
| 4256 | } |
| 4257 | |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4258 | /// RewriteByRefVar - For each __block typex ND variable this routine transforms |
| 4259 | /// the declaration into: |
| 4260 | /// struct __Block_byref_ND { |
| 4261 | /// void *__isa; // NULL for everything except __weak pointers |
| 4262 | /// struct __Block_byref_ND *__forwarding; |
| 4263 | /// int32_t __flags; |
| 4264 | /// int32_t __size; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4265 | /// void *__Block_byref_id_object_copy; // If variable is __block ObjC object |
| 4266 | /// void *__Block_byref_id_object_dispose; // If variable is __block ObjC object |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4267 | /// typex ND; |
| 4268 | /// }; |
| 4269 | /// |
| 4270 | /// It then replaces declaration of ND variable with: |
| 4271 | /// struct __Block_byref_ND ND = {__isa=0B, __forwarding=&ND, __flags=some_flag, |
| 4272 | /// __size=sizeof(struct __Block_byref_ND), |
| 4273 | /// ND=initializer-if-any}; |
| 4274 | /// |
| 4275 | /// |
| 4276 | void RewriteObjC::RewriteByRefVar(VarDecl *ND) { |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 4277 | // Insert declaration for the function in which block literal is |
| 4278 | // used. |
| 4279 | if (CurFunctionDeclToDeclareForBlock) |
| 4280 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4281 | int flag = 0; |
| 4282 | int isa = 0; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4283 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
Fariborz Jahanian | d64a4f4 | 2010-02-26 22:49:11 +0000 | [diff] [blame] | 4284 | if (DeclLoc.isInvalid()) |
| 4285 | // If type location is missing, it is because of missing type (a warning). |
| 4286 | // Use variable's location which is good for this case. |
| 4287 | DeclLoc = ND->getLocation(); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4288 | const char *startBuf = SM->getCharacterData(DeclLoc); |
Fariborz Jahanian | 6f0a0a9 | 2009-12-30 20:38:08 +0000 | [diff] [blame] | 4289 | SourceLocation X = ND->getLocEnd(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 4290 | X = SM->getExpansionLoc(X); |
Fariborz Jahanian | 6f0a0a9 | 2009-12-30 20:38:08 +0000 | [diff] [blame] | 4291 | const char *endBuf = SM->getCharacterData(X); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4292 | std::string Name(ND->getNameAsString()); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4293 | std::string ByrefType; |
Fariborz Jahanian | 1e8011e | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 4294 | RewriteByRefString(ByrefType, Name, ND, true); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4295 | ByrefType += " {\n"; |
| 4296 | ByrefType += " void *__isa;\n"; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4297 | RewriteByRefString(ByrefType, Name, ND); |
| 4298 | ByrefType += " *__forwarding;\n"; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4299 | ByrefType += " int __flags;\n"; |
| 4300 | ByrefType += " int __size;\n"; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4301 | // Add void *__Block_byref_id_object_copy; |
| 4302 | // void *__Block_byref_id_object_dispose; if needed. |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4303 | QualType Ty = ND->getType(); |
| 4304 | bool HasCopyAndDispose = Context->BlockRequiresCopying(Ty); |
| 4305 | if (HasCopyAndDispose) { |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4306 | ByrefType += " void (*__Block_byref_id_object_copy)(void*, void*);\n"; |
| 4307 | ByrefType += " void (*__Block_byref_id_object_dispose)(void*);\n"; |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4308 | } |
Fariborz Jahanian | 822ac87 | 2011-03-31 22:49:32 +0000 | [diff] [blame] | 4309 | |
| 4310 | QualType T = Ty; |
| 4311 | (void)convertBlockPointerToFunctionPointer(T); |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 4312 | T.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Fariborz Jahanian | 822ac87 | 2011-03-31 22:49:32 +0000 | [diff] [blame] | 4313 | |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4314 | ByrefType += " " + Name + ";\n"; |
| 4315 | ByrefType += "};\n"; |
| 4316 | // Insert this type in global scope. It is needed by helper function. |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4317 | SourceLocation FunLocStart; |
| 4318 | if (CurFunctionDef) |
| 4319 | FunLocStart = CurFunctionDef->getTypeSpecStartLoc(); |
| 4320 | else { |
| 4321 | assert(CurMethodDef && "RewriteByRefVar - CurMethodDef is null"); |
| 4322 | FunLocStart = CurMethodDef->getLocStart(); |
| 4323 | } |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4324 | InsertText(FunLocStart, ByrefType); |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4325 | if (Ty.isObjCGCWeak()) { |
| 4326 | flag |= BLOCK_FIELD_IS_WEAK; |
| 4327 | isa = 1; |
| 4328 | } |
| 4329 | |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4330 | if (HasCopyAndDispose) { |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4331 | flag = BLOCK_BYREF_CALLER; |
| 4332 | QualType Ty = ND->getType(); |
| 4333 | // FIXME. Handle __weak variable (BLOCK_FIELD_IS_WEAK) as well. |
| 4334 | if (Ty->isBlockPointerType()) |
| 4335 | flag |= BLOCK_FIELD_IS_BLOCK; |
| 4336 | else |
| 4337 | flag |= BLOCK_FIELD_IS_OBJECT; |
| 4338 | std::string HF = SynthesizeByrefCopyDestroyHelper(ND, flag); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4339 | if (!HF.empty()) |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4340 | InsertText(FunLocStart, HF); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4341 | } |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4342 | |
| 4343 | // struct __Block_byref_ND ND = |
| 4344 | // {0, &ND, some_flag, __size=sizeof(struct __Block_byref_ND), |
| 4345 | // initializer-if-any}; |
| 4346 | bool hasInit = (ND->getInit() != 0); |
Fariborz Jahanian | e1f84f8 | 2010-01-05 18:15:57 +0000 | [diff] [blame] | 4347 | unsigned flags = 0; |
| 4348 | if (HasCopyAndDispose) |
| 4349 | flags |= BLOCK_HAS_COPY_DISPOSE; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4350 | Name = ND->getNameAsString(); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4351 | ByrefType.clear(); |
| 4352 | RewriteByRefString(ByrefType, Name, ND); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4353 | std::string ForwardingCastType("("); |
| 4354 | ForwardingCastType += ByrefType + " *)"; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4355 | if (!hasInit) { |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4356 | ByrefType += " " + Name + " = {(void*)"; |
| 4357 | ByrefType += utostr(isa); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4358 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4359 | ByrefType += utostr(flags); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4360 | ByrefType += ", "; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4361 | ByrefType += "sizeof("; |
| 4362 | RewriteByRefString(ByrefType, Name, ND); |
| 4363 | ByrefType += ")"; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4364 | if (HasCopyAndDispose) { |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4365 | ByrefType += ", __Block_byref_id_object_copy_"; |
| 4366 | ByrefType += utostr(flag); |
| 4367 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 4368 | ByrefType += utostr(flag); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4369 | } |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4370 | ByrefType += "};\n"; |
Fariborz Jahanian | 822ac87 | 2011-03-31 22:49:32 +0000 | [diff] [blame] | 4371 | unsigned nameSize = Name.size(); |
| 4372 | // for block or function pointer declaration. Name is aleady |
| 4373 | // part of the declaration. |
| 4374 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) |
| 4375 | nameSize = 1; |
| 4376 | ReplaceText(DeclLoc, endBuf-startBuf+nameSize, ByrefType); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4377 | } |
| 4378 | else { |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4379 | SourceLocation startLoc; |
| 4380 | Expr *E = ND->getInit(); |
| 4381 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 4382 | startLoc = ECE->getLParenLoc(); |
| 4383 | else |
| 4384 | startLoc = E->getLocStart(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 4385 | startLoc = SM->getExpansionLoc(startLoc); |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4386 | endBuf = SM->getCharacterData(startLoc); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4387 | ByrefType += " " + Name; |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4388 | ByrefType += " = {(void*)"; |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4389 | ByrefType += utostr(isa); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4390 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4391 | ByrefType += utostr(flags); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4392 | ByrefType += ", "; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4393 | ByrefType += "sizeof("; |
| 4394 | RewriteByRefString(ByrefType, Name, ND); |
| 4395 | ByrefType += "), "; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4396 | if (HasCopyAndDispose) { |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4397 | ByrefType += "__Block_byref_id_object_copy_"; |
| 4398 | ByrefType += utostr(flag); |
| 4399 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 4400 | ByrefType += utostr(flag); |
| 4401 | ByrefType += ", "; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4402 | } |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4403 | ReplaceText(DeclLoc, endBuf-startBuf, ByrefType); |
Steve Naroff | c5143c5 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 4404 | |
| 4405 | // Complete the newly synthesized compound expression by inserting a right |
| 4406 | // curly brace before the end of the declaration. |
| 4407 | // FIXME: This approach avoids rewriting the initializer expression. It |
| 4408 | // also assumes there is only one declarator. For example, the following |
| 4409 | // isn't currently supported by this routine (in general): |
| 4410 | // |
| 4411 | // double __block BYREFVAR = 1.34, BYREFVAR2 = 1.37; |
| 4412 | // |
Fariborz Jahanian | 5f371ee | 2010-07-21 17:36:39 +0000 | [diff] [blame] | 4413 | const char *startInitializerBuf = SM->getCharacterData(startLoc); |
| 4414 | const char *semiBuf = strchr(startInitializerBuf, ';'); |
Steve Naroff | c5143c5 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 4415 | assert((*semiBuf == ';') && "RewriteByRefVar: can't find ';'"); |
| 4416 | SourceLocation semiLoc = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4417 | startLoc.getLocWithOffset(semiBuf-startInitializerBuf); |
Steve Naroff | c5143c5 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 4418 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4419 | InsertText(semiLoc, "}"); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4420 | } |
Fariborz Jahanian | 1be6b46 | 2009-12-22 00:48:54 +0000 | [diff] [blame] | 4421 | return; |
| 4422 | } |
| 4423 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4424 | void RewriteObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4425 | // Add initializers for any closure decl refs. |
| 4426 | GetBlockDeclRefExprs(Exp->getBody()); |
| 4427 | if (BlockDeclRefs.size()) { |
| 4428 | // Unique all "by copy" declarations. |
| 4429 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 4430 | if (!BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>()) { |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4431 | if (!BlockByCopyDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 4432 | BlockByCopyDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 4433 | BlockByCopyDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 4434 | } |
| 4435 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4436 | // Unique all "by ref" declarations. |
| 4437 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 4438 | if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>()) { |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4439 | if (!BlockByRefDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 4440 | BlockByRefDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 4441 | BlockByRefDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 4442 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4443 | } |
| 4444 | // Find any imported blocks...they will need special attention. |
| 4445 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 4446 | if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>() || |
Fariborz Jahanian | 4fcc4fd | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 4447 | BlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
Fariborz Jahanian | 5b011b0 | 2010-02-26 21:46:27 +0000 | [diff] [blame] | 4448 | BlockDeclRefs[i]->getType()->isBlockPointerType()) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4449 | ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4450 | } |
| 4451 | } |
| 4452 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4453 | FunctionDecl *RewriteObjC::SynthBlockInitFunctionDecl(StringRef name) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4454 | IdentifierInfo *ID = &Context->Idents.get(name); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4455 | QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4456 | return FunctionDecl::Create(*Context, TUDecl, SourceLocation(), |
| 4457 | SourceLocation(), ID, FType, 0, SC_Extern, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4458 | SC_None, false, false); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4459 | } |
| 4460 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4461 | Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 4462 | const SmallVector<DeclRefExpr *, 8> &InnerBlockDeclRefs) { |
Fariborz Jahanian | 05318d8 | 2011-02-16 22:37:10 +0000 | [diff] [blame] | 4463 | const BlockDecl *block = Exp->getBlockDecl(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4464 | Blocks.push_back(Exp); |
| 4465 | |
| 4466 | CollectBlockDeclRefInfo(Exp); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4467 | |
| 4468 | // Add inner imported variables now used in current block. |
| 4469 | int countOfInnerDecls = 0; |
Fariborz Jahanian | 1276bfe | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4470 | if (!InnerBlockDeclRefs.empty()) { |
| 4471 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) { |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 4472 | DeclRefExpr *Exp = InnerBlockDeclRefs[i]; |
Fariborz Jahanian | 1276bfe | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4473 | ValueDecl *VD = Exp->getDecl(); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 4474 | if (!VD->hasAttr<BlocksAttr>() && !BlockByCopyDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4475 | // We need to save the copied-in variables in nested |
| 4476 | // blocks because it is needed at the end for some of the API generations. |
| 4477 | // See SynthesizeBlockLiterals routine. |
Fariborz Jahanian | 1276bfe | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4478 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 4479 | BlockDeclRefs.push_back(Exp); |
| 4480 | BlockByCopyDeclsPtrSet.insert(VD); |
| 4481 | BlockByCopyDecls.push_back(VD); |
| 4482 | } |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 4483 | if (VD->hasAttr<BlocksAttr>() && !BlockByRefDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 1276bfe | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4484 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 4485 | BlockDeclRefs.push_back(Exp); |
| 4486 | BlockByRefDeclsPtrSet.insert(VD); |
| 4487 | BlockByRefDecls.push_back(VD); |
| 4488 | } |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4489 | } |
Fariborz Jahanian | 1276bfe | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4490 | // Find any imported blocks...they will need special attention. |
| 4491 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 4492 | if (InnerBlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>() || |
Fariborz Jahanian | 1276bfe | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4493 | InnerBlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
| 4494 | InnerBlockDeclRefs[i]->getType()->isBlockPointerType()) |
| 4495 | ImportedBlockDecls.insert(InnerBlockDeclRefs[i]->getDecl()); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4496 | } |
| 4497 | InnerDeclRefsCount.push_back(countOfInnerDecls); |
| 4498 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4499 | std::string FuncName; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4500 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4501 | if (CurFunctionDef) |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4502 | FuncName = CurFunctionDef->getNameAsString(); |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 4503 | else if (CurMethodDef) |
| 4504 | BuildUniqueMethodName(FuncName, CurMethodDef); |
| 4505 | else if (GlobalVarDecl) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4506 | FuncName = std::string(GlobalVarDecl->getNameAsString()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4507 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4508 | std::string BlockNumber = utostr(Blocks.size()-1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4509 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4510 | std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber; |
| 4511 | std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4512 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4513 | // Get a pointer to the function type so we can cast appropriately. |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4514 | QualType BFT = convertFunctionTypeOfBlocks(Exp->getFunctionType()); |
| 4515 | QualType FType = Context->getPointerType(BFT); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4516 | |
| 4517 | FunctionDecl *FD; |
| 4518 | Expr *NewRep; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4519 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4520 | // Simulate a contructor call... |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4521 | FD = SynthBlockInitFunctionDecl(Tag); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 4522 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, false, FType, VK_RValue, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4523 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4524 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4525 | SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4526 | |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4527 | // Initialize the block function. |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4528 | FD = SynthBlockInitFunctionDecl(Func); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 4529 | DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, false, FD->getType(), |
| 4530 | VK_LValue, SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4531 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4532 | CK_BitCast, Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4533 | InitExprs.push_back(castExpr); |
| 4534 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4535 | // Initialize the block descriptor. |
| 4536 | std::string DescData = "__" + FuncName + "_block_desc_" + BlockNumber + "_DATA"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4537 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4538 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, |
| 4539 | SourceLocation(), SourceLocation(), |
| 4540 | &Context->Idents.get(DescData.c_str()), |
| 4541 | Context->VoidPtrTy, 0, |
| 4542 | SC_Static, SC_None); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4543 | UnaryOperator *DescRefExpr = |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 4544 | new (Context) UnaryOperator(new (Context) DeclRefExpr(NewVD, false, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4545 | Context->VoidPtrTy, |
| 4546 | VK_LValue, |
| 4547 | SourceLocation()), |
| 4548 | UO_AddrOf, |
| 4549 | Context->getPointerType(Context->VoidPtrTy), |
| 4550 | VK_RValue, OK_Ordinary, |
| 4551 | SourceLocation()); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4552 | InitExprs.push_back(DescRefExpr); |
| 4553 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4554 | // Add initializers for any closure decl refs. |
| 4555 | if (BlockDeclRefs.size()) { |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4556 | Expr *Exp; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4557 | // Output all "by copy" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4558 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4559 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4560 | if (isObjCType((*I)->getType())) { |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4561 | // FIXME: Conform to ABI ([[obj retain] autorelease]). |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4562 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 4563 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4564 | SourceLocation()); |
Fariborz Jahanian | a5a7987 | 2010-05-24 18:32:56 +0000 | [diff] [blame] | 4565 | if (HasLocalVariableExternalStorage(*I)) { |
| 4566 | QualType QT = (*I)->getType(); |
| 4567 | QT = Context->getPointerType(QT); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4568 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue, |
| 4569 | OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | a5a7987 | 2010-05-24 18:32:56 +0000 | [diff] [blame] | 4570 | } |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4571 | } else if (isTopLevelBlockPointerType((*I)->getType())) { |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4572 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 4573 | Arg = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4574 | SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4575 | Exp = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4576 | CK_BitCast, Arg); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4577 | } else { |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4578 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 4579 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4580 | SourceLocation()); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4581 | if (HasLocalVariableExternalStorage(*I)) { |
| 4582 | QualType QT = (*I)->getType(); |
| 4583 | QT = Context->getPointerType(QT); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4584 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue, |
| 4585 | OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4586 | } |
| 4587 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4588 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4589 | InitExprs.push_back(Exp); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4590 | } |
| 4591 | // Output all "by ref" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4592 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4593 | E = BlockByRefDecls.end(); I != E; ++I) { |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4594 | ValueDecl *ND = (*I); |
| 4595 | std::string Name(ND->getNameAsString()); |
| 4596 | std::string RecName; |
Fariborz Jahanian | 1e8011e | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 4597 | RewriteByRefString(RecName, Name, ND, true); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4598 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str() |
| 4599 | + sizeof("struct")); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4600 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4601 | SourceLocation(), SourceLocation(), |
| 4602 | II); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4603 | assert(RD && "SynthBlockInitExpr(): Can't find RecordDecl"); |
| 4604 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 4605 | |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4606 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 4607 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4608 | SourceLocation()); |
Fariborz Jahanian | 05318d8 | 2011-02-16 22:37:10 +0000 | [diff] [blame] | 4609 | bool isNestedCapturedVar = false; |
| 4610 | if (block) |
| 4611 | for (BlockDecl::capture_const_iterator ci = block->capture_begin(), |
| 4612 | ce = block->capture_end(); ci != ce; ++ci) { |
| 4613 | const VarDecl *variable = ci->getVariable(); |
| 4614 | if (variable == ND && ci->isNested()) { |
| 4615 | assert (ci->isByRef() && |
| 4616 | "SynthBlockInitExpr - captured block variable is not byref"); |
| 4617 | isNestedCapturedVar = true; |
| 4618 | break; |
| 4619 | } |
| 4620 | } |
| 4621 | // captured nested byref variable has its address passed. Do not take |
| 4622 | // its address again. |
| 4623 | if (!isNestedCapturedVar) |
| 4624 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4625 | Context->getPointerType(Exp->getType()), |
| 4626 | VK_RValue, OK_Ordinary, SourceLocation()); |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4627 | Exp = NoTypeInfoCStyleCastExpr(Context, castT, CK_BitCast, Exp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4628 | InitExprs.push_back(Exp); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4629 | } |
| 4630 | } |
Fariborz Jahanian | ff12788 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 4631 | if (ImportedBlockDecls.size()) { |
| 4632 | // generate BLOCK_HAS_COPY_DISPOSE(have helper funcs) | BLOCK_HAS_DESCRIPTOR |
| 4633 | int flag = (BLOCK_HAS_COPY_DISPOSE | BLOCK_HAS_DESCRIPTOR); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4634 | unsigned IntSize = |
| 4635 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 4636 | Expr *FlagExp = IntegerLiteral::Create(*Context, llvm::APInt(IntSize, flag), |
| 4637 | Context->IntTy, SourceLocation()); |
Fariborz Jahanian | ff12788 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 4638 | InitExprs.push_back(FlagExp); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4639 | } |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 4640 | NewRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], InitExprs.size(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4641 | FType, VK_LValue, SourceLocation()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4642 | NewRep = new (Context) UnaryOperator(NewRep, UO_AddrOf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4643 | Context->getPointerType(NewRep->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4644 | VK_RValue, OK_Ordinary, SourceLocation()); |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4645 | NewRep = NoTypeInfoCStyleCastExpr(Context, FType, CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4646 | NewRep); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4647 | BlockDeclRefs.clear(); |
| 4648 | BlockByRefDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4649 | BlockByRefDeclsPtrSet.clear(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4650 | BlockByCopyDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4651 | BlockByCopyDeclsPtrSet.clear(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4652 | ImportedBlockDecls.clear(); |
| 4653 | return NewRep; |
| 4654 | } |
| 4655 | |
Fariborz Jahanian | 42f1e65 | 2011-02-24 21:29:21 +0000 | [diff] [blame] | 4656 | bool RewriteObjC::IsDeclStmtInForeachHeader(DeclStmt *DS) { |
| 4657 | if (const ObjCForCollectionStmt * CS = |
| 4658 | dyn_cast<ObjCForCollectionStmt>(Stmts.back())) |
| 4659 | return CS->getElement() == DS; |
| 4660 | return false; |
| 4661 | } |
| 4662 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4663 | //===----------------------------------------------------------------------===// |
| 4664 | // Function Body / Expression rewriting |
| 4665 | //===----------------------------------------------------------------------===// |
| 4666 | |
| 4667 | Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4668 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4669 | isa<DoStmt>(S) || isa<ForStmt>(S)) |
| 4670 | Stmts.push_back(S); |
| 4671 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 4672 | Stmts.push_back(S); |
Chris Lattner | 4824fcd | 2010-01-09 21:45:57 +0000 | [diff] [blame] | 4673 | ObjCBcLabelNo.push_back(++BcLabelCount); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4674 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4675 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4676 | // Pseudo-object operations and ivar references need special |
| 4677 | // treatment because we're going to recursively rewrite them. |
| 4678 | if (PseudoObjectExpr *PseudoOp = dyn_cast<PseudoObjectExpr>(S)) { |
| 4679 | if (isa<BinaryOperator>(PseudoOp->getSyntacticForm())) { |
| 4680 | return RewritePropertyOrImplicitSetter(PseudoOp); |
| 4681 | } else { |
| 4682 | return RewritePropertyOrImplicitGetter(PseudoOp); |
| 4683 | } |
| 4684 | } else if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) { |
| 4685 | return RewriteObjCIvarRefExpr(IvarRefExpr); |
| 4686 | } |
| 4687 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4688 | SourceRange OrigStmtRange = S->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4689 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4690 | // Perform a bottom up rewrite of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 4691 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4692 | if (*CI) { |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4693 | Stmt *childStmt = (*CI); |
| 4694 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(childStmt); |
Fariborz Jahanian | 1d01531 | 2011-04-11 21:17:02 +0000 | [diff] [blame] | 4695 | if (newStmt) { |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4696 | *CI = newStmt; |
Fariborz Jahanian | 1d01531 | 2011-04-11 21:17:02 +0000 | [diff] [blame] | 4697 | } |
Nick Lewycky | 7e74924 | 2010-10-31 21:07:24 +0000 | [diff] [blame] | 4698 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4699 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4700 | if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame^] | 4701 | SmallVector<DeclRefExpr *, 8> InnerBlockDeclRefs; |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4702 | llvm::SmallPtrSet<const DeclContext *, 8> InnerContexts; |
| 4703 | InnerContexts.insert(BE->getBlockDecl()); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4704 | ImportedLocalExternalDecls.clear(); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4705 | GetInnerBlockDeclRefExprs(BE->getBody(), |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4706 | InnerBlockDeclRefs, InnerContexts); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4707 | // Rewrite the block body in place. |
Fariborz Jahanian | da4ad9f | 2010-11-08 18:37:50 +0000 | [diff] [blame] | 4708 | Stmt *SaveCurrentBody = CurrentBody; |
| 4709 | CurrentBody = BE->getBody(); |
| 4710 | PropParentMap = 0; |
Fariborz Jahanian | f23a0ff | 2011-08-02 20:28:46 +0000 | [diff] [blame] | 4711 | // block literal on rhs of a property-dot-sytax assignment |
| 4712 | // must be replaced by its synthesize ast so getRewrittenText |
| 4713 | // works as expected. In this case, what actually ends up on RHS |
| 4714 | // is the blockTranscribed which is the helper function for the |
| 4715 | // block literal; as in: self.c = ^() {[ace ARR];}; |
| 4716 | bool saveDisableReplaceStmt = DisableReplaceStmt; |
| 4717 | DisableReplaceStmt = false; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4718 | RewriteFunctionBodyOrGlobalInitializer(BE->getBody()); |
Fariborz Jahanian | f23a0ff | 2011-08-02 20:28:46 +0000 | [diff] [blame] | 4719 | DisableReplaceStmt = saveDisableReplaceStmt; |
Fariborz Jahanian | da4ad9f | 2010-11-08 18:37:50 +0000 | [diff] [blame] | 4720 | CurrentBody = SaveCurrentBody; |
| 4721 | PropParentMap = 0; |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4722 | ImportedLocalExternalDecls.clear(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4723 | // 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] | 4724 | std::string Str = Rewrite.getRewrittenText(BE->getSourceRange()); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4725 | RewrittenBlockExprs[BE] = Str; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4726 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4727 | Stmt *blockTranscribed = SynthBlockInitExpr(BE, InnerBlockDeclRefs); |
| 4728 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4729 | //blockTranscribed->dump(); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4730 | ReplaceStmt(S, blockTranscribed); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4731 | return blockTranscribed; |
| 4732 | } |
| 4733 | // Handle specific things. |
| 4734 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 4735 | return RewriteAtEncode(AtEncode); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4736 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4737 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 4738 | return RewriteAtSelector(AtSelector); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4739 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4740 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 4741 | return RewriteObjCStringLiteral(AtString); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4742 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4743 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4744 | #if 0 |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4745 | // Before we rewrite it, put the original message expression in a comment. |
| 4746 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 4747 | SourceLocation endLoc = MessExpr->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4748 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4749 | const char *startBuf = SM->getCharacterData(startLoc); |
| 4750 | const char *endBuf = SM->getCharacterData(endLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4751 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4752 | std::string messString; |
| 4753 | messString += "// "; |
| 4754 | messString.append(startBuf, endBuf-startBuf+1); |
| 4755 | messString += "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4756 | |
| 4757 | // FIXME: Missing definition of |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4758 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
| 4759 | // InsertText(startLoc, messString.c_str(), messString.size()); |
| 4760 | // Tried this, but it didn't work either... |
| 4761 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4762 | #endif |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4763 | return RewriteMessageExpr(MessExpr); |
| 4764 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4765 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4766 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 4767 | return RewriteObjCTryStmt(StmtTry); |
| 4768 | |
| 4769 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 4770 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 4771 | |
| 4772 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 4773 | return RewriteObjCThrowStmt(StmtThrow); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4774 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4775 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 4776 | return RewriteObjCProtocolExpr(ProtocolExp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4777 | |
| 4778 | if (ObjCForCollectionStmt *StmtForCollection = |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4779 | dyn_cast<ObjCForCollectionStmt>(S)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4780 | return RewriteObjCForCollectionStmt(StmtForCollection, |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4781 | OrigStmtRange.getEnd()); |
| 4782 | if (BreakStmt *StmtBreakStmt = |
| 4783 | dyn_cast<BreakStmt>(S)) |
| 4784 | return RewriteBreakStmt(StmtBreakStmt); |
| 4785 | if (ContinueStmt *StmtContinueStmt = |
| 4786 | dyn_cast<ContinueStmt>(S)) |
| 4787 | return RewriteContinueStmt(StmtContinueStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4788 | |
| 4789 | // 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] | 4790 | // and cast exprs. |
| 4791 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
| 4792 | // FIXME: What we're doing here is modifying the type-specifier that |
| 4793 | // precedes the first Decl. In the future the DeclGroup should have |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4794 | // a separate type-specifier that we can rewrite. |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4795 | // NOTE: We need to avoid rewriting the DeclStmt if it is within |
| 4796 | // the context of an ObjCForCollectionStmt. For example: |
| 4797 | // NSArray *someArray; |
| 4798 | // for (id <FooProtocol> index in someArray) ; |
| 4799 | // This is because RewriteObjCForCollectionStmt() does textual rewriting |
| 4800 | // and it depends on the original text locations/positions. |
Fariborz Jahanian | 42f1e65 | 2011-02-24 21:29:21 +0000 | [diff] [blame] | 4801 | if (Stmts.empty() || !IsDeclStmtInForeachHeader(DS)) |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4802 | RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4803 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4804 | // Blocks rewrite rules. |
| 4805 | for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); |
| 4806 | DI != DE; ++DI) { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 4807 | Decl *SD = *DI; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4808 | if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4809 | if (isTopLevelBlockPointerType(ND->getType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4810 | RewriteBlockPointerDecl(ND); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4811 | else if (ND->getType()->isFunctionPointerType()) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4812 | CheckFunctionPointerDecl(ND->getType(), ND); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 4813 | if (VarDecl *VD = dyn_cast<VarDecl>(SD)) { |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4814 | if (VD->hasAttr<BlocksAttr>()) { |
| 4815 | static unsigned uniqueByrefDeclCount = 0; |
| 4816 | assert(!BlockByRefDeclNo.count(ND) && |
| 4817 | "RewriteFunctionBodyOrGlobalInitializer: Duplicate byref decl"); |
| 4818 | BlockByRefDeclNo[ND] = uniqueByrefDeclCount++; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4819 | RewriteByRefVar(VD); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4820 | } |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 4821 | else |
| 4822 | RewriteTypeOfDecl(VD); |
| 4823 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4824 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4825 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4826 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4827 | RewriteBlockPointerDecl(TD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4828 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4829 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 4830 | } |
| 4831 | } |
| 4832 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4833 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4834 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) |
| 4835 | RewriteObjCQualifiedInterfaceTypes(CE); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4836 | |
| 4837 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4838 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 4839 | assert(!Stmts.empty() && "Statement stack is empty"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4840 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 4841 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4842 | && "Statement stack mismatch"); |
| 4843 | Stmts.pop_back(); |
| 4844 | } |
| 4845 | // Handle blocks rewriting. |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4846 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 4847 | ValueDecl *VD = DRE->getDecl(); |
| 4848 | if (VD->hasAttr<BlocksAttr>()) |
| 4849 | return RewriteBlockDeclRefExpr(DRE); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4850 | if (HasLocalVariableExternalStorage(VD)) |
| 4851 | return RewriteLocalVariableExternalStorage(DRE); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4852 | } |
| 4853 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4854 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4855 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4856 | Stmt *BlockCall = SynthesizeBlockCall(CE, CE->getCallee()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4857 | ReplaceStmt(S, BlockCall); |
| 4858 | return BlockCall; |
| 4859 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4860 | } |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4861 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4862 | RewriteCastExpr(CE); |
| 4863 | } |
| 4864 | #if 0 |
| 4865 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4866 | CastExpr *Replacement = new (Context) CastExpr(ICE->getType(), |
| 4867 | ICE->getSubExpr(), |
| 4868 | SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4869 | // Get the new text. |
| 4870 | std::string SStr; |
| 4871 | llvm::raw_string_ostream Buf(SStr); |
Eli Friedman | 3a9eb44 | 2009-05-30 05:19:26 +0000 | [diff] [blame] | 4872 | Replacement->printPretty(Buf, *Context); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4873 | const std::string &Str = Buf.str(); |
| 4874 | |
| 4875 | printf("CAST = %s\n", &Str[0]); |
| 4876 | InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 4877 | delete S; |
| 4878 | return Replacement; |
| 4879 | } |
| 4880 | #endif |
| 4881 | // Return this stmt unmodified. |
| 4882 | return S; |
| 4883 | } |
| 4884 | |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4885 | void RewriteObjC::RewriteRecordBody(RecordDecl *RD) { |
| 4886 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 4887 | e = RD->field_end(); i != e; ++i) { |
| 4888 | FieldDecl *FD = *i; |
| 4889 | if (isTopLevelBlockPointerType(FD->getType())) |
| 4890 | RewriteBlockPointerDecl(FD); |
| 4891 | if (FD->getType()->isObjCQualifiedIdType() || |
| 4892 | FD->getType()->isObjCQualifiedInterfaceType()) |
| 4893 | RewriteObjCQualifiedInterfaceTypes(FD); |
| 4894 | } |
| 4895 | } |
| 4896 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4897 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 4898 | /// main file of the input. |
| 4899 | void RewriteObjC::HandleDeclInMainFile(Decl *D) { |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4900 | switch (D->getKind()) { |
| 4901 | case Decl::Function: { |
| 4902 | FunctionDecl *FD = cast<FunctionDecl>(D); |
| 4903 | if (FD->isOverloadedOperator()) |
| 4904 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4905 | |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4906 | // Since function prototypes don't have ParmDecl's, we check the function |
| 4907 | // prototype. This enables us to rewrite function declarations and |
| 4908 | // definitions using the same code. |
| 4909 | RewriteBlocksInFunctionProtoType(FD->getType(), FD); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4910 | |
Argyrios Kyrtzidis | 9335df3 | 2012-02-12 04:48:45 +0000 | [diff] [blame] | 4911 | if (!FD->isThisDeclarationADefinition()) |
| 4912 | break; |
| 4913 | |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4914 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
| 4915 | if (CompoundStmt *Body = dyn_cast_or_null<CompoundStmt>(FD->getBody())) { |
| 4916 | CurFunctionDef = FD; |
| 4917 | CurFunctionDeclToDeclareForBlock = FD; |
| 4918 | CurrentBody = Body; |
| 4919 | Body = |
| 4920 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 4921 | FD->setBody(Body); |
| 4922 | CurrentBody = 0; |
| 4923 | if (PropParentMap) { |
| 4924 | delete PropParentMap; |
| 4925 | PropParentMap = 0; |
| 4926 | } |
| 4927 | // This synthesizes and inserts the block "impl" struct, invoke function, |
| 4928 | // and any copy/dispose helper functions. |
| 4929 | InsertBlockLiteralsWithinFunction(FD); |
| 4930 | CurFunctionDef = 0; |
| 4931 | CurFunctionDeclToDeclareForBlock = 0; |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4932 | } |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4933 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4934 | } |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4935 | case Decl::ObjCMethod: { |
| 4936 | ObjCMethodDecl *MD = cast<ObjCMethodDecl>(D); |
| 4937 | if (CompoundStmt *Body = MD->getCompoundBody()) { |
| 4938 | CurMethodDef = MD; |
| 4939 | CurrentBody = Body; |
| 4940 | Body = |
| 4941 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 4942 | MD->setBody(Body); |
| 4943 | CurrentBody = 0; |
| 4944 | if (PropParentMap) { |
| 4945 | delete PropParentMap; |
| 4946 | PropParentMap = 0; |
| 4947 | } |
| 4948 | InsertBlockLiteralsWithinMethod(MD); |
| 4949 | CurMethodDef = 0; |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4950 | } |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4951 | break; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4952 | } |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4953 | case Decl::ObjCImplementation: { |
| 4954 | ObjCImplementationDecl *CI = cast<ObjCImplementationDecl>(D); |
| 4955 | ClassImplementation.push_back(CI); |
| 4956 | break; |
| 4957 | } |
| 4958 | case Decl::ObjCCategoryImpl: { |
| 4959 | ObjCCategoryImplDecl *CI = cast<ObjCCategoryImplDecl>(D); |
| 4960 | CategoryImplementation.push_back(CI); |
| 4961 | break; |
| 4962 | } |
| 4963 | case Decl::Var: { |
| 4964 | VarDecl *VD = cast<VarDecl>(D); |
| 4965 | RewriteObjCQualifiedInterfaceTypes(VD); |
| 4966 | if (isTopLevelBlockPointerType(VD->getType())) |
| 4967 | RewriteBlockPointerDecl(VD); |
| 4968 | else if (VD->getType()->isFunctionPointerType()) { |
| 4969 | CheckFunctionPointerDecl(VD->getType(), VD); |
| 4970 | if (VD->getInit()) { |
| 4971 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
| 4972 | RewriteCastExpr(CE); |
| 4973 | } |
| 4974 | } |
| 4975 | } else if (VD->getType()->isRecordType()) { |
| 4976 | RecordDecl *RD = VD->getType()->getAs<RecordType>()->getDecl(); |
| 4977 | if (RD->isCompleteDefinition()) |
| 4978 | RewriteRecordBody(RD); |
| 4979 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4980 | if (VD->getInit()) { |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4981 | GlobalVarDecl = VD; |
| 4982 | CurrentBody = VD->getInit(); |
| 4983 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
| 4984 | CurrentBody = 0; |
| 4985 | if (PropParentMap) { |
| 4986 | delete PropParentMap; |
| 4987 | PropParentMap = 0; |
| 4988 | } |
| 4989 | SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), VD->getName()); |
| 4990 | GlobalVarDecl = 0; |
| 4991 | |
| 4992 | // This is needed for blocks. |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4993 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4994 | RewriteCastExpr(CE); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4995 | } |
| 4996 | } |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4997 | break; |
| 4998 | } |
| 4999 | case Decl::TypeAlias: |
| 5000 | case Decl::Typedef: { |
| 5001 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
| 5002 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
| 5003 | RewriteBlockPointerDecl(TD); |
| 5004 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
| 5005 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 5006 | } |
| 5007 | break; |
| 5008 | } |
| 5009 | case Decl::CXXRecord: |
| 5010 | case Decl::Record: { |
| 5011 | RecordDecl *RD = cast<RecordDecl>(D); |
| 5012 | if (RD->isCompleteDefinition()) |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5013 | RewriteRecordBody(RD); |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 5014 | break; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5015 | } |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 5016 | default: |
| 5017 | break; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5018 | } |
| 5019 | // Nothing yet. |
| 5020 | } |
| 5021 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 5022 | void RewriteObjC::HandleTranslationUnit(ASTContext &C) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5023 | if (Diags.hasErrorOccurred()) |
| 5024 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5025 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5026 | RewriteInclude(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5027 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5028 | // Here's a great place to add any extra declarations that may be needed. |
| 5029 | // Write out meta data for each @protocol(<expr>). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5030 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5031 | E = ProtocolExprDecls.end(); I != E; ++I) |
| 5032 | RewriteObjCProtocolMetaData(*I, "", "", Preamble); |
| 5033 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5034 | InsertText(SM->getLocForStartOfFile(MainFileID), Preamble, false); |
Steve Naroff | 0aab796 | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5035 | if (ClassImplementation.size() || CategoryImplementation.size()) |
| 5036 | RewriteImplementations(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5037 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5038 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 5039 | // we are done. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5040 | if (const RewriteBuffer *RewriteBuf = |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5041 | Rewrite.getRewriteBufferFor(MainFileID)) { |
| 5042 | //printf("Changed:\n"); |
| 5043 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 5044 | } else { |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5045 | llvm::errs() << "No changes\n"; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5046 | } |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 5047 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5048 | if (ClassImplementation.size() || CategoryImplementation.size() || |
| 5049 | ProtocolExprDecls.size()) { |
Steve Naroff | 0aab796 | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5050 | // Rewrite Objective-c meta data* |
| 5051 | std::string ResultStr; |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 5052 | RewriteMetaDataIntoBuffer(ResultStr); |
Steve Naroff | 0aab796 | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5053 | // Emit metadata. |
| 5054 | *OutFile << ResultStr; |
| 5055 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5056 | OutFile->flush(); |
| 5057 | } |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5058 | |
| 5059 | void RewriteObjCFragileABI::Initialize(ASTContext &context) { |
| 5060 | InitializeCommon(context); |
| 5061 | |
| 5062 | // declaring objc_selector outside the parameter list removes a silly |
| 5063 | // scope related warning... |
| 5064 | if (IsHeader) |
| 5065 | Preamble = "#pragma once\n"; |
| 5066 | Preamble += "struct objc_selector; struct objc_class;\n"; |
| 5067 | Preamble += "struct __rw_objc_super { struct objc_object *object; "; |
| 5068 | Preamble += "struct objc_object *superClass; "; |
| 5069 | if (LangOpts.MicrosoftExt) { |
| 5070 | // Add a constructor for creating temporary objects. |
| 5071 | Preamble += "__rw_objc_super(struct objc_object *o, struct objc_object *s) " |
| 5072 | ": "; |
| 5073 | Preamble += "object(o), superClass(s) {} "; |
| 5074 | } |
| 5075 | Preamble += "};\n"; |
| 5076 | Preamble += "#ifndef _REWRITER_typedef_Protocol\n"; |
| 5077 | Preamble += "typedef struct objc_object Protocol;\n"; |
| 5078 | Preamble += "#define _REWRITER_typedef_Protocol\n"; |
| 5079 | Preamble += "#endif\n"; |
| 5080 | if (LangOpts.MicrosoftExt) { |
| 5081 | Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n"; |
| 5082 | Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n"; |
| 5083 | } else |
| 5084 | Preamble += "#define __OBJC_RW_DLLIMPORT extern\n"; |
| 5085 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend"; |
| 5086 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
| 5087 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper"; |
| 5088 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
| 5089 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object* objc_msgSend_stret"; |
| 5090 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
| 5091 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object* objc_msgSendSuper_stret"; |
| 5092 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
| 5093 | Preamble += "__OBJC_RW_DLLIMPORT double objc_msgSend_fpret"; |
| 5094 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
| 5095 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getClass"; |
| 5096 | Preamble += "(const char *);\n"; |
| 5097 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_class *class_getSuperclass"; |
| 5098 | Preamble += "(struct objc_class *);\n"; |
| 5099 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getMetaClass"; |
| 5100 | Preamble += "(const char *);\n"; |
| 5101 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw(struct objc_object *);\n"; |
| 5102 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_enter(void *);\n"; |
| 5103 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_exit(void *);\n"; |
| 5104 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_exception_extract(void *);\n"; |
| 5105 | Preamble += "__OBJC_RW_DLLIMPORT int objc_exception_match"; |
| 5106 | Preamble += "(struct objc_class *, struct objc_object *);\n"; |
| 5107 | // @synchronized hooks. |
| 5108 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_enter(struct objc_object *);\n"; |
| 5109 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_exit(struct objc_object *);\n"; |
| 5110 | Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n"; |
| 5111 | Preamble += "#ifndef __FASTENUMERATIONSTATE\n"; |
| 5112 | Preamble += "struct __objcFastEnumerationState {\n\t"; |
| 5113 | Preamble += "unsigned long state;\n\t"; |
| 5114 | Preamble += "void **itemsPtr;\n\t"; |
| 5115 | Preamble += "unsigned long *mutationsPtr;\n\t"; |
| 5116 | Preamble += "unsigned long extra[5];\n};\n"; |
| 5117 | Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n"; |
| 5118 | Preamble += "#define __FASTENUMERATIONSTATE\n"; |
| 5119 | Preamble += "#endif\n"; |
| 5120 | Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n"; |
| 5121 | Preamble += "struct __NSConstantStringImpl {\n"; |
| 5122 | Preamble += " int *isa;\n"; |
| 5123 | Preamble += " int flags;\n"; |
| 5124 | Preamble += " char *str;\n"; |
| 5125 | Preamble += " long length;\n"; |
| 5126 | Preamble += "};\n"; |
| 5127 | Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n"; |
| 5128 | Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n"; |
| 5129 | Preamble += "#else\n"; |
| 5130 | Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n"; |
| 5131 | Preamble += "#endif\n"; |
| 5132 | Preamble += "#define __NSCONSTANTSTRINGIMPL\n"; |
| 5133 | Preamble += "#endif\n"; |
| 5134 | // Blocks preamble. |
| 5135 | Preamble += "#ifndef BLOCK_IMPL\n"; |
| 5136 | Preamble += "#define BLOCK_IMPL\n"; |
| 5137 | Preamble += "struct __block_impl {\n"; |
| 5138 | Preamble += " void *isa;\n"; |
| 5139 | Preamble += " int Flags;\n"; |
| 5140 | Preamble += " int Reserved;\n"; |
| 5141 | Preamble += " void *FuncPtr;\n"; |
| 5142 | Preamble += "};\n"; |
| 5143 | Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n"; |
| 5144 | Preamble += "#ifdef __OBJC_EXPORT_BLOCKS\n"; |
| 5145 | Preamble += "extern \"C\" __declspec(dllexport) " |
| 5146 | "void _Block_object_assign(void *, const void *, const int);\n"; |
| 5147 | Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_dispose(const void *, const int);\n"; |
| 5148 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteGlobalBlock[32];\n"; |
| 5149 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteStackBlock[32];\n"; |
| 5150 | Preamble += "#else\n"; |
| 5151 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_assign(void *, const void *, const int);\n"; |
| 5152 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_dispose(const void *, const int);\n"; |
| 5153 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteGlobalBlock[32];\n"; |
| 5154 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteStackBlock[32];\n"; |
| 5155 | Preamble += "#endif\n"; |
| 5156 | Preamble += "#endif\n"; |
| 5157 | if (LangOpts.MicrosoftExt) { |
| 5158 | Preamble += "#undef __OBJC_RW_DLLIMPORT\n"; |
| 5159 | Preamble += "#undef __OBJC_RW_STATICIMPORT\n"; |
| 5160 | Preamble += "#ifndef KEEP_ATTRIBUTES\n"; // We use this for clang tests. |
| 5161 | Preamble += "#define __attribute__(X)\n"; |
| 5162 | Preamble += "#endif\n"; |
| 5163 | Preamble += "#define __weak\n"; |
| 5164 | } |
| 5165 | else { |
| 5166 | Preamble += "#define __block\n"; |
| 5167 | Preamble += "#define __weak\n"; |
| 5168 | } |
| 5169 | // NOTE! Windows uses LLP64 for 64bit mode. So, cast pointer to long long |
| 5170 | // as this avoids warning in any 64bit/32bit compilation model. |
| 5171 | Preamble += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((long long) &((TYPE *)0)->MEMBER)\n"; |
| 5172 | } |
| 5173 | |
| 5174 | /// RewriteIvarOffsetComputation - This rutine synthesizes computation of |
| 5175 | /// ivar offset. |
| 5176 | void RewriteObjCFragileABI::RewriteIvarOffsetComputation(ObjCIvarDecl *ivar, |
| 5177 | std::string &Result) { |
| 5178 | if (ivar->isBitField()) { |
| 5179 | // FIXME: The hack below doesn't work for bitfields. For now, we simply |
| 5180 | // place all bitfields at offset 0. |
| 5181 | Result += "0"; |
| 5182 | } else { |
| 5183 | Result += "__OFFSETOFIVAR__(struct "; |
| 5184 | Result += ivar->getContainingInterface()->getNameAsString(); |
| 5185 | if (LangOpts.MicrosoftExt) |
| 5186 | Result += "_IMPL"; |
| 5187 | Result += ", "; |
| 5188 | Result += ivar->getNameAsString(); |
| 5189 | Result += ")"; |
| 5190 | } |
| 5191 | } |
| 5192 | |
| 5193 | /// RewriteObjCProtocolMetaData - Rewrite protocols meta-data. |
| 5194 | void RewriteObjCFragileABI::RewriteObjCProtocolMetaData( |
| 5195 | ObjCProtocolDecl *PDecl, StringRef prefix, |
| 5196 | StringRef ClassName, std::string &Result) { |
| 5197 | static bool objc_protocol_methods = false; |
| 5198 | |
| 5199 | // Output struct protocol_methods holder of method selector and type. |
Douglas Gregor | 5e2a1ff | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 5200 | if (!objc_protocol_methods && PDecl->hasDefinition()) { |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5201 | /* struct protocol_methods { |
| 5202 | SEL _cmd; |
| 5203 | char *method_types; |
| 5204 | } |
| 5205 | */ |
| 5206 | Result += "\nstruct _protocol_methods {\n"; |
| 5207 | Result += "\tstruct objc_selector *_cmd;\n"; |
| 5208 | Result += "\tchar *method_types;\n"; |
| 5209 | Result += "};\n"; |
| 5210 | |
| 5211 | objc_protocol_methods = true; |
| 5212 | } |
| 5213 | // Do not synthesize the protocol more than once. |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 5214 | if (ObjCSynthesizedProtocols.count(PDecl->getCanonicalDecl())) |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5215 | return; |
| 5216 | |
Douglas Gregor | 61cc296 | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 5217 | if (ObjCProtocolDecl *Def = PDecl->getDefinition()) |
| 5218 | PDecl = Def; |
| 5219 | |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5220 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 5221 | unsigned NumMethods = std::distance(PDecl->instmeth_begin(), |
| 5222 | PDecl->instmeth_end()); |
| 5223 | /* struct _objc_protocol_method_list { |
| 5224 | int protocol_method_count; |
| 5225 | struct protocol_methods protocols[]; |
| 5226 | } |
| 5227 | */ |
| 5228 | Result += "\nstatic struct {\n"; |
| 5229 | Result += "\tint protocol_method_count;\n"; |
| 5230 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 5231 | Result += utostr(NumMethods); |
| 5232 | Result += "];\n} _OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 5233 | Result += PDecl->getNameAsString(); |
| 5234 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= " |
| 5235 | "{\n\t" + utostr(NumMethods) + "\n"; |
| 5236 | |
| 5237 | // Output instance methods declared in this protocol. |
| 5238 | for (ObjCProtocolDecl::instmeth_iterator |
| 5239 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
| 5240 | I != E; ++I) { |
| 5241 | if (I == PDecl->instmeth_begin()) |
| 5242 | Result += "\t ,{{(struct objc_selector *)\""; |
| 5243 | else |
| 5244 | Result += "\t ,{(struct objc_selector *)\""; |
| 5245 | Result += (*I)->getSelector().getAsString(); |
| 5246 | std::string MethodTypeString; |
| 5247 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 5248 | Result += "\", \""; |
| 5249 | Result += MethodTypeString; |
| 5250 | Result += "\"}\n"; |
| 5251 | } |
| 5252 | Result += "\t }\n};\n"; |
| 5253 | } |
| 5254 | |
| 5255 | // Output class methods declared in this protocol. |
| 5256 | unsigned NumMethods = std::distance(PDecl->classmeth_begin(), |
| 5257 | PDecl->classmeth_end()); |
| 5258 | if (NumMethods > 0) { |
| 5259 | /* struct _objc_protocol_method_list { |
| 5260 | int protocol_method_count; |
| 5261 | struct protocol_methods protocols[]; |
| 5262 | } |
| 5263 | */ |
| 5264 | Result += "\nstatic struct {\n"; |
| 5265 | Result += "\tint protocol_method_count;\n"; |
| 5266 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 5267 | Result += utostr(NumMethods); |
| 5268 | Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_"; |
| 5269 | Result += PDecl->getNameAsString(); |
| 5270 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 5271 | "{\n\t"; |
| 5272 | Result += utostr(NumMethods); |
| 5273 | Result += "\n"; |
| 5274 | |
| 5275 | // Output instance methods declared in this protocol. |
| 5276 | for (ObjCProtocolDecl::classmeth_iterator |
| 5277 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
| 5278 | I != E; ++I) { |
| 5279 | if (I == PDecl->classmeth_begin()) |
| 5280 | Result += "\t ,{{(struct objc_selector *)\""; |
| 5281 | else |
| 5282 | Result += "\t ,{(struct objc_selector *)\""; |
| 5283 | Result += (*I)->getSelector().getAsString(); |
| 5284 | std::string MethodTypeString; |
| 5285 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 5286 | Result += "\", \""; |
| 5287 | Result += MethodTypeString; |
| 5288 | Result += "\"}\n"; |
| 5289 | } |
| 5290 | Result += "\t }\n};\n"; |
| 5291 | } |
| 5292 | |
| 5293 | // Output: |
| 5294 | /* struct _objc_protocol { |
| 5295 | // Objective-C 1.0 extensions |
| 5296 | struct _objc_protocol_extension *isa; |
| 5297 | char *protocol_name; |
| 5298 | struct _objc_protocol **protocol_list; |
| 5299 | struct _objc_protocol_method_list *instance_methods; |
| 5300 | struct _objc_protocol_method_list *class_methods; |
| 5301 | }; |
| 5302 | */ |
| 5303 | static bool objc_protocol = false; |
| 5304 | if (!objc_protocol) { |
| 5305 | Result += "\nstruct _objc_protocol {\n"; |
| 5306 | Result += "\tstruct _objc_protocol_extension *isa;\n"; |
| 5307 | Result += "\tchar *protocol_name;\n"; |
| 5308 | Result += "\tstruct _objc_protocol **protocol_list;\n"; |
| 5309 | Result += "\tstruct _objc_protocol_method_list *instance_methods;\n"; |
| 5310 | Result += "\tstruct _objc_protocol_method_list *class_methods;\n"; |
| 5311 | Result += "};\n"; |
| 5312 | |
| 5313 | objc_protocol = true; |
| 5314 | } |
| 5315 | |
| 5316 | Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_"; |
| 5317 | Result += PDecl->getNameAsString(); |
| 5318 | Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= " |
| 5319 | "{\n\t0, \""; |
| 5320 | Result += PDecl->getNameAsString(); |
| 5321 | Result += "\", 0, "; |
| 5322 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 5323 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 5324 | Result += PDecl->getNameAsString(); |
| 5325 | Result += ", "; |
| 5326 | } |
| 5327 | else |
| 5328 | Result += "0, "; |
| 5329 | if (PDecl->classmeth_begin() != PDecl->classmeth_end()) { |
| 5330 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
| 5331 | Result += PDecl->getNameAsString(); |
| 5332 | Result += "\n"; |
| 5333 | } |
| 5334 | else |
| 5335 | Result += "0\n"; |
| 5336 | Result += "};\n"; |
| 5337 | |
| 5338 | // Mark this protocol as having been generated. |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 5339 | if (!ObjCSynthesizedProtocols.insert(PDecl->getCanonicalDecl())) |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5340 | llvm_unreachable("protocol already synthesized"); |
| 5341 | |
| 5342 | } |
| 5343 | |
| 5344 | void RewriteObjCFragileABI::RewriteObjCProtocolListMetaData( |
| 5345 | const ObjCList<ObjCProtocolDecl> &Protocols, |
| 5346 | StringRef prefix, StringRef ClassName, |
| 5347 | std::string &Result) { |
| 5348 | if (Protocols.empty()) return; |
| 5349 | |
| 5350 | for (unsigned i = 0; i != Protocols.size(); i++) |
| 5351 | RewriteObjCProtocolMetaData(Protocols[i], prefix, ClassName, Result); |
| 5352 | |
| 5353 | // Output the top lovel protocol meta-data for the class. |
| 5354 | /* struct _objc_protocol_list { |
| 5355 | struct _objc_protocol_list *next; |
| 5356 | int protocol_count; |
| 5357 | struct _objc_protocol *class_protocols[]; |
| 5358 | } |
| 5359 | */ |
| 5360 | Result += "\nstatic struct {\n"; |
| 5361 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 5362 | Result += "\tint protocol_count;\n"; |
| 5363 | Result += "\tstruct _objc_protocol *class_protocols["; |
| 5364 | Result += utostr(Protocols.size()); |
| 5365 | Result += "];\n} _OBJC_"; |
| 5366 | Result += prefix; |
| 5367 | Result += "_PROTOCOLS_"; |
| 5368 | Result += ClassName; |
| 5369 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 5370 | "{\n\t0, "; |
| 5371 | Result += utostr(Protocols.size()); |
| 5372 | Result += "\n"; |
| 5373 | |
| 5374 | Result += "\t,{&_OBJC_PROTOCOL_"; |
| 5375 | Result += Protocols[0]->getNameAsString(); |
| 5376 | Result += " \n"; |
| 5377 | |
| 5378 | for (unsigned i = 1; i != Protocols.size(); i++) { |
| 5379 | Result += "\t ,&_OBJC_PROTOCOL_"; |
| 5380 | Result += Protocols[i]->getNameAsString(); |
| 5381 | Result += "\n"; |
| 5382 | } |
| 5383 | Result += "\t }\n};\n"; |
| 5384 | } |
| 5385 | |
| 5386 | void RewriteObjCFragileABI::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
| 5387 | std::string &Result) { |
| 5388 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
| 5389 | |
| 5390 | // Explicitly declared @interface's are already synthesized. |
| 5391 | if (CDecl->isImplicitInterfaceDecl()) { |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 5392 | // FIXME: Implementation of a class with no @interface (legacy) does not |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5393 | // produce correct synthesis as yet. |
| 5394 | RewriteObjCInternalStruct(CDecl, Result); |
| 5395 | } |
| 5396 | |
| 5397 | // Build _objc_ivar_list metadata for classes ivars if needed |
| 5398 | unsigned NumIvars = !IDecl->ivar_empty() |
| 5399 | ? IDecl->ivar_size() |
| 5400 | : (CDecl ? CDecl->ivar_size() : 0); |
| 5401 | if (NumIvars > 0) { |
| 5402 | static bool objc_ivar = false; |
| 5403 | if (!objc_ivar) { |
| 5404 | /* struct _objc_ivar { |
| 5405 | char *ivar_name; |
| 5406 | char *ivar_type; |
| 5407 | int ivar_offset; |
| 5408 | }; |
| 5409 | */ |
| 5410 | Result += "\nstruct _objc_ivar {\n"; |
| 5411 | Result += "\tchar *ivar_name;\n"; |
| 5412 | Result += "\tchar *ivar_type;\n"; |
| 5413 | Result += "\tint ivar_offset;\n"; |
| 5414 | Result += "};\n"; |
| 5415 | |
| 5416 | objc_ivar = true; |
| 5417 | } |
| 5418 | |
| 5419 | /* struct { |
| 5420 | int ivar_count; |
| 5421 | struct _objc_ivar ivar_list[nIvars]; |
| 5422 | }; |
| 5423 | */ |
| 5424 | Result += "\nstatic struct {\n"; |
| 5425 | Result += "\tint ivar_count;\n"; |
| 5426 | Result += "\tstruct _objc_ivar ivar_list["; |
| 5427 | Result += utostr(NumIvars); |
| 5428 | Result += "];\n} _OBJC_INSTANCE_VARIABLES_"; |
| 5429 | Result += IDecl->getNameAsString(); |
| 5430 | Result += " __attribute__ ((used, section (\"__OBJC, __instance_vars\")))= " |
| 5431 | "{\n\t"; |
| 5432 | Result += utostr(NumIvars); |
| 5433 | Result += "\n"; |
| 5434 | |
| 5435 | ObjCInterfaceDecl::ivar_iterator IVI, IVE; |
| 5436 | SmallVector<ObjCIvarDecl *, 8> IVars; |
| 5437 | if (!IDecl->ivar_empty()) { |
| 5438 | for (ObjCInterfaceDecl::ivar_iterator |
| 5439 | IV = IDecl->ivar_begin(), IVEnd = IDecl->ivar_end(); |
| 5440 | IV != IVEnd; ++IV) |
| 5441 | IVars.push_back(*IV); |
| 5442 | IVI = IDecl->ivar_begin(); |
| 5443 | IVE = IDecl->ivar_end(); |
| 5444 | } else { |
| 5445 | IVI = CDecl->ivar_begin(); |
| 5446 | IVE = CDecl->ivar_end(); |
| 5447 | } |
| 5448 | Result += "\t,{{\""; |
| 5449 | Result += (*IVI)->getNameAsString(); |
| 5450 | Result += "\", \""; |
| 5451 | std::string TmpString, StrEncoding; |
| 5452 | Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI); |
| 5453 | QuoteDoublequotes(TmpString, StrEncoding); |
| 5454 | Result += StrEncoding; |
| 5455 | Result += "\", "; |
| 5456 | RewriteIvarOffsetComputation(*IVI, Result); |
| 5457 | Result += "}\n"; |
| 5458 | for (++IVI; IVI != IVE; ++IVI) { |
| 5459 | Result += "\t ,{\""; |
| 5460 | Result += (*IVI)->getNameAsString(); |
| 5461 | Result += "\", \""; |
| 5462 | std::string TmpString, StrEncoding; |
| 5463 | Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI); |
| 5464 | QuoteDoublequotes(TmpString, StrEncoding); |
| 5465 | Result += StrEncoding; |
| 5466 | Result += "\", "; |
| 5467 | RewriteIvarOffsetComputation((*IVI), Result); |
| 5468 | Result += "}\n"; |
| 5469 | } |
| 5470 | |
| 5471 | Result += "\t }\n};\n"; |
| 5472 | } |
| 5473 | |
| 5474 | // Build _objc_method_list for class's instance methods if needed |
| 5475 | SmallVector<ObjCMethodDecl *, 32> |
| 5476 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
| 5477 | |
| 5478 | // If any of our property implementations have associated getters or |
| 5479 | // setters, produce metadata for them as well. |
| 5480 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 5481 | PropEnd = IDecl->propimpl_end(); |
| 5482 | Prop != PropEnd; ++Prop) { |
| 5483 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 5484 | continue; |
| 5485 | if (!(*Prop)->getPropertyIvarDecl()) |
| 5486 | continue; |
| 5487 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 5488 | if (!PD) |
| 5489 | continue; |
| 5490 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 5491 | if (!Getter->isDefined()) |
| 5492 | InstanceMethods.push_back(Getter); |
| 5493 | if (PD->isReadOnly()) |
| 5494 | continue; |
| 5495 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 5496 | if (!Setter->isDefined()) |
| 5497 | InstanceMethods.push_back(Setter); |
| 5498 | } |
| 5499 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
| 5500 | true, "", IDecl->getName(), Result); |
| 5501 | |
| 5502 | // Build _objc_method_list for class's class methods if needed |
| 5503 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
| 5504 | false, "", IDecl->getName(), Result); |
| 5505 | |
| 5506 | // Protocols referenced in class declaration? |
| 5507 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), |
| 5508 | "CLASS", CDecl->getName(), Result); |
| 5509 | |
| 5510 | // Declaration of class/meta-class metadata |
| 5511 | /* struct _objc_class { |
| 5512 | struct _objc_class *isa; // or const char *root_class_name when metadata |
| 5513 | const char *super_class_name; |
| 5514 | char *name; |
| 5515 | long version; |
| 5516 | long info; |
| 5517 | long instance_size; |
| 5518 | struct _objc_ivar_list *ivars; |
| 5519 | struct _objc_method_list *methods; |
| 5520 | struct objc_cache *cache; |
| 5521 | struct objc_protocol_list *protocols; |
| 5522 | const char *ivar_layout; |
| 5523 | struct _objc_class_ext *ext; |
| 5524 | }; |
| 5525 | */ |
| 5526 | static bool objc_class = false; |
| 5527 | if (!objc_class) { |
| 5528 | Result += "\nstruct _objc_class {\n"; |
| 5529 | Result += "\tstruct _objc_class *isa;\n"; |
| 5530 | Result += "\tconst char *super_class_name;\n"; |
| 5531 | Result += "\tchar *name;\n"; |
| 5532 | Result += "\tlong version;\n"; |
| 5533 | Result += "\tlong info;\n"; |
| 5534 | Result += "\tlong instance_size;\n"; |
| 5535 | Result += "\tstruct _objc_ivar_list *ivars;\n"; |
| 5536 | Result += "\tstruct _objc_method_list *methods;\n"; |
| 5537 | Result += "\tstruct objc_cache *cache;\n"; |
| 5538 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 5539 | Result += "\tconst char *ivar_layout;\n"; |
| 5540 | Result += "\tstruct _objc_class_ext *ext;\n"; |
| 5541 | Result += "};\n"; |
| 5542 | objc_class = true; |
| 5543 | } |
| 5544 | |
| 5545 | // Meta-class metadata generation. |
| 5546 | ObjCInterfaceDecl *RootClass = 0; |
| 5547 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
| 5548 | while (SuperClass) { |
| 5549 | RootClass = SuperClass; |
| 5550 | SuperClass = SuperClass->getSuperClass(); |
| 5551 | } |
| 5552 | SuperClass = CDecl->getSuperClass(); |
| 5553 | |
| 5554 | Result += "\nstatic struct _objc_class _OBJC_METACLASS_"; |
| 5555 | Result += CDecl->getNameAsString(); |
| 5556 | Result += " __attribute__ ((used, section (\"__OBJC, __meta_class\")))= " |
| 5557 | "{\n\t(struct _objc_class *)\""; |
| 5558 | Result += (RootClass ? RootClass->getNameAsString() : CDecl->getNameAsString()); |
| 5559 | Result += "\""; |
| 5560 | |
| 5561 | if (SuperClass) { |
| 5562 | Result += ", \""; |
| 5563 | Result += SuperClass->getNameAsString(); |
| 5564 | Result += "\", \""; |
| 5565 | Result += CDecl->getNameAsString(); |
| 5566 | Result += "\""; |
| 5567 | } |
| 5568 | else { |
| 5569 | Result += ", 0, \""; |
| 5570 | Result += CDecl->getNameAsString(); |
| 5571 | Result += "\""; |
| 5572 | } |
| 5573 | // Set 'ivars' field for root class to 0. ObjC1 runtime does not use it. |
| 5574 | // 'info' field is initialized to CLS_META(2) for metaclass |
| 5575 | Result += ", 0,2, sizeof(struct _objc_class), 0"; |
| 5576 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
| 5577 | Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_"; |
| 5578 | Result += IDecl->getNameAsString(); |
| 5579 | Result += "\n"; |
| 5580 | } |
| 5581 | else |
| 5582 | Result += ", 0\n"; |
| 5583 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
| 5584 | Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_"; |
| 5585 | Result += CDecl->getNameAsString(); |
| 5586 | Result += ",0,0\n"; |
| 5587 | } |
| 5588 | else |
| 5589 | Result += "\t,0,0,0,0\n"; |
| 5590 | Result += "};\n"; |
| 5591 | |
| 5592 | // class metadata generation. |
| 5593 | Result += "\nstatic struct _objc_class _OBJC_CLASS_"; |
| 5594 | Result += CDecl->getNameAsString(); |
| 5595 | Result += " __attribute__ ((used, section (\"__OBJC, __class\")))= " |
| 5596 | "{\n\t&_OBJC_METACLASS_"; |
| 5597 | Result += CDecl->getNameAsString(); |
| 5598 | if (SuperClass) { |
| 5599 | Result += ", \""; |
| 5600 | Result += SuperClass->getNameAsString(); |
| 5601 | Result += "\", \""; |
| 5602 | Result += CDecl->getNameAsString(); |
| 5603 | Result += "\""; |
| 5604 | } |
| 5605 | else { |
| 5606 | Result += ", 0, \""; |
| 5607 | Result += CDecl->getNameAsString(); |
| 5608 | Result += "\""; |
| 5609 | } |
| 5610 | // 'info' field is initialized to CLS_CLASS(1) for class |
| 5611 | Result += ", 0,1"; |
| 5612 | if (!ObjCSynthesizedStructs.count(CDecl)) |
| 5613 | Result += ",0"; |
| 5614 | else { |
| 5615 | // class has size. Must synthesize its size. |
| 5616 | Result += ",sizeof(struct "; |
| 5617 | Result += CDecl->getNameAsString(); |
| 5618 | if (LangOpts.MicrosoftExt) |
| 5619 | Result += "_IMPL"; |
| 5620 | Result += ")"; |
| 5621 | } |
| 5622 | if (NumIvars > 0) { |
| 5623 | Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_"; |
| 5624 | Result += CDecl->getNameAsString(); |
| 5625 | Result += "\n\t"; |
| 5626 | } |
| 5627 | else |
| 5628 | Result += ",0"; |
| 5629 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
| 5630 | Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_"; |
| 5631 | Result += CDecl->getNameAsString(); |
| 5632 | Result += ", 0\n\t"; |
| 5633 | } |
| 5634 | else |
| 5635 | Result += ",0,0"; |
| 5636 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
| 5637 | Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_"; |
| 5638 | Result += CDecl->getNameAsString(); |
| 5639 | Result += ", 0,0\n"; |
| 5640 | } |
| 5641 | else |
| 5642 | Result += ",0,0,0\n"; |
| 5643 | Result += "};\n"; |
| 5644 | } |
| 5645 | |
| 5646 | void RewriteObjCFragileABI::RewriteMetaDataIntoBuffer(std::string &Result) { |
| 5647 | int ClsDefCount = ClassImplementation.size(); |
| 5648 | int CatDefCount = CategoryImplementation.size(); |
| 5649 | |
| 5650 | // For each implemented class, write out all its meta data. |
| 5651 | for (int i = 0; i < ClsDefCount; i++) |
| 5652 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
| 5653 | |
| 5654 | // For each implemented category, write out all its meta data. |
| 5655 | for (int i = 0; i < CatDefCount; i++) |
| 5656 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
| 5657 | |
| 5658 | // Write objc_symtab metadata |
| 5659 | /* |
| 5660 | struct _objc_symtab |
| 5661 | { |
| 5662 | long sel_ref_cnt; |
| 5663 | SEL *refs; |
| 5664 | short cls_def_cnt; |
| 5665 | short cat_def_cnt; |
| 5666 | void *defs[cls_def_cnt + cat_def_cnt]; |
| 5667 | }; |
| 5668 | */ |
| 5669 | |
| 5670 | Result += "\nstruct _objc_symtab {\n"; |
| 5671 | Result += "\tlong sel_ref_cnt;\n"; |
| 5672 | Result += "\tSEL *refs;\n"; |
| 5673 | Result += "\tshort cls_def_cnt;\n"; |
| 5674 | Result += "\tshort cat_def_cnt;\n"; |
| 5675 | Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n"; |
| 5676 | Result += "};\n\n"; |
| 5677 | |
| 5678 | Result += "static struct _objc_symtab " |
| 5679 | "_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n"; |
| 5680 | Result += "\t0, 0, " + utostr(ClsDefCount) |
| 5681 | + ", " + utostr(CatDefCount) + "\n"; |
| 5682 | for (int i = 0; i < ClsDefCount; i++) { |
| 5683 | Result += "\t,&_OBJC_CLASS_"; |
| 5684 | Result += ClassImplementation[i]->getNameAsString(); |
| 5685 | Result += "\n"; |
| 5686 | } |
| 5687 | |
| 5688 | for (int i = 0; i < CatDefCount; i++) { |
| 5689 | Result += "\t,&_OBJC_CATEGORY_"; |
| 5690 | Result += CategoryImplementation[i]->getClassInterface()->getNameAsString(); |
| 5691 | Result += "_"; |
| 5692 | Result += CategoryImplementation[i]->getNameAsString(); |
| 5693 | Result += "\n"; |
| 5694 | } |
| 5695 | |
| 5696 | Result += "};\n\n"; |
| 5697 | |
| 5698 | // Write objc_module metadata |
| 5699 | |
| 5700 | /* |
| 5701 | struct _objc_module { |
| 5702 | long version; |
| 5703 | long size; |
| 5704 | const char *name; |
| 5705 | struct _objc_symtab *symtab; |
| 5706 | } |
| 5707 | */ |
| 5708 | |
| 5709 | Result += "\nstruct _objc_module {\n"; |
| 5710 | Result += "\tlong version;\n"; |
| 5711 | Result += "\tlong size;\n"; |
| 5712 | Result += "\tconst char *name;\n"; |
| 5713 | Result += "\tstruct _objc_symtab *symtab;\n"; |
| 5714 | Result += "};\n\n"; |
| 5715 | Result += "static struct _objc_module " |
| 5716 | "_OBJC_MODULES __attribute__ ((used, section (\"__OBJC, __module_info\")))= {\n"; |
| 5717 | Result += "\t" + utostr(OBJC_ABI_VERSION) + |
| 5718 | ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n"; |
| 5719 | Result += "};\n\n"; |
| 5720 | |
| 5721 | if (LangOpts.MicrosoftExt) { |
| 5722 | if (ProtocolExprDecls.size()) { |
| 5723 | Result += "#pragma section(\".objc_protocol$B\",long,read,write)\n"; |
| 5724 | Result += "#pragma data_seg(push, \".objc_protocol$B\")\n"; |
| 5725 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
| 5726 | E = ProtocolExprDecls.end(); I != E; ++I) { |
| 5727 | Result += "static struct _objc_protocol *_POINTER_OBJC_PROTOCOL_"; |
| 5728 | Result += (*I)->getNameAsString(); |
| 5729 | Result += " = &_OBJC_PROTOCOL_"; |
| 5730 | Result += (*I)->getNameAsString(); |
| 5731 | Result += ";\n"; |
| 5732 | } |
| 5733 | Result += "#pragma data_seg(pop)\n\n"; |
| 5734 | } |
| 5735 | Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n"; |
| 5736 | Result += "#pragma data_seg(push, \".objc_module_info$B\")\n"; |
| 5737 | Result += "static struct _objc_module *_POINTER_OBJC_MODULES = "; |
| 5738 | Result += "&_OBJC_MODULES;\n"; |
| 5739 | Result += "#pragma data_seg(pop)\n\n"; |
| 5740 | } |
| 5741 | } |
| 5742 | |
| 5743 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
| 5744 | /// implementation. |
| 5745 | void RewriteObjCFragileABI::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
| 5746 | std::string &Result) { |
| 5747 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
| 5748 | // Find category declaration for this implementation. |
| 5749 | ObjCCategoryDecl *CDecl; |
| 5750 | for (CDecl = ClassDecl->getCategoryList(); CDecl; |
| 5751 | CDecl = CDecl->getNextClassCategory()) |
| 5752 | if (CDecl->getIdentifier() == IDecl->getIdentifier()) |
| 5753 | break; |
| 5754 | |
| 5755 | std::string FullCategoryName = ClassDecl->getNameAsString(); |
| 5756 | FullCategoryName += '_'; |
| 5757 | FullCategoryName += IDecl->getNameAsString(); |
| 5758 | |
| 5759 | // Build _objc_method_list for class's instance methods if needed |
| 5760 | SmallVector<ObjCMethodDecl *, 32> |
| 5761 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
| 5762 | |
| 5763 | // If any of our property implementations have associated getters or |
| 5764 | // setters, produce metadata for them as well. |
| 5765 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 5766 | PropEnd = IDecl->propimpl_end(); |
| 5767 | Prop != PropEnd; ++Prop) { |
| 5768 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 5769 | continue; |
| 5770 | if (!(*Prop)->getPropertyIvarDecl()) |
| 5771 | continue; |
| 5772 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 5773 | if (!PD) |
| 5774 | continue; |
| 5775 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 5776 | InstanceMethods.push_back(Getter); |
| 5777 | if (PD->isReadOnly()) |
| 5778 | continue; |
| 5779 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 5780 | InstanceMethods.push_back(Setter); |
| 5781 | } |
| 5782 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
| 5783 | true, "CATEGORY_", FullCategoryName.c_str(), |
| 5784 | Result); |
| 5785 | |
| 5786 | // Build _objc_method_list for class's class methods if needed |
| 5787 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
| 5788 | false, "CATEGORY_", FullCategoryName.c_str(), |
| 5789 | Result); |
| 5790 | |
| 5791 | // Protocols referenced in class declaration? |
| 5792 | // Null CDecl is case of a category implementation with no category interface |
| 5793 | if (CDecl) |
| 5794 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), "CATEGORY", |
| 5795 | FullCategoryName, Result); |
| 5796 | /* struct _objc_category { |
| 5797 | char *category_name; |
| 5798 | char *class_name; |
| 5799 | struct _objc_method_list *instance_methods; |
| 5800 | struct _objc_method_list *class_methods; |
| 5801 | struct _objc_protocol_list *protocols; |
| 5802 | // Objective-C 1.0 extensions |
| 5803 | uint32_t size; // sizeof (struct _objc_category) |
| 5804 | struct _objc_property_list *instance_properties; // category's own |
| 5805 | // @property decl. |
| 5806 | }; |
| 5807 | */ |
| 5808 | |
| 5809 | static bool objc_category = false; |
| 5810 | if (!objc_category) { |
| 5811 | Result += "\nstruct _objc_category {\n"; |
| 5812 | Result += "\tchar *category_name;\n"; |
| 5813 | Result += "\tchar *class_name;\n"; |
| 5814 | Result += "\tstruct _objc_method_list *instance_methods;\n"; |
| 5815 | Result += "\tstruct _objc_method_list *class_methods;\n"; |
| 5816 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 5817 | Result += "\tunsigned int size;\n"; |
| 5818 | Result += "\tstruct _objc_property_list *instance_properties;\n"; |
| 5819 | Result += "};\n"; |
| 5820 | objc_category = true; |
| 5821 | } |
| 5822 | Result += "\nstatic struct _objc_category _OBJC_CATEGORY_"; |
| 5823 | Result += FullCategoryName; |
| 5824 | Result += " __attribute__ ((used, section (\"__OBJC, __category\")))= {\n\t\""; |
| 5825 | Result += IDecl->getNameAsString(); |
| 5826 | Result += "\"\n\t, \""; |
| 5827 | Result += ClassDecl->getNameAsString(); |
| 5828 | Result += "\"\n"; |
| 5829 | |
| 5830 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
| 5831 | Result += "\t, (struct _objc_method_list *)" |
| 5832 | "&_OBJC_CATEGORY_INSTANCE_METHODS_"; |
| 5833 | Result += FullCategoryName; |
| 5834 | Result += "\n"; |
| 5835 | } |
| 5836 | else |
| 5837 | Result += "\t, 0\n"; |
| 5838 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
| 5839 | Result += "\t, (struct _objc_method_list *)" |
| 5840 | "&_OBJC_CATEGORY_CLASS_METHODS_"; |
| 5841 | Result += FullCategoryName; |
| 5842 | Result += "\n"; |
| 5843 | } |
| 5844 | else |
| 5845 | Result += "\t, 0\n"; |
| 5846 | |
| 5847 | if (CDecl && CDecl->protocol_begin() != CDecl->protocol_end()) { |
| 5848 | Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_"; |
| 5849 | Result += FullCategoryName; |
| 5850 | Result += "\n"; |
| 5851 | } |
| 5852 | else |
| 5853 | Result += "\t, 0\n"; |
| 5854 | Result += "\t, sizeof(struct _objc_category), 0\n};\n"; |
| 5855 | } |
| 5856 | |
| 5857 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
| 5858 | /// class methods. |
| 5859 | template<typename MethodIterator> |
| 5860 | void RewriteObjCFragileABI::RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 5861 | MethodIterator MethodEnd, |
| 5862 | bool IsInstanceMethod, |
| 5863 | StringRef prefix, |
| 5864 | StringRef ClassName, |
| 5865 | std::string &Result) { |
| 5866 | if (MethodBegin == MethodEnd) return; |
| 5867 | |
| 5868 | if (!objc_impl_method) { |
| 5869 | /* struct _objc_method { |
| 5870 | SEL _cmd; |
| 5871 | char *method_types; |
| 5872 | void *_imp; |
| 5873 | } |
| 5874 | */ |
| 5875 | Result += "\nstruct _objc_method {\n"; |
| 5876 | Result += "\tSEL _cmd;\n"; |
| 5877 | Result += "\tchar *method_types;\n"; |
| 5878 | Result += "\tvoid *_imp;\n"; |
| 5879 | Result += "};\n"; |
| 5880 | |
| 5881 | objc_impl_method = true; |
| 5882 | } |
| 5883 | |
| 5884 | // Build _objc_method_list for class's methods if needed |
| 5885 | |
| 5886 | /* struct { |
| 5887 | struct _objc_method_list *next_method; |
| 5888 | int method_count; |
| 5889 | struct _objc_method method_list[]; |
| 5890 | } |
| 5891 | */ |
| 5892 | unsigned NumMethods = std::distance(MethodBegin, MethodEnd); |
| 5893 | Result += "\nstatic struct {\n"; |
| 5894 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 5895 | Result += "\tint method_count;\n"; |
| 5896 | Result += "\tstruct _objc_method method_list["; |
| 5897 | Result += utostr(NumMethods); |
| 5898 | Result += "];\n} _OBJC_"; |
| 5899 | Result += prefix; |
| 5900 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 5901 | Result += "_METHODS_"; |
| 5902 | Result += ClassName; |
| 5903 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
| 5904 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 5905 | Result += "_meth\")))= "; |
| 5906 | Result += "{\n\t0, " + utostr(NumMethods) + "\n"; |
| 5907 | |
| 5908 | Result += "\t,{{(SEL)\""; |
| 5909 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
| 5910 | std::string MethodTypeString; |
| 5911 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
| 5912 | Result += "\", \""; |
| 5913 | Result += MethodTypeString; |
| 5914 | Result += "\", (void *)"; |
| 5915 | Result += MethodInternalNames[*MethodBegin]; |
| 5916 | Result += "}\n"; |
| 5917 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 5918 | Result += "\t ,{(SEL)\""; |
| 5919 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
| 5920 | std::string MethodTypeString; |
| 5921 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
| 5922 | Result += "\", \""; |
| 5923 | Result += MethodTypeString; |
| 5924 | Result += "\", (void *)"; |
| 5925 | Result += MethodInternalNames[*MethodBegin]; |
| 5926 | Result += "}\n"; |
| 5927 | } |
| 5928 | Result += "\t }\n};\n"; |
| 5929 | } |
| 5930 | |
| 5931 | Stmt *RewriteObjCFragileABI::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV) { |
| 5932 | SourceRange OldRange = IV->getSourceRange(); |
| 5933 | Expr *BaseExpr = IV->getBase(); |
| 5934 | |
| 5935 | // Rewrite the base, but without actually doing replaces. |
| 5936 | { |
| 5937 | DisableReplaceStmtScope S(*this); |
| 5938 | BaseExpr = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(BaseExpr)); |
| 5939 | IV->setBase(BaseExpr); |
| 5940 | } |
| 5941 | |
| 5942 | ObjCIvarDecl *D = IV->getDecl(); |
| 5943 | |
| 5944 | Expr *Replacement = IV; |
| 5945 | if (CurMethodDef) { |
| 5946 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
| 5947 | const ObjCInterfaceType *iFaceDecl = |
| 5948 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
| 5949 | assert(iFaceDecl && "RewriteObjCIvarRefExpr - iFaceDecl is null"); |
| 5950 | // lookup which class implements the instance variable. |
| 5951 | ObjCInterfaceDecl *clsDeclared = 0; |
| 5952 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
| 5953 | clsDeclared); |
| 5954 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
| 5955 | |
| 5956 | // Synthesize an explicit cast to gain access to the ivar. |
| 5957 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 5958 | RecName += "_IMPL"; |
| 5959 | IdentifierInfo *II = &Context->Idents.get(RecName); |
| 5960 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 5961 | SourceLocation(), SourceLocation(), |
| 5962 | II); |
| 5963 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 5964 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 5965 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, |
| 5966 | CK_BitCast, |
| 5967 | IV->getBase()); |
| 5968 | // Don't forget the parens to enforce the proper binding. |
| 5969 | ParenExpr *PE = new (Context) ParenExpr(OldRange.getBegin(), |
| 5970 | OldRange.getEnd(), |
| 5971 | castExpr); |
| 5972 | if (IV->isFreeIvar() && |
Douglas Gregor | 60ef308 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 5973 | declaresSameEntity(CurMethodDef->getClassInterface(), iFaceDecl->getDecl())) { |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5974 | MemberExpr *ME = new (Context) MemberExpr(PE, true, D, |
| 5975 | IV->getLocation(), |
| 5976 | D->getType(), |
| 5977 | VK_LValue, OK_Ordinary); |
| 5978 | Replacement = ME; |
| 5979 | } else { |
| 5980 | IV->setBase(PE); |
| 5981 | } |
| 5982 | } |
| 5983 | } else { // we are outside a method. |
| 5984 | assert(!IV->isFreeIvar() && "Cannot have a free standing ivar outside a method"); |
| 5985 | |
| 5986 | // Explicit ivar refs need to have a cast inserted. |
| 5987 | // FIXME: consider sharing some of this code with the code above. |
| 5988 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
| 5989 | const ObjCInterfaceType *iFaceDecl = |
| 5990 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
| 5991 | // lookup which class implements the instance variable. |
| 5992 | ObjCInterfaceDecl *clsDeclared = 0; |
| 5993 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
| 5994 | clsDeclared); |
| 5995 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
| 5996 | |
| 5997 | // Synthesize an explicit cast to gain access to the ivar. |
| 5998 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 5999 | RecName += "_IMPL"; |
| 6000 | IdentifierInfo *II = &Context->Idents.get(RecName); |
| 6001 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 6002 | SourceLocation(), SourceLocation(), |
| 6003 | II); |
| 6004 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 6005 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 6006 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, |
| 6007 | CK_BitCast, |
| 6008 | IV->getBase()); |
| 6009 | // Don't forget the parens to enforce the proper binding. |
| 6010 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
| 6011 | IV->getBase()->getLocEnd(), castExpr); |
| 6012 | // Cannot delete IV->getBase(), since PE points to it. |
| 6013 | // Replace the old base with the cast. This is important when doing |
| 6014 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
| 6015 | IV->setBase(PE); |
| 6016 | } |
| 6017 | } |
| 6018 | |
| 6019 | ReplaceStmtWithRange(IV, Replacement, OldRange); |
| 6020 | return Replacement; |
| 6021 | } |
| 6022 | |