Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1 | //===--- RewriteObjC.cpp - Playground for the code rewriter ---------------===// |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Hacks and fun related to the code rewriter. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Daniel Dunbar | 9b414d3 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 14 | #include "clang/Rewrite/ASTConsumers.h" |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 15 | #include "clang/Rewrite/Rewriter.h" |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 16 | #include "clang/AST/AST.h" |
| 17 | #include "clang/AST/ASTConsumer.h" |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 18 | #include "clang/AST/ParentMap.h" |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 19 | #include "clang/Basic/SourceManager.h" |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 20 | #include "clang/Basic/IdentifierTable.h" |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 21 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | 26de465 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 22 | #include "clang/Lex/Lexer.h" |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MemoryBuffer.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringExtras.h" |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallPtrSet.h" |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/OwningPtr.h" |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/DenseSet.h" |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 30 | using namespace clang; |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 31 | using llvm::utostr; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 32 | |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 33 | namespace { |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 34 | class RewriteObjC : public ASTConsumer { |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 35 | enum { |
Nico Weber | 59b173d | 2010-11-22 10:26:41 +0000 | [diff] [blame] | 36 | BLOCK_FIELD_IS_OBJECT = 3, /* id, NSObject, __attribute__((NSObject)), |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 37 | block, ... */ |
| 38 | BLOCK_FIELD_IS_BLOCK = 7, /* a block variable */ |
| 39 | BLOCK_FIELD_IS_BYREF = 8, /* the on stack structure holding the |
| 40 | __block variable */ |
Nico Weber | 59b173d | 2010-11-22 10:26:41 +0000 | [diff] [blame] | 41 | BLOCK_FIELD_IS_WEAK = 16, /* declared __weak, only used in byref copy |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 42 | helpers */ |
Nico Weber | 59b173d | 2010-11-22 10:26:41 +0000 | [diff] [blame] | 43 | BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 44 | support routines */ |
| 45 | BLOCK_BYREF_CURRENT_MAX = 256 |
| 46 | }; |
| 47 | |
| 48 | enum { |
| 49 | BLOCK_NEEDS_FREE = (1 << 24), |
| 50 | BLOCK_HAS_COPY_DISPOSE = (1 << 25), |
| 51 | BLOCK_HAS_CXX_OBJ = (1 << 26), |
| 52 | BLOCK_IS_GC = (1 << 27), |
| 53 | BLOCK_IS_GLOBAL = (1 << 28), |
| 54 | BLOCK_HAS_DESCRIPTOR = (1 << 29) |
| 55 | }; |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 56 | static const int OBJC_ABI_VERSION = 7; |
Fariborz Jahanian | dd8079c | 2011-12-05 19:50:04 +0000 | [diff] [blame^] | 57 | |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 58 | Rewriter Rewrite; |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 59 | DiagnosticsEngine &Diags; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 60 | const LangOptions &LangOpts; |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 61 | ASTContext *Context; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 62 | SourceManager *SM; |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 63 | TranslationUnitDecl *TUDecl; |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 64 | FileID MainFileID; |
Chris Lattner | 26de465 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 65 | const char *MainFileStart, *MainFileEnd; |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 66 | Stmt *CurrentBody; |
| 67 | ParentMap *PropParentMap; // created lazily. |
Fariborz Jahanian | dd8079c | 2011-12-05 19:50:04 +0000 | [diff] [blame^] | 68 | std::string InFileName; |
| 69 | raw_ostream* OutFile; |
| 70 | std::string Preamble; |
| 71 | |
| 72 | TypeDecl *ProtocolTypeDecl; |
| 73 | VarDecl *GlobalVarDecl; |
| 74 | unsigned RewriteFailedDiag; |
| 75 | unsigned TryFinallyContainsReturnDiag; |
| 76 | // ObjC string constant support. |
| 77 | unsigned NumObjCStringLiterals; |
| 78 | VarDecl *ConstantStringClassReference; |
| 79 | RecordDecl *NSStringRecord; |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 80 | |
Fariborz Jahanian | dd8079c | 2011-12-05 19:50:04 +0000 | [diff] [blame^] | 81 | // ObjC foreach break/continue generation support. |
| 82 | int BcLabelCount; |
| 83 | |
| 84 | // Needed for super. |
| 85 | ObjCMethodDecl *CurMethodDef; |
| 86 | RecordDecl *SuperStructDecl; |
| 87 | RecordDecl *ConstantStringDecl; |
| 88 | |
| 89 | FunctionDecl *MsgSendFunctionDecl; |
| 90 | FunctionDecl *MsgSendSuperFunctionDecl; |
| 91 | FunctionDecl *MsgSendStretFunctionDecl; |
| 92 | FunctionDecl *MsgSendSuperStretFunctionDecl; |
| 93 | FunctionDecl *MsgSendFpretFunctionDecl; |
| 94 | FunctionDecl *GetClassFunctionDecl; |
| 95 | FunctionDecl *GetMetaClassFunctionDecl; |
| 96 | FunctionDecl *GetSuperClassFunctionDecl; |
| 97 | FunctionDecl *SelGetUidFunctionDecl; |
| 98 | FunctionDecl *CFStringFunctionDecl; |
| 99 | FunctionDecl *SuperContructorFunctionDecl; |
| 100 | FunctionDecl *CurFunctionDef; |
| 101 | FunctionDecl *CurFunctionDeclToDeclareForBlock; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | |
Fariborz Jahanian | dd8079c | 2011-12-05 19:50:04 +0000 | [diff] [blame^] | 103 | /* Misc. containers needed for meta-data rewrite. */ |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 104 | SmallVector<ObjCImplementationDecl *, 8> ClassImplementation; |
| 105 | SmallVector<ObjCCategoryImplDecl *, 8> CategoryImplementation; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 106 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCSynthesizedStructs; |
Steve Naroff | fbfe825 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 107 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> ObjCSynthesizedProtocols; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 108 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCForwardDecls; |
| 109 | llvm::DenseMap<ObjCMethodDecl*, std::string> MethodInternalNames; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 110 | SmallVector<Stmt *, 32> Stmts; |
| 111 | SmallVector<int, 8> ObjCBcLabelNo; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 112 | // Remember all the @protocol(<expr>) expressions. |
| 113 | llvm::SmallPtrSet<ObjCProtocolDecl *, 32> ProtocolExprDecls; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 114 | |
| 115 | llvm::DenseSet<uint64_t> CopyDestroyCache; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 116 | |
| 117 | // Block expressions. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 118 | SmallVector<BlockExpr *, 32> Blocks; |
| 119 | SmallVector<int, 32> InnerDeclRefsCount; |
| 120 | SmallVector<BlockDeclRefExpr *, 32> InnerDeclRefs; |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 121 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 122 | SmallVector<BlockDeclRefExpr *, 32> BlockDeclRefs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 123 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 124 | // Block related declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 125 | SmallVector<ValueDecl *, 8> BlockByCopyDecls; |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 126 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByCopyDeclsPtrSet; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 127 | SmallVector<ValueDecl *, 8> BlockByRefDecls; |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 128 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByRefDeclsPtrSet; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 129 | llvm::DenseMap<ValueDecl *, unsigned> BlockByRefDeclNo; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 130 | llvm::SmallPtrSet<ValueDecl *, 8> ImportedBlockDecls; |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 131 | llvm::SmallPtrSet<VarDecl *, 8> ImportedLocalExternalDecls; |
| 132 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 133 | llvm::DenseMap<BlockExpr *, std::string> RewrittenBlockExprs; |
| 134 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 135 | // This maps an original source AST to it's rewritten form. This allows |
| 136 | // us to avoid rewriting the same node twice (which is very uncommon). |
| 137 | // This is needed to support some of the exotic property rewriting. |
| 138 | llvm::DenseMap<Stmt *, Stmt *> ReplacedNodes; |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 139 | |
Fariborz Jahanian | dd8079c | 2011-12-05 19:50:04 +0000 | [diff] [blame^] | 140 | // Needed for header files being rewritten |
| 141 | bool IsHeader; |
| 142 | bool SilenceRewriteMacroWarning; |
| 143 | bool objc_impl_method; |
| 144 | |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 145 | bool DisableReplaceStmt; |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 146 | class DisableReplaceStmtScope { |
| 147 | RewriteObjC &R; |
| 148 | bool SavedValue; |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 149 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 150 | public: |
| 151 | DisableReplaceStmtScope(RewriteObjC &R) |
| 152 | : R(R), SavedValue(R.DisableReplaceStmt) { |
| 153 | R.DisableReplaceStmt = true; |
| 154 | } |
| 155 | ~DisableReplaceStmtScope() { |
| 156 | R.DisableReplaceStmt = SavedValue; |
| 157 | } |
| 158 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 159 | |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 160 | public: |
Ted Kremenek | e3a6198 | 2008-05-31 20:11:04 +0000 | [diff] [blame] | 161 | virtual void Initialize(ASTContext &context); |
| 162 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 163 | // Top Level Driver code. |
Argyrios Kyrtzidis | 88c2596 | 2011-11-18 00:26:59 +0000 | [diff] [blame] | 164 | virtual bool HandleTopLevelDecl(DeclGroupRef D) { |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 165 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
| 166 | if (isa<ObjCClassDecl>((*I))) { |
| 167 | RewriteForwardClassDecl(D); |
| 168 | break; |
| 169 | } |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 170 | HandleTopLevelSingleDecl(*I); |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 171 | } |
Argyrios Kyrtzidis | 88c2596 | 2011-11-18 00:26:59 +0000 | [diff] [blame] | 172 | return true; |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 173 | } |
| 174 | void HandleTopLevelSingleDecl(Decl *D); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 175 | void HandleDeclInMainFile(Decl *D); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 176 | RewriteObjC(std::string inFile, raw_ostream *OS, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 177 | DiagnosticsEngine &D, const LangOptions &LOpts, |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 178 | bool silenceMacroWarn); |
Ted Kremenek | e452e0f | 2008-08-08 04:15:52 +0000 | [diff] [blame] | 179 | |
| 180 | ~RewriteObjC() {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 181 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 182 | virtual void HandleTranslationUnit(ASTContext &C); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 183 | |
Fariborz Jahanian | 88906cd | 2010-02-05 16:43:40 +0000 | [diff] [blame] | 184 | void ReplaceStmt(Stmt *Old, Stmt *New) { |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 185 | Stmt *ReplacingStmt = ReplacedNodes[Old]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 186 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 187 | if (ReplacingStmt) |
| 188 | return; // We can't rewrite the same node twice. |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 189 | |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 190 | if (DisableReplaceStmt) |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 191 | return; |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 192 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 193 | // If replacement succeeded or warning disabled return with no warning. |
Fariborz Jahanian | 88906cd | 2010-02-05 16:43:40 +0000 | [diff] [blame] | 194 | if (!Rewrite.ReplaceStmt(Old, New)) { |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 195 | ReplacedNodes[Old] = New; |
| 196 | return; |
| 197 | } |
| 198 | if (SilenceRewriteMacroWarning) |
| 199 | return; |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 200 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 201 | << Old->getSourceRange(); |
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 | |
| 204 | void ReplaceStmtWithRange(Stmt *Old, Stmt *New, SourceRange SrcRange) { |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 205 | if (DisableReplaceStmt) |
| 206 | return; |
| 207 | |
Nick Lewycky | 7e74924 | 2010-10-31 21:07:24 +0000 | [diff] [blame] | 208 | // Measure the old text. |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 209 | int Size = Rewrite.getRangeSize(SrcRange); |
| 210 | if (Size == -1) { |
| 211 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 212 | << Old->getSourceRange(); |
| 213 | return; |
| 214 | } |
| 215 | // Get the new text. |
| 216 | std::string SStr; |
| 217 | llvm::raw_string_ostream S(SStr); |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 218 | New->printPretty(S, *Context, 0, PrintingPolicy(LangOpts)); |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 219 | const std::string &Str = S.str(); |
| 220 | |
| 221 | // If replacement succeeded or warning disabled return with no warning. |
Daniel Dunbar | d7407dc | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 222 | if (!Rewrite.ReplaceText(SrcRange.getBegin(), Size, Str)) { |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 223 | ReplacedNodes[Old] = New; |
| 224 | return; |
| 225 | } |
| 226 | if (SilenceRewriteMacroWarning) |
| 227 | return; |
| 228 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 229 | << Old->getSourceRange(); |
| 230 | } |
| 231 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 232 | void InsertText(SourceLocation Loc, StringRef Str, |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 233 | bool InsertAfter = true) { |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 234 | // If insertion succeeded or warning disabled return with no warning. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 235 | if (!Rewrite.InsertText(Loc, Str, InsertAfter) || |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 236 | SilenceRewriteMacroWarning) |
| 237 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 238 | |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 239 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 240 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 241 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 242 | void ReplaceText(SourceLocation Start, unsigned OrigLength, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 243 | StringRef Str) { |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 244 | // If removal succeeded or warning disabled return with no warning. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 245 | if (!Rewrite.ReplaceText(Start, OrigLength, Str) || |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 246 | SilenceRewriteMacroWarning) |
| 247 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 248 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 249 | Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag); |
| 250 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 251 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 252 | // Syntactic Rewriting. |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 253 | void RewriteRecordBody(RecordDecl *RD); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 254 | void RewriteInclude(); |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 255 | void RewriteForwardClassDecl(DeclGroupRef D); |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 256 | void RewriteForwardClassDecl(const llvm::SmallVector<Decl*, 8> &DG); |
| 257 | void RewriteForwardClassEpilogue(ObjCClassDecl *ClassDecl, |
| 258 | const std::string &typedefString); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 259 | void RewriteImplementations(); |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 260 | void RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 261 | ObjCImplementationDecl *IMD, |
| 262 | ObjCCategoryImplDecl *CID); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 263 | void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 264 | void RewriteImplementationDecl(Decl *Dcl); |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 265 | void RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl, |
| 266 | ObjCMethodDecl *MDecl, std::string &ResultStr); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 267 | void RewriteTypeIntoString(QualType T, std::string &ResultStr, |
| 268 | const FunctionType *&FPRetType); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 269 | void RewriteByRefString(std::string &ResultStr, const std::string &Name, |
Fariborz Jahanian | 1e8011e | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 270 | ValueDecl *VD, bool def=false); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 271 | void RewriteCategoryDecl(ObjCCategoryDecl *Dcl); |
| 272 | void RewriteProtocolDecl(ObjCProtocolDecl *Dcl); |
| 273 | void RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *Dcl); |
| 274 | void RewriteMethodDeclaration(ObjCMethodDecl *Method); |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 275 | void RewriteProperty(ObjCPropertyDecl *prop); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 276 | void RewriteFunctionDecl(FunctionDecl *FD); |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 277 | void RewriteBlockPointerType(std::string& Str, QualType Type); |
| 278 | void RewriteBlockPointerTypeVariable(std::string& Str, ValueDecl *VD); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 279 | void RewriteBlockLiteralFunctionDecl(FunctionDecl *FD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 280 | void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 281 | void RewriteTypeOfDecl(VarDecl *VD); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 282 | void RewriteObjCQualifiedInterfaceTypes(Expr *E); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 283 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 284 | // Expression Rewriting. |
Steve Naroff | f3473a7 | 2007-11-09 15:20:18 +0000 | [diff] [blame] | 285 | Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S); |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 286 | Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 287 | Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV); |
| 288 | Stmt *RewritePropertyOrImplicitGetter(PseudoObjectExpr *Pseudo); |
| 289 | Stmt *RewritePropertyOrImplicitSetter(PseudoObjectExpr *Pseudo); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 290 | Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp); |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 291 | Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp); |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 292 | Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp); |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 293 | Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 294 | void RewriteTryReturnStmts(Stmt *S); |
| 295 | void RewriteSyncReturnStmts(Stmt *S, std::string buf); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 296 | Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 297 | Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 298 | Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S); |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 299 | Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
| 300 | SourceLocation OrigEnd); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 301 | Stmt *RewriteBreakStmt(BreakStmt *S); |
| 302 | Stmt *RewriteContinueStmt(ContinueStmt *S); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 303 | void RewriteCastExpr(CStyleCastExpr *CE); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 304 | |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 305 | // Metadata Rewriting. |
| 306 | void RewriteMetaDataIntoBuffer(std::string &Result); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 307 | void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 308 | std::string &Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 309 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 310 | void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 311 | std::string &Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 312 | |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 313 | template<typename MethodIterator> |
| 314 | void RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 315 | MethodIterator MethodEnd, |
Fariborz Jahanian | 8e991ba | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 316 | bool IsInstanceMethod, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 317 | StringRef prefix, |
| 318 | StringRef ClassName, |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 319 | std::string &Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 320 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 321 | void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 322 | StringRef prefix, |
| 323 | StringRef ClassName, |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 324 | std::string &Result); |
| 325 | void RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Prots, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 326 | StringRef prefix, |
| 327 | StringRef ClassName, |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 328 | std::string &Result); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 329 | // Block rewriting. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 330 | void RewriteBlocksInFunctionProtoType(QualType funcType, NamedDecl *D); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 331 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 332 | // Block specific rewrite rules. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 333 | void RewriteBlockPointerDecl(NamedDecl *VD); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 334 | void RewriteByRefVar(VarDecl *VD); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 335 | Stmt *RewriteBlockDeclRefExpr(Expr *VD); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 336 | Stmt *RewriteLocalVariableExternalStorage(DeclRefExpr *DRE); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 337 | void RewriteBlockPointerFunctionArgs(FunctionDecl *FD); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 338 | |
| 339 | void RewriteObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
| 340 | std::string &Result); |
| 341 | void RewriteIvarOffsetComputation(ObjCIvarDecl *ivar, |
| 342 | std::string &Result); |
| 343 | |
| 344 | // Misc. AST transformation routines. Somtimes they end up calling |
| 345 | // rewriting routines on the new ASTs. |
| 346 | CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD, |
| 347 | Expr **args, unsigned nargs, |
| 348 | SourceLocation StartLoc=SourceLocation(), |
| 349 | SourceLocation EndLoc=SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 350 | |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 351 | Stmt *SynthMessageExpr(ObjCMessageExpr *Exp, |
| 352 | SourceLocation StartLoc=SourceLocation(), |
| 353 | SourceLocation EndLoc=SourceLocation()); |
| 354 | |
| 355 | void SynthCountByEnumWithState(std::string &buf); |
| 356 | void SynthMsgSendFunctionDecl(); |
| 357 | void SynthMsgSendSuperFunctionDecl(); |
| 358 | void SynthMsgSendStretFunctionDecl(); |
| 359 | void SynthMsgSendFpretFunctionDecl(); |
| 360 | void SynthMsgSendSuperStretFunctionDecl(); |
| 361 | void SynthGetClassFunctionDecl(); |
| 362 | void SynthGetMetaClassFunctionDecl(); |
| 363 | void SynthGetSuperClassFunctionDecl(); |
| 364 | void SynthSelGetUidFunctionDecl(); |
| 365 | void SynthSuperContructorFunctionDecl(); |
| 366 | |
| 367 | std::string SynthesizeByrefCopyDestroyHelper(VarDecl *VD, int flag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 368 | std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 369 | StringRef funcName, std::string Tag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 370 | std::string SynthesizeBlockFunc(BlockExpr *CE, int i, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 371 | StringRef funcName, std::string Tag); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 372 | std::string SynthesizeBlockImpl(BlockExpr *CE, |
| 373 | std::string Tag, std::string Desc); |
| 374 | std::string SynthesizeBlockDescriptor(std::string DescTag, |
| 375 | std::string ImplTag, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 376 | int i, StringRef funcName, |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 377 | unsigned hasCopy); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 378 | Stmt *SynthesizeBlockCall(CallExpr *Exp, const Expr* BlockExp); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 379 | void SynthesizeBlockLiterals(SourceLocation FunLocStart, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 380 | StringRef FunName); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 381 | FunctionDecl *SynthBlockInitFunctionDecl(StringRef name); |
| 382 | Stmt *SynthBlockInitExpr(BlockExpr *Exp, |
| 383 | const SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 384 | |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 385 | // Misc. helper routines. |
Fariborz Jahanian | dd8079c | 2011-12-05 19:50:04 +0000 | [diff] [blame^] | 386 | QualType getProtocolType(); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 387 | void WarnAboutReturnGotoStmts(Stmt *S); |
| 388 | void HasReturnStmts(Stmt *S, bool &hasReturns); |
| 389 | void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND); |
| 390 | void InsertBlockLiteralsWithinFunction(FunctionDecl *FD); |
| 391 | void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD); |
| 392 | |
| 393 | bool IsDeclStmtInForeachHeader(DeclStmt *DS); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 394 | void CollectBlockDeclRefInfo(BlockExpr *Exp); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 395 | void GetBlockDeclRefExprs(Stmt *S); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 396 | void GetInnerBlockDeclRefExprs(Stmt *S, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 397 | SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs, |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 398 | llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 399 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 400 | // We avoid calling Type::isBlockPointerType(), since it operates on the |
| 401 | // canonical type. We only care if the top-level type is a closure pointer. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 402 | bool isTopLevelBlockPointerType(QualType T) { |
| 403 | return isa<BlockPointerType>(T); |
| 404 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 405 | |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 406 | /// convertBlockPointerToFunctionPointer - Converts a block-pointer type |
| 407 | /// to a function pointer type and upon success, returns true; false |
| 408 | /// otherwise. |
| 409 | bool convertBlockPointerToFunctionPointer(QualType &T) { |
| 410 | if (isTopLevelBlockPointerType(T)) { |
| 411 | const BlockPointerType *BPT = T->getAs<BlockPointerType>(); |
| 412 | T = Context->getPointerType(BPT->getPointeeType()); |
| 413 | return true; |
| 414 | } |
| 415 | return false; |
| 416 | } |
| 417 | |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 418 | bool needToScanForQualifiers(QualType T); |
| 419 | QualType getSuperStructType(); |
| 420 | QualType getConstantStringStructType(); |
| 421 | QualType convertFunctionTypeOfBlocks(const FunctionType *FT); |
| 422 | bool BufferContainsPPDirectives(const char *startBuf, const char *endBuf); |
| 423 | |
Fariborz Jahanian | 8188e5f | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 424 | void convertToUnqualifiedObjCType(QualType &T) { |
| 425 | if (T->isObjCQualifiedIdType()) |
| 426 | T = Context->getObjCIdType(); |
| 427 | else if (T->isObjCQualifiedClassType()) |
| 428 | T = Context->getObjCClassType(); |
| 429 | else if (T->isObjCObjectPointerType() && |
Fariborz Jahanian | 3a448fb | 2011-09-10 17:01:56 +0000 | [diff] [blame] | 430 | T->getPointeeType()->isObjCQualifiedInterfaceType()) { |
| 431 | if (const ObjCObjectPointerType * OBJPT = |
| 432 | T->getAsObjCInterfacePointerType()) { |
| 433 | const ObjCInterfaceType *IFaceT = OBJPT->getInterfaceType(); |
| 434 | T = QualType(IFaceT, 0); |
| 435 | T = Context->getPointerType(T); |
| 436 | } |
| 437 | } |
Fariborz Jahanian | 8188e5f | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 440 | // FIXME: This predicate seems like it would be useful to add to ASTContext. |
| 441 | bool isObjCType(QualType T) { |
| 442 | if (!LangOpts.ObjC1 && !LangOpts.ObjC2) |
| 443 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 444 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 445 | QualType OCT = Context->getCanonicalType(T).getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 446 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 447 | if (OCT == Context->getCanonicalType(Context->getObjCIdType()) || |
| 448 | OCT == Context->getCanonicalType(Context->getObjCClassType())) |
| 449 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 450 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 451 | if (const PointerType *PT = OCT->getAs<PointerType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 452 | if (isa<ObjCInterfaceType>(PT->getPointeeType()) || |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 453 | PT->getPointeeType()->isObjCQualifiedIdType()) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 454 | return true; |
| 455 | } |
| 456 | return false; |
| 457 | } |
| 458 | bool PointerTypeTakesAnyBlockArguments(QualType QT); |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 459 | bool PointerTypeTakesAnyObjCQualifiedType(QualType QT); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 460 | void GetExtentOfArgList(const char *Name, const char *&LParen, |
| 461 | const char *&RParen); |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 462 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 463 | void QuoteDoublequotes(std::string &From, std::string &To) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 464 | for (unsigned i = 0; i < From.length(); i++) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 465 | if (From[i] == '"') |
| 466 | To += "\\\""; |
| 467 | else |
| 468 | To += From[i]; |
| 469 | } |
| 470 | } |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 471 | |
| 472 | QualType getSimpleFunctionType(QualType result, |
| 473 | const QualType *args, |
| 474 | unsigned numArgs, |
| 475 | bool variadic = false) { |
Fariborz Jahanian | 8891480 | 2011-09-09 20:35:22 +0000 | [diff] [blame] | 476 | if (result == Context->getObjCInstanceType()) |
| 477 | result = Context->getObjCIdType(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 478 | FunctionProtoType::ExtProtoInfo fpi; |
| 479 | fpi.Variadic = variadic; |
| 480 | return Context->getFunctionType(result, args, numArgs, fpi); |
| 481 | } |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 482 | |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 483 | // Helper function: create a CStyleCastExpr with trivial type source info. |
| 484 | CStyleCastExpr* NoTypeInfoCStyleCastExpr(ASTContext *Ctx, QualType Ty, |
| 485 | CastKind Kind, Expr *E) { |
| 486 | TypeSourceInfo *TInfo = Ctx->getTrivialTypeSourceInfo(Ty, SourceLocation()); |
| 487 | return CStyleCastExpr::Create(*Ctx, Ty, VK_RValue, Kind, E, 0, TInfo, |
| 488 | SourceLocation(), SourceLocation()); |
| 489 | } |
| 490 | }; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 493 | void RewriteObjC::RewriteBlocksInFunctionProtoType(QualType funcType, |
| 494 | NamedDecl *D) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 495 | if (const FunctionProtoType *fproto |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 496 | = dyn_cast<FunctionProtoType>(funcType.IgnoreParens())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 497 | for (FunctionProtoType::arg_type_iterator I = fproto->arg_type_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 498 | E = fproto->arg_type_end(); I && (I != E); ++I) |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 499 | if (isTopLevelBlockPointerType(*I)) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 500 | // All the args are checked/rewritten. Don't call twice! |
| 501 | RewriteBlockPointerDecl(D); |
| 502 | break; |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | void RewriteObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 508 | const PointerType *PT = funcType->getAs<PointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 509 | if (PT && PointerTypeTakesAnyBlockArguments(funcType)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 510 | RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 513 | static bool IsHeaderFile(const std::string &Filename) { |
| 514 | std::string::size_type DotPos = Filename.rfind('.'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 515 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 516 | if (DotPos == std::string::npos) { |
| 517 | // no file extension |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 518 | return false; |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 519 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 520 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 521 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 522 | // C header: .h |
| 523 | // C++ header: .hh or .H; |
| 524 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 525 | } |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 526 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 527 | RewriteObjC::RewriteObjC(std::string inFile, raw_ostream* OS, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 528 | DiagnosticsEngine &D, const LangOptions &LOpts, |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 529 | bool silenceMacroWarn) |
| 530 | : Diags(D), LangOpts(LOpts), InFileName(inFile), OutFile(OS), |
| 531 | SilenceRewriteMacroWarning(silenceMacroWarn) { |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 532 | IsHeader = IsHeaderFile(inFile); |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 533 | RewriteFailedDiag = Diags.getCustomDiagID(DiagnosticsEngine::Warning, |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 534 | "rewriting sub-expression within a macro (may not be correct)"); |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 535 | TryFinallyContainsReturnDiag = Diags.getCustomDiagID( |
| 536 | DiagnosticsEngine::Warning, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 537 | "rewriter doesn't support user-specified control flow semantics " |
| 538 | "for @try/@finally (code may not execute properly)"); |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Eli Friedman | bce831b | 2009-05-18 22:29:17 +0000 | [diff] [blame] | 541 | ASTConsumer *clang::CreateObjCRewriter(const std::string& InFile, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 542 | raw_ostream* OS, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 543 | DiagnosticsEngine &Diags, |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 544 | const LangOptions &LOpts, |
| 545 | bool SilenceRewriteMacroWarning) { |
| 546 | return new RewriteObjC(InFile, OS, Diags, LOpts, SilenceRewriteMacroWarning); |
Chris Lattner | e365c50 | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 547 | } |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 548 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 549 | void RewriteObjC::Initialize(ASTContext &context) { |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 550 | Context = &context; |
| 551 | SM = &Context->getSourceManager(); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 552 | TUDecl = Context->getTranslationUnitDecl(); |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 553 | MsgSendFunctionDecl = 0; |
| 554 | MsgSendSuperFunctionDecl = 0; |
| 555 | MsgSendStretFunctionDecl = 0; |
| 556 | MsgSendSuperStretFunctionDecl = 0; |
| 557 | MsgSendFpretFunctionDecl = 0; |
| 558 | GetClassFunctionDecl = 0; |
| 559 | GetMetaClassFunctionDecl = 0; |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 560 | GetSuperClassFunctionDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 561 | SelGetUidFunctionDecl = 0; |
| 562 | CFStringFunctionDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 563 | ConstantStringClassReference = 0; |
| 564 | NSStringRecord = 0; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 565 | CurMethodDef = 0; |
| 566 | CurFunctionDef = 0; |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 567 | CurFunctionDeclToDeclareForBlock = 0; |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 568 | GlobalVarDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 569 | SuperStructDecl = 0; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 570 | ProtocolTypeDecl = 0; |
Steve Naroff | 9630ec5 | 2008-03-27 22:59:54 +0000 | [diff] [blame] | 571 | ConstantStringDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 572 | BcLabelCount = 0; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 573 | SuperContructorFunctionDecl = 0; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 574 | NumObjCStringLiterals = 0; |
Steve Naroff | 68272b8 | 2008-12-08 20:01:41 +0000 | [diff] [blame] | 575 | PropParentMap = 0; |
| 576 | CurrentBody = 0; |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 577 | DisableReplaceStmt = false; |
Fariborz Jahanian | f292fcf | 2010-01-07 22:51:18 +0000 | [diff] [blame] | 578 | objc_impl_method = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 579 | |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 580 | // Get the ID and start/end of the main file. |
| 581 | MainFileID = SM->getMainFileID(); |
| 582 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 583 | MainFileStart = MainBuf->getBufferStart(); |
| 584 | MainFileEnd = MainBuf->getBufferEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 585 | |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 586 | Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOptions()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 587 | |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 588 | // declaring objc_selector outside the parameter list removes a silly |
| 589 | // scope related warning... |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 590 | if (IsHeader) |
Steve Naroff | 62c2632 | 2009-02-03 20:39:18 +0000 | [diff] [blame] | 591 | Preamble = "#pragma once\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 592 | Preamble += "struct objc_selector; struct objc_class;\n"; |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 593 | Preamble += "struct __rw_objc_super { struct objc_object *object; "; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 594 | Preamble += "struct objc_object *superClass; "; |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 595 | if (LangOpts.MicrosoftExt) { |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 596 | // Add a constructor for creating temporary objects. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 597 | Preamble += "__rw_objc_super(struct objc_object *o, struct objc_object *s) " |
| 598 | ": "; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 599 | Preamble += "object(o), superClass(s) {} "; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 600 | } |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 601 | Preamble += "};\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 602 | Preamble += "#ifndef _REWRITER_typedef_Protocol\n"; |
| 603 | Preamble += "typedef struct objc_object Protocol;\n"; |
| 604 | Preamble += "#define _REWRITER_typedef_Protocol\n"; |
| 605 | Preamble += "#endif\n"; |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 606 | if (LangOpts.MicrosoftExt) { |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 607 | Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n"; |
| 608 | Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n"; |
| 609 | } else |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 610 | Preamble += "#define __OBJC_RW_DLLIMPORT extern\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 611 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 612 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 613 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 614 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Fariborz Jahanian | 336c8f7 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 615 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object* objc_msgSend_stret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 616 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Fariborz Jahanian | 336c8f7 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 617 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object* objc_msgSendSuper_stret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 618 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 619 | Preamble += "__OBJC_RW_DLLIMPORT double objc_msgSend_fpret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 620 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 621 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getClass"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 622 | Preamble += "(const char *);\n"; |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 623 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_class *class_getSuperclass"; |
| 624 | Preamble += "(struct objc_class *);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 625 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getMetaClass"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 626 | Preamble += "(const char *);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 627 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw(struct objc_object *);\n"; |
| 628 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_enter(void *);\n"; |
| 629 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_exit(void *);\n"; |
| 630 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_exception_extract(void *);\n"; |
| 631 | Preamble += "__OBJC_RW_DLLIMPORT int objc_exception_match"; |
Steve Naroff | 580ca78 | 2008-05-09 21:17:56 +0000 | [diff] [blame] | 632 | Preamble += "(struct objc_class *, struct objc_object *);\n"; |
Steve Naroff | 59f05a4 | 2008-07-16 18:58:11 +0000 | [diff] [blame] | 633 | // @synchronized hooks. |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 634 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_enter(struct objc_object *);\n"; |
| 635 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_exit(struct objc_object *);\n"; |
| 636 | Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 637 | Preamble += "#ifndef __FASTENUMERATIONSTATE\n"; |
| 638 | Preamble += "struct __objcFastEnumerationState {\n\t"; |
| 639 | Preamble += "unsigned long state;\n\t"; |
Steve Naroff | b10f273 | 2008-04-04 22:58:22 +0000 | [diff] [blame] | 640 | Preamble += "void **itemsPtr;\n\t"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 641 | Preamble += "unsigned long *mutationsPtr;\n\t"; |
| 642 | Preamble += "unsigned long extra[5];\n};\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 643 | Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 644 | Preamble += "#define __FASTENUMERATIONSTATE\n"; |
| 645 | Preamble += "#endif\n"; |
| 646 | Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n"; |
| 647 | Preamble += "struct __NSConstantStringImpl {\n"; |
| 648 | Preamble += " int *isa;\n"; |
| 649 | Preamble += " int flags;\n"; |
| 650 | Preamble += " char *str;\n"; |
| 651 | Preamble += " long length;\n"; |
| 652 | Preamble += "};\n"; |
Steve Naroff | 88bee74 | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 653 | Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n"; |
| 654 | Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n"; |
| 655 | Preamble += "#else\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 656 | Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n"; |
Steve Naroff | 88bee74 | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 657 | Preamble += "#endif\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 658 | Preamble += "#define __NSCONSTANTSTRINGIMPL\n"; |
| 659 | Preamble += "#endif\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 660 | // Blocks preamble. |
| 661 | Preamble += "#ifndef BLOCK_IMPL\n"; |
| 662 | Preamble += "#define BLOCK_IMPL\n"; |
| 663 | Preamble += "struct __block_impl {\n"; |
| 664 | Preamble += " void *isa;\n"; |
| 665 | Preamble += " int Flags;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 666 | Preamble += " int Reserved;\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 667 | Preamble += " void *FuncPtr;\n"; |
| 668 | Preamble += "};\n"; |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 669 | Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n"; |
Steve Naroff | c9c1e9c | 2009-12-06 01:52:22 +0000 | [diff] [blame] | 670 | Preamble += "#ifdef __OBJC_EXPORT_BLOCKS\n"; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 671 | Preamble += "extern \"C\" __declspec(dllexport) " |
| 672 | "void _Block_object_assign(void *, const void *, const int);\n"; |
Steve Naroff | a851e60 | 2009-12-06 01:33:56 +0000 | [diff] [blame] | 673 | Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_dispose(const void *, const int);\n"; |
| 674 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteGlobalBlock[32];\n"; |
| 675 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteStackBlock[32];\n"; |
| 676 | Preamble += "#else\n"; |
Steve Naroff | cd82637 | 2010-01-05 18:09:31 +0000 | [diff] [blame] | 677 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_assign(void *, const void *, const int);\n"; |
| 678 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_dispose(const void *, const int);\n"; |
Steve Naroff | a851e60 | 2009-12-06 01:33:56 +0000 | [diff] [blame] | 679 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteGlobalBlock[32];\n"; |
| 680 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteStackBlock[32];\n"; |
| 681 | Preamble += "#endif\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 682 | Preamble += "#endif\n"; |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 683 | if (LangOpts.MicrosoftExt) { |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 684 | Preamble += "#undef __OBJC_RW_DLLIMPORT\n"; |
| 685 | Preamble += "#undef __OBJC_RW_STATICIMPORT\n"; |
Chris Lattner | 553e583 | 2010-04-13 17:33:56 +0000 | [diff] [blame] | 686 | Preamble += "#ifndef KEEP_ATTRIBUTES\n"; // We use this for clang tests. |
Steve Naroff | a48396e | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 687 | Preamble += "#define __attribute__(X)\n"; |
Chris Lattner | 553e583 | 2010-04-13 17:33:56 +0000 | [diff] [blame] | 688 | Preamble += "#endif\n"; |
Fariborz Jahanian | 3420419 | 2010-01-15 22:29:39 +0000 | [diff] [blame] | 689 | Preamble += "#define __weak\n"; |
Steve Naroff | a48396e | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 690 | } |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 691 | else { |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 692 | Preamble += "#define __block\n"; |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 693 | Preamble += "#define __weak\n"; |
| 694 | } |
Fariborz Jahanian | df49652 | 2010-03-02 01:19:04 +0000 | [diff] [blame] | 695 | // NOTE! Windows uses LLP64 for 64bit mode. So, cast pointer to long long |
| 696 | // as this avoids warning in any 64bit/32bit compilation model. |
| 697 | Preamble += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((long long) &((TYPE *)0)->MEMBER)\n"; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 701 | //===----------------------------------------------------------------------===// |
| 702 | // Top Level Driver Code |
| 703 | //===----------------------------------------------------------------------===// |
| 704 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 705 | void RewriteObjC::HandleTopLevelSingleDecl(Decl *D) { |
Ted Kremenek | e50187a | 2010-02-05 21:28:51 +0000 | [diff] [blame] | 706 | if (Diags.hasErrorOccurred()) |
| 707 | return; |
| 708 | |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 709 | // Two cases: either the decl could be in the main file, or it could be in a |
| 710 | // #included file. If the former, rewrite it now. If the later, check to see |
| 711 | // if we rewrote the #include/#import. |
| 712 | SourceLocation Loc = D->getLocation(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 713 | Loc = SM->getExpansionLoc(Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 714 | |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 715 | // If this is for a builtin, ignore it. |
| 716 | if (Loc.isInvalid()) return; |
| 717 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 718 | // Look for built-in declarations that we need to refer during the rewrite. |
| 719 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 720 | RewriteFunctionDecl(FD); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 721 | } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) { |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 722 | // declared in <Foundation/NSString.h> |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 723 | if (FVD->getName() == "_NSConstantStringClassReference") { |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 724 | ConstantStringClassReference = FVD; |
| 725 | return; |
| 726 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 727 | } else if (ObjCInterfaceDecl *MD = dyn_cast<ObjCInterfaceDecl>(D)) { |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 728 | RewriteInterfaceDecl(MD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 729 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 730 | RewriteCategoryDecl(CD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 731 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 732 | RewriteProtocolDecl(PD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 733 | } else if (ObjCForwardProtocolDecl *FP = |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 734 | dyn_cast<ObjCForwardProtocolDecl>(D)){ |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 735 | RewriteForwardProtocolDecl(FP); |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 736 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
| 737 | // Recurse into linkage specifications |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 738 | for (DeclContext::decl_iterator DI = LSD->decls_begin(), |
| 739 | DIEnd = LSD->decls_end(); |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 740 | DI != DIEnd; ) { |
| 741 | if (isa<ObjCClassDecl>((*DI))) { |
| 742 | SmallVector<Decl *, 8> DG; |
| 743 | Decl *D = (*DI); |
| 744 | SourceLocation Loc = D->getLocation(); |
| 745 | while (DI != DIEnd && |
| 746 | isa<ObjCClassDecl>(D) && D->getLocation() == Loc) { |
| 747 | DG.push_back(D); |
| 748 | ++DI; |
| 749 | D = (*DI); |
| 750 | } |
| 751 | RewriteForwardClassDecl(DG); |
| 752 | continue; |
| 753 | } |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 754 | HandleTopLevelSingleDecl(*DI); |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 755 | ++DI; |
| 756 | } |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 757 | } |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 758 | // 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] | 759 | if (SM->isFromMainFile(Loc)) |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 760 | return HandleDeclInMainFile(D); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 763 | //===----------------------------------------------------------------------===// |
| 764 | // Syntactic (non-AST) Rewriting Code |
| 765 | //===----------------------------------------------------------------------===// |
| 766 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 767 | void RewriteObjC::RewriteInclude() { |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 768 | SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 769 | StringRef MainBuf = SM->getBufferData(MainFileID); |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 770 | const char *MainBufStart = MainBuf.begin(); |
| 771 | const char *MainBufEnd = MainBuf.end(); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 772 | size_t ImportLen = strlen("import"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 773 | |
Fariborz Jahanian | af57b46 | 2008-01-19 01:03:17 +0000 | [diff] [blame] | 774 | // Loop over the whole file, looking for includes. |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 775 | for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { |
| 776 | if (*BufPtr == '#') { |
| 777 | if (++BufPtr == MainBufEnd) |
| 778 | return; |
| 779 | while (*BufPtr == ' ' || *BufPtr == '\t') |
| 780 | if (++BufPtr == MainBufEnd) |
| 781 | return; |
| 782 | if (!strncmp(BufPtr, "import", ImportLen)) { |
| 783 | // replace import with include |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 784 | SourceLocation ImportLoc = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 785 | LocStart.getLocWithOffset(BufPtr-MainBufStart); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 786 | ReplaceText(ImportLoc, ImportLen, "include"); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 787 | BufPtr += ImportLen; |
| 788 | } |
| 789 | } |
| 790 | } |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 793 | static std::string getIvarAccessString(ObjCIvarDecl *OID) { |
| 794 | const ObjCInterfaceDecl *ClassDecl = OID->getContainingInterface(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 795 | std::string S; |
| 796 | S = "((struct "; |
| 797 | S += ClassDecl->getIdentifier()->getName(); |
| 798 | S += "_IMPL *)self)->"; |
Daniel Dunbar | 5ffe14c | 2009-10-18 20:26:27 +0000 | [diff] [blame] | 799 | S += OID->getName(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 800 | return S; |
| 801 | } |
| 802 | |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 803 | void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 804 | ObjCImplementationDecl *IMD, |
| 805 | ObjCCategoryImplDecl *CID) { |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 806 | static bool objcGetPropertyDefined = false; |
| 807 | static bool objcSetPropertyDefined = false; |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 808 | SourceLocation startLoc = PID->getLocStart(); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 809 | InsertText(startLoc, "// "); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 810 | const char *startBuf = SM->getCharacterData(startLoc); |
| 811 | assert((*startBuf == '@') && "bogus @synthesize location"); |
| 812 | const char *semiBuf = strchr(startBuf, ';'); |
| 813 | assert((*semiBuf == ';') && "@synthesize: can't find ';'"); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 814 | SourceLocation onePastSemiLoc = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 815 | startLoc.getLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 816 | |
| 817 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 818 | return; // FIXME: is this correct? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 819 | |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 820 | // Generate the 'getter' function. |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 821 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 822 | ObjCIvarDecl *OID = PID->getPropertyIvarDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 823 | |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 824 | if (!OID) |
| 825 | return; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 826 | unsigned Attributes = PD->getPropertyAttributes(); |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 827 | if (!PD->getGetterMethodDecl()->isDefined()) { |
| 828 | bool GenGetProperty = !(Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) && |
| 829 | (Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
| 830 | ObjCPropertyDecl::OBJC_PR_copy)); |
| 831 | std::string Getr; |
| 832 | if (GenGetProperty && !objcGetPropertyDefined) { |
| 833 | objcGetPropertyDefined = true; |
| 834 | // FIXME. Is this attribute correct in all cases? |
| 835 | Getr = "\nextern \"C\" __declspec(dllimport) " |
| 836 | "id objc_getProperty(id, SEL, long, bool);\n"; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 837 | } |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 838 | RewriteObjCMethodDecl(OID->getContainingInterface(), |
| 839 | PD->getGetterMethodDecl(), Getr); |
| 840 | Getr += "{ "; |
| 841 | // Synthesize an explicit cast to gain access to the ivar. |
| 842 | // See objc-act.c:objc_synthesize_new_getter() for details. |
| 843 | if (GenGetProperty) { |
| 844 | // return objc_getProperty(self, _cmd, offsetof(ClassDecl, OID), 1) |
| 845 | Getr += "typedef "; |
| 846 | const FunctionType *FPRetType = 0; |
| 847 | RewriteTypeIntoString(PD->getGetterMethodDecl()->getResultType(), Getr, |
| 848 | FPRetType); |
| 849 | Getr += " _TYPE"; |
| 850 | if (FPRetType) { |
| 851 | Getr += ")"; // close the precedence "scope" for "*". |
| 852 | |
| 853 | // Now, emit the argument types (if any). |
| 854 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)){ |
| 855 | Getr += "("; |
| 856 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 857 | if (i) Getr += ", "; |
| 858 | std::string ParamStr = FT->getArgType(i).getAsString( |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 859 | Context->getPrintingPolicy()); |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 860 | Getr += ParamStr; |
| 861 | } |
| 862 | if (FT->isVariadic()) { |
| 863 | if (FT->getNumArgs()) Getr += ", "; |
| 864 | Getr += "..."; |
| 865 | } |
| 866 | Getr += ")"; |
| 867 | } else |
| 868 | Getr += "()"; |
| 869 | } |
| 870 | Getr += ";\n"; |
| 871 | Getr += "return (_TYPE)"; |
| 872 | Getr += "objc_getProperty(self, _cmd, "; |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 873 | RewriteIvarOffsetComputation(OID, Getr); |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 874 | Getr += ", 1)"; |
| 875 | } |
| 876 | else |
| 877 | Getr += "return " + getIvarAccessString(OID); |
| 878 | Getr += "; }"; |
| 879 | InsertText(onePastSemiLoc, Getr); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 880 | } |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 881 | |
| 882 | if (PD->isReadOnly() || PD->getSetterMethodDecl()->isDefined()) |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 883 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 884 | |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 885 | // Generate the 'setter' function. |
| 886 | std::string Setr; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 887 | bool GenSetProperty = Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
| 888 | ObjCPropertyDecl::OBJC_PR_copy); |
| 889 | if (GenSetProperty && !objcSetPropertyDefined) { |
| 890 | objcSetPropertyDefined = true; |
| 891 | // FIXME. Is this attribute correct in all cases? |
| 892 | Setr = "\nextern \"C\" __declspec(dllimport) " |
| 893 | "void objc_setProperty (id, SEL, long, id, bool, bool);\n"; |
| 894 | } |
| 895 | |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 896 | RewriteObjCMethodDecl(OID->getContainingInterface(), |
| 897 | PD->getSetterMethodDecl(), Setr); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 898 | Setr += "{ "; |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 899 | // Synthesize an explicit cast to initialize the ivar. |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 900 | // See objc-act.c:objc_synthesize_new_setter() for details. |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 901 | if (GenSetProperty) { |
| 902 | Setr += "objc_setProperty (self, _cmd, "; |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 903 | RewriteIvarOffsetComputation(OID, Setr); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 904 | Setr += ", (id)"; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 905 | Setr += PD->getName(); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 906 | Setr += ", "; |
| 907 | if (Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 908 | Setr += "0, "; |
| 909 | else |
| 910 | Setr += "1, "; |
| 911 | if (Attributes & ObjCPropertyDecl::OBJC_PR_copy) |
| 912 | Setr += "1)"; |
| 913 | else |
| 914 | Setr += "0)"; |
| 915 | } |
| 916 | else { |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 917 | Setr += getIvarAccessString(OID) + " = "; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 918 | Setr += PD->getName(); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 919 | } |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 920 | Setr += "; }"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 921 | InsertText(onePastSemiLoc, Setr); |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 922 | } |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 923 | |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 924 | static void RewriteOneForwardClassDecl(ObjCInterfaceDecl *ForwardDecl, |
| 925 | std::string &typedefString) { |
| 926 | typedefString += "#ifndef _REWRITER_typedef_"; |
| 927 | typedefString += ForwardDecl->getNameAsString(); |
| 928 | typedefString += "\n"; |
| 929 | typedefString += "#define _REWRITER_typedef_"; |
| 930 | typedefString += ForwardDecl->getNameAsString(); |
| 931 | typedefString += "\n"; |
| 932 | typedefString += "typedef struct objc_object "; |
| 933 | typedefString += ForwardDecl->getNameAsString(); |
| 934 | typedefString += ";\n#endif\n"; |
| 935 | } |
| 936 | |
| 937 | void RewriteObjC::RewriteForwardClassEpilogue(ObjCClassDecl *ClassDecl, |
| 938 | const std::string &typedefString) { |
| 939 | SourceLocation startLoc = ClassDecl->getLocation(); |
| 940 | const char *startBuf = SM->getCharacterData(startLoc); |
| 941 | const char *semiPtr = strchr(startBuf, ';'); |
| 942 | // Replace the @class with typedefs corresponding to the classes. |
| 943 | ReplaceText(startLoc, semiPtr-startBuf+1, typedefString); |
| 944 | } |
| 945 | |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 946 | void RewriteObjC::RewriteForwardClassDecl(DeclGroupRef D) { |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 947 | std::string typedefString; |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 948 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
| 949 | ObjCClassDecl *ClassDecl = cast<ObjCClassDecl>(*I); |
| 950 | ObjCInterfaceDecl *ForwardDecl = ClassDecl->getForwardInterfaceDecl(); |
| 951 | if (I == D.begin()) { |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 952 | // Translate to typedef's that forward reference structs with the same name |
| 953 | // as the class. As a convenience, we include the original declaration |
| 954 | // as a comment. |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 955 | typedefString += "// @class "; |
| 956 | typedefString += ForwardDecl->getNameAsString(); |
Fariborz Jahanian | 91fbd12 | 2010-01-11 22:48:40 +0000 | [diff] [blame] | 957 | typedefString += ";\n"; |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 958 | } |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 959 | RewriteOneForwardClassDecl(ForwardDecl, typedefString); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 960 | } |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 961 | DeclGroupRef::iterator I = D.begin(); |
| 962 | RewriteForwardClassEpilogue(cast<ObjCClassDecl>(*I), typedefString); |
| 963 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 964 | |
Fariborz Jahanian | a5e2b23 | 2011-08-29 22:21:46 +0000 | [diff] [blame] | 965 | void RewriteObjC::RewriteForwardClassDecl( |
| 966 | const llvm::SmallVector<Decl*, 8> &D) { |
| 967 | std::string typedefString; |
| 968 | for (unsigned i = 0; i < D.size(); i++) { |
| 969 | ObjCClassDecl *ClassDecl = cast<ObjCClassDecl>(D[i]); |
| 970 | ObjCInterfaceDecl *ForwardDecl = ClassDecl->getForwardInterfaceDecl(); |
| 971 | if (i == 0) { |
| 972 | typedefString += "// @class "; |
| 973 | typedefString += ForwardDecl->getNameAsString(); |
| 974 | typedefString += ";\n"; |
| 975 | } |
| 976 | RewriteOneForwardClassDecl(ForwardDecl, typedefString); |
| 977 | } |
| 978 | RewriteForwardClassEpilogue(cast<ObjCClassDecl>(D[0]), typedefString); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 979 | } |
| 980 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 981 | void RewriteObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) { |
Fariborz Jahanian | d050240 | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 982 | // When method is a synthesized one, such as a getter/setter there is |
| 983 | // nothing to rewrite. |
Fariborz Jahanian | 8891480 | 2011-09-09 20:35:22 +0000 | [diff] [blame] | 984 | if (Method->isImplicit()) |
Fariborz Jahanian | d050240 | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 985 | return; |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 986 | SourceLocation LocStart = Method->getLocStart(); |
| 987 | SourceLocation LocEnd = Method->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 988 | |
Chandler Carruth | 6421162 | 2011-07-25 21:09:52 +0000 | [diff] [blame] | 989 | if (SM->getExpansionLineNumber(LocEnd) > |
| 990 | SM->getExpansionLineNumber(LocStart)) { |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 991 | InsertText(LocStart, "#if 0\n"); |
| 992 | ReplaceText(LocEnd, 1, ";\n#endif\n"); |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 993 | } else { |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 994 | InsertText(LocStart, "// "); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 995 | } |
| 996 | } |
| 997 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 998 | void RewriteObjC::RewriteProperty(ObjCPropertyDecl *prop) { |
Fariborz Jahanian | d050240 | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 999 | SourceLocation Loc = prop->getAtLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1000 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1001 | ReplaceText(Loc, 0, "// "); |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 1002 | // FIXME: handle properties that are declared across multiple lines. |
Fariborz Jahanian | 957cf65 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1005 | void RewriteObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 1006 | SourceLocation LocStart = CatDecl->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1007 | |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 1008 | // FIXME: handle category headers that are declared across multiple lines. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1009 | ReplaceText(LocStart, 0, "// "); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1010 | |
Fariborz Jahanian | 13751e3 | 2010-02-10 01:15:09 +0000 | [diff] [blame] | 1011 | for (ObjCCategoryDecl::prop_iterator I = CatDecl->prop_begin(), |
| 1012 | E = CatDecl->prop_end(); I != E; ++I) |
| 1013 | RewriteProperty(*I); |
| 1014 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1015 | for (ObjCCategoryDecl::instmeth_iterator |
| 1016 | I = CatDecl->instmeth_begin(), E = CatDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1017 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1018 | RewriteMethodDeclaration(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1019 | for (ObjCCategoryDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1020 | I = CatDecl->classmeth_begin(), E = CatDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1021 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1022 | RewriteMethodDeclaration(*I); |
| 1023 | |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 1024 | // Lastly, comment out the @end. |
Fariborz Jahanian | 73d1eb0 | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 1025 | ReplaceText(CatDecl->getAtEndRange().getBegin(), |
| 1026 | strlen("@end"), "/* @end */"); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1029 | void RewriteObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 1030 | SourceLocation LocStart = PDecl->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1031 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 1032 | // FIXME: handle protocol headers that are declared across multiple lines. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1033 | ReplaceText(LocStart, 0, "// "); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1034 | |
| 1035 | for (ObjCProtocolDecl::instmeth_iterator |
| 1036 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1037 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1038 | RewriteMethodDeclaration(*I); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1039 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1040 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1041 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1042 | RewriteMethodDeclaration(*I); |
| 1043 | |
Fariborz Jahanian | 07acdf4 | 2010-09-24 18:36:58 +0000 | [diff] [blame] | 1044 | for (ObjCInterfaceDecl::prop_iterator I = PDecl->prop_begin(), |
| 1045 | E = PDecl->prop_end(); I != E; ++I) |
| 1046 | RewriteProperty(*I); |
| 1047 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 1048 | // Lastly, comment out the @end. |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1049 | SourceLocation LocEnd = PDecl->getAtEndRange().getBegin(); |
Fariborz Jahanian | 73d1eb0 | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 1050 | ReplaceText(LocEnd, strlen("@end"), "/* @end */"); |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 1051 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 1052 | // Must comment out @optional/@required |
| 1053 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1054 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 1055 | for (const char *p = startBuf; p < endBuf; p++) { |
| 1056 | if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1057 | SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1058 | ReplaceText(OptionalLoc, strlen("@optional"), "/* @optional */"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1059 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 1060 | } |
| 1061 | else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1062 | SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1063 | ReplaceText(OptionalLoc, strlen("@required"), "/* @required */"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1064 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 1065 | } |
| 1066 | } |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1069 | void RewriteObjC::RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *PDecl) { |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 1070 | SourceLocation LocStart = PDecl->getLocation(); |
Steve Naroff | b7fa992 | 2007-11-14 03:37:28 +0000 | [diff] [blame] | 1071 | if (LocStart.isInvalid()) |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1072 | llvm_unreachable("Invalid SourceLocation"); |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 1073 | // FIXME: handle forward protocol that are declared across multiple lines. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1074 | ReplaceText(LocStart, 0, "// "); |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1077 | void RewriteObjC::RewriteTypeIntoString(QualType T, std::string &ResultStr, |
| 1078 | const FunctionType *&FPRetType) { |
| 1079 | if (T->isObjCQualifiedIdType()) |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1080 | ResultStr += "id"; |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1081 | else if (T->isFunctionPointerType() || |
| 1082 | T->isBlockPointerType()) { |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1083 | // needs special handling, since pointer-to-functions have special |
| 1084 | // syntax (where a decaration models use). |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1085 | QualType retType = T; |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1086 | QualType PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1087 | if (const PointerType* PT = retType->getAs<PointerType>()) |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1088 | PointeeTy = PT->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1089 | else if (const BlockPointerType *BPT = retType->getAs<BlockPointerType>()) |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1090 | PointeeTy = BPT->getPointeeType(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1091 | if ((FPRetType = PointeeTy->getAs<FunctionType>())) { |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1092 | ResultStr += FPRetType->getResultType().getAsString( |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1093 | Context->getPrintingPolicy()); |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1094 | ResultStr += "(*"; |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1095 | } |
| 1096 | } else |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1097 | ResultStr += T.getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1100 | void RewriteObjC::RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl, |
| 1101 | ObjCMethodDecl *OMD, |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1102 | std::string &ResultStr) { |
| 1103 | //fprintf(stderr,"In RewriteObjCMethodDecl\n"); |
| 1104 | const FunctionType *FPRetType = 0; |
| 1105 | ResultStr += "\nstatic "; |
| 1106 | RewriteTypeIntoString(OMD->getResultType(), ResultStr, FPRetType); |
Fariborz Jahanian | 531a1ea | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 1107 | ResultStr += " "; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1108 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1109 | // Unique method name |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1110 | std::string NameStr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1111 | |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1112 | if (OMD->isInstanceMethod()) |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1113 | NameStr += "_I_"; |
| 1114 | else |
| 1115 | NameStr += "_C_"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1116 | |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1117 | NameStr += IDecl->getNameAsString(); |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1118 | NameStr += "_"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1119 | |
| 1120 | if (ObjCCategoryImplDecl *CID = |
Steve Naroff | 3e0a540 | 2009-01-08 19:41:02 +0000 | [diff] [blame] | 1121 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1122 | NameStr += CID->getNameAsString(); |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1123 | NameStr += "_"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1124 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1125 | // Append selector names, replacing ':' with '_' |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1126 | { |
| 1127 | std::string selString = OMD->getSelector().getAsString(); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1128 | int len = selString.size(); |
| 1129 | for (int i = 0; i < len; i++) |
| 1130 | if (selString[i] == ':') |
| 1131 | selString[i] = '_'; |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1132 | NameStr += selString; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1133 | } |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1134 | // Remember this name for metadata emission |
| 1135 | MethodInternalNames[OMD] = NameStr; |
| 1136 | ResultStr += NameStr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1137 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1138 | // Rewrite arguments |
| 1139 | ResultStr += "("; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1140 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1141 | // invisible arguments |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1142 | if (OMD->isInstanceMethod()) { |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1143 | QualType selfTy = Context->getObjCInterfaceType(IDecl); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1144 | selfTy = Context->getPointerType(selfTy); |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 1145 | if (!LangOpts.MicrosoftExt) { |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1146 | if (ObjCSynthesizedStructs.count(const_cast<ObjCInterfaceDecl*>(IDecl))) |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 1147 | ResultStr += "struct "; |
| 1148 | } |
| 1149 | // When rewriting for Microsoft, explicitly omit the structure name. |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1150 | ResultStr += IDecl->getNameAsString(); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1151 | ResultStr += " *"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1152 | } |
| 1153 | else |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1154 | ResultStr += Context->getObjCClassType().getAsString( |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1155 | Context->getPrintingPolicy()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1156 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1157 | ResultStr += " self, "; |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1158 | ResultStr += Context->getObjCSelType().getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1159 | ResultStr += " _cmd"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1160 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1161 | // Method arguments. |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1162 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 1163 | E = OMD->param_end(); PI != E; ++PI) { |
| 1164 | ParmVarDecl *PDecl = *PI; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1165 | ResultStr += ", "; |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1166 | if (PDecl->getType()->isObjCQualifiedIdType()) { |
| 1167 | ResultStr += "id "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1168 | ResultStr += PDecl->getNameAsString(); |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1169 | } else { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1170 | std::string Name = PDecl->getNameAsString(); |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 1171 | QualType QT = PDecl->getType(); |
| 1172 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
| 1173 | if (convertBlockPointerToFunctionPointer(QT)) |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1174 | QT.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 1175 | else |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1176 | PDecl->getType().getAsStringInternal(Name, Context->getPrintingPolicy()); |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1177 | ResultStr += Name; |
| 1178 | } |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1179 | } |
Fariborz Jahanian | 7c39ff7 | 2008-01-21 20:14:23 +0000 | [diff] [blame] | 1180 | if (OMD->isVariadic()) |
| 1181 | ResultStr += ", ..."; |
Fariborz Jahanian | 531a1ea | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 1182 | ResultStr += ") "; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1183 | |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1184 | if (FPRetType) { |
| 1185 | ResultStr += ")"; // close the precedence "scope" for "*". |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1186 | |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1187 | // Now, emit the argument types (if any). |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1188 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) { |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1189 | ResultStr += "("; |
| 1190 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 1191 | if (i) ResultStr += ", "; |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 1192 | std::string ParamStr = FT->getArgType(i).getAsString( |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1193 | Context->getPrintingPolicy()); |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1194 | ResultStr += ParamStr; |
| 1195 | } |
| 1196 | if (FT->isVariadic()) { |
| 1197 | if (FT->getNumArgs()) ResultStr += ", "; |
| 1198 | ResultStr += "..."; |
| 1199 | } |
| 1200 | ResultStr += ")"; |
| 1201 | } else { |
| 1202 | ResultStr += "()"; |
| 1203 | } |
| 1204 | } |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1205 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1206 | void RewriteObjC::RewriteImplementationDecl(Decl *OID) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1207 | ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID); |
| 1208 | ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1209 | |
Fariborz Jahanian | a135216 | 2010-02-15 21:11:41 +0000 | [diff] [blame] | 1210 | InsertText(IMD ? IMD->getLocStart() : CID->getLocStart(), "// "); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1211 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1212 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1213 | I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(), |
| 1214 | E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1215 | I != E; ++I) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1216 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1217 | ObjCMethodDecl *OMD = *I; |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1218 | RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1219 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1220 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1221 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1222 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1223 | const char *endBuf = SM->getCharacterData(LocEnd); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1224 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1225 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1226 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1227 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1228 | I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(), |
| 1229 | E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1230 | I != E; ++I) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1231 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1232 | ObjCMethodDecl *OMD = *I; |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 1233 | RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1234 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1235 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1236 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1237 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1238 | const char *endBuf = SM->getCharacterData(LocEnd); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1239 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1240 | } |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1241 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1242 | I = IMD ? IMD->propimpl_begin() : CID->propimpl_begin(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1243 | E = IMD ? IMD->propimpl_end() : CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1244 | I != E; ++I) { |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 1245 | RewritePropertyImplDecl(*I, IMD, CID); |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1248 | InsertText(IMD ? IMD->getLocEnd() : CID->getLocEnd(), "// "); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1251 | void RewriteObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { |
Steve Naroff | f908a87 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 1252 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1253 | if (!ObjCForwardDecls.count(ClassDecl)) { |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1254 | // we haven't seen a forward decl - generate a typedef. |
Steve Naroff | 5086a8d | 2007-11-14 23:02:56 +0000 | [diff] [blame] | 1255 | ResultStr = "#ifndef _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1256 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1257 | ResultStr += "\n"; |
| 1258 | ResultStr += "#define _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1259 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1260 | ResultStr += "\n"; |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1261 | ResultStr += "typedef struct objc_object "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1262 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1263 | ResultStr += ";\n#endif\n"; |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1264 | // Mark this typedef as having been generated. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1265 | ObjCForwardDecls.insert(ClassDecl); |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1266 | } |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 1267 | RewriteObjCInternalStruct(ClassDecl, ResultStr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1268 | |
| 1269 | for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1270 | E = ClassDecl->prop_end(); I != E; ++I) |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 1271 | RewriteProperty(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1272 | for (ObjCInterfaceDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1273 | I = ClassDecl->instmeth_begin(), E = ClassDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1274 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1275 | RewriteMethodDeclaration(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1276 | for (ObjCInterfaceDecl::classmeth_iterator |
| 1277 | I = ClassDecl->classmeth_begin(), E = ClassDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1278 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1279 | RewriteMethodDeclaration(*I); |
| 1280 | |
Steve Naroff | 2feac5e | 2007-10-30 03:43:13 +0000 | [diff] [blame] | 1281 | // Lastly, comment out the @end. |
Fariborz Jahanian | 73d1eb0 | 2010-05-24 17:22:38 +0000 | [diff] [blame] | 1282 | ReplaceText(ClassDecl->getAtEndRange().getBegin(), strlen("@end"), |
| 1283 | "/* @end */"); |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1286 | Stmt *RewriteObjC::RewritePropertyOrImplicitSetter(PseudoObjectExpr *PseudoOp) { |
| 1287 | SourceRange OldRange = PseudoOp->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1288 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1289 | // We just magically know some things about the structure of this |
| 1290 | // expression. |
| 1291 | ObjCMessageExpr *OldMsg = |
| 1292 | cast<ObjCMessageExpr>(PseudoOp->getSemanticExpr( |
| 1293 | PseudoOp->getNumSemanticExprs() - 1)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1294 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1295 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 1296 | // we need to suppress rewriting the sub-statements. |
| 1297 | Expr *Base, *RHS; |
| 1298 | { |
| 1299 | DisableReplaceStmtScope S(*this); |
| 1300 | |
| 1301 | // Rebuild the base expression if we have one. |
| 1302 | Base = 0; |
| 1303 | if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) { |
| 1304 | Base = OldMsg->getInstanceReceiver(); |
| 1305 | Base = cast<OpaqueValueExpr>(Base)->getSourceExpr(); |
| 1306 | Base = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Base)); |
| 1307 | } |
| 1308 | |
| 1309 | // Rebuild the RHS. |
| 1310 | RHS = cast<BinaryOperator>(PseudoOp->getSyntacticForm())->getRHS(); |
| 1311 | RHS = cast<OpaqueValueExpr>(RHS)->getSourceExpr(); |
| 1312 | RHS = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(RHS)); |
| 1313 | } |
| 1314 | |
| 1315 | // TODO: avoid this copy. |
| 1316 | SmallVector<SourceLocation, 1> SelLocs; |
| 1317 | OldMsg->getSelectorLocs(SelLocs); |
| 1318 | |
| 1319 | ObjCMessageExpr *NewMsg; |
| 1320 | switch (OldMsg->getReceiverKind()) { |
| 1321 | case ObjCMessageExpr::Class: |
| 1322 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1323 | OldMsg->getValueKind(), |
| 1324 | OldMsg->getLeftLoc(), |
| 1325 | OldMsg->getClassReceiverTypeInfo(), |
| 1326 | OldMsg->getSelector(), |
| 1327 | SelLocs, |
| 1328 | OldMsg->getMethodDecl(), |
| 1329 | RHS, |
| 1330 | OldMsg->getRightLoc()); |
| 1331 | break; |
| 1332 | |
| 1333 | case ObjCMessageExpr::Instance: |
| 1334 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1335 | OldMsg->getValueKind(), |
| 1336 | OldMsg->getLeftLoc(), |
| 1337 | Base, |
| 1338 | OldMsg->getSelector(), |
| 1339 | SelLocs, |
| 1340 | OldMsg->getMethodDecl(), |
| 1341 | RHS, |
| 1342 | OldMsg->getRightLoc()); |
| 1343 | break; |
| 1344 | |
| 1345 | case ObjCMessageExpr::SuperClass: |
| 1346 | case ObjCMessageExpr::SuperInstance: |
| 1347 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1348 | OldMsg->getValueKind(), |
| 1349 | OldMsg->getLeftLoc(), |
| 1350 | OldMsg->getSuperLoc(), |
| 1351 | OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance, |
| 1352 | OldMsg->getSuperType(), |
| 1353 | OldMsg->getSelector(), |
| 1354 | SelLocs, |
| 1355 | OldMsg->getMethodDecl(), |
| 1356 | RHS, |
| 1357 | OldMsg->getRightLoc()); |
| 1358 | break; |
| 1359 | } |
| 1360 | |
| 1361 | Stmt *Replacement = SynthMessageExpr(NewMsg); |
| 1362 | ReplaceStmtWithRange(PseudoOp, Replacement, OldRange); |
| 1363 | return Replacement; |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1366 | Stmt *RewriteObjC::RewritePropertyOrImplicitGetter(PseudoObjectExpr *PseudoOp) { |
| 1367 | SourceRange OldRange = PseudoOp->getSourceRange(); |
| 1368 | |
| 1369 | // We just magically know some things about the structure of this |
| 1370 | // expression. |
| 1371 | ObjCMessageExpr *OldMsg = |
| 1372 | cast<ObjCMessageExpr>(PseudoOp->getResultExpr()->IgnoreImplicit()); |
| 1373 | |
| 1374 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 1375 | // we need to suppress rewriting the sub-statements. |
| 1376 | Expr *Base = 0; |
| 1377 | { |
| 1378 | DisableReplaceStmtScope S(*this); |
| 1379 | |
| 1380 | // Rebuild the base expression if we have one. |
| 1381 | if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) { |
| 1382 | Base = OldMsg->getInstanceReceiver(); |
| 1383 | Base = cast<OpaqueValueExpr>(Base)->getSourceExpr(); |
| 1384 | Base = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Base)); |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 1385 | } |
Fariborz Jahanian | e0f8386 | 2010-10-20 16:07:20 +0000 | [diff] [blame] | 1386 | } |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1387 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1388 | // Intentionally empty. |
| 1389 | SmallVector<SourceLocation, 1> SelLocs; |
| 1390 | SmallVector<Expr*, 1> Args; |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1391 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1392 | ObjCMessageExpr *NewMsg; |
| 1393 | switch (OldMsg->getReceiverKind()) { |
| 1394 | case ObjCMessageExpr::Class: |
| 1395 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1396 | OldMsg->getValueKind(), |
| 1397 | OldMsg->getLeftLoc(), |
| 1398 | OldMsg->getClassReceiverTypeInfo(), |
| 1399 | OldMsg->getSelector(), |
| 1400 | SelLocs, |
| 1401 | OldMsg->getMethodDecl(), |
| 1402 | Args, |
| 1403 | OldMsg->getRightLoc()); |
| 1404 | break; |
| 1405 | |
| 1406 | case ObjCMessageExpr::Instance: |
| 1407 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1408 | OldMsg->getValueKind(), |
| 1409 | OldMsg->getLeftLoc(), |
| 1410 | Base, |
| 1411 | OldMsg->getSelector(), |
| 1412 | SelLocs, |
| 1413 | OldMsg->getMethodDecl(), |
| 1414 | Args, |
| 1415 | OldMsg->getRightLoc()); |
| 1416 | break; |
| 1417 | |
| 1418 | case ObjCMessageExpr::SuperClass: |
| 1419 | case ObjCMessageExpr::SuperInstance: |
| 1420 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1421 | OldMsg->getValueKind(), |
| 1422 | OldMsg->getLeftLoc(), |
| 1423 | OldMsg->getSuperLoc(), |
| 1424 | OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance, |
| 1425 | OldMsg->getSuperType(), |
| 1426 | OldMsg->getSelector(), |
| 1427 | SelLocs, |
| 1428 | OldMsg->getMethodDecl(), |
| 1429 | Args, |
| 1430 | OldMsg->getRightLoc()); |
| 1431 | break; |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1432 | } |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1433 | |
| 1434 | Stmt *Replacement = SynthMessageExpr(NewMsg); |
| 1435 | ReplaceStmtWithRange(PseudoOp, Replacement, OldRange); |
| 1436 | return Replacement; |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1439 | Stmt *RewriteObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV) { |
| 1440 | SourceRange OldRange = IV->getSourceRange(); |
| 1441 | Expr *BaseExpr = IV->getBase(); |
| 1442 | |
| 1443 | // Rewrite the base, but without actually doing replaces. |
| 1444 | { |
| 1445 | DisableReplaceStmtScope S(*this); |
| 1446 | BaseExpr = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(BaseExpr)); |
| 1447 | IV->setBase(BaseExpr); |
| 1448 | } |
| 1449 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1450 | ObjCIvarDecl *D = IV->getDecl(); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1451 | |
| 1452 | Expr *Replacement = IV; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1453 | if (CurMethodDef) { |
Fariborz Jahanian | 8f09543 | 2010-01-26 18:28:51 +0000 | [diff] [blame] | 1454 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1455 | const ObjCInterfaceType *iFaceDecl = |
Fariborz Jahanian | 84ed600 | 2010-01-07 18:18:32 +0000 | [diff] [blame] | 1456 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
Fariborz Jahanian | ffbdead | 2010-01-26 20:37:44 +0000 | [diff] [blame] | 1457 | assert(iFaceDecl && "RewriteObjCIvarRefExpr - iFaceDecl is null"); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1458 | // lookup which class implements the instance variable. |
| 1459 | ObjCInterfaceDecl *clsDeclared = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1460 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1461 | clsDeclared); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1462 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1463 | |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1464 | // Synthesize an explicit cast to gain access to the ivar. |
| 1465 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1466 | RecName += "_IMPL"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1467 | IdentifierInfo *II = &Context->Idents.get(RecName); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1468 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1469 | SourceLocation(), SourceLocation(), |
| 1470 | II); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1471 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1472 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1473 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 1474 | CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1475 | IV->getBase()); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1476 | // Don't forget the parens to enforce the proper binding. |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1477 | ParenExpr *PE = new (Context) ParenExpr(OldRange.getBegin(), |
| 1478 | OldRange.getEnd(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1479 | castExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1480 | if (IV->isFreeIvar() && |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1481 | CurMethodDef->getClassInterface() == iFaceDecl->getDecl()) { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1482 | MemberExpr *ME = new (Context) MemberExpr(PE, true, D, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1483 | IV->getLocation(), |
| 1484 | D->getType(), |
| 1485 | VK_LValue, OK_Ordinary); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1486 | Replacement = ME; |
| 1487 | } else { |
| 1488 | IV->setBase(PE); |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1489 | } |
| 1490 | } |
Steve Naroff | 84472a8 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1491 | } else { // we are outside a method. |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1492 | assert(!IV->isFreeIvar() && "Cannot have a free standing ivar outside a method"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1493 | |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1494 | // Explicit ivar refs need to have a cast inserted. |
| 1495 | // FIXME: consider sharing some of this code with the code above. |
Fariborz Jahanian | 26337b2 | 2010-01-12 17:31:23 +0000 | [diff] [blame] | 1496 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1497 | const ObjCInterfaceType *iFaceDecl = |
Fariborz Jahanian | c374cd9 | 2010-01-11 17:50:35 +0000 | [diff] [blame] | 1498 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1499 | // lookup which class implements the instance variable. |
| 1500 | ObjCInterfaceDecl *clsDeclared = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1501 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1502 | clsDeclared); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1503 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1504 | |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1505 | // Synthesize an explicit cast to gain access to the ivar. |
| 1506 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1507 | RecName += "_IMPL"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1508 | IdentifierInfo *II = &Context->Idents.get(RecName); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1509 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1510 | SourceLocation(), SourceLocation(), |
| 1511 | II); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1512 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1513 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1514 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 1515 | CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1516 | IV->getBase()); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1517 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1518 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
Chris Lattner | 8d36616 | 2008-05-28 16:38:23 +0000 | [diff] [blame] | 1519 | IV->getBase()->getLocEnd(), castExpr); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1520 | // Cannot delete IV->getBase(), since PE points to it. |
| 1521 | // Replace the old base with the cast. This is important when doing |
| 1522 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1523 | IV->setBase(PE); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1524 | } |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1525 | } |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 1526 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1527 | ReplaceStmtWithRange(IV, Replacement, OldRange); |
| 1528 | return Replacement; |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1531 | /// SynthCountByEnumWithState - To print: |
| 1532 | /// ((unsigned int (*) |
| 1533 | /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1534 | /// (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1535 | /// sel_registerName( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1536 | /// "countByEnumeratingWithState:objects:count:"), |
| 1537 | /// &enumState, |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1538 | /// (id *)__rw_items, (unsigned int)16) |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1539 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1540 | void RewriteObjC::SynthCountByEnumWithState(std::string &buf) { |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1541 | buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, " |
| 1542 | "id *, unsigned int))(void *)objc_msgSend)"; |
| 1543 | buf += "\n\t\t"; |
| 1544 | buf += "((id)l_collection,\n\t\t"; |
| 1545 | buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; |
| 1546 | buf += "\n\t\t"; |
| 1547 | buf += "&enumState, " |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1548 | "(id *)__rw_items, (unsigned int)16)"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1549 | } |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1550 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1551 | /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach |
| 1552 | /// statement to exit to its outer synthesized loop. |
| 1553 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1554 | Stmt *RewriteObjC::RewriteBreakStmt(BreakStmt *S) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1555 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1556 | return S; |
| 1557 | // replace break with goto __break_label |
| 1558 | std::string buf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1559 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1560 | SourceLocation startLoc = S->getLocStart(); |
| 1561 | buf = "goto __break_label_"; |
| 1562 | buf += utostr(ObjCBcLabelNo.back()); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1563 | ReplaceText(startLoc, strlen("break"), buf); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1564 | |
| 1565 | return 0; |
| 1566 | } |
| 1567 | |
| 1568 | /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach |
| 1569 | /// statement to continue with its inner synthesized loop. |
| 1570 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1571 | Stmt *RewriteObjC::RewriteContinueStmt(ContinueStmt *S) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1572 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1573 | return S; |
| 1574 | // replace continue with goto __continue_label |
| 1575 | std::string buf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1576 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1577 | SourceLocation startLoc = S->getLocStart(); |
| 1578 | buf = "goto __continue_label_"; |
| 1579 | buf += utostr(ObjCBcLabelNo.back()); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1580 | ReplaceText(startLoc, strlen("continue"), buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1581 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1582 | return 0; |
| 1583 | } |
| 1584 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1585 | /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1586 | /// It rewrites: |
| 1587 | /// for ( type elem in collection) { stmts; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1588 | |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1589 | /// Into: |
| 1590 | /// { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1591 | /// type elem; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1592 | /// struct __objcFastEnumerationState enumState = { 0 }; |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1593 | /// id __rw_items[16]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1594 | /// id l_collection = (id)collection; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1595 | /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1596 | /// objects:__rw_items count:16]; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1597 | /// if (limit) { |
| 1598 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1599 | /// do { |
| 1600 | /// unsigned long counter = 0; |
| 1601 | /// do { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1602 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1603 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1604 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1605 | /// stmts; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1606 | /// __continue_label: ; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1607 | /// } while (counter < limit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1608 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1609 | /// objects:__rw_items count:16]); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1610 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1611 | /// __break_label: ; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1612 | /// } |
| 1613 | /// else |
| 1614 | /// elem = nil; |
| 1615 | /// } |
| 1616 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1617 | Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 1618 | SourceLocation OrigEnd) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1619 | assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1620 | assert(isa<ObjCForCollectionStmt>(Stmts.back()) && |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1621 | "ObjCForCollectionStmt Statement stack mismatch"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1622 | assert(!ObjCBcLabelNo.empty() && |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1623 | "ObjCForCollectionStmt - Label No stack empty"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1624 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1625 | SourceLocation startLoc = S->getLocStart(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1626 | const char *startBuf = SM->getCharacterData(startLoc); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1627 | StringRef elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1628 | std::string elementTypeAsString; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1629 | std::string buf; |
| 1630 | buf = "\n{\n\t"; |
| 1631 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { |
| 1632 | // type elem; |
Chris Lattner | 7e24e82 | 2009-03-28 06:33:19 +0000 | [diff] [blame] | 1633 | NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl()); |
Ted Kremenek | 1ed8e2a | 2008-10-06 22:16:13 +0000 | [diff] [blame] | 1634 | QualType ElementType = cast<ValueDecl>(D)->getType(); |
Steve Naroff | e89b8e7 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1635 | if (ElementType->isObjCQualifiedIdType() || |
| 1636 | ElementType->isObjCQualifiedInterfaceType()) |
| 1637 | // Simply use 'id' for all qualified types. |
| 1638 | elementTypeAsString = "id"; |
| 1639 | else |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1640 | elementTypeAsString = ElementType.getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1641 | buf += elementTypeAsString; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1642 | buf += " "; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 1643 | elementName = D->getName(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1644 | buf += elementName; |
| 1645 | buf += ";\n\t"; |
| 1646 | } |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1647 | else { |
| 1648 | DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 1649 | elementName = DR->getDecl()->getName(); |
Steve Naroff | e89b8e7 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1650 | ValueDecl *VD = cast<ValueDecl>(DR->getDecl()); |
| 1651 | if (VD->getType()->isObjCQualifiedIdType() || |
| 1652 | VD->getType()->isObjCQualifiedInterfaceType()) |
| 1653 | // Simply use 'id' for all qualified types. |
| 1654 | elementTypeAsString = "id"; |
| 1655 | else |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1656 | elementTypeAsString = VD->getType().getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1657 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1658 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1659 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1660 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1661 | // id __rw_items[16]; |
| 1662 | buf += "id __rw_items[16];\n\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1663 | // id l_collection = (id) |
| 1664 | buf += "id l_collection = (id)"; |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1665 | // Find start location of 'collection' the hard way! |
| 1666 | const char *startCollectionBuf = startBuf; |
| 1667 | startCollectionBuf += 3; // skip 'for' |
| 1668 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1669 | startCollectionBuf++; // skip '(' |
| 1670 | // find 'in' and skip it. |
| 1671 | while (*startCollectionBuf != ' ' || |
| 1672 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1673 | (*(startCollectionBuf+3) != ' ' && |
| 1674 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1675 | startCollectionBuf++; |
| 1676 | startCollectionBuf += 3; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1677 | |
| 1678 | // Replace: "for (type element in" with string constructed thus far. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1679 | ReplaceText(startLoc, startCollectionBuf - startBuf, buf); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1680 | // Replace ')' in for '(' type elem in collection ')' with ';' |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1681 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1682 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1683 | SourceLocation lparenLoc = startLoc.getLocWithOffset(rparenBuf-startBuf); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1684 | buf = ";\n\t"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1685 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1686 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1687 | // objects:__rw_items count:16]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1688 | // which is synthesized into: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1689 | // unsigned int limit = |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1690 | // ((unsigned int (*) |
| 1691 | // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1692 | // (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1693 | // sel_registerName( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1694 | // "countByEnumeratingWithState:objects:count:"), |
| 1695 | // (struct __objcFastEnumerationState *)&state, |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1696 | // (id *)__rw_items, (unsigned int)16); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1697 | buf += "unsigned long limit =\n\t\t"; |
| 1698 | SynthCountByEnumWithState(buf); |
| 1699 | buf += ";\n\t"; |
| 1700 | /// if (limit) { |
| 1701 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1702 | /// do { |
| 1703 | /// unsigned long counter = 0; |
| 1704 | /// do { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1705 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1706 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1707 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1708 | buf += "if (limit) {\n\t"; |
| 1709 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1710 | buf += "do {\n\t\t"; |
| 1711 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1712 | buf += "do {\n\t\t\t"; |
| 1713 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1714 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1715 | buf += elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1716 | buf += " = ("; |
| 1717 | buf += elementTypeAsString; |
| 1718 | buf += ")enumState.itemsPtr[counter++];"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1719 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1720 | ReplaceText(lparenLoc, 1, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1721 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1722 | /// __continue_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1723 | /// } while (counter < limit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1724 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 108fb14 | 2011-11-09 17:41:43 +0000 | [diff] [blame] | 1725 | /// objects:__rw_items count:16]); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1726 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1727 | /// __break_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1728 | /// } |
| 1729 | /// else |
| 1730 | /// elem = nil; |
| 1731 | /// } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1732 | /// |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1733 | buf = ";\n\t"; |
| 1734 | buf += "__continue_label_"; |
| 1735 | buf += utostr(ObjCBcLabelNo.back()); |
| 1736 | buf += ": ;"; |
| 1737 | buf += "\n\t\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1738 | buf += "} while (counter < limit);\n\t"; |
| 1739 | buf += "} while (limit = "; |
| 1740 | SynthCountByEnumWithState(buf); |
| 1741 | buf += ");\n\t"; |
| 1742 | buf += elementName; |
Fariborz Jahanian | 65b0aa5 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1743 | buf += " = (("; |
| 1744 | buf += elementTypeAsString; |
| 1745 | buf += ")0);\n\t"; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1746 | buf += "__break_label_"; |
| 1747 | buf += utostr(ObjCBcLabelNo.back()); |
| 1748 | buf += ": ;\n\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1749 | buf += "}\n\t"; |
| 1750 | buf += "else\n\t\t"; |
| 1751 | buf += elementName; |
Fariborz Jahanian | 65b0aa5 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1752 | buf += " = (("; |
| 1753 | buf += elementTypeAsString; |
| 1754 | buf += ")0);\n\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1755 | buf += "}\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1756 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1757 | // Insert all these *after* the statement body. |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1758 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1759 | if (isa<CompoundStmt>(S->getBody())) { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1760 | SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1761 | InsertText(endBodyLoc, buf); |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1762 | } else { |
| 1763 | /* Need to treat single statements specially. For example: |
| 1764 | * |
| 1765 | * for (A *a in b) if (stuff()) break; |
| 1766 | * for (A *a in b) xxxyy; |
| 1767 | * |
| 1768 | * The following code simply scans ahead to the semi to find the actual end. |
| 1769 | */ |
| 1770 | const char *stmtBuf = SM->getCharacterData(OrigEnd); |
| 1771 | const char *semiBuf = strchr(stmtBuf, ';'); |
| 1772 | assert(semiBuf && "Can't find ';'"); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1773 | SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(semiBuf-stmtBuf+1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1774 | InsertText(endBodyLoc, buf); |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1775 | } |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1776 | Stmts.pop_back(); |
| 1777 | ObjCBcLabelNo.pop_back(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1778 | return 0; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1781 | /// RewriteObjCSynchronizedStmt - |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1782 | /// This routine rewrites @synchronized(expr) stmt; |
| 1783 | /// into: |
| 1784 | /// objc_sync_enter(expr); |
| 1785 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1786 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1787 | Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1788 | // Get the start location and compute the semi location. |
| 1789 | SourceLocation startLoc = S->getLocStart(); |
| 1790 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1791 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1792 | assert((*startBuf == '@') && "bogus @synchronized location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1793 | |
| 1794 | std::string buf; |
Steve Naroff | 3498cc9 | 2008-08-21 13:03:03 +0000 | [diff] [blame] | 1795 | buf = "objc_sync_enter((id)"; |
| 1796 | const char *lparenBuf = startBuf; |
| 1797 | while (*lparenBuf != '(') lparenBuf++; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1798 | ReplaceText(startLoc, lparenBuf-startBuf+1, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1799 | // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since |
| 1800 | // the sync expression is typically a message expression that's already |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1801 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1802 | SourceLocation endLoc = S->getSynchBody()->getLocStart(); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1803 | const char *endBuf = SM->getCharacterData(endLoc); |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1804 | while (*endBuf != ')') endBuf--; |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1805 | SourceLocation rparenLoc = startLoc.getLocWithOffset(endBuf-startBuf); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1806 | buf = ");\n"; |
| 1807 | // declare a new scope with two variables, _stack and _rethrow. |
| 1808 | buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n"; |
| 1809 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1810 | buf += "char *pointers[4];} _stack;\n"; |
| 1811 | buf += "id volatile _rethrow = 0;\n"; |
| 1812 | buf += "objc_exception_try_enter(&_stack);\n"; |
| 1813 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1814 | ReplaceText(rparenLoc, 1, buf); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1815 | startLoc = S->getSynchBody()->getLocEnd(); |
| 1816 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1817 | |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1818 | assert((*startBuf == '}') && "bogus @synchronized block"); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1819 | SourceLocation lastCurlyLoc = startLoc; |
| 1820 | buf = "}\nelse {\n"; |
| 1821 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 1822 | buf += "}\n"; |
| 1823 | buf += "{ /* implicit finally clause */\n"; |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1824 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1825 | |
| 1826 | std::string syncBuf; |
| 1827 | syncBuf += " objc_sync_exit("; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 1828 | |
| 1829 | Expr *syncExpr = S->getSynchExpr(); |
| 1830 | CastKind CK = syncExpr->getType()->isObjCObjectPointerType() |
| 1831 | ? CK_BitCast : |
| 1832 | syncExpr->getType()->isBlockPointerType() |
| 1833 | ? CK_BlockPointerToObjCPointerCast |
| 1834 | : CK_CPointerToObjCPointerCast; |
| 1835 | syncExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 1836 | CK, syncExpr); |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1837 | std::string syncExprBufS; |
| 1838 | llvm::raw_string_ostream syncExprBuf(syncExprBufS); |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1839 | syncExpr->printPretty(syncExprBuf, *Context, 0, |
| 1840 | PrintingPolicy(LangOpts)); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1841 | syncBuf += syncExprBuf.str(); |
| 1842 | syncBuf += ");"; |
| 1843 | |
| 1844 | buf += syncBuf; |
| 1845 | buf += "\n if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1846 | buf += "}\n"; |
| 1847 | buf += "}"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1848 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1849 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1850 | |
| 1851 | bool hasReturns = false; |
| 1852 | HasReturnStmts(S->getSynchBody(), hasReturns); |
| 1853 | if (hasReturns) |
| 1854 | RewriteSyncReturnStmts(S->getSynchBody(), syncBuf); |
| 1855 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1856 | return 0; |
| 1857 | } |
| 1858 | |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1859 | void RewriteObjC::WarnAboutReturnGotoStmts(Stmt *S) |
| 1860 | { |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1861 | // Perform a bottom up traversal of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1862 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1863 | if (*CI) |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1864 | WarnAboutReturnGotoStmts(*CI); |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1865 | |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1866 | if (isa<ReturnStmt>(S) || isa<GotoStmt>(S)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1867 | Diags.Report(Context->getFullLoc(S->getLocStart()), |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1868 | TryFinallyContainsReturnDiag); |
| 1869 | } |
| 1870 | return; |
| 1871 | } |
| 1872 | |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1873 | void RewriteObjC::HasReturnStmts(Stmt *S, bool &hasReturns) |
| 1874 | { |
| 1875 | // Perform a bottom up traversal of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1876 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1877 | if (*CI) |
| 1878 | HasReturnStmts(*CI, hasReturns); |
| 1879 | |
| 1880 | if (isa<ReturnStmt>(S)) |
| 1881 | hasReturns = true; |
| 1882 | return; |
| 1883 | } |
| 1884 | |
| 1885 | void RewriteObjC::RewriteTryReturnStmts(Stmt *S) { |
| 1886 | // Perform a bottom up traversal of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1887 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1888 | if (*CI) { |
| 1889 | RewriteTryReturnStmts(*CI); |
| 1890 | } |
| 1891 | if (isa<ReturnStmt>(S)) { |
| 1892 | SourceLocation startLoc = S->getLocStart(); |
| 1893 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1894 | |
| 1895 | const char *semiBuf = strchr(startBuf, ';'); |
| 1896 | assert((*semiBuf == ';') && "RewriteTryReturnStmts: can't find ';'"); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1897 | SourceLocation onePastSemiLoc = startLoc.getLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1898 | |
| 1899 | std::string buf; |
| 1900 | buf = "{ objc_exception_try_exit(&_stack); return"; |
| 1901 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1902 | ReplaceText(startLoc, 6, buf); |
| 1903 | InsertText(onePastSemiLoc, "}"); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1904 | } |
| 1905 | return; |
| 1906 | } |
| 1907 | |
| 1908 | void RewriteObjC::RewriteSyncReturnStmts(Stmt *S, std::string syncExitBuf) { |
| 1909 | // Perform a bottom up traversal of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1910 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1911 | if (*CI) { |
| 1912 | RewriteSyncReturnStmts(*CI, syncExitBuf); |
| 1913 | } |
| 1914 | if (isa<ReturnStmt>(S)) { |
| 1915 | SourceLocation startLoc = S->getLocStart(); |
| 1916 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1917 | |
| 1918 | const char *semiBuf = strchr(startBuf, ';'); |
| 1919 | assert((*semiBuf == ';') && "RewriteSyncReturnStmts: can't find ';'"); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1920 | SourceLocation onePastSemiLoc = startLoc.getLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1921 | |
| 1922 | std::string buf; |
| 1923 | buf = "{ objc_exception_try_exit(&_stack);"; |
| 1924 | buf += syncExitBuf; |
| 1925 | buf += " return"; |
| 1926 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1927 | ReplaceText(startLoc, 6, buf); |
| 1928 | InsertText(onePastSemiLoc, "}"); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1929 | } |
| 1930 | return; |
| 1931 | } |
| 1932 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1933 | Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1934 | // Get the start location and compute the semi location. |
| 1935 | SourceLocation startLoc = S->getLocStart(); |
| 1936 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1937 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1938 | assert((*startBuf == '@') && "bogus @try location"); |
| 1939 | |
| 1940 | std::string buf; |
| 1941 | // declare a new scope with two variables, _stack and _rethrow. |
| 1942 | buf = "/* @try scope begin */ { struct _objc_exception_data {\n"; |
| 1943 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1944 | buf += "char *pointers[4];} _stack;\n"; |
| 1945 | buf += "id volatile _rethrow = 0;\n"; |
| 1946 | buf += "objc_exception_try_enter(&_stack);\n"; |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1947 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1948 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1949 | ReplaceText(startLoc, 4, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1950 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1951 | startLoc = S->getTryBody()->getLocEnd(); |
| 1952 | startBuf = SM->getCharacterData(startLoc); |
| 1953 | |
| 1954 | assert((*startBuf == '}') && "bogus @try block"); |
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 | SourceLocation lastCurlyLoc = startLoc; |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1957 | if (S->getNumCatchStmts()) { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1958 | startLoc = startLoc.getLocWithOffset(1); |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1959 | buf = " /* @catch begin */ else {\n"; |
| 1960 | buf += " id _caught = objc_exception_extract(&_stack);\n"; |
| 1961 | buf += " objc_exception_try_enter (&_stack);\n"; |
| 1962 | buf += " if (_setjmp(_stack.buf))\n"; |
| 1963 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1964 | buf += " else { /* @catch continue */"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1965 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1966 | InsertText(startLoc, buf); |
Steve Naroff | 8bd3dc6 | 2008-09-09 19:59:12 +0000 | [diff] [blame] | 1967 | } else { /* no catch list */ |
| 1968 | buf = "}\nelse {\n"; |
| 1969 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1970 | buf += "}"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1971 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1972 | } |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1973 | Stmt *lastCatchBody = 0; |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1974 | for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I) { |
| 1975 | ObjCAtCatchStmt *Catch = S->getCatchStmt(I); |
Douglas Gregor | c00d8e1 | 2010-04-26 16:46:50 +0000 | [diff] [blame] | 1976 | VarDecl *catchDecl = Catch->getCatchParamDecl(); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1977 | |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1978 | if (I == 0) |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1979 | buf = "if ("; // we are generating code for the first catch clause |
| 1980 | else |
| 1981 | buf = "else if ("; |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1982 | startLoc = Catch->getLocStart(); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1983 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1984 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1985 | assert((*startBuf == '@') && "bogus @catch location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1986 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1987 | const char *lParenLoc = strchr(startBuf, '('); |
| 1988 | |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1989 | if (Catch->hasEllipsis()) { |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1990 | // Now rewrite the body... |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1991 | lastCatchBody = Catch->getCatchBody(); |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1992 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1993 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1994 | assert(*SM->getCharacterData(Catch->getRParenLoc()) == ')' && |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1995 | "bogus @catch paren location"); |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1996 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1997 | |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1998 | buf += "1) { id _tmp = _caught;"; |
Daniel Dunbar | d7407dc | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 1999 | Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, buf); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2000 | } else if (catchDecl) { |
| 2001 | QualType t = catchDecl->getType(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2002 | if (t == Context->getObjCIdType()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2003 | buf += "1) { "; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2004 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf); |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 2005 | } else if (const ObjCObjectPointerType *Ptr = |
| 2006 | t->getAs<ObjCObjectPointerType>()) { |
| 2007 | // Should be a pointer to a class. |
| 2008 | ObjCInterfaceDecl *IDecl = Ptr->getObjectType()->getInterface(); |
| 2009 | if (IDecl) { |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 2010 | buf += "objc_exception_match((struct objc_class *)objc_getClass(\""; |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 2011 | buf += IDecl->getNameAsString(); |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 2012 | buf += "\"), (struct objc_object *)_caught)) { "; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2013 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2014 | } |
| 2015 | } |
| 2016 | // Now rewrite the body... |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 2017 | lastCatchBody = Catch->getCatchBody(); |
| 2018 | SourceLocation rParenLoc = Catch->getRParenLoc(); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2019 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 2020 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 2021 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 2022 | assert((*rParenBuf == ')') && "bogus @catch paren location"); |
| 2023 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2024 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2025 | // Here we replace ") {" with "= _caught;" (which initializes and |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2026 | // declares the @catch parameter). |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2027 | ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, " = _caught;"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2028 | } else { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2029 | llvm_unreachable("@catch rewrite bug"); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2030 | } |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2031 | } |
| 2032 | // Complete the catch list... |
| 2033 | if (lastCatchBody) { |
| 2034 | SourceLocation bodyLoc = lastCatchBody->getLocEnd(); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 2035 | assert(*SM->getCharacterData(bodyLoc) == '}' && |
| 2036 | "bogus @catch body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2037 | |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 2038 | // Insert the last (implicit) else clause *before* the right curly brace. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2039 | bodyLoc = bodyLoc.getLocWithOffset(-1); |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 2040 | buf = "} /* last catch end */\n"; |
| 2041 | buf += "else {\n"; |
| 2042 | buf += " _rethrow = _caught;\n"; |
| 2043 | buf += " objc_exception_try_exit(&_stack);\n"; |
| 2044 | buf += "} } /* @catch end */\n"; |
| 2045 | if (!S->getFinallyStmt()) |
| 2046 | buf += "}\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2047 | InsertText(bodyLoc, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2048 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2049 | // Set lastCurlyLoc |
| 2050 | lastCurlyLoc = lastCatchBody->getLocEnd(); |
| 2051 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2052 | if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2053 | startLoc = finalStmt->getLocStart(); |
| 2054 | startBuf = SM->getCharacterData(startLoc); |
| 2055 | assert((*startBuf == '@') && "bogus @finally start"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2056 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2057 | ReplaceText(startLoc, 8, "/* @finally */"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2058 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2059 | Stmt *body = finalStmt->getFinallyBody(); |
| 2060 | SourceLocation startLoc = body->getLocStart(); |
| 2061 | SourceLocation endLoc = body->getLocEnd(); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 2062 | assert(*SM->getCharacterData(startLoc) == '{' && |
| 2063 | "bogus @finally body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2064 | assert(*SM->getCharacterData(endLoc) == '}' && |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 2065 | "bogus @finally body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2066 | |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2067 | startLoc = startLoc.getLocWithOffset(1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2068 | InsertText(startLoc, " if (!_rethrow) objc_exception_try_exit(&_stack);\n"); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2069 | endLoc = endLoc.getLocWithOffset(-1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2070 | InsertText(endLoc, " if (_rethrow) objc_exception_throw(_rethrow);\n"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2071 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2072 | // Set lastCurlyLoc |
| 2073 | lastCurlyLoc = body->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2074 | |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 2075 | // Now check for any return/continue/go statements within the @try. |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 2076 | WarnAboutReturnGotoStmts(S->getTryBody()); |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 2077 | } else { /* no finally clause - make sure we synthesize an implicit one */ |
| 2078 | buf = "{ /* implicit finally clause */\n"; |
| 2079 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
| 2080 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 2081 | buf += "}"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2082 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 2083 | |
| 2084 | // Now check for any return/continue/go statements within the @try. |
| 2085 | // The implicit finally clause won't called if the @try contains any |
| 2086 | // jump statements. |
| 2087 | bool hasReturns = false; |
| 2088 | HasReturnStmts(S->getTryBody(), hasReturns); |
| 2089 | if (hasReturns) |
| 2090 | RewriteTryReturnStmts(S->getTryBody()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 2091 | } |
| 2092 | // Now emit the final closing curly brace... |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2093 | lastCurlyLoc = lastCurlyLoc.getLocWithOffset(1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2094 | InsertText(lastCurlyLoc, " } /* @try scope end */\n"); |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 2095 | return 0; |
| 2096 | } |
| 2097 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2098 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
| 2099 | // the throw expression is typically a message expression that's already |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2100 | // been rewritten! (which implies the SourceLocation's are invalid). |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2101 | Stmt *RewriteObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2102 | // Get the start location and compute the semi location. |
| 2103 | SourceLocation startLoc = S->getLocStart(); |
| 2104 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2105 | |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2106 | assert((*startBuf == '@') && "bogus @throw location"); |
| 2107 | |
| 2108 | std::string buf; |
| 2109 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
Steve Naroff | 20ebf8f | 2008-01-19 00:42:38 +0000 | [diff] [blame] | 2110 | if (S->getThrowExpr()) |
| 2111 | buf = "objc_exception_throw("; |
| 2112 | else // add an implicit argument |
| 2113 | buf = "objc_exception_throw(_caught"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2114 | |
Steve Naroff | 4ba0acb | 2008-07-25 15:41:30 +0000 | [diff] [blame] | 2115 | // handle "@ throw" correctly. |
| 2116 | const char *wBuf = strchr(startBuf, 'w'); |
| 2117 | assert((*wBuf == 'w') && "@throw: can't find 'w'"); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2118 | ReplaceText(startLoc, wBuf-startBuf+1, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2119 | |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2120 | const char *semiBuf = strchr(startBuf, ';'); |
| 2121 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2122 | SourceLocation semiLoc = startLoc.getLocWithOffset(semiBuf-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2123 | ReplaceText(semiLoc, 1, ");"); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2124 | return 0; |
| 2125 | } |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 2126 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2127 | Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 2128 | // Create a new string expression. |
| 2129 | QualType StrType = Context->getPointerType(Context->CharTy); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2130 | std::string StrEncoding; |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 2131 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2132 | Expr *Replacement = StringLiteral::Create(*Context, StrEncoding, |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2133 | StringLiteral::Ascii, false, |
| 2134 | StrType, SourceLocation()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2135 | ReplaceStmt(Exp, Replacement); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2136 | |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 2137 | // Replace this subexpr in the parent. |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2138 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 2139 | return Replacement; |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 2140 | } |
| 2141 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2142 | Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
Steve Naroff | 1a93764 | 2008-12-22 22:16:07 +0000 | [diff] [blame] | 2143 | if (!SelGetUidFunctionDecl) |
| 2144 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2145 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 2146 | // Create a call to sel_registerName("selName"). |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2147 | SmallVector<Expr*, 8> SelExprs; |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2148 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2149 | SelExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2150 | Exp->getSelector().getAsString(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2151 | StringLiteral::Ascii, false, |
| 2152 | argType, SourceLocation())); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2153 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2154 | &SelExprs[0], SelExprs.size()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2155 | ReplaceStmt(Exp, SelExp); |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2156 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2157 | return SelExp; |
| 2158 | } |
| 2159 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2160 | CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl( |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2161 | FunctionDecl *FD, Expr **args, unsigned nargs, SourceLocation StartLoc, |
| 2162 | SourceLocation EndLoc) { |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2163 | // 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] | 2164 | QualType msgSendType = FD->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2165 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2166 | // Create a reference to the objc_msgSend() declaration. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2167 | DeclRefExpr *DRE = |
| 2168 | new (Context) DeclRefExpr(FD, msgSendType, VK_LValue, SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2169 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2170 | // 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] | 2171 | QualType pToFunc = Context->getPointerType(msgSendType); |
Anders Carlsson | 88465d3 | 2010-04-23 22:18:37 +0000 | [diff] [blame] | 2172 | ImplicitCastExpr *ICE = |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2173 | ImplicitCastExpr::Create(*Context, pToFunc, CK_FunctionToPointerDecay, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2174 | DRE, 0, VK_RValue); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2175 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2176 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2177 | |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2178 | CallExpr *Exp = |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 2179 | new (Context) CallExpr(*Context, ICE, args, nargs, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2180 | FT->getCallResultType(*Context), |
| 2181 | VK_RValue, EndLoc); |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2182 | return Exp; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2183 | } |
| 2184 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2185 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 2186 | const char *&startRef, const char *&endRef) { |
| 2187 | while (startBuf < endBuf) { |
| 2188 | if (*startBuf == '<') |
| 2189 | startRef = startBuf; // mark the start. |
| 2190 | if (*startBuf == '>') { |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 2191 | if (startRef && *startRef == '<') { |
| 2192 | endRef = startBuf; // mark the end. |
| 2193 | return true; |
| 2194 | } |
| 2195 | return false; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2196 | } |
| 2197 | startBuf++; |
| 2198 | } |
| 2199 | return false; |
| 2200 | } |
| 2201 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2202 | static void scanToNextArgument(const char *&argRef) { |
| 2203 | int angle = 0; |
| 2204 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 2205 | if (*argRef == '<') |
| 2206 | angle++; |
| 2207 | else if (*argRef == '>') |
| 2208 | angle--; |
| 2209 | argRef++; |
| 2210 | } |
| 2211 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 2212 | } |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2213 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2214 | bool RewriteObjC::needToScanForQualifiers(QualType T) { |
Fariborz Jahanian | 32132a0 | 2010-02-03 21:29:28 +0000 | [diff] [blame] | 2215 | if (T->isObjCQualifiedIdType()) |
| 2216 | return true; |
Fariborz Jahanian | 84aa946 | 2010-02-02 18:35:07 +0000 | [diff] [blame] | 2217 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 2218 | if (PT->getPointeeType()->isObjCQualifiedIdType()) |
| 2219 | return true; |
| 2220 | } |
| 2221 | if (T->isObjCObjectPointerType()) { |
| 2222 | T = T->getPointeeType(); |
| 2223 | return T->isObjCQualifiedInterfaceType(); |
| 2224 | } |
Fariborz Jahanian | 24f9cab | 2010-09-30 20:41:32 +0000 | [diff] [blame] | 2225 | if (T->isArrayType()) { |
| 2226 | QualType ElemTy = Context->getBaseElementType(T); |
| 2227 | return needToScanForQualifiers(ElemTy); |
| 2228 | } |
Fariborz Jahanian | 84aa946 | 2010-02-02 18:35:07 +0000 | [diff] [blame] | 2229 | return false; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2230 | } |
| 2231 | |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2232 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) { |
| 2233 | QualType Type = E->getType(); |
| 2234 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2235 | SourceLocation Loc, EndLoc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2236 | |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2237 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) { |
| 2238 | Loc = ECE->getLParenLoc(); |
| 2239 | EndLoc = ECE->getRParenLoc(); |
| 2240 | } else { |
| 2241 | Loc = E->getLocStart(); |
| 2242 | EndLoc = E->getLocEnd(); |
| 2243 | } |
| 2244 | // This will defend against trying to rewrite synthesized expressions. |
| 2245 | if (Loc.isInvalid() || EndLoc.isInvalid()) |
| 2246 | return; |
| 2247 | |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2248 | const char *startBuf = SM->getCharacterData(Loc); |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2249 | const char *endBuf = SM->getCharacterData(EndLoc); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2250 | const char *startRef = 0, *endRef = 0; |
| 2251 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2252 | // Get the locations of the startRef, endRef. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2253 | SourceLocation LessLoc = Loc.getLocWithOffset(startRef-startBuf); |
| 2254 | SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-startBuf+1); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2255 | // Comment out the protocol references. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2256 | InsertText(LessLoc, "/*"); |
| 2257 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2258 | } |
| 2259 | } |
| 2260 | } |
| 2261 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2262 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2263 | SourceLocation Loc; |
| 2264 | QualType Type; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2265 | const FunctionProtoType *proto = 0; |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2266 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 2267 | Loc = VD->getLocation(); |
| 2268 | Type = VD->getType(); |
| 2269 | } |
| 2270 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 2271 | Loc = FD->getLocation(); |
| 2272 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 2273 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2274 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2275 | assert(funcType && "missing function type"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2276 | proto = dyn_cast<FunctionProtoType>(funcType); |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2277 | if (!proto) |
| 2278 | return; |
| 2279 | Type = proto->getResultType(); |
| 2280 | } |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 2281 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(Dcl)) { |
| 2282 | Loc = FD->getLocation(); |
| 2283 | Type = FD->getType(); |
| 2284 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2285 | else |
| 2286 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2287 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2288 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2289 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2290 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2291 | const char *endBuf = SM->getCharacterData(Loc); |
| 2292 | const char *startBuf = endBuf; |
Steve Naroff | 6cafbf2 | 2008-05-31 05:02:17 +0000 | [diff] [blame] | 2293 | while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2294 | startBuf--; // scan backward (from the decl location) for return type. |
| 2295 | const char *startRef = 0, *endRef = 0; |
| 2296 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2297 | // Get the locations of the startRef, endRef. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2298 | SourceLocation LessLoc = Loc.getLocWithOffset(startRef-endBuf); |
| 2299 | SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-endBuf+1); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2300 | // Comment out the protocol references. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2301 | InsertText(LessLoc, "/*"); |
| 2302 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2303 | } |
| 2304 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2305 | if (!proto) |
| 2306 | return; // most likely, was a variable |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2307 | // Now check arguments. |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2308 | const char *startBuf = SM->getCharacterData(Loc); |
| 2309 | const char *startFuncBuf = startBuf; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2310 | for (unsigned i = 0; i < proto->getNumArgs(); i++) { |
| 2311 | if (needToScanForQualifiers(proto->getArgType(i))) { |
| 2312 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2313 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2314 | const char *endBuf = startBuf; |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2315 | // scan forward (from the decl location) for argument types. |
| 2316 | scanToNextArgument(endBuf); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2317 | const char *startRef = 0, *endRef = 0; |
| 2318 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2319 | // Get the locations of the startRef, endRef. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2320 | SourceLocation LessLoc = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2321 | Loc.getLocWithOffset(startRef-startFuncBuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2322 | SourceLocation GreaterLoc = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2323 | Loc.getLocWithOffset(endRef-startFuncBuf+1); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2324 | // Comment out the protocol references. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2325 | InsertText(LessLoc, "/*"); |
| 2326 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2327 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2328 | startBuf = ++endBuf; |
| 2329 | } |
| 2330 | else { |
Steve Naroff | aba49d1 | 2008-08-06 15:58:23 +0000 | [diff] [blame] | 2331 | // If the function name is derived from a macro expansion, then the |
| 2332 | // argument buffer will not follow the name. Need to speak with Chris. |
| 2333 | while (*startBuf && *startBuf != ')' && *startBuf != ',') |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2334 | startBuf++; // scan forward (from the decl location) for argument types. |
| 2335 | startBuf++; |
| 2336 | } |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2337 | } |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2338 | } |
| 2339 | |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2340 | void RewriteObjC::RewriteTypeOfDecl(VarDecl *ND) { |
| 2341 | QualType QT = ND->getType(); |
| 2342 | const Type* TypePtr = QT->getAs<Type>(); |
| 2343 | if (!isa<TypeOfExprType>(TypePtr)) |
| 2344 | return; |
| 2345 | while (isa<TypeOfExprType>(TypePtr)) { |
| 2346 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 2347 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 2348 | TypePtr = QT->getAs<Type>(); |
| 2349 | } |
| 2350 | // FIXME. This will not work for multiple declarators; as in: |
| 2351 | // __typeof__(a) b,c,d; |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2352 | std::string TypeAsString(QT.getAsString(Context->getPrintingPolicy())); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2353 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
| 2354 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 2355 | if (ND->getInit()) { |
| 2356 | std::string Name(ND->getNameAsString()); |
| 2357 | TypeAsString += " " + Name + " = "; |
| 2358 | Expr *E = ND->getInit(); |
| 2359 | SourceLocation startLoc; |
| 2360 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 2361 | startLoc = ECE->getLParenLoc(); |
| 2362 | else |
| 2363 | startLoc = E->getLocStart(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 2364 | startLoc = SM->getExpansionLoc(startLoc); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2365 | const char *endBuf = SM->getCharacterData(startLoc); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2366 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2367 | } |
| 2368 | else { |
| 2369 | SourceLocation X = ND->getLocEnd(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 2370 | X = SM->getExpansionLoc(X); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2371 | const char *endBuf = SM->getCharacterData(X); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2372 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2373 | } |
| 2374 | } |
| 2375 | |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2376 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2377 | void RewriteObjC::SynthSelGetUidFunctionDecl() { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2378 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2379 | SmallVector<QualType, 16> ArgTys; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2380 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2381 | QualType getFuncType = |
| 2382 | getSimpleFunctionType(Context->getObjCSelType(), &ArgTys[0], ArgTys.size()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2383 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2384 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2385 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2386 | SelGetUidIdent, getFuncType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2387 | SC_Extern, |
| 2388 | SC_None, false); |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2389 | } |
| 2390 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2391 | void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2392 | // declared in <objc/objc.h> |
Douglas Gregor | 51efe56 | 2009-01-09 01:47:02 +0000 | [diff] [blame] | 2393 | if (FD->getIdentifier() && |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 2394 | FD->getName() == "sel_registerName") { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2395 | SelGetUidFunctionDecl = FD; |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2396 | return; |
| 2397 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2398 | RewriteObjCQualifiedInterfaceTypes(FD); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2399 | } |
| 2400 | |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2401 | void RewriteObjC::RewriteBlockPointerType(std::string& Str, QualType Type) { |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2402 | std::string TypeString(Type.getAsString(Context->getPrintingPolicy())); |
Fariborz Jahanian | 52b2e1e | 2010-02-12 17:52:31 +0000 | [diff] [blame] | 2403 | const char *argPtr = TypeString.c_str(); |
| 2404 | if (!strchr(argPtr, '^')) { |
| 2405 | Str += TypeString; |
| 2406 | return; |
| 2407 | } |
| 2408 | while (*argPtr) { |
| 2409 | Str += (*argPtr == '^' ? '*' : *argPtr); |
| 2410 | argPtr++; |
| 2411 | } |
| 2412 | } |
| 2413 | |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2414 | // FIXME. Consolidate this routine with RewriteBlockPointerType. |
Daniel Dunbar | fa297fb | 2010-06-30 19:16:53 +0000 | [diff] [blame] | 2415 | void RewriteObjC::RewriteBlockPointerTypeVariable(std::string& Str, |
| 2416 | ValueDecl *VD) { |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2417 | QualType Type = VD->getType(); |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2418 | std::string TypeString(Type.getAsString(Context->getPrintingPolicy())); |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2419 | const char *argPtr = TypeString.c_str(); |
| 2420 | int paren = 0; |
| 2421 | while (*argPtr) { |
| 2422 | switch (*argPtr) { |
| 2423 | case '(': |
| 2424 | Str += *argPtr; |
| 2425 | paren++; |
| 2426 | break; |
| 2427 | case ')': |
| 2428 | Str += *argPtr; |
| 2429 | paren--; |
| 2430 | break; |
| 2431 | case '^': |
| 2432 | Str += '*'; |
| 2433 | if (paren == 1) |
| 2434 | Str += VD->getNameAsString(); |
| 2435 | break; |
| 2436 | default: |
| 2437 | Str += *argPtr; |
| 2438 | break; |
| 2439 | } |
| 2440 | argPtr++; |
| 2441 | } |
| 2442 | } |
| 2443 | |
| 2444 | |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2445 | void RewriteObjC::RewriteBlockLiteralFunctionDecl(FunctionDecl *FD) { |
| 2446 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
| 2447 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
| 2448 | const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(funcType); |
| 2449 | if (!proto) |
| 2450 | return; |
| 2451 | QualType Type = proto->getResultType(); |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2452 | std::string FdStr = Type.getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2453 | FdStr += " "; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 2454 | FdStr += FD->getName(); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2455 | FdStr += "("; |
| 2456 | unsigned numArgs = proto->getNumArgs(); |
| 2457 | for (unsigned i = 0; i < numArgs; i++) { |
| 2458 | QualType ArgType = proto->getArgType(i); |
Fariborz Jahanian | 52b2e1e | 2010-02-12 17:52:31 +0000 | [diff] [blame] | 2459 | RewriteBlockPointerType(FdStr, ArgType); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2460 | if (i+1 < numArgs) |
| 2461 | FdStr += ", "; |
| 2462 | } |
| 2463 | FdStr += ");\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2464 | InsertText(FunLocStart, FdStr); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2465 | CurFunctionDeclToDeclareForBlock = 0; |
| 2466 | } |
| 2467 | |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2468 | // SynthSuperContructorFunctionDecl - id objc_super(id obj, id super); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2469 | void RewriteObjC::SynthSuperContructorFunctionDecl() { |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2470 | if (SuperContructorFunctionDecl) |
| 2471 | return; |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2472 | IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2473 | SmallVector<QualType, 16> ArgTys; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2474 | QualType argT = Context->getObjCIdType(); |
| 2475 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2476 | ArgTys.push_back(argT); |
| 2477 | ArgTys.push_back(argT); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2478 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2479 | &ArgTys[0], ArgTys.size()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2480 | SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2481 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2482 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2483 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2484 | SC_Extern, |
| 2485 | SC_None, false); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2486 | } |
| 2487 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2488 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2489 | void RewriteObjC::SynthMsgSendFunctionDecl() { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2490 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2491 | SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2492 | QualType argT = Context->getObjCIdType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2493 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2494 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2495 | argT = Context->getObjCSelType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2496 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2497 | ArgTys.push_back(argT); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2498 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2499 | &ArgTys[0], ArgTys.size(), |
| 2500 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2501 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2502 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2503 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2504 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2505 | SC_Extern, |
| 2506 | SC_None, false); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2507 | } |
| 2508 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2509 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2510 | void RewriteObjC::SynthMsgSendSuperFunctionDecl() { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2511 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2512 | SmallVector<QualType, 16> ArgTys; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2513 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2514 | SourceLocation(), SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2515 | &Context->Idents.get("objc_super")); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2516 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2517 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2518 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2519 | argT = Context->getObjCSelType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2520 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2521 | ArgTys.push_back(argT); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2522 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2523 | &ArgTys[0], ArgTys.size(), |
| 2524 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2525 | MsgSendSuperFunctionDecl = 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 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2529 | SC_Extern, |
| 2530 | SC_None, false); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2531 | } |
| 2532 | |
Fariborz Jahanian | 336c8f7 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2533 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2534 | void RewriteObjC::SynthMsgSendStretFunctionDecl() { |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2535 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2536 | SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2537 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2538 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2539 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2540 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2541 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2542 | ArgTys.push_back(argT); |
Fariborz Jahanian | 336c8f7 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2543 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2544 | &ArgTys[0], ArgTys.size(), |
| 2545 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2546 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2547 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2548 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2549 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2550 | SC_Extern, |
| 2551 | SC_None, false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2552 | } |
| 2553 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2554 | // SynthMsgSendSuperStretFunctionDecl - |
Fariborz Jahanian | 336c8f7 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2555 | // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2556 | void RewriteObjC::SynthMsgSendSuperStretFunctionDecl() { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2557 | IdentifierInfo *msgSendIdent = |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2558 | &Context->Idents.get("objc_msgSendSuper_stret"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2559 | SmallVector<QualType, 16> ArgTys; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2560 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2561 | SourceLocation(), SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2562 | &Context->Idents.get("objc_super")); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2563 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2564 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2565 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2566 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2567 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2568 | ArgTys.push_back(argT); |
Fariborz Jahanian | 336c8f7 | 2011-10-11 23:02:37 +0000 | [diff] [blame] | 2569 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2570 | &ArgTys[0], ArgTys.size(), |
| 2571 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2572 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2573 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2574 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2575 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2576 | SC_Extern, |
| 2577 | SC_None, false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2578 | } |
| 2579 | |
Steve Naroff | 1284db8 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2580 | // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2581 | void RewriteObjC::SynthMsgSendFpretFunctionDecl() { |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2582 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2583 | SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2584 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2585 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2586 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2587 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2588 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2589 | ArgTys.push_back(argT); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2590 | QualType msgSendType = getSimpleFunctionType(Context->DoubleTy, |
| 2591 | &ArgTys[0], ArgTys.size(), |
| 2592 | true /*isVariadic*/); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2593 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2594 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2595 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2596 | msgSendIdent, msgSendType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2597 | SC_Extern, |
| 2598 | SC_None, false); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2599 | } |
| 2600 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2601 | // SynthGetClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2602 | void RewriteObjC::SynthGetClassFunctionDecl() { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2603 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2604 | SmallVector<QualType, 16> ArgTys; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2605 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2606 | QualType getClassType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2607 | &ArgTys[0], ArgTys.size()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2608 | GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2609 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2610 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2611 | getClassIdent, getClassType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2612 | SC_Extern, |
| 2613 | SC_None, false); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2614 | } |
| 2615 | |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2616 | // SynthGetSuperClassFunctionDecl - Class class_getSuperclass(Class cls); |
| 2617 | void RewriteObjC::SynthGetSuperClassFunctionDecl() { |
| 2618 | IdentifierInfo *getSuperClassIdent = |
| 2619 | &Context->Idents.get("class_getSuperclass"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2620 | SmallVector<QualType, 16> ArgTys; |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2621 | ArgTys.push_back(Context->getObjCClassType()); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2622 | QualType getClassType = getSimpleFunctionType(Context->getObjCClassType(), |
| 2623 | &ArgTys[0], ArgTys.size()); |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2624 | GetSuperClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2625 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2626 | SourceLocation(), |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2627 | getSuperClassIdent, |
| 2628 | getClassType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2629 | SC_Extern, |
| 2630 | SC_None, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2631 | false); |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2632 | } |
| 2633 | |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2634 | // SynthGetMetaClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2635 | void RewriteObjC::SynthGetMetaClassFunctionDecl() { |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2636 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2637 | SmallVector<QualType, 16> ArgTys; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2638 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2639 | QualType getClassType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2640 | &ArgTys[0], ArgTys.size()); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2641 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2642 | SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2643 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2644 | getClassIdent, getClassType, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2645 | SC_Extern, |
| 2646 | SC_None, false); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2647 | } |
| 2648 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2649 | Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2650 | QualType strType = getConstantStringStructType(); |
| 2651 | |
| 2652 | std::string S = "__NSConstantStringImpl_"; |
Steve Naroff | 7691d9b | 2008-05-31 03:35:42 +0000 | [diff] [blame] | 2653 | |
| 2654 | std::string tmpName = InFileName; |
| 2655 | unsigned i; |
| 2656 | for (i=0; i < tmpName.length(); i++) { |
| 2657 | char c = tmpName.at(i); |
| 2658 | // replace any non alphanumeric characters with '_'. |
| 2659 | if (!isalpha(c) && (c < '0' || c > '9')) |
| 2660 | tmpName[i] = '_'; |
| 2661 | } |
| 2662 | S += tmpName; |
| 2663 | S += "_"; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2664 | S += utostr(NumObjCStringLiterals++); |
| 2665 | |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2666 | Preamble += "static __NSConstantStringImpl " + S; |
| 2667 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 2668 | Preamble += "0x000007c8,"; // utf8_str |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2669 | // The pretty printer for StringLiteral handles escape characters properly. |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2670 | std::string prettyBufS; |
| 2671 | llvm::raw_string_ostream prettyBuf(prettyBufS); |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2672 | Exp->getString()->printPretty(prettyBuf, *Context, 0, |
| 2673 | PrintingPolicy(LangOpts)); |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2674 | Preamble += prettyBuf.str(); |
| 2675 | Preamble += ","; |
Steve Naroff | fd5b76f | 2009-12-06 01:48:44 +0000 | [diff] [blame] | 2676 | Preamble += utostr(Exp->getString()->getByteLength()) + "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2677 | |
| 2678 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2679 | SourceLocation(), &Context->Idents.get(S), |
| 2680 | strType, 0, SC_Static, SC_None); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2681 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, strType, VK_LValue, |
| 2682 | SourceLocation()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2683 | Expr *Unop = new (Context) UnaryOperator(DRE, UO_AddrOf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2684 | Context->getPointerType(DRE->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2685 | VK_RValue, OK_Ordinary, |
| 2686 | SourceLocation()); |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2687 | // cast to NSConstantString * |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2688 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Exp->getType(), |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2689 | CK_CPointerToObjCPointerCast, Unop); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2690 | ReplaceStmt(Exp, cast); |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 2691 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2692 | return cast; |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2693 | } |
| 2694 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2695 | // struct objc_super { struct objc_object *receiver; struct objc_class *super; }; |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2696 | QualType RewriteObjC::getSuperStructType() { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2697 | if (!SuperStructDecl) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2698 | SuperStructDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2699 | SourceLocation(), SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2700 | &Context->Idents.get("objc_super")); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2701 | QualType FieldTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2702 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2703 | // struct objc_object *receiver; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2704 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2705 | // struct objc_class *super; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2706 | FieldTypes[1] = Context->getObjCClassType(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2707 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2708 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2709 | for (unsigned i = 0; i < 2; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2710 | SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2711 | SourceLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2712 | SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2713 | FieldTypes[i], 0, |
| 2714 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2715 | /*Mutable=*/false, |
| 2716 | /*HasInit=*/false)); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2717 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2718 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2719 | SuperStructDecl->completeDefinition(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2720 | } |
| 2721 | return Context->getTagDeclType(SuperStructDecl); |
| 2722 | } |
| 2723 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2724 | QualType RewriteObjC::getConstantStringStructType() { |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2725 | if (!ConstantStringDecl) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2726 | ConstantStringDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2727 | SourceLocation(), SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2728 | &Context->Idents.get("__NSConstantStringImpl")); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2729 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2730 | |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2731 | // struct objc_object *receiver; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2732 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2733 | // int flags; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2734 | FieldTypes[1] = Context->IntTy; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2735 | // char *str; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2736 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2737 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2738 | FieldTypes[3] = Context->LongTy; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2739 | |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2740 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2741 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2742 | ConstantStringDecl->addDecl(FieldDecl::Create(*Context, |
| 2743 | ConstantStringDecl, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2744 | SourceLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2745 | SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2746 | FieldTypes[i], 0, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2747 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2748 | /*Mutable=*/true, |
| 2749 | /*HasInit=*/false)); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2750 | } |
| 2751 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2752 | ConstantStringDecl->completeDefinition(); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2753 | } |
| 2754 | return Context->getTagDeclType(ConstantStringDecl); |
| 2755 | } |
| 2756 | |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2757 | Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp, |
| 2758 | SourceLocation StartLoc, |
| 2759 | SourceLocation EndLoc) { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2760 | if (!SelGetUidFunctionDecl) |
| 2761 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2762 | if (!MsgSendFunctionDecl) |
| 2763 | SynthMsgSendFunctionDecl(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2764 | if (!MsgSendSuperFunctionDecl) |
| 2765 | SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2766 | if (!MsgSendStretFunctionDecl) |
| 2767 | SynthMsgSendStretFunctionDecl(); |
| 2768 | if (!MsgSendSuperStretFunctionDecl) |
| 2769 | SynthMsgSendSuperStretFunctionDecl(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2770 | if (!MsgSendFpretFunctionDecl) |
| 2771 | SynthMsgSendFpretFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2772 | if (!GetClassFunctionDecl) |
| 2773 | SynthGetClassFunctionDecl(); |
Fariborz Jahanian | d314e9e | 2010-03-10 21:17:41 +0000 | [diff] [blame] | 2774 | if (!GetSuperClassFunctionDecl) |
| 2775 | SynthGetSuperClassFunctionDecl(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2776 | if (!GetMetaClassFunctionDecl) |
| 2777 | SynthGetMetaClassFunctionDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2778 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2779 | // default to objc_msgSend(). |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2780 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2781 | // May need to use objc_msgSend_stret() as well. |
| 2782 | FunctionDecl *MsgSendStretFlavor = 0; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2783 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
| 2784 | QualType resultType = mDecl->getResultType(); |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 2785 | if (resultType->isRecordType()) |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2786 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
Chris Lattner | 8b51fd7 | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2787 | else if (resultType->isRealFloatingType()) |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2788 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2789 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2790 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2791 | // Synthesize a call to objc_msgSend(). |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2792 | SmallVector<Expr*, 8> MsgExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2793 | switch (Exp->getReceiverKind()) { |
| 2794 | case ObjCMessageExpr::SuperClass: { |
| 2795 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2796 | if (MsgSendStretFlavor) |
| 2797 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2798 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2799 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2800 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2801 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2802 | SmallVector<Expr*, 4> InitExprs; |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2803 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2804 | // set the receiver to self, the first argument to all methods. |
| 2805 | InitExprs.push_back( |
| 2806 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2807 | CK_BitCast, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2808 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2809 | Context->getObjCIdType(), |
| 2810 | VK_RValue, |
| 2811 | SourceLocation())) |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2812 | ); // set the 'receiver'. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2813 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2814 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2815 | SmallVector<Expr*, 8> ClsExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2816 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2817 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2818 | ClassDecl->getIdentifier()->getName(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2819 | StringLiteral::Ascii, false, |
| 2820 | argType, SourceLocation())); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2821 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
| 2822 | &ClsExprs[0], |
| 2823 | ClsExprs.size(), |
| 2824 | StartLoc, |
| 2825 | EndLoc); |
| 2826 | // (Class)objc_getClass("CurrentClass") |
| 2827 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 2828 | Context->getObjCClassType(), |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2829 | CK_CPointerToObjCPointerCast, Cls); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2830 | ClsExprs.clear(); |
| 2831 | ClsExprs.push_back(ArgExpr); |
| 2832 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 2833 | &ClsExprs[0], ClsExprs.size(), |
| 2834 | StartLoc, EndLoc); |
| 2835 | |
| 2836 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2837 | // To turn off a warning, type-cast to 'id' |
| 2838 | InitExprs.push_back( // set 'super class', using class_getSuperclass(). |
| 2839 | NoTypeInfoCStyleCastExpr(Context, |
| 2840 | Context->getObjCIdType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2841 | CK_BitCast, Cls)); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2842 | // struct objc_super |
| 2843 | QualType superType = getSuperStructType(); |
| 2844 | Expr *SuperRep; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2845 | |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 2846 | if (LangOpts.MicrosoftExt) { |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2847 | SynthSuperContructorFunctionDecl(); |
| 2848 | // Simulate a contructor call... |
| 2849 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2850 | superType, VK_LValue, |
| 2851 | SourceLocation()); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2852 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
| 2853 | InitExprs.size(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2854 | superType, VK_LValue, |
| 2855 | SourceLocation()); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2856 | // The code for super is a little tricky to prevent collision with |
| 2857 | // the structure definition in the header. The rewriter has it's own |
| 2858 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2859 | // we need the cast below. For example: |
| 2860 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2861 | // |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2862 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2863 | Context->getPointerType(SuperRep->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2864 | VK_RValue, OK_Ordinary, |
| 2865 | SourceLocation()); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2866 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2867 | Context->getPointerType(superType), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2868 | CK_BitCast, SuperRep); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2869 | } else { |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2870 | // (struct objc_super) { <exprs from above> } |
| 2871 | InitListExpr *ILE = |
| 2872 | new (Context) InitListExpr(*Context, SourceLocation(), |
| 2873 | &InitExprs[0], InitExprs.size(), |
| 2874 | SourceLocation()); |
| 2875 | TypeSourceInfo *superTInfo |
| 2876 | = Context->getTrivialTypeSourceInfo(superType); |
| 2877 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2878 | superType, VK_LValue, |
| 2879 | ILE, false); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2880 | // struct objc_super * |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2881 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2882 | Context->getPointerType(SuperRep->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2883 | VK_RValue, OK_Ordinary, |
| 2884 | SourceLocation()); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2885 | } |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2886 | MsgExprs.push_back(SuperRep); |
| 2887 | break; |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2888 | } |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2889 | |
| 2890 | case ObjCMessageExpr::Class: { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2891 | SmallVector<Expr*, 8> ClsExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2892 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2893 | ObjCInterfaceDecl *Class |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 2894 | = Exp->getClassReceiver()->getAs<ObjCObjectType>()->getInterface(); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2895 | IdentifierInfo *clsName = Class->getIdentifier(); |
| 2896 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2897 | clsName->getName(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2898 | StringLiteral::Ascii, false, |
Anders Carlsson | 3e2193c | 2011-04-14 00:40:03 +0000 | [diff] [blame] | 2899 | argType, SourceLocation())); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2900 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2901 | &ClsExprs[0], |
| 2902 | ClsExprs.size(), |
| 2903 | StartLoc, EndLoc); |
| 2904 | MsgExprs.push_back(Cls); |
| 2905 | break; |
| 2906 | } |
| 2907 | |
| 2908 | case ObjCMessageExpr::SuperInstance:{ |
| 2909 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2910 | if (MsgSendStretFlavor) |
| 2911 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2912 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 2913 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2914 | SmallVector<Expr*, 4> InitExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2915 | |
| 2916 | InitExprs.push_back( |
| 2917 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2918 | CK_BitCast, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2919 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2920 | Context->getObjCIdType(), |
| 2921 | VK_RValue, SourceLocation())) |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2922 | ); // set the 'receiver'. |
| 2923 | |
| 2924 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2925 | SmallVector<Expr*, 8> ClsExprs; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2926 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2927 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 2928 | ClassDecl->getIdentifier()->getName(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2929 | StringLiteral::Ascii, false, argType, |
| 2930 | SourceLocation())); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2931 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2932 | &ClsExprs[0], |
| 2933 | ClsExprs.size(), |
| 2934 | StartLoc, EndLoc); |
| 2935 | // (Class)objc_getClass("CurrentClass") |
| 2936 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 2937 | Context->getObjCClassType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2938 | CK_BitCast, Cls); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2939 | ClsExprs.clear(); |
| 2940 | ClsExprs.push_back(ArgExpr); |
| 2941 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 2942 | &ClsExprs[0], ClsExprs.size(), |
| 2943 | StartLoc, EndLoc); |
| 2944 | |
| 2945 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2946 | // To turn off a warning, type-cast to 'id' |
| 2947 | InitExprs.push_back( |
| 2948 | // set 'super class', using class_getSuperclass(). |
| 2949 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2950 | CK_BitCast, Cls)); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2951 | // struct objc_super |
| 2952 | QualType superType = getSuperStructType(); |
| 2953 | Expr *SuperRep; |
| 2954 | |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 2955 | if (LangOpts.MicrosoftExt) { |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2956 | SynthSuperContructorFunctionDecl(); |
| 2957 | // Simulate a contructor call... |
| 2958 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2959 | superType, VK_LValue, |
| 2960 | SourceLocation()); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2961 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
| 2962 | InitExprs.size(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2963 | superType, VK_LValue, SourceLocation()); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2964 | // The code for super is a little tricky to prevent collision with |
| 2965 | // the structure definition in the header. The rewriter has it's own |
| 2966 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2967 | // we need the cast below. For example: |
| 2968 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2969 | // |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2970 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2971 | Context->getPointerType(SuperRep->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2972 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2973 | SourceLocation()); |
| 2974 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2975 | Context->getPointerType(superType), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 2976 | CK_BitCast, SuperRep); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2977 | } else { |
| 2978 | // (struct objc_super) { <exprs from above> } |
| 2979 | InitListExpr *ILE = |
| 2980 | new (Context) InitListExpr(*Context, SourceLocation(), |
| 2981 | &InitExprs[0], InitExprs.size(), |
| 2982 | SourceLocation()); |
| 2983 | TypeSourceInfo *superTInfo |
| 2984 | = Context->getTrivialTypeSourceInfo(superType); |
| 2985 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2986 | superType, VK_RValue, ILE, |
| 2987 | false); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2988 | } |
| 2989 | MsgExprs.push_back(SuperRep); |
| 2990 | break; |
| 2991 | } |
| 2992 | |
| 2993 | case ObjCMessageExpr::Instance: { |
| 2994 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 2995 | // Foo<Proto> *. |
| 2996 | Expr *recExpr = Exp->getInstanceReceiver(); |
| 2997 | while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr)) |
| 2998 | recExpr = CE->getSubExpr(); |
Fariborz Jahanian | baac1ea | 2011-10-07 17:17:45 +0000 | [diff] [blame] | 2999 | CastKind CK = recExpr->getType()->isObjCObjectPointerType() |
| 3000 | ? CK_BitCast : recExpr->getType()->isBlockPointerType() |
| 3001 | ? CK_BlockPointerToObjCPointerCast |
| 3002 | : CK_CPointerToObjCPointerCast; |
| 3003 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 3004 | recExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
Fariborz Jahanian | baac1ea | 2011-10-07 17:17:45 +0000 | [diff] [blame] | 3005 | CK, recExpr); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 3006 | MsgExprs.push_back(recExpr); |
| 3007 | break; |
| 3008 | } |
| 3009 | } |
| 3010 | |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 3011 | // 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] | 3012 | SmallVector<Expr*, 8> SelExprs; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 3013 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3014 | SelExprs.push_back(StringLiteral::Create(*Context, |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 3015 | Exp->getSelector().getAsString(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 3016 | StringLiteral::Ascii, false, |
| 3017 | argType, SourceLocation())); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 3018 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 3019 | &SelExprs[0], SelExprs.size(), |
| 3020 | StartLoc, |
| 3021 | EndLoc); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 3022 | MsgExprs.push_back(SelExp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3023 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 3024 | // Now push any user supplied arguments. |
| 3025 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 3026 | Expr *userExpr = Exp->getArg(i); |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 3027 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 3028 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 3029 | // Reuse the ICE type, it is exactly what the doctor ordered. |
Fariborz Jahanian | dff3f01 | 2011-02-26 01:31:36 +0000 | [diff] [blame] | 3030 | QualType type = ICE->getType(); |
| 3031 | if (needToScanForQualifiers(type)) |
| 3032 | type = Context->getObjCIdType(); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 3033 | // Make sure we convert "type (^)(...)" to "type (*)(...)". |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3034 | (void)convertBlockPointerToFunctionPointer(type); |
Fariborz Jahanian | 1a38b46 | 2011-08-04 23:58:03 +0000 | [diff] [blame] | 3035 | const Expr *SubExpr = ICE->IgnoreParenImpCasts(); |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3036 | CastKind CK; |
| 3037 | if (SubExpr->getType()->isIntegralType(*Context) && |
| 3038 | type->isBooleanType()) { |
| 3039 | CK = CK_IntegralToBoolean; |
| 3040 | } else if (type->isObjCObjectPointerType()) { |
| 3041 | if (SubExpr->getType()->isBlockPointerType()) { |
| 3042 | CK = CK_BlockPointerToObjCPointerCast; |
| 3043 | } else if (SubExpr->getType()->isPointerType()) { |
| 3044 | CK = CK_CPointerToObjCPointerCast; |
| 3045 | } else { |
| 3046 | CK = CK_BitCast; |
| 3047 | } |
| 3048 | } else { |
| 3049 | CK = CK_BitCast; |
| 3050 | } |
| 3051 | |
| 3052 | userExpr = NoTypeInfoCStyleCastExpr(Context, type, CK, userExpr); |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 3053 | } |
| 3054 | // Make id<P...> cast into an 'id' cast. |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 3055 | else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3056 | if (CE->getType()->isObjCQualifiedIdType()) { |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 3057 | while ((CE = dyn_cast<CStyleCastExpr>(userExpr))) |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 3058 | userExpr = CE->getSubExpr(); |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3059 | CastKind CK; |
| 3060 | if (userExpr->getType()->isIntegralType(*Context)) { |
| 3061 | CK = CK_IntegralToPointer; |
| 3062 | } else if (userExpr->getType()->isBlockPointerType()) { |
| 3063 | CK = CK_BlockPointerToObjCPointerCast; |
| 3064 | } else if (userExpr->getType()->isPointerType()) { |
| 3065 | CK = CK_CPointerToObjCPointerCast; |
| 3066 | } else { |
| 3067 | CK = CK_BitCast; |
| 3068 | } |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3069 | userExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3070 | CK, userExpr); |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 3071 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3072 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 3073 | MsgExprs.push_back(userExpr); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3074 | // We've transferred the ownership to MsgExprs. For now, we *don't* null |
| 3075 | // out the argument in the original expression (since we aren't deleting |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3076 | // the ObjCMessageExpr). See RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3077 | //Exp->setArg(i, 0); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 3078 | } |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3079 | // Generate the funky cast. |
| 3080 | CastExpr *cast; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3081 | SmallVector<QualType, 8> ArgTypes; |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3082 | QualType returnType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3083 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3084 | // Push 'id' and 'SEL', the 2 implicit arguments. |
Steve Naroff | c3a438c | 2007-11-15 10:43:57 +0000 | [diff] [blame] | 3085 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 3086 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 3087 | else |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3088 | ArgTypes.push_back(Context->getObjCIdType()); |
| 3089 | ArgTypes.push_back(Context->getObjCSelType()); |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3090 | if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) { |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3091 | // Push any user argument types. |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3092 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 3093 | E = OMD->param_end(); PI != E; ++PI) { |
| 3094 | QualType t = (*PI)->getType()->isObjCQualifiedIdType() |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3095 | ? Context->getObjCIdType() |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3096 | : (*PI)->getType(); |
Steve Naroff | a206b06 | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 3097 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3098 | (void)convertBlockPointerToFunctionPointer(t); |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 3099 | ArgTypes.push_back(t); |
| 3100 | } |
Fariborz Jahanian | 3a448fb | 2011-09-10 17:01:56 +0000 | [diff] [blame] | 3101 | returnType = Exp->getType(); |
| 3102 | convertToUnqualifiedObjCType(returnType); |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 3103 | (void)convertBlockPointerToFunctionPointer(returnType); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3104 | } else { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3105 | returnType = Context->getObjCIdType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3106 | } |
| 3107 | // 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] | 3108 | QualType msgSendType = MsgSendFlavor->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3109 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3110 | // Create a reference to the objc_msgSend() declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3111 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, msgSendType, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3112 | VK_LValue, SourceLocation()); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3113 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3114 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3115 | // If we don't do this cast, we get the following bizarre warning/note: |
| 3116 | // xx.m:13: warning: function called through a non-compatible type |
| 3117 | // 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] | 3118 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 3119 | Context->getPointerType(Context->VoidTy), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3120 | CK_BitCast, DRE); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3121 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3122 | // Now do the "normal" pointer to function cast. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3123 | QualType castType = |
| 3124 | getSimpleFunctionType(returnType, &ArgTypes[0], ArgTypes.size(), |
| 3125 | // If we don't have a method decl, force a variadic cast. |
| 3126 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3127 | castType = Context->getPointerType(castType); |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3128 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3129 | cast); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 3130 | |
| 3131 | // Don't forget the parens to enforce the proper binding. |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 3132 | ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3133 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3134 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3135 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3136 | MsgExprs.size(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3137 | FT->getResultType(), VK_RValue, |
| 3138 | EndLoc); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3139 | Stmt *ReplacingStmt = CE; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3140 | if (MsgSendStretFlavor) { |
| 3141 | // We have the method which returns a struct/union. Must also generate |
| 3142 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 3143 | // expression which dictate which one to envoke depending on size of |
| 3144 | // method's return type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3145 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3146 | // Create a reference to the objc_msgSend_stret() declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3147 | DeclRefExpr *STDRE = new (Context) DeclRefExpr(MsgSendStretFlavor, msgSendType, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3148 | VK_LValue, SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3149 | // Need to cast objc_msgSend_stret to "void *" (see above comment). |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3150 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 3151 | Context->getPointerType(Context->VoidTy), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3152 | CK_BitCast, STDRE); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3153 | // Now do the "normal" pointer to function cast. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3154 | castType = getSimpleFunctionType(returnType, &ArgTypes[0], ArgTypes.size(), |
| 3155 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3156 | castType = Context->getPointerType(castType); |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3157 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3158 | cast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3159 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3160 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3161 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3162 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3163 | FT = msgSendType->getAs<FunctionType>(); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3164 | CallExpr *STCE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3165 | MsgExprs.size(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3166 | FT->getResultType(), VK_RValue, |
| 3167 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3168 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3169 | // Build sizeof(returnType) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3170 | UnaryExprOrTypeTraitExpr *sizeofExpr = |
| 3171 | new (Context) UnaryExprOrTypeTraitExpr(UETT_SizeOf, |
| 3172 | Context->getTrivialTypeSourceInfo(returnType), |
| 3173 | Context->getSizeType(), SourceLocation(), |
| 3174 | SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3175 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 3176 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 3177 | // For X86 it is more complicated and some kind of target specific routine |
| 3178 | // is needed to decide what to do. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3179 | unsigned IntSize = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3180 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 3181 | IntegerLiteral *limit = IntegerLiteral::Create(*Context, |
| 3182 | llvm::APInt(IntSize, 8), |
| 3183 | Context->IntTy, |
| 3184 | SourceLocation()); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3185 | BinaryOperator *lessThanExpr = |
| 3186 | new (Context) BinaryOperator(sizeofExpr, limit, BO_LE, Context->IntTy, |
| 3187 | VK_RValue, OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3188 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3189 | ConditionalOperator *CondExpr = |
Douglas Gregor | 47e1f7c | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 3190 | new (Context) ConditionalOperator(lessThanExpr, |
| 3191 | SourceLocation(), CE, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3192 | SourceLocation(), STCE, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3193 | returnType, VK_RValue, OK_Ordinary); |
Fariborz Jahanian | f9b949f | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 3194 | ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3195 | CondExpr); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 3196 | } |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3197 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3198 | return ReplacingStmt; |
| 3199 | } |
| 3200 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3201 | Stmt *RewriteObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 3202 | Stmt *ReplacingStmt = SynthMessageExpr(Exp, Exp->getLocStart(), |
| 3203 | Exp->getLocEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3204 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 3205 | // Now do the actual rewrite. |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 3206 | ReplaceStmt(Exp, ReplacingStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3207 | |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3208 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3209 | return ReplacingStmt; |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 3210 | } |
| 3211 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3212 | // typedef struct objc_object Protocol; |
| 3213 | QualType RewriteObjC::getProtocolType() { |
| 3214 | if (!ProtocolTypeDecl) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3215 | TypeSourceInfo *TInfo |
| 3216 | = Context->getTrivialTypeSourceInfo(Context->getObjCIdType()); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3217 | ProtocolTypeDecl = TypedefDecl::Create(*Context, TUDecl, |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 3218 | SourceLocation(), SourceLocation(), |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3219 | &Context->Idents.get("Protocol"), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3220 | TInfo); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3221 | } |
| 3222 | return Context->getTypeDeclType(ProtocolTypeDecl); |
| 3223 | } |
| 3224 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 3225 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3226 | /// a synthesized/forward data reference (to the protocol's metadata). |
| 3227 | /// The forward references (and metadata) are generated in |
| 3228 | /// RewriteObjC::HandleTranslationUnit(). |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3229 | Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3230 | std::string Name = "_OBJC_PROTOCOL_" + Exp->getProtocol()->getNameAsString(); |
| 3231 | IdentifierInfo *ID = &Context->Idents.get(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3232 | VarDecl *VD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3233 | SourceLocation(), ID, getProtocolType(), 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3234 | SC_Extern, SC_None); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3235 | DeclRefExpr *DRE = new (Context) DeclRefExpr(VD, getProtocolType(), VK_LValue, |
| 3236 | SourceLocation()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3237 | Expr *DerefExpr = new (Context) UnaryOperator(DRE, UO_AddrOf, |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3238 | Context->getPointerType(DRE->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3239 | VK_RValue, OK_Ordinary, SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3240 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, DerefExpr->getType(), |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 3241 | CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3242 | DerefExpr); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3243 | ReplaceStmt(Exp, castExpr); |
| 3244 | ProtocolExprDecls.insert(Exp->getProtocol()); |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3245 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3246 | return castExpr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3247 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 3248 | } |
| 3249 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3250 | bool RewriteObjC::BufferContainsPPDirectives(const char *startBuf, |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3251 | const char *endBuf) { |
| 3252 | while (startBuf < endBuf) { |
| 3253 | if (*startBuf == '#') { |
| 3254 | // Skip whitespace. |
| 3255 | for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf) |
| 3256 | ; |
| 3257 | if (!strncmp(startBuf, "if", strlen("if")) || |
| 3258 | !strncmp(startBuf, "ifdef", strlen("ifdef")) || |
| 3259 | !strncmp(startBuf, "ifndef", strlen("ifndef")) || |
| 3260 | !strncmp(startBuf, "define", strlen("define")) || |
| 3261 | !strncmp(startBuf, "undef", strlen("undef")) || |
| 3262 | !strncmp(startBuf, "else", strlen("else")) || |
| 3263 | !strncmp(startBuf, "elif", strlen("elif")) || |
| 3264 | !strncmp(startBuf, "endif", strlen("endif")) || |
| 3265 | !strncmp(startBuf, "pragma", strlen("pragma")) || |
| 3266 | !strncmp(startBuf, "include", strlen("include")) || |
| 3267 | !strncmp(startBuf, "import", strlen("import")) || |
| 3268 | !strncmp(startBuf, "include_next", strlen("include_next"))) |
| 3269 | return true; |
| 3270 | } |
| 3271 | startBuf++; |
| 3272 | } |
| 3273 | return false; |
| 3274 | } |
| 3275 | |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 3276 | /// RewriteObjCInternalStruct - Rewrite one internal struct corresponding to |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3277 | /// an objective-c class with ivars. |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 3278 | void RewriteObjC::RewriteObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3279 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3280 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3281 | assert(CDecl->getName() != "" && |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 3282 | "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 3283 | // Do not synthesize more than once. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3284 | if (ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 3285 | return; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3286 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3287 | int NumIvars = CDecl->ivar_size(); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3288 | SourceLocation LocStart = CDecl->getLocStart(); |
| 3289 | SourceLocation LocEnd = CDecl->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3290 | |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3291 | const char *startBuf = SM->getCharacterData(LocStart); |
| 3292 | const char *endBuf = SM->getCharacterData(LocEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3293 | |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3294 | // If no ivars and no root or if its root, directly or indirectly, |
| 3295 | // have no ivars (thus not synthesized) then no need to synthesize this class. |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3296 | if ((CDecl->isForwardDecl() || NumIvars == 0) && |
| 3297 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3298 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3299 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3300 | return; |
| 3301 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3302 | |
| 3303 | // FIXME: This has potential of causing problem. If |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3304 | // SynthesizeObjCInternalStruct is ever called recursively. |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3305 | Result += "\nstruct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3306 | Result += CDecl->getNameAsString(); |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 3307 | if (LangOpts.MicrosoftExt) |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 3308 | Result += "_IMPL"; |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 3309 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3310 | if (NumIvars > 0) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3311 | const char *cursor = strchr(startBuf, '{'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3312 | assert((cursor && endBuf) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3313 | && "SynthesizeObjCInternalStruct - malformed @interface"); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3314 | // If the buffer contains preprocessor directives, we do more fine-grained |
| 3315 | // rewrites. This is intended to fix code that looks like (which occurs in |
| 3316 | // NSURL.h, for example): |
| 3317 | // |
| 3318 | // #ifdef XYZ |
| 3319 | // @interface Foo : NSObject |
| 3320 | // #else |
| 3321 | // @interface FooBar : NSObject |
| 3322 | // #endif |
| 3323 | // { |
| 3324 | // int i; |
| 3325 | // } |
| 3326 | // @end |
| 3327 | // |
| 3328 | // This clause is segregated to avoid breaking the common case. |
| 3329 | if (BufferContainsPPDirectives(startBuf, cursor)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3330 | SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() : |
Argyrios Kyrtzidis | 1711fc9 | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 3331 | CDecl->getAtStartLoc(); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3332 | const char *endHeader = SM->getCharacterData(L); |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3333 | endHeader += Lexer::MeasureTokenLength(L, *SM, LangOpts); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3334 | |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3335 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3336 | // advance to the end of the referenced protocols. |
| 3337 | while (endHeader < cursor && *endHeader != '>') endHeader++; |
| 3338 | endHeader++; |
| 3339 | } |
| 3340 | // rewrite the original header |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3341 | ReplaceText(LocStart, endHeader-startBuf, Result); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3342 | } else { |
| 3343 | // rewrite the original header *without* disturbing the '{' |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3344 | ReplaceText(LocStart, cursor-startBuf, Result); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3345 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3346 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3347 | Result = "\n struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3348 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 39bbd9f | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 3349 | Result += "_IMPL "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3350 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 819173c | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 3351 | Result += "_IVARS;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3352 | |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3353 | // insert the super class structure definition. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 3354 | SourceLocation OnePastCurly = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3355 | LocStart.getLocWithOffset(cursor-startBuf+1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3356 | InsertText(OnePastCurly, Result); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3357 | } |
| 3358 | cursor++; // past '{' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3359 | |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3360 | // Now comment out any visibility specifiers. |
| 3361 | while (cursor < endBuf) { |
| 3362 | if (*cursor == '@') { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3363 | SourceLocation atLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Chris Lattner | df6a51b | 2007-11-14 22:57:51 +0000 | [diff] [blame] | 3364 | // Skip whitespace. |
| 3365 | for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor) |
| 3366 | /*scan*/; |
| 3367 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3368 | // FIXME: presence of @public, etc. inside comment results in |
| 3369 | // this transformation as well, which is still correct c-code. |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3370 | if (!strncmp(cursor, "public", strlen("public")) || |
| 3371 | !strncmp(cursor, "private", strlen("private")) || |
Steve Naroff | c5e3277 | 2008-04-04 22:34:24 +0000 | [diff] [blame] | 3372 | !strncmp(cursor, "package", strlen("package")) || |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3373 | !strncmp(cursor, "protected", strlen("protected"))) |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3374 | InsertText(atLoc, "// "); |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3375 | } |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3376 | // FIXME: If there are cases where '<' is used in ivar declaration part |
| 3377 | // of user code, then scan the ivar list and use needToScanForQualifiers |
| 3378 | // for type checking. |
| 3379 | else if (*cursor == '<') { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3380 | SourceLocation atLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3381 | InsertText(atLoc, "/* "); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3382 | cursor = strchr(cursor, '>'); |
| 3383 | cursor++; |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3384 | atLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3385 | InsertText(atLoc, " */"); |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3386 | } else if (*cursor == '^') { // rewrite block specifier. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3387 | SourceLocation caretLoc = LocStart.getLocWithOffset(cursor-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3388 | ReplaceText(caretLoc, 1, "*"); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3389 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3390 | cursor++; |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3391 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3392 | // Don't forget to add a ';'!! |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3393 | InsertText(LocEnd.getLocWithOffset(1), ";"); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3394 | } else { // we don't have any instance variables - insert super struct. |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3395 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3396 | Result += " {\n struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3397 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 39bbd9f | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 3398 | Result += "_IMPL "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3399 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 819173c | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 3400 | Result += "_IVARS;\n};\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3401 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3402 | } |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3403 | // Mark this struct as having been generated. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3404 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3405 | llvm_unreachable("struct already synthesize- SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3406 | } |
| 3407 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3408 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3409 | /// class methods. |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3410 | template<typename MethodIterator> |
| 3411 | void RewriteObjC::RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 3412 | MethodIterator MethodEnd, |
Fariborz Jahanian | 8e991ba | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 3413 | bool IsInstanceMethod, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3414 | StringRef prefix, |
| 3415 | StringRef ClassName, |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 3416 | std::string &Result) { |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3417 | if (MethodBegin == MethodEnd) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3418 | |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3419 | if (!objc_impl_method) { |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3420 | /* struct _objc_method { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3421 | SEL _cmd; |
| 3422 | char *method_types; |
| 3423 | void *_imp; |
| 3424 | } |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3425 | */ |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 3426 | Result += "\nstruct _objc_method {\n"; |
| 3427 | Result += "\tSEL _cmd;\n"; |
| 3428 | Result += "\tchar *method_types;\n"; |
| 3429 | Result += "\tvoid *_imp;\n"; |
| 3430 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3431 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3432 | objc_impl_method = true; |
Fariborz Jahanian | 776d6ff | 2007-10-19 00:36:46 +0000 | [diff] [blame] | 3433 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3434 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3435 | // Build _objc_method_list for class's methods if needed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3436 | |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3437 | /* struct { |
| 3438 | struct _objc_method_list *next_method; |
| 3439 | int method_count; |
| 3440 | struct _objc_method method_list[]; |
| 3441 | } |
| 3442 | */ |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3443 | unsigned NumMethods = std::distance(MethodBegin, MethodEnd); |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3444 | Result += "\nstatic struct {\n"; |
| 3445 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 3446 | Result += "\tint method_count;\n"; |
| 3447 | Result += "\tstruct _objc_method method_list["; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3448 | Result += utostr(NumMethods); |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3449 | Result += "];\n} _OBJC_"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3450 | Result += prefix; |
| 3451 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 3452 | Result += "_METHODS_"; |
| 3453 | Result += ClassName; |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3454 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3455 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 3456 | Result += "_meth\")))= "; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3457 | Result += "{\n\t0, " + utostr(NumMethods) + "\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3458 | |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3459 | Result += "\t,{{(SEL)\""; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3460 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3461 | std::string MethodTypeString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3462 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3463 | Result += "\", \""; |
| 3464 | Result += MethodTypeString; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3465 | Result += "\", (void *)"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3466 | Result += MethodInternalNames[*MethodBegin]; |
| 3467 | Result += "}\n"; |
| 3468 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 3469 | Result += "\t ,{(SEL)\""; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3470 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3471 | std::string MethodTypeString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3472 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3473 | Result += "\", \""; |
| 3474 | Result += MethodTypeString; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3475 | Result += "\", (void *)"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3476 | Result += MethodInternalNames[*MethodBegin]; |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 3477 | Result += "}\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3478 | } |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3479 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3480 | } |
| 3481 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3482 | /// RewriteObjCProtocolMetaData - Rewrite protocols meta-data. |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 3483 | void RewriteObjC:: |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3484 | RewriteObjCProtocolMetaData(ObjCProtocolDecl *PDecl, StringRef prefix, |
| 3485 | StringRef ClassName, std::string &Result) { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3486 | static bool objc_protocol_methods = false; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3487 | |
| 3488 | // Output struct protocol_methods holder of method selector and type. |
| 3489 | if (!objc_protocol_methods && !PDecl->isForwardDecl()) { |
| 3490 | /* struct protocol_methods { |
| 3491 | SEL _cmd; |
| 3492 | char *method_types; |
| 3493 | } |
| 3494 | */ |
| 3495 | Result += "\nstruct _protocol_methods {\n"; |
| 3496 | Result += "\tstruct objc_selector *_cmd;\n"; |
| 3497 | Result += "\tchar *method_types;\n"; |
| 3498 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3499 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3500 | objc_protocol_methods = true; |
| 3501 | } |
| 3502 | // Do not synthesize the protocol more than once. |
| 3503 | if (ObjCSynthesizedProtocols.count(PDecl)) |
| 3504 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3505 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3506 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 3507 | unsigned NumMethods = std::distance(PDecl->instmeth_begin(), |
| 3508 | PDecl->instmeth_end()); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3509 | /* struct _objc_protocol_method_list { |
| 3510 | int protocol_method_count; |
| 3511 | struct protocol_methods protocols[]; |
| 3512 | } |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3513 | */ |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3514 | Result += "\nstatic struct {\n"; |
| 3515 | Result += "\tint protocol_method_count;\n"; |
| 3516 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 3517 | Result += utostr(NumMethods); |
| 3518 | Result += "];\n} _OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 3519 | Result += PDecl->getNameAsString(); |
| 3520 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= " |
| 3521 | "{\n\t" + utostr(NumMethods) + "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3522 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3523 | // Output instance methods declared in this protocol. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3524 | for (ObjCProtocolDecl::instmeth_iterator |
| 3525 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3526 | I != E; ++I) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3527 | if (I == PDecl->instmeth_begin()) |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3528 | Result += "\t ,{{(struct objc_selector *)\""; |
| 3529 | else |
| 3530 | Result += "\t ,{(struct objc_selector *)\""; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3531 | Result += (*I)->getSelector().getAsString(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3532 | std::string MethodTypeString; |
| 3533 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 3534 | Result += "\", \""; |
| 3535 | Result += MethodTypeString; |
| 3536 | Result += "\"}\n"; |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3537 | } |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3538 | Result += "\t }\n};\n"; |
| 3539 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3540 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3541 | // Output class methods declared in this protocol. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3542 | unsigned NumMethods = std::distance(PDecl->classmeth_begin(), |
| 3543 | PDecl->classmeth_end()); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3544 | if (NumMethods > 0) { |
| 3545 | /* struct _objc_protocol_method_list { |
| 3546 | int protocol_method_count; |
| 3547 | struct protocol_methods protocols[]; |
| 3548 | } |
| 3549 | */ |
| 3550 | Result += "\nstatic struct {\n"; |
| 3551 | Result += "\tint protocol_method_count;\n"; |
| 3552 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 3553 | Result += utostr(NumMethods); |
| 3554 | Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_"; |
| 3555 | Result += PDecl->getNameAsString(); |
| 3556 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 3557 | "{\n\t"; |
| 3558 | Result += utostr(NumMethods); |
| 3559 | Result += "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3560 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3561 | // Output instance methods declared in this protocol. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3562 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3563 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3564 | I != E; ++I) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3565 | if (I == PDecl->classmeth_begin()) |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3566 | Result += "\t ,{{(struct objc_selector *)\""; |
| 3567 | else |
| 3568 | Result += "\t ,{(struct objc_selector *)\""; |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3569 | Result += (*I)->getSelector().getAsString(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3570 | std::string MethodTypeString; |
| 3571 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 3572 | Result += "\", \""; |
| 3573 | Result += MethodTypeString; |
| 3574 | Result += "\"}\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3575 | } |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3576 | Result += "\t }\n};\n"; |
| 3577 | } |
| 3578 | |
| 3579 | // Output: |
| 3580 | /* struct _objc_protocol { |
| 3581 | // Objective-C 1.0 extensions |
| 3582 | struct _objc_protocol_extension *isa; |
| 3583 | char *protocol_name; |
| 3584 | struct _objc_protocol **protocol_list; |
| 3585 | struct _objc_protocol_method_list *instance_methods; |
| 3586 | struct _objc_protocol_method_list *class_methods; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3587 | }; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3588 | */ |
| 3589 | static bool objc_protocol = false; |
| 3590 | if (!objc_protocol) { |
| 3591 | Result += "\nstruct _objc_protocol {\n"; |
| 3592 | Result += "\tstruct _objc_protocol_extension *isa;\n"; |
| 3593 | Result += "\tchar *protocol_name;\n"; |
| 3594 | Result += "\tstruct _objc_protocol **protocol_list;\n"; |
| 3595 | Result += "\tstruct _objc_protocol_method_list *instance_methods;\n"; |
| 3596 | Result += "\tstruct _objc_protocol_method_list *class_methods;\n"; |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3597 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3598 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3599 | objc_protocol = true; |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3600 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3601 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3602 | Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_"; |
| 3603 | Result += PDecl->getNameAsString(); |
| 3604 | Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= " |
| 3605 | "{\n\t0, \""; |
| 3606 | Result += PDecl->getNameAsString(); |
| 3607 | Result += "\", 0, "; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3608 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3609 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 3610 | Result += PDecl->getNameAsString(); |
| 3611 | Result += ", "; |
| 3612 | } |
| 3613 | else |
| 3614 | Result += "0, "; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3615 | if (PDecl->classmeth_begin() != PDecl->classmeth_end()) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3616 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
| 3617 | Result += PDecl->getNameAsString(); |
| 3618 | Result += "\n"; |
| 3619 | } |
| 3620 | else |
| 3621 | Result += "0\n"; |
| 3622 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3623 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3624 | // Mark this protocol as having been generated. |
| 3625 | if (!ObjCSynthesizedProtocols.insert(PDecl)) |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3626 | llvm_unreachable("protocol already synthesized"); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3627 | |
| 3628 | } |
| 3629 | |
| 3630 | void RewriteObjC:: |
| 3631 | RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Protocols, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3632 | StringRef prefix, StringRef ClassName, |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3633 | std::string &Result) { |
| 3634 | if (Protocols.empty()) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3635 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3636 | for (unsigned i = 0; i != Protocols.size(); i++) |
| 3637 | RewriteObjCProtocolMetaData(Protocols[i], prefix, ClassName, Result); |
| 3638 | |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3639 | // Output the top lovel protocol meta-data for the class. |
| 3640 | /* struct _objc_protocol_list { |
| 3641 | struct _objc_protocol_list *next; |
| 3642 | int protocol_count; |
| 3643 | struct _objc_protocol *class_protocols[]; |
| 3644 | } |
| 3645 | */ |
| 3646 | Result += "\nstatic struct {\n"; |
| 3647 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 3648 | Result += "\tint protocol_count;\n"; |
| 3649 | Result += "\tstruct _objc_protocol *class_protocols["; |
| 3650 | Result += utostr(Protocols.size()); |
| 3651 | Result += "];\n} _OBJC_"; |
| 3652 | Result += prefix; |
| 3653 | Result += "_PROTOCOLS_"; |
| 3654 | Result += ClassName; |
| 3655 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 3656 | "{\n\t0, "; |
| 3657 | Result += utostr(Protocols.size()); |
| 3658 | Result += "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3659 | |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3660 | Result += "\t,{&_OBJC_PROTOCOL_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3661 | Result += Protocols[0]->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3662 | Result += " \n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3663 | |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3664 | for (unsigned i = 1; i != Protocols.size(); i++) { |
| 3665 | Result += "\t ,&_OBJC_PROTOCOL_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3666 | Result += Protocols[i]->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3667 | Result += "\n"; |
| 3668 | } |
| 3669 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3670 | } |
| 3671 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3672 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3673 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3674 | /// implementation. |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3675 | void RewriteObjC::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3676 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3677 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3678 | // Find category declaration for this implementation. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3679 | ObjCCategoryDecl *CDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3680 | for (CDecl = ClassDecl->getCategoryList(); CDecl; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3681 | CDecl = CDecl->getNextClassCategory()) |
| 3682 | if (CDecl->getIdentifier() == IDecl->getIdentifier()) |
| 3683 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3684 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3685 | std::string FullCategoryName = ClassDecl->getNameAsString(); |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3686 | FullCategoryName += '_'; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3687 | FullCategoryName += IDecl->getNameAsString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3688 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3689 | // Build _objc_method_list for class's instance methods if needed |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3690 | SmallVector<ObjCMethodDecl *, 32> |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3691 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3692 | |
| 3693 | // If any of our property implementations have associated getters or |
| 3694 | // setters, produce metadata for them as well. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3695 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 3696 | PropEnd = IDecl->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3697 | Prop != PropEnd; ++Prop) { |
| 3698 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 3699 | continue; |
| 3700 | if (!(*Prop)->getPropertyIvarDecl()) |
| 3701 | continue; |
| 3702 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 3703 | if (!PD) |
| 3704 | continue; |
| 3705 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 3706 | InstanceMethods.push_back(Getter); |
| 3707 | if (PD->isReadOnly()) |
| 3708 | continue; |
| 3709 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 3710 | InstanceMethods.push_back(Setter); |
| 3711 | } |
| 3712 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3713 | true, "CATEGORY_", FullCategoryName.c_str(), |
| 3714 | Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3715 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3716 | // Build _objc_method_list for class's class methods if needed |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3717 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3718 | false, "CATEGORY_", FullCategoryName.c_str(), |
| 3719 | Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3720 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3721 | // Protocols referenced in class declaration? |
Fariborz Jahanian | bac97d4 | 2007-11-13 22:09:49 +0000 | [diff] [blame] | 3722 | // Null CDecl is case of a category implementation with no category interface |
| 3723 | if (CDecl) |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3724 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), "CATEGORY", |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3725 | FullCategoryName, Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3726 | /* struct _objc_category { |
| 3727 | char *category_name; |
| 3728 | char *class_name; |
| 3729 | struct _objc_method_list *instance_methods; |
| 3730 | struct _objc_method_list *class_methods; |
| 3731 | struct _objc_protocol_list *protocols; |
| 3732 | // Objective-C 1.0 extensions |
| 3733 | uint32_t size; // sizeof (struct _objc_category) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3734 | struct _objc_property_list *instance_properties; // category's own |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3735 | // @property decl. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3736 | }; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3737 | */ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3738 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3739 | static bool objc_category = false; |
| 3740 | if (!objc_category) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3741 | Result += "\nstruct _objc_category {\n"; |
| 3742 | Result += "\tchar *category_name;\n"; |
| 3743 | Result += "\tchar *class_name;\n"; |
| 3744 | Result += "\tstruct _objc_method_list *instance_methods;\n"; |
| 3745 | Result += "\tstruct _objc_method_list *class_methods;\n"; |
| 3746 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3747 | Result += "\tunsigned int size;\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3748 | Result += "\tstruct _objc_property_list *instance_properties;\n"; |
| 3749 | Result += "};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3750 | objc_category = true; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3751 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3752 | Result += "\nstatic struct _objc_category _OBJC_CATEGORY_"; |
| 3753 | Result += FullCategoryName; |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3754 | Result += " __attribute__ ((used, section (\"__OBJC, __category\")))= {\n\t\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3755 | Result += IDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3756 | Result += "\"\n\t, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3757 | Result += ClassDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3758 | Result += "\"\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3759 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3760 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3761 | Result += "\t, (struct _objc_method_list *)" |
| 3762 | "&_OBJC_CATEGORY_INSTANCE_METHODS_"; |
| 3763 | Result += FullCategoryName; |
| 3764 | Result += "\n"; |
| 3765 | } |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3766 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3767 | Result += "\t, 0\n"; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3768 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3769 | Result += "\t, (struct _objc_method_list *)" |
| 3770 | "&_OBJC_CATEGORY_CLASS_METHODS_"; |
| 3771 | Result += FullCategoryName; |
| 3772 | Result += "\n"; |
| 3773 | } |
| 3774 | else |
| 3775 | Result += "\t, 0\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3776 | |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3777 | if (CDecl && CDecl->protocol_begin() != CDecl->protocol_end()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3778 | Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3779 | Result += FullCategoryName; |
| 3780 | Result += "\n"; |
| 3781 | } |
| 3782 | else |
| 3783 | Result += "\t, 0\n"; |
| 3784 | Result += "\t, sizeof(struct _objc_category), 0\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3785 | } |
| 3786 | |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 3787 | /// RewriteIvarOffsetComputation - This rutine synthesizes computation of |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3788 | /// ivar offset. |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 3789 | void RewriteObjC::RewriteIvarOffsetComputation(ObjCIvarDecl *ivar, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3790 | std::string &Result) { |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3791 | if (ivar->isBitField()) { |
| 3792 | // FIXME: The hack below doesn't work for bitfields. For now, we simply |
| 3793 | // place all bitfields at offset 0. |
| 3794 | Result += "0"; |
| 3795 | } else { |
| 3796 | Result += "__OFFSETOFIVAR__(struct "; |
Fariborz Jahanian | 2d8c1fd | 2010-10-16 00:29:27 +0000 | [diff] [blame] | 3797 | Result += ivar->getContainingInterface()->getNameAsString(); |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 3798 | if (LangOpts.MicrosoftExt) |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3799 | Result += "_IMPL"; |
| 3800 | Result += ", "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3801 | Result += ivar->getNameAsString(); |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3802 | Result += ")"; |
| 3803 | } |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3804 | } |
| 3805 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3806 | //===----------------------------------------------------------------------===// |
| 3807 | // Meta Data Emission |
| 3808 | //===----------------------------------------------------------------------===// |
| 3809 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3810 | void RewriteObjC::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3811 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3812 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3813 | |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3814 | // Explicitly declared @interface's are already synthesized. |
Steve Naroff | 33feeb0 | 2009-04-20 20:09:33 +0000 | [diff] [blame] | 3815 | if (CDecl->isImplicitInterfaceDecl()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3816 | // FIXME: Implementation of a class with no @interface (legacy) doese not |
Fariborz Jahanian | ebe668f | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3817 | // produce correct synthesis as yet. |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 3818 | RewriteObjCInternalStruct(CDecl, Result); |
Fariborz Jahanian | ebe668f | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3819 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3820 | |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3821 | // Build _objc_ivar_list metadata for classes ivars if needed |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3822 | unsigned NumIvars = !IDecl->ivar_empty() |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3823 | ? IDecl->ivar_size() |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3824 | : (CDecl ? CDecl->ivar_size() : 0); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3825 | if (NumIvars > 0) { |
| 3826 | static bool objc_ivar = false; |
| 3827 | if (!objc_ivar) { |
| 3828 | /* struct _objc_ivar { |
| 3829 | char *ivar_name; |
| 3830 | char *ivar_type; |
| 3831 | int ivar_offset; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3832 | }; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3833 | */ |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3834 | Result += "\nstruct _objc_ivar {\n"; |
| 3835 | Result += "\tchar *ivar_name;\n"; |
| 3836 | Result += "\tchar *ivar_type;\n"; |
| 3837 | Result += "\tint ivar_offset;\n"; |
| 3838 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3839 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3840 | objc_ivar = true; |
| 3841 | } |
| 3842 | |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3843 | /* struct { |
| 3844 | int ivar_count; |
| 3845 | struct _objc_ivar ivar_list[nIvars]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3846 | }; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3847 | */ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3848 | Result += "\nstatic struct {\n"; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3849 | Result += "\tint ivar_count;\n"; |
| 3850 | Result += "\tstruct _objc_ivar ivar_list["; |
| 3851 | Result += utostr(NumIvars); |
| 3852 | Result += "];\n} _OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3853 | Result += IDecl->getNameAsString(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3854 | Result += " __attribute__ ((used, section (\"__OBJC, __instance_vars\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3855 | "{\n\t"; |
| 3856 | Result += utostr(NumIvars); |
| 3857 | Result += "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3858 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3859 | ObjCInterfaceDecl::ivar_iterator IVI, IVE; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3860 | SmallVector<ObjCIvarDecl *, 8> IVars; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3861 | if (!IDecl->ivar_empty()) { |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 3862 | for (ObjCInterfaceDecl::ivar_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3863 | IV = IDecl->ivar_begin(), IVEnd = IDecl->ivar_end(); |
Douglas Gregor | 8f36aba | 2009-04-23 03:23:08 +0000 | [diff] [blame] | 3864 | IV != IVEnd; ++IV) |
| 3865 | IVars.push_back(*IV); |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 3866 | IVI = IDecl->ivar_begin(); |
| 3867 | IVE = IDecl->ivar_end(); |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3868 | } else { |
| 3869 | IVI = CDecl->ivar_begin(); |
| 3870 | IVE = CDecl->ivar_end(); |
| 3871 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3872 | Result += "\t,{{\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3873 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3874 | Result += "\", \""; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3875 | std::string TmpString, StrEncoding; |
| 3876 | Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI); |
| 3877 | QuoteDoublequotes(TmpString, StrEncoding); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3878 | Result += StrEncoding; |
| 3879 | Result += "\", "; |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 3880 | RewriteIvarOffsetComputation(*IVI, Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3881 | Result += "}\n"; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3882 | for (++IVI; IVI != IVE; ++IVI) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3883 | Result += "\t ,{\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3884 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3885 | Result += "\", \""; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3886 | std::string TmpString, StrEncoding; |
| 3887 | Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI); |
| 3888 | QuoteDoublequotes(TmpString, StrEncoding); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3889 | Result += StrEncoding; |
| 3890 | Result += "\", "; |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 3891 | RewriteIvarOffsetComputation((*IVI), Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3892 | Result += "}\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3893 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3894 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3895 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3896 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3897 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3898 | // Build _objc_method_list for class's instance methods if needed |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3899 | SmallVector<ObjCMethodDecl *, 32> |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3900 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3901 | |
| 3902 | // If any of our property implementations have associated getters or |
| 3903 | // setters, produce metadata for them as well. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3904 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 3905 | PropEnd = IDecl->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3906 | Prop != PropEnd; ++Prop) { |
| 3907 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 3908 | continue; |
| 3909 | if (!(*Prop)->getPropertyIvarDecl()) |
| 3910 | continue; |
| 3911 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 3912 | if (!PD) |
| 3913 | continue; |
| 3914 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 3915 | if (!Getter->isDefined()) |
| 3916 | InstanceMethods.push_back(Getter); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3917 | if (PD->isReadOnly()) |
| 3918 | continue; |
| 3919 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
Fariborz Jahanian | ec3683b | 2010-10-19 23:47:54 +0000 | [diff] [blame] | 3920 | if (!Setter->isDefined()) |
| 3921 | InstanceMethods.push_back(Setter); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3922 | } |
| 3923 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3924 | true, "", IDecl->getName(), Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3925 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3926 | // Build _objc_method_list for class's class methods if needed |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3927 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3928 | false, "", IDecl->getName(), Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3929 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3930 | // Protocols referenced in class declaration? |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3931 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3932 | "CLASS", CDecl->getName(), Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3933 | |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3934 | // Declaration of class/meta-class metadata |
| 3935 | /* struct _objc_class { |
| 3936 | struct _objc_class *isa; // or const char *root_class_name when metadata |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3937 | const char *super_class_name; |
| 3938 | char *name; |
| 3939 | long version; |
| 3940 | long info; |
| 3941 | long instance_size; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3942 | struct _objc_ivar_list *ivars; |
| 3943 | struct _objc_method_list *methods; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3944 | struct objc_cache *cache; |
| 3945 | struct objc_protocol_list *protocols; |
| 3946 | const char *ivar_layout; |
| 3947 | struct _objc_class_ext *ext; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3948 | }; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3949 | */ |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3950 | static bool objc_class = false; |
| 3951 | if (!objc_class) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3952 | Result += "\nstruct _objc_class {\n"; |
| 3953 | Result += "\tstruct _objc_class *isa;\n"; |
| 3954 | Result += "\tconst char *super_class_name;\n"; |
| 3955 | Result += "\tchar *name;\n"; |
| 3956 | Result += "\tlong version;\n"; |
| 3957 | Result += "\tlong info;\n"; |
| 3958 | Result += "\tlong instance_size;\n"; |
| 3959 | Result += "\tstruct _objc_ivar_list *ivars;\n"; |
| 3960 | Result += "\tstruct _objc_method_list *methods;\n"; |
| 3961 | Result += "\tstruct objc_cache *cache;\n"; |
| 3962 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 3963 | Result += "\tconst char *ivar_layout;\n"; |
| 3964 | Result += "\tstruct _objc_class_ext *ext;\n"; |
| 3965 | Result += "};\n"; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3966 | objc_class = true; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3967 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3968 | |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3969 | // Meta-class metadata generation. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3970 | ObjCInterfaceDecl *RootClass = 0; |
| 3971 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3972 | while (SuperClass) { |
| 3973 | RootClass = SuperClass; |
| 3974 | SuperClass = SuperClass->getSuperClass(); |
| 3975 | } |
| 3976 | SuperClass = CDecl->getSuperClass(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3977 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3978 | Result += "\nstatic struct _objc_class _OBJC_METACLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3979 | Result += CDecl->getNameAsString(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3980 | Result += " __attribute__ ((used, section (\"__OBJC, __meta_class\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3981 | "{\n\t(struct _objc_class *)\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3982 | Result += (RootClass ? RootClass->getNameAsString() : CDecl->getNameAsString()); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3983 | Result += "\""; |
| 3984 | |
| 3985 | if (SuperClass) { |
| 3986 | Result += ", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3987 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3988 | Result += "\", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3989 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3990 | Result += "\""; |
| 3991 | } |
| 3992 | else { |
| 3993 | Result += ", 0, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3994 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3995 | Result += "\""; |
| 3996 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3997 | // Set 'ivars' field for root class to 0. ObjC1 runtime does not use it. |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3998 | // 'info' field is initialized to CLS_META(2) for metaclass |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3999 | Result += ", 0,2, sizeof(struct _objc_class), 0"; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4000 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 4001 | Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4002 | Result += IDecl->getNameAsString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4003 | Result += "\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4004 | } |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 4005 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4006 | Result += ", 0\n"; |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 4007 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 4008 | Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4009 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4010 | Result += ",0,0\n"; |
| 4011 | } |
Fariborz Jahanian | 454cb01 | 2007-10-24 20:54:23 +0000 | [diff] [blame] | 4012 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4013 | Result += "\t,0,0,0,0\n"; |
| 4014 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4015 | |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 4016 | // class metadata generation. |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4017 | Result += "\nstatic struct _objc_class _OBJC_CLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4018 | Result += CDecl->getNameAsString(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 4019 | Result += " __attribute__ ((used, section (\"__OBJC, __class\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4020 | "{\n\t&_OBJC_METACLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4021 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4022 | if (SuperClass) { |
| 4023 | Result += ", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4024 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4025 | Result += "\", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4026 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4027 | Result += "\""; |
| 4028 | } |
| 4029 | else { |
| 4030 | Result += ", 0, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4031 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4032 | Result += "\""; |
| 4033 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 4034 | // 'info' field is initialized to CLS_CLASS(1) for class |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 4035 | Result += ", 0,1"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4036 | if (!ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 4037 | Result += ",0"; |
| 4038 | else { |
| 4039 | // class has size. Must synthesize its size. |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 4040 | Result += ",sizeof(struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4041 | Result += CDecl->getNameAsString(); |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 4042 | if (LangOpts.MicrosoftExt) |
Steve Naroff | ba9ac4e | 2008-03-10 23:33:22 +0000 | [diff] [blame] | 4043 | Result += "_IMPL"; |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 4044 | Result += ")"; |
| 4045 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4046 | if (NumIvars > 0) { |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 4047 | Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4048 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4049 | Result += "\n\t"; |
| 4050 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 4051 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4052 | Result += ",0"; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4053 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 4054 | Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4055 | Result += CDecl->getNameAsString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4056 | Result += ", 0\n\t"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4057 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 4058 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4059 | Result += ",0,0"; |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 4060 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 4061 | Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4062 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4063 | Result += ", 0,0\n"; |
| 4064 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 4065 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4066 | Result += ",0,0,0\n"; |
| 4067 | Result += "};\n"; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 4068 | } |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 4069 | |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 4070 | /// RewriteImplementations - This routine rewrites all method implementations |
| 4071 | /// and emits meta-data. |
| 4072 | |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 4073 | void RewriteObjC::RewriteImplementations() { |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 4074 | int ClsDefCount = ClassImplementation.size(); |
| 4075 | int CatDefCount = CategoryImplementation.size(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4076 | |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 4077 | // Rewrite implemented methods |
| 4078 | for (int i = 0; i < ClsDefCount; i++) |
| 4079 | RewriteImplementationDecl(ClassImplementation[i]); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4080 | |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 4081 | for (int i = 0; i < CatDefCount; i++) |
| 4082 | RewriteImplementationDecl(CategoryImplementation[i]); |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 4083 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4084 | |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 4085 | void RewriteObjC::RewriteMetaDataIntoBuffer(std::string &Result) { |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 4086 | int ClsDefCount = ClassImplementation.size(); |
| 4087 | int CatDefCount = CategoryImplementation.size(); |
Fariborz Jahanian | 7c63fdd | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 4088 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 4089 | // For each implemented class, write out all its meta data. |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 4090 | for (int i = 0; i < ClsDefCount; i++) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4091 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4092 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 4093 | // For each implemented category, write out all its meta data. |
| 4094 | for (int i = 0; i < CatDefCount; i++) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4095 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4096 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 4097 | // Write objc_symtab metadata |
| 4098 | /* |
| 4099 | struct _objc_symtab |
| 4100 | { |
| 4101 | long sel_ref_cnt; |
| 4102 | SEL *refs; |
| 4103 | short cls_def_cnt; |
| 4104 | short cat_def_cnt; |
| 4105 | void *defs[cls_def_cnt + cat_def_cnt]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4106 | }; |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 4107 | */ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4108 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4109 | Result += "\nstruct _objc_symtab {\n"; |
| 4110 | Result += "\tlong sel_ref_cnt;\n"; |
| 4111 | Result += "\tSEL *refs;\n"; |
| 4112 | Result += "\tshort cls_def_cnt;\n"; |
| 4113 | Result += "\tshort cat_def_cnt;\n"; |
| 4114 | Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n"; |
| 4115 | Result += "};\n\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4116 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4117 | Result += "static struct _objc_symtab " |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 4118 | "_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4119 | Result += "\t0, 0, " + utostr(ClsDefCount) |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4120 | + ", " + utostr(CatDefCount) + "\n"; |
| 4121 | for (int i = 0; i < ClsDefCount; i++) { |
| 4122 | Result += "\t,&_OBJC_CLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4123 | Result += ClassImplementation[i]->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4124 | Result += "\n"; |
| 4125 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4126 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4127 | for (int i = 0; i < CatDefCount; i++) { |
| 4128 | Result += "\t,&_OBJC_CATEGORY_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4129 | Result += CategoryImplementation[i]->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4130 | Result += "_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4131 | Result += CategoryImplementation[i]->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4132 | Result += "\n"; |
| 4133 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4134 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4135 | Result += "};\n\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4136 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 4137 | // Write objc_module metadata |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4138 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 4139 | /* |
| 4140 | struct _objc_module { |
| 4141 | long version; |
| 4142 | long size; |
| 4143 | const char *name; |
| 4144 | struct _objc_symtab *symtab; |
| 4145 | } |
| 4146 | */ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4147 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4148 | Result += "\nstruct _objc_module {\n"; |
| 4149 | Result += "\tlong version;\n"; |
| 4150 | Result += "\tlong size;\n"; |
| 4151 | Result += "\tconst char *name;\n"; |
| 4152 | Result += "\tstruct _objc_symtab *symtab;\n"; |
| 4153 | Result += "};\n\n"; |
| 4154 | Result += "static struct _objc_module " |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 4155 | "_OBJC_MODULES __attribute__ ((used, section (\"__OBJC, __module_info\")))= {\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4156 | Result += "\t" + utostr(OBJC_ABI_VERSION) + |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 4157 | ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 4158 | Result += "};\n\n"; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 4159 | |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 4160 | if (LangOpts.MicrosoftExt) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4161 | if (ProtocolExprDecls.size()) { |
| 4162 | Result += "#pragma section(\".objc_protocol$B\",long,read,write)\n"; |
| 4163 | Result += "#pragma data_seg(push, \".objc_protocol$B\")\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4164 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4165 | E = ProtocolExprDecls.end(); I != E; ++I) { |
| 4166 | Result += "static struct _objc_protocol *_POINTER_OBJC_PROTOCOL_"; |
| 4167 | Result += (*I)->getNameAsString(); |
| 4168 | Result += " = &_OBJC_PROTOCOL_"; |
| 4169 | Result += (*I)->getNameAsString(); |
| 4170 | Result += ";\n"; |
| 4171 | } |
| 4172 | Result += "#pragma data_seg(pop)\n\n"; |
| 4173 | } |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 4174 | Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n"; |
Steve Naroff | 1919032 | 2008-05-07 00:06:16 +0000 | [diff] [blame] | 4175 | Result += "#pragma data_seg(push, \".objc_module_info$B\")\n"; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 4176 | Result += "static struct _objc_module *_POINTER_OBJC_MODULES = "; |
| 4177 | Result += "&_OBJC_MODULES;\n"; |
| 4178 | Result += "#pragma data_seg(pop)\n\n"; |
| 4179 | } |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 4180 | } |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 4181 | |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4182 | void RewriteObjC::RewriteByRefString(std::string &ResultStr, |
| 4183 | const std::string &Name, |
Fariborz Jahanian | 1e8011e | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 4184 | ValueDecl *VD, bool def) { |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4185 | assert(BlockByRefDeclNo.count(VD) && |
| 4186 | "RewriteByRefString: ByRef decl missing"); |
Fariborz Jahanian | 1e8011e | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 4187 | if (def) |
| 4188 | ResultStr += "struct "; |
| 4189 | ResultStr += "__Block_byref_" + Name + |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4190 | "_" + utostr(BlockByRefDeclNo[VD]) ; |
| 4191 | } |
| 4192 | |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4193 | static bool HasLocalVariableExternalStorage(ValueDecl *VD) { |
| 4194 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 4195 | return (Var->isFunctionOrMethodVarDecl() && !Var->hasLocalStorage()); |
| 4196 | return false; |
| 4197 | } |
| 4198 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4199 | std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4200 | StringRef funcName, |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4201 | std::string Tag) { |
| 4202 | const FunctionType *AFT = CE->getFunctionType(); |
| 4203 | QualType RT = AFT->getResultType(); |
| 4204 | std::string StructRef = "struct " + Tag; |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 4205 | std::string S = "static " + RT.getAsString(Context->getPrintingPolicy()) + " __" + |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4206 | funcName.str() + "_" + "block_func_" + utostr(i); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 4207 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4208 | BlockDecl *BD = CE->getBlockDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4209 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4210 | if (isa<FunctionNoProtoType>(AFT)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4211 | // 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] | 4212 | // block (to reference imported block decl refs). |
| 4213 | S += "(" + StructRef + " *__cself)"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4214 | } else if (BD->param_empty()) { |
| 4215 | S += "(" + StructRef + " *__cself)"; |
| 4216 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4217 | const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4218 | assert(FT && "SynthesizeBlockFunc: No function proto"); |
| 4219 | S += '('; |
| 4220 | // first add the implicit argument. |
| 4221 | S += StructRef + " *__cself, "; |
| 4222 | std::string ParamStr; |
| 4223 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 4224 | E = BD->param_end(); AI != E; ++AI) { |
| 4225 | if (AI != BD->param_begin()) S += ", "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4226 | ParamStr = (*AI)->getNameAsString(); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4227 | QualType QT = (*AI)->getType(); |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 4228 | if (convertBlockPointerToFunctionPointer(QT)) |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 4229 | QT.getAsStringInternal(ParamStr, Context->getPrintingPolicy()); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4230 | else |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 4231 | QT.getAsStringInternal(ParamStr, Context->getPrintingPolicy()); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4232 | S += ParamStr; |
| 4233 | } |
| 4234 | if (FT->isVariadic()) { |
| 4235 | if (!BD->param_empty()) S += ", "; |
| 4236 | S += "..."; |
| 4237 | } |
| 4238 | S += ')'; |
| 4239 | } |
| 4240 | S += " {\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4241 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4242 | // Create local declarations to avoid rewriting all closure decl ref exprs. |
| 4243 | // First, emit a declaration for all "by ref" decls. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4244 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4245 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 4246 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4247 | std::string Name = (*I)->getNameAsString(); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4248 | std::string TypeString; |
| 4249 | RewriteByRefString(TypeString, Name, (*I)); |
| 4250 | TypeString += " *"; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4251 | Name = TypeString + Name; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4252 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4253 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4254 | // Next, emit a declaration for all "by copy" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4255 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4256 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 4257 | S += " "; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4258 | // Handle nested closure invocation. For example: |
| 4259 | // |
| 4260 | // void (^myImportedClosure)(void); |
| 4261 | // myImportedClosure = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4262 | // |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4263 | // void (^anotherClosure)(void); |
| 4264 | // anotherClosure = ^(void) { |
| 4265 | // myImportedClosure(); // import and invoke the closure |
| 4266 | // }; |
| 4267 | // |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 4268 | if (isTopLevelBlockPointerType((*I)->getType())) { |
| 4269 | RewriteBlockPointerTypeVariable(S, (*I)); |
| 4270 | S += " = ("; |
| 4271 | RewriteBlockPointerType(S, (*I)->getType()); |
| 4272 | S += ")"; |
| 4273 | S += "__cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; |
| 4274 | } |
| 4275 | else { |
Fariborz Jahanian | 210c248 | 2010-02-16 17:26:03 +0000 | [diff] [blame] | 4276 | std::string Name = (*I)->getNameAsString(); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4277 | QualType QT = (*I)->getType(); |
| 4278 | if (HasLocalVariableExternalStorage(*I)) |
| 4279 | QT = Context->getPointerType(QT); |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 4280 | QT.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 4281 | S += Name + " = __cself->" + |
| 4282 | (*I)->getNameAsString() + "; // bound by copy\n"; |
| 4283 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4284 | } |
| 4285 | std::string RewrittenStr = RewrittenBlockExprs[CE]; |
| 4286 | const char *cstr = RewrittenStr.c_str(); |
| 4287 | while (*cstr++ != '{') ; |
| 4288 | S += cstr; |
| 4289 | S += "\n"; |
| 4290 | return S; |
| 4291 | } |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 4292 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4293 | std::string RewriteObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4294 | StringRef funcName, |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4295 | std::string Tag) { |
| 4296 | std::string StructRef = "struct " + Tag; |
| 4297 | std::string S = "static void __"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4298 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4299 | S += funcName; |
| 4300 | S += "_block_copy_" + utostr(i); |
| 4301 | S += "(" + StructRef; |
| 4302 | S += "*dst, " + StructRef; |
| 4303 | S += "*src) {"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4304 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4305 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 4306 | ValueDecl *VD = (*I); |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 4307 | S += "_Block_object_assign((void*)&dst->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4308 | S += (*I)->getNameAsString(); |
Steve Naroff | 47a2422 | 2008-12-11 20:51:38 +0000 | [diff] [blame] | 4309 | S += ", (void*)src->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4310 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4311 | if (BlockByRefDeclsPtrSet.count((*I))) |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 4312 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 4313 | else if (VD->getType()->isBlockPointerType()) |
Fariborz Jahanian | 06433c6 | 2011-07-30 01:07:55 +0000 | [diff] [blame] | 4314 | S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);"; |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 4315 | else |
| 4316 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4317 | } |
Fariborz Jahanian | d25d1b5 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 4318 | S += "}\n"; |
| 4319 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4320 | S += "\nstatic void __"; |
| 4321 | S += funcName; |
| 4322 | S += "_block_dispose_" + utostr(i); |
| 4323 | S += "(" + StructRef; |
| 4324 | S += "*src) {"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4325 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4326 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 4327 | ValueDecl *VD = (*I); |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 4328 | S += "_Block_object_dispose((void*)src->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4329 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4330 | if (BlockByRefDeclsPtrSet.count((*I))) |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 4331 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 4332 | else if (VD->getType()->isBlockPointerType()) |
Fariborz Jahanian | 06433c6 | 2011-07-30 01:07:55 +0000 | [diff] [blame] | 4333 | S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);"; |
Fariborz Jahanian | a3f61ae | 2011-07-30 01:21:41 +0000 | [diff] [blame] | 4334 | else |
| 4335 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4336 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4337 | S += "}\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4338 | return S; |
| 4339 | } |
| 4340 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4341 | std::string RewriteObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 4342 | std::string Desc) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 4343 | std::string S = "\nstruct " + Tag; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4344 | std::string Constructor = " " + Tag; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4345 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4346 | S += " {\n struct __block_impl impl;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4347 | S += " struct " + Desc; |
| 4348 | S += "* Desc;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4349 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4350 | Constructor += "(void *fp, "; // Invoke function pointer. |
| 4351 | Constructor += "struct " + Desc; // Descriptor pointer. |
| 4352 | Constructor += " *desc"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4353 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4354 | if (BlockDeclRefs.size()) { |
| 4355 | // Output all "by copy" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4356 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4357 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 4358 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4359 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4360 | std::string ArgName = "_" + FieldName; |
| 4361 | // Handle nested closure invocation. For example: |
| 4362 | // |
| 4363 | // void (^myImportedBlock)(void); |
| 4364 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4365 | // |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4366 | // void (^anotherBlock)(void); |
| 4367 | // anotherBlock = ^(void) { |
| 4368 | // myImportedBlock(); // import and invoke the closure |
| 4369 | // }; |
| 4370 | // |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4371 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4372 | S += "struct __block_impl *"; |
| 4373 | Constructor += ", void *" + ArgName; |
| 4374 | } else { |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4375 | QualType QT = (*I)->getType(); |
| 4376 | if (HasLocalVariableExternalStorage(*I)) |
| 4377 | QT = Context->getPointerType(QT); |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 4378 | QT.getAsStringInternal(FieldName, Context->getPrintingPolicy()); |
| 4379 | QT.getAsStringInternal(ArgName, Context->getPrintingPolicy()); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4380 | Constructor += ", " + ArgName; |
| 4381 | } |
| 4382 | S += FieldName + ";\n"; |
| 4383 | } |
| 4384 | // Output all "by ref" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4385 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4386 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 4387 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4388 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4389 | std::string ArgName = "_" + FieldName; |
Fariborz Jahanian | 651ba52 | 2011-04-01 23:08:13 +0000 | [diff] [blame] | 4390 | { |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4391 | std::string TypeString; |
| 4392 | RewriteByRefString(TypeString, FieldName, (*I)); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4393 | TypeString += " *"; |
| 4394 | FieldName = TypeString + FieldName; |
| 4395 | ArgName = TypeString + ArgName; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4396 | Constructor += ", " + ArgName; |
| 4397 | } |
| 4398 | S += FieldName + "; // by ref\n"; |
| 4399 | } |
| 4400 | // Finish writing the constructor. |
Fariborz Jahanian | 20432ef | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 4401 | Constructor += ", int flags=0)"; |
| 4402 | // Initialize all "by copy" arguments. |
| 4403 | bool firsTime = true; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4404 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Fariborz Jahanian | 20432ef | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 4405 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 4406 | std::string Name = (*I)->getNameAsString(); |
| 4407 | if (firsTime) { |
| 4408 | Constructor += " : "; |
| 4409 | firsTime = false; |
| 4410 | } |
| 4411 | else |
| 4412 | Constructor += ", "; |
| 4413 | if (isTopLevelBlockPointerType((*I)->getType())) |
| 4414 | Constructor += Name + "((struct __block_impl *)_" + Name + ")"; |
| 4415 | else |
| 4416 | Constructor += Name + "(_" + Name + ")"; |
| 4417 | } |
| 4418 | // Initialize all "by ref" arguments. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4419 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Fariborz Jahanian | 20432ef | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 4420 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 4421 | std::string Name = (*I)->getNameAsString(); |
| 4422 | if (firsTime) { |
| 4423 | Constructor += " : "; |
| 4424 | firsTime = false; |
| 4425 | } |
| 4426 | else |
| 4427 | Constructor += ", "; |
Fariborz Jahanian | 651ba52 | 2011-04-01 23:08:13 +0000 | [diff] [blame] | 4428 | Constructor += Name + "(_" + Name + "->__forwarding)"; |
Fariborz Jahanian | 20432ef | 2010-07-28 23:27:30 +0000 | [diff] [blame] | 4429 | } |
| 4430 | |
| 4431 | Constructor += " {\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4432 | if (GlobalVarDecl) |
| 4433 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 4434 | else |
| 4435 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4436 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4437 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4438 | Constructor += " Desc = desc;\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4439 | } else { |
| 4440 | // Finish writing the constructor. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4441 | Constructor += ", int flags=0) {\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4442 | if (GlobalVarDecl) |
| 4443 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 4444 | else |
| 4445 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4446 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 4447 | Constructor += " Desc = desc;\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4448 | } |
| 4449 | Constructor += " "; |
| 4450 | Constructor += "}\n"; |
| 4451 | S += Constructor; |
| 4452 | S += "};\n"; |
| 4453 | return S; |
| 4454 | } |
| 4455 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4456 | std::string RewriteObjC::SynthesizeBlockDescriptor(std::string DescTag, |
| 4457 | std::string ImplTag, int i, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4458 | StringRef FunName, |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4459 | unsigned hasCopy) { |
| 4460 | std::string S = "\nstatic struct " + DescTag; |
| 4461 | |
| 4462 | S += " {\n unsigned long reserved;\n"; |
| 4463 | S += " unsigned long Block_size;\n"; |
| 4464 | if (hasCopy) { |
Fariborz Jahanian | 4fcc4fd | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 4465 | S += " void (*copy)(struct "; |
| 4466 | S += ImplTag; S += "*, struct "; |
| 4467 | S += ImplTag; S += "*);\n"; |
| 4468 | |
| 4469 | S += " void (*dispose)(struct "; |
| 4470 | S += ImplTag; S += "*);\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4471 | } |
| 4472 | S += "} "; |
| 4473 | |
| 4474 | S += DescTag + "_DATA = { 0, sizeof(struct "; |
| 4475 | S += ImplTag + ")"; |
| 4476 | if (hasCopy) { |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4477 | S += ", __" + FunName.str() + "_block_copy_" + utostr(i); |
| 4478 | S += ", __" + FunName.str() + "_block_dispose_" + utostr(i); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4479 | } |
| 4480 | S += "};\n"; |
| 4481 | return S; |
| 4482 | } |
| 4483 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4484 | void RewriteObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4485 | StringRef FunName) { |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 4486 | // Insert declaration for the function in which block literal is used. |
Fariborz Jahanian | bf07012 | 2010-01-15 18:14:52 +0000 | [diff] [blame] | 4487 | if (CurFunctionDeclToDeclareForBlock && !Blocks.empty()) |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 4488 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Fariborz Jahanian | 8611eb0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 4489 | bool RewriteSC = (GlobalVarDecl && |
| 4490 | !Blocks.empty() && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4491 | GlobalVarDecl->getStorageClass() == SC_Static && |
Fariborz Jahanian | 8611eb0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 4492 | GlobalVarDecl->getType().getCVRQualifiers()); |
| 4493 | if (RewriteSC) { |
| 4494 | std::string SC(" void __"); |
| 4495 | SC += GlobalVarDecl->getNameAsString(); |
| 4496 | SC += "() {}"; |
| 4497 | InsertText(FunLocStart, SC); |
| 4498 | } |
| 4499 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4500 | // Insert closures that were part of the function. |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4501 | for (unsigned i = 0, count=0; i < Blocks.size(); i++) { |
| 4502 | CollectBlockDeclRefInfo(Blocks[i]); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4503 | // Need to copy-in the inner copied-in variables not actually used in this |
| 4504 | // block. |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4505 | for (int j = 0; j < InnerDeclRefsCount[i]; j++) { |
| 4506 | BlockDeclRefExpr *Exp = InnerDeclRefs[count++]; |
| 4507 | ValueDecl *VD = Exp->getDecl(); |
| 4508 | BlockDeclRefs.push_back(Exp); |
| 4509 | if (!Exp->isByRef() && !BlockByCopyDeclsPtrSet.count(VD)) { |
| 4510 | BlockByCopyDeclsPtrSet.insert(VD); |
| 4511 | BlockByCopyDecls.push_back(VD); |
| 4512 | } |
| 4513 | if (Exp->isByRef() && !BlockByRefDeclsPtrSet.count(VD)) { |
| 4514 | BlockByRefDeclsPtrSet.insert(VD); |
| 4515 | BlockByRefDecls.push_back(VD); |
| 4516 | } |
Fariborz Jahanian | 92c8568 | 2010-10-05 18:05:06 +0000 | [diff] [blame] | 4517 | // imported objects in the inner blocks not used in the outer |
| 4518 | // blocks must be copied/disposed in the outer block as well. |
| 4519 | if (Exp->isByRef() || |
| 4520 | VD->getType()->isObjCObjectPointerType() || |
| 4521 | VD->getType()->isBlockPointerType()) |
| 4522 | ImportedBlockDecls.insert(VD); |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4523 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4524 | |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4525 | std::string ImplTag = "__" + FunName.str() + "_block_impl_" + utostr(i); |
| 4526 | std::string DescTag = "__" + FunName.str() + "_block_desc_" + utostr(i); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4527 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4528 | std::string CI = SynthesizeBlockImpl(Blocks[i], ImplTag, DescTag); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4529 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4530 | InsertText(FunLocStart, CI); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4531 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4532 | std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, ImplTag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4533 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4534 | InsertText(FunLocStart, CF); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4535 | |
| 4536 | if (ImportedBlockDecls.size()) { |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4537 | std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, ImplTag); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4538 | InsertText(FunLocStart, HF); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4539 | } |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4540 | std::string BD = SynthesizeBlockDescriptor(DescTag, ImplTag, i, FunName, |
| 4541 | ImportedBlockDecls.size() > 0); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4542 | InsertText(FunLocStart, BD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4543 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4544 | BlockDeclRefs.clear(); |
| 4545 | BlockByRefDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4546 | BlockByRefDeclsPtrSet.clear(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4547 | BlockByCopyDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4548 | BlockByCopyDeclsPtrSet.clear(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4549 | ImportedBlockDecls.clear(); |
| 4550 | } |
Fariborz Jahanian | 8611eb0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 4551 | if (RewriteSC) { |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 4552 | // Must insert any 'const/volatile/static here. Since it has been |
| 4553 | // removed as result of rewriting of block literals. |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 4554 | std::string SC; |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4555 | if (GlobalVarDecl->getStorageClass() == SC_Static) |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 4556 | SC = "static "; |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 4557 | if (GlobalVarDecl->getType().isConstQualified()) |
| 4558 | SC += "const "; |
| 4559 | if (GlobalVarDecl->getType().isVolatileQualified()) |
| 4560 | SC += "volatile "; |
Fariborz Jahanian | 8611eb0 | 2010-03-04 21:35:37 +0000 | [diff] [blame] | 4561 | if (GlobalVarDecl->getType().isRestrictQualified()) |
| 4562 | SC += "restrict "; |
| 4563 | InsertText(FunLocStart, SC); |
Fariborz Jahanian | 61b82e3 | 2010-03-04 18:54:29 +0000 | [diff] [blame] | 4564 | } |
| 4565 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4566 | Blocks.clear(); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4567 | InnerDeclRefsCount.clear(); |
| 4568 | InnerDeclRefs.clear(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4569 | RewrittenBlockExprs.clear(); |
| 4570 | } |
| 4571 | |
| 4572 | void RewriteObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { |
| 4573 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4574 | StringRef FuncName = FD->getName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4575 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4576 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 4577 | } |
| 4578 | |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 4579 | static void BuildUniqueMethodName(std::string &Name, |
| 4580 | ObjCMethodDecl *MD) { |
| 4581 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4582 | Name = IFace->getName(); |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 4583 | Name += "__" + MD->getSelector().getAsString(); |
| 4584 | // Convert colons to underscores. |
| 4585 | std::string::size_type loc = 0; |
| 4586 | while ((loc = Name.find(":", loc)) != std::string::npos) |
| 4587 | Name.replace(loc, 1, "_"); |
| 4588 | } |
| 4589 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4590 | void RewriteObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 4591 | //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n"); |
| 4592 | //SourceLocation FunLocStart = MD->getLocStart(); |
Fariborz Jahanian | 0e1c99a | 2010-01-29 01:55:49 +0000 | [diff] [blame] | 4593 | SourceLocation FunLocStart = MD->getLocStart(); |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 4594 | std::string FuncName; |
| 4595 | BuildUniqueMethodName(FuncName, MD); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4596 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4597 | } |
| 4598 | |
| 4599 | void RewriteObjC::GetBlockDeclRefExprs(Stmt *S) { |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 4600 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4601 | if (*CI) { |
| 4602 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 4603 | GetBlockDeclRefExprs(CBE->getBody()); |
| 4604 | else |
| 4605 | GetBlockDeclRefExprs(*CI); |
| 4606 | } |
| 4607 | // Handle specific things. |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4608 | if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4609 | // FIXME: Handle enums. |
| 4610 | if (!isa<FunctionDecl>(CDRE->getDecl())) |
| 4611 | BlockDeclRefs.push_back(CDRE); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4612 | } |
| 4613 | else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) |
| 4614 | if (HasLocalVariableExternalStorage(DRE->getDecl())) { |
| 4615 | BlockDeclRefExpr *BDRE = |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4616 | new (Context)BlockDeclRefExpr(cast<VarDecl>(DRE->getDecl()), |
| 4617 | DRE->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4618 | VK_LValue, DRE->getLocation(), false); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4619 | BlockDeclRefs.push_back(BDRE); |
| 4620 | } |
| 4621 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4622 | return; |
| 4623 | } |
| 4624 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4625 | void RewriteObjC::GetInnerBlockDeclRefExprs(Stmt *S, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4626 | SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs, |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4627 | llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts) { |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 4628 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4629 | if (*CI) { |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4630 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) { |
| 4631 | InnerContexts.insert(cast<DeclContext>(CBE->getBlockDecl())); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4632 | GetInnerBlockDeclRefExprs(CBE->getBody(), |
| 4633 | InnerBlockDeclRefs, |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4634 | InnerContexts); |
| 4635 | } |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4636 | else |
| 4637 | GetInnerBlockDeclRefExprs(*CI, |
| 4638 | InnerBlockDeclRefs, |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4639 | InnerContexts); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4640 | |
| 4641 | } |
| 4642 | // Handle specific things. |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4643 | if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4644 | if (!isa<FunctionDecl>(CDRE->getDecl()) && |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4645 | !InnerContexts.count(CDRE->getDecl()->getDeclContext())) |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4646 | InnerBlockDeclRefs.push_back(CDRE); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4647 | } |
| 4648 | else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 4649 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) |
| 4650 | if (Var->isFunctionOrMethodVarDecl()) |
| 4651 | ImportedLocalExternalDecls.insert(Var); |
| 4652 | } |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4653 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4654 | return; |
| 4655 | } |
| 4656 | |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4657 | /// convertFunctionTypeOfBlocks - This routine converts a function type |
| 4658 | /// 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] | 4659 | /// might be block pointers to an equivalent function type replacing |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4660 | /// all block pointers to function pointers. |
| 4661 | QualType RewriteObjC::convertFunctionTypeOfBlocks(const FunctionType *FT) { |
| 4662 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
| 4663 | // FTP will be null for closures that don't take arguments. |
| 4664 | // Generate a funky cast. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4665 | SmallVector<QualType, 8> ArgTypes; |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4666 | QualType Res = FT->getResultType(); |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 4667 | bool HasBlockType = convertBlockPointerToFunctionPointer(Res); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4668 | |
| 4669 | if (FTP) { |
| 4670 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
| 4671 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 4672 | QualType t = *I; |
| 4673 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 4fc8453 | 2010-05-25 17:12:52 +0000 | [diff] [blame] | 4674 | if (convertBlockPointerToFunctionPointer(t)) |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4675 | HasBlockType = true; |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4676 | ArgTypes.push_back(t); |
| 4677 | } |
| 4678 | } |
| 4679 | QualType FuncType; |
| 4680 | // FIXME. Does this work if block takes no argument but has a return type |
| 4681 | // which is of block type? |
| 4682 | if (HasBlockType) |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 4683 | FuncType = getSimpleFunctionType(Res, &ArgTypes[0], ArgTypes.size()); |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 4684 | else FuncType = QualType(FT, 0); |
| 4685 | return FuncType; |
| 4686 | } |
| 4687 | |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4688 | Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp, const Expr *BlockExp) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4689 | // Navigate to relevant type information. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4690 | const BlockPointerType *CPT = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4691 | |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4692 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BlockExp)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4693 | CPT = DRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4694 | } else if (const BlockDeclRefExpr *CDRE = |
| 4695 | dyn_cast<BlockDeclRefExpr>(BlockExp)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4696 | CPT = CDRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4697 | } else if (const MemberExpr *MExpr = dyn_cast<MemberExpr>(BlockExp)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4698 | CPT = MExpr->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4699 | } |
| 4700 | else if (const ParenExpr *PRE = dyn_cast<ParenExpr>(BlockExp)) { |
| 4701 | return SynthesizeBlockCall(Exp, PRE->getSubExpr()); |
| 4702 | } |
| 4703 | else if (const ImplicitCastExpr *IEXPR = dyn_cast<ImplicitCastExpr>(BlockExp)) |
| 4704 | CPT = IEXPR->getType()->getAs<BlockPointerType>(); |
| 4705 | else if (const ConditionalOperator *CEXPR = |
| 4706 | dyn_cast<ConditionalOperator>(BlockExp)) { |
| 4707 | Expr *LHSExp = CEXPR->getLHS(); |
| 4708 | Stmt *LHSStmt = SynthesizeBlockCall(Exp, LHSExp); |
| 4709 | Expr *RHSExp = CEXPR->getRHS(); |
| 4710 | Stmt *RHSStmt = SynthesizeBlockCall(Exp, RHSExp); |
| 4711 | Expr *CONDExp = CEXPR->getCond(); |
| 4712 | ConditionalOperator *CondExpr = |
| 4713 | new (Context) ConditionalOperator(CONDExp, |
| 4714 | SourceLocation(), cast<Expr>(LHSStmt), |
Fariborz Jahanian | f9b949f | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 4715 | SourceLocation(), cast<Expr>(RHSStmt), |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4716 | Exp->getType(), VK_RValue, OK_Ordinary); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4717 | return CondExpr; |
Fariborz Jahanian | e24b22b | 2009-12-18 01:15:21 +0000 | [diff] [blame] | 4718 | } else if (const ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(BlockExp)) { |
| 4719 | CPT = IRE->getType()->getAs<BlockPointerType>(); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4720 | } else if (const PseudoObjectExpr *POE |
| 4721 | = dyn_cast<PseudoObjectExpr>(BlockExp)) { |
| 4722 | CPT = POE->getType()->castAs<BlockPointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4723 | } else { |
| 4724 | assert(1 && "RewriteBlockClass: Bad type"); |
| 4725 | } |
| 4726 | assert(CPT && "RewriteBlockClass: Bad type"); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4727 | const FunctionType *FT = CPT->getPointeeType()->getAs<FunctionType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4728 | assert(FT && "RewriteBlockClass: Bad type"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4729 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4730 | // FTP will be null for closures that don't take arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4731 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4732 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4733 | SourceLocation(), SourceLocation(), |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4734 | &Context->Idents.get("__block_impl")); |
| 4735 | QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD)); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4736 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4737 | // Generate a funky cast. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4738 | SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4739 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4740 | // Push the block argument type. |
| 4741 | ArgTypes.push_back(PtrBlock); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4742 | if (FTP) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4743 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4744 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 4745 | QualType t = *I; |
| 4746 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 8188e5f | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 4747 | if (!convertBlockPointerToFunctionPointer(t)) |
| 4748 | convertToUnqualifiedObjCType(t); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4749 | ArgTypes.push_back(t); |
| 4750 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4751 | } |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4752 | // Now do the pointer to function cast. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 4753 | QualType PtrToFuncCastType |
| 4754 | = getSimpleFunctionType(Exp->getType(), &ArgTypes[0], ArgTypes.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4755 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4756 | PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4757 | |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4758 | CastExpr *BlkCast = NoTypeInfoCStyleCastExpr(Context, PtrBlock, |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4759 | CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4760 | const_cast<Expr*>(BlockExp)); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4761 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4762 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 4763 | BlkCast); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4764 | //PE->dump(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4765 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4766 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4767 | SourceLocation(), |
| 4768 | &Context->Idents.get("FuncPtr"), |
| 4769 | Context->VoidPtrTy, 0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4770 | /*BitWidth=*/0, /*Mutable=*/true, |
| 4771 | /*HasInit=*/false); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4772 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4773 | FD->getType(), VK_LValue, |
| 4774 | OK_Ordinary); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4775 | |
Fariborz Jahanian | 8188e5f | 2010-11-05 18:34:46 +0000 | [diff] [blame] | 4776 | |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4777 | CastExpr *FunkCast = NoTypeInfoCStyleCastExpr(Context, PtrToFuncCastType, |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 4778 | CK_BitCast, ME); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4779 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4780 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4781 | SmallVector<Expr*, 8> BlkExprs; |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4782 | // Add the implicit argument. |
| 4783 | BlkExprs.push_back(BlkCast); |
| 4784 | // Add the user arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4785 | for (CallExpr::arg_iterator I = Exp->arg_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4786 | E = Exp->arg_end(); I != E; ++I) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4787 | BlkExprs.push_back(*I); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4788 | } |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 4789 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &BlkExprs[0], |
| 4790 | BlkExprs.size(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4791 | Exp->getType(), VK_RValue, |
| 4792 | SourceLocation()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4793 | return CE; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4794 | } |
| 4795 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4796 | // We need to return the rewritten expression to handle cases where the |
| 4797 | // BlockDeclRefExpr is embedded in another expression being rewritten. |
| 4798 | // For example: |
| 4799 | // |
| 4800 | // int main() { |
| 4801 | // __block Foo *f; |
| 4802 | // __block int i; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4803 | // |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4804 | // void (^myblock)() = ^() { |
| 4805 | // [f test]; // f is a BlockDeclRefExpr embedded in a message (which is being rewritten). |
| 4806 | // i = 77; |
| 4807 | // }; |
| 4808 | //} |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4809 | Stmt *RewriteObjC::RewriteBlockDeclRefExpr(Expr *DeclRefExp) { |
Fariborz Jahanian | bbf37e2 | 2009-12-23 19:26:34 +0000 | [diff] [blame] | 4810 | // Rewrite the byref variable into BYREFVAR->__forwarding->BYREFVAR |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4811 | // for each DeclRefExp where BYREFVAR is name of the variable. |
| 4812 | ValueDecl *VD; |
| 4813 | bool isArrow = true; |
| 4814 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(DeclRefExp)) |
| 4815 | VD = BDRE->getDecl(); |
| 4816 | else { |
| 4817 | VD = cast<DeclRefExpr>(DeclRefExp)->getDecl(); |
| 4818 | isArrow = false; |
| 4819 | } |
| 4820 | |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4821 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4822 | SourceLocation(), |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4823 | &Context->Idents.get("__forwarding"), |
| 4824 | Context->VoidPtrTy, 0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4825 | /*BitWidth=*/0, /*Mutable=*/true, |
| 4826 | /*HasInit=*/false); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4827 | MemberExpr *ME = new (Context) MemberExpr(DeclRefExp, isArrow, |
| 4828 | FD, SourceLocation(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4829 | FD->getType(), VK_LValue, |
| 4830 | OK_Ordinary); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4831 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4832 | StringRef Name = VD->getName(); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4833 | FD = FieldDecl::Create(*Context, 0, SourceLocation(), SourceLocation(), |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4834 | &Context->Idents.get(Name), |
| 4835 | Context->VoidPtrTy, 0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4836 | /*BitWidth=*/0, /*Mutable=*/true, |
| 4837 | /*HasInit=*/false); |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4838 | ME = new (Context) MemberExpr(ME, true, FD, SourceLocation(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4839 | DeclRefExp->getType(), VK_LValue, OK_Ordinary); |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4840 | |
| 4841 | |
| 4842 | |
Steve Naroff | df8570d | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 4843 | // Need parens to enforce precedence. |
Fariborz Jahanian | 380ee50 | 2011-04-01 19:19:28 +0000 | [diff] [blame] | 4844 | ParenExpr *PE = new (Context) ParenExpr(DeclRefExp->getExprLoc(), |
| 4845 | DeclRefExp->getExprLoc(), |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4846 | ME); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4847 | ReplaceStmt(DeclRefExp, PE); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4848 | return PE; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4849 | } |
| 4850 | |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4851 | // Rewrites the imported local variable V with external storage |
| 4852 | // (static, extern, etc.) as *V |
| 4853 | // |
| 4854 | Stmt *RewriteObjC::RewriteLocalVariableExternalStorage(DeclRefExpr *DRE) { |
| 4855 | ValueDecl *VD = DRE->getDecl(); |
| 4856 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 4857 | if (!ImportedLocalExternalDecls.count(Var)) |
| 4858 | return DRE; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4859 | Expr *Exp = new (Context) UnaryOperator(DRE, UO_Deref, DRE->getType(), |
| 4860 | VK_LValue, OK_Ordinary, |
| 4861 | DRE->getLocation()); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 4862 | // Need parens to enforce precedence. |
| 4863 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 4864 | Exp); |
| 4865 | ReplaceStmt(DRE, PE); |
| 4866 | return PE; |
| 4867 | } |
| 4868 | |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4869 | void RewriteObjC::RewriteCastExpr(CStyleCastExpr *CE) { |
| 4870 | SourceLocation LocStart = CE->getLParenLoc(); |
| 4871 | SourceLocation LocEnd = CE->getRParenLoc(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4872 | |
| 4873 | // Need to avoid trying to rewrite synthesized casts. |
| 4874 | if (LocStart.isInvalid()) |
| 4875 | return; |
Steve Naroff | 8f6ce57 | 2008-11-03 11:20:24 +0000 | [diff] [blame] | 4876 | // Need to avoid trying to rewrite casts contained in macros. |
| 4877 | if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) |
| 4878 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4879 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4880 | const char *startBuf = SM->getCharacterData(LocStart); |
| 4881 | const char *endBuf = SM->getCharacterData(LocEnd); |
Fariborz Jahanian | 1d4fca2 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4882 | QualType QT = CE->getType(); |
| 4883 | const Type* TypePtr = QT->getAs<Type>(); |
| 4884 | if (isa<TypeOfExprType>(TypePtr)) { |
| 4885 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 4886 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 4887 | std::string TypeAsString = "("; |
Fariborz Jahanian | afad76f | 2010-02-18 01:20:22 +0000 | [diff] [blame] | 4888 | RewriteBlockPointerType(TypeAsString, QT); |
Fariborz Jahanian | 1d4fca2 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4889 | TypeAsString += ")"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4890 | ReplaceText(LocStart, endBuf-startBuf+1, TypeAsString); |
Fariborz Jahanian | 1d4fca2 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4891 | return; |
| 4892 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4893 | // advance the location to startArgList. |
| 4894 | const char *argPtr = startBuf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4895 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4896 | while (*argPtr++ && (argPtr < endBuf)) { |
| 4897 | switch (*argPtr) { |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4898 | case '^': |
| 4899 | // Replace the '^' with '*'. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4900 | LocStart = LocStart.getLocWithOffset(argPtr-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4901 | ReplaceText(LocStart, 1, "*"); |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4902 | break; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4903 | } |
| 4904 | } |
| 4905 | return; |
| 4906 | } |
| 4907 | |
| 4908 | void RewriteObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { |
| 4909 | SourceLocation DeclLoc = FD->getLocation(); |
| 4910 | unsigned parenCount = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4911 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4912 | // We have 1 or more arguments that have closure pointers. |
| 4913 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4914 | const char *startArgList = strchr(startBuf, '('); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4915 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4916 | assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4917 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4918 | parenCount++; |
| 4919 | // advance the location to startArgList. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4920 | DeclLoc = DeclLoc.getLocWithOffset(startArgList-startBuf); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4921 | assert((DeclLoc.isValid()) && "Invalid DeclLoc"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4922 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4923 | const char *argPtr = startArgList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4924 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4925 | while (*argPtr++ && parenCount) { |
| 4926 | switch (*argPtr) { |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4927 | case '^': |
| 4928 | // Replace the '^' with '*'. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4929 | DeclLoc = DeclLoc.getLocWithOffset(argPtr-startArgList); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4930 | ReplaceText(DeclLoc, 1, "*"); |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4931 | break; |
| 4932 | case '(': |
| 4933 | parenCount++; |
| 4934 | break; |
| 4935 | case ')': |
| 4936 | parenCount--; |
| 4937 | break; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4938 | } |
| 4939 | } |
| 4940 | return; |
| 4941 | } |
| 4942 | |
| 4943 | bool RewriteObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4944 | const FunctionProtoType *FTP; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4945 | const PointerType *PT = QT->getAs<PointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4946 | if (PT) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4947 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4948 | } else { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4949 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4950 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4951 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4952 | } |
| 4953 | if (FTP) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4954 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4955 | E = FTP->arg_type_end(); I != E; ++I) |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4956 | if (isTopLevelBlockPointerType(*I)) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4957 | return true; |
| 4958 | } |
| 4959 | return false; |
| 4960 | } |
| 4961 | |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4962 | bool RewriteObjC::PointerTypeTakesAnyObjCQualifiedType(QualType QT) { |
| 4963 | const FunctionProtoType *FTP; |
| 4964 | const PointerType *PT = QT->getAs<PointerType>(); |
| 4965 | if (PT) { |
| 4966 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4967 | } else { |
| 4968 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
| 4969 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
| 4970 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4971 | } |
| 4972 | if (FTP) { |
| 4973 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Fariborz Jahanian | 06de2cf | 2010-11-03 23:50:34 +0000 | [diff] [blame] | 4974 | E = FTP->arg_type_end(); I != E; ++I) { |
| 4975 | if ((*I)->isObjCQualifiedIdType()) |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4976 | return true; |
Fariborz Jahanian | 06de2cf | 2010-11-03 23:50:34 +0000 | [diff] [blame] | 4977 | if ((*I)->isObjCObjectPointerType() && |
| 4978 | (*I)->getPointeeType()->isObjCQualifiedInterfaceType()) |
| 4979 | return true; |
| 4980 | } |
| 4981 | |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 4982 | } |
| 4983 | return false; |
| 4984 | } |
| 4985 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4986 | void RewriteObjC::GetExtentOfArgList(const char *Name, const char *&LParen, |
| 4987 | const char *&RParen) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4988 | const char *argPtr = strchr(Name, '('); |
| 4989 | assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4990 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4991 | LParen = argPtr; // output the start. |
| 4992 | argPtr++; // skip past the left paren. |
| 4993 | unsigned parenCount = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4994 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4995 | while (*argPtr && parenCount) { |
| 4996 | switch (*argPtr) { |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4997 | case '(': parenCount++; break; |
| 4998 | case ')': parenCount--; break; |
| 4999 | default: break; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5000 | } |
| 5001 | if (parenCount) argPtr++; |
| 5002 | } |
| 5003 | assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); |
| 5004 | RParen = argPtr; // output the end |
| 5005 | } |
| 5006 | |
| 5007 | void RewriteObjC::RewriteBlockPointerDecl(NamedDecl *ND) { |
| 5008 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 5009 | RewriteBlockPointerFunctionArgs(FD); |
| 5010 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5011 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5012 | // Handle Variables and Typedefs. |
| 5013 | SourceLocation DeclLoc = ND->getLocation(); |
| 5014 | QualType DeclT; |
| 5015 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 5016 | DeclT = VD->getType(); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5017 | else if (TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(ND)) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5018 | DeclT = TDD->getUnderlyingType(); |
| 5019 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) |
| 5020 | DeclT = FD->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5021 | else |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5022 | llvm_unreachable("RewriteBlockPointerDecl(): Decl type not yet handled"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5023 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5024 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 5025 | const char *endBuf = startBuf; |
| 5026 | // scan backward (from the decl location) for the end of the previous decl. |
| 5027 | while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) |
| 5028 | startBuf--; |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 5029 | SourceLocation Start = DeclLoc.getLocWithOffset(startBuf-endBuf); |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 5030 | std::string buf; |
| 5031 | unsigned OrigLength=0; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5032 | // *startBuf != '^' if we are dealing with a pointer to function that |
| 5033 | // may take block argument types (which will be handled below). |
| 5034 | if (*startBuf == '^') { |
| 5035 | // Replace the '^' with '*', computing a negative offset. |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 5036 | buf = '*'; |
| 5037 | startBuf++; |
| 5038 | OrigLength++; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5039 | } |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 5040 | while (*startBuf != ')') { |
| 5041 | buf += *startBuf; |
| 5042 | startBuf++; |
| 5043 | OrigLength++; |
| 5044 | } |
| 5045 | buf += ')'; |
| 5046 | OrigLength++; |
| 5047 | |
| 5048 | if (PointerTypeTakesAnyBlockArguments(DeclT) || |
| 5049 | PointerTypeTakesAnyObjCQualifiedType(DeclT)) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5050 | // Replace the '^' with '*' for arguments. |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 5051 | // Replace id<P> with id/*<>*/ |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5052 | DeclLoc = ND->getLocation(); |
| 5053 | startBuf = SM->getCharacterData(DeclLoc); |
| 5054 | const char *argListBegin, *argListEnd; |
| 5055 | GetExtentOfArgList(startBuf, argListBegin, argListEnd); |
| 5056 | while (argListBegin < argListEnd) { |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 5057 | if (*argListBegin == '^') |
| 5058 | buf += '*'; |
| 5059 | else if (*argListBegin == '<') { |
| 5060 | buf += "/*"; |
| 5061 | buf += *argListBegin++; |
| 5062 | OrigLength++;; |
| 5063 | while (*argListBegin != '>') { |
| 5064 | buf += *argListBegin++; |
| 5065 | OrigLength++; |
| 5066 | } |
| 5067 | buf += *argListBegin; |
| 5068 | buf += "*/"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5069 | } |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 5070 | else |
| 5071 | buf += *argListBegin; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5072 | argListBegin++; |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 5073 | OrigLength++; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5074 | } |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 5075 | buf += ')'; |
| 5076 | OrigLength++; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5077 | } |
Fariborz Jahanian | e985d01 | 2010-11-03 23:29:24 +0000 | [diff] [blame] | 5078 | ReplaceText(Start, OrigLength, buf); |
| 5079 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5080 | return; |
| 5081 | } |
| 5082 | |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5083 | |
| 5084 | /// SynthesizeByrefCopyDestroyHelper - This routine synthesizes: |
| 5085 | /// void __Block_byref_id_object_copy(struct Block_byref_id_object *dst, |
| 5086 | /// struct Block_byref_id_object *src) { |
| 5087 | /// _Block_object_assign (&_dest->object, _src->object, |
| 5088 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 5089 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 5090 | /// _Block_object_assign(&_dest->object, _src->object, |
| 5091 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 5092 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 5093 | /// } |
| 5094 | /// And: |
| 5095 | /// void __Block_byref_id_object_dispose(struct Block_byref_id_object *_src) { |
| 5096 | /// _Block_object_dispose(_src->object, |
| 5097 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 5098 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 5099 | /// _Block_object_dispose(_src->object, |
| 5100 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 5101 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 5102 | /// } |
| 5103 | |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5104 | std::string RewriteObjC::SynthesizeByrefCopyDestroyHelper(VarDecl *VD, |
| 5105 | int flag) { |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5106 | std::string S; |
Benjamin Kramer | 1211a71 | 2010-01-10 19:57:50 +0000 | [diff] [blame] | 5107 | if (CopyDestroyCache.count(flag)) |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5108 | return S; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5109 | CopyDestroyCache.insert(flag); |
| 5110 | S = "static void __Block_byref_id_object_copy_"; |
| 5111 | S += utostr(flag); |
| 5112 | S += "(void *dst, void *src) {\n"; |
| 5113 | |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5114 | // offset into the object pointer is computed as: |
| 5115 | // void * + void* + int + int + void* + void * |
| 5116 | unsigned IntSize = |
| 5117 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 5118 | unsigned VoidPtrSize = |
| 5119 | static_cast<unsigned>(Context->getTypeSize(Context->VoidPtrTy)); |
| 5120 | |
Ken Dyck | 0c4e5d6 | 2011-04-30 16:08:27 +0000 | [diff] [blame] | 5121 | unsigned offset = (VoidPtrSize*4 + IntSize + IntSize)/Context->getCharWidth(); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5122 | S += " _Block_object_assign((char*)dst + "; |
| 5123 | S += utostr(offset); |
| 5124 | S += ", *(void * *) ((char*)src + "; |
| 5125 | S += utostr(offset); |
| 5126 | S += "), "; |
| 5127 | S += utostr(flag); |
| 5128 | S += ");\n}\n"; |
| 5129 | |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5130 | S += "static void __Block_byref_id_object_dispose_"; |
| 5131 | S += utostr(flag); |
| 5132 | S += "(void *src) {\n"; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5133 | S += " _Block_object_dispose(*(void * *) ((char*)src + "; |
| 5134 | S += utostr(offset); |
| 5135 | S += "), "; |
| 5136 | S += utostr(flag); |
| 5137 | S += ");\n}\n"; |
| 5138 | return S; |
| 5139 | } |
| 5140 | |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5141 | /// RewriteByRefVar - For each __block typex ND variable this routine transforms |
| 5142 | /// the declaration into: |
| 5143 | /// struct __Block_byref_ND { |
| 5144 | /// void *__isa; // NULL for everything except __weak pointers |
| 5145 | /// struct __Block_byref_ND *__forwarding; |
| 5146 | /// int32_t __flags; |
| 5147 | /// int32_t __size; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5148 | /// void *__Block_byref_id_object_copy; // If variable is __block ObjC object |
| 5149 | /// void *__Block_byref_id_object_dispose; // If variable is __block ObjC object |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5150 | /// typex ND; |
| 5151 | /// }; |
| 5152 | /// |
| 5153 | /// It then replaces declaration of ND variable with: |
| 5154 | /// struct __Block_byref_ND ND = {__isa=0B, __forwarding=&ND, __flags=some_flag, |
| 5155 | /// __size=sizeof(struct __Block_byref_ND), |
| 5156 | /// ND=initializer-if-any}; |
| 5157 | /// |
| 5158 | /// |
| 5159 | void RewriteObjC::RewriteByRefVar(VarDecl *ND) { |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 5160 | // Insert declaration for the function in which block literal is |
| 5161 | // used. |
| 5162 | if (CurFunctionDeclToDeclareForBlock) |
| 5163 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 5164 | int flag = 0; |
| 5165 | int isa = 0; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5166 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
Fariborz Jahanian | d64a4f4 | 2010-02-26 22:49:11 +0000 | [diff] [blame] | 5167 | if (DeclLoc.isInvalid()) |
| 5168 | // If type location is missing, it is because of missing type (a warning). |
| 5169 | // Use variable's location which is good for this case. |
| 5170 | DeclLoc = ND->getLocation(); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5171 | const char *startBuf = SM->getCharacterData(DeclLoc); |
Fariborz Jahanian | 6f0a0a9 | 2009-12-30 20:38:08 +0000 | [diff] [blame] | 5172 | SourceLocation X = ND->getLocEnd(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 5173 | X = SM->getExpansionLoc(X); |
Fariborz Jahanian | 6f0a0a9 | 2009-12-30 20:38:08 +0000 | [diff] [blame] | 5174 | const char *endBuf = SM->getCharacterData(X); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5175 | std::string Name(ND->getNameAsString()); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5176 | std::string ByrefType; |
Fariborz Jahanian | 1e8011e | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 5177 | RewriteByRefString(ByrefType, Name, ND, true); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5178 | ByrefType += " {\n"; |
| 5179 | ByrefType += " void *__isa;\n"; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5180 | RewriteByRefString(ByrefType, Name, ND); |
| 5181 | ByrefType += " *__forwarding;\n"; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5182 | ByrefType += " int __flags;\n"; |
| 5183 | ByrefType += " int __size;\n"; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5184 | // Add void *__Block_byref_id_object_copy; |
| 5185 | // void *__Block_byref_id_object_dispose; if needed. |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5186 | QualType Ty = ND->getType(); |
| 5187 | bool HasCopyAndDispose = Context->BlockRequiresCopying(Ty); |
| 5188 | if (HasCopyAndDispose) { |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5189 | ByrefType += " void (*__Block_byref_id_object_copy)(void*, void*);\n"; |
| 5190 | ByrefType += " void (*__Block_byref_id_object_dispose)(void*);\n"; |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5191 | } |
Fariborz Jahanian | 822ac87 | 2011-03-31 22:49:32 +0000 | [diff] [blame] | 5192 | |
| 5193 | QualType T = Ty; |
| 5194 | (void)convertBlockPointerToFunctionPointer(T); |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 5195 | T.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Fariborz Jahanian | 822ac87 | 2011-03-31 22:49:32 +0000 | [diff] [blame] | 5196 | |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5197 | ByrefType += " " + Name + ";\n"; |
| 5198 | ByrefType += "};\n"; |
| 5199 | // Insert this type in global scope. It is needed by helper function. |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 5200 | SourceLocation FunLocStart; |
| 5201 | if (CurFunctionDef) |
| 5202 | FunLocStart = CurFunctionDef->getTypeSpecStartLoc(); |
| 5203 | else { |
| 5204 | assert(CurMethodDef && "RewriteByRefVar - CurMethodDef is null"); |
| 5205 | FunLocStart = CurMethodDef->getLocStart(); |
| 5206 | } |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5207 | InsertText(FunLocStart, ByrefType); |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 5208 | if (Ty.isObjCGCWeak()) { |
| 5209 | flag |= BLOCK_FIELD_IS_WEAK; |
| 5210 | isa = 1; |
| 5211 | } |
| 5212 | |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5213 | if (HasCopyAndDispose) { |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5214 | flag = BLOCK_BYREF_CALLER; |
| 5215 | QualType Ty = ND->getType(); |
| 5216 | // FIXME. Handle __weak variable (BLOCK_FIELD_IS_WEAK) as well. |
| 5217 | if (Ty->isBlockPointerType()) |
| 5218 | flag |= BLOCK_FIELD_IS_BLOCK; |
| 5219 | else |
| 5220 | flag |= BLOCK_FIELD_IS_OBJECT; |
| 5221 | std::string HF = SynthesizeByrefCopyDestroyHelper(ND, flag); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5222 | if (!HF.empty()) |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5223 | InsertText(FunLocStart, HF); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5224 | } |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5225 | |
| 5226 | // struct __Block_byref_ND ND = |
| 5227 | // {0, &ND, some_flag, __size=sizeof(struct __Block_byref_ND), |
| 5228 | // initializer-if-any}; |
| 5229 | bool hasInit = (ND->getInit() != 0); |
Fariborz Jahanian | e1f84f8 | 2010-01-05 18:15:57 +0000 | [diff] [blame] | 5230 | unsigned flags = 0; |
| 5231 | if (HasCopyAndDispose) |
| 5232 | flags |= BLOCK_HAS_COPY_DISPOSE; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5233 | Name = ND->getNameAsString(); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5234 | ByrefType.clear(); |
| 5235 | RewriteByRefString(ByrefType, Name, ND); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5236 | std::string ForwardingCastType("("); |
| 5237 | ForwardingCastType += ByrefType + " *)"; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5238 | if (!hasInit) { |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 5239 | ByrefType += " " + Name + " = {(void*)"; |
| 5240 | ByrefType += utostr(isa); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5241 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5242 | ByrefType += utostr(flags); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5243 | ByrefType += ", "; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5244 | ByrefType += "sizeof("; |
| 5245 | RewriteByRefString(ByrefType, Name, ND); |
| 5246 | ByrefType += ")"; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5247 | if (HasCopyAndDispose) { |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5248 | ByrefType += ", __Block_byref_id_object_copy_"; |
| 5249 | ByrefType += utostr(flag); |
| 5250 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 5251 | ByrefType += utostr(flag); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5252 | } |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5253 | ByrefType += "};\n"; |
Fariborz Jahanian | 822ac87 | 2011-03-31 22:49:32 +0000 | [diff] [blame] | 5254 | unsigned nameSize = Name.size(); |
| 5255 | // for block or function pointer declaration. Name is aleady |
| 5256 | // part of the declaration. |
| 5257 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) |
| 5258 | nameSize = 1; |
| 5259 | ReplaceText(DeclLoc, endBuf-startBuf+nameSize, ByrefType); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5260 | } |
| 5261 | else { |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 5262 | SourceLocation startLoc; |
| 5263 | Expr *E = ND->getInit(); |
| 5264 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 5265 | startLoc = ECE->getLParenLoc(); |
| 5266 | else |
| 5267 | startLoc = E->getLocStart(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 5268 | startLoc = SM->getExpansionLoc(startLoc); |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 5269 | endBuf = SM->getCharacterData(startLoc); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5270 | ByrefType += " " + Name; |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 5271 | ByrefType += " = {(void*)"; |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 5272 | ByrefType += utostr(isa); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5273 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5274 | ByrefType += utostr(flags); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5275 | ByrefType += ", "; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5276 | ByrefType += "sizeof("; |
| 5277 | RewriteByRefString(ByrefType, Name, ND); |
| 5278 | ByrefType += "), "; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5279 | if (HasCopyAndDispose) { |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 5280 | ByrefType += "__Block_byref_id_object_copy_"; |
| 5281 | ByrefType += utostr(flag); |
| 5282 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 5283 | ByrefType += utostr(flag); |
| 5284 | ByrefType += ", "; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 5285 | } |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5286 | ReplaceText(DeclLoc, endBuf-startBuf, ByrefType); |
Steve Naroff | c5143c5 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 5287 | |
| 5288 | // Complete the newly synthesized compound expression by inserting a right |
| 5289 | // curly brace before the end of the declaration. |
| 5290 | // FIXME: This approach avoids rewriting the initializer expression. It |
| 5291 | // also assumes there is only one declarator. For example, the following |
| 5292 | // isn't currently supported by this routine (in general): |
| 5293 | // |
| 5294 | // double __block BYREFVAR = 1.34, BYREFVAR2 = 1.37; |
| 5295 | // |
Fariborz Jahanian | 5f371ee | 2010-07-21 17:36:39 +0000 | [diff] [blame] | 5296 | const char *startInitializerBuf = SM->getCharacterData(startLoc); |
| 5297 | const char *semiBuf = strchr(startInitializerBuf, ';'); |
Steve Naroff | c5143c5 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 5298 | assert((*semiBuf == ';') && "RewriteByRefVar: can't find ';'"); |
| 5299 | SourceLocation semiLoc = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 5300 | startLoc.getLocWithOffset(semiBuf-startInitializerBuf); |
Steve Naroff | c5143c5 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 5301 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5302 | InsertText(semiLoc, "}"); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5303 | } |
Fariborz Jahanian | 1be6b46 | 2009-12-22 00:48:54 +0000 | [diff] [blame] | 5304 | return; |
| 5305 | } |
| 5306 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5307 | void RewriteObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5308 | // Add initializers for any closure decl refs. |
| 5309 | GetBlockDeclRefExprs(Exp->getBody()); |
| 5310 | if (BlockDeclRefs.size()) { |
| 5311 | // Unique all "by copy" declarations. |
| 5312 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5313 | if (!BlockDeclRefs[i]->isByRef()) { |
| 5314 | if (!BlockByCopyDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 5315 | BlockByCopyDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 5316 | BlockByCopyDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 5317 | } |
| 5318 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5319 | // Unique all "by ref" declarations. |
| 5320 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 5321 | if (BlockDeclRefs[i]->isByRef()) { |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5322 | if (!BlockByRefDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 5323 | BlockByRefDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 5324 | BlockByRefDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 5325 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5326 | } |
| 5327 | // Find any imported blocks...they will need special attention. |
| 5328 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
Fariborz Jahanian | 4fcc4fd | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 5329 | if (BlockDeclRefs[i]->isByRef() || |
| 5330 | BlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
Fariborz Jahanian | 5b011b0 | 2010-02-26 21:46:27 +0000 | [diff] [blame] | 5331 | BlockDeclRefs[i]->getType()->isBlockPointerType()) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5332 | ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 5333 | } |
| 5334 | } |
| 5335 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5336 | FunctionDecl *RewriteObjC::SynthBlockInitFunctionDecl(StringRef name) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5337 | IdentifierInfo *ID = &Context->Idents.get(name); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5338 | QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5339 | return FunctionDecl::Create(*Context, TUDecl, SourceLocation(), |
| 5340 | SourceLocation(), ID, FType, 0, SC_Extern, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5341 | SC_None, false, false); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5342 | } |
| 5343 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5344 | Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5345 | const SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs) { |
Fariborz Jahanian | 05318d8 | 2011-02-16 22:37:10 +0000 | [diff] [blame] | 5346 | const BlockDecl *block = Exp->getBlockDecl(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5347 | Blocks.push_back(Exp); |
| 5348 | |
| 5349 | CollectBlockDeclRefInfo(Exp); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5350 | |
| 5351 | // Add inner imported variables now used in current block. |
| 5352 | int countOfInnerDecls = 0; |
Fariborz Jahanian | 1276bfe | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 5353 | if (!InnerBlockDeclRefs.empty()) { |
| 5354 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) { |
| 5355 | BlockDeclRefExpr *Exp = InnerBlockDeclRefs[i]; |
| 5356 | ValueDecl *VD = Exp->getDecl(); |
| 5357 | if (!Exp->isByRef() && !BlockByCopyDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5358 | // We need to save the copied-in variables in nested |
| 5359 | // blocks because it is needed at the end for some of the API generations. |
| 5360 | // See SynthesizeBlockLiterals routine. |
Fariborz Jahanian | 1276bfe | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 5361 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 5362 | BlockDeclRefs.push_back(Exp); |
| 5363 | BlockByCopyDeclsPtrSet.insert(VD); |
| 5364 | BlockByCopyDecls.push_back(VD); |
| 5365 | } |
| 5366 | if (Exp->isByRef() && !BlockByRefDeclsPtrSet.count(VD)) { |
| 5367 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 5368 | BlockDeclRefs.push_back(Exp); |
| 5369 | BlockByRefDeclsPtrSet.insert(VD); |
| 5370 | BlockByRefDecls.push_back(VD); |
| 5371 | } |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5372 | } |
Fariborz Jahanian | 1276bfe | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 5373 | // Find any imported blocks...they will need special attention. |
| 5374 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) |
| 5375 | if (InnerBlockDeclRefs[i]->isByRef() || |
| 5376 | InnerBlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
| 5377 | InnerBlockDeclRefs[i]->getType()->isBlockPointerType()) |
| 5378 | ImportedBlockDecls.insert(InnerBlockDeclRefs[i]->getDecl()); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5379 | } |
| 5380 | InnerDeclRefsCount.push_back(countOfInnerDecls); |
| 5381 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5382 | std::string FuncName; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5383 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5384 | if (CurFunctionDef) |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 5385 | FuncName = CurFunctionDef->getNameAsString(); |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 5386 | else if (CurMethodDef) |
| 5387 | BuildUniqueMethodName(FuncName, CurMethodDef); |
| 5388 | else if (GlobalVarDecl) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5389 | FuncName = std::string(GlobalVarDecl->getNameAsString()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5390 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5391 | std::string BlockNumber = utostr(Blocks.size()-1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5392 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5393 | std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber; |
| 5394 | std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5395 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5396 | // Get a pointer to the function type so we can cast appropriately. |
Fariborz Jahanian | 1f90622 | 2010-05-25 15:56:08 +0000 | [diff] [blame] | 5397 | QualType BFT = convertFunctionTypeOfBlocks(Exp->getFunctionType()); |
| 5398 | QualType FType = Context->getPointerType(BFT); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5399 | |
| 5400 | FunctionDecl *FD; |
| 5401 | Expr *NewRep; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5402 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5403 | // Simulate a contructor call... |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5404 | FD = SynthBlockInitFunctionDecl(Tag); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5405 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, FType, VK_RValue, |
| 5406 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5407 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5408 | SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5409 | |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 5410 | // Initialize the block function. |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5411 | FD = SynthBlockInitFunctionDecl(Func); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5412 | DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, FD->getType(), VK_LValue, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5413 | SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 5414 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 5415 | CK_BitCast, Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5416 | InitExprs.push_back(castExpr); |
| 5417 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5418 | // Initialize the block descriptor. |
| 5419 | std::string DescData = "__" + FuncName + "_block_desc_" + BlockNumber + "_DATA"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5420 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5421 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, |
| 5422 | SourceLocation(), SourceLocation(), |
| 5423 | &Context->Idents.get(DescData.c_str()), |
| 5424 | Context->VoidPtrTy, 0, |
| 5425 | SC_Static, SC_None); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5426 | UnaryOperator *DescRefExpr = |
| 5427 | new (Context) UnaryOperator(new (Context) DeclRefExpr(NewVD, |
| 5428 | Context->VoidPtrTy, |
| 5429 | VK_LValue, |
| 5430 | SourceLocation()), |
| 5431 | UO_AddrOf, |
| 5432 | Context->getPointerType(Context->VoidPtrTy), |
| 5433 | VK_RValue, OK_Ordinary, |
| 5434 | SourceLocation()); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5435 | InitExprs.push_back(DescRefExpr); |
| 5436 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5437 | // Add initializers for any closure decl refs. |
| 5438 | if (BlockDeclRefs.size()) { |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 5439 | Expr *Exp; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5440 | // Output all "by copy" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5441 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5442 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5443 | if (isObjCType((*I)->getType())) { |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 5444 | // FIXME: Conform to ABI ([[obj retain] autorelease]). |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5445 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5446 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), VK_LValue, |
| 5447 | SourceLocation()); |
Fariborz Jahanian | a5a7987 | 2010-05-24 18:32:56 +0000 | [diff] [blame] | 5448 | if (HasLocalVariableExternalStorage(*I)) { |
| 5449 | QualType QT = (*I)->getType(); |
| 5450 | QT = Context->getPointerType(QT); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5451 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue, |
| 5452 | OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | a5a7987 | 2010-05-24 18:32:56 +0000 | [diff] [blame] | 5453 | } |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5454 | } else if (isTopLevelBlockPointerType((*I)->getType())) { |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5455 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5456 | Arg = new (Context) DeclRefExpr(FD, FD->getType(), VK_LValue, |
| 5457 | SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 5458 | Exp = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 5459 | CK_BitCast, Arg); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5460 | } else { |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5461 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5462 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), VK_LValue, |
| 5463 | SourceLocation()); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 5464 | if (HasLocalVariableExternalStorage(*I)) { |
| 5465 | QualType QT = (*I)->getType(); |
| 5466 | QT = Context->getPointerType(QT); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5467 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue, |
| 5468 | OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 5469 | } |
| 5470 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5471 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5472 | InitExprs.push_back(Exp); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5473 | } |
| 5474 | // Output all "by ref" declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5475 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5476 | E = BlockByRefDecls.end(); I != E; ++I) { |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5477 | ValueDecl *ND = (*I); |
| 5478 | std::string Name(ND->getNameAsString()); |
| 5479 | std::string RecName; |
Fariborz Jahanian | 1e8011e | 2011-01-27 23:18:15 +0000 | [diff] [blame] | 5480 | RewriteByRefString(RecName, Name, ND, true); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5481 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str() |
| 5482 | + sizeof("struct")); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5483 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5484 | SourceLocation(), SourceLocation(), |
| 5485 | II); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5486 | assert(RD && "SynthBlockInitExpr(): Can't find RecordDecl"); |
| 5487 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 5488 | |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5489 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5490 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), VK_LValue, |
| 5491 | SourceLocation()); |
Fariborz Jahanian | 05318d8 | 2011-02-16 22:37:10 +0000 | [diff] [blame] | 5492 | bool isNestedCapturedVar = false; |
| 5493 | if (block) |
| 5494 | for (BlockDecl::capture_const_iterator ci = block->capture_begin(), |
| 5495 | ce = block->capture_end(); ci != ce; ++ci) { |
| 5496 | const VarDecl *variable = ci->getVariable(); |
| 5497 | if (variable == ND && ci->isNested()) { |
| 5498 | assert (ci->isByRef() && |
| 5499 | "SynthBlockInitExpr - captured block variable is not byref"); |
| 5500 | isNestedCapturedVar = true; |
| 5501 | break; |
| 5502 | } |
| 5503 | } |
| 5504 | // captured nested byref variable has its address passed. Do not take |
| 5505 | // its address again. |
| 5506 | if (!isNestedCapturedVar) |
| 5507 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5508 | Context->getPointerType(Exp->getType()), |
| 5509 | VK_RValue, OK_Ordinary, SourceLocation()); |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 5510 | Exp = NoTypeInfoCStyleCastExpr(Context, castT, CK_BitCast, Exp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5511 | InitExprs.push_back(Exp); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5512 | } |
| 5513 | } |
Fariborz Jahanian | ff12788 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 5514 | if (ImportedBlockDecls.size()) { |
| 5515 | // generate BLOCK_HAS_COPY_DISPOSE(have helper funcs) | BLOCK_HAS_DESCRIPTOR |
| 5516 | int flag = (BLOCK_HAS_COPY_DISPOSE | BLOCK_HAS_DESCRIPTOR); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5517 | unsigned IntSize = |
| 5518 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 5519 | Expr *FlagExp = IntegerLiteral::Create(*Context, llvm::APInt(IntSize, flag), |
| 5520 | Context->IntTy, SourceLocation()); |
Fariborz Jahanian | ff12788 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 5521 | InitExprs.push_back(FlagExp); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5522 | } |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 5523 | NewRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], InitExprs.size(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5524 | FType, VK_LValue, SourceLocation()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5525 | NewRep = new (Context) UnaryOperator(NewRep, UO_AddrOf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5526 | Context->getPointerType(NewRep->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5527 | VK_RValue, OK_Ordinary, SourceLocation()); |
John McCall | a5bbc50 | 2010-11-15 09:46:46 +0000 | [diff] [blame] | 5528 | NewRep = NoTypeInfoCStyleCastExpr(Context, FType, CK_BitCast, |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 5529 | NewRep); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5530 | BlockDeclRefs.clear(); |
| 5531 | BlockByRefDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5532 | BlockByRefDeclsPtrSet.clear(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5533 | BlockByCopyDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5534 | BlockByCopyDeclsPtrSet.clear(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5535 | ImportedBlockDecls.clear(); |
| 5536 | return NewRep; |
| 5537 | } |
| 5538 | |
Fariborz Jahanian | 42f1e65 | 2011-02-24 21:29:21 +0000 | [diff] [blame] | 5539 | bool RewriteObjC::IsDeclStmtInForeachHeader(DeclStmt *DS) { |
| 5540 | if (const ObjCForCollectionStmt * CS = |
| 5541 | dyn_cast<ObjCForCollectionStmt>(Stmts.back())) |
| 5542 | return CS->getElement() == DS; |
| 5543 | return false; |
| 5544 | } |
| 5545 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5546 | //===----------------------------------------------------------------------===// |
| 5547 | // Function Body / Expression rewriting |
| 5548 | //===----------------------------------------------------------------------===// |
| 5549 | |
| 5550 | Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5551 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5552 | isa<DoStmt>(S) || isa<ForStmt>(S)) |
| 5553 | Stmts.push_back(S); |
| 5554 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 5555 | Stmts.push_back(S); |
Chris Lattner | 4824fcd | 2010-01-09 21:45:57 +0000 | [diff] [blame] | 5556 | ObjCBcLabelNo.push_back(++BcLabelCount); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5557 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5558 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 5559 | // Pseudo-object operations and ivar references need special |
| 5560 | // treatment because we're going to recursively rewrite them. |
| 5561 | if (PseudoObjectExpr *PseudoOp = dyn_cast<PseudoObjectExpr>(S)) { |
| 5562 | if (isa<BinaryOperator>(PseudoOp->getSyntacticForm())) { |
| 5563 | return RewritePropertyOrImplicitSetter(PseudoOp); |
| 5564 | } else { |
| 5565 | return RewritePropertyOrImplicitGetter(PseudoOp); |
| 5566 | } |
| 5567 | } else if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) { |
| 5568 | return RewriteObjCIvarRefExpr(IvarRefExpr); |
| 5569 | } |
| 5570 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5571 | SourceRange OrigStmtRange = S->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5572 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5573 | // Perform a bottom up rewrite of all children. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 5574 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5575 | if (*CI) { |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 5576 | Stmt *childStmt = (*CI); |
| 5577 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(childStmt); |
Fariborz Jahanian | 1d01531 | 2011-04-11 21:17:02 +0000 | [diff] [blame] | 5578 | if (newStmt) { |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 5579 | *CI = newStmt; |
Fariborz Jahanian | 1d01531 | 2011-04-11 21:17:02 +0000 | [diff] [blame] | 5580 | } |
Nick Lewycky | 7e74924 | 2010-10-31 21:07:24 +0000 | [diff] [blame] | 5581 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5582 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5583 | if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5584 | SmallVector<BlockDeclRefExpr *, 8> InnerBlockDeclRefs; |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 5585 | llvm::SmallPtrSet<const DeclContext *, 8> InnerContexts; |
| 5586 | InnerContexts.insert(BE->getBlockDecl()); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 5587 | ImportedLocalExternalDecls.clear(); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5588 | GetInnerBlockDeclRefExprs(BE->getBody(), |
Fariborz Jahanian | 72952fc | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 5589 | InnerBlockDeclRefs, InnerContexts); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5590 | // Rewrite the block body in place. |
Fariborz Jahanian | da4ad9f | 2010-11-08 18:37:50 +0000 | [diff] [blame] | 5591 | Stmt *SaveCurrentBody = CurrentBody; |
| 5592 | CurrentBody = BE->getBody(); |
| 5593 | PropParentMap = 0; |
Fariborz Jahanian | f23a0ff | 2011-08-02 20:28:46 +0000 | [diff] [blame] | 5594 | // block literal on rhs of a property-dot-sytax assignment |
| 5595 | // must be replaced by its synthesize ast so getRewrittenText |
| 5596 | // works as expected. In this case, what actually ends up on RHS |
| 5597 | // is the blockTranscribed which is the helper function for the |
| 5598 | // block literal; as in: self.c = ^() {[ace ARR];}; |
| 5599 | bool saveDisableReplaceStmt = DisableReplaceStmt; |
| 5600 | DisableReplaceStmt = false; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5601 | RewriteFunctionBodyOrGlobalInitializer(BE->getBody()); |
Fariborz Jahanian | f23a0ff | 2011-08-02 20:28:46 +0000 | [diff] [blame] | 5602 | DisableReplaceStmt = saveDisableReplaceStmt; |
Fariborz Jahanian | da4ad9f | 2010-11-08 18:37:50 +0000 | [diff] [blame] | 5603 | CurrentBody = SaveCurrentBody; |
| 5604 | PropParentMap = 0; |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 5605 | ImportedLocalExternalDecls.clear(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5606 | // 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] | 5607 | std::string Str = Rewrite.getRewrittenText(BE->getSourceRange()); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5608 | RewrittenBlockExprs[BE] = Str; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5609 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5610 | Stmt *blockTranscribed = SynthBlockInitExpr(BE, InnerBlockDeclRefs); |
| 5611 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5612 | //blockTranscribed->dump(); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5613 | ReplaceStmt(S, blockTranscribed); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5614 | return blockTranscribed; |
| 5615 | } |
| 5616 | // Handle specific things. |
| 5617 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 5618 | return RewriteAtEncode(AtEncode); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5619 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5620 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 5621 | return RewriteAtSelector(AtSelector); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5622 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5623 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 5624 | return RewriteObjCStringLiteral(AtString); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5625 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5626 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5627 | #if 0 |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5628 | // Before we rewrite it, put the original message expression in a comment. |
| 5629 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 5630 | SourceLocation endLoc = MessExpr->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5631 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5632 | const char *startBuf = SM->getCharacterData(startLoc); |
| 5633 | const char *endBuf = SM->getCharacterData(endLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5634 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5635 | std::string messString; |
| 5636 | messString += "// "; |
| 5637 | messString.append(startBuf, endBuf-startBuf+1); |
| 5638 | messString += "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5639 | |
| 5640 | // FIXME: Missing definition of |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5641 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
| 5642 | // InsertText(startLoc, messString.c_str(), messString.size()); |
| 5643 | // Tried this, but it didn't work either... |
| 5644 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5645 | #endif |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5646 | return RewriteMessageExpr(MessExpr); |
| 5647 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5648 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5649 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 5650 | return RewriteObjCTryStmt(StmtTry); |
| 5651 | |
| 5652 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 5653 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 5654 | |
| 5655 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 5656 | return RewriteObjCThrowStmt(StmtThrow); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5657 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5658 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 5659 | return RewriteObjCProtocolExpr(ProtocolExp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5660 | |
| 5661 | if (ObjCForCollectionStmt *StmtForCollection = |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5662 | dyn_cast<ObjCForCollectionStmt>(S)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5663 | return RewriteObjCForCollectionStmt(StmtForCollection, |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5664 | OrigStmtRange.getEnd()); |
| 5665 | if (BreakStmt *StmtBreakStmt = |
| 5666 | dyn_cast<BreakStmt>(S)) |
| 5667 | return RewriteBreakStmt(StmtBreakStmt); |
| 5668 | if (ContinueStmt *StmtContinueStmt = |
| 5669 | dyn_cast<ContinueStmt>(S)) |
| 5670 | return RewriteContinueStmt(StmtContinueStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5671 | |
| 5672 | // 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] | 5673 | // and cast exprs. |
| 5674 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
| 5675 | // FIXME: What we're doing here is modifying the type-specifier that |
| 5676 | // precedes the first Decl. In the future the DeclGroup should have |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5677 | // a separate type-specifier that we can rewrite. |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5678 | // NOTE: We need to avoid rewriting the DeclStmt if it is within |
| 5679 | // the context of an ObjCForCollectionStmt. For example: |
| 5680 | // NSArray *someArray; |
| 5681 | // for (id <FooProtocol> index in someArray) ; |
| 5682 | // This is because RewriteObjCForCollectionStmt() does textual rewriting |
| 5683 | // and it depends on the original text locations/positions. |
Fariborz Jahanian | 42f1e65 | 2011-02-24 21:29:21 +0000 | [diff] [blame] | 5684 | if (Stmts.empty() || !IsDeclStmtInForeachHeader(DS)) |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5685 | RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5686 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5687 | // Blocks rewrite rules. |
| 5688 | for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); |
| 5689 | DI != DE; ++DI) { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 5690 | Decl *SD = *DI; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5691 | if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5692 | if (isTopLevelBlockPointerType(ND->getType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5693 | RewriteBlockPointerDecl(ND); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5694 | else if (ND->getType()->isFunctionPointerType()) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5695 | CheckFunctionPointerDecl(ND->getType(), ND); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 5696 | if (VarDecl *VD = dyn_cast<VarDecl>(SD)) { |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5697 | if (VD->hasAttr<BlocksAttr>()) { |
| 5698 | static unsigned uniqueByrefDeclCount = 0; |
| 5699 | assert(!BlockByRefDeclNo.count(ND) && |
| 5700 | "RewriteFunctionBodyOrGlobalInitializer: Duplicate byref decl"); |
| 5701 | BlockByRefDeclNo[ND] = uniqueByrefDeclCount++; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5702 | RewriteByRefVar(VD); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5703 | } |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 5704 | else |
| 5705 | RewriteTypeOfDecl(VD); |
| 5706 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5707 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5708 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5709 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5710 | RewriteBlockPointerDecl(TD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5711 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5712 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 5713 | } |
| 5714 | } |
| 5715 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5716 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5717 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) |
| 5718 | RewriteObjCQualifiedInterfaceTypes(CE); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5719 | |
| 5720 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5721 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 5722 | assert(!Stmts.empty() && "Statement stack is empty"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5723 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 5724 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5725 | && "Statement stack mismatch"); |
| 5726 | Stmts.pop_back(); |
| 5727 | } |
| 5728 | // Handle blocks rewriting. |
| 5729 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
| 5730 | if (BDRE->isByRef()) |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5731 | return RewriteBlockDeclRefExpr(BDRE); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5732 | } |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5733 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 5734 | ValueDecl *VD = DRE->getDecl(); |
| 5735 | if (VD->hasAttr<BlocksAttr>()) |
| 5736 | return RewriteBlockDeclRefExpr(DRE); |
Fariborz Jahanian | 6cb6eb4 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 5737 | if (HasLocalVariableExternalStorage(VD)) |
| 5738 | return RewriteLocalVariableExternalStorage(DRE); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5739 | } |
| 5740 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5741 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 5742 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 5743 | Stmt *BlockCall = SynthesizeBlockCall(CE, CE->getCallee()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 5744 | ReplaceStmt(S, BlockCall); |
| 5745 | return BlockCall; |
| 5746 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5747 | } |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 5748 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5749 | RewriteCastExpr(CE); |
| 5750 | } |
| 5751 | #if 0 |
| 5752 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5753 | CastExpr *Replacement = new (Context) CastExpr(ICE->getType(), |
| 5754 | ICE->getSubExpr(), |
| 5755 | SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5756 | // Get the new text. |
| 5757 | std::string SStr; |
| 5758 | llvm::raw_string_ostream Buf(SStr); |
Eli Friedman | 3a9eb44 | 2009-05-30 05:19:26 +0000 | [diff] [blame] | 5759 | Replacement->printPretty(Buf, *Context); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5760 | const std::string &Str = Buf.str(); |
| 5761 | |
| 5762 | printf("CAST = %s\n", &Str[0]); |
| 5763 | InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 5764 | delete S; |
| 5765 | return Replacement; |
| 5766 | } |
| 5767 | #endif |
| 5768 | // Return this stmt unmodified. |
| 5769 | return S; |
| 5770 | } |
| 5771 | |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5772 | void RewriteObjC::RewriteRecordBody(RecordDecl *RD) { |
| 5773 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 5774 | e = RD->field_end(); i != e; ++i) { |
| 5775 | FieldDecl *FD = *i; |
| 5776 | if (isTopLevelBlockPointerType(FD->getType())) |
| 5777 | RewriteBlockPointerDecl(FD); |
| 5778 | if (FD->getType()->isObjCQualifiedIdType() || |
| 5779 | FD->getType()->isObjCQualifiedInterfaceType()) |
| 5780 | RewriteObjCQualifiedInterfaceTypes(FD); |
| 5781 | } |
| 5782 | } |
| 5783 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5784 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 5785 | /// main file of the input. |
| 5786 | void RewriteObjC::HandleDeclInMainFile(Decl *D) { |
| 5787 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | cb73530 | 2008-12-17 00:20:22 +0000 | [diff] [blame] | 5788 | if (FD->isOverloadedOperator()) |
| 5789 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5790 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5791 | // Since function prototypes don't have ParmDecl's, we check the function |
| 5792 | // prototype. This enables us to rewrite function declarations and |
| 5793 | // definitions using the same code. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5794 | RewriteBlocksInFunctionProtoType(FD->getType(), FD); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5795 | |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 5796 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Argyrios Kyrtzidis | 5f1bfc1 | 2010-07-07 11:31:34 +0000 | [diff] [blame] | 5797 | if (CompoundStmt *Body = dyn_cast_or_null<CompoundStmt>(FD->getBody())) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5798 | CurFunctionDef = FD; |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 5799 | CurFunctionDeclToDeclareForBlock = FD; |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5800 | CurrentBody = Body; |
Ted Kremenek | eaab206 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 5801 | Body = |
| 5802 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 5803 | FD->setBody(Body); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5804 | CurrentBody = 0; |
| 5805 | if (PropParentMap) { |
| 5806 | delete PropParentMap; |
| 5807 | PropParentMap = 0; |
| 5808 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5809 | // This synthesizes and inserts the block "impl" struct, invoke function, |
| 5810 | // and any copy/dispose helper functions. |
| 5811 | InsertBlockLiteralsWithinFunction(FD); |
| 5812 | CurFunctionDef = 0; |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 5813 | CurFunctionDeclToDeclareForBlock = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5814 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5815 | return; |
| 5816 | } |
| 5817 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 5818 | if (CompoundStmt *Body = MD->getCompoundBody()) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5819 | CurMethodDef = MD; |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5820 | CurrentBody = Body; |
Ted Kremenek | eaab206 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 5821 | Body = |
| 5822 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 5823 | MD->setBody(Body); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5824 | CurrentBody = 0; |
| 5825 | if (PropParentMap) { |
| 5826 | delete PropParentMap; |
| 5827 | PropParentMap = 0; |
| 5828 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5829 | InsertBlockLiteralsWithinMethod(MD); |
| 5830 | CurMethodDef = 0; |
| 5831 | } |
| 5832 | } |
| 5833 | if (ObjCImplementationDecl *CI = dyn_cast<ObjCImplementationDecl>(D)) |
| 5834 | ClassImplementation.push_back(CI); |
| 5835 | else if (ObjCCategoryImplDecl *CI = dyn_cast<ObjCCategoryImplDecl>(D)) |
| 5836 | CategoryImplementation.push_back(CI); |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 5837 | else if (isa<ObjCClassDecl>(D)) |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5838 | llvm_unreachable("RewriteObjC::HandleDeclInMainFile - ObjCClassDecl"); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5839 | else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 5840 | RewriteObjCQualifiedInterfaceTypes(VD); |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5841 | if (isTopLevelBlockPointerType(VD->getType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5842 | RewriteBlockPointerDecl(VD); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5843 | else if (VD->getType()->isFunctionPointerType()) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5844 | CheckFunctionPointerDecl(VD->getType(), VD); |
| 5845 | if (VD->getInit()) { |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 5846 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5847 | RewriteCastExpr(CE); |
| 5848 | } |
| 5849 | } |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5850 | } else if (VD->getType()->isRecordType()) { |
| 5851 | RecordDecl *RD = VD->getType()->getAs<RecordType>()->getDecl(); |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 5852 | if (RD->isCompleteDefinition()) |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5853 | RewriteRecordBody(RD); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5854 | } |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5855 | if (VD->getInit()) { |
| 5856 | GlobalVarDecl = VD; |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5857 | CurrentBody = VD->getInit(); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5858 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5859 | CurrentBody = 0; |
| 5860 | if (PropParentMap) { |
| 5861 | delete PropParentMap; |
| 5862 | PropParentMap = 0; |
| 5863 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5864 | SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5865 | VD->getName()); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5866 | GlobalVarDecl = 0; |
| 5867 | |
| 5868 | // This is needed for blocks. |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 5869 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5870 | RewriteCastExpr(CE); |
| 5871 | } |
| 5872 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5873 | return; |
| 5874 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5875 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5876 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5877 | RewriteBlockPointerDecl(TD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5878 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5879 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 5880 | return; |
| 5881 | } |
| 5882 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) { |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 5883 | if (RD->isCompleteDefinition()) |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5884 | RewriteRecordBody(RD); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5885 | return; |
| 5886 | } |
| 5887 | // Nothing yet. |
| 5888 | } |
| 5889 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 5890 | void RewriteObjC::HandleTranslationUnit(ASTContext &C) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5891 | if (Diags.hasErrorOccurred()) |
| 5892 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5893 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5894 | RewriteInclude(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5895 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5896 | // Here's a great place to add any extra declarations that may be needed. |
| 5897 | // Write out meta data for each @protocol(<expr>). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5898 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5899 | E = ProtocolExprDecls.end(); I != E; ++I) |
| 5900 | RewriteObjCProtocolMetaData(*I, "", "", Preamble); |
| 5901 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5902 | InsertText(SM->getLocForStartOfFile(MainFileID), Preamble, false); |
Steve Naroff | 0aab796 | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5903 | if (ClassImplementation.size() || CategoryImplementation.size()) |
| 5904 | RewriteImplementations(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5905 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5906 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 5907 | // we are done. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5908 | if (const RewriteBuffer *RewriteBuf = |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5909 | Rewrite.getRewriteBufferFor(MainFileID)) { |
| 5910 | //printf("Changed:\n"); |
| 5911 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 5912 | } else { |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5913 | llvm::errs() << "No changes\n"; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5914 | } |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 5915 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5916 | if (ClassImplementation.size() || CategoryImplementation.size() || |
| 5917 | ProtocolExprDecls.size()) { |
Steve Naroff | 0aab796 | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5918 | // Rewrite Objective-c meta data* |
| 5919 | std::string ResultStr; |
Fariborz Jahanian | 5845717 | 2011-12-05 18:43:13 +0000 | [diff] [blame] | 5920 | RewriteMetaDataIntoBuffer(ResultStr); |
Steve Naroff | 0aab796 | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5921 | // Emit metadata. |
| 5922 | *OutFile << ResultStr; |
| 5923 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5924 | OutFile->flush(); |
| 5925 | } |