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" |
Benjamin Kramer | 2fa67ef | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 18 | #include "clang/AST/Attr.h" |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 19 | #include "clang/AST/ParentMap.h" |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 20 | #include "clang/Basic/Diagnostic.h" |
Benjamin Kramer | 2fa67ef | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 21 | #include "clang/Basic/IdentifierTable.h" |
| 22 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 26de465 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 23 | #include "clang/Lex/Lexer.h" |
Benjamin Kramer | 2fa67ef | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/DenseSet.h" |
| 25 | #include "llvm/ADT/OwningPtr.h" |
| 26 | #include "llvm/ADT/SmallPtrSet.h" |
| 27 | #include "llvm/ADT/StringExtras.h" |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 28 | #include "llvm/Support/MemoryBuffer.h" |
| 29 | #include "llvm/Support/raw_ostream.h" |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 31 | using namespace clang; |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 32 | using llvm::utostr; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 34 | namespace { |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 35 | class RewriteObjC : public ASTConsumer { |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 36 | protected: |
| 37 | |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 38 | enum { |
Nico Weber | 59b173d | 2010-11-22 10:26:41 +0000 | [diff] [blame] | 39 | BLOCK_FIELD_IS_OBJECT = 3, /* id, NSObject, __attribute__((NSObject)), |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 40 | block, ... */ |
| 41 | BLOCK_FIELD_IS_BLOCK = 7, /* a block variable */ |
| 42 | BLOCK_FIELD_IS_BYREF = 8, /* the on stack structure holding the |
| 43 | __block variable */ |
Nico Weber | 59b173d | 2010-11-22 10:26:41 +0000 | [diff] [blame] | 44 | BLOCK_FIELD_IS_WEAK = 16, /* declared __weak, only used in byref copy |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 45 | helpers */ |
Nico Weber | 59b173d | 2010-11-22 10:26:41 +0000 | [diff] [blame] | 46 | BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 47 | support routines */ |
| 48 | BLOCK_BYREF_CURRENT_MAX = 256 |
| 49 | }; |
| 50 | |
| 51 | enum { |
| 52 | BLOCK_NEEDS_FREE = (1 << 24), |
| 53 | BLOCK_HAS_COPY_DISPOSE = (1 << 25), |
| 54 | BLOCK_HAS_CXX_OBJ = (1 << 26), |
| 55 | BLOCK_IS_GC = (1 << 27), |
| 56 | BLOCK_IS_GLOBAL = (1 << 28), |
| 57 | BLOCK_HAS_DESCRIPTOR = (1 << 29) |
| 58 | }; |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 59 | static const int OBJC_ABI_VERSION = 7; |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 61 | Rewriter Rewrite; |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 62 | DiagnosticsEngine &Diags; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 63 | const LangOptions &LangOpts; |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 64 | ASTContext *Context; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 65 | SourceManager *SM; |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 66 | TranslationUnitDecl *TUDecl; |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 67 | FileID MainFileID; |
Chris Lattner | 26de465 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 68 | const char *MainFileStart, *MainFileEnd; |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 69 | Stmt *CurrentBody; |
| 70 | ParentMap *PropParentMap; // created lazily. |
Fariborz Jahanian | dd8079c | 2011-12-05 19:50:04 +0000 | [diff] [blame] | 71 | std::string InFileName; |
| 72 | raw_ostream* OutFile; |
| 73 | std::string Preamble; |
| 74 | |
| 75 | TypeDecl *ProtocolTypeDecl; |
| 76 | VarDecl *GlobalVarDecl; |
| 77 | unsigned RewriteFailedDiag; |
Fariborz Jahanian | dd8079c | 2011-12-05 19:50:04 +0000 | [diff] [blame] | 78 | // ObjC string constant support. |
| 79 | unsigned NumObjCStringLiterals; |
| 80 | VarDecl *ConstantStringClassReference; |
| 81 | RecordDecl *NSStringRecord; |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 82 | |
Fariborz Jahanian | dd8079c | 2011-12-05 19:50:04 +0000 | [diff] [blame] | 83 | // ObjC foreach break/continue generation support. |
| 84 | int BcLabelCount; |
| 85 | |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 86 | unsigned TryFinallyContainsReturnDiag; |
Fariborz Jahanian | dd8079c | 2011-12-05 19:50:04 +0000 | [diff] [blame] | 87 | // Needed for super. |
| 88 | ObjCMethodDecl *CurMethodDef; |
| 89 | RecordDecl *SuperStructDecl; |
| 90 | RecordDecl *ConstantStringDecl; |
| 91 | |
| 92 | FunctionDecl *MsgSendFunctionDecl; |
| 93 | FunctionDecl *MsgSendSuperFunctionDecl; |
| 94 | FunctionDecl *MsgSendStretFunctionDecl; |
| 95 | FunctionDecl *MsgSendSuperStretFunctionDecl; |
| 96 | FunctionDecl *MsgSendFpretFunctionDecl; |
| 97 | FunctionDecl *GetClassFunctionDecl; |
| 98 | FunctionDecl *GetMetaClassFunctionDecl; |
| 99 | FunctionDecl *GetSuperClassFunctionDecl; |
| 100 | FunctionDecl *SelGetUidFunctionDecl; |
| 101 | FunctionDecl *CFStringFunctionDecl; |
| 102 | FunctionDecl *SuperContructorFunctionDecl; |
| 103 | FunctionDecl *CurFunctionDef; |
| 104 | FunctionDecl *CurFunctionDeclToDeclareForBlock; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 | |
Fariborz Jahanian | dd8079c | 2011-12-05 19:50:04 +0000 | [diff] [blame] | 106 | /* Misc. containers needed for meta-data rewrite. */ |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 107 | SmallVector<ObjCImplementationDecl *, 8> ClassImplementation; |
| 108 | SmallVector<ObjCCategoryImplDecl *, 8> CategoryImplementation; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 109 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCSynthesizedStructs; |
Steve Naroff | fbfe825 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 110 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> ObjCSynthesizedProtocols; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 111 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCForwardDecls; |
| 112 | llvm::DenseMap<ObjCMethodDecl*, std::string> MethodInternalNames; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 113 | SmallVector<Stmt *, 32> Stmts; |
| 114 | SmallVector<int, 8> ObjCBcLabelNo; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 115 | // Remember all the @protocol(<expr>) expressions. |
| 116 | llvm::SmallPtrSet<ObjCProtocolDecl *, 32> ProtocolExprDecls; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 117 | |
| 118 | llvm::DenseSet<uint64_t> CopyDestroyCache; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 119 | |
| 120 | // Block expressions. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 121 | SmallVector<BlockExpr *, 32> Blocks; |
| 122 | SmallVector<int, 32> InnerDeclRefsCount; |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 123 | SmallVector<DeclRefExpr *, 32> InnerDeclRefs; |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 124 | |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 125 | SmallVector<DeclRefExpr *, 32> BlockDeclRefs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 126 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 127 | // Block related declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 128 | SmallVector<ValueDecl *, 8> BlockByCopyDecls; |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 129 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByCopyDeclsPtrSet; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 130 | SmallVector<ValueDecl *, 8> BlockByRefDecls; |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 131 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByRefDeclsPtrSet; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 132 | llvm::DenseMap<ValueDecl *, unsigned> BlockByRefDeclNo; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 133 | llvm::SmallPtrSet<ValueDecl *, 8> ImportedBlockDecls; |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 134 | llvm::SmallPtrSet<VarDecl *, 8> ImportedLocalExternalDecls; |
| 135 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 136 | llvm::DenseMap<BlockExpr *, std::string> RewrittenBlockExprs; |
| 137 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 138 | // This maps an original source AST to it's rewritten form. This allows |
| 139 | // us to avoid rewriting the same node twice (which is very uncommon). |
| 140 | // This is needed to support some of the exotic property rewriting. |
| 141 | llvm::DenseMap<Stmt *, Stmt *> ReplacedNodes; |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 142 | |
Fariborz Jahanian | dd8079c | 2011-12-05 19:50:04 +0000 | [diff] [blame] | 143 | // Needed for header files being rewritten |
| 144 | bool IsHeader; |
| 145 | bool SilenceRewriteMacroWarning; |
| 146 | bool objc_impl_method; |
| 147 | |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 148 | bool DisableReplaceStmt; |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 149 | class DisableReplaceStmtScope { |
| 150 | RewriteObjC &R; |
| 151 | bool SavedValue; |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 152 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 153 | public: |
| 154 | DisableReplaceStmtScope(RewriteObjC &R) |
| 155 | : R(R), SavedValue(R.DisableReplaceStmt) { |
| 156 | R.DisableReplaceStmt = true; |
| 157 | } |
| 158 | ~DisableReplaceStmtScope() { |
| 159 | R.DisableReplaceStmt = SavedValue; |
| 160 | } |
| 161 | }; |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 162 | void InitializeCommon(ASTContext &context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 163 | |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 164 | public: |
Ted Kremenek | e3a6198 | 2008-05-31 20:11:04 +0000 | [diff] [blame] | 165 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 166 | // Top Level Driver code. |
Argyrios Kyrtzidis | 88c2596 | 2011-11-18 00:26:59 +0000 | [diff] [blame] | 167 | virtual bool HandleTopLevelDecl(DeclGroupRef D) { |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 168 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 169 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(*I)) { |
| 170 | if (!Class->isThisDeclarationADefinition()) { |
| 171 | RewriteForwardClassDecl(D); |
| 172 | break; |
| 173 | } |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 174 | } |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 175 | |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 176 | if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(*I)) { |
| 177 | if (!Proto->isThisDeclarationADefinition()) { |
| 178 | RewriteForwardProtocolDecl(D); |
| 179 | break; |
| 180 | } |
| 181 | } |
| 182 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 183 | HandleTopLevelSingleDecl(*I); |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 184 | } |
Argyrios Kyrtzidis | 88c2596 | 2011-11-18 00:26:59 +0000 | [diff] [blame] | 185 | return true; |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 186 | } |
| 187 | void HandleTopLevelSingleDecl(Decl *D); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 188 | void HandleDeclInMainFile(Decl *D); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 189 | RewriteObjC(std::string inFile, raw_ostream *OS, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 190 | DiagnosticsEngine &D, const LangOptions &LOpts, |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 191 | bool silenceMacroWarn); |
Ted Kremenek | e452e0f | 2008-08-08 04:15:52 +0000 | [diff] [blame] | 192 | |
| 193 | ~RewriteObjC() {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 194 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 195 | virtual void HandleTranslationUnit(ASTContext &C); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 196 | |
Fariborz Jahanian | 88906cd | 2010-02-05 16:43:40 +0000 | [diff] [blame] | 197 | void ReplaceStmt(Stmt *Old, Stmt *New) { |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 198 | Stmt *ReplacingStmt = ReplacedNodes[Old]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 199 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 200 | if (ReplacingStmt) |
| 201 | return; // We can't rewrite the same node twice. |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 202 | |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 203 | if (DisableReplaceStmt) |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 204 | return; |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 205 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 206 | // If replacement succeeded or warning disabled return with no warning. |
Fariborz Jahanian | 88906cd | 2010-02-05 16:43:40 +0000 | [diff] [blame] | 207 | if (!Rewrite.ReplaceStmt(Old, New)) { |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 208 | ReplacedNodes[Old] = New; |
| 209 | return; |
| 210 | } |
| 211 | if (SilenceRewriteMacroWarning) |
| 212 | return; |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 213 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 214 | << Old->getSourceRange(); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 215 | } |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 216 | |
| 217 | void ReplaceStmtWithRange(Stmt *Old, Stmt *New, SourceRange SrcRange) { |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 218 | if (DisableReplaceStmt) |
| 219 | return; |
| 220 | |
Nick Lewycky | 7e74924 | 2010-10-31 21:07:24 +0000 | [diff] [blame] | 221 | // Measure the old text. |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 222 | int Size = Rewrite.getRangeSize(SrcRange); |
| 223 | if (Size == -1) { |
| 224 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 225 | << Old->getSourceRange(); |
| 226 | return; |
| 227 | } |
| 228 | // Get the new text. |
| 229 | std::string SStr; |
| 230 | llvm::raw_string_ostream S(SStr); |
Richard Smith | d1420c6 | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 231 | New->printPretty(S, 0, PrintingPolicy(LangOpts)); |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 232 | const std::string &Str = S.str(); |
| 233 | |
| 234 | // If replacement succeeded or warning disabled return with no warning. |
Daniel Dunbar | d7407dc | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 235 | if (!Rewrite.ReplaceText(SrcRange.getBegin(), Size, Str)) { |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 236 | ReplacedNodes[Old] = New; |
| 237 | return; |
| 238 | } |
| 239 | if (SilenceRewriteMacroWarning) |
| 240 | return; |
| 241 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 242 | << Old->getSourceRange(); |
| 243 | } |
| 244 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 245 | void InsertText(SourceLocation Loc, StringRef Str, |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 246 | bool InsertAfter = true) { |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 247 | // If insertion succeeded or warning disabled return with no warning. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 248 | if (!Rewrite.InsertText(Loc, Str, InsertAfter) || |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 249 | SilenceRewriteMacroWarning) |
| 250 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 251 | |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 252 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 253 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 254 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 255 | void ReplaceText(SourceLocation Start, unsigned OrigLength, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 256 | StringRef Str) { |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 257 | // If removal succeeded or warning disabled return with no warning. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 258 | if (!Rewrite.ReplaceText(Start, OrigLength, Str) || |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 259 | SilenceRewriteMacroWarning) |
| 260 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 261 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 262 | Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag); |
| 263 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 264 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 265 | // Syntactic Rewriting. |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 266 | void RewriteRecordBody(RecordDecl *RD); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 267 | void RewriteInclude(); |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 268 | void RewriteForwardClassDecl(DeclGroupRef D); |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 269 | void RewriteForwardClassDecl(const llvm::SmallVector<Decl*, 8> &DG); |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 270 | void RewriteForwardClassEpilogue(ObjCInterfaceDecl *ClassDecl, |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 271 | const std::string &typedefString); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 272 | void RewriteImplementations(); |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 273 | void RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 274 | ObjCImplementationDecl *IMD, |
| 275 | ObjCCategoryImplDecl *CID); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 276 | void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 277 | void RewriteImplementationDecl(Decl *Dcl); |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 278 | void RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl, |
| 279 | ObjCMethodDecl *MDecl, std::string &ResultStr); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 280 | void RewriteTypeIntoString(QualType T, std::string &ResultStr, |
| 281 | const FunctionType *&FPRetType); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 282 | void RewriteByRefString(std::string &ResultStr, const std::string &Name, |
Fariborz Jahanian | 1e8011e | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 283 | ValueDecl *VD, bool def=false); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 284 | void RewriteCategoryDecl(ObjCCategoryDecl *Dcl); |
| 285 | void RewriteProtocolDecl(ObjCProtocolDecl *Dcl); |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 286 | void RewriteForwardProtocolDecl(DeclGroupRef D); |
| 287 | void RewriteForwardProtocolDecl(const llvm::SmallVector<Decl*, 8> &DG); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 288 | void RewriteMethodDeclaration(ObjCMethodDecl *Method); |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 289 | void RewriteProperty(ObjCPropertyDecl *prop); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 290 | void RewriteFunctionDecl(FunctionDecl *FD); |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 291 | void RewriteBlockPointerType(std::string& Str, QualType Type); |
| 292 | void RewriteBlockPointerTypeVariable(std::string& Str, ValueDecl *VD); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 293 | void RewriteBlockLiteralFunctionDecl(FunctionDecl *FD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 294 | void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 295 | void RewriteTypeOfDecl(VarDecl *VD); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 296 | void RewriteObjCQualifiedInterfaceTypes(Expr *E); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 297 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 298 | // Expression Rewriting. |
Steve Naroff | f3473a7 | 2007-11-09 15:20:18 +0000 | [diff] [blame] | 299 | Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S); |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 300 | Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 301 | Stmt *RewritePropertyOrImplicitGetter(PseudoObjectExpr *Pseudo); |
| 302 | Stmt *RewritePropertyOrImplicitSetter(PseudoObjectExpr *Pseudo); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 303 | Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp); |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 304 | Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp); |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 305 | Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp); |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 306 | Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 307 | void RewriteTryReturnStmts(Stmt *S); |
| 308 | void RewriteSyncReturnStmts(Stmt *S, std::string buf); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 309 | Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 310 | Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 311 | Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S); |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 312 | Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
| 313 | SourceLocation OrigEnd); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 314 | Stmt *RewriteBreakStmt(BreakStmt *S); |
| 315 | Stmt *RewriteContinueStmt(ContinueStmt *S); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 316 | void RewriteCastExpr(CStyleCastExpr *CE); |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 317 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 318 | // Block rewriting. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 319 | void RewriteBlocksInFunctionProtoType(QualType funcType, NamedDecl *D); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 320 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 321 | // Block specific rewrite rules. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 322 | void RewriteBlockPointerDecl(NamedDecl *VD); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 323 | void RewriteByRefVar(VarDecl *VD); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 324 | Stmt *RewriteBlockDeclRefExpr(DeclRefExpr *VD); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 325 | Stmt *RewriteLocalVariableExternalStorage(DeclRefExpr *DRE); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 326 | void RewriteBlockPointerFunctionArgs(FunctionDecl *FD); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 327 | |
| 328 | void RewriteObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
| 329 | std::string &Result); |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 330 | |
| 331 | virtual void Initialize(ASTContext &context) = 0; |
| 332 | |
| 333 | // Metadata Rewriting. |
| 334 | virtual void RewriteMetaDataIntoBuffer(std::string &Result) = 0; |
| 335 | virtual void RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Prots, |
| 336 | StringRef prefix, |
| 337 | StringRef ClassName, |
| 338 | std::string &Result) = 0; |
| 339 | virtual void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
| 340 | std::string &Result) = 0; |
| 341 | virtual void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol, |
| 342 | StringRef prefix, |
| 343 | StringRef ClassName, |
| 344 | std::string &Result) = 0; |
| 345 | virtual void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
| 346 | std::string &Result) = 0; |
| 347 | |
| 348 | // Rewriting ivar access |
| 349 | virtual Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV) = 0; |
| 350 | virtual void RewriteIvarOffsetComputation(ObjCIvarDecl *ivar, |
| 351 | std::string &Result) = 0; |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 352 | |
Benjamin Kramer | 48d798c | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 353 | // Misc. AST transformation routines. Sometimes they end up calling |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 354 | // rewriting routines on the new ASTs. |
| 355 | CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD, |
| 356 | Expr **args, unsigned nargs, |
| 357 | SourceLocation StartLoc=SourceLocation(), |
| 358 | SourceLocation EndLoc=SourceLocation()); |
Fariborz Jahanian | 9fc5b00 | 2012-06-28 21:20:35 +0000 | [diff] [blame] | 359 | CallExpr *SynthMsgSendStretCallExpr(FunctionDecl *MsgSendStretFlavor, |
| 360 | QualType msgSendType, |
| 361 | QualType returnType, |
| 362 | SmallVectorImpl<QualType> &ArgTypes, |
| 363 | SmallVectorImpl<Expr*> &MsgExprs, |
| 364 | ObjCMethodDecl *Method); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 365 | Stmt *SynthMessageExpr(ObjCMessageExpr *Exp, |
| 366 | SourceLocation StartLoc=SourceLocation(), |
| 367 | SourceLocation EndLoc=SourceLocation()); |
| 368 | |
| 369 | void SynthCountByEnumWithState(std::string &buf); |
| 370 | void SynthMsgSendFunctionDecl(); |
| 371 | void SynthMsgSendSuperFunctionDecl(); |
| 372 | void SynthMsgSendStretFunctionDecl(); |
| 373 | void SynthMsgSendFpretFunctionDecl(); |
| 374 | void SynthMsgSendSuperStretFunctionDecl(); |
| 375 | void SynthGetClassFunctionDecl(); |
| 376 | void SynthGetMetaClassFunctionDecl(); |
| 377 | void SynthGetSuperClassFunctionDecl(); |
| 378 | void SynthSelGetUidFunctionDecl(); |
| 379 | void SynthSuperContructorFunctionDecl(); |
| 380 | |
| 381 | std::string SynthesizeByrefCopyDestroyHelper(VarDecl *VD, int flag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 382 | std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 383 | StringRef funcName, std::string Tag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 384 | std::string SynthesizeBlockFunc(BlockExpr *CE, int i, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 385 | StringRef funcName, std::string Tag); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 386 | std::string SynthesizeBlockImpl(BlockExpr *CE, |
| 387 | std::string Tag, std::string Desc); |
| 388 | std::string SynthesizeBlockDescriptor(std::string DescTag, |
| 389 | std::string ImplTag, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 390 | int i, StringRef funcName, |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 391 | unsigned hasCopy); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 392 | Stmt *SynthesizeBlockCall(CallExpr *Exp, const Expr* BlockExp); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 393 | void SynthesizeBlockLiterals(SourceLocation FunLocStart, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 394 | StringRef FunName); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 395 | FunctionDecl *SynthBlockInitFunctionDecl(StringRef name); |
| 396 | Stmt *SynthBlockInitExpr(BlockExpr *Exp, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 397 | const SmallVector<DeclRefExpr *, 8> &InnerBlockDeclRefs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 398 | |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 399 | // Misc. helper routines. |
Fariborz Jahanian | dd8079c | 2011-12-05 19:50:04 +0000 | [diff] [blame] | 400 | QualType getProtocolType(); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 401 | void WarnAboutReturnGotoStmts(Stmt *S); |
| 402 | void HasReturnStmts(Stmt *S, bool &hasReturns); |
| 403 | void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND); |
| 404 | void InsertBlockLiteralsWithinFunction(FunctionDecl *FD); |
| 405 | void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD); |
| 406 | |
| 407 | bool IsDeclStmtInForeachHeader(DeclStmt *DS); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 408 | void CollectBlockDeclRefInfo(BlockExpr *Exp); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 409 | void GetBlockDeclRefExprs(Stmt *S); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 410 | void GetInnerBlockDeclRefExprs(Stmt *S, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 411 | SmallVector<DeclRefExpr *, 8> &InnerBlockDeclRefs, |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 412 | llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 413 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 414 | // We avoid calling Type::isBlockPointerType(), since it operates on the |
| 415 | // 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] | 416 | bool isTopLevelBlockPointerType(QualType T) { |
| 417 | return isa<BlockPointerType>(T); |
| 418 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 419 | |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 420 | /// convertBlockPointerToFunctionPointer - Converts a block-pointer type |
| 421 | /// to a function pointer type and upon success, returns true; false |
| 422 | /// otherwise. |
| 423 | bool convertBlockPointerToFunctionPointer(QualType &T) { |
| 424 | if (isTopLevelBlockPointerType(T)) { |
| 425 | const BlockPointerType *BPT = T->getAs<BlockPointerType>(); |
| 426 | T = Context->getPointerType(BPT->getPointeeType()); |
| 427 | return true; |
| 428 | } |
| 429 | return false; |
| 430 | } |
| 431 | |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 432 | bool needToScanForQualifiers(QualType T); |
| 433 | QualType getSuperStructType(); |
| 434 | QualType getConstantStringStructType(); |
| 435 | QualType convertFunctionTypeOfBlocks(const FunctionType *FT); |
| 436 | bool BufferContainsPPDirectives(const char *startBuf, const char *endBuf); |
| 437 | |
Fariborz Jahanian | 8188e5f | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 438 | void convertToUnqualifiedObjCType(QualType &T) { |
| 439 | if (T->isObjCQualifiedIdType()) |
| 440 | T = Context->getObjCIdType(); |
| 441 | else if (T->isObjCQualifiedClassType()) |
| 442 | T = Context->getObjCClassType(); |
| 443 | else if (T->isObjCObjectPointerType() && |
Fariborz Jahanian | 3a448fb | 2011-09-10 17:01:56 +0000 | [diff] [blame] | 444 | T->getPointeeType()->isObjCQualifiedInterfaceType()) { |
| 445 | if (const ObjCObjectPointerType * OBJPT = |
| 446 | T->getAsObjCInterfacePointerType()) { |
| 447 | const ObjCInterfaceType *IFaceT = OBJPT->getInterfaceType(); |
| 448 | T = QualType(IFaceT, 0); |
| 449 | T = Context->getPointerType(T); |
| 450 | } |
| 451 | } |
Fariborz Jahanian | 8188e5f | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 454 | // FIXME: This predicate seems like it would be useful to add to ASTContext. |
| 455 | bool isObjCType(QualType T) { |
| 456 | if (!LangOpts.ObjC1 && !LangOpts.ObjC2) |
| 457 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 458 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 459 | QualType OCT = Context->getCanonicalType(T).getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 460 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 461 | if (OCT == Context->getCanonicalType(Context->getObjCIdType()) || |
| 462 | OCT == Context->getCanonicalType(Context->getObjCClassType())) |
| 463 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 464 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 465 | if (const PointerType *PT = OCT->getAs<PointerType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 466 | if (isa<ObjCInterfaceType>(PT->getPointeeType()) || |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 467 | PT->getPointeeType()->isObjCQualifiedIdType()) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 468 | return true; |
| 469 | } |
| 470 | return false; |
| 471 | } |
| 472 | bool PointerTypeTakesAnyBlockArguments(QualType QT); |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 473 | bool PointerTypeTakesAnyObjCQualifiedType(QualType QT); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 474 | void GetExtentOfArgList(const char *Name, const char *&LParen, |
| 475 | const char *&RParen); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 476 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 477 | void QuoteDoublequotes(std::string &From, std::string &To) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 478 | for (unsigned i = 0; i < From.length(); i++) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 479 | if (From[i] == '"') |
| 480 | To += "\\\""; |
| 481 | else |
| 482 | To += From[i]; |
| 483 | } |
| 484 | } |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 485 | |
| 486 | QualType getSimpleFunctionType(QualType result, |
| 487 | const QualType *args, |
| 488 | unsigned numArgs, |
| 489 | bool variadic = false) { |
Fariborz Jahanian | 8891480 | 2011-09-09 20:35:22 +0000 | [diff] [blame] | 490 | if (result == Context->getObjCInstanceType()) |
| 491 | result = Context->getObjCIdType(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 492 | FunctionProtoType::ExtProtoInfo fpi; |
| 493 | fpi.Variadic = variadic; |
| 494 | return Context->getFunctionType(result, args, numArgs, fpi); |
| 495 | } |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 496 | |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 497 | // Helper function: create a CStyleCastExpr with trivial type source info. |
| 498 | CStyleCastExpr* NoTypeInfoCStyleCastExpr(ASTContext *Ctx, QualType Ty, |
| 499 | CastKind Kind, Expr *E) { |
| 500 | TypeSourceInfo *TInfo = Ctx->getTrivialTypeSourceInfo(Ty, SourceLocation()); |
| 501 | return CStyleCastExpr::Create(*Ctx, Ty, VK_RValue, Kind, E, 0, TInfo, |
| 502 | SourceLocation(), SourceLocation()); |
| 503 | } |
| 504 | }; |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 505 | |
| 506 | class RewriteObjCFragileABI : public RewriteObjC { |
| 507 | public: |
| 508 | |
| 509 | RewriteObjCFragileABI(std::string inFile, raw_ostream *OS, |
| 510 | DiagnosticsEngine &D, const LangOptions &LOpts, |
| 511 | bool silenceMacroWarn) : RewriteObjC(inFile, OS, |
| 512 | D, LOpts, |
| 513 | silenceMacroWarn) {} |
| 514 | |
| 515 | ~RewriteObjCFragileABI() {} |
| 516 | virtual void Initialize(ASTContext &context); |
| 517 | |
| 518 | // Rewriting metadata |
| 519 | template<typename MethodIterator> |
| 520 | void RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 521 | MethodIterator MethodEnd, |
| 522 | bool IsInstanceMethod, |
| 523 | StringRef prefix, |
| 524 | StringRef ClassName, |
| 525 | std::string &Result); |
| 526 | virtual void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol, |
| 527 | StringRef prefix, |
| 528 | StringRef ClassName, |
| 529 | std::string &Result); |
| 530 | virtual void RewriteObjCProtocolListMetaData( |
| 531 | const ObjCList<ObjCProtocolDecl> &Prots, |
| 532 | StringRef prefix, StringRef ClassName, std::string &Result); |
| 533 | virtual void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
| 534 | std::string &Result); |
| 535 | virtual void RewriteMetaDataIntoBuffer(std::string &Result); |
| 536 | virtual void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
| 537 | std::string &Result); |
| 538 | |
| 539 | // Rewriting ivar |
| 540 | virtual void RewriteIvarOffsetComputation(ObjCIvarDecl *ivar, |
| 541 | std::string &Result); |
| 542 | virtual Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV); |
| 543 | }; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 546 | void RewriteObjC::RewriteBlocksInFunctionProtoType(QualType funcType, |
| 547 | NamedDecl *D) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 548 | if (const FunctionProtoType *fproto |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 549 | = dyn_cast<FunctionProtoType>(funcType.IgnoreParens())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 550 | for (FunctionProtoType::arg_type_iterator I = fproto->arg_type_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 551 | E = fproto->arg_type_end(); I && (I != E); ++I) |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 552 | if (isTopLevelBlockPointerType(*I)) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 553 | // All the args are checked/rewritten. Don't call twice! |
| 554 | RewriteBlockPointerDecl(D); |
| 555 | break; |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | void RewriteObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 561 | const PointerType *PT = funcType->getAs<PointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 562 | if (PT && PointerTypeTakesAnyBlockArguments(funcType)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 563 | RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 566 | static bool IsHeaderFile(const std::string &Filename) { |
| 567 | std::string::size_type DotPos = Filename.rfind('.'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 568 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 569 | if (DotPos == std::string::npos) { |
| 570 | // no file extension |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 571 | return false; |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 572 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 573 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 574 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 575 | // C header: .h |
| 576 | // C++ header: .hh or .H; |
| 577 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 578 | } |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 579 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 580 | RewriteObjC::RewriteObjC(std::string inFile, raw_ostream* OS, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 581 | DiagnosticsEngine &D, const LangOptions &LOpts, |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 582 | bool silenceMacroWarn) |
| 583 | : Diags(D), LangOpts(LOpts), InFileName(inFile), OutFile(OS), |
| 584 | SilenceRewriteMacroWarning(silenceMacroWarn) { |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 585 | IsHeader = IsHeaderFile(inFile); |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 586 | RewriteFailedDiag = Diags.getCustomDiagID(DiagnosticsEngine::Warning, |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 587 | "rewriting sub-expression within a macro (may not be correct)"); |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 588 | TryFinallyContainsReturnDiag = Diags.getCustomDiagID( |
| 589 | DiagnosticsEngine::Warning, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 590 | "rewriter doesn't support user-specified control flow semantics " |
| 591 | "for @try/@finally (code may not execute properly)"); |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Eli Friedman | bce831b | 2009-05-18 22:29:17 +0000 | [diff] [blame] | 594 | ASTConsumer *clang::CreateObjCRewriter(const std::string& InFile, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 595 | raw_ostream* OS, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 596 | DiagnosticsEngine &Diags, |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 597 | const LangOptions &LOpts, |
| 598 | bool SilenceRewriteMacroWarning) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 599 | return new RewriteObjCFragileABI(InFile, OS, Diags, LOpts, SilenceRewriteMacroWarning); |
Chris Lattner | e365c50 | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 600 | } |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 601 | |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 602 | void RewriteObjC::InitializeCommon(ASTContext &context) { |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 603 | Context = &context; |
| 604 | SM = &Context->getSourceManager(); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 605 | TUDecl = Context->getTranslationUnitDecl(); |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 606 | MsgSendFunctionDecl = 0; |
| 607 | MsgSendSuperFunctionDecl = 0; |
| 608 | MsgSendStretFunctionDecl = 0; |
| 609 | MsgSendSuperStretFunctionDecl = 0; |
| 610 | MsgSendFpretFunctionDecl = 0; |
| 611 | GetClassFunctionDecl = 0; |
| 612 | GetMetaClassFunctionDecl = 0; |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 613 | GetSuperClassFunctionDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 614 | SelGetUidFunctionDecl = 0; |
| 615 | CFStringFunctionDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 616 | ConstantStringClassReference = 0; |
| 617 | NSStringRecord = 0; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 618 | CurMethodDef = 0; |
| 619 | CurFunctionDef = 0; |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 620 | CurFunctionDeclToDeclareForBlock = 0; |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 621 | GlobalVarDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 622 | SuperStructDecl = 0; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 623 | ProtocolTypeDecl = 0; |
Steve Naroff | 9630ec5 | 2008-03-27 22:59:54 +0000 | [diff] [blame] | 624 | ConstantStringDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 625 | BcLabelCount = 0; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 626 | SuperContructorFunctionDecl = 0; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 627 | NumObjCStringLiterals = 0; |
Steve Naroff | 68272b8 | 2008-12-08 20:01:41 +0000 | [diff] [blame] | 628 | PropParentMap = 0; |
| 629 | CurrentBody = 0; |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 630 | DisableReplaceStmt = false; |
Fariborz Jahanian | f292fcf | 2010-01-07 22:51:18 +0000 | [diff] [blame] | 631 | objc_impl_method = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 632 | |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 633 | // Get the ID and start/end of the main file. |
| 634 | MainFileID = SM->getMainFileID(); |
| 635 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 636 | MainFileStart = MainBuf->getBufferStart(); |
| 637 | MainFileEnd = MainBuf->getBufferEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 638 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 639 | Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOpts()); |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 642 | //===----------------------------------------------------------------------===// |
| 643 | // Top Level Driver Code |
| 644 | //===----------------------------------------------------------------------===// |
| 645 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 646 | void RewriteObjC::HandleTopLevelSingleDecl(Decl *D) { |
Ted Kremenek | e50187a | 2010-02-05 21:28:51 +0000 | [diff] [blame] | 647 | if (Diags.hasErrorOccurred()) |
| 648 | return; |
| 649 | |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 650 | // Two cases: either the decl could be in the main file, or it could be in a |
| 651 | // #included file. If the former, rewrite it now. If the later, check to see |
| 652 | // if we rewrote the #include/#import. |
| 653 | SourceLocation Loc = D->getLocation(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 654 | Loc = SM->getExpansionLoc(Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 655 | |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 656 | // If this is for a builtin, ignore it. |
| 657 | if (Loc.isInvalid()) return; |
| 658 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 659 | // Look for built-in declarations that we need to refer during the rewrite. |
| 660 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 661 | RewriteFunctionDecl(FD); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 662 | } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) { |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 663 | // declared in <Foundation/NSString.h> |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 664 | if (FVD->getName() == "_NSConstantStringClassReference") { |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 665 | ConstantStringClassReference = FVD; |
| 666 | return; |
| 667 | } |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 668 | } else if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) { |
| 669 | if (ID->isThisDeclarationADefinition()) |
| 670 | RewriteInterfaceDecl(ID); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 671 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 672 | RewriteCategoryDecl(CD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 673 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 674 | if (PD->isThisDeclarationADefinition()) |
| 675 | RewriteProtocolDecl(PD); |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 676 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
| 677 | // Recurse into linkage specifications |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 678 | for (DeclContext::decl_iterator DI = LSD->decls_begin(), |
| 679 | DIEnd = LSD->decls_end(); |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 680 | DI != DIEnd; ) { |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 681 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>((*DI))) { |
| 682 | if (!IFace->isThisDeclarationADefinition()) { |
| 683 | SmallVector<Decl *, 8> DG; |
| 684 | SourceLocation StartLoc = IFace->getLocStart(); |
| 685 | do { |
| 686 | if (isa<ObjCInterfaceDecl>(*DI) && |
| 687 | !cast<ObjCInterfaceDecl>(*DI)->isThisDeclarationADefinition() && |
| 688 | StartLoc == (*DI)->getLocStart()) |
| 689 | DG.push_back(*DI); |
| 690 | else |
| 691 | break; |
| 692 | |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 693 | ++DI; |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 694 | } while (DI != DIEnd); |
| 695 | RewriteForwardClassDecl(DG); |
| 696 | continue; |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 697 | } |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 698 | } |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 699 | |
| 700 | if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>((*DI))) { |
| 701 | if (!Proto->isThisDeclarationADefinition()) { |
| 702 | SmallVector<Decl *, 8> DG; |
| 703 | SourceLocation StartLoc = Proto->getLocStart(); |
| 704 | do { |
| 705 | if (isa<ObjCProtocolDecl>(*DI) && |
| 706 | !cast<ObjCProtocolDecl>(*DI)->isThisDeclarationADefinition() && |
| 707 | StartLoc == (*DI)->getLocStart()) |
| 708 | DG.push_back(*DI); |
| 709 | else |
| 710 | break; |
| 711 | |
| 712 | ++DI; |
| 713 | } while (DI != DIEnd); |
| 714 | RewriteForwardProtocolDecl(DG); |
| 715 | continue; |
| 716 | } |
| 717 | } |
| 718 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 719 | HandleTopLevelSingleDecl(*DI); |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 720 | ++DI; |
| 721 | } |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 722 | } |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 723 | // 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] | 724 | if (SM->isFromMainFile(Loc)) |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 725 | return HandleDeclInMainFile(D); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 728 | //===----------------------------------------------------------------------===// |
| 729 | // Syntactic (non-AST) Rewriting Code |
| 730 | //===----------------------------------------------------------------------===// |
| 731 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 732 | void RewriteObjC::RewriteInclude() { |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 733 | SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 734 | StringRef MainBuf = SM->getBufferData(MainFileID); |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 735 | const char *MainBufStart = MainBuf.begin(); |
| 736 | const char *MainBufEnd = MainBuf.end(); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 737 | size_t ImportLen = strlen("import"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 738 | |
Fariborz Jahanian | af57b46 | 2008-01-19 01:03:17 +0000 | [diff] [blame] | 739 | // Loop over the whole file, looking for includes. |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 740 | for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { |
| 741 | if (*BufPtr == '#') { |
| 742 | if (++BufPtr == MainBufEnd) |
| 743 | return; |
| 744 | while (*BufPtr == ' ' || *BufPtr == '\t') |
| 745 | if (++BufPtr == MainBufEnd) |
| 746 | return; |
| 747 | if (!strncmp(BufPtr, "import", ImportLen)) { |
| 748 | // replace import with include |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 749 | SourceLocation ImportLoc = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 750 | LocStart.getLocWithOffset(BufPtr-MainBufStart); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 751 | ReplaceText(ImportLoc, ImportLen, "include"); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 752 | BufPtr += ImportLen; |
| 753 | } |
| 754 | } |
| 755 | } |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 758 | static std::string getIvarAccessString(ObjCIvarDecl *OID) { |
| 759 | const ObjCInterfaceDecl *ClassDecl = OID->getContainingInterface(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 760 | std::string S; |
| 761 | S = "((struct "; |
| 762 | S += ClassDecl->getIdentifier()->getName(); |
| 763 | S += "_IMPL *)self)->"; |
Daniel Dunbar | 5ffe14c | 2009-10-18 20:26:27 +0000 | [diff] [blame] | 764 | S += OID->getName(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 765 | return S; |
| 766 | } |
| 767 | |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 768 | void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 769 | ObjCImplementationDecl *IMD, |
| 770 | ObjCCategoryImplDecl *CID) { |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 771 | static bool objcGetPropertyDefined = false; |
| 772 | static bool objcSetPropertyDefined = false; |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 773 | SourceLocation startLoc = PID->getLocStart(); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 774 | InsertText(startLoc, "// "); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 775 | const char *startBuf = SM->getCharacterData(startLoc); |
| 776 | assert((*startBuf == '@') && "bogus @synthesize location"); |
| 777 | const char *semiBuf = strchr(startBuf, ';'); |
| 778 | assert((*semiBuf == ';') && "@synthesize: can't find ';'"); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 779 | SourceLocation onePastSemiLoc = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 780 | startLoc.getLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 781 | |
| 782 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 783 | return; // FIXME: is this correct? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 784 | |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 785 | // Generate the 'getter' function. |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 786 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 787 | ObjCIvarDecl *OID = PID->getPropertyIvarDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 788 | |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 789 | if (!OID) |
| 790 | return; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 791 | unsigned Attributes = PD->getPropertyAttributes(); |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 792 | if (!PD->getGetterMethodDecl()->isDefined()) { |
| 793 | bool GenGetProperty = !(Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) && |
| 794 | (Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
| 795 | ObjCPropertyDecl::OBJC_PR_copy)); |
| 796 | std::string Getr; |
| 797 | if (GenGetProperty && !objcGetPropertyDefined) { |
| 798 | objcGetPropertyDefined = true; |
| 799 | // FIXME. Is this attribute correct in all cases? |
| 800 | Getr = "\nextern \"C\" __declspec(dllimport) " |
| 801 | "id objc_getProperty(id, SEL, long, bool);\n"; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 802 | } |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 803 | RewriteObjCMethodDecl(OID->getContainingInterface(), |
| 804 | PD->getGetterMethodDecl(), Getr); |
| 805 | Getr += "{ "; |
| 806 | // Synthesize an explicit cast to gain access to the ivar. |
| 807 | // See objc-act.c:objc_synthesize_new_getter() for details. |
| 808 | if (GenGetProperty) { |
| 809 | // return objc_getProperty(self, _cmd, offsetof(ClassDecl, OID), 1) |
| 810 | Getr += "typedef "; |
| 811 | const FunctionType *FPRetType = 0; |
| 812 | RewriteTypeIntoString(PD->getGetterMethodDecl()->getResultType(), Getr, |
| 813 | FPRetType); |
| 814 | Getr += " _TYPE"; |
| 815 | if (FPRetType) { |
| 816 | Getr += ")"; // close the precedence "scope" for "*". |
| 817 | |
| 818 | // Now, emit the argument types (if any). |
| 819 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)){ |
| 820 | Getr += "("; |
| 821 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 822 | if (i) Getr += ", "; |
| 823 | std::string ParamStr = FT->getArgType(i).getAsString( |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 824 | Context->getPrintingPolicy()); |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 825 | Getr += ParamStr; |
| 826 | } |
| 827 | if (FT->isVariadic()) { |
| 828 | if (FT->getNumArgs()) Getr += ", "; |
| 829 | Getr += "..."; |
| 830 | } |
| 831 | Getr += ")"; |
| 832 | } else |
| 833 | Getr += "()"; |
| 834 | } |
| 835 | Getr += ";\n"; |
| 836 | Getr += "return (_TYPE)"; |
| 837 | Getr += "objc_getProperty(self, _cmd, "; |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 838 | RewriteIvarOffsetComputation(OID, Getr); |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 839 | Getr += ", 1)"; |
| 840 | } |
| 841 | else |
| 842 | Getr += "return " + getIvarAccessString(OID); |
| 843 | Getr += "; }"; |
| 844 | InsertText(onePastSemiLoc, Getr); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 845 | } |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 846 | |
| 847 | if (PD->isReadOnly() || PD->getSetterMethodDecl()->isDefined()) |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 848 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 849 | |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 850 | // Generate the 'setter' function. |
| 851 | std::string Setr; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 852 | bool GenSetProperty = Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
| 853 | ObjCPropertyDecl::OBJC_PR_copy); |
| 854 | if (GenSetProperty && !objcSetPropertyDefined) { |
| 855 | objcSetPropertyDefined = true; |
| 856 | // FIXME. Is this attribute correct in all cases? |
| 857 | Setr = "\nextern \"C\" __declspec(dllimport) " |
| 858 | "void objc_setProperty (id, SEL, long, id, bool, bool);\n"; |
| 859 | } |
| 860 | |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 861 | RewriteObjCMethodDecl(OID->getContainingInterface(), |
| 862 | PD->getSetterMethodDecl(), Setr); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 863 | Setr += "{ "; |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 864 | // Synthesize an explicit cast to initialize the ivar. |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 865 | // See objc-act.c:objc_synthesize_new_setter() for details. |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 866 | if (GenSetProperty) { |
| 867 | Setr += "objc_setProperty (self, _cmd, "; |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 868 | RewriteIvarOffsetComputation(OID, Setr); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 869 | Setr += ", (id)"; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 870 | Setr += PD->getName(); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 871 | Setr += ", "; |
| 872 | if (Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 873 | Setr += "0, "; |
| 874 | else |
| 875 | Setr += "1, "; |
| 876 | if (Attributes & ObjCPropertyDecl::OBJC_PR_copy) |
| 877 | Setr += "1)"; |
| 878 | else |
| 879 | Setr += "0)"; |
| 880 | } |
| 881 | else { |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 882 | Setr += getIvarAccessString(OID) + " = "; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 883 | Setr += PD->getName(); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 884 | } |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 885 | Setr += "; }"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 886 | InsertText(onePastSemiLoc, Setr); |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 887 | } |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 888 | |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 889 | static void RewriteOneForwardClassDecl(ObjCInterfaceDecl *ForwardDecl, |
| 890 | std::string &typedefString) { |
| 891 | typedefString += "#ifndef _REWRITER_typedef_"; |
| 892 | typedefString += ForwardDecl->getNameAsString(); |
| 893 | typedefString += "\n"; |
| 894 | typedefString += "#define _REWRITER_typedef_"; |
| 895 | typedefString += ForwardDecl->getNameAsString(); |
| 896 | typedefString += "\n"; |
| 897 | typedefString += "typedef struct objc_object "; |
| 898 | typedefString += ForwardDecl->getNameAsString(); |
| 899 | typedefString += ";\n#endif\n"; |
| 900 | } |
| 901 | |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 902 | void RewriteObjC::RewriteForwardClassEpilogue(ObjCInterfaceDecl *ClassDecl, |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 903 | const std::string &typedefString) { |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 904 | SourceLocation startLoc = ClassDecl->getLocStart(); |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 905 | const char *startBuf = SM->getCharacterData(startLoc); |
| 906 | const char *semiPtr = strchr(startBuf, ';'); |
| 907 | // Replace the @class with typedefs corresponding to the classes. |
| 908 | ReplaceText(startLoc, semiPtr-startBuf+1, typedefString); |
| 909 | } |
| 910 | |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 911 | void RewriteObjC::RewriteForwardClassDecl(DeclGroupRef D) { |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 912 | std::string typedefString; |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 913 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 914 | ObjCInterfaceDecl *ForwardDecl = cast<ObjCInterfaceDecl>(*I); |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 915 | if (I == D.begin()) { |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 916 | // Translate to typedef's that forward reference structs with the same name |
| 917 | // as the class. As a convenience, we include the original declaration |
| 918 | // as a comment. |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 919 | typedefString += "// @class "; |
| 920 | typedefString += ForwardDecl->getNameAsString(); |
Fariborz Jahanian | 91fbd12 | 2010-01-11 22:48:40 +0000 | [diff] [blame] | 921 | typedefString += ";\n"; |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 922 | } |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 923 | RewriteOneForwardClassDecl(ForwardDecl, typedefString); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 924 | } |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 925 | DeclGroupRef::iterator I = D.begin(); |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 926 | RewriteForwardClassEpilogue(cast<ObjCInterfaceDecl>(*I), typedefString); |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 927 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 928 | |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 929 | void RewriteObjC::RewriteForwardClassDecl( |
| 930 | const llvm::SmallVector<Decl*, 8> &D) { |
| 931 | std::string typedefString; |
| 932 | for (unsigned i = 0; i < D.size(); i++) { |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 933 | ObjCInterfaceDecl *ForwardDecl = cast<ObjCInterfaceDecl>(D[i]); |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 934 | if (i == 0) { |
| 935 | typedefString += "// @class "; |
| 936 | typedefString += ForwardDecl->getNameAsString(); |
| 937 | typedefString += ";\n"; |
| 938 | } |
| 939 | RewriteOneForwardClassDecl(ForwardDecl, typedefString); |
| 940 | } |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 941 | RewriteForwardClassEpilogue(cast<ObjCInterfaceDecl>(D[0]), typedefString); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 944 | void RewriteObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) { |
Fariborz Jahanian | d050240 | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 945 | // When method is a synthesized one, such as a getter/setter there is |
| 946 | // nothing to rewrite. |
Fariborz Jahanian | 8891480 | 2011-09-09 20:35:22 +0000 | [diff] [blame] | 947 | if (Method->isImplicit()) |
Fariborz Jahanian | d050240 | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 948 | return; |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 949 | SourceLocation LocStart = Method->getLocStart(); |
| 950 | SourceLocation LocEnd = Method->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 951 | |
Chandler Carruth | 6421162 | 2011-07-25 21:09:52 +0000 | [diff] [blame] | 952 | if (SM->getExpansionLineNumber(LocEnd) > |
| 953 | SM->getExpansionLineNumber(LocStart)) { |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 954 | InsertText(LocStart, "#if 0\n"); |
| 955 | ReplaceText(LocEnd, 1, ";\n#endif\n"); |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 956 | } else { |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 957 | InsertText(LocStart, "// "); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 958 | } |
| 959 | } |
| 960 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 961 | void RewriteObjC::RewriteProperty(ObjCPropertyDecl *prop) { |
Fariborz Jahanian | d050240 | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 962 | SourceLocation Loc = prop->getAtLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 963 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 964 | ReplaceText(Loc, 0, "// "); |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 965 | // FIXME: handle properties that are declared across multiple lines. |
Fariborz Jahanian | 957cf65 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 966 | } |
| 967 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 968 | void RewriteObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 969 | SourceLocation LocStart = CatDecl->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 970 | |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 971 | // FIXME: handle category headers that are declared across multiple lines. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 972 | ReplaceText(LocStart, 0, "// "); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 973 | |
Fariborz Jahanian | 13751e3 | 2010-02-10 01:15:09 +0000 | [diff] [blame] | 974 | for (ObjCCategoryDecl::prop_iterator I = CatDecl->prop_begin(), |
| 975 | E = CatDecl->prop_end(); I != E; ++I) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 976 | RewriteProperty(*I); |
Fariborz Jahanian | 13751e3 | 2010-02-10 01:15:09 +0000 | [diff] [blame] | 977 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 978 | for (ObjCCategoryDecl::instmeth_iterator |
| 979 | I = CatDecl->instmeth_begin(), E = CatDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 980 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 981 | RewriteMethodDeclaration(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 982 | for (ObjCCategoryDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 983 | I = CatDecl->classmeth_begin(), E = CatDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 984 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 985 | RewriteMethodDeclaration(*I); |
| 986 | |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 987 | // Lastly, comment out the @end. |
Fariborz Jahanian | 73d1eb0 | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 988 | ReplaceText(CatDecl->getAtEndRange().getBegin(), |
| 989 | strlen("@end"), "/* @end */"); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 990 | } |
| 991 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 992 | void RewriteObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 993 | SourceLocation LocStart = PDecl->getLocStart(); |
Douglas Gregor | 61cc296 | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 994 | assert(PDecl->isThisDeclarationADefinition()); |
| 995 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 996 | // FIXME: handle protocol headers that are declared across multiple lines. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 997 | ReplaceText(LocStart, 0, "// "); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 998 | |
| 999 | for (ObjCProtocolDecl::instmeth_iterator |
| 1000 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1001 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1002 | RewriteMethodDeclaration(*I); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1003 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1004 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1005 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1006 | RewriteMethodDeclaration(*I); |
| 1007 | |
Fariborz Jahanian | 07acdf4 | 2010-09-24 18:36:58 +0000 | [diff] [blame] | 1008 | for (ObjCInterfaceDecl::prop_iterator I = PDecl->prop_begin(), |
| 1009 | E = PDecl->prop_end(); I != E; ++I) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1010 | RewriteProperty(*I); |
Fariborz Jahanian | 07acdf4 | 2010-09-24 18:36:58 +0000 | [diff] [blame] | 1011 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 1012 | // Lastly, comment out the @end. |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1013 | SourceLocation LocEnd = PDecl->getAtEndRange().getBegin(); |
Fariborz Jahanian | 73d1eb0 | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 1014 | ReplaceText(LocEnd, strlen("@end"), "/* @end */"); |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 1015 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 1016 | // Must comment out @optional/@required |
| 1017 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1018 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 1019 | for (const char *p = startBuf; p < endBuf; p++) { |
| 1020 | if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1021 | SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1022 | ReplaceText(OptionalLoc, strlen("@optional"), "/* @optional */"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1023 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 1024 | } |
| 1025 | else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1026 | SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1027 | ReplaceText(OptionalLoc, strlen("@required"), "/* @required */"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1028 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 1029 | } |
| 1030 | } |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1033 | void RewriteObjC::RewriteForwardProtocolDecl(DeclGroupRef D) { |
| 1034 | SourceLocation LocStart = (*D.begin())->getLocStart(); |
| 1035 | if (LocStart.isInvalid()) |
| 1036 | llvm_unreachable("Invalid SourceLocation"); |
| 1037 | // FIXME: handle forward protocol that are declared across multiple lines. |
| 1038 | ReplaceText(LocStart, 0, "// "); |
| 1039 | } |
| 1040 | |
| 1041 | void |
| 1042 | RewriteObjC::RewriteForwardProtocolDecl(const llvm::SmallVector<Decl*, 8> &DG) { |
| 1043 | SourceLocation LocStart = DG[0]->getLocStart(); |
Steve Naroff | b7fa992 | 2007-11-14 03:37:28 +0000 | [diff] [blame] | 1044 | if (LocStart.isInvalid()) |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1045 | llvm_unreachable("Invalid SourceLocation"); |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 1046 | // FIXME: handle forward protocol that are declared across multiple lines. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1047 | ReplaceText(LocStart, 0, "// "); |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1050 | void RewriteObjC::RewriteTypeIntoString(QualType T, std::string &ResultStr, |
| 1051 | const FunctionType *&FPRetType) { |
| 1052 | if (T->isObjCQualifiedIdType()) |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1053 | ResultStr += "id"; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1054 | else if (T->isFunctionPointerType() || |
| 1055 | T->isBlockPointerType()) { |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1056 | // needs special handling, since pointer-to-functions have special |
| 1057 | // syntax (where a decaration models use). |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1058 | QualType retType = T; |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1059 | QualType PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1060 | if (const PointerType* PT = retType->getAs<PointerType>()) |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1061 | PointeeTy = PT->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1062 | else if (const BlockPointerType *BPT = retType->getAs<BlockPointerType>()) |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1063 | PointeeTy = BPT->getPointeeType(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1064 | if ((FPRetType = PointeeTy->getAs<FunctionType>())) { |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1065 | ResultStr += FPRetType->getResultType().getAsString( |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1066 | Context->getPrintingPolicy()); |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1067 | ResultStr += "(*"; |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1068 | } |
| 1069 | } else |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1070 | ResultStr += T.getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1073 | void RewriteObjC::RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl, |
| 1074 | ObjCMethodDecl *OMD, |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1075 | std::string &ResultStr) { |
| 1076 | //fprintf(stderr,"In RewriteObjCMethodDecl\n"); |
| 1077 | const FunctionType *FPRetType = 0; |
| 1078 | ResultStr += "\nstatic "; |
| 1079 | RewriteTypeIntoString(OMD->getResultType(), ResultStr, FPRetType); |
Fariborz Jahanian | 531a1ea | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 1080 | ResultStr += " "; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1081 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1082 | // Unique method name |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1083 | std::string NameStr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1084 | |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1085 | if (OMD->isInstanceMethod()) |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1086 | NameStr += "_I_"; |
| 1087 | else |
| 1088 | NameStr += "_C_"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1089 | |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1090 | NameStr += IDecl->getNameAsString(); |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1091 | NameStr += "_"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1092 | |
| 1093 | if (ObjCCategoryImplDecl *CID = |
Steve Naroff | 3e0a540 | 2009-01-08 19:41:02 +0000 | [diff] [blame] | 1094 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1095 | NameStr += CID->getNameAsString(); |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1096 | NameStr += "_"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1097 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1098 | // Append selector names, replacing ':' with '_' |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1099 | { |
| 1100 | std::string selString = OMD->getSelector().getAsString(); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1101 | int len = selString.size(); |
| 1102 | for (int i = 0; i < len; i++) |
| 1103 | if (selString[i] == ':') |
| 1104 | selString[i] = '_'; |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1105 | NameStr += selString; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1106 | } |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1107 | // Remember this name for metadata emission |
| 1108 | MethodInternalNames[OMD] = NameStr; |
| 1109 | ResultStr += NameStr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1110 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1111 | // Rewrite arguments |
| 1112 | ResultStr += "("; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1113 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1114 | // invisible arguments |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1115 | if (OMD->isInstanceMethod()) { |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1116 | QualType selfTy = Context->getObjCInterfaceType(IDecl); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1117 | selfTy = Context->getPointerType(selfTy); |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 1118 | if (!LangOpts.MicrosoftExt) { |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1119 | if (ObjCSynthesizedStructs.count(const_cast<ObjCInterfaceDecl*>(IDecl))) |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 1120 | ResultStr += "struct "; |
| 1121 | } |
| 1122 | // When rewriting for Microsoft, explicitly omit the structure name. |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1123 | ResultStr += IDecl->getNameAsString(); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1124 | ResultStr += " *"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1125 | } |
| 1126 | else |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1127 | ResultStr += Context->getObjCClassType().getAsString( |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1128 | Context->getPrintingPolicy()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1129 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1130 | ResultStr += " self, "; |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1131 | ResultStr += Context->getObjCSelType().getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1132 | ResultStr += " _cmd"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1133 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1134 | // Method arguments. |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1135 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 1136 | E = OMD->param_end(); PI != E; ++PI) { |
| 1137 | ParmVarDecl *PDecl = *PI; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1138 | ResultStr += ", "; |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1139 | if (PDecl->getType()->isObjCQualifiedIdType()) { |
| 1140 | ResultStr += "id "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1141 | ResultStr += PDecl->getNameAsString(); |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1142 | } else { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1143 | std::string Name = PDecl->getNameAsString(); |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 1144 | QualType QT = PDecl->getType(); |
| 1145 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 2610f90 | 2012-03-27 16:42:20 +0000 | [diff] [blame] | 1146 | (void)convertBlockPointerToFunctionPointer(QT); |
| 1147 | QT.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1148 | ResultStr += Name; |
| 1149 | } |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1150 | } |
Fariborz Jahanian | 7c39ff7 | 2008-01-21 20:14:23 +0000 | [diff] [blame] | 1151 | if (OMD->isVariadic()) |
| 1152 | ResultStr += ", ..."; |
Fariborz Jahanian | 531a1ea | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 1153 | ResultStr += ") "; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1154 | |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1155 | if (FPRetType) { |
| 1156 | ResultStr += ")"; // close the precedence "scope" for "*". |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1157 | |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1158 | // Now, emit the argument types (if any). |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1159 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) { |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1160 | ResultStr += "("; |
| 1161 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 1162 | if (i) ResultStr += ", "; |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1163 | std::string ParamStr = FT->getArgType(i).getAsString( |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1164 | Context->getPrintingPolicy()); |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1165 | ResultStr += ParamStr; |
| 1166 | } |
| 1167 | if (FT->isVariadic()) { |
| 1168 | if (FT->getNumArgs()) ResultStr += ", "; |
| 1169 | ResultStr += "..."; |
| 1170 | } |
| 1171 | ResultStr += ")"; |
| 1172 | } else { |
| 1173 | ResultStr += "()"; |
| 1174 | } |
| 1175 | } |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1176 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1177 | void RewriteObjC::RewriteImplementationDecl(Decl *OID) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1178 | ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID); |
| 1179 | ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1180 | |
Fariborz Jahanian | a135216 | 2010-02-15 21:11:41 +0000 | [diff] [blame] | 1181 | InsertText(IMD ? IMD->getLocStart() : CID->getLocStart(), "// "); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1182 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1183 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1184 | I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(), |
| 1185 | E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1186 | I != E; ++I) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1187 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1188 | ObjCMethodDecl *OMD = *I; |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1189 | RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1190 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1191 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1192 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1193 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1194 | const char *endBuf = SM->getCharacterData(LocEnd); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1195 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1196 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1197 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1198 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1199 | I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(), |
| 1200 | E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1201 | I != E; ++I) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1202 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1203 | ObjCMethodDecl *OMD = *I; |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1204 | RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1205 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1206 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1207 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1208 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1209 | const char *endBuf = SM->getCharacterData(LocEnd); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1210 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1211 | } |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1212 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1213 | I = IMD ? IMD->propimpl_begin() : CID->propimpl_begin(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1214 | E = IMD ? IMD->propimpl_end() : CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1215 | I != E; ++I) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1216 | RewritePropertyImplDecl(*I, IMD, CID); |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1219 | InsertText(IMD ? IMD->getLocEnd() : CID->getLocEnd(), "// "); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1222 | void RewriteObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { |
Steve Naroff | f908a87 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 1223 | std::string ResultStr; |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1224 | if (!ObjCForwardDecls.count(ClassDecl->getCanonicalDecl())) { |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1225 | // we haven't seen a forward decl - generate a typedef. |
Steve Naroff | 5086a8d | 2007-11-14 23:02:56 +0000 | [diff] [blame] | 1226 | ResultStr = "#ifndef _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1227 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1228 | ResultStr += "\n"; |
| 1229 | ResultStr += "#define _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1230 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1231 | ResultStr += "\n"; |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1232 | ResultStr += "typedef struct objc_object "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1233 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1234 | ResultStr += ";\n#endif\n"; |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1235 | // Mark this typedef as having been generated. |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1236 | ObjCForwardDecls.insert(ClassDecl->getCanonicalDecl()); |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1237 | } |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 1238 | RewriteObjCInternalStruct(ClassDecl, ResultStr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1239 | |
| 1240 | for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1241 | E = ClassDecl->prop_end(); I != E; ++I) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1242 | RewriteProperty(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1243 | for (ObjCInterfaceDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1244 | I = ClassDecl->instmeth_begin(), E = ClassDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1245 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1246 | RewriteMethodDeclaration(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1247 | for (ObjCInterfaceDecl::classmeth_iterator |
| 1248 | I = ClassDecl->classmeth_begin(), E = ClassDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1249 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1250 | RewriteMethodDeclaration(*I); |
| 1251 | |
Steve Naroff | 2feac5e | 2007-10-30 03:43:13 +0000 | [diff] [blame] | 1252 | // Lastly, comment out the @end. |
Fariborz Jahanian | 73d1eb0 | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 1253 | ReplaceText(ClassDecl->getAtEndRange().getBegin(), strlen("@end"), |
| 1254 | "/* @end */"); |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1257 | Stmt *RewriteObjC::RewritePropertyOrImplicitSetter(PseudoObjectExpr *PseudoOp) { |
| 1258 | SourceRange OldRange = PseudoOp->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1259 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1260 | // We just magically know some things about the structure of this |
| 1261 | // expression. |
| 1262 | ObjCMessageExpr *OldMsg = |
| 1263 | cast<ObjCMessageExpr>(PseudoOp->getSemanticExpr( |
| 1264 | PseudoOp->getNumSemanticExprs() - 1)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1265 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1266 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 1267 | // we need to suppress rewriting the sub-statements. |
| 1268 | Expr *Base, *RHS; |
| 1269 | { |
| 1270 | DisableReplaceStmtScope S(*this); |
| 1271 | |
| 1272 | // Rebuild the base expression if we have one. |
| 1273 | Base = 0; |
| 1274 | if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) { |
| 1275 | Base = OldMsg->getInstanceReceiver(); |
| 1276 | Base = cast<OpaqueValueExpr>(Base)->getSourceExpr(); |
| 1277 | Base = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Base)); |
| 1278 | } |
| 1279 | |
| 1280 | // Rebuild the RHS. |
| 1281 | RHS = cast<BinaryOperator>(PseudoOp->getSyntacticForm())->getRHS(); |
| 1282 | RHS = cast<OpaqueValueExpr>(RHS)->getSourceExpr(); |
| 1283 | RHS = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(RHS)); |
| 1284 | } |
| 1285 | |
| 1286 | // TODO: avoid this copy. |
| 1287 | SmallVector<SourceLocation, 1> SelLocs; |
| 1288 | OldMsg->getSelectorLocs(SelLocs); |
| 1289 | |
Chad Rosier | 0774ba7 | 2012-01-06 20:05:14 +0000 | [diff] [blame] | 1290 | ObjCMessageExpr *NewMsg = 0; |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1291 | switch (OldMsg->getReceiverKind()) { |
| 1292 | case ObjCMessageExpr::Class: |
| 1293 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1294 | OldMsg->getValueKind(), |
| 1295 | OldMsg->getLeftLoc(), |
| 1296 | OldMsg->getClassReceiverTypeInfo(), |
| 1297 | OldMsg->getSelector(), |
| 1298 | SelLocs, |
| 1299 | OldMsg->getMethodDecl(), |
| 1300 | RHS, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1301 | OldMsg->getRightLoc(), |
| 1302 | OldMsg->isImplicit()); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1303 | break; |
| 1304 | |
| 1305 | case ObjCMessageExpr::Instance: |
| 1306 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1307 | OldMsg->getValueKind(), |
| 1308 | OldMsg->getLeftLoc(), |
| 1309 | Base, |
| 1310 | OldMsg->getSelector(), |
| 1311 | SelLocs, |
| 1312 | OldMsg->getMethodDecl(), |
| 1313 | RHS, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1314 | OldMsg->getRightLoc(), |
| 1315 | OldMsg->isImplicit()); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1316 | break; |
| 1317 | |
| 1318 | case ObjCMessageExpr::SuperClass: |
| 1319 | case ObjCMessageExpr::SuperInstance: |
| 1320 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1321 | OldMsg->getValueKind(), |
| 1322 | OldMsg->getLeftLoc(), |
| 1323 | OldMsg->getSuperLoc(), |
| 1324 | OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance, |
| 1325 | OldMsg->getSuperType(), |
| 1326 | OldMsg->getSelector(), |
| 1327 | SelLocs, |
| 1328 | OldMsg->getMethodDecl(), |
| 1329 | RHS, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1330 | OldMsg->getRightLoc(), |
| 1331 | OldMsg->isImplicit()); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1332 | break; |
| 1333 | } |
| 1334 | |
| 1335 | Stmt *Replacement = SynthMessageExpr(NewMsg); |
| 1336 | ReplaceStmtWithRange(PseudoOp, Replacement, OldRange); |
| 1337 | return Replacement; |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1340 | Stmt *RewriteObjC::RewritePropertyOrImplicitGetter(PseudoObjectExpr *PseudoOp) { |
| 1341 | SourceRange OldRange = PseudoOp->getSourceRange(); |
| 1342 | |
| 1343 | // We just magically know some things about the structure of this |
| 1344 | // expression. |
| 1345 | ObjCMessageExpr *OldMsg = |
| 1346 | cast<ObjCMessageExpr>(PseudoOp->getResultExpr()->IgnoreImplicit()); |
| 1347 | |
| 1348 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 1349 | // we need to suppress rewriting the sub-statements. |
| 1350 | Expr *Base = 0; |
| 1351 | { |
| 1352 | DisableReplaceStmtScope S(*this); |
| 1353 | |
| 1354 | // Rebuild the base expression if we have one. |
| 1355 | if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) { |
| 1356 | Base = OldMsg->getInstanceReceiver(); |
| 1357 | Base = cast<OpaqueValueExpr>(Base)->getSourceExpr(); |
| 1358 | Base = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Base)); |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 1359 | } |
Fariborz Jahanian | e0f8386 | 2010-10-20 16:07:20 +0000 | [diff] [blame] | 1360 | } |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1361 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1362 | // Intentionally empty. |
| 1363 | SmallVector<SourceLocation, 1> SelLocs; |
| 1364 | SmallVector<Expr*, 1> Args; |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1365 | |
Chad Rosier | 0774ba7 | 2012-01-06 20:05:14 +0000 | [diff] [blame] | 1366 | ObjCMessageExpr *NewMsg = 0; |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1367 | switch (OldMsg->getReceiverKind()) { |
| 1368 | case ObjCMessageExpr::Class: |
| 1369 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1370 | OldMsg->getValueKind(), |
| 1371 | OldMsg->getLeftLoc(), |
| 1372 | OldMsg->getClassReceiverTypeInfo(), |
| 1373 | OldMsg->getSelector(), |
| 1374 | SelLocs, |
| 1375 | OldMsg->getMethodDecl(), |
| 1376 | Args, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1377 | OldMsg->getRightLoc(), |
| 1378 | OldMsg->isImplicit()); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1379 | break; |
| 1380 | |
| 1381 | case ObjCMessageExpr::Instance: |
| 1382 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1383 | OldMsg->getValueKind(), |
| 1384 | OldMsg->getLeftLoc(), |
| 1385 | Base, |
| 1386 | OldMsg->getSelector(), |
| 1387 | SelLocs, |
| 1388 | OldMsg->getMethodDecl(), |
| 1389 | Args, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1390 | OldMsg->getRightLoc(), |
| 1391 | OldMsg->isImplicit()); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1392 | break; |
| 1393 | |
| 1394 | case ObjCMessageExpr::SuperClass: |
| 1395 | case ObjCMessageExpr::SuperInstance: |
| 1396 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1397 | OldMsg->getValueKind(), |
| 1398 | OldMsg->getLeftLoc(), |
| 1399 | OldMsg->getSuperLoc(), |
| 1400 | OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance, |
| 1401 | OldMsg->getSuperType(), |
| 1402 | OldMsg->getSelector(), |
| 1403 | SelLocs, |
| 1404 | OldMsg->getMethodDecl(), |
| 1405 | Args, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1406 | OldMsg->getRightLoc(), |
| 1407 | OldMsg->isImplicit()); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1408 | break; |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1409 | } |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1410 | |
| 1411 | Stmt *Replacement = SynthMessageExpr(NewMsg); |
| 1412 | ReplaceStmtWithRange(PseudoOp, Replacement, OldRange); |
| 1413 | return Replacement; |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1416 | /// SynthCountByEnumWithState - To print: |
| 1417 | /// ((unsigned int (*) |
| 1418 | /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1419 | /// (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1420 | /// sel_registerName( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1421 | /// "countByEnumeratingWithState:objects:count:"), |
| 1422 | /// &enumState, |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1423 | /// (id *)__rw_items, (unsigned int)16) |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1424 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1425 | void RewriteObjC::SynthCountByEnumWithState(std::string &buf) { |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1426 | buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, " |
| 1427 | "id *, unsigned int))(void *)objc_msgSend)"; |
| 1428 | buf += "\n\t\t"; |
| 1429 | buf += "((id)l_collection,\n\t\t"; |
| 1430 | buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; |
| 1431 | buf += "\n\t\t"; |
| 1432 | buf += "&enumState, " |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1433 | "(id *)__rw_items, (unsigned int)16)"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1434 | } |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1435 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1436 | /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach |
| 1437 | /// statement to exit to its outer synthesized loop. |
| 1438 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1439 | Stmt *RewriteObjC::RewriteBreakStmt(BreakStmt *S) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1440 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1441 | return S; |
| 1442 | // replace break with goto __break_label |
| 1443 | std::string buf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1444 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1445 | SourceLocation startLoc = S->getLocStart(); |
| 1446 | buf = "goto __break_label_"; |
| 1447 | buf += utostr(ObjCBcLabelNo.back()); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1448 | ReplaceText(startLoc, strlen("break"), buf); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1449 | |
| 1450 | return 0; |
| 1451 | } |
| 1452 | |
| 1453 | /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach |
| 1454 | /// statement to continue with its inner synthesized loop. |
| 1455 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1456 | Stmt *RewriteObjC::RewriteContinueStmt(ContinueStmt *S) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1457 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1458 | return S; |
| 1459 | // replace continue with goto __continue_label |
| 1460 | std::string buf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1461 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1462 | SourceLocation startLoc = S->getLocStart(); |
| 1463 | buf = "goto __continue_label_"; |
| 1464 | buf += utostr(ObjCBcLabelNo.back()); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1465 | ReplaceText(startLoc, strlen("continue"), buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1466 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1467 | return 0; |
| 1468 | } |
| 1469 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1470 | /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1471 | /// It rewrites: |
| 1472 | /// for ( type elem in collection) { stmts; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1473 | |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1474 | /// Into: |
| 1475 | /// { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1476 | /// type elem; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1477 | /// struct __objcFastEnumerationState enumState = { 0 }; |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1478 | /// id __rw_items[16]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1479 | /// id l_collection = (id)collection; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1480 | /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1481 | /// objects:__rw_items count:16]; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1482 | /// if (limit) { |
| 1483 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1484 | /// do { |
| 1485 | /// unsigned long counter = 0; |
| 1486 | /// do { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1487 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1488 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1489 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1490 | /// stmts; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1491 | /// __continue_label: ; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1492 | /// } while (counter < limit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1493 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1494 | /// objects:__rw_items count:16]); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1495 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1496 | /// __break_label: ; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1497 | /// } |
| 1498 | /// else |
| 1499 | /// elem = nil; |
| 1500 | /// } |
| 1501 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1502 | Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 1503 | SourceLocation OrigEnd) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1504 | assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1505 | assert(isa<ObjCForCollectionStmt>(Stmts.back()) && |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1506 | "ObjCForCollectionStmt Statement stack mismatch"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1507 | assert(!ObjCBcLabelNo.empty() && |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1508 | "ObjCForCollectionStmt - Label No stack empty"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1509 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1510 | SourceLocation startLoc = S->getLocStart(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1511 | const char *startBuf = SM->getCharacterData(startLoc); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1512 | StringRef elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1513 | std::string elementTypeAsString; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1514 | std::string buf; |
| 1515 | buf = "\n{\n\t"; |
| 1516 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { |
| 1517 | // type elem; |
Chris Lattner | 7e24e82 | 2009-03-28 06:33:19 +0000 | [diff] [blame] | 1518 | NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl()); |
Ted Kremenek | 1ed8e2a | 2008-10-06 22:16:13 +0000 | [diff] [blame] | 1519 | QualType ElementType = cast<ValueDecl>(D)->getType(); |
Steve Naroff | e89b8e7 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1520 | if (ElementType->isObjCQualifiedIdType() || |
| 1521 | ElementType->isObjCQualifiedInterfaceType()) |
| 1522 | // Simply use 'id' for all qualified types. |
| 1523 | elementTypeAsString = "id"; |
| 1524 | else |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1525 | elementTypeAsString = ElementType.getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1526 | buf += elementTypeAsString; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1527 | buf += " "; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 1528 | elementName = D->getName(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1529 | buf += elementName; |
| 1530 | buf += ";\n\t"; |
| 1531 | } |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1532 | else { |
| 1533 | DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 1534 | elementName = DR->getDecl()->getName(); |
Steve Naroff | e89b8e7 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1535 | ValueDecl *VD = cast<ValueDecl>(DR->getDecl()); |
| 1536 | if (VD->getType()->isObjCQualifiedIdType() || |
| 1537 | VD->getType()->isObjCQualifiedInterfaceType()) |
| 1538 | // Simply use 'id' for all qualified types. |
| 1539 | elementTypeAsString = "id"; |
| 1540 | else |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1541 | elementTypeAsString = VD->getType().getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1542 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1543 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1544 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1545 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1546 | // id __rw_items[16]; |
| 1547 | buf += "id __rw_items[16];\n\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1548 | // id l_collection = (id) |
| 1549 | buf += "id l_collection = (id)"; |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1550 | // Find start location of 'collection' the hard way! |
| 1551 | const char *startCollectionBuf = startBuf; |
| 1552 | startCollectionBuf += 3; // skip 'for' |
| 1553 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1554 | startCollectionBuf++; // skip '(' |
| 1555 | // find 'in' and skip it. |
| 1556 | while (*startCollectionBuf != ' ' || |
| 1557 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1558 | (*(startCollectionBuf+3) != ' ' && |
| 1559 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1560 | startCollectionBuf++; |
| 1561 | startCollectionBuf += 3; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1562 | |
| 1563 | // Replace: "for (type element in" with string constructed thus far. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1564 | ReplaceText(startLoc, startCollectionBuf - startBuf, buf); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1565 | // Replace ')' in for '(' type elem in collection ')' with ';' |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1566 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1567 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1568 | SourceLocation lparenLoc = startLoc.getLocWithOffset(rparenBuf-startBuf); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1569 | buf = ";\n\t"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1570 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1571 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1572 | // objects:__rw_items count:16]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1573 | // which is synthesized into: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1574 | // unsigned int limit = |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1575 | // ((unsigned int (*) |
| 1576 | // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1577 | // (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1578 | // sel_registerName( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1579 | // "countByEnumeratingWithState:objects:count:"), |
| 1580 | // (struct __objcFastEnumerationState *)&state, |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1581 | // (id *)__rw_items, (unsigned int)16); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1582 | buf += "unsigned long limit =\n\t\t"; |
| 1583 | SynthCountByEnumWithState(buf); |
| 1584 | buf += ";\n\t"; |
| 1585 | /// if (limit) { |
| 1586 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1587 | /// do { |
| 1588 | /// unsigned long counter = 0; |
| 1589 | /// do { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1590 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1591 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1592 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1593 | buf += "if (limit) {\n\t"; |
| 1594 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1595 | buf += "do {\n\t\t"; |
| 1596 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1597 | buf += "do {\n\t\t\t"; |
| 1598 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1599 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1600 | buf += elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1601 | buf += " = ("; |
| 1602 | buf += elementTypeAsString; |
| 1603 | buf += ")enumState.itemsPtr[counter++];"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1604 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1605 | ReplaceText(lparenLoc, 1, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1606 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1607 | /// __continue_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1608 | /// } while (counter < limit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1609 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1610 | /// objects:__rw_items count:16]); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1611 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1612 | /// __break_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1613 | /// } |
| 1614 | /// else |
| 1615 | /// elem = nil; |
| 1616 | /// } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1617 | /// |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1618 | buf = ";\n\t"; |
| 1619 | buf += "__continue_label_"; |
| 1620 | buf += utostr(ObjCBcLabelNo.back()); |
| 1621 | buf += ": ;"; |
| 1622 | buf += "\n\t\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1623 | buf += "} while (counter < limit);\n\t"; |
| 1624 | buf += "} while (limit = "; |
| 1625 | SynthCountByEnumWithState(buf); |
| 1626 | buf += ");\n\t"; |
| 1627 | buf += elementName; |
Fariborz Jahanian | 65b0aa5 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1628 | buf += " = (("; |
| 1629 | buf += elementTypeAsString; |
| 1630 | buf += ")0);\n\t"; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1631 | buf += "__break_label_"; |
| 1632 | buf += utostr(ObjCBcLabelNo.back()); |
| 1633 | buf += ": ;\n\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1634 | buf += "}\n\t"; |
| 1635 | buf += "else\n\t\t"; |
| 1636 | buf += elementName; |
Fariborz Jahanian | 65b0aa5 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1637 | buf += " = (("; |
| 1638 | buf += elementTypeAsString; |
| 1639 | buf += ")0);\n\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1640 | buf += "}\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1641 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1642 | // Insert all these *after* the statement body. |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1643 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1644 | if (isa<CompoundStmt>(S->getBody())) { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1645 | SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1646 | InsertText(endBodyLoc, buf); |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1647 | } else { |
| 1648 | /* Need to treat single statements specially. For example: |
| 1649 | * |
| 1650 | * for (A *a in b) if (stuff()) break; |
| 1651 | * for (A *a in b) xxxyy; |
| 1652 | * |
| 1653 | * The following code simply scans ahead to the semi to find the actual end. |
| 1654 | */ |
| 1655 | const char *stmtBuf = SM->getCharacterData(OrigEnd); |
| 1656 | const char *semiBuf = strchr(stmtBuf, ';'); |
| 1657 | assert(semiBuf && "Can't find ';'"); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1658 | SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(semiBuf-stmtBuf+1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1659 | InsertText(endBodyLoc, buf); |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1660 | } |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1661 | Stmts.pop_back(); |
| 1662 | ObjCBcLabelNo.pop_back(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1663 | return 0; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1666 | /// RewriteObjCSynchronizedStmt - |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1667 | /// This routine rewrites @synchronized(expr) stmt; |
| 1668 | /// into: |
| 1669 | /// objc_sync_enter(expr); |
| 1670 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1671 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1672 | Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1673 | // Get the start location and compute the semi location. |
| 1674 | SourceLocation startLoc = S->getLocStart(); |
| 1675 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1676 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1677 | assert((*startBuf == '@') && "bogus @synchronized location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1678 | |
| 1679 | std::string buf; |
Steve Naroff | 3498cc9 | 2008-08-21 13:03:03 +0000 | [diff] [blame] | 1680 | buf = "objc_sync_enter((id)"; |
| 1681 | const char *lparenBuf = startBuf; |
| 1682 | while (*lparenBuf != '(') lparenBuf++; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1683 | ReplaceText(startLoc, lparenBuf-startBuf+1, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1684 | // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since |
| 1685 | // the sync expression is typically a message expression that's already |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1686 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1687 | SourceLocation endLoc = S->getSynchBody()->getLocStart(); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1688 | const char *endBuf = SM->getCharacterData(endLoc); |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1689 | while (*endBuf != ')') endBuf--; |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1690 | SourceLocation rparenLoc = startLoc.getLocWithOffset(endBuf-startBuf); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1691 | buf = ");\n"; |
| 1692 | // declare a new scope with two variables, _stack and _rethrow. |
| 1693 | buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n"; |
| 1694 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1695 | buf += "char *pointers[4];} _stack;\n"; |
| 1696 | buf += "id volatile _rethrow = 0;\n"; |
| 1697 | buf += "objc_exception_try_enter(&_stack);\n"; |
| 1698 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1699 | ReplaceText(rparenLoc, 1, buf); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1700 | startLoc = S->getSynchBody()->getLocEnd(); |
| 1701 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1702 | |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1703 | assert((*startBuf == '}') && "bogus @synchronized block"); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1704 | SourceLocation lastCurlyLoc = startLoc; |
| 1705 | buf = "}\nelse {\n"; |
| 1706 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 1707 | buf += "}\n"; |
| 1708 | buf += "{ /* implicit finally clause */\n"; |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1709 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1710 | |
| 1711 | std::string syncBuf; |
| 1712 | syncBuf += " objc_sync_exit("; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 1713 | |
| 1714 | Expr *syncExpr = S->getSynchExpr(); |
| 1715 | CastKind CK = syncExpr->getType()->isObjCObjectPointerType() |
| 1716 | ? CK_BitCast : |
| 1717 | syncExpr->getType()->isBlockPointerType() |
| 1718 | ? CK_BlockPointerToObjCPointerCast |
| 1719 | : CK_CPointerToObjCPointerCast; |
| 1720 | syncExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 1721 | CK, syncExpr); |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1722 | std::string syncExprBufS; |
| 1723 | llvm::raw_string_ostream syncExprBuf(syncExprBufS); |
Richard Smith | d1420c6 | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 1724 | syncExpr->printPretty(syncExprBuf, 0, PrintingPolicy(LangOpts)); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1725 | syncBuf += syncExprBuf.str(); |
| 1726 | syncBuf += ");"; |
| 1727 | |
| 1728 | buf += syncBuf; |
| 1729 | buf += "\n if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1730 | buf += "}\n"; |
| 1731 | buf += "}"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1732 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1733 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1734 | |
| 1735 | bool hasReturns = false; |
| 1736 | HasReturnStmts(S->getSynchBody(), hasReturns); |
| 1737 | if (hasReturns) |
| 1738 | RewriteSyncReturnStmts(S->getSynchBody(), syncBuf); |
| 1739 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1740 | return 0; |
| 1741 | } |
| 1742 | |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1743 | void RewriteObjC::WarnAboutReturnGotoStmts(Stmt *S) |
| 1744 | { |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1745 | // Perform a bottom up traversal of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1746 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1747 | if (*CI) |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1748 | WarnAboutReturnGotoStmts(*CI); |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1749 | |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1750 | if (isa<ReturnStmt>(S) || isa<GotoStmt>(S)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1751 | Diags.Report(Context->getFullLoc(S->getLocStart()), |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1752 | TryFinallyContainsReturnDiag); |
| 1753 | } |
| 1754 | return; |
| 1755 | } |
| 1756 | |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1757 | void RewriteObjC::HasReturnStmts(Stmt *S, bool &hasReturns) |
| 1758 | { |
| 1759 | // Perform a bottom up traversal of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1760 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1761 | if (*CI) |
| 1762 | HasReturnStmts(*CI, hasReturns); |
| 1763 | |
| 1764 | if (isa<ReturnStmt>(S)) |
| 1765 | hasReturns = true; |
| 1766 | return; |
| 1767 | } |
| 1768 | |
| 1769 | void RewriteObjC::RewriteTryReturnStmts(Stmt *S) { |
| 1770 | // Perform a bottom up traversal of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1771 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1772 | if (*CI) { |
| 1773 | RewriteTryReturnStmts(*CI); |
| 1774 | } |
| 1775 | if (isa<ReturnStmt>(S)) { |
| 1776 | SourceLocation startLoc = S->getLocStart(); |
| 1777 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1778 | |
| 1779 | const char *semiBuf = strchr(startBuf, ';'); |
| 1780 | assert((*semiBuf == ';') && "RewriteTryReturnStmts: can't find ';'"); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1781 | SourceLocation onePastSemiLoc = startLoc.getLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1782 | |
| 1783 | std::string buf; |
| 1784 | buf = "{ objc_exception_try_exit(&_stack); return"; |
| 1785 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1786 | ReplaceText(startLoc, 6, buf); |
| 1787 | InsertText(onePastSemiLoc, "}"); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1788 | } |
| 1789 | return; |
| 1790 | } |
| 1791 | |
| 1792 | void RewriteObjC::RewriteSyncReturnStmts(Stmt *S, std::string syncExitBuf) { |
| 1793 | // Perform a bottom up traversal of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1794 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1795 | if (*CI) { |
| 1796 | RewriteSyncReturnStmts(*CI, syncExitBuf); |
| 1797 | } |
| 1798 | if (isa<ReturnStmt>(S)) { |
| 1799 | SourceLocation startLoc = S->getLocStart(); |
| 1800 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1801 | |
| 1802 | const char *semiBuf = strchr(startBuf, ';'); |
| 1803 | assert((*semiBuf == ';') && "RewriteSyncReturnStmts: can't find ';'"); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1804 | SourceLocation onePastSemiLoc = startLoc.getLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1805 | |
| 1806 | std::string buf; |
| 1807 | buf = "{ objc_exception_try_exit(&_stack);"; |
| 1808 | buf += syncExitBuf; |
| 1809 | buf += " return"; |
| 1810 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1811 | ReplaceText(startLoc, 6, buf); |
| 1812 | InsertText(onePastSemiLoc, "}"); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1813 | } |
| 1814 | return; |
| 1815 | } |
| 1816 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1817 | Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1818 | // Get the start location and compute the semi location. |
| 1819 | SourceLocation startLoc = S->getLocStart(); |
| 1820 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1821 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1822 | assert((*startBuf == '@') && "bogus @try location"); |
| 1823 | |
| 1824 | std::string buf; |
| 1825 | // declare a new scope with two variables, _stack and _rethrow. |
| 1826 | buf = "/* @try scope begin */ { struct _objc_exception_data {\n"; |
| 1827 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1828 | buf += "char *pointers[4];} _stack;\n"; |
| 1829 | buf += "id volatile _rethrow = 0;\n"; |
| 1830 | buf += "objc_exception_try_enter(&_stack);\n"; |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1831 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1832 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1833 | ReplaceText(startLoc, 4, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1834 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1835 | startLoc = S->getTryBody()->getLocEnd(); |
| 1836 | startBuf = SM->getCharacterData(startLoc); |
| 1837 | |
| 1838 | assert((*startBuf == '}') && "bogus @try block"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1839 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1840 | SourceLocation lastCurlyLoc = startLoc; |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1841 | if (S->getNumCatchStmts()) { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1842 | startLoc = startLoc.getLocWithOffset(1); |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1843 | buf = " /* @catch begin */ else {\n"; |
| 1844 | buf += " id _caught = objc_exception_extract(&_stack);\n"; |
| 1845 | buf += " objc_exception_try_enter (&_stack);\n"; |
| 1846 | buf += " if (_setjmp(_stack.buf))\n"; |
| 1847 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1848 | buf += " else { /* @catch continue */"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1849 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1850 | InsertText(startLoc, buf); |
Steve Naroff | 8bd3dc6 | 2008-09-09 19:59:12 +0000 | [diff] [blame] | 1851 | } else { /* no catch list */ |
| 1852 | buf = "}\nelse {\n"; |
| 1853 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1854 | buf += "}"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1855 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1856 | } |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1857 | Stmt *lastCatchBody = 0; |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1858 | for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I) { |
| 1859 | ObjCAtCatchStmt *Catch = S->getCatchStmt(I); |
Douglas Gregor | c00d8e1 | 2010-04-26 16:46:50 +0000 | [diff] [blame] | 1860 | VarDecl *catchDecl = Catch->getCatchParamDecl(); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1861 | |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1862 | if (I == 0) |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1863 | buf = "if ("; // we are generating code for the first catch clause |
| 1864 | else |
| 1865 | buf = "else if ("; |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1866 | startLoc = Catch->getLocStart(); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1867 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1868 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1869 | assert((*startBuf == '@') && "bogus @catch location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1870 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1871 | const char *lParenLoc = strchr(startBuf, '('); |
| 1872 | |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1873 | if (Catch->hasEllipsis()) { |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1874 | // Now rewrite the body... |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1875 | lastCatchBody = Catch->getCatchBody(); |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1876 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1877 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1878 | assert(*SM->getCharacterData(Catch->getRParenLoc()) == ')' && |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1879 | "bogus @catch paren location"); |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1880 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1881 | |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1882 | buf += "1) { id _tmp = _caught;"; |
Daniel Dunbar | d7407dc | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 1883 | Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, buf); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1884 | } else if (catchDecl) { |
| 1885 | QualType t = catchDecl->getType(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1886 | if (t == Context->getObjCIdType()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1887 | buf += "1) { "; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1888 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf); |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 1889 | } else if (const ObjCObjectPointerType *Ptr = |
| 1890 | t->getAs<ObjCObjectPointerType>()) { |
| 1891 | // Should be a pointer to a class. |
| 1892 | ObjCInterfaceDecl *IDecl = Ptr->getObjectType()->getInterface(); |
| 1893 | if (IDecl) { |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1894 | buf += "objc_exception_match((struct objc_class *)objc_getClass(\""; |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 1895 | buf += IDecl->getNameAsString(); |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1896 | buf += "\"), (struct objc_object *)_caught)) { "; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1897 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1898 | } |
| 1899 | } |
| 1900 | // Now rewrite the body... |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1901 | lastCatchBody = Catch->getCatchBody(); |
| 1902 | SourceLocation rParenLoc = Catch->getRParenLoc(); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1903 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1904 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 1905 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 1906 | assert((*rParenBuf == ')') && "bogus @catch paren location"); |
| 1907 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1908 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1909 | // Here we replace ") {" with "= _caught;" (which initializes and |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1910 | // declares the @catch parameter). |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1911 | ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, " = _caught;"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1912 | } else { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1913 | llvm_unreachable("@catch rewrite bug"); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1914 | } |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1915 | } |
| 1916 | // Complete the catch list... |
| 1917 | if (lastCatchBody) { |
| 1918 | SourceLocation bodyLoc = lastCatchBody->getLocEnd(); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1919 | assert(*SM->getCharacterData(bodyLoc) == '}' && |
| 1920 | "bogus @catch body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1921 | |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1922 | // Insert the last (implicit) else clause *before* the right curly brace. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1923 | bodyLoc = bodyLoc.getLocWithOffset(-1); |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1924 | buf = "} /* last catch end */\n"; |
| 1925 | buf += "else {\n"; |
| 1926 | buf += " _rethrow = _caught;\n"; |
| 1927 | buf += " objc_exception_try_exit(&_stack);\n"; |
| 1928 | buf += "} } /* @catch end */\n"; |
| 1929 | if (!S->getFinallyStmt()) |
| 1930 | buf += "}\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1931 | InsertText(bodyLoc, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1932 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1933 | // Set lastCurlyLoc |
| 1934 | lastCurlyLoc = lastCatchBody->getLocEnd(); |
| 1935 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1936 | if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1937 | startLoc = finalStmt->getLocStart(); |
| 1938 | startBuf = SM->getCharacterData(startLoc); |
| 1939 | assert((*startBuf == '@') && "bogus @finally start"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1940 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1941 | ReplaceText(startLoc, 8, "/* @finally */"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1942 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1943 | Stmt *body = finalStmt->getFinallyBody(); |
| 1944 | SourceLocation startLoc = body->getLocStart(); |
| 1945 | SourceLocation endLoc = body->getLocEnd(); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1946 | assert(*SM->getCharacterData(startLoc) == '{' && |
| 1947 | "bogus @finally body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1948 | assert(*SM->getCharacterData(endLoc) == '}' && |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1949 | "bogus @finally body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1950 | |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1951 | startLoc = startLoc.getLocWithOffset(1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1952 | InsertText(startLoc, " if (!_rethrow) objc_exception_try_exit(&_stack);\n"); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1953 | endLoc = endLoc.getLocWithOffset(-1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1954 | InsertText(endLoc, " if (_rethrow) objc_exception_throw(_rethrow);\n"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1955 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1956 | // Set lastCurlyLoc |
| 1957 | lastCurlyLoc = body->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1958 | |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1959 | // Now check for any return/continue/go statements within the @try. |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1960 | WarnAboutReturnGotoStmts(S->getTryBody()); |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1961 | } else { /* no finally clause - make sure we synthesize an implicit one */ |
| 1962 | buf = "{ /* implicit finally clause */\n"; |
| 1963 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
| 1964 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 1965 | buf += "}"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1966 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1967 | |
| 1968 | // Now check for any return/continue/go statements within the @try. |
| 1969 | // The implicit finally clause won't called if the @try contains any |
| 1970 | // jump statements. |
| 1971 | bool hasReturns = false; |
| 1972 | HasReturnStmts(S->getTryBody(), hasReturns); |
| 1973 | if (hasReturns) |
| 1974 | RewriteTryReturnStmts(S->getTryBody()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1975 | } |
| 1976 | // Now emit the final closing curly brace... |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1977 | lastCurlyLoc = lastCurlyLoc.getLocWithOffset(1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1978 | InsertText(lastCurlyLoc, " } /* @try scope end */\n"); |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1979 | return 0; |
| 1980 | } |
| 1981 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1982 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
| 1983 | // the throw expression is typically a message expression that's already |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1984 | // been rewritten! (which implies the SourceLocation's are invalid). |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1985 | Stmt *RewriteObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1986 | // Get the start location and compute the semi location. |
| 1987 | SourceLocation startLoc = S->getLocStart(); |
| 1988 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1989 | |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1990 | assert((*startBuf == '@') && "bogus @throw location"); |
| 1991 | |
| 1992 | std::string buf; |
| 1993 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
Steve Naroff | 20ebf8f | 2008-01-19 00:42:38 +0000 | [diff] [blame] | 1994 | if (S->getThrowExpr()) |
| 1995 | buf = "objc_exception_throw("; |
| 1996 | else // add an implicit argument |
| 1997 | buf = "objc_exception_throw(_caught"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1998 | |
Steve Naroff | 4ba0acb | 2008-07-25 15:41:30 +0000 | [diff] [blame] | 1999 | // handle "@ throw" correctly. |
| 2000 | const char *wBuf = strchr(startBuf, 'w'); |
| 2001 | assert((*wBuf == 'w') && "@throw: can't find 'w'"); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2002 | ReplaceText(startLoc, wBuf-startBuf+1, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2003 | |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2004 | const char *semiBuf = strchr(startBuf, ';'); |
| 2005 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2006 | SourceLocation semiLoc = startLoc.getLocWithOffset(semiBuf-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2007 | ReplaceText(semiLoc, 1, ");"); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2008 | return 0; |
| 2009 | } |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 2010 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2011 | Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 2012 | // Create a new string expression. |
| 2013 | QualType StrType = Context->getPointerType(Context->CharTy); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2014 | std::string StrEncoding; |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 2015 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2016 | Expr *Replacement = StringLiteral::Create(*Context, StrEncoding, |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2017 | StringLiteral::Ascii, false, |
| 2018 | StrType, SourceLocation()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2019 | ReplaceStmt(Exp, Replacement); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2020 | |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 2021 | // Replace this subexpr in the parent. |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2022 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 2023 | return Replacement; |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2026 | Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
Steve Naroff | 1a93764 | 2008-12-22 22:16:07 +0000 | [diff] [blame] | 2027 | if (!SelGetUidFunctionDecl) |
| 2028 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2029 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 2030 | // Create a call to sel_registerName("selName"). |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2031 | SmallVector<Expr*, 8> SelExprs; |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2032 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2033 | SelExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2034 | Exp->getSelector().getAsString(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2035 | StringLiteral::Ascii, false, |
| 2036 | argType, SourceLocation())); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2037 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2038 | &SelExprs[0], SelExprs.size()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2039 | ReplaceStmt(Exp, SelExp); |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2040 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2041 | return SelExp; |
| 2042 | } |
| 2043 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2044 | CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl( |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2045 | FunctionDecl *FD, Expr **args, unsigned nargs, SourceLocation StartLoc, |
| 2046 | SourceLocation EndLoc) { |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2047 | // 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] | 2048 | QualType msgSendType = FD->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2049 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2050 | // Create a reference to the objc_msgSend() declaration. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2051 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, false, msgSendType, |
| 2052 | VK_LValue, SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2053 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2054 | // 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] | 2055 | QualType pToFunc = Context->getPointerType(msgSendType); |
Anders Carlsson | 88465d3 | 2010-04-23 22:18:37 +0000 | [diff] [blame] | 2056 | ImplicitCastExpr *ICE = |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2057 | ImplicitCastExpr::Create(*Context, pToFunc, CK_FunctionToPointerDecay, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2058 | DRE, 0, VK_RValue); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2059 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2060 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2061 | |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2062 | CallExpr *Exp = |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2063 | new (Context) CallExpr(*Context, ICE, llvm::makeArrayRef(args, nargs), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2064 | FT->getCallResultType(*Context), |
| 2065 | VK_RValue, EndLoc); |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2066 | return Exp; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2069 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 2070 | const char *&startRef, const char *&endRef) { |
| 2071 | while (startBuf < endBuf) { |
| 2072 | if (*startBuf == '<') |
| 2073 | startRef = startBuf; // mark the start. |
| 2074 | if (*startBuf == '>') { |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 2075 | if (startRef && *startRef == '<') { |
| 2076 | endRef = startBuf; // mark the end. |
| 2077 | return true; |
| 2078 | } |
| 2079 | return false; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2080 | } |
| 2081 | startBuf++; |
| 2082 | } |
| 2083 | return false; |
| 2084 | } |
| 2085 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2086 | static void scanToNextArgument(const char *&argRef) { |
| 2087 | int angle = 0; |
| 2088 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 2089 | if (*argRef == '<') |
| 2090 | angle++; |
| 2091 | else if (*argRef == '>') |
| 2092 | angle--; |
| 2093 | argRef++; |
| 2094 | } |
| 2095 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 2096 | } |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2097 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2098 | bool RewriteObjC::needToScanForQualifiers(QualType T) { |
Fariborz Jahanian | 32132a0 | 2010-02-03 21:29:28 +0000 | [diff] [blame] | 2099 | if (T->isObjCQualifiedIdType()) |
| 2100 | return true; |
Fariborz Jahanian | 84aa946 | 2010-02-02 18:35:07 +0000 | [diff] [blame] | 2101 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 2102 | if (PT->getPointeeType()->isObjCQualifiedIdType()) |
| 2103 | return true; |
| 2104 | } |
| 2105 | if (T->isObjCObjectPointerType()) { |
| 2106 | T = T->getPointeeType(); |
| 2107 | return T->isObjCQualifiedInterfaceType(); |
| 2108 | } |
Fariborz Jahanian | 24f9cab | 2010-09-30 20:41:32 +0000 | [diff] [blame] | 2109 | if (T->isArrayType()) { |
| 2110 | QualType ElemTy = Context->getBaseElementType(T); |
| 2111 | return needToScanForQualifiers(ElemTy); |
| 2112 | } |
Fariborz Jahanian | 84aa946 | 2010-02-02 18:35:07 +0000 | [diff] [blame] | 2113 | return false; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2114 | } |
| 2115 | |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2116 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) { |
| 2117 | QualType Type = E->getType(); |
| 2118 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2119 | SourceLocation Loc, EndLoc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2120 | |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2121 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) { |
| 2122 | Loc = ECE->getLParenLoc(); |
| 2123 | EndLoc = ECE->getRParenLoc(); |
| 2124 | } else { |
| 2125 | Loc = E->getLocStart(); |
| 2126 | EndLoc = E->getLocEnd(); |
| 2127 | } |
| 2128 | // This will defend against trying to rewrite synthesized expressions. |
| 2129 | if (Loc.isInvalid() || EndLoc.isInvalid()) |
| 2130 | return; |
| 2131 | |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2132 | const char *startBuf = SM->getCharacterData(Loc); |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2133 | const char *endBuf = SM->getCharacterData(EndLoc); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2134 | const char *startRef = 0, *endRef = 0; |
| 2135 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2136 | // Get the locations of the startRef, endRef. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2137 | SourceLocation LessLoc = Loc.getLocWithOffset(startRef-startBuf); |
| 2138 | SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-startBuf+1); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2139 | // Comment out the protocol references. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2140 | InsertText(LessLoc, "/*"); |
| 2141 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2142 | } |
| 2143 | } |
| 2144 | } |
| 2145 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2146 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2147 | SourceLocation Loc; |
| 2148 | QualType Type; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2149 | const FunctionProtoType *proto = 0; |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2150 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 2151 | Loc = VD->getLocation(); |
| 2152 | Type = VD->getType(); |
| 2153 | } |
| 2154 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 2155 | Loc = FD->getLocation(); |
| 2156 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 2157 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2158 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2159 | assert(funcType && "missing function type"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2160 | proto = dyn_cast<FunctionProtoType>(funcType); |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2161 | if (!proto) |
| 2162 | return; |
| 2163 | Type = proto->getResultType(); |
| 2164 | } |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 2165 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(Dcl)) { |
| 2166 | Loc = FD->getLocation(); |
| 2167 | Type = FD->getType(); |
| 2168 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2169 | else |
| 2170 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2171 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2172 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2173 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2174 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2175 | const char *endBuf = SM->getCharacterData(Loc); |
| 2176 | const char *startBuf = endBuf; |
Steve Naroff | 6cafbf2 | 2008-05-31 05:02:17 +0000 | [diff] [blame] | 2177 | while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2178 | startBuf--; // scan backward (from the decl location) for return type. |
| 2179 | const char *startRef = 0, *endRef = 0; |
| 2180 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2181 | // Get the locations of the startRef, endRef. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2182 | SourceLocation LessLoc = Loc.getLocWithOffset(startRef-endBuf); |
| 2183 | SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-endBuf+1); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2184 | // Comment out the protocol references. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2185 | InsertText(LessLoc, "/*"); |
| 2186 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2187 | } |
| 2188 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2189 | if (!proto) |
| 2190 | return; // most likely, was a variable |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2191 | // Now check arguments. |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2192 | const char *startBuf = SM->getCharacterData(Loc); |
| 2193 | const char *startFuncBuf = startBuf; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2194 | for (unsigned i = 0; i < proto->getNumArgs(); i++) { |
| 2195 | if (needToScanForQualifiers(proto->getArgType(i))) { |
| 2196 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2197 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2198 | const char *endBuf = startBuf; |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2199 | // scan forward (from the decl location) for argument types. |
| 2200 | scanToNextArgument(endBuf); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2201 | const char *startRef = 0, *endRef = 0; |
| 2202 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2203 | // Get the locations of the startRef, endRef. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2204 | SourceLocation LessLoc = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2205 | Loc.getLocWithOffset(startRef-startFuncBuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2206 | SourceLocation GreaterLoc = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2207 | Loc.getLocWithOffset(endRef-startFuncBuf+1); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2208 | // Comment out the protocol references. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2209 | InsertText(LessLoc, "/*"); |
| 2210 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2211 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2212 | startBuf = ++endBuf; |
| 2213 | } |
| 2214 | else { |
Steve Naroff | aba49d1 | 2008-08-06 15:58:23 +0000 | [diff] [blame] | 2215 | // If the function name is derived from a macro expansion, then the |
| 2216 | // argument buffer will not follow the name. Need to speak with Chris. |
| 2217 | while (*startBuf && *startBuf != ')' && *startBuf != ',') |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2218 | startBuf++; // scan forward (from the decl location) for argument types. |
| 2219 | startBuf++; |
| 2220 | } |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2221 | } |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2222 | } |
| 2223 | |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2224 | void RewriteObjC::RewriteTypeOfDecl(VarDecl *ND) { |
| 2225 | QualType QT = ND->getType(); |
| 2226 | const Type* TypePtr = QT->getAs<Type>(); |
| 2227 | if (!isa<TypeOfExprType>(TypePtr)) |
| 2228 | return; |
| 2229 | while (isa<TypeOfExprType>(TypePtr)) { |
| 2230 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 2231 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 2232 | TypePtr = QT->getAs<Type>(); |
| 2233 | } |
| 2234 | // FIXME. This will not work for multiple declarators; as in: |
| 2235 | // __typeof__(a) b,c,d; |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2236 | std::string TypeAsString(QT.getAsString(Context->getPrintingPolicy())); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2237 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
| 2238 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 2239 | if (ND->getInit()) { |
| 2240 | std::string Name(ND->getNameAsString()); |
| 2241 | TypeAsString += " " + Name + " = "; |
| 2242 | Expr *E = ND->getInit(); |
| 2243 | SourceLocation startLoc; |
| 2244 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 2245 | startLoc = ECE->getLParenLoc(); |
| 2246 | else |
| 2247 | startLoc = E->getLocStart(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 2248 | startLoc = SM->getExpansionLoc(startLoc); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2249 | const char *endBuf = SM->getCharacterData(startLoc); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2250 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2251 | } |
| 2252 | else { |
| 2253 | SourceLocation X = ND->getLocEnd(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 2254 | X = SM->getExpansionLoc(X); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2255 | const char *endBuf = SM->getCharacterData(X); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2256 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2257 | } |
| 2258 | } |
| 2259 | |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2260 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2261 | void RewriteObjC::SynthSelGetUidFunctionDecl() { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2262 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2263 | SmallVector<QualType, 16> ArgTys; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2264 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2265 | QualType getFuncType = |
| 2266 | getSimpleFunctionType(Context->getObjCSelType(), &ArgTys[0], ArgTys.size()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2267 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2268 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2269 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2270 | SelGetUidIdent, getFuncType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2271 | SC_Extern, |
| 2272 | SC_None, false); |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2273 | } |
| 2274 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2275 | void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2276 | // declared in <objc/objc.h> |
Douglas Gregor | 51efe56 | 2009-01-09 01:47:02 +0000 | [diff] [blame] | 2277 | if (FD->getIdentifier() && |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 2278 | FD->getName() == "sel_registerName") { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2279 | SelGetUidFunctionDecl = FD; |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2280 | return; |
| 2281 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2282 | RewriteObjCQualifiedInterfaceTypes(FD); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2283 | } |
| 2284 | |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2285 | void RewriteObjC::RewriteBlockPointerType(std::string& Str, QualType Type) { |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2286 | std::string TypeString(Type.getAsString(Context->getPrintingPolicy())); |
Fariborz Jahanian | 52b2e1e | 2010-02-12 17:52:31 +0000 | [diff] [blame] | 2287 | const char *argPtr = TypeString.c_str(); |
| 2288 | if (!strchr(argPtr, '^')) { |
| 2289 | Str += TypeString; |
| 2290 | return; |
| 2291 | } |
| 2292 | while (*argPtr) { |
| 2293 | Str += (*argPtr == '^' ? '*' : *argPtr); |
| 2294 | argPtr++; |
| 2295 | } |
| 2296 | } |
| 2297 | |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2298 | // FIXME. Consolidate this routine with RewriteBlockPointerType. |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2299 | void RewriteObjC::RewriteBlockPointerTypeVariable(std::string& Str, |
| 2300 | ValueDecl *VD) { |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2301 | QualType Type = VD->getType(); |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2302 | std::string TypeString(Type.getAsString(Context->getPrintingPolicy())); |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2303 | const char *argPtr = TypeString.c_str(); |
| 2304 | int paren = 0; |
| 2305 | while (*argPtr) { |
| 2306 | switch (*argPtr) { |
| 2307 | case '(': |
| 2308 | Str += *argPtr; |
| 2309 | paren++; |
| 2310 | break; |
| 2311 | case ')': |
| 2312 | Str += *argPtr; |
| 2313 | paren--; |
| 2314 | break; |
| 2315 | case '^': |
| 2316 | Str += '*'; |
| 2317 | if (paren == 1) |
| 2318 | Str += VD->getNameAsString(); |
| 2319 | break; |
| 2320 | default: |
| 2321 | Str += *argPtr; |
| 2322 | break; |
| 2323 | } |
| 2324 | argPtr++; |
| 2325 | } |
| 2326 | } |
| 2327 | |
| 2328 | |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2329 | void RewriteObjC::RewriteBlockLiteralFunctionDecl(FunctionDecl *FD) { |
| 2330 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
| 2331 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
| 2332 | const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(funcType); |
| 2333 | if (!proto) |
| 2334 | return; |
| 2335 | QualType Type = proto->getResultType(); |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2336 | std::string FdStr = Type.getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2337 | FdStr += " "; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 2338 | FdStr += FD->getName(); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2339 | FdStr += "("; |
| 2340 | unsigned numArgs = proto->getNumArgs(); |
| 2341 | for (unsigned i = 0; i < numArgs; i++) { |
| 2342 | QualType ArgType = proto->getArgType(i); |
Fariborz Jahanian | 52b2e1e | 2010-02-12 17:52:31 +0000 | [diff] [blame] | 2343 | RewriteBlockPointerType(FdStr, ArgType); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2344 | if (i+1 < numArgs) |
| 2345 | FdStr += ", "; |
| 2346 | } |
| 2347 | FdStr += ");\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2348 | InsertText(FunLocStart, FdStr); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2349 | CurFunctionDeclToDeclareForBlock = 0; |
| 2350 | } |
| 2351 | |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2352 | // SynthSuperContructorFunctionDecl - id objc_super(id obj, id super); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2353 | void RewriteObjC::SynthSuperContructorFunctionDecl() { |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2354 | if (SuperContructorFunctionDecl) |
| 2355 | return; |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2356 | IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2357 | SmallVector<QualType, 16> ArgTys; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2358 | QualType argT = Context->getObjCIdType(); |
| 2359 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2360 | ArgTys.push_back(argT); |
| 2361 | ArgTys.push_back(argT); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2362 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2363 | &ArgTys[0], ArgTys.size()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2364 | SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2365 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2366 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2367 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2368 | SC_Extern, |
| 2369 | SC_None, false); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2370 | } |
| 2371 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2372 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2373 | void RewriteObjC::SynthMsgSendFunctionDecl() { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2374 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2375 | SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2376 | QualType argT = Context->getObjCIdType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2377 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2378 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2379 | argT = Context->getObjCSelType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2380 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2381 | ArgTys.push_back(argT); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2382 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2383 | &ArgTys[0], ArgTys.size(), |
| 2384 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2385 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2386 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2387 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2388 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2389 | SC_Extern, |
| 2390 | SC_None, false); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2391 | } |
| 2392 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2393 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2394 | void RewriteObjC::SynthMsgSendSuperFunctionDecl() { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2395 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2396 | SmallVector<QualType, 16> ArgTys; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2397 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2398 | SourceLocation(), SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2399 | &Context->Idents.get("objc_super")); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2400 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2401 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2402 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2403 | argT = Context->getObjCSelType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2404 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2405 | ArgTys.push_back(argT); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2406 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2407 | &ArgTys[0], ArgTys.size(), |
| 2408 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2409 | MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2410 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2411 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2412 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2413 | SC_Extern, |
| 2414 | SC_None, false); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2415 | } |
| 2416 | |
Fariborz Jahanian | 336c8f7 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2417 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2418 | void RewriteObjC::SynthMsgSendStretFunctionDecl() { |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2419 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2420 | SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2421 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2422 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2423 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2424 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2425 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2426 | ArgTys.push_back(argT); |
Fariborz Jahanian | 336c8f7 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2427 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2428 | &ArgTys[0], ArgTys.size(), |
| 2429 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2430 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2431 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2432 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2433 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2434 | SC_Extern, |
| 2435 | SC_None, false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2436 | } |
| 2437 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2438 | // SynthMsgSendSuperStretFunctionDecl - |
Fariborz Jahanian | 336c8f7 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2439 | // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2440 | void RewriteObjC::SynthMsgSendSuperStretFunctionDecl() { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2441 | IdentifierInfo *msgSendIdent = |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2442 | &Context->Idents.get("objc_msgSendSuper_stret"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2443 | SmallVector<QualType, 16> ArgTys; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2444 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2445 | SourceLocation(), SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2446 | &Context->Idents.get("objc_super")); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2447 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2448 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2449 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2450 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2451 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2452 | ArgTys.push_back(argT); |
Fariborz Jahanian | 336c8f7 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2453 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2454 | &ArgTys[0], ArgTys.size(), |
| 2455 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2456 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2457 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2458 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2459 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2460 | SC_Extern, |
| 2461 | SC_None, false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2462 | } |
| 2463 | |
Steve Naroff | 1284db8 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2464 | // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2465 | void RewriteObjC::SynthMsgSendFpretFunctionDecl() { |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2466 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2467 | SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2468 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2469 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2470 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2471 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2472 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2473 | ArgTys.push_back(argT); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2474 | QualType msgSendType = getSimpleFunctionType(Context->DoubleTy, |
| 2475 | &ArgTys[0], ArgTys.size(), |
| 2476 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2477 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2478 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2479 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2480 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2481 | SC_Extern, |
| 2482 | SC_None, false); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2483 | } |
| 2484 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2485 | // SynthGetClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2486 | void RewriteObjC::SynthGetClassFunctionDecl() { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2487 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2488 | SmallVector<QualType, 16> ArgTys; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2489 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2490 | QualType getClassType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2491 | &ArgTys[0], ArgTys.size()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2492 | GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2493 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2494 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2495 | getClassIdent, getClassType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2496 | SC_Extern, |
| 2497 | SC_None, false); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2498 | } |
| 2499 | |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2500 | // SynthGetSuperClassFunctionDecl - Class class_getSuperclass(Class cls); |
| 2501 | void RewriteObjC::SynthGetSuperClassFunctionDecl() { |
| 2502 | IdentifierInfo *getSuperClassIdent = |
| 2503 | &Context->Idents.get("class_getSuperclass"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2504 | SmallVector<QualType, 16> ArgTys; |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2505 | ArgTys.push_back(Context->getObjCClassType()); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2506 | QualType getClassType = getSimpleFunctionType(Context->getObjCClassType(), |
| 2507 | &ArgTys[0], ArgTys.size()); |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2508 | GetSuperClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2509 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2510 | SourceLocation(), |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2511 | getSuperClassIdent, |
| 2512 | getClassType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2513 | SC_Extern, |
| 2514 | SC_None, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2515 | false); |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2516 | } |
| 2517 | |
Fariborz Jahanian | 97bbab2 | 2011-12-21 19:48:07 +0000 | [diff] [blame] | 2518 | // SynthGetMetaClassFunctionDecl - id objc_getMetaClass(const char *name); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2519 | void RewriteObjC::SynthGetMetaClassFunctionDecl() { |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2520 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2521 | SmallVector<QualType, 16> ArgTys; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2522 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2523 | QualType getClassType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2524 | &ArgTys[0], ArgTys.size()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2525 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2526 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2527 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2528 | getClassIdent, getClassType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2529 | SC_Extern, |
| 2530 | SC_None, false); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2531 | } |
| 2532 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2533 | Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2534 | QualType strType = getConstantStringStructType(); |
| 2535 | |
| 2536 | std::string S = "__NSConstantStringImpl_"; |
Steve Naroff | 7691d9b | 2008-05-31 03:35:42 +0000 | [diff] [blame] | 2537 | |
| 2538 | std::string tmpName = InFileName; |
| 2539 | unsigned i; |
| 2540 | for (i=0; i < tmpName.length(); i++) { |
| 2541 | char c = tmpName.at(i); |
| 2542 | // replace any non alphanumeric characters with '_'. |
| 2543 | if (!isalpha(c) && (c < '0' || c > '9')) |
| 2544 | tmpName[i] = '_'; |
| 2545 | } |
| 2546 | S += tmpName; |
| 2547 | S += "_"; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2548 | S += utostr(NumObjCStringLiterals++); |
| 2549 | |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2550 | Preamble += "static __NSConstantStringImpl " + S; |
| 2551 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 2552 | Preamble += "0x000007c8,"; // utf8_str |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2553 | // The pretty printer for StringLiteral handles escape characters properly. |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2554 | std::string prettyBufS; |
| 2555 | llvm::raw_string_ostream prettyBuf(prettyBufS); |
Richard Smith | d1420c6 | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 2556 | Exp->getString()->printPretty(prettyBuf, 0, PrintingPolicy(LangOpts)); |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2557 | Preamble += prettyBuf.str(); |
| 2558 | Preamble += ","; |
Steve Naroff | fd5b76f | 2009-12-06 01:48:44 +0000 | [diff] [blame] | 2559 | Preamble += utostr(Exp->getString()->getByteLength()) + "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2560 | |
| 2561 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2562 | SourceLocation(), &Context->Idents.get(S), |
| 2563 | strType, 0, SC_Static, SC_None); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2564 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, false, strType, VK_LValue, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2565 | SourceLocation()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2566 | Expr *Unop = new (Context) UnaryOperator(DRE, UO_AddrOf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2567 | Context->getPointerType(DRE->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2568 | VK_RValue, OK_Ordinary, |
| 2569 | SourceLocation()); |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2570 | // cast to NSConstantString * |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2571 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Exp->getType(), |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2572 | CK_CPointerToObjCPointerCast, Unop); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2573 | ReplaceStmt(Exp, cast); |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2574 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2575 | return cast; |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2576 | } |
| 2577 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2578 | // struct objc_super { struct objc_object *receiver; struct objc_class *super; }; |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2579 | QualType RewriteObjC::getSuperStructType() { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2580 | if (!SuperStructDecl) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2581 | SuperStructDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2582 | SourceLocation(), SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2583 | &Context->Idents.get("objc_super")); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2584 | QualType FieldTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2585 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2586 | // struct objc_object *receiver; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2587 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2588 | // struct objc_class *super; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2589 | FieldTypes[1] = Context->getObjCClassType(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2590 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2591 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2592 | for (unsigned i = 0; i < 2; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2593 | SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2594 | SourceLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2595 | SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2596 | FieldTypes[i], 0, |
| 2597 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2598 | /*Mutable=*/false, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2599 | ICIS_NoInit)); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2600 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2601 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2602 | SuperStructDecl->completeDefinition(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2603 | } |
| 2604 | return Context->getTagDeclType(SuperStructDecl); |
| 2605 | } |
| 2606 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2607 | QualType RewriteObjC::getConstantStringStructType() { |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2608 | if (!ConstantStringDecl) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2609 | ConstantStringDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2610 | SourceLocation(), SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2611 | &Context->Idents.get("__NSConstantStringImpl")); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2612 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2613 | |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2614 | // struct objc_object *receiver; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2615 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2616 | // int flags; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2617 | FieldTypes[1] = Context->IntTy; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2618 | // char *str; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2619 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2620 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2621 | FieldTypes[3] = Context->LongTy; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2622 | |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2623 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2624 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2625 | ConstantStringDecl->addDecl(FieldDecl::Create(*Context, |
| 2626 | ConstantStringDecl, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2627 | SourceLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2628 | SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2629 | FieldTypes[i], 0, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2630 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2631 | /*Mutable=*/true, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2632 | ICIS_NoInit)); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2633 | } |
| 2634 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2635 | ConstantStringDecl->completeDefinition(); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2636 | } |
| 2637 | return Context->getTagDeclType(ConstantStringDecl); |
| 2638 | } |
| 2639 | |
Fariborz Jahanian | 9fc5b00 | 2012-06-28 21:20:35 +0000 | [diff] [blame] | 2640 | CallExpr *RewriteObjC::SynthMsgSendStretCallExpr(FunctionDecl *MsgSendStretFlavor, |
| 2641 | QualType msgSendType, |
| 2642 | QualType returnType, |
| 2643 | SmallVectorImpl<QualType> &ArgTypes, |
| 2644 | SmallVectorImpl<Expr*> &MsgExprs, |
| 2645 | ObjCMethodDecl *Method) { |
| 2646 | // Create a reference to the objc_msgSend_stret() declaration. |
| 2647 | DeclRefExpr *STDRE = new (Context) DeclRefExpr(MsgSendStretFlavor, |
| 2648 | false, msgSendType, |
| 2649 | VK_LValue, SourceLocation()); |
| 2650 | // Need to cast objc_msgSend_stret to "void *" (see above comment). |
| 2651 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, |
| 2652 | Context->getPointerType(Context->VoidTy), |
| 2653 | CK_BitCast, STDRE); |
| 2654 | // Now do the "normal" pointer to function cast. |
| 2655 | QualType castType = getSimpleFunctionType(returnType, &ArgTypes[0], ArgTypes.size(), |
| 2656 | Method ? Method->isVariadic() : false); |
| 2657 | castType = Context->getPointerType(castType); |
| 2658 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
| 2659 | cast); |
| 2660 | |
| 2661 | // Don't forget the parens to enforce the proper binding. |
| 2662 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
| 2663 | |
| 2664 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2665 | CallExpr *STCE = new (Context) CallExpr(*Context, PE, MsgExprs, |
Fariborz Jahanian | 9fc5b00 | 2012-06-28 21:20:35 +0000 | [diff] [blame] | 2666 | FT->getResultType(), VK_RValue, |
| 2667 | SourceLocation()); |
| 2668 | return STCE; |
| 2669 | |
| 2670 | } |
| 2671 | |
| 2672 | |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2673 | Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp, |
| 2674 | SourceLocation StartLoc, |
| 2675 | SourceLocation EndLoc) { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2676 | if (!SelGetUidFunctionDecl) |
| 2677 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2678 | if (!MsgSendFunctionDecl) |
| 2679 | SynthMsgSendFunctionDecl(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2680 | if (!MsgSendSuperFunctionDecl) |
| 2681 | SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2682 | if (!MsgSendStretFunctionDecl) |
| 2683 | SynthMsgSendStretFunctionDecl(); |
| 2684 | if (!MsgSendSuperStretFunctionDecl) |
| 2685 | SynthMsgSendSuperStretFunctionDecl(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2686 | if (!MsgSendFpretFunctionDecl) |
| 2687 | SynthMsgSendFpretFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2688 | if (!GetClassFunctionDecl) |
| 2689 | SynthGetClassFunctionDecl(); |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2690 | if (!GetSuperClassFunctionDecl) |
| 2691 | SynthGetSuperClassFunctionDecl(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2692 | if (!GetMetaClassFunctionDecl) |
| 2693 | SynthGetMetaClassFunctionDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2694 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2695 | // default to objc_msgSend(). |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2696 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2697 | // May need to use objc_msgSend_stret() as well. |
| 2698 | FunctionDecl *MsgSendStretFlavor = 0; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2699 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
| 2700 | QualType resultType = mDecl->getResultType(); |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 2701 | if (resultType->isRecordType()) |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2702 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
Chris Lattner | 8b51fd7 | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2703 | else if (resultType->isRealFloatingType()) |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2704 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2705 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2706 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2707 | // Synthesize a call to objc_msgSend(). |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2708 | SmallVector<Expr*, 8> MsgExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2709 | switch (Exp->getReceiverKind()) { |
| 2710 | case ObjCMessageExpr::SuperClass: { |
| 2711 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2712 | if (MsgSendStretFlavor) |
| 2713 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2714 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2715 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2716 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2717 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2718 | SmallVector<Expr*, 4> InitExprs; |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2719 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2720 | // set the receiver to self, the first argument to all methods. |
| 2721 | InitExprs.push_back( |
| 2722 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2723 | CK_BitCast, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2724 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2725 | false, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2726 | Context->getObjCIdType(), |
| 2727 | VK_RValue, |
| 2728 | SourceLocation())) |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2729 | ); // set the 'receiver'. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2730 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2731 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2732 | SmallVector<Expr*, 8> ClsExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2733 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2734 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2735 | ClassDecl->getIdentifier()->getName(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2736 | StringLiteral::Ascii, false, |
| 2737 | argType, SourceLocation())); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2738 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
| 2739 | &ClsExprs[0], |
| 2740 | ClsExprs.size(), |
| 2741 | StartLoc, |
| 2742 | EndLoc); |
| 2743 | // (Class)objc_getClass("CurrentClass") |
| 2744 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 2745 | Context->getObjCClassType(), |
Fariborz Jahanian | 97bbab2 | 2011-12-21 19:48:07 +0000 | [diff] [blame] | 2746 | CK_BitCast, Cls); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2747 | ClsExprs.clear(); |
| 2748 | ClsExprs.push_back(ArgExpr); |
| 2749 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 2750 | &ClsExprs[0], ClsExprs.size(), |
| 2751 | StartLoc, EndLoc); |
| 2752 | |
| 2753 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2754 | // To turn off a warning, type-cast to 'id' |
| 2755 | InitExprs.push_back( // set 'super class', using class_getSuperclass(). |
| 2756 | NoTypeInfoCStyleCastExpr(Context, |
| 2757 | Context->getObjCIdType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2758 | CK_BitCast, Cls)); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2759 | // struct objc_super |
| 2760 | QualType superType = getSuperStructType(); |
| 2761 | Expr *SuperRep; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2762 | |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 2763 | if (LangOpts.MicrosoftExt) { |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2764 | SynthSuperContructorFunctionDecl(); |
| 2765 | // Simulate a contructor call... |
| 2766 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2767 | false, superType, VK_LValue, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2768 | SourceLocation()); |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2769 | SuperRep = new (Context) CallExpr(*Context, DRE, InitExprs, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2770 | superType, VK_LValue, |
| 2771 | SourceLocation()); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2772 | // The code for super is a little tricky to prevent collision with |
| 2773 | // the structure definition in the header. The rewriter has it's own |
| 2774 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2775 | // we need the cast below. For example: |
| 2776 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2777 | // |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2778 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2779 | Context->getPointerType(SuperRep->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2780 | VK_RValue, OK_Ordinary, |
| 2781 | SourceLocation()); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2782 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2783 | Context->getPointerType(superType), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2784 | CK_BitCast, SuperRep); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2785 | } else { |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2786 | // (struct objc_super) { <exprs from above> } |
| 2787 | InitListExpr *ILE = |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2788 | new (Context) InitListExpr(*Context, SourceLocation(), InitExprs, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2789 | SourceLocation()); |
| 2790 | TypeSourceInfo *superTInfo |
| 2791 | = Context->getTrivialTypeSourceInfo(superType); |
| 2792 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2793 | superType, VK_LValue, |
| 2794 | ILE, false); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2795 | // struct objc_super * |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2796 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2797 | Context->getPointerType(SuperRep->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2798 | VK_RValue, OK_Ordinary, |
| 2799 | SourceLocation()); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2800 | } |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2801 | MsgExprs.push_back(SuperRep); |
| 2802 | break; |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2803 | } |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2804 | |
| 2805 | case ObjCMessageExpr::Class: { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2806 | SmallVector<Expr*, 8> ClsExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2807 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2808 | ObjCInterfaceDecl *Class |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 2809 | = Exp->getClassReceiver()->getAs<ObjCObjectType>()->getInterface(); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2810 | IdentifierInfo *clsName = Class->getIdentifier(); |
| 2811 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2812 | clsName->getName(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2813 | StringLiteral::Ascii, false, |
Anders Carlsson | 3e2193c | 2011-04-14 00:40:03 +0000 | [diff] [blame] | 2814 | argType, SourceLocation())); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2815 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2816 | &ClsExprs[0], |
| 2817 | ClsExprs.size(), |
| 2818 | StartLoc, EndLoc); |
| 2819 | MsgExprs.push_back(Cls); |
| 2820 | break; |
| 2821 | } |
| 2822 | |
| 2823 | case ObjCMessageExpr::SuperInstance:{ |
| 2824 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2825 | if (MsgSendStretFlavor) |
| 2826 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2827 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 2828 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2829 | SmallVector<Expr*, 4> InitExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2830 | |
| 2831 | InitExprs.push_back( |
| 2832 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2833 | CK_BitCast, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2834 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2835 | false, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2836 | Context->getObjCIdType(), |
| 2837 | VK_RValue, SourceLocation())) |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2838 | ); // set the 'receiver'. |
| 2839 | |
| 2840 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2841 | SmallVector<Expr*, 8> ClsExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2842 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2843 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2844 | ClassDecl->getIdentifier()->getName(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2845 | StringLiteral::Ascii, false, argType, |
| 2846 | SourceLocation())); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2847 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2848 | &ClsExprs[0], |
| 2849 | ClsExprs.size(), |
| 2850 | StartLoc, EndLoc); |
| 2851 | // (Class)objc_getClass("CurrentClass") |
| 2852 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 2853 | Context->getObjCClassType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2854 | CK_BitCast, Cls); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2855 | ClsExprs.clear(); |
| 2856 | ClsExprs.push_back(ArgExpr); |
| 2857 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 2858 | &ClsExprs[0], ClsExprs.size(), |
| 2859 | StartLoc, EndLoc); |
| 2860 | |
| 2861 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2862 | // To turn off a warning, type-cast to 'id' |
| 2863 | InitExprs.push_back( |
| 2864 | // set 'super class', using class_getSuperclass(). |
| 2865 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2866 | CK_BitCast, Cls)); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2867 | // struct objc_super |
| 2868 | QualType superType = getSuperStructType(); |
| 2869 | Expr *SuperRep; |
| 2870 | |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 2871 | if (LangOpts.MicrosoftExt) { |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2872 | SynthSuperContructorFunctionDecl(); |
| 2873 | // Simulate a contructor call... |
| 2874 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2875 | false, superType, VK_LValue, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2876 | SourceLocation()); |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2877 | SuperRep = new (Context) CallExpr(*Context, DRE, InitExprs, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2878 | superType, VK_LValue, SourceLocation()); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2879 | // The code for super is a little tricky to prevent collision with |
| 2880 | // the structure definition in the header. The rewriter has it's own |
| 2881 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2882 | // we need the cast below. For example: |
| 2883 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2884 | // |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2885 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2886 | Context->getPointerType(SuperRep->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2887 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2888 | SourceLocation()); |
| 2889 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2890 | Context->getPointerType(superType), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2891 | CK_BitCast, SuperRep); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2892 | } else { |
| 2893 | // (struct objc_super) { <exprs from above> } |
| 2894 | InitListExpr *ILE = |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2895 | new (Context) InitListExpr(*Context, SourceLocation(), InitExprs, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2896 | SourceLocation()); |
| 2897 | TypeSourceInfo *superTInfo |
| 2898 | = Context->getTrivialTypeSourceInfo(superType); |
| 2899 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2900 | superType, VK_RValue, ILE, |
| 2901 | false); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2902 | } |
| 2903 | MsgExprs.push_back(SuperRep); |
| 2904 | break; |
| 2905 | } |
| 2906 | |
| 2907 | case ObjCMessageExpr::Instance: { |
| 2908 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 2909 | // Foo<Proto> *. |
| 2910 | Expr *recExpr = Exp->getInstanceReceiver(); |
| 2911 | while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr)) |
| 2912 | recExpr = CE->getSubExpr(); |
Fariborz Jahanian | baac1ea | 2011-10-07 17:17:45 +0000 | [diff] [blame] | 2913 | CastKind CK = recExpr->getType()->isObjCObjectPointerType() |
| 2914 | ? CK_BitCast : recExpr->getType()->isBlockPointerType() |
| 2915 | ? CK_BlockPointerToObjCPointerCast |
| 2916 | : CK_CPointerToObjCPointerCast; |
| 2917 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2918 | recExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
Fariborz Jahanian | baac1ea | 2011-10-07 17:17:45 +0000 | [diff] [blame] | 2919 | CK, recExpr); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2920 | MsgExprs.push_back(recExpr); |
| 2921 | break; |
| 2922 | } |
| 2923 | } |
| 2924 | |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2925 | // 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] | 2926 | SmallVector<Expr*, 8> SelExprs; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2927 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2928 | SelExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2929 | Exp->getSelector().getAsString(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2930 | StringLiteral::Ascii, false, |
| 2931 | argType, SourceLocation())); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2932 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2933 | &SelExprs[0], SelExprs.size(), |
| 2934 | StartLoc, |
| 2935 | EndLoc); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2936 | MsgExprs.push_back(SelExp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2937 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2938 | // Now push any user supplied arguments. |
| 2939 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2940 | Expr *userExpr = Exp->getArg(i); |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2941 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 2942 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 2943 | // Reuse the ICE type, it is exactly what the doctor ordered. |
Fariborz Jahanian | dff3f01 | 2011-02-26 01:31:36 +0000 | [diff] [blame] | 2944 | QualType type = ICE->getType(); |
| 2945 | if (needToScanForQualifiers(type)) |
| 2946 | type = Context->getObjCIdType(); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 2947 | // Make sure we convert "type (^)(...)" to "type (*)(...)". |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 2948 | (void)convertBlockPointerToFunctionPointer(type); |
Fariborz Jahanian | 1a38b46 | 2011-08-04 23:58:03 +0000 | [diff] [blame] | 2949 | const Expr *SubExpr = ICE->IgnoreParenImpCasts(); |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2950 | CastKind CK; |
| 2951 | if (SubExpr->getType()->isIntegralType(*Context) && |
| 2952 | type->isBooleanType()) { |
| 2953 | CK = CK_IntegralToBoolean; |
| 2954 | } else if (type->isObjCObjectPointerType()) { |
| 2955 | if (SubExpr->getType()->isBlockPointerType()) { |
| 2956 | CK = CK_BlockPointerToObjCPointerCast; |
| 2957 | } else if (SubExpr->getType()->isPointerType()) { |
| 2958 | CK = CK_CPointerToObjCPointerCast; |
| 2959 | } else { |
| 2960 | CK = CK_BitCast; |
| 2961 | } |
| 2962 | } else { |
| 2963 | CK = CK_BitCast; |
| 2964 | } |
| 2965 | |
| 2966 | userExpr = NoTypeInfoCStyleCastExpr(Context, type, CK, userExpr); |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2967 | } |
| 2968 | // Make id<P...> cast into an 'id' cast. |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2969 | else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2970 | if (CE->getType()->isObjCQualifiedIdType()) { |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2971 | while ((CE = dyn_cast<CStyleCastExpr>(userExpr))) |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2972 | userExpr = CE->getSubExpr(); |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2973 | CastKind CK; |
| 2974 | if (userExpr->getType()->isIntegralType(*Context)) { |
| 2975 | CK = CK_IntegralToPointer; |
| 2976 | } else if (userExpr->getType()->isBlockPointerType()) { |
| 2977 | CK = CK_BlockPointerToObjCPointerCast; |
| 2978 | } else if (userExpr->getType()->isPointerType()) { |
| 2979 | CK = CK_CPointerToObjCPointerCast; |
| 2980 | } else { |
| 2981 | CK = CK_BitCast; |
| 2982 | } |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2983 | userExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2984 | CK, userExpr); |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2985 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2986 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2987 | MsgExprs.push_back(userExpr); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2988 | // We've transferred the ownership to MsgExprs. For now, we *don't* null |
| 2989 | // out the argument in the original expression (since we aren't deleting |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2990 | // the ObjCMessageExpr). See RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2991 | //Exp->setArg(i, 0); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2992 | } |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2993 | // Generate the funky cast. |
| 2994 | CastExpr *cast; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2995 | SmallVector<QualType, 8> ArgTypes; |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2996 | QualType returnType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2997 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2998 | // Push 'id' and 'SEL', the 2 implicit arguments. |
Steve Naroff | c3a438c | 2007-11-15 10:43:57 +0000 | [diff] [blame] | 2999 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 3000 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 3001 | else |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3002 | ArgTypes.push_back(Context->getObjCIdType()); |
| 3003 | ArgTypes.push_back(Context->getObjCSelType()); |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3004 | if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) { |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3005 | // Push any user argument types. |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3006 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 3007 | E = OMD->param_end(); PI != E; ++PI) { |
| 3008 | QualType t = (*PI)->getType()->isObjCQualifiedIdType() |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3009 | ? Context->getObjCIdType() |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3010 | : (*PI)->getType(); |
Steve Naroff | a206b06 | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 3011 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3012 | (void)convertBlockPointerToFunctionPointer(t); |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 3013 | ArgTypes.push_back(t); |
| 3014 | } |
Fariborz Jahanian | 3a448fb | 2011-09-10 17:01:56 +0000 | [diff] [blame] | 3015 | returnType = Exp->getType(); |
| 3016 | convertToUnqualifiedObjCType(returnType); |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3017 | (void)convertBlockPointerToFunctionPointer(returnType); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3018 | } else { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3019 | returnType = Context->getObjCIdType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3020 | } |
| 3021 | // 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] | 3022 | QualType msgSendType = MsgSendFlavor->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3023 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3024 | // Create a reference to the objc_msgSend() declaration. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3025 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, false, msgSendType, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3026 | VK_LValue, SourceLocation()); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3027 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3028 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3029 | // If we don't do this cast, we get the following bizarre warning/note: |
| 3030 | // xx.m:13: warning: function called through a non-compatible type |
| 3031 | // 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] | 3032 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 3033 | Context->getPointerType(Context->VoidTy), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3034 | CK_BitCast, DRE); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3035 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3036 | // Now do the "normal" pointer to function cast. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3037 | QualType castType = |
| 3038 | getSimpleFunctionType(returnType, &ArgTypes[0], ArgTypes.size(), |
| 3039 | // If we don't have a method decl, force a variadic cast. |
| 3040 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3041 | castType = Context->getPointerType(castType); |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3042 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3043 | cast); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3044 | |
| 3045 | // Don't forget the parens to enforce the proper binding. |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 3046 | ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3047 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3048 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3049 | CallExpr *CE = new (Context) CallExpr(*Context, PE, MsgExprs, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3050 | FT->getResultType(), VK_RValue, |
| 3051 | EndLoc); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3052 | Stmt *ReplacingStmt = CE; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3053 | if (MsgSendStretFlavor) { |
| 3054 | // We have the method which returns a struct/union. Must also generate |
| 3055 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 3056 | // expression which dictate which one to envoke depending on size of |
| 3057 | // method's return type. |
Fariborz Jahanian | 9fc5b00 | 2012-06-28 21:20:35 +0000 | [diff] [blame] | 3058 | |
| 3059 | CallExpr *STCE = SynthMsgSendStretCallExpr(MsgSendStretFlavor, |
| 3060 | msgSendType, returnType, |
| 3061 | ArgTypes, MsgExprs, |
| 3062 | Exp->getMethodDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3063 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3064 | // Build sizeof(returnType) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3065 | UnaryExprOrTypeTraitExpr *sizeofExpr = |
| 3066 | new (Context) UnaryExprOrTypeTraitExpr(UETT_SizeOf, |
| 3067 | Context->getTrivialTypeSourceInfo(returnType), |
| 3068 | Context->getSizeType(), SourceLocation(), |
| 3069 | SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3070 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 3071 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 3072 | // For X86 it is more complicated and some kind of target specific routine |
| 3073 | // is needed to decide what to do. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3074 | unsigned IntSize = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3075 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 3076 | IntegerLiteral *limit = IntegerLiteral::Create(*Context, |
| 3077 | llvm::APInt(IntSize, 8), |
| 3078 | Context->IntTy, |
| 3079 | SourceLocation()); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3080 | BinaryOperator *lessThanExpr = |
| 3081 | new (Context) BinaryOperator(sizeofExpr, limit, BO_LE, Context->IntTy, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 3082 | VK_RValue, OK_Ordinary, SourceLocation(), |
| 3083 | false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3084 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3085 | ConditionalOperator *CondExpr = |
Douglas Gregor | 47e1f7c | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 3086 | new (Context) ConditionalOperator(lessThanExpr, |
| 3087 | SourceLocation(), CE, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3088 | SourceLocation(), STCE, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3089 | returnType, VK_RValue, OK_Ordinary); |
Fariborz Jahanian | f9b949f | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 3090 | ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3091 | CondExpr); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3092 | } |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3093 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3094 | return ReplacingStmt; |
| 3095 | } |
| 3096 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3097 | Stmt *RewriteObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 3098 | Stmt *ReplacingStmt = SynthMessageExpr(Exp, Exp->getLocStart(), |
| 3099 | Exp->getLocEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3100 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 3101 | // Now do the actual rewrite. |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 3102 | ReplaceStmt(Exp, ReplacingStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3103 | |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3104 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3105 | return ReplacingStmt; |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 3106 | } |
| 3107 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3108 | // typedef struct objc_object Protocol; |
| 3109 | QualType RewriteObjC::getProtocolType() { |
| 3110 | if (!ProtocolTypeDecl) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3111 | TypeSourceInfo *TInfo |
| 3112 | = Context->getTrivialTypeSourceInfo(Context->getObjCIdType()); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3113 | ProtocolTypeDecl = TypedefDecl::Create(*Context, TUDecl, |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 3114 | SourceLocation(), SourceLocation(), |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3115 | &Context->Idents.get("Protocol"), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3116 | TInfo); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3117 | } |
| 3118 | return Context->getTypeDeclType(ProtocolTypeDecl); |
| 3119 | } |
| 3120 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 3121 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3122 | /// a synthesized/forward data reference (to the protocol's metadata). |
| 3123 | /// The forward references (and metadata) are generated in |
| 3124 | /// RewriteObjC::HandleTranslationUnit(). |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3125 | Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3126 | std::string Name = "_OBJC_PROTOCOL_" + Exp->getProtocol()->getNameAsString(); |
| 3127 | IdentifierInfo *ID = &Context->Idents.get(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3128 | VarDecl *VD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3129 | SourceLocation(), ID, getProtocolType(), 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3130 | SC_Extern, SC_None); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3131 | DeclRefExpr *DRE = new (Context) DeclRefExpr(VD, false, getProtocolType(), |
| 3132 | VK_LValue, SourceLocation()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3133 | Expr *DerefExpr = new (Context) UnaryOperator(DRE, UO_AddrOf, |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3134 | Context->getPointerType(DRE->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3135 | VK_RValue, OK_Ordinary, SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3136 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, DerefExpr->getType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3137 | CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3138 | DerefExpr); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3139 | ReplaceStmt(Exp, castExpr); |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 3140 | ProtocolExprDecls.insert(Exp->getProtocol()->getCanonicalDecl()); |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3141 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3142 | return castExpr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3143 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 3144 | } |
| 3145 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3146 | bool RewriteObjC::BufferContainsPPDirectives(const char *startBuf, |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3147 | const char *endBuf) { |
| 3148 | while (startBuf < endBuf) { |
| 3149 | if (*startBuf == '#') { |
| 3150 | // Skip whitespace. |
| 3151 | for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf) |
| 3152 | ; |
| 3153 | if (!strncmp(startBuf, "if", strlen("if")) || |
| 3154 | !strncmp(startBuf, "ifdef", strlen("ifdef")) || |
| 3155 | !strncmp(startBuf, "ifndef", strlen("ifndef")) || |
| 3156 | !strncmp(startBuf, "define", strlen("define")) || |
| 3157 | !strncmp(startBuf, "undef", strlen("undef")) || |
| 3158 | !strncmp(startBuf, "else", strlen("else")) || |
| 3159 | !strncmp(startBuf, "elif", strlen("elif")) || |
| 3160 | !strncmp(startBuf, "endif", strlen("endif")) || |
| 3161 | !strncmp(startBuf, "pragma", strlen("pragma")) || |
| 3162 | !strncmp(startBuf, "include", strlen("include")) || |
| 3163 | !strncmp(startBuf, "import", strlen("import")) || |
| 3164 | !strncmp(startBuf, "include_next", strlen("include_next"))) |
| 3165 | return true; |
| 3166 | } |
| 3167 | startBuf++; |
| 3168 | } |
| 3169 | return false; |
| 3170 | } |
| 3171 | |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 3172 | /// RewriteObjCInternalStruct - Rewrite one internal struct corresponding to |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3173 | /// an objective-c class with ivars. |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 3174 | void RewriteObjC::RewriteObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3175 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3176 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3177 | assert(CDecl->getName() != "" && |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 3178 | "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 3179 | // Do not synthesize more than once. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3180 | if (ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 3181 | return; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3182 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3183 | int NumIvars = CDecl->ivar_size(); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3184 | SourceLocation LocStart = CDecl->getLocStart(); |
Douglas Gregor | 05c272f | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 3185 | SourceLocation LocEnd = CDecl->getEndOfDefinitionLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3186 | |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3187 | const char *startBuf = SM->getCharacterData(LocStart); |
| 3188 | const char *endBuf = SM->getCharacterData(LocEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3189 | |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3190 | // If no ivars and no root or if its root, directly or indirectly, |
| 3191 | // 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] | 3192 | if ((!CDecl->isThisDeclarationADefinition() || NumIvars == 0) && |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3193 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3194 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3195 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3196 | return; |
| 3197 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3198 | |
| 3199 | // FIXME: This has potential of causing problem. If |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3200 | // SynthesizeObjCInternalStruct is ever called recursively. |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3201 | Result += "\nstruct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3202 | Result += CDecl->getNameAsString(); |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 3203 | if (LangOpts.MicrosoftExt) |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 3204 | Result += "_IMPL"; |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 3205 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3206 | if (NumIvars > 0) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3207 | const char *cursor = strchr(startBuf, '{'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3208 | assert((cursor && endBuf) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3209 | && "SynthesizeObjCInternalStruct - malformed @interface"); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3210 | // If the buffer contains preprocessor directives, we do more fine-grained |
| 3211 | // rewrites. This is intended to fix code that looks like (which occurs in |
| 3212 | // NSURL.h, for example): |
| 3213 | // |
| 3214 | // #ifdef XYZ |
| 3215 | // @interface Foo : NSObject |
| 3216 | // #else |
| 3217 | // @interface FooBar : NSObject |
| 3218 | // #endif |
| 3219 | // { |
| 3220 | // int i; |
| 3221 | // } |
| 3222 | // @end |
| 3223 | // |
| 3224 | // This clause is segregated to avoid breaking the common case. |
| 3225 | if (BufferContainsPPDirectives(startBuf, cursor)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3226 | SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() : |
Argyrios Kyrtzidis | 1711fc9 | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 3227 | CDecl->getAtStartLoc(); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3228 | const char *endHeader = SM->getCharacterData(L); |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3229 | endHeader += Lexer::MeasureTokenLength(L, *SM, LangOpts); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3230 | |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3231 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3232 | // advance to the end of the referenced protocols. |
| 3233 | while (endHeader < cursor && *endHeader != '>') endHeader++; |
| 3234 | endHeader++; |
| 3235 | } |
| 3236 | // rewrite the original header |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3237 | ReplaceText(LocStart, endHeader-startBuf, Result); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3238 | } else { |
| 3239 | // rewrite the original header *without* disturbing the '{' |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3240 | ReplaceText(LocStart, cursor-startBuf, Result); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3241 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3242 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3243 | Result = "\n struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3244 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 39bbd9f | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 3245 | Result += "_IMPL "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3246 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 819173c | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 3247 | Result += "_IVARS;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3248 | |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3249 | // insert the super class structure definition. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 3250 | SourceLocation OnePastCurly = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3251 | LocStart.getLocWithOffset(cursor-startBuf+1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3252 | InsertText(OnePastCurly, Result); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3253 | } |
| 3254 | cursor++; // past '{' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3255 | |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3256 | // Now comment out any visibility specifiers. |
| 3257 | while (cursor < endBuf) { |
| 3258 | if (*cursor == '@') { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3259 | SourceLocation atLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Chris Lattner | df6a51b | 2007-11-14 22:57:51 +0000 | [diff] [blame] | 3260 | // Skip whitespace. |
| 3261 | for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor) |
| 3262 | /*scan*/; |
| 3263 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3264 | // FIXME: presence of @public, etc. inside comment results in |
| 3265 | // this transformation as well, which is still correct c-code. |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3266 | if (!strncmp(cursor, "public", strlen("public")) || |
| 3267 | !strncmp(cursor, "private", strlen("private")) || |
Steve Naroff | c5e3277 | 2008-04-04 22:34:24 +0000 | [diff] [blame] | 3268 | !strncmp(cursor, "package", strlen("package")) || |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3269 | !strncmp(cursor, "protected", strlen("protected"))) |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3270 | InsertText(atLoc, "// "); |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3271 | } |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3272 | // FIXME: If there are cases where '<' is used in ivar declaration part |
| 3273 | // of user code, then scan the ivar list and use needToScanForQualifiers |
| 3274 | // for type checking. |
| 3275 | else if (*cursor == '<') { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3276 | SourceLocation atLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3277 | InsertText(atLoc, "/* "); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3278 | cursor = strchr(cursor, '>'); |
| 3279 | cursor++; |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3280 | atLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3281 | InsertText(atLoc, " */"); |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3282 | } else if (*cursor == '^') { // rewrite block specifier. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3283 | SourceLocation caretLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3284 | ReplaceText(caretLoc, 1, "*"); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3285 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3286 | cursor++; |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3287 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3288 | // Don't forget to add a ';'!! |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3289 | InsertText(LocEnd.getLocWithOffset(1), ";"); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3290 | } else { // we don't have any instance variables - insert super struct. |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3291 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3292 | Result += " {\n struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3293 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 39bbd9f | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 3294 | Result += "_IMPL "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3295 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 819173c | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 3296 | Result += "_IVARS;\n};\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3297 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3298 | } |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3299 | // Mark this struct as having been generated. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3300 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3301 | llvm_unreachable("struct already synthesize- SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3302 | } |
| 3303 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3304 | //===----------------------------------------------------------------------===// |
| 3305 | // Meta Data Emission |
| 3306 | //===----------------------------------------------------------------------===// |
| 3307 | |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3308 | |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3309 | /// RewriteImplementations - This routine rewrites all method implementations |
| 3310 | /// and emits meta-data. |
| 3311 | |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3312 | void RewriteObjC::RewriteImplementations() { |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3313 | int ClsDefCount = ClassImplementation.size(); |
| 3314 | int CatDefCount = CategoryImplementation.size(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3315 | |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3316 | // Rewrite implemented methods |
| 3317 | for (int i = 0; i < ClsDefCount; i++) |
| 3318 | RewriteImplementationDecl(ClassImplementation[i]); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3319 | |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 3320 | for (int i = 0; i < CatDefCount; i++) |
| 3321 | RewriteImplementationDecl(CategoryImplementation[i]); |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3322 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3323 | |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3324 | void RewriteObjC::RewriteByRefString(std::string &ResultStr, |
| 3325 | const std::string &Name, |
Fariborz Jahanian | 1e8011e | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 3326 | ValueDecl *VD, bool def) { |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3327 | assert(BlockByRefDeclNo.count(VD) && |
| 3328 | "RewriteByRefString: ByRef decl missing"); |
Fariborz Jahanian | 1e8011e | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 3329 | if (def) |
| 3330 | ResultStr += "struct "; |
| 3331 | ResultStr += "__Block_byref_" + Name + |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3332 | "_" + utostr(BlockByRefDeclNo[VD]) ; |
| 3333 | } |
| 3334 | |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3335 | static bool HasLocalVariableExternalStorage(ValueDecl *VD) { |
| 3336 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 3337 | return (Var->isFunctionOrMethodVarDecl() && !Var->hasLocalStorage()); |
| 3338 | return false; |
| 3339 | } |
| 3340 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3341 | std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3342 | StringRef funcName, |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3343 | std::string Tag) { |
| 3344 | const FunctionType *AFT = CE->getFunctionType(); |
| 3345 | QualType RT = AFT->getResultType(); |
| 3346 | std::string StructRef = "struct " + Tag; |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 3347 | std::string S = "static " + RT.getAsString(Context->getPrintingPolicy()) + " __" + |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3348 | funcName.str() + "_" + "block_func_" + utostr(i); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 3349 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3350 | BlockDecl *BD = CE->getBlockDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3351 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3352 | if (isa<FunctionNoProtoType>(AFT)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3353 | // 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] | 3354 | // block (to reference imported block decl refs). |
| 3355 | S += "(" + StructRef + " *__cself)"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3356 | } else if (BD->param_empty()) { |
| 3357 | S += "(" + StructRef + " *__cself)"; |
| 3358 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3359 | const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3360 | assert(FT && "SynthesizeBlockFunc: No function proto"); |
| 3361 | S += '('; |
| 3362 | // first add the implicit argument. |
| 3363 | S += StructRef + " *__cself, "; |
| 3364 | std::string ParamStr; |
| 3365 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 3366 | E = BD->param_end(); AI != E; ++AI) { |
| 3367 | if (AI != BD->param_begin()) S += ", "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3368 | ParamStr = (*AI)->getNameAsString(); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3369 | QualType QT = (*AI)->getType(); |
Fariborz Jahanian | 2610f90 | 2012-03-27 16:42:20 +0000 | [diff] [blame] | 3370 | (void)convertBlockPointerToFunctionPointer(QT); |
| 3371 | QT.getAsStringInternal(ParamStr, Context->getPrintingPolicy()); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3372 | S += ParamStr; |
| 3373 | } |
| 3374 | if (FT->isVariadic()) { |
| 3375 | if (!BD->param_empty()) S += ", "; |
| 3376 | S += "..."; |
| 3377 | } |
| 3378 | S += ')'; |
| 3379 | } |
| 3380 | S += " {\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3381 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3382 | // Create local declarations to avoid rewriting all closure decl ref exprs. |
| 3383 | // First, emit a declaration for all "by ref" decls. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3384 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3385 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3386 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3387 | std::string Name = (*I)->getNameAsString(); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3388 | std::string TypeString; |
| 3389 | RewriteByRefString(TypeString, Name, (*I)); |
| 3390 | TypeString += " *"; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 3391 | Name = TypeString + Name; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3392 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3393 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3394 | // Next, emit a declaration for all "by copy" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3395 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3396 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3397 | S += " "; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3398 | // Handle nested closure invocation. For example: |
| 3399 | // |
| 3400 | // void (^myImportedClosure)(void); |
| 3401 | // myImportedClosure = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3402 | // |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3403 | // void (^anotherClosure)(void); |
| 3404 | // anotherClosure = ^(void) { |
| 3405 | // myImportedClosure(); // import and invoke the closure |
| 3406 | // }; |
| 3407 | // |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 3408 | if (isTopLevelBlockPointerType((*I)->getType())) { |
| 3409 | RewriteBlockPointerTypeVariable(S, (*I)); |
| 3410 | S += " = ("; |
| 3411 | RewriteBlockPointerType(S, (*I)->getType()); |
| 3412 | S += ")"; |
| 3413 | S += "__cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; |
| 3414 | } |
| 3415 | else { |
Fariborz Jahanian | 210c248 | 2010-02-16 17:26:03 +0000 | [diff] [blame] | 3416 | std::string Name = (*I)->getNameAsString(); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3417 | QualType QT = (*I)->getType(); |
| 3418 | if (HasLocalVariableExternalStorage(*I)) |
| 3419 | QT = Context->getPointerType(QT); |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 3420 | QT.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 3421 | S += Name + " = __cself->" + |
| 3422 | (*I)->getNameAsString() + "; // bound by copy\n"; |
| 3423 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3424 | } |
| 3425 | std::string RewrittenStr = RewrittenBlockExprs[CE]; |
| 3426 | const char *cstr = RewrittenStr.c_str(); |
| 3427 | while (*cstr++ != '{') ; |
| 3428 | S += cstr; |
| 3429 | S += "\n"; |
| 3430 | return S; |
| 3431 | } |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 3432 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3433 | std::string RewriteObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3434 | StringRef funcName, |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3435 | std::string Tag) { |
| 3436 | std::string StructRef = "struct " + Tag; |
| 3437 | std::string S = "static void __"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3438 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3439 | S += funcName; |
| 3440 | S += "_block_copy_" + utostr(i); |
| 3441 | S += "(" + StructRef; |
| 3442 | S += "*dst, " + StructRef; |
| 3443 | S += "*src) {"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3444 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3445 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3446 | ValueDecl *VD = (*I); |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3447 | S += "_Block_object_assign((void*)&dst->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3448 | S += (*I)->getNameAsString(); |
Steve Naroff | 47a2422 | 2008-12-11 20:51:38 +0000 | [diff] [blame] | 3449 | S += ", (void*)src->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3450 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 3451 | if (BlockByRefDeclsPtrSet.count((*I))) |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 3452 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3453 | else if (VD->getType()->isBlockPointerType()) |
Fariborz Jahanian | 06433c6 | 2011-07-30 01:07:55 +0000 | [diff] [blame] | 3454 | S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);"; |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3455 | else |
| 3456 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3457 | } |
Fariborz Jahanian | d25d1b5 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 3458 | S += "}\n"; |
| 3459 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3460 | S += "\nstatic void __"; |
| 3461 | S += funcName; |
| 3462 | S += "_block_dispose_" + utostr(i); |
| 3463 | S += "(" + StructRef; |
| 3464 | S += "*src) {"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3465 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3466 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3467 | ValueDecl *VD = (*I); |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3468 | S += "_Block_object_dispose((void*)src->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3469 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 3470 | if (BlockByRefDeclsPtrSet.count((*I))) |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 3471 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3472 | else if (VD->getType()->isBlockPointerType()) |
Fariborz Jahanian | 06433c6 | 2011-07-30 01:07:55 +0000 | [diff] [blame] | 3473 | S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);"; |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 3474 | else |
| 3475 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3476 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3477 | S += "}\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3478 | return S; |
| 3479 | } |
| 3480 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3481 | std::string RewriteObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 3482 | std::string Desc) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3483 | std::string S = "\nstruct " + Tag; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3484 | std::string Constructor = " " + Tag; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3485 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3486 | S += " {\n struct __block_impl impl;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3487 | S += " struct " + Desc; |
| 3488 | S += "* Desc;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3489 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3490 | Constructor += "(void *fp, "; // Invoke function pointer. |
| 3491 | Constructor += "struct " + Desc; // Descriptor pointer. |
| 3492 | Constructor += " *desc"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3493 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3494 | if (BlockDeclRefs.size()) { |
| 3495 | // Output all "by copy" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3496 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3497 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3498 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3499 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3500 | std::string ArgName = "_" + FieldName; |
| 3501 | // Handle nested closure invocation. For example: |
| 3502 | // |
| 3503 | // void (^myImportedBlock)(void); |
| 3504 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3505 | // |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3506 | // void (^anotherBlock)(void); |
| 3507 | // anotherBlock = ^(void) { |
| 3508 | // myImportedBlock(); // import and invoke the closure |
| 3509 | // }; |
| 3510 | // |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3511 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3512 | S += "struct __block_impl *"; |
| 3513 | Constructor += ", void *" + ArgName; |
| 3514 | } else { |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3515 | QualType QT = (*I)->getType(); |
| 3516 | if (HasLocalVariableExternalStorage(*I)) |
| 3517 | QT = Context->getPointerType(QT); |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 3518 | QT.getAsStringInternal(FieldName, Context->getPrintingPolicy()); |
| 3519 | QT.getAsStringInternal(ArgName, Context->getPrintingPolicy()); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3520 | Constructor += ", " + ArgName; |
| 3521 | } |
| 3522 | S += FieldName + ";\n"; |
| 3523 | } |
| 3524 | // Output all "by ref" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3525 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3526 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3527 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3528 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3529 | std::string ArgName = "_" + FieldName; |
Fariborz Jahanian | 651ba52 | 2011-04-01 23:08:13 +0000 | [diff] [blame] | 3530 | { |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3531 | std::string TypeString; |
| 3532 | RewriteByRefString(TypeString, FieldName, (*I)); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 3533 | TypeString += " *"; |
| 3534 | FieldName = TypeString + FieldName; |
| 3535 | ArgName = TypeString + ArgName; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3536 | Constructor += ", " + ArgName; |
| 3537 | } |
| 3538 | S += FieldName + "; // by ref\n"; |
| 3539 | } |
| 3540 | // Finish writing the constructor. |
Fariborz Jahanian | 20432ef | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 3541 | Constructor += ", int flags=0)"; |
| 3542 | // Initialize all "by copy" arguments. |
| 3543 | bool firsTime = true; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3544 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Fariborz Jahanian | 20432ef | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 3545 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3546 | std::string Name = (*I)->getNameAsString(); |
| 3547 | if (firsTime) { |
| 3548 | Constructor += " : "; |
| 3549 | firsTime = false; |
| 3550 | } |
| 3551 | else |
| 3552 | Constructor += ", "; |
| 3553 | if (isTopLevelBlockPointerType((*I)->getType())) |
| 3554 | Constructor += Name + "((struct __block_impl *)_" + Name + ")"; |
| 3555 | else |
| 3556 | Constructor += Name + "(_" + Name + ")"; |
| 3557 | } |
| 3558 | // Initialize all "by ref" arguments. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3559 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Fariborz Jahanian | 20432ef | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 3560 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3561 | std::string Name = (*I)->getNameAsString(); |
| 3562 | if (firsTime) { |
| 3563 | Constructor += " : "; |
| 3564 | firsTime = false; |
| 3565 | } |
| 3566 | else |
| 3567 | Constructor += ", "; |
Fariborz Jahanian | 651ba52 | 2011-04-01 23:08:13 +0000 | [diff] [blame] | 3568 | Constructor += Name + "(_" + Name + "->__forwarding)"; |
Fariborz Jahanian | 20432ef | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 3569 | } |
| 3570 | |
| 3571 | Constructor += " {\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3572 | if (GlobalVarDecl) |
| 3573 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 3574 | else |
| 3575 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3576 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3577 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3578 | Constructor += " Desc = desc;\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3579 | } else { |
| 3580 | // Finish writing the constructor. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3581 | Constructor += ", int flags=0) {\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3582 | if (GlobalVarDecl) |
| 3583 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 3584 | else |
| 3585 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3586 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 3587 | Constructor += " Desc = desc;\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3588 | } |
| 3589 | Constructor += " "; |
| 3590 | Constructor += "}\n"; |
| 3591 | S += Constructor; |
| 3592 | S += "};\n"; |
| 3593 | return S; |
| 3594 | } |
| 3595 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3596 | std::string RewriteObjC::SynthesizeBlockDescriptor(std::string DescTag, |
| 3597 | std::string ImplTag, int i, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3598 | StringRef FunName, |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3599 | unsigned hasCopy) { |
| 3600 | std::string S = "\nstatic struct " + DescTag; |
| 3601 | |
| 3602 | S += " {\n unsigned long reserved;\n"; |
| 3603 | S += " unsigned long Block_size;\n"; |
| 3604 | if (hasCopy) { |
Fariborz Jahanian | 4fcc4fd | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 3605 | S += " void (*copy)(struct "; |
| 3606 | S += ImplTag; S += "*, struct "; |
| 3607 | S += ImplTag; S += "*);\n"; |
| 3608 | |
| 3609 | S += " void (*dispose)(struct "; |
| 3610 | S += ImplTag; S += "*);\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3611 | } |
| 3612 | S += "} "; |
| 3613 | |
| 3614 | S += DescTag + "_DATA = { 0, sizeof(struct "; |
| 3615 | S += ImplTag + ")"; |
| 3616 | if (hasCopy) { |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3617 | S += ", __" + FunName.str() + "_block_copy_" + utostr(i); |
| 3618 | S += ", __" + FunName.str() + "_block_dispose_" + utostr(i); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3619 | } |
| 3620 | S += "};\n"; |
| 3621 | return S; |
| 3622 | } |
| 3623 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3624 | void RewriteObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3625 | StringRef FunName) { |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 3626 | // Insert declaration for the function in which block literal is used. |
Fariborz Jahanian | bf07012 | 2010-01-15 18:14:52 +0000 | [diff] [blame] | 3627 | if (CurFunctionDeclToDeclareForBlock && !Blocks.empty()) |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 3628 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Fariborz Jahanian | 8611eb0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 3629 | bool RewriteSC = (GlobalVarDecl && |
| 3630 | !Blocks.empty() && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3631 | GlobalVarDecl->getStorageClass() == SC_Static && |
Fariborz Jahanian | 8611eb0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 3632 | GlobalVarDecl->getType().getCVRQualifiers()); |
| 3633 | if (RewriteSC) { |
| 3634 | std::string SC(" void __"); |
| 3635 | SC += GlobalVarDecl->getNameAsString(); |
| 3636 | SC += "() {}"; |
| 3637 | InsertText(FunLocStart, SC); |
| 3638 | } |
| 3639 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3640 | // Insert closures that were part of the function. |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3641 | for (unsigned i = 0, count=0; i < Blocks.size(); i++) { |
| 3642 | CollectBlockDeclRefInfo(Blocks[i]); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3643 | // Need to copy-in the inner copied-in variables not actually used in this |
| 3644 | // block. |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3645 | for (int j = 0; j < InnerDeclRefsCount[i]; j++) { |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3646 | DeclRefExpr *Exp = InnerDeclRefs[count++]; |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3647 | ValueDecl *VD = Exp->getDecl(); |
| 3648 | BlockDeclRefs.push_back(Exp); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3649 | if (!VD->hasAttr<BlocksAttr>() && !BlockByCopyDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3650 | BlockByCopyDeclsPtrSet.insert(VD); |
| 3651 | BlockByCopyDecls.push_back(VD); |
| 3652 | } |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3653 | if (VD->hasAttr<BlocksAttr>() && !BlockByRefDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3654 | BlockByRefDeclsPtrSet.insert(VD); |
| 3655 | BlockByRefDecls.push_back(VD); |
| 3656 | } |
Fariborz Jahanian | 92c8568 | 2010-10-05 18:05:06 +0000 | [diff] [blame] | 3657 | // imported objects in the inner blocks not used in the outer |
| 3658 | // blocks must be copied/disposed in the outer block as well. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3659 | if (VD->hasAttr<BlocksAttr>() || |
Fariborz Jahanian | 92c8568 | 2010-10-05 18:05:06 +0000 | [diff] [blame] | 3660 | VD->getType()->isObjCObjectPointerType() || |
| 3661 | VD->getType()->isBlockPointerType()) |
| 3662 | ImportedBlockDecls.insert(VD); |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3663 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3664 | |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3665 | std::string ImplTag = "__" + FunName.str() + "_block_impl_" + utostr(i); |
| 3666 | std::string DescTag = "__" + FunName.str() + "_block_desc_" + utostr(i); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3667 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3668 | std::string CI = SynthesizeBlockImpl(Blocks[i], ImplTag, DescTag); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3669 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3670 | InsertText(FunLocStart, CI); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3671 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3672 | std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, ImplTag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3673 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3674 | InsertText(FunLocStart, CF); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3675 | |
| 3676 | if (ImportedBlockDecls.size()) { |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3677 | std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, ImplTag); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3678 | InsertText(FunLocStart, HF); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3679 | } |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3680 | std::string BD = SynthesizeBlockDescriptor(DescTag, ImplTag, i, FunName, |
| 3681 | ImportedBlockDecls.size() > 0); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3682 | InsertText(FunLocStart, BD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3683 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3684 | BlockDeclRefs.clear(); |
| 3685 | BlockByRefDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 3686 | BlockByRefDeclsPtrSet.clear(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3687 | BlockByCopyDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 3688 | BlockByCopyDeclsPtrSet.clear(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3689 | ImportedBlockDecls.clear(); |
| 3690 | } |
Fariborz Jahanian | 8611eb0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 3691 | if (RewriteSC) { |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3692 | // Must insert any 'const/volatile/static here. Since it has been |
| 3693 | // removed as result of rewriting of block literals. |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3694 | std::string SC; |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3695 | if (GlobalVarDecl->getStorageClass() == SC_Static) |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3696 | SC = "static "; |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3697 | if (GlobalVarDecl->getType().isConstQualified()) |
| 3698 | SC += "const "; |
| 3699 | if (GlobalVarDecl->getType().isVolatileQualified()) |
| 3700 | SC += "volatile "; |
Fariborz Jahanian | 8611eb0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 3701 | if (GlobalVarDecl->getType().isRestrictQualified()) |
| 3702 | SC += "restrict "; |
| 3703 | InsertText(FunLocStart, SC); |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 3704 | } |
| 3705 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3706 | Blocks.clear(); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3707 | InnerDeclRefsCount.clear(); |
| 3708 | InnerDeclRefs.clear(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3709 | RewrittenBlockExprs.clear(); |
| 3710 | } |
| 3711 | |
| 3712 | void RewriteObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { |
| 3713 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3714 | StringRef FuncName = FD->getName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3715 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3716 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 3717 | } |
| 3718 | |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 3719 | static void BuildUniqueMethodName(std::string &Name, |
| 3720 | ObjCMethodDecl *MD) { |
| 3721 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3722 | Name = IFace->getName(); |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 3723 | Name += "__" + MD->getSelector().getAsString(); |
| 3724 | // Convert colons to underscores. |
| 3725 | std::string::size_type loc = 0; |
| 3726 | while ((loc = Name.find(":", loc)) != std::string::npos) |
| 3727 | Name.replace(loc, 1, "_"); |
| 3728 | } |
| 3729 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3730 | void RewriteObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3731 | //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n"); |
| 3732 | //SourceLocation FunLocStart = MD->getLocStart(); |
Fariborz Jahanian | 0e1c99a | 2010-01-29 01:55:49 +0000 | [diff] [blame] | 3733 | SourceLocation FunLocStart = MD->getLocStart(); |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 3734 | std::string FuncName; |
| 3735 | BuildUniqueMethodName(FuncName, MD); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3736 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3737 | } |
| 3738 | |
| 3739 | void RewriteObjC::GetBlockDeclRefExprs(Stmt *S) { |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 3740 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3741 | if (*CI) { |
| 3742 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 3743 | GetBlockDeclRefExprs(CBE->getBody()); |
| 3744 | else |
| 3745 | GetBlockDeclRefExprs(*CI); |
| 3746 | } |
| 3747 | // Handle specific things. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3748 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 3749 | if (DRE->refersToEnclosingLocal()) { |
| 3750 | // FIXME: Handle enums. |
| 3751 | if (!isa<FunctionDecl>(DRE->getDecl())) |
| 3752 | BlockDeclRefs.push_back(DRE); |
| 3753 | if (HasLocalVariableExternalStorage(DRE->getDecl())) |
| 3754 | BlockDeclRefs.push_back(DRE); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3755 | } |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3756 | } |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3757 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3758 | return; |
| 3759 | } |
| 3760 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3761 | void RewriteObjC::GetInnerBlockDeclRefExprs(Stmt *S, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3762 | SmallVector<DeclRefExpr *, 8> &InnerBlockDeclRefs, |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3763 | llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts) { |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 3764 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3765 | if (*CI) { |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3766 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) { |
| 3767 | InnerContexts.insert(cast<DeclContext>(CBE->getBlockDecl())); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3768 | GetInnerBlockDeclRefExprs(CBE->getBody(), |
| 3769 | InnerBlockDeclRefs, |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3770 | InnerContexts); |
| 3771 | } |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3772 | else |
| 3773 | GetInnerBlockDeclRefExprs(*CI, |
| 3774 | InnerBlockDeclRefs, |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3775 | InnerContexts); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3776 | |
| 3777 | } |
| 3778 | // Handle specific things. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3779 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 3780 | if (DRE->refersToEnclosingLocal()) { |
| 3781 | if (!isa<FunctionDecl>(DRE->getDecl()) && |
| 3782 | !InnerContexts.count(DRE->getDecl()->getDeclContext())) |
| 3783 | InnerBlockDeclRefs.push_back(DRE); |
| 3784 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) |
| 3785 | if (Var->isFunctionOrMethodVarDecl()) |
| 3786 | ImportedLocalExternalDecls.insert(Var); |
| 3787 | } |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3788 | } |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 3789 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 3790 | return; |
| 3791 | } |
| 3792 | |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3793 | /// convertFunctionTypeOfBlocks - This routine converts a function type |
| 3794 | /// 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] | 3795 | /// might be block pointers to an equivalent function type replacing |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3796 | /// all block pointers to function pointers. |
| 3797 | QualType RewriteObjC::convertFunctionTypeOfBlocks(const FunctionType *FT) { |
| 3798 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
| 3799 | // FTP will be null for closures that don't take arguments. |
| 3800 | // Generate a funky cast. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3801 | SmallVector<QualType, 8> ArgTypes; |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3802 | QualType Res = FT->getResultType(); |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3803 | bool HasBlockType = convertBlockPointerToFunctionPointer(Res); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3804 | |
| 3805 | if (FTP) { |
| 3806 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
| 3807 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 3808 | QualType t = *I; |
| 3809 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3810 | if (convertBlockPointerToFunctionPointer(t)) |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3811 | HasBlockType = true; |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3812 | ArgTypes.push_back(t); |
| 3813 | } |
| 3814 | } |
| 3815 | QualType FuncType; |
| 3816 | // FIXME. Does this work if block takes no argument but has a return type |
| 3817 | // which is of block type? |
| 3818 | if (HasBlockType) |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3819 | FuncType = getSimpleFunctionType(Res, &ArgTypes[0], ArgTypes.size()); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3820 | else FuncType = QualType(FT, 0); |
| 3821 | return FuncType; |
| 3822 | } |
| 3823 | |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3824 | Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp, const Expr *BlockExp) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3825 | // Navigate to relevant type information. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3826 | const BlockPointerType *CPT = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3827 | |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3828 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BlockExp)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3829 | CPT = DRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3830 | } else if (const MemberExpr *MExpr = dyn_cast<MemberExpr>(BlockExp)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3831 | CPT = MExpr->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3832 | } |
| 3833 | else if (const ParenExpr *PRE = dyn_cast<ParenExpr>(BlockExp)) { |
| 3834 | return SynthesizeBlockCall(Exp, PRE->getSubExpr()); |
| 3835 | } |
| 3836 | else if (const ImplicitCastExpr *IEXPR = dyn_cast<ImplicitCastExpr>(BlockExp)) |
| 3837 | CPT = IEXPR->getType()->getAs<BlockPointerType>(); |
| 3838 | else if (const ConditionalOperator *CEXPR = |
| 3839 | dyn_cast<ConditionalOperator>(BlockExp)) { |
| 3840 | Expr *LHSExp = CEXPR->getLHS(); |
| 3841 | Stmt *LHSStmt = SynthesizeBlockCall(Exp, LHSExp); |
| 3842 | Expr *RHSExp = CEXPR->getRHS(); |
| 3843 | Stmt *RHSStmt = SynthesizeBlockCall(Exp, RHSExp); |
| 3844 | Expr *CONDExp = CEXPR->getCond(); |
| 3845 | ConditionalOperator *CondExpr = |
| 3846 | new (Context) ConditionalOperator(CONDExp, |
| 3847 | SourceLocation(), cast<Expr>(LHSStmt), |
Fariborz Jahanian | f9b949f | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 3848 | SourceLocation(), cast<Expr>(RHSStmt), |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3849 | Exp->getType(), VK_RValue, OK_Ordinary); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 3850 | return CondExpr; |
Fariborz Jahanian | e24b22b | 2009-12-18 01:15:21 +0000 | [diff] [blame] | 3851 | } else if (const ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(BlockExp)) { |
| 3852 | CPT = IRE->getType()->getAs<BlockPointerType>(); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 3853 | } else if (const PseudoObjectExpr *POE |
| 3854 | = dyn_cast<PseudoObjectExpr>(BlockExp)) { |
| 3855 | CPT = POE->getType()->castAs<BlockPointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3856 | } else { |
| 3857 | assert(1 && "RewriteBlockClass: Bad type"); |
| 3858 | } |
| 3859 | assert(CPT && "RewriteBlockClass: Bad type"); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3860 | const FunctionType *FT = CPT->getPointeeType()->getAs<FunctionType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3861 | assert(FT && "RewriteBlockClass: Bad type"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3862 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3863 | // FTP will be null for closures that don't take arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3864 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3865 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3866 | SourceLocation(), SourceLocation(), |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3867 | &Context->Idents.get("__block_impl")); |
| 3868 | QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD)); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3869 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3870 | // Generate a funky cast. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3871 | SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3872 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3873 | // Push the block argument type. |
| 3874 | ArgTypes.push_back(PtrBlock); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3875 | if (FTP) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3876 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3877 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 3878 | QualType t = *I; |
| 3879 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 8188e5f | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 3880 | if (!convertBlockPointerToFunctionPointer(t)) |
| 3881 | convertToUnqualifiedObjCType(t); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3882 | ArgTypes.push_back(t); |
| 3883 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3884 | } |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3885 | // Now do the pointer to function cast. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3886 | QualType PtrToFuncCastType |
| 3887 | = getSimpleFunctionType(Exp->getType(), &ArgTypes[0], ArgTypes.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3888 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3889 | PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3890 | |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3891 | CastExpr *BlkCast = NoTypeInfoCStyleCastExpr(Context, PtrBlock, |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3892 | CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3893 | const_cast<Expr*>(BlockExp)); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3894 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3895 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3896 | BlkCast); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3897 | //PE->dump(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3898 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3899 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3900 | SourceLocation(), |
| 3901 | &Context->Idents.get("FuncPtr"), |
| 3902 | Context->VoidPtrTy, 0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3903 | /*BitWidth=*/0, /*Mutable=*/true, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3904 | ICIS_NoInit); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3905 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3906 | FD->getType(), VK_LValue, |
| 3907 | OK_Ordinary); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3908 | |
Fariborz Jahanian | 8188e5f | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 3909 | |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3910 | CastExpr *FunkCast = NoTypeInfoCStyleCastExpr(Context, PtrToFuncCastType, |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3911 | CK_BitCast, ME); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3912 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3913 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3914 | SmallVector<Expr*, 8> BlkExprs; |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3915 | // Add the implicit argument. |
| 3916 | BlkExprs.push_back(BlkCast); |
| 3917 | // Add the user arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3918 | for (CallExpr::arg_iterator I = Exp->arg_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3919 | E = Exp->arg_end(); I != E; ++I) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3920 | BlkExprs.push_back(*I); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3921 | } |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3922 | CallExpr *CE = new (Context) CallExpr(*Context, PE, BlkExprs, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3923 | Exp->getType(), VK_RValue, |
| 3924 | SourceLocation()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3925 | return CE; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3926 | } |
| 3927 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3928 | // We need to return the rewritten expression to handle cases where the |
| 3929 | // BlockDeclRefExpr is embedded in another expression being rewritten. |
| 3930 | // For example: |
| 3931 | // |
| 3932 | // int main() { |
| 3933 | // __block Foo *f; |
| 3934 | // __block int i; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3935 | // |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3936 | // void (^myblock)() = ^() { |
| 3937 | // [f test]; // f is a BlockDeclRefExpr embedded in a message (which is being rewritten). |
| 3938 | // i = 77; |
| 3939 | // }; |
| 3940 | //} |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3941 | Stmt *RewriteObjC::RewriteBlockDeclRefExpr(DeclRefExpr *DeclRefExp) { |
Fariborz Jahanian | bbf37e2 | 2009-12-23 19:26:34 +0000 | [diff] [blame] | 3942 | // Rewrite the byref variable into BYREFVAR->__forwarding->BYREFVAR |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3943 | // for each DeclRefExp where BYREFVAR is name of the variable. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3944 | ValueDecl *VD = DeclRefExp->getDecl(); |
| 3945 | bool isArrow = DeclRefExp->refersToEnclosingLocal(); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3946 | |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3947 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3948 | SourceLocation(), |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3949 | &Context->Idents.get("__forwarding"), |
| 3950 | Context->VoidPtrTy, 0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3951 | /*BitWidth=*/0, /*Mutable=*/true, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3952 | ICIS_NoInit); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3953 | MemberExpr *ME = new (Context) MemberExpr(DeclRefExp, isArrow, |
| 3954 | FD, SourceLocation(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3955 | FD->getType(), VK_LValue, |
| 3956 | OK_Ordinary); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3957 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3958 | StringRef Name = VD->getName(); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3959 | FD = FieldDecl::Create(*Context, 0, SourceLocation(), SourceLocation(), |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3960 | &Context->Idents.get(Name), |
| 3961 | Context->VoidPtrTy, 0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3962 | /*BitWidth=*/0, /*Mutable=*/true, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3963 | ICIS_NoInit); |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3964 | ME = new (Context) MemberExpr(ME, true, FD, SourceLocation(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3965 | DeclRefExp->getType(), VK_LValue, OK_Ordinary); |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3966 | |
| 3967 | |
| 3968 | |
Steve Naroff | df8570d | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 3969 | // Need parens to enforce precedence. |
Fariborz Jahanian | 380ee50 | 2011-04-01 19:19:28 +0000 | [diff] [blame] | 3970 | ParenExpr *PE = new (Context) ParenExpr(DeclRefExp->getExprLoc(), |
| 3971 | DeclRefExp->getExprLoc(), |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 3972 | ME); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 3973 | ReplaceStmt(DeclRefExp, PE); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3974 | return PE; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3975 | } |
| 3976 | |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3977 | // Rewrites the imported local variable V with external storage |
| 3978 | // (static, extern, etc.) as *V |
| 3979 | // |
| 3980 | Stmt *RewriteObjC::RewriteLocalVariableExternalStorage(DeclRefExpr *DRE) { |
| 3981 | ValueDecl *VD = DRE->getDecl(); |
| 3982 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 3983 | if (!ImportedLocalExternalDecls.count(Var)) |
| 3984 | return DRE; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3985 | Expr *Exp = new (Context) UnaryOperator(DRE, UO_Deref, DRE->getType(), |
| 3986 | VK_LValue, OK_Ordinary, |
| 3987 | DRE->getLocation()); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 3988 | // Need parens to enforce precedence. |
| 3989 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3990 | Exp); |
| 3991 | ReplaceStmt(DRE, PE); |
| 3992 | return PE; |
| 3993 | } |
| 3994 | |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 3995 | void RewriteObjC::RewriteCastExpr(CStyleCastExpr *CE) { |
| 3996 | SourceLocation LocStart = CE->getLParenLoc(); |
| 3997 | SourceLocation LocEnd = CE->getRParenLoc(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3998 | |
| 3999 | // Need to avoid trying to rewrite synthesized casts. |
| 4000 | if (LocStart.isInvalid()) |
| 4001 | return; |
Steve Naroff | 8f6ce57 | 2008-11-03 11:20:24 +0000 | [diff] [blame] | 4002 | // Need to avoid trying to rewrite casts contained in macros. |
| 4003 | if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) |
| 4004 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4005 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4006 | const char *startBuf = SM->getCharacterData(LocStart); |
| 4007 | const char *endBuf = SM->getCharacterData(LocEnd); |
Fariborz Jahanian | 1d4fca2 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4008 | QualType QT = CE->getType(); |
| 4009 | const Type* TypePtr = QT->getAs<Type>(); |
| 4010 | if (isa<TypeOfExprType>(TypePtr)) { |
| 4011 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 4012 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 4013 | std::string TypeAsString = "("; |
Fariborz Jahanian | afad76f | 2010-02-18 01:20:22 +0000 | [diff] [blame] | 4014 | RewriteBlockPointerType(TypeAsString, QT); |
Fariborz Jahanian | 1d4fca2 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4015 | TypeAsString += ")"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4016 | ReplaceText(LocStart, endBuf-startBuf+1, TypeAsString); |
Fariborz Jahanian | 1d4fca2 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4017 | return; |
| 4018 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4019 | // advance the location to startArgList. |
| 4020 | const char *argPtr = startBuf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4021 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4022 | while (*argPtr++ && (argPtr < endBuf)) { |
| 4023 | switch (*argPtr) { |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4024 | case '^': |
| 4025 | // Replace the '^' with '*'. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4026 | LocStart = LocStart.getLocWithOffset(argPtr-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4027 | ReplaceText(LocStart, 1, "*"); |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4028 | break; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4029 | } |
| 4030 | } |
| 4031 | return; |
| 4032 | } |
| 4033 | |
| 4034 | void RewriteObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { |
| 4035 | SourceLocation DeclLoc = FD->getLocation(); |
| 4036 | unsigned parenCount = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4037 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4038 | // We have 1 or more arguments that have closure pointers. |
| 4039 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4040 | const char *startArgList = strchr(startBuf, '('); |
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 | assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4043 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4044 | parenCount++; |
| 4045 | // advance the location to startArgList. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4046 | DeclLoc = DeclLoc.getLocWithOffset(startArgList-startBuf); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4047 | assert((DeclLoc.isValid()) && "Invalid DeclLoc"); |
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 | const char *argPtr = startArgList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4050 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4051 | while (*argPtr++ && parenCount) { |
| 4052 | switch (*argPtr) { |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4053 | case '^': |
| 4054 | // Replace the '^' with '*'. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4055 | DeclLoc = DeclLoc.getLocWithOffset(argPtr-startArgList); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4056 | ReplaceText(DeclLoc, 1, "*"); |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4057 | break; |
| 4058 | case '(': |
| 4059 | parenCount++; |
| 4060 | break; |
| 4061 | case ')': |
| 4062 | parenCount--; |
| 4063 | break; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4064 | } |
| 4065 | } |
| 4066 | return; |
| 4067 | } |
| 4068 | |
| 4069 | bool RewriteObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4070 | const FunctionProtoType *FTP; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4071 | const PointerType *PT = QT->getAs<PointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4072 | if (PT) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4073 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4074 | } else { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4075 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4076 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4077 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4078 | } |
| 4079 | if (FTP) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4080 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4081 | E = FTP->arg_type_end(); I != E; ++I) |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4082 | if (isTopLevelBlockPointerType(*I)) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4083 | return true; |
| 4084 | } |
| 4085 | return false; |
| 4086 | } |
| 4087 | |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4088 | bool RewriteObjC::PointerTypeTakesAnyObjCQualifiedType(QualType QT) { |
| 4089 | const FunctionProtoType *FTP; |
| 4090 | const PointerType *PT = QT->getAs<PointerType>(); |
| 4091 | if (PT) { |
| 4092 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4093 | } else { |
| 4094 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
| 4095 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
| 4096 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4097 | } |
| 4098 | if (FTP) { |
| 4099 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Fariborz Jahanian | 06de2cf | 2010-11-03 23:50:34 +0000 | [diff] [blame] | 4100 | E = FTP->arg_type_end(); I != E; ++I) { |
| 4101 | if ((*I)->isObjCQualifiedIdType()) |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4102 | return true; |
Fariborz Jahanian | 06de2cf | 2010-11-03 23:50:34 +0000 | [diff] [blame] | 4103 | if ((*I)->isObjCObjectPointerType() && |
| 4104 | (*I)->getPointeeType()->isObjCQualifiedInterfaceType()) |
| 4105 | return true; |
| 4106 | } |
| 4107 | |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4108 | } |
| 4109 | return false; |
| 4110 | } |
| 4111 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4112 | void RewriteObjC::GetExtentOfArgList(const char *Name, const char *&LParen, |
| 4113 | const char *&RParen) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4114 | const char *argPtr = strchr(Name, '('); |
| 4115 | assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4116 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4117 | LParen = argPtr; // output the start. |
| 4118 | argPtr++; // skip past the left paren. |
| 4119 | unsigned parenCount = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4120 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4121 | while (*argPtr && parenCount) { |
| 4122 | switch (*argPtr) { |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4123 | case '(': parenCount++; break; |
| 4124 | case ')': parenCount--; break; |
| 4125 | default: break; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4126 | } |
| 4127 | if (parenCount) argPtr++; |
| 4128 | } |
| 4129 | assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); |
| 4130 | RParen = argPtr; // output the end |
| 4131 | } |
| 4132 | |
| 4133 | void RewriteObjC::RewriteBlockPointerDecl(NamedDecl *ND) { |
| 4134 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 4135 | RewriteBlockPointerFunctionArgs(FD); |
| 4136 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4137 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4138 | // Handle Variables and Typedefs. |
| 4139 | SourceLocation DeclLoc = ND->getLocation(); |
| 4140 | QualType DeclT; |
| 4141 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4142 | DeclT = VD->getType(); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4143 | else if (TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(ND)) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4144 | DeclT = TDD->getUnderlyingType(); |
| 4145 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) |
| 4146 | DeclT = FD->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4147 | else |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4148 | llvm_unreachable("RewriteBlockPointerDecl(): Decl type not yet handled"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4149 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4150 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4151 | const char *endBuf = startBuf; |
| 4152 | // scan backward (from the decl location) for the end of the previous decl. |
| 4153 | while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) |
| 4154 | startBuf--; |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4155 | SourceLocation Start = DeclLoc.getLocWithOffset(startBuf-endBuf); |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4156 | std::string buf; |
| 4157 | unsigned OrigLength=0; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4158 | // *startBuf != '^' if we are dealing with a pointer to function that |
| 4159 | // may take block argument types (which will be handled below). |
| 4160 | if (*startBuf == '^') { |
| 4161 | // Replace the '^' with '*', computing a negative offset. |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4162 | buf = '*'; |
| 4163 | startBuf++; |
| 4164 | OrigLength++; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4165 | } |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4166 | while (*startBuf != ')') { |
| 4167 | buf += *startBuf; |
| 4168 | startBuf++; |
| 4169 | OrigLength++; |
| 4170 | } |
| 4171 | buf += ')'; |
| 4172 | OrigLength++; |
| 4173 | |
| 4174 | if (PointerTypeTakesAnyBlockArguments(DeclT) || |
| 4175 | PointerTypeTakesAnyObjCQualifiedType(DeclT)) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4176 | // Replace the '^' with '*' for arguments. |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4177 | // Replace id<P> with id/*<>*/ |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4178 | DeclLoc = ND->getLocation(); |
| 4179 | startBuf = SM->getCharacterData(DeclLoc); |
| 4180 | const char *argListBegin, *argListEnd; |
| 4181 | GetExtentOfArgList(startBuf, argListBegin, argListEnd); |
| 4182 | while (argListBegin < argListEnd) { |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4183 | if (*argListBegin == '^') |
| 4184 | buf += '*'; |
| 4185 | else if (*argListBegin == '<') { |
| 4186 | buf += "/*"; |
| 4187 | buf += *argListBegin++; |
Dmitri Gribenko | 1ad23d6 | 2012-09-10 21:20:09 +0000 | [diff] [blame] | 4188 | OrigLength++; |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4189 | while (*argListBegin != '>') { |
| 4190 | buf += *argListBegin++; |
| 4191 | OrigLength++; |
| 4192 | } |
| 4193 | buf += *argListBegin; |
| 4194 | buf += "*/"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4195 | } |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4196 | else |
| 4197 | buf += *argListBegin; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4198 | argListBegin++; |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4199 | OrigLength++; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4200 | } |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4201 | buf += ')'; |
| 4202 | OrigLength++; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4203 | } |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4204 | ReplaceText(Start, OrigLength, buf); |
| 4205 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4206 | return; |
| 4207 | } |
| 4208 | |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4209 | |
| 4210 | /// SynthesizeByrefCopyDestroyHelper - This routine synthesizes: |
| 4211 | /// void __Block_byref_id_object_copy(struct Block_byref_id_object *dst, |
| 4212 | /// struct Block_byref_id_object *src) { |
| 4213 | /// _Block_object_assign (&_dest->object, _src->object, |
| 4214 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4215 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4216 | /// _Block_object_assign(&_dest->object, _src->object, |
| 4217 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4218 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4219 | /// } |
| 4220 | /// And: |
| 4221 | /// void __Block_byref_id_object_dispose(struct Block_byref_id_object *_src) { |
| 4222 | /// _Block_object_dispose(_src->object, |
| 4223 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4224 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4225 | /// _Block_object_dispose(_src->object, |
| 4226 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4227 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4228 | /// } |
| 4229 | |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4230 | std::string RewriteObjC::SynthesizeByrefCopyDestroyHelper(VarDecl *VD, |
| 4231 | int flag) { |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4232 | std::string S; |
Benjamin Kramer | 1211a71 | 2010-01-10 19:57:50 +0000 | [diff] [blame] | 4233 | if (CopyDestroyCache.count(flag)) |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4234 | return S; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4235 | CopyDestroyCache.insert(flag); |
| 4236 | S = "static void __Block_byref_id_object_copy_"; |
| 4237 | S += utostr(flag); |
| 4238 | S += "(void *dst, void *src) {\n"; |
| 4239 | |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4240 | // offset into the object pointer is computed as: |
| 4241 | // void * + void* + int + int + void* + void * |
| 4242 | unsigned IntSize = |
| 4243 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 4244 | unsigned VoidPtrSize = |
| 4245 | static_cast<unsigned>(Context->getTypeSize(Context->VoidPtrTy)); |
| 4246 | |
Ken Dyck | 0c4e5d6 | 2011-04-30 16:08:27 +0000 | [diff] [blame] | 4247 | unsigned offset = (VoidPtrSize*4 + IntSize + IntSize)/Context->getCharWidth(); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4248 | S += " _Block_object_assign((char*)dst + "; |
| 4249 | S += utostr(offset); |
| 4250 | S += ", *(void * *) ((char*)src + "; |
| 4251 | S += utostr(offset); |
| 4252 | S += "), "; |
| 4253 | S += utostr(flag); |
| 4254 | S += ");\n}\n"; |
| 4255 | |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4256 | S += "static void __Block_byref_id_object_dispose_"; |
| 4257 | S += utostr(flag); |
| 4258 | S += "(void *src) {\n"; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4259 | S += " _Block_object_dispose(*(void * *) ((char*)src + "; |
| 4260 | S += utostr(offset); |
| 4261 | S += "), "; |
| 4262 | S += utostr(flag); |
| 4263 | S += ");\n}\n"; |
| 4264 | return S; |
| 4265 | } |
| 4266 | |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4267 | /// RewriteByRefVar - For each __block typex ND variable this routine transforms |
| 4268 | /// the declaration into: |
| 4269 | /// struct __Block_byref_ND { |
| 4270 | /// void *__isa; // NULL for everything except __weak pointers |
| 4271 | /// struct __Block_byref_ND *__forwarding; |
| 4272 | /// int32_t __flags; |
| 4273 | /// int32_t __size; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4274 | /// void *__Block_byref_id_object_copy; // If variable is __block ObjC object |
| 4275 | /// void *__Block_byref_id_object_dispose; // If variable is __block ObjC object |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4276 | /// typex ND; |
| 4277 | /// }; |
| 4278 | /// |
| 4279 | /// It then replaces declaration of ND variable with: |
| 4280 | /// struct __Block_byref_ND ND = {__isa=0B, __forwarding=&ND, __flags=some_flag, |
| 4281 | /// __size=sizeof(struct __Block_byref_ND), |
| 4282 | /// ND=initializer-if-any}; |
| 4283 | /// |
| 4284 | /// |
| 4285 | void RewriteObjC::RewriteByRefVar(VarDecl *ND) { |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 4286 | // Insert declaration for the function in which block literal is |
| 4287 | // used. |
| 4288 | if (CurFunctionDeclToDeclareForBlock) |
| 4289 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4290 | int flag = 0; |
| 4291 | int isa = 0; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4292 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
Fariborz Jahanian | d64a4f4 | 2010-02-26 22:49:11 +0000 | [diff] [blame] | 4293 | if (DeclLoc.isInvalid()) |
| 4294 | // If type location is missing, it is because of missing type (a warning). |
| 4295 | // Use variable's location which is good for this case. |
| 4296 | DeclLoc = ND->getLocation(); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4297 | const char *startBuf = SM->getCharacterData(DeclLoc); |
Fariborz Jahanian | 6f0a0a9 | 2009-12-30 20:38:08 +0000 | [diff] [blame] | 4298 | SourceLocation X = ND->getLocEnd(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 4299 | X = SM->getExpansionLoc(X); |
Fariborz Jahanian | 6f0a0a9 | 2009-12-30 20:38:08 +0000 | [diff] [blame] | 4300 | const char *endBuf = SM->getCharacterData(X); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4301 | std::string Name(ND->getNameAsString()); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4302 | std::string ByrefType; |
Fariborz Jahanian | 1e8011e | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 4303 | RewriteByRefString(ByrefType, Name, ND, true); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4304 | ByrefType += " {\n"; |
| 4305 | ByrefType += " void *__isa;\n"; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4306 | RewriteByRefString(ByrefType, Name, ND); |
| 4307 | ByrefType += " *__forwarding;\n"; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4308 | ByrefType += " int __flags;\n"; |
| 4309 | ByrefType += " int __size;\n"; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4310 | // Add void *__Block_byref_id_object_copy; |
| 4311 | // void *__Block_byref_id_object_dispose; if needed. |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4312 | QualType Ty = ND->getType(); |
Fariborz Jahanian | b15c898 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 4313 | bool HasCopyAndDispose = Context->BlockRequiresCopying(Ty, ND); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4314 | if (HasCopyAndDispose) { |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4315 | ByrefType += " void (*__Block_byref_id_object_copy)(void*, void*);\n"; |
| 4316 | ByrefType += " void (*__Block_byref_id_object_dispose)(void*);\n"; |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4317 | } |
Fariborz Jahanian | 822ac87 | 2011-03-31 22:49:32 +0000 | [diff] [blame] | 4318 | |
| 4319 | QualType T = Ty; |
| 4320 | (void)convertBlockPointerToFunctionPointer(T); |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 4321 | T.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Fariborz Jahanian | 822ac87 | 2011-03-31 22:49:32 +0000 | [diff] [blame] | 4322 | |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4323 | ByrefType += " " + Name + ";\n"; |
| 4324 | ByrefType += "};\n"; |
| 4325 | // Insert this type in global scope. It is needed by helper function. |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4326 | SourceLocation FunLocStart; |
| 4327 | if (CurFunctionDef) |
| 4328 | FunLocStart = CurFunctionDef->getTypeSpecStartLoc(); |
| 4329 | else { |
| 4330 | assert(CurMethodDef && "RewriteByRefVar - CurMethodDef is null"); |
| 4331 | FunLocStart = CurMethodDef->getLocStart(); |
| 4332 | } |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4333 | InsertText(FunLocStart, ByrefType); |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4334 | if (Ty.isObjCGCWeak()) { |
| 4335 | flag |= BLOCK_FIELD_IS_WEAK; |
| 4336 | isa = 1; |
| 4337 | } |
| 4338 | |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4339 | if (HasCopyAndDispose) { |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4340 | flag = BLOCK_BYREF_CALLER; |
| 4341 | QualType Ty = ND->getType(); |
| 4342 | // FIXME. Handle __weak variable (BLOCK_FIELD_IS_WEAK) as well. |
| 4343 | if (Ty->isBlockPointerType()) |
| 4344 | flag |= BLOCK_FIELD_IS_BLOCK; |
| 4345 | else |
| 4346 | flag |= BLOCK_FIELD_IS_OBJECT; |
| 4347 | std::string HF = SynthesizeByrefCopyDestroyHelper(ND, flag); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4348 | if (!HF.empty()) |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4349 | InsertText(FunLocStart, HF); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4350 | } |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4351 | |
| 4352 | // struct __Block_byref_ND ND = |
| 4353 | // {0, &ND, some_flag, __size=sizeof(struct __Block_byref_ND), |
| 4354 | // initializer-if-any}; |
| 4355 | bool hasInit = (ND->getInit() != 0); |
Fariborz Jahanian | e1f84f8 | 2010-01-05 18:15:57 +0000 | [diff] [blame] | 4356 | unsigned flags = 0; |
| 4357 | if (HasCopyAndDispose) |
| 4358 | flags |= BLOCK_HAS_COPY_DISPOSE; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4359 | Name = ND->getNameAsString(); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4360 | ByrefType.clear(); |
| 4361 | RewriteByRefString(ByrefType, Name, ND); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4362 | std::string ForwardingCastType("("); |
| 4363 | ForwardingCastType += ByrefType + " *)"; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4364 | if (!hasInit) { |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4365 | ByrefType += " " + Name + " = {(void*)"; |
| 4366 | ByrefType += utostr(isa); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4367 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4368 | ByrefType += utostr(flags); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4369 | ByrefType += ", "; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4370 | ByrefType += "sizeof("; |
| 4371 | RewriteByRefString(ByrefType, Name, ND); |
| 4372 | ByrefType += ")"; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4373 | if (HasCopyAndDispose) { |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4374 | ByrefType += ", __Block_byref_id_object_copy_"; |
| 4375 | ByrefType += utostr(flag); |
| 4376 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 4377 | ByrefType += utostr(flag); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4378 | } |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4379 | ByrefType += "};\n"; |
Fariborz Jahanian | 822ac87 | 2011-03-31 22:49:32 +0000 | [diff] [blame] | 4380 | unsigned nameSize = Name.size(); |
| 4381 | // for block or function pointer declaration. Name is aleady |
| 4382 | // part of the declaration. |
| 4383 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) |
| 4384 | nameSize = 1; |
| 4385 | ReplaceText(DeclLoc, endBuf-startBuf+nameSize, ByrefType); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4386 | } |
| 4387 | else { |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4388 | SourceLocation startLoc; |
| 4389 | Expr *E = ND->getInit(); |
| 4390 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 4391 | startLoc = ECE->getLParenLoc(); |
| 4392 | else |
| 4393 | startLoc = E->getLocStart(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 4394 | startLoc = SM->getExpansionLoc(startLoc); |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4395 | endBuf = SM->getCharacterData(startLoc); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4396 | ByrefType += " " + Name; |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4397 | ByrefType += " = {(void*)"; |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4398 | ByrefType += utostr(isa); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4399 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4400 | ByrefType += utostr(flags); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4401 | ByrefType += ", "; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4402 | ByrefType += "sizeof("; |
| 4403 | RewriteByRefString(ByrefType, Name, ND); |
| 4404 | ByrefType += "), "; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4405 | if (HasCopyAndDispose) { |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4406 | ByrefType += "__Block_byref_id_object_copy_"; |
| 4407 | ByrefType += utostr(flag); |
| 4408 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 4409 | ByrefType += utostr(flag); |
| 4410 | ByrefType += ", "; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4411 | } |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4412 | ReplaceText(DeclLoc, endBuf-startBuf, ByrefType); |
Steve Naroff | c5143c5 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 4413 | |
| 4414 | // Complete the newly synthesized compound expression by inserting a right |
| 4415 | // curly brace before the end of the declaration. |
| 4416 | // FIXME: This approach avoids rewriting the initializer expression. It |
| 4417 | // also assumes there is only one declarator. For example, the following |
| 4418 | // isn't currently supported by this routine (in general): |
| 4419 | // |
| 4420 | // double __block BYREFVAR = 1.34, BYREFVAR2 = 1.37; |
| 4421 | // |
Fariborz Jahanian | 5f371ee | 2010-07-21 17:36:39 +0000 | [diff] [blame] | 4422 | const char *startInitializerBuf = SM->getCharacterData(startLoc); |
| 4423 | const char *semiBuf = strchr(startInitializerBuf, ';'); |
Steve Naroff | c5143c5 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 4424 | assert((*semiBuf == ';') && "RewriteByRefVar: can't find ';'"); |
| 4425 | SourceLocation semiLoc = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4426 | startLoc.getLocWithOffset(semiBuf-startInitializerBuf); |
Steve Naroff | c5143c5 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 4427 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4428 | InsertText(semiLoc, "}"); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4429 | } |
Fariborz Jahanian | 1be6b46 | 2009-12-22 00:48:54 +0000 | [diff] [blame] | 4430 | return; |
| 4431 | } |
| 4432 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4433 | void RewriteObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4434 | // Add initializers for any closure decl refs. |
| 4435 | GetBlockDeclRefExprs(Exp->getBody()); |
| 4436 | if (BlockDeclRefs.size()) { |
| 4437 | // Unique all "by copy" declarations. |
| 4438 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4439 | if (!BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>()) { |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4440 | if (!BlockByCopyDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 4441 | BlockByCopyDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 4442 | BlockByCopyDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 4443 | } |
| 4444 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4445 | // Unique all "by ref" declarations. |
| 4446 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4447 | if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>()) { |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4448 | if (!BlockByRefDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 4449 | BlockByRefDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 4450 | BlockByRefDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 4451 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4452 | } |
| 4453 | // Find any imported blocks...they will need special attention. |
| 4454 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4455 | if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>() || |
Fariborz Jahanian | 4fcc4fd | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 4456 | BlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
Fariborz Jahanian | 5b011b0 | 2010-02-26 21:46:27 +0000 | [diff] [blame] | 4457 | BlockDeclRefs[i]->getType()->isBlockPointerType()) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4458 | ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4459 | } |
| 4460 | } |
| 4461 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4462 | FunctionDecl *RewriteObjC::SynthBlockInitFunctionDecl(StringRef name) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4463 | IdentifierInfo *ID = &Context->Idents.get(name); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4464 | QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4465 | return FunctionDecl::Create(*Context, TUDecl, SourceLocation(), |
| 4466 | SourceLocation(), ID, FType, 0, SC_Extern, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4467 | SC_None, false, false); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4468 | } |
| 4469 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4470 | Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4471 | const SmallVector<DeclRefExpr *, 8> &InnerBlockDeclRefs) { |
Fariborz Jahanian | 05318d8 | 2011-02-16 22:37:10 +0000 | [diff] [blame] | 4472 | const BlockDecl *block = Exp->getBlockDecl(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4473 | Blocks.push_back(Exp); |
| 4474 | |
| 4475 | CollectBlockDeclRefInfo(Exp); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4476 | |
| 4477 | // Add inner imported variables now used in current block. |
| 4478 | int countOfInnerDecls = 0; |
Fariborz Jahanian | 1276bfe | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4479 | if (!InnerBlockDeclRefs.empty()) { |
| 4480 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) { |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4481 | DeclRefExpr *Exp = InnerBlockDeclRefs[i]; |
Fariborz Jahanian | 1276bfe | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4482 | ValueDecl *VD = Exp->getDecl(); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4483 | if (!VD->hasAttr<BlocksAttr>() && !BlockByCopyDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4484 | // We need to save the copied-in variables in nested |
| 4485 | // blocks because it is needed at the end for some of the API generations. |
| 4486 | // See SynthesizeBlockLiterals routine. |
Fariborz Jahanian | 1276bfe | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4487 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 4488 | BlockDeclRefs.push_back(Exp); |
| 4489 | BlockByCopyDeclsPtrSet.insert(VD); |
| 4490 | BlockByCopyDecls.push_back(VD); |
| 4491 | } |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4492 | if (VD->hasAttr<BlocksAttr>() && !BlockByRefDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 1276bfe | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4493 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 4494 | BlockDeclRefs.push_back(Exp); |
| 4495 | BlockByRefDeclsPtrSet.insert(VD); |
| 4496 | BlockByRefDecls.push_back(VD); |
| 4497 | } |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4498 | } |
Fariborz Jahanian | 1276bfe | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4499 | // Find any imported blocks...they will need special attention. |
| 4500 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4501 | if (InnerBlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>() || |
Fariborz Jahanian | 1276bfe | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4502 | InnerBlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
| 4503 | InnerBlockDeclRefs[i]->getType()->isBlockPointerType()) |
| 4504 | ImportedBlockDecls.insert(InnerBlockDeclRefs[i]->getDecl()); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4505 | } |
| 4506 | InnerDeclRefsCount.push_back(countOfInnerDecls); |
| 4507 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4508 | std::string FuncName; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4509 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4510 | if (CurFunctionDef) |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4511 | FuncName = CurFunctionDef->getNameAsString(); |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 4512 | else if (CurMethodDef) |
| 4513 | BuildUniqueMethodName(FuncName, CurMethodDef); |
| 4514 | else if (GlobalVarDecl) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4515 | FuncName = std::string(GlobalVarDecl->getNameAsString()); |
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 BlockNumber = utostr(Blocks.size()-1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4518 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4519 | std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber; |
| 4520 | std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4521 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4522 | // Get a pointer to the function type so we can cast appropriately. |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4523 | QualType BFT = convertFunctionTypeOfBlocks(Exp->getFunctionType()); |
| 4524 | QualType FType = Context->getPointerType(BFT); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4525 | |
| 4526 | FunctionDecl *FD; |
| 4527 | Expr *NewRep; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4528 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4529 | // Simulate a contructor call... |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4530 | FD = SynthBlockInitFunctionDecl(Tag); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4531 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, false, FType, VK_RValue, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4532 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4533 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4534 | SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4535 | |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4536 | // Initialize the block function. |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4537 | FD = SynthBlockInitFunctionDecl(Func); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4538 | DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, false, FD->getType(), |
| 4539 | VK_LValue, SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4540 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4541 | CK_BitCast, Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4542 | InitExprs.push_back(castExpr); |
| 4543 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4544 | // Initialize the block descriptor. |
| 4545 | std::string DescData = "__" + FuncName + "_block_desc_" + BlockNumber + "_DATA"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4546 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4547 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, |
| 4548 | SourceLocation(), SourceLocation(), |
| 4549 | &Context->Idents.get(DescData.c_str()), |
| 4550 | Context->VoidPtrTy, 0, |
| 4551 | SC_Static, SC_None); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4552 | UnaryOperator *DescRefExpr = |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4553 | new (Context) UnaryOperator(new (Context) DeclRefExpr(NewVD, false, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4554 | Context->VoidPtrTy, |
| 4555 | VK_LValue, |
| 4556 | SourceLocation()), |
| 4557 | UO_AddrOf, |
| 4558 | Context->getPointerType(Context->VoidPtrTy), |
| 4559 | VK_RValue, OK_Ordinary, |
| 4560 | SourceLocation()); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4561 | InitExprs.push_back(DescRefExpr); |
| 4562 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4563 | // Add initializers for any closure decl refs. |
| 4564 | if (BlockDeclRefs.size()) { |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4565 | Expr *Exp; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4566 | // Output all "by copy" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4567 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4568 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4569 | if (isObjCType((*I)->getType())) { |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4570 | // FIXME: Conform to ABI ([[obj retain] autorelease]). |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4571 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4572 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4573 | SourceLocation()); |
Fariborz Jahanian | a5a7987 | 2010-05-24 18:32:56 +0000 | [diff] [blame] | 4574 | if (HasLocalVariableExternalStorage(*I)) { |
| 4575 | QualType QT = (*I)->getType(); |
| 4576 | QT = Context->getPointerType(QT); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4577 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue, |
| 4578 | OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | a5a7987 | 2010-05-24 18:32:56 +0000 | [diff] [blame] | 4579 | } |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4580 | } else if (isTopLevelBlockPointerType((*I)->getType())) { |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4581 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4582 | Arg = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4583 | SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4584 | Exp = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4585 | CK_BitCast, Arg); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4586 | } else { |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4587 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4588 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4589 | SourceLocation()); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4590 | if (HasLocalVariableExternalStorage(*I)) { |
| 4591 | QualType QT = (*I)->getType(); |
| 4592 | QT = Context->getPointerType(QT); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4593 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue, |
| 4594 | OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4595 | } |
| 4596 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4597 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4598 | InitExprs.push_back(Exp); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4599 | } |
| 4600 | // Output all "by ref" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4601 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4602 | E = BlockByRefDecls.end(); I != E; ++I) { |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4603 | ValueDecl *ND = (*I); |
| 4604 | std::string Name(ND->getNameAsString()); |
| 4605 | std::string RecName; |
Fariborz Jahanian | 1e8011e | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 4606 | RewriteByRefString(RecName, Name, ND, true); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4607 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str() |
| 4608 | + sizeof("struct")); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4609 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4610 | SourceLocation(), SourceLocation(), |
| 4611 | II); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4612 | assert(RD && "SynthBlockInitExpr(): Can't find RecordDecl"); |
| 4613 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 4614 | |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4615 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4616 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4617 | SourceLocation()); |
Fariborz Jahanian | 05318d8 | 2011-02-16 22:37:10 +0000 | [diff] [blame] | 4618 | bool isNestedCapturedVar = false; |
| 4619 | if (block) |
| 4620 | for (BlockDecl::capture_const_iterator ci = block->capture_begin(), |
| 4621 | ce = block->capture_end(); ci != ce; ++ci) { |
| 4622 | const VarDecl *variable = ci->getVariable(); |
| 4623 | if (variable == ND && ci->isNested()) { |
| 4624 | assert (ci->isByRef() && |
| 4625 | "SynthBlockInitExpr - captured block variable is not byref"); |
| 4626 | isNestedCapturedVar = true; |
| 4627 | break; |
| 4628 | } |
| 4629 | } |
| 4630 | // captured nested byref variable has its address passed. Do not take |
| 4631 | // its address again. |
| 4632 | if (!isNestedCapturedVar) |
| 4633 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4634 | Context->getPointerType(Exp->getType()), |
| 4635 | VK_RValue, OK_Ordinary, SourceLocation()); |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4636 | Exp = NoTypeInfoCStyleCastExpr(Context, castT, CK_BitCast, Exp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4637 | InitExprs.push_back(Exp); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4638 | } |
| 4639 | } |
Fariborz Jahanian | ff12788 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 4640 | if (ImportedBlockDecls.size()) { |
| 4641 | // generate BLOCK_HAS_COPY_DISPOSE(have helper funcs) | BLOCK_HAS_DESCRIPTOR |
| 4642 | int flag = (BLOCK_HAS_COPY_DISPOSE | BLOCK_HAS_DESCRIPTOR); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4643 | unsigned IntSize = |
| 4644 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 4645 | Expr *FlagExp = IntegerLiteral::Create(*Context, llvm::APInt(IntSize, flag), |
| 4646 | Context->IntTy, SourceLocation()); |
Fariborz Jahanian | ff12788 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 4647 | InitExprs.push_back(FlagExp); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4648 | } |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 4649 | NewRep = new (Context) CallExpr(*Context, DRE, InitExprs, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4650 | FType, VK_LValue, SourceLocation()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4651 | NewRep = new (Context) UnaryOperator(NewRep, UO_AddrOf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4652 | Context->getPointerType(NewRep->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4653 | VK_RValue, OK_Ordinary, SourceLocation()); |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4654 | NewRep = NoTypeInfoCStyleCastExpr(Context, FType, CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4655 | NewRep); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4656 | BlockDeclRefs.clear(); |
| 4657 | BlockByRefDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4658 | BlockByRefDeclsPtrSet.clear(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4659 | BlockByCopyDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4660 | BlockByCopyDeclsPtrSet.clear(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4661 | ImportedBlockDecls.clear(); |
| 4662 | return NewRep; |
| 4663 | } |
| 4664 | |
Fariborz Jahanian | 42f1e65 | 2011-02-24 21:29:21 +0000 | [diff] [blame] | 4665 | bool RewriteObjC::IsDeclStmtInForeachHeader(DeclStmt *DS) { |
| 4666 | if (const ObjCForCollectionStmt * CS = |
| 4667 | dyn_cast<ObjCForCollectionStmt>(Stmts.back())) |
| 4668 | return CS->getElement() == DS; |
| 4669 | return false; |
| 4670 | } |
| 4671 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4672 | //===----------------------------------------------------------------------===// |
| 4673 | // Function Body / Expression rewriting |
| 4674 | //===----------------------------------------------------------------------===// |
| 4675 | |
| 4676 | Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4677 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4678 | isa<DoStmt>(S) || isa<ForStmt>(S)) |
| 4679 | Stmts.push_back(S); |
| 4680 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 4681 | Stmts.push_back(S); |
Chris Lattner | 4824fcd | 2010-01-09 21:45:57 +0000 | [diff] [blame] | 4682 | ObjCBcLabelNo.push_back(++BcLabelCount); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4683 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4684 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4685 | // Pseudo-object operations and ivar references need special |
| 4686 | // treatment because we're going to recursively rewrite them. |
| 4687 | if (PseudoObjectExpr *PseudoOp = dyn_cast<PseudoObjectExpr>(S)) { |
| 4688 | if (isa<BinaryOperator>(PseudoOp->getSyntacticForm())) { |
| 4689 | return RewritePropertyOrImplicitSetter(PseudoOp); |
| 4690 | } else { |
| 4691 | return RewritePropertyOrImplicitGetter(PseudoOp); |
| 4692 | } |
| 4693 | } else if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) { |
| 4694 | return RewriteObjCIvarRefExpr(IvarRefExpr); |
| 4695 | } |
| 4696 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4697 | SourceRange OrigStmtRange = S->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4698 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4699 | // Perform a bottom up rewrite of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 4700 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4701 | if (*CI) { |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4702 | Stmt *childStmt = (*CI); |
| 4703 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(childStmt); |
Fariborz Jahanian | 1d01531 | 2011-04-11 21:17:02 +0000 | [diff] [blame] | 4704 | if (newStmt) { |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4705 | *CI = newStmt; |
Fariborz Jahanian | 1d01531 | 2011-04-11 21:17:02 +0000 | [diff] [blame] | 4706 | } |
Nick Lewycky | 7e74924 | 2010-10-31 21:07:24 +0000 | [diff] [blame] | 4707 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4708 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4709 | if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4710 | SmallVector<DeclRefExpr *, 8> InnerBlockDeclRefs; |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4711 | llvm::SmallPtrSet<const DeclContext *, 8> InnerContexts; |
| 4712 | InnerContexts.insert(BE->getBlockDecl()); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4713 | ImportedLocalExternalDecls.clear(); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4714 | GetInnerBlockDeclRefExprs(BE->getBody(), |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4715 | InnerBlockDeclRefs, InnerContexts); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4716 | // Rewrite the block body in place. |
Fariborz Jahanian | da4ad9f | 2010-11-08 18:37:50 +0000 | [diff] [blame] | 4717 | Stmt *SaveCurrentBody = CurrentBody; |
| 4718 | CurrentBody = BE->getBody(); |
| 4719 | PropParentMap = 0; |
Fariborz Jahanian | f23a0ff | 2011-08-02 20:28:46 +0000 | [diff] [blame] | 4720 | // block literal on rhs of a property-dot-sytax assignment |
| 4721 | // must be replaced by its synthesize ast so getRewrittenText |
| 4722 | // works as expected. In this case, what actually ends up on RHS |
| 4723 | // is the blockTranscribed which is the helper function for the |
| 4724 | // block literal; as in: self.c = ^() {[ace ARR];}; |
| 4725 | bool saveDisableReplaceStmt = DisableReplaceStmt; |
| 4726 | DisableReplaceStmt = false; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4727 | RewriteFunctionBodyOrGlobalInitializer(BE->getBody()); |
Fariborz Jahanian | f23a0ff | 2011-08-02 20:28:46 +0000 | [diff] [blame] | 4728 | DisableReplaceStmt = saveDisableReplaceStmt; |
Fariborz Jahanian | da4ad9f | 2010-11-08 18:37:50 +0000 | [diff] [blame] | 4729 | CurrentBody = SaveCurrentBody; |
| 4730 | PropParentMap = 0; |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4731 | ImportedLocalExternalDecls.clear(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4732 | // 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] | 4733 | std::string Str = Rewrite.getRewrittenText(BE->getSourceRange()); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4734 | RewrittenBlockExprs[BE] = Str; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4735 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4736 | Stmt *blockTranscribed = SynthBlockInitExpr(BE, InnerBlockDeclRefs); |
| 4737 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4738 | //blockTranscribed->dump(); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4739 | ReplaceStmt(S, blockTranscribed); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4740 | return blockTranscribed; |
| 4741 | } |
| 4742 | // Handle specific things. |
| 4743 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 4744 | return RewriteAtEncode(AtEncode); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4745 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4746 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 4747 | return RewriteAtSelector(AtSelector); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4748 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4749 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 4750 | return RewriteObjCStringLiteral(AtString); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4751 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4752 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4753 | #if 0 |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4754 | // Before we rewrite it, put the original message expression in a comment. |
| 4755 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 4756 | SourceLocation endLoc = MessExpr->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4757 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4758 | const char *startBuf = SM->getCharacterData(startLoc); |
| 4759 | const char *endBuf = SM->getCharacterData(endLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4760 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4761 | std::string messString; |
| 4762 | messString += "// "; |
| 4763 | messString.append(startBuf, endBuf-startBuf+1); |
| 4764 | messString += "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4765 | |
| 4766 | // FIXME: Missing definition of |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4767 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
| 4768 | // InsertText(startLoc, messString.c_str(), messString.size()); |
| 4769 | // Tried this, but it didn't work either... |
| 4770 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4771 | #endif |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4772 | return RewriteMessageExpr(MessExpr); |
| 4773 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4774 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4775 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 4776 | return RewriteObjCTryStmt(StmtTry); |
| 4777 | |
| 4778 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 4779 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 4780 | |
| 4781 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 4782 | return RewriteObjCThrowStmt(StmtThrow); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4783 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4784 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 4785 | return RewriteObjCProtocolExpr(ProtocolExp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4786 | |
| 4787 | if (ObjCForCollectionStmt *StmtForCollection = |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4788 | dyn_cast<ObjCForCollectionStmt>(S)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4789 | return RewriteObjCForCollectionStmt(StmtForCollection, |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4790 | OrigStmtRange.getEnd()); |
| 4791 | if (BreakStmt *StmtBreakStmt = |
| 4792 | dyn_cast<BreakStmt>(S)) |
| 4793 | return RewriteBreakStmt(StmtBreakStmt); |
| 4794 | if (ContinueStmt *StmtContinueStmt = |
| 4795 | dyn_cast<ContinueStmt>(S)) |
| 4796 | return RewriteContinueStmt(StmtContinueStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4797 | |
| 4798 | // 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] | 4799 | // and cast exprs. |
| 4800 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
| 4801 | // FIXME: What we're doing here is modifying the type-specifier that |
| 4802 | // precedes the first Decl. In the future the DeclGroup should have |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4803 | // a separate type-specifier that we can rewrite. |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4804 | // NOTE: We need to avoid rewriting the DeclStmt if it is within |
| 4805 | // the context of an ObjCForCollectionStmt. For example: |
| 4806 | // NSArray *someArray; |
| 4807 | // for (id <FooProtocol> index in someArray) ; |
| 4808 | // This is because RewriteObjCForCollectionStmt() does textual rewriting |
| 4809 | // and it depends on the original text locations/positions. |
Fariborz Jahanian | 42f1e65 | 2011-02-24 21:29:21 +0000 | [diff] [blame] | 4810 | if (Stmts.empty() || !IsDeclStmtInForeachHeader(DS)) |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4811 | RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4812 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4813 | // Blocks rewrite rules. |
| 4814 | for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); |
| 4815 | DI != DE; ++DI) { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 4816 | Decl *SD = *DI; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4817 | if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4818 | if (isTopLevelBlockPointerType(ND->getType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4819 | RewriteBlockPointerDecl(ND); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4820 | else if (ND->getType()->isFunctionPointerType()) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4821 | CheckFunctionPointerDecl(ND->getType(), ND); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 4822 | if (VarDecl *VD = dyn_cast<VarDecl>(SD)) { |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4823 | if (VD->hasAttr<BlocksAttr>()) { |
| 4824 | static unsigned uniqueByrefDeclCount = 0; |
| 4825 | assert(!BlockByRefDeclNo.count(ND) && |
| 4826 | "RewriteFunctionBodyOrGlobalInitializer: Duplicate byref decl"); |
| 4827 | BlockByRefDeclNo[ND] = uniqueByrefDeclCount++; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4828 | RewriteByRefVar(VD); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4829 | } |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 4830 | else |
| 4831 | RewriteTypeOfDecl(VD); |
| 4832 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4833 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4834 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4835 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4836 | RewriteBlockPointerDecl(TD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4837 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4838 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 4839 | } |
| 4840 | } |
| 4841 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4842 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4843 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) |
| 4844 | RewriteObjCQualifiedInterfaceTypes(CE); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4845 | |
| 4846 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4847 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 4848 | assert(!Stmts.empty() && "Statement stack is empty"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4849 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 4850 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4851 | && "Statement stack mismatch"); |
| 4852 | Stmts.pop_back(); |
| 4853 | } |
| 4854 | // Handle blocks rewriting. |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4855 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 4856 | ValueDecl *VD = DRE->getDecl(); |
| 4857 | if (VD->hasAttr<BlocksAttr>()) |
| 4858 | return RewriteBlockDeclRefExpr(DRE); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4859 | if (HasLocalVariableExternalStorage(VD)) |
| 4860 | return RewriteLocalVariableExternalStorage(DRE); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4861 | } |
| 4862 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4863 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4864 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4865 | Stmt *BlockCall = SynthesizeBlockCall(CE, CE->getCallee()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4866 | ReplaceStmt(S, BlockCall); |
| 4867 | return BlockCall; |
| 4868 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4869 | } |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4870 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4871 | RewriteCastExpr(CE); |
| 4872 | } |
| 4873 | #if 0 |
| 4874 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4875 | CastExpr *Replacement = new (Context) CastExpr(ICE->getType(), |
| 4876 | ICE->getSubExpr(), |
| 4877 | SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4878 | // Get the new text. |
| 4879 | std::string SStr; |
| 4880 | llvm::raw_string_ostream Buf(SStr); |
Richard Smith | d1420c6 | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 4881 | Replacement->printPretty(Buf); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4882 | const std::string &Str = Buf.str(); |
| 4883 | |
| 4884 | printf("CAST = %s\n", &Str[0]); |
| 4885 | InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 4886 | delete S; |
| 4887 | return Replacement; |
| 4888 | } |
| 4889 | #endif |
| 4890 | // Return this stmt unmodified. |
| 4891 | return S; |
| 4892 | } |
| 4893 | |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4894 | void RewriteObjC::RewriteRecordBody(RecordDecl *RD) { |
| 4895 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 4896 | e = RD->field_end(); i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4897 | FieldDecl *FD = *i; |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4898 | if (isTopLevelBlockPointerType(FD->getType())) |
| 4899 | RewriteBlockPointerDecl(FD); |
| 4900 | if (FD->getType()->isObjCQualifiedIdType() || |
| 4901 | FD->getType()->isObjCQualifiedInterfaceType()) |
| 4902 | RewriteObjCQualifiedInterfaceTypes(FD); |
| 4903 | } |
| 4904 | } |
| 4905 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4906 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 4907 | /// main file of the input. |
| 4908 | void RewriteObjC::HandleDeclInMainFile(Decl *D) { |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4909 | switch (D->getKind()) { |
| 4910 | case Decl::Function: { |
| 4911 | FunctionDecl *FD = cast<FunctionDecl>(D); |
| 4912 | if (FD->isOverloadedOperator()) |
| 4913 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4914 | |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4915 | // Since function prototypes don't have ParmDecl's, we check the function |
| 4916 | // prototype. This enables us to rewrite function declarations and |
| 4917 | // definitions using the same code. |
| 4918 | RewriteBlocksInFunctionProtoType(FD->getType(), FD); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4919 | |
Argyrios Kyrtzidis | 9335df3 | 2012-02-12 04:48:45 +0000 | [diff] [blame] | 4920 | if (!FD->isThisDeclarationADefinition()) |
| 4921 | break; |
| 4922 | |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4923 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
| 4924 | if (CompoundStmt *Body = dyn_cast_or_null<CompoundStmt>(FD->getBody())) { |
| 4925 | CurFunctionDef = FD; |
| 4926 | CurFunctionDeclToDeclareForBlock = FD; |
| 4927 | CurrentBody = Body; |
| 4928 | Body = |
| 4929 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 4930 | FD->setBody(Body); |
| 4931 | CurrentBody = 0; |
| 4932 | if (PropParentMap) { |
| 4933 | delete PropParentMap; |
| 4934 | PropParentMap = 0; |
| 4935 | } |
| 4936 | // This synthesizes and inserts the block "impl" struct, invoke function, |
| 4937 | // and any copy/dispose helper functions. |
| 4938 | InsertBlockLiteralsWithinFunction(FD); |
| 4939 | CurFunctionDef = 0; |
| 4940 | CurFunctionDeclToDeclareForBlock = 0; |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4941 | } |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4942 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4943 | } |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4944 | case Decl::ObjCMethod: { |
| 4945 | ObjCMethodDecl *MD = cast<ObjCMethodDecl>(D); |
| 4946 | if (CompoundStmt *Body = MD->getCompoundBody()) { |
| 4947 | CurMethodDef = MD; |
| 4948 | CurrentBody = Body; |
| 4949 | Body = |
| 4950 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 4951 | MD->setBody(Body); |
| 4952 | CurrentBody = 0; |
| 4953 | if (PropParentMap) { |
| 4954 | delete PropParentMap; |
| 4955 | PropParentMap = 0; |
| 4956 | } |
| 4957 | InsertBlockLiteralsWithinMethod(MD); |
| 4958 | CurMethodDef = 0; |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4959 | } |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4960 | break; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4961 | } |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4962 | case Decl::ObjCImplementation: { |
| 4963 | ObjCImplementationDecl *CI = cast<ObjCImplementationDecl>(D); |
| 4964 | ClassImplementation.push_back(CI); |
| 4965 | break; |
| 4966 | } |
| 4967 | case Decl::ObjCCategoryImpl: { |
| 4968 | ObjCCategoryImplDecl *CI = cast<ObjCCategoryImplDecl>(D); |
| 4969 | CategoryImplementation.push_back(CI); |
| 4970 | break; |
| 4971 | } |
| 4972 | case Decl::Var: { |
| 4973 | VarDecl *VD = cast<VarDecl>(D); |
| 4974 | RewriteObjCQualifiedInterfaceTypes(VD); |
| 4975 | if (isTopLevelBlockPointerType(VD->getType())) |
| 4976 | RewriteBlockPointerDecl(VD); |
| 4977 | else if (VD->getType()->isFunctionPointerType()) { |
| 4978 | CheckFunctionPointerDecl(VD->getType(), VD); |
| 4979 | if (VD->getInit()) { |
| 4980 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
| 4981 | RewriteCastExpr(CE); |
| 4982 | } |
| 4983 | } |
| 4984 | } else if (VD->getType()->isRecordType()) { |
| 4985 | RecordDecl *RD = VD->getType()->getAs<RecordType>()->getDecl(); |
| 4986 | if (RD->isCompleteDefinition()) |
| 4987 | RewriteRecordBody(RD); |
| 4988 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4989 | if (VD->getInit()) { |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 4990 | GlobalVarDecl = VD; |
| 4991 | CurrentBody = VD->getInit(); |
| 4992 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
| 4993 | CurrentBody = 0; |
| 4994 | if (PropParentMap) { |
| 4995 | delete PropParentMap; |
| 4996 | PropParentMap = 0; |
| 4997 | } |
| 4998 | SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), VD->getName()); |
| 4999 | GlobalVarDecl = 0; |
| 5000 | |
| 5001 | // This is needed for blocks. |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 5002 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 5003 | RewriteCastExpr(CE); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5004 | } |
| 5005 | } |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 5006 | break; |
| 5007 | } |
| 5008 | case Decl::TypeAlias: |
| 5009 | case Decl::Typedef: { |
| 5010 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
| 5011 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
| 5012 | RewriteBlockPointerDecl(TD); |
| 5013 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
| 5014 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 5015 | } |
| 5016 | break; |
| 5017 | } |
| 5018 | case Decl::CXXRecord: |
| 5019 | case Decl::Record: { |
| 5020 | RecordDecl *RD = cast<RecordDecl>(D); |
| 5021 | if (RD->isCompleteDefinition()) |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5022 | RewriteRecordBody(RD); |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 5023 | break; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5024 | } |
Fariborz Jahanian | 02f8396 | 2011-12-05 22:59:54 +0000 | [diff] [blame] | 5025 | default: |
| 5026 | break; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5027 | } |
| 5028 | // Nothing yet. |
| 5029 | } |
| 5030 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 5031 | void RewriteObjC::HandleTranslationUnit(ASTContext &C) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5032 | if (Diags.hasErrorOccurred()) |
| 5033 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5034 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5035 | RewriteInclude(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5036 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5037 | // Here's a great place to add any extra declarations that may be needed. |
| 5038 | // Write out meta data for each @protocol(<expr>). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5039 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5040 | E = ProtocolExprDecls.end(); I != E; ++I) |
| 5041 | RewriteObjCProtocolMetaData(*I, "", "", Preamble); |
| 5042 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5043 | InsertText(SM->getLocForStartOfFile(MainFileID), Preamble, false); |
Steve Naroff | 0aab796 | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5044 | if (ClassImplementation.size() || CategoryImplementation.size()) |
| 5045 | RewriteImplementations(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5046 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5047 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 5048 | // we are done. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5049 | if (const RewriteBuffer *RewriteBuf = |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5050 | Rewrite.getRewriteBufferFor(MainFileID)) { |
| 5051 | //printf("Changed:\n"); |
| 5052 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 5053 | } else { |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5054 | llvm::errs() << "No changes\n"; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5055 | } |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 5056 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5057 | if (ClassImplementation.size() || CategoryImplementation.size() || |
| 5058 | ProtocolExprDecls.size()) { |
Steve Naroff | 0aab796 | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5059 | // Rewrite Objective-c meta data* |
| 5060 | std::string ResultStr; |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 5061 | RewriteMetaDataIntoBuffer(ResultStr); |
Steve Naroff | 0aab796 | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5062 | // Emit metadata. |
| 5063 | *OutFile << ResultStr; |
| 5064 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5065 | OutFile->flush(); |
| 5066 | } |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5067 | |
| 5068 | void RewriteObjCFragileABI::Initialize(ASTContext &context) { |
| 5069 | InitializeCommon(context); |
| 5070 | |
| 5071 | // declaring objc_selector outside the parameter list removes a silly |
| 5072 | // scope related warning... |
| 5073 | if (IsHeader) |
| 5074 | Preamble = "#pragma once\n"; |
| 5075 | Preamble += "struct objc_selector; struct objc_class;\n"; |
| 5076 | Preamble += "struct __rw_objc_super { struct objc_object *object; "; |
| 5077 | Preamble += "struct objc_object *superClass; "; |
| 5078 | if (LangOpts.MicrosoftExt) { |
| 5079 | // Add a constructor for creating temporary objects. |
| 5080 | Preamble += "__rw_objc_super(struct objc_object *o, struct objc_object *s) " |
| 5081 | ": "; |
| 5082 | Preamble += "object(o), superClass(s) {} "; |
| 5083 | } |
| 5084 | Preamble += "};\n"; |
| 5085 | Preamble += "#ifndef _REWRITER_typedef_Protocol\n"; |
| 5086 | Preamble += "typedef struct objc_object Protocol;\n"; |
| 5087 | Preamble += "#define _REWRITER_typedef_Protocol\n"; |
| 5088 | Preamble += "#endif\n"; |
| 5089 | if (LangOpts.MicrosoftExt) { |
| 5090 | Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n"; |
| 5091 | Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n"; |
| 5092 | } else |
| 5093 | Preamble += "#define __OBJC_RW_DLLIMPORT extern\n"; |
| 5094 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend"; |
| 5095 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
| 5096 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper"; |
| 5097 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
| 5098 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object* objc_msgSend_stret"; |
| 5099 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
| 5100 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object* objc_msgSendSuper_stret"; |
| 5101 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
| 5102 | Preamble += "__OBJC_RW_DLLIMPORT double objc_msgSend_fpret"; |
| 5103 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
| 5104 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getClass"; |
| 5105 | Preamble += "(const char *);\n"; |
| 5106 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_class *class_getSuperclass"; |
| 5107 | Preamble += "(struct objc_class *);\n"; |
| 5108 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getMetaClass"; |
| 5109 | Preamble += "(const char *);\n"; |
| 5110 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw(struct objc_object *);\n"; |
| 5111 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_enter(void *);\n"; |
| 5112 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_exit(void *);\n"; |
| 5113 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_exception_extract(void *);\n"; |
| 5114 | Preamble += "__OBJC_RW_DLLIMPORT int objc_exception_match"; |
| 5115 | Preamble += "(struct objc_class *, struct objc_object *);\n"; |
| 5116 | // @synchronized hooks. |
Aaron Ballman | 2d234d73 | 2012-09-06 16:44:16 +0000 | [diff] [blame] | 5117 | Preamble += "__OBJC_RW_DLLIMPORT int objc_sync_enter(struct objc_object *);\n"; |
| 5118 | Preamble += "__OBJC_RW_DLLIMPORT int objc_sync_exit(struct objc_object *);\n"; |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5119 | Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n"; |
| 5120 | Preamble += "#ifndef __FASTENUMERATIONSTATE\n"; |
| 5121 | Preamble += "struct __objcFastEnumerationState {\n\t"; |
| 5122 | Preamble += "unsigned long state;\n\t"; |
| 5123 | Preamble += "void **itemsPtr;\n\t"; |
| 5124 | Preamble += "unsigned long *mutationsPtr;\n\t"; |
| 5125 | Preamble += "unsigned long extra[5];\n};\n"; |
| 5126 | Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n"; |
| 5127 | Preamble += "#define __FASTENUMERATIONSTATE\n"; |
| 5128 | Preamble += "#endif\n"; |
| 5129 | Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n"; |
| 5130 | Preamble += "struct __NSConstantStringImpl {\n"; |
| 5131 | Preamble += " int *isa;\n"; |
| 5132 | Preamble += " int flags;\n"; |
| 5133 | Preamble += " char *str;\n"; |
| 5134 | Preamble += " long length;\n"; |
| 5135 | Preamble += "};\n"; |
| 5136 | Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n"; |
| 5137 | Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n"; |
| 5138 | Preamble += "#else\n"; |
| 5139 | Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n"; |
| 5140 | Preamble += "#endif\n"; |
| 5141 | Preamble += "#define __NSCONSTANTSTRINGIMPL\n"; |
| 5142 | Preamble += "#endif\n"; |
| 5143 | // Blocks preamble. |
| 5144 | Preamble += "#ifndef BLOCK_IMPL\n"; |
| 5145 | Preamble += "#define BLOCK_IMPL\n"; |
| 5146 | Preamble += "struct __block_impl {\n"; |
| 5147 | Preamble += " void *isa;\n"; |
| 5148 | Preamble += " int Flags;\n"; |
| 5149 | Preamble += " int Reserved;\n"; |
| 5150 | Preamble += " void *FuncPtr;\n"; |
| 5151 | Preamble += "};\n"; |
| 5152 | Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n"; |
| 5153 | Preamble += "#ifdef __OBJC_EXPORT_BLOCKS\n"; |
| 5154 | Preamble += "extern \"C\" __declspec(dllexport) " |
| 5155 | "void _Block_object_assign(void *, const void *, const int);\n"; |
| 5156 | Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_dispose(const void *, const int);\n"; |
| 5157 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteGlobalBlock[32];\n"; |
| 5158 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteStackBlock[32];\n"; |
| 5159 | Preamble += "#else\n"; |
| 5160 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_assign(void *, const void *, const int);\n"; |
| 5161 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_dispose(const void *, const int);\n"; |
| 5162 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteGlobalBlock[32];\n"; |
| 5163 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteStackBlock[32];\n"; |
| 5164 | Preamble += "#endif\n"; |
| 5165 | Preamble += "#endif\n"; |
| 5166 | if (LangOpts.MicrosoftExt) { |
| 5167 | Preamble += "#undef __OBJC_RW_DLLIMPORT\n"; |
| 5168 | Preamble += "#undef __OBJC_RW_STATICIMPORT\n"; |
| 5169 | Preamble += "#ifndef KEEP_ATTRIBUTES\n"; // We use this for clang tests. |
| 5170 | Preamble += "#define __attribute__(X)\n"; |
| 5171 | Preamble += "#endif\n"; |
| 5172 | Preamble += "#define __weak\n"; |
| 5173 | } |
| 5174 | else { |
| 5175 | Preamble += "#define __block\n"; |
| 5176 | Preamble += "#define __weak\n"; |
| 5177 | } |
| 5178 | // NOTE! Windows uses LLP64 for 64bit mode. So, cast pointer to long long |
| 5179 | // as this avoids warning in any 64bit/32bit compilation model. |
| 5180 | Preamble += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((long long) &((TYPE *)0)->MEMBER)\n"; |
| 5181 | } |
| 5182 | |
| 5183 | /// RewriteIvarOffsetComputation - This rutine synthesizes computation of |
| 5184 | /// ivar offset. |
| 5185 | void RewriteObjCFragileABI::RewriteIvarOffsetComputation(ObjCIvarDecl *ivar, |
| 5186 | std::string &Result) { |
| 5187 | if (ivar->isBitField()) { |
| 5188 | // FIXME: The hack below doesn't work for bitfields. For now, we simply |
| 5189 | // place all bitfields at offset 0. |
| 5190 | Result += "0"; |
| 5191 | } else { |
| 5192 | Result += "__OFFSETOFIVAR__(struct "; |
| 5193 | Result += ivar->getContainingInterface()->getNameAsString(); |
| 5194 | if (LangOpts.MicrosoftExt) |
| 5195 | Result += "_IMPL"; |
| 5196 | Result += ", "; |
| 5197 | Result += ivar->getNameAsString(); |
| 5198 | Result += ")"; |
| 5199 | } |
| 5200 | } |
| 5201 | |
| 5202 | /// RewriteObjCProtocolMetaData - Rewrite protocols meta-data. |
| 5203 | void RewriteObjCFragileABI::RewriteObjCProtocolMetaData( |
| 5204 | ObjCProtocolDecl *PDecl, StringRef prefix, |
| 5205 | StringRef ClassName, std::string &Result) { |
| 5206 | static bool objc_protocol_methods = false; |
| 5207 | |
| 5208 | // Output struct protocol_methods holder of method selector and type. |
Douglas Gregor | 5e2a1ff | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 5209 | if (!objc_protocol_methods && PDecl->hasDefinition()) { |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5210 | /* struct protocol_methods { |
| 5211 | SEL _cmd; |
| 5212 | char *method_types; |
| 5213 | } |
| 5214 | */ |
| 5215 | Result += "\nstruct _protocol_methods {\n"; |
| 5216 | Result += "\tstruct objc_selector *_cmd;\n"; |
| 5217 | Result += "\tchar *method_types;\n"; |
| 5218 | Result += "};\n"; |
| 5219 | |
| 5220 | objc_protocol_methods = true; |
| 5221 | } |
| 5222 | // Do not synthesize the protocol more than once. |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 5223 | if (ObjCSynthesizedProtocols.count(PDecl->getCanonicalDecl())) |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5224 | return; |
| 5225 | |
Douglas Gregor | 61cc296 | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 5226 | if (ObjCProtocolDecl *Def = PDecl->getDefinition()) |
| 5227 | PDecl = Def; |
| 5228 | |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5229 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 5230 | unsigned NumMethods = std::distance(PDecl->instmeth_begin(), |
| 5231 | PDecl->instmeth_end()); |
| 5232 | /* struct _objc_protocol_method_list { |
| 5233 | int protocol_method_count; |
| 5234 | struct protocol_methods protocols[]; |
| 5235 | } |
| 5236 | */ |
| 5237 | Result += "\nstatic struct {\n"; |
| 5238 | Result += "\tint protocol_method_count;\n"; |
| 5239 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 5240 | Result += utostr(NumMethods); |
| 5241 | Result += "];\n} _OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 5242 | Result += PDecl->getNameAsString(); |
| 5243 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= " |
| 5244 | "{\n\t" + utostr(NumMethods) + "\n"; |
| 5245 | |
| 5246 | // Output instance methods declared in this protocol. |
| 5247 | for (ObjCProtocolDecl::instmeth_iterator |
| 5248 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
| 5249 | I != E; ++I) { |
| 5250 | if (I == PDecl->instmeth_begin()) |
| 5251 | Result += "\t ,{{(struct objc_selector *)\""; |
| 5252 | else |
| 5253 | Result += "\t ,{(struct objc_selector *)\""; |
| 5254 | Result += (*I)->getSelector().getAsString(); |
| 5255 | std::string MethodTypeString; |
| 5256 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 5257 | Result += "\", \""; |
| 5258 | Result += MethodTypeString; |
| 5259 | Result += "\"}\n"; |
| 5260 | } |
| 5261 | Result += "\t }\n};\n"; |
| 5262 | } |
| 5263 | |
| 5264 | // Output class methods declared in this protocol. |
| 5265 | unsigned NumMethods = std::distance(PDecl->classmeth_begin(), |
| 5266 | PDecl->classmeth_end()); |
| 5267 | if (NumMethods > 0) { |
| 5268 | /* struct _objc_protocol_method_list { |
| 5269 | int protocol_method_count; |
| 5270 | struct protocol_methods protocols[]; |
| 5271 | } |
| 5272 | */ |
| 5273 | Result += "\nstatic struct {\n"; |
| 5274 | Result += "\tint protocol_method_count;\n"; |
| 5275 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 5276 | Result += utostr(NumMethods); |
| 5277 | Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_"; |
| 5278 | Result += PDecl->getNameAsString(); |
| 5279 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 5280 | "{\n\t"; |
| 5281 | Result += utostr(NumMethods); |
| 5282 | Result += "\n"; |
| 5283 | |
| 5284 | // Output instance methods declared in this protocol. |
| 5285 | for (ObjCProtocolDecl::classmeth_iterator |
| 5286 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
| 5287 | I != E; ++I) { |
| 5288 | if (I == PDecl->classmeth_begin()) |
| 5289 | Result += "\t ,{{(struct objc_selector *)\""; |
| 5290 | else |
| 5291 | Result += "\t ,{(struct objc_selector *)\""; |
| 5292 | Result += (*I)->getSelector().getAsString(); |
| 5293 | std::string MethodTypeString; |
| 5294 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 5295 | Result += "\", \""; |
| 5296 | Result += MethodTypeString; |
| 5297 | Result += "\"}\n"; |
| 5298 | } |
| 5299 | Result += "\t }\n};\n"; |
| 5300 | } |
| 5301 | |
| 5302 | // Output: |
| 5303 | /* struct _objc_protocol { |
| 5304 | // Objective-C 1.0 extensions |
| 5305 | struct _objc_protocol_extension *isa; |
| 5306 | char *protocol_name; |
| 5307 | struct _objc_protocol **protocol_list; |
| 5308 | struct _objc_protocol_method_list *instance_methods; |
| 5309 | struct _objc_protocol_method_list *class_methods; |
| 5310 | }; |
| 5311 | */ |
| 5312 | static bool objc_protocol = false; |
| 5313 | if (!objc_protocol) { |
| 5314 | Result += "\nstruct _objc_protocol {\n"; |
| 5315 | Result += "\tstruct _objc_protocol_extension *isa;\n"; |
| 5316 | Result += "\tchar *protocol_name;\n"; |
| 5317 | Result += "\tstruct _objc_protocol **protocol_list;\n"; |
| 5318 | Result += "\tstruct _objc_protocol_method_list *instance_methods;\n"; |
| 5319 | Result += "\tstruct _objc_protocol_method_list *class_methods;\n"; |
| 5320 | Result += "};\n"; |
| 5321 | |
| 5322 | objc_protocol = true; |
| 5323 | } |
| 5324 | |
| 5325 | Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_"; |
| 5326 | Result += PDecl->getNameAsString(); |
| 5327 | Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= " |
| 5328 | "{\n\t0, \""; |
| 5329 | Result += PDecl->getNameAsString(); |
| 5330 | Result += "\", 0, "; |
| 5331 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 5332 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 5333 | Result += PDecl->getNameAsString(); |
| 5334 | Result += ", "; |
| 5335 | } |
| 5336 | else |
| 5337 | Result += "0, "; |
| 5338 | if (PDecl->classmeth_begin() != PDecl->classmeth_end()) { |
| 5339 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
| 5340 | Result += PDecl->getNameAsString(); |
| 5341 | Result += "\n"; |
| 5342 | } |
| 5343 | else |
| 5344 | Result += "0\n"; |
| 5345 | Result += "};\n"; |
| 5346 | |
| 5347 | // Mark this protocol as having been generated. |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 5348 | if (!ObjCSynthesizedProtocols.insert(PDecl->getCanonicalDecl())) |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5349 | llvm_unreachable("protocol already synthesized"); |
| 5350 | |
| 5351 | } |
| 5352 | |
| 5353 | void RewriteObjCFragileABI::RewriteObjCProtocolListMetaData( |
| 5354 | const ObjCList<ObjCProtocolDecl> &Protocols, |
| 5355 | StringRef prefix, StringRef ClassName, |
| 5356 | std::string &Result) { |
| 5357 | if (Protocols.empty()) return; |
| 5358 | |
| 5359 | for (unsigned i = 0; i != Protocols.size(); i++) |
| 5360 | RewriteObjCProtocolMetaData(Protocols[i], prefix, ClassName, Result); |
| 5361 | |
| 5362 | // Output the top lovel protocol meta-data for the class. |
| 5363 | /* struct _objc_protocol_list { |
| 5364 | struct _objc_protocol_list *next; |
| 5365 | int protocol_count; |
| 5366 | struct _objc_protocol *class_protocols[]; |
| 5367 | } |
| 5368 | */ |
| 5369 | Result += "\nstatic struct {\n"; |
| 5370 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 5371 | Result += "\tint protocol_count;\n"; |
| 5372 | Result += "\tstruct _objc_protocol *class_protocols["; |
| 5373 | Result += utostr(Protocols.size()); |
| 5374 | Result += "];\n} _OBJC_"; |
| 5375 | Result += prefix; |
| 5376 | Result += "_PROTOCOLS_"; |
| 5377 | Result += ClassName; |
| 5378 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 5379 | "{\n\t0, "; |
| 5380 | Result += utostr(Protocols.size()); |
| 5381 | Result += "\n"; |
| 5382 | |
| 5383 | Result += "\t,{&_OBJC_PROTOCOL_"; |
| 5384 | Result += Protocols[0]->getNameAsString(); |
| 5385 | Result += " \n"; |
| 5386 | |
| 5387 | for (unsigned i = 1; i != Protocols.size(); i++) { |
| 5388 | Result += "\t ,&_OBJC_PROTOCOL_"; |
| 5389 | Result += Protocols[i]->getNameAsString(); |
| 5390 | Result += "\n"; |
| 5391 | } |
| 5392 | Result += "\t }\n};\n"; |
| 5393 | } |
| 5394 | |
| 5395 | void RewriteObjCFragileABI::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
| 5396 | std::string &Result) { |
| 5397 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
| 5398 | |
| 5399 | // Explicitly declared @interface's are already synthesized. |
| 5400 | if (CDecl->isImplicitInterfaceDecl()) { |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 5401 | // FIXME: Implementation of a class with no @interface (legacy) does not |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5402 | // produce correct synthesis as yet. |
| 5403 | RewriteObjCInternalStruct(CDecl, Result); |
| 5404 | } |
| 5405 | |
| 5406 | // Build _objc_ivar_list metadata for classes ivars if needed |
| 5407 | unsigned NumIvars = !IDecl->ivar_empty() |
| 5408 | ? IDecl->ivar_size() |
| 5409 | : (CDecl ? CDecl->ivar_size() : 0); |
| 5410 | if (NumIvars > 0) { |
| 5411 | static bool objc_ivar = false; |
| 5412 | if (!objc_ivar) { |
| 5413 | /* struct _objc_ivar { |
| 5414 | char *ivar_name; |
| 5415 | char *ivar_type; |
| 5416 | int ivar_offset; |
| 5417 | }; |
| 5418 | */ |
| 5419 | Result += "\nstruct _objc_ivar {\n"; |
| 5420 | Result += "\tchar *ivar_name;\n"; |
| 5421 | Result += "\tchar *ivar_type;\n"; |
| 5422 | Result += "\tint ivar_offset;\n"; |
| 5423 | Result += "};\n"; |
| 5424 | |
| 5425 | objc_ivar = true; |
| 5426 | } |
| 5427 | |
| 5428 | /* struct { |
| 5429 | int ivar_count; |
| 5430 | struct _objc_ivar ivar_list[nIvars]; |
| 5431 | }; |
| 5432 | */ |
| 5433 | Result += "\nstatic struct {\n"; |
| 5434 | Result += "\tint ivar_count;\n"; |
| 5435 | Result += "\tstruct _objc_ivar ivar_list["; |
| 5436 | Result += utostr(NumIvars); |
| 5437 | Result += "];\n} _OBJC_INSTANCE_VARIABLES_"; |
| 5438 | Result += IDecl->getNameAsString(); |
| 5439 | Result += " __attribute__ ((used, section (\"__OBJC, __instance_vars\")))= " |
| 5440 | "{\n\t"; |
| 5441 | Result += utostr(NumIvars); |
| 5442 | Result += "\n"; |
| 5443 | |
| 5444 | ObjCInterfaceDecl::ivar_iterator IVI, IVE; |
| 5445 | SmallVector<ObjCIvarDecl *, 8> IVars; |
| 5446 | if (!IDecl->ivar_empty()) { |
| 5447 | for (ObjCInterfaceDecl::ivar_iterator |
| 5448 | IV = IDecl->ivar_begin(), IVEnd = IDecl->ivar_end(); |
| 5449 | IV != IVEnd; ++IV) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5450 | IVars.push_back(*IV); |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5451 | IVI = IDecl->ivar_begin(); |
| 5452 | IVE = IDecl->ivar_end(); |
| 5453 | } else { |
| 5454 | IVI = CDecl->ivar_begin(); |
| 5455 | IVE = CDecl->ivar_end(); |
| 5456 | } |
| 5457 | Result += "\t,{{\""; |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5458 | Result += IVI->getNameAsString(); |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5459 | Result += "\", \""; |
| 5460 | std::string TmpString, StrEncoding; |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5461 | Context->getObjCEncodingForType(IVI->getType(), TmpString, *IVI); |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5462 | QuoteDoublequotes(TmpString, StrEncoding); |
| 5463 | Result += StrEncoding; |
| 5464 | Result += "\", "; |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5465 | RewriteIvarOffsetComputation(*IVI, Result); |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5466 | Result += "}\n"; |
| 5467 | for (++IVI; IVI != IVE; ++IVI) { |
| 5468 | Result += "\t ,{\""; |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5469 | Result += IVI->getNameAsString(); |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5470 | Result += "\", \""; |
| 5471 | std::string TmpString, StrEncoding; |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5472 | Context->getObjCEncodingForType(IVI->getType(), TmpString, *IVI); |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5473 | QuoteDoublequotes(TmpString, StrEncoding); |
| 5474 | Result += StrEncoding; |
| 5475 | Result += "\", "; |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5476 | RewriteIvarOffsetComputation(*IVI, Result); |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5477 | Result += "}\n"; |
| 5478 | } |
| 5479 | |
| 5480 | Result += "\t }\n};\n"; |
| 5481 | } |
| 5482 | |
| 5483 | // Build _objc_method_list for class's instance methods if needed |
| 5484 | SmallVector<ObjCMethodDecl *, 32> |
| 5485 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
| 5486 | |
| 5487 | // If any of our property implementations have associated getters or |
| 5488 | // setters, produce metadata for them as well. |
| 5489 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 5490 | PropEnd = IDecl->propimpl_end(); |
| 5491 | Prop != PropEnd; ++Prop) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5492 | if (Prop->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
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 | if (!Prop->getPropertyIvarDecl()) |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5495 | continue; |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5496 | ObjCPropertyDecl *PD = Prop->getPropertyDecl(); |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5497 | if (!PD) |
| 5498 | continue; |
| 5499 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 5500 | if (!Getter->isDefined()) |
| 5501 | InstanceMethods.push_back(Getter); |
| 5502 | if (PD->isReadOnly()) |
| 5503 | continue; |
| 5504 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 5505 | if (!Setter->isDefined()) |
| 5506 | InstanceMethods.push_back(Setter); |
| 5507 | } |
| 5508 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
| 5509 | true, "", IDecl->getName(), Result); |
| 5510 | |
| 5511 | // Build _objc_method_list for class's class methods if needed |
| 5512 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
| 5513 | false, "", IDecl->getName(), Result); |
| 5514 | |
| 5515 | // Protocols referenced in class declaration? |
| 5516 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), |
| 5517 | "CLASS", CDecl->getName(), Result); |
| 5518 | |
| 5519 | // Declaration of class/meta-class metadata |
| 5520 | /* struct _objc_class { |
| 5521 | struct _objc_class *isa; // or const char *root_class_name when metadata |
| 5522 | const char *super_class_name; |
| 5523 | char *name; |
| 5524 | long version; |
| 5525 | long info; |
| 5526 | long instance_size; |
| 5527 | struct _objc_ivar_list *ivars; |
| 5528 | struct _objc_method_list *methods; |
| 5529 | struct objc_cache *cache; |
| 5530 | struct objc_protocol_list *protocols; |
| 5531 | const char *ivar_layout; |
| 5532 | struct _objc_class_ext *ext; |
| 5533 | }; |
| 5534 | */ |
| 5535 | static bool objc_class = false; |
| 5536 | if (!objc_class) { |
| 5537 | Result += "\nstruct _objc_class {\n"; |
| 5538 | Result += "\tstruct _objc_class *isa;\n"; |
| 5539 | Result += "\tconst char *super_class_name;\n"; |
| 5540 | Result += "\tchar *name;\n"; |
| 5541 | Result += "\tlong version;\n"; |
| 5542 | Result += "\tlong info;\n"; |
| 5543 | Result += "\tlong instance_size;\n"; |
| 5544 | Result += "\tstruct _objc_ivar_list *ivars;\n"; |
| 5545 | Result += "\tstruct _objc_method_list *methods;\n"; |
| 5546 | Result += "\tstruct objc_cache *cache;\n"; |
| 5547 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 5548 | Result += "\tconst char *ivar_layout;\n"; |
| 5549 | Result += "\tstruct _objc_class_ext *ext;\n"; |
| 5550 | Result += "};\n"; |
| 5551 | objc_class = true; |
| 5552 | } |
| 5553 | |
| 5554 | // Meta-class metadata generation. |
| 5555 | ObjCInterfaceDecl *RootClass = 0; |
| 5556 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
| 5557 | while (SuperClass) { |
| 5558 | RootClass = SuperClass; |
| 5559 | SuperClass = SuperClass->getSuperClass(); |
| 5560 | } |
| 5561 | SuperClass = CDecl->getSuperClass(); |
| 5562 | |
| 5563 | Result += "\nstatic struct _objc_class _OBJC_METACLASS_"; |
| 5564 | Result += CDecl->getNameAsString(); |
| 5565 | Result += " __attribute__ ((used, section (\"__OBJC, __meta_class\")))= " |
| 5566 | "{\n\t(struct _objc_class *)\""; |
| 5567 | Result += (RootClass ? RootClass->getNameAsString() : CDecl->getNameAsString()); |
| 5568 | Result += "\""; |
| 5569 | |
| 5570 | if (SuperClass) { |
| 5571 | Result += ", \""; |
| 5572 | Result += SuperClass->getNameAsString(); |
| 5573 | Result += "\", \""; |
| 5574 | Result += CDecl->getNameAsString(); |
| 5575 | Result += "\""; |
| 5576 | } |
| 5577 | else { |
| 5578 | Result += ", 0, \""; |
| 5579 | Result += CDecl->getNameAsString(); |
| 5580 | Result += "\""; |
| 5581 | } |
| 5582 | // Set 'ivars' field for root class to 0. ObjC1 runtime does not use it. |
| 5583 | // 'info' field is initialized to CLS_META(2) for metaclass |
| 5584 | Result += ", 0,2, sizeof(struct _objc_class), 0"; |
| 5585 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
| 5586 | Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_"; |
| 5587 | Result += IDecl->getNameAsString(); |
| 5588 | Result += "\n"; |
| 5589 | } |
| 5590 | else |
| 5591 | Result += ", 0\n"; |
| 5592 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
| 5593 | Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_"; |
| 5594 | Result += CDecl->getNameAsString(); |
| 5595 | Result += ",0,0\n"; |
| 5596 | } |
| 5597 | else |
| 5598 | Result += "\t,0,0,0,0\n"; |
| 5599 | Result += "};\n"; |
| 5600 | |
| 5601 | // class metadata generation. |
| 5602 | Result += "\nstatic struct _objc_class _OBJC_CLASS_"; |
| 5603 | Result += CDecl->getNameAsString(); |
| 5604 | Result += " __attribute__ ((used, section (\"__OBJC, __class\")))= " |
| 5605 | "{\n\t&_OBJC_METACLASS_"; |
| 5606 | Result += CDecl->getNameAsString(); |
| 5607 | if (SuperClass) { |
| 5608 | Result += ", \""; |
| 5609 | Result += SuperClass->getNameAsString(); |
| 5610 | Result += "\", \""; |
| 5611 | Result += CDecl->getNameAsString(); |
| 5612 | Result += "\""; |
| 5613 | } |
| 5614 | else { |
| 5615 | Result += ", 0, \""; |
| 5616 | Result += CDecl->getNameAsString(); |
| 5617 | Result += "\""; |
| 5618 | } |
| 5619 | // 'info' field is initialized to CLS_CLASS(1) for class |
| 5620 | Result += ", 0,1"; |
| 5621 | if (!ObjCSynthesizedStructs.count(CDecl)) |
| 5622 | Result += ",0"; |
| 5623 | else { |
| 5624 | // class has size. Must synthesize its size. |
| 5625 | Result += ",sizeof(struct "; |
| 5626 | Result += CDecl->getNameAsString(); |
| 5627 | if (LangOpts.MicrosoftExt) |
| 5628 | Result += "_IMPL"; |
| 5629 | Result += ")"; |
| 5630 | } |
| 5631 | if (NumIvars > 0) { |
| 5632 | Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_"; |
| 5633 | Result += CDecl->getNameAsString(); |
| 5634 | Result += "\n\t"; |
| 5635 | } |
| 5636 | else |
| 5637 | Result += ",0"; |
| 5638 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
| 5639 | Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_"; |
| 5640 | Result += CDecl->getNameAsString(); |
| 5641 | Result += ", 0\n\t"; |
| 5642 | } |
| 5643 | else |
| 5644 | Result += ",0,0"; |
| 5645 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
| 5646 | Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_"; |
| 5647 | Result += CDecl->getNameAsString(); |
| 5648 | Result += ", 0,0\n"; |
| 5649 | } |
| 5650 | else |
| 5651 | Result += ",0,0,0\n"; |
| 5652 | Result += "};\n"; |
| 5653 | } |
| 5654 | |
| 5655 | void RewriteObjCFragileABI::RewriteMetaDataIntoBuffer(std::string &Result) { |
| 5656 | int ClsDefCount = ClassImplementation.size(); |
| 5657 | int CatDefCount = CategoryImplementation.size(); |
| 5658 | |
| 5659 | // For each implemented class, write out all its meta data. |
| 5660 | for (int i = 0; i < ClsDefCount; i++) |
| 5661 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
| 5662 | |
| 5663 | // For each implemented category, write out all its meta data. |
| 5664 | for (int i = 0; i < CatDefCount; i++) |
| 5665 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
| 5666 | |
| 5667 | // Write objc_symtab metadata |
| 5668 | /* |
| 5669 | struct _objc_symtab |
| 5670 | { |
| 5671 | long sel_ref_cnt; |
| 5672 | SEL *refs; |
| 5673 | short cls_def_cnt; |
| 5674 | short cat_def_cnt; |
| 5675 | void *defs[cls_def_cnt + cat_def_cnt]; |
| 5676 | }; |
| 5677 | */ |
| 5678 | |
| 5679 | Result += "\nstruct _objc_symtab {\n"; |
| 5680 | Result += "\tlong sel_ref_cnt;\n"; |
| 5681 | Result += "\tSEL *refs;\n"; |
| 5682 | Result += "\tshort cls_def_cnt;\n"; |
| 5683 | Result += "\tshort cat_def_cnt;\n"; |
| 5684 | Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n"; |
| 5685 | Result += "};\n\n"; |
| 5686 | |
| 5687 | Result += "static struct _objc_symtab " |
| 5688 | "_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n"; |
| 5689 | Result += "\t0, 0, " + utostr(ClsDefCount) |
| 5690 | + ", " + utostr(CatDefCount) + "\n"; |
| 5691 | for (int i = 0; i < ClsDefCount; i++) { |
| 5692 | Result += "\t,&_OBJC_CLASS_"; |
| 5693 | Result += ClassImplementation[i]->getNameAsString(); |
| 5694 | Result += "\n"; |
| 5695 | } |
| 5696 | |
| 5697 | for (int i = 0; i < CatDefCount; i++) { |
| 5698 | Result += "\t,&_OBJC_CATEGORY_"; |
| 5699 | Result += CategoryImplementation[i]->getClassInterface()->getNameAsString(); |
| 5700 | Result += "_"; |
| 5701 | Result += CategoryImplementation[i]->getNameAsString(); |
| 5702 | Result += "\n"; |
| 5703 | } |
| 5704 | |
| 5705 | Result += "};\n\n"; |
| 5706 | |
| 5707 | // Write objc_module metadata |
| 5708 | |
| 5709 | /* |
| 5710 | struct _objc_module { |
| 5711 | long version; |
| 5712 | long size; |
| 5713 | const char *name; |
| 5714 | struct _objc_symtab *symtab; |
| 5715 | } |
| 5716 | */ |
| 5717 | |
| 5718 | Result += "\nstruct _objc_module {\n"; |
| 5719 | Result += "\tlong version;\n"; |
| 5720 | Result += "\tlong size;\n"; |
| 5721 | Result += "\tconst char *name;\n"; |
| 5722 | Result += "\tstruct _objc_symtab *symtab;\n"; |
| 5723 | Result += "};\n\n"; |
| 5724 | Result += "static struct _objc_module " |
| 5725 | "_OBJC_MODULES __attribute__ ((used, section (\"__OBJC, __module_info\")))= {\n"; |
| 5726 | Result += "\t" + utostr(OBJC_ABI_VERSION) + |
| 5727 | ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n"; |
| 5728 | Result += "};\n\n"; |
| 5729 | |
| 5730 | if (LangOpts.MicrosoftExt) { |
| 5731 | if (ProtocolExprDecls.size()) { |
| 5732 | Result += "#pragma section(\".objc_protocol$B\",long,read,write)\n"; |
| 5733 | Result += "#pragma data_seg(push, \".objc_protocol$B\")\n"; |
| 5734 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
| 5735 | E = ProtocolExprDecls.end(); I != E; ++I) { |
| 5736 | Result += "static struct _objc_protocol *_POINTER_OBJC_PROTOCOL_"; |
| 5737 | Result += (*I)->getNameAsString(); |
| 5738 | Result += " = &_OBJC_PROTOCOL_"; |
| 5739 | Result += (*I)->getNameAsString(); |
| 5740 | Result += ";\n"; |
| 5741 | } |
| 5742 | Result += "#pragma data_seg(pop)\n\n"; |
| 5743 | } |
| 5744 | Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n"; |
| 5745 | Result += "#pragma data_seg(push, \".objc_module_info$B\")\n"; |
| 5746 | Result += "static struct _objc_module *_POINTER_OBJC_MODULES = "; |
| 5747 | Result += "&_OBJC_MODULES;\n"; |
| 5748 | Result += "#pragma data_seg(pop)\n\n"; |
| 5749 | } |
| 5750 | } |
| 5751 | |
| 5752 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
| 5753 | /// implementation. |
| 5754 | void RewriteObjCFragileABI::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
| 5755 | std::string &Result) { |
| 5756 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
| 5757 | // Find category declaration for this implementation. |
| 5758 | ObjCCategoryDecl *CDecl; |
| 5759 | for (CDecl = ClassDecl->getCategoryList(); CDecl; |
| 5760 | CDecl = CDecl->getNextClassCategory()) |
| 5761 | if (CDecl->getIdentifier() == IDecl->getIdentifier()) |
| 5762 | break; |
| 5763 | |
| 5764 | std::string FullCategoryName = ClassDecl->getNameAsString(); |
| 5765 | FullCategoryName += '_'; |
| 5766 | FullCategoryName += IDecl->getNameAsString(); |
| 5767 | |
| 5768 | // Build _objc_method_list for class's instance methods if needed |
| 5769 | SmallVector<ObjCMethodDecl *, 32> |
| 5770 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
| 5771 | |
| 5772 | // If any of our property implementations have associated getters or |
| 5773 | // setters, produce metadata for them as well. |
| 5774 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 5775 | PropEnd = IDecl->propimpl_end(); |
| 5776 | Prop != PropEnd; ++Prop) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5777 | if (Prop->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
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 | if (!Prop->getPropertyIvarDecl()) |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5780 | continue; |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5781 | ObjCPropertyDecl *PD = Prop->getPropertyDecl(); |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5782 | if (!PD) |
| 5783 | continue; |
| 5784 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 5785 | InstanceMethods.push_back(Getter); |
| 5786 | if (PD->isReadOnly()) |
| 5787 | continue; |
| 5788 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 5789 | InstanceMethods.push_back(Setter); |
| 5790 | } |
| 5791 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
| 5792 | true, "CATEGORY_", FullCategoryName.c_str(), |
| 5793 | Result); |
| 5794 | |
| 5795 | // Build _objc_method_list for class's class methods if needed |
| 5796 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
| 5797 | false, "CATEGORY_", FullCategoryName.c_str(), |
| 5798 | Result); |
| 5799 | |
| 5800 | // Protocols referenced in class declaration? |
| 5801 | // Null CDecl is case of a category implementation with no category interface |
| 5802 | if (CDecl) |
| 5803 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), "CATEGORY", |
| 5804 | FullCategoryName, Result); |
| 5805 | /* struct _objc_category { |
| 5806 | char *category_name; |
| 5807 | char *class_name; |
| 5808 | struct _objc_method_list *instance_methods; |
| 5809 | struct _objc_method_list *class_methods; |
| 5810 | struct _objc_protocol_list *protocols; |
| 5811 | // Objective-C 1.0 extensions |
| 5812 | uint32_t size; // sizeof (struct _objc_category) |
| 5813 | struct _objc_property_list *instance_properties; // category's own |
| 5814 | // @property decl. |
| 5815 | }; |
| 5816 | */ |
| 5817 | |
| 5818 | static bool objc_category = false; |
| 5819 | if (!objc_category) { |
| 5820 | Result += "\nstruct _objc_category {\n"; |
| 5821 | Result += "\tchar *category_name;\n"; |
| 5822 | Result += "\tchar *class_name;\n"; |
| 5823 | Result += "\tstruct _objc_method_list *instance_methods;\n"; |
| 5824 | Result += "\tstruct _objc_method_list *class_methods;\n"; |
| 5825 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 5826 | Result += "\tunsigned int size;\n"; |
| 5827 | Result += "\tstruct _objc_property_list *instance_properties;\n"; |
| 5828 | Result += "};\n"; |
| 5829 | objc_category = true; |
| 5830 | } |
| 5831 | Result += "\nstatic struct _objc_category _OBJC_CATEGORY_"; |
| 5832 | Result += FullCategoryName; |
| 5833 | Result += " __attribute__ ((used, section (\"__OBJC, __category\")))= {\n\t\""; |
| 5834 | Result += IDecl->getNameAsString(); |
| 5835 | Result += "\"\n\t, \""; |
| 5836 | Result += ClassDecl->getNameAsString(); |
| 5837 | Result += "\"\n"; |
| 5838 | |
| 5839 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
| 5840 | Result += "\t, (struct _objc_method_list *)" |
| 5841 | "&_OBJC_CATEGORY_INSTANCE_METHODS_"; |
| 5842 | Result += FullCategoryName; |
| 5843 | Result += "\n"; |
| 5844 | } |
| 5845 | else |
| 5846 | Result += "\t, 0\n"; |
| 5847 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
| 5848 | Result += "\t, (struct _objc_method_list *)" |
| 5849 | "&_OBJC_CATEGORY_CLASS_METHODS_"; |
| 5850 | Result += FullCategoryName; |
| 5851 | Result += "\n"; |
| 5852 | } |
| 5853 | else |
| 5854 | Result += "\t, 0\n"; |
| 5855 | |
| 5856 | if (CDecl && CDecl->protocol_begin() != CDecl->protocol_end()) { |
| 5857 | Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_"; |
| 5858 | Result += FullCategoryName; |
| 5859 | Result += "\n"; |
| 5860 | } |
| 5861 | else |
| 5862 | Result += "\t, 0\n"; |
| 5863 | Result += "\t, sizeof(struct _objc_category), 0\n};\n"; |
| 5864 | } |
| 5865 | |
| 5866 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
| 5867 | /// class methods. |
| 5868 | template<typename MethodIterator> |
| 5869 | void RewriteObjCFragileABI::RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 5870 | MethodIterator MethodEnd, |
| 5871 | bool IsInstanceMethod, |
| 5872 | StringRef prefix, |
| 5873 | StringRef ClassName, |
| 5874 | std::string &Result) { |
| 5875 | if (MethodBegin == MethodEnd) return; |
| 5876 | |
| 5877 | if (!objc_impl_method) { |
| 5878 | /* struct _objc_method { |
| 5879 | SEL _cmd; |
| 5880 | char *method_types; |
| 5881 | void *_imp; |
| 5882 | } |
| 5883 | */ |
| 5884 | Result += "\nstruct _objc_method {\n"; |
| 5885 | Result += "\tSEL _cmd;\n"; |
| 5886 | Result += "\tchar *method_types;\n"; |
| 5887 | Result += "\tvoid *_imp;\n"; |
| 5888 | Result += "};\n"; |
| 5889 | |
| 5890 | objc_impl_method = true; |
| 5891 | } |
| 5892 | |
| 5893 | // Build _objc_method_list for class's methods if needed |
| 5894 | |
| 5895 | /* struct { |
| 5896 | struct _objc_method_list *next_method; |
| 5897 | int method_count; |
| 5898 | struct _objc_method method_list[]; |
| 5899 | } |
| 5900 | */ |
| 5901 | unsigned NumMethods = std::distance(MethodBegin, MethodEnd); |
| 5902 | Result += "\nstatic struct {\n"; |
| 5903 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 5904 | Result += "\tint method_count;\n"; |
| 5905 | Result += "\tstruct _objc_method method_list["; |
| 5906 | Result += utostr(NumMethods); |
| 5907 | Result += "];\n} _OBJC_"; |
| 5908 | Result += prefix; |
| 5909 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 5910 | Result += "_METHODS_"; |
| 5911 | Result += ClassName; |
| 5912 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
| 5913 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 5914 | Result += "_meth\")))= "; |
| 5915 | Result += "{\n\t0, " + utostr(NumMethods) + "\n"; |
| 5916 | |
| 5917 | Result += "\t,{{(SEL)\""; |
| 5918 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
| 5919 | std::string MethodTypeString; |
| 5920 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
| 5921 | Result += "\", \""; |
| 5922 | Result += MethodTypeString; |
| 5923 | Result += "\", (void *)"; |
| 5924 | Result += MethodInternalNames[*MethodBegin]; |
| 5925 | Result += "}\n"; |
| 5926 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 5927 | Result += "\t ,{(SEL)\""; |
| 5928 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
| 5929 | std::string MethodTypeString; |
| 5930 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
| 5931 | Result += "\", \""; |
| 5932 | Result += MethodTypeString; |
| 5933 | Result += "\", (void *)"; |
| 5934 | Result += MethodInternalNames[*MethodBegin]; |
| 5935 | Result += "}\n"; |
| 5936 | } |
| 5937 | Result += "\t }\n};\n"; |
| 5938 | } |
| 5939 | |
| 5940 | Stmt *RewriteObjCFragileABI::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV) { |
| 5941 | SourceRange OldRange = IV->getSourceRange(); |
| 5942 | Expr *BaseExpr = IV->getBase(); |
| 5943 | |
| 5944 | // Rewrite the base, but without actually doing replaces. |
| 5945 | { |
| 5946 | DisableReplaceStmtScope S(*this); |
| 5947 | BaseExpr = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(BaseExpr)); |
| 5948 | IV->setBase(BaseExpr); |
| 5949 | } |
| 5950 | |
| 5951 | ObjCIvarDecl *D = IV->getDecl(); |
| 5952 | |
| 5953 | Expr *Replacement = IV; |
| 5954 | if (CurMethodDef) { |
| 5955 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
| 5956 | const ObjCInterfaceType *iFaceDecl = |
| 5957 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
| 5958 | assert(iFaceDecl && "RewriteObjCIvarRefExpr - iFaceDecl is null"); |
| 5959 | // lookup which class implements the instance variable. |
| 5960 | ObjCInterfaceDecl *clsDeclared = 0; |
| 5961 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
| 5962 | clsDeclared); |
| 5963 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
| 5964 | |
| 5965 | // Synthesize an explicit cast to gain access to the ivar. |
| 5966 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 5967 | RecName += "_IMPL"; |
| 5968 | IdentifierInfo *II = &Context->Idents.get(RecName); |
| 5969 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 5970 | SourceLocation(), SourceLocation(), |
| 5971 | II); |
| 5972 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 5973 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 5974 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, |
| 5975 | CK_BitCast, |
| 5976 | IV->getBase()); |
| 5977 | // Don't forget the parens to enforce the proper binding. |
| 5978 | ParenExpr *PE = new (Context) ParenExpr(OldRange.getBegin(), |
| 5979 | OldRange.getEnd(), |
| 5980 | castExpr); |
| 5981 | if (IV->isFreeIvar() && |
Douglas Gregor | 60ef308 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 5982 | declaresSameEntity(CurMethodDef->getClassInterface(), iFaceDecl->getDecl())) { |
Fariborz Jahanian | d5c3fa2 | 2011-12-08 18:25:15 +0000 | [diff] [blame] | 5983 | MemberExpr *ME = new (Context) MemberExpr(PE, true, D, |
| 5984 | IV->getLocation(), |
| 5985 | D->getType(), |
| 5986 | VK_LValue, OK_Ordinary); |
| 5987 | Replacement = ME; |
| 5988 | } else { |
| 5989 | IV->setBase(PE); |
| 5990 | } |
| 5991 | } |
| 5992 | } else { // we are outside a method. |
| 5993 | assert(!IV->isFreeIvar() && "Cannot have a free standing ivar outside a method"); |
| 5994 | |
| 5995 | // Explicit ivar refs need to have a cast inserted. |
| 5996 | // FIXME: consider sharing some of this code with the code above. |
| 5997 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
| 5998 | const ObjCInterfaceType *iFaceDecl = |
| 5999 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
| 6000 | // lookup which class implements the instance variable. |
| 6001 | ObjCInterfaceDecl *clsDeclared = 0; |
| 6002 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
| 6003 | clsDeclared); |
| 6004 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
| 6005 | |
| 6006 | // Synthesize an explicit cast to gain access to the ivar. |
| 6007 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 6008 | RecName += "_IMPL"; |
| 6009 | IdentifierInfo *II = &Context->Idents.get(RecName); |
| 6010 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 6011 | SourceLocation(), SourceLocation(), |
| 6012 | II); |
| 6013 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 6014 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 6015 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, |
| 6016 | CK_BitCast, |
| 6017 | IV->getBase()); |
| 6018 | // Don't forget the parens to enforce the proper binding. |
| 6019 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
| 6020 | IV->getBase()->getLocEnd(), castExpr); |
| 6021 | // Cannot delete IV->getBase(), since PE points to it. |
| 6022 | // Replace the old base with the cast. This is important when doing |
| 6023 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
| 6024 | IV->setBase(PE); |
| 6025 | } |
| 6026 | } |
| 6027 | |
| 6028 | ReplaceStmtWithRange(IV, Replacement, OldRange); |
| 6029 | return Replacement; |
| 6030 | } |