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 | |
Eli Friedman | 39d7c4d | 2009-05-18 22:50:54 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/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" |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 29 | using namespace clang; |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 30 | using llvm::utostr; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 32 | namespace { |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 33 | class RewriteObjC : public ASTConsumer { |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 34 | enum { |
| 35 | BLOCK_FIELD_IS_OBJECT = 3, /* id, NSObject, __attribute__((NSObject)), |
| 36 | block, ... */ |
| 37 | BLOCK_FIELD_IS_BLOCK = 7, /* a block variable */ |
| 38 | BLOCK_FIELD_IS_BYREF = 8, /* the on stack structure holding the |
| 39 | __block variable */ |
| 40 | BLOCK_FIELD_IS_WEAK = 16, /* declared __weak, only used in byref copy |
| 41 | helpers */ |
| 42 | BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose |
| 43 | support routines */ |
| 44 | BLOCK_BYREF_CURRENT_MAX = 256 |
| 45 | }; |
| 46 | |
| 47 | enum { |
| 48 | BLOCK_NEEDS_FREE = (1 << 24), |
| 49 | BLOCK_HAS_COPY_DISPOSE = (1 << 25), |
| 50 | BLOCK_HAS_CXX_OBJ = (1 << 26), |
| 51 | BLOCK_IS_GC = (1 << 27), |
| 52 | BLOCK_IS_GLOBAL = (1 << 28), |
| 53 | BLOCK_HAS_DESCRIPTOR = (1 << 29) |
| 54 | }; |
| 55 | |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 56 | Rewriter Rewrite; |
Chris Lattner | e365c50 | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 57 | Diagnostic &Diags; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 58 | const LangOptions &LangOpts; |
Steve Naroff | f69cc5d | 2008-01-30 19:17:43 +0000 | [diff] [blame] | 59 | unsigned RewriteFailedDiag; |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 60 | unsigned TryFinallyContainsReturnDiag; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 61 | |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 62 | ASTContext *Context; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 63 | SourceManager *SM; |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 64 | TranslationUnitDecl *TUDecl; |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 65 | FileID MainFileID; |
Chris Lattner | 26de465 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 66 | const char *MainFileStart, *MainFileEnd; |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 67 | SourceLocation LastIncLoc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 68 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 69 | llvm::SmallVector<ObjCImplementationDecl *, 8> ClassImplementation; |
| 70 | llvm::SmallVector<ObjCCategoryImplDecl *, 8> CategoryImplementation; |
| 71 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCSynthesizedStructs; |
Steve Naroff | fbfe825 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 72 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> ObjCSynthesizedProtocols; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 73 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCForwardDecls; |
| 74 | llvm::DenseMap<ObjCMethodDecl*, std::string> MethodInternalNames; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 75 | llvm::SmallVector<Stmt *, 32> Stmts; |
| 76 | llvm::SmallVector<int, 8> ObjCBcLabelNo; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 77 | // Remember all the @protocol(<expr>) expressions. |
| 78 | llvm::SmallPtrSet<ObjCProtocolDecl *, 32> ProtocolExprDecls; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 79 | |
| 80 | llvm::DenseSet<uint64_t> CopyDestroyCache; |
| 81 | |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 82 | unsigned NumObjCStringLiterals; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 83 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 84 | FunctionDecl *MsgSendFunctionDecl; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 85 | FunctionDecl *MsgSendSuperFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 86 | FunctionDecl *MsgSendStretFunctionDecl; |
| 87 | FunctionDecl *MsgSendSuperStretFunctionDecl; |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 88 | FunctionDecl *MsgSendFpretFunctionDecl; |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 89 | FunctionDecl *GetClassFunctionDecl; |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 90 | FunctionDecl *GetMetaClassFunctionDecl; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 91 | FunctionDecl *SelGetUidFunctionDecl; |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 92 | FunctionDecl *CFStringFunctionDecl; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 93 | FunctionDecl *SuperContructorFunctionDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 94 | |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 95 | // ObjC string constant support. |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 96 | VarDecl *ConstantStringClassReference; |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 97 | RecordDecl *NSStringRecord; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 98 | |
Fariborz Jahanian | b586cce | 2008-01-16 00:09:11 +0000 | [diff] [blame] | 99 | // ObjC foreach break/continue generation support. |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 100 | int BcLabelCount; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 101 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 102 | // Needed for super. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 103 | ObjCMethodDecl *CurMethodDef; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 104 | RecordDecl *SuperStructDecl; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 105 | RecordDecl *ConstantStringDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 107 | TypeDecl *ProtocolTypeDecl; |
| 108 | QualType getProtocolType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 109 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 110 | // Needed for header files being rewritten |
| 111 | bool IsHeader; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 112 | |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 113 | std::string InFileName; |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 114 | llvm::raw_ostream* OutFile; |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 115 | |
| 116 | bool SilenceRewriteMacroWarning; |
Fariborz Jahanian | f292fcf | 2010-01-07 22:51:18 +0000 | [diff] [blame] | 117 | bool objc_impl_method; |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 118 | |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 119 | std::string Preamble; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 120 | |
| 121 | // Block expressions. |
| 122 | llvm::SmallVector<BlockExpr *, 32> Blocks; |
| 123 | llvm::SmallVector<BlockDeclRefExpr *, 32> BlockDeclRefs; |
| 124 | llvm::DenseMap<BlockDeclRefExpr *, CallExpr *> BlockCallExprs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 125 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 126 | // Block related declarations. |
| 127 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByCopyDecls; |
| 128 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByRefDecls; |
| 129 | llvm::SmallPtrSet<ValueDecl *, 8> ImportedBlockDecls; |
| 130 | |
| 131 | llvm::DenseMap<BlockExpr *, std::string> RewrittenBlockExprs; |
| 132 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 133 | // This maps a property to it's assignment statement. |
| 134 | llvm::DenseMap<ObjCPropertyRefExpr *, BinaryOperator *> PropSetters; |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 135 | // This maps a property to it's synthesied message expression. |
| 136 | // This allows us to rewrite chained getters (e.g. o.a.b.c). |
| 137 | llvm::DenseMap<ObjCPropertyRefExpr *, Stmt *> PropGetters; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 138 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 139 | // This maps an original source AST to it's rewritten form. This allows |
| 140 | // us to avoid rewriting the same node twice (which is very uncommon). |
| 141 | // This is needed to support some of the exotic property rewriting. |
| 142 | llvm::DenseMap<Stmt *, Stmt *> ReplacedNodes; |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 143 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 144 | FunctionDecl *CurFunctionDef; |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 145 | VarDecl *GlobalVarDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 146 | |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 147 | bool DisableReplaceStmt; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 148 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 149 | static const int OBJC_ABI_VERSION =7 ; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 150 | public: |
Ted Kremenek | e3a6198 | 2008-05-31 20:11:04 +0000 | [diff] [blame] | 151 | virtual void Initialize(ASTContext &context); |
| 152 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 153 | // Top Level Driver code. |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 154 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
| 155 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) |
| 156 | HandleTopLevelSingleDecl(*I); |
| 157 | } |
| 158 | void HandleTopLevelSingleDecl(Decl *D); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 159 | void HandleDeclInMainFile(Decl *D); |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 160 | RewriteObjC(std::string inFile, llvm::raw_ostream *OS, |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 161 | Diagnostic &D, const LangOptions &LOpts, |
| 162 | bool silenceMacroWarn); |
Ted Kremenek | e452e0f | 2008-08-08 04:15:52 +0000 | [diff] [blame] | 163 | |
| 164 | ~RewriteObjC() {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 165 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 166 | virtual void HandleTranslationUnit(ASTContext &C); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 168 | void ReplaceStmt(Stmt *Old, Stmt *New) { |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 169 | Stmt *ReplacingStmt = ReplacedNodes[Old]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 170 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 171 | if (ReplacingStmt) |
| 172 | return; // We can't rewrite the same node twice. |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 173 | |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 174 | if (DisableReplaceStmt) |
| 175 | return; // Used when rewriting the assignment of a property setter. |
| 176 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 177 | // If replacement succeeded or warning disabled return with no warning. |
| 178 | if (!Rewrite.ReplaceStmt(Old, New)) { |
| 179 | ReplacedNodes[Old] = New; |
| 180 | return; |
| 181 | } |
| 182 | if (SilenceRewriteMacroWarning) |
| 183 | return; |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 184 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 185 | << Old->getSourceRange(); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 186 | } |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 187 | |
| 188 | void ReplaceStmtWithRange(Stmt *Old, Stmt *New, SourceRange SrcRange) { |
| 189 | // Measaure the old text. |
| 190 | int Size = Rewrite.getRangeSize(SrcRange); |
| 191 | if (Size == -1) { |
| 192 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 193 | << Old->getSourceRange(); |
| 194 | return; |
| 195 | } |
| 196 | // Get the new text. |
| 197 | std::string SStr; |
| 198 | llvm::raw_string_ostream S(SStr); |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 199 | New->printPretty(S, *Context, 0, PrintingPolicy(LangOpts)); |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 200 | const std::string &Str = S.str(); |
| 201 | |
| 202 | // If replacement succeeded or warning disabled return with no warning. |
Daniel Dunbar | d7407dc | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 203 | if (!Rewrite.ReplaceText(SrcRange.getBegin(), Size, Str)) { |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 204 | ReplacedNodes[Old] = New; |
| 205 | return; |
| 206 | } |
| 207 | if (SilenceRewriteMacroWarning) |
| 208 | return; |
| 209 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 210 | << Old->getSourceRange(); |
| 211 | } |
| 212 | |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 213 | void InsertText(SourceLocation Loc, const char *StrData, unsigned StrLen, |
| 214 | bool InsertAfter = true) { |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 215 | // If insertion succeeded or warning disabled return with no warning. |
Daniel Dunbar | d7407dc | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 216 | if (!Rewrite.InsertText(Loc, llvm::StringRef(StrData, StrLen), |
| 217 | InsertAfter) || |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 218 | SilenceRewriteMacroWarning) |
| 219 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 220 | |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 221 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 222 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 223 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 224 | void RemoveText(SourceLocation Loc, unsigned StrLen) { |
| 225 | // If removal succeeded or warning disabled return with no warning. |
| 226 | if (!Rewrite.RemoveText(Loc, StrLen) || SilenceRewriteMacroWarning) |
| 227 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 228 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 229 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 230 | } |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 231 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 232 | void ReplaceText(SourceLocation Start, unsigned OrigLength, |
| 233 | const char *NewStr, unsigned NewLength) { |
| 234 | // If removal succeeded or warning disabled return with no warning. |
Daniel Dunbar | d7407dc | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 235 | if (!Rewrite.ReplaceText(Start, OrigLength, |
| 236 | llvm::StringRef(NewStr, NewLength)) || |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 237 | SilenceRewriteMacroWarning) |
| 238 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 239 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 240 | Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag); |
| 241 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 242 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 243 | // Syntactic Rewriting. |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 244 | void RewritePrologue(SourceLocation Loc); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 245 | void RewriteInclude(); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 246 | void RewriteTabs(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 247 | void RewriteForwardClassDecl(ObjCClassDecl *Dcl); |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 248 | void RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 249 | ObjCImplementationDecl *IMD, |
| 250 | ObjCCategoryImplDecl *CID); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 251 | void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 252 | void RewriteImplementationDecl(Decl *Dcl); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 253 | void RewriteObjCMethodDecl(ObjCMethodDecl *MDecl, std::string &ResultStr); |
| 254 | void RewriteCategoryDecl(ObjCCategoryDecl *Dcl); |
| 255 | void RewriteProtocolDecl(ObjCProtocolDecl *Dcl); |
| 256 | void RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *Dcl); |
| 257 | void RewriteMethodDeclaration(ObjCMethodDecl *Method); |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 258 | void RewriteProperty(ObjCPropertyDecl *prop); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 259 | void RewriteFunctionDecl(FunctionDecl *FD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 260 | void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 261 | void RewriteObjCQualifiedInterfaceTypes(Expr *E); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 262 | bool needToScanForQualifiers(QualType T); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 263 | ObjCInterfaceDecl *isSuperReceiver(Expr *recExpr); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 264 | QualType getSuperStructType(); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 265 | QualType getConstantStringStructType(); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 266 | bool BufferContainsPPDirectives(const char *startBuf, const char *endBuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 267 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 268 | // Expression Rewriting. |
Steve Naroff | f3473a7 | 2007-11-09 15:20:18 +0000 | [diff] [blame] | 269 | Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 270 | void CollectPropertySetters(Stmt *S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 271 | |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 272 | Stmt *CurrentBody; |
| 273 | ParentMap *PropParentMap; // created lazily. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 274 | |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 275 | Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp); |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 276 | Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, SourceLocation OrigStart); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 277 | Stmt *RewritePropertyGetter(ObjCPropertyRefExpr *PropRefExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 278 | Stmt *RewritePropertySetter(BinaryOperator *BinOp, Expr *newStmt, |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 279 | SourceRange SrcRange); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 280 | Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp); |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 281 | Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp); |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 282 | Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp); |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 283 | Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 284 | void WarnAboutReturnGotoStmts(Stmt *S); |
| 285 | void HasReturnStmts(Stmt *S, bool &hasReturns); |
| 286 | void RewriteTryReturnStmts(Stmt *S); |
| 287 | void RewriteSyncReturnStmts(Stmt *S, std::string buf); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 288 | Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 289 | Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 290 | Stmt *RewriteObjCCatchStmt(ObjCAtCatchStmt *S); |
| 291 | Stmt *RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S); |
| 292 | Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S); |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 293 | Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
| 294 | SourceLocation OrigEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 295 | CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD, |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 296 | Expr **args, unsigned nargs); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 297 | Stmt *SynthMessageExpr(ObjCMessageExpr *Exp); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 298 | Stmt *RewriteBreakStmt(BreakStmt *S); |
| 299 | Stmt *RewriteContinueStmt(ContinueStmt *S); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 300 | void SynthCountByEnumWithState(std::string &buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 301 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 302 | void SynthMsgSendFunctionDecl(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 303 | void SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 304 | void SynthMsgSendStretFunctionDecl(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 305 | void SynthMsgSendFpretFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 306 | void SynthMsgSendSuperStretFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 307 | void SynthGetClassFunctionDecl(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 308 | void SynthGetMetaClassFunctionDecl(); |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 309 | void SynthSelGetUidFunctionDecl(); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 310 | void SynthSuperContructorFunctionDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 311 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 312 | // Metadata emission. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 313 | void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 314 | std::string &Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 315 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 316 | void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 317 | std::string &Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 318 | |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 319 | template<typename MethodIterator> |
| 320 | void RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 321 | MethodIterator MethodEnd, |
Fariborz Jahanian | 8e991ba | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 322 | bool IsInstanceMethod, |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 323 | const char *prefix, |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 324 | const char *ClassName, |
| 325 | std::string &Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 326 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 327 | void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol, |
| 328 | const char *prefix, |
| 329 | const char *ClassName, |
| 330 | std::string &Result); |
| 331 | void RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Prots, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 332 | const char *prefix, |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 333 | const char *ClassName, |
| 334 | std::string &Result); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 335 | void SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 336 | std::string &Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 337 | void SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl, |
| 338 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 339 | std::string &Result); |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 340 | void RewriteImplementations(); |
| 341 | void SynthesizeMetaDataIntoBuffer(std::string &Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 342 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 343 | // Block rewriting. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 344 | void RewriteBlocksInFunctionProtoType(QualType funcType, NamedDecl *D); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 345 | void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 346 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 347 | void InsertBlockLiteralsWithinFunction(FunctionDecl *FD); |
| 348 | void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 349 | |
| 350 | // Block specific rewrite rules. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 351 | void RewriteBlockCall(CallExpr *Exp); |
| 352 | void RewriteBlockPointerDecl(NamedDecl *VD); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 353 | void RewriteByRefVar(VarDecl *VD); |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 354 | std::string SynthesizeByrefCopyDestroyHelper(VarDecl *VD, int flag); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 355 | Stmt *RewriteBlockDeclRefExpr(Expr *VD); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 356 | void RewriteBlockPointerFunctionArgs(FunctionDecl *FD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 357 | |
| 358 | std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 359 | const char *funcName, std::string Tag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 360 | std::string SynthesizeBlockFunc(BlockExpr *CE, int i, |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 361 | const char *funcName, std::string Tag); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 362 | std::string SynthesizeBlockImpl(BlockExpr *CE, |
| 363 | std::string Tag, std::string Desc); |
| 364 | std::string SynthesizeBlockDescriptor(std::string DescTag, |
| 365 | std::string ImplTag, |
| 366 | int i, const char *funcName, |
| 367 | unsigned hasCopy); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 368 | Stmt *SynthesizeBlockCall(CallExpr *Exp, const Expr* BlockExp); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 369 | void SynthesizeBlockLiterals(SourceLocation FunLocStart, |
| 370 | const char *FunName); |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 371 | void RewriteRecordBody(RecordDecl *RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 372 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 373 | void CollectBlockDeclRefInfo(BlockExpr *Exp); |
| 374 | void GetBlockCallExprs(Stmt *S); |
| 375 | void GetBlockDeclRefExprs(Stmt *S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 376 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 377 | // We avoid calling Type::isBlockPointerType(), since it operates on the |
| 378 | // 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] | 379 | bool isTopLevelBlockPointerType(QualType T) { |
| 380 | return isa<BlockPointerType>(T); |
| 381 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 382 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 383 | // FIXME: This predicate seems like it would be useful to add to ASTContext. |
| 384 | bool isObjCType(QualType T) { |
| 385 | if (!LangOpts.ObjC1 && !LangOpts.ObjC2) |
| 386 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 387 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 388 | QualType OCT = Context->getCanonicalType(T).getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 389 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 390 | if (OCT == Context->getCanonicalType(Context->getObjCIdType()) || |
| 391 | OCT == Context->getCanonicalType(Context->getObjCClassType())) |
| 392 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 393 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 394 | if (const PointerType *PT = OCT->getAs<PointerType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 395 | if (isa<ObjCInterfaceType>(PT->getPointeeType()) || |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 396 | PT->getPointeeType()->isObjCQualifiedIdType()) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 397 | return true; |
| 398 | } |
| 399 | return false; |
| 400 | } |
| 401 | bool PointerTypeTakesAnyBlockArguments(QualType QT); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 402 | void GetExtentOfArgList(const char *Name, const char *&LParen, |
| 403 | const char *&RParen); |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 404 | void RewriteCastExpr(CStyleCastExpr *CE); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 405 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 406 | FunctionDecl *SynthBlockInitFunctionDecl(const char *name); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 407 | Stmt *SynthBlockInitExpr(BlockExpr *Exp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 408 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 409 | void QuoteDoublequotes(std::string &From, std::string &To) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 410 | for (unsigned i = 0; i < From.length(); i++) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 411 | if (From[i] == '"') |
| 412 | To += "\\\""; |
| 413 | else |
| 414 | To += From[i]; |
| 415 | } |
| 416 | } |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 417 | }; |
| 418 | } |
| 419 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 420 | void RewriteObjC::RewriteBlocksInFunctionProtoType(QualType funcType, |
| 421 | NamedDecl *D) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 422 | if (FunctionProtoType *fproto = dyn_cast<FunctionProtoType>(funcType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 423 | for (FunctionProtoType::arg_type_iterator I = fproto->arg_type_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 424 | E = fproto->arg_type_end(); I && (I != E); ++I) |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 425 | if (isTopLevelBlockPointerType(*I)) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 426 | // All the args are checked/rewritten. Don't call twice! |
| 427 | RewriteBlockPointerDecl(D); |
| 428 | break; |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | void RewriteObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 434 | const PointerType *PT = funcType->getAs<PointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 435 | if (PT && PointerTypeTakesAnyBlockArguments(funcType)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 436 | RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 439 | static bool IsHeaderFile(const std::string &Filename) { |
| 440 | std::string::size_type DotPos = Filename.rfind('.'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 441 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 442 | if (DotPos == std::string::npos) { |
| 443 | // no file extension |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 444 | return false; |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 445 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 446 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 447 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 448 | // C header: .h |
| 449 | // C++ header: .hh or .H; |
| 450 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 451 | } |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 452 | |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 453 | RewriteObjC::RewriteObjC(std::string inFile, llvm::raw_ostream* OS, |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 454 | Diagnostic &D, const LangOptions &LOpts, |
| 455 | bool silenceMacroWarn) |
| 456 | : Diags(D), LangOpts(LOpts), InFileName(inFile), OutFile(OS), |
| 457 | SilenceRewriteMacroWarning(silenceMacroWarn) { |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 458 | IsHeader = IsHeaderFile(inFile); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 459 | RewriteFailedDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 460 | "rewriting sub-expression within a macro (may not be correct)"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 461 | TryFinallyContainsReturnDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 462 | "rewriter doesn't support user-specified control flow semantics " |
| 463 | "for @try/@finally (code may not execute properly)"); |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Eli Friedman | bce831b | 2009-05-18 22:29:17 +0000 | [diff] [blame] | 466 | ASTConsumer *clang::CreateObjCRewriter(const std::string& InFile, |
| 467 | llvm::raw_ostream* OS, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 468 | Diagnostic &Diags, |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 469 | const LangOptions &LOpts, |
| 470 | bool SilenceRewriteMacroWarning) { |
| 471 | return new RewriteObjC(InFile, OS, Diags, LOpts, SilenceRewriteMacroWarning); |
Chris Lattner | e365c50 | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 472 | } |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 473 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 474 | void RewriteObjC::Initialize(ASTContext &context) { |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 475 | Context = &context; |
| 476 | SM = &Context->getSourceManager(); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 477 | TUDecl = Context->getTranslationUnitDecl(); |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 478 | MsgSendFunctionDecl = 0; |
| 479 | MsgSendSuperFunctionDecl = 0; |
| 480 | MsgSendStretFunctionDecl = 0; |
| 481 | MsgSendSuperStretFunctionDecl = 0; |
| 482 | MsgSendFpretFunctionDecl = 0; |
| 483 | GetClassFunctionDecl = 0; |
| 484 | GetMetaClassFunctionDecl = 0; |
| 485 | SelGetUidFunctionDecl = 0; |
| 486 | CFStringFunctionDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 487 | ConstantStringClassReference = 0; |
| 488 | NSStringRecord = 0; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 489 | CurMethodDef = 0; |
| 490 | CurFunctionDef = 0; |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 491 | GlobalVarDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 492 | SuperStructDecl = 0; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 493 | ProtocolTypeDecl = 0; |
Steve Naroff | 9630ec5 | 2008-03-27 22:59:54 +0000 | [diff] [blame] | 494 | ConstantStringDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 495 | BcLabelCount = 0; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 496 | SuperContructorFunctionDecl = 0; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 497 | NumObjCStringLiterals = 0; |
Steve Naroff | 68272b8 | 2008-12-08 20:01:41 +0000 | [diff] [blame] | 498 | PropParentMap = 0; |
| 499 | CurrentBody = 0; |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 500 | DisableReplaceStmt = false; |
Fariborz Jahanian | f292fcf | 2010-01-07 22:51:18 +0000 | [diff] [blame] | 501 | objc_impl_method = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 502 | |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 503 | // Get the ID and start/end of the main file. |
| 504 | MainFileID = SM->getMainFileID(); |
| 505 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 506 | MainFileStart = MainBuf->getBufferStart(); |
| 507 | MainFileEnd = MainBuf->getBufferEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 508 | |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 509 | Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOptions()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 510 | |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 511 | // declaring objc_selector outside the parameter list removes a silly |
| 512 | // scope related warning... |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 513 | if (IsHeader) |
Steve Naroff | 62c2632 | 2009-02-03 20:39:18 +0000 | [diff] [blame] | 514 | Preamble = "#pragma once\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 515 | Preamble += "struct objc_selector; struct objc_class;\n"; |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 516 | Preamble += "struct __rw_objc_super { struct objc_object *object; "; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 517 | Preamble += "struct objc_object *superClass; "; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 518 | if (LangOpts.Microsoft) { |
| 519 | // Add a constructor for creating temporary objects. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 520 | Preamble += "__rw_objc_super(struct objc_object *o, struct objc_object *s) " |
| 521 | ": "; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 522 | Preamble += "object(o), superClass(s) {} "; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 523 | } |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 524 | Preamble += "};\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 525 | Preamble += "#ifndef _REWRITER_typedef_Protocol\n"; |
| 526 | Preamble += "typedef struct objc_object Protocol;\n"; |
| 527 | Preamble += "#define _REWRITER_typedef_Protocol\n"; |
| 528 | Preamble += "#endif\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 529 | if (LangOpts.Microsoft) { |
| 530 | Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n"; |
| 531 | Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n"; |
| 532 | } else |
| 533 | Preamble += "#define __OBJC_RW_DLLIMPORT extern\n"; |
| 534 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 535 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 536 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 537 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 538 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend_stret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 539 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 540 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper_stret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 541 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 542 | Preamble += "__OBJC_RW_DLLIMPORT double objc_msgSend_fpret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 543 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 544 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getClass"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 545 | Preamble += "(const char *);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 546 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getMetaClass"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 547 | Preamble += "(const char *);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 548 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw(struct objc_object *);\n"; |
| 549 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_enter(void *);\n"; |
| 550 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_exit(void *);\n"; |
| 551 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_exception_extract(void *);\n"; |
| 552 | Preamble += "__OBJC_RW_DLLIMPORT int objc_exception_match"; |
Steve Naroff | 580ca78 | 2008-05-09 21:17:56 +0000 | [diff] [blame] | 553 | Preamble += "(struct objc_class *, struct objc_object *);\n"; |
Steve Naroff | 59f05a4 | 2008-07-16 18:58:11 +0000 | [diff] [blame] | 554 | // @synchronized hooks. |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 555 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_enter(struct objc_object *);\n"; |
| 556 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_exit(struct objc_object *);\n"; |
| 557 | Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 558 | Preamble += "#ifndef __FASTENUMERATIONSTATE\n"; |
| 559 | Preamble += "struct __objcFastEnumerationState {\n\t"; |
| 560 | Preamble += "unsigned long state;\n\t"; |
Steve Naroff | b10f273 | 2008-04-04 22:58:22 +0000 | [diff] [blame] | 561 | Preamble += "void **itemsPtr;\n\t"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 562 | Preamble += "unsigned long *mutationsPtr;\n\t"; |
| 563 | Preamble += "unsigned long extra[5];\n};\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 564 | Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 565 | Preamble += "#define __FASTENUMERATIONSTATE\n"; |
| 566 | Preamble += "#endif\n"; |
| 567 | Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n"; |
| 568 | Preamble += "struct __NSConstantStringImpl {\n"; |
| 569 | Preamble += " int *isa;\n"; |
| 570 | Preamble += " int flags;\n"; |
| 571 | Preamble += " char *str;\n"; |
| 572 | Preamble += " long length;\n"; |
| 573 | Preamble += "};\n"; |
Steve Naroff | 88bee74 | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 574 | Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n"; |
| 575 | Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n"; |
| 576 | Preamble += "#else\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 577 | Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n"; |
Steve Naroff | 88bee74 | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 578 | Preamble += "#endif\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 579 | Preamble += "#define __NSCONSTANTSTRINGIMPL\n"; |
| 580 | Preamble += "#endif\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 581 | // Blocks preamble. |
| 582 | Preamble += "#ifndef BLOCK_IMPL\n"; |
| 583 | Preamble += "#define BLOCK_IMPL\n"; |
| 584 | Preamble += "struct __block_impl {\n"; |
| 585 | Preamble += " void *isa;\n"; |
| 586 | Preamble += " int Flags;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 587 | Preamble += " int Reserved;\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 588 | Preamble += " void *FuncPtr;\n"; |
| 589 | Preamble += "};\n"; |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 590 | Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n"; |
Steve Naroff | c9c1e9c | 2009-12-06 01:52:22 +0000 | [diff] [blame] | 591 | Preamble += "#ifdef __OBJC_EXPORT_BLOCKS\n"; |
Steve Naroff | a851e60 | 2009-12-06 01:33:56 +0000 | [diff] [blame] | 592 | Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_assign(void *, const void *, const int);\n"; |
| 593 | Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_dispose(const void *, const int);\n"; |
| 594 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteGlobalBlock[32];\n"; |
| 595 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteStackBlock[32];\n"; |
| 596 | Preamble += "#else\n"; |
Steve Naroff | cd82637 | 2010-01-05 18:09:31 +0000 | [diff] [blame] | 597 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_assign(void *, const void *, const int);\n"; |
| 598 | 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] | 599 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteGlobalBlock[32];\n"; |
| 600 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteStackBlock[32];\n"; |
| 601 | Preamble += "#endif\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 602 | Preamble += "#endif\n"; |
Steve Naroff | a48396e | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 603 | if (LangOpts.Microsoft) { |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 604 | Preamble += "#undef __OBJC_RW_DLLIMPORT\n"; |
| 605 | Preamble += "#undef __OBJC_RW_STATICIMPORT\n"; |
Steve Naroff | a48396e | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 606 | Preamble += "#define __attribute__(X)\n"; |
| 607 | } |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 608 | else { |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 609 | Preamble += "#define __block\n"; |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 610 | Preamble += "#define __weak\n"; |
| 611 | } |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 615 | //===----------------------------------------------------------------------===// |
| 616 | // Top Level Driver Code |
| 617 | //===----------------------------------------------------------------------===// |
| 618 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 619 | void RewriteObjC::HandleTopLevelSingleDecl(Decl *D) { |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 620 | // Two cases: either the decl could be in the main file, or it could be in a |
| 621 | // #included file. If the former, rewrite it now. If the later, check to see |
| 622 | // if we rewrote the #include/#import. |
| 623 | SourceLocation Loc = D->getLocation(); |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 624 | Loc = SM->getInstantiationLoc(Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 625 | |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 626 | // If this is for a builtin, ignore it. |
| 627 | if (Loc.isInvalid()) return; |
| 628 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 629 | // Look for built-in declarations that we need to refer during the rewrite. |
| 630 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 631 | RewriteFunctionDecl(FD); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 632 | } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) { |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 633 | // declared in <Foundation/NSString.h> |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 634 | if (strcmp(FVD->getNameAsCString(), "_NSConstantStringClassReference") == 0) { |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 635 | ConstantStringClassReference = FVD; |
| 636 | return; |
| 637 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 638 | } else if (ObjCInterfaceDecl *MD = dyn_cast<ObjCInterfaceDecl>(D)) { |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 639 | RewriteInterfaceDecl(MD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 640 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 641 | RewriteCategoryDecl(CD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 642 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 643 | RewriteProtocolDecl(PD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 644 | } else if (ObjCForwardProtocolDecl *FP = |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 645 | dyn_cast<ObjCForwardProtocolDecl>(D)){ |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 646 | RewriteForwardProtocolDecl(FP); |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 647 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
| 648 | // Recurse into linkage specifications |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 649 | for (DeclContext::decl_iterator DI = LSD->decls_begin(), |
| 650 | DIEnd = LSD->decls_end(); |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 651 | DI != DIEnd; ++DI) |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 652 | HandleTopLevelSingleDecl(*DI); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 653 | } |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 654 | // 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] | 655 | if (SM->isFromMainFile(Loc)) |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 656 | return HandleDeclInMainFile(D); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 659 | //===----------------------------------------------------------------------===// |
| 660 | // Syntactic (non-AST) Rewriting Code |
| 661 | //===----------------------------------------------------------------------===// |
| 662 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 663 | void RewriteObjC::RewriteInclude() { |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 664 | SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 665 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
| 666 | const char *MainBufStart = MainBuf.first; |
| 667 | const char *MainBufEnd = MainBuf.second; |
| 668 | size_t ImportLen = strlen("import"); |
| 669 | size_t IncludeLen = strlen("include"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 670 | |
Fariborz Jahanian | af57b46 | 2008-01-19 01:03:17 +0000 | [diff] [blame] | 671 | // Loop over the whole file, looking for includes. |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 672 | for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { |
| 673 | if (*BufPtr == '#') { |
| 674 | if (++BufPtr == MainBufEnd) |
| 675 | return; |
| 676 | while (*BufPtr == ' ' || *BufPtr == '\t') |
| 677 | if (++BufPtr == MainBufEnd) |
| 678 | return; |
| 679 | if (!strncmp(BufPtr, "import", ImportLen)) { |
| 680 | // replace import with include |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 681 | SourceLocation ImportLoc = |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 682 | LocStart.getFileLocWithOffset(BufPtr-MainBufStart); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 683 | ReplaceText(ImportLoc, ImportLen, "include", IncludeLen); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 684 | BufPtr += ImportLen; |
| 685 | } |
| 686 | } |
| 687 | } |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 690 | void RewriteObjC::RewriteTabs() { |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 691 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
| 692 | const char *MainBufStart = MainBuf.first; |
| 693 | const char *MainBufEnd = MainBuf.second; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 694 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 695 | // Loop over the whole file, looking for tabs. |
| 696 | for (const char *BufPtr = MainBufStart; BufPtr != MainBufEnd; ++BufPtr) { |
| 697 | if (*BufPtr != '\t') |
| 698 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 699 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 700 | // Okay, we found a tab. This tab will turn into at least one character, |
| 701 | // but it depends on which 'virtual column' it is in. Compute that now. |
| 702 | unsigned VCol = 0; |
| 703 | while (BufPtr-VCol != MainBufStart && BufPtr[-VCol-1] != '\t' && |
| 704 | BufPtr[-VCol-1] != '\n' && BufPtr[-VCol-1] != '\r') |
| 705 | ++VCol; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 706 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 707 | // Okay, now that we know the virtual column, we know how many spaces to |
| 708 | // insert. We assume 8-character tab-stops. |
| 709 | unsigned Spaces = 8-(VCol & 7); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 710 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 711 | // Get the location of the tab. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 712 | SourceLocation TabLoc = SM->getLocForStartOfFile(MainFileID); |
| 713 | TabLoc = TabLoc.getFileLocWithOffset(BufPtr-MainBufStart); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 714 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 715 | // Rewrite the single tab character into a sequence of spaces. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 716 | ReplaceText(TabLoc, 1, " ", Spaces); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 717 | } |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 718 | } |
| 719 | |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 720 | static std::string getIvarAccessString(ObjCInterfaceDecl *ClassDecl, |
| 721 | ObjCIvarDecl *OID) { |
| 722 | std::string S; |
| 723 | S = "((struct "; |
| 724 | S += ClassDecl->getIdentifier()->getName(); |
| 725 | S += "_IMPL *)self)->"; |
Daniel Dunbar | 5ffe14c | 2009-10-18 20:26:27 +0000 | [diff] [blame] | 726 | S += OID->getName(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 727 | return S; |
| 728 | } |
| 729 | |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 730 | void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 731 | ObjCImplementationDecl *IMD, |
| 732 | ObjCCategoryImplDecl *CID) { |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 733 | SourceLocation startLoc = PID->getLocStart(); |
| 734 | InsertText(startLoc, "// ", 3); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 735 | const char *startBuf = SM->getCharacterData(startLoc); |
| 736 | assert((*startBuf == '@') && "bogus @synthesize location"); |
| 737 | const char *semiBuf = strchr(startBuf, ';'); |
| 738 | assert((*semiBuf == ';') && "@synthesize: can't find ';'"); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 739 | SourceLocation onePastSemiLoc = |
| 740 | startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 741 | |
| 742 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 743 | return; // FIXME: is this correct? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 744 | |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 745 | // Generate the 'getter' function. |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 746 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 747 | ObjCInterfaceDecl *ClassDecl = PD->getGetterMethodDecl()->getClassInterface(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 748 | ObjCIvarDecl *OID = PID->getPropertyIvarDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 749 | |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 750 | if (!OID) |
| 751 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 752 | |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 753 | std::string Getr; |
| 754 | RewriteObjCMethodDecl(PD->getGetterMethodDecl(), Getr); |
| 755 | Getr += "{ "; |
| 756 | // Synthesize an explicit cast to gain access to the ivar. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 757 | // FIXME: deal with code generation implications for various property |
| 758 | // attributes (copy, retain, nonatomic). |
Steve Naroff | 3539cdb | 2008-12-02 17:54:50 +0000 | [diff] [blame] | 759 | // See objc-act.c:objc_synthesize_new_getter() for details. |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 760 | Getr += "return " + getIvarAccessString(ClassDecl, OID); |
| 761 | Getr += "; }"; |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 762 | InsertText(onePastSemiLoc, Getr.c_str(), Getr.size()); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 763 | if (PD->isReadOnly()) |
| 764 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 765 | |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 766 | // Generate the 'setter' function. |
| 767 | std::string Setr; |
| 768 | RewriteObjCMethodDecl(PD->getSetterMethodDecl(), Setr); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 769 | Setr += "{ "; |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 770 | // Synthesize an explicit cast to initialize the ivar. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 771 | // FIXME: deal with code generation implications for various property |
| 772 | // attributes (copy, retain, nonatomic). |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 773 | // See objc-act.c:objc_synthesize_new_setter() for details. |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 774 | Setr += getIvarAccessString(ClassDecl, OID) + " = "; |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 775 | Setr += PD->getNameAsCString(); |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 776 | Setr += "; }"; |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 777 | InsertText(onePastSemiLoc, Setr.c_str(), Setr.size()); |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 778 | } |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 779 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 780 | void RewriteObjC::RewriteForwardClassDecl(ObjCClassDecl *ClassDecl) { |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 781 | // Get the start location and compute the semi location. |
| 782 | SourceLocation startLoc = ClassDecl->getLocation(); |
| 783 | const char *startBuf = SM->getCharacterData(startLoc); |
| 784 | const char *semiPtr = strchr(startBuf, ';'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 785 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 786 | // Translate to typedef's that forward reference structs with the same name |
| 787 | // as the class. As a convenience, we include the original declaration |
| 788 | // as a comment. |
| 789 | std::string typedefString; |
| 790 | typedefString += "// "; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 791 | typedefString.append(startBuf, semiPtr-startBuf+1); |
| 792 | typedefString += "\n"; |
Chris Lattner | 6795605 | 2009-02-20 18:04:31 +0000 | [diff] [blame] | 793 | for (ObjCClassDecl::iterator I = ClassDecl->begin(), E = ClassDecl->end(); |
| 794 | I != E; ++I) { |
Ted Kremenek | 321c22f | 2009-11-18 00:28:11 +0000 | [diff] [blame] | 795 | ObjCInterfaceDecl *ForwardDecl = I->getInterface(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 796 | typedefString += "#ifndef _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 797 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 798 | typedefString += "\n"; |
| 799 | typedefString += "#define _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 800 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 801 | typedefString += "\n"; |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 802 | typedefString += "typedef struct objc_object "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 803 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 804 | typedefString += ";\n#endif\n"; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 805 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 806 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 807 | // Replace the @class with typedefs corresponding to the classes. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 808 | ReplaceText(startLoc, semiPtr-startBuf+1, |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 809 | typedefString.c_str(), typedefString.size()); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 810 | } |
| 811 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 812 | void RewriteObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) { |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 813 | SourceLocation LocStart = Method->getLocStart(); |
| 814 | SourceLocation LocEnd = Method->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 815 | |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 816 | if (SM->getInstantiationLineNumber(LocEnd) > |
| 817 | SM->getInstantiationLineNumber(LocStart)) { |
Steve Naroff | 94ac21e | 2008-10-21 13:37:27 +0000 | [diff] [blame] | 818 | InsertText(LocStart, "#if 0\n", 6); |
| 819 | ReplaceText(LocEnd, 1, ";\n#endif\n", 9); |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 820 | } else { |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 821 | InsertText(LocStart, "// ", 3); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 822 | } |
| 823 | } |
| 824 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 825 | void RewriteObjC::RewriteProperty(ObjCPropertyDecl *prop) { |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 826 | SourceLocation Loc = prop->getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 827 | |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 828 | ReplaceText(Loc, 0, "// ", 3); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 829 | |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 830 | // FIXME: handle properties that are declared across multiple lines. |
Fariborz Jahanian | 957cf65 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 833 | void RewriteObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 834 | SourceLocation LocStart = CatDecl->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 835 | |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 836 | // FIXME: handle category headers that are declared across multiple lines. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 837 | ReplaceText(LocStart, 0, "// ", 3); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 838 | |
| 839 | for (ObjCCategoryDecl::instmeth_iterator |
| 840 | I = CatDecl->instmeth_begin(), E = CatDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 841 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 842 | RewriteMethodDeclaration(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 843 | for (ObjCCategoryDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 844 | I = CatDecl->classmeth_begin(), E = CatDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 845 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 846 | RewriteMethodDeclaration(*I); |
| 847 | |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 848 | // Lastly, comment out the @end. |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 849 | ReplaceText(CatDecl->getAtEndRange().getBegin(), 0, "// ", 3); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 850 | } |
| 851 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 852 | void RewriteObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 853 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 854 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 855 | SourceLocation LocStart = PDecl->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 856 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 857 | // FIXME: handle protocol headers that are declared across multiple lines. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 858 | ReplaceText(LocStart, 0, "// ", 3); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 859 | |
| 860 | for (ObjCProtocolDecl::instmeth_iterator |
| 861 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 862 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 863 | RewriteMethodDeclaration(*I); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 864 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 865 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 866 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 867 | RewriteMethodDeclaration(*I); |
| 868 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 869 | // Lastly, comment out the @end. |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 870 | SourceLocation LocEnd = PDecl->getAtEndRange().getBegin(); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 871 | ReplaceText(LocEnd, 0, "// ", 3); |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 872 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 873 | // Must comment out @optional/@required |
| 874 | const char *startBuf = SM->getCharacterData(LocStart); |
| 875 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 876 | for (const char *p = startBuf; p < endBuf; p++) { |
| 877 | if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { |
| 878 | std::string CommentedOptional = "/* @optional */"; |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 879 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 880 | ReplaceText(OptionalLoc, strlen("@optional"), |
| 881 | CommentedOptional.c_str(), CommentedOptional.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 882 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 883 | } |
| 884 | else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { |
| 885 | std::string CommentedRequired = "/* @required */"; |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 886 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 887 | ReplaceText(OptionalLoc, strlen("@required"), |
| 888 | CommentedRequired.c_str(), CommentedRequired.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 889 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 890 | } |
| 891 | } |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 894 | void RewriteObjC::RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *PDecl) { |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 895 | SourceLocation LocStart = PDecl->getLocation(); |
Steve Naroff | b7fa992 | 2007-11-14 03:37:28 +0000 | [diff] [blame] | 896 | if (LocStart.isInvalid()) |
| 897 | assert(false && "Invalid SourceLocation"); |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 898 | // FIXME: handle forward protocol that are declared across multiple lines. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 899 | ReplaceText(LocStart, 0, "// ", 3); |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 900 | } |
| 901 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 902 | void RewriteObjC::RewriteObjCMethodDecl(ObjCMethodDecl *OMD, |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 903 | std::string &ResultStr) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 904 | //fprintf(stderr,"In RewriteObjCMethodDecl\n"); |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 905 | const FunctionType *FPRetType = 0; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 906 | ResultStr += "\nstatic "; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 907 | if (OMD->getResultType()->isObjCQualifiedIdType()) |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 908 | ResultStr += "id"; |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 909 | else if (OMD->getResultType()->isFunctionPointerType() || |
| 910 | OMD->getResultType()->isBlockPointerType()) { |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 911 | // needs special handling, since pointer-to-functions have special |
| 912 | // syntax (where a decaration models use). |
| 913 | QualType retType = OMD->getResultType(); |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 914 | QualType PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 915 | if (const PointerType* PT = retType->getAs<PointerType>()) |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 916 | PointeeTy = PT->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 917 | else if (const BlockPointerType *BPT = retType->getAs<BlockPointerType>()) |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 918 | PointeeTy = BPT->getPointeeType(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 919 | if ((FPRetType = PointeeTy->getAs<FunctionType>())) { |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 920 | ResultStr += FPRetType->getResultType().getAsString(); |
| 921 | ResultStr += "(*"; |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 922 | } |
| 923 | } else |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 924 | ResultStr += OMD->getResultType().getAsString(); |
Fariborz Jahanian | 531a1ea | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 925 | ResultStr += " "; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 926 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 927 | // Unique method name |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 928 | std::string NameStr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 929 | |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 930 | if (OMD->isInstanceMethod()) |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 931 | NameStr += "_I_"; |
| 932 | else |
| 933 | NameStr += "_C_"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 934 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 935 | NameStr += OMD->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 936 | NameStr += "_"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 937 | |
| 938 | if (ObjCCategoryImplDecl *CID = |
Steve Naroff | 3e0a540 | 2009-01-08 19:41:02 +0000 | [diff] [blame] | 939 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 940 | NameStr += CID->getNameAsString(); |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 941 | NameStr += "_"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 942 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 943 | // Append selector names, replacing ':' with '_' |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 944 | { |
| 945 | std::string selString = OMD->getSelector().getAsString(); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 946 | int len = selString.size(); |
| 947 | for (int i = 0; i < len; i++) |
| 948 | if (selString[i] == ':') |
| 949 | selString[i] = '_'; |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 950 | NameStr += selString; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 951 | } |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 952 | // Remember this name for metadata emission |
| 953 | MethodInternalNames[OMD] = NameStr; |
| 954 | ResultStr += NameStr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 955 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 956 | // Rewrite arguments |
| 957 | ResultStr += "("; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 958 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 959 | // invisible arguments |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 960 | if (OMD->isInstanceMethod()) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 961 | QualType selfTy = Context->getObjCInterfaceType(OMD->getClassInterface()); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 962 | selfTy = Context->getPointerType(selfTy); |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 963 | if (!LangOpts.Microsoft) { |
| 964 | if (ObjCSynthesizedStructs.count(OMD->getClassInterface())) |
| 965 | ResultStr += "struct "; |
| 966 | } |
| 967 | // When rewriting for Microsoft, explicitly omit the structure name. |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 968 | ResultStr += OMD->getClassInterface()->getNameAsString(); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 969 | ResultStr += " *"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 970 | } |
| 971 | else |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 972 | ResultStr += Context->getObjCClassType().getAsString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 973 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 974 | ResultStr += " self, "; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 975 | ResultStr += Context->getObjCSelType().getAsString(); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 976 | ResultStr += " _cmd"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 977 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 978 | // Method arguments. |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 979 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 980 | E = OMD->param_end(); PI != E; ++PI) { |
| 981 | ParmVarDecl *PDecl = *PI; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 982 | ResultStr += ", "; |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 983 | if (PDecl->getType()->isObjCQualifiedIdType()) { |
| 984 | ResultStr += "id "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 985 | ResultStr += PDecl->getNameAsString(); |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 986 | } else { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 987 | std::string Name = PDecl->getNameAsString(); |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 988 | if (isTopLevelBlockPointerType(PDecl->getType())) { |
Steve Naroff | c8ad87b | 2008-10-30 14:45:29 +0000 | [diff] [blame] | 989 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 990 | const BlockPointerType *BPT = PDecl->getType()->getAs<BlockPointerType>(); |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 991 | Context->getPointerType(BPT->getPointeeType()).getAsStringInternal(Name, |
| 992 | Context->PrintingPolicy); |
Steve Naroff | c8ad87b | 2008-10-30 14:45:29 +0000 | [diff] [blame] | 993 | } else |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 994 | PDecl->getType().getAsStringInternal(Name, Context->PrintingPolicy); |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 995 | ResultStr += Name; |
| 996 | } |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 997 | } |
Fariborz Jahanian | 7c39ff7 | 2008-01-21 20:14:23 +0000 | [diff] [blame] | 998 | if (OMD->isVariadic()) |
| 999 | ResultStr += ", ..."; |
Fariborz Jahanian | 531a1ea | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 1000 | ResultStr += ") "; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1001 | |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1002 | if (FPRetType) { |
| 1003 | ResultStr += ")"; // close the precedence "scope" for "*". |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1004 | |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1005 | // Now, emit the argument types (if any). |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1006 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) { |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1007 | ResultStr += "("; |
| 1008 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 1009 | if (i) ResultStr += ", "; |
| 1010 | std::string ParamStr = FT->getArgType(i).getAsString(); |
| 1011 | ResultStr += ParamStr; |
| 1012 | } |
| 1013 | if (FT->isVariadic()) { |
| 1014 | if (FT->getNumArgs()) ResultStr += ", "; |
| 1015 | ResultStr += "..."; |
| 1016 | } |
| 1017 | ResultStr += ")"; |
| 1018 | } else { |
| 1019 | ResultStr += "()"; |
| 1020 | } |
| 1021 | } |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1022 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1023 | void RewriteObjC::RewriteImplementationDecl(Decl *OID) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1024 | ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID); |
| 1025 | ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1026 | |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 1027 | if (IMD) |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1028 | InsertText(IMD->getLocStart(), "// ", 3); |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 1029 | else |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1030 | InsertText(CID->getLocStart(), "// ", 3); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1031 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1032 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1033 | I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(), |
| 1034 | E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1035 | I != E; ++I) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1036 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1037 | ObjCMethodDecl *OMD = *I; |
| 1038 | RewriteObjCMethodDecl(OMD, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1039 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1040 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1041 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1042 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1043 | const char *endBuf = SM->getCharacterData(LocEnd); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1044 | ReplaceText(LocStart, endBuf-startBuf, |
| 1045 | ResultStr.c_str(), ResultStr.size()); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1046 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1047 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1048 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1049 | I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(), |
| 1050 | E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1051 | I != E; ++I) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1052 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1053 | ObjCMethodDecl *OMD = *I; |
| 1054 | RewriteObjCMethodDecl(OMD, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1055 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1056 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1057 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1058 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1059 | const char *endBuf = SM->getCharacterData(LocEnd); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1060 | ReplaceText(LocStart, endBuf-startBuf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1061 | ResultStr.c_str(), ResultStr.size()); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1062 | } |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1063 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1064 | I = IMD ? IMD->propimpl_begin() : CID->propimpl_begin(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1065 | E = IMD ? IMD->propimpl_end() : CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1066 | I != E; ++I) { |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 1067 | RewritePropertyImplDecl(*I, IMD, CID); |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 1070 | if (IMD) |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1071 | InsertText(IMD->getLocEnd(), "// ", 3); |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 1072 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1073 | InsertText(CID->getLocEnd(), "// ", 3); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1076 | void RewriteObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { |
Steve Naroff | f908a87 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 1077 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1078 | if (!ObjCForwardDecls.count(ClassDecl)) { |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1079 | // we haven't seen a forward decl - generate a typedef. |
Steve Naroff | 5086a8d | 2007-11-14 23:02:56 +0000 | [diff] [blame] | 1080 | ResultStr = "#ifndef _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1081 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1082 | ResultStr += "\n"; |
| 1083 | ResultStr += "#define _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1084 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1085 | ResultStr += "\n"; |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1086 | ResultStr += "typedef struct objc_object "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1087 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1088 | ResultStr += ";\n#endif\n"; |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1089 | // Mark this typedef as having been generated. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1090 | ObjCForwardDecls.insert(ClassDecl); |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1091 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1092 | SynthesizeObjCInternalStruct(ClassDecl, ResultStr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1093 | |
| 1094 | for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1095 | E = ClassDecl->prop_end(); I != E; ++I) |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 1096 | RewriteProperty(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1097 | for (ObjCInterfaceDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1098 | I = ClassDecl->instmeth_begin(), E = ClassDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1099 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1100 | RewriteMethodDeclaration(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1101 | for (ObjCInterfaceDecl::classmeth_iterator |
| 1102 | I = ClassDecl->classmeth_begin(), E = ClassDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1103 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1104 | RewriteMethodDeclaration(*I); |
| 1105 | |
Steve Naroff | 2feac5e | 2007-10-30 03:43:13 +0000 | [diff] [blame] | 1106 | // Lastly, comment out the @end. |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1107 | ReplaceText(ClassDecl->getAtEndRange().getBegin(), 0, "// ", 3); |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 1110 | Stmt *RewriteObjC::RewritePropertySetter(BinaryOperator *BinOp, Expr *newStmt, |
| 1111 | SourceRange SrcRange) { |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1112 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr. |
| 1113 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
| 1114 | ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS()); |
| 1115 | ObjCMessageExpr *MsgExpr; |
| 1116 | ObjCPropertyDecl *PDecl = PropRefExpr->getProperty(); |
| 1117 | llvm::SmallVector<Expr *, 1> ExprVec; |
| 1118 | ExprVec.push_back(newStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1119 | |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1120 | Stmt *Receiver = PropRefExpr->getBase(); |
| 1121 | ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(Receiver); |
| 1122 | if (PRE && PropGetters[PRE]) { |
| 1123 | // This allows us to handle chain/nested property getters. |
| 1124 | Receiver = PropGetters[PRE]; |
| 1125 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1126 | MsgExpr = new (Context) ObjCMessageExpr(dyn_cast<Expr>(Receiver), |
| 1127 | PDecl->getSetterName(), PDecl->getType(), |
| 1128 | PDecl->getSetterMethodDecl(), |
| 1129 | SourceLocation(), SourceLocation(), |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1130 | &ExprVec[0], 1); |
| 1131 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1132 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1133 | // Now do the actual rewrite. |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 1134 | ReplaceStmtWithRange(BinOp, ReplacingStmt, SrcRange); |
Steve Naroff | e58ee0c | 2008-12-10 14:53:27 +0000 | [diff] [blame] | 1135 | //delete BinOp; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1136 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1137 | // to things that stay around. |
| 1138 | Context->Deallocate(MsgExpr); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1139 | return ReplacingStmt; |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1142 | Stmt *RewriteObjC::RewritePropertyGetter(ObjCPropertyRefExpr *PropRefExpr) { |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1143 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr. |
| 1144 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
| 1145 | ObjCMessageExpr *MsgExpr; |
| 1146 | ObjCPropertyDecl *PDecl = PropRefExpr->getProperty(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1147 | |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1148 | Stmt *Receiver = PropRefExpr->getBase(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1149 | |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1150 | ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(Receiver); |
| 1151 | if (PRE && PropGetters[PRE]) { |
| 1152 | // This allows us to handle chain/nested property getters. |
| 1153 | Receiver = PropGetters[PRE]; |
| 1154 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1155 | MsgExpr = new (Context) ObjCMessageExpr(dyn_cast<Expr>(Receiver), |
| 1156 | PDecl->getGetterName(), PDecl->getType(), |
| 1157 | PDecl->getGetterMethodDecl(), |
| 1158 | SourceLocation(), SourceLocation(), |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1159 | 0, 0); |
| 1160 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1161 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1162 | |
| 1163 | if (!PropParentMap) |
| 1164 | PropParentMap = new ParentMap(CurrentBody); |
| 1165 | |
| 1166 | Stmt *Parent = PropParentMap->getParent(PropRefExpr); |
| 1167 | if (Parent && isa<ObjCPropertyRefExpr>(Parent)) { |
| 1168 | // We stash away the ReplacingStmt since actually doing the |
| 1169 | // replacement/rewrite won't work for nested getters (e.g. obj.p.i) |
| 1170 | PropGetters[PropRefExpr] = ReplacingStmt; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1171 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1172 | // to things that stay around. |
| 1173 | Context->Deallocate(MsgExpr); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1174 | return PropRefExpr; // return the original... |
| 1175 | } else { |
| 1176 | ReplaceStmt(PropRefExpr, ReplacingStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1177 | // delete PropRefExpr; elsewhere... |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1178 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1179 | // to things that stay around. |
| 1180 | Context->Deallocate(MsgExpr); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1181 | return ReplacingStmt; |
| 1182 | } |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1185 | Stmt *RewriteObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1186 | SourceLocation OrigStart) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1187 | ObjCIvarDecl *D = IV->getDecl(); |
Fariborz Jahanian | 84ed600 | 2010-01-07 18:18:32 +0000 | [diff] [blame] | 1188 | const Expr *BaseExpr = IV->getBase(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1189 | if (CurMethodDef) { |
Fariborz Jahanian | d83658d | 2010-01-07 22:15:31 +0000 | [diff] [blame] | 1190 | if (IV->isArrow() && isa<DeclRefExpr>(BaseExpr)) { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1191 | ObjCInterfaceType *iFaceDecl = |
Fariborz Jahanian | 84ed600 | 2010-01-07 18:18:32 +0000 | [diff] [blame] | 1192 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1193 | // lookup which class implements the instance variable. |
| 1194 | ObjCInterfaceDecl *clsDeclared = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1195 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1196 | clsDeclared); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1197 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1198 | |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1199 | // Synthesize an explicit cast to gain access to the ivar. |
| 1200 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1201 | RecName += "_IMPL"; |
| 1202 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str()); |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1203 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1204 | SourceLocation(), II); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1205 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1206 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1207 | CastExpr *castExpr = new (Context) CStyleCastExpr(castT, |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 1208 | CastExpr::CK_Unknown, |
| 1209 | IV->getBase(), |
| 1210 | castT,SourceLocation(), |
| 1211 | SourceLocation()); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1212 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1213 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
| 1214 | IV->getBase()->getLocEnd(), |
| 1215 | castExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1216 | if (IV->isFreeIvar() && |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1217 | CurMethodDef->getClassInterface() == iFaceDecl->getDecl()) { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1218 | MemberExpr *ME = new (Context) MemberExpr(PE, true, D, |
| 1219 | IV->getLocation(), |
| 1220 | D->getType()); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1221 | ReplaceStmt(IV, ME); |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1222 | // delete IV; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1223 | return ME; |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1224 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1225 | |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1226 | ReplaceStmt(IV->getBase(), PE); |
| 1227 | // Cannot delete IV->getBase(), since PE points to it. |
| 1228 | // Replace the old base with the cast. This is important when doing |
| 1229 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1230 | IV->setBase(PE); |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1231 | return IV; |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1232 | } |
Steve Naroff | 84472a8 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1233 | } else { // we are outside a method. |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1234 | assert(!IV->isFreeIvar() && "Cannot have a free standing ivar outside a method"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1235 | |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1236 | // Explicit ivar refs need to have a cast inserted. |
| 1237 | // FIXME: consider sharing some of this code with the code above. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1238 | if (const PointerType *pType = IV->getBase()->getType()->getAs<PointerType>()) { |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1239 | ObjCInterfaceType *iFaceDecl = dyn_cast<ObjCInterfaceType>(pType->getPointeeType()); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1240 | // lookup which class implements the instance variable. |
| 1241 | ObjCInterfaceDecl *clsDeclared = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1242 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1243 | clsDeclared); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1244 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1245 | |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1246 | // Synthesize an explicit cast to gain access to the ivar. |
| 1247 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1248 | RecName += "_IMPL"; |
| 1249 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str()); |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1250 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1251 | SourceLocation(), II); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1252 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1253 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1254 | CastExpr *castExpr = new (Context) CStyleCastExpr(castT, |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 1255 | CastExpr::CK_Unknown, |
| 1256 | IV->getBase(), |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1257 | castT, SourceLocation(), |
| 1258 | SourceLocation()); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1259 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1260 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
Chris Lattner | 8d36616 | 2008-05-28 16:38:23 +0000 | [diff] [blame] | 1261 | IV->getBase()->getLocEnd(), castExpr); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1262 | ReplaceStmt(IV->getBase(), PE); |
| 1263 | // Cannot delete IV->getBase(), since PE points to it. |
| 1264 | // Replace the old base with the cast. This is important when doing |
| 1265 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1266 | IV->setBase(PE); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1267 | return IV; |
| 1268 | } |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1269 | } |
Steve Naroff | 84472a8 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1270 | return IV; |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1273 | /// SynthCountByEnumWithState - To print: |
| 1274 | /// ((unsigned int (*) |
| 1275 | /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1276 | /// (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1277 | /// sel_registerName( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1278 | /// "countByEnumeratingWithState:objects:count:"), |
| 1279 | /// &enumState, |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1280 | /// (id *)items, (unsigned int)16) |
| 1281 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1282 | void RewriteObjC::SynthCountByEnumWithState(std::string &buf) { |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1283 | buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, " |
| 1284 | "id *, unsigned int))(void *)objc_msgSend)"; |
| 1285 | buf += "\n\t\t"; |
| 1286 | buf += "((id)l_collection,\n\t\t"; |
| 1287 | buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; |
| 1288 | buf += "\n\t\t"; |
| 1289 | buf += "&enumState, " |
| 1290 | "(id *)items, (unsigned int)16)"; |
| 1291 | } |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1292 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1293 | /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach |
| 1294 | /// statement to exit to its outer synthesized loop. |
| 1295 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1296 | Stmt *RewriteObjC::RewriteBreakStmt(BreakStmt *S) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1297 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1298 | return S; |
| 1299 | // replace break with goto __break_label |
| 1300 | std::string buf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1301 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1302 | SourceLocation startLoc = S->getLocStart(); |
| 1303 | buf = "goto __break_label_"; |
| 1304 | buf += utostr(ObjCBcLabelNo.back()); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1305 | ReplaceText(startLoc, strlen("break"), buf.c_str(), buf.size()); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1306 | |
| 1307 | return 0; |
| 1308 | } |
| 1309 | |
| 1310 | /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach |
| 1311 | /// statement to continue with its inner synthesized loop. |
| 1312 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1313 | Stmt *RewriteObjC::RewriteContinueStmt(ContinueStmt *S) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1314 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1315 | return S; |
| 1316 | // replace continue with goto __continue_label |
| 1317 | std::string buf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1318 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1319 | SourceLocation startLoc = S->getLocStart(); |
| 1320 | buf = "goto __continue_label_"; |
| 1321 | buf += utostr(ObjCBcLabelNo.back()); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1322 | ReplaceText(startLoc, strlen("continue"), buf.c_str(), buf.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1323 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1324 | return 0; |
| 1325 | } |
| 1326 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1327 | /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1328 | /// It rewrites: |
| 1329 | /// for ( type elem in collection) { stmts; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1330 | |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1331 | /// Into: |
| 1332 | /// { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1333 | /// type elem; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1334 | /// struct __objcFastEnumerationState enumState = { 0 }; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1335 | /// id items[16]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1336 | /// id l_collection = (id)collection; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1337 | /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1338 | /// objects:items count:16]; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1339 | /// if (limit) { |
| 1340 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1341 | /// do { |
| 1342 | /// unsigned long counter = 0; |
| 1343 | /// do { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1344 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1345 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1346 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1347 | /// stmts; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1348 | /// __continue_label: ; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1349 | /// } while (counter < limit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1350 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1351 | /// objects:items count:16]); |
| 1352 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1353 | /// __break_label: ; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1354 | /// } |
| 1355 | /// else |
| 1356 | /// elem = nil; |
| 1357 | /// } |
| 1358 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1359 | Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 1360 | SourceLocation OrigEnd) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1361 | assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1362 | assert(isa<ObjCForCollectionStmt>(Stmts.back()) && |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1363 | "ObjCForCollectionStmt Statement stack mismatch"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1364 | assert(!ObjCBcLabelNo.empty() && |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1365 | "ObjCForCollectionStmt - Label No stack empty"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1366 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1367 | SourceLocation startLoc = S->getLocStart(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1368 | const char *startBuf = SM->getCharacterData(startLoc); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1369 | const char *elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1370 | std::string elementTypeAsString; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1371 | std::string buf; |
| 1372 | buf = "\n{\n\t"; |
| 1373 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { |
| 1374 | // type elem; |
Chris Lattner | 7e24e82 | 2009-03-28 06:33:19 +0000 | [diff] [blame] | 1375 | NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl()); |
Ted Kremenek | 1ed8e2a | 2008-10-06 22:16:13 +0000 | [diff] [blame] | 1376 | QualType ElementType = cast<ValueDecl>(D)->getType(); |
Steve Naroff | e89b8e7 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1377 | if (ElementType->isObjCQualifiedIdType() || |
| 1378 | ElementType->isObjCQualifiedInterfaceType()) |
| 1379 | // Simply use 'id' for all qualified types. |
| 1380 | elementTypeAsString = "id"; |
| 1381 | else |
| 1382 | elementTypeAsString = ElementType.getAsString(); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1383 | buf += elementTypeAsString; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1384 | buf += " "; |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1385 | elementName = D->getNameAsCString(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1386 | buf += elementName; |
| 1387 | buf += ";\n\t"; |
| 1388 | } |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1389 | else { |
| 1390 | DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1391 | elementName = DR->getDecl()->getNameAsCString(); |
Steve Naroff | e89b8e7 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1392 | ValueDecl *VD = cast<ValueDecl>(DR->getDecl()); |
| 1393 | if (VD->getType()->isObjCQualifiedIdType() || |
| 1394 | VD->getType()->isObjCQualifiedInterfaceType()) |
| 1395 | // Simply use 'id' for all qualified types. |
| 1396 | elementTypeAsString = "id"; |
| 1397 | else |
| 1398 | elementTypeAsString = VD->getType().getAsString(); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1399 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1400 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1401 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1402 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
| 1403 | // id items[16]; |
| 1404 | buf += "id items[16];\n\t"; |
| 1405 | // id l_collection = (id) |
| 1406 | buf += "id l_collection = (id)"; |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1407 | // Find start location of 'collection' the hard way! |
| 1408 | const char *startCollectionBuf = startBuf; |
| 1409 | startCollectionBuf += 3; // skip 'for' |
| 1410 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1411 | startCollectionBuf++; // skip '(' |
| 1412 | // find 'in' and skip it. |
| 1413 | while (*startCollectionBuf != ' ' || |
| 1414 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1415 | (*(startCollectionBuf+3) != ' ' && |
| 1416 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1417 | startCollectionBuf++; |
| 1418 | startCollectionBuf += 3; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1419 | |
| 1420 | // Replace: "for (type element in" with string constructed thus far. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1421 | ReplaceText(startLoc, startCollectionBuf - startBuf, |
| 1422 | buf.c_str(), buf.size()); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1423 | // Replace ')' in for '(' type elem in collection ')' with ';' |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1424 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1425 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
| 1426 | SourceLocation lparenLoc = startLoc.getFileLocWithOffset(rparenBuf-startBuf); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1427 | buf = ";\n\t"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1428 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1429 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1430 | // objects:items count:16]; |
| 1431 | // which is synthesized into: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1432 | // unsigned int limit = |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1433 | // ((unsigned int (*) |
| 1434 | // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1435 | // (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1436 | // sel_registerName( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1437 | // "countByEnumeratingWithState:objects:count:"), |
| 1438 | // (struct __objcFastEnumerationState *)&state, |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1439 | // (id *)items, (unsigned int)16); |
| 1440 | buf += "unsigned long limit =\n\t\t"; |
| 1441 | SynthCountByEnumWithState(buf); |
| 1442 | buf += ";\n\t"; |
| 1443 | /// if (limit) { |
| 1444 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1445 | /// do { |
| 1446 | /// unsigned long counter = 0; |
| 1447 | /// do { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1448 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1449 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1450 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1451 | buf += "if (limit) {\n\t"; |
| 1452 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1453 | buf += "do {\n\t\t"; |
| 1454 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1455 | buf += "do {\n\t\t\t"; |
| 1456 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1457 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1458 | buf += elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1459 | buf += " = ("; |
| 1460 | buf += elementTypeAsString; |
| 1461 | buf += ")enumState.itemsPtr[counter++];"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1462 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1463 | ReplaceText(lparenLoc, 1, buf.c_str(), buf.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1464 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1465 | /// __continue_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1466 | /// } while (counter < limit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1467 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1468 | /// objects:items count:16]); |
| 1469 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1470 | /// __break_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1471 | /// } |
| 1472 | /// else |
| 1473 | /// elem = nil; |
| 1474 | /// } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1475 | /// |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1476 | buf = ";\n\t"; |
| 1477 | buf += "__continue_label_"; |
| 1478 | buf += utostr(ObjCBcLabelNo.back()); |
| 1479 | buf += ": ;"; |
| 1480 | buf += "\n\t\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1481 | buf += "} while (counter < limit);\n\t"; |
| 1482 | buf += "} while (limit = "; |
| 1483 | SynthCountByEnumWithState(buf); |
| 1484 | buf += ");\n\t"; |
| 1485 | buf += elementName; |
Fariborz Jahanian | 65b0aa5 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1486 | buf += " = (("; |
| 1487 | buf += elementTypeAsString; |
| 1488 | buf += ")0);\n\t"; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1489 | buf += "__break_label_"; |
| 1490 | buf += utostr(ObjCBcLabelNo.back()); |
| 1491 | buf += ": ;\n\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1492 | buf += "}\n\t"; |
| 1493 | buf += "else\n\t\t"; |
| 1494 | buf += elementName; |
Fariborz Jahanian | 65b0aa5 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1495 | buf += " = (("; |
| 1496 | buf += elementTypeAsString; |
| 1497 | buf += ")0);\n\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1498 | buf += "}\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1499 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1500 | // Insert all these *after* the statement body. |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1501 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1502 | if (isa<CompoundStmt>(S->getBody())) { |
| 1503 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(1); |
| 1504 | InsertText(endBodyLoc, buf.c_str(), buf.size()); |
| 1505 | } else { |
| 1506 | /* Need to treat single statements specially. For example: |
| 1507 | * |
| 1508 | * for (A *a in b) if (stuff()) break; |
| 1509 | * for (A *a in b) xxxyy; |
| 1510 | * |
| 1511 | * The following code simply scans ahead to the semi to find the actual end. |
| 1512 | */ |
| 1513 | const char *stmtBuf = SM->getCharacterData(OrigEnd); |
| 1514 | const char *semiBuf = strchr(stmtBuf, ';'); |
| 1515 | assert(semiBuf && "Can't find ';'"); |
| 1516 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(semiBuf-stmtBuf+1); |
| 1517 | InsertText(endBodyLoc, buf.c_str(), buf.size()); |
| 1518 | } |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1519 | Stmts.pop_back(); |
| 1520 | ObjCBcLabelNo.pop_back(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1521 | return 0; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1524 | /// RewriteObjCSynchronizedStmt - |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1525 | /// This routine rewrites @synchronized(expr) stmt; |
| 1526 | /// into: |
| 1527 | /// objc_sync_enter(expr); |
| 1528 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1529 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1530 | Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1531 | // Get the start location and compute the semi location. |
| 1532 | SourceLocation startLoc = S->getLocStart(); |
| 1533 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1534 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1535 | assert((*startBuf == '@') && "bogus @synchronized location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1536 | |
| 1537 | std::string buf; |
Steve Naroff | 3498cc9 | 2008-08-21 13:03:03 +0000 | [diff] [blame] | 1538 | buf = "objc_sync_enter((id)"; |
| 1539 | const char *lparenBuf = startBuf; |
| 1540 | while (*lparenBuf != '(') lparenBuf++; |
| 1541 | ReplaceText(startLoc, lparenBuf-startBuf+1, buf.c_str(), buf.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1542 | // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since |
| 1543 | // the sync expression is typically a message expression that's already |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1544 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1545 | SourceLocation endLoc = S->getSynchBody()->getLocStart(); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1546 | const char *endBuf = SM->getCharacterData(endLoc); |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1547 | while (*endBuf != ')') endBuf--; |
| 1548 | SourceLocation rparenLoc = startLoc.getFileLocWithOffset(endBuf-startBuf); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1549 | buf = ");\n"; |
| 1550 | // declare a new scope with two variables, _stack and _rethrow. |
| 1551 | buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n"; |
| 1552 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1553 | buf += "char *pointers[4];} _stack;\n"; |
| 1554 | buf += "id volatile _rethrow = 0;\n"; |
| 1555 | buf += "objc_exception_try_enter(&_stack);\n"; |
| 1556 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1557 | ReplaceText(rparenLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1558 | startLoc = S->getSynchBody()->getLocEnd(); |
| 1559 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1560 | |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1561 | assert((*startBuf == '}') && "bogus @synchronized block"); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1562 | SourceLocation lastCurlyLoc = startLoc; |
| 1563 | buf = "}\nelse {\n"; |
| 1564 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 1565 | buf += "}\n"; |
| 1566 | buf += "{ /* implicit finally clause */\n"; |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1567 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1568 | |
| 1569 | std::string syncBuf; |
| 1570 | syncBuf += " objc_sync_exit("; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1571 | Expr *syncExpr = new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 1572 | CastExpr::CK_Unknown, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1573 | S->getSynchExpr(), |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1574 | Context->getObjCIdType(), |
| 1575 | SourceLocation(), |
| 1576 | SourceLocation()); |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1577 | std::string syncExprBufS; |
| 1578 | llvm::raw_string_ostream syncExprBuf(syncExprBufS); |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1579 | syncExpr->printPretty(syncExprBuf, *Context, 0, |
| 1580 | PrintingPolicy(LangOpts)); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1581 | syncBuf += syncExprBuf.str(); |
| 1582 | syncBuf += ");"; |
| 1583 | |
| 1584 | buf += syncBuf; |
| 1585 | buf += "\n if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1586 | buf += "}\n"; |
| 1587 | buf += "}"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1588 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1589 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1590 | |
| 1591 | bool hasReturns = false; |
| 1592 | HasReturnStmts(S->getSynchBody(), hasReturns); |
| 1593 | if (hasReturns) |
| 1594 | RewriteSyncReturnStmts(S->getSynchBody(), syncBuf); |
| 1595 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1596 | return 0; |
| 1597 | } |
| 1598 | |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1599 | void RewriteObjC::WarnAboutReturnGotoStmts(Stmt *S) |
| 1600 | { |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1601 | // Perform a bottom up traversal of all children. |
| 1602 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1603 | CI != E; ++CI) |
| 1604 | if (*CI) |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1605 | WarnAboutReturnGotoStmts(*CI); |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1606 | |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1607 | if (isa<ReturnStmt>(S) || isa<GotoStmt>(S)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1608 | Diags.Report(Context->getFullLoc(S->getLocStart()), |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1609 | TryFinallyContainsReturnDiag); |
| 1610 | } |
| 1611 | return; |
| 1612 | } |
| 1613 | |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1614 | void RewriteObjC::HasReturnStmts(Stmt *S, bool &hasReturns) |
| 1615 | { |
| 1616 | // Perform a bottom up traversal of all children. |
| 1617 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1618 | CI != E; ++CI) |
| 1619 | if (*CI) |
| 1620 | HasReturnStmts(*CI, hasReturns); |
| 1621 | |
| 1622 | if (isa<ReturnStmt>(S)) |
| 1623 | hasReturns = true; |
| 1624 | return; |
| 1625 | } |
| 1626 | |
| 1627 | void RewriteObjC::RewriteTryReturnStmts(Stmt *S) { |
| 1628 | // Perform a bottom up traversal of all children. |
| 1629 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1630 | CI != E; ++CI) |
| 1631 | if (*CI) { |
| 1632 | RewriteTryReturnStmts(*CI); |
| 1633 | } |
| 1634 | if (isa<ReturnStmt>(S)) { |
| 1635 | SourceLocation startLoc = S->getLocStart(); |
| 1636 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1637 | |
| 1638 | const char *semiBuf = strchr(startBuf, ';'); |
| 1639 | assert((*semiBuf == ';') && "RewriteTryReturnStmts: can't find ';'"); |
| 1640 | SourceLocation onePastSemiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
| 1641 | |
| 1642 | std::string buf; |
| 1643 | buf = "{ objc_exception_try_exit(&_stack); return"; |
| 1644 | |
| 1645 | ReplaceText(startLoc, 6, buf.c_str(), buf.size()); |
| 1646 | InsertText(onePastSemiLoc, "}", 1); |
| 1647 | } |
| 1648 | return; |
| 1649 | } |
| 1650 | |
| 1651 | void RewriteObjC::RewriteSyncReturnStmts(Stmt *S, std::string syncExitBuf) { |
| 1652 | // Perform a bottom up traversal of all children. |
| 1653 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1654 | CI != E; ++CI) |
| 1655 | if (*CI) { |
| 1656 | RewriteSyncReturnStmts(*CI, syncExitBuf); |
| 1657 | } |
| 1658 | if (isa<ReturnStmt>(S)) { |
| 1659 | SourceLocation startLoc = S->getLocStart(); |
| 1660 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1661 | |
| 1662 | const char *semiBuf = strchr(startBuf, ';'); |
| 1663 | assert((*semiBuf == ';') && "RewriteSyncReturnStmts: can't find ';'"); |
| 1664 | SourceLocation onePastSemiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
| 1665 | |
| 1666 | std::string buf; |
| 1667 | buf = "{ objc_exception_try_exit(&_stack);"; |
| 1668 | buf += syncExitBuf; |
| 1669 | buf += " return"; |
| 1670 | |
| 1671 | ReplaceText(startLoc, 6, buf.c_str(), buf.size()); |
| 1672 | InsertText(onePastSemiLoc, "}", 1); |
| 1673 | } |
| 1674 | return; |
| 1675 | } |
| 1676 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1677 | Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1678 | // Get the start location and compute the semi location. |
| 1679 | SourceLocation startLoc = S->getLocStart(); |
| 1680 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1681 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1682 | assert((*startBuf == '@') && "bogus @try location"); |
| 1683 | |
| 1684 | std::string buf; |
| 1685 | // declare a new scope with two variables, _stack and _rethrow. |
| 1686 | buf = "/* @try scope begin */ { struct _objc_exception_data {\n"; |
| 1687 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1688 | buf += "char *pointers[4];} _stack;\n"; |
| 1689 | buf += "id volatile _rethrow = 0;\n"; |
| 1690 | buf += "objc_exception_try_enter(&_stack);\n"; |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1691 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1692 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1693 | ReplaceText(startLoc, 4, buf.c_str(), buf.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1694 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1695 | startLoc = S->getTryBody()->getLocEnd(); |
| 1696 | startBuf = SM->getCharacterData(startLoc); |
| 1697 | |
| 1698 | assert((*startBuf == '}') && "bogus @try block"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1699 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1700 | SourceLocation lastCurlyLoc = startLoc; |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1701 | ObjCAtCatchStmt *catchList = S->getCatchStmts(); |
| 1702 | if (catchList) { |
| 1703 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1704 | buf = " /* @catch begin */ else {\n"; |
| 1705 | buf += " id _caught = objc_exception_extract(&_stack);\n"; |
| 1706 | buf += " objc_exception_try_enter (&_stack);\n"; |
| 1707 | buf += " if (_setjmp(_stack.buf))\n"; |
| 1708 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1709 | buf += " else { /* @catch continue */"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1710 | |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1711 | InsertText(startLoc, buf.c_str(), buf.size()); |
Steve Naroff | 8bd3dc6 | 2008-09-09 19:59:12 +0000 | [diff] [blame] | 1712 | } else { /* no catch list */ |
| 1713 | buf = "}\nelse {\n"; |
| 1714 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1715 | buf += "}"; |
| 1716 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1717 | } |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1718 | bool sawIdTypedCatch = false; |
| 1719 | Stmt *lastCatchBody = 0; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1720 | while (catchList) { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1721 | ParmVarDecl *catchDecl = catchList->getCatchParamDecl(); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1722 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1723 | if (catchList == S->getCatchStmts()) |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1724 | buf = "if ("; // we are generating code for the first catch clause |
| 1725 | else |
| 1726 | buf = "else if ("; |
| 1727 | startLoc = catchList->getLocStart(); |
| 1728 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1729 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1730 | assert((*startBuf == '@') && "bogus @catch location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1731 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1732 | const char *lParenLoc = strchr(startBuf, '('); |
| 1733 | |
Steve Naroff | be4b333 | 2008-02-01 22:08:12 +0000 | [diff] [blame] | 1734 | if (catchList->hasEllipsis()) { |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1735 | // Now rewrite the body... |
| 1736 | lastCatchBody = catchList->getCatchBody(); |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1737 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1738 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1739 | assert(*SM->getCharacterData(catchList->getRParenLoc()) == ')' && |
| 1740 | "bogus @catch paren location"); |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1741 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1742 | |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1743 | buf += "1) { id _tmp = _caught;"; |
Daniel Dunbar | d7407dc | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 1744 | Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, buf); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1745 | } else if (catchDecl) { |
| 1746 | QualType t = catchDecl->getType(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1747 | if (t == Context->getObjCIdType()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1748 | buf += "1) { "; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1749 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1750 | sawIdTypedCatch = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1751 | } else if (const PointerType *pType = t->getAs<PointerType>()) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1752 | ObjCInterfaceType *cls; // Should be a pointer to a class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1753 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1754 | cls = dyn_cast<ObjCInterfaceType>(pType->getPointeeType().getTypePtr()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1755 | if (cls) { |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1756 | buf += "objc_exception_match((struct objc_class *)objc_getClass(\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1757 | buf += cls->getDecl()->getNameAsString(); |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1758 | buf += "\"), (struct objc_object *)_caught)) { "; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1759 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1760 | } |
| 1761 | } |
| 1762 | // Now rewrite the body... |
| 1763 | lastCatchBody = catchList->getCatchBody(); |
| 1764 | SourceLocation rParenLoc = catchList->getRParenLoc(); |
| 1765 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1766 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 1767 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 1768 | assert((*rParenBuf == ')') && "bogus @catch paren location"); |
| 1769 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1770 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1771 | buf = " = _caught;"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1772 | // Here we replace ") {" with "= _caught;" (which initializes and |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1773 | // declares the @catch parameter). |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1774 | ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1775 | } else { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1776 | assert(false && "@catch rewrite bug"); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1777 | } |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1778 | // make sure all the catch bodies get rewritten! |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1779 | catchList = catchList->getNextCatchStmt(); |
| 1780 | } |
| 1781 | // Complete the catch list... |
| 1782 | if (lastCatchBody) { |
| 1783 | SourceLocation bodyLoc = lastCatchBody->getLocEnd(); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1784 | assert(*SM->getCharacterData(bodyLoc) == '}' && |
| 1785 | "bogus @catch body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1786 | |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1787 | // Insert the last (implicit) else clause *before* the right curly brace. |
| 1788 | bodyLoc = bodyLoc.getFileLocWithOffset(-1); |
| 1789 | buf = "} /* last catch end */\n"; |
| 1790 | buf += "else {\n"; |
| 1791 | buf += " _rethrow = _caught;\n"; |
| 1792 | buf += " objc_exception_try_exit(&_stack);\n"; |
| 1793 | buf += "} } /* @catch end */\n"; |
| 1794 | if (!S->getFinallyStmt()) |
| 1795 | buf += "}\n"; |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1796 | InsertText(bodyLoc, buf.c_str(), buf.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1797 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1798 | // Set lastCurlyLoc |
| 1799 | lastCurlyLoc = lastCatchBody->getLocEnd(); |
| 1800 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1801 | if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1802 | startLoc = finalStmt->getLocStart(); |
| 1803 | startBuf = SM->getCharacterData(startLoc); |
| 1804 | assert((*startBuf == '@') && "bogus @finally start"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1805 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1806 | buf = "/* @finally */"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1807 | ReplaceText(startLoc, 8, buf.c_str(), buf.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1808 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1809 | Stmt *body = finalStmt->getFinallyBody(); |
| 1810 | SourceLocation startLoc = body->getLocStart(); |
| 1811 | SourceLocation endLoc = body->getLocEnd(); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1812 | assert(*SM->getCharacterData(startLoc) == '{' && |
| 1813 | "bogus @finally body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1814 | assert(*SM->getCharacterData(endLoc) == '}' && |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1815 | "bogus @finally body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1816 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1817 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1818 | buf = " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1819 | InsertText(startLoc, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1820 | endLoc = endLoc.getFileLocWithOffset(-1); |
| 1821 | buf = " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1822 | InsertText(endLoc, buf.c_str(), buf.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1823 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1824 | // Set lastCurlyLoc |
| 1825 | lastCurlyLoc = body->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1826 | |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1827 | // Now check for any return/continue/go statements within the @try. |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1828 | WarnAboutReturnGotoStmts(S->getTryBody()); |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1829 | } else { /* no finally clause - make sure we synthesize an implicit one */ |
| 1830 | buf = "{ /* implicit finally clause */\n"; |
| 1831 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
| 1832 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 1833 | buf += "}"; |
| 1834 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1835 | |
| 1836 | // Now check for any return/continue/go statements within the @try. |
| 1837 | // The implicit finally clause won't called if the @try contains any |
| 1838 | // jump statements. |
| 1839 | bool hasReturns = false; |
| 1840 | HasReturnStmts(S->getTryBody(), hasReturns); |
| 1841 | if (hasReturns) |
| 1842 | RewriteTryReturnStmts(S->getTryBody()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1843 | } |
| 1844 | // Now emit the final closing curly brace... |
| 1845 | lastCurlyLoc = lastCurlyLoc.getFileLocWithOffset(1); |
| 1846 | buf = " } /* @try scope end */\n"; |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1847 | InsertText(lastCurlyLoc, buf.c_str(), buf.size()); |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1848 | return 0; |
| 1849 | } |
| 1850 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1851 | Stmt *RewriteObjC::RewriteObjCCatchStmt(ObjCAtCatchStmt *S) { |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1852 | return 0; |
| 1853 | } |
| 1854 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1855 | Stmt *RewriteObjC::RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S) { |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1856 | return 0; |
| 1857 | } |
| 1858 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1859 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
| 1860 | // the throw expression is typically a message expression that's already |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1861 | // been rewritten! (which implies the SourceLocation's are invalid). |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1862 | Stmt *RewriteObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1863 | // Get the start location and compute the semi location. |
| 1864 | SourceLocation startLoc = S->getLocStart(); |
| 1865 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1866 | |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1867 | assert((*startBuf == '@') && "bogus @throw location"); |
| 1868 | |
| 1869 | std::string buf; |
| 1870 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
Steve Naroff | 20ebf8f | 2008-01-19 00:42:38 +0000 | [diff] [blame] | 1871 | if (S->getThrowExpr()) |
| 1872 | buf = "objc_exception_throw("; |
| 1873 | else // add an implicit argument |
| 1874 | buf = "objc_exception_throw(_caught"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1875 | |
Steve Naroff | 4ba0acb | 2008-07-25 15:41:30 +0000 | [diff] [blame] | 1876 | // handle "@ throw" correctly. |
| 1877 | const char *wBuf = strchr(startBuf, 'w'); |
| 1878 | assert((*wBuf == 'w') && "@throw: can't find 'w'"); |
| 1879 | ReplaceText(startLoc, wBuf-startBuf+1, buf.c_str(), buf.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1880 | |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1881 | const char *semiBuf = strchr(startBuf, ';'); |
| 1882 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
| 1883 | SourceLocation semiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf); |
| 1884 | buf = ");"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1885 | ReplaceText(semiLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1886 | return 0; |
| 1887 | } |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1888 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1889 | Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 1890 | // Create a new string expression. |
| 1891 | QualType StrType = Context->getPointerType(Context->CharTy); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1892 | std::string StrEncoding; |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1893 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 1894 | Expr *Replacement = StringLiteral::Create(*Context,StrEncoding.c_str(), |
| 1895 | StrEncoding.length(), false,StrType, |
| 1896 | SourceLocation()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1897 | ReplaceStmt(Exp, Replacement); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1898 | |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 1899 | // Replace this subexpr in the parent. |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1900 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 1901 | return Replacement; |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 1902 | } |
| 1903 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1904 | Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
Steve Naroff | 1a93764 | 2008-12-22 22:16:07 +0000 | [diff] [blame] | 1905 | if (!SelGetUidFunctionDecl) |
| 1906 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1907 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 1908 | // Create a call to sel_registerName("selName"). |
| 1909 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 1910 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1911 | SelExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 6e94ef5 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 1912 | Exp->getSelector().getAsString().c_str(), |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1913 | Exp->getSelector().getAsString().size(), |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 1914 | false, argType, SourceLocation())); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1915 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 1916 | &SelExprs[0], SelExprs.size()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1917 | ReplaceStmt(Exp, SelExp); |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1918 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1919 | return SelExp; |
| 1920 | } |
| 1921 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1922 | CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl( |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1923 | FunctionDecl *FD, Expr **args, unsigned nargs) { |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1924 | // 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] | 1925 | QualType msgSendType = FD->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1926 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1927 | // Create a reference to the objc_msgSend() declaration. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1928 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, msgSendType, SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1929 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1930 | // 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] | 1931 | QualType pToFunc = Context->getPointerType(msgSendType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1932 | ImplicitCastExpr *ICE = new (Context) ImplicitCastExpr(pToFunc, |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 1933 | CastExpr::CK_Unknown, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1934 | DRE, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1935 | /*isLvalue=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1936 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1937 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1938 | |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 1939 | return new (Context) CallExpr(*Context, ICE, args, nargs, FT->getResultType(), |
| 1940 | SourceLocation()); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1941 | } |
| 1942 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1943 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 1944 | const char *&startRef, const char *&endRef) { |
| 1945 | while (startBuf < endBuf) { |
| 1946 | if (*startBuf == '<') |
| 1947 | startRef = startBuf; // mark the start. |
| 1948 | if (*startBuf == '>') { |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1949 | if (startRef && *startRef == '<') { |
| 1950 | endRef = startBuf; // mark the end. |
| 1951 | return true; |
| 1952 | } |
| 1953 | return false; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1954 | } |
| 1955 | startBuf++; |
| 1956 | } |
| 1957 | return false; |
| 1958 | } |
| 1959 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1960 | static void scanToNextArgument(const char *&argRef) { |
| 1961 | int angle = 0; |
| 1962 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 1963 | if (*argRef == '<') |
| 1964 | angle++; |
| 1965 | else if (*argRef == '>') |
| 1966 | angle--; |
| 1967 | argRef++; |
| 1968 | } |
| 1969 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 1970 | } |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 1971 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1972 | bool RewriteObjC::needToScanForQualifiers(QualType T) { |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 1973 | return T->isObjCQualifiedIdType() || T->isObjCQualifiedInterfaceType(); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 1976 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) { |
| 1977 | QualType Type = E->getType(); |
| 1978 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 1979 | SourceLocation Loc, EndLoc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1980 | |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 1981 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) { |
| 1982 | Loc = ECE->getLParenLoc(); |
| 1983 | EndLoc = ECE->getRParenLoc(); |
| 1984 | } else { |
| 1985 | Loc = E->getLocStart(); |
| 1986 | EndLoc = E->getLocEnd(); |
| 1987 | } |
| 1988 | // This will defend against trying to rewrite synthesized expressions. |
| 1989 | if (Loc.isInvalid() || EndLoc.isInvalid()) |
| 1990 | return; |
| 1991 | |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 1992 | const char *startBuf = SM->getCharacterData(Loc); |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 1993 | const char *endBuf = SM->getCharacterData(EndLoc); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 1994 | const char *startRef = 0, *endRef = 0; |
| 1995 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 1996 | // Get the locations of the startRef, endRef. |
| 1997 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-startBuf); |
| 1998 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-startBuf+1); |
| 1999 | // Comment out the protocol references. |
| 2000 | InsertText(LessLoc, "/*", 2); |
| 2001 | InsertText(GreaterLoc, "*/", 2); |
| 2002 | } |
| 2003 | } |
| 2004 | } |
| 2005 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2006 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2007 | SourceLocation Loc; |
| 2008 | QualType Type; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2009 | const FunctionProtoType *proto = 0; |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2010 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 2011 | Loc = VD->getLocation(); |
| 2012 | Type = VD->getType(); |
| 2013 | } |
| 2014 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 2015 | Loc = FD->getLocation(); |
| 2016 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 2017 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2018 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2019 | assert(funcType && "missing function type"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2020 | proto = dyn_cast<FunctionProtoType>(funcType); |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2021 | if (!proto) |
| 2022 | return; |
| 2023 | Type = proto->getResultType(); |
| 2024 | } |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 2025 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(Dcl)) { |
| 2026 | Loc = FD->getLocation(); |
| 2027 | Type = FD->getType(); |
| 2028 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2029 | else |
| 2030 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2031 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2032 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2033 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2034 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2035 | const char *endBuf = SM->getCharacterData(Loc); |
| 2036 | const char *startBuf = endBuf; |
Steve Naroff | 6cafbf2 | 2008-05-31 05:02:17 +0000 | [diff] [blame] | 2037 | while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2038 | startBuf--; // scan backward (from the decl location) for return type. |
| 2039 | const char *startRef = 0, *endRef = 0; |
| 2040 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2041 | // Get the locations of the startRef, endRef. |
| 2042 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-endBuf); |
| 2043 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-endBuf+1); |
| 2044 | // Comment out the protocol references. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2045 | InsertText(LessLoc, "/*", 2); |
| 2046 | InsertText(GreaterLoc, "*/", 2); |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2047 | } |
| 2048 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2049 | if (!proto) |
| 2050 | return; // most likely, was a variable |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2051 | // Now check arguments. |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2052 | const char *startBuf = SM->getCharacterData(Loc); |
| 2053 | const char *startFuncBuf = startBuf; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2054 | for (unsigned i = 0; i < proto->getNumArgs(); i++) { |
| 2055 | if (needToScanForQualifiers(proto->getArgType(i))) { |
| 2056 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2057 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2058 | const char *endBuf = startBuf; |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2059 | // scan forward (from the decl location) for argument types. |
| 2060 | scanToNextArgument(endBuf); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2061 | const char *startRef = 0, *endRef = 0; |
| 2062 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2063 | // Get the locations of the startRef, endRef. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2064 | SourceLocation LessLoc = |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2065 | Loc.getFileLocWithOffset(startRef-startFuncBuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2066 | SourceLocation GreaterLoc = |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2067 | Loc.getFileLocWithOffset(endRef-startFuncBuf+1); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2068 | // Comment out the protocol references. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2069 | InsertText(LessLoc, "/*", 2); |
| 2070 | InsertText(GreaterLoc, "*/", 2); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2071 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2072 | startBuf = ++endBuf; |
| 2073 | } |
| 2074 | else { |
Steve Naroff | aba49d1 | 2008-08-06 15:58:23 +0000 | [diff] [blame] | 2075 | // If the function name is derived from a macro expansion, then the |
| 2076 | // argument buffer will not follow the name. Need to speak with Chris. |
| 2077 | while (*startBuf && *startBuf != ')' && *startBuf != ',') |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2078 | startBuf++; // scan forward (from the decl location) for argument types. |
| 2079 | startBuf++; |
| 2080 | } |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2081 | } |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2084 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2085 | void RewriteObjC::SynthSelGetUidFunctionDecl() { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2086 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
| 2087 | llvm::SmallVector<QualType, 16> ArgTys; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2088 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2089 | QualType getFuncType = Context->getFunctionType(Context->getObjCSelType(), |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2090 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2091 | false /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2092 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2093 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2094 | SelGetUidIdent, getFuncType, 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2095 | FunctionDecl::Extern, false); |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2096 | } |
| 2097 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2098 | void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2099 | // declared in <objc/objc.h> |
Douglas Gregor | 51efe56 | 2009-01-09 01:47:02 +0000 | [diff] [blame] | 2100 | if (FD->getIdentifier() && |
| 2101 | strcmp(FD->getNameAsCString(), "sel_registerName") == 0) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2102 | SelGetUidFunctionDecl = FD; |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2103 | return; |
| 2104 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2105 | RewriteObjCQualifiedInterfaceTypes(FD); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2106 | } |
| 2107 | |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2108 | // SynthSuperContructorFunctionDecl - id objc_super(id obj, id super); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2109 | void RewriteObjC::SynthSuperContructorFunctionDecl() { |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2110 | if (SuperContructorFunctionDecl) |
| 2111 | return; |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2112 | IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super"); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2113 | llvm::SmallVector<QualType, 16> ArgTys; |
| 2114 | QualType argT = Context->getObjCIdType(); |
| 2115 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2116 | ArgTys.push_back(argT); |
| 2117 | ArgTys.push_back(argT); |
| 2118 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
| 2119 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2120 | false, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2121 | SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2122 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2123 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2124 | FunctionDecl::Extern, false); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2125 | } |
| 2126 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2127 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2128 | void RewriteObjC::SynthMsgSendFunctionDecl() { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2129 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
| 2130 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2131 | QualType argT = Context->getObjCIdType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2132 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2133 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2134 | argT = Context->getObjCSelType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2135 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2136 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2137 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2138 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2139 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2140 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2141 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2142 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2143 | FunctionDecl::Extern, false); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2144 | } |
| 2145 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2146 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2147 | void RewriteObjC::SynthMsgSendSuperFunctionDecl() { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2148 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
| 2149 | llvm::SmallVector<QualType, 16> ArgTys; |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2150 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2151 | SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2152 | &Context->Idents.get("objc_super")); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2153 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2154 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2155 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2156 | argT = Context->getObjCSelType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2157 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2158 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2159 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2160 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2161 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2162 | MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2163 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2164 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2165 | FunctionDecl::Extern, false); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2166 | } |
| 2167 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2168 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2169 | void RewriteObjC::SynthMsgSendStretFunctionDecl() { |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2170 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
| 2171 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2172 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2173 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2174 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2175 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2176 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2177 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2178 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2179 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2180 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2181 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2182 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2183 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2184 | FunctionDecl::Extern, false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2185 | } |
| 2186 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2187 | // SynthMsgSendSuperStretFunctionDecl - |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2188 | // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2189 | void RewriteObjC::SynthMsgSendSuperStretFunctionDecl() { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2190 | IdentifierInfo *msgSendIdent = |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2191 | &Context->Idents.get("objc_msgSendSuper_stret"); |
| 2192 | llvm::SmallVector<QualType, 16> ArgTys; |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2193 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2194 | SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2195 | &Context->Idents.get("objc_super")); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2196 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2197 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2198 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2199 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2200 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2201 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2202 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2203 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2204 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2205 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2206 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2207 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2208 | FunctionDecl::Extern, false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2209 | } |
| 2210 | |
Steve Naroff | 1284db8 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2211 | // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2212 | void RewriteObjC::SynthMsgSendFpretFunctionDecl() { |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2213 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
| 2214 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2215 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2216 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2217 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2218 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2219 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2220 | ArgTys.push_back(argT); |
Steve Naroff | 1284db8 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2221 | QualType msgSendType = Context->getFunctionType(Context->DoubleTy, |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2222 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2223 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2224 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2225 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2226 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2227 | FunctionDecl::Extern, false); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2230 | // SynthGetClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2231 | void RewriteObjC::SynthGetClassFunctionDecl() { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2232 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
| 2233 | llvm::SmallVector<QualType, 16> ArgTys; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2234 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2235 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2236 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2237 | false /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2238 | GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2239 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2240 | getClassIdent, getClassType, 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2241 | FunctionDecl::Extern, false); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2242 | } |
| 2243 | |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2244 | // SynthGetMetaClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2245 | void RewriteObjC::SynthGetMetaClassFunctionDecl() { |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2246 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
| 2247 | llvm::SmallVector<QualType, 16> ArgTys; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2248 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2249 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2250 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2251 | false /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2252 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2253 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2254 | getClassIdent, getClassType, 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2255 | FunctionDecl::Extern, false); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2256 | } |
| 2257 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2258 | Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2259 | QualType strType = getConstantStringStructType(); |
| 2260 | |
| 2261 | std::string S = "__NSConstantStringImpl_"; |
Steve Naroff | 7691d9b | 2008-05-31 03:35:42 +0000 | [diff] [blame] | 2262 | |
| 2263 | std::string tmpName = InFileName; |
| 2264 | unsigned i; |
| 2265 | for (i=0; i < tmpName.length(); i++) { |
| 2266 | char c = tmpName.at(i); |
| 2267 | // replace any non alphanumeric characters with '_'. |
| 2268 | if (!isalpha(c) && (c < '0' || c > '9')) |
| 2269 | tmpName[i] = '_'; |
| 2270 | } |
| 2271 | S += tmpName; |
| 2272 | S += "_"; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2273 | S += utostr(NumObjCStringLiterals++); |
| 2274 | |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2275 | Preamble += "static __NSConstantStringImpl " + S; |
| 2276 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 2277 | Preamble += "0x000007c8,"; // utf8_str |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2278 | // The pretty printer for StringLiteral handles escape characters properly. |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2279 | std::string prettyBufS; |
| 2280 | llvm::raw_string_ostream prettyBuf(prettyBufS); |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2281 | Exp->getString()->printPretty(prettyBuf, *Context, 0, |
| 2282 | PrintingPolicy(LangOpts)); |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2283 | Preamble += prettyBuf.str(); |
| 2284 | Preamble += ","; |
Steve Naroff | fd5b76f | 2009-12-06 01:48:44 +0000 | [diff] [blame] | 2285 | Preamble += utostr(Exp->getString()->getByteLength()) + "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2286 | |
| 2287 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
| 2288 | &Context->Idents.get(S.c_str()), strType, 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2289 | VarDecl::Static); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2290 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, strType, SourceLocation()); |
| 2291 | Expr *Unop = new (Context) UnaryOperator(DRE, UnaryOperator::AddrOf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2292 | Context->getPointerType(DRE->getType()), |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2293 | SourceLocation()); |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2294 | // cast to NSConstantString * |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2295 | CastExpr *cast = new (Context) CStyleCastExpr(Exp->getType(), |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2296 | CastExpr::CK_Unknown, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2297 | Unop, Exp->getType(), |
| 2298 | SourceLocation(), |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2299 | SourceLocation()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2300 | ReplaceStmt(Exp, cast); |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 2301 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2302 | return cast; |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2303 | } |
| 2304 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2305 | ObjCInterfaceDecl *RewriteObjC::isSuperReceiver(Expr *recExpr) { |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2306 | // check if we are sending a message to 'super' |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 2307 | if (!CurMethodDef || !CurMethodDef->isInstanceMethod()) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2308 | |
Douglas Gregor | cd9b46e | 2008-11-04 14:56:14 +0000 | [diff] [blame] | 2309 | if (ObjCSuperExpr *Super = dyn_cast<ObjCSuperExpr>(recExpr)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2310 | const ObjCObjectPointerType *OPT = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2311 | Super->getType()->getAs<ObjCObjectPointerType>(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2312 | assert(OPT); |
| 2313 | const ObjCInterfaceType *IT = OPT->getInterfaceType(); |
Chris Lattner | 0d17f6f | 2008-06-21 18:04:54 +0000 | [diff] [blame] | 2314 | return IT->getDecl(); |
| 2315 | } |
| 2316 | return 0; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2317 | } |
| 2318 | |
| 2319 | // struct objc_super { struct objc_object *receiver; struct objc_class *super; }; |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2320 | QualType RewriteObjC::getSuperStructType() { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2321 | if (!SuperStructDecl) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2322 | SuperStructDecl = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2323 | SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2324 | &Context->Idents.get("objc_super")); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2325 | QualType FieldTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2326 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2327 | // struct objc_object *receiver; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2328 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2329 | // struct objc_class *super; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2330 | FieldTypes[1] = Context->getObjCClassType(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2331 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2332 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2333 | for (unsigned i = 0; i < 2; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2334 | SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl, |
| 2335 | SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2336 | FieldTypes[i], 0, |
| 2337 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2338 | /*Mutable=*/false)); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2339 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2340 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2341 | SuperStructDecl->completeDefinition(*Context); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2342 | } |
| 2343 | return Context->getTagDeclType(SuperStructDecl); |
| 2344 | } |
| 2345 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2346 | QualType RewriteObjC::getConstantStringStructType() { |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2347 | if (!ConstantStringDecl) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2348 | ConstantStringDecl = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2349 | SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2350 | &Context->Idents.get("__NSConstantStringImpl")); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2351 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2352 | |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2353 | // struct objc_object *receiver; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2354 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2355 | // int flags; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2356 | FieldTypes[1] = Context->IntTy; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2357 | // char *str; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2358 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2359 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2360 | FieldTypes[3] = Context->LongTy; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2361 | |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2362 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2363 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2364 | ConstantStringDecl->addDecl(FieldDecl::Create(*Context, |
| 2365 | ConstantStringDecl, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2366 | SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2367 | FieldTypes[i], 0, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2368 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2369 | /*Mutable=*/true)); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2370 | } |
| 2371 | |
| 2372 | ConstantStringDecl->completeDefinition(*Context); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2373 | } |
| 2374 | return Context->getTagDeclType(ConstantStringDecl); |
| 2375 | } |
| 2376 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2377 | Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2378 | if (!SelGetUidFunctionDecl) |
| 2379 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2380 | if (!MsgSendFunctionDecl) |
| 2381 | SynthMsgSendFunctionDecl(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2382 | if (!MsgSendSuperFunctionDecl) |
| 2383 | SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2384 | if (!MsgSendStretFunctionDecl) |
| 2385 | SynthMsgSendStretFunctionDecl(); |
| 2386 | if (!MsgSendSuperStretFunctionDecl) |
| 2387 | SynthMsgSendSuperStretFunctionDecl(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2388 | if (!MsgSendFpretFunctionDecl) |
| 2389 | SynthMsgSendFpretFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2390 | if (!GetClassFunctionDecl) |
| 2391 | SynthGetClassFunctionDecl(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2392 | if (!GetMetaClassFunctionDecl) |
| 2393 | SynthGetMetaClassFunctionDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2394 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2395 | // default to objc_msgSend(). |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2396 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2397 | // May need to use objc_msgSend_stret() as well. |
| 2398 | FunctionDecl *MsgSendStretFlavor = 0; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2399 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
| 2400 | QualType resultType = mDecl->getResultType(); |
Chris Lattner | 8b51fd7 | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2401 | if (resultType->isStructureType() || resultType->isUnionType()) |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2402 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
Chris Lattner | 8b51fd7 | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2403 | else if (resultType->isRealFloatingType()) |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2404 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2405 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2406 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2407 | // Synthesize a call to objc_msgSend(). |
| 2408 | llvm::SmallVector<Expr*, 8> MsgExprs; |
| 2409 | IdentifierInfo *clsName = Exp->getClassName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2410 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2411 | // Derive/push the receiver/selector, 2 implicit arguments to objc_msgSend(). |
| 2412 | if (clsName) { // class message. |
Steve Naroff | fc93d52 | 2008-07-24 19:44:33 +0000 | [diff] [blame] | 2413 | // FIXME: We need to fix Sema (and the AST for ObjCMessageExpr) to handle |
| 2414 | // the 'super' idiom within a class method. |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 2415 | if (clsName->getName() == "super") { |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2416 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2417 | if (MsgSendStretFlavor) |
| 2418 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2419 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2420 | |
| 2421 | ObjCInterfaceDecl *SuperDecl = |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 2422 | CurMethodDef->getClassInterface()->getSuperClass(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2423 | |
| 2424 | llvm::SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2425 | |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2426 | // set the receiver to self, the first argument to all methods. |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2427 | InitExprs.push_back( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2428 | new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2429 | CastExpr::CK_Unknown, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2430 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2431 | Context->getObjCIdType(), |
| 2432 | SourceLocation()), |
| 2433 | Context->getObjCIdType(), |
| 2434 | SourceLocation(), SourceLocation())); // set the 'receiver'. |
| 2435 | |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2436 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2437 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2438 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2439 | SuperDecl->getIdentifier()->getNameStart(), |
| 2440 | SuperDecl->getIdentifier()->getLength(), |
| 2441 | false, argType, SourceLocation())); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2442 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2443 | &ClsExprs[0], |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2444 | ClsExprs.size()); |
| 2445 | // To turn off a warning, type-cast to 'id' |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2446 | InitExprs.push_back( // set 'super class', using objc_getClass(). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2447 | new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2448 | CastExpr::CK_Unknown, |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2449 | Cls, Context->getObjCIdType(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2450 | SourceLocation(), SourceLocation())); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2451 | // struct objc_super |
| 2452 | QualType superType = getSuperStructType(); |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2453 | Expr *SuperRep; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2454 | |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2455 | if (LangOpts.Microsoft) { |
| 2456 | SynthSuperContructorFunctionDecl(); |
| 2457 | // Simulate a contructor call... |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2458 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2459 | superType, SourceLocation()); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2460 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2461 | InitExprs.size(), |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2462 | superType, SourceLocation()); |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2463 | // The code for super is a little tricky to prevent collision with |
| 2464 | // the structure definition in the header. The rewriter has it's own |
| 2465 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2466 | // we need the cast below. For example: |
| 2467 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2468 | // |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2469 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2470 | Context->getPointerType(SuperRep->getType()), |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2471 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2472 | SuperRep = new (Context) CStyleCastExpr(Context->getPointerType(superType), |
| 2473 | CastExpr::CK_Unknown, SuperRep, |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2474 | Context->getPointerType(superType), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2475 | SourceLocation(), SourceLocation()); |
| 2476 | } else { |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2477 | // (struct objc_super) { <exprs from above> } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2478 | InitListExpr *ILE = new (Context) InitListExpr(SourceLocation(), |
| 2479 | &InitExprs[0], InitExprs.size(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2480 | SourceLocation()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2481 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superType, ILE, |
Chris Lattner | 418f6c7 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 2482 | false); |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2483 | // struct objc_super * |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2484 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2485 | Context->getPointerType(SuperRep->getType()), |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2486 | SourceLocation()); |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2487 | } |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2488 | MsgExprs.push_back(SuperRep); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2489 | } else { |
| 2490 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2491 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2492 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2493 | clsName->getNameStart(), |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2494 | clsName->getLength(), |
| 2495 | false, argType, |
| 2496 | SourceLocation())); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2497 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2498 | &ClsExprs[0], |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2499 | ClsExprs.size()); |
| 2500 | MsgExprs.push_back(Cls); |
| 2501 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2502 | } else { // instance message. |
| 2503 | Expr *recExpr = Exp->getReceiver(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2504 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2505 | if (ObjCInterfaceDecl *SuperDecl = isSuperReceiver(recExpr)) { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2506 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2507 | if (MsgSendStretFlavor) |
| 2508 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2509 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2510 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2511 | llvm::SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2512 | |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2513 | InitExprs.push_back( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2514 | new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2515 | CastExpr::CK_Unknown, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2516 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
Steve Naroff | f616ebb | 2008-07-16 22:35:27 +0000 | [diff] [blame] | 2517 | Context->getObjCIdType(), |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2518 | SourceLocation()), |
| 2519 | Context->getObjCIdType(), |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2520 | SourceLocation(), SourceLocation())); // set the 'receiver'. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2521 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2522 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2523 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2524 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2525 | SuperDecl->getIdentifier()->getNameStart(), |
| 2526 | SuperDecl->getIdentifier()->getLength(), |
| 2527 | false, argType, SourceLocation())); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2528 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2529 | &ClsExprs[0], |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2530 | ClsExprs.size()); |
Fariborz Jahanian | 7127431 | 2007-12-05 17:29:46 +0000 | [diff] [blame] | 2531 | // To turn off a warning, type-cast to 'id' |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2532 | InitExprs.push_back( |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2533 | // set 'super class', using objc_getClass(). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2534 | new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2535 | CastExpr::CK_Unknown, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2536 | Cls, Context->getObjCIdType(), SourceLocation(), SourceLocation())); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2537 | // struct objc_super |
| 2538 | QualType superType = getSuperStructType(); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2539 | Expr *SuperRep; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2540 | |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2541 | if (LangOpts.Microsoft) { |
| 2542 | SynthSuperContructorFunctionDecl(); |
| 2543 | // Simulate a contructor call... |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2544 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2545 | superType, SourceLocation()); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2546 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2547 | InitExprs.size(), |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2548 | superType, SourceLocation()); |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2549 | // The code for super is a little tricky to prevent collision with |
| 2550 | // the structure definition in the header. The rewriter has it's own |
| 2551 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2552 | // we need the cast below. For example: |
| 2553 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2554 | // |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2555 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2556 | Context->getPointerType(SuperRep->getType()), |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2557 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2558 | SuperRep = new (Context) CStyleCastExpr(Context->getPointerType(superType), |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2559 | CastExpr::CK_Unknown, |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2560 | SuperRep, Context->getPointerType(superType), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2561 | SourceLocation(), SourceLocation()); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2562 | } else { |
| 2563 | // (struct objc_super) { <exprs from above> } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2564 | InitListExpr *ILE = new (Context) InitListExpr(SourceLocation(), |
| 2565 | &InitExprs[0], InitExprs.size(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2566 | SourceLocation()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2567 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superType, ILE, false); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2568 | } |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2569 | MsgExprs.push_back(SuperRep); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2570 | } else { |
Fariborz Jahanian | 7dd8283 | 2007-12-07 21:21:21 +0000 | [diff] [blame] | 2571 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 2572 | // Foo<Proto> *. |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2573 | while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr)) |
Fariborz Jahanian | 7dd8283 | 2007-12-07 21:21:21 +0000 | [diff] [blame] | 2574 | recExpr = CE->getSubExpr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2575 | recExpr = new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2576 | CastExpr::CK_Unknown, recExpr, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2577 | Context->getObjCIdType(), |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2578 | SourceLocation(), SourceLocation()); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2579 | MsgExprs.push_back(recExpr); |
| 2580 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2581 | } |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2582 | // Create a call to sel_registerName("selName"), it will be the 2nd argument. |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2583 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 2584 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2585 | SelExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 6e94ef5 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2586 | Exp->getSelector().getAsString().c_str(), |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2587 | Exp->getSelector().getAsString().size(), |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 2588 | false, argType, SourceLocation())); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2589 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2590 | &SelExprs[0], SelExprs.size()); |
| 2591 | MsgExprs.push_back(SelExp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2592 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2593 | // Now push any user supplied arguments. |
| 2594 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2595 | Expr *userExpr = Exp->getArg(i); |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2596 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 2597 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 2598 | // Reuse the ICE type, it is exactly what the doctor ordered. |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2599 | QualType type = ICE->getType()->isObjCQualifiedIdType() |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2600 | ? Context->getObjCIdType() |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2601 | : ICE->getType(); |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2602 | userExpr = new (Context) CStyleCastExpr(type, CastExpr::CK_Unknown, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2603 | userExpr, type, SourceLocation(), |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2604 | SourceLocation()); |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2605 | } |
| 2606 | // Make id<P...> cast into an 'id' cast. |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2607 | else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2608 | if (CE->getType()->isObjCQualifiedIdType()) { |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2609 | while ((CE = dyn_cast<CStyleCastExpr>(userExpr))) |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2610 | userExpr = CE->getSubExpr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2611 | userExpr = new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2612 | CastExpr::CK_Unknown, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2613 | userExpr, Context->getObjCIdType(), |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2614 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2615 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2616 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2617 | MsgExprs.push_back(userExpr); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2618 | // We've transferred the ownership to MsgExprs. For now, we *don't* null |
| 2619 | // out the argument in the original expression (since we aren't deleting |
| 2620 | // the ObjCMessageExpr). See RewritePropertySetter() usage for more info. |
| 2621 | //Exp->setArg(i, 0); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2622 | } |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2623 | // Generate the funky cast. |
| 2624 | CastExpr *cast; |
| 2625 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 2626 | QualType returnType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2627 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2628 | // Push 'id' and 'SEL', the 2 implicit arguments. |
Steve Naroff | c3a438c | 2007-11-15 10:43:57 +0000 | [diff] [blame] | 2629 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 2630 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 2631 | else |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2632 | ArgTypes.push_back(Context->getObjCIdType()); |
| 2633 | ArgTypes.push_back(Context->getObjCSelType()); |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2634 | if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) { |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2635 | // Push any user argument types. |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2636 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 2637 | E = OMD->param_end(); PI != E; ++PI) { |
| 2638 | QualType t = (*PI)->getType()->isObjCQualifiedIdType() |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2639 | ? Context->getObjCIdType() |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2640 | : (*PI)->getType(); |
Steve Naroff | a206b06 | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 2641 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 2642 | if (isTopLevelBlockPointerType(t)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2643 | const BlockPointerType *BPT = t->getAs<BlockPointerType>(); |
Steve Naroff | a206b06 | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 2644 | t = Context->getPointerType(BPT->getPointeeType()); |
| 2645 | } |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 2646 | ArgTypes.push_back(t); |
| 2647 | } |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2648 | returnType = OMD->getResultType()->isObjCQualifiedIdType() |
| 2649 | ? Context->getObjCIdType() : OMD->getResultType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2650 | } else { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2651 | returnType = Context->getObjCIdType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2652 | } |
| 2653 | // 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] | 2654 | QualType msgSendType = MsgSendFlavor->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2655 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2656 | // Create a reference to the objc_msgSend() declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2657 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, msgSendType, |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2658 | SourceLocation()); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2659 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2660 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2661 | // If we don't do this cast, we get the following bizarre warning/note: |
| 2662 | // xx.m:13: warning: function called through a non-compatible type |
| 2663 | // xx.m:13: note: if this code is reached, the program will abort |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2664 | cast = new (Context) CStyleCastExpr(Context->getPointerType(Context->VoidTy), |
| 2665 | CastExpr::CK_Unknown, DRE, |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2666 | Context->getPointerType(Context->VoidTy), |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2667 | SourceLocation(), SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2668 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2669 | // Now do the "normal" pointer to function cast. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2670 | QualType castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | d0ee6f9 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 2671 | &ArgTypes[0], ArgTypes.size(), |
Steve Naroff | 2679e48 | 2008-03-18 02:02:04 +0000 | [diff] [blame] | 2672 | // If we don't have a method decl, force a variadic cast. |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2673 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true, 0); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2674 | castType = Context->getPointerType(castType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2675 | cast = new (Context) CStyleCastExpr(castType, CastExpr::CK_Unknown, cast, |
| 2676 | castType, SourceLocation(), |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2677 | SourceLocation()); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2678 | |
| 2679 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2680 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2681 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2682 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2683 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2684 | MsgExprs.size(), |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2685 | FT->getResultType(), SourceLocation()); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2686 | Stmt *ReplacingStmt = CE; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2687 | if (MsgSendStretFlavor) { |
| 2688 | // We have the method which returns a struct/union. Must also generate |
| 2689 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 2690 | // expression which dictate which one to envoke depending on size of |
| 2691 | // method's return type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2692 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2693 | // Create a reference to the objc_msgSend_stret() declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2694 | DeclRefExpr *STDRE = new (Context) DeclRefExpr(MsgSendStretFlavor, msgSendType, |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2695 | SourceLocation()); |
| 2696 | // Need to cast objc_msgSend_stret to "void *" (see above comment). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2697 | cast = new (Context) CStyleCastExpr(Context->getPointerType(Context->VoidTy), |
| 2698 | CastExpr::CK_Unknown, STDRE, |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2699 | Context->getPointerType(Context->VoidTy), |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2700 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2701 | // Now do the "normal" pointer to function cast. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2702 | castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | d0ee6f9 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 2703 | &ArgTypes[0], ArgTypes.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2704 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false, 0); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2705 | castType = Context->getPointerType(castType); |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2706 | cast = new (Context) CStyleCastExpr(castType, CastExpr::CK_Unknown, |
| 2707 | cast, castType, SourceLocation(), SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2708 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2709 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2710 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2711 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2712 | FT = msgSendType->getAs<FunctionType>(); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2713 | CallExpr *STCE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2714 | MsgExprs.size(), |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2715 | FT->getResultType(), SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2716 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2717 | // Build sizeof(returnType) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2718 | SizeOfAlignOfExpr *sizeofExpr = new (Context) SizeOfAlignOfExpr(true, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2719 | Context->getTrivialTypeSourceInfo(returnType), |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2720 | Context->getSizeType(), |
| 2721 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2722 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 2723 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 2724 | // For X86 it is more complicated and some kind of target specific routine |
| 2725 | // is needed to decide what to do. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2726 | unsigned IntSize = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2727 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2728 | IntegerLiteral *limit = new (Context) IntegerLiteral(llvm::APInt(IntSize, 8), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2729 | Context->IntTy, |
| 2730 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2731 | BinaryOperator *lessThanExpr = new (Context) BinaryOperator(sizeofExpr, limit, |
| 2732 | BinaryOperator::LE, |
| 2733 | Context->IntTy, |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2734 | SourceLocation()); |
| 2735 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2736 | ConditionalOperator *CondExpr = |
Douglas Gregor | 47e1f7c | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 2737 | new (Context) ConditionalOperator(lessThanExpr, |
| 2738 | SourceLocation(), CE, |
| 2739 | SourceLocation(), STCE, returnType); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2740 | ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), CondExpr); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2741 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2742 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2743 | return ReplacingStmt; |
| 2744 | } |
| 2745 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2746 | Stmt *RewriteObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2747 | Stmt *ReplacingStmt = SynthMessageExpr(Exp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2748 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2749 | // Now do the actual rewrite. |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2750 | ReplaceStmt(Exp, ReplacingStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2751 | |
| 2752 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2753 | return ReplacingStmt; |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2754 | } |
| 2755 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2756 | // typedef struct objc_object Protocol; |
| 2757 | QualType RewriteObjC::getProtocolType() { |
| 2758 | if (!ProtocolTypeDecl) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2759 | TypeSourceInfo *TInfo |
| 2760 | = Context->getTrivialTypeSourceInfo(Context->getObjCIdType()); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2761 | ProtocolTypeDecl = TypedefDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2762 | SourceLocation(), |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2763 | &Context->Idents.get("Protocol"), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2764 | TInfo); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2765 | } |
| 2766 | return Context->getTypeDeclType(ProtocolTypeDecl); |
| 2767 | } |
| 2768 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2769 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2770 | /// a synthesized/forward data reference (to the protocol's metadata). |
| 2771 | /// The forward references (and metadata) are generated in |
| 2772 | /// RewriteObjC::HandleTranslationUnit(). |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2773 | Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2774 | std::string Name = "_OBJC_PROTOCOL_" + Exp->getProtocol()->getNameAsString(); |
| 2775 | IdentifierInfo *ID = &Context->Idents.get(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2776 | VarDecl *VD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Douglas Gregor | 0da76df | 2009-11-23 11:41:28 +0000 | [diff] [blame] | 2777 | ID, getProtocolType(), 0, VarDecl::Extern); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2778 | DeclRefExpr *DRE = new (Context) DeclRefExpr(VD, getProtocolType(), SourceLocation()); |
| 2779 | Expr *DerefExpr = new (Context) UnaryOperator(DRE, UnaryOperator::AddrOf, |
| 2780 | Context->getPointerType(DRE->getType()), |
| 2781 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2782 | CastExpr *castExpr = new (Context) CStyleCastExpr(DerefExpr->getType(), |
| 2783 | CastExpr::CK_Unknown, |
| 2784 | DerefExpr, DerefExpr->getType(), |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2785 | SourceLocation(), SourceLocation()); |
| 2786 | ReplaceStmt(Exp, castExpr); |
| 2787 | ProtocolExprDecls.insert(Exp->getProtocol()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2788 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2789 | return castExpr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2790 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2791 | } |
| 2792 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2793 | bool RewriteObjC::BufferContainsPPDirectives(const char *startBuf, |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2794 | const char *endBuf) { |
| 2795 | while (startBuf < endBuf) { |
| 2796 | if (*startBuf == '#') { |
| 2797 | // Skip whitespace. |
| 2798 | for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf) |
| 2799 | ; |
| 2800 | if (!strncmp(startBuf, "if", strlen("if")) || |
| 2801 | !strncmp(startBuf, "ifdef", strlen("ifdef")) || |
| 2802 | !strncmp(startBuf, "ifndef", strlen("ifndef")) || |
| 2803 | !strncmp(startBuf, "define", strlen("define")) || |
| 2804 | !strncmp(startBuf, "undef", strlen("undef")) || |
| 2805 | !strncmp(startBuf, "else", strlen("else")) || |
| 2806 | !strncmp(startBuf, "elif", strlen("elif")) || |
| 2807 | !strncmp(startBuf, "endif", strlen("endif")) || |
| 2808 | !strncmp(startBuf, "pragma", strlen("pragma")) || |
| 2809 | !strncmp(startBuf, "include", strlen("include")) || |
| 2810 | !strncmp(startBuf, "import", strlen("import")) || |
| 2811 | !strncmp(startBuf, "include_next", strlen("include_next"))) |
| 2812 | return true; |
| 2813 | } |
| 2814 | startBuf++; |
| 2815 | } |
| 2816 | return false; |
| 2817 | } |
| 2818 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2819 | /// SynthesizeObjCInternalStruct - Rewrite one internal struct corresponding to |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2820 | /// an objective-c class with ivars. |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2821 | void RewriteObjC::SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2822 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2823 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2824 | assert(CDecl->getNameAsCString() && |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2825 | "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 2826 | // Do not synthesize more than once. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2827 | if (ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 2828 | return; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2829 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 2830 | int NumIvars = CDecl->ivar_size(); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2831 | SourceLocation LocStart = CDecl->getLocStart(); |
| 2832 | SourceLocation LocEnd = CDecl->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2833 | |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2834 | const char *startBuf = SM->getCharacterData(LocStart); |
| 2835 | const char *endBuf = SM->getCharacterData(LocEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2836 | |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2837 | // If no ivars and no root or if its root, directly or indirectly, |
| 2838 | // 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] | 2839 | if ((CDecl->isForwardDecl() || NumIvars == 0) && |
| 2840 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 2841 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 2842 | ReplaceText(LocStart, endBuf-startBuf, Result.c_str(), Result.size()); |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2843 | return; |
| 2844 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2845 | |
| 2846 | // FIXME: This has potential of causing problem. If |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2847 | // SynthesizeObjCInternalStruct is ever called recursively. |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2848 | Result += "\nstruct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2849 | Result += CDecl->getNameAsString(); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 2850 | if (LangOpts.Microsoft) |
| 2851 | Result += "_IMPL"; |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 2852 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2853 | if (NumIvars > 0) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2854 | const char *cursor = strchr(startBuf, '{'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2855 | assert((cursor && endBuf) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2856 | && "SynthesizeObjCInternalStruct - malformed @interface"); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2857 | // If the buffer contains preprocessor directives, we do more fine-grained |
| 2858 | // rewrites. This is intended to fix code that looks like (which occurs in |
| 2859 | // NSURL.h, for example): |
| 2860 | // |
| 2861 | // #ifdef XYZ |
| 2862 | // @interface Foo : NSObject |
| 2863 | // #else |
| 2864 | // @interface FooBar : NSObject |
| 2865 | // #endif |
| 2866 | // { |
| 2867 | // int i; |
| 2868 | // } |
| 2869 | // @end |
| 2870 | // |
| 2871 | // This clause is segregated to avoid breaking the common case. |
| 2872 | if (BufferContainsPPDirectives(startBuf, cursor)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2873 | SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() : |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2874 | CDecl->getClassLoc(); |
| 2875 | const char *endHeader = SM->getCharacterData(L); |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 2876 | endHeader += Lexer::MeasureTokenLength(L, *SM, LangOpts); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2877 | |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 2878 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2879 | // advance to the end of the referenced protocols. |
| 2880 | while (endHeader < cursor && *endHeader != '>') endHeader++; |
| 2881 | endHeader++; |
| 2882 | } |
| 2883 | // rewrite the original header |
| 2884 | ReplaceText(LocStart, endHeader-startBuf, Result.c_str(), Result.size()); |
| 2885 | } else { |
| 2886 | // rewrite the original header *without* disturbing the '{' |
Steve Naroff | 17c8778 | 2009-12-04 21:36:32 +0000 | [diff] [blame] | 2887 | ReplaceText(LocStart, cursor-startBuf, Result.c_str(), Result.size()); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2888 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2889 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2890 | Result = "\n struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2891 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 39bbd9f | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 2892 | Result += "_IMPL "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2893 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 819173c | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 2894 | Result += "_IVARS;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2895 | |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2896 | // insert the super class structure definition. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2897 | SourceLocation OnePastCurly = |
| 2898 | LocStart.getFileLocWithOffset(cursor-startBuf+1); |
| 2899 | InsertText(OnePastCurly, Result.c_str(), Result.size()); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2900 | } |
| 2901 | cursor++; // past '{' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2902 | |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2903 | // Now comment out any visibility specifiers. |
| 2904 | while (cursor < endBuf) { |
| 2905 | if (*cursor == '@') { |
| 2906 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | df6a51b | 2007-11-14 22:57:51 +0000 | [diff] [blame] | 2907 | // Skip whitespace. |
| 2908 | for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor) |
| 2909 | /*scan*/; |
| 2910 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2911 | // FIXME: presence of @public, etc. inside comment results in |
| 2912 | // this transformation as well, which is still correct c-code. |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2913 | if (!strncmp(cursor, "public", strlen("public")) || |
| 2914 | !strncmp(cursor, "private", strlen("private")) || |
Steve Naroff | c5e3277 | 2008-04-04 22:34:24 +0000 | [diff] [blame] | 2915 | !strncmp(cursor, "package", strlen("package")) || |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2916 | !strncmp(cursor, "protected", strlen("protected"))) |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2917 | InsertText(atLoc, "// ", 3); |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2918 | } |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2919 | // FIXME: If there are cases where '<' is used in ivar declaration part |
| 2920 | // of user code, then scan the ivar list and use needToScanForQualifiers |
| 2921 | // for type checking. |
| 2922 | else if (*cursor == '<') { |
| 2923 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2924 | InsertText(atLoc, "/* ", 3); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2925 | cursor = strchr(cursor, '>'); |
| 2926 | cursor++; |
| 2927 | atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2928 | InsertText(atLoc, " */", 3); |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 2929 | } else if (*cursor == '^') { // rewrite block specifier. |
| 2930 | SourceLocation caretLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
| 2931 | ReplaceText(caretLoc, 1, "*", 1); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2932 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2933 | cursor++; |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2934 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2935 | // Don't forget to add a ';'!! |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2936 | InsertText(LocEnd.getFileLocWithOffset(1), ";", 1); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2937 | } else { // we don't have any instance variables - insert super struct. |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 2938 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2939 | Result += " {\n struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2940 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 39bbd9f | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 2941 | Result += "_IMPL "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2942 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 819173c | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 2943 | Result += "_IVARS;\n};\n"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 2944 | ReplaceText(LocStart, endBuf-startBuf, Result.c_str(), Result.size()); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2945 | } |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2946 | // Mark this struct as having been generated. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2947 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
Steve Naroff | fbfe825 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 2948 | assert(false && "struct already synthesize- SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2949 | } |
| 2950 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2951 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2952 | /// class methods. |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2953 | template<typename MethodIterator> |
| 2954 | void RewriteObjC::RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 2955 | MethodIterator MethodEnd, |
Fariborz Jahanian | 8e991ba | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 2956 | bool IsInstanceMethod, |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2957 | const char *prefix, |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 2958 | const char *ClassName, |
| 2959 | std::string &Result) { |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2960 | if (MethodBegin == MethodEnd) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2961 | |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2962 | if (!objc_impl_method) { |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2963 | /* struct _objc_method { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2964 | SEL _cmd; |
| 2965 | char *method_types; |
| 2966 | void *_imp; |
| 2967 | } |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2968 | */ |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 2969 | Result += "\nstruct _objc_method {\n"; |
| 2970 | Result += "\tSEL _cmd;\n"; |
| 2971 | Result += "\tchar *method_types;\n"; |
| 2972 | Result += "\tvoid *_imp;\n"; |
| 2973 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2974 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2975 | objc_impl_method = true; |
Fariborz Jahanian | 776d6ff | 2007-10-19 00:36:46 +0000 | [diff] [blame] | 2976 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2977 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2978 | // Build _objc_method_list for class's methods if needed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2979 | |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2980 | /* struct { |
| 2981 | struct _objc_method_list *next_method; |
| 2982 | int method_count; |
| 2983 | struct _objc_method method_list[]; |
| 2984 | } |
| 2985 | */ |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2986 | unsigned NumMethods = std::distance(MethodBegin, MethodEnd); |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2987 | Result += "\nstatic struct {\n"; |
| 2988 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 2989 | Result += "\tint method_count;\n"; |
| 2990 | Result += "\tstruct _objc_method method_list["; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2991 | Result += utostr(NumMethods); |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2992 | Result += "];\n} _OBJC_"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2993 | Result += prefix; |
| 2994 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 2995 | Result += "_METHODS_"; |
| 2996 | Result += ClassName; |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 2997 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2998 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 2999 | Result += "_meth\")))= "; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3000 | Result += "{\n\t0, " + utostr(NumMethods) + "\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3001 | |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3002 | Result += "\t,{{(SEL)\""; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3003 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3004 | std::string MethodTypeString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3005 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3006 | Result += "\", \""; |
| 3007 | Result += MethodTypeString; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3008 | Result += "\", (void *)"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3009 | Result += MethodInternalNames[*MethodBegin]; |
| 3010 | Result += "}\n"; |
| 3011 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 3012 | Result += "\t ,{(SEL)\""; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3013 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3014 | std::string MethodTypeString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3015 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3016 | Result += "\", \""; |
| 3017 | Result += MethodTypeString; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3018 | Result += "\", (void *)"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3019 | Result += MethodInternalNames[*MethodBegin]; |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 3020 | Result += "}\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3021 | } |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3022 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3023 | } |
| 3024 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3025 | /// RewriteObjCProtocolMetaData - Rewrite protocols meta-data. |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 3026 | void RewriteObjC:: |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3027 | RewriteObjCProtocolMetaData(ObjCProtocolDecl *PDecl, const char *prefix, |
| 3028 | const char *ClassName, std::string &Result) { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3029 | static bool objc_protocol_methods = false; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3030 | |
| 3031 | // Output struct protocol_methods holder of method selector and type. |
| 3032 | if (!objc_protocol_methods && !PDecl->isForwardDecl()) { |
| 3033 | /* struct protocol_methods { |
| 3034 | SEL _cmd; |
| 3035 | char *method_types; |
| 3036 | } |
| 3037 | */ |
| 3038 | Result += "\nstruct _protocol_methods {\n"; |
| 3039 | Result += "\tstruct objc_selector *_cmd;\n"; |
| 3040 | Result += "\tchar *method_types;\n"; |
| 3041 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3042 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3043 | objc_protocol_methods = true; |
| 3044 | } |
| 3045 | // Do not synthesize the protocol more than once. |
| 3046 | if (ObjCSynthesizedProtocols.count(PDecl)) |
| 3047 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3048 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3049 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 3050 | unsigned NumMethods = std::distance(PDecl->instmeth_begin(), |
| 3051 | PDecl->instmeth_end()); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3052 | /* struct _objc_protocol_method_list { |
| 3053 | int protocol_method_count; |
| 3054 | struct protocol_methods protocols[]; |
| 3055 | } |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3056 | */ |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3057 | Result += "\nstatic struct {\n"; |
| 3058 | Result += "\tint protocol_method_count;\n"; |
| 3059 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 3060 | Result += utostr(NumMethods); |
| 3061 | Result += "];\n} _OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 3062 | Result += PDecl->getNameAsString(); |
| 3063 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= " |
| 3064 | "{\n\t" + utostr(NumMethods) + "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3065 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3066 | // Output instance methods declared in this protocol. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3067 | for (ObjCProtocolDecl::instmeth_iterator |
| 3068 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3069 | I != E; ++I) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3070 | if (I == PDecl->instmeth_begin()) |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3071 | Result += "\t ,{{(struct objc_selector *)\""; |
| 3072 | else |
| 3073 | Result += "\t ,{(struct objc_selector *)\""; |
| 3074 | Result += (*I)->getSelector().getAsString().c_str(); |
| 3075 | std::string MethodTypeString; |
| 3076 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 3077 | Result += "\", \""; |
| 3078 | Result += MethodTypeString; |
| 3079 | Result += "\"}\n"; |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3080 | } |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3081 | Result += "\t }\n};\n"; |
| 3082 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3083 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3084 | // Output class methods declared in this protocol. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3085 | unsigned NumMethods = std::distance(PDecl->classmeth_begin(), |
| 3086 | PDecl->classmeth_end()); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3087 | if (NumMethods > 0) { |
| 3088 | /* struct _objc_protocol_method_list { |
| 3089 | int protocol_method_count; |
| 3090 | struct protocol_methods protocols[]; |
| 3091 | } |
| 3092 | */ |
| 3093 | Result += "\nstatic struct {\n"; |
| 3094 | Result += "\tint protocol_method_count;\n"; |
| 3095 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 3096 | Result += utostr(NumMethods); |
| 3097 | Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_"; |
| 3098 | Result += PDecl->getNameAsString(); |
| 3099 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 3100 | "{\n\t"; |
| 3101 | Result += utostr(NumMethods); |
| 3102 | Result += "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3103 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3104 | // Output instance methods declared in this protocol. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3105 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3106 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3107 | I != E; ++I) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3108 | if (I == PDecl->classmeth_begin()) |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3109 | Result += "\t ,{{(struct objc_selector *)\""; |
| 3110 | else |
| 3111 | Result += "\t ,{(struct objc_selector *)\""; |
| 3112 | Result += (*I)->getSelector().getAsString().c_str(); |
| 3113 | std::string MethodTypeString; |
| 3114 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 3115 | Result += "\", \""; |
| 3116 | Result += MethodTypeString; |
| 3117 | Result += "\"}\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3118 | } |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3119 | Result += "\t }\n};\n"; |
| 3120 | } |
| 3121 | |
| 3122 | // Output: |
| 3123 | /* struct _objc_protocol { |
| 3124 | // Objective-C 1.0 extensions |
| 3125 | struct _objc_protocol_extension *isa; |
| 3126 | char *protocol_name; |
| 3127 | struct _objc_protocol **protocol_list; |
| 3128 | struct _objc_protocol_method_list *instance_methods; |
| 3129 | struct _objc_protocol_method_list *class_methods; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3130 | }; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3131 | */ |
| 3132 | static bool objc_protocol = false; |
| 3133 | if (!objc_protocol) { |
| 3134 | Result += "\nstruct _objc_protocol {\n"; |
| 3135 | Result += "\tstruct _objc_protocol_extension *isa;\n"; |
| 3136 | Result += "\tchar *protocol_name;\n"; |
| 3137 | Result += "\tstruct _objc_protocol **protocol_list;\n"; |
| 3138 | Result += "\tstruct _objc_protocol_method_list *instance_methods;\n"; |
| 3139 | Result += "\tstruct _objc_protocol_method_list *class_methods;\n"; |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3140 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3141 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3142 | objc_protocol = true; |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3143 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3144 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3145 | Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_"; |
| 3146 | Result += PDecl->getNameAsString(); |
| 3147 | Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= " |
| 3148 | "{\n\t0, \""; |
| 3149 | Result += PDecl->getNameAsString(); |
| 3150 | Result += "\", 0, "; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3151 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3152 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 3153 | Result += PDecl->getNameAsString(); |
| 3154 | Result += ", "; |
| 3155 | } |
| 3156 | else |
| 3157 | Result += "0, "; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3158 | if (PDecl->classmeth_begin() != PDecl->classmeth_end()) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3159 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
| 3160 | Result += PDecl->getNameAsString(); |
| 3161 | Result += "\n"; |
| 3162 | } |
| 3163 | else |
| 3164 | Result += "0\n"; |
| 3165 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3166 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3167 | // Mark this protocol as having been generated. |
| 3168 | if (!ObjCSynthesizedProtocols.insert(PDecl)) |
| 3169 | assert(false && "protocol already synthesized"); |
| 3170 | |
| 3171 | } |
| 3172 | |
| 3173 | void RewriteObjC:: |
| 3174 | RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Protocols, |
| 3175 | const char *prefix, const char *ClassName, |
| 3176 | std::string &Result) { |
| 3177 | if (Protocols.empty()) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3178 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3179 | for (unsigned i = 0; i != Protocols.size(); i++) |
| 3180 | RewriteObjCProtocolMetaData(Protocols[i], prefix, ClassName, Result); |
| 3181 | |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3182 | // Output the top lovel protocol meta-data for the class. |
| 3183 | /* struct _objc_protocol_list { |
| 3184 | struct _objc_protocol_list *next; |
| 3185 | int protocol_count; |
| 3186 | struct _objc_protocol *class_protocols[]; |
| 3187 | } |
| 3188 | */ |
| 3189 | Result += "\nstatic struct {\n"; |
| 3190 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 3191 | Result += "\tint protocol_count;\n"; |
| 3192 | Result += "\tstruct _objc_protocol *class_protocols["; |
| 3193 | Result += utostr(Protocols.size()); |
| 3194 | Result += "];\n} _OBJC_"; |
| 3195 | Result += prefix; |
| 3196 | Result += "_PROTOCOLS_"; |
| 3197 | Result += ClassName; |
| 3198 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 3199 | "{\n\t0, "; |
| 3200 | Result += utostr(Protocols.size()); |
| 3201 | Result += "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3202 | |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3203 | Result += "\t,{&_OBJC_PROTOCOL_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3204 | Result += Protocols[0]->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3205 | Result += " \n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3206 | |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3207 | for (unsigned i = 1; i != Protocols.size(); i++) { |
| 3208 | Result += "\t ,&_OBJC_PROTOCOL_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3209 | Result += Protocols[i]->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3210 | Result += "\n"; |
| 3211 | } |
| 3212 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3213 | } |
| 3214 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3215 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3216 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3217 | /// implementation. |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3218 | void RewriteObjC::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3219 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3220 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3221 | // Find category declaration for this implementation. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3222 | ObjCCategoryDecl *CDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3223 | for (CDecl = ClassDecl->getCategoryList(); CDecl; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3224 | CDecl = CDecl->getNextClassCategory()) |
| 3225 | if (CDecl->getIdentifier() == IDecl->getIdentifier()) |
| 3226 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3227 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3228 | std::string FullCategoryName = ClassDecl->getNameAsString(); |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3229 | FullCategoryName += '_'; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3230 | FullCategoryName += IDecl->getNameAsString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3231 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3232 | // Build _objc_method_list for class's instance methods if needed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3233 | llvm::SmallVector<ObjCMethodDecl *, 32> |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3234 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3235 | |
| 3236 | // If any of our property implementations have associated getters or |
| 3237 | // setters, produce metadata for them as well. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3238 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 3239 | PropEnd = IDecl->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3240 | Prop != PropEnd; ++Prop) { |
| 3241 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 3242 | continue; |
| 3243 | if (!(*Prop)->getPropertyIvarDecl()) |
| 3244 | continue; |
| 3245 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 3246 | if (!PD) |
| 3247 | continue; |
| 3248 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 3249 | InstanceMethods.push_back(Getter); |
| 3250 | if (PD->isReadOnly()) |
| 3251 | continue; |
| 3252 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 3253 | InstanceMethods.push_back(Setter); |
| 3254 | } |
| 3255 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3256 | true, "CATEGORY_", FullCategoryName.c_str(), |
| 3257 | Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3258 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3259 | // Build _objc_method_list for class's class methods if needed |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3260 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3261 | false, "CATEGORY_", FullCategoryName.c_str(), |
| 3262 | Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3263 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3264 | // Protocols referenced in class declaration? |
Fariborz Jahanian | bac97d4 | 2007-11-13 22:09:49 +0000 | [diff] [blame] | 3265 | // Null CDecl is case of a category implementation with no category interface |
| 3266 | if (CDecl) |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3267 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), "CATEGORY", |
| 3268 | FullCategoryName.c_str(), Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3269 | /* struct _objc_category { |
| 3270 | char *category_name; |
| 3271 | char *class_name; |
| 3272 | struct _objc_method_list *instance_methods; |
| 3273 | struct _objc_method_list *class_methods; |
| 3274 | struct _objc_protocol_list *protocols; |
| 3275 | // Objective-C 1.0 extensions |
| 3276 | uint32_t size; // sizeof (struct _objc_category) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3277 | struct _objc_property_list *instance_properties; // category's own |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3278 | // @property decl. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3279 | }; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3280 | */ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3281 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3282 | static bool objc_category = false; |
| 3283 | if (!objc_category) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3284 | Result += "\nstruct _objc_category {\n"; |
| 3285 | Result += "\tchar *category_name;\n"; |
| 3286 | Result += "\tchar *class_name;\n"; |
| 3287 | Result += "\tstruct _objc_method_list *instance_methods;\n"; |
| 3288 | Result += "\tstruct _objc_method_list *class_methods;\n"; |
| 3289 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3290 | Result += "\tunsigned int size;\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3291 | Result += "\tstruct _objc_property_list *instance_properties;\n"; |
| 3292 | Result += "};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3293 | objc_category = true; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3294 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3295 | Result += "\nstatic struct _objc_category _OBJC_CATEGORY_"; |
| 3296 | Result += FullCategoryName; |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3297 | Result += " __attribute__ ((used, section (\"__OBJC, __category\")))= {\n\t\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3298 | Result += IDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3299 | Result += "\"\n\t, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3300 | Result += ClassDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3301 | Result += "\"\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3302 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3303 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3304 | Result += "\t, (struct _objc_method_list *)" |
| 3305 | "&_OBJC_CATEGORY_INSTANCE_METHODS_"; |
| 3306 | Result += FullCategoryName; |
| 3307 | Result += "\n"; |
| 3308 | } |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3309 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3310 | Result += "\t, 0\n"; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3311 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3312 | Result += "\t, (struct _objc_method_list *)" |
| 3313 | "&_OBJC_CATEGORY_CLASS_METHODS_"; |
| 3314 | Result += FullCategoryName; |
| 3315 | Result += "\n"; |
| 3316 | } |
| 3317 | else |
| 3318 | Result += "\t, 0\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3319 | |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3320 | if (CDecl && CDecl->protocol_begin() != CDecl->protocol_end()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3321 | Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3322 | Result += FullCategoryName; |
| 3323 | Result += "\n"; |
| 3324 | } |
| 3325 | else |
| 3326 | Result += "\t, 0\n"; |
| 3327 | Result += "\t, sizeof(struct _objc_category), 0\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3328 | } |
| 3329 | |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3330 | /// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of |
| 3331 | /// ivar offset. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3332 | void RewriteObjC::SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl, |
| 3333 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3334 | std::string &Result) { |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3335 | if (ivar->isBitField()) { |
| 3336 | // FIXME: The hack below doesn't work for bitfields. For now, we simply |
| 3337 | // place all bitfields at offset 0. |
| 3338 | Result += "0"; |
| 3339 | } else { |
| 3340 | Result += "__OFFSETOFIVAR__(struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3341 | Result += IDecl->getNameAsString(); |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3342 | if (LangOpts.Microsoft) |
| 3343 | Result += "_IMPL"; |
| 3344 | Result += ", "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3345 | Result += ivar->getNameAsString(); |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3346 | Result += ")"; |
| 3347 | } |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3348 | } |
| 3349 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3350 | //===----------------------------------------------------------------------===// |
| 3351 | // Meta Data Emission |
| 3352 | //===----------------------------------------------------------------------===// |
| 3353 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3354 | void RewriteObjC::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3355 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3356 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3357 | |
Fariborz Jahanian | ebe668f | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3358 | // Explictly declared @interface's are already synthesized. |
Steve Naroff | 33feeb0 | 2009-04-20 20:09:33 +0000 | [diff] [blame] | 3359 | if (CDecl->isImplicitInterfaceDecl()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3360 | // FIXME: Implementation of a class with no @interface (legacy) doese not |
Fariborz Jahanian | ebe668f | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3361 | // produce correct synthesis as yet. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3362 | SynthesizeObjCInternalStruct(CDecl, Result); |
Fariborz Jahanian | ebe668f | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3363 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3364 | |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3365 | // Build _objc_ivar_list metadata for classes ivars if needed |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3366 | unsigned NumIvars = !IDecl->ivar_empty() |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3367 | ? IDecl->ivar_size() |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3368 | : (CDecl ? CDecl->ivar_size() : 0); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3369 | if (NumIvars > 0) { |
| 3370 | static bool objc_ivar = false; |
| 3371 | if (!objc_ivar) { |
| 3372 | /* struct _objc_ivar { |
| 3373 | char *ivar_name; |
| 3374 | char *ivar_type; |
| 3375 | int ivar_offset; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3376 | }; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3377 | */ |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3378 | Result += "\nstruct _objc_ivar {\n"; |
| 3379 | Result += "\tchar *ivar_name;\n"; |
| 3380 | Result += "\tchar *ivar_type;\n"; |
| 3381 | Result += "\tint ivar_offset;\n"; |
| 3382 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3383 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3384 | objc_ivar = true; |
| 3385 | } |
| 3386 | |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3387 | /* struct { |
| 3388 | int ivar_count; |
| 3389 | struct _objc_ivar ivar_list[nIvars]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3390 | }; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3391 | */ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3392 | Result += "\nstatic struct {\n"; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3393 | Result += "\tint ivar_count;\n"; |
| 3394 | Result += "\tstruct _objc_ivar ivar_list["; |
| 3395 | Result += utostr(NumIvars); |
| 3396 | Result += "];\n} _OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3397 | Result += IDecl->getNameAsString(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3398 | Result += " __attribute__ ((used, section (\"__OBJC, __instance_vars\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3399 | "{\n\t"; |
| 3400 | Result += utostr(NumIvars); |
| 3401 | Result += "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3402 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3403 | ObjCInterfaceDecl::ivar_iterator IVI, IVE; |
Douglas Gregor | 8f36aba | 2009-04-23 03:23:08 +0000 | [diff] [blame] | 3404 | llvm::SmallVector<ObjCIvarDecl *, 8> IVars; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3405 | if (!IDecl->ivar_empty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3406 | for (ObjCImplementationDecl::ivar_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3407 | IV = IDecl->ivar_begin(), IVEnd = IDecl->ivar_end(); |
Douglas Gregor | 8f36aba | 2009-04-23 03:23:08 +0000 | [diff] [blame] | 3408 | IV != IVEnd; ++IV) |
| 3409 | IVars.push_back(*IV); |
| 3410 | IVI = IVars.begin(); |
| 3411 | IVE = IVars.end(); |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3412 | } else { |
| 3413 | IVI = CDecl->ivar_begin(); |
| 3414 | IVE = CDecl->ivar_end(); |
| 3415 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3416 | Result += "\t,{{\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3417 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3418 | Result += "\", \""; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3419 | std::string TmpString, StrEncoding; |
| 3420 | Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI); |
| 3421 | QuoteDoublequotes(TmpString, StrEncoding); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3422 | Result += StrEncoding; |
| 3423 | Result += "\", "; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3424 | SynthesizeIvarOffsetComputation(IDecl, *IVI, Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3425 | Result += "}\n"; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3426 | for (++IVI; IVI != IVE; ++IVI) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3427 | Result += "\t ,{\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3428 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3429 | Result += "\", \""; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3430 | std::string TmpString, StrEncoding; |
| 3431 | Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI); |
| 3432 | QuoteDoublequotes(TmpString, StrEncoding); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3433 | Result += StrEncoding; |
| 3434 | Result += "\", "; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3435 | SynthesizeIvarOffsetComputation(IDecl, (*IVI), Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3436 | Result += "}\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3437 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3438 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3439 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3440 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3441 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3442 | // Build _objc_method_list for class's instance methods if needed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3443 | llvm::SmallVector<ObjCMethodDecl *, 32> |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3444 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3445 | |
| 3446 | // If any of our property implementations have associated getters or |
| 3447 | // setters, produce metadata for them as well. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3448 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 3449 | PropEnd = IDecl->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3450 | Prop != PropEnd; ++Prop) { |
| 3451 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 3452 | continue; |
| 3453 | if (!(*Prop)->getPropertyIvarDecl()) |
| 3454 | continue; |
| 3455 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 3456 | if (!PD) |
| 3457 | continue; |
| 3458 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 3459 | InstanceMethods.push_back(Getter); |
| 3460 | if (PD->isReadOnly()) |
| 3461 | continue; |
| 3462 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 3463 | InstanceMethods.push_back(Setter); |
| 3464 | } |
| 3465 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3466 | true, "", IDecl->getNameAsCString(), Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3467 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3468 | // Build _objc_method_list for class's class methods if needed |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3469 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3470 | false, "", IDecl->getNameAsCString(), Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3471 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3472 | // Protocols referenced in class declaration? |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3473 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), |
| 3474 | "CLASS", CDecl->getNameAsCString(), Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3475 | |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3476 | // Declaration of class/meta-class metadata |
| 3477 | /* struct _objc_class { |
| 3478 | struct _objc_class *isa; // or const char *root_class_name when metadata |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3479 | const char *super_class_name; |
| 3480 | char *name; |
| 3481 | long version; |
| 3482 | long info; |
| 3483 | long instance_size; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3484 | struct _objc_ivar_list *ivars; |
| 3485 | struct _objc_method_list *methods; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3486 | struct objc_cache *cache; |
| 3487 | struct objc_protocol_list *protocols; |
| 3488 | const char *ivar_layout; |
| 3489 | struct _objc_class_ext *ext; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3490 | }; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3491 | */ |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3492 | static bool objc_class = false; |
| 3493 | if (!objc_class) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3494 | Result += "\nstruct _objc_class {\n"; |
| 3495 | Result += "\tstruct _objc_class *isa;\n"; |
| 3496 | Result += "\tconst char *super_class_name;\n"; |
| 3497 | Result += "\tchar *name;\n"; |
| 3498 | Result += "\tlong version;\n"; |
| 3499 | Result += "\tlong info;\n"; |
| 3500 | Result += "\tlong instance_size;\n"; |
| 3501 | Result += "\tstruct _objc_ivar_list *ivars;\n"; |
| 3502 | Result += "\tstruct _objc_method_list *methods;\n"; |
| 3503 | Result += "\tstruct objc_cache *cache;\n"; |
| 3504 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 3505 | Result += "\tconst char *ivar_layout;\n"; |
| 3506 | Result += "\tstruct _objc_class_ext *ext;\n"; |
| 3507 | Result += "};\n"; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3508 | objc_class = true; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3509 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3510 | |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3511 | // Meta-class metadata generation. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3512 | ObjCInterfaceDecl *RootClass = 0; |
| 3513 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3514 | while (SuperClass) { |
| 3515 | RootClass = SuperClass; |
| 3516 | SuperClass = SuperClass->getSuperClass(); |
| 3517 | } |
| 3518 | SuperClass = CDecl->getSuperClass(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3519 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3520 | Result += "\nstatic struct _objc_class _OBJC_METACLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3521 | Result += CDecl->getNameAsString(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3522 | Result += " __attribute__ ((used, section (\"__OBJC, __meta_class\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3523 | "{\n\t(struct _objc_class *)\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3524 | Result += (RootClass ? RootClass->getNameAsString() : CDecl->getNameAsString()); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3525 | Result += "\""; |
| 3526 | |
| 3527 | if (SuperClass) { |
| 3528 | Result += ", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3529 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3530 | Result += "\", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3531 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3532 | Result += "\""; |
| 3533 | } |
| 3534 | else { |
| 3535 | Result += ", 0, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3536 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3537 | Result += "\""; |
| 3538 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3539 | // 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] | 3540 | // 'info' field is initialized to CLS_META(2) for metaclass |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3541 | Result += ", 0,2, sizeof(struct _objc_class), 0"; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3542 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 3543 | Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3544 | Result += IDecl->getNameAsString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3545 | Result += "\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3546 | } |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3547 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3548 | Result += ", 0\n"; |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3549 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3550 | Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3551 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3552 | Result += ",0,0\n"; |
| 3553 | } |
Fariborz Jahanian | 454cb01 | 2007-10-24 20:54:23 +0000 | [diff] [blame] | 3554 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3555 | Result += "\t,0,0,0,0\n"; |
| 3556 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3557 | |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3558 | // class metadata generation. |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3559 | Result += "\nstatic struct _objc_class _OBJC_CLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3560 | Result += CDecl->getNameAsString(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3561 | Result += " __attribute__ ((used, section (\"__OBJC, __class\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3562 | "{\n\t&_OBJC_METACLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3563 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3564 | if (SuperClass) { |
| 3565 | Result += ", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3566 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3567 | Result += "\", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3568 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3569 | Result += "\""; |
| 3570 | } |
| 3571 | else { |
| 3572 | Result += ", 0, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3573 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3574 | Result += "\""; |
| 3575 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3576 | // 'info' field is initialized to CLS_CLASS(1) for class |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3577 | Result += ", 0,1"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3578 | if (!ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3579 | Result += ",0"; |
| 3580 | else { |
| 3581 | // class has size. Must synthesize its size. |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 3582 | Result += ",sizeof(struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3583 | Result += CDecl->getNameAsString(); |
Steve Naroff | ba9ac4e | 2008-03-10 23:33:22 +0000 | [diff] [blame] | 3584 | if (LangOpts.Microsoft) |
| 3585 | Result += "_IMPL"; |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3586 | Result += ")"; |
| 3587 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3588 | if (NumIvars > 0) { |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 3589 | Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3590 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3591 | Result += "\n\t"; |
| 3592 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3593 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3594 | Result += ",0"; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3595 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3596 | Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3597 | Result += CDecl->getNameAsString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3598 | Result += ", 0\n\t"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3599 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3600 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3601 | Result += ",0,0"; |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3602 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3603 | Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3604 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3605 | Result += ", 0,0\n"; |
| 3606 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3607 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3608 | Result += ",0,0,0\n"; |
| 3609 | Result += "};\n"; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3610 | } |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3611 | |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3612 | /// RewriteImplementations - This routine rewrites all method implementations |
| 3613 | /// and emits meta-data. |
| 3614 | |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3615 | void RewriteObjC::RewriteImplementations() { |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3616 | int ClsDefCount = ClassImplementation.size(); |
| 3617 | int CatDefCount = CategoryImplementation.size(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3618 | |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3619 | // Rewrite implemented methods |
| 3620 | for (int i = 0; i < ClsDefCount; i++) |
| 3621 | RewriteImplementationDecl(ClassImplementation[i]); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3622 | |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 3623 | for (int i = 0; i < CatDefCount; i++) |
| 3624 | RewriteImplementationDecl(CategoryImplementation[i]); |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3625 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3626 | |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3627 | void RewriteObjC::SynthesizeMetaDataIntoBuffer(std::string &Result) { |
| 3628 | int ClsDefCount = ClassImplementation.size(); |
| 3629 | int CatDefCount = CategoryImplementation.size(); |
| 3630 | |
Steve Naroff | 5df5b76 | 2008-05-07 21:23:49 +0000 | [diff] [blame] | 3631 | // This is needed for determining instance variable offsets. |
Fariborz Jahanian | c98cbb4 | 2010-01-07 18:31:42 +0000 | [diff] [blame] | 3632 | Result += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((long) &((TYPE *)0)->MEMBER)\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3633 | // For each implemented class, write out all its meta data. |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3634 | for (int i = 0; i < ClsDefCount; i++) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3635 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3636 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3637 | // For each implemented category, write out all its meta data. |
| 3638 | for (int i = 0; i < CatDefCount; i++) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3639 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3640 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3641 | // Write objc_symtab metadata |
| 3642 | /* |
| 3643 | struct _objc_symtab |
| 3644 | { |
| 3645 | long sel_ref_cnt; |
| 3646 | SEL *refs; |
| 3647 | short cls_def_cnt; |
| 3648 | short cat_def_cnt; |
| 3649 | void *defs[cls_def_cnt + cat_def_cnt]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3650 | }; |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3651 | */ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3652 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3653 | Result += "\nstruct _objc_symtab {\n"; |
| 3654 | Result += "\tlong sel_ref_cnt;\n"; |
| 3655 | Result += "\tSEL *refs;\n"; |
| 3656 | Result += "\tshort cls_def_cnt;\n"; |
| 3657 | Result += "\tshort cat_def_cnt;\n"; |
| 3658 | Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n"; |
| 3659 | Result += "};\n\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3660 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3661 | Result += "static struct _objc_symtab " |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3662 | "_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3663 | Result += "\t0, 0, " + utostr(ClsDefCount) |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3664 | + ", " + utostr(CatDefCount) + "\n"; |
| 3665 | for (int i = 0; i < ClsDefCount; i++) { |
| 3666 | Result += "\t,&_OBJC_CLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3667 | Result += ClassImplementation[i]->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3668 | Result += "\n"; |
| 3669 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3670 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3671 | for (int i = 0; i < CatDefCount; i++) { |
| 3672 | Result += "\t,&_OBJC_CATEGORY_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3673 | Result += CategoryImplementation[i]->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3674 | Result += "_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3675 | Result += CategoryImplementation[i]->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3676 | Result += "\n"; |
| 3677 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3678 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3679 | Result += "};\n\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3680 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3681 | // Write objc_module metadata |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3682 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3683 | /* |
| 3684 | struct _objc_module { |
| 3685 | long version; |
| 3686 | long size; |
| 3687 | const char *name; |
| 3688 | struct _objc_symtab *symtab; |
| 3689 | } |
| 3690 | */ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3691 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3692 | Result += "\nstruct _objc_module {\n"; |
| 3693 | Result += "\tlong version;\n"; |
| 3694 | Result += "\tlong size;\n"; |
| 3695 | Result += "\tconst char *name;\n"; |
| 3696 | Result += "\tstruct _objc_symtab *symtab;\n"; |
| 3697 | Result += "};\n\n"; |
| 3698 | Result += "static struct _objc_module " |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3699 | "_OBJC_MODULES __attribute__ ((used, section (\"__OBJC, __module_info\")))= {\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3700 | Result += "\t" + utostr(OBJC_ABI_VERSION) + |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3701 | ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3702 | Result += "};\n\n"; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3703 | |
| 3704 | if (LangOpts.Microsoft) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3705 | if (ProtocolExprDecls.size()) { |
| 3706 | Result += "#pragma section(\".objc_protocol$B\",long,read,write)\n"; |
| 3707 | Result += "#pragma data_seg(push, \".objc_protocol$B\")\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3708 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3709 | E = ProtocolExprDecls.end(); I != E; ++I) { |
| 3710 | Result += "static struct _objc_protocol *_POINTER_OBJC_PROTOCOL_"; |
| 3711 | Result += (*I)->getNameAsString(); |
| 3712 | Result += " = &_OBJC_PROTOCOL_"; |
| 3713 | Result += (*I)->getNameAsString(); |
| 3714 | Result += ";\n"; |
| 3715 | } |
| 3716 | Result += "#pragma data_seg(pop)\n\n"; |
| 3717 | } |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3718 | Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n"; |
Steve Naroff | 1919032 | 2008-05-07 00:06:16 +0000 | [diff] [blame] | 3719 | Result += "#pragma data_seg(push, \".objc_module_info$B\")\n"; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3720 | Result += "static struct _objc_module *_POINTER_OBJC_MODULES = "; |
| 3721 | Result += "&_OBJC_MODULES;\n"; |
| 3722 | Result += "#pragma data_seg(pop)\n\n"; |
| 3723 | } |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3724 | } |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 3725 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3726 | std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, |
| 3727 | const char *funcName, |
| 3728 | std::string Tag) { |
| 3729 | const FunctionType *AFT = CE->getFunctionType(); |
| 3730 | QualType RT = AFT->getResultType(); |
| 3731 | std::string StructRef = "struct " + Tag; |
| 3732 | std::string S = "static " + RT.getAsString() + " __" + |
| 3733 | funcName + "_" + "block_func_" + utostr(i); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 3734 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3735 | BlockDecl *BD = CE->getBlockDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3736 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3737 | if (isa<FunctionNoProtoType>(AFT)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3738 | // 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] | 3739 | // block (to reference imported block decl refs). |
| 3740 | S += "(" + StructRef + " *__cself)"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3741 | } else if (BD->param_empty()) { |
| 3742 | S += "(" + StructRef + " *__cself)"; |
| 3743 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3744 | const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3745 | assert(FT && "SynthesizeBlockFunc: No function proto"); |
| 3746 | S += '('; |
| 3747 | // first add the implicit argument. |
| 3748 | S += StructRef + " *__cself, "; |
| 3749 | std::string ParamStr; |
| 3750 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 3751 | E = BD->param_end(); AI != E; ++AI) { |
| 3752 | if (AI != BD->param_begin()) S += ", "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3753 | ParamStr = (*AI)->getNameAsString(); |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 3754 | (*AI)->getType().getAsStringInternal(ParamStr, Context->PrintingPolicy); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3755 | S += ParamStr; |
| 3756 | } |
| 3757 | if (FT->isVariadic()) { |
| 3758 | if (!BD->param_empty()) S += ", "; |
| 3759 | S += "..."; |
| 3760 | } |
| 3761 | S += ')'; |
| 3762 | } |
| 3763 | S += " {\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3764 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3765 | // Create local declarations to avoid rewriting all closure decl ref exprs. |
| 3766 | // First, emit a declaration for all "by ref" decls. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3767 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3768 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3769 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3770 | std::string Name = (*I)->getNameAsString(); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 3771 | std::string TypeString = "struct __Block_byref_" + Name + " *"; |
| 3772 | Name = TypeString + Name; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3773 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3774 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3775 | // Next, emit a declaration for all "by copy" declarations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3776 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3777 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3778 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3779 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3780 | // Handle nested closure invocation. For example: |
| 3781 | // |
| 3782 | // void (^myImportedClosure)(void); |
| 3783 | // myImportedClosure = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3784 | // |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3785 | // void (^anotherClosure)(void); |
| 3786 | // anotherClosure = ^(void) { |
| 3787 | // myImportedClosure(); // import and invoke the closure |
| 3788 | // }; |
| 3789 | // |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3790 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3791 | S += "struct __block_impl *"; |
| 3792 | else |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 3793 | (*I)->getType().getAsStringInternal(Name, Context->PrintingPolicy); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3794 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3795 | } |
| 3796 | std::string RewrittenStr = RewrittenBlockExprs[CE]; |
| 3797 | const char *cstr = RewrittenStr.c_str(); |
| 3798 | while (*cstr++ != '{') ; |
| 3799 | S += cstr; |
| 3800 | S += "\n"; |
| 3801 | return S; |
| 3802 | } |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 3803 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3804 | std::string RewriteObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
| 3805 | const char *funcName, |
| 3806 | std::string Tag) { |
| 3807 | std::string StructRef = "struct " + Tag; |
| 3808 | std::string S = "static void __"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3809 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3810 | S += funcName; |
| 3811 | S += "_block_copy_" + utostr(i); |
| 3812 | S += "(" + StructRef; |
| 3813 | S += "*dst, " + StructRef; |
| 3814 | S += "*src) {"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3815 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3816 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3817 | S += "_Block_object_assign((void*)&dst->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3818 | S += (*I)->getNameAsString(); |
Steve Naroff | 47a2422 | 2008-12-11 20:51:38 +0000 | [diff] [blame] | 3819 | S += ", (void*)src->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3820 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | d25d1b5 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 3821 | if (BlockByRefDecls.count((*I))) |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 3822 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | d25d1b5 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 3823 | else |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 3824 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3825 | } |
Fariborz Jahanian | d25d1b5 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 3826 | S += "}\n"; |
| 3827 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3828 | S += "\nstatic void __"; |
| 3829 | S += funcName; |
| 3830 | S += "_block_dispose_" + utostr(i); |
| 3831 | S += "(" + StructRef; |
| 3832 | S += "*src) {"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3833 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3834 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3835 | S += "_Block_object_dispose((void*)src->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3836 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | d25d1b5 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 3837 | if (BlockByRefDecls.count((*I))) |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 3838 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | d25d1b5 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 3839 | else |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 3840 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3841 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3842 | S += "}\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3843 | return S; |
| 3844 | } |
| 3845 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3846 | std::string RewriteObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 3847 | std::string Desc) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3848 | std::string S = "\nstruct " + Tag; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3849 | std::string Constructor = " " + Tag; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3850 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3851 | S += " {\n struct __block_impl impl;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3852 | S += " struct " + Desc; |
| 3853 | S += "* Desc;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3854 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3855 | Constructor += "(void *fp, "; // Invoke function pointer. |
| 3856 | Constructor += "struct " + Desc; // Descriptor pointer. |
| 3857 | Constructor += " *desc"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3858 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3859 | if (BlockDeclRefs.size()) { |
| 3860 | // Output all "by copy" declarations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3861 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3862 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3863 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3864 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3865 | std::string ArgName = "_" + FieldName; |
| 3866 | // Handle nested closure invocation. For example: |
| 3867 | // |
| 3868 | // void (^myImportedBlock)(void); |
| 3869 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3870 | // |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3871 | // void (^anotherBlock)(void); |
| 3872 | // anotherBlock = ^(void) { |
| 3873 | // myImportedBlock(); // import and invoke the closure |
| 3874 | // }; |
| 3875 | // |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3876 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3877 | S += "struct __block_impl *"; |
| 3878 | Constructor += ", void *" + ArgName; |
| 3879 | } else { |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 3880 | (*I)->getType().getAsStringInternal(FieldName, Context->PrintingPolicy); |
| 3881 | (*I)->getType().getAsStringInternal(ArgName, Context->PrintingPolicy); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3882 | Constructor += ", " + ArgName; |
| 3883 | } |
| 3884 | S += FieldName + ";\n"; |
| 3885 | } |
| 3886 | // Output all "by ref" declarations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3887 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3888 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3889 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3890 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3891 | std::string ArgName = "_" + FieldName; |
| 3892 | // Handle nested closure invocation. For example: |
| 3893 | // |
| 3894 | // void (^myImportedBlock)(void); |
| 3895 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3896 | // |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3897 | // void (^anotherBlock)(void); |
| 3898 | // anotherBlock = ^(void) { |
| 3899 | // myImportedBlock(); // import and invoke the closure |
| 3900 | // }; |
| 3901 | // |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3902 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3903 | S += "struct __block_impl *"; |
| 3904 | Constructor += ", void *" + ArgName; |
| 3905 | } else { |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 3906 | std::string TypeString = "struct __Block_byref_" + FieldName; |
| 3907 | TypeString += " *"; |
| 3908 | FieldName = TypeString + FieldName; |
| 3909 | ArgName = TypeString + ArgName; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3910 | Constructor += ", " + ArgName; |
| 3911 | } |
| 3912 | S += FieldName + "; // by ref\n"; |
| 3913 | } |
| 3914 | // Finish writing the constructor. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3915 | Constructor += ", int flags=0) {\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3916 | if (GlobalVarDecl) |
| 3917 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 3918 | else |
| 3919 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3920 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3921 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3922 | Constructor += " Desc = desc;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3923 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3924 | // Initialize all "by copy" arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3925 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3926 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3927 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3928 | Constructor += " "; |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3929 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3930 | Constructor += Name + " = (struct __block_impl *)_"; |
| 3931 | else |
| 3932 | Constructor += Name + " = _"; |
| 3933 | Constructor += Name + ";\n"; |
| 3934 | } |
| 3935 | // Initialize all "by ref" arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3936 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3937 | E = BlockByRefDecls.end(); I != E; ++I) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3938 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3939 | Constructor += " "; |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3940 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3941 | Constructor += Name + " = (struct __block_impl *)_"; |
| 3942 | else |
| 3943 | Constructor += Name + " = _"; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 3944 | Constructor += Name + "->__forwarding;\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3945 | } |
| 3946 | } else { |
| 3947 | // Finish writing the constructor. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3948 | Constructor += ", int flags=0) {\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3949 | if (GlobalVarDecl) |
| 3950 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 3951 | else |
| 3952 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3953 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 3954 | Constructor += " Desc = desc;\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3955 | } |
| 3956 | Constructor += " "; |
| 3957 | Constructor += "}\n"; |
| 3958 | S += Constructor; |
| 3959 | S += "};\n"; |
| 3960 | return S; |
| 3961 | } |
| 3962 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3963 | std::string RewriteObjC::SynthesizeBlockDescriptor(std::string DescTag, |
| 3964 | std::string ImplTag, int i, |
| 3965 | const char *FunName, |
| 3966 | unsigned hasCopy) { |
| 3967 | std::string S = "\nstatic struct " + DescTag; |
| 3968 | |
| 3969 | S += " {\n unsigned long reserved;\n"; |
| 3970 | S += " unsigned long Block_size;\n"; |
| 3971 | if (hasCopy) { |
Fariborz Jahanian | 4fcc4fd | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 3972 | S += " void (*copy)(struct "; |
| 3973 | S += ImplTag; S += "*, struct "; |
| 3974 | S += ImplTag; S += "*);\n"; |
| 3975 | |
| 3976 | S += " void (*dispose)(struct "; |
| 3977 | S += ImplTag; S += "*);\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3978 | } |
| 3979 | S += "} "; |
| 3980 | |
| 3981 | S += DescTag + "_DATA = { 0, sizeof(struct "; |
| 3982 | S += ImplTag + ")"; |
| 3983 | if (hasCopy) { |
| 3984 | S += ", __" + std::string(FunName) + "_block_copy_" + utostr(i); |
| 3985 | S += ", __" + std::string(FunName) + "_block_dispose_" + utostr(i); |
| 3986 | } |
| 3987 | S += "};\n"; |
| 3988 | return S; |
| 3989 | } |
| 3990 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3991 | void RewriteObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, |
| 3992 | const char *FunName) { |
| 3993 | // Insert closures that were part of the function. |
| 3994 | for (unsigned i = 0; i < Blocks.size(); i++) { |
| 3995 | |
| 3996 | CollectBlockDeclRefInfo(Blocks[i]); |
| 3997 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3998 | std::string ImplTag = "__" + std::string(FunName) + "_block_impl_" + utostr(i); |
| 3999 | std::string DescTag = "__" + std::string(FunName) + "_block_desc_" + utostr(i); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4000 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4001 | std::string CI = SynthesizeBlockImpl(Blocks[i], ImplTag, DescTag); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4002 | |
| 4003 | InsertText(FunLocStart, CI.c_str(), CI.size()); |
| 4004 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4005 | std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, ImplTag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4006 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4007 | InsertText(FunLocStart, CF.c_str(), CF.size()); |
| 4008 | |
| 4009 | if (ImportedBlockDecls.size()) { |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4010 | std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, ImplTag); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4011 | InsertText(FunLocStart, HF.c_str(), HF.size()); |
| 4012 | } |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4013 | std::string BD = SynthesizeBlockDescriptor(DescTag, ImplTag, i, FunName, |
| 4014 | ImportedBlockDecls.size() > 0); |
| 4015 | InsertText(FunLocStart, BD.c_str(), BD.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4016 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4017 | BlockDeclRefs.clear(); |
| 4018 | BlockByRefDecls.clear(); |
| 4019 | BlockByCopyDecls.clear(); |
| 4020 | BlockCallExprs.clear(); |
| 4021 | ImportedBlockDecls.clear(); |
| 4022 | } |
| 4023 | Blocks.clear(); |
| 4024 | RewrittenBlockExprs.clear(); |
| 4025 | } |
| 4026 | |
| 4027 | void RewriteObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { |
| 4028 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4029 | const char *FuncName = FD->getNameAsCString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4030 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4031 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 4032 | } |
| 4033 | |
| 4034 | void RewriteObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 4035 | //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n"); |
| 4036 | //SourceLocation FunLocStart = MD->getLocStart(); |
| 4037 | // FIXME: This hack works around a bug in Rewrite.InsertText(). |
| 4038 | SourceLocation FunLocStart = MD->getLocStart().getFileLocWithOffset(-1); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4039 | std::string FuncName = MD->getSelector().getAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4040 | // Convert colons to underscores. |
| 4041 | std::string::size_type loc = 0; |
| 4042 | while ((loc = FuncName.find(":", loc)) != std::string::npos) |
| 4043 | FuncName.replace(loc, 1, "_"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4044 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4045 | SynthesizeBlockLiterals(FunLocStart, FuncName.c_str()); |
| 4046 | } |
| 4047 | |
| 4048 | void RewriteObjC::GetBlockDeclRefExprs(Stmt *S) { |
| 4049 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4050 | CI != E; ++CI) |
| 4051 | if (*CI) { |
| 4052 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 4053 | GetBlockDeclRefExprs(CBE->getBody()); |
| 4054 | else |
| 4055 | GetBlockDeclRefExprs(*CI); |
| 4056 | } |
| 4057 | // Handle specific things. |
| 4058 | if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) |
| 4059 | // FIXME: Handle enums. |
| 4060 | if (!isa<FunctionDecl>(CDRE->getDecl())) |
| 4061 | BlockDeclRefs.push_back(CDRE); |
| 4062 | return; |
| 4063 | } |
| 4064 | |
| 4065 | void RewriteObjC::GetBlockCallExprs(Stmt *S) { |
| 4066 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4067 | CI != E; ++CI) |
| 4068 | if (*CI) { |
| 4069 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 4070 | GetBlockCallExprs(CBE->getBody()); |
| 4071 | else |
| 4072 | GetBlockCallExprs(*CI); |
| 4073 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4074 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4075 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
| 4076 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
| 4077 | BlockCallExprs[dyn_cast<BlockDeclRefExpr>(CE->getCallee())] = CE; |
| 4078 | } |
| 4079 | } |
| 4080 | return; |
| 4081 | } |
| 4082 | |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4083 | Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp, const Expr *BlockExp) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4084 | // Navigate to relevant type information. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4085 | const BlockPointerType *CPT = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4086 | |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4087 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BlockExp)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4088 | CPT = DRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4089 | } else if (const BlockDeclRefExpr *CDRE = |
| 4090 | dyn_cast<BlockDeclRefExpr>(BlockExp)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4091 | CPT = CDRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4092 | } else if (const MemberExpr *MExpr = dyn_cast<MemberExpr>(BlockExp)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4093 | CPT = MExpr->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4094 | } |
| 4095 | else if (const ParenExpr *PRE = dyn_cast<ParenExpr>(BlockExp)) { |
| 4096 | return SynthesizeBlockCall(Exp, PRE->getSubExpr()); |
| 4097 | } |
| 4098 | else if (const ImplicitCastExpr *IEXPR = dyn_cast<ImplicitCastExpr>(BlockExp)) |
| 4099 | CPT = IEXPR->getType()->getAs<BlockPointerType>(); |
| 4100 | else if (const ConditionalOperator *CEXPR = |
| 4101 | dyn_cast<ConditionalOperator>(BlockExp)) { |
| 4102 | Expr *LHSExp = CEXPR->getLHS(); |
| 4103 | Stmt *LHSStmt = SynthesizeBlockCall(Exp, LHSExp); |
| 4104 | Expr *RHSExp = CEXPR->getRHS(); |
| 4105 | Stmt *RHSStmt = SynthesizeBlockCall(Exp, RHSExp); |
| 4106 | Expr *CONDExp = CEXPR->getCond(); |
| 4107 | ConditionalOperator *CondExpr = |
| 4108 | new (Context) ConditionalOperator(CONDExp, |
| 4109 | SourceLocation(), cast<Expr>(LHSStmt), |
| 4110 | SourceLocation(), cast<Expr>(RHSStmt), |
| 4111 | Exp->getType()); |
| 4112 | return CondExpr; |
Fariborz Jahanian | e24b22b | 2009-12-18 01:15:21 +0000 | [diff] [blame] | 4113 | } else if (const ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(BlockExp)) { |
| 4114 | CPT = IRE->getType()->getAs<BlockPointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4115 | } else { |
| 4116 | assert(1 && "RewriteBlockClass: Bad type"); |
| 4117 | } |
| 4118 | assert(CPT && "RewriteBlockClass: Bad type"); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4119 | const FunctionType *FT = CPT->getPointeeType()->getAs<FunctionType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4120 | assert(FT && "RewriteBlockClass: Bad type"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4121 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4122 | // FTP will be null for closures that don't take arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4123 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4124 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
| 4125 | SourceLocation(), |
| 4126 | &Context->Idents.get("__block_impl")); |
| 4127 | QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD)); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4128 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4129 | // Generate a funky cast. |
| 4130 | llvm::SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4131 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4132 | // Push the block argument type. |
| 4133 | ArgTypes.push_back(PtrBlock); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4134 | if (FTP) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4135 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4136 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 4137 | QualType t = *I; |
| 4138 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4139 | if (isTopLevelBlockPointerType(t)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4140 | const BlockPointerType *BPT = t->getAs<BlockPointerType>(); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4141 | t = Context->getPointerType(BPT->getPointeeType()); |
| 4142 | } |
| 4143 | ArgTypes.push_back(t); |
| 4144 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4145 | } |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4146 | // Now do the pointer to function cast. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4147 | QualType PtrToFuncCastType = Context->getFunctionType(Exp->getType(), |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4148 | &ArgTypes[0], ArgTypes.size(), false/*no variadic*/, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4149 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4150 | PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4151 | |
| 4152 | CastExpr *BlkCast = new (Context) CStyleCastExpr(PtrBlock, |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 4153 | CastExpr::CK_Unknown, |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4154 | const_cast<Expr*>(BlockExp), |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4155 | PtrBlock, SourceLocation(), |
| 4156 | SourceLocation()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4157 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4158 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 4159 | BlkCast); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4160 | //PE->dump(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4161 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4162 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4163 | &Context->Idents.get("FuncPtr"), Context->VoidPtrTy, 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 4164 | /*BitWidth=*/0, /*Mutable=*/true); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4165 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
| 4166 | FD->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4167 | |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 4168 | CastExpr *FunkCast = new (Context) CStyleCastExpr(PtrToFuncCastType, |
| 4169 | CastExpr::CK_Unknown, ME, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4170 | PtrToFuncCastType, |
| 4171 | SourceLocation(), |
| 4172 | SourceLocation()); |
| 4173 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4174 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4175 | llvm::SmallVector<Expr*, 8> BlkExprs; |
| 4176 | // Add the implicit argument. |
| 4177 | BlkExprs.push_back(BlkCast); |
| 4178 | // Add the user arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4179 | for (CallExpr::arg_iterator I = Exp->arg_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4180 | E = Exp->arg_end(); I != E; ++I) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4181 | BlkExprs.push_back(*I); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4182 | } |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 4183 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &BlkExprs[0], |
| 4184 | BlkExprs.size(), |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4185 | Exp->getType(), SourceLocation()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4186 | return CE; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4187 | } |
| 4188 | |
| 4189 | void RewriteObjC::RewriteBlockCall(CallExpr *Exp) { |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4190 | Stmt *BlockCall = SynthesizeBlockCall(Exp, Exp->getCallee()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4191 | ReplaceStmt(Exp, BlockCall); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4192 | } |
| 4193 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4194 | // We need to return the rewritten expression to handle cases where the |
| 4195 | // BlockDeclRefExpr is embedded in another expression being rewritten. |
| 4196 | // For example: |
| 4197 | // |
| 4198 | // int main() { |
| 4199 | // __block Foo *f; |
| 4200 | // __block int i; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4201 | // |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4202 | // void (^myblock)() = ^() { |
| 4203 | // [f test]; // f is a BlockDeclRefExpr embedded in a message (which is being rewritten). |
| 4204 | // i = 77; |
| 4205 | // }; |
| 4206 | //} |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4207 | Stmt *RewriteObjC::RewriteBlockDeclRefExpr(Expr *DeclRefExp) { |
Fariborz Jahanian | bbf37e2 | 2009-12-23 19:26:34 +0000 | [diff] [blame] | 4208 | // Rewrite the byref variable into BYREFVAR->__forwarding->BYREFVAR |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4209 | // for each DeclRefExp where BYREFVAR is name of the variable. |
| 4210 | ValueDecl *VD; |
| 4211 | bool isArrow = true; |
| 4212 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(DeclRefExp)) |
| 4213 | VD = BDRE->getDecl(); |
| 4214 | else { |
| 4215 | VD = cast<DeclRefExpr>(DeclRefExp)->getDecl(); |
| 4216 | isArrow = false; |
| 4217 | } |
| 4218 | |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4219 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 4220 | &Context->Idents.get("__forwarding"), |
| 4221 | Context->VoidPtrTy, 0, |
| 4222 | /*BitWidth=*/0, /*Mutable=*/true); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4223 | MemberExpr *ME = new (Context) MemberExpr(DeclRefExp, isArrow, |
| 4224 | FD, SourceLocation(), |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4225 | FD->getType()); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4226 | |
| 4227 | const char *Name = VD->getNameAsCString(); |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4228 | FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 4229 | &Context->Idents.get(Name), |
| 4230 | Context->VoidPtrTy, 0, |
| 4231 | /*BitWidth=*/0, /*Mutable=*/true); |
| 4232 | ME = new (Context) MemberExpr(ME, true, FD, SourceLocation(), |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4233 | DeclRefExp->getType()); |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4234 | |
| 4235 | |
| 4236 | |
Steve Naroff | df8570d | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 4237 | // Need parens to enforce precedence. |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4238 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 4239 | ME); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4240 | ReplaceStmt(DeclRefExp, PE); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4241 | return PE; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4242 | } |
| 4243 | |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4244 | void RewriteObjC::RewriteCastExpr(CStyleCastExpr *CE) { |
| 4245 | SourceLocation LocStart = CE->getLParenLoc(); |
| 4246 | SourceLocation LocEnd = CE->getRParenLoc(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4247 | |
| 4248 | // Need to avoid trying to rewrite synthesized casts. |
| 4249 | if (LocStart.isInvalid()) |
| 4250 | return; |
Steve Naroff | 8f6ce57 | 2008-11-03 11:20:24 +0000 | [diff] [blame] | 4251 | // Need to avoid trying to rewrite casts contained in macros. |
| 4252 | if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) |
| 4253 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4254 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4255 | const char *startBuf = SM->getCharacterData(LocStart); |
| 4256 | const char *endBuf = SM->getCharacterData(LocEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4257 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4258 | // advance the location to startArgList. |
| 4259 | const char *argPtr = startBuf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4260 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4261 | while (*argPtr++ && (argPtr < endBuf)) { |
| 4262 | switch (*argPtr) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4263 | case '^': |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4264 | // Replace the '^' with '*'. |
| 4265 | LocStart = LocStart.getFileLocWithOffset(argPtr-startBuf); |
| 4266 | ReplaceText(LocStart, 1, "*", 1); |
| 4267 | break; |
| 4268 | } |
| 4269 | } |
| 4270 | return; |
| 4271 | } |
| 4272 | |
| 4273 | void RewriteObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { |
| 4274 | SourceLocation DeclLoc = FD->getLocation(); |
| 4275 | unsigned parenCount = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4276 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4277 | // We have 1 or more arguments that have closure pointers. |
| 4278 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4279 | const char *startArgList = strchr(startBuf, '('); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4280 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4281 | assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4282 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4283 | parenCount++; |
| 4284 | // advance the location to startArgList. |
| 4285 | DeclLoc = DeclLoc.getFileLocWithOffset(startArgList-startBuf); |
| 4286 | assert((DeclLoc.isValid()) && "Invalid DeclLoc"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4287 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4288 | const char *argPtr = startArgList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4289 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4290 | while (*argPtr++ && parenCount) { |
| 4291 | switch (*argPtr) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4292 | case '^': |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4293 | // Replace the '^' with '*'. |
| 4294 | DeclLoc = DeclLoc.getFileLocWithOffset(argPtr-startArgList); |
| 4295 | ReplaceText(DeclLoc, 1, "*", 1); |
| 4296 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4297 | case '(': |
| 4298 | parenCount++; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4299 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4300 | case ')': |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4301 | parenCount--; |
| 4302 | break; |
| 4303 | } |
| 4304 | } |
| 4305 | return; |
| 4306 | } |
| 4307 | |
| 4308 | bool RewriteObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4309 | const FunctionProtoType *FTP; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4310 | const PointerType *PT = QT->getAs<PointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4311 | if (PT) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4312 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4313 | } else { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4314 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4315 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4316 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4317 | } |
| 4318 | if (FTP) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4319 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4320 | E = FTP->arg_type_end(); I != E; ++I) |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4321 | if (isTopLevelBlockPointerType(*I)) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4322 | return true; |
| 4323 | } |
| 4324 | return false; |
| 4325 | } |
| 4326 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4327 | void RewriteObjC::GetExtentOfArgList(const char *Name, const char *&LParen, |
| 4328 | const char *&RParen) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4329 | const char *argPtr = strchr(Name, '('); |
| 4330 | assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4331 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4332 | LParen = argPtr; // output the start. |
| 4333 | argPtr++; // skip past the left paren. |
| 4334 | unsigned parenCount = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4335 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4336 | while (*argPtr && parenCount) { |
| 4337 | switch (*argPtr) { |
| 4338 | case '(': parenCount++; break; |
| 4339 | case ')': parenCount--; break; |
| 4340 | default: break; |
| 4341 | } |
| 4342 | if (parenCount) argPtr++; |
| 4343 | } |
| 4344 | assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); |
| 4345 | RParen = argPtr; // output the end |
| 4346 | } |
| 4347 | |
| 4348 | void RewriteObjC::RewriteBlockPointerDecl(NamedDecl *ND) { |
| 4349 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 4350 | RewriteBlockPointerFunctionArgs(FD); |
| 4351 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4352 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4353 | // Handle Variables and Typedefs. |
| 4354 | SourceLocation DeclLoc = ND->getLocation(); |
| 4355 | QualType DeclT; |
| 4356 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4357 | DeclT = VD->getType(); |
| 4358 | else if (TypedefDecl *TDD = dyn_cast<TypedefDecl>(ND)) |
| 4359 | DeclT = TDD->getUnderlyingType(); |
| 4360 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) |
| 4361 | DeclT = FD->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4362 | else |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4363 | assert(0 && "RewriteBlockPointerDecl(): Decl type not yet handled"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4364 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4365 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4366 | const char *endBuf = startBuf; |
| 4367 | // scan backward (from the decl location) for the end of the previous decl. |
| 4368 | while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) |
| 4369 | startBuf--; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4370 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4371 | // *startBuf != '^' if we are dealing with a pointer to function that |
| 4372 | // may take block argument types (which will be handled below). |
| 4373 | if (*startBuf == '^') { |
| 4374 | // Replace the '^' with '*', computing a negative offset. |
| 4375 | DeclLoc = DeclLoc.getFileLocWithOffset(startBuf-endBuf); |
| 4376 | ReplaceText(DeclLoc, 1, "*", 1); |
| 4377 | } |
| 4378 | if (PointerTypeTakesAnyBlockArguments(DeclT)) { |
| 4379 | // Replace the '^' with '*' for arguments. |
| 4380 | DeclLoc = ND->getLocation(); |
| 4381 | startBuf = SM->getCharacterData(DeclLoc); |
| 4382 | const char *argListBegin, *argListEnd; |
| 4383 | GetExtentOfArgList(startBuf, argListBegin, argListEnd); |
| 4384 | while (argListBegin < argListEnd) { |
| 4385 | if (*argListBegin == '^') { |
| 4386 | SourceLocation CaretLoc = DeclLoc.getFileLocWithOffset(argListBegin-startBuf); |
| 4387 | ReplaceText(CaretLoc, 1, "*", 1); |
| 4388 | } |
| 4389 | argListBegin++; |
| 4390 | } |
| 4391 | } |
| 4392 | return; |
| 4393 | } |
| 4394 | |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4395 | |
| 4396 | /// SynthesizeByrefCopyDestroyHelper - This routine synthesizes: |
| 4397 | /// void __Block_byref_id_object_copy(struct Block_byref_id_object *dst, |
| 4398 | /// struct Block_byref_id_object *src) { |
| 4399 | /// _Block_object_assign (&_dest->object, _src->object, |
| 4400 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4401 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4402 | /// _Block_object_assign(&_dest->object, _src->object, |
| 4403 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4404 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4405 | /// } |
| 4406 | /// And: |
| 4407 | /// void __Block_byref_id_object_dispose(struct Block_byref_id_object *_src) { |
| 4408 | /// _Block_object_dispose(_src->object, |
| 4409 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4410 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4411 | /// _Block_object_dispose(_src->object, |
| 4412 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4413 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4414 | /// } |
| 4415 | |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4416 | std::string RewriteObjC::SynthesizeByrefCopyDestroyHelper(VarDecl *VD, |
| 4417 | int flag) { |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4418 | std::string S; |
Benjamin Kramer | 1211a71 | 2010-01-10 19:57:50 +0000 | [diff] [blame^] | 4419 | if (CopyDestroyCache.count(flag)) |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4420 | return S; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4421 | CopyDestroyCache.insert(flag); |
| 4422 | S = "static void __Block_byref_id_object_copy_"; |
| 4423 | S += utostr(flag); |
| 4424 | S += "(void *dst, void *src) {\n"; |
| 4425 | |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4426 | // offset into the object pointer is computed as: |
| 4427 | // void * + void* + int + int + void* + void * |
| 4428 | unsigned IntSize = |
| 4429 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 4430 | unsigned VoidPtrSize = |
| 4431 | static_cast<unsigned>(Context->getTypeSize(Context->VoidPtrTy)); |
| 4432 | |
| 4433 | unsigned offset = (VoidPtrSize*4 + IntSize + IntSize)/8; |
| 4434 | S += " _Block_object_assign((char*)dst + "; |
| 4435 | S += utostr(offset); |
| 4436 | S += ", *(void * *) ((char*)src + "; |
| 4437 | S += utostr(offset); |
| 4438 | S += "), "; |
| 4439 | S += utostr(flag); |
| 4440 | S += ");\n}\n"; |
| 4441 | |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4442 | S += "static void __Block_byref_id_object_dispose_"; |
| 4443 | S += utostr(flag); |
| 4444 | S += "(void *src) {\n"; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4445 | S += " _Block_object_dispose(*(void * *) ((char*)src + "; |
| 4446 | S += utostr(offset); |
| 4447 | S += "), "; |
| 4448 | S += utostr(flag); |
| 4449 | S += ");\n}\n"; |
| 4450 | return S; |
| 4451 | } |
| 4452 | |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4453 | /// RewriteByRefVar - For each __block typex ND variable this routine transforms |
| 4454 | /// the declaration into: |
| 4455 | /// struct __Block_byref_ND { |
| 4456 | /// void *__isa; // NULL for everything except __weak pointers |
| 4457 | /// struct __Block_byref_ND *__forwarding; |
| 4458 | /// int32_t __flags; |
| 4459 | /// int32_t __size; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4460 | /// void *__Block_byref_id_object_copy; // If variable is __block ObjC object |
| 4461 | /// void *__Block_byref_id_object_dispose; // If variable is __block ObjC object |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4462 | /// typex ND; |
| 4463 | /// }; |
| 4464 | /// |
| 4465 | /// It then replaces declaration of ND variable with: |
| 4466 | /// struct __Block_byref_ND ND = {__isa=0B, __forwarding=&ND, __flags=some_flag, |
| 4467 | /// __size=sizeof(struct __Block_byref_ND), |
| 4468 | /// ND=initializer-if-any}; |
| 4469 | /// |
| 4470 | /// |
| 4471 | void RewriteObjC::RewriteByRefVar(VarDecl *ND) { |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4472 | int flag = 0; |
| 4473 | int isa = 0; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4474 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
| 4475 | const char *startBuf = SM->getCharacterData(DeclLoc); |
Fariborz Jahanian | 6f0a0a9 | 2009-12-30 20:38:08 +0000 | [diff] [blame] | 4476 | SourceLocation X = ND->getLocEnd(); |
| 4477 | X = SM->getInstantiationLoc(X); |
| 4478 | const char *endBuf = SM->getCharacterData(X); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4479 | std::string Name(ND->getNameAsString()); |
| 4480 | std::string ByrefType = "struct __Block_byref_"; |
| 4481 | ByrefType += Name; |
| 4482 | ByrefType += " {\n"; |
| 4483 | ByrefType += " void *__isa;\n"; |
| 4484 | ByrefType += " struct __Block_byref_" + Name + " *__forwarding;\n"; |
| 4485 | ByrefType += " int __flags;\n"; |
| 4486 | ByrefType += " int __size;\n"; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4487 | // Add void *__Block_byref_id_object_copy; |
| 4488 | // void *__Block_byref_id_object_dispose; if needed. |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4489 | QualType Ty = ND->getType(); |
| 4490 | bool HasCopyAndDispose = Context->BlockRequiresCopying(Ty); |
| 4491 | if (HasCopyAndDispose) { |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4492 | ByrefType += " void (*__Block_byref_id_object_copy)(void*, void*);\n"; |
| 4493 | ByrefType += " void (*__Block_byref_id_object_dispose)(void*);\n"; |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4494 | } |
| 4495 | |
| 4496 | Ty.getAsStringInternal(Name, Context->PrintingPolicy); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4497 | ByrefType += " " + Name + ";\n"; |
| 4498 | ByrefType += "};\n"; |
| 4499 | // Insert this type in global scope. It is needed by helper function. |
| 4500 | assert(CurFunctionDef && "RewriteByRefVar - CurFunctionDef is null"); |
| 4501 | SourceLocation FunLocStart = CurFunctionDef->getTypeSpecStartLoc(); |
| 4502 | InsertText(FunLocStart, ByrefType.c_str(), ByrefType.size()); |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4503 | if (Ty.isObjCGCWeak()) { |
| 4504 | flag |= BLOCK_FIELD_IS_WEAK; |
| 4505 | isa = 1; |
| 4506 | } |
| 4507 | |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4508 | if (HasCopyAndDispose) { |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4509 | flag = BLOCK_BYREF_CALLER; |
| 4510 | QualType Ty = ND->getType(); |
| 4511 | // FIXME. Handle __weak variable (BLOCK_FIELD_IS_WEAK) as well. |
| 4512 | if (Ty->isBlockPointerType()) |
| 4513 | flag |= BLOCK_FIELD_IS_BLOCK; |
| 4514 | else |
| 4515 | flag |= BLOCK_FIELD_IS_OBJECT; |
| 4516 | std::string HF = SynthesizeByrefCopyDestroyHelper(ND, flag); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4517 | if (!HF.empty()) |
| 4518 | InsertText(FunLocStart, HF.c_str(), HF.size()); |
| 4519 | } |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4520 | |
| 4521 | // struct __Block_byref_ND ND = |
| 4522 | // {0, &ND, some_flag, __size=sizeof(struct __Block_byref_ND), |
| 4523 | // initializer-if-any}; |
| 4524 | bool hasInit = (ND->getInit() != 0); |
Fariborz Jahanian | e1f84f8 | 2010-01-05 18:15:57 +0000 | [diff] [blame] | 4525 | unsigned flags = 0; |
| 4526 | if (HasCopyAndDispose) |
| 4527 | flags |= BLOCK_HAS_COPY_DISPOSE; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4528 | Name = ND->getNameAsString(); |
| 4529 | ByrefType = "struct __Block_byref_" + Name; |
| 4530 | if (!hasInit) { |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4531 | ByrefType += " " + Name + " = {(void*)"; |
| 4532 | ByrefType += utostr(isa); |
| 4533 | ByrefType += ", &" + Name + ", "; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4534 | ByrefType += utostr(flags); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4535 | ByrefType += ", "; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4536 | ByrefType += "sizeof(struct __Block_byref_" + Name + ")"; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4537 | if (HasCopyAndDispose) { |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4538 | ByrefType += ", __Block_byref_id_object_copy_"; |
| 4539 | ByrefType += utostr(flag); |
| 4540 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 4541 | ByrefType += utostr(flag); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4542 | } |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4543 | ByrefType += "};\n"; |
| 4544 | ReplaceText(DeclLoc, endBuf-startBuf+Name.size(), |
| 4545 | ByrefType.c_str(), ByrefType.size()); |
| 4546 | } |
| 4547 | else { |
| 4548 | SourceLocation startLoc = ND->getInit()->getLocStart(); |
Fariborz Jahanian | 791b10d | 2010-01-05 23:06:29 +0000 | [diff] [blame] | 4549 | startLoc = SM->getInstantiationLoc(startLoc); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4550 | ByrefType += " " + Name; |
| 4551 | ReplaceText(DeclLoc, endBuf-startBuf, |
| 4552 | ByrefType.c_str(), ByrefType.size()); |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4553 | ByrefType = " = {(void*)"; |
| 4554 | ByrefType += utostr(isa); |
| 4555 | ByrefType += ", &" + Name + ", "; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4556 | ByrefType += utostr(flags); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4557 | ByrefType += ", "; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4558 | ByrefType += "sizeof(struct __Block_byref_" + Name + "), "; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4559 | if (HasCopyAndDispose) { |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4560 | ByrefType += "__Block_byref_id_object_copy_"; |
| 4561 | ByrefType += utostr(flag); |
| 4562 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 4563 | ByrefType += utostr(flag); |
| 4564 | ByrefType += ", "; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4565 | } |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4566 | InsertText(startLoc, ByrefType.c_str(), ByrefType.size()); |
Steve Naroff | c5143c5 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 4567 | |
| 4568 | // Complete the newly synthesized compound expression by inserting a right |
| 4569 | // curly brace before the end of the declaration. |
| 4570 | // FIXME: This approach avoids rewriting the initializer expression. It |
| 4571 | // also assumes there is only one declarator. For example, the following |
| 4572 | // isn't currently supported by this routine (in general): |
| 4573 | // |
| 4574 | // double __block BYREFVAR = 1.34, BYREFVAR2 = 1.37; |
| 4575 | // |
| 4576 | const char *startBuf = SM->getCharacterData(startLoc); |
| 4577 | const char *semiBuf = strchr(startBuf, ';'); |
| 4578 | assert((*semiBuf == ';') && "RewriteByRefVar: can't find ';'"); |
| 4579 | SourceLocation semiLoc = |
| 4580 | startLoc.getFileLocWithOffset(semiBuf-startBuf); |
| 4581 | |
| 4582 | InsertText(semiLoc, "}", 1); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4583 | } |
Fariborz Jahanian | 1be6b46 | 2009-12-22 00:48:54 +0000 | [diff] [blame] | 4584 | return; |
| 4585 | } |
| 4586 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4587 | void RewriteObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4588 | // Add initializers for any closure decl refs. |
| 4589 | GetBlockDeclRefExprs(Exp->getBody()); |
| 4590 | if (BlockDeclRefs.size()) { |
| 4591 | // Unique all "by copy" declarations. |
| 4592 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 4593 | if (!BlockDeclRefs[i]->isByRef()) |
| 4594 | BlockByCopyDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 4595 | // Unique all "by ref" declarations. |
| 4596 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 4597 | if (BlockDeclRefs[i]->isByRef()) { |
| 4598 | BlockByRefDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 4599 | } |
| 4600 | // Find any imported blocks...they will need special attention. |
| 4601 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
Fariborz Jahanian | 4fcc4fd | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 4602 | if (BlockDeclRefs[i]->isByRef() || |
| 4603 | BlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
| 4604 | BlockDeclRefs[i]->getType()->isBlockPointerType()) { |
Steve Naroff | 0007268 | 2008-11-13 17:40:07 +0000 | [diff] [blame] | 4605 | GetBlockCallExprs(BlockDeclRefs[i]); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4606 | ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 4607 | } |
| 4608 | } |
| 4609 | } |
| 4610 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4611 | FunctionDecl *RewriteObjC::SynthBlockInitFunctionDecl(const char *name) { |
| 4612 | IdentifierInfo *ID = &Context->Idents.get(name); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4613 | QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4614 | return FunctionDecl::Create(*Context, TUDecl,SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 4615 | ID, FType, 0, FunctionDecl::Extern, false, |
Douglas Gregor | 2224f84 | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 4616 | false); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4617 | } |
| 4618 | |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4619 | Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4620 | Blocks.push_back(Exp); |
| 4621 | |
| 4622 | CollectBlockDeclRefInfo(Exp); |
| 4623 | std::string FuncName; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4624 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4625 | if (CurFunctionDef) |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4626 | FuncName = CurFunctionDef->getNameAsString(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4627 | else if (CurMethodDef) { |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4628 | FuncName = CurMethodDef->getSelector().getAsString(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4629 | // Convert colons to underscores. |
| 4630 | std::string::size_type loc = 0; |
| 4631 | while ((loc = FuncName.find(":", loc)) != std::string::npos) |
| 4632 | FuncName.replace(loc, 1, "_"); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4633 | } else if (GlobalVarDecl) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4634 | FuncName = std::string(GlobalVarDecl->getNameAsString()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4635 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4636 | std::string BlockNumber = utostr(Blocks.size()-1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4637 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4638 | std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber; |
| 4639 | std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4640 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4641 | // Get a pointer to the function type so we can cast appropriately. |
| 4642 | QualType FType = Context->getPointerType(QualType(Exp->getFunctionType(),0)); |
| 4643 | |
| 4644 | FunctionDecl *FD; |
| 4645 | Expr *NewRep; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4646 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4647 | // Simulate a contructor call... |
| 4648 | FD = SynthBlockInitFunctionDecl(Tag.c_str()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4649 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, FType, SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4650 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4651 | llvm::SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4652 | |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4653 | // Initialize the block function. |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4654 | FD = SynthBlockInitFunctionDecl(Func.c_str()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4655 | DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, FD->getType(), |
| 4656 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4657 | CastExpr *castExpr = new (Context) CStyleCastExpr(Context->VoidPtrTy, |
| 4658 | CastExpr::CK_Unknown, Arg, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4659 | Context->VoidPtrTy, SourceLocation(), |
| 4660 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4661 | InitExprs.push_back(castExpr); |
| 4662 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4663 | // Initialize the block descriptor. |
| 4664 | std::string DescData = "__" + FuncName + "_block_desc_" + BlockNumber + "_DATA"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4665 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4666 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
| 4667 | &Context->Idents.get(DescData.c_str()), |
| 4668 | Context->VoidPtrTy, 0, |
| 4669 | VarDecl::Static); |
| 4670 | UnaryOperator *DescRefExpr = new (Context) UnaryOperator( |
| 4671 | new (Context) DeclRefExpr(NewVD, |
| 4672 | Context->VoidPtrTy, SourceLocation()), |
| 4673 | UnaryOperator::AddrOf, |
| 4674 | Context->getPointerType(Context->VoidPtrTy), |
| 4675 | SourceLocation()); |
| 4676 | InitExprs.push_back(DescRefExpr); |
| 4677 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4678 | // Add initializers for any closure decl refs. |
| 4679 | if (BlockDeclRefs.size()) { |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4680 | Expr *Exp; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4681 | // Output all "by copy" declarations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4682 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4683 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4684 | if (isObjCType((*I)->getType())) { |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4685 | // FIXME: Conform to ABI ([[obj retain] autorelease]). |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4686 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4687 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4688 | } else if (isTopLevelBlockPointerType((*I)->getType())) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4689 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4690 | Arg = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4691 | Exp = new (Context) CStyleCastExpr(Context->VoidPtrTy, |
| 4692 | CastExpr::CK_Unknown, Arg, |
| 4693 | Context->VoidPtrTy, |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 4694 | SourceLocation(), |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4695 | SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4696 | } else { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4697 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4698 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4699 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4700 | InitExprs.push_back(Exp); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4701 | } |
| 4702 | // Output all "by ref" declarations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4703 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4704 | E = BlockByRefDecls.end(); I != E; ++I) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4705 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4706 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 4707 | Exp = new (Context) UnaryOperator(Exp, UnaryOperator::AddrOf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4708 | Context->getPointerType(Exp->getType()), |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4709 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4710 | InitExprs.push_back(Exp); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4711 | } |
| 4712 | } |
Fariborz Jahanian | ff12788 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 4713 | if (ImportedBlockDecls.size()) { |
| 4714 | // generate BLOCK_HAS_COPY_DISPOSE(have helper funcs) | BLOCK_HAS_DESCRIPTOR |
| 4715 | int flag = (BLOCK_HAS_COPY_DISPOSE | BLOCK_HAS_DESCRIPTOR); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4716 | unsigned IntSize = |
| 4717 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Fariborz Jahanian | ff12788 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 4718 | Expr *FlagExp = new (Context) IntegerLiteral(llvm::APInt(IntSize, flag), |
| 4719 | Context->IntTy, SourceLocation()); |
| 4720 | InitExprs.push_back(FlagExp); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4721 | } |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 4722 | NewRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], InitExprs.size(), |
| 4723 | FType, SourceLocation()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4724 | NewRep = new (Context) UnaryOperator(NewRep, UnaryOperator::AddrOf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4725 | Context->getPointerType(NewRep->getType()), |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4726 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4727 | NewRep = new (Context) CStyleCastExpr(FType, CastExpr::CK_Unknown, NewRep, |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 4728 | FType, SourceLocation(), |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4729 | SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4730 | BlockDeclRefs.clear(); |
| 4731 | BlockByRefDecls.clear(); |
| 4732 | BlockByCopyDecls.clear(); |
| 4733 | ImportedBlockDecls.clear(); |
| 4734 | return NewRep; |
| 4735 | } |
| 4736 | |
| 4737 | //===----------------------------------------------------------------------===// |
| 4738 | // Function Body / Expression rewriting |
| 4739 | //===----------------------------------------------------------------------===// |
| 4740 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4741 | // This is run as a first "pass" prior to RewriteFunctionBodyOrGlobalInitializer(). |
| 4742 | // The allows the main rewrite loop to associate all ObjCPropertyRefExprs with |
| 4743 | // their respective BinaryOperator. Without this knowledge, we'd need to rewrite |
| 4744 | // the ObjCPropertyRefExpr twice (once as a getter, and later as a setter). |
| 4745 | // Since the rewriter isn't capable of rewriting rewritten code, it's important |
| 4746 | // we get this right. |
| 4747 | void RewriteObjC::CollectPropertySetters(Stmt *S) { |
| 4748 | // Perform a bottom up traversal of all children. |
| 4749 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4750 | CI != E; ++CI) |
| 4751 | if (*CI) |
| 4752 | CollectPropertySetters(*CI); |
| 4753 | |
| 4754 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) { |
| 4755 | if (BinOp->isAssignmentOp()) { |
| 4756 | if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS())) |
| 4757 | PropSetters[PRE] = BinOp; |
| 4758 | } |
| 4759 | } |
| 4760 | } |
| 4761 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4762 | Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4763 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4764 | isa<DoStmt>(S) || isa<ForStmt>(S)) |
| 4765 | Stmts.push_back(S); |
| 4766 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 4767 | Stmts.push_back(S); |
Chris Lattner | 4824fcd | 2010-01-09 21:45:57 +0000 | [diff] [blame] | 4768 | ObjCBcLabelNo.push_back(++BcLabelCount); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4769 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4770 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4771 | SourceRange OrigStmtRange = S->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4772 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4773 | // Perform a bottom up rewrite of all children. |
| 4774 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4775 | CI != E; ++CI) |
| 4776 | if (*CI) { |
| 4777 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(*CI); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4778 | if (newStmt) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4779 | *CI = newStmt; |
| 4780 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4781 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4782 | if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
| 4783 | // Rewrite the block body in place. |
| 4784 | RewriteFunctionBodyOrGlobalInitializer(BE->getBody()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4785 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4786 | // Now we snarf the rewritten text and stash it away for later use. |
Ted Kremenek | 6a12a14 | 2010-01-07 18:00:35 +0000 | [diff] [blame] | 4787 | std::string Str = Rewrite.getRewrittenText(BE->getSourceRange()); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4788 | RewrittenBlockExprs[BE] = Str; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4789 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4790 | Stmt *blockTranscribed = SynthBlockInitExpr(BE); |
| 4791 | //blockTranscribed->dump(); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4792 | ReplaceStmt(S, blockTranscribed); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4793 | return blockTranscribed; |
| 4794 | } |
| 4795 | // Handle specific things. |
| 4796 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 4797 | return RewriteAtEncode(AtEncode); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4798 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4799 | if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) |
| 4800 | return RewriteObjCIvarRefExpr(IvarRefExpr, OrigStmtRange.getBegin()); |
| 4801 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4802 | if (ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(S)) { |
| 4803 | BinaryOperator *BinOp = PropSetters[PropRefExpr]; |
| 4804 | if (BinOp) { |
| 4805 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 4806 | // we need to rewrite the right hand side prior to rewriting the setter. |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 4807 | DisableReplaceStmt = true; |
| 4808 | // Save the source range. Even if we disable the replacement, the |
| 4809 | // rewritten node will have been inserted into the tree. If the synthesized |
| 4810 | // node is at the 'end', the rewriter will fail. Consider this: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4811 | // self.errorHandler = handler ? handler : |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 4812 | // ^(NSURL *errorURL, NSError *error) { return (BOOL)1; }; |
| 4813 | SourceRange SrcRange = BinOp->getSourceRange(); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4814 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(BinOp->getRHS()); |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 4815 | DisableReplaceStmt = false; |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 4816 | // |
| 4817 | // Unlike the main iterator, we explicily avoid changing 'BinOp'. If |
| 4818 | // we changed the RHS of BinOp, the rewriter would fail (since it needs |
| 4819 | // to see the original expression). Consider this example: |
| 4820 | // |
| 4821 | // Foo *obj1, *obj2; |
| 4822 | // |
| 4823 | // obj1.i = [obj2 rrrr]; |
| 4824 | // |
| 4825 | // 'BinOp' for the previous expression looks like: |
| 4826 | // |
| 4827 | // (BinaryOperator 0x231ccf0 'int' '=' |
| 4828 | // (ObjCPropertyRefExpr 0x231cc70 'int' Kind=PropertyRef Property="i" |
| 4829 | // (DeclRefExpr 0x231cc50 'Foo *' Var='obj1' 0x231cbb0)) |
| 4830 | // (ObjCMessageExpr 0x231ccb0 'int' selector=rrrr |
| 4831 | // (DeclRefExpr 0x231cc90 'Foo *' Var='obj2' 0x231cbe0))) |
| 4832 | // |
| 4833 | // 'newStmt' represents the rewritten message expression. For example: |
| 4834 | // |
| 4835 | // (CallExpr 0x231d300 'id':'struct objc_object *' |
| 4836 | // (ParenExpr 0x231d2e0 'int (*)(id, SEL)' |
| 4837 | // (CStyleCastExpr 0x231d2c0 'int (*)(id, SEL)' |
| 4838 | // (CStyleCastExpr 0x231d220 'void *' |
| 4839 | // (DeclRefExpr 0x231d200 'id (id, SEL, ...)' FunctionDecl='objc_msgSend' 0x231cdc0)))) |
| 4840 | // |
| 4841 | // Note that 'newStmt' is passed to RewritePropertySetter so that it |
| 4842 | // can be used as the setter argument. ReplaceStmt() will still 'see' |
| 4843 | // the original RHS (since we haven't altered BinOp). |
| 4844 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4845 | // This implies the Rewrite* routines can no longer delete the original |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 4846 | // node. As a result, we now leak the original AST nodes. |
| 4847 | // |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 4848 | return RewritePropertySetter(BinOp, dyn_cast<Expr>(newStmt), SrcRange); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4849 | } else { |
| 4850 | return RewritePropertyGetter(PropRefExpr); |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 4851 | } |
| 4852 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4853 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 4854 | return RewriteAtSelector(AtSelector); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4855 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4856 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 4857 | return RewriteObjCStringLiteral(AtString); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4858 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4859 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4860 | #if 0 |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4861 | // Before we rewrite it, put the original message expression in a comment. |
| 4862 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 4863 | SourceLocation endLoc = MessExpr->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4864 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4865 | const char *startBuf = SM->getCharacterData(startLoc); |
| 4866 | const char *endBuf = SM->getCharacterData(endLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4867 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4868 | std::string messString; |
| 4869 | messString += "// "; |
| 4870 | messString.append(startBuf, endBuf-startBuf+1); |
| 4871 | messString += "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4872 | |
| 4873 | // FIXME: Missing definition of |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4874 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
| 4875 | // InsertText(startLoc, messString.c_str(), messString.size()); |
| 4876 | // Tried this, but it didn't work either... |
| 4877 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4878 | #endif |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4879 | return RewriteMessageExpr(MessExpr); |
| 4880 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4881 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4882 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 4883 | return RewriteObjCTryStmt(StmtTry); |
| 4884 | |
| 4885 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 4886 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 4887 | |
| 4888 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 4889 | return RewriteObjCThrowStmt(StmtThrow); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4890 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4891 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 4892 | return RewriteObjCProtocolExpr(ProtocolExp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4893 | |
| 4894 | if (ObjCForCollectionStmt *StmtForCollection = |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4895 | dyn_cast<ObjCForCollectionStmt>(S)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4896 | return RewriteObjCForCollectionStmt(StmtForCollection, |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4897 | OrigStmtRange.getEnd()); |
| 4898 | if (BreakStmt *StmtBreakStmt = |
| 4899 | dyn_cast<BreakStmt>(S)) |
| 4900 | return RewriteBreakStmt(StmtBreakStmt); |
| 4901 | if (ContinueStmt *StmtContinueStmt = |
| 4902 | dyn_cast<ContinueStmt>(S)) |
| 4903 | return RewriteContinueStmt(StmtContinueStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4904 | |
| 4905 | // 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] | 4906 | // and cast exprs. |
| 4907 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
| 4908 | // FIXME: What we're doing here is modifying the type-specifier that |
| 4909 | // precedes the first Decl. In the future the DeclGroup should have |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4910 | // a separate type-specifier that we can rewrite. |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4911 | // NOTE: We need to avoid rewriting the DeclStmt if it is within |
| 4912 | // the context of an ObjCForCollectionStmt. For example: |
| 4913 | // NSArray *someArray; |
| 4914 | // for (id <FooProtocol> index in someArray) ; |
| 4915 | // This is because RewriteObjCForCollectionStmt() does textual rewriting |
| 4916 | // and it depends on the original text locations/positions. |
Benjamin Kramer | b2041de | 2009-12-05 22:16:51 +0000 | [diff] [blame] | 4917 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4918 | RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4919 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4920 | // Blocks rewrite rules. |
| 4921 | for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); |
| 4922 | DI != DE; ++DI) { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 4923 | Decl *SD = *DI; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4924 | if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4925 | if (isTopLevelBlockPointerType(ND->getType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4926 | RewriteBlockPointerDecl(ND); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4927 | else if (ND->getType()->isFunctionPointerType()) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4928 | CheckFunctionPointerDecl(ND->getType(), ND); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4929 | if (VarDecl *VD = dyn_cast<VarDecl>(SD)) |
| 4930 | if (VD->hasAttr<BlocksAttr>()) |
| 4931 | RewriteByRefVar(VD); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4932 | } |
| 4933 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4934 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4935 | RewriteBlockPointerDecl(TD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4936 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4937 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 4938 | } |
| 4939 | } |
| 4940 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4941 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4942 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) |
| 4943 | RewriteObjCQualifiedInterfaceTypes(CE); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4944 | |
| 4945 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4946 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 4947 | assert(!Stmts.empty() && "Statement stack is empty"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4948 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 4949 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4950 | && "Statement stack mismatch"); |
| 4951 | Stmts.pop_back(); |
| 4952 | } |
| 4953 | // Handle blocks rewriting. |
| 4954 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
| 4955 | if (BDRE->isByRef()) |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4956 | return RewriteBlockDeclRefExpr(BDRE); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4957 | } |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4958 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 4959 | ValueDecl *VD = DRE->getDecl(); |
| 4960 | if (VD->hasAttr<BlocksAttr>()) |
| 4961 | return RewriteBlockDeclRefExpr(DRE); |
| 4962 | } |
| 4963 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4964 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4965 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4966 | Stmt *BlockCall = SynthesizeBlockCall(CE, CE->getCallee()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4967 | ReplaceStmt(S, BlockCall); |
| 4968 | return BlockCall; |
| 4969 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4970 | } |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4971 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4972 | RewriteCastExpr(CE); |
| 4973 | } |
| 4974 | #if 0 |
| 4975 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4976 | CastExpr *Replacement = new (Context) CastExpr(ICE->getType(), ICE->getSubExpr(), SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4977 | // Get the new text. |
| 4978 | std::string SStr; |
| 4979 | llvm::raw_string_ostream Buf(SStr); |
Eli Friedman | 3a9eb44 | 2009-05-30 05:19:26 +0000 | [diff] [blame] | 4980 | Replacement->printPretty(Buf, *Context); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4981 | const std::string &Str = Buf.str(); |
| 4982 | |
| 4983 | printf("CAST = %s\n", &Str[0]); |
| 4984 | InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 4985 | delete S; |
| 4986 | return Replacement; |
| 4987 | } |
| 4988 | #endif |
| 4989 | // Return this stmt unmodified. |
| 4990 | return S; |
| 4991 | } |
| 4992 | |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 4993 | void RewriteObjC::RewriteRecordBody(RecordDecl *RD) { |
| 4994 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 4995 | e = RD->field_end(); i != e; ++i) { |
| 4996 | FieldDecl *FD = *i; |
| 4997 | if (isTopLevelBlockPointerType(FD->getType())) |
| 4998 | RewriteBlockPointerDecl(FD); |
| 4999 | if (FD->getType()->isObjCQualifiedIdType() || |
| 5000 | FD->getType()->isObjCQualifiedInterfaceType()) |
| 5001 | RewriteObjCQualifiedInterfaceTypes(FD); |
| 5002 | } |
| 5003 | } |
| 5004 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5005 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 5006 | /// main file of the input. |
| 5007 | void RewriteObjC::HandleDeclInMainFile(Decl *D) { |
| 5008 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | cb73530 | 2008-12-17 00:20:22 +0000 | [diff] [blame] | 5009 | if (FD->isOverloadedOperator()) |
| 5010 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5011 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5012 | // Since function prototypes don't have ParmDecl's, we check the function |
| 5013 | // prototype. This enables us to rewrite function declarations and |
| 5014 | // definitions using the same code. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5015 | RewriteBlocksInFunctionProtoType(FD->getType(), FD); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5016 | |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 5017 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 5018 | if (CompoundStmt *Body = FD->getCompoundBody()) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5019 | CurFunctionDef = FD; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5020 | CollectPropertySetters(Body); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5021 | CurrentBody = Body; |
Ted Kremenek | eaab206 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 5022 | Body = |
| 5023 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 5024 | FD->setBody(Body); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5025 | CurrentBody = 0; |
| 5026 | if (PropParentMap) { |
| 5027 | delete PropParentMap; |
| 5028 | PropParentMap = 0; |
| 5029 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5030 | // This synthesizes and inserts the block "impl" struct, invoke function, |
| 5031 | // and any copy/dispose helper functions. |
| 5032 | InsertBlockLiteralsWithinFunction(FD); |
| 5033 | CurFunctionDef = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5034 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5035 | return; |
| 5036 | } |
| 5037 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 5038 | if (CompoundStmt *Body = MD->getCompoundBody()) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5039 | CurMethodDef = MD; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5040 | CollectPropertySetters(Body); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5041 | CurrentBody = Body; |
Ted Kremenek | eaab206 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 5042 | Body = |
| 5043 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 5044 | MD->setBody(Body); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5045 | CurrentBody = 0; |
| 5046 | if (PropParentMap) { |
| 5047 | delete PropParentMap; |
| 5048 | PropParentMap = 0; |
| 5049 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5050 | InsertBlockLiteralsWithinMethod(MD); |
| 5051 | CurMethodDef = 0; |
| 5052 | } |
| 5053 | } |
| 5054 | if (ObjCImplementationDecl *CI = dyn_cast<ObjCImplementationDecl>(D)) |
| 5055 | ClassImplementation.push_back(CI); |
| 5056 | else if (ObjCCategoryImplDecl *CI = dyn_cast<ObjCCategoryImplDecl>(D)) |
| 5057 | CategoryImplementation.push_back(CI); |
| 5058 | else if (ObjCClassDecl *CD = dyn_cast<ObjCClassDecl>(D)) |
| 5059 | RewriteForwardClassDecl(CD); |
| 5060 | else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 5061 | RewriteObjCQualifiedInterfaceTypes(VD); |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5062 | if (isTopLevelBlockPointerType(VD->getType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5063 | RewriteBlockPointerDecl(VD); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5064 | else if (VD->getType()->isFunctionPointerType()) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5065 | CheckFunctionPointerDecl(VD->getType(), VD); |
| 5066 | if (VD->getInit()) { |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 5067 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5068 | RewriteCastExpr(CE); |
| 5069 | } |
| 5070 | } |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5071 | } else if (VD->getType()->isRecordType()) { |
| 5072 | RecordDecl *RD = VD->getType()->getAs<RecordType>()->getDecl(); |
| 5073 | if (RD->isDefinition()) |
| 5074 | RewriteRecordBody(RD); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5075 | } |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5076 | if (VD->getInit()) { |
| 5077 | GlobalVarDecl = VD; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5078 | CollectPropertySetters(VD->getInit()); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5079 | CurrentBody = VD->getInit(); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5080 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5081 | CurrentBody = 0; |
| 5082 | if (PropParentMap) { |
| 5083 | delete PropParentMap; |
| 5084 | PropParentMap = 0; |
| 5085 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5086 | SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 5087 | VD->getNameAsCString()); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5088 | GlobalVarDecl = 0; |
| 5089 | |
| 5090 | // This is needed for blocks. |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 5091 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5092 | RewriteCastExpr(CE); |
| 5093 | } |
| 5094 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5095 | return; |
| 5096 | } |
| 5097 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5098 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5099 | RewriteBlockPointerDecl(TD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5100 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5101 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5102 | else if (TD->getUnderlyingType()->isRecordType()) { |
| 5103 | RecordDecl *RD = TD->getUnderlyingType()->getAs<RecordType>()->getDecl(); |
| 5104 | if (RD->isDefinition()) |
| 5105 | RewriteRecordBody(RD); |
| 5106 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5107 | return; |
| 5108 | } |
| 5109 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) { |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5110 | if (RD->isDefinition()) |
| 5111 | RewriteRecordBody(RD); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5112 | return; |
| 5113 | } |
| 5114 | // Nothing yet. |
| 5115 | } |
| 5116 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 5117 | void RewriteObjC::HandleTranslationUnit(ASTContext &C) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5118 | // Get the top-level buffer that this corresponds to. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5119 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5120 | // Rewrite tabs if we care. |
| 5121 | //RewriteTabs(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5122 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5123 | if (Diags.hasErrorOccurred()) |
| 5124 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5125 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5126 | RewriteInclude(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5127 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5128 | // Here's a great place to add any extra declarations that may be needed. |
| 5129 | // Write out meta data for each @protocol(<expr>). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5130 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5131 | E = ProtocolExprDecls.end(); I != E; ++I) |
| 5132 | RewriteObjCProtocolMetaData(*I, "", "", Preamble); |
| 5133 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5134 | InsertText(SM->getLocForStartOfFile(MainFileID), |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5135 | Preamble.c_str(), Preamble.size(), false); |
Steve Naroff | 0aab796 | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5136 | if (ClassImplementation.size() || CategoryImplementation.size()) |
| 5137 | RewriteImplementations(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5138 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5139 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 5140 | // we are done. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5141 | if (const RewriteBuffer *RewriteBuf = |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5142 | Rewrite.getRewriteBufferFor(MainFileID)) { |
| 5143 | //printf("Changed:\n"); |
| 5144 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 5145 | } else { |
| 5146 | fprintf(stderr, "No changes\n"); |
| 5147 | } |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 5148 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5149 | if (ClassImplementation.size() || CategoryImplementation.size() || |
| 5150 | ProtocolExprDecls.size()) { |
Steve Naroff | 0aab796 | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5151 | // Rewrite Objective-c meta data* |
| 5152 | std::string ResultStr; |
| 5153 | SynthesizeMetaDataIntoBuffer(ResultStr); |
| 5154 | // Emit metadata. |
| 5155 | *OutFile << ResultStr; |
| 5156 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5157 | OutFile->flush(); |
| 5158 | } |
| 5159 | |