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 | |
Ted Kremenek | 305c613 | 2012-09-01 05:09:24 +0000 | [diff] [blame] | 14 | #include "clang/Rewrite/Frontend/ASTConsumers.h" |
| 15 | #include "clang/Rewrite/Core/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); |
Richard Smith | d1420c6 | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 230 | New->printPretty(S, 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 | |
Benjamin Kramer | 48d798c | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 352 | // Misc. AST transformation routines. Sometimes they end up calling |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 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()); |
Fariborz Jahanian | 9fc5b00 | 2012-06-28 21:20:35 +0000 | [diff] [blame] | 358 | CallExpr *SynthMsgSendStretCallExpr(FunctionDecl *MsgSendStretFlavor, |
| 359 | QualType msgSendType, |
| 360 | QualType returnType, |
| 361 | SmallVectorImpl<QualType> &ArgTypes, |
| 362 | SmallVectorImpl<Expr*> &MsgExprs, |
| 363 | ObjCMethodDecl *Method); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 364 | Stmt *SynthMessageExpr(ObjCMessageExpr *Exp, |
| 365 | SourceLocation StartLoc=SourceLocation(), |
| 366 | SourceLocation EndLoc=SourceLocation()); |
| 367 | |
| 368 | void SynthCountByEnumWithState(std::string &buf); |
| 369 | void SynthMsgSendFunctionDecl(); |
| 370 | void SynthMsgSendSuperFunctionDecl(); |
| 371 | void SynthMsgSendStretFunctionDecl(); |
| 372 | void SynthMsgSendFpretFunctionDecl(); |
| 373 | void SynthMsgSendSuperStretFunctionDecl(); |
| 374 | void SynthGetClassFunctionDecl(); |
| 375 | void SynthGetMetaClassFunctionDecl(); |
| 376 | void SynthGetSuperClassFunctionDecl(); |
| 377 | void SynthSelGetUidFunctionDecl(); |
| 378 | void SynthSuperContructorFunctionDecl(); |
| 379 | |
| 380 | std::string SynthesizeByrefCopyDestroyHelper(VarDecl *VD, int flag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 381 | std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 382 | StringRef funcName, std::string Tag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 383 | std::string SynthesizeBlockFunc(BlockExpr *CE, int i, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 384 | StringRef funcName, std::string Tag); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 385 | std::string SynthesizeBlockImpl(BlockExpr *CE, |
| 386 | std::string Tag, std::string Desc); |
| 387 | std::string SynthesizeBlockDescriptor(std::string DescTag, |
| 388 | std::string ImplTag, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 389 | int i, StringRef funcName, |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 390 | unsigned hasCopy); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 391 | Stmt *SynthesizeBlockCall(CallExpr *Exp, const Expr* BlockExp); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 392 | void SynthesizeBlockLiterals(SourceLocation FunLocStart, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 393 | StringRef FunName); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 394 | FunctionDecl *SynthBlockInitFunctionDecl(StringRef name); |
| 395 | Stmt *SynthBlockInitExpr(BlockExpr *Exp, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 396 | const SmallVector<DeclRefExpr *, 8> &InnerBlockDeclRefs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 397 | |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 398 | // Misc. helper routines. |
Fariborz Jahanian | dd8079c | 2011-12-05 19:50:04 +0000 | [diff] [blame] | 399 | QualType getProtocolType(); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 400 | void WarnAboutReturnGotoStmts(Stmt *S); |
| 401 | void HasReturnStmts(Stmt *S, bool &hasReturns); |
| 402 | void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND); |
| 403 | void InsertBlockLiteralsWithinFunction(FunctionDecl *FD); |
| 404 | void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD); |
| 405 | |
| 406 | bool IsDeclStmtInForeachHeader(DeclStmt *DS); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 407 | void CollectBlockDeclRefInfo(BlockExpr *Exp); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 408 | void GetBlockDeclRefExprs(Stmt *S); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 409 | void GetInnerBlockDeclRefExprs(Stmt *S, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 410 | SmallVector<DeclRefExpr *, 8> &InnerBlockDeclRefs, |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 411 | llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 412 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 413 | // We avoid calling Type::isBlockPointerType(), since it operates on the |
| 414 | // 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] | 415 | bool isTopLevelBlockPointerType(QualType T) { |
| 416 | return isa<BlockPointerType>(T); |
| 417 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 418 | |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 419 | /// convertBlockPointerToFunctionPointer - Converts a block-pointer type |
| 420 | /// to a function pointer type and upon success, returns true; false |
| 421 | /// otherwise. |
| 422 | bool convertBlockPointerToFunctionPointer(QualType &T) { |
| 423 | if (isTopLevelBlockPointerType(T)) { |
| 424 | const BlockPointerType *BPT = T->getAs<BlockPointerType>(); |
| 425 | T = Context->getPointerType(BPT->getPointeeType()); |
| 426 | return true; |
| 427 | } |
| 428 | return false; |
| 429 | } |
| 430 | |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 431 | bool needToScanForQualifiers(QualType T); |
| 432 | QualType getSuperStructType(); |
| 433 | QualType getConstantStringStructType(); |
| 434 | QualType convertFunctionTypeOfBlocks(const FunctionType *FT); |
| 435 | bool BufferContainsPPDirectives(const char *startBuf, const char *endBuf); |
| 436 | |
Fariborz Jahanian | 8188e5f | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 437 | void convertToUnqualifiedObjCType(QualType &T) { |
| 438 | if (T->isObjCQualifiedIdType()) |
| 439 | T = Context->getObjCIdType(); |
| 440 | else if (T->isObjCQualifiedClassType()) |
| 441 | T = Context->getObjCClassType(); |
| 442 | else if (T->isObjCObjectPointerType() && |
Fariborz Jahanian | 3a448fb | 2011-09-10 17:01:56 +0000 | [diff] [blame] | 443 | T->getPointeeType()->isObjCQualifiedInterfaceType()) { |
| 444 | if (const ObjCObjectPointerType * OBJPT = |
| 445 | T->getAsObjCInterfacePointerType()) { |
| 446 | const ObjCInterfaceType *IFaceT = OBJPT->getInterfaceType(); |
| 447 | T = QualType(IFaceT, 0); |
| 448 | T = Context->getPointerType(T); |
| 449 | } |
| 450 | } |
Fariborz Jahanian | 8188e5f | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 453 | // FIXME: This predicate seems like it would be useful to add to ASTContext. |
| 454 | bool isObjCType(QualType T) { |
| 455 | if (!LangOpts.ObjC1 && !LangOpts.ObjC2) |
| 456 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 457 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 458 | QualType OCT = Context->getCanonicalType(T).getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 459 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 460 | if (OCT == Context->getCanonicalType(Context->getObjCIdType()) || |
| 461 | OCT == Context->getCanonicalType(Context->getObjCClassType())) |
| 462 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 463 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 464 | if (const PointerType *PT = OCT->getAs<PointerType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 465 | if (isa<ObjCInterfaceType>(PT->getPointeeType()) || |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 466 | PT->getPointeeType()->isObjCQualifiedIdType()) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 467 | return true; |
| 468 | } |
| 469 | return false; |
| 470 | } |
| 471 | bool PointerTypeTakesAnyBlockArguments(QualType QT); |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 472 | bool PointerTypeTakesAnyObjCQualifiedType(QualType QT); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 473 | void GetExtentOfArgList(const char *Name, const char *&LParen, |
| 474 | const char *&RParen); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 475 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 476 | void QuoteDoublequotes(std::string &From, std::string &To) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 477 | for (unsigned i = 0; i < From.length(); i++) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 478 | if (From[i] == '"') |
| 479 | To += "\\\""; |
| 480 | else |
| 481 | To += From[i]; |
| 482 | } |
| 483 | } |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 484 | |
| 485 | QualType getSimpleFunctionType(QualType result, |
| 486 | const QualType *args, |
| 487 | unsigned numArgs, |
| 488 | bool variadic = false) { |
Fariborz Jahanian | 8891480 | 2011-09-09 20:35:22 +0000 | [diff] [blame] | 489 | if (result == Context->getObjCInstanceType()) |
| 490 | result = Context->getObjCIdType(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 491 | FunctionProtoType::ExtProtoInfo fpi; |
| 492 | fpi.Variadic = variadic; |
| 493 | return Context->getFunctionType(result, args, numArgs, fpi); |
| 494 | } |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 495 | |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 496 | // Helper function: create a CStyleCastExpr with trivial type source info. |
| 497 | CStyleCastExpr* NoTypeInfoCStyleCastExpr(ASTContext *Ctx, QualType Ty, |
| 498 | CastKind Kind, Expr *E) { |
| 499 | TypeSourceInfo *TInfo = Ctx->getTrivialTypeSourceInfo(Ty, SourceLocation()); |
| 500 | return CStyleCastExpr::Create(*Ctx, Ty, VK_RValue, Kind, E, 0, TInfo, |
| 501 | SourceLocation(), SourceLocation()); |
| 502 | } |
| 503 | }; |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 504 | |
| 505 | class RewriteObjCFragileABI : public RewriteObjC { |
| 506 | public: |
| 507 | |
| 508 | RewriteObjCFragileABI(std::string inFile, raw_ostream *OS, |
| 509 | DiagnosticsEngine &D, const LangOptions &LOpts, |
| 510 | bool silenceMacroWarn) : RewriteObjC(inFile, OS, |
| 511 | D, LOpts, |
| 512 | silenceMacroWarn) {} |
| 513 | |
| 514 | ~RewriteObjCFragileABI() {} |
| 515 | virtual void Initialize(ASTContext &context); |
| 516 | |
| 517 | // Rewriting metadata |
| 518 | template<typename MethodIterator> |
| 519 | void RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 520 | MethodIterator MethodEnd, |
| 521 | bool IsInstanceMethod, |
| 522 | StringRef prefix, |
| 523 | StringRef ClassName, |
| 524 | std::string &Result); |
| 525 | virtual void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol, |
| 526 | StringRef prefix, |
| 527 | StringRef ClassName, |
| 528 | std::string &Result); |
| 529 | virtual void RewriteObjCProtocolListMetaData( |
| 530 | const ObjCList<ObjCProtocolDecl> &Prots, |
| 531 | StringRef prefix, StringRef ClassName, std::string &Result); |
| 532 | virtual void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
| 533 | std::string &Result); |
| 534 | virtual void RewriteMetaDataIntoBuffer(std::string &Result); |
| 535 | virtual void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
| 536 | std::string &Result); |
| 537 | |
| 538 | // Rewriting ivar |
| 539 | virtual void RewriteIvarOffsetComputation(ObjCIvarDecl *ivar, |
| 540 | std::string &Result); |
| 541 | virtual Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV); |
| 542 | }; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 545 | void RewriteObjC::RewriteBlocksInFunctionProtoType(QualType funcType, |
| 546 | NamedDecl *D) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 547 | if (const FunctionProtoType *fproto |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 548 | = dyn_cast<FunctionProtoType>(funcType.IgnoreParens())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 549 | for (FunctionProtoType::arg_type_iterator I = fproto->arg_type_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 550 | E = fproto->arg_type_end(); I && (I != E); ++I) |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 551 | if (isTopLevelBlockPointerType(*I)) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 552 | // All the args are checked/rewritten. Don't call twice! |
| 553 | RewriteBlockPointerDecl(D); |
| 554 | break; |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | void RewriteObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 560 | const PointerType *PT = funcType->getAs<PointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 561 | if (PT && PointerTypeTakesAnyBlockArguments(funcType)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 562 | RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 565 | static bool IsHeaderFile(const std::string &Filename) { |
| 566 | std::string::size_type DotPos = Filename.rfind('.'); |
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 | if (DotPos == std::string::npos) { |
| 569 | // no file extension |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 570 | return false; |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 571 | } |
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 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 574 | // C header: .h |
| 575 | // C++ header: .hh or .H; |
| 576 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 577 | } |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 578 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 579 | RewriteObjC::RewriteObjC(std::string inFile, raw_ostream* OS, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 580 | DiagnosticsEngine &D, const LangOptions &LOpts, |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 581 | bool silenceMacroWarn) |
| 582 | : Diags(D), LangOpts(LOpts), InFileName(inFile), OutFile(OS), |
| 583 | SilenceRewriteMacroWarning(silenceMacroWarn) { |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 584 | IsHeader = IsHeaderFile(inFile); |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 585 | RewriteFailedDiag = Diags.getCustomDiagID(DiagnosticsEngine::Warning, |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 586 | "rewriting sub-expression within a macro (may not be correct)"); |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 587 | TryFinallyContainsReturnDiag = Diags.getCustomDiagID( |
| 588 | DiagnosticsEngine::Warning, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 589 | "rewriter doesn't support user-specified control flow semantics " |
| 590 | "for @try/@finally (code may not execute properly)"); |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Eli Friedman | bce831b | 2009-05-18 22:29:17 +0000 | [diff] [blame] | 593 | ASTConsumer *clang::CreateObjCRewriter(const std::string& InFile, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 594 | raw_ostream* OS, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 595 | DiagnosticsEngine &Diags, |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 596 | const LangOptions &LOpts, |
| 597 | bool SilenceRewriteMacroWarning) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 598 | return new RewriteObjCFragileABI(InFile, OS, Diags, LOpts, SilenceRewriteMacroWarning); |
Chris Lattner | e365c50 | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 599 | } |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 600 | |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 601 | void RewriteObjC::InitializeCommon(ASTContext &context) { |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 602 | Context = &context; |
| 603 | SM = &Context->getSourceManager(); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 604 | TUDecl = Context->getTranslationUnitDecl(); |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 605 | MsgSendFunctionDecl = 0; |
| 606 | MsgSendSuperFunctionDecl = 0; |
| 607 | MsgSendStretFunctionDecl = 0; |
| 608 | MsgSendSuperStretFunctionDecl = 0; |
| 609 | MsgSendFpretFunctionDecl = 0; |
| 610 | GetClassFunctionDecl = 0; |
| 611 | GetMetaClassFunctionDecl = 0; |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 612 | GetSuperClassFunctionDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 613 | SelGetUidFunctionDecl = 0; |
| 614 | CFStringFunctionDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 615 | ConstantStringClassReference = 0; |
| 616 | NSStringRecord = 0; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 617 | CurMethodDef = 0; |
| 618 | CurFunctionDef = 0; |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 619 | CurFunctionDeclToDeclareForBlock = 0; |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 620 | GlobalVarDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 621 | SuperStructDecl = 0; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 622 | ProtocolTypeDecl = 0; |
Steve Naroff | 9630ec5 | 2008-03-27 22:59:54 +0000 | [diff] [blame] | 623 | ConstantStringDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 624 | BcLabelCount = 0; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 625 | SuperContructorFunctionDecl = 0; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 626 | NumObjCStringLiterals = 0; |
Steve Naroff | 68272b8 | 2008-12-08 20:01:41 +0000 | [diff] [blame] | 627 | PropParentMap = 0; |
| 628 | CurrentBody = 0; |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 629 | DisableReplaceStmt = false; |
Fariborz Jahanian | f292fcf | 2010-01-07 22:51:18 +0000 | [diff] [blame] | 630 | objc_impl_method = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 631 | |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 632 | // Get the ID and start/end of the main file. |
| 633 | MainFileID = SM->getMainFileID(); |
| 634 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 635 | MainFileStart = MainBuf->getBufferStart(); |
| 636 | MainFileEnd = MainBuf->getBufferEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 637 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 638 | Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOpts()); |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 641 | //===----------------------------------------------------------------------===// |
| 642 | // Top Level Driver Code |
| 643 | //===----------------------------------------------------------------------===// |
| 644 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 645 | void RewriteObjC::HandleTopLevelSingleDecl(Decl *D) { |
Ted Kremenek | e50187a | 2010-02-05 21:28:51 +0000 | [diff] [blame] | 646 | if (Diags.hasErrorOccurred()) |
| 647 | return; |
| 648 | |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 649 | // Two cases: either the decl could be in the main file, or it could be in a |
| 650 | // #included file. If the former, rewrite it now. If the later, check to see |
| 651 | // if we rewrote the #include/#import. |
| 652 | SourceLocation Loc = D->getLocation(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 653 | Loc = SM->getExpansionLoc(Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 654 | |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 655 | // If this is for a builtin, ignore it. |
| 656 | if (Loc.isInvalid()) return; |
| 657 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 658 | // Look for built-in declarations that we need to refer during the rewrite. |
| 659 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 660 | RewriteFunctionDecl(FD); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 661 | } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) { |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 662 | // declared in <Foundation/NSString.h> |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 663 | if (FVD->getName() == "_NSConstantStringClassReference") { |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 664 | ConstantStringClassReference = FVD; |
| 665 | return; |
| 666 | } |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 667 | } else if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) { |
| 668 | if (ID->isThisDeclarationADefinition()) |
| 669 | RewriteInterfaceDecl(ID); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 670 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 671 | RewriteCategoryDecl(CD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 672 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 673 | if (PD->isThisDeclarationADefinition()) |
| 674 | RewriteProtocolDecl(PD); |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 675 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
| 676 | // Recurse into linkage specifications |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 677 | for (DeclContext::decl_iterator DI = LSD->decls_begin(), |
| 678 | DIEnd = LSD->decls_end(); |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 679 | DI != DIEnd; ) { |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 680 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>((*DI))) { |
| 681 | if (!IFace->isThisDeclarationADefinition()) { |
| 682 | SmallVector<Decl *, 8> DG; |
| 683 | SourceLocation StartLoc = IFace->getLocStart(); |
| 684 | do { |
| 685 | if (isa<ObjCInterfaceDecl>(*DI) && |
| 686 | !cast<ObjCInterfaceDecl>(*DI)->isThisDeclarationADefinition() && |
| 687 | StartLoc == (*DI)->getLocStart()) |
| 688 | DG.push_back(*DI); |
| 689 | else |
| 690 | break; |
| 691 | |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 692 | ++DI; |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 693 | } while (DI != DIEnd); |
| 694 | RewriteForwardClassDecl(DG); |
| 695 | continue; |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 696 | } |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 697 | } |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 698 | |
| 699 | if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>((*DI))) { |
| 700 | if (!Proto->isThisDeclarationADefinition()) { |
| 701 | SmallVector<Decl *, 8> DG; |
| 702 | SourceLocation StartLoc = Proto->getLocStart(); |
| 703 | do { |
| 704 | if (isa<ObjCProtocolDecl>(*DI) && |
| 705 | !cast<ObjCProtocolDecl>(*DI)->isThisDeclarationADefinition() && |
| 706 | StartLoc == (*DI)->getLocStart()) |
| 707 | DG.push_back(*DI); |
| 708 | else |
| 709 | break; |
| 710 | |
| 711 | ++DI; |
| 712 | } while (DI != DIEnd); |
| 713 | RewriteForwardProtocolDecl(DG); |
| 714 | continue; |
| 715 | } |
| 716 | } |
| 717 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 718 | HandleTopLevelSingleDecl(*DI); |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 719 | ++DI; |
| 720 | } |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 721 | } |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 722 | // 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] | 723 | if (SM->isFromMainFile(Loc)) |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 724 | return HandleDeclInMainFile(D); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 727 | //===----------------------------------------------------------------------===// |
| 728 | // Syntactic (non-AST) Rewriting Code |
| 729 | //===----------------------------------------------------------------------===// |
| 730 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 731 | void RewriteObjC::RewriteInclude() { |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 732 | SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 733 | StringRef MainBuf = SM->getBufferData(MainFileID); |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 734 | const char *MainBufStart = MainBuf.begin(); |
| 735 | const char *MainBufEnd = MainBuf.end(); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 736 | size_t ImportLen = strlen("import"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 737 | |
Fariborz Jahanian | af57b46 | 2008-01-19 01:03:17 +0000 | [diff] [blame] | 738 | // Loop over the whole file, looking for includes. |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 739 | for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { |
| 740 | if (*BufPtr == '#') { |
| 741 | if (++BufPtr == MainBufEnd) |
| 742 | return; |
| 743 | while (*BufPtr == ' ' || *BufPtr == '\t') |
| 744 | if (++BufPtr == MainBufEnd) |
| 745 | return; |
| 746 | if (!strncmp(BufPtr, "import", ImportLen)) { |
| 747 | // replace import with include |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 748 | SourceLocation ImportLoc = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 749 | LocStart.getLocWithOffset(BufPtr-MainBufStart); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 750 | ReplaceText(ImportLoc, ImportLen, "include"); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 751 | BufPtr += ImportLen; |
| 752 | } |
| 753 | } |
| 754 | } |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 757 | static std::string getIvarAccessString(ObjCIvarDecl *OID) { |
| 758 | const ObjCInterfaceDecl *ClassDecl = OID->getContainingInterface(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 759 | std::string S; |
| 760 | S = "((struct "; |
| 761 | S += ClassDecl->getIdentifier()->getName(); |
| 762 | S += "_IMPL *)self)->"; |
Daniel Dunbar | 5ffe14c | 2009-10-18 20:26:27 +0000 | [diff] [blame] | 763 | S += OID->getName(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 764 | return S; |
| 765 | } |
| 766 | |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 767 | void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 768 | ObjCImplementationDecl *IMD, |
| 769 | ObjCCategoryImplDecl *CID) { |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 770 | static bool objcGetPropertyDefined = false; |
| 771 | static bool objcSetPropertyDefined = false; |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 772 | SourceLocation startLoc = PID->getLocStart(); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 773 | InsertText(startLoc, "// "); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 774 | const char *startBuf = SM->getCharacterData(startLoc); |
| 775 | assert((*startBuf == '@') && "bogus @synthesize location"); |
| 776 | const char *semiBuf = strchr(startBuf, ';'); |
| 777 | assert((*semiBuf == ';') && "@synthesize: can't find ';'"); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 778 | SourceLocation onePastSemiLoc = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 779 | startLoc.getLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 780 | |
| 781 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 782 | return; // FIXME: is this correct? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 783 | |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 784 | // Generate the 'getter' function. |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 785 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 786 | ObjCIvarDecl *OID = PID->getPropertyIvarDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 787 | |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 788 | if (!OID) |
| 789 | return; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 790 | unsigned Attributes = PD->getPropertyAttributes(); |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 791 | if (!PD->getGetterMethodDecl()->isDefined()) { |
| 792 | bool GenGetProperty = !(Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) && |
| 793 | (Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
| 794 | ObjCPropertyDecl::OBJC_PR_copy)); |
| 795 | std::string Getr; |
| 796 | if (GenGetProperty && !objcGetPropertyDefined) { |
| 797 | objcGetPropertyDefined = true; |
| 798 | // FIXME. Is this attribute correct in all cases? |
| 799 | Getr = "\nextern \"C\" __declspec(dllimport) " |
| 800 | "id objc_getProperty(id, SEL, long, bool);\n"; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 801 | } |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 802 | RewriteObjCMethodDecl(OID->getContainingInterface(), |
| 803 | PD->getGetterMethodDecl(), Getr); |
| 804 | Getr += "{ "; |
| 805 | // Synthesize an explicit cast to gain access to the ivar. |
| 806 | // See objc-act.c:objc_synthesize_new_getter() for details. |
| 807 | if (GenGetProperty) { |
| 808 | // return objc_getProperty(self, _cmd, offsetof(ClassDecl, OID), 1) |
| 809 | Getr += "typedef "; |
| 810 | const FunctionType *FPRetType = 0; |
| 811 | RewriteTypeIntoString(PD->getGetterMethodDecl()->getResultType(), Getr, |
| 812 | FPRetType); |
| 813 | Getr += " _TYPE"; |
| 814 | if (FPRetType) { |
| 815 | Getr += ")"; // close the precedence "scope" for "*". |
| 816 | |
| 817 | // Now, emit the argument types (if any). |
| 818 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)){ |
| 819 | Getr += "("; |
| 820 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 821 | if (i) Getr += ", "; |
| 822 | std::string ParamStr = FT->getArgType(i).getAsString( |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 823 | Context->getPrintingPolicy()); |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 824 | Getr += ParamStr; |
| 825 | } |
| 826 | if (FT->isVariadic()) { |
| 827 | if (FT->getNumArgs()) Getr += ", "; |
| 828 | Getr += "..."; |
| 829 | } |
| 830 | Getr += ")"; |
| 831 | } else |
| 832 | Getr += "()"; |
| 833 | } |
| 834 | Getr += ";\n"; |
| 835 | Getr += "return (_TYPE)"; |
| 836 | Getr += "objc_getProperty(self, _cmd, "; |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 837 | RewriteIvarOffsetComputation(OID, Getr); |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 838 | Getr += ", 1)"; |
| 839 | } |
| 840 | else |
| 841 | Getr += "return " + getIvarAccessString(OID); |
| 842 | Getr += "; }"; |
| 843 | InsertText(onePastSemiLoc, Getr); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 844 | } |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 845 | |
| 846 | if (PD->isReadOnly() || PD->getSetterMethodDecl()->isDefined()) |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 847 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 848 | |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 849 | // Generate the 'setter' function. |
| 850 | std::string Setr; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 851 | bool GenSetProperty = Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
| 852 | ObjCPropertyDecl::OBJC_PR_copy); |
| 853 | if (GenSetProperty && !objcSetPropertyDefined) { |
| 854 | objcSetPropertyDefined = true; |
| 855 | // FIXME. Is this attribute correct in all cases? |
| 856 | Setr = "\nextern \"C\" __declspec(dllimport) " |
| 857 | "void objc_setProperty (id, SEL, long, id, bool, bool);\n"; |
| 858 | } |
| 859 | |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 860 | RewriteObjCMethodDecl(OID->getContainingInterface(), |
| 861 | PD->getSetterMethodDecl(), Setr); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 862 | Setr += "{ "; |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 863 | // Synthesize an explicit cast to initialize the ivar. |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 864 | // See objc-act.c:objc_synthesize_new_setter() for details. |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 865 | if (GenSetProperty) { |
| 866 | Setr += "objc_setProperty (self, _cmd, "; |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 867 | RewriteIvarOffsetComputation(OID, Setr); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 868 | Setr += ", (id)"; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 869 | Setr += PD->getName(); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 870 | Setr += ", "; |
| 871 | if (Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 872 | Setr += "0, "; |
| 873 | else |
| 874 | Setr += "1, "; |
| 875 | if (Attributes & ObjCPropertyDecl::OBJC_PR_copy) |
| 876 | Setr += "1)"; |
| 877 | else |
| 878 | Setr += "0)"; |
| 879 | } |
| 880 | else { |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 881 | Setr += getIvarAccessString(OID) + " = "; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 882 | Setr += PD->getName(); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 883 | } |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 884 | Setr += "; }"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 885 | InsertText(onePastSemiLoc, Setr); |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 886 | } |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 887 | |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 888 | static void RewriteOneForwardClassDecl(ObjCInterfaceDecl *ForwardDecl, |
| 889 | std::string &typedefString) { |
| 890 | typedefString += "#ifndef _REWRITER_typedef_"; |
| 891 | typedefString += ForwardDecl->getNameAsString(); |
| 892 | typedefString += "\n"; |
| 893 | typedefString += "#define _REWRITER_typedef_"; |
| 894 | typedefString += ForwardDecl->getNameAsString(); |
| 895 | typedefString += "\n"; |
| 896 | typedefString += "typedef struct objc_object "; |
| 897 | typedefString += ForwardDecl->getNameAsString(); |
| 898 | typedefString += ";\n#endif\n"; |
| 899 | } |
| 900 | |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 901 | void RewriteObjC::RewriteForwardClassEpilogue(ObjCInterfaceDecl *ClassDecl, |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 902 | const std::string &typedefString) { |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 903 | SourceLocation startLoc = ClassDecl->getLocStart(); |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 904 | const char *startBuf = SM->getCharacterData(startLoc); |
| 905 | const char *semiPtr = strchr(startBuf, ';'); |
| 906 | // Replace the @class with typedefs corresponding to the classes. |
| 907 | ReplaceText(startLoc, semiPtr-startBuf+1, typedefString); |
| 908 | } |
| 909 | |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 910 | void RewriteObjC::RewriteForwardClassDecl(DeclGroupRef D) { |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 911 | std::string typedefString; |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 912 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 913 | ObjCInterfaceDecl *ForwardDecl = cast<ObjCInterfaceDecl>(*I); |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 914 | if (I == D.begin()) { |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 915 | // Translate to typedef's that forward reference structs with the same name |
| 916 | // as the class. As a convenience, we include the original declaration |
| 917 | // as a comment. |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 918 | typedefString += "// @class "; |
| 919 | typedefString += ForwardDecl->getNameAsString(); |
Fariborz Jahanian | 91fbd12 | 2010-01-11 22:48:40 +0000 | [diff] [blame] | 920 | typedefString += ";\n"; |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 921 | } |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 922 | RewriteOneForwardClassDecl(ForwardDecl, typedefString); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 923 | } |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 924 | DeclGroupRef::iterator I = D.begin(); |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 925 | RewriteForwardClassEpilogue(cast<ObjCInterfaceDecl>(*I), typedefString); |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 926 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 927 | |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 928 | void RewriteObjC::RewriteForwardClassDecl( |
| 929 | const llvm::SmallVector<Decl*, 8> &D) { |
| 930 | std::string typedefString; |
| 931 | for (unsigned i = 0; i < D.size(); i++) { |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 932 | ObjCInterfaceDecl *ForwardDecl = cast<ObjCInterfaceDecl>(D[i]); |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 933 | if (i == 0) { |
| 934 | typedefString += "// @class "; |
| 935 | typedefString += ForwardDecl->getNameAsString(); |
| 936 | typedefString += ";\n"; |
| 937 | } |
| 938 | RewriteOneForwardClassDecl(ForwardDecl, typedefString); |
| 939 | } |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 940 | RewriteForwardClassEpilogue(cast<ObjCInterfaceDecl>(D[0]), typedefString); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 941 | } |
| 942 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 943 | void RewriteObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) { |
Fariborz Jahanian | d050240 | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 944 | // When method is a synthesized one, such as a getter/setter there is |
| 945 | // nothing to rewrite. |
Fariborz Jahanian | 8891480 | 2011-09-09 20:35:22 +0000 | [diff] [blame] | 946 | if (Method->isImplicit()) |
Fariborz Jahanian | d050240 | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 947 | return; |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 948 | SourceLocation LocStart = Method->getLocStart(); |
| 949 | SourceLocation LocEnd = Method->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 950 | |
Chandler Carruth | 6421162 | 2011-07-25 21:09:52 +0000 | [diff] [blame] | 951 | if (SM->getExpansionLineNumber(LocEnd) > |
| 952 | SM->getExpansionLineNumber(LocStart)) { |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 953 | InsertText(LocStart, "#if 0\n"); |
| 954 | ReplaceText(LocEnd, 1, ";\n#endif\n"); |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 955 | } else { |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 956 | InsertText(LocStart, "// "); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 957 | } |
| 958 | } |
| 959 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 960 | void RewriteObjC::RewriteProperty(ObjCPropertyDecl *prop) { |
Fariborz Jahanian | d050240 | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 961 | SourceLocation Loc = prop->getAtLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 962 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 963 | ReplaceText(Loc, 0, "// "); |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 964 | // FIXME: handle properties that are declared across multiple lines. |
Fariborz Jahanian | 957cf65 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 965 | } |
| 966 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 967 | void RewriteObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 968 | SourceLocation LocStart = CatDecl->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 969 | |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 970 | // FIXME: handle category headers that are declared across multiple lines. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 971 | ReplaceText(LocStart, 0, "// "); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 972 | |
Fariborz Jahanian | 13751e3 | 2010-02-10 01:15:09 +0000 | [diff] [blame] | 973 | for (ObjCCategoryDecl::prop_iterator I = CatDecl->prop_begin(), |
| 974 | E = CatDecl->prop_end(); I != E; ++I) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 975 | RewriteProperty(*I); |
Fariborz Jahanian | 13751e3 | 2010-02-10 01:15:09 +0000 | [diff] [blame] | 976 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 977 | for (ObjCCategoryDecl::instmeth_iterator |
| 978 | I = CatDecl->instmeth_begin(), E = CatDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 979 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 980 | RewriteMethodDeclaration(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 981 | for (ObjCCategoryDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 982 | I = CatDecl->classmeth_begin(), E = CatDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 983 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 984 | RewriteMethodDeclaration(*I); |
| 985 | |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 986 | // Lastly, comment out the @end. |
Fariborz Jahanian | 73d1eb0 | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 987 | ReplaceText(CatDecl->getAtEndRange().getBegin(), |
| 988 | strlen("@end"), "/* @end */"); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 989 | } |
| 990 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 991 | void RewriteObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 992 | SourceLocation LocStart = PDecl->getLocStart(); |
Douglas Gregor | 61cc296 | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 993 | assert(PDecl->isThisDeclarationADefinition()); |
| 994 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 995 | // FIXME: handle protocol headers that are declared across multiple lines. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 996 | ReplaceText(LocStart, 0, "// "); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 997 | |
| 998 | for (ObjCProtocolDecl::instmeth_iterator |
| 999 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1000 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1001 | RewriteMethodDeclaration(*I); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1002 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1003 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1004 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1005 | RewriteMethodDeclaration(*I); |
| 1006 | |
Fariborz Jahanian | 07acdf4 | 2010-09-24 18:36:58 +0000 | [diff] [blame] | 1007 | for (ObjCInterfaceDecl::prop_iterator I = PDecl->prop_begin(), |
| 1008 | E = PDecl->prop_end(); I != E; ++I) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1009 | RewriteProperty(*I); |
Fariborz Jahanian | 07acdf4 | 2010-09-24 18:36:58 +0000 | [diff] [blame] | 1010 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 1011 | // Lastly, comment out the @end. |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1012 | SourceLocation LocEnd = PDecl->getAtEndRange().getBegin(); |
Fariborz Jahanian | 73d1eb0 | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 1013 | ReplaceText(LocEnd, strlen("@end"), "/* @end */"); |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 1014 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 1015 | // Must comment out @optional/@required |
| 1016 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1017 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 1018 | for (const char *p = startBuf; p < endBuf; p++) { |
| 1019 | if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { |
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("@optional"), "/* @optional */"); |
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 | else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1025 | SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1026 | ReplaceText(OptionalLoc, strlen("@required"), "/* @required */"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1027 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 1028 | } |
| 1029 | } |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1032 | void RewriteObjC::RewriteForwardProtocolDecl(DeclGroupRef D) { |
| 1033 | SourceLocation LocStart = (*D.begin())->getLocStart(); |
| 1034 | if (LocStart.isInvalid()) |
| 1035 | llvm_unreachable("Invalid SourceLocation"); |
| 1036 | // FIXME: handle forward protocol that are declared across multiple lines. |
| 1037 | ReplaceText(LocStart, 0, "// "); |
| 1038 | } |
| 1039 | |
| 1040 | void |
| 1041 | RewriteObjC::RewriteForwardProtocolDecl(const llvm::SmallVector<Decl*, 8> &DG) { |
| 1042 | SourceLocation LocStart = DG[0]->getLocStart(); |
Steve Naroff | b7fa992 | 2007-11-14 03:37:28 +0000 | [diff] [blame] | 1043 | if (LocStart.isInvalid()) |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1044 | llvm_unreachable("Invalid SourceLocation"); |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 1045 | // FIXME: handle forward protocol that are declared across multiple lines. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1046 | ReplaceText(LocStart, 0, "// "); |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1049 | void RewriteObjC::RewriteTypeIntoString(QualType T, std::string &ResultStr, |
| 1050 | const FunctionType *&FPRetType) { |
| 1051 | if (T->isObjCQualifiedIdType()) |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1052 | ResultStr += "id"; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1053 | else if (T->isFunctionPointerType() || |
| 1054 | T->isBlockPointerType()) { |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1055 | // needs special handling, since pointer-to-functions have special |
| 1056 | // syntax (where a decaration models use). |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1057 | QualType retType = T; |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1058 | QualType PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1059 | if (const PointerType* PT = retType->getAs<PointerType>()) |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1060 | PointeeTy = PT->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1061 | else if (const BlockPointerType *BPT = retType->getAs<BlockPointerType>()) |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1062 | PointeeTy = BPT->getPointeeType(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1063 | if ((FPRetType = PointeeTy->getAs<FunctionType>())) { |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1064 | ResultStr += FPRetType->getResultType().getAsString( |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1065 | Context->getPrintingPolicy()); |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1066 | ResultStr += "(*"; |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1067 | } |
| 1068 | } else |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1069 | ResultStr += T.getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1072 | void RewriteObjC::RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl, |
| 1073 | ObjCMethodDecl *OMD, |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1074 | std::string &ResultStr) { |
| 1075 | //fprintf(stderr,"In RewriteObjCMethodDecl\n"); |
| 1076 | const FunctionType *FPRetType = 0; |
| 1077 | ResultStr += "\nstatic "; |
| 1078 | RewriteTypeIntoString(OMD->getResultType(), ResultStr, FPRetType); |
Fariborz Jahanian | 531a1ea | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 1079 | ResultStr += " "; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1080 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1081 | // Unique method name |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1082 | std::string NameStr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1083 | |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1084 | if (OMD->isInstanceMethod()) |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1085 | NameStr += "_I_"; |
| 1086 | else |
| 1087 | NameStr += "_C_"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1088 | |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1089 | NameStr += IDecl->getNameAsString(); |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1090 | NameStr += "_"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1091 | |
| 1092 | if (ObjCCategoryImplDecl *CID = |
Steve Naroff | 3e0a540 | 2009-01-08 19:41:02 +0000 | [diff] [blame] | 1093 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1094 | NameStr += CID->getNameAsString(); |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1095 | NameStr += "_"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1096 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1097 | // Append selector names, replacing ':' with '_' |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1098 | { |
| 1099 | std::string selString = OMD->getSelector().getAsString(); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1100 | int len = selString.size(); |
| 1101 | for (int i = 0; i < len; i++) |
| 1102 | if (selString[i] == ':') |
| 1103 | selString[i] = '_'; |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1104 | NameStr += selString; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1105 | } |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1106 | // Remember this name for metadata emission |
| 1107 | MethodInternalNames[OMD] = NameStr; |
| 1108 | ResultStr += NameStr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1109 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1110 | // Rewrite arguments |
| 1111 | ResultStr += "("; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1112 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1113 | // invisible arguments |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1114 | if (OMD->isInstanceMethod()) { |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1115 | QualType selfTy = Context->getObjCInterfaceType(IDecl); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1116 | selfTy = Context->getPointerType(selfTy); |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 1117 | if (!LangOpts.MicrosoftExt) { |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1118 | if (ObjCSynthesizedStructs.count(const_cast<ObjCInterfaceDecl*>(IDecl))) |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 1119 | ResultStr += "struct "; |
| 1120 | } |
| 1121 | // When rewriting for Microsoft, explicitly omit the structure name. |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1122 | ResultStr += IDecl->getNameAsString(); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1123 | ResultStr += " *"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1124 | } |
| 1125 | else |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1126 | ResultStr += Context->getObjCClassType().getAsString( |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1127 | Context->getPrintingPolicy()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1128 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1129 | ResultStr += " self, "; |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1130 | ResultStr += Context->getObjCSelType().getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1131 | ResultStr += " _cmd"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1132 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1133 | // Method arguments. |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1134 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 1135 | E = OMD->param_end(); PI != E; ++PI) { |
| 1136 | ParmVarDecl *PDecl = *PI; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1137 | ResultStr += ", "; |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1138 | if (PDecl->getType()->isObjCQualifiedIdType()) { |
| 1139 | ResultStr += "id "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1140 | ResultStr += PDecl->getNameAsString(); |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1141 | } else { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1142 | std::string Name = PDecl->getNameAsString(); |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 1143 | QualType QT = PDecl->getType(); |
| 1144 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 2610f90 | 2012-03-27 16:42:20 +0000 | [diff] [blame] | 1145 | (void)convertBlockPointerToFunctionPointer(QT); |
| 1146 | QT.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1147 | ResultStr += Name; |
| 1148 | } |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1149 | } |
Fariborz Jahanian | 7c39ff7 | 2008-01-21 20:14:23 +0000 | [diff] [blame] | 1150 | if (OMD->isVariadic()) |
| 1151 | ResultStr += ", ..."; |
Fariborz Jahanian | 531a1ea | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 1152 | ResultStr += ") "; |
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 | if (FPRetType) { |
| 1155 | ResultStr += ")"; // close the precedence "scope" for "*". |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1156 | |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1157 | // Now, emit the argument types (if any). |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1158 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) { |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1159 | ResultStr += "("; |
| 1160 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 1161 | if (i) ResultStr += ", "; |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1162 | std::string ParamStr = FT->getArgType(i).getAsString( |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1163 | Context->getPrintingPolicy()); |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1164 | ResultStr += ParamStr; |
| 1165 | } |
| 1166 | if (FT->isVariadic()) { |
| 1167 | if (FT->getNumArgs()) ResultStr += ", "; |
| 1168 | ResultStr += "..."; |
| 1169 | } |
| 1170 | ResultStr += ")"; |
| 1171 | } else { |
| 1172 | ResultStr += "()"; |
| 1173 | } |
| 1174 | } |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1175 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1176 | void RewriteObjC::RewriteImplementationDecl(Decl *OID) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1177 | ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID); |
| 1178 | ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1179 | |
Fariborz Jahanian | a135216 | 2010-02-15 21:11:41 +0000 | [diff] [blame] | 1180 | InsertText(IMD ? IMD->getLocStart() : CID->getLocStart(), "// "); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1181 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1182 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1183 | I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(), |
| 1184 | E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1185 | I != E; ++I) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1186 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1187 | ObjCMethodDecl *OMD = *I; |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1188 | RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1189 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1190 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1191 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1192 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1193 | const char *endBuf = SM->getCharacterData(LocEnd); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1194 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1195 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1196 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1197 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1198 | I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(), |
| 1199 | E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1200 | I != E; ++I) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1201 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1202 | ObjCMethodDecl *OMD = *I; |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1203 | RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1204 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1205 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1206 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1207 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1208 | const char *endBuf = SM->getCharacterData(LocEnd); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1209 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1210 | } |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1211 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1212 | I = IMD ? IMD->propimpl_begin() : CID->propimpl_begin(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1213 | E = IMD ? IMD->propimpl_end() : CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1214 | I != E; ++I) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1215 | RewritePropertyImplDecl(*I, IMD, CID); |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1218 | InsertText(IMD ? IMD->getLocEnd() : CID->getLocEnd(), "// "); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1221 | void RewriteObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { |
Steve Naroff | f908a87 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 1222 | std::string ResultStr; |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1223 | if (!ObjCForwardDecls.count(ClassDecl->getCanonicalDecl())) { |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1224 | // we haven't seen a forward decl - generate a typedef. |
Steve Naroff | 5086a8d | 2007-11-14 23:02:56 +0000 | [diff] [blame] | 1225 | ResultStr = "#ifndef _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"; |
| 1228 | ResultStr += "#define _REWRITER_typedef_"; |
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"; |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1231 | ResultStr += "typedef struct objc_object "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1232 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1233 | ResultStr += ";\n#endif\n"; |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1234 | // Mark this typedef as having been generated. |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1235 | ObjCForwardDecls.insert(ClassDecl->getCanonicalDecl()); |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1236 | } |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 1237 | RewriteObjCInternalStruct(ClassDecl, ResultStr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1238 | |
| 1239 | for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1240 | E = ClassDecl->prop_end(); I != E; ++I) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1241 | RewriteProperty(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1242 | for (ObjCInterfaceDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1243 | I = ClassDecl->instmeth_begin(), E = ClassDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1244 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1245 | RewriteMethodDeclaration(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1246 | for (ObjCInterfaceDecl::classmeth_iterator |
| 1247 | I = ClassDecl->classmeth_begin(), E = ClassDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1248 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1249 | RewriteMethodDeclaration(*I); |
| 1250 | |
Steve Naroff | 2feac5e | 2007-10-30 03:43:13 +0000 | [diff] [blame] | 1251 | // Lastly, comment out the @end. |
Fariborz Jahanian | 73d1eb0 | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 1252 | ReplaceText(ClassDecl->getAtEndRange().getBegin(), strlen("@end"), |
| 1253 | "/* @end */"); |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1256 | Stmt *RewriteObjC::RewritePropertyOrImplicitSetter(PseudoObjectExpr *PseudoOp) { |
| 1257 | SourceRange OldRange = PseudoOp->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1258 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1259 | // We just magically know some things about the structure of this |
| 1260 | // expression. |
| 1261 | ObjCMessageExpr *OldMsg = |
| 1262 | cast<ObjCMessageExpr>(PseudoOp->getSemanticExpr( |
| 1263 | PseudoOp->getNumSemanticExprs() - 1)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1264 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1265 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 1266 | // we need to suppress rewriting the sub-statements. |
| 1267 | Expr *Base, *RHS; |
| 1268 | { |
| 1269 | DisableReplaceStmtScope S(*this); |
| 1270 | |
| 1271 | // Rebuild the base expression if we have one. |
| 1272 | Base = 0; |
| 1273 | if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) { |
| 1274 | Base = OldMsg->getInstanceReceiver(); |
| 1275 | Base = cast<OpaqueValueExpr>(Base)->getSourceExpr(); |
| 1276 | Base = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Base)); |
| 1277 | } |
| 1278 | |
| 1279 | // Rebuild the RHS. |
| 1280 | RHS = cast<BinaryOperator>(PseudoOp->getSyntacticForm())->getRHS(); |
| 1281 | RHS = cast<OpaqueValueExpr>(RHS)->getSourceExpr(); |
| 1282 | RHS = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(RHS)); |
| 1283 | } |
| 1284 | |
| 1285 | // TODO: avoid this copy. |
| 1286 | SmallVector<SourceLocation, 1> SelLocs; |
| 1287 | OldMsg->getSelectorLocs(SelLocs); |
| 1288 | |
Chad Rosier | 0774ba7 | 2012-01-06 20:05:14 +0000 | [diff] [blame] | 1289 | ObjCMessageExpr *NewMsg = 0; |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1290 | switch (OldMsg->getReceiverKind()) { |
| 1291 | case ObjCMessageExpr::Class: |
| 1292 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1293 | OldMsg->getValueKind(), |
| 1294 | OldMsg->getLeftLoc(), |
| 1295 | OldMsg->getClassReceiverTypeInfo(), |
| 1296 | OldMsg->getSelector(), |
| 1297 | SelLocs, |
| 1298 | OldMsg->getMethodDecl(), |
| 1299 | RHS, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1300 | OldMsg->getRightLoc(), |
| 1301 | OldMsg->isImplicit()); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1302 | break; |
| 1303 | |
| 1304 | case ObjCMessageExpr::Instance: |
| 1305 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1306 | OldMsg->getValueKind(), |
| 1307 | OldMsg->getLeftLoc(), |
| 1308 | Base, |
| 1309 | OldMsg->getSelector(), |
| 1310 | SelLocs, |
| 1311 | OldMsg->getMethodDecl(), |
| 1312 | RHS, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1313 | OldMsg->getRightLoc(), |
| 1314 | OldMsg->isImplicit()); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1315 | break; |
| 1316 | |
| 1317 | case ObjCMessageExpr::SuperClass: |
| 1318 | case ObjCMessageExpr::SuperInstance: |
| 1319 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1320 | OldMsg->getValueKind(), |
| 1321 | OldMsg->getLeftLoc(), |
| 1322 | OldMsg->getSuperLoc(), |
| 1323 | OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance, |
| 1324 | OldMsg->getSuperType(), |
| 1325 | OldMsg->getSelector(), |
| 1326 | SelLocs, |
| 1327 | OldMsg->getMethodDecl(), |
| 1328 | RHS, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1329 | OldMsg->getRightLoc(), |
| 1330 | OldMsg->isImplicit()); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1331 | break; |
| 1332 | } |
| 1333 | |
| 1334 | Stmt *Replacement = SynthMessageExpr(NewMsg); |
| 1335 | ReplaceStmtWithRange(PseudoOp, Replacement, OldRange); |
| 1336 | return Replacement; |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1337 | } |
| 1338 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1339 | Stmt *RewriteObjC::RewritePropertyOrImplicitGetter(PseudoObjectExpr *PseudoOp) { |
| 1340 | SourceRange OldRange = PseudoOp->getSourceRange(); |
| 1341 | |
| 1342 | // We just magically know some things about the structure of this |
| 1343 | // expression. |
| 1344 | ObjCMessageExpr *OldMsg = |
| 1345 | cast<ObjCMessageExpr>(PseudoOp->getResultExpr()->IgnoreImplicit()); |
| 1346 | |
| 1347 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 1348 | // we need to suppress rewriting the sub-statements. |
| 1349 | Expr *Base = 0; |
| 1350 | { |
| 1351 | DisableReplaceStmtScope S(*this); |
| 1352 | |
| 1353 | // Rebuild the base expression if we have one. |
| 1354 | if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) { |
| 1355 | Base = OldMsg->getInstanceReceiver(); |
| 1356 | Base = cast<OpaqueValueExpr>(Base)->getSourceExpr(); |
| 1357 | Base = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Base)); |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 1358 | } |
Fariborz Jahanian | e0f8386 | 2010-10-20 16:07:20 +0000 | [diff] [blame] | 1359 | } |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1360 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1361 | // Intentionally empty. |
| 1362 | SmallVector<SourceLocation, 1> SelLocs; |
| 1363 | SmallVector<Expr*, 1> Args; |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1364 | |
Chad Rosier | 0774ba7 | 2012-01-06 20:05:14 +0000 | [diff] [blame] | 1365 | ObjCMessageExpr *NewMsg = 0; |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1366 | switch (OldMsg->getReceiverKind()) { |
| 1367 | case ObjCMessageExpr::Class: |
| 1368 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1369 | OldMsg->getValueKind(), |
| 1370 | OldMsg->getLeftLoc(), |
| 1371 | OldMsg->getClassReceiverTypeInfo(), |
| 1372 | OldMsg->getSelector(), |
| 1373 | SelLocs, |
| 1374 | OldMsg->getMethodDecl(), |
| 1375 | Args, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1376 | OldMsg->getRightLoc(), |
| 1377 | OldMsg->isImplicit()); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1378 | break; |
| 1379 | |
| 1380 | case ObjCMessageExpr::Instance: |
| 1381 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1382 | OldMsg->getValueKind(), |
| 1383 | OldMsg->getLeftLoc(), |
| 1384 | Base, |
| 1385 | OldMsg->getSelector(), |
| 1386 | SelLocs, |
| 1387 | OldMsg->getMethodDecl(), |
| 1388 | Args, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1389 | OldMsg->getRightLoc(), |
| 1390 | OldMsg->isImplicit()); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1391 | break; |
| 1392 | |
| 1393 | case ObjCMessageExpr::SuperClass: |
| 1394 | case ObjCMessageExpr::SuperInstance: |
| 1395 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1396 | OldMsg->getValueKind(), |
| 1397 | OldMsg->getLeftLoc(), |
| 1398 | OldMsg->getSuperLoc(), |
| 1399 | OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance, |
| 1400 | OldMsg->getSuperType(), |
| 1401 | OldMsg->getSelector(), |
| 1402 | SelLocs, |
| 1403 | OldMsg->getMethodDecl(), |
| 1404 | Args, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1405 | OldMsg->getRightLoc(), |
| 1406 | OldMsg->isImplicit()); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1407 | break; |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1408 | } |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1409 | |
| 1410 | Stmt *Replacement = SynthMessageExpr(NewMsg); |
| 1411 | ReplaceStmtWithRange(PseudoOp, Replacement, OldRange); |
| 1412 | return Replacement; |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1415 | /// SynthCountByEnumWithState - To print: |
| 1416 | /// ((unsigned int (*) |
| 1417 | /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1418 | /// (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1419 | /// sel_registerName( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1420 | /// "countByEnumeratingWithState:objects:count:"), |
| 1421 | /// &enumState, |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1422 | /// (id *)__rw_items, (unsigned int)16) |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1423 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1424 | void RewriteObjC::SynthCountByEnumWithState(std::string &buf) { |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1425 | buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, " |
| 1426 | "id *, unsigned int))(void *)objc_msgSend)"; |
| 1427 | buf += "\n\t\t"; |
| 1428 | buf += "((id)l_collection,\n\t\t"; |
| 1429 | buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; |
| 1430 | buf += "\n\t\t"; |
| 1431 | buf += "&enumState, " |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1432 | "(id *)__rw_items, (unsigned int)16)"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1433 | } |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1434 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1435 | /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach |
| 1436 | /// statement to exit to its outer synthesized loop. |
| 1437 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1438 | Stmt *RewriteObjC::RewriteBreakStmt(BreakStmt *S) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1439 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1440 | return S; |
| 1441 | // replace break with goto __break_label |
| 1442 | std::string buf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1443 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1444 | SourceLocation startLoc = S->getLocStart(); |
| 1445 | buf = "goto __break_label_"; |
| 1446 | buf += utostr(ObjCBcLabelNo.back()); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1447 | ReplaceText(startLoc, strlen("break"), buf); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1448 | |
| 1449 | return 0; |
| 1450 | } |
| 1451 | |
| 1452 | /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach |
| 1453 | /// statement to continue with its inner synthesized loop. |
| 1454 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1455 | Stmt *RewriteObjC::RewriteContinueStmt(ContinueStmt *S) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1456 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1457 | return S; |
| 1458 | // replace continue with goto __continue_label |
| 1459 | std::string buf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1460 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1461 | SourceLocation startLoc = S->getLocStart(); |
| 1462 | buf = "goto __continue_label_"; |
| 1463 | buf += utostr(ObjCBcLabelNo.back()); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1464 | ReplaceText(startLoc, strlen("continue"), buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1465 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1466 | return 0; |
| 1467 | } |
| 1468 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1469 | /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1470 | /// It rewrites: |
| 1471 | /// for ( type elem in collection) { stmts; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1472 | |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1473 | /// Into: |
| 1474 | /// { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1475 | /// type elem; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1476 | /// struct __objcFastEnumerationState enumState = { 0 }; |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1477 | /// id __rw_items[16]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1478 | /// id l_collection = (id)collection; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1479 | /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1480 | /// objects:__rw_items count:16]; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1481 | /// if (limit) { |
| 1482 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1483 | /// do { |
| 1484 | /// unsigned long counter = 0; |
| 1485 | /// do { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1486 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1487 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1488 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1489 | /// stmts; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1490 | /// __continue_label: ; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1491 | /// } while (counter < limit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1492 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1493 | /// objects:__rw_items count:16]); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1494 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1495 | /// __break_label: ; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1496 | /// } |
| 1497 | /// else |
| 1498 | /// elem = nil; |
| 1499 | /// } |
| 1500 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1501 | Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 1502 | SourceLocation OrigEnd) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1503 | assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1504 | assert(isa<ObjCForCollectionStmt>(Stmts.back()) && |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1505 | "ObjCForCollectionStmt Statement stack mismatch"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1506 | assert(!ObjCBcLabelNo.empty() && |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1507 | "ObjCForCollectionStmt - Label No stack empty"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1508 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1509 | SourceLocation startLoc = S->getLocStart(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1510 | const char *startBuf = SM->getCharacterData(startLoc); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1511 | StringRef elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1512 | std::string elementTypeAsString; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1513 | std::string buf; |
| 1514 | buf = "\n{\n\t"; |
| 1515 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { |
| 1516 | // type elem; |
Chris Lattner | 7e24e82 | 2009-03-28 06:33:19 +0000 | [diff] [blame] | 1517 | NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl()); |
Ted Kremenek | 1ed8e2a | 2008-10-06 22:16:13 +0000 | [diff] [blame] | 1518 | QualType ElementType = cast<ValueDecl>(D)->getType(); |
Steve Naroff | e89b8e7 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1519 | if (ElementType->isObjCQualifiedIdType() || |
| 1520 | ElementType->isObjCQualifiedInterfaceType()) |
| 1521 | // Simply use 'id' for all qualified types. |
| 1522 | elementTypeAsString = "id"; |
| 1523 | else |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1524 | elementTypeAsString = ElementType.getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1525 | buf += elementTypeAsString; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1526 | buf += " "; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 1527 | elementName = D->getName(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1528 | buf += elementName; |
| 1529 | buf += ";\n\t"; |
| 1530 | } |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1531 | else { |
| 1532 | DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 1533 | elementName = DR->getDecl()->getName(); |
Steve Naroff | e89b8e7 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1534 | ValueDecl *VD = cast<ValueDecl>(DR->getDecl()); |
| 1535 | if (VD->getType()->isObjCQualifiedIdType() || |
| 1536 | VD->getType()->isObjCQualifiedInterfaceType()) |
| 1537 | // Simply use 'id' for all qualified types. |
| 1538 | elementTypeAsString = "id"; |
| 1539 | else |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1540 | elementTypeAsString = VD->getType().getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1541 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1542 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1543 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1544 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1545 | // id __rw_items[16]; |
| 1546 | buf += "id __rw_items[16];\n\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1547 | // id l_collection = (id) |
| 1548 | buf += "id l_collection = (id)"; |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1549 | // Find start location of 'collection' the hard way! |
| 1550 | const char *startCollectionBuf = startBuf; |
| 1551 | startCollectionBuf += 3; // skip 'for' |
| 1552 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1553 | startCollectionBuf++; // skip '(' |
| 1554 | // find 'in' and skip it. |
| 1555 | while (*startCollectionBuf != ' ' || |
| 1556 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1557 | (*(startCollectionBuf+3) != ' ' && |
| 1558 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1559 | startCollectionBuf++; |
| 1560 | startCollectionBuf += 3; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1561 | |
| 1562 | // Replace: "for (type element in" with string constructed thus far. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1563 | ReplaceText(startLoc, startCollectionBuf - startBuf, buf); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1564 | // Replace ')' in for '(' type elem in collection ')' with ';' |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1565 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1566 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1567 | SourceLocation lparenLoc = startLoc.getLocWithOffset(rparenBuf-startBuf); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1568 | buf = ";\n\t"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1569 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1570 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1571 | // objects:__rw_items count:16]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1572 | // which is synthesized into: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1573 | // unsigned int limit = |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1574 | // ((unsigned int (*) |
| 1575 | // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1576 | // (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1577 | // sel_registerName( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1578 | // "countByEnumeratingWithState:objects:count:"), |
| 1579 | // (struct __objcFastEnumerationState *)&state, |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1580 | // (id *)__rw_items, (unsigned int)16); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1581 | buf += "unsigned long limit =\n\t\t"; |
| 1582 | SynthCountByEnumWithState(buf); |
| 1583 | buf += ";\n\t"; |
| 1584 | /// if (limit) { |
| 1585 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1586 | /// do { |
| 1587 | /// unsigned long counter = 0; |
| 1588 | /// do { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1589 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1590 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1591 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1592 | buf += "if (limit) {\n\t"; |
| 1593 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1594 | buf += "do {\n\t\t"; |
| 1595 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1596 | buf += "do {\n\t\t\t"; |
| 1597 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1598 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1599 | buf += elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1600 | buf += " = ("; |
| 1601 | buf += elementTypeAsString; |
| 1602 | buf += ")enumState.itemsPtr[counter++];"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1603 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1604 | ReplaceText(lparenLoc, 1, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1605 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1606 | /// __continue_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1607 | /// } while (counter < limit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1608 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1609 | /// objects:__rw_items count:16]); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1610 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1611 | /// __break_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1612 | /// } |
| 1613 | /// else |
| 1614 | /// elem = nil; |
| 1615 | /// } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1616 | /// |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1617 | buf = ";\n\t"; |
| 1618 | buf += "__continue_label_"; |
| 1619 | buf += utostr(ObjCBcLabelNo.back()); |
| 1620 | buf += ": ;"; |
| 1621 | buf += "\n\t\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1622 | buf += "} while (counter < limit);\n\t"; |
| 1623 | buf += "} while (limit = "; |
| 1624 | SynthCountByEnumWithState(buf); |
| 1625 | buf += ");\n\t"; |
| 1626 | buf += elementName; |
Fariborz Jahanian | 65b0aa5 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1627 | buf += " = (("; |
| 1628 | buf += elementTypeAsString; |
| 1629 | buf += ")0);\n\t"; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1630 | buf += "__break_label_"; |
| 1631 | buf += utostr(ObjCBcLabelNo.back()); |
| 1632 | buf += ": ;\n\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1633 | buf += "}\n\t"; |
| 1634 | buf += "else\n\t\t"; |
| 1635 | buf += elementName; |
Fariborz Jahanian | 65b0aa5 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1636 | buf += " = (("; |
| 1637 | buf += elementTypeAsString; |
| 1638 | buf += ")0);\n\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1639 | buf += "}\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1640 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1641 | // Insert all these *after* the statement body. |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1642 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1643 | if (isa<CompoundStmt>(S->getBody())) { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1644 | SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1645 | InsertText(endBodyLoc, buf); |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1646 | } else { |
| 1647 | /* Need to treat single statements specially. For example: |
| 1648 | * |
| 1649 | * for (A *a in b) if (stuff()) break; |
| 1650 | * for (A *a in b) xxxyy; |
| 1651 | * |
| 1652 | * The following code simply scans ahead to the semi to find the actual end. |
| 1653 | */ |
| 1654 | const char *stmtBuf = SM->getCharacterData(OrigEnd); |
| 1655 | const char *semiBuf = strchr(stmtBuf, ';'); |
| 1656 | assert(semiBuf && "Can't find ';'"); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1657 | SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(semiBuf-stmtBuf+1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1658 | InsertText(endBodyLoc, buf); |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1659 | } |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1660 | Stmts.pop_back(); |
| 1661 | ObjCBcLabelNo.pop_back(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1662 | return 0; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1663 | } |
| 1664 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1665 | /// RewriteObjCSynchronizedStmt - |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1666 | /// This routine rewrites @synchronized(expr) stmt; |
| 1667 | /// into: |
| 1668 | /// objc_sync_enter(expr); |
| 1669 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1670 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1671 | Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1672 | // Get the start location and compute the semi location. |
| 1673 | SourceLocation startLoc = S->getLocStart(); |
| 1674 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1675 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1676 | assert((*startBuf == '@') && "bogus @synchronized location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1677 | |
| 1678 | std::string buf; |
Steve Naroff | 3498cc9 | 2008-08-21 13:03:03 +0000 | [diff] [blame] | 1679 | buf = "objc_sync_enter((id)"; |
| 1680 | const char *lparenBuf = startBuf; |
| 1681 | while (*lparenBuf != '(') lparenBuf++; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1682 | ReplaceText(startLoc, lparenBuf-startBuf+1, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1683 | // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since |
| 1684 | // the sync expression is typically a message expression that's already |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1685 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1686 | SourceLocation endLoc = S->getSynchBody()->getLocStart(); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1687 | const char *endBuf = SM->getCharacterData(endLoc); |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1688 | while (*endBuf != ')') endBuf--; |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1689 | SourceLocation rparenLoc = startLoc.getLocWithOffset(endBuf-startBuf); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1690 | buf = ");\n"; |
| 1691 | // declare a new scope with two variables, _stack and _rethrow. |
| 1692 | buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n"; |
| 1693 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1694 | buf += "char *pointers[4];} _stack;\n"; |
| 1695 | buf += "id volatile _rethrow = 0;\n"; |
| 1696 | buf += "objc_exception_try_enter(&_stack);\n"; |
| 1697 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1698 | ReplaceText(rparenLoc, 1, buf); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1699 | startLoc = S->getSynchBody()->getLocEnd(); |
| 1700 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1701 | |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1702 | assert((*startBuf == '}') && "bogus @synchronized block"); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1703 | SourceLocation lastCurlyLoc = startLoc; |
| 1704 | buf = "}\nelse {\n"; |
| 1705 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 1706 | buf += "}\n"; |
| 1707 | buf += "{ /* implicit finally clause */\n"; |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1708 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1709 | |
| 1710 | std::string syncBuf; |
| 1711 | syncBuf += " objc_sync_exit("; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 1712 | |
| 1713 | Expr *syncExpr = S->getSynchExpr(); |
| 1714 | CastKind CK = syncExpr->getType()->isObjCObjectPointerType() |
| 1715 | ? CK_BitCast : |
| 1716 | syncExpr->getType()->isBlockPointerType() |
| 1717 | ? CK_BlockPointerToObjCPointerCast |
| 1718 | : CK_CPointerToObjCPointerCast; |
| 1719 | syncExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 1720 | CK, syncExpr); |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1721 | std::string syncExprBufS; |
| 1722 | llvm::raw_string_ostream syncExprBuf(syncExprBufS); |
Richard Smith | d1420c6 | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 1723 | syncExpr->printPretty(syncExprBuf, 0, PrintingPolicy(LangOpts)); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1724 | syncBuf += syncExprBuf.str(); |
| 1725 | syncBuf += ");"; |
| 1726 | |
| 1727 | buf += syncBuf; |
| 1728 | buf += "\n if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1729 | buf += "}\n"; |
| 1730 | buf += "}"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1731 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1732 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1733 | |
| 1734 | bool hasReturns = false; |
| 1735 | HasReturnStmts(S->getSynchBody(), hasReturns); |
| 1736 | if (hasReturns) |
| 1737 | RewriteSyncReturnStmts(S->getSynchBody(), syncBuf); |
| 1738 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1739 | return 0; |
| 1740 | } |
| 1741 | |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1742 | void RewriteObjC::WarnAboutReturnGotoStmts(Stmt *S) |
| 1743 | { |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1744 | // Perform a bottom up traversal of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1745 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1746 | if (*CI) |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1747 | WarnAboutReturnGotoStmts(*CI); |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1748 | |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1749 | if (isa<ReturnStmt>(S) || isa<GotoStmt>(S)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1750 | Diags.Report(Context->getFullLoc(S->getLocStart()), |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1751 | TryFinallyContainsReturnDiag); |
| 1752 | } |
| 1753 | return; |
| 1754 | } |
| 1755 | |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1756 | void RewriteObjC::HasReturnStmts(Stmt *S, bool &hasReturns) |
| 1757 | { |
| 1758 | // Perform a bottom up traversal of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1759 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1760 | if (*CI) |
| 1761 | HasReturnStmts(*CI, hasReturns); |
| 1762 | |
| 1763 | if (isa<ReturnStmt>(S)) |
| 1764 | hasReturns = true; |
| 1765 | return; |
| 1766 | } |
| 1767 | |
| 1768 | void RewriteObjC::RewriteTryReturnStmts(Stmt *S) { |
| 1769 | // Perform a bottom up traversal of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1770 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1771 | if (*CI) { |
| 1772 | RewriteTryReturnStmts(*CI); |
| 1773 | } |
| 1774 | if (isa<ReturnStmt>(S)) { |
| 1775 | SourceLocation startLoc = S->getLocStart(); |
| 1776 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1777 | |
| 1778 | const char *semiBuf = strchr(startBuf, ';'); |
| 1779 | assert((*semiBuf == ';') && "RewriteTryReturnStmts: can't find ';'"); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1780 | SourceLocation onePastSemiLoc = startLoc.getLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1781 | |
| 1782 | std::string buf; |
| 1783 | buf = "{ objc_exception_try_exit(&_stack); return"; |
| 1784 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1785 | ReplaceText(startLoc, 6, buf); |
| 1786 | InsertText(onePastSemiLoc, "}"); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1787 | } |
| 1788 | return; |
| 1789 | } |
| 1790 | |
| 1791 | void RewriteObjC::RewriteSyncReturnStmts(Stmt *S, std::string syncExitBuf) { |
| 1792 | // Perform a bottom up traversal of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1793 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1794 | if (*CI) { |
| 1795 | RewriteSyncReturnStmts(*CI, syncExitBuf); |
| 1796 | } |
| 1797 | if (isa<ReturnStmt>(S)) { |
| 1798 | SourceLocation startLoc = S->getLocStart(); |
| 1799 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1800 | |
| 1801 | const char *semiBuf = strchr(startBuf, ';'); |
| 1802 | assert((*semiBuf == ';') && "RewriteSyncReturnStmts: can't find ';'"); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1803 | SourceLocation onePastSemiLoc = startLoc.getLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1804 | |
| 1805 | std::string buf; |
| 1806 | buf = "{ objc_exception_try_exit(&_stack);"; |
| 1807 | buf += syncExitBuf; |
| 1808 | buf += " return"; |
| 1809 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1810 | ReplaceText(startLoc, 6, buf); |
| 1811 | InsertText(onePastSemiLoc, "}"); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1812 | } |
| 1813 | return; |
| 1814 | } |
| 1815 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1816 | Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1817 | // Get the start location and compute the semi location. |
| 1818 | SourceLocation startLoc = S->getLocStart(); |
| 1819 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1820 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1821 | assert((*startBuf == '@') && "bogus @try location"); |
| 1822 | |
| 1823 | std::string buf; |
| 1824 | // declare a new scope with two variables, _stack and _rethrow. |
| 1825 | buf = "/* @try scope begin */ { struct _objc_exception_data {\n"; |
| 1826 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1827 | buf += "char *pointers[4];} _stack;\n"; |
| 1828 | buf += "id volatile _rethrow = 0;\n"; |
| 1829 | buf += "objc_exception_try_enter(&_stack);\n"; |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1830 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1831 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1832 | ReplaceText(startLoc, 4, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1833 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1834 | startLoc = S->getTryBody()->getLocEnd(); |
| 1835 | startBuf = SM->getCharacterData(startLoc); |
| 1836 | |
| 1837 | assert((*startBuf == '}') && "bogus @try block"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1838 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1839 | SourceLocation lastCurlyLoc = startLoc; |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1840 | if (S->getNumCatchStmts()) { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1841 | startLoc = startLoc.getLocWithOffset(1); |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1842 | buf = " /* @catch begin */ else {\n"; |
| 1843 | buf += " id _caught = objc_exception_extract(&_stack);\n"; |
| 1844 | buf += " objc_exception_try_enter (&_stack);\n"; |
| 1845 | buf += " if (_setjmp(_stack.buf))\n"; |
| 1846 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1847 | buf += " else { /* @catch continue */"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1848 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1849 | InsertText(startLoc, buf); |
Steve Naroff | 8bd3dc6 | 2008-09-09 19:59:12 +0000 | [diff] [blame] | 1850 | } else { /* no catch list */ |
| 1851 | buf = "}\nelse {\n"; |
| 1852 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1853 | buf += "}"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1854 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1855 | } |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1856 | Stmt *lastCatchBody = 0; |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1857 | for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I) { |
| 1858 | ObjCAtCatchStmt *Catch = S->getCatchStmt(I); |
Douglas Gregor | c00d8e1 | 2010-04-26 16:46:50 +0000 | [diff] [blame] | 1859 | VarDecl *catchDecl = Catch->getCatchParamDecl(); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1860 | |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1861 | if (I == 0) |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1862 | buf = "if ("; // we are generating code for the first catch clause |
| 1863 | else |
| 1864 | buf = "else if ("; |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1865 | startLoc = Catch->getLocStart(); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1866 | startBuf = SM->getCharacterData(startLoc); |
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 | assert((*startBuf == '@') && "bogus @catch location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1869 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1870 | const char *lParenLoc = strchr(startBuf, '('); |
| 1871 | |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1872 | if (Catch->hasEllipsis()) { |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1873 | // Now rewrite the body... |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1874 | lastCatchBody = Catch->getCatchBody(); |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1875 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1876 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1877 | assert(*SM->getCharacterData(Catch->getRParenLoc()) == ')' && |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1878 | "bogus @catch paren location"); |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1879 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1880 | |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1881 | buf += "1) { id _tmp = _caught;"; |
Daniel Dunbar | d7407dc | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 1882 | Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, buf); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1883 | } else if (catchDecl) { |
| 1884 | QualType t = catchDecl->getType(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1885 | if (t == Context->getObjCIdType()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1886 | buf += "1) { "; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1887 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf); |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 1888 | } else if (const ObjCObjectPointerType *Ptr = |
| 1889 | t->getAs<ObjCObjectPointerType>()) { |
| 1890 | // Should be a pointer to a class. |
| 1891 | ObjCInterfaceDecl *IDecl = Ptr->getObjectType()->getInterface(); |
| 1892 | if (IDecl) { |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1893 | buf += "objc_exception_match((struct objc_class *)objc_getClass(\""; |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 1894 | buf += IDecl->getNameAsString(); |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1895 | buf += "\"), (struct objc_object *)_caught)) { "; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1896 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1897 | } |
| 1898 | } |
| 1899 | // Now rewrite the body... |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1900 | lastCatchBody = Catch->getCatchBody(); |
| 1901 | SourceLocation rParenLoc = Catch->getRParenLoc(); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1902 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1903 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 1904 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 1905 | assert((*rParenBuf == ')') && "bogus @catch paren location"); |
| 1906 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1907 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1908 | // Here we replace ") {" with "= _caught;" (which initializes and |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1909 | // declares the @catch parameter). |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1910 | ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, " = _caught;"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1911 | } else { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1912 | llvm_unreachable("@catch rewrite bug"); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1913 | } |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1914 | } |
| 1915 | // Complete the catch list... |
| 1916 | if (lastCatchBody) { |
| 1917 | SourceLocation bodyLoc = lastCatchBody->getLocEnd(); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1918 | assert(*SM->getCharacterData(bodyLoc) == '}' && |
| 1919 | "bogus @catch body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1920 | |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1921 | // Insert the last (implicit) else clause *before* the right curly brace. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1922 | bodyLoc = bodyLoc.getLocWithOffset(-1); |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1923 | buf = "} /* last catch end */\n"; |
| 1924 | buf += "else {\n"; |
| 1925 | buf += " _rethrow = _caught;\n"; |
| 1926 | buf += " objc_exception_try_exit(&_stack);\n"; |
| 1927 | buf += "} } /* @catch end */\n"; |
| 1928 | if (!S->getFinallyStmt()) |
| 1929 | buf += "}\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1930 | InsertText(bodyLoc, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1931 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1932 | // Set lastCurlyLoc |
| 1933 | lastCurlyLoc = lastCatchBody->getLocEnd(); |
| 1934 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1935 | if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1936 | startLoc = finalStmt->getLocStart(); |
| 1937 | startBuf = SM->getCharacterData(startLoc); |
| 1938 | assert((*startBuf == '@') && "bogus @finally start"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1939 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1940 | ReplaceText(startLoc, 8, "/* @finally */"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1941 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1942 | Stmt *body = finalStmt->getFinallyBody(); |
| 1943 | SourceLocation startLoc = body->getLocStart(); |
| 1944 | SourceLocation endLoc = body->getLocEnd(); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1945 | assert(*SM->getCharacterData(startLoc) == '{' && |
| 1946 | "bogus @finally body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1947 | assert(*SM->getCharacterData(endLoc) == '}' && |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1948 | "bogus @finally body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1949 | |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1950 | startLoc = startLoc.getLocWithOffset(1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1951 | InsertText(startLoc, " if (!_rethrow) objc_exception_try_exit(&_stack);\n"); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1952 | endLoc = endLoc.getLocWithOffset(-1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1953 | InsertText(endLoc, " if (_rethrow) objc_exception_throw(_rethrow);\n"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1954 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1955 | // Set lastCurlyLoc |
| 1956 | lastCurlyLoc = body->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1957 | |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1958 | // Now check for any return/continue/go statements within the @try. |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1959 | WarnAboutReturnGotoStmts(S->getTryBody()); |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1960 | } else { /* no finally clause - make sure we synthesize an implicit one */ |
| 1961 | buf = "{ /* implicit finally clause */\n"; |
| 1962 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
| 1963 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 1964 | buf += "}"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1965 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1966 | |
| 1967 | // Now check for any return/continue/go statements within the @try. |
| 1968 | // The implicit finally clause won't called if the @try contains any |
| 1969 | // jump statements. |
| 1970 | bool hasReturns = false; |
| 1971 | HasReturnStmts(S->getTryBody(), hasReturns); |
| 1972 | if (hasReturns) |
| 1973 | RewriteTryReturnStmts(S->getTryBody()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1974 | } |
| 1975 | // Now emit the final closing curly brace... |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1976 | lastCurlyLoc = lastCurlyLoc.getLocWithOffset(1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1977 | InsertText(lastCurlyLoc, " } /* @try scope end */\n"); |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1978 | return 0; |
| 1979 | } |
| 1980 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1981 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
| 1982 | // the throw expression is typically a message expression that's already |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1983 | // been rewritten! (which implies the SourceLocation's are invalid). |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1984 | Stmt *RewriteObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1985 | // Get the start location and compute the semi location. |
| 1986 | SourceLocation startLoc = S->getLocStart(); |
| 1987 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1988 | |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1989 | assert((*startBuf == '@') && "bogus @throw location"); |
| 1990 | |
| 1991 | std::string buf; |
| 1992 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
Steve Naroff | 20ebf8f | 2008-01-19 00:42:38 +0000 | [diff] [blame] | 1993 | if (S->getThrowExpr()) |
| 1994 | buf = "objc_exception_throw("; |
| 1995 | else // add an implicit argument |
| 1996 | buf = "objc_exception_throw(_caught"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1997 | |
Steve Naroff | 4ba0acb | 2008-07-25 15:41:30 +0000 | [diff] [blame] | 1998 | // handle "@ throw" correctly. |
| 1999 | const char *wBuf = strchr(startBuf, 'w'); |
| 2000 | assert((*wBuf == 'w') && "@throw: can't find 'w'"); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2001 | ReplaceText(startLoc, wBuf-startBuf+1, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2002 | |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2003 | const char *semiBuf = strchr(startBuf, ';'); |
| 2004 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2005 | SourceLocation semiLoc = startLoc.getLocWithOffset(semiBuf-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2006 | ReplaceText(semiLoc, 1, ");"); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2007 | return 0; |
| 2008 | } |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 2009 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2010 | Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 2011 | // Create a new string expression. |
| 2012 | QualType StrType = Context->getPointerType(Context->CharTy); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2013 | std::string StrEncoding; |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 2014 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2015 | Expr *Replacement = StringLiteral::Create(*Context, StrEncoding, |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2016 | StringLiteral::Ascii, false, |
| 2017 | StrType, SourceLocation()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2018 | ReplaceStmt(Exp, Replacement); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2019 | |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 2020 | // Replace this subexpr in the parent. |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2021 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 2022 | return Replacement; |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 2023 | } |
| 2024 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2025 | Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
Steve Naroff | 1a93764 | 2008-12-22 22:16:07 +0000 | [diff] [blame] | 2026 | if (!SelGetUidFunctionDecl) |
| 2027 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2028 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 2029 | // Create a call to sel_registerName("selName"). |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2030 | SmallVector<Expr*, 8> SelExprs; |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2031 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2032 | SelExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2033 | Exp->getSelector().getAsString(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2034 | StringLiteral::Ascii, false, |
| 2035 | argType, SourceLocation())); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2036 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2037 | &SelExprs[0], SelExprs.size()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2038 | ReplaceStmt(Exp, SelExp); |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2039 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2040 | return SelExp; |
| 2041 | } |
| 2042 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2043 | CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl( |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2044 | FunctionDecl *FD, Expr **args, unsigned nargs, SourceLocation StartLoc, |
| 2045 | SourceLocation EndLoc) { |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2046 | // 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] | 2047 | QualType msgSendType = FD->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2048 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2049 | // Create a reference to the objc_msgSend() declaration. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2050 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, false, msgSendType, |
| 2051 | VK_LValue, SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2052 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2053 | // 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] | 2054 | QualType pToFunc = Context->getPointerType(msgSendType); |
Anders Carlsson | 88465d3 | 2010-04-23 22:18:37 +0000 | [diff] [blame] | 2055 | ImplicitCastExpr *ICE = |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2056 | ImplicitCastExpr::Create(*Context, pToFunc, CK_FunctionToPointerDecay, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2057 | DRE, 0, VK_RValue); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2058 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2059 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2060 | |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2061 | CallExpr *Exp = |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2062 | new (Context) CallExpr(*Context, ICE, llvm::makeArrayRef(args, nargs), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2063 | FT->getCallResultType(*Context), |
| 2064 | VK_RValue, EndLoc); |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2065 | return Exp; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2066 | } |
| 2067 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2068 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 2069 | const char *&startRef, const char *&endRef) { |
| 2070 | while (startBuf < endBuf) { |
| 2071 | if (*startBuf == '<') |
| 2072 | startRef = startBuf; // mark the start. |
| 2073 | if (*startBuf == '>') { |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 2074 | if (startRef && *startRef == '<') { |
| 2075 | endRef = startBuf; // mark the end. |
| 2076 | return true; |
| 2077 | } |
| 2078 | return false; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2079 | } |
| 2080 | startBuf++; |
| 2081 | } |
| 2082 | return false; |
| 2083 | } |
| 2084 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2085 | static void scanToNextArgument(const char *&argRef) { |
| 2086 | int angle = 0; |
| 2087 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 2088 | if (*argRef == '<') |
| 2089 | angle++; |
| 2090 | else if (*argRef == '>') |
| 2091 | angle--; |
| 2092 | argRef++; |
| 2093 | } |
| 2094 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 2095 | } |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2096 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2097 | bool RewriteObjC::needToScanForQualifiers(QualType T) { |
Fariborz Jahanian | 32132a0 | 2010-02-03 21:29:28 +0000 | [diff] [blame] | 2098 | if (T->isObjCQualifiedIdType()) |
| 2099 | return true; |
Fariborz Jahanian | 84aa946 | 2010-02-02 18:35:07 +0000 | [diff] [blame] | 2100 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 2101 | if (PT->getPointeeType()->isObjCQualifiedIdType()) |
| 2102 | return true; |
| 2103 | } |
| 2104 | if (T->isObjCObjectPointerType()) { |
| 2105 | T = T->getPointeeType(); |
| 2106 | return T->isObjCQualifiedInterfaceType(); |
| 2107 | } |
Fariborz Jahanian | 24f9cab | 2010-09-30 20:41:32 +0000 | [diff] [blame] | 2108 | if (T->isArrayType()) { |
| 2109 | QualType ElemTy = Context->getBaseElementType(T); |
| 2110 | return needToScanForQualifiers(ElemTy); |
| 2111 | } |
Fariborz Jahanian | 84aa946 | 2010-02-02 18:35:07 +0000 | [diff] [blame] | 2112 | return false; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2113 | } |
| 2114 | |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2115 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) { |
| 2116 | QualType Type = E->getType(); |
| 2117 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2118 | SourceLocation Loc, EndLoc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2119 | |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2120 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) { |
| 2121 | Loc = ECE->getLParenLoc(); |
| 2122 | EndLoc = ECE->getRParenLoc(); |
| 2123 | } else { |
| 2124 | Loc = E->getLocStart(); |
| 2125 | EndLoc = E->getLocEnd(); |
| 2126 | } |
| 2127 | // This will defend against trying to rewrite synthesized expressions. |
| 2128 | if (Loc.isInvalid() || EndLoc.isInvalid()) |
| 2129 | return; |
| 2130 | |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2131 | const char *startBuf = SM->getCharacterData(Loc); |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2132 | const char *endBuf = SM->getCharacterData(EndLoc); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2133 | const char *startRef = 0, *endRef = 0; |
| 2134 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2135 | // Get the locations of the startRef, endRef. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2136 | SourceLocation LessLoc = Loc.getLocWithOffset(startRef-startBuf); |
| 2137 | SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-startBuf+1); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2138 | // Comment out the protocol references. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2139 | InsertText(LessLoc, "/*"); |
| 2140 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2141 | } |
| 2142 | } |
| 2143 | } |
| 2144 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2145 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2146 | SourceLocation Loc; |
| 2147 | QualType Type; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2148 | const FunctionProtoType *proto = 0; |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2149 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 2150 | Loc = VD->getLocation(); |
| 2151 | Type = VD->getType(); |
| 2152 | } |
| 2153 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 2154 | Loc = FD->getLocation(); |
| 2155 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 2156 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2157 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2158 | assert(funcType && "missing function type"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2159 | proto = dyn_cast<FunctionProtoType>(funcType); |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2160 | if (!proto) |
| 2161 | return; |
| 2162 | Type = proto->getResultType(); |
| 2163 | } |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 2164 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(Dcl)) { |
| 2165 | Loc = FD->getLocation(); |
| 2166 | Type = FD->getType(); |
| 2167 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2168 | else |
| 2169 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2170 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2171 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2172 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2173 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2174 | const char *endBuf = SM->getCharacterData(Loc); |
| 2175 | const char *startBuf = endBuf; |
Steve Naroff | 6cafbf2 | 2008-05-31 05:02:17 +0000 | [diff] [blame] | 2176 | while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2177 | startBuf--; // scan backward (from the decl location) for return type. |
| 2178 | const char *startRef = 0, *endRef = 0; |
| 2179 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2180 | // Get the locations of the startRef, endRef. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2181 | SourceLocation LessLoc = Loc.getLocWithOffset(startRef-endBuf); |
| 2182 | SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-endBuf+1); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2183 | // Comment out the protocol references. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2184 | InsertText(LessLoc, "/*"); |
| 2185 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2186 | } |
| 2187 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2188 | if (!proto) |
| 2189 | return; // most likely, was a variable |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2190 | // Now check arguments. |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2191 | const char *startBuf = SM->getCharacterData(Loc); |
| 2192 | const char *startFuncBuf = startBuf; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2193 | for (unsigned i = 0; i < proto->getNumArgs(); i++) { |
| 2194 | if (needToScanForQualifiers(proto->getArgType(i))) { |
| 2195 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2196 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2197 | const char *endBuf = startBuf; |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2198 | // scan forward (from the decl location) for argument types. |
| 2199 | scanToNextArgument(endBuf); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2200 | const char *startRef = 0, *endRef = 0; |
| 2201 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2202 | // Get the locations of the startRef, endRef. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2203 | SourceLocation LessLoc = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2204 | Loc.getLocWithOffset(startRef-startFuncBuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2205 | SourceLocation GreaterLoc = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2206 | Loc.getLocWithOffset(endRef-startFuncBuf+1); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2207 | // Comment out the protocol references. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2208 | InsertText(LessLoc, "/*"); |
| 2209 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2210 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2211 | startBuf = ++endBuf; |
| 2212 | } |
| 2213 | else { |
Steve Naroff | aba49d1 | 2008-08-06 15:58:23 +0000 | [diff] [blame] | 2214 | // If the function name is derived from a macro expansion, then the |
| 2215 | // argument buffer will not follow the name. Need to speak with Chris. |
| 2216 | while (*startBuf && *startBuf != ')' && *startBuf != ',') |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2217 | startBuf++; // scan forward (from the decl location) for argument types. |
| 2218 | startBuf++; |
| 2219 | } |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2220 | } |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2223 | void RewriteObjC::RewriteTypeOfDecl(VarDecl *ND) { |
| 2224 | QualType QT = ND->getType(); |
| 2225 | const Type* TypePtr = QT->getAs<Type>(); |
| 2226 | if (!isa<TypeOfExprType>(TypePtr)) |
| 2227 | return; |
| 2228 | while (isa<TypeOfExprType>(TypePtr)) { |
| 2229 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 2230 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 2231 | TypePtr = QT->getAs<Type>(); |
| 2232 | } |
| 2233 | // FIXME. This will not work for multiple declarators; as in: |
| 2234 | // __typeof__(a) b,c,d; |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2235 | std::string TypeAsString(QT.getAsString(Context->getPrintingPolicy())); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2236 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
| 2237 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 2238 | if (ND->getInit()) { |
| 2239 | std::string Name(ND->getNameAsString()); |
| 2240 | TypeAsString += " " + Name + " = "; |
| 2241 | Expr *E = ND->getInit(); |
| 2242 | SourceLocation startLoc; |
| 2243 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 2244 | startLoc = ECE->getLParenLoc(); |
| 2245 | else |
| 2246 | startLoc = E->getLocStart(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 2247 | startLoc = SM->getExpansionLoc(startLoc); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2248 | const char *endBuf = SM->getCharacterData(startLoc); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2249 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2250 | } |
| 2251 | else { |
| 2252 | SourceLocation X = ND->getLocEnd(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 2253 | X = SM->getExpansionLoc(X); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2254 | const char *endBuf = SM->getCharacterData(X); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2255 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2256 | } |
| 2257 | } |
| 2258 | |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2259 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2260 | void RewriteObjC::SynthSelGetUidFunctionDecl() { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2261 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2262 | SmallVector<QualType, 16> ArgTys; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2263 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2264 | QualType getFuncType = |
| 2265 | getSimpleFunctionType(Context->getObjCSelType(), &ArgTys[0], ArgTys.size()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2266 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2267 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2268 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2269 | SelGetUidIdent, getFuncType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2270 | SC_Extern, |
| 2271 | SC_None, false); |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2272 | } |
| 2273 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2274 | void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2275 | // declared in <objc/objc.h> |
Douglas Gregor | 51efe56 | 2009-01-09 01:47:02 +0000 | [diff] [blame] | 2276 | if (FD->getIdentifier() && |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 2277 | FD->getName() == "sel_registerName") { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2278 | SelGetUidFunctionDecl = FD; |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2279 | return; |
| 2280 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2281 | RewriteObjCQualifiedInterfaceTypes(FD); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2282 | } |
| 2283 | |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2284 | void RewriteObjC::RewriteBlockPointerType(std::string& Str, QualType Type) { |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2285 | std::string TypeString(Type.getAsString(Context->getPrintingPolicy())); |
Fariborz Jahanian | 52b2e1e | 2010-02-12 17:52:31 +0000 | [diff] [blame] | 2286 | const char *argPtr = TypeString.c_str(); |
| 2287 | if (!strchr(argPtr, '^')) { |
| 2288 | Str += TypeString; |
| 2289 | return; |
| 2290 | } |
| 2291 | while (*argPtr) { |
| 2292 | Str += (*argPtr == '^' ? '*' : *argPtr); |
| 2293 | argPtr++; |
| 2294 | } |
| 2295 | } |
| 2296 | |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2297 | // FIXME. Consolidate this routine with RewriteBlockPointerType. |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2298 | void RewriteObjC::RewriteBlockPointerTypeVariable(std::string& Str, |
| 2299 | ValueDecl *VD) { |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2300 | QualType Type = VD->getType(); |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2301 | std::string TypeString(Type.getAsString(Context->getPrintingPolicy())); |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2302 | const char *argPtr = TypeString.c_str(); |
| 2303 | int paren = 0; |
| 2304 | while (*argPtr) { |
| 2305 | switch (*argPtr) { |
| 2306 | case '(': |
| 2307 | Str += *argPtr; |
| 2308 | paren++; |
| 2309 | break; |
| 2310 | case ')': |
| 2311 | Str += *argPtr; |
| 2312 | paren--; |
| 2313 | break; |
| 2314 | case '^': |
| 2315 | Str += '*'; |
| 2316 | if (paren == 1) |
| 2317 | Str += VD->getNameAsString(); |
| 2318 | break; |
| 2319 | default: |
| 2320 | Str += *argPtr; |
| 2321 | break; |
| 2322 | } |
| 2323 | argPtr++; |
| 2324 | } |
| 2325 | } |
| 2326 | |
| 2327 | |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2328 | void RewriteObjC::RewriteBlockLiteralFunctionDecl(FunctionDecl *FD) { |
| 2329 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
| 2330 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
| 2331 | const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(funcType); |
| 2332 | if (!proto) |
| 2333 | return; |
| 2334 | QualType Type = proto->getResultType(); |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2335 | std::string FdStr = Type.getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2336 | FdStr += " "; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 2337 | FdStr += FD->getName(); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2338 | FdStr += "("; |
| 2339 | unsigned numArgs = proto->getNumArgs(); |
| 2340 | for (unsigned i = 0; i < numArgs; i++) { |
| 2341 | QualType ArgType = proto->getArgType(i); |
Fariborz Jahanian | 52b2e1e | 2010-02-12 17:52:31 +0000 | [diff] [blame] | 2342 | RewriteBlockPointerType(FdStr, ArgType); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2343 | if (i+1 < numArgs) |
| 2344 | FdStr += ", "; |
| 2345 | } |
| 2346 | FdStr += ");\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2347 | InsertText(FunLocStart, FdStr); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2348 | CurFunctionDeclToDeclareForBlock = 0; |
| 2349 | } |
| 2350 | |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2351 | // SynthSuperContructorFunctionDecl - id objc_super(id obj, id super); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2352 | void RewriteObjC::SynthSuperContructorFunctionDecl() { |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2353 | if (SuperContructorFunctionDecl) |
| 2354 | return; |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2355 | IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2356 | SmallVector<QualType, 16> ArgTys; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2357 | QualType argT = Context->getObjCIdType(); |
| 2358 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2359 | ArgTys.push_back(argT); |
| 2360 | ArgTys.push_back(argT); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2361 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2362 | &ArgTys[0], ArgTys.size()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2363 | SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2364 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2365 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2366 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2367 | SC_Extern, |
| 2368 | SC_None, false); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2369 | } |
| 2370 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2371 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2372 | void RewriteObjC::SynthMsgSendFunctionDecl() { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2373 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2374 | SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2375 | QualType argT = Context->getObjCIdType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2376 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2377 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2378 | argT = Context->getObjCSelType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2379 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2380 | ArgTys.push_back(argT); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2381 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2382 | &ArgTys[0], ArgTys.size(), |
| 2383 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2384 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2385 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2386 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2387 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2388 | SC_Extern, |
| 2389 | SC_None, false); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2390 | } |
| 2391 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2392 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2393 | void RewriteObjC::SynthMsgSendSuperFunctionDecl() { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2394 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2395 | SmallVector<QualType, 16> ArgTys; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2396 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2397 | SourceLocation(), SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2398 | &Context->Idents.get("objc_super")); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2399 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2400 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2401 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2402 | argT = Context->getObjCSelType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2403 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2404 | ArgTys.push_back(argT); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2405 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2406 | &ArgTys[0], ArgTys.size(), |
| 2407 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2408 | MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2409 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2410 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2411 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2412 | SC_Extern, |
| 2413 | SC_None, false); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2414 | } |
| 2415 | |
Fariborz Jahanian | 336c8f7 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2416 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2417 | void RewriteObjC::SynthMsgSendStretFunctionDecl() { |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2418 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2419 | SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2420 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2421 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2422 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2423 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2424 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2425 | ArgTys.push_back(argT); |
Fariborz Jahanian | 336c8f7 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2426 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2427 | &ArgTys[0], ArgTys.size(), |
| 2428 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2429 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2430 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2431 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2432 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2433 | SC_Extern, |
| 2434 | SC_None, false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2435 | } |
| 2436 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2437 | // SynthMsgSendSuperStretFunctionDecl - |
Fariborz Jahanian | 336c8f7 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2438 | // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2439 | void RewriteObjC::SynthMsgSendSuperStretFunctionDecl() { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2440 | IdentifierInfo *msgSendIdent = |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2441 | &Context->Idents.get("objc_msgSendSuper_stret"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2442 | SmallVector<QualType, 16> ArgTys; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2443 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2444 | SourceLocation(), SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2445 | &Context->Idents.get("objc_super")); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2446 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2447 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2448 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2449 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2450 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2451 | ArgTys.push_back(argT); |
Fariborz Jahanian | 336c8f7 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2452 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2453 | &ArgTys[0], ArgTys.size(), |
| 2454 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2455 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2456 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2457 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2458 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2459 | SC_Extern, |
| 2460 | SC_None, false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2461 | } |
| 2462 | |
Steve Naroff | 1284db8 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2463 | // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2464 | void RewriteObjC::SynthMsgSendFpretFunctionDecl() { |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2465 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2466 | SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2467 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2468 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2469 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2470 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2471 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2472 | ArgTys.push_back(argT); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2473 | QualType msgSendType = getSimpleFunctionType(Context->DoubleTy, |
| 2474 | &ArgTys[0], ArgTys.size(), |
| 2475 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2476 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2477 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2478 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2479 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2480 | SC_Extern, |
| 2481 | SC_None, false); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2482 | } |
| 2483 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2484 | // SynthGetClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2485 | void RewriteObjC::SynthGetClassFunctionDecl() { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2486 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2487 | SmallVector<QualType, 16> ArgTys; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2488 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2489 | QualType getClassType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2490 | &ArgTys[0], ArgTys.size()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2491 | GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2492 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2493 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2494 | getClassIdent, getClassType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2495 | SC_Extern, |
| 2496 | SC_None, false); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2497 | } |
| 2498 | |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2499 | // SynthGetSuperClassFunctionDecl - Class class_getSuperclass(Class cls); |
| 2500 | void RewriteObjC::SynthGetSuperClassFunctionDecl() { |
| 2501 | IdentifierInfo *getSuperClassIdent = |
| 2502 | &Context->Idents.get("class_getSuperclass"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2503 | SmallVector<QualType, 16> ArgTys; |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2504 | ArgTys.push_back(Context->getObjCClassType()); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2505 | QualType getClassType = getSimpleFunctionType(Context->getObjCClassType(), |
| 2506 | &ArgTys[0], ArgTys.size()); |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2507 | GetSuperClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2508 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2509 | SourceLocation(), |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2510 | getSuperClassIdent, |
| 2511 | getClassType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2512 | SC_Extern, |
| 2513 | SC_None, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2514 | false); |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2515 | } |
| 2516 | |
Fariborz Jahanian | 97bbab2 | 2011-12-21 19:48:07 +0000 | [diff] [blame] | 2517 | // SynthGetMetaClassFunctionDecl - id objc_getMetaClass(const char *name); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2518 | void RewriteObjC::SynthGetMetaClassFunctionDecl() { |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2519 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2520 | SmallVector<QualType, 16> ArgTys; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2521 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2522 | QualType getClassType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2523 | &ArgTys[0], ArgTys.size()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2524 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2525 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2526 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2527 | getClassIdent, getClassType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2528 | SC_Extern, |
| 2529 | SC_None, false); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2530 | } |
| 2531 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2532 | Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2533 | QualType strType = getConstantStringStructType(); |
| 2534 | |
| 2535 | std::string S = "__NSConstantStringImpl_"; |
Steve Naroff | 7691d9b | 2008-05-31 03:35:42 +0000 | [diff] [blame] | 2536 | |
| 2537 | std::string tmpName = InFileName; |
| 2538 | unsigned i; |
| 2539 | for (i=0; i < tmpName.length(); i++) { |
| 2540 | char c = tmpName.at(i); |
| 2541 | // replace any non alphanumeric characters with '_'. |
| 2542 | if (!isalpha(c) && (c < '0' || c > '9')) |
| 2543 | tmpName[i] = '_'; |
| 2544 | } |
| 2545 | S += tmpName; |
| 2546 | S += "_"; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2547 | S += utostr(NumObjCStringLiterals++); |
| 2548 | |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2549 | Preamble += "static __NSConstantStringImpl " + S; |
| 2550 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 2551 | Preamble += "0x000007c8,"; // utf8_str |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2552 | // The pretty printer for StringLiteral handles escape characters properly. |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2553 | std::string prettyBufS; |
| 2554 | llvm::raw_string_ostream prettyBuf(prettyBufS); |
Richard Smith | d1420c6 | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 2555 | Exp->getString()->printPretty(prettyBuf, 0, PrintingPolicy(LangOpts)); |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2556 | Preamble += prettyBuf.str(); |
| 2557 | Preamble += ","; |
Steve Naroff | fd5b76f | 2009-12-06 01:48:44 +0000 | [diff] [blame] | 2558 | Preamble += utostr(Exp->getString()->getByteLength()) + "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2559 | |
| 2560 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2561 | SourceLocation(), &Context->Idents.get(S), |
| 2562 | strType, 0, SC_Static, SC_None); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2563 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, false, strType, VK_LValue, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2564 | SourceLocation()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2565 | Expr *Unop = new (Context) UnaryOperator(DRE, UO_AddrOf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2566 | Context->getPointerType(DRE->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2567 | VK_RValue, OK_Ordinary, |
| 2568 | SourceLocation()); |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2569 | // cast to NSConstantString * |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2570 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Exp->getType(), |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2571 | CK_CPointerToObjCPointerCast, Unop); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2572 | ReplaceStmt(Exp, cast); |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2573 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2574 | return cast; |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2575 | } |
| 2576 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2577 | // struct objc_super { struct objc_object *receiver; struct objc_class *super; }; |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2578 | QualType RewriteObjC::getSuperStructType() { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2579 | if (!SuperStructDecl) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2580 | SuperStructDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2581 | SourceLocation(), SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2582 | &Context->Idents.get("objc_super")); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2583 | QualType FieldTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2584 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2585 | // struct objc_object *receiver; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2586 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2587 | // struct objc_class *super; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2588 | FieldTypes[1] = Context->getObjCClassType(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2589 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2590 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2591 | for (unsigned i = 0; i < 2; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2592 | SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2593 | SourceLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2594 | SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2595 | FieldTypes[i], 0, |
| 2596 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2597 | /*Mutable=*/false, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2598 | ICIS_NoInit)); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2599 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2600 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2601 | SuperStructDecl->completeDefinition(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2602 | } |
| 2603 | return Context->getTagDeclType(SuperStructDecl); |
| 2604 | } |
| 2605 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2606 | QualType RewriteObjC::getConstantStringStructType() { |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2607 | if (!ConstantStringDecl) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2608 | ConstantStringDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2609 | SourceLocation(), SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2610 | &Context->Idents.get("__NSConstantStringImpl")); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2611 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2612 | |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2613 | // struct objc_object *receiver; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2614 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2615 | // int flags; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2616 | FieldTypes[1] = Context->IntTy; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2617 | // char *str; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2618 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2619 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2620 | FieldTypes[3] = Context->LongTy; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2621 | |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2622 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2623 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2624 | ConstantStringDecl->addDecl(FieldDecl::Create(*Context, |
| 2625 | ConstantStringDecl, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2626 | SourceLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2627 | SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2628 | FieldTypes[i], 0, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2629 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2630 | /*Mutable=*/true, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2631 | ICIS_NoInit)); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2632 | } |
| 2633 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2634 | ConstantStringDecl->completeDefinition(); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2635 | } |
| 2636 | return Context->getTagDeclType(ConstantStringDecl); |
| 2637 | } |
| 2638 | |
Fariborz Jahanian | 9fc5b00 | 2012-06-28 21:20:35 +0000 | [diff] [blame] | 2639 | CallExpr *RewriteObjC::SynthMsgSendStretCallExpr(FunctionDecl *MsgSendStretFlavor, |
| 2640 | QualType msgSendType, |
| 2641 | QualType returnType, |
| 2642 | SmallVectorImpl<QualType> &ArgTypes, |
| 2643 | SmallVectorImpl<Expr*> &MsgExprs, |
| 2644 | ObjCMethodDecl *Method) { |
| 2645 | // Create a reference to the objc_msgSend_stret() declaration. |
| 2646 | DeclRefExpr *STDRE = new (Context) DeclRefExpr(MsgSendStretFlavor, |
| 2647 | false, msgSendType, |
| 2648 | VK_LValue, SourceLocation()); |
| 2649 | // Need to cast objc_msgSend_stret to "void *" (see above comment). |
| 2650 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, |
| 2651 | Context->getPointerType(Context->VoidTy), |
| 2652 | CK_BitCast, STDRE); |
| 2653 | // Now do the "normal" pointer to function cast. |
| 2654 | QualType castType = getSimpleFunctionType(returnType, &ArgTypes[0], ArgTypes.size(), |
| 2655 | Method ? Method->isVariadic() : false); |
| 2656 | castType = Context->getPointerType(castType); |
| 2657 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
| 2658 | cast); |
| 2659 | |
| 2660 | // Don't forget the parens to enforce the proper binding. |
| 2661 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
| 2662 | |
| 2663 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2664 | CallExpr *STCE = new (Context) CallExpr(*Context, PE, MsgExprs, |
Fariborz Jahanian | 9fc5b00 | 2012-06-28 21:20:35 +0000 | [diff] [blame] | 2665 | FT->getResultType(), VK_RValue, |
| 2666 | SourceLocation()); |
| 2667 | return STCE; |
| 2668 | |
| 2669 | } |
| 2670 | |
| 2671 | |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2672 | Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp, |
| 2673 | SourceLocation StartLoc, |
| 2674 | SourceLocation EndLoc) { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2675 | if (!SelGetUidFunctionDecl) |
| 2676 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2677 | if (!MsgSendFunctionDecl) |
| 2678 | SynthMsgSendFunctionDecl(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2679 | if (!MsgSendSuperFunctionDecl) |
| 2680 | SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2681 | if (!MsgSendStretFunctionDecl) |
| 2682 | SynthMsgSendStretFunctionDecl(); |
| 2683 | if (!MsgSendSuperStretFunctionDecl) |
| 2684 | SynthMsgSendSuperStretFunctionDecl(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2685 | if (!MsgSendFpretFunctionDecl) |
| 2686 | SynthMsgSendFpretFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2687 | if (!GetClassFunctionDecl) |
| 2688 | SynthGetClassFunctionDecl(); |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2689 | if (!GetSuperClassFunctionDecl) |
| 2690 | SynthGetSuperClassFunctionDecl(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2691 | if (!GetMetaClassFunctionDecl) |
| 2692 | SynthGetMetaClassFunctionDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2693 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2694 | // default to objc_msgSend(). |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2695 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2696 | // May need to use objc_msgSend_stret() as well. |
| 2697 | FunctionDecl *MsgSendStretFlavor = 0; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2698 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
| 2699 | QualType resultType = mDecl->getResultType(); |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 2700 | if (resultType->isRecordType()) |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2701 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
Chris Lattner | 8b51fd7 | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2702 | else if (resultType->isRealFloatingType()) |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2703 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2704 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2705 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2706 | // Synthesize a call to objc_msgSend(). |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2707 | SmallVector<Expr*, 8> MsgExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2708 | switch (Exp->getReceiverKind()) { |
| 2709 | case ObjCMessageExpr::SuperClass: { |
| 2710 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2711 | if (MsgSendStretFlavor) |
| 2712 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2713 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2714 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2715 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2716 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2717 | SmallVector<Expr*, 4> InitExprs; |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2718 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2719 | // set the receiver to self, the first argument to all methods. |
| 2720 | InitExprs.push_back( |
| 2721 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2722 | CK_BitCast, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2723 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2724 | false, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2725 | Context->getObjCIdType(), |
| 2726 | VK_RValue, |
| 2727 | SourceLocation())) |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2728 | ); // set the 'receiver'. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2729 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2730 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2731 | SmallVector<Expr*, 8> ClsExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2732 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2733 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2734 | ClassDecl->getIdentifier()->getName(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2735 | StringLiteral::Ascii, false, |
| 2736 | argType, SourceLocation())); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2737 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
| 2738 | &ClsExprs[0], |
| 2739 | ClsExprs.size(), |
| 2740 | StartLoc, |
| 2741 | EndLoc); |
| 2742 | // (Class)objc_getClass("CurrentClass") |
| 2743 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 2744 | Context->getObjCClassType(), |
Fariborz Jahanian | 97bbab2 | 2011-12-21 19:48:07 +0000 | [diff] [blame] | 2745 | CK_BitCast, Cls); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2746 | ClsExprs.clear(); |
| 2747 | ClsExprs.push_back(ArgExpr); |
| 2748 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 2749 | &ClsExprs[0], ClsExprs.size(), |
| 2750 | StartLoc, EndLoc); |
| 2751 | |
| 2752 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2753 | // To turn off a warning, type-cast to 'id' |
| 2754 | InitExprs.push_back( // set 'super class', using class_getSuperclass(). |
| 2755 | NoTypeInfoCStyleCastExpr(Context, |
| 2756 | Context->getObjCIdType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2757 | CK_BitCast, Cls)); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2758 | // struct objc_super |
| 2759 | QualType superType = getSuperStructType(); |
| 2760 | Expr *SuperRep; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2761 | |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 2762 | if (LangOpts.MicrosoftExt) { |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2763 | SynthSuperContructorFunctionDecl(); |
| 2764 | // Simulate a contructor call... |
| 2765 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2766 | false, superType, VK_LValue, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2767 | SourceLocation()); |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2768 | SuperRep = new (Context) CallExpr(*Context, DRE, InitExprs, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2769 | superType, VK_LValue, |
| 2770 | SourceLocation()); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2771 | // The code for super is a little tricky to prevent collision with |
| 2772 | // the structure definition in the header. The rewriter has it's own |
| 2773 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2774 | // we need the cast below. For example: |
| 2775 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2776 | // |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2777 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2778 | Context->getPointerType(SuperRep->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2779 | VK_RValue, OK_Ordinary, |
| 2780 | SourceLocation()); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2781 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2782 | Context->getPointerType(superType), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2783 | CK_BitCast, SuperRep); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2784 | } else { |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2785 | // (struct objc_super) { <exprs from above> } |
| 2786 | InitListExpr *ILE = |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2787 | new (Context) InitListExpr(*Context, SourceLocation(), InitExprs, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2788 | SourceLocation()); |
| 2789 | TypeSourceInfo *superTInfo |
| 2790 | = Context->getTrivialTypeSourceInfo(superType); |
| 2791 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2792 | superType, VK_LValue, |
| 2793 | ILE, false); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2794 | // struct objc_super * |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2795 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2796 | Context->getPointerType(SuperRep->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2797 | VK_RValue, OK_Ordinary, |
| 2798 | SourceLocation()); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2799 | } |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2800 | MsgExprs.push_back(SuperRep); |
| 2801 | break; |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2802 | } |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2803 | |
| 2804 | case ObjCMessageExpr::Class: { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2805 | SmallVector<Expr*, 8> ClsExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2806 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2807 | ObjCInterfaceDecl *Class |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 2808 | = Exp->getClassReceiver()->getAs<ObjCObjectType>()->getInterface(); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2809 | IdentifierInfo *clsName = Class->getIdentifier(); |
| 2810 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2811 | clsName->getName(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2812 | StringLiteral::Ascii, false, |
Anders Carlsson | 3e2193c | 2011-04-14 00:40:03 +0000 | [diff] [blame] | 2813 | argType, 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 | MsgExprs.push_back(Cls); |
| 2819 | break; |
| 2820 | } |
| 2821 | |
| 2822 | case ObjCMessageExpr::SuperInstance:{ |
| 2823 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2824 | if (MsgSendStretFlavor) |
| 2825 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2826 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 2827 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2828 | SmallVector<Expr*, 4> InitExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2829 | |
| 2830 | InitExprs.push_back( |
| 2831 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2832 | CK_BitCast, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2833 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2834 | false, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2835 | Context->getObjCIdType(), |
| 2836 | VK_RValue, SourceLocation())) |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2837 | ); // set the 'receiver'. |
| 2838 | |
| 2839 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2840 | SmallVector<Expr*, 8> ClsExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2841 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2842 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2843 | ClassDecl->getIdentifier()->getName(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2844 | StringLiteral::Ascii, false, argType, |
| 2845 | SourceLocation())); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2846 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2847 | &ClsExprs[0], |
| 2848 | ClsExprs.size(), |
| 2849 | StartLoc, EndLoc); |
| 2850 | // (Class)objc_getClass("CurrentClass") |
| 2851 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 2852 | Context->getObjCClassType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2853 | CK_BitCast, Cls); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2854 | ClsExprs.clear(); |
| 2855 | ClsExprs.push_back(ArgExpr); |
| 2856 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 2857 | &ClsExprs[0], ClsExprs.size(), |
| 2858 | StartLoc, EndLoc); |
| 2859 | |
| 2860 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2861 | // To turn off a warning, type-cast to 'id' |
| 2862 | InitExprs.push_back( |
| 2863 | // set 'super class', using class_getSuperclass(). |
| 2864 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2865 | CK_BitCast, Cls)); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2866 | // struct objc_super |
| 2867 | QualType superType = getSuperStructType(); |
| 2868 | Expr *SuperRep; |
| 2869 | |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 2870 | if (LangOpts.MicrosoftExt) { |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2871 | SynthSuperContructorFunctionDecl(); |
| 2872 | // Simulate a contructor call... |
| 2873 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2874 | false, superType, VK_LValue, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2875 | SourceLocation()); |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2876 | SuperRep = new (Context) CallExpr(*Context, DRE, InitExprs, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2877 | superType, VK_LValue, SourceLocation()); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2878 | // The code for super is a little tricky to prevent collision with |
| 2879 | // the structure definition in the header. The rewriter has it's own |
| 2880 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2881 | // we need the cast below. For example: |
| 2882 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2883 | // |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2884 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2885 | Context->getPointerType(SuperRep->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2886 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2887 | SourceLocation()); |
| 2888 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2889 | Context->getPointerType(superType), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2890 | CK_BitCast, SuperRep); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2891 | } else { |
| 2892 | // (struct objc_super) { <exprs from above> } |
| 2893 | InitListExpr *ILE = |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2894 | new (Context) InitListExpr(*Context, SourceLocation(), InitExprs, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2895 | SourceLocation()); |
| 2896 | TypeSourceInfo *superTInfo |
| 2897 | = Context->getTrivialTypeSourceInfo(superType); |
| 2898 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2899 | superType, VK_RValue, ILE, |
| 2900 | false); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2901 | } |
| 2902 | MsgExprs.push_back(SuperRep); |
| 2903 | break; |
| 2904 | } |
| 2905 | |
| 2906 | case ObjCMessageExpr::Instance: { |
| 2907 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 2908 | // Foo<Proto> *. |
| 2909 | Expr *recExpr = Exp->getInstanceReceiver(); |
| 2910 | while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr)) |
| 2911 | recExpr = CE->getSubExpr(); |
Fariborz Jahanian | baac1ea | 2011-10-07 17:17:45 +0000 | [diff] [blame] | 2912 | CastKind CK = recExpr->getType()->isObjCObjectPointerType() |
| 2913 | ? CK_BitCast : recExpr->getType()->isBlockPointerType() |
| 2914 | ? CK_BlockPointerToObjCPointerCast |
| 2915 | : CK_CPointerToObjCPointerCast; |
| 2916 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2917 | recExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
Fariborz Jahanian | baac1ea | 2011-10-07 17:17:45 +0000 | [diff] [blame] | 2918 | CK, recExpr); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2919 | MsgExprs.push_back(recExpr); |
| 2920 | break; |
| 2921 | } |
| 2922 | } |
| 2923 | |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2924 | // 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] | 2925 | SmallVector<Expr*, 8> SelExprs; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2926 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2927 | SelExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2928 | Exp->getSelector().getAsString(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2929 | StringLiteral::Ascii, false, |
| 2930 | argType, SourceLocation())); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2931 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2932 | &SelExprs[0], SelExprs.size(), |
| 2933 | StartLoc, |
| 2934 | EndLoc); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2935 | MsgExprs.push_back(SelExp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2936 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2937 | // Now push any user supplied arguments. |
| 2938 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2939 | Expr *userExpr = Exp->getArg(i); |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2940 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 2941 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 2942 | // Reuse the ICE type, it is exactly what the doctor ordered. |
Fariborz Jahanian | dff3f01 | 2011-02-26 01:31:36 +0000 | [diff] [blame] | 2943 | QualType type = ICE->getType(); |
| 2944 | if (needToScanForQualifiers(type)) |
| 2945 | type = Context->getObjCIdType(); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 2946 | // Make sure we convert "type (^)(...)" to "type (*)(...)". |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 2947 | (void)convertBlockPointerToFunctionPointer(type); |
Fariborz Jahanian | 1a38b46 | 2011-08-04 23:58:03 +0000 | [diff] [blame] | 2948 | const Expr *SubExpr = ICE->IgnoreParenImpCasts(); |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2949 | CastKind CK; |
| 2950 | if (SubExpr->getType()->isIntegralType(*Context) && |
| 2951 | type->isBooleanType()) { |
| 2952 | CK = CK_IntegralToBoolean; |
| 2953 | } else if (type->isObjCObjectPointerType()) { |
| 2954 | if (SubExpr->getType()->isBlockPointerType()) { |
| 2955 | CK = CK_BlockPointerToObjCPointerCast; |
| 2956 | } else if (SubExpr->getType()->isPointerType()) { |
| 2957 | CK = CK_CPointerToObjCPointerCast; |
| 2958 | } else { |
| 2959 | CK = CK_BitCast; |
| 2960 | } |
| 2961 | } else { |
| 2962 | CK = CK_BitCast; |
| 2963 | } |
| 2964 | |
| 2965 | userExpr = NoTypeInfoCStyleCastExpr(Context, type, CK, userExpr); |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2966 | } |
| 2967 | // Make id<P...> cast into an 'id' cast. |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2968 | else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2969 | if (CE->getType()->isObjCQualifiedIdType()) { |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2970 | while ((CE = dyn_cast<CStyleCastExpr>(userExpr))) |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2971 | userExpr = CE->getSubExpr(); |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2972 | CastKind CK; |
| 2973 | if (userExpr->getType()->isIntegralType(*Context)) { |
| 2974 | CK = CK_IntegralToPointer; |
| 2975 | } else if (userExpr->getType()->isBlockPointerType()) { |
| 2976 | CK = CK_BlockPointerToObjCPointerCast; |
| 2977 | } else if (userExpr->getType()->isPointerType()) { |
| 2978 | CK = CK_CPointerToObjCPointerCast; |
| 2979 | } else { |
| 2980 | CK = CK_BitCast; |
| 2981 | } |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2982 | userExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2983 | CK, userExpr); |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2984 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2985 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2986 | MsgExprs.push_back(userExpr); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2987 | // We've transferred the ownership to MsgExprs. For now, we *don't* null |
| 2988 | // out the argument in the original expression (since we aren't deleting |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2989 | // the ObjCMessageExpr). See RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2990 | //Exp->setArg(i, 0); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2991 | } |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2992 | // Generate the funky cast. |
| 2993 | CastExpr *cast; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2994 | SmallVector<QualType, 8> ArgTypes; |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2995 | QualType returnType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2996 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2997 | // Push 'id' and 'SEL', the 2 implicit arguments. |
Steve Naroff | c3a438c | 2007-11-15 10:43:57 +0000 | [diff] [blame] | 2998 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 2999 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 3000 | else |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3001 | ArgTypes.push_back(Context->getObjCIdType()); |
| 3002 | ArgTypes.push_back(Context->getObjCSelType()); |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3003 | if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) { |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3004 | // Push any user argument types. |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3005 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 3006 | E = OMD->param_end(); PI != E; ++PI) { |
| 3007 | QualType t = (*PI)->getType()->isObjCQualifiedIdType() |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3008 | ? Context->getObjCIdType() |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3009 | : (*PI)->getType(); |
Steve Naroff | a206b06 | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 3010 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3011 | (void)convertBlockPointerToFunctionPointer(t); |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 3012 | ArgTypes.push_back(t); |
| 3013 | } |
Fariborz Jahanian | 3a448fb | 2011-09-10 17:01:56 +0000 | [diff] [blame] | 3014 | returnType = Exp->getType(); |
| 3015 | convertToUnqualifiedObjCType(returnType); |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3016 | (void)convertBlockPointerToFunctionPointer(returnType); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3017 | } else { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3018 | returnType = Context->getObjCIdType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3019 | } |
| 3020 | // 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] | 3021 | QualType msgSendType = MsgSendFlavor->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3022 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3023 | // Create a reference to the objc_msgSend() declaration. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3024 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, false, msgSendType, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3025 | VK_LValue, SourceLocation()); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3026 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3027 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3028 | // If we don't do this cast, we get the following bizarre warning/note: |
| 3029 | // xx.m:13: warning: function called through a non-compatible type |
| 3030 | // 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] | 3031 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 3032 | Context->getPointerType(Context->VoidTy), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3033 | CK_BitCast, DRE); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3034 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3035 | // Now do the "normal" pointer to function cast. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3036 | QualType castType = |
| 3037 | getSimpleFunctionType(returnType, &ArgTypes[0], ArgTypes.size(), |
| 3038 | // If we don't have a method decl, force a variadic cast. |
| 3039 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +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); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3043 | |
| 3044 | // Don't forget the parens to enforce the proper binding. |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 3045 | ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, 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 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3048 | CallExpr *CE = new (Context) CallExpr(*Context, PE, MsgExprs, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3049 | FT->getResultType(), VK_RValue, |
| 3050 | EndLoc); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3051 | Stmt *ReplacingStmt = CE; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3052 | if (MsgSendStretFlavor) { |
| 3053 | // We have the method which returns a struct/union. Must also generate |
| 3054 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 3055 | // expression which dictate which one to envoke depending on size of |
| 3056 | // method's return type. |
Fariborz Jahanian | 9fc5b00 | 2012-06-28 21:20:35 +0000 | [diff] [blame] | 3057 | |
| 3058 | CallExpr *STCE = SynthMsgSendStretCallExpr(MsgSendStretFlavor, |
| 3059 | msgSendType, returnType, |
| 3060 | ArgTypes, MsgExprs, |
| 3061 | Exp->getMethodDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3062 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3063 | // Build sizeof(returnType) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3064 | UnaryExprOrTypeTraitExpr *sizeofExpr = |
| 3065 | new (Context) UnaryExprOrTypeTraitExpr(UETT_SizeOf, |
| 3066 | Context->getTrivialTypeSourceInfo(returnType), |
| 3067 | Context->getSizeType(), SourceLocation(), |
| 3068 | SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3069 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 3070 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 3071 | // For X86 it is more complicated and some kind of target specific routine |
| 3072 | // is needed to decide what to do. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3073 | unsigned IntSize = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3074 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 3075 | IntegerLiteral *limit = IntegerLiteral::Create(*Context, |
| 3076 | llvm::APInt(IntSize, 8), |
| 3077 | Context->IntTy, |
| 3078 | SourceLocation()); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3079 | BinaryOperator *lessThanExpr = |
| 3080 | new (Context) BinaryOperator(sizeofExpr, limit, BO_LE, Context->IntTy, |
| 3081 | VK_RValue, OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3082 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3083 | ConditionalOperator *CondExpr = |
Douglas Gregor | 47e1f7c | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 3084 | new (Context) ConditionalOperator(lessThanExpr, |
| 3085 | SourceLocation(), CE, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3086 | SourceLocation(), STCE, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3087 | returnType, VK_RValue, OK_Ordinary); |
Fariborz Jahanian | f9b949f | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 3088 | ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3089 | CondExpr); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3090 | } |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3091 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3092 | return ReplacingStmt; |
| 3093 | } |
| 3094 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3095 | Stmt *RewriteObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 3096 | Stmt *ReplacingStmt = SynthMessageExpr(Exp, Exp->getLocStart(), |
| 3097 | Exp->getLocEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3098 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 3099 | // Now do the actual rewrite. |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 3100 | ReplaceStmt(Exp, ReplacingStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3101 | |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3102 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3103 | return ReplacingStmt; |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 3104 | } |
| 3105 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3106 | // typedef struct objc_object Protocol; |
| 3107 | QualType RewriteObjC::getProtocolType() { |
| 3108 | if (!ProtocolTypeDecl) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3109 | TypeSourceInfo *TInfo |
| 3110 | = Context->getTrivialTypeSourceInfo(Context->getObjCIdType()); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3111 | ProtocolTypeDecl = TypedefDecl::Create(*Context, TUDecl, |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 3112 | SourceLocation(), SourceLocation(), |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3113 | &Context->Idents.get("Protocol"), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3114 | TInfo); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3115 | } |
| 3116 | return Context->getTypeDeclType(ProtocolTypeDecl); |
| 3117 | } |
| 3118 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 3119 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3120 | /// a synthesized/forward data reference (to the protocol's metadata). |
| 3121 | /// The forward references (and metadata) are generated in |
| 3122 | /// RewriteObjC::HandleTranslationUnit(). |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3123 | Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3124 | std::string Name = "_OBJC_PROTOCOL_" + Exp->getProtocol()->getNameAsString(); |
| 3125 | IdentifierInfo *ID = &Context->Idents.get(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3126 | VarDecl *VD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3127 | SourceLocation(), ID, getProtocolType(), 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3128 | SC_Extern, SC_None); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3129 | DeclRefExpr *DRE = new (Context) DeclRefExpr(VD, false, getProtocolType(), |
| 3130 | VK_LValue, SourceLocation()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3131 | Expr *DerefExpr = new (Context) UnaryOperator(DRE, UO_AddrOf, |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3132 | Context->getPointerType(DRE->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3133 | VK_RValue, OK_Ordinary, SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3134 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, DerefExpr->getType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3135 | CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3136 | DerefExpr); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3137 | ReplaceStmt(Exp, castExpr); |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 3138 | ProtocolExprDecls.insert(Exp->getProtocol()->getCanonicalDecl()); |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3139 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3140 | return castExpr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3141 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 3142 | } |
| 3143 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3144 | bool RewriteObjC::BufferContainsPPDirectives(const char *startBuf, |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3145 | const char *endBuf) { |
| 3146 | while (startBuf < endBuf) { |
| 3147 | if (*startBuf == '#') { |
| 3148 | // Skip whitespace. |
| 3149 | for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf) |
| 3150 | ; |
| 3151 | if (!strncmp(startBuf, "if", strlen("if")) || |
| 3152 | !strncmp(startBuf, "ifdef", strlen("ifdef")) || |
| 3153 | !strncmp(startBuf, "ifndef", strlen("ifndef")) || |
| 3154 | !strncmp(startBuf, "define", strlen("define")) || |
| 3155 | !strncmp(startBuf, "undef", strlen("undef")) || |
| 3156 | !strncmp(startBuf, "else", strlen("else")) || |
| 3157 | !strncmp(startBuf, "elif", strlen("elif")) || |
| 3158 | !strncmp(startBuf, "endif", strlen("endif")) || |
| 3159 | !strncmp(startBuf, "pragma", strlen("pragma")) || |
| 3160 | !strncmp(startBuf, "include", strlen("include")) || |
| 3161 | !strncmp(startBuf, "import", strlen("import")) || |
| 3162 | !strncmp(startBuf, "include_next", strlen("include_next"))) |
| 3163 | return true; |
| 3164 | } |
| 3165 | startBuf++; |
| 3166 | } |
| 3167 | return false; |
| 3168 | } |
| 3169 | |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 3170 | /// RewriteObjCInternalStruct - Rewrite one internal struct corresponding to |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3171 | /// an objective-c class with ivars. |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 3172 | void RewriteObjC::RewriteObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3173 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3174 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3175 | assert(CDecl->getName() != "" && |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 3176 | "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 3177 | // Do not synthesize more than once. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3178 | if (ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 3179 | return; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3180 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3181 | int NumIvars = CDecl->ivar_size(); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3182 | SourceLocation LocStart = CDecl->getLocStart(); |
Douglas Gregor | 05c272f | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 3183 | SourceLocation LocEnd = CDecl->getEndOfDefinitionLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3184 | |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3185 | const char *startBuf = SM->getCharacterData(LocStart); |
| 3186 | const char *endBuf = SM->getCharacterData(LocEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3187 | |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3188 | // If no ivars and no root or if its root, directly or indirectly, |
| 3189 | // 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] | 3190 | if ((!CDecl->isThisDeclarationADefinition() || NumIvars == 0) && |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3191 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3192 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3193 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3194 | return; |
| 3195 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3196 | |
| 3197 | // FIXME: This has potential of causing problem. If |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3198 | // SynthesizeObjCInternalStruct is ever called recursively. |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3199 | Result += "\nstruct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3200 | Result += CDecl->getNameAsString(); |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 3201 | if (LangOpts.MicrosoftExt) |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 3202 | Result += "_IMPL"; |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 3203 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3204 | if (NumIvars > 0) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3205 | const char *cursor = strchr(startBuf, '{'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3206 | assert((cursor && endBuf) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3207 | && "SynthesizeObjCInternalStruct - malformed @interface"); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3208 | // If the buffer contains preprocessor directives, we do more fine-grained |
| 3209 | // rewrites. This is intended to fix code that looks like (which occurs in |
| 3210 | // NSURL.h, for example): |
| 3211 | // |
| 3212 | // #ifdef XYZ |
| 3213 | // @interface Foo : NSObject |
| 3214 | // #else |
| 3215 | // @interface FooBar : NSObject |
| 3216 | // #endif |
| 3217 | // { |
| 3218 | // int i; |
| 3219 | // } |
| 3220 | // @end |
| 3221 | // |
| 3222 | // This clause is segregated to avoid breaking the common case. |
| 3223 | if (BufferContainsPPDirectives(startBuf, cursor)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3224 | SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() : |
Argyrios Kyrtzidis | 1711fc9 | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 3225 | CDecl->getAtStartLoc(); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3226 | const char *endHeader = SM->getCharacterData(L); |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3227 | endHeader += Lexer::MeasureTokenLength(L, *SM, LangOpts); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3228 | |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3229 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3230 | // advance to the end of the referenced protocols. |
| 3231 | while (endHeader < cursor && *endHeader != '>') endHeader++; |
| 3232 | endHeader++; |
| 3233 | } |
| 3234 | // rewrite the original header |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3235 | ReplaceText(LocStart, endHeader-startBuf, Result); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3236 | } else { |
| 3237 | // rewrite the original header *without* disturbing the '{' |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3238 | ReplaceText(LocStart, cursor-startBuf, Result); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3239 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3240 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3241 | Result = "\n struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3242 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 39bbd9f | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 3243 | Result += "_IMPL "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3244 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 819173c | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 3245 | Result += "_IVARS;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3246 | |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3247 | // insert the super class structure definition. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 3248 | SourceLocation OnePastCurly = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3249 | LocStart.getLocWithOffset(cursor-startBuf+1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3250 | InsertText(OnePastCurly, Result); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3251 | } |
| 3252 | cursor++; // past '{' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3253 | |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3254 | // Now comment out any visibility specifiers. |
| 3255 | while (cursor < endBuf) { |
| 3256 | if (*cursor == '@') { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3257 | SourceLocation atLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Chris Lattner | df6a51b | 2007-11-14 22:57:51 +0000 | [diff] [blame] | 3258 | // Skip whitespace. |
| 3259 | for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor) |
| 3260 | /*scan*/; |
| 3261 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3262 | // FIXME: presence of @public, etc. inside comment results in |
| 3263 | // this transformation as well, which is still correct c-code. |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3264 | if (!strncmp(cursor, "public", strlen("public")) || |
| 3265 | !strncmp(cursor, "private", strlen("private")) || |
Steve Naroff | c5e3277 | 2008-04-04 22:34:24 +0000 | [diff] [blame] | 3266 | !strncmp(cursor, "package", strlen("package")) || |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3267 | !strncmp(cursor, "protected", strlen("protected"))) |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3268 | InsertText(atLoc, "// "); |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3269 | } |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3270 | // FIXME: If there are cases where '<' is used in ivar declaration part |
| 3271 | // of user code, then scan the ivar list and use needToScanForQualifiers |
| 3272 | // for type checking. |
| 3273 | else if (*cursor == '<') { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3274 | SourceLocation atLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3275 | InsertText(atLoc, "/* "); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3276 | cursor = strchr(cursor, '>'); |
| 3277 | cursor++; |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3278 | atLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3279 | InsertText(atLoc, " */"); |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3280 | } else if (*cursor == '^') { // rewrite block specifier. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3281 | SourceLocation caretLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3282 | ReplaceText(caretLoc, 1, "*"); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3283 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3284 | cursor++; |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3285 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3286 | // Don't forget to add a ';'!! |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3287 | InsertText(LocEnd.getLocWithOffset(1), ";"); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3288 | } else { // we don't have any instance variables - insert super struct. |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3289 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3290 | Result += " {\n struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3291 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 39bbd9f | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 3292 | Result += "_IMPL "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3293 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 819173c | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 3294 | Result += "_IVARS;\n};\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3295 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3296 | } |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3297 | // Mark this struct as having been generated. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3298 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3299 | llvm_unreachable("struct already synthesize- SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3300 | } |
| 3301 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3302 | //===----------------------------------------------------------------------===// |
| 3303 | // Meta Data Emission |
| 3304 | //===----------------------------------------------------------------------===// |
| 3305 | |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3306 | |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3307 | /// RewriteImplementations - This routine rewrites all method implementations |
| 3308 | /// and emits meta-data. |
| 3309 | |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3310 | void RewriteObjC::RewriteImplementations() { |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3311 | int ClsDefCount = ClassImplementation.size(); |
| 3312 | int CatDefCount = CategoryImplementation.size(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3313 | |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3314 | // Rewrite implemented methods |
| 3315 | for (int i = 0; i < ClsDefCount; i++) |
| 3316 | RewriteImplementationDecl(ClassImplementation[i]); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3317 | |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 3318 | for (int i = 0; i < CatDefCount; i++) |
| 3319 | RewriteImplementationDecl(CategoryImplementation[i]); |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3320 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3321 | |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3322 | void RewriteObjC::RewriteByRefString(std::string &ResultStr, |
| 3323 | const std::string &Name, |
Fariborz Jahanian | 1e8011e | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 3324 | ValueDecl *VD, bool def) { |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3325 | assert(BlockByRefDeclNo.count(VD) && |
| 3326 | "RewriteByRefString: ByRef decl missing"); |
Fariborz Jahanian | 1e8011e | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 3327 | if (def) |
| 3328 | ResultStr += "struct "; |
| 3329 | ResultStr += "__Block_byref_" + Name + |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3330 | "_" + utostr(BlockByRefDeclNo[VD]) ; |
| 3331 | } |
| 3332 | |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3333 | static bool HasLocalVariableExternalStorage(ValueDecl *VD) { |
| 3334 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 3335 | return (Var->isFunctionOrMethodVarDecl() && !Var->hasLocalStorage()); |
| 3336 | return false; |
| 3337 | } |
| 3338 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3339 | std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3340 | StringRef funcName, |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3341 | std::string Tag) { |
| 3342 | const FunctionType *AFT = CE->getFunctionType(); |
| 3343 | QualType RT = AFT->getResultType(); |
| 3344 | std::string StructRef = "struct " + Tag; |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 3345 | std::string S = "static " + RT.getAsString(Context->getPrintingPolicy()) + " __" + |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3346 | funcName.str() + "_" + "block_func_" + utostr(i); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 3347 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3348 | BlockDecl *BD = CE->getBlockDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3349 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3350 | if (isa<FunctionNoProtoType>(AFT)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3351 | // 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] | 3352 | // block (to reference imported block decl refs). |
| 3353 | S += "(" + StructRef + " *__cself)"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3354 | } else if (BD->param_empty()) { |
| 3355 | S += "(" + StructRef + " *__cself)"; |
| 3356 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3357 | const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3358 | assert(FT && "SynthesizeBlockFunc: No function proto"); |
| 3359 | S += '('; |
| 3360 | // first add the implicit argument. |
| 3361 | S += StructRef + " *__cself, "; |
| 3362 | std::string ParamStr; |
| 3363 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 3364 | E = BD->param_end(); AI != E; ++AI) { |
| 3365 | if (AI != BD->param_begin()) S += ", "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3366 | ParamStr = (*AI)->getNameAsString(); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3367 | QualType QT = (*AI)->getType(); |
Fariborz Jahanian | 2610f90 | 2012-03-27 16:42:20 +0000 | [diff] [blame] | 3368 | (void)convertBlockPointerToFunctionPointer(QT); |
| 3369 | QT.getAsStringInternal(ParamStr, Context->getPrintingPolicy()); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3370 | S += ParamStr; |
| 3371 | } |
| 3372 | if (FT->isVariadic()) { |
| 3373 | if (!BD->param_empty()) S += ", "; |
| 3374 | S += "..."; |
| 3375 | } |
| 3376 | S += ')'; |
| 3377 | } |
| 3378 | S += " {\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3379 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3380 | // Create local declarations to avoid rewriting all closure decl ref exprs. |
| 3381 | // First, emit a declaration for all "by ref" decls. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3382 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3383 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3384 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3385 | std::string Name = (*I)->getNameAsString(); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3386 | std::string TypeString; |
| 3387 | RewriteByRefString(TypeString, Name, (*I)); |
| 3388 | TypeString += " *"; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 3389 | Name = TypeString + Name; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3390 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3391 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3392 | // Next, emit a declaration for all "by copy" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3393 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3394 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3395 | S += " "; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3396 | // Handle nested closure invocation. For example: |
| 3397 | // |
| 3398 | // void (^myImportedClosure)(void); |
| 3399 | // myImportedClosure = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3400 | // |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3401 | // void (^anotherClosure)(void); |
| 3402 | // anotherClosure = ^(void) { |
| 3403 | // myImportedClosure(); // import and invoke the closure |
| 3404 | // }; |
| 3405 | // |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 3406 | if (isTopLevelBlockPointerType((*I)->getType())) { |
| 3407 | RewriteBlockPointerTypeVariable(S, (*I)); |
| 3408 | S += " = ("; |
| 3409 | RewriteBlockPointerType(S, (*I)->getType()); |
| 3410 | S += ")"; |
| 3411 | S += "__cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; |
| 3412 | } |
| 3413 | else { |
Fariborz Jahanian | 210c248 | 2010-02-16 17:26:03 +0000 | [diff] [blame] | 3414 | std::string Name = (*I)->getNameAsString(); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3415 | QualType QT = (*I)->getType(); |
| 3416 | if (HasLocalVariableExternalStorage(*I)) |
| 3417 | QT = Context->getPointerType(QT); |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 3418 | QT.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 3419 | S += Name + " = __cself->" + |
| 3420 | (*I)->getNameAsString() + "; // bound by copy\n"; |
| 3421 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3422 | } |
| 3423 | std::string RewrittenStr = RewrittenBlockExprs[CE]; |
| 3424 | const char *cstr = RewrittenStr.c_str(); |
| 3425 | while (*cstr++ != '{') ; |
| 3426 | S += cstr; |
| 3427 | S += "\n"; |
| 3428 | return S; |
| 3429 | } |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 3430 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3431 | std::string RewriteObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3432 | StringRef funcName, |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3433 | std::string Tag) { |
| 3434 | std::string StructRef = "struct " + Tag; |
| 3435 | std::string S = "static void __"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3436 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3437 | S += funcName; |
| 3438 | S += "_block_copy_" + utostr(i); |
| 3439 | S += "(" + StructRef; |
| 3440 | S += "*dst, " + StructRef; |
| 3441 | S += "*src) {"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3442 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3443 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3444 | ValueDecl *VD = (*I); |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3445 | S += "_Block_object_assign((void*)&dst->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3446 | S += (*I)->getNameAsString(); |
Steve Naroff | 47a2422 | 2008-12-11 20:51:38 +0000 | [diff] [blame] | 3447 | S += ", (void*)src->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3448 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 3449 | if (BlockByRefDeclsPtrSet.count((*I))) |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 3450 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3451 | else if (VD->getType()->isBlockPointerType()) |
Fariborz Jahanian | 06433c6 | 2011-07-30 01:07:55 +0000 | [diff] [blame] | 3452 | S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);"; |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3453 | else |
| 3454 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3455 | } |
Fariborz Jahanian | d25d1b5 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 3456 | S += "}\n"; |
| 3457 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3458 | S += "\nstatic void __"; |
| 3459 | S += funcName; |
| 3460 | S += "_block_dispose_" + utostr(i); |
| 3461 | S += "(" + StructRef; |
| 3462 | S += "*src) {"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3463 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3464 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3465 | ValueDecl *VD = (*I); |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3466 | S += "_Block_object_dispose((void*)src->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3467 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 3468 | if (BlockByRefDeclsPtrSet.count((*I))) |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 3469 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3470 | else if (VD->getType()->isBlockPointerType()) |
Fariborz Jahanian | 06433c6 | 2011-07-30 01:07:55 +0000 | [diff] [blame] | 3471 | S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);"; |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3472 | else |
| 3473 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3474 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3475 | S += "}\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3476 | return S; |
| 3477 | } |
| 3478 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3479 | std::string RewriteObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 3480 | std::string Desc) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3481 | std::string S = "\nstruct " + Tag; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3482 | std::string Constructor = " " + Tag; |
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 | S += " {\n struct __block_impl impl;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3485 | S += " struct " + Desc; |
| 3486 | S += "* Desc;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3487 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3488 | Constructor += "(void *fp, "; // Invoke function pointer. |
| 3489 | Constructor += "struct " + Desc; // Descriptor pointer. |
| 3490 | Constructor += " *desc"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3491 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3492 | if (BlockDeclRefs.size()) { |
| 3493 | // Output all "by copy" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3494 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3495 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3496 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3497 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3498 | std::string ArgName = "_" + FieldName; |
| 3499 | // Handle nested closure invocation. For example: |
| 3500 | // |
| 3501 | // void (^myImportedBlock)(void); |
| 3502 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3503 | // |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3504 | // void (^anotherBlock)(void); |
| 3505 | // anotherBlock = ^(void) { |
| 3506 | // myImportedBlock(); // import and invoke the closure |
| 3507 | // }; |
| 3508 | // |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3509 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3510 | S += "struct __block_impl *"; |
| 3511 | Constructor += ", void *" + ArgName; |
| 3512 | } else { |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3513 | QualType QT = (*I)->getType(); |
| 3514 | if (HasLocalVariableExternalStorage(*I)) |
| 3515 | QT = Context->getPointerType(QT); |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 3516 | QT.getAsStringInternal(FieldName, Context->getPrintingPolicy()); |
| 3517 | QT.getAsStringInternal(ArgName, Context->getPrintingPolicy()); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3518 | Constructor += ", " + ArgName; |
| 3519 | } |
| 3520 | S += FieldName + ";\n"; |
| 3521 | } |
| 3522 | // Output all "by ref" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3523 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3524 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3525 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3526 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3527 | std::string ArgName = "_" + FieldName; |
Fariborz Jahanian | 651ba52 | 2011-04-01 23:08:13 +0000 | [diff] [blame] | 3528 | { |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3529 | std::string TypeString; |
| 3530 | RewriteByRefString(TypeString, FieldName, (*I)); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 3531 | TypeString += " *"; |
| 3532 | FieldName = TypeString + FieldName; |
| 3533 | ArgName = TypeString + ArgName; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3534 | Constructor += ", " + ArgName; |
| 3535 | } |
| 3536 | S += FieldName + "; // by ref\n"; |
| 3537 | } |
| 3538 | // Finish writing the constructor. |
Fariborz Jahanian | 20432ef | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 3539 | Constructor += ", int flags=0)"; |
| 3540 | // Initialize all "by copy" arguments. |
| 3541 | bool firsTime = true; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3542 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Fariborz Jahanian | 20432ef | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 3543 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3544 | std::string Name = (*I)->getNameAsString(); |
| 3545 | if (firsTime) { |
| 3546 | Constructor += " : "; |
| 3547 | firsTime = false; |
| 3548 | } |
| 3549 | else |
| 3550 | Constructor += ", "; |
| 3551 | if (isTopLevelBlockPointerType((*I)->getType())) |
| 3552 | Constructor += Name + "((struct __block_impl *)_" + Name + ")"; |
| 3553 | else |
| 3554 | Constructor += Name + "(_" + Name + ")"; |
| 3555 | } |
| 3556 | // Initialize all "by ref" arguments. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3557 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Fariborz Jahanian | 20432ef | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 3558 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3559 | std::string Name = (*I)->getNameAsString(); |
| 3560 | if (firsTime) { |
| 3561 | Constructor += " : "; |
| 3562 | firsTime = false; |
| 3563 | } |
| 3564 | else |
| 3565 | Constructor += ", "; |
Fariborz Jahanian | 651ba52 | 2011-04-01 23:08:13 +0000 | [diff] [blame] | 3566 | Constructor += Name + "(_" + Name + "->__forwarding)"; |
Fariborz Jahanian | 20432ef | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 3567 | } |
| 3568 | |
| 3569 | Constructor += " {\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3570 | if (GlobalVarDecl) |
| 3571 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 3572 | else |
| 3573 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3574 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3575 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3576 | Constructor += " Desc = desc;\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3577 | } else { |
| 3578 | // Finish writing the constructor. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3579 | Constructor += ", int flags=0) {\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3580 | if (GlobalVarDecl) |
| 3581 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 3582 | else |
| 3583 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3584 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 3585 | Constructor += " Desc = desc;\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3586 | } |
| 3587 | Constructor += " "; |
| 3588 | Constructor += "}\n"; |
| 3589 | S += Constructor; |
| 3590 | S += "};\n"; |
| 3591 | return S; |
| 3592 | } |
| 3593 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3594 | std::string RewriteObjC::SynthesizeBlockDescriptor(std::string DescTag, |
| 3595 | std::string ImplTag, int i, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3596 | StringRef FunName, |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3597 | unsigned hasCopy) { |
| 3598 | std::string S = "\nstatic struct " + DescTag; |
| 3599 | |
| 3600 | S += " {\n unsigned long reserved;\n"; |
| 3601 | S += " unsigned long Block_size;\n"; |
| 3602 | if (hasCopy) { |
Fariborz Jahanian | 4fcc4fd | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 3603 | S += " void (*copy)(struct "; |
| 3604 | S += ImplTag; S += "*, struct "; |
| 3605 | S += ImplTag; S += "*);\n"; |
| 3606 | |
| 3607 | S += " void (*dispose)(struct "; |
| 3608 | S += ImplTag; S += "*);\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3609 | } |
| 3610 | S += "} "; |
| 3611 | |
| 3612 | S += DescTag + "_DATA = { 0, sizeof(struct "; |
| 3613 | S += ImplTag + ")"; |
| 3614 | if (hasCopy) { |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3615 | S += ", __" + FunName.str() + "_block_copy_" + utostr(i); |
| 3616 | S += ", __" + FunName.str() + "_block_dispose_" + utostr(i); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3617 | } |
| 3618 | S += "};\n"; |
| 3619 | return S; |
| 3620 | } |
| 3621 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3622 | void RewriteObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3623 | StringRef FunName) { |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 3624 | // Insert declaration for the function in which block literal is used. |
Fariborz Jahanian | bf07012 | 2010-01-15 18:14:52 +0000 | [diff] [blame] | 3625 | if (CurFunctionDeclToDeclareForBlock && !Blocks.empty()) |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 3626 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Fariborz Jahanian | 8611eb0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 3627 | bool RewriteSC = (GlobalVarDecl && |
| 3628 | !Blocks.empty() && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3629 | GlobalVarDecl->getStorageClass() == SC_Static && |
Fariborz Jahanian | 8611eb0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 3630 | GlobalVarDecl->getType().getCVRQualifiers()); |
| 3631 | if (RewriteSC) { |
| 3632 | std::string SC(" void __"); |
| 3633 | SC += GlobalVarDecl->getNameAsString(); |
| 3634 | SC += "() {}"; |
| 3635 | InsertText(FunLocStart, SC); |
| 3636 | } |
| 3637 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3638 | // Insert closures that were part of the function. |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3639 | for (unsigned i = 0, count=0; i < Blocks.size(); i++) { |
| 3640 | CollectBlockDeclRefInfo(Blocks[i]); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3641 | // Need to copy-in the inner copied-in variables not actually used in this |
| 3642 | // block. |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3643 | for (int j = 0; j < InnerDeclRefsCount[i]; j++) { |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3644 | DeclRefExpr *Exp = InnerDeclRefs[count++]; |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3645 | ValueDecl *VD = Exp->getDecl(); |
| 3646 | BlockDeclRefs.push_back(Exp); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3647 | if (!VD->hasAttr<BlocksAttr>() && !BlockByCopyDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3648 | BlockByCopyDeclsPtrSet.insert(VD); |
| 3649 | BlockByCopyDecls.push_back(VD); |
| 3650 | } |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3651 | if (VD->hasAttr<BlocksAttr>() && !BlockByRefDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3652 | BlockByRefDeclsPtrSet.insert(VD); |
| 3653 | BlockByRefDecls.push_back(VD); |
| 3654 | } |
Fariborz Jahanian | 92c8568 | 2010-10-05 18:05:06 +0000 | [diff] [blame] | 3655 | // imported objects in the inner blocks not used in the outer |
| 3656 | // blocks must be copied/disposed in the outer block as well. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3657 | if (VD->hasAttr<BlocksAttr>() || |
Fariborz Jahanian | 92c8568 | 2010-10-05 18:05:06 +0000 | [diff] [blame] | 3658 | VD->getType()->isObjCObjectPointerType() || |
| 3659 | VD->getType()->isBlockPointerType()) |
| 3660 | ImportedBlockDecls.insert(VD); |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3661 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3662 | |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3663 | std::string ImplTag = "__" + FunName.str() + "_block_impl_" + utostr(i); |
| 3664 | std::string DescTag = "__" + FunName.str() + "_block_desc_" + utostr(i); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3665 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3666 | std::string CI = SynthesizeBlockImpl(Blocks[i], ImplTag, DescTag); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3667 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3668 | InsertText(FunLocStart, CI); |
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 CF = SynthesizeBlockFunc(Blocks[i], i, FunName, ImplTag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3671 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3672 | InsertText(FunLocStart, CF); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3673 | |
| 3674 | if (ImportedBlockDecls.size()) { |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3675 | std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, ImplTag); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3676 | InsertText(FunLocStart, HF); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3677 | } |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3678 | std::string BD = SynthesizeBlockDescriptor(DescTag, ImplTag, i, FunName, |
| 3679 | ImportedBlockDecls.size() > 0); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3680 | InsertText(FunLocStart, BD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3681 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3682 | BlockDeclRefs.clear(); |
| 3683 | BlockByRefDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 3684 | BlockByRefDeclsPtrSet.clear(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3685 | BlockByCopyDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 3686 | BlockByCopyDeclsPtrSet.clear(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3687 | ImportedBlockDecls.clear(); |
| 3688 | } |
Fariborz Jahanian | 8611eb0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 3689 | if (RewriteSC) { |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3690 | // Must insert any 'const/volatile/static here. Since it has been |
| 3691 | // removed as result of rewriting of block literals. |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3692 | std::string SC; |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3693 | if (GlobalVarDecl->getStorageClass() == SC_Static) |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3694 | SC = "static "; |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3695 | if (GlobalVarDecl->getType().isConstQualified()) |
| 3696 | SC += "const "; |
| 3697 | if (GlobalVarDecl->getType().isVolatileQualified()) |
| 3698 | SC += "volatile "; |
Fariborz Jahanian | 8611eb0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 3699 | if (GlobalVarDecl->getType().isRestrictQualified()) |
| 3700 | SC += "restrict "; |
| 3701 | InsertText(FunLocStart, SC); |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3702 | } |
| 3703 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3704 | Blocks.clear(); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3705 | InnerDeclRefsCount.clear(); |
| 3706 | InnerDeclRefs.clear(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3707 | RewrittenBlockExprs.clear(); |
| 3708 | } |
| 3709 | |
| 3710 | void RewriteObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { |
| 3711 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3712 | StringRef FuncName = FD->getName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3713 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3714 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 3715 | } |
| 3716 | |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 3717 | static void BuildUniqueMethodName(std::string &Name, |
| 3718 | ObjCMethodDecl *MD) { |
| 3719 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3720 | Name = IFace->getName(); |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 3721 | Name += "__" + MD->getSelector().getAsString(); |
| 3722 | // Convert colons to underscores. |
| 3723 | std::string::size_type loc = 0; |
| 3724 | while ((loc = Name.find(":", loc)) != std::string::npos) |
| 3725 | Name.replace(loc, 1, "_"); |
| 3726 | } |
| 3727 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3728 | void RewriteObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3729 | //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n"); |
| 3730 | //SourceLocation FunLocStart = MD->getLocStart(); |
Fariborz Jahanian | 0e1c99a | 2010-01-29 01:55:49 +0000 | [diff] [blame] | 3731 | SourceLocation FunLocStart = MD->getLocStart(); |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 3732 | std::string FuncName; |
| 3733 | BuildUniqueMethodName(FuncName, MD); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3734 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3735 | } |
| 3736 | |
| 3737 | void RewriteObjC::GetBlockDeclRefExprs(Stmt *S) { |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 3738 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3739 | if (*CI) { |
| 3740 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 3741 | GetBlockDeclRefExprs(CBE->getBody()); |
| 3742 | else |
| 3743 | GetBlockDeclRefExprs(*CI); |
| 3744 | } |
| 3745 | // Handle specific things. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3746 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 3747 | if (DRE->refersToEnclosingLocal()) { |
| 3748 | // FIXME: Handle enums. |
| 3749 | if (!isa<FunctionDecl>(DRE->getDecl())) |
| 3750 | BlockDeclRefs.push_back(DRE); |
| 3751 | if (HasLocalVariableExternalStorage(DRE->getDecl())) |
| 3752 | BlockDeclRefs.push_back(DRE); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3753 | } |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3754 | } |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3755 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3756 | return; |
| 3757 | } |
| 3758 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3759 | void RewriteObjC::GetInnerBlockDeclRefExprs(Stmt *S, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3760 | SmallVector<DeclRefExpr *, 8> &InnerBlockDeclRefs, |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3761 | llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts) { |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 3762 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3763 | if (*CI) { |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3764 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) { |
| 3765 | InnerContexts.insert(cast<DeclContext>(CBE->getBlockDecl())); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3766 | GetInnerBlockDeclRefExprs(CBE->getBody(), |
| 3767 | InnerBlockDeclRefs, |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3768 | InnerContexts); |
| 3769 | } |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3770 | else |
| 3771 | GetInnerBlockDeclRefExprs(*CI, |
| 3772 | InnerBlockDeclRefs, |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3773 | InnerContexts); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3774 | |
| 3775 | } |
| 3776 | // Handle specific things. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3777 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 3778 | if (DRE->refersToEnclosingLocal()) { |
| 3779 | if (!isa<FunctionDecl>(DRE->getDecl()) && |
| 3780 | !InnerContexts.count(DRE->getDecl()->getDeclContext())) |
| 3781 | InnerBlockDeclRefs.push_back(DRE); |
| 3782 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) |
| 3783 | if (Var->isFunctionOrMethodVarDecl()) |
| 3784 | ImportedLocalExternalDecls.insert(Var); |
| 3785 | } |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3786 | } |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3787 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3788 | return; |
| 3789 | } |
| 3790 | |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3791 | /// convertFunctionTypeOfBlocks - This routine converts a function type |
| 3792 | /// 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] | 3793 | /// might be block pointers to an equivalent function type replacing |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3794 | /// all block pointers to function pointers. |
| 3795 | QualType RewriteObjC::convertFunctionTypeOfBlocks(const FunctionType *FT) { |
| 3796 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
| 3797 | // FTP will be null for closures that don't take arguments. |
| 3798 | // Generate a funky cast. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3799 | SmallVector<QualType, 8> ArgTypes; |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3800 | QualType Res = FT->getResultType(); |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3801 | bool HasBlockType = convertBlockPointerToFunctionPointer(Res); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3802 | |
| 3803 | if (FTP) { |
| 3804 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
| 3805 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 3806 | QualType t = *I; |
| 3807 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3808 | if (convertBlockPointerToFunctionPointer(t)) |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3809 | HasBlockType = true; |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3810 | ArgTypes.push_back(t); |
| 3811 | } |
| 3812 | } |
| 3813 | QualType FuncType; |
| 3814 | // FIXME. Does this work if block takes no argument but has a return type |
| 3815 | // which is of block type? |
| 3816 | if (HasBlockType) |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3817 | FuncType = getSimpleFunctionType(Res, &ArgTypes[0], ArgTypes.size()); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3818 | else FuncType = QualType(FT, 0); |
| 3819 | return FuncType; |
| 3820 | } |
| 3821 | |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3822 | Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp, const Expr *BlockExp) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3823 | // Navigate to relevant type information. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3824 | const BlockPointerType *CPT = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3825 | |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3826 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BlockExp)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3827 | CPT = DRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3828 | } else if (const MemberExpr *MExpr = dyn_cast<MemberExpr>(BlockExp)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3829 | CPT = MExpr->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3830 | } |
| 3831 | else if (const ParenExpr *PRE = dyn_cast<ParenExpr>(BlockExp)) { |
| 3832 | return SynthesizeBlockCall(Exp, PRE->getSubExpr()); |
| 3833 | } |
| 3834 | else if (const ImplicitCastExpr *IEXPR = dyn_cast<ImplicitCastExpr>(BlockExp)) |
| 3835 | CPT = IEXPR->getType()->getAs<BlockPointerType>(); |
| 3836 | else if (const ConditionalOperator *CEXPR = |
| 3837 | dyn_cast<ConditionalOperator>(BlockExp)) { |
| 3838 | Expr *LHSExp = CEXPR->getLHS(); |
| 3839 | Stmt *LHSStmt = SynthesizeBlockCall(Exp, LHSExp); |
| 3840 | Expr *RHSExp = CEXPR->getRHS(); |
| 3841 | Stmt *RHSStmt = SynthesizeBlockCall(Exp, RHSExp); |
| 3842 | Expr *CONDExp = CEXPR->getCond(); |
| 3843 | ConditionalOperator *CondExpr = |
| 3844 | new (Context) ConditionalOperator(CONDExp, |
| 3845 | SourceLocation(), cast<Expr>(LHSStmt), |
Fariborz Jahanian | f9b949f | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 3846 | SourceLocation(), cast<Expr>(RHSStmt), |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3847 | Exp->getType(), VK_RValue, OK_Ordinary); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3848 | return CondExpr; |
Fariborz Jahanian | e24b22b | 2009-12-18 01:15:21 +0000 | [diff] [blame] | 3849 | } else if (const ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(BlockExp)) { |
| 3850 | CPT = IRE->getType()->getAs<BlockPointerType>(); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 3851 | } else if (const PseudoObjectExpr *POE |
| 3852 | = dyn_cast<PseudoObjectExpr>(BlockExp)) { |
| 3853 | CPT = POE->getType()->castAs<BlockPointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3854 | } else { |
| 3855 | assert(1 && "RewriteBlockClass: Bad type"); |
| 3856 | } |
| 3857 | assert(CPT && "RewriteBlockClass: Bad type"); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3858 | const FunctionType *FT = CPT->getPointeeType()->getAs<FunctionType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3859 | assert(FT && "RewriteBlockClass: Bad type"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3860 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3861 | // FTP will be null for closures that don't take arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3862 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3863 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3864 | SourceLocation(), SourceLocation(), |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3865 | &Context->Idents.get("__block_impl")); |
| 3866 | QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD)); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3867 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3868 | // Generate a funky cast. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3869 | SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3870 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3871 | // Push the block argument type. |
| 3872 | ArgTypes.push_back(PtrBlock); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3873 | if (FTP) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3874 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3875 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 3876 | QualType t = *I; |
| 3877 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 8188e5f | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 3878 | if (!convertBlockPointerToFunctionPointer(t)) |
| 3879 | convertToUnqualifiedObjCType(t); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3880 | ArgTypes.push_back(t); |
| 3881 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3882 | } |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3883 | // Now do the pointer to function cast. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3884 | QualType PtrToFuncCastType |
| 3885 | = getSimpleFunctionType(Exp->getType(), &ArgTypes[0], ArgTypes.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3886 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3887 | PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3888 | |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3889 | CastExpr *BlkCast = NoTypeInfoCStyleCastExpr(Context, PtrBlock, |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3890 | CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3891 | const_cast<Expr*>(BlockExp)); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3892 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3893 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3894 | BlkCast); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3895 | //PE->dump(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3896 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3897 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3898 | SourceLocation(), |
| 3899 | &Context->Idents.get("FuncPtr"), |
| 3900 | Context->VoidPtrTy, 0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3901 | /*BitWidth=*/0, /*Mutable=*/true, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3902 | ICIS_NoInit); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3903 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3904 | FD->getType(), VK_LValue, |
| 3905 | OK_Ordinary); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3906 | |
Fariborz Jahanian | 8188e5f | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 3907 | |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3908 | CastExpr *FunkCast = NoTypeInfoCStyleCastExpr(Context, PtrToFuncCastType, |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3909 | CK_BitCast, ME); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3910 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3911 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3912 | SmallVector<Expr*, 8> BlkExprs; |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3913 | // Add the implicit argument. |
| 3914 | BlkExprs.push_back(BlkCast); |
| 3915 | // Add the user arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3916 | for (CallExpr::arg_iterator I = Exp->arg_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3917 | E = Exp->arg_end(); I != E; ++I) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3918 | BlkExprs.push_back(*I); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3919 | } |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3920 | CallExpr *CE = new (Context) CallExpr(*Context, PE, BlkExprs, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3921 | Exp->getType(), VK_RValue, |
| 3922 | SourceLocation()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3923 | return CE; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3924 | } |
| 3925 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3926 | // We need to return the rewritten expression to handle cases where the |
| 3927 | // BlockDeclRefExpr is embedded in another expression being rewritten. |
| 3928 | // For example: |
| 3929 | // |
| 3930 | // int main() { |
| 3931 | // __block Foo *f; |
| 3932 | // __block int i; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3933 | // |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3934 | // void (^myblock)() = ^() { |
| 3935 | // [f test]; // f is a BlockDeclRefExpr embedded in a message (which is being rewritten). |
| 3936 | // i = 77; |
| 3937 | // }; |
| 3938 | //} |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3939 | Stmt *RewriteObjC::RewriteBlockDeclRefExpr(DeclRefExpr *DeclRefExp) { |
Fariborz Jahanian | bbf37e2 | 2009-12-23 19:26:34 +0000 | [diff] [blame] | 3940 | // Rewrite the byref variable into BYREFVAR->__forwarding->BYREFVAR |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3941 | // for each DeclRefExp where BYREFVAR is name of the variable. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3942 | ValueDecl *VD = DeclRefExp->getDecl(); |
| 3943 | bool isArrow = DeclRefExp->refersToEnclosingLocal(); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3944 | |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3945 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3946 | SourceLocation(), |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3947 | &Context->Idents.get("__forwarding"), |
| 3948 | Context->VoidPtrTy, 0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3949 | /*BitWidth=*/0, /*Mutable=*/true, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3950 | ICIS_NoInit); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3951 | MemberExpr *ME = new (Context) MemberExpr(DeclRefExp, isArrow, |
| 3952 | FD, SourceLocation(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3953 | FD->getType(), VK_LValue, |
| 3954 | OK_Ordinary); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3955 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3956 | StringRef Name = VD->getName(); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3957 | FD = FieldDecl::Create(*Context, 0, SourceLocation(), SourceLocation(), |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3958 | &Context->Idents.get(Name), |
| 3959 | Context->VoidPtrTy, 0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3960 | /*BitWidth=*/0, /*Mutable=*/true, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3961 | ICIS_NoInit); |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3962 | ME = new (Context) MemberExpr(ME, true, FD, SourceLocation(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3963 | DeclRefExp->getType(), VK_LValue, OK_Ordinary); |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3964 | |
| 3965 | |
| 3966 | |
Steve Naroff | df8570d | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 3967 | // Need parens to enforce precedence. |
Fariborz Jahanian | 380ee50 | 2011-04-01 19:19:28 +0000 | [diff] [blame] | 3968 | ParenExpr *PE = new (Context) ParenExpr(DeclRefExp->getExprLoc(), |
| 3969 | DeclRefExp->getExprLoc(), |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3970 | ME); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3971 | ReplaceStmt(DeclRefExp, PE); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3972 | return PE; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3973 | } |
| 3974 | |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3975 | // Rewrites the imported local variable V with external storage |
| 3976 | // (static, extern, etc.) as *V |
| 3977 | // |
| 3978 | Stmt *RewriteObjC::RewriteLocalVariableExternalStorage(DeclRefExpr *DRE) { |
| 3979 | ValueDecl *VD = DRE->getDecl(); |
| 3980 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 3981 | if (!ImportedLocalExternalDecls.count(Var)) |
| 3982 | return DRE; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3983 | Expr *Exp = new (Context) UnaryOperator(DRE, UO_Deref, DRE->getType(), |
| 3984 | VK_LValue, OK_Ordinary, |
| 3985 | DRE->getLocation()); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3986 | // Need parens to enforce precedence. |
| 3987 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3988 | Exp); |
| 3989 | ReplaceStmt(DRE, PE); |
| 3990 | return PE; |
| 3991 | } |
| 3992 | |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 3993 | void RewriteObjC::RewriteCastExpr(CStyleCastExpr *CE) { |
| 3994 | SourceLocation LocStart = CE->getLParenLoc(); |
| 3995 | SourceLocation LocEnd = CE->getRParenLoc(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3996 | |
| 3997 | // Need to avoid trying to rewrite synthesized casts. |
| 3998 | if (LocStart.isInvalid()) |
| 3999 | return; |
Steve Naroff | 8f6ce57 | 2008-11-03 11:20:24 +0000 | [diff] [blame] | 4000 | // Need to avoid trying to rewrite casts contained in macros. |
| 4001 | if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) |
| 4002 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4003 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4004 | const char *startBuf = SM->getCharacterData(LocStart); |
| 4005 | const char *endBuf = SM->getCharacterData(LocEnd); |
Fariborz Jahanian | 1d4fca2 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4006 | QualType QT = CE->getType(); |
| 4007 | const Type* TypePtr = QT->getAs<Type>(); |
| 4008 | if (isa<TypeOfExprType>(TypePtr)) { |
| 4009 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 4010 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 4011 | std::string TypeAsString = "("; |
Fariborz Jahanian | afad76f | 2010-02-18 01:20:22 +0000 | [diff] [blame] | 4012 | RewriteBlockPointerType(TypeAsString, QT); |
Fariborz Jahanian | 1d4fca2 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4013 | TypeAsString += ")"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4014 | ReplaceText(LocStart, endBuf-startBuf+1, TypeAsString); |
Fariborz Jahanian | 1d4fca2 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4015 | return; |
| 4016 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4017 | // advance the location to startArgList. |
| 4018 | const char *argPtr = startBuf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4019 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4020 | while (*argPtr++ && (argPtr < endBuf)) { |
| 4021 | switch (*argPtr) { |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4022 | case '^': |
| 4023 | // Replace the '^' with '*'. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4024 | LocStart = LocStart.getLocWithOffset(argPtr-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4025 | ReplaceText(LocStart, 1, "*"); |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4026 | break; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4027 | } |
| 4028 | } |
| 4029 | return; |
| 4030 | } |
| 4031 | |
| 4032 | void RewriteObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { |
| 4033 | SourceLocation DeclLoc = FD->getLocation(); |
| 4034 | unsigned parenCount = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4035 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4036 | // We have 1 or more arguments that have closure pointers. |
| 4037 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4038 | const char *startArgList = strchr(startBuf, '('); |
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 | assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); |
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 | parenCount++; |
| 4043 | // advance the location to startArgList. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4044 | DeclLoc = DeclLoc.getLocWithOffset(startArgList-startBuf); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4045 | assert((DeclLoc.isValid()) && "Invalid DeclLoc"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4046 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4047 | const char *argPtr = startArgList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4048 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4049 | while (*argPtr++ && parenCount) { |
| 4050 | switch (*argPtr) { |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4051 | case '^': |
| 4052 | // Replace the '^' with '*'. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4053 | DeclLoc = DeclLoc.getLocWithOffset(argPtr-startArgList); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4054 | ReplaceText(DeclLoc, 1, "*"); |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4055 | break; |
| 4056 | case '(': |
| 4057 | parenCount++; |
| 4058 | break; |
| 4059 | case ')': |
| 4060 | parenCount--; |
| 4061 | break; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4062 | } |
| 4063 | } |
| 4064 | return; |
| 4065 | } |
| 4066 | |
| 4067 | bool RewriteObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4068 | const FunctionProtoType *FTP; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4069 | const PointerType *PT = QT->getAs<PointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4070 | if (PT) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4071 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4072 | } else { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4073 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4074 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4075 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4076 | } |
| 4077 | if (FTP) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4078 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4079 | E = FTP->arg_type_end(); I != E; ++I) |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4080 | if (isTopLevelBlockPointerType(*I)) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4081 | return true; |
| 4082 | } |
| 4083 | return false; |
| 4084 | } |
| 4085 | |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4086 | bool RewriteObjC::PointerTypeTakesAnyObjCQualifiedType(QualType QT) { |
| 4087 | const FunctionProtoType *FTP; |
| 4088 | const PointerType *PT = QT->getAs<PointerType>(); |
| 4089 | if (PT) { |
| 4090 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4091 | } else { |
| 4092 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
| 4093 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
| 4094 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4095 | } |
| 4096 | if (FTP) { |
| 4097 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Fariborz Jahanian | 06de2cf | 2010-11-03 23:50:34 +0000 | [diff] [blame] | 4098 | E = FTP->arg_type_end(); I != E; ++I) { |
| 4099 | if ((*I)->isObjCQualifiedIdType()) |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4100 | return true; |
Fariborz Jahanian | 06de2cf | 2010-11-03 23:50:34 +0000 | [diff] [blame] | 4101 | if ((*I)->isObjCObjectPointerType() && |
| 4102 | (*I)->getPointeeType()->isObjCQualifiedInterfaceType()) |
| 4103 | return true; |
| 4104 | } |
| 4105 | |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4106 | } |
| 4107 | return false; |
| 4108 | } |
| 4109 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4110 | void RewriteObjC::GetExtentOfArgList(const char *Name, const char *&LParen, |
| 4111 | const char *&RParen) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4112 | const char *argPtr = strchr(Name, '('); |
| 4113 | assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4114 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4115 | LParen = argPtr; // output the start. |
| 4116 | argPtr++; // skip past the left paren. |
| 4117 | unsigned parenCount = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4118 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4119 | while (*argPtr && parenCount) { |
| 4120 | switch (*argPtr) { |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4121 | case '(': parenCount++; break; |
| 4122 | case ')': parenCount--; break; |
| 4123 | default: break; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4124 | } |
| 4125 | if (parenCount) argPtr++; |
| 4126 | } |
| 4127 | assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); |
| 4128 | RParen = argPtr; // output the end |
| 4129 | } |
| 4130 | |
| 4131 | void RewriteObjC::RewriteBlockPointerDecl(NamedDecl *ND) { |
| 4132 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 4133 | RewriteBlockPointerFunctionArgs(FD); |
| 4134 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4135 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4136 | // Handle Variables and Typedefs. |
| 4137 | SourceLocation DeclLoc = ND->getLocation(); |
| 4138 | QualType DeclT; |
| 4139 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4140 | DeclT = VD->getType(); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4141 | else if (TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(ND)) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4142 | DeclT = TDD->getUnderlyingType(); |
| 4143 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) |
| 4144 | DeclT = FD->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4145 | else |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4146 | llvm_unreachable("RewriteBlockPointerDecl(): Decl type not yet handled"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4147 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4148 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4149 | const char *endBuf = startBuf; |
| 4150 | // scan backward (from the decl location) for the end of the previous decl. |
| 4151 | while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) |
| 4152 | startBuf--; |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4153 | SourceLocation Start = DeclLoc.getLocWithOffset(startBuf-endBuf); |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4154 | std::string buf; |
| 4155 | unsigned OrigLength=0; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4156 | // *startBuf != '^' if we are dealing with a pointer to function that |
| 4157 | // may take block argument types (which will be handled below). |
| 4158 | if (*startBuf == '^') { |
| 4159 | // Replace the '^' with '*', computing a negative offset. |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4160 | buf = '*'; |
| 4161 | startBuf++; |
| 4162 | OrigLength++; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4163 | } |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4164 | while (*startBuf != ')') { |
| 4165 | buf += *startBuf; |
| 4166 | startBuf++; |
| 4167 | OrigLength++; |
| 4168 | } |
| 4169 | buf += ')'; |
| 4170 | OrigLength++; |
| 4171 | |
| 4172 | if (PointerTypeTakesAnyBlockArguments(DeclT) || |
| 4173 | PointerTypeTakesAnyObjCQualifiedType(DeclT)) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4174 | // Replace the '^' with '*' for arguments. |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4175 | // Replace id<P> with id/*<>*/ |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4176 | DeclLoc = ND->getLocation(); |
| 4177 | startBuf = SM->getCharacterData(DeclLoc); |
| 4178 | const char *argListBegin, *argListEnd; |
| 4179 | GetExtentOfArgList(startBuf, argListBegin, argListEnd); |
| 4180 | while (argListBegin < argListEnd) { |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4181 | if (*argListBegin == '^') |
| 4182 | buf += '*'; |
| 4183 | else if (*argListBegin == '<') { |
| 4184 | buf += "/*"; |
| 4185 | buf += *argListBegin++; |
| 4186 | OrigLength++;; |
| 4187 | while (*argListBegin != '>') { |
| 4188 | buf += *argListBegin++; |
| 4189 | OrigLength++; |
| 4190 | } |
| 4191 | buf += *argListBegin; |
| 4192 | buf += "*/"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4193 | } |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4194 | else |
| 4195 | buf += *argListBegin; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4196 | argListBegin++; |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4197 | OrigLength++; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4198 | } |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4199 | buf += ')'; |
| 4200 | OrigLength++; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4201 | } |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4202 | ReplaceText(Start, OrigLength, buf); |
| 4203 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4204 | return; |
| 4205 | } |
| 4206 | |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4207 | |
| 4208 | /// SynthesizeByrefCopyDestroyHelper - This routine synthesizes: |
| 4209 | /// void __Block_byref_id_object_copy(struct Block_byref_id_object *dst, |
| 4210 | /// struct Block_byref_id_object *src) { |
| 4211 | /// _Block_object_assign (&_dest->object, _src->object, |
| 4212 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4213 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4214 | /// _Block_object_assign(&_dest->object, _src->object, |
| 4215 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4216 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4217 | /// } |
| 4218 | /// And: |
| 4219 | /// void __Block_byref_id_object_dispose(struct Block_byref_id_object *_src) { |
| 4220 | /// _Block_object_dispose(_src->object, |
| 4221 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4222 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4223 | /// _Block_object_dispose(_src->object, |
| 4224 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4225 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4226 | /// } |
| 4227 | |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4228 | std::string RewriteObjC::SynthesizeByrefCopyDestroyHelper(VarDecl *VD, |
| 4229 | int flag) { |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4230 | std::string S; |
Benjamin Kramer | 1211a71 | 2010-01-10 19:57:50 +0000 | [diff] [blame] | 4231 | if (CopyDestroyCache.count(flag)) |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4232 | return S; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4233 | CopyDestroyCache.insert(flag); |
| 4234 | S = "static void __Block_byref_id_object_copy_"; |
| 4235 | S += utostr(flag); |
| 4236 | S += "(void *dst, void *src) {\n"; |
| 4237 | |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4238 | // offset into the object pointer is computed as: |
| 4239 | // void * + void* + int + int + void* + void * |
| 4240 | unsigned IntSize = |
| 4241 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 4242 | unsigned VoidPtrSize = |
| 4243 | static_cast<unsigned>(Context->getTypeSize(Context->VoidPtrTy)); |
| 4244 | |
Ken Dyck | 0c4e5d6 | 2011-04-30 16:08:27 +0000 | [diff] [blame] | 4245 | unsigned offset = (VoidPtrSize*4 + IntSize + IntSize)/Context->getCharWidth(); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4246 | S += " _Block_object_assign((char*)dst + "; |
| 4247 | S += utostr(offset); |
| 4248 | S += ", *(void * *) ((char*)src + "; |
| 4249 | S += utostr(offset); |
| 4250 | S += "), "; |
| 4251 | S += utostr(flag); |
| 4252 | S += ");\n}\n"; |
| 4253 | |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4254 | S += "static void __Block_byref_id_object_dispose_"; |
| 4255 | S += utostr(flag); |
| 4256 | S += "(void *src) {\n"; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4257 | S += " _Block_object_dispose(*(void * *) ((char*)src + "; |
| 4258 | S += utostr(offset); |
| 4259 | S += "), "; |
| 4260 | S += utostr(flag); |
| 4261 | S += ");\n}\n"; |
| 4262 | return S; |
| 4263 | } |
| 4264 | |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4265 | /// RewriteByRefVar - For each __block typex ND variable this routine transforms |
| 4266 | /// the declaration into: |
| 4267 | /// struct __Block_byref_ND { |
| 4268 | /// void *__isa; // NULL for everything except __weak pointers |
| 4269 | /// struct __Block_byref_ND *__forwarding; |
| 4270 | /// int32_t __flags; |
| 4271 | /// int32_t __size; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4272 | /// void *__Block_byref_id_object_copy; // If variable is __block ObjC object |
| 4273 | /// void *__Block_byref_id_object_dispose; // If variable is __block ObjC object |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4274 | /// typex ND; |
| 4275 | /// }; |
| 4276 | /// |
| 4277 | /// It then replaces declaration of ND variable with: |
| 4278 | /// struct __Block_byref_ND ND = {__isa=0B, __forwarding=&ND, __flags=some_flag, |
| 4279 | /// __size=sizeof(struct __Block_byref_ND), |
| 4280 | /// ND=initializer-if-any}; |
| 4281 | /// |
| 4282 | /// |
| 4283 | void RewriteObjC::RewriteByRefVar(VarDecl *ND) { |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 4284 | // Insert declaration for the function in which block literal is |
| 4285 | // used. |
| 4286 | if (CurFunctionDeclToDeclareForBlock) |
| 4287 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4288 | int flag = 0; |
| 4289 | int isa = 0; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4290 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
Fariborz Jahanian | d64a4f4 | 2010-02-26 22:49:11 +0000 | [diff] [blame] | 4291 | if (DeclLoc.isInvalid()) |
| 4292 | // If type location is missing, it is because of missing type (a warning). |
| 4293 | // Use variable's location which is good for this case. |
| 4294 | DeclLoc = ND->getLocation(); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4295 | const char *startBuf = SM->getCharacterData(DeclLoc); |
Fariborz Jahanian | 6f0a0a9 | 2009-12-30 20:38:08 +0000 | [diff] [blame] | 4296 | SourceLocation X = ND->getLocEnd(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 4297 | X = SM->getExpansionLoc(X); |
Fariborz Jahanian | 6f0a0a9 | 2009-12-30 20:38:08 +0000 | [diff] [blame] | 4298 | const char *endBuf = SM->getCharacterData(X); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4299 | std::string Name(ND->getNameAsString()); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4300 | std::string ByrefType; |
Fariborz Jahanian | 1e8011e | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 4301 | RewriteByRefString(ByrefType, Name, ND, true); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4302 | ByrefType += " {\n"; |
| 4303 | ByrefType += " void *__isa;\n"; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4304 | RewriteByRefString(ByrefType, Name, ND); |
| 4305 | ByrefType += " *__forwarding;\n"; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4306 | ByrefType += " int __flags;\n"; |
| 4307 | ByrefType += " int __size;\n"; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4308 | // Add void *__Block_byref_id_object_copy; |
| 4309 | // void *__Block_byref_id_object_dispose; if needed. |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4310 | QualType Ty = ND->getType(); |
| 4311 | bool HasCopyAndDispose = Context->BlockRequiresCopying(Ty); |
| 4312 | if (HasCopyAndDispose) { |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4313 | ByrefType += " void (*__Block_byref_id_object_copy)(void*, void*);\n"; |
| 4314 | ByrefType += " void (*__Block_byref_id_object_dispose)(void*);\n"; |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4315 | } |
Fariborz Jahanian | 822ac87 | 2011-03-31 22:49:32 +0000 | [diff] [blame] | 4316 | |
| 4317 | QualType T = Ty; |
| 4318 | (void)convertBlockPointerToFunctionPointer(T); |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 4319 | T.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Fariborz Jahanian | 822ac87 | 2011-03-31 22:49:32 +0000 | [diff] [blame] | 4320 | |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4321 | ByrefType += " " + Name + ";\n"; |
| 4322 | ByrefType += "};\n"; |
| 4323 | // Insert this type in global scope. It is needed by helper function. |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4324 | SourceLocation FunLocStart; |
| 4325 | if (CurFunctionDef) |
| 4326 | FunLocStart = CurFunctionDef->getTypeSpecStartLoc(); |
| 4327 | else { |
| 4328 | assert(CurMethodDef && "RewriteByRefVar - CurMethodDef is null"); |
| 4329 | FunLocStart = CurMethodDef->getLocStart(); |
| 4330 | } |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4331 | InsertText(FunLocStart, ByrefType); |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4332 | if (Ty.isObjCGCWeak()) { |
| 4333 | flag |= BLOCK_FIELD_IS_WEAK; |
| 4334 | isa = 1; |
| 4335 | } |
| 4336 | |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4337 | if (HasCopyAndDispose) { |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4338 | flag = BLOCK_BYREF_CALLER; |
| 4339 | QualType Ty = ND->getType(); |
| 4340 | // FIXME. Handle __weak variable (BLOCK_FIELD_IS_WEAK) as well. |
| 4341 | if (Ty->isBlockPointerType()) |
| 4342 | flag |= BLOCK_FIELD_IS_BLOCK; |
| 4343 | else |
| 4344 | flag |= BLOCK_FIELD_IS_OBJECT; |
| 4345 | std::string HF = SynthesizeByrefCopyDestroyHelper(ND, flag); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4346 | if (!HF.empty()) |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4347 | InsertText(FunLocStart, HF); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4348 | } |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4349 | |
| 4350 | // struct __Block_byref_ND ND = |
| 4351 | // {0, &ND, some_flag, __size=sizeof(struct __Block_byref_ND), |
| 4352 | // initializer-if-any}; |
| 4353 | bool hasInit = (ND->getInit() != 0); |
Fariborz Jahanian | e1f84f8 | 2010-01-05 18:15:57 +0000 | [diff] [blame] | 4354 | unsigned flags = 0; |
| 4355 | if (HasCopyAndDispose) |
| 4356 | flags |= BLOCK_HAS_COPY_DISPOSE; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4357 | Name = ND->getNameAsString(); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4358 | ByrefType.clear(); |
| 4359 | RewriteByRefString(ByrefType, Name, ND); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4360 | std::string ForwardingCastType("("); |
| 4361 | ForwardingCastType += ByrefType + " *)"; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4362 | if (!hasInit) { |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4363 | ByrefType += " " + Name + " = {(void*)"; |
| 4364 | ByrefType += utostr(isa); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4365 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4366 | ByrefType += utostr(flags); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4367 | ByrefType += ", "; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4368 | ByrefType += "sizeof("; |
| 4369 | RewriteByRefString(ByrefType, Name, ND); |
| 4370 | ByrefType += ")"; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4371 | if (HasCopyAndDispose) { |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4372 | ByrefType += ", __Block_byref_id_object_copy_"; |
| 4373 | ByrefType += utostr(flag); |
| 4374 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 4375 | ByrefType += utostr(flag); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4376 | } |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4377 | ByrefType += "};\n"; |
Fariborz Jahanian | 822ac87 | 2011-03-31 22:49:32 +0000 | [diff] [blame] | 4378 | unsigned nameSize = Name.size(); |
| 4379 | // for block or function pointer declaration. Name is aleady |
| 4380 | // part of the declaration. |
| 4381 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) |
| 4382 | nameSize = 1; |
| 4383 | ReplaceText(DeclLoc, endBuf-startBuf+nameSize, ByrefType); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4384 | } |
| 4385 | else { |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4386 | SourceLocation startLoc; |
| 4387 | Expr *E = ND->getInit(); |
| 4388 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 4389 | startLoc = ECE->getLParenLoc(); |
| 4390 | else |
| 4391 | startLoc = E->getLocStart(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 4392 | startLoc = SM->getExpansionLoc(startLoc); |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4393 | endBuf = SM->getCharacterData(startLoc); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4394 | ByrefType += " " + Name; |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4395 | ByrefType += " = {(void*)"; |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4396 | ByrefType += utostr(isa); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4397 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4398 | ByrefType += utostr(flags); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4399 | ByrefType += ", "; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4400 | ByrefType += "sizeof("; |
| 4401 | RewriteByRefString(ByrefType, Name, ND); |
| 4402 | ByrefType += "), "; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4403 | if (HasCopyAndDispose) { |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4404 | ByrefType += "__Block_byref_id_object_copy_"; |
| 4405 | ByrefType += utostr(flag); |
| 4406 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 4407 | ByrefType += utostr(flag); |
| 4408 | ByrefType += ", "; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4409 | } |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4410 | ReplaceText(DeclLoc, endBuf-startBuf, ByrefType); |
Steve Naroff | c5143c5 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 4411 | |
| 4412 | // Complete the newly synthesized compound expression by inserting a right |
| 4413 | // curly brace before the end of the declaration. |
| 4414 | // FIXME: This approach avoids rewriting the initializer expression. It |
| 4415 | // also assumes there is only one declarator. For example, the following |
| 4416 | // isn't currently supported by this routine (in general): |
| 4417 | // |
| 4418 | // double __block BYREFVAR = 1.34, BYREFVAR2 = 1.37; |
| 4419 | // |
Fariborz Jahanian | 5f371ee | 2010-07-21 17:36:39 +0000 | [diff] [blame] | 4420 | const char *startInitializerBuf = SM->getCharacterData(startLoc); |
| 4421 | const char *semiBuf = strchr(startInitializerBuf, ';'); |
Steve Naroff | c5143c5 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 4422 | assert((*semiBuf == ';') && "RewriteByRefVar: can't find ';'"); |
| 4423 | SourceLocation semiLoc = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4424 | startLoc.getLocWithOffset(semiBuf-startInitializerBuf); |
Steve Naroff | c5143c5 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 4425 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4426 | InsertText(semiLoc, "}"); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4427 | } |
Fariborz Jahanian | 1be6b46 | 2009-12-22 00:48:54 +0000 | [diff] [blame] | 4428 | return; |
| 4429 | } |
| 4430 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4431 | void RewriteObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4432 | // Add initializers for any closure decl refs. |
| 4433 | GetBlockDeclRefExprs(Exp->getBody()); |
| 4434 | if (BlockDeclRefs.size()) { |
| 4435 | // Unique all "by copy" declarations. |
| 4436 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4437 | if (!BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>()) { |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4438 | if (!BlockByCopyDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 4439 | BlockByCopyDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 4440 | BlockByCopyDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 4441 | } |
| 4442 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4443 | // Unique all "by ref" declarations. |
| 4444 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4445 | if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>()) { |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4446 | if (!BlockByRefDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 4447 | BlockByRefDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 4448 | BlockByRefDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 4449 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4450 | } |
| 4451 | // Find any imported blocks...they will need special attention. |
| 4452 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4453 | if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>() || |
Fariborz Jahanian | 4fcc4fd | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 4454 | BlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
Fariborz Jahanian | 5b011b0 | 2010-02-26 21:46:27 +0000 | [diff] [blame] | 4455 | BlockDeclRefs[i]->getType()->isBlockPointerType()) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4456 | ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4457 | } |
| 4458 | } |
| 4459 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4460 | FunctionDecl *RewriteObjC::SynthBlockInitFunctionDecl(StringRef name) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4461 | IdentifierInfo *ID = &Context->Idents.get(name); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4462 | QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4463 | return FunctionDecl::Create(*Context, TUDecl, SourceLocation(), |
| 4464 | SourceLocation(), ID, FType, 0, SC_Extern, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4465 | SC_None, false, false); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4466 | } |
| 4467 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4468 | Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4469 | const SmallVector<DeclRefExpr *, 8> &InnerBlockDeclRefs) { |
Fariborz Jahanian | 05318d8 | 2011-02-16 22:37:10 +0000 | [diff] [blame] | 4470 | const BlockDecl *block = Exp->getBlockDecl(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4471 | Blocks.push_back(Exp); |
| 4472 | |
| 4473 | CollectBlockDeclRefInfo(Exp); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4474 | |
| 4475 | // Add inner imported variables now used in current block. |
| 4476 | int countOfInnerDecls = 0; |
Fariborz Jahanian | 1276bfe | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4477 | if (!InnerBlockDeclRefs.empty()) { |
| 4478 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) { |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4479 | DeclRefExpr *Exp = InnerBlockDeclRefs[i]; |
Fariborz Jahanian | 1276bfe | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4480 | ValueDecl *VD = Exp->getDecl(); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4481 | if (!VD->hasAttr<BlocksAttr>() && !BlockByCopyDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4482 | // We need to save the copied-in variables in nested |
| 4483 | // blocks because it is needed at the end for some of the API generations. |
| 4484 | // See SynthesizeBlockLiterals routine. |
Fariborz Jahanian | 1276bfe | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4485 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 4486 | BlockDeclRefs.push_back(Exp); |
| 4487 | BlockByCopyDeclsPtrSet.insert(VD); |
| 4488 | BlockByCopyDecls.push_back(VD); |
| 4489 | } |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4490 | if (VD->hasAttr<BlocksAttr>() && !BlockByRefDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 1276bfe | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4491 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 4492 | BlockDeclRefs.push_back(Exp); |
| 4493 | BlockByRefDeclsPtrSet.insert(VD); |
| 4494 | BlockByRefDecls.push_back(VD); |
| 4495 | } |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4496 | } |
Fariborz Jahanian | 1276bfe | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4497 | // Find any imported blocks...they will need special attention. |
| 4498 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4499 | if (InnerBlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>() || |
Fariborz Jahanian | 1276bfe | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4500 | InnerBlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
| 4501 | InnerBlockDeclRefs[i]->getType()->isBlockPointerType()) |
| 4502 | ImportedBlockDecls.insert(InnerBlockDeclRefs[i]->getDecl()); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4503 | } |
| 4504 | InnerDeclRefsCount.push_back(countOfInnerDecls); |
| 4505 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4506 | std::string FuncName; |
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 | if (CurFunctionDef) |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4509 | FuncName = CurFunctionDef->getNameAsString(); |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 4510 | else if (CurMethodDef) |
| 4511 | BuildUniqueMethodName(FuncName, CurMethodDef); |
| 4512 | else if (GlobalVarDecl) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4513 | FuncName = std::string(GlobalVarDecl->getNameAsString()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4514 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4515 | std::string BlockNumber = utostr(Blocks.size()-1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4516 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4517 | std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber; |
| 4518 | std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; |
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 | // Get a pointer to the function type so we can cast appropriately. |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4521 | QualType BFT = convertFunctionTypeOfBlocks(Exp->getFunctionType()); |
| 4522 | QualType FType = Context->getPointerType(BFT); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4523 | |
| 4524 | FunctionDecl *FD; |
| 4525 | Expr *NewRep; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4526 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4527 | // Simulate a contructor call... |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4528 | FD = SynthBlockInitFunctionDecl(Tag); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4529 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, false, FType, VK_RValue, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4530 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4531 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4532 | SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4533 | |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4534 | // Initialize the block function. |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4535 | FD = SynthBlockInitFunctionDecl(Func); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4536 | DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, false, FD->getType(), |
| 4537 | VK_LValue, SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4538 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4539 | CK_BitCast, Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4540 | InitExprs.push_back(castExpr); |
| 4541 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4542 | // Initialize the block descriptor. |
| 4543 | std::string DescData = "__" + FuncName + "_block_desc_" + BlockNumber + "_DATA"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4544 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4545 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, |
| 4546 | SourceLocation(), SourceLocation(), |
| 4547 | &Context->Idents.get(DescData.c_str()), |
| 4548 | Context->VoidPtrTy, 0, |
| 4549 | SC_Static, SC_None); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4550 | UnaryOperator *DescRefExpr = |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4551 | new (Context) UnaryOperator(new (Context) DeclRefExpr(NewVD, false, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4552 | Context->VoidPtrTy, |
| 4553 | VK_LValue, |
| 4554 | SourceLocation()), |
| 4555 | UO_AddrOf, |
| 4556 | Context->getPointerType(Context->VoidPtrTy), |
| 4557 | VK_RValue, OK_Ordinary, |
| 4558 | SourceLocation()); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4559 | InitExprs.push_back(DescRefExpr); |
| 4560 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4561 | // Add initializers for any closure decl refs. |
| 4562 | if (BlockDeclRefs.size()) { |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4563 | Expr *Exp; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4564 | // Output all "by copy" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4565 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4566 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4567 | if (isObjCType((*I)->getType())) { |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4568 | // FIXME: Conform to ABI ([[obj retain] autorelease]). |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4569 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4570 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4571 | SourceLocation()); |
Fariborz Jahanian | a5a7987 | 2010-05-24 18:32:56 +0000 | [diff] [blame] | 4572 | if (HasLocalVariableExternalStorage(*I)) { |
| 4573 | QualType QT = (*I)->getType(); |
| 4574 | QT = Context->getPointerType(QT); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4575 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue, |
| 4576 | OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | a5a7987 | 2010-05-24 18:32:56 +0000 | [diff] [blame] | 4577 | } |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4578 | } else if (isTopLevelBlockPointerType((*I)->getType())) { |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4579 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4580 | Arg = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4581 | SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4582 | Exp = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4583 | CK_BitCast, Arg); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4584 | } else { |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4585 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4586 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4587 | SourceLocation()); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4588 | if (HasLocalVariableExternalStorage(*I)) { |
| 4589 | QualType QT = (*I)->getType(); |
| 4590 | QT = Context->getPointerType(QT); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4591 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue, |
| 4592 | OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4593 | } |
| 4594 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4595 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4596 | InitExprs.push_back(Exp); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4597 | } |
| 4598 | // Output all "by ref" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4599 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4600 | E = BlockByRefDecls.end(); I != E; ++I) { |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4601 | ValueDecl *ND = (*I); |
| 4602 | std::string Name(ND->getNameAsString()); |
| 4603 | std::string RecName; |
Fariborz Jahanian | 1e8011e | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 4604 | RewriteByRefString(RecName, Name, ND, true); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4605 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str() |
| 4606 | + sizeof("struct")); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4607 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4608 | SourceLocation(), SourceLocation(), |
| 4609 | II); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4610 | assert(RD && "SynthBlockInitExpr(): Can't find RecordDecl"); |
| 4611 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 4612 | |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4613 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4614 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4615 | SourceLocation()); |
Fariborz Jahanian | 05318d8 | 2011-02-16 22:37:10 +0000 | [diff] [blame] | 4616 | bool isNestedCapturedVar = false; |
| 4617 | if (block) |
| 4618 | for (BlockDecl::capture_const_iterator ci = block->capture_begin(), |
| 4619 | ce = block->capture_end(); ci != ce; ++ci) { |
| 4620 | const VarDecl *variable = ci->getVariable(); |
| 4621 | if (variable == ND && ci->isNested()) { |
| 4622 | assert (ci->isByRef() && |
| 4623 | "SynthBlockInitExpr - captured block variable is not byref"); |
| 4624 | isNestedCapturedVar = true; |
| 4625 | break; |
| 4626 | } |
| 4627 | } |
| 4628 | // captured nested byref variable has its address passed. Do not take |
| 4629 | // its address again. |
| 4630 | if (!isNestedCapturedVar) |
| 4631 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4632 | Context->getPointerType(Exp->getType()), |
| 4633 | VK_RValue, OK_Ordinary, SourceLocation()); |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4634 | Exp = NoTypeInfoCStyleCastExpr(Context, castT, CK_BitCast, Exp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4635 | InitExprs.push_back(Exp); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4636 | } |
| 4637 | } |
Fariborz Jahanian | ff12788 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 4638 | if (ImportedBlockDecls.size()) { |
| 4639 | // generate BLOCK_HAS_COPY_DISPOSE(have helper funcs) | BLOCK_HAS_DESCRIPTOR |
| 4640 | int flag = (BLOCK_HAS_COPY_DISPOSE | BLOCK_HAS_DESCRIPTOR); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4641 | unsigned IntSize = |
| 4642 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 4643 | Expr *FlagExp = IntegerLiteral::Create(*Context, llvm::APInt(IntSize, flag), |
| 4644 | Context->IntTy, SourceLocation()); |
Fariborz Jahanian | ff12788 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 4645 | InitExprs.push_back(FlagExp); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4646 | } |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 4647 | NewRep = new (Context) CallExpr(*Context, DRE, InitExprs, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4648 | FType, VK_LValue, SourceLocation()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4649 | NewRep = new (Context) UnaryOperator(NewRep, UO_AddrOf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4650 | Context->getPointerType(NewRep->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4651 | VK_RValue, OK_Ordinary, SourceLocation()); |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4652 | NewRep = NoTypeInfoCStyleCastExpr(Context, FType, CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4653 | NewRep); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4654 | BlockDeclRefs.clear(); |
| 4655 | BlockByRefDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4656 | BlockByRefDeclsPtrSet.clear(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4657 | BlockByCopyDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4658 | BlockByCopyDeclsPtrSet.clear(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4659 | ImportedBlockDecls.clear(); |
| 4660 | return NewRep; |
| 4661 | } |
| 4662 | |
Fariborz Jahanian | 42f1e65 | 2011-02-24 21:29:21 +0000 | [diff] [blame] | 4663 | bool RewriteObjC::IsDeclStmtInForeachHeader(DeclStmt *DS) { |
| 4664 | if (const ObjCForCollectionStmt * CS = |
| 4665 | dyn_cast<ObjCForCollectionStmt>(Stmts.back())) |
| 4666 | return CS->getElement() == DS; |
| 4667 | return false; |
| 4668 | } |
| 4669 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4670 | //===----------------------------------------------------------------------===// |
| 4671 | // Function Body / Expression rewriting |
| 4672 | //===----------------------------------------------------------------------===// |
| 4673 | |
| 4674 | Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4675 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4676 | isa<DoStmt>(S) || isa<ForStmt>(S)) |
| 4677 | Stmts.push_back(S); |
| 4678 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 4679 | Stmts.push_back(S); |
Chris Lattner | 4824fcd | 2010-01-09 21:45:57 +0000 | [diff] [blame] | 4680 | ObjCBcLabelNo.push_back(++BcLabelCount); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4681 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4682 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4683 | // Pseudo-object operations and ivar references need special |
| 4684 | // treatment because we're going to recursively rewrite them. |
| 4685 | if (PseudoObjectExpr *PseudoOp = dyn_cast<PseudoObjectExpr>(S)) { |
| 4686 | if (isa<BinaryOperator>(PseudoOp->getSyntacticForm())) { |
| 4687 | return RewritePropertyOrImplicitSetter(PseudoOp); |
| 4688 | } else { |
| 4689 | return RewritePropertyOrImplicitGetter(PseudoOp); |
| 4690 | } |
| 4691 | } else if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) { |
| 4692 | return RewriteObjCIvarRefExpr(IvarRefExpr); |
| 4693 | } |
| 4694 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4695 | SourceRange OrigStmtRange = S->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4696 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4697 | // Perform a bottom up rewrite of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 4698 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4699 | if (*CI) { |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4700 | Stmt *childStmt = (*CI); |
| 4701 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(childStmt); |
Fariborz Jahanian | 1d01531 | 2011-04-11 21:17:02 +0000 | [diff] [blame] | 4702 | if (newStmt) { |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4703 | *CI = newStmt; |
Fariborz Jahanian | 1d01531 | 2011-04-11 21:17:02 +0000 | [diff] [blame] | 4704 | } |
Nick Lewycky | 7e74924 | 2010-10-31 21:07:24 +0000 | [diff] [blame] | 4705 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4706 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4707 | if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4708 | SmallVector<DeclRefExpr *, 8> InnerBlockDeclRefs; |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4709 | llvm::SmallPtrSet<const DeclContext *, 8> InnerContexts; |
| 4710 | InnerContexts.insert(BE->getBlockDecl()); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4711 | ImportedLocalExternalDecls.clear(); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4712 | GetInnerBlockDeclRefExprs(BE->getBody(), |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4713 | InnerBlockDeclRefs, InnerContexts); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4714 | // Rewrite the block body in place. |
Fariborz Jahanian | da4ad9f | 2010-11-08 18:37:50 +0000 | [diff] [blame] | 4715 | Stmt *SaveCurrentBody = CurrentBody; |
| 4716 | CurrentBody = BE->getBody(); |
| 4717 | PropParentMap = 0; |
Fariborz Jahanian | f23a0ff | 2011-08-02 20:28:46 +0000 | [diff] [blame] | 4718 | // block literal on rhs of a property-dot-sytax assignment |
| 4719 | // must be replaced by its synthesize ast so getRewrittenText |
| 4720 | // works as expected. In this case, what actually ends up on RHS |
| 4721 | // is the blockTranscribed which is the helper function for the |
| 4722 | // block literal; as in: self.c = ^() {[ace ARR];}; |
| 4723 | bool saveDisableReplaceStmt = DisableReplaceStmt; |
| 4724 | DisableReplaceStmt = false; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4725 | RewriteFunctionBodyOrGlobalInitializer(BE->getBody()); |
Fariborz Jahanian | f23a0ff | 2011-08-02 20:28:46 +0000 | [diff] [blame] | 4726 | DisableReplaceStmt = saveDisableReplaceStmt; |
Fariborz Jahanian | da4ad9f | 2010-11-08 18:37:50 +0000 | [diff] [blame] | 4727 | CurrentBody = SaveCurrentBody; |
| 4728 | PropParentMap = 0; |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4729 | ImportedLocalExternalDecls.clear(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4730 | // 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] | 4731 | std::string Str = Rewrite.getRewrittenText(BE->getSourceRange()); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4732 | RewrittenBlockExprs[BE] = Str; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4733 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4734 | Stmt *blockTranscribed = SynthBlockInitExpr(BE, InnerBlockDeclRefs); |
| 4735 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4736 | //blockTranscribed->dump(); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4737 | ReplaceStmt(S, blockTranscribed); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4738 | return blockTranscribed; |
| 4739 | } |
| 4740 | // Handle specific things. |
| 4741 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 4742 | return RewriteAtEncode(AtEncode); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4743 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4744 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 4745 | return RewriteAtSelector(AtSelector); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4746 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4747 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 4748 | return RewriteObjCStringLiteral(AtString); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4749 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4750 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4751 | #if 0 |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4752 | // Before we rewrite it, put the original message expression in a comment. |
| 4753 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 4754 | SourceLocation endLoc = MessExpr->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4755 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4756 | const char *startBuf = SM->getCharacterData(startLoc); |
| 4757 | const char *endBuf = SM->getCharacterData(endLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4758 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4759 | std::string messString; |
| 4760 | messString += "// "; |
| 4761 | messString.append(startBuf, endBuf-startBuf+1); |
| 4762 | messString += "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4763 | |
| 4764 | // FIXME: Missing definition of |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4765 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
| 4766 | // InsertText(startLoc, messString.c_str(), messString.size()); |
| 4767 | // Tried this, but it didn't work either... |
| 4768 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4769 | #endif |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4770 | return RewriteMessageExpr(MessExpr); |
| 4771 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4772 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4773 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 4774 | return RewriteObjCTryStmt(StmtTry); |
| 4775 | |
| 4776 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 4777 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 4778 | |
| 4779 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 4780 | return RewriteObjCThrowStmt(StmtThrow); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4781 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4782 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 4783 | return RewriteObjCProtocolExpr(ProtocolExp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4784 | |
| 4785 | if (ObjCForCollectionStmt *StmtForCollection = |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4786 | dyn_cast<ObjCForCollectionStmt>(S)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4787 | return RewriteObjCForCollectionStmt(StmtForCollection, |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4788 | OrigStmtRange.getEnd()); |
| 4789 | if (BreakStmt *StmtBreakStmt = |
| 4790 | dyn_cast<BreakStmt>(S)) |
| 4791 | return RewriteBreakStmt(StmtBreakStmt); |
| 4792 | if (ContinueStmt *StmtContinueStmt = |
| 4793 | dyn_cast<ContinueStmt>(S)) |
| 4794 | return RewriteContinueStmt(StmtContinueStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4795 | |
| 4796 | // 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] | 4797 | // and cast exprs. |
| 4798 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
| 4799 | // FIXME: What we're doing here is modifying the type-specifier that |
| 4800 | // precedes the first Decl. In the future the DeclGroup should have |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4801 | // a separate type-specifier that we can rewrite. |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4802 | // NOTE: We need to avoid rewriting the DeclStmt if it is within |
| 4803 | // the context of an ObjCForCollectionStmt. For example: |
| 4804 | // NSArray *someArray; |
| 4805 | // for (id <FooProtocol> index in someArray) ; |
| 4806 | // This is because RewriteObjCForCollectionStmt() does textual rewriting |
| 4807 | // and it depends on the original text locations/positions. |
Fariborz Jahanian | 42f1e65 | 2011-02-24 21:29:21 +0000 | [diff] [blame] | 4808 | if (Stmts.empty() || !IsDeclStmtInForeachHeader(DS)) |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4809 | RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4810 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4811 | // Blocks rewrite rules. |
| 4812 | for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); |
| 4813 | DI != DE; ++DI) { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 4814 | Decl *SD = *DI; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4815 | if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4816 | if (isTopLevelBlockPointerType(ND->getType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4817 | RewriteBlockPointerDecl(ND); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4818 | else if (ND->getType()->isFunctionPointerType()) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4819 | CheckFunctionPointerDecl(ND->getType(), ND); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 4820 | if (VarDecl *VD = dyn_cast<VarDecl>(SD)) { |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4821 | if (VD->hasAttr<BlocksAttr>()) { |
| 4822 | static unsigned uniqueByrefDeclCount = 0; |
| 4823 | assert(!BlockByRefDeclNo.count(ND) && |
| 4824 | "RewriteFunctionBodyOrGlobalInitializer: Duplicate byref decl"); |
| 4825 | BlockByRefDeclNo[ND] = uniqueByrefDeclCount++; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4826 | RewriteByRefVar(VD); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4827 | } |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 4828 | else |
| 4829 | RewriteTypeOfDecl(VD); |
| 4830 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4831 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4832 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4833 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4834 | RewriteBlockPointerDecl(TD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4835 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4836 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 4837 | } |
| 4838 | } |
| 4839 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4840 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4841 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) |
| 4842 | RewriteObjCQualifiedInterfaceTypes(CE); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4843 | |
| 4844 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4845 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 4846 | assert(!Stmts.empty() && "Statement stack is empty"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4847 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 4848 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4849 | && "Statement stack mismatch"); |
| 4850 | Stmts.pop_back(); |
| 4851 | } |
| 4852 | // Handle blocks rewriting. |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4853 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 4854 | ValueDecl *VD = DRE->getDecl(); |
| 4855 | if (VD->hasAttr<BlocksAttr>()) |
| 4856 | return RewriteBlockDeclRefExpr(DRE); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4857 | if (HasLocalVariableExternalStorage(VD)) |
| 4858 | return RewriteLocalVariableExternalStorage(DRE); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4859 | } |
| 4860 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4861 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4862 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4863 | Stmt *BlockCall = SynthesizeBlockCall(CE, CE->getCallee()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4864 | ReplaceStmt(S, BlockCall); |
| 4865 | return BlockCall; |
| 4866 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4867 | } |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4868 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4869 | RewriteCastExpr(CE); |
| 4870 | } |
| 4871 | #if 0 |
| 4872 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4873 | CastExpr *Replacement = new (Context) CastExpr(ICE->getType(), |
| 4874 | ICE->getSubExpr(), |
| 4875 | SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4876 | // Get the new text. |
| 4877 | std::string SStr; |
| 4878 | llvm::raw_string_ostream Buf(SStr); |
Richard Smith | d1420c6 | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 4879 | Replacement->printPretty(Buf); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4880 | const std::string &Str = Buf.str(); |
| 4881 | |
| 4882 | printf("CAST = %s\n", &Str[0]); |
| 4883 | InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 4884 | delete S; |
| 4885 | return Replacement; |
| 4886 | } |
| 4887 | #endif |
| 4888 | // Return this stmt unmodified. |
| 4889 | return S; |
| 4890 | } |
| 4891 | |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4892 | void RewriteObjC::RewriteRecordBody(RecordDecl *RD) { |
| 4893 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 4894 | e = RD->field_end(); i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4895 | FieldDecl *FD = *i; |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4896 | if (isTopLevelBlockPointerType(FD->getType())) |
| 4897 | RewriteBlockPointerDecl(FD); |
| 4898 | if (FD->getType()->isObjCQualifiedIdType() || |
| 4899 | FD->getType()->isObjCQualifiedInterfaceType()) |
| 4900 | RewriteObjCQualifiedInterfaceTypes(FD); |
| 4901 | } |
| 4902 | } |
| 4903 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4904 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 4905 | /// main file of the input. |
| 4906 | void RewriteObjC::HandleDeclInMainFile(Decl *D) { |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4907 | switch (D->getKind()) { |
| 4908 | case Decl::Function: { |
| 4909 | FunctionDecl *FD = cast<FunctionDecl>(D); |
| 4910 | if (FD->isOverloadedOperator()) |
| 4911 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4912 | |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4913 | // Since function prototypes don't have ParmDecl's, we check the function |
| 4914 | // prototype. This enables us to rewrite function declarations and |
| 4915 | // definitions using the same code. |
| 4916 | RewriteBlocksInFunctionProtoType(FD->getType(), FD); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4917 | |
Argyrios Kyrtzidis | 9335df3 | 2012-02-12 04:48:45 +0000 | [diff] [blame] | 4918 | if (!FD->isThisDeclarationADefinition()) |
| 4919 | break; |
| 4920 | |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4921 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
| 4922 | if (CompoundStmt *Body = dyn_cast_or_null<CompoundStmt>(FD->getBody())) { |
| 4923 | CurFunctionDef = FD; |
| 4924 | CurFunctionDeclToDeclareForBlock = FD; |
| 4925 | CurrentBody = Body; |
| 4926 | Body = |
| 4927 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 4928 | FD->setBody(Body); |
| 4929 | CurrentBody = 0; |
| 4930 | if (PropParentMap) { |
| 4931 | delete PropParentMap; |
| 4932 | PropParentMap = 0; |
| 4933 | } |
| 4934 | // This synthesizes and inserts the block "impl" struct, invoke function, |
| 4935 | // and any copy/dispose helper functions. |
| 4936 | InsertBlockLiteralsWithinFunction(FD); |
| 4937 | CurFunctionDef = 0; |
| 4938 | CurFunctionDeclToDeclareForBlock = 0; |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4939 | } |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4940 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4941 | } |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4942 | case Decl::ObjCMethod: { |
| 4943 | ObjCMethodDecl *MD = cast<ObjCMethodDecl>(D); |
| 4944 | if (CompoundStmt *Body = MD->getCompoundBody()) { |
| 4945 | CurMethodDef = MD; |
| 4946 | CurrentBody = Body; |
| 4947 | Body = |
| 4948 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 4949 | MD->setBody(Body); |
| 4950 | CurrentBody = 0; |
| 4951 | if (PropParentMap) { |
| 4952 | delete PropParentMap; |
| 4953 | PropParentMap = 0; |
| 4954 | } |
| 4955 | InsertBlockLiteralsWithinMethod(MD); |
| 4956 | CurMethodDef = 0; |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4957 | } |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4958 | break; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4959 | } |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4960 | case Decl::ObjCImplementation: { |
| 4961 | ObjCImplementationDecl *CI = cast<ObjCImplementationDecl>(D); |
| 4962 | ClassImplementation.push_back(CI); |
| 4963 | break; |
| 4964 | } |
| 4965 | case Decl::ObjCCategoryImpl: { |
| 4966 | ObjCCategoryImplDecl *CI = cast<ObjCCategoryImplDecl>(D); |
| 4967 | CategoryImplementation.push_back(CI); |
| 4968 | break; |
| 4969 | } |
| 4970 | case Decl::Var: { |
| 4971 | VarDecl *VD = cast<VarDecl>(D); |
| 4972 | RewriteObjCQualifiedInterfaceTypes(VD); |
| 4973 | if (isTopLevelBlockPointerType(VD->getType())) |
| 4974 | RewriteBlockPointerDecl(VD); |
| 4975 | else if (VD->getType()->isFunctionPointerType()) { |
| 4976 | CheckFunctionPointerDecl(VD->getType(), VD); |
| 4977 | if (VD->getInit()) { |
| 4978 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
| 4979 | RewriteCastExpr(CE); |
| 4980 | } |
| 4981 | } |
| 4982 | } else if (VD->getType()->isRecordType()) { |
| 4983 | RecordDecl *RD = VD->getType()->getAs<RecordType>()->getDecl(); |
| 4984 | if (RD->isCompleteDefinition()) |
| 4985 | RewriteRecordBody(RD); |
| 4986 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4987 | if (VD->getInit()) { |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4988 | GlobalVarDecl = VD; |
| 4989 | CurrentBody = VD->getInit(); |
| 4990 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
| 4991 | CurrentBody = 0; |
| 4992 | if (PropParentMap) { |
| 4993 | delete PropParentMap; |
| 4994 | PropParentMap = 0; |
| 4995 | } |
| 4996 | SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), VD->getName()); |
| 4997 | GlobalVarDecl = 0; |
| 4998 | |
| 4999 | // This is needed for blocks. |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 5000 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 5001 | RewriteCastExpr(CE); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5002 | } |
| 5003 | } |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 5004 | break; |
| 5005 | } |
| 5006 | case Decl::TypeAlias: |
| 5007 | case Decl::Typedef: { |
| 5008 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
| 5009 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
| 5010 | RewriteBlockPointerDecl(TD); |
| 5011 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
| 5012 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 5013 | } |
| 5014 | break; |
| 5015 | } |
| 5016 | case Decl::CXXRecord: |
| 5017 | case Decl::Record: { |
| 5018 | RecordDecl *RD = cast<RecordDecl>(D); |
| 5019 | if (RD->isCompleteDefinition()) |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5020 | RewriteRecordBody(RD); |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 5021 | break; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5022 | } |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 5023 | default: |
| 5024 | break; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5025 | } |
| 5026 | // Nothing yet. |
| 5027 | } |
| 5028 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 5029 | void RewriteObjC::HandleTranslationUnit(ASTContext &C) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5030 | if (Diags.hasErrorOccurred()) |
| 5031 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5032 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5033 | RewriteInclude(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5034 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5035 | // Here's a great place to add any extra declarations that may be needed. |
| 5036 | // Write out meta data for each @protocol(<expr>). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5037 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5038 | E = ProtocolExprDecls.end(); I != E; ++I) |
| 5039 | RewriteObjCProtocolMetaData(*I, "", "", Preamble); |
| 5040 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5041 | InsertText(SM->getLocForStartOfFile(MainFileID), Preamble, false); |
Steve Naroff | 0aab796 | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5042 | if (ClassImplementation.size() || CategoryImplementation.size()) |
| 5043 | RewriteImplementations(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5044 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5045 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 5046 | // we are done. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5047 | if (const RewriteBuffer *RewriteBuf = |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5048 | Rewrite.getRewriteBufferFor(MainFileID)) { |
| 5049 | //printf("Changed:\n"); |
| 5050 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 5051 | } else { |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5052 | llvm::errs() << "No changes\n"; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5053 | } |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 5054 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5055 | if (ClassImplementation.size() || CategoryImplementation.size() || |
| 5056 | ProtocolExprDecls.size()) { |
Steve Naroff | 0aab796 | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5057 | // Rewrite Objective-c meta data* |
| 5058 | std::string ResultStr; |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 5059 | RewriteMetaDataIntoBuffer(ResultStr); |
Steve Naroff | 0aab796 | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5060 | // Emit metadata. |
| 5061 | *OutFile << ResultStr; |
| 5062 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5063 | OutFile->flush(); |
| 5064 | } |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5065 | |
| 5066 | void RewriteObjCFragileABI::Initialize(ASTContext &context) { |
| 5067 | InitializeCommon(context); |
| 5068 | |
| 5069 | // declaring objc_selector outside the parameter list removes a silly |
| 5070 | // scope related warning... |
| 5071 | if (IsHeader) |
| 5072 | Preamble = "#pragma once\n"; |
| 5073 | Preamble += "struct objc_selector; struct objc_class;\n"; |
| 5074 | Preamble += "struct __rw_objc_super { struct objc_object *object; "; |
| 5075 | Preamble += "struct objc_object *superClass; "; |
| 5076 | if (LangOpts.MicrosoftExt) { |
| 5077 | // Add a constructor for creating temporary objects. |
| 5078 | Preamble += "__rw_objc_super(struct objc_object *o, struct objc_object *s) " |
| 5079 | ": "; |
| 5080 | Preamble += "object(o), superClass(s) {} "; |
| 5081 | } |
| 5082 | Preamble += "};\n"; |
| 5083 | Preamble += "#ifndef _REWRITER_typedef_Protocol\n"; |
| 5084 | Preamble += "typedef struct objc_object Protocol;\n"; |
| 5085 | Preamble += "#define _REWRITER_typedef_Protocol\n"; |
| 5086 | Preamble += "#endif\n"; |
| 5087 | if (LangOpts.MicrosoftExt) { |
| 5088 | Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n"; |
| 5089 | Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n"; |
| 5090 | } else |
| 5091 | Preamble += "#define __OBJC_RW_DLLIMPORT extern\n"; |
| 5092 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend"; |
| 5093 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
| 5094 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper"; |
| 5095 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
| 5096 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object* objc_msgSend_stret"; |
| 5097 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
| 5098 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object* objc_msgSendSuper_stret"; |
| 5099 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
| 5100 | Preamble += "__OBJC_RW_DLLIMPORT double objc_msgSend_fpret"; |
| 5101 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
| 5102 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getClass"; |
| 5103 | Preamble += "(const char *);\n"; |
| 5104 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_class *class_getSuperclass"; |
| 5105 | Preamble += "(struct objc_class *);\n"; |
| 5106 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getMetaClass"; |
| 5107 | Preamble += "(const char *);\n"; |
| 5108 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw(struct objc_object *);\n"; |
| 5109 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_enter(void *);\n"; |
| 5110 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_exit(void *);\n"; |
| 5111 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_exception_extract(void *);\n"; |
| 5112 | Preamble += "__OBJC_RW_DLLIMPORT int objc_exception_match"; |
| 5113 | Preamble += "(struct objc_class *, struct objc_object *);\n"; |
| 5114 | // @synchronized hooks. |
Aaron Ballman | 2d234d73 | 2012-09-06 16:44:16 +0000 | [diff] [blame^] | 5115 | Preamble += "__OBJC_RW_DLLIMPORT int objc_sync_enter(struct objc_object *);\n"; |
| 5116 | Preamble += "__OBJC_RW_DLLIMPORT int objc_sync_exit(struct objc_object *);\n"; |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5117 | Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n"; |
| 5118 | Preamble += "#ifndef __FASTENUMERATIONSTATE\n"; |
| 5119 | Preamble += "struct __objcFastEnumerationState {\n\t"; |
| 5120 | Preamble += "unsigned long state;\n\t"; |
| 5121 | Preamble += "void **itemsPtr;\n\t"; |
| 5122 | Preamble += "unsigned long *mutationsPtr;\n\t"; |
| 5123 | Preamble += "unsigned long extra[5];\n};\n"; |
| 5124 | Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n"; |
| 5125 | Preamble += "#define __FASTENUMERATIONSTATE\n"; |
| 5126 | Preamble += "#endif\n"; |
| 5127 | Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n"; |
| 5128 | Preamble += "struct __NSConstantStringImpl {\n"; |
| 5129 | Preamble += " int *isa;\n"; |
| 5130 | Preamble += " int flags;\n"; |
| 5131 | Preamble += " char *str;\n"; |
| 5132 | Preamble += " long length;\n"; |
| 5133 | Preamble += "};\n"; |
| 5134 | Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n"; |
| 5135 | Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n"; |
| 5136 | Preamble += "#else\n"; |
| 5137 | Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n"; |
| 5138 | Preamble += "#endif\n"; |
| 5139 | Preamble += "#define __NSCONSTANTSTRINGIMPL\n"; |
| 5140 | Preamble += "#endif\n"; |
| 5141 | // Blocks preamble. |
| 5142 | Preamble += "#ifndef BLOCK_IMPL\n"; |
| 5143 | Preamble += "#define BLOCK_IMPL\n"; |
| 5144 | Preamble += "struct __block_impl {\n"; |
| 5145 | Preamble += " void *isa;\n"; |
| 5146 | Preamble += " int Flags;\n"; |
| 5147 | Preamble += " int Reserved;\n"; |
| 5148 | Preamble += " void *FuncPtr;\n"; |
| 5149 | Preamble += "};\n"; |
| 5150 | Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n"; |
| 5151 | Preamble += "#ifdef __OBJC_EXPORT_BLOCKS\n"; |
| 5152 | Preamble += "extern \"C\" __declspec(dllexport) " |
| 5153 | "void _Block_object_assign(void *, const void *, const int);\n"; |
| 5154 | Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_dispose(const void *, const int);\n"; |
| 5155 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteGlobalBlock[32];\n"; |
| 5156 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteStackBlock[32];\n"; |
| 5157 | Preamble += "#else\n"; |
| 5158 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_assign(void *, const void *, const int);\n"; |
| 5159 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_dispose(const void *, const int);\n"; |
| 5160 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteGlobalBlock[32];\n"; |
| 5161 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteStackBlock[32];\n"; |
| 5162 | Preamble += "#endif\n"; |
| 5163 | Preamble += "#endif\n"; |
| 5164 | if (LangOpts.MicrosoftExt) { |
| 5165 | Preamble += "#undef __OBJC_RW_DLLIMPORT\n"; |
| 5166 | Preamble += "#undef __OBJC_RW_STATICIMPORT\n"; |
| 5167 | Preamble += "#ifndef KEEP_ATTRIBUTES\n"; // We use this for clang tests. |
| 5168 | Preamble += "#define __attribute__(X)\n"; |
| 5169 | Preamble += "#endif\n"; |
| 5170 | Preamble += "#define __weak\n"; |
| 5171 | } |
| 5172 | else { |
| 5173 | Preamble += "#define __block\n"; |
| 5174 | Preamble += "#define __weak\n"; |
| 5175 | } |
| 5176 | // NOTE! Windows uses LLP64 for 64bit mode. So, cast pointer to long long |
| 5177 | // as this avoids warning in any 64bit/32bit compilation model. |
| 5178 | Preamble += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((long long) &((TYPE *)0)->MEMBER)\n"; |
| 5179 | } |
| 5180 | |
| 5181 | /// RewriteIvarOffsetComputation - This rutine synthesizes computation of |
| 5182 | /// ivar offset. |
| 5183 | void RewriteObjCFragileABI::RewriteIvarOffsetComputation(ObjCIvarDecl *ivar, |
| 5184 | std::string &Result) { |
| 5185 | if (ivar->isBitField()) { |
| 5186 | // FIXME: The hack below doesn't work for bitfields. For now, we simply |
| 5187 | // place all bitfields at offset 0. |
| 5188 | Result += "0"; |
| 5189 | } else { |
| 5190 | Result += "__OFFSETOFIVAR__(struct "; |
| 5191 | Result += ivar->getContainingInterface()->getNameAsString(); |
| 5192 | if (LangOpts.MicrosoftExt) |
| 5193 | Result += "_IMPL"; |
| 5194 | Result += ", "; |
| 5195 | Result += ivar->getNameAsString(); |
| 5196 | Result += ")"; |
| 5197 | } |
| 5198 | } |
| 5199 | |
| 5200 | /// RewriteObjCProtocolMetaData - Rewrite protocols meta-data. |
| 5201 | void RewriteObjCFragileABI::RewriteObjCProtocolMetaData( |
| 5202 | ObjCProtocolDecl *PDecl, StringRef prefix, |
| 5203 | StringRef ClassName, std::string &Result) { |
| 5204 | static bool objc_protocol_methods = false; |
| 5205 | |
| 5206 | // Output struct protocol_methods holder of method selector and type. |
Douglas Gregor | 5e2a1ff | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 5207 | if (!objc_protocol_methods && PDecl->hasDefinition()) { |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5208 | /* struct protocol_methods { |
| 5209 | SEL _cmd; |
| 5210 | char *method_types; |
| 5211 | } |
| 5212 | */ |
| 5213 | Result += "\nstruct _protocol_methods {\n"; |
| 5214 | Result += "\tstruct objc_selector *_cmd;\n"; |
| 5215 | Result += "\tchar *method_types;\n"; |
| 5216 | Result += "};\n"; |
| 5217 | |
| 5218 | objc_protocol_methods = true; |
| 5219 | } |
| 5220 | // Do not synthesize the protocol more than once. |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 5221 | if (ObjCSynthesizedProtocols.count(PDecl->getCanonicalDecl())) |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5222 | return; |
| 5223 | |
Douglas Gregor | 61cc296 | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 5224 | if (ObjCProtocolDecl *Def = PDecl->getDefinition()) |
| 5225 | PDecl = Def; |
| 5226 | |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5227 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 5228 | unsigned NumMethods = std::distance(PDecl->instmeth_begin(), |
| 5229 | PDecl->instmeth_end()); |
| 5230 | /* struct _objc_protocol_method_list { |
| 5231 | int protocol_method_count; |
| 5232 | struct protocol_methods protocols[]; |
| 5233 | } |
| 5234 | */ |
| 5235 | Result += "\nstatic struct {\n"; |
| 5236 | Result += "\tint protocol_method_count;\n"; |
| 5237 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 5238 | Result += utostr(NumMethods); |
| 5239 | Result += "];\n} _OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 5240 | Result += PDecl->getNameAsString(); |
| 5241 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= " |
| 5242 | "{\n\t" + utostr(NumMethods) + "\n"; |
| 5243 | |
| 5244 | // Output instance methods declared in this protocol. |
| 5245 | for (ObjCProtocolDecl::instmeth_iterator |
| 5246 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
| 5247 | I != E; ++I) { |
| 5248 | if (I == PDecl->instmeth_begin()) |
| 5249 | Result += "\t ,{{(struct objc_selector *)\""; |
| 5250 | else |
| 5251 | Result += "\t ,{(struct objc_selector *)\""; |
| 5252 | Result += (*I)->getSelector().getAsString(); |
| 5253 | std::string MethodTypeString; |
| 5254 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 5255 | Result += "\", \""; |
| 5256 | Result += MethodTypeString; |
| 5257 | Result += "\"}\n"; |
| 5258 | } |
| 5259 | Result += "\t }\n};\n"; |
| 5260 | } |
| 5261 | |
| 5262 | // Output class methods declared in this protocol. |
| 5263 | unsigned NumMethods = std::distance(PDecl->classmeth_begin(), |
| 5264 | PDecl->classmeth_end()); |
| 5265 | if (NumMethods > 0) { |
| 5266 | /* struct _objc_protocol_method_list { |
| 5267 | int protocol_method_count; |
| 5268 | struct protocol_methods protocols[]; |
| 5269 | } |
| 5270 | */ |
| 5271 | Result += "\nstatic struct {\n"; |
| 5272 | Result += "\tint protocol_method_count;\n"; |
| 5273 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 5274 | Result += utostr(NumMethods); |
| 5275 | Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_"; |
| 5276 | Result += PDecl->getNameAsString(); |
| 5277 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 5278 | "{\n\t"; |
| 5279 | Result += utostr(NumMethods); |
| 5280 | Result += "\n"; |
| 5281 | |
| 5282 | // Output instance methods declared in this protocol. |
| 5283 | for (ObjCProtocolDecl::classmeth_iterator |
| 5284 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
| 5285 | I != E; ++I) { |
| 5286 | if (I == PDecl->classmeth_begin()) |
| 5287 | Result += "\t ,{{(struct objc_selector *)\""; |
| 5288 | else |
| 5289 | Result += "\t ,{(struct objc_selector *)\""; |
| 5290 | Result += (*I)->getSelector().getAsString(); |
| 5291 | std::string MethodTypeString; |
| 5292 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 5293 | Result += "\", \""; |
| 5294 | Result += MethodTypeString; |
| 5295 | Result += "\"}\n"; |
| 5296 | } |
| 5297 | Result += "\t }\n};\n"; |
| 5298 | } |
| 5299 | |
| 5300 | // Output: |
| 5301 | /* struct _objc_protocol { |
| 5302 | // Objective-C 1.0 extensions |
| 5303 | struct _objc_protocol_extension *isa; |
| 5304 | char *protocol_name; |
| 5305 | struct _objc_protocol **protocol_list; |
| 5306 | struct _objc_protocol_method_list *instance_methods; |
| 5307 | struct _objc_protocol_method_list *class_methods; |
| 5308 | }; |
| 5309 | */ |
| 5310 | static bool objc_protocol = false; |
| 5311 | if (!objc_protocol) { |
| 5312 | Result += "\nstruct _objc_protocol {\n"; |
| 5313 | Result += "\tstruct _objc_protocol_extension *isa;\n"; |
| 5314 | Result += "\tchar *protocol_name;\n"; |
| 5315 | Result += "\tstruct _objc_protocol **protocol_list;\n"; |
| 5316 | Result += "\tstruct _objc_protocol_method_list *instance_methods;\n"; |
| 5317 | Result += "\tstruct _objc_protocol_method_list *class_methods;\n"; |
| 5318 | Result += "};\n"; |
| 5319 | |
| 5320 | objc_protocol = true; |
| 5321 | } |
| 5322 | |
| 5323 | Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_"; |
| 5324 | Result += PDecl->getNameAsString(); |
| 5325 | Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= " |
| 5326 | "{\n\t0, \""; |
| 5327 | Result += PDecl->getNameAsString(); |
| 5328 | Result += "\", 0, "; |
| 5329 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 5330 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 5331 | Result += PDecl->getNameAsString(); |
| 5332 | Result += ", "; |
| 5333 | } |
| 5334 | else |
| 5335 | Result += "0, "; |
| 5336 | if (PDecl->classmeth_begin() != PDecl->classmeth_end()) { |
| 5337 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
| 5338 | Result += PDecl->getNameAsString(); |
| 5339 | Result += "\n"; |
| 5340 | } |
| 5341 | else |
| 5342 | Result += "0\n"; |
| 5343 | Result += "};\n"; |
| 5344 | |
| 5345 | // Mark this protocol as having been generated. |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 5346 | if (!ObjCSynthesizedProtocols.insert(PDecl->getCanonicalDecl())) |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5347 | llvm_unreachable("protocol already synthesized"); |
| 5348 | |
| 5349 | } |
| 5350 | |
| 5351 | void RewriteObjCFragileABI::RewriteObjCProtocolListMetaData( |
| 5352 | const ObjCList<ObjCProtocolDecl> &Protocols, |
| 5353 | StringRef prefix, StringRef ClassName, |
| 5354 | std::string &Result) { |
| 5355 | if (Protocols.empty()) return; |
| 5356 | |
| 5357 | for (unsigned i = 0; i != Protocols.size(); i++) |
| 5358 | RewriteObjCProtocolMetaData(Protocols[i], prefix, ClassName, Result); |
| 5359 | |
| 5360 | // Output the top lovel protocol meta-data for the class. |
| 5361 | /* struct _objc_protocol_list { |
| 5362 | struct _objc_protocol_list *next; |
| 5363 | int protocol_count; |
| 5364 | struct _objc_protocol *class_protocols[]; |
| 5365 | } |
| 5366 | */ |
| 5367 | Result += "\nstatic struct {\n"; |
| 5368 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 5369 | Result += "\tint protocol_count;\n"; |
| 5370 | Result += "\tstruct _objc_protocol *class_protocols["; |
| 5371 | Result += utostr(Protocols.size()); |
| 5372 | Result += "];\n} _OBJC_"; |
| 5373 | Result += prefix; |
| 5374 | Result += "_PROTOCOLS_"; |
| 5375 | Result += ClassName; |
| 5376 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 5377 | "{\n\t0, "; |
| 5378 | Result += utostr(Protocols.size()); |
| 5379 | Result += "\n"; |
| 5380 | |
| 5381 | Result += "\t,{&_OBJC_PROTOCOL_"; |
| 5382 | Result += Protocols[0]->getNameAsString(); |
| 5383 | Result += " \n"; |
| 5384 | |
| 5385 | for (unsigned i = 1; i != Protocols.size(); i++) { |
| 5386 | Result += "\t ,&_OBJC_PROTOCOL_"; |
| 5387 | Result += Protocols[i]->getNameAsString(); |
| 5388 | Result += "\n"; |
| 5389 | } |
| 5390 | Result += "\t }\n};\n"; |
| 5391 | } |
| 5392 | |
| 5393 | void RewriteObjCFragileABI::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
| 5394 | std::string &Result) { |
| 5395 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
| 5396 | |
| 5397 | // Explicitly declared @interface's are already synthesized. |
| 5398 | if (CDecl->isImplicitInterfaceDecl()) { |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 5399 | // FIXME: Implementation of a class with no @interface (legacy) does not |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5400 | // produce correct synthesis as yet. |
| 5401 | RewriteObjCInternalStruct(CDecl, Result); |
| 5402 | } |
| 5403 | |
| 5404 | // Build _objc_ivar_list metadata for classes ivars if needed |
| 5405 | unsigned NumIvars = !IDecl->ivar_empty() |
| 5406 | ? IDecl->ivar_size() |
| 5407 | : (CDecl ? CDecl->ivar_size() : 0); |
| 5408 | if (NumIvars > 0) { |
| 5409 | static bool objc_ivar = false; |
| 5410 | if (!objc_ivar) { |
| 5411 | /* struct _objc_ivar { |
| 5412 | char *ivar_name; |
| 5413 | char *ivar_type; |
| 5414 | int ivar_offset; |
| 5415 | }; |
| 5416 | */ |
| 5417 | Result += "\nstruct _objc_ivar {\n"; |
| 5418 | Result += "\tchar *ivar_name;\n"; |
| 5419 | Result += "\tchar *ivar_type;\n"; |
| 5420 | Result += "\tint ivar_offset;\n"; |
| 5421 | Result += "};\n"; |
| 5422 | |
| 5423 | objc_ivar = true; |
| 5424 | } |
| 5425 | |
| 5426 | /* struct { |
| 5427 | int ivar_count; |
| 5428 | struct _objc_ivar ivar_list[nIvars]; |
| 5429 | }; |
| 5430 | */ |
| 5431 | Result += "\nstatic struct {\n"; |
| 5432 | Result += "\tint ivar_count;\n"; |
| 5433 | Result += "\tstruct _objc_ivar ivar_list["; |
| 5434 | Result += utostr(NumIvars); |
| 5435 | Result += "];\n} _OBJC_INSTANCE_VARIABLES_"; |
| 5436 | Result += IDecl->getNameAsString(); |
| 5437 | Result += " __attribute__ ((used, section (\"__OBJC, __instance_vars\")))= " |
| 5438 | "{\n\t"; |
| 5439 | Result += utostr(NumIvars); |
| 5440 | Result += "\n"; |
| 5441 | |
| 5442 | ObjCInterfaceDecl::ivar_iterator IVI, IVE; |
| 5443 | SmallVector<ObjCIvarDecl *, 8> IVars; |
| 5444 | if (!IDecl->ivar_empty()) { |
| 5445 | for (ObjCInterfaceDecl::ivar_iterator |
| 5446 | IV = IDecl->ivar_begin(), IVEnd = IDecl->ivar_end(); |
| 5447 | IV != IVEnd; ++IV) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5448 | IVars.push_back(*IV); |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5449 | IVI = IDecl->ivar_begin(); |
| 5450 | IVE = IDecl->ivar_end(); |
| 5451 | } else { |
| 5452 | IVI = CDecl->ivar_begin(); |
| 5453 | IVE = CDecl->ivar_end(); |
| 5454 | } |
| 5455 | Result += "\t,{{\""; |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5456 | Result += IVI->getNameAsString(); |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5457 | Result += "\", \""; |
| 5458 | std::string TmpString, StrEncoding; |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5459 | Context->getObjCEncodingForType(IVI->getType(), TmpString, *IVI); |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5460 | QuoteDoublequotes(TmpString, StrEncoding); |
| 5461 | Result += StrEncoding; |
| 5462 | Result += "\", "; |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5463 | RewriteIvarOffsetComputation(*IVI, Result); |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5464 | Result += "}\n"; |
| 5465 | for (++IVI; IVI != IVE; ++IVI) { |
| 5466 | Result += "\t ,{\""; |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5467 | Result += IVI->getNameAsString(); |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5468 | Result += "\", \""; |
| 5469 | std::string TmpString, StrEncoding; |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5470 | Context->getObjCEncodingForType(IVI->getType(), TmpString, *IVI); |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5471 | QuoteDoublequotes(TmpString, StrEncoding); |
| 5472 | Result += StrEncoding; |
| 5473 | Result += "\", "; |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5474 | RewriteIvarOffsetComputation(*IVI, Result); |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5475 | Result += "}\n"; |
| 5476 | } |
| 5477 | |
| 5478 | Result += "\t }\n};\n"; |
| 5479 | } |
| 5480 | |
| 5481 | // Build _objc_method_list for class's instance methods if needed |
| 5482 | SmallVector<ObjCMethodDecl *, 32> |
| 5483 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
| 5484 | |
| 5485 | // If any of our property implementations have associated getters or |
| 5486 | // setters, produce metadata for them as well. |
| 5487 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 5488 | PropEnd = IDecl->propimpl_end(); |
| 5489 | Prop != PropEnd; ++Prop) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5490 | if (Prop->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5491 | continue; |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5492 | if (!Prop->getPropertyIvarDecl()) |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5493 | continue; |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5494 | ObjCPropertyDecl *PD = Prop->getPropertyDecl(); |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5495 | if (!PD) |
| 5496 | continue; |
| 5497 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 5498 | if (!Getter->isDefined()) |
| 5499 | InstanceMethods.push_back(Getter); |
| 5500 | if (PD->isReadOnly()) |
| 5501 | continue; |
| 5502 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 5503 | if (!Setter->isDefined()) |
| 5504 | InstanceMethods.push_back(Setter); |
| 5505 | } |
| 5506 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
| 5507 | true, "", IDecl->getName(), Result); |
| 5508 | |
| 5509 | // Build _objc_method_list for class's class methods if needed |
| 5510 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
| 5511 | false, "", IDecl->getName(), Result); |
| 5512 | |
| 5513 | // Protocols referenced in class declaration? |
| 5514 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), |
| 5515 | "CLASS", CDecl->getName(), Result); |
| 5516 | |
| 5517 | // Declaration of class/meta-class metadata |
| 5518 | /* struct _objc_class { |
| 5519 | struct _objc_class *isa; // or const char *root_class_name when metadata |
| 5520 | const char *super_class_name; |
| 5521 | char *name; |
| 5522 | long version; |
| 5523 | long info; |
| 5524 | long instance_size; |
| 5525 | struct _objc_ivar_list *ivars; |
| 5526 | struct _objc_method_list *methods; |
| 5527 | struct objc_cache *cache; |
| 5528 | struct objc_protocol_list *protocols; |
| 5529 | const char *ivar_layout; |
| 5530 | struct _objc_class_ext *ext; |
| 5531 | }; |
| 5532 | */ |
| 5533 | static bool objc_class = false; |
| 5534 | if (!objc_class) { |
| 5535 | Result += "\nstruct _objc_class {\n"; |
| 5536 | Result += "\tstruct _objc_class *isa;\n"; |
| 5537 | Result += "\tconst char *super_class_name;\n"; |
| 5538 | Result += "\tchar *name;\n"; |
| 5539 | Result += "\tlong version;\n"; |
| 5540 | Result += "\tlong info;\n"; |
| 5541 | Result += "\tlong instance_size;\n"; |
| 5542 | Result += "\tstruct _objc_ivar_list *ivars;\n"; |
| 5543 | Result += "\tstruct _objc_method_list *methods;\n"; |
| 5544 | Result += "\tstruct objc_cache *cache;\n"; |
| 5545 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 5546 | Result += "\tconst char *ivar_layout;\n"; |
| 5547 | Result += "\tstruct _objc_class_ext *ext;\n"; |
| 5548 | Result += "};\n"; |
| 5549 | objc_class = true; |
| 5550 | } |
| 5551 | |
| 5552 | // Meta-class metadata generation. |
| 5553 | ObjCInterfaceDecl *RootClass = 0; |
| 5554 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
| 5555 | while (SuperClass) { |
| 5556 | RootClass = SuperClass; |
| 5557 | SuperClass = SuperClass->getSuperClass(); |
| 5558 | } |
| 5559 | SuperClass = CDecl->getSuperClass(); |
| 5560 | |
| 5561 | Result += "\nstatic struct _objc_class _OBJC_METACLASS_"; |
| 5562 | Result += CDecl->getNameAsString(); |
| 5563 | Result += " __attribute__ ((used, section (\"__OBJC, __meta_class\")))= " |
| 5564 | "{\n\t(struct _objc_class *)\""; |
| 5565 | Result += (RootClass ? RootClass->getNameAsString() : CDecl->getNameAsString()); |
| 5566 | Result += "\""; |
| 5567 | |
| 5568 | if (SuperClass) { |
| 5569 | Result += ", \""; |
| 5570 | Result += SuperClass->getNameAsString(); |
| 5571 | Result += "\", \""; |
| 5572 | Result += CDecl->getNameAsString(); |
| 5573 | Result += "\""; |
| 5574 | } |
| 5575 | else { |
| 5576 | Result += ", 0, \""; |
| 5577 | Result += CDecl->getNameAsString(); |
| 5578 | Result += "\""; |
| 5579 | } |
| 5580 | // Set 'ivars' field for root class to 0. ObjC1 runtime does not use it. |
| 5581 | // 'info' field is initialized to CLS_META(2) for metaclass |
| 5582 | Result += ", 0,2, sizeof(struct _objc_class), 0"; |
| 5583 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
| 5584 | Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_"; |
| 5585 | Result += IDecl->getNameAsString(); |
| 5586 | Result += "\n"; |
| 5587 | } |
| 5588 | else |
| 5589 | Result += ", 0\n"; |
| 5590 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
| 5591 | Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_"; |
| 5592 | Result += CDecl->getNameAsString(); |
| 5593 | Result += ",0,0\n"; |
| 5594 | } |
| 5595 | else |
| 5596 | Result += "\t,0,0,0,0\n"; |
| 5597 | Result += "};\n"; |
| 5598 | |
| 5599 | // class metadata generation. |
| 5600 | Result += "\nstatic struct _objc_class _OBJC_CLASS_"; |
| 5601 | Result += CDecl->getNameAsString(); |
| 5602 | Result += " __attribute__ ((used, section (\"__OBJC, __class\")))= " |
| 5603 | "{\n\t&_OBJC_METACLASS_"; |
| 5604 | Result += CDecl->getNameAsString(); |
| 5605 | if (SuperClass) { |
| 5606 | Result += ", \""; |
| 5607 | Result += SuperClass->getNameAsString(); |
| 5608 | Result += "\", \""; |
| 5609 | Result += CDecl->getNameAsString(); |
| 5610 | Result += "\""; |
| 5611 | } |
| 5612 | else { |
| 5613 | Result += ", 0, \""; |
| 5614 | Result += CDecl->getNameAsString(); |
| 5615 | Result += "\""; |
| 5616 | } |
| 5617 | // 'info' field is initialized to CLS_CLASS(1) for class |
| 5618 | Result += ", 0,1"; |
| 5619 | if (!ObjCSynthesizedStructs.count(CDecl)) |
| 5620 | Result += ",0"; |
| 5621 | else { |
| 5622 | // class has size. Must synthesize its size. |
| 5623 | Result += ",sizeof(struct "; |
| 5624 | Result += CDecl->getNameAsString(); |
| 5625 | if (LangOpts.MicrosoftExt) |
| 5626 | Result += "_IMPL"; |
| 5627 | Result += ")"; |
| 5628 | } |
| 5629 | if (NumIvars > 0) { |
| 5630 | Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_"; |
| 5631 | Result += CDecl->getNameAsString(); |
| 5632 | Result += "\n\t"; |
| 5633 | } |
| 5634 | else |
| 5635 | Result += ",0"; |
| 5636 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
| 5637 | Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_"; |
| 5638 | Result += CDecl->getNameAsString(); |
| 5639 | Result += ", 0\n\t"; |
| 5640 | } |
| 5641 | else |
| 5642 | Result += ",0,0"; |
| 5643 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
| 5644 | Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_"; |
| 5645 | Result += CDecl->getNameAsString(); |
| 5646 | Result += ", 0,0\n"; |
| 5647 | } |
| 5648 | else |
| 5649 | Result += ",0,0,0\n"; |
| 5650 | Result += "};\n"; |
| 5651 | } |
| 5652 | |
| 5653 | void RewriteObjCFragileABI::RewriteMetaDataIntoBuffer(std::string &Result) { |
| 5654 | int ClsDefCount = ClassImplementation.size(); |
| 5655 | int CatDefCount = CategoryImplementation.size(); |
| 5656 | |
| 5657 | // For each implemented class, write out all its meta data. |
| 5658 | for (int i = 0; i < ClsDefCount; i++) |
| 5659 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
| 5660 | |
| 5661 | // For each implemented category, write out all its meta data. |
| 5662 | for (int i = 0; i < CatDefCount; i++) |
| 5663 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
| 5664 | |
| 5665 | // Write objc_symtab metadata |
| 5666 | /* |
| 5667 | struct _objc_symtab |
| 5668 | { |
| 5669 | long sel_ref_cnt; |
| 5670 | SEL *refs; |
| 5671 | short cls_def_cnt; |
| 5672 | short cat_def_cnt; |
| 5673 | void *defs[cls_def_cnt + cat_def_cnt]; |
| 5674 | }; |
| 5675 | */ |
| 5676 | |
| 5677 | Result += "\nstruct _objc_symtab {\n"; |
| 5678 | Result += "\tlong sel_ref_cnt;\n"; |
| 5679 | Result += "\tSEL *refs;\n"; |
| 5680 | Result += "\tshort cls_def_cnt;\n"; |
| 5681 | Result += "\tshort cat_def_cnt;\n"; |
| 5682 | Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n"; |
| 5683 | Result += "};\n\n"; |
| 5684 | |
| 5685 | Result += "static struct _objc_symtab " |
| 5686 | "_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n"; |
| 5687 | Result += "\t0, 0, " + utostr(ClsDefCount) |
| 5688 | + ", " + utostr(CatDefCount) + "\n"; |
| 5689 | for (int i = 0; i < ClsDefCount; i++) { |
| 5690 | Result += "\t,&_OBJC_CLASS_"; |
| 5691 | Result += ClassImplementation[i]->getNameAsString(); |
| 5692 | Result += "\n"; |
| 5693 | } |
| 5694 | |
| 5695 | for (int i = 0; i < CatDefCount; i++) { |
| 5696 | Result += "\t,&_OBJC_CATEGORY_"; |
| 5697 | Result += CategoryImplementation[i]->getClassInterface()->getNameAsString(); |
| 5698 | Result += "_"; |
| 5699 | Result += CategoryImplementation[i]->getNameAsString(); |
| 5700 | Result += "\n"; |
| 5701 | } |
| 5702 | |
| 5703 | Result += "};\n\n"; |
| 5704 | |
| 5705 | // Write objc_module metadata |
| 5706 | |
| 5707 | /* |
| 5708 | struct _objc_module { |
| 5709 | long version; |
| 5710 | long size; |
| 5711 | const char *name; |
| 5712 | struct _objc_symtab *symtab; |
| 5713 | } |
| 5714 | */ |
| 5715 | |
| 5716 | Result += "\nstruct _objc_module {\n"; |
| 5717 | Result += "\tlong version;\n"; |
| 5718 | Result += "\tlong size;\n"; |
| 5719 | Result += "\tconst char *name;\n"; |
| 5720 | Result += "\tstruct _objc_symtab *symtab;\n"; |
| 5721 | Result += "};\n\n"; |
| 5722 | Result += "static struct _objc_module " |
| 5723 | "_OBJC_MODULES __attribute__ ((used, section (\"__OBJC, __module_info\")))= {\n"; |
| 5724 | Result += "\t" + utostr(OBJC_ABI_VERSION) + |
| 5725 | ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n"; |
| 5726 | Result += "};\n\n"; |
| 5727 | |
| 5728 | if (LangOpts.MicrosoftExt) { |
| 5729 | if (ProtocolExprDecls.size()) { |
| 5730 | Result += "#pragma section(\".objc_protocol$B\",long,read,write)\n"; |
| 5731 | Result += "#pragma data_seg(push, \".objc_protocol$B\")\n"; |
| 5732 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
| 5733 | E = ProtocolExprDecls.end(); I != E; ++I) { |
| 5734 | Result += "static struct _objc_protocol *_POINTER_OBJC_PROTOCOL_"; |
| 5735 | Result += (*I)->getNameAsString(); |
| 5736 | Result += " = &_OBJC_PROTOCOL_"; |
| 5737 | Result += (*I)->getNameAsString(); |
| 5738 | Result += ";\n"; |
| 5739 | } |
| 5740 | Result += "#pragma data_seg(pop)\n\n"; |
| 5741 | } |
| 5742 | Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n"; |
| 5743 | Result += "#pragma data_seg(push, \".objc_module_info$B\")\n"; |
| 5744 | Result += "static struct _objc_module *_POINTER_OBJC_MODULES = "; |
| 5745 | Result += "&_OBJC_MODULES;\n"; |
| 5746 | Result += "#pragma data_seg(pop)\n\n"; |
| 5747 | } |
| 5748 | } |
| 5749 | |
| 5750 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
| 5751 | /// implementation. |
| 5752 | void RewriteObjCFragileABI::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
| 5753 | std::string &Result) { |
| 5754 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
| 5755 | // Find category declaration for this implementation. |
| 5756 | ObjCCategoryDecl *CDecl; |
| 5757 | for (CDecl = ClassDecl->getCategoryList(); CDecl; |
| 5758 | CDecl = CDecl->getNextClassCategory()) |
| 5759 | if (CDecl->getIdentifier() == IDecl->getIdentifier()) |
| 5760 | break; |
| 5761 | |
| 5762 | std::string FullCategoryName = ClassDecl->getNameAsString(); |
| 5763 | FullCategoryName += '_'; |
| 5764 | FullCategoryName += IDecl->getNameAsString(); |
| 5765 | |
| 5766 | // Build _objc_method_list for class's instance methods if needed |
| 5767 | SmallVector<ObjCMethodDecl *, 32> |
| 5768 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
| 5769 | |
| 5770 | // If any of our property implementations have associated getters or |
| 5771 | // setters, produce metadata for them as well. |
| 5772 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 5773 | PropEnd = IDecl->propimpl_end(); |
| 5774 | Prop != PropEnd; ++Prop) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5775 | if (Prop->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5776 | continue; |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5777 | if (!Prop->getPropertyIvarDecl()) |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5778 | continue; |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5779 | ObjCPropertyDecl *PD = Prop->getPropertyDecl(); |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5780 | if (!PD) |
| 5781 | continue; |
| 5782 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 5783 | InstanceMethods.push_back(Getter); |
| 5784 | if (PD->isReadOnly()) |
| 5785 | continue; |
| 5786 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 5787 | InstanceMethods.push_back(Setter); |
| 5788 | } |
| 5789 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
| 5790 | true, "CATEGORY_", FullCategoryName.c_str(), |
| 5791 | Result); |
| 5792 | |
| 5793 | // Build _objc_method_list for class's class methods if needed |
| 5794 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
| 5795 | false, "CATEGORY_", FullCategoryName.c_str(), |
| 5796 | Result); |
| 5797 | |
| 5798 | // Protocols referenced in class declaration? |
| 5799 | // Null CDecl is case of a category implementation with no category interface |
| 5800 | if (CDecl) |
| 5801 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), "CATEGORY", |
| 5802 | FullCategoryName, Result); |
| 5803 | /* struct _objc_category { |
| 5804 | char *category_name; |
| 5805 | char *class_name; |
| 5806 | struct _objc_method_list *instance_methods; |
| 5807 | struct _objc_method_list *class_methods; |
| 5808 | struct _objc_protocol_list *protocols; |
| 5809 | // Objective-C 1.0 extensions |
| 5810 | uint32_t size; // sizeof (struct _objc_category) |
| 5811 | struct _objc_property_list *instance_properties; // category's own |
| 5812 | // @property decl. |
| 5813 | }; |
| 5814 | */ |
| 5815 | |
| 5816 | static bool objc_category = false; |
| 5817 | if (!objc_category) { |
| 5818 | Result += "\nstruct _objc_category {\n"; |
| 5819 | Result += "\tchar *category_name;\n"; |
| 5820 | Result += "\tchar *class_name;\n"; |
| 5821 | Result += "\tstruct _objc_method_list *instance_methods;\n"; |
| 5822 | Result += "\tstruct _objc_method_list *class_methods;\n"; |
| 5823 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 5824 | Result += "\tunsigned int size;\n"; |
| 5825 | Result += "\tstruct _objc_property_list *instance_properties;\n"; |
| 5826 | Result += "};\n"; |
| 5827 | objc_category = true; |
| 5828 | } |
| 5829 | Result += "\nstatic struct _objc_category _OBJC_CATEGORY_"; |
| 5830 | Result += FullCategoryName; |
| 5831 | Result += " __attribute__ ((used, section (\"__OBJC, __category\")))= {\n\t\""; |
| 5832 | Result += IDecl->getNameAsString(); |
| 5833 | Result += "\"\n\t, \""; |
| 5834 | Result += ClassDecl->getNameAsString(); |
| 5835 | Result += "\"\n"; |
| 5836 | |
| 5837 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
| 5838 | Result += "\t, (struct _objc_method_list *)" |
| 5839 | "&_OBJC_CATEGORY_INSTANCE_METHODS_"; |
| 5840 | Result += FullCategoryName; |
| 5841 | Result += "\n"; |
| 5842 | } |
| 5843 | else |
| 5844 | Result += "\t, 0\n"; |
| 5845 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
| 5846 | Result += "\t, (struct _objc_method_list *)" |
| 5847 | "&_OBJC_CATEGORY_CLASS_METHODS_"; |
| 5848 | Result += FullCategoryName; |
| 5849 | Result += "\n"; |
| 5850 | } |
| 5851 | else |
| 5852 | Result += "\t, 0\n"; |
| 5853 | |
| 5854 | if (CDecl && CDecl->protocol_begin() != CDecl->protocol_end()) { |
| 5855 | Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_"; |
| 5856 | Result += FullCategoryName; |
| 5857 | Result += "\n"; |
| 5858 | } |
| 5859 | else |
| 5860 | Result += "\t, 0\n"; |
| 5861 | Result += "\t, sizeof(struct _objc_category), 0\n};\n"; |
| 5862 | } |
| 5863 | |
| 5864 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
| 5865 | /// class methods. |
| 5866 | template<typename MethodIterator> |
| 5867 | void RewriteObjCFragileABI::RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 5868 | MethodIterator MethodEnd, |
| 5869 | bool IsInstanceMethod, |
| 5870 | StringRef prefix, |
| 5871 | StringRef ClassName, |
| 5872 | std::string &Result) { |
| 5873 | if (MethodBegin == MethodEnd) return; |
| 5874 | |
| 5875 | if (!objc_impl_method) { |
| 5876 | /* struct _objc_method { |
| 5877 | SEL _cmd; |
| 5878 | char *method_types; |
| 5879 | void *_imp; |
| 5880 | } |
| 5881 | */ |
| 5882 | Result += "\nstruct _objc_method {\n"; |
| 5883 | Result += "\tSEL _cmd;\n"; |
| 5884 | Result += "\tchar *method_types;\n"; |
| 5885 | Result += "\tvoid *_imp;\n"; |
| 5886 | Result += "};\n"; |
| 5887 | |
| 5888 | objc_impl_method = true; |
| 5889 | } |
| 5890 | |
| 5891 | // Build _objc_method_list for class's methods if needed |
| 5892 | |
| 5893 | /* struct { |
| 5894 | struct _objc_method_list *next_method; |
| 5895 | int method_count; |
| 5896 | struct _objc_method method_list[]; |
| 5897 | } |
| 5898 | */ |
| 5899 | unsigned NumMethods = std::distance(MethodBegin, MethodEnd); |
| 5900 | Result += "\nstatic struct {\n"; |
| 5901 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 5902 | Result += "\tint method_count;\n"; |
| 5903 | Result += "\tstruct _objc_method method_list["; |
| 5904 | Result += utostr(NumMethods); |
| 5905 | Result += "];\n} _OBJC_"; |
| 5906 | Result += prefix; |
| 5907 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 5908 | Result += "_METHODS_"; |
| 5909 | Result += ClassName; |
| 5910 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
| 5911 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 5912 | Result += "_meth\")))= "; |
| 5913 | Result += "{\n\t0, " + utostr(NumMethods) + "\n"; |
| 5914 | |
| 5915 | Result += "\t,{{(SEL)\""; |
| 5916 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
| 5917 | std::string MethodTypeString; |
| 5918 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
| 5919 | Result += "\", \""; |
| 5920 | Result += MethodTypeString; |
| 5921 | Result += "\", (void *)"; |
| 5922 | Result += MethodInternalNames[*MethodBegin]; |
| 5923 | Result += "}\n"; |
| 5924 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 5925 | Result += "\t ,{(SEL)\""; |
| 5926 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
| 5927 | std::string MethodTypeString; |
| 5928 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
| 5929 | Result += "\", \""; |
| 5930 | Result += MethodTypeString; |
| 5931 | Result += "\", (void *)"; |
| 5932 | Result += MethodInternalNames[*MethodBegin]; |
| 5933 | Result += "}\n"; |
| 5934 | } |
| 5935 | Result += "\t }\n};\n"; |
| 5936 | } |
| 5937 | |
| 5938 | Stmt *RewriteObjCFragileABI::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV) { |
| 5939 | SourceRange OldRange = IV->getSourceRange(); |
| 5940 | Expr *BaseExpr = IV->getBase(); |
| 5941 | |
| 5942 | // Rewrite the base, but without actually doing replaces. |
| 5943 | { |
| 5944 | DisableReplaceStmtScope S(*this); |
| 5945 | BaseExpr = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(BaseExpr)); |
| 5946 | IV->setBase(BaseExpr); |
| 5947 | } |
| 5948 | |
| 5949 | ObjCIvarDecl *D = IV->getDecl(); |
| 5950 | |
| 5951 | Expr *Replacement = IV; |
| 5952 | if (CurMethodDef) { |
| 5953 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
| 5954 | const ObjCInterfaceType *iFaceDecl = |
| 5955 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
| 5956 | assert(iFaceDecl && "RewriteObjCIvarRefExpr - iFaceDecl is null"); |
| 5957 | // lookup which class implements the instance variable. |
| 5958 | ObjCInterfaceDecl *clsDeclared = 0; |
| 5959 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
| 5960 | clsDeclared); |
| 5961 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
| 5962 | |
| 5963 | // Synthesize an explicit cast to gain access to the ivar. |
| 5964 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 5965 | RecName += "_IMPL"; |
| 5966 | IdentifierInfo *II = &Context->Idents.get(RecName); |
| 5967 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 5968 | SourceLocation(), SourceLocation(), |
| 5969 | II); |
| 5970 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 5971 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 5972 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, |
| 5973 | CK_BitCast, |
| 5974 | IV->getBase()); |
| 5975 | // Don't forget the parens to enforce the proper binding. |
| 5976 | ParenExpr *PE = new (Context) ParenExpr(OldRange.getBegin(), |
| 5977 | OldRange.getEnd(), |
| 5978 | castExpr); |
| 5979 | if (IV->isFreeIvar() && |
Douglas Gregor | 60ef308 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 5980 | declaresSameEntity(CurMethodDef->getClassInterface(), iFaceDecl->getDecl())) { |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5981 | MemberExpr *ME = new (Context) MemberExpr(PE, true, D, |
| 5982 | IV->getLocation(), |
| 5983 | D->getType(), |
| 5984 | VK_LValue, OK_Ordinary); |
| 5985 | Replacement = ME; |
| 5986 | } else { |
| 5987 | IV->setBase(PE); |
| 5988 | } |
| 5989 | } |
| 5990 | } else { // we are outside a method. |
| 5991 | assert(!IV->isFreeIvar() && "Cannot have a free standing ivar outside a method"); |
| 5992 | |
| 5993 | // Explicit ivar refs need to have a cast inserted. |
| 5994 | // FIXME: consider sharing some of this code with the code above. |
| 5995 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
| 5996 | const ObjCInterfaceType *iFaceDecl = |
| 5997 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
| 5998 | // lookup which class implements the instance variable. |
| 5999 | ObjCInterfaceDecl *clsDeclared = 0; |
| 6000 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
| 6001 | clsDeclared); |
| 6002 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
| 6003 | |
| 6004 | // Synthesize an explicit cast to gain access to the ivar. |
| 6005 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 6006 | RecName += "_IMPL"; |
| 6007 | IdentifierInfo *II = &Context->Idents.get(RecName); |
| 6008 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 6009 | SourceLocation(), SourceLocation(), |
| 6010 | II); |
| 6011 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 6012 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 6013 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, |
| 6014 | CK_BitCast, |
| 6015 | IV->getBase()); |
| 6016 | // Don't forget the parens to enforce the proper binding. |
| 6017 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
| 6018 | IV->getBase()->getLocEnd(), castExpr); |
| 6019 | // Cannot delete IV->getBase(), since PE points to it. |
| 6020 | // Replace the old base with the cast. This is important when doing |
| 6021 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
| 6022 | IV->setBase(PE); |
| 6023 | } |
| 6024 | } |
| 6025 | |
| 6026 | ReplaceStmtWithRange(IV, Replacement, OldRange); |
| 6027 | return Replacement; |
| 6028 | } |