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; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 129 | llvm::DenseMap<ValueDecl *, unsigned> BlockByRefDeclNo; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 130 | llvm::SmallPtrSet<ValueDecl *, 8> ImportedBlockDecls; |
| 131 | |
| 132 | llvm::DenseMap<BlockExpr *, std::string> RewrittenBlockExprs; |
| 133 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 134 | // This maps a property to it's assignment statement. |
| 135 | llvm::DenseMap<ObjCPropertyRefExpr *, BinaryOperator *> PropSetters; |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 136 | // This maps a property to it's synthesied message expression. |
| 137 | // This allows us to rewrite chained getters (e.g. o.a.b.c). |
| 138 | llvm::DenseMap<ObjCPropertyRefExpr *, Stmt *> PropGetters; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 139 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 140 | // This maps an original source AST to it's rewritten form. This allows |
| 141 | // us to avoid rewriting the same node twice (which is very uncommon). |
| 142 | // This is needed to support some of the exotic property rewriting. |
| 143 | llvm::DenseMap<Stmt *, Stmt *> ReplacedNodes; |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 144 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 145 | FunctionDecl *CurFunctionDef; |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 146 | FunctionDecl *CurFunctionDeclToDeclareForBlock; |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 147 | VarDecl *GlobalVarDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 148 | |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 149 | bool DisableReplaceStmt; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 150 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 151 | static const int OBJC_ABI_VERSION =7 ; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 152 | public: |
Ted Kremenek | e3a6198 | 2008-05-31 20:11:04 +0000 | [diff] [blame] | 153 | virtual void Initialize(ASTContext &context); |
| 154 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 155 | // Top Level Driver code. |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 156 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
| 157 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) |
| 158 | HandleTopLevelSingleDecl(*I); |
| 159 | } |
| 160 | void HandleTopLevelSingleDecl(Decl *D); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 161 | void HandleDeclInMainFile(Decl *D); |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 162 | RewriteObjC(std::string inFile, llvm::raw_ostream *OS, |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 163 | Diagnostic &D, const LangOptions &LOpts, |
| 164 | bool silenceMacroWarn); |
Ted Kremenek | e452e0f | 2008-08-08 04:15:52 +0000 | [diff] [blame] | 165 | |
| 166 | ~RewriteObjC() {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 168 | virtual void HandleTranslationUnit(ASTContext &C); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 169 | |
Fariborz Jahanian | 88906cd | 2010-02-05 16:43:40 +0000 | [diff] [blame^] | 170 | void ReplaceStmt(Stmt *Old, Stmt *New) { |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 171 | Stmt *ReplacingStmt = ReplacedNodes[Old]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 172 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 173 | if (ReplacingStmt) |
| 174 | return; // We can't rewrite the same node twice. |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 175 | |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 176 | if (DisableReplaceStmt) |
| 177 | return; // Used when rewriting the assignment of a property setter. |
| 178 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 179 | // If replacement succeeded or warning disabled return with no warning. |
Fariborz Jahanian | 88906cd | 2010-02-05 16:43:40 +0000 | [diff] [blame^] | 180 | if (!Rewrite.ReplaceStmt(Old, New)) { |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 181 | ReplacedNodes[Old] = New; |
| 182 | return; |
| 183 | } |
| 184 | if (SilenceRewriteMacroWarning) |
| 185 | return; |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 186 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 187 | << Old->getSourceRange(); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 188 | } |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 189 | |
| 190 | void ReplaceStmtWithRange(Stmt *Old, Stmt *New, SourceRange SrcRange) { |
| 191 | // Measaure the old text. |
| 192 | int Size = Rewrite.getRangeSize(SrcRange); |
| 193 | if (Size == -1) { |
| 194 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 195 | << Old->getSourceRange(); |
| 196 | return; |
| 197 | } |
| 198 | // Get the new text. |
| 199 | std::string SStr; |
| 200 | llvm::raw_string_ostream S(SStr); |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 201 | New->printPretty(S, *Context, 0, PrintingPolicy(LangOpts)); |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 202 | const std::string &Str = S.str(); |
| 203 | |
| 204 | // If replacement succeeded or warning disabled return with no warning. |
Daniel Dunbar | d7407dc | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 205 | if (!Rewrite.ReplaceText(SrcRange.getBegin(), Size, Str)) { |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 206 | ReplacedNodes[Old] = New; |
| 207 | return; |
| 208 | } |
| 209 | if (SilenceRewriteMacroWarning) |
| 210 | return; |
| 211 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 212 | << Old->getSourceRange(); |
| 213 | } |
| 214 | |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 215 | void InsertText(SourceLocation Loc, const char *StrData, unsigned StrLen, |
| 216 | bool InsertAfter = true) { |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 217 | // If insertion succeeded or warning disabled return with no warning. |
Daniel Dunbar | d7407dc | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 218 | if (!Rewrite.InsertText(Loc, llvm::StringRef(StrData, StrLen), |
| 219 | InsertAfter) || |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 220 | SilenceRewriteMacroWarning) |
| 221 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 222 | |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 223 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 224 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 225 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 226 | void RemoveText(SourceLocation Loc, unsigned StrLen) { |
| 227 | // If removal succeeded or warning disabled return with no warning. |
| 228 | if (!Rewrite.RemoveText(Loc, StrLen) || SilenceRewriteMacroWarning) |
| 229 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 230 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 231 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 232 | } |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 233 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 234 | void ReplaceText(SourceLocation Start, unsigned OrigLength, |
| 235 | const char *NewStr, unsigned NewLength) { |
| 236 | // If removal succeeded or warning disabled return with no warning. |
Daniel Dunbar | d7407dc | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 237 | if (!Rewrite.ReplaceText(Start, OrigLength, |
| 238 | llvm::StringRef(NewStr, NewLength)) || |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 239 | SilenceRewriteMacroWarning) |
| 240 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 241 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 242 | Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag); |
| 243 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 244 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 245 | // Syntactic Rewriting. |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 246 | void RewritePrologue(SourceLocation Loc); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 247 | void RewriteInclude(); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 248 | void RewriteTabs(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 249 | void RewriteForwardClassDecl(ObjCClassDecl *Dcl); |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 250 | void RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 251 | ObjCImplementationDecl *IMD, |
| 252 | ObjCCategoryImplDecl *CID); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 253 | void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 254 | void RewriteImplementationDecl(Decl *Dcl); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 255 | void RewriteObjCMethodDecl(ObjCMethodDecl *MDecl, std::string &ResultStr); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 256 | void RewriteByRefString(std::string &ResultStr, const std::string &Name, |
| 257 | ValueDecl *VD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 258 | void RewriteCategoryDecl(ObjCCategoryDecl *Dcl); |
| 259 | void RewriteProtocolDecl(ObjCProtocolDecl *Dcl); |
| 260 | void RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *Dcl); |
| 261 | void RewriteMethodDeclaration(ObjCMethodDecl *Method); |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 262 | void RewriteProperty(ObjCPropertyDecl *prop); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 263 | void RewriteFunctionDecl(FunctionDecl *FD); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 264 | void RewriteBlockLiteralFunctionDecl(FunctionDecl *FD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 265 | void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 266 | void RewriteObjCQualifiedInterfaceTypes(Expr *E); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 267 | bool needToScanForQualifiers(QualType T); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 268 | ObjCInterfaceDecl *isSuperReceiver(Expr *recExpr); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 269 | QualType getSuperStructType(); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 270 | QualType getConstantStringStructType(); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 271 | bool BufferContainsPPDirectives(const char *startBuf, const char *endBuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 272 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 273 | // Expression Rewriting. |
Steve Naroff | f3473a7 | 2007-11-09 15:20:18 +0000 | [diff] [blame] | 274 | Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 275 | void CollectPropertySetters(Stmt *S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 276 | |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 277 | Stmt *CurrentBody; |
| 278 | ParentMap *PropParentMap; // created lazily. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 279 | |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 280 | Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp); |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 281 | Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, SourceLocation OrigStart, |
| 282 | bool &replaced); |
| 283 | Stmt *RewriteObjCNestedIvarRefExpr(Stmt *S, bool &replaced); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 284 | Stmt *RewritePropertyGetter(ObjCPropertyRefExpr *PropRefExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 285 | Stmt *RewritePropertySetter(BinaryOperator *BinOp, Expr *newStmt, |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 286 | SourceRange SrcRange); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 287 | Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp); |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 288 | Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp); |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 289 | Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp); |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 290 | Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 291 | void WarnAboutReturnGotoStmts(Stmt *S); |
| 292 | void HasReturnStmts(Stmt *S, bool &hasReturns); |
| 293 | void RewriteTryReturnStmts(Stmt *S); |
| 294 | void RewriteSyncReturnStmts(Stmt *S, std::string buf); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 295 | Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 296 | Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 297 | Stmt *RewriteObjCCatchStmt(ObjCAtCatchStmt *S); |
| 298 | Stmt *RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S); |
| 299 | Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S); |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 300 | Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
| 301 | SourceLocation OrigEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 302 | CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD, |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 303 | Expr **args, unsigned nargs); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 304 | Stmt *SynthMessageExpr(ObjCMessageExpr *Exp); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 305 | Stmt *RewriteBreakStmt(BreakStmt *S); |
| 306 | Stmt *RewriteContinueStmt(ContinueStmt *S); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 307 | void SynthCountByEnumWithState(std::string &buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 308 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 309 | void SynthMsgSendFunctionDecl(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 310 | void SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 311 | void SynthMsgSendStretFunctionDecl(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 312 | void SynthMsgSendFpretFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 313 | void SynthMsgSendSuperStretFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 314 | void SynthGetClassFunctionDecl(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 315 | void SynthGetMetaClassFunctionDecl(); |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 316 | void SynthSelGetUidFunctionDecl(); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 317 | void SynthSuperContructorFunctionDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 318 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 319 | // Metadata emission. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 320 | void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 321 | std::string &Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 322 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 323 | void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 324 | std::string &Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 325 | |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 326 | template<typename MethodIterator> |
| 327 | void RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 328 | MethodIterator MethodEnd, |
Fariborz Jahanian | 8e991ba | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 329 | bool IsInstanceMethod, |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 330 | const char *prefix, |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 331 | const char *ClassName, |
| 332 | std::string &Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 333 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 334 | void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol, |
| 335 | const char *prefix, |
| 336 | const char *ClassName, |
| 337 | std::string &Result); |
| 338 | void RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Prots, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 339 | const char *prefix, |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 340 | const char *ClassName, |
| 341 | std::string &Result); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 342 | void SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 343 | std::string &Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 344 | void SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl, |
| 345 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 346 | std::string &Result); |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 347 | void RewriteImplementations(); |
| 348 | void SynthesizeMetaDataIntoBuffer(std::string &Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 349 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 350 | // Block rewriting. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 351 | void RewriteBlocksInFunctionProtoType(QualType funcType, NamedDecl *D); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 352 | void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 353 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 354 | void InsertBlockLiteralsWithinFunction(FunctionDecl *FD); |
| 355 | void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 356 | |
| 357 | // Block specific rewrite rules. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 358 | void RewriteBlockCall(CallExpr *Exp); |
| 359 | void RewriteBlockPointerDecl(NamedDecl *VD); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 360 | void RewriteByRefVar(VarDecl *VD); |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 361 | std::string SynthesizeByrefCopyDestroyHelper(VarDecl *VD, int flag); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 362 | Stmt *RewriteBlockDeclRefExpr(Expr *VD); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 363 | void RewriteBlockPointerFunctionArgs(FunctionDecl *FD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 364 | |
| 365 | std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 366 | const char *funcName, std::string Tag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 367 | std::string SynthesizeBlockFunc(BlockExpr *CE, int i, |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 368 | const char *funcName, std::string Tag); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 369 | std::string SynthesizeBlockImpl(BlockExpr *CE, |
| 370 | std::string Tag, std::string Desc); |
| 371 | std::string SynthesizeBlockDescriptor(std::string DescTag, |
| 372 | std::string ImplTag, |
| 373 | int i, const char *funcName, |
| 374 | unsigned hasCopy); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 375 | Stmt *SynthesizeBlockCall(CallExpr *Exp, const Expr* BlockExp); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 376 | void SynthesizeBlockLiterals(SourceLocation FunLocStart, |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 377 | const char *FunName); |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 378 | void RewriteRecordBody(RecordDecl *RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 379 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 380 | void CollectBlockDeclRefInfo(BlockExpr *Exp); |
| 381 | void GetBlockCallExprs(Stmt *S); |
| 382 | void GetBlockDeclRefExprs(Stmt *S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 383 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 384 | // We avoid calling Type::isBlockPointerType(), since it operates on the |
| 385 | // 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] | 386 | bool isTopLevelBlockPointerType(QualType T) { |
| 387 | return isa<BlockPointerType>(T); |
| 388 | } |
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 | // FIXME: This predicate seems like it would be useful to add to ASTContext. |
| 391 | bool isObjCType(QualType T) { |
| 392 | if (!LangOpts.ObjC1 && !LangOpts.ObjC2) |
| 393 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 394 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 395 | QualType OCT = Context->getCanonicalType(T).getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 396 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 397 | if (OCT == Context->getCanonicalType(Context->getObjCIdType()) || |
| 398 | OCT == Context->getCanonicalType(Context->getObjCClassType())) |
| 399 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 400 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 401 | if (const PointerType *PT = OCT->getAs<PointerType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 402 | if (isa<ObjCInterfaceType>(PT->getPointeeType()) || |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 403 | PT->getPointeeType()->isObjCQualifiedIdType()) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 404 | return true; |
| 405 | } |
| 406 | return false; |
| 407 | } |
| 408 | bool PointerTypeTakesAnyBlockArguments(QualType QT); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 409 | void GetExtentOfArgList(const char *Name, const char *&LParen, |
| 410 | const char *&RParen); |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 411 | void RewriteCastExpr(CStyleCastExpr *CE); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 412 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 413 | FunctionDecl *SynthBlockInitFunctionDecl(const char *name); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 414 | Stmt *SynthBlockInitExpr(BlockExpr *Exp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 415 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 416 | void QuoteDoublequotes(std::string &From, std::string &To) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 417 | for (unsigned i = 0; i < From.length(); i++) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 418 | if (From[i] == '"') |
| 419 | To += "\\\""; |
| 420 | else |
| 421 | To += From[i]; |
| 422 | } |
| 423 | } |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 424 | }; |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 425 | |
| 426 | // Helper function: create a CStyleCastExpr with trivial type source info. |
| 427 | CStyleCastExpr* NoTypeInfoCStyleCastExpr(ASTContext *Ctx, QualType Ty, |
| 428 | CastExpr::CastKind Kind, Expr *E) { |
| 429 | TypeSourceInfo *TInfo = Ctx->getTrivialTypeSourceInfo(Ty, SourceLocation()); |
| 430 | return new (Ctx) CStyleCastExpr(Ty, Kind, E, TInfo, |
| 431 | SourceLocation(), SourceLocation()); |
| 432 | } |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 435 | void RewriteObjC::RewriteBlocksInFunctionProtoType(QualType funcType, |
| 436 | NamedDecl *D) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 437 | if (FunctionProtoType *fproto = dyn_cast<FunctionProtoType>(funcType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 438 | for (FunctionProtoType::arg_type_iterator I = fproto->arg_type_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 439 | E = fproto->arg_type_end(); I && (I != E); ++I) |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 440 | if (isTopLevelBlockPointerType(*I)) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 441 | // All the args are checked/rewritten. Don't call twice! |
| 442 | RewriteBlockPointerDecl(D); |
| 443 | break; |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | void RewriteObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 449 | const PointerType *PT = funcType->getAs<PointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 450 | if (PT && PointerTypeTakesAnyBlockArguments(funcType)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 451 | RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 454 | static bool IsHeaderFile(const std::string &Filename) { |
| 455 | std::string::size_type DotPos = Filename.rfind('.'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 456 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 457 | if (DotPos == std::string::npos) { |
| 458 | // no file extension |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 459 | return false; |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 460 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 461 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 462 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 463 | // C header: .h |
| 464 | // C++ header: .hh or .H; |
| 465 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 466 | } |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 467 | |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 468 | RewriteObjC::RewriteObjC(std::string inFile, llvm::raw_ostream* OS, |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 469 | Diagnostic &D, const LangOptions &LOpts, |
| 470 | bool silenceMacroWarn) |
| 471 | : Diags(D), LangOpts(LOpts), InFileName(inFile), OutFile(OS), |
| 472 | SilenceRewriteMacroWarning(silenceMacroWarn) { |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 473 | IsHeader = IsHeaderFile(inFile); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 474 | RewriteFailedDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 475 | "rewriting sub-expression within a macro (may not be correct)"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 476 | TryFinallyContainsReturnDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 477 | "rewriter doesn't support user-specified control flow semantics " |
| 478 | "for @try/@finally (code may not execute properly)"); |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Eli Friedman | bce831b | 2009-05-18 22:29:17 +0000 | [diff] [blame] | 481 | ASTConsumer *clang::CreateObjCRewriter(const std::string& InFile, |
| 482 | llvm::raw_ostream* OS, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 483 | Diagnostic &Diags, |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 484 | const LangOptions &LOpts, |
| 485 | bool SilenceRewriteMacroWarning) { |
| 486 | return new RewriteObjC(InFile, OS, Diags, LOpts, SilenceRewriteMacroWarning); |
Chris Lattner | e365c50 | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 487 | } |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 488 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 489 | void RewriteObjC::Initialize(ASTContext &context) { |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 490 | Context = &context; |
| 491 | SM = &Context->getSourceManager(); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 492 | TUDecl = Context->getTranslationUnitDecl(); |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 493 | MsgSendFunctionDecl = 0; |
| 494 | MsgSendSuperFunctionDecl = 0; |
| 495 | MsgSendStretFunctionDecl = 0; |
| 496 | MsgSendSuperStretFunctionDecl = 0; |
| 497 | MsgSendFpretFunctionDecl = 0; |
| 498 | GetClassFunctionDecl = 0; |
| 499 | GetMetaClassFunctionDecl = 0; |
| 500 | SelGetUidFunctionDecl = 0; |
| 501 | CFStringFunctionDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 502 | ConstantStringClassReference = 0; |
| 503 | NSStringRecord = 0; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 504 | CurMethodDef = 0; |
| 505 | CurFunctionDef = 0; |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 506 | CurFunctionDeclToDeclareForBlock = 0; |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 507 | GlobalVarDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 508 | SuperStructDecl = 0; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 509 | ProtocolTypeDecl = 0; |
Steve Naroff | 9630ec5 | 2008-03-27 22:59:54 +0000 | [diff] [blame] | 510 | ConstantStringDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 511 | BcLabelCount = 0; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 512 | SuperContructorFunctionDecl = 0; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 513 | NumObjCStringLiterals = 0; |
Steve Naroff | 68272b8 | 2008-12-08 20:01:41 +0000 | [diff] [blame] | 514 | PropParentMap = 0; |
| 515 | CurrentBody = 0; |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 516 | DisableReplaceStmt = false; |
Fariborz Jahanian | f292fcf | 2010-01-07 22:51:18 +0000 | [diff] [blame] | 517 | objc_impl_method = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 518 | |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 519 | // Get the ID and start/end of the main file. |
| 520 | MainFileID = SM->getMainFileID(); |
| 521 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 522 | MainFileStart = MainBuf->getBufferStart(); |
| 523 | MainFileEnd = MainBuf->getBufferEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 524 | |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 525 | Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOptions()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 526 | |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 527 | // declaring objc_selector outside the parameter list removes a silly |
| 528 | // scope related warning... |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 529 | if (IsHeader) |
Steve Naroff | 62c2632 | 2009-02-03 20:39:18 +0000 | [diff] [blame] | 530 | Preamble = "#pragma once\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 531 | Preamble += "struct objc_selector; struct objc_class;\n"; |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 532 | Preamble += "struct __rw_objc_super { struct objc_object *object; "; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 533 | Preamble += "struct objc_object *superClass; "; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 534 | if (LangOpts.Microsoft) { |
| 535 | // Add a constructor for creating temporary objects. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 536 | Preamble += "__rw_objc_super(struct objc_object *o, struct objc_object *s) " |
| 537 | ": "; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 538 | Preamble += "object(o), superClass(s) {} "; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 539 | } |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 540 | Preamble += "};\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 541 | Preamble += "#ifndef _REWRITER_typedef_Protocol\n"; |
| 542 | Preamble += "typedef struct objc_object Protocol;\n"; |
| 543 | Preamble += "#define _REWRITER_typedef_Protocol\n"; |
| 544 | Preamble += "#endif\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 545 | if (LangOpts.Microsoft) { |
| 546 | Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n"; |
| 547 | Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n"; |
| 548 | } else |
| 549 | Preamble += "#define __OBJC_RW_DLLIMPORT extern\n"; |
| 550 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 551 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 552 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 553 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 554 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend_stret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 555 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 556 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper_stret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 557 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 558 | Preamble += "__OBJC_RW_DLLIMPORT double objc_msgSend_fpret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 559 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 560 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getClass"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 561 | Preamble += "(const char *);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 562 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getMetaClass"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 563 | Preamble += "(const char *);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 564 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw(struct objc_object *);\n"; |
| 565 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_enter(void *);\n"; |
| 566 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_exit(void *);\n"; |
| 567 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_exception_extract(void *);\n"; |
| 568 | Preamble += "__OBJC_RW_DLLIMPORT int objc_exception_match"; |
Steve Naroff | 580ca78 | 2008-05-09 21:17:56 +0000 | [diff] [blame] | 569 | Preamble += "(struct objc_class *, struct objc_object *);\n"; |
Steve Naroff | 59f05a4 | 2008-07-16 18:58:11 +0000 | [diff] [blame] | 570 | // @synchronized hooks. |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 571 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_enter(struct objc_object *);\n"; |
| 572 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_exit(struct objc_object *);\n"; |
| 573 | Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 574 | Preamble += "#ifndef __FASTENUMERATIONSTATE\n"; |
| 575 | Preamble += "struct __objcFastEnumerationState {\n\t"; |
| 576 | Preamble += "unsigned long state;\n\t"; |
Steve Naroff | b10f273 | 2008-04-04 22:58:22 +0000 | [diff] [blame] | 577 | Preamble += "void **itemsPtr;\n\t"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 578 | Preamble += "unsigned long *mutationsPtr;\n\t"; |
| 579 | Preamble += "unsigned long extra[5];\n};\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 580 | Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 581 | Preamble += "#define __FASTENUMERATIONSTATE\n"; |
| 582 | Preamble += "#endif\n"; |
| 583 | Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n"; |
| 584 | Preamble += "struct __NSConstantStringImpl {\n"; |
| 585 | Preamble += " int *isa;\n"; |
| 586 | Preamble += " int flags;\n"; |
| 587 | Preamble += " char *str;\n"; |
| 588 | Preamble += " long length;\n"; |
| 589 | Preamble += "};\n"; |
Steve Naroff | 88bee74 | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 590 | Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n"; |
| 591 | Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n"; |
| 592 | Preamble += "#else\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 593 | Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n"; |
Steve Naroff | 88bee74 | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 594 | Preamble += "#endif\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 595 | Preamble += "#define __NSCONSTANTSTRINGIMPL\n"; |
| 596 | Preamble += "#endif\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 597 | // Blocks preamble. |
| 598 | Preamble += "#ifndef BLOCK_IMPL\n"; |
| 599 | Preamble += "#define BLOCK_IMPL\n"; |
| 600 | Preamble += "struct __block_impl {\n"; |
| 601 | Preamble += " void *isa;\n"; |
| 602 | Preamble += " int Flags;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 603 | Preamble += " int Reserved;\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 604 | Preamble += " void *FuncPtr;\n"; |
| 605 | Preamble += "};\n"; |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 606 | Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n"; |
Steve Naroff | c9c1e9c | 2009-12-06 01:52:22 +0000 | [diff] [blame] | 607 | Preamble += "#ifdef __OBJC_EXPORT_BLOCKS\n"; |
Steve Naroff | a851e60 | 2009-12-06 01:33:56 +0000 | [diff] [blame] | 608 | Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_assign(void *, const void *, const int);\n"; |
| 609 | Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_dispose(const void *, const int);\n"; |
| 610 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteGlobalBlock[32];\n"; |
| 611 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteStackBlock[32];\n"; |
| 612 | Preamble += "#else\n"; |
Steve Naroff | cd82637 | 2010-01-05 18:09:31 +0000 | [diff] [blame] | 613 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_assign(void *, const void *, const int);\n"; |
| 614 | 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] | 615 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteGlobalBlock[32];\n"; |
| 616 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteStackBlock[32];\n"; |
| 617 | Preamble += "#endif\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 618 | Preamble += "#endif\n"; |
Steve Naroff | a48396e | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 619 | if (LangOpts.Microsoft) { |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 620 | Preamble += "#undef __OBJC_RW_DLLIMPORT\n"; |
| 621 | Preamble += "#undef __OBJC_RW_STATICIMPORT\n"; |
Steve Naroff | a48396e | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 622 | Preamble += "#define __attribute__(X)\n"; |
Fariborz Jahanian | 3420419 | 2010-01-15 22:29:39 +0000 | [diff] [blame] | 623 | Preamble += "#define __weak\n"; |
Steve Naroff | a48396e | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 624 | } |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 625 | else { |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 626 | Preamble += "#define __block\n"; |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 627 | Preamble += "#define __weak\n"; |
| 628 | } |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 632 | //===----------------------------------------------------------------------===// |
| 633 | // Top Level Driver Code |
| 634 | //===----------------------------------------------------------------------===// |
| 635 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 636 | void RewriteObjC::HandleTopLevelSingleDecl(Decl *D) { |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 637 | // Two cases: either the decl could be in the main file, or it could be in a |
| 638 | // #included file. If the former, rewrite it now. If the later, check to see |
| 639 | // if we rewrote the #include/#import. |
| 640 | SourceLocation Loc = D->getLocation(); |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 641 | Loc = SM->getInstantiationLoc(Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 642 | |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 643 | // If this is for a builtin, ignore it. |
| 644 | if (Loc.isInvalid()) return; |
| 645 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 646 | // Look for built-in declarations that we need to refer during the rewrite. |
| 647 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 648 | RewriteFunctionDecl(FD); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 649 | } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) { |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 650 | // declared in <Foundation/NSString.h> |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 651 | if (strcmp(FVD->getNameAsCString(), "_NSConstantStringClassReference") == 0) { |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 652 | ConstantStringClassReference = FVD; |
| 653 | return; |
| 654 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 655 | } else if (ObjCInterfaceDecl *MD = dyn_cast<ObjCInterfaceDecl>(D)) { |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 656 | RewriteInterfaceDecl(MD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 657 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 658 | RewriteCategoryDecl(CD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 659 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 660 | RewriteProtocolDecl(PD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 661 | } else if (ObjCForwardProtocolDecl *FP = |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 662 | dyn_cast<ObjCForwardProtocolDecl>(D)){ |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 663 | RewriteForwardProtocolDecl(FP); |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 664 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
| 665 | // Recurse into linkage specifications |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 666 | for (DeclContext::decl_iterator DI = LSD->decls_begin(), |
| 667 | DIEnd = LSD->decls_end(); |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 668 | DI != DIEnd; ++DI) |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 669 | HandleTopLevelSingleDecl(*DI); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 670 | } |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 671 | // 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] | 672 | if (SM->isFromMainFile(Loc)) |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 673 | return HandleDeclInMainFile(D); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 676 | //===----------------------------------------------------------------------===// |
| 677 | // Syntactic (non-AST) Rewriting Code |
| 678 | //===----------------------------------------------------------------------===// |
| 679 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 680 | void RewriteObjC::RewriteInclude() { |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 681 | SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 682 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
| 683 | const char *MainBufStart = MainBuf.first; |
| 684 | const char *MainBufEnd = MainBuf.second; |
| 685 | size_t ImportLen = strlen("import"); |
| 686 | size_t IncludeLen = strlen("include"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 687 | |
Fariborz Jahanian | af57b46 | 2008-01-19 01:03:17 +0000 | [diff] [blame] | 688 | // Loop over the whole file, looking for includes. |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 689 | for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { |
| 690 | if (*BufPtr == '#') { |
| 691 | if (++BufPtr == MainBufEnd) |
| 692 | return; |
| 693 | while (*BufPtr == ' ' || *BufPtr == '\t') |
| 694 | if (++BufPtr == MainBufEnd) |
| 695 | return; |
| 696 | if (!strncmp(BufPtr, "import", ImportLen)) { |
| 697 | // replace import with include |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 698 | SourceLocation ImportLoc = |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 699 | LocStart.getFileLocWithOffset(BufPtr-MainBufStart); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 700 | ReplaceText(ImportLoc, ImportLen, "include", IncludeLen); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 701 | BufPtr += ImportLen; |
| 702 | } |
| 703 | } |
| 704 | } |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 707 | void RewriteObjC::RewriteTabs() { |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 708 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
| 709 | const char *MainBufStart = MainBuf.first; |
| 710 | const char *MainBufEnd = MainBuf.second; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 711 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 712 | // Loop over the whole file, looking for tabs. |
| 713 | for (const char *BufPtr = MainBufStart; BufPtr != MainBufEnd; ++BufPtr) { |
| 714 | if (*BufPtr != '\t') |
| 715 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 716 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 717 | // Okay, we found a tab. This tab will turn into at least one character, |
| 718 | // but it depends on which 'virtual column' it is in. Compute that now. |
| 719 | unsigned VCol = 0; |
| 720 | while (BufPtr-VCol != MainBufStart && BufPtr[-VCol-1] != '\t' && |
| 721 | BufPtr[-VCol-1] != '\n' && BufPtr[-VCol-1] != '\r') |
| 722 | ++VCol; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 723 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 724 | // Okay, now that we know the virtual column, we know how many spaces to |
| 725 | // insert. We assume 8-character tab-stops. |
| 726 | unsigned Spaces = 8-(VCol & 7); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 727 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 728 | // Get the location of the tab. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 729 | SourceLocation TabLoc = SM->getLocForStartOfFile(MainFileID); |
| 730 | TabLoc = TabLoc.getFileLocWithOffset(BufPtr-MainBufStart); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 731 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 732 | // Rewrite the single tab character into a sequence of spaces. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 733 | ReplaceText(TabLoc, 1, " ", Spaces); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 734 | } |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 737 | static std::string getIvarAccessString(ObjCInterfaceDecl *ClassDecl, |
| 738 | ObjCIvarDecl *OID) { |
| 739 | std::string S; |
| 740 | S = "((struct "; |
| 741 | S += ClassDecl->getIdentifier()->getName(); |
| 742 | S += "_IMPL *)self)->"; |
Daniel Dunbar | 5ffe14c | 2009-10-18 20:26:27 +0000 | [diff] [blame] | 743 | S += OID->getName(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 744 | return S; |
| 745 | } |
| 746 | |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 747 | void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 748 | ObjCImplementationDecl *IMD, |
| 749 | ObjCCategoryImplDecl *CID) { |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 750 | SourceLocation startLoc = PID->getLocStart(); |
| 751 | InsertText(startLoc, "// ", 3); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 752 | const char *startBuf = SM->getCharacterData(startLoc); |
| 753 | assert((*startBuf == '@') && "bogus @synthesize location"); |
| 754 | const char *semiBuf = strchr(startBuf, ';'); |
| 755 | assert((*semiBuf == ';') && "@synthesize: can't find ';'"); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 756 | SourceLocation onePastSemiLoc = |
| 757 | startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 758 | |
| 759 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 760 | return; // FIXME: is this correct? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 761 | |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 762 | // Generate the 'getter' function. |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 763 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 764 | ObjCInterfaceDecl *ClassDecl = PD->getGetterMethodDecl()->getClassInterface(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 765 | ObjCIvarDecl *OID = PID->getPropertyIvarDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 766 | |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 767 | if (!OID) |
| 768 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 769 | |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 770 | std::string Getr; |
| 771 | RewriteObjCMethodDecl(PD->getGetterMethodDecl(), Getr); |
| 772 | Getr += "{ "; |
| 773 | // Synthesize an explicit cast to gain access to the ivar. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 774 | // FIXME: deal with code generation implications for various property |
| 775 | // attributes (copy, retain, nonatomic). |
Steve Naroff | 3539cdb | 2008-12-02 17:54:50 +0000 | [diff] [blame] | 776 | // See objc-act.c:objc_synthesize_new_getter() for details. |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 777 | Getr += "return " + getIvarAccessString(ClassDecl, OID); |
| 778 | Getr += "; }"; |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 779 | InsertText(onePastSemiLoc, Getr.c_str(), Getr.size()); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 780 | if (PD->isReadOnly()) |
| 781 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 782 | |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 783 | // Generate the 'setter' function. |
| 784 | std::string Setr; |
| 785 | RewriteObjCMethodDecl(PD->getSetterMethodDecl(), Setr); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 786 | Setr += "{ "; |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 787 | // Synthesize an explicit cast to initialize the ivar. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 788 | // FIXME: deal with code generation implications for various property |
| 789 | // attributes (copy, retain, nonatomic). |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 790 | // See objc-act.c:objc_synthesize_new_setter() for details. |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 791 | Setr += getIvarAccessString(ClassDecl, OID) + " = "; |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 792 | Setr += PD->getNameAsCString(); |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 793 | Setr += "; }"; |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 794 | InsertText(onePastSemiLoc, Setr.c_str(), Setr.size()); |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 795 | } |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 796 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 797 | void RewriteObjC::RewriteForwardClassDecl(ObjCClassDecl *ClassDecl) { |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 798 | // Get the start location and compute the semi location. |
| 799 | SourceLocation startLoc = ClassDecl->getLocation(); |
| 800 | const char *startBuf = SM->getCharacterData(startLoc); |
| 801 | const char *semiPtr = strchr(startBuf, ';'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 802 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 803 | // Translate to typedef's that forward reference structs with the same name |
| 804 | // as the class. As a convenience, we include the original declaration |
| 805 | // as a comment. |
| 806 | std::string typedefString; |
Fariborz Jahanian | 91fbd12 | 2010-01-11 22:48:40 +0000 | [diff] [blame] | 807 | typedefString += "// @class "; |
| 808 | for (ObjCClassDecl::iterator I = ClassDecl->begin(), E = ClassDecl->end(); |
| 809 | I != E; ++I) { |
| 810 | ObjCInterfaceDecl *ForwardDecl = I->getInterface(); |
| 811 | typedefString += ForwardDecl->getNameAsString(); |
| 812 | if (I+1 != E) |
| 813 | typedefString += ", "; |
| 814 | else |
| 815 | typedefString += ";\n"; |
| 816 | } |
| 817 | |
Chris Lattner | 6795605 | 2009-02-20 18:04:31 +0000 | [diff] [blame] | 818 | for (ObjCClassDecl::iterator I = ClassDecl->begin(), E = ClassDecl->end(); |
| 819 | I != E; ++I) { |
Ted Kremenek | 321c22f | 2009-11-18 00:28:11 +0000 | [diff] [blame] | 820 | ObjCInterfaceDecl *ForwardDecl = I->getInterface(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 821 | typedefString += "#ifndef _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 822 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 823 | typedefString += "\n"; |
| 824 | typedefString += "#define _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 825 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 826 | typedefString += "\n"; |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 827 | typedefString += "typedef struct objc_object "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 828 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 829 | typedefString += ";\n#endif\n"; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 830 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 831 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 832 | // Replace the @class with typedefs corresponding to the classes. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 833 | ReplaceText(startLoc, semiPtr-startBuf+1, |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 834 | typedefString.c_str(), typedefString.size()); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 837 | void RewriteObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) { |
Fariborz Jahanian | d050240 | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 838 | // When method is a synthesized one, such as a getter/setter there is |
| 839 | // nothing to rewrite. |
| 840 | if (Method->isSynthesized()) |
| 841 | return; |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 842 | SourceLocation LocStart = Method->getLocStart(); |
| 843 | SourceLocation LocEnd = Method->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 844 | |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 845 | if (SM->getInstantiationLineNumber(LocEnd) > |
| 846 | SM->getInstantiationLineNumber(LocStart)) { |
Steve Naroff | 94ac21e | 2008-10-21 13:37:27 +0000 | [diff] [blame] | 847 | InsertText(LocStart, "#if 0\n", 6); |
| 848 | ReplaceText(LocEnd, 1, ";\n#endif\n", 9); |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 849 | } else { |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 850 | InsertText(LocStart, "// ", 3); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 851 | } |
| 852 | } |
| 853 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 854 | void RewriteObjC::RewriteProperty(ObjCPropertyDecl *prop) { |
Fariborz Jahanian | d050240 | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 855 | SourceLocation Loc = prop->getAtLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 856 | |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 857 | ReplaceText(Loc, 0, "// ", 3); |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 858 | // FIXME: handle properties that are declared across multiple lines. |
Fariborz Jahanian | 957cf65 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 861 | void RewriteObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 862 | SourceLocation LocStart = CatDecl->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 863 | |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 864 | // FIXME: handle category headers that are declared across multiple lines. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 865 | ReplaceText(LocStart, 0, "// ", 3); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 866 | |
| 867 | for (ObjCCategoryDecl::instmeth_iterator |
| 868 | I = CatDecl->instmeth_begin(), E = CatDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 869 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 870 | RewriteMethodDeclaration(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 871 | for (ObjCCategoryDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 872 | I = CatDecl->classmeth_begin(), E = CatDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 873 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 874 | RewriteMethodDeclaration(*I); |
| 875 | |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 876 | // Lastly, comment out the @end. |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 877 | ReplaceText(CatDecl->getAtEndRange().getBegin(), 0, "// ", 3); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 880 | void RewriteObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 881 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 882 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 883 | SourceLocation LocStart = PDecl->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 884 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 885 | // FIXME: handle protocol headers that are declared across multiple lines. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 886 | ReplaceText(LocStart, 0, "// ", 3); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 887 | |
| 888 | for (ObjCProtocolDecl::instmeth_iterator |
| 889 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 890 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 891 | RewriteMethodDeclaration(*I); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 892 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 893 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 894 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 895 | RewriteMethodDeclaration(*I); |
| 896 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 897 | // Lastly, comment out the @end. |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 898 | SourceLocation LocEnd = PDecl->getAtEndRange().getBegin(); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 899 | ReplaceText(LocEnd, 0, "// ", 3); |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 900 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 901 | // Must comment out @optional/@required |
| 902 | const char *startBuf = SM->getCharacterData(LocStart); |
| 903 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 904 | for (const char *p = startBuf; p < endBuf; p++) { |
| 905 | if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { |
| 906 | std::string CommentedOptional = "/* @optional */"; |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 907 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 908 | ReplaceText(OptionalLoc, strlen("@optional"), |
| 909 | CommentedOptional.c_str(), CommentedOptional.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 910 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 911 | } |
| 912 | else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { |
| 913 | std::string CommentedRequired = "/* @required */"; |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 914 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 915 | ReplaceText(OptionalLoc, strlen("@required"), |
| 916 | CommentedRequired.c_str(), CommentedRequired.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 917 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 918 | } |
| 919 | } |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 922 | void RewriteObjC::RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *PDecl) { |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 923 | SourceLocation LocStart = PDecl->getLocation(); |
Steve Naroff | b7fa992 | 2007-11-14 03:37:28 +0000 | [diff] [blame] | 924 | if (LocStart.isInvalid()) |
| 925 | assert(false && "Invalid SourceLocation"); |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 926 | // FIXME: handle forward protocol that are declared across multiple lines. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 927 | ReplaceText(LocStart, 0, "// ", 3); |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 928 | } |
| 929 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 930 | void RewriteObjC::RewriteObjCMethodDecl(ObjCMethodDecl *OMD, |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 931 | std::string &ResultStr) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 932 | //fprintf(stderr,"In RewriteObjCMethodDecl\n"); |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 933 | const FunctionType *FPRetType = 0; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 934 | ResultStr += "\nstatic "; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 935 | if (OMD->getResultType()->isObjCQualifiedIdType()) |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 936 | ResultStr += "id"; |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 937 | else if (OMD->getResultType()->isFunctionPointerType() || |
| 938 | OMD->getResultType()->isBlockPointerType()) { |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 939 | // needs special handling, since pointer-to-functions have special |
| 940 | // syntax (where a decaration models use). |
| 941 | QualType retType = OMD->getResultType(); |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 942 | QualType PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 943 | if (const PointerType* PT = retType->getAs<PointerType>()) |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 944 | PointeeTy = PT->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 945 | else if (const BlockPointerType *BPT = retType->getAs<BlockPointerType>()) |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 946 | PointeeTy = BPT->getPointeeType(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 947 | if ((FPRetType = PointeeTy->getAs<FunctionType>())) { |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 948 | ResultStr += FPRetType->getResultType().getAsString(); |
| 949 | ResultStr += "(*"; |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 950 | } |
| 951 | } else |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 952 | ResultStr += OMD->getResultType().getAsString(); |
Fariborz Jahanian | 531a1ea | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 953 | ResultStr += " "; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 954 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 955 | // Unique method name |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 956 | std::string NameStr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 957 | |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 958 | if (OMD->isInstanceMethod()) |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 959 | NameStr += "_I_"; |
| 960 | else |
| 961 | NameStr += "_C_"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 962 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 963 | NameStr += OMD->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 964 | NameStr += "_"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 965 | |
| 966 | if (ObjCCategoryImplDecl *CID = |
Steve Naroff | 3e0a540 | 2009-01-08 19:41:02 +0000 | [diff] [blame] | 967 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 968 | NameStr += CID->getNameAsString(); |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 969 | NameStr += "_"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 970 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 971 | // Append selector names, replacing ':' with '_' |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 972 | { |
| 973 | std::string selString = OMD->getSelector().getAsString(); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 974 | int len = selString.size(); |
| 975 | for (int i = 0; i < len; i++) |
| 976 | if (selString[i] == ':') |
| 977 | selString[i] = '_'; |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 978 | NameStr += selString; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 979 | } |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 980 | // Remember this name for metadata emission |
| 981 | MethodInternalNames[OMD] = NameStr; |
| 982 | ResultStr += NameStr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 983 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 984 | // Rewrite arguments |
| 985 | ResultStr += "("; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 986 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 987 | // invisible arguments |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 988 | if (OMD->isInstanceMethod()) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 989 | QualType selfTy = Context->getObjCInterfaceType(OMD->getClassInterface()); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 990 | selfTy = Context->getPointerType(selfTy); |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 991 | if (!LangOpts.Microsoft) { |
| 992 | if (ObjCSynthesizedStructs.count(OMD->getClassInterface())) |
| 993 | ResultStr += "struct "; |
| 994 | } |
| 995 | // When rewriting for Microsoft, explicitly omit the structure name. |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 996 | ResultStr += OMD->getClassInterface()->getNameAsString(); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 997 | ResultStr += " *"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 998 | } |
| 999 | else |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 1000 | ResultStr += Context->getObjCClassType().getAsString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1001 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1002 | ResultStr += " self, "; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1003 | ResultStr += Context->getObjCSelType().getAsString(); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1004 | ResultStr += " _cmd"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1005 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1006 | // Method arguments. |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1007 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 1008 | E = OMD->param_end(); PI != E; ++PI) { |
| 1009 | ParmVarDecl *PDecl = *PI; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1010 | ResultStr += ", "; |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1011 | if (PDecl->getType()->isObjCQualifiedIdType()) { |
| 1012 | ResultStr += "id "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1013 | ResultStr += PDecl->getNameAsString(); |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1014 | } else { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1015 | std::string Name = PDecl->getNameAsString(); |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 1016 | if (isTopLevelBlockPointerType(PDecl->getType())) { |
Steve Naroff | c8ad87b | 2008-10-30 14:45:29 +0000 | [diff] [blame] | 1017 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1018 | const BlockPointerType *BPT = PDecl->getType()->getAs<BlockPointerType>(); |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1019 | Context->getPointerType(BPT->getPointeeType()).getAsStringInternal(Name, |
| 1020 | Context->PrintingPolicy); |
Steve Naroff | c8ad87b | 2008-10-30 14:45:29 +0000 | [diff] [blame] | 1021 | } else |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1022 | PDecl->getType().getAsStringInternal(Name, Context->PrintingPolicy); |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1023 | ResultStr += Name; |
| 1024 | } |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1025 | } |
Fariborz Jahanian | 7c39ff7 | 2008-01-21 20:14:23 +0000 | [diff] [blame] | 1026 | if (OMD->isVariadic()) |
| 1027 | ResultStr += ", ..."; |
Fariborz Jahanian | 531a1ea | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 1028 | ResultStr += ") "; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1029 | |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1030 | if (FPRetType) { |
| 1031 | ResultStr += ")"; // close the precedence "scope" for "*". |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1032 | |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1033 | // Now, emit the argument types (if any). |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1034 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) { |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1035 | ResultStr += "("; |
| 1036 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 1037 | if (i) ResultStr += ", "; |
| 1038 | std::string ParamStr = FT->getArgType(i).getAsString(); |
| 1039 | ResultStr += ParamStr; |
| 1040 | } |
| 1041 | if (FT->isVariadic()) { |
| 1042 | if (FT->getNumArgs()) ResultStr += ", "; |
| 1043 | ResultStr += "..."; |
| 1044 | } |
| 1045 | ResultStr += ")"; |
| 1046 | } else { |
| 1047 | ResultStr += "()"; |
| 1048 | } |
| 1049 | } |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1050 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1051 | void RewriteObjC::RewriteImplementationDecl(Decl *OID) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1052 | ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID); |
| 1053 | ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1054 | |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 1055 | if (IMD) |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1056 | InsertText(IMD->getLocStart(), "// ", 3); |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 1057 | else |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1058 | InsertText(CID->getLocStart(), "// ", 3); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1059 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1060 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1061 | I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(), |
| 1062 | E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1063 | I != E; ++I) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1064 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1065 | ObjCMethodDecl *OMD = *I; |
| 1066 | RewriteObjCMethodDecl(OMD, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1067 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1068 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1069 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1070 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1071 | const char *endBuf = SM->getCharacterData(LocEnd); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1072 | ReplaceText(LocStart, endBuf-startBuf, |
| 1073 | ResultStr.c_str(), ResultStr.size()); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1074 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1075 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1076 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1077 | I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(), |
| 1078 | E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1079 | I != E; ++I) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1080 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1081 | ObjCMethodDecl *OMD = *I; |
| 1082 | RewriteObjCMethodDecl(OMD, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1083 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1084 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1085 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1086 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1087 | const char *endBuf = SM->getCharacterData(LocEnd); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1088 | ReplaceText(LocStart, endBuf-startBuf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1089 | ResultStr.c_str(), ResultStr.size()); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1090 | } |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1091 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1092 | I = IMD ? IMD->propimpl_begin() : CID->propimpl_begin(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1093 | E = IMD ? IMD->propimpl_end() : CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1094 | I != E; ++I) { |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 1095 | RewritePropertyImplDecl(*I, IMD, CID); |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 1098 | if (IMD) |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1099 | InsertText(IMD->getLocEnd(), "// ", 3); |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 1100 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1101 | InsertText(CID->getLocEnd(), "// ", 3); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1104 | void RewriteObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { |
Steve Naroff | f908a87 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 1105 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1106 | if (!ObjCForwardDecls.count(ClassDecl)) { |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1107 | // we haven't seen a forward decl - generate a typedef. |
Steve Naroff | 5086a8d | 2007-11-14 23:02:56 +0000 | [diff] [blame] | 1108 | ResultStr = "#ifndef _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1109 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1110 | ResultStr += "\n"; |
| 1111 | ResultStr += "#define _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1112 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1113 | ResultStr += "\n"; |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1114 | ResultStr += "typedef struct objc_object "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1115 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1116 | ResultStr += ";\n#endif\n"; |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1117 | // Mark this typedef as having been generated. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1118 | ObjCForwardDecls.insert(ClassDecl); |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1119 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1120 | SynthesizeObjCInternalStruct(ClassDecl, ResultStr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1121 | |
| 1122 | for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1123 | E = ClassDecl->prop_end(); I != E; ++I) |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 1124 | RewriteProperty(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1125 | for (ObjCInterfaceDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1126 | I = ClassDecl->instmeth_begin(), E = ClassDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1127 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1128 | RewriteMethodDeclaration(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1129 | for (ObjCInterfaceDecl::classmeth_iterator |
| 1130 | I = ClassDecl->classmeth_begin(), E = ClassDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1131 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1132 | RewriteMethodDeclaration(*I); |
| 1133 | |
Steve Naroff | 2feac5e | 2007-10-30 03:43:13 +0000 | [diff] [blame] | 1134 | // Lastly, comment out the @end. |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1135 | ReplaceText(ClassDecl->getAtEndRange().getBegin(), 0, "// ", 3); |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 1138 | Stmt *RewriteObjC::RewritePropertySetter(BinaryOperator *BinOp, Expr *newStmt, |
| 1139 | SourceRange SrcRange) { |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1140 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr. |
| 1141 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
| 1142 | ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS()); |
| 1143 | ObjCMessageExpr *MsgExpr; |
| 1144 | ObjCPropertyDecl *PDecl = PropRefExpr->getProperty(); |
| 1145 | llvm::SmallVector<Expr *, 1> ExprVec; |
| 1146 | ExprVec.push_back(newStmt); |
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(); |
| 1149 | ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(Receiver); |
| 1150 | if (PRE && PropGetters[PRE]) { |
| 1151 | // This allows us to handle chain/nested property getters. |
| 1152 | Receiver = PropGetters[PRE]; |
| 1153 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1154 | MsgExpr = new (Context) ObjCMessageExpr(dyn_cast<Expr>(Receiver), |
| 1155 | PDecl->getSetterName(), PDecl->getType(), |
| 1156 | PDecl->getSetterMethodDecl(), |
| 1157 | SourceLocation(), SourceLocation(), |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1158 | &ExprVec[0], 1); |
| 1159 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1160 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1161 | // Now do the actual rewrite. |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 1162 | ReplaceStmtWithRange(BinOp, ReplacingStmt, SrcRange); |
Steve Naroff | e58ee0c | 2008-12-10 14:53:27 +0000 | [diff] [blame] | 1163 | //delete BinOp; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1164 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1165 | // to things that stay around. |
| 1166 | Context->Deallocate(MsgExpr); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1167 | return ReplacingStmt; |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1170 | Stmt *RewriteObjC::RewritePropertyGetter(ObjCPropertyRefExpr *PropRefExpr) { |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1171 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr. |
| 1172 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
| 1173 | ObjCMessageExpr *MsgExpr; |
| 1174 | ObjCPropertyDecl *PDecl = PropRefExpr->getProperty(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1175 | |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1176 | Stmt *Receiver = PropRefExpr->getBase(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1177 | |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1178 | ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(Receiver); |
| 1179 | if (PRE && PropGetters[PRE]) { |
| 1180 | // This allows us to handle chain/nested property getters. |
| 1181 | Receiver = PropGetters[PRE]; |
| 1182 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1183 | MsgExpr = new (Context) ObjCMessageExpr(dyn_cast<Expr>(Receiver), |
| 1184 | PDecl->getGetterName(), PDecl->getType(), |
| 1185 | PDecl->getGetterMethodDecl(), |
| 1186 | SourceLocation(), SourceLocation(), |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1187 | 0, 0); |
| 1188 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1189 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1190 | |
| 1191 | if (!PropParentMap) |
| 1192 | PropParentMap = new ParentMap(CurrentBody); |
| 1193 | |
| 1194 | Stmt *Parent = PropParentMap->getParent(PropRefExpr); |
| 1195 | if (Parent && isa<ObjCPropertyRefExpr>(Parent)) { |
| 1196 | // We stash away the ReplacingStmt since actually doing the |
| 1197 | // replacement/rewrite won't work for nested getters (e.g. obj.p.i) |
| 1198 | PropGetters[PropRefExpr] = ReplacingStmt; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1199 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1200 | // to things that stay around. |
| 1201 | Context->Deallocate(MsgExpr); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1202 | return PropRefExpr; // return the original... |
| 1203 | } else { |
| 1204 | ReplaceStmt(PropRefExpr, ReplacingStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1205 | // delete PropRefExpr; elsewhere... |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1206 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1207 | // to things that stay around. |
| 1208 | Context->Deallocate(MsgExpr); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1209 | return ReplacingStmt; |
| 1210 | } |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1213 | Stmt *RewriteObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1214 | SourceLocation OrigStart, |
| 1215 | bool &replaced) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1216 | ObjCIvarDecl *D = IV->getDecl(); |
Fariborz Jahanian | 84ed600 | 2010-01-07 18:18:32 +0000 | [diff] [blame] | 1217 | const Expr *BaseExpr = IV->getBase(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1218 | if (CurMethodDef) { |
Fariborz Jahanian | 8f09543 | 2010-01-26 18:28:51 +0000 | [diff] [blame] | 1219 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1220 | ObjCInterfaceType *iFaceDecl = |
Fariborz Jahanian | 84ed600 | 2010-01-07 18:18:32 +0000 | [diff] [blame] | 1221 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
Fariborz Jahanian | ffbdead | 2010-01-26 20:37:44 +0000 | [diff] [blame] | 1222 | assert(iFaceDecl && "RewriteObjCIvarRefExpr - iFaceDecl is null"); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1223 | // lookup which class implements the instance variable. |
| 1224 | ObjCInterfaceDecl *clsDeclared = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1225 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1226 | clsDeclared); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1227 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1228 | |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1229 | // Synthesize an explicit cast to gain access to the ivar. |
| 1230 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1231 | RecName += "_IMPL"; |
| 1232 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str()); |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1233 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1234 | SourceLocation(), II); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1235 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1236 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1237 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, |
| 1238 | CastExpr::CK_Unknown, |
| 1239 | IV->getBase()); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1240 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1241 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
| 1242 | IV->getBase()->getLocEnd(), |
| 1243 | castExpr); |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1244 | replaced = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1245 | if (IV->isFreeIvar() && |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1246 | CurMethodDef->getClassInterface() == iFaceDecl->getDecl()) { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1247 | MemberExpr *ME = new (Context) MemberExpr(PE, true, D, |
| 1248 | IV->getLocation(), |
| 1249 | D->getType()); |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1250 | // delete IV; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1251 | return ME; |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1252 | } |
Fariborz Jahanian | 7e20ffe | 2010-01-28 01:41:20 +0000 | [diff] [blame] | 1253 | // Get the new text |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1254 | // Cannot delete IV->getBase(), since PE points to it. |
| 1255 | // Replace the old base with the cast. This is important when doing |
| 1256 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1257 | IV->setBase(PE); |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1258 | return IV; |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1259 | } |
Steve Naroff | 84472a8 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1260 | } else { // we are outside a method. |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1261 | assert(!IV->isFreeIvar() && "Cannot have a free standing ivar outside a method"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1262 | |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1263 | // Explicit ivar refs need to have a cast inserted. |
| 1264 | // FIXME: consider sharing some of this code with the code above. |
Fariborz Jahanian | 26337b2 | 2010-01-12 17:31:23 +0000 | [diff] [blame] | 1265 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
Fariborz Jahanian | c374cd9 | 2010-01-11 17:50:35 +0000 | [diff] [blame] | 1266 | ObjCInterfaceType *iFaceDecl = |
| 1267 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1268 | // lookup which class implements the instance variable. |
| 1269 | ObjCInterfaceDecl *clsDeclared = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1270 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1271 | clsDeclared); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1272 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1273 | |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1274 | // Synthesize an explicit cast to gain access to the ivar. |
| 1275 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1276 | RecName += "_IMPL"; |
| 1277 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str()); |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1278 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1279 | SourceLocation(), II); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1280 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1281 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1282 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, |
| 1283 | CastExpr::CK_Unknown, |
| 1284 | IV->getBase()); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1285 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1286 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
Chris Lattner | 8d36616 | 2008-05-28 16:38:23 +0000 | [diff] [blame] | 1287 | IV->getBase()->getLocEnd(), castExpr); |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1288 | replaced = true; |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1289 | // Cannot delete IV->getBase(), since PE points to it. |
| 1290 | // Replace the old base with the cast. This is important when doing |
| 1291 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1292 | IV->setBase(PE); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1293 | return IV; |
| 1294 | } |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1295 | } |
Steve Naroff | 84472a8 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1296 | return IV; |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1299 | Stmt *RewriteObjC::RewriteObjCNestedIvarRefExpr(Stmt *S, bool &replaced) { |
| 1300 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1301 | CI != E; ++CI) { |
| 1302 | if (*CI) { |
| 1303 | Stmt *newStmt = RewriteObjCNestedIvarRefExpr(*CI, replaced); |
| 1304 | if (newStmt) |
| 1305 | *CI = newStmt; |
| 1306 | } |
| 1307 | } |
| 1308 | if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) { |
| 1309 | SourceRange OrigStmtRange = S->getSourceRange(); |
| 1310 | Stmt *newStmt = RewriteObjCIvarRefExpr(IvarRefExpr, OrigStmtRange.getBegin(), |
| 1311 | replaced); |
| 1312 | return newStmt; |
| 1313 | } |
| 1314 | return S; |
| 1315 | } |
| 1316 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1317 | /// SynthCountByEnumWithState - To print: |
| 1318 | /// ((unsigned int (*) |
| 1319 | /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1320 | /// (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1321 | /// sel_registerName( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1322 | /// "countByEnumeratingWithState:objects:count:"), |
| 1323 | /// &enumState, |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1324 | /// (id *)items, (unsigned int)16) |
| 1325 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1326 | void RewriteObjC::SynthCountByEnumWithState(std::string &buf) { |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1327 | buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, " |
| 1328 | "id *, unsigned int))(void *)objc_msgSend)"; |
| 1329 | buf += "\n\t\t"; |
| 1330 | buf += "((id)l_collection,\n\t\t"; |
| 1331 | buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; |
| 1332 | buf += "\n\t\t"; |
| 1333 | buf += "&enumState, " |
| 1334 | "(id *)items, (unsigned int)16)"; |
| 1335 | } |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1336 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1337 | /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach |
| 1338 | /// statement to exit to its outer synthesized loop. |
| 1339 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1340 | Stmt *RewriteObjC::RewriteBreakStmt(BreakStmt *S) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1341 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1342 | return S; |
| 1343 | // replace break with goto __break_label |
| 1344 | std::string buf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1345 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1346 | SourceLocation startLoc = S->getLocStart(); |
| 1347 | buf = "goto __break_label_"; |
| 1348 | buf += utostr(ObjCBcLabelNo.back()); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1349 | ReplaceText(startLoc, strlen("break"), buf.c_str(), buf.size()); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1350 | |
| 1351 | return 0; |
| 1352 | } |
| 1353 | |
| 1354 | /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach |
| 1355 | /// statement to continue with its inner synthesized loop. |
| 1356 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1357 | Stmt *RewriteObjC::RewriteContinueStmt(ContinueStmt *S) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1358 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1359 | return S; |
| 1360 | // replace continue with goto __continue_label |
| 1361 | std::string buf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1362 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1363 | SourceLocation startLoc = S->getLocStart(); |
| 1364 | buf = "goto __continue_label_"; |
| 1365 | buf += utostr(ObjCBcLabelNo.back()); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1366 | ReplaceText(startLoc, strlen("continue"), buf.c_str(), buf.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1367 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1368 | return 0; |
| 1369 | } |
| 1370 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1371 | /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1372 | /// It rewrites: |
| 1373 | /// for ( type elem in collection) { stmts; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1374 | |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1375 | /// Into: |
| 1376 | /// { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1377 | /// type elem; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1378 | /// struct __objcFastEnumerationState enumState = { 0 }; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1379 | /// id items[16]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1380 | /// id l_collection = (id)collection; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1381 | /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1382 | /// objects:items count:16]; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1383 | /// if (limit) { |
| 1384 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1385 | /// do { |
| 1386 | /// unsigned long counter = 0; |
| 1387 | /// do { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1388 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1389 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1390 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1391 | /// stmts; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1392 | /// __continue_label: ; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1393 | /// } while (counter < limit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1394 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1395 | /// objects:items count:16]); |
| 1396 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1397 | /// __break_label: ; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1398 | /// } |
| 1399 | /// else |
| 1400 | /// elem = nil; |
| 1401 | /// } |
| 1402 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1403 | Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 1404 | SourceLocation OrigEnd) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1405 | assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1406 | assert(isa<ObjCForCollectionStmt>(Stmts.back()) && |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1407 | "ObjCForCollectionStmt Statement stack mismatch"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1408 | assert(!ObjCBcLabelNo.empty() && |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1409 | "ObjCForCollectionStmt - Label No stack empty"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1410 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1411 | SourceLocation startLoc = S->getLocStart(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1412 | const char *startBuf = SM->getCharacterData(startLoc); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1413 | const char *elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1414 | std::string elementTypeAsString; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1415 | std::string buf; |
| 1416 | buf = "\n{\n\t"; |
| 1417 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { |
| 1418 | // type elem; |
Chris Lattner | 7e24e82 | 2009-03-28 06:33:19 +0000 | [diff] [blame] | 1419 | NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl()); |
Ted Kremenek | 1ed8e2a | 2008-10-06 22:16:13 +0000 | [diff] [blame] | 1420 | QualType ElementType = cast<ValueDecl>(D)->getType(); |
Steve Naroff | e89b8e7 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1421 | if (ElementType->isObjCQualifiedIdType() || |
| 1422 | ElementType->isObjCQualifiedInterfaceType()) |
| 1423 | // Simply use 'id' for all qualified types. |
| 1424 | elementTypeAsString = "id"; |
| 1425 | else |
| 1426 | elementTypeAsString = ElementType.getAsString(); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1427 | buf += elementTypeAsString; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1428 | buf += " "; |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1429 | elementName = D->getNameAsCString(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1430 | buf += elementName; |
| 1431 | buf += ";\n\t"; |
| 1432 | } |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1433 | else { |
| 1434 | DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1435 | elementName = DR->getDecl()->getNameAsCString(); |
Steve Naroff | e89b8e7 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1436 | ValueDecl *VD = cast<ValueDecl>(DR->getDecl()); |
| 1437 | if (VD->getType()->isObjCQualifiedIdType() || |
| 1438 | VD->getType()->isObjCQualifiedInterfaceType()) |
| 1439 | // Simply use 'id' for all qualified types. |
| 1440 | elementTypeAsString = "id"; |
| 1441 | else |
| 1442 | elementTypeAsString = VD->getType().getAsString(); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1443 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1444 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1445 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1446 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
| 1447 | // id items[16]; |
| 1448 | buf += "id items[16];\n\t"; |
| 1449 | // id l_collection = (id) |
| 1450 | buf += "id l_collection = (id)"; |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1451 | // Find start location of 'collection' the hard way! |
| 1452 | const char *startCollectionBuf = startBuf; |
| 1453 | startCollectionBuf += 3; // skip 'for' |
| 1454 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1455 | startCollectionBuf++; // skip '(' |
| 1456 | // find 'in' and skip it. |
| 1457 | while (*startCollectionBuf != ' ' || |
| 1458 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1459 | (*(startCollectionBuf+3) != ' ' && |
| 1460 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1461 | startCollectionBuf++; |
| 1462 | startCollectionBuf += 3; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1463 | |
| 1464 | // Replace: "for (type element in" with string constructed thus far. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1465 | ReplaceText(startLoc, startCollectionBuf - startBuf, |
| 1466 | buf.c_str(), buf.size()); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1467 | // Replace ')' in for '(' type elem in collection ')' with ';' |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1468 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1469 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
| 1470 | SourceLocation lparenLoc = startLoc.getFileLocWithOffset(rparenBuf-startBuf); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1471 | buf = ";\n\t"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1472 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1473 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1474 | // objects:items count:16]; |
| 1475 | // which is synthesized into: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1476 | // unsigned int limit = |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1477 | // ((unsigned int (*) |
| 1478 | // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1479 | // (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1480 | // sel_registerName( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1481 | // "countByEnumeratingWithState:objects:count:"), |
| 1482 | // (struct __objcFastEnumerationState *)&state, |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1483 | // (id *)items, (unsigned int)16); |
| 1484 | buf += "unsigned long limit =\n\t\t"; |
| 1485 | SynthCountByEnumWithState(buf); |
| 1486 | buf += ";\n\t"; |
| 1487 | /// if (limit) { |
| 1488 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1489 | /// do { |
| 1490 | /// unsigned long counter = 0; |
| 1491 | /// do { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1492 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1493 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1494 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1495 | buf += "if (limit) {\n\t"; |
| 1496 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1497 | buf += "do {\n\t\t"; |
| 1498 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1499 | buf += "do {\n\t\t\t"; |
| 1500 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1501 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1502 | buf += elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1503 | buf += " = ("; |
| 1504 | buf += elementTypeAsString; |
| 1505 | buf += ")enumState.itemsPtr[counter++];"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1506 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1507 | ReplaceText(lparenLoc, 1, buf.c_str(), buf.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1508 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1509 | /// __continue_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1510 | /// } while (counter < limit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1511 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1512 | /// objects:items count:16]); |
| 1513 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1514 | /// __break_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1515 | /// } |
| 1516 | /// else |
| 1517 | /// elem = nil; |
| 1518 | /// } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1519 | /// |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1520 | buf = ";\n\t"; |
| 1521 | buf += "__continue_label_"; |
| 1522 | buf += utostr(ObjCBcLabelNo.back()); |
| 1523 | buf += ": ;"; |
| 1524 | buf += "\n\t\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1525 | buf += "} while (counter < limit);\n\t"; |
| 1526 | buf += "} while (limit = "; |
| 1527 | SynthCountByEnumWithState(buf); |
| 1528 | buf += ");\n\t"; |
| 1529 | buf += elementName; |
Fariborz Jahanian | 65b0aa5 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1530 | buf += " = (("; |
| 1531 | buf += elementTypeAsString; |
| 1532 | buf += ")0);\n\t"; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1533 | buf += "__break_label_"; |
| 1534 | buf += utostr(ObjCBcLabelNo.back()); |
| 1535 | buf += ": ;\n\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1536 | buf += "}\n\t"; |
| 1537 | buf += "else\n\t\t"; |
| 1538 | buf += elementName; |
Fariborz Jahanian | 65b0aa5 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1539 | buf += " = (("; |
| 1540 | buf += elementTypeAsString; |
| 1541 | buf += ")0);\n\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1542 | buf += "}\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1543 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1544 | // Insert all these *after* the statement body. |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1545 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1546 | if (isa<CompoundStmt>(S->getBody())) { |
| 1547 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(1); |
| 1548 | InsertText(endBodyLoc, buf.c_str(), buf.size()); |
| 1549 | } else { |
| 1550 | /* Need to treat single statements specially. For example: |
| 1551 | * |
| 1552 | * for (A *a in b) if (stuff()) break; |
| 1553 | * for (A *a in b) xxxyy; |
| 1554 | * |
| 1555 | * The following code simply scans ahead to the semi to find the actual end. |
| 1556 | */ |
| 1557 | const char *stmtBuf = SM->getCharacterData(OrigEnd); |
| 1558 | const char *semiBuf = strchr(stmtBuf, ';'); |
| 1559 | assert(semiBuf && "Can't find ';'"); |
| 1560 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(semiBuf-stmtBuf+1); |
| 1561 | InsertText(endBodyLoc, buf.c_str(), buf.size()); |
| 1562 | } |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1563 | Stmts.pop_back(); |
| 1564 | ObjCBcLabelNo.pop_back(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1565 | return 0; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1568 | /// RewriteObjCSynchronizedStmt - |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1569 | /// This routine rewrites @synchronized(expr) stmt; |
| 1570 | /// into: |
| 1571 | /// objc_sync_enter(expr); |
| 1572 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1573 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1574 | Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1575 | // Get the start location and compute the semi location. |
| 1576 | SourceLocation startLoc = S->getLocStart(); |
| 1577 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1578 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1579 | assert((*startBuf == '@') && "bogus @synchronized location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1580 | |
| 1581 | std::string buf; |
Steve Naroff | 3498cc9 | 2008-08-21 13:03:03 +0000 | [diff] [blame] | 1582 | buf = "objc_sync_enter((id)"; |
| 1583 | const char *lparenBuf = startBuf; |
| 1584 | while (*lparenBuf != '(') lparenBuf++; |
| 1585 | ReplaceText(startLoc, lparenBuf-startBuf+1, buf.c_str(), buf.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1586 | // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since |
| 1587 | // the sync expression is typically a message expression that's already |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1588 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1589 | SourceLocation endLoc = S->getSynchBody()->getLocStart(); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1590 | const char *endBuf = SM->getCharacterData(endLoc); |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1591 | while (*endBuf != ')') endBuf--; |
| 1592 | SourceLocation rparenLoc = startLoc.getFileLocWithOffset(endBuf-startBuf); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1593 | buf = ");\n"; |
| 1594 | // declare a new scope with two variables, _stack and _rethrow. |
| 1595 | buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n"; |
| 1596 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1597 | buf += "char *pointers[4];} _stack;\n"; |
| 1598 | buf += "id volatile _rethrow = 0;\n"; |
| 1599 | buf += "objc_exception_try_enter(&_stack);\n"; |
| 1600 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1601 | ReplaceText(rparenLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1602 | startLoc = S->getSynchBody()->getLocEnd(); |
| 1603 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1604 | |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1605 | assert((*startBuf == '}') && "bogus @synchronized block"); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1606 | SourceLocation lastCurlyLoc = startLoc; |
| 1607 | buf = "}\nelse {\n"; |
| 1608 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 1609 | buf += "}\n"; |
| 1610 | buf += "{ /* implicit finally clause */\n"; |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1611 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1612 | |
| 1613 | std::string syncBuf; |
| 1614 | syncBuf += " objc_sync_exit("; |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1615 | Expr *syncExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 1616 | CastExpr::CK_Unknown, |
| 1617 | S->getSynchExpr()); |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1618 | std::string syncExprBufS; |
| 1619 | llvm::raw_string_ostream syncExprBuf(syncExprBufS); |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1620 | syncExpr->printPretty(syncExprBuf, *Context, 0, |
| 1621 | PrintingPolicy(LangOpts)); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1622 | syncBuf += syncExprBuf.str(); |
| 1623 | syncBuf += ");"; |
| 1624 | |
| 1625 | buf += syncBuf; |
| 1626 | buf += "\n if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1627 | buf += "}\n"; |
| 1628 | buf += "}"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1629 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1630 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1631 | |
| 1632 | bool hasReturns = false; |
| 1633 | HasReturnStmts(S->getSynchBody(), hasReturns); |
| 1634 | if (hasReturns) |
| 1635 | RewriteSyncReturnStmts(S->getSynchBody(), syncBuf); |
| 1636 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1637 | return 0; |
| 1638 | } |
| 1639 | |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1640 | void RewriteObjC::WarnAboutReturnGotoStmts(Stmt *S) |
| 1641 | { |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1642 | // Perform a bottom up traversal of all children. |
| 1643 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1644 | CI != E; ++CI) |
| 1645 | if (*CI) |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1646 | WarnAboutReturnGotoStmts(*CI); |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1647 | |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1648 | if (isa<ReturnStmt>(S) || isa<GotoStmt>(S)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1649 | Diags.Report(Context->getFullLoc(S->getLocStart()), |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1650 | TryFinallyContainsReturnDiag); |
| 1651 | } |
| 1652 | return; |
| 1653 | } |
| 1654 | |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1655 | void RewriteObjC::HasReturnStmts(Stmt *S, bool &hasReturns) |
| 1656 | { |
| 1657 | // Perform a bottom up traversal of all children. |
| 1658 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1659 | CI != E; ++CI) |
| 1660 | if (*CI) |
| 1661 | HasReturnStmts(*CI, hasReturns); |
| 1662 | |
| 1663 | if (isa<ReturnStmt>(S)) |
| 1664 | hasReturns = true; |
| 1665 | return; |
| 1666 | } |
| 1667 | |
| 1668 | void RewriteObjC::RewriteTryReturnStmts(Stmt *S) { |
| 1669 | // Perform a bottom up traversal of all children. |
| 1670 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1671 | CI != E; ++CI) |
| 1672 | if (*CI) { |
| 1673 | RewriteTryReturnStmts(*CI); |
| 1674 | } |
| 1675 | if (isa<ReturnStmt>(S)) { |
| 1676 | SourceLocation startLoc = S->getLocStart(); |
| 1677 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1678 | |
| 1679 | const char *semiBuf = strchr(startBuf, ';'); |
| 1680 | assert((*semiBuf == ';') && "RewriteTryReturnStmts: can't find ';'"); |
| 1681 | SourceLocation onePastSemiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
| 1682 | |
| 1683 | std::string buf; |
| 1684 | buf = "{ objc_exception_try_exit(&_stack); return"; |
| 1685 | |
| 1686 | ReplaceText(startLoc, 6, buf.c_str(), buf.size()); |
| 1687 | InsertText(onePastSemiLoc, "}", 1); |
| 1688 | } |
| 1689 | return; |
| 1690 | } |
| 1691 | |
| 1692 | void RewriteObjC::RewriteSyncReturnStmts(Stmt *S, std::string syncExitBuf) { |
| 1693 | // Perform a bottom up traversal of all children. |
| 1694 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1695 | CI != E; ++CI) |
| 1696 | if (*CI) { |
| 1697 | RewriteSyncReturnStmts(*CI, syncExitBuf); |
| 1698 | } |
| 1699 | if (isa<ReturnStmt>(S)) { |
| 1700 | SourceLocation startLoc = S->getLocStart(); |
| 1701 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1702 | |
| 1703 | const char *semiBuf = strchr(startBuf, ';'); |
| 1704 | assert((*semiBuf == ';') && "RewriteSyncReturnStmts: can't find ';'"); |
| 1705 | SourceLocation onePastSemiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
| 1706 | |
| 1707 | std::string buf; |
| 1708 | buf = "{ objc_exception_try_exit(&_stack);"; |
| 1709 | buf += syncExitBuf; |
| 1710 | buf += " return"; |
| 1711 | |
| 1712 | ReplaceText(startLoc, 6, buf.c_str(), buf.size()); |
| 1713 | InsertText(onePastSemiLoc, "}", 1); |
| 1714 | } |
| 1715 | return; |
| 1716 | } |
| 1717 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1718 | Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1719 | // Get the start location and compute the semi location. |
| 1720 | SourceLocation startLoc = S->getLocStart(); |
| 1721 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1722 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1723 | assert((*startBuf == '@') && "bogus @try location"); |
| 1724 | |
| 1725 | std::string buf; |
| 1726 | // declare a new scope with two variables, _stack and _rethrow. |
| 1727 | buf = "/* @try scope begin */ { struct _objc_exception_data {\n"; |
| 1728 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1729 | buf += "char *pointers[4];} _stack;\n"; |
| 1730 | buf += "id volatile _rethrow = 0;\n"; |
| 1731 | buf += "objc_exception_try_enter(&_stack);\n"; |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1732 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1733 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1734 | ReplaceText(startLoc, 4, buf.c_str(), buf.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1735 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1736 | startLoc = S->getTryBody()->getLocEnd(); |
| 1737 | startBuf = SM->getCharacterData(startLoc); |
| 1738 | |
| 1739 | assert((*startBuf == '}') && "bogus @try block"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1740 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1741 | SourceLocation lastCurlyLoc = startLoc; |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1742 | ObjCAtCatchStmt *catchList = S->getCatchStmts(); |
| 1743 | if (catchList) { |
| 1744 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1745 | buf = " /* @catch begin */ else {\n"; |
| 1746 | buf += " id _caught = objc_exception_extract(&_stack);\n"; |
| 1747 | buf += " objc_exception_try_enter (&_stack);\n"; |
| 1748 | buf += " if (_setjmp(_stack.buf))\n"; |
| 1749 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1750 | buf += " else { /* @catch continue */"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1751 | |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1752 | InsertText(startLoc, buf.c_str(), buf.size()); |
Steve Naroff | 8bd3dc6 | 2008-09-09 19:59:12 +0000 | [diff] [blame] | 1753 | } else { /* no catch list */ |
| 1754 | buf = "}\nelse {\n"; |
| 1755 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1756 | buf += "}"; |
| 1757 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1758 | } |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1759 | bool sawIdTypedCatch = false; |
| 1760 | Stmt *lastCatchBody = 0; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1761 | while (catchList) { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1762 | ParmVarDecl *catchDecl = catchList->getCatchParamDecl(); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1763 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1764 | if (catchList == S->getCatchStmts()) |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1765 | buf = "if ("; // we are generating code for the first catch clause |
| 1766 | else |
| 1767 | buf = "else if ("; |
| 1768 | startLoc = catchList->getLocStart(); |
| 1769 | startBuf = SM->getCharacterData(startLoc); |
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 | assert((*startBuf == '@') && "bogus @catch location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1772 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1773 | const char *lParenLoc = strchr(startBuf, '('); |
| 1774 | |
Steve Naroff | be4b333 | 2008-02-01 22:08:12 +0000 | [diff] [blame] | 1775 | if (catchList->hasEllipsis()) { |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1776 | // Now rewrite the body... |
| 1777 | lastCatchBody = catchList->getCatchBody(); |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1778 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1779 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1780 | assert(*SM->getCharacterData(catchList->getRParenLoc()) == ')' && |
| 1781 | "bogus @catch paren location"); |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1782 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1783 | |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1784 | buf += "1) { id _tmp = _caught;"; |
Daniel Dunbar | d7407dc | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 1785 | Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, buf); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1786 | } else if (catchDecl) { |
| 1787 | QualType t = catchDecl->getType(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1788 | if (t == Context->getObjCIdType()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1789 | buf += "1) { "; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1790 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1791 | sawIdTypedCatch = true; |
Fariborz Jahanian | 66867c5 | 2010-01-12 01:22:23 +0000 | [diff] [blame] | 1792 | } else if (t->isObjCObjectPointerType()) { |
| 1793 | QualType InterfaceTy = t->getPointeeType(); |
| 1794 | const ObjCInterfaceType *cls = // Should be a pointer to a class. |
| 1795 | InterfaceTy->getAs<ObjCInterfaceType>(); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1796 | if (cls) { |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1797 | buf += "objc_exception_match((struct objc_class *)objc_getClass(\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1798 | buf += cls->getDecl()->getNameAsString(); |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1799 | buf += "\"), (struct objc_object *)_caught)) { "; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1800 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1801 | } |
| 1802 | } |
| 1803 | // Now rewrite the body... |
| 1804 | lastCatchBody = catchList->getCatchBody(); |
| 1805 | SourceLocation rParenLoc = catchList->getRParenLoc(); |
| 1806 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1807 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 1808 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 1809 | assert((*rParenBuf == ')') && "bogus @catch paren location"); |
| 1810 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1811 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1812 | buf = " = _caught;"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1813 | // Here we replace ") {" with "= _caught;" (which initializes and |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1814 | // declares the @catch parameter). |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1815 | ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1816 | } else { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1817 | assert(false && "@catch rewrite bug"); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1818 | } |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1819 | // make sure all the catch bodies get rewritten! |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1820 | catchList = catchList->getNextCatchStmt(); |
| 1821 | } |
| 1822 | // Complete the catch list... |
| 1823 | if (lastCatchBody) { |
| 1824 | SourceLocation bodyLoc = lastCatchBody->getLocEnd(); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1825 | assert(*SM->getCharacterData(bodyLoc) == '}' && |
| 1826 | "bogus @catch body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1827 | |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1828 | // Insert the last (implicit) else clause *before* the right curly brace. |
| 1829 | bodyLoc = bodyLoc.getFileLocWithOffset(-1); |
| 1830 | buf = "} /* last catch end */\n"; |
| 1831 | buf += "else {\n"; |
| 1832 | buf += " _rethrow = _caught;\n"; |
| 1833 | buf += " objc_exception_try_exit(&_stack);\n"; |
| 1834 | buf += "} } /* @catch end */\n"; |
| 1835 | if (!S->getFinallyStmt()) |
| 1836 | buf += "}\n"; |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1837 | InsertText(bodyLoc, buf.c_str(), buf.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1838 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1839 | // Set lastCurlyLoc |
| 1840 | lastCurlyLoc = lastCatchBody->getLocEnd(); |
| 1841 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1842 | if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1843 | startLoc = finalStmt->getLocStart(); |
| 1844 | startBuf = SM->getCharacterData(startLoc); |
| 1845 | assert((*startBuf == '@') && "bogus @finally start"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1846 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1847 | buf = "/* @finally */"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1848 | ReplaceText(startLoc, 8, buf.c_str(), buf.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1849 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1850 | Stmt *body = finalStmt->getFinallyBody(); |
| 1851 | SourceLocation startLoc = body->getLocStart(); |
| 1852 | SourceLocation endLoc = body->getLocEnd(); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1853 | assert(*SM->getCharacterData(startLoc) == '{' && |
| 1854 | "bogus @finally body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1855 | assert(*SM->getCharacterData(endLoc) == '}' && |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1856 | "bogus @finally body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1857 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1858 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1859 | buf = " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1860 | InsertText(startLoc, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1861 | endLoc = endLoc.getFileLocWithOffset(-1); |
| 1862 | buf = " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1863 | InsertText(endLoc, buf.c_str(), buf.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1864 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1865 | // Set lastCurlyLoc |
| 1866 | lastCurlyLoc = body->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1867 | |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1868 | // Now check for any return/continue/go statements within the @try. |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1869 | WarnAboutReturnGotoStmts(S->getTryBody()); |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1870 | } else { /* no finally clause - make sure we synthesize an implicit one */ |
| 1871 | buf = "{ /* implicit finally clause */\n"; |
| 1872 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
| 1873 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 1874 | buf += "}"; |
| 1875 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1876 | |
| 1877 | // Now check for any return/continue/go statements within the @try. |
| 1878 | // The implicit finally clause won't called if the @try contains any |
| 1879 | // jump statements. |
| 1880 | bool hasReturns = false; |
| 1881 | HasReturnStmts(S->getTryBody(), hasReturns); |
| 1882 | if (hasReturns) |
| 1883 | RewriteTryReturnStmts(S->getTryBody()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1884 | } |
| 1885 | // Now emit the final closing curly brace... |
| 1886 | lastCurlyLoc = lastCurlyLoc.getFileLocWithOffset(1); |
| 1887 | buf = " } /* @try scope end */\n"; |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1888 | InsertText(lastCurlyLoc, buf.c_str(), buf.size()); |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1889 | return 0; |
| 1890 | } |
| 1891 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1892 | Stmt *RewriteObjC::RewriteObjCCatchStmt(ObjCAtCatchStmt *S) { |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1893 | return 0; |
| 1894 | } |
| 1895 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1896 | Stmt *RewriteObjC::RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S) { |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1897 | return 0; |
| 1898 | } |
| 1899 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1900 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
| 1901 | // the throw expression is typically a message expression that's already |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1902 | // been rewritten! (which implies the SourceLocation's are invalid). |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1903 | Stmt *RewriteObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1904 | // Get the start location and compute the semi location. |
| 1905 | SourceLocation startLoc = S->getLocStart(); |
| 1906 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1907 | |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1908 | assert((*startBuf == '@') && "bogus @throw location"); |
| 1909 | |
| 1910 | std::string buf; |
| 1911 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
Steve Naroff | 20ebf8f | 2008-01-19 00:42:38 +0000 | [diff] [blame] | 1912 | if (S->getThrowExpr()) |
| 1913 | buf = "objc_exception_throw("; |
| 1914 | else // add an implicit argument |
| 1915 | buf = "objc_exception_throw(_caught"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1916 | |
Steve Naroff | 4ba0acb | 2008-07-25 15:41:30 +0000 | [diff] [blame] | 1917 | // handle "@ throw" correctly. |
| 1918 | const char *wBuf = strchr(startBuf, 'w'); |
| 1919 | assert((*wBuf == 'w') && "@throw: can't find 'w'"); |
| 1920 | ReplaceText(startLoc, wBuf-startBuf+1, buf.c_str(), buf.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1921 | |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1922 | const char *semiBuf = strchr(startBuf, ';'); |
| 1923 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
| 1924 | SourceLocation semiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf); |
| 1925 | buf = ");"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1926 | ReplaceText(semiLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1927 | return 0; |
| 1928 | } |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1929 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1930 | Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 1931 | // Create a new string expression. |
| 1932 | QualType StrType = Context->getPointerType(Context->CharTy); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1933 | std::string StrEncoding; |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1934 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 1935 | Expr *Replacement = StringLiteral::Create(*Context,StrEncoding.c_str(), |
| 1936 | StrEncoding.length(), false,StrType, |
| 1937 | SourceLocation()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1938 | ReplaceStmt(Exp, Replacement); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1939 | |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 1940 | // Replace this subexpr in the parent. |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1941 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 1942 | return Replacement; |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1945 | Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
Steve Naroff | 1a93764 | 2008-12-22 22:16:07 +0000 | [diff] [blame] | 1946 | if (!SelGetUidFunctionDecl) |
| 1947 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1948 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 1949 | // Create a call to sel_registerName("selName"). |
| 1950 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 1951 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1952 | SelExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 6e94ef5 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 1953 | Exp->getSelector().getAsString().c_str(), |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1954 | Exp->getSelector().getAsString().size(), |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 1955 | false, argType, SourceLocation())); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1956 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 1957 | &SelExprs[0], SelExprs.size()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1958 | ReplaceStmt(Exp, SelExp); |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1959 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1960 | return SelExp; |
| 1961 | } |
| 1962 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1963 | CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl( |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1964 | FunctionDecl *FD, Expr **args, unsigned nargs) { |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1965 | // 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] | 1966 | QualType msgSendType = FD->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1967 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1968 | // Create a reference to the objc_msgSend() declaration. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1969 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, msgSendType, SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1970 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1971 | // 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] | 1972 | QualType pToFunc = Context->getPointerType(msgSendType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1973 | ImplicitCastExpr *ICE = new (Context) ImplicitCastExpr(pToFunc, |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 1974 | CastExpr::CK_Unknown, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1975 | DRE, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1976 | /*isLvalue=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1977 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1978 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1979 | |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 1980 | return new (Context) CallExpr(*Context, ICE, args, nargs, FT->getResultType(), |
| 1981 | SourceLocation()); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1984 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 1985 | const char *&startRef, const char *&endRef) { |
| 1986 | while (startBuf < endBuf) { |
| 1987 | if (*startBuf == '<') |
| 1988 | startRef = startBuf; // mark the start. |
| 1989 | if (*startBuf == '>') { |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1990 | if (startRef && *startRef == '<') { |
| 1991 | endRef = startBuf; // mark the end. |
| 1992 | return true; |
| 1993 | } |
| 1994 | return false; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1995 | } |
| 1996 | startBuf++; |
| 1997 | } |
| 1998 | return false; |
| 1999 | } |
| 2000 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2001 | static void scanToNextArgument(const char *&argRef) { |
| 2002 | int angle = 0; |
| 2003 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 2004 | if (*argRef == '<') |
| 2005 | angle++; |
| 2006 | else if (*argRef == '>') |
| 2007 | angle--; |
| 2008 | argRef++; |
| 2009 | } |
| 2010 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 2011 | } |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2012 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2013 | bool RewriteObjC::needToScanForQualifiers(QualType T) { |
Fariborz Jahanian | 32132a0 | 2010-02-03 21:29:28 +0000 | [diff] [blame] | 2014 | if (T->isObjCQualifiedIdType()) |
| 2015 | return true; |
Fariborz Jahanian | 84aa946 | 2010-02-02 18:35:07 +0000 | [diff] [blame] | 2016 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 2017 | if (PT->getPointeeType()->isObjCQualifiedIdType()) |
| 2018 | return true; |
| 2019 | } |
| 2020 | if (T->isObjCObjectPointerType()) { |
| 2021 | T = T->getPointeeType(); |
| 2022 | return T->isObjCQualifiedInterfaceType(); |
| 2023 | } |
| 2024 | return false; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2025 | } |
| 2026 | |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2027 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) { |
| 2028 | QualType Type = E->getType(); |
| 2029 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2030 | SourceLocation Loc, EndLoc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2031 | |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2032 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) { |
| 2033 | Loc = ECE->getLParenLoc(); |
| 2034 | EndLoc = ECE->getRParenLoc(); |
| 2035 | } else { |
| 2036 | Loc = E->getLocStart(); |
| 2037 | EndLoc = E->getLocEnd(); |
| 2038 | } |
| 2039 | // This will defend against trying to rewrite synthesized expressions. |
| 2040 | if (Loc.isInvalid() || EndLoc.isInvalid()) |
| 2041 | return; |
| 2042 | |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2043 | const char *startBuf = SM->getCharacterData(Loc); |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2044 | const char *endBuf = SM->getCharacterData(EndLoc); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2045 | const char *startRef = 0, *endRef = 0; |
| 2046 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2047 | // Get the locations of the startRef, endRef. |
| 2048 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-startBuf); |
| 2049 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-startBuf+1); |
| 2050 | // Comment out the protocol references. |
| 2051 | InsertText(LessLoc, "/*", 2); |
| 2052 | InsertText(GreaterLoc, "*/", 2); |
| 2053 | } |
| 2054 | } |
| 2055 | } |
| 2056 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2057 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2058 | SourceLocation Loc; |
| 2059 | QualType Type; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2060 | const FunctionProtoType *proto = 0; |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2061 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 2062 | Loc = VD->getLocation(); |
| 2063 | Type = VD->getType(); |
| 2064 | } |
| 2065 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 2066 | Loc = FD->getLocation(); |
| 2067 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 2068 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2069 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2070 | assert(funcType && "missing function type"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2071 | proto = dyn_cast<FunctionProtoType>(funcType); |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2072 | if (!proto) |
| 2073 | return; |
| 2074 | Type = proto->getResultType(); |
| 2075 | } |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 2076 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(Dcl)) { |
| 2077 | Loc = FD->getLocation(); |
| 2078 | Type = FD->getType(); |
| 2079 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2080 | else |
| 2081 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2082 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2083 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2084 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2085 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2086 | const char *endBuf = SM->getCharacterData(Loc); |
| 2087 | const char *startBuf = endBuf; |
Steve Naroff | 6cafbf2 | 2008-05-31 05:02:17 +0000 | [diff] [blame] | 2088 | while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2089 | startBuf--; // scan backward (from the decl location) for return type. |
| 2090 | const char *startRef = 0, *endRef = 0; |
| 2091 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2092 | // Get the locations of the startRef, endRef. |
| 2093 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-endBuf); |
| 2094 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-endBuf+1); |
| 2095 | // Comment out the protocol references. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2096 | InsertText(LessLoc, "/*", 2); |
| 2097 | InsertText(GreaterLoc, "*/", 2); |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2098 | } |
| 2099 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2100 | if (!proto) |
| 2101 | return; // most likely, was a variable |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2102 | // Now check arguments. |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2103 | const char *startBuf = SM->getCharacterData(Loc); |
| 2104 | const char *startFuncBuf = startBuf; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2105 | for (unsigned i = 0; i < proto->getNumArgs(); i++) { |
| 2106 | if (needToScanForQualifiers(proto->getArgType(i))) { |
| 2107 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2108 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2109 | const char *endBuf = startBuf; |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2110 | // scan forward (from the decl location) for argument types. |
| 2111 | scanToNextArgument(endBuf); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2112 | const char *startRef = 0, *endRef = 0; |
| 2113 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2114 | // Get the locations of the startRef, endRef. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2115 | SourceLocation LessLoc = |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2116 | Loc.getFileLocWithOffset(startRef-startFuncBuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2117 | SourceLocation GreaterLoc = |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2118 | Loc.getFileLocWithOffset(endRef-startFuncBuf+1); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2119 | // Comment out the protocol references. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2120 | InsertText(LessLoc, "/*", 2); |
| 2121 | InsertText(GreaterLoc, "*/", 2); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2122 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2123 | startBuf = ++endBuf; |
| 2124 | } |
| 2125 | else { |
Steve Naroff | aba49d1 | 2008-08-06 15:58:23 +0000 | [diff] [blame] | 2126 | // If the function name is derived from a macro expansion, then the |
| 2127 | // argument buffer will not follow the name. Need to speak with Chris. |
| 2128 | while (*startBuf && *startBuf != ')' && *startBuf != ',') |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2129 | startBuf++; // scan forward (from the decl location) for argument types. |
| 2130 | startBuf++; |
| 2131 | } |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2132 | } |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2133 | } |
| 2134 | |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2135 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2136 | void RewriteObjC::SynthSelGetUidFunctionDecl() { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2137 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
| 2138 | llvm::SmallVector<QualType, 16> ArgTys; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2139 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2140 | QualType getFuncType = Context->getFunctionType(Context->getObjCSelType(), |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2141 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2142 | false /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2143 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2144 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2145 | SelGetUidIdent, getFuncType, 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2146 | FunctionDecl::Extern, false); |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2147 | } |
| 2148 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2149 | void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2150 | // declared in <objc/objc.h> |
Douglas Gregor | 51efe56 | 2009-01-09 01:47:02 +0000 | [diff] [blame] | 2151 | if (FD->getIdentifier() && |
| 2152 | strcmp(FD->getNameAsCString(), "sel_registerName") == 0) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2153 | SelGetUidFunctionDecl = FD; |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2154 | return; |
| 2155 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2156 | RewriteObjCQualifiedInterfaceTypes(FD); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2157 | } |
| 2158 | |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2159 | void RewriteObjC::RewriteBlockLiteralFunctionDecl(FunctionDecl *FD) { |
| 2160 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
| 2161 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
| 2162 | const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(funcType); |
| 2163 | if (!proto) |
| 2164 | return; |
| 2165 | QualType Type = proto->getResultType(); |
| 2166 | std::string FdStr = Type.getAsString(); |
| 2167 | FdStr += " "; |
| 2168 | FdStr += FD->getNameAsCString(); |
| 2169 | FdStr += "("; |
| 2170 | unsigned numArgs = proto->getNumArgs(); |
| 2171 | for (unsigned i = 0; i < numArgs; i++) { |
| 2172 | QualType ArgType = proto->getArgType(i); |
| 2173 | FdStr += ArgType.getAsString(); |
| 2174 | |
| 2175 | if (i+1 < numArgs) |
| 2176 | FdStr += ", "; |
| 2177 | } |
| 2178 | FdStr += ");\n"; |
| 2179 | InsertText(FunLocStart, FdStr.c_str(), FdStr.size()); |
| 2180 | CurFunctionDeclToDeclareForBlock = 0; |
| 2181 | } |
| 2182 | |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2183 | // SynthSuperContructorFunctionDecl - id objc_super(id obj, id super); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2184 | void RewriteObjC::SynthSuperContructorFunctionDecl() { |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2185 | if (SuperContructorFunctionDecl) |
| 2186 | return; |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2187 | IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super"); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2188 | llvm::SmallVector<QualType, 16> ArgTys; |
| 2189 | QualType argT = Context->getObjCIdType(); |
| 2190 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2191 | ArgTys.push_back(argT); |
| 2192 | ArgTys.push_back(argT); |
| 2193 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
| 2194 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2195 | false, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2196 | SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2197 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2198 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2199 | FunctionDecl::Extern, false); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2200 | } |
| 2201 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2202 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2203 | void RewriteObjC::SynthMsgSendFunctionDecl() { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2204 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
| 2205 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2206 | QualType argT = Context->getObjCIdType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2207 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2208 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2209 | argT = Context->getObjCSelType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2210 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2211 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2212 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2213 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2214 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2215 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2216 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2217 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2218 | FunctionDecl::Extern, false); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2219 | } |
| 2220 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2221 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2222 | void RewriteObjC::SynthMsgSendSuperFunctionDecl() { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2223 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
| 2224 | llvm::SmallVector<QualType, 16> ArgTys; |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2225 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2226 | SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2227 | &Context->Idents.get("objc_super")); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2228 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2229 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2230 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2231 | argT = Context->getObjCSelType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2232 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2233 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2234 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2235 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2236 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2237 | MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2238 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2239 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2240 | FunctionDecl::Extern, false); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2241 | } |
| 2242 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2243 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2244 | void RewriteObjC::SynthMsgSendStretFunctionDecl() { |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2245 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
| 2246 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2247 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2248 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2249 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2250 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2251 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2252 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2253 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2254 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2255 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2256 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2257 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2258 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2259 | FunctionDecl::Extern, false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2262 | // SynthMsgSendSuperStretFunctionDecl - |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2263 | // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2264 | void RewriteObjC::SynthMsgSendSuperStretFunctionDecl() { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2265 | IdentifierInfo *msgSendIdent = |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2266 | &Context->Idents.get("objc_msgSendSuper_stret"); |
| 2267 | llvm::SmallVector<QualType, 16> ArgTys; |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2268 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2269 | SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2270 | &Context->Idents.get("objc_super")); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2271 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2272 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2273 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2274 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2275 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2276 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2277 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2278 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2279 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2280 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2281 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2282 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2283 | FunctionDecl::Extern, false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2284 | } |
| 2285 | |
Steve Naroff | 1284db8 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2286 | // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2287 | void RewriteObjC::SynthMsgSendFpretFunctionDecl() { |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2288 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
| 2289 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2290 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2291 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2292 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2293 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2294 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2295 | ArgTys.push_back(argT); |
Steve Naroff | 1284db8 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2296 | QualType msgSendType = Context->getFunctionType(Context->DoubleTy, |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2297 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2298 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2299 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2300 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2301 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2302 | FunctionDecl::Extern, false); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2303 | } |
| 2304 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2305 | // SynthGetClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2306 | void RewriteObjC::SynthGetClassFunctionDecl() { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2307 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
| 2308 | llvm::SmallVector<QualType, 16> ArgTys; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2309 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2310 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2311 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2312 | false /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2313 | GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2314 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2315 | getClassIdent, getClassType, 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2316 | FunctionDecl::Extern, false); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2317 | } |
| 2318 | |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2319 | // SynthGetMetaClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2320 | void RewriteObjC::SynthGetMetaClassFunctionDecl() { |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2321 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
| 2322 | llvm::SmallVector<QualType, 16> ArgTys; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2323 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2324 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2325 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2326 | false /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2327 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2328 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2329 | getClassIdent, getClassType, 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2330 | FunctionDecl::Extern, false); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2331 | } |
| 2332 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2333 | Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2334 | QualType strType = getConstantStringStructType(); |
| 2335 | |
| 2336 | std::string S = "__NSConstantStringImpl_"; |
Steve Naroff | 7691d9b | 2008-05-31 03:35:42 +0000 | [diff] [blame] | 2337 | |
| 2338 | std::string tmpName = InFileName; |
| 2339 | unsigned i; |
| 2340 | for (i=0; i < tmpName.length(); i++) { |
| 2341 | char c = tmpName.at(i); |
| 2342 | // replace any non alphanumeric characters with '_'. |
| 2343 | if (!isalpha(c) && (c < '0' || c > '9')) |
| 2344 | tmpName[i] = '_'; |
| 2345 | } |
| 2346 | S += tmpName; |
| 2347 | S += "_"; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2348 | S += utostr(NumObjCStringLiterals++); |
| 2349 | |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2350 | Preamble += "static __NSConstantStringImpl " + S; |
| 2351 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 2352 | Preamble += "0x000007c8,"; // utf8_str |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2353 | // The pretty printer for StringLiteral handles escape characters properly. |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2354 | std::string prettyBufS; |
| 2355 | llvm::raw_string_ostream prettyBuf(prettyBufS); |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2356 | Exp->getString()->printPretty(prettyBuf, *Context, 0, |
| 2357 | PrintingPolicy(LangOpts)); |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2358 | Preamble += prettyBuf.str(); |
| 2359 | Preamble += ","; |
Steve Naroff | fd5b76f | 2009-12-06 01:48:44 +0000 | [diff] [blame] | 2360 | Preamble += utostr(Exp->getString()->getByteLength()) + "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2361 | |
| 2362 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
| 2363 | &Context->Idents.get(S.c_str()), strType, 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2364 | VarDecl::Static); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2365 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, strType, SourceLocation()); |
| 2366 | Expr *Unop = new (Context) UnaryOperator(DRE, UnaryOperator::AddrOf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2367 | Context->getPointerType(DRE->getType()), |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2368 | SourceLocation()); |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2369 | // cast to NSConstantString * |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2370 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Exp->getType(), |
| 2371 | CastExpr::CK_Unknown, Unop); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2372 | ReplaceStmt(Exp, cast); |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 2373 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2374 | return cast; |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2375 | } |
| 2376 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2377 | ObjCInterfaceDecl *RewriteObjC::isSuperReceiver(Expr *recExpr) { |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2378 | // check if we are sending a message to 'super' |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 2379 | if (!CurMethodDef || !CurMethodDef->isInstanceMethod()) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2380 | |
Douglas Gregor | cd9b46e | 2008-11-04 14:56:14 +0000 | [diff] [blame] | 2381 | if (ObjCSuperExpr *Super = dyn_cast<ObjCSuperExpr>(recExpr)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2382 | const ObjCObjectPointerType *OPT = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2383 | Super->getType()->getAs<ObjCObjectPointerType>(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2384 | assert(OPT); |
| 2385 | const ObjCInterfaceType *IT = OPT->getInterfaceType(); |
Chris Lattner | 0d17f6f | 2008-06-21 18:04:54 +0000 | [diff] [blame] | 2386 | return IT->getDecl(); |
| 2387 | } |
| 2388 | return 0; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2389 | } |
| 2390 | |
| 2391 | // struct objc_super { struct objc_object *receiver; struct objc_class *super; }; |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2392 | QualType RewriteObjC::getSuperStructType() { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2393 | if (!SuperStructDecl) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2394 | SuperStructDecl = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2395 | SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2396 | &Context->Idents.get("objc_super")); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2397 | QualType FieldTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2398 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2399 | // struct objc_object *receiver; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2400 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2401 | // struct objc_class *super; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2402 | FieldTypes[1] = Context->getObjCClassType(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2403 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2404 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2405 | for (unsigned i = 0; i < 2; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2406 | SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl, |
| 2407 | SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2408 | FieldTypes[i], 0, |
| 2409 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2410 | /*Mutable=*/false)); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2411 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2412 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2413 | SuperStructDecl->completeDefinition(*Context); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2414 | } |
| 2415 | return Context->getTagDeclType(SuperStructDecl); |
| 2416 | } |
| 2417 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2418 | QualType RewriteObjC::getConstantStringStructType() { |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2419 | if (!ConstantStringDecl) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2420 | ConstantStringDecl = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2421 | SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2422 | &Context->Idents.get("__NSConstantStringImpl")); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2423 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2424 | |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2425 | // struct objc_object *receiver; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2426 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2427 | // int flags; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2428 | FieldTypes[1] = Context->IntTy; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2429 | // char *str; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2430 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2431 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2432 | FieldTypes[3] = Context->LongTy; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2433 | |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2434 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2435 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2436 | ConstantStringDecl->addDecl(FieldDecl::Create(*Context, |
| 2437 | ConstantStringDecl, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2438 | SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2439 | FieldTypes[i], 0, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2440 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2441 | /*Mutable=*/true)); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2442 | } |
| 2443 | |
| 2444 | ConstantStringDecl->completeDefinition(*Context); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2445 | } |
| 2446 | return Context->getTagDeclType(ConstantStringDecl); |
| 2447 | } |
| 2448 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2449 | Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2450 | if (!SelGetUidFunctionDecl) |
| 2451 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2452 | if (!MsgSendFunctionDecl) |
| 2453 | SynthMsgSendFunctionDecl(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2454 | if (!MsgSendSuperFunctionDecl) |
| 2455 | SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2456 | if (!MsgSendStretFunctionDecl) |
| 2457 | SynthMsgSendStretFunctionDecl(); |
| 2458 | if (!MsgSendSuperStretFunctionDecl) |
| 2459 | SynthMsgSendSuperStretFunctionDecl(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2460 | if (!MsgSendFpretFunctionDecl) |
| 2461 | SynthMsgSendFpretFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2462 | if (!GetClassFunctionDecl) |
| 2463 | SynthGetClassFunctionDecl(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2464 | if (!GetMetaClassFunctionDecl) |
| 2465 | SynthGetMetaClassFunctionDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2466 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2467 | // default to objc_msgSend(). |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2468 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2469 | // May need to use objc_msgSend_stret() as well. |
| 2470 | FunctionDecl *MsgSendStretFlavor = 0; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2471 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
| 2472 | QualType resultType = mDecl->getResultType(); |
Chris Lattner | 8b51fd7 | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2473 | if (resultType->isStructureType() || resultType->isUnionType()) |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2474 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
Chris Lattner | 8b51fd7 | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2475 | else if (resultType->isRealFloatingType()) |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2476 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2477 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2478 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2479 | // Synthesize a call to objc_msgSend(). |
| 2480 | llvm::SmallVector<Expr*, 8> MsgExprs; |
| 2481 | IdentifierInfo *clsName = Exp->getClassName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2482 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2483 | // Derive/push the receiver/selector, 2 implicit arguments to objc_msgSend(). |
| 2484 | if (clsName) { // class message. |
Steve Naroff | fc93d52 | 2008-07-24 19:44:33 +0000 | [diff] [blame] | 2485 | // FIXME: We need to fix Sema (and the AST for ObjCMessageExpr) to handle |
| 2486 | // the 'super' idiom within a class method. |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 2487 | if (clsName->getName() == "super") { |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2488 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2489 | if (MsgSendStretFlavor) |
| 2490 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2491 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2492 | |
| 2493 | ObjCInterfaceDecl *SuperDecl = |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 2494 | CurMethodDef->getClassInterface()->getSuperClass(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2495 | |
| 2496 | llvm::SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2497 | |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2498 | // set the receiver to self, the first argument to all methods. |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2499 | InitExprs.push_back( |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2500 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 2501 | CastExpr::CK_Unknown, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2502 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2503 | Context->getObjCIdType(), |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2504 | SourceLocation())) |
| 2505 | ); // set the 'receiver'. |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2506 | |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2507 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2508 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2509 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2510 | SuperDecl->getIdentifier()->getNameStart(), |
| 2511 | SuperDecl->getIdentifier()->getLength(), |
| 2512 | false, argType, SourceLocation())); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2513 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2514 | &ClsExprs[0], |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2515 | ClsExprs.size()); |
| 2516 | // To turn off a warning, type-cast to 'id' |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2517 | InitExprs.push_back( // set 'super class', using objc_getClass(). |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2518 | NoTypeInfoCStyleCastExpr(Context, |
| 2519 | Context->getObjCIdType(), |
| 2520 | CastExpr::CK_Unknown, Cls)); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2521 | // struct objc_super |
| 2522 | QualType superType = getSuperStructType(); |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2523 | Expr *SuperRep; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2524 | |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2525 | if (LangOpts.Microsoft) { |
| 2526 | SynthSuperContructorFunctionDecl(); |
| 2527 | // Simulate a contructor call... |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2528 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2529 | superType, SourceLocation()); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2530 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2531 | InitExprs.size(), |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2532 | superType, SourceLocation()); |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2533 | // The code for super is a little tricky to prevent collision with |
| 2534 | // the structure definition in the header. The rewriter has it's own |
| 2535 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2536 | // we need the cast below. For example: |
| 2537 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2538 | // |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2539 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2540 | Context->getPointerType(SuperRep->getType()), |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2541 | SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2542 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2543 | Context->getPointerType(superType), |
| 2544 | CastExpr::CK_Unknown, SuperRep); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2545 | } else { |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2546 | // (struct objc_super) { <exprs from above> } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2547 | InitListExpr *ILE = new (Context) InitListExpr(SourceLocation(), |
| 2548 | &InitExprs[0], InitExprs.size(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2549 | SourceLocation()); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 2550 | TypeSourceInfo *superTInfo |
| 2551 | = Context->getTrivialTypeSourceInfo(superType); |
John McCall | 1d7d8d6 | 2010-01-19 22:33:45 +0000 | [diff] [blame] | 2552 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
| 2553 | superType, ILE, false); |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2554 | // struct objc_super * |
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()); |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2558 | } |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2559 | MsgExprs.push_back(SuperRep); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2560 | } else { |
| 2561 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2562 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2563 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2564 | clsName->getNameStart(), |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2565 | clsName->getLength(), |
| 2566 | false, argType, |
| 2567 | SourceLocation())); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2568 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2569 | &ClsExprs[0], |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2570 | ClsExprs.size()); |
| 2571 | MsgExprs.push_back(Cls); |
| 2572 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2573 | } else { // instance message. |
| 2574 | Expr *recExpr = Exp->getReceiver(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2575 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2576 | if (ObjCInterfaceDecl *SuperDecl = isSuperReceiver(recExpr)) { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2577 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2578 | if (MsgSendStretFlavor) |
| 2579 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2580 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2581 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2582 | llvm::SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2583 | |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2584 | InitExprs.push_back( |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2585 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 2586 | CastExpr::CK_Unknown, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2587 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
Steve Naroff | f616ebb | 2008-07-16 22:35:27 +0000 | [diff] [blame] | 2588 | Context->getObjCIdType(), |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2589 | SourceLocation())) |
| 2590 | ); // set the 'receiver'. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2591 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2592 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2593 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2594 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2595 | SuperDecl->getIdentifier()->getNameStart(), |
| 2596 | SuperDecl->getIdentifier()->getLength(), |
| 2597 | false, argType, SourceLocation())); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2598 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2599 | &ClsExprs[0], |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2600 | ClsExprs.size()); |
Fariborz Jahanian | 7127431 | 2007-12-05 17:29:46 +0000 | [diff] [blame] | 2601 | // To turn off a warning, type-cast to 'id' |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2602 | InitExprs.push_back( |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2603 | // set 'super class', using objc_getClass(). |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2604 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 2605 | CastExpr::CK_Unknown, Cls)); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2606 | // struct objc_super |
| 2607 | QualType superType = getSuperStructType(); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2608 | Expr *SuperRep; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2609 | |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2610 | if (LangOpts.Microsoft) { |
| 2611 | SynthSuperContructorFunctionDecl(); |
| 2612 | // Simulate a contructor call... |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2613 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2614 | superType, SourceLocation()); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2615 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2616 | InitExprs.size(), |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2617 | superType, SourceLocation()); |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2618 | // The code for super is a little tricky to prevent collision with |
| 2619 | // the structure definition in the header. The rewriter has it's own |
| 2620 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2621 | // we need the cast below. For example: |
| 2622 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2623 | // |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2624 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2625 | Context->getPointerType(SuperRep->getType()), |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2626 | SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2627 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2628 | Context->getPointerType(superType), |
| 2629 | CastExpr::CK_Unknown, SuperRep); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2630 | } else { |
| 2631 | // (struct objc_super) { <exprs from above> } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2632 | InitListExpr *ILE = new (Context) InitListExpr(SourceLocation(), |
| 2633 | &InitExprs[0], InitExprs.size(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2634 | SourceLocation()); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 2635 | TypeSourceInfo *superTInfo |
| 2636 | = Context->getTrivialTypeSourceInfo(superType); |
John McCall | 1d7d8d6 | 2010-01-19 22:33:45 +0000 | [diff] [blame] | 2637 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
| 2638 | superType, ILE, false); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2639 | } |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2640 | MsgExprs.push_back(SuperRep); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2641 | } else { |
Fariborz Jahanian | 7dd8283 | 2007-12-07 21:21:21 +0000 | [diff] [blame] | 2642 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 2643 | // Foo<Proto> *. |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2644 | while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr)) |
Fariborz Jahanian | 7dd8283 | 2007-12-07 21:21:21 +0000 | [diff] [blame] | 2645 | recExpr = CE->getSubExpr(); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2646 | recExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 2647 | CastExpr::CK_Unknown, recExpr); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2648 | MsgExprs.push_back(recExpr); |
| 2649 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2650 | } |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2651 | // 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] | 2652 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 2653 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2654 | SelExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 6e94ef5 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2655 | Exp->getSelector().getAsString().c_str(), |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2656 | Exp->getSelector().getAsString().size(), |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 2657 | false, argType, SourceLocation())); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2658 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2659 | &SelExprs[0], SelExprs.size()); |
| 2660 | MsgExprs.push_back(SelExp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2661 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2662 | // Now push any user supplied arguments. |
| 2663 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2664 | Expr *userExpr = Exp->getArg(i); |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2665 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 2666 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 2667 | // Reuse the ICE type, it is exactly what the doctor ordered. |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2668 | QualType type = ICE->getType()->isObjCQualifiedIdType() |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2669 | ? Context->getObjCIdType() |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2670 | : ICE->getType(); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2671 | userExpr = NoTypeInfoCStyleCastExpr(Context, type, CastExpr::CK_Unknown, |
| 2672 | userExpr); |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2673 | } |
| 2674 | // Make id<P...> cast into an 'id' cast. |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2675 | else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2676 | if (CE->getType()->isObjCQualifiedIdType()) { |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2677 | while ((CE = dyn_cast<CStyleCastExpr>(userExpr))) |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2678 | userExpr = CE->getSubExpr(); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2679 | userExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 2680 | CastExpr::CK_Unknown, userExpr); |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2681 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2682 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2683 | MsgExprs.push_back(userExpr); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2684 | // We've transferred the ownership to MsgExprs. For now, we *don't* null |
| 2685 | // out the argument in the original expression (since we aren't deleting |
| 2686 | // the ObjCMessageExpr). See RewritePropertySetter() usage for more info. |
| 2687 | //Exp->setArg(i, 0); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2688 | } |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2689 | // Generate the funky cast. |
| 2690 | CastExpr *cast; |
| 2691 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 2692 | QualType returnType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2693 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2694 | // Push 'id' and 'SEL', the 2 implicit arguments. |
Steve Naroff | c3a438c | 2007-11-15 10:43:57 +0000 | [diff] [blame] | 2695 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 2696 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 2697 | else |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2698 | ArgTypes.push_back(Context->getObjCIdType()); |
| 2699 | ArgTypes.push_back(Context->getObjCSelType()); |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2700 | if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) { |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2701 | // Push any user argument types. |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2702 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 2703 | E = OMD->param_end(); PI != E; ++PI) { |
| 2704 | QualType t = (*PI)->getType()->isObjCQualifiedIdType() |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2705 | ? Context->getObjCIdType() |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2706 | : (*PI)->getType(); |
Steve Naroff | a206b06 | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 2707 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 2708 | if (isTopLevelBlockPointerType(t)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2709 | const BlockPointerType *BPT = t->getAs<BlockPointerType>(); |
Steve Naroff | a206b06 | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 2710 | t = Context->getPointerType(BPT->getPointeeType()); |
| 2711 | } |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 2712 | ArgTypes.push_back(t); |
| 2713 | } |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2714 | returnType = OMD->getResultType()->isObjCQualifiedIdType() |
| 2715 | ? Context->getObjCIdType() : OMD->getResultType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2716 | } else { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2717 | returnType = Context->getObjCIdType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2718 | } |
| 2719 | // 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] | 2720 | QualType msgSendType = MsgSendFlavor->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2721 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2722 | // Create a reference to the objc_msgSend() declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2723 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, msgSendType, |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2724 | SourceLocation()); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2725 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2726 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2727 | // If we don't do this cast, we get the following bizarre warning/note: |
| 2728 | // xx.m:13: warning: function called through a non-compatible type |
| 2729 | // xx.m:13: note: if this code is reached, the program will abort |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2730 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 2731 | Context->getPointerType(Context->VoidTy), |
| 2732 | CastExpr::CK_Unknown, DRE); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2733 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2734 | // Now do the "normal" pointer to function cast. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2735 | QualType castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | d0ee6f9 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 2736 | &ArgTypes[0], ArgTypes.size(), |
Steve Naroff | 2679e48 | 2008-03-18 02:02:04 +0000 | [diff] [blame] | 2737 | // If we don't have a method decl, force a variadic cast. |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2738 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true, 0); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2739 | castType = Context->getPointerType(castType); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2740 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CastExpr::CK_Unknown, |
| 2741 | cast); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2742 | |
| 2743 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2744 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2745 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2746 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2747 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2748 | MsgExprs.size(), |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2749 | FT->getResultType(), SourceLocation()); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2750 | Stmt *ReplacingStmt = CE; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2751 | if (MsgSendStretFlavor) { |
| 2752 | // We have the method which returns a struct/union. Must also generate |
| 2753 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 2754 | // expression which dictate which one to envoke depending on size of |
| 2755 | // method's return type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2756 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2757 | // Create a reference to the objc_msgSend_stret() declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2758 | DeclRefExpr *STDRE = new (Context) DeclRefExpr(MsgSendStretFlavor, msgSendType, |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2759 | SourceLocation()); |
| 2760 | // Need to cast objc_msgSend_stret to "void *" (see above comment). |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2761 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 2762 | Context->getPointerType(Context->VoidTy), |
| 2763 | CastExpr::CK_Unknown, STDRE); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2764 | // Now do the "normal" pointer to function cast. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2765 | castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | d0ee6f9 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 2766 | &ArgTypes[0], ArgTypes.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2767 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false, 0); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2768 | castType = Context->getPointerType(castType); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2769 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CastExpr::CK_Unknown, |
| 2770 | cast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2771 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2772 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2773 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2774 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2775 | FT = msgSendType->getAs<FunctionType>(); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2776 | CallExpr *STCE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2777 | MsgExprs.size(), |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2778 | FT->getResultType(), SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2779 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2780 | // Build sizeof(returnType) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2781 | SizeOfAlignOfExpr *sizeofExpr = new (Context) SizeOfAlignOfExpr(true, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2782 | Context->getTrivialTypeSourceInfo(returnType), |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2783 | Context->getSizeType(), |
| 2784 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2785 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 2786 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 2787 | // For X86 it is more complicated and some kind of target specific routine |
| 2788 | // is needed to decide what to do. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2789 | unsigned IntSize = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2790 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2791 | IntegerLiteral *limit = new (Context) IntegerLiteral(llvm::APInt(IntSize, 8), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2792 | Context->IntTy, |
| 2793 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2794 | BinaryOperator *lessThanExpr = new (Context) BinaryOperator(sizeofExpr, limit, |
| 2795 | BinaryOperator::LE, |
| 2796 | Context->IntTy, |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2797 | SourceLocation()); |
| 2798 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2799 | ConditionalOperator *CondExpr = |
Douglas Gregor | 47e1f7c | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 2800 | new (Context) ConditionalOperator(lessThanExpr, |
| 2801 | SourceLocation(), CE, |
| 2802 | SourceLocation(), STCE, returnType); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2803 | ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), CondExpr); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2804 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2805 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2806 | return ReplacingStmt; |
| 2807 | } |
| 2808 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2809 | Stmt *RewriteObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2810 | Stmt *ReplacingStmt = SynthMessageExpr(Exp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2811 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2812 | // Now do the actual rewrite. |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2813 | ReplaceStmt(Exp, ReplacingStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2814 | |
| 2815 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2816 | return ReplacingStmt; |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2817 | } |
| 2818 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2819 | // typedef struct objc_object Protocol; |
| 2820 | QualType RewriteObjC::getProtocolType() { |
| 2821 | if (!ProtocolTypeDecl) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2822 | TypeSourceInfo *TInfo |
| 2823 | = Context->getTrivialTypeSourceInfo(Context->getObjCIdType()); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2824 | ProtocolTypeDecl = TypedefDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2825 | SourceLocation(), |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2826 | &Context->Idents.get("Protocol"), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2827 | TInfo); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2828 | } |
| 2829 | return Context->getTypeDeclType(ProtocolTypeDecl); |
| 2830 | } |
| 2831 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2832 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2833 | /// a synthesized/forward data reference (to the protocol's metadata). |
| 2834 | /// The forward references (and metadata) are generated in |
| 2835 | /// RewriteObjC::HandleTranslationUnit(). |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2836 | Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2837 | std::string Name = "_OBJC_PROTOCOL_" + Exp->getProtocol()->getNameAsString(); |
| 2838 | IdentifierInfo *ID = &Context->Idents.get(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2839 | VarDecl *VD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Douglas Gregor | 0da76df | 2009-11-23 11:41:28 +0000 | [diff] [blame] | 2840 | ID, getProtocolType(), 0, VarDecl::Extern); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2841 | DeclRefExpr *DRE = new (Context) DeclRefExpr(VD, getProtocolType(), SourceLocation()); |
| 2842 | Expr *DerefExpr = new (Context) UnaryOperator(DRE, UnaryOperator::AddrOf, |
| 2843 | Context->getPointerType(DRE->getType()), |
| 2844 | SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2845 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, DerefExpr->getType(), |
| 2846 | CastExpr::CK_Unknown, |
| 2847 | DerefExpr); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2848 | ReplaceStmt(Exp, castExpr); |
| 2849 | ProtocolExprDecls.insert(Exp->getProtocol()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2850 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2851 | return castExpr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2852 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2853 | } |
| 2854 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2855 | bool RewriteObjC::BufferContainsPPDirectives(const char *startBuf, |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2856 | const char *endBuf) { |
| 2857 | while (startBuf < endBuf) { |
| 2858 | if (*startBuf == '#') { |
| 2859 | // Skip whitespace. |
| 2860 | for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf) |
| 2861 | ; |
| 2862 | if (!strncmp(startBuf, "if", strlen("if")) || |
| 2863 | !strncmp(startBuf, "ifdef", strlen("ifdef")) || |
| 2864 | !strncmp(startBuf, "ifndef", strlen("ifndef")) || |
| 2865 | !strncmp(startBuf, "define", strlen("define")) || |
| 2866 | !strncmp(startBuf, "undef", strlen("undef")) || |
| 2867 | !strncmp(startBuf, "else", strlen("else")) || |
| 2868 | !strncmp(startBuf, "elif", strlen("elif")) || |
| 2869 | !strncmp(startBuf, "endif", strlen("endif")) || |
| 2870 | !strncmp(startBuf, "pragma", strlen("pragma")) || |
| 2871 | !strncmp(startBuf, "include", strlen("include")) || |
| 2872 | !strncmp(startBuf, "import", strlen("import")) || |
| 2873 | !strncmp(startBuf, "include_next", strlen("include_next"))) |
| 2874 | return true; |
| 2875 | } |
| 2876 | startBuf++; |
| 2877 | } |
| 2878 | return false; |
| 2879 | } |
| 2880 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2881 | /// SynthesizeObjCInternalStruct - Rewrite one internal struct corresponding to |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2882 | /// an objective-c class with ivars. |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2883 | void RewriteObjC::SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2884 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2885 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2886 | assert(CDecl->getNameAsCString() && |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2887 | "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 2888 | // Do not synthesize more than once. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2889 | if (ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 2890 | return; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2891 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 2892 | int NumIvars = CDecl->ivar_size(); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2893 | SourceLocation LocStart = CDecl->getLocStart(); |
| 2894 | SourceLocation LocEnd = CDecl->getLocEnd(); |
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 | const char *startBuf = SM->getCharacterData(LocStart); |
| 2897 | const char *endBuf = SM->getCharacterData(LocEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2898 | |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2899 | // If no ivars and no root or if its root, directly or indirectly, |
| 2900 | // 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] | 2901 | if ((CDecl->isForwardDecl() || NumIvars == 0) && |
| 2902 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 2903 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 2904 | ReplaceText(LocStart, endBuf-startBuf, Result.c_str(), Result.size()); |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2905 | return; |
| 2906 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2907 | |
| 2908 | // FIXME: This has potential of causing problem. If |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2909 | // SynthesizeObjCInternalStruct is ever called recursively. |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2910 | Result += "\nstruct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2911 | Result += CDecl->getNameAsString(); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 2912 | if (LangOpts.Microsoft) |
| 2913 | Result += "_IMPL"; |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 2914 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2915 | if (NumIvars > 0) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2916 | const char *cursor = strchr(startBuf, '{'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2917 | assert((cursor && endBuf) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2918 | && "SynthesizeObjCInternalStruct - malformed @interface"); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2919 | // If the buffer contains preprocessor directives, we do more fine-grained |
| 2920 | // rewrites. This is intended to fix code that looks like (which occurs in |
| 2921 | // NSURL.h, for example): |
| 2922 | // |
| 2923 | // #ifdef XYZ |
| 2924 | // @interface Foo : NSObject |
| 2925 | // #else |
| 2926 | // @interface FooBar : NSObject |
| 2927 | // #endif |
| 2928 | // { |
| 2929 | // int i; |
| 2930 | // } |
| 2931 | // @end |
| 2932 | // |
| 2933 | // This clause is segregated to avoid breaking the common case. |
| 2934 | if (BufferContainsPPDirectives(startBuf, cursor)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2935 | SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() : |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2936 | CDecl->getClassLoc(); |
| 2937 | const char *endHeader = SM->getCharacterData(L); |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 2938 | endHeader += Lexer::MeasureTokenLength(L, *SM, LangOpts); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2939 | |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 2940 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2941 | // advance to the end of the referenced protocols. |
| 2942 | while (endHeader < cursor && *endHeader != '>') endHeader++; |
| 2943 | endHeader++; |
| 2944 | } |
| 2945 | // rewrite the original header |
| 2946 | ReplaceText(LocStart, endHeader-startBuf, Result.c_str(), Result.size()); |
| 2947 | } else { |
| 2948 | // rewrite the original header *without* disturbing the '{' |
Steve Naroff | 17c8778 | 2009-12-04 21:36:32 +0000 | [diff] [blame] | 2949 | ReplaceText(LocStart, cursor-startBuf, Result.c_str(), Result.size()); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2950 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2951 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2952 | Result = "\n struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2953 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 39bbd9f | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 2954 | Result += "_IMPL "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2955 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 819173c | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 2956 | Result += "_IVARS;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2957 | |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2958 | // insert the super class structure definition. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2959 | SourceLocation OnePastCurly = |
| 2960 | LocStart.getFileLocWithOffset(cursor-startBuf+1); |
| 2961 | InsertText(OnePastCurly, Result.c_str(), Result.size()); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2962 | } |
| 2963 | cursor++; // past '{' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2964 | |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2965 | // Now comment out any visibility specifiers. |
| 2966 | while (cursor < endBuf) { |
| 2967 | if (*cursor == '@') { |
| 2968 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | df6a51b | 2007-11-14 22:57:51 +0000 | [diff] [blame] | 2969 | // Skip whitespace. |
| 2970 | for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor) |
| 2971 | /*scan*/; |
| 2972 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2973 | // FIXME: presence of @public, etc. inside comment results in |
| 2974 | // this transformation as well, which is still correct c-code. |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2975 | if (!strncmp(cursor, "public", strlen("public")) || |
| 2976 | !strncmp(cursor, "private", strlen("private")) || |
Steve Naroff | c5e3277 | 2008-04-04 22:34:24 +0000 | [diff] [blame] | 2977 | !strncmp(cursor, "package", strlen("package")) || |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2978 | !strncmp(cursor, "protected", strlen("protected"))) |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2979 | InsertText(atLoc, "// ", 3); |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2980 | } |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2981 | // FIXME: If there are cases where '<' is used in ivar declaration part |
| 2982 | // of user code, then scan the ivar list and use needToScanForQualifiers |
| 2983 | // for type checking. |
| 2984 | else if (*cursor == '<') { |
| 2985 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2986 | InsertText(atLoc, "/* ", 3); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2987 | cursor = strchr(cursor, '>'); |
| 2988 | cursor++; |
| 2989 | atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2990 | InsertText(atLoc, " */", 3); |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 2991 | } else if (*cursor == '^') { // rewrite block specifier. |
| 2992 | SourceLocation caretLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
| 2993 | ReplaceText(caretLoc, 1, "*", 1); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2994 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2995 | cursor++; |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2996 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2997 | // Don't forget to add a ';'!! |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2998 | InsertText(LocEnd.getFileLocWithOffset(1), ";", 1); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2999 | } else { // we don't have any instance variables - insert super struct. |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3000 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3001 | Result += " {\n struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3002 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 39bbd9f | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 3003 | Result += "_IMPL "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3004 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 819173c | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 3005 | Result += "_IVARS;\n};\n"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 3006 | ReplaceText(LocStart, endBuf-startBuf, Result.c_str(), Result.size()); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3007 | } |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3008 | // Mark this struct as having been generated. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3009 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
Steve Naroff | fbfe825 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 3010 | assert(false && "struct already synthesize- SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3011 | } |
| 3012 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3013 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3014 | /// class methods. |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3015 | template<typename MethodIterator> |
| 3016 | void RewriteObjC::RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 3017 | MethodIterator MethodEnd, |
Fariborz Jahanian | 8e991ba | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 3018 | bool IsInstanceMethod, |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3019 | const char *prefix, |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 3020 | const char *ClassName, |
| 3021 | std::string &Result) { |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3022 | if (MethodBegin == MethodEnd) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3023 | |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3024 | if (!objc_impl_method) { |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3025 | /* struct _objc_method { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3026 | SEL _cmd; |
| 3027 | char *method_types; |
| 3028 | void *_imp; |
| 3029 | } |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3030 | */ |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 3031 | Result += "\nstruct _objc_method {\n"; |
| 3032 | Result += "\tSEL _cmd;\n"; |
| 3033 | Result += "\tchar *method_types;\n"; |
| 3034 | Result += "\tvoid *_imp;\n"; |
| 3035 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3036 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3037 | objc_impl_method = true; |
Fariborz Jahanian | 776d6ff | 2007-10-19 00:36:46 +0000 | [diff] [blame] | 3038 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3039 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3040 | // Build _objc_method_list for class's methods if needed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3041 | |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3042 | /* struct { |
| 3043 | struct _objc_method_list *next_method; |
| 3044 | int method_count; |
| 3045 | struct _objc_method method_list[]; |
| 3046 | } |
| 3047 | */ |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3048 | unsigned NumMethods = std::distance(MethodBegin, MethodEnd); |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3049 | Result += "\nstatic struct {\n"; |
| 3050 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 3051 | Result += "\tint method_count;\n"; |
| 3052 | Result += "\tstruct _objc_method method_list["; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3053 | Result += utostr(NumMethods); |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3054 | Result += "];\n} _OBJC_"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3055 | Result += prefix; |
| 3056 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 3057 | Result += "_METHODS_"; |
| 3058 | Result += ClassName; |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3059 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3060 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 3061 | Result += "_meth\")))= "; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3062 | Result += "{\n\t0, " + utostr(NumMethods) + "\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3063 | |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3064 | Result += "\t,{{(SEL)\""; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3065 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3066 | std::string MethodTypeString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3067 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3068 | Result += "\", \""; |
| 3069 | Result += MethodTypeString; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3070 | Result += "\", (void *)"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3071 | Result += MethodInternalNames[*MethodBegin]; |
| 3072 | Result += "}\n"; |
| 3073 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 3074 | Result += "\t ,{(SEL)\""; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3075 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3076 | std::string MethodTypeString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3077 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3078 | Result += "\", \""; |
| 3079 | Result += MethodTypeString; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3080 | Result += "\", (void *)"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3081 | Result += MethodInternalNames[*MethodBegin]; |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 3082 | Result += "}\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3083 | } |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3084 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3085 | } |
| 3086 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3087 | /// RewriteObjCProtocolMetaData - Rewrite protocols meta-data. |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 3088 | void RewriteObjC:: |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3089 | RewriteObjCProtocolMetaData(ObjCProtocolDecl *PDecl, const char *prefix, |
| 3090 | const char *ClassName, std::string &Result) { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3091 | static bool objc_protocol_methods = false; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3092 | |
| 3093 | // Output struct protocol_methods holder of method selector and type. |
| 3094 | if (!objc_protocol_methods && !PDecl->isForwardDecl()) { |
| 3095 | /* struct protocol_methods { |
| 3096 | SEL _cmd; |
| 3097 | char *method_types; |
| 3098 | } |
| 3099 | */ |
| 3100 | Result += "\nstruct _protocol_methods {\n"; |
| 3101 | Result += "\tstruct objc_selector *_cmd;\n"; |
| 3102 | Result += "\tchar *method_types;\n"; |
| 3103 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3104 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3105 | objc_protocol_methods = true; |
| 3106 | } |
| 3107 | // Do not synthesize the protocol more than once. |
| 3108 | if (ObjCSynthesizedProtocols.count(PDecl)) |
| 3109 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3110 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3111 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 3112 | unsigned NumMethods = std::distance(PDecl->instmeth_begin(), |
| 3113 | PDecl->instmeth_end()); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3114 | /* struct _objc_protocol_method_list { |
| 3115 | int protocol_method_count; |
| 3116 | struct protocol_methods protocols[]; |
| 3117 | } |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3118 | */ |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3119 | Result += "\nstatic struct {\n"; |
| 3120 | Result += "\tint protocol_method_count;\n"; |
| 3121 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 3122 | Result += utostr(NumMethods); |
| 3123 | Result += "];\n} _OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 3124 | Result += PDecl->getNameAsString(); |
| 3125 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= " |
| 3126 | "{\n\t" + utostr(NumMethods) + "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3127 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3128 | // Output instance methods declared in this protocol. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3129 | for (ObjCProtocolDecl::instmeth_iterator |
| 3130 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3131 | I != E; ++I) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3132 | if (I == PDecl->instmeth_begin()) |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3133 | Result += "\t ,{{(struct objc_selector *)\""; |
| 3134 | else |
| 3135 | Result += "\t ,{(struct objc_selector *)\""; |
| 3136 | Result += (*I)->getSelector().getAsString().c_str(); |
| 3137 | std::string MethodTypeString; |
| 3138 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 3139 | Result += "\", \""; |
| 3140 | Result += MethodTypeString; |
| 3141 | Result += "\"}\n"; |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3142 | } |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3143 | Result += "\t }\n};\n"; |
| 3144 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3145 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3146 | // Output class methods declared in this protocol. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3147 | unsigned NumMethods = std::distance(PDecl->classmeth_begin(), |
| 3148 | PDecl->classmeth_end()); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3149 | if (NumMethods > 0) { |
| 3150 | /* struct _objc_protocol_method_list { |
| 3151 | int protocol_method_count; |
| 3152 | struct protocol_methods protocols[]; |
| 3153 | } |
| 3154 | */ |
| 3155 | Result += "\nstatic struct {\n"; |
| 3156 | Result += "\tint protocol_method_count;\n"; |
| 3157 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 3158 | Result += utostr(NumMethods); |
| 3159 | Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_"; |
| 3160 | Result += PDecl->getNameAsString(); |
| 3161 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 3162 | "{\n\t"; |
| 3163 | Result += utostr(NumMethods); |
| 3164 | Result += "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3165 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3166 | // Output instance methods declared in this protocol. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3167 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3168 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3169 | I != E; ++I) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3170 | if (I == PDecl->classmeth_begin()) |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3171 | Result += "\t ,{{(struct objc_selector *)\""; |
| 3172 | else |
| 3173 | Result += "\t ,{(struct objc_selector *)\""; |
| 3174 | Result += (*I)->getSelector().getAsString().c_str(); |
| 3175 | std::string MethodTypeString; |
| 3176 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 3177 | Result += "\", \""; |
| 3178 | Result += MethodTypeString; |
| 3179 | Result += "\"}\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3180 | } |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3181 | Result += "\t }\n};\n"; |
| 3182 | } |
| 3183 | |
| 3184 | // Output: |
| 3185 | /* struct _objc_protocol { |
| 3186 | // Objective-C 1.0 extensions |
| 3187 | struct _objc_protocol_extension *isa; |
| 3188 | char *protocol_name; |
| 3189 | struct _objc_protocol **protocol_list; |
| 3190 | struct _objc_protocol_method_list *instance_methods; |
| 3191 | struct _objc_protocol_method_list *class_methods; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3192 | }; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3193 | */ |
| 3194 | static bool objc_protocol = false; |
| 3195 | if (!objc_protocol) { |
| 3196 | Result += "\nstruct _objc_protocol {\n"; |
| 3197 | Result += "\tstruct _objc_protocol_extension *isa;\n"; |
| 3198 | Result += "\tchar *protocol_name;\n"; |
| 3199 | Result += "\tstruct _objc_protocol **protocol_list;\n"; |
| 3200 | Result += "\tstruct _objc_protocol_method_list *instance_methods;\n"; |
| 3201 | Result += "\tstruct _objc_protocol_method_list *class_methods;\n"; |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3202 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3203 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3204 | objc_protocol = true; |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3205 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3206 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3207 | Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_"; |
| 3208 | Result += PDecl->getNameAsString(); |
| 3209 | Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= " |
| 3210 | "{\n\t0, \""; |
| 3211 | Result += PDecl->getNameAsString(); |
| 3212 | Result += "\", 0, "; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3213 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3214 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 3215 | Result += PDecl->getNameAsString(); |
| 3216 | Result += ", "; |
| 3217 | } |
| 3218 | else |
| 3219 | Result += "0, "; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3220 | if (PDecl->classmeth_begin() != PDecl->classmeth_end()) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3221 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
| 3222 | Result += PDecl->getNameAsString(); |
| 3223 | Result += "\n"; |
| 3224 | } |
| 3225 | else |
| 3226 | Result += "0\n"; |
| 3227 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3228 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3229 | // Mark this protocol as having been generated. |
| 3230 | if (!ObjCSynthesizedProtocols.insert(PDecl)) |
| 3231 | assert(false && "protocol already synthesized"); |
| 3232 | |
| 3233 | } |
| 3234 | |
| 3235 | void RewriteObjC:: |
| 3236 | RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Protocols, |
| 3237 | const char *prefix, const char *ClassName, |
| 3238 | std::string &Result) { |
| 3239 | if (Protocols.empty()) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3240 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3241 | for (unsigned i = 0; i != Protocols.size(); i++) |
| 3242 | RewriteObjCProtocolMetaData(Protocols[i], prefix, ClassName, Result); |
| 3243 | |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3244 | // Output the top lovel protocol meta-data for the class. |
| 3245 | /* struct _objc_protocol_list { |
| 3246 | struct _objc_protocol_list *next; |
| 3247 | int protocol_count; |
| 3248 | struct _objc_protocol *class_protocols[]; |
| 3249 | } |
| 3250 | */ |
| 3251 | Result += "\nstatic struct {\n"; |
| 3252 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 3253 | Result += "\tint protocol_count;\n"; |
| 3254 | Result += "\tstruct _objc_protocol *class_protocols["; |
| 3255 | Result += utostr(Protocols.size()); |
| 3256 | Result += "];\n} _OBJC_"; |
| 3257 | Result += prefix; |
| 3258 | Result += "_PROTOCOLS_"; |
| 3259 | Result += ClassName; |
| 3260 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 3261 | "{\n\t0, "; |
| 3262 | Result += utostr(Protocols.size()); |
| 3263 | Result += "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3264 | |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3265 | Result += "\t,{&_OBJC_PROTOCOL_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3266 | Result += Protocols[0]->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3267 | Result += " \n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3268 | |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3269 | for (unsigned i = 1; i != Protocols.size(); i++) { |
| 3270 | Result += "\t ,&_OBJC_PROTOCOL_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3271 | Result += Protocols[i]->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3272 | Result += "\n"; |
| 3273 | } |
| 3274 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3275 | } |
| 3276 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3277 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3278 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3279 | /// implementation. |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3280 | void RewriteObjC::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3281 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3282 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3283 | // Find category declaration for this implementation. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3284 | ObjCCategoryDecl *CDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3285 | for (CDecl = ClassDecl->getCategoryList(); CDecl; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3286 | CDecl = CDecl->getNextClassCategory()) |
| 3287 | if (CDecl->getIdentifier() == IDecl->getIdentifier()) |
| 3288 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3289 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3290 | std::string FullCategoryName = ClassDecl->getNameAsString(); |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3291 | FullCategoryName += '_'; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3292 | FullCategoryName += IDecl->getNameAsString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3293 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3294 | // Build _objc_method_list for class's instance methods if needed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3295 | llvm::SmallVector<ObjCMethodDecl *, 32> |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3296 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3297 | |
| 3298 | // If any of our property implementations have associated getters or |
| 3299 | // setters, produce metadata for them as well. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3300 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 3301 | PropEnd = IDecl->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3302 | Prop != PropEnd; ++Prop) { |
| 3303 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 3304 | continue; |
| 3305 | if (!(*Prop)->getPropertyIvarDecl()) |
| 3306 | continue; |
| 3307 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 3308 | if (!PD) |
| 3309 | continue; |
| 3310 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 3311 | InstanceMethods.push_back(Getter); |
| 3312 | if (PD->isReadOnly()) |
| 3313 | continue; |
| 3314 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 3315 | InstanceMethods.push_back(Setter); |
| 3316 | } |
| 3317 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3318 | true, "CATEGORY_", FullCategoryName.c_str(), |
| 3319 | Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3320 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3321 | // Build _objc_method_list for class's class methods if needed |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3322 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3323 | false, "CATEGORY_", FullCategoryName.c_str(), |
| 3324 | Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3325 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3326 | // Protocols referenced in class declaration? |
Fariborz Jahanian | bac97d4 | 2007-11-13 22:09:49 +0000 | [diff] [blame] | 3327 | // Null CDecl is case of a category implementation with no category interface |
| 3328 | if (CDecl) |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3329 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), "CATEGORY", |
| 3330 | FullCategoryName.c_str(), Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3331 | /* struct _objc_category { |
| 3332 | char *category_name; |
| 3333 | char *class_name; |
| 3334 | struct _objc_method_list *instance_methods; |
| 3335 | struct _objc_method_list *class_methods; |
| 3336 | struct _objc_protocol_list *protocols; |
| 3337 | // Objective-C 1.0 extensions |
| 3338 | uint32_t size; // sizeof (struct _objc_category) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3339 | struct _objc_property_list *instance_properties; // category's own |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3340 | // @property decl. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3341 | }; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3342 | */ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3343 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3344 | static bool objc_category = false; |
| 3345 | if (!objc_category) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3346 | Result += "\nstruct _objc_category {\n"; |
| 3347 | Result += "\tchar *category_name;\n"; |
| 3348 | Result += "\tchar *class_name;\n"; |
| 3349 | Result += "\tstruct _objc_method_list *instance_methods;\n"; |
| 3350 | Result += "\tstruct _objc_method_list *class_methods;\n"; |
| 3351 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3352 | Result += "\tunsigned int size;\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3353 | Result += "\tstruct _objc_property_list *instance_properties;\n"; |
| 3354 | Result += "};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3355 | objc_category = true; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3356 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3357 | Result += "\nstatic struct _objc_category _OBJC_CATEGORY_"; |
| 3358 | Result += FullCategoryName; |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3359 | Result += " __attribute__ ((used, section (\"__OBJC, __category\")))= {\n\t\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3360 | Result += IDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3361 | Result += "\"\n\t, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3362 | Result += ClassDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3363 | Result += "\"\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3364 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3365 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3366 | Result += "\t, (struct _objc_method_list *)" |
| 3367 | "&_OBJC_CATEGORY_INSTANCE_METHODS_"; |
| 3368 | Result += FullCategoryName; |
| 3369 | Result += "\n"; |
| 3370 | } |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3371 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3372 | Result += "\t, 0\n"; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3373 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3374 | Result += "\t, (struct _objc_method_list *)" |
| 3375 | "&_OBJC_CATEGORY_CLASS_METHODS_"; |
| 3376 | Result += FullCategoryName; |
| 3377 | Result += "\n"; |
| 3378 | } |
| 3379 | else |
| 3380 | Result += "\t, 0\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3381 | |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3382 | if (CDecl && CDecl->protocol_begin() != CDecl->protocol_end()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3383 | Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3384 | Result += FullCategoryName; |
| 3385 | Result += "\n"; |
| 3386 | } |
| 3387 | else |
| 3388 | Result += "\t, 0\n"; |
| 3389 | Result += "\t, sizeof(struct _objc_category), 0\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3390 | } |
| 3391 | |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3392 | /// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of |
| 3393 | /// ivar offset. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3394 | void RewriteObjC::SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl, |
| 3395 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3396 | std::string &Result) { |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3397 | if (ivar->isBitField()) { |
| 3398 | // FIXME: The hack below doesn't work for bitfields. For now, we simply |
| 3399 | // place all bitfields at offset 0. |
| 3400 | Result += "0"; |
| 3401 | } else { |
| 3402 | Result += "__OFFSETOFIVAR__(struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3403 | Result += IDecl->getNameAsString(); |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3404 | if (LangOpts.Microsoft) |
| 3405 | Result += "_IMPL"; |
| 3406 | Result += ", "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3407 | Result += ivar->getNameAsString(); |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3408 | Result += ")"; |
| 3409 | } |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3410 | } |
| 3411 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3412 | //===----------------------------------------------------------------------===// |
| 3413 | // Meta Data Emission |
| 3414 | //===----------------------------------------------------------------------===// |
| 3415 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3416 | void RewriteObjC::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3417 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3418 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3419 | |
Fariborz Jahanian | ebe668f | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3420 | // Explictly declared @interface's are already synthesized. |
Steve Naroff | 33feeb0 | 2009-04-20 20:09:33 +0000 | [diff] [blame] | 3421 | if (CDecl->isImplicitInterfaceDecl()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3422 | // FIXME: Implementation of a class with no @interface (legacy) doese not |
Fariborz Jahanian | ebe668f | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3423 | // produce correct synthesis as yet. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3424 | SynthesizeObjCInternalStruct(CDecl, Result); |
Fariborz Jahanian | ebe668f | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3425 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3426 | |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3427 | // Build _objc_ivar_list metadata for classes ivars if needed |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3428 | unsigned NumIvars = !IDecl->ivar_empty() |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3429 | ? IDecl->ivar_size() |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3430 | : (CDecl ? CDecl->ivar_size() : 0); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3431 | if (NumIvars > 0) { |
| 3432 | static bool objc_ivar = false; |
| 3433 | if (!objc_ivar) { |
| 3434 | /* struct _objc_ivar { |
| 3435 | char *ivar_name; |
| 3436 | char *ivar_type; |
| 3437 | int ivar_offset; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3438 | }; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3439 | */ |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3440 | Result += "\nstruct _objc_ivar {\n"; |
| 3441 | Result += "\tchar *ivar_name;\n"; |
| 3442 | Result += "\tchar *ivar_type;\n"; |
| 3443 | Result += "\tint ivar_offset;\n"; |
| 3444 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3445 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3446 | objc_ivar = true; |
| 3447 | } |
| 3448 | |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3449 | /* struct { |
| 3450 | int ivar_count; |
| 3451 | struct _objc_ivar ivar_list[nIvars]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3452 | }; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3453 | */ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3454 | Result += "\nstatic struct {\n"; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3455 | Result += "\tint ivar_count;\n"; |
| 3456 | Result += "\tstruct _objc_ivar ivar_list["; |
| 3457 | Result += utostr(NumIvars); |
| 3458 | Result += "];\n} _OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3459 | Result += IDecl->getNameAsString(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3460 | Result += " __attribute__ ((used, section (\"__OBJC, __instance_vars\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3461 | "{\n\t"; |
| 3462 | Result += utostr(NumIvars); |
| 3463 | Result += "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3464 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3465 | ObjCInterfaceDecl::ivar_iterator IVI, IVE; |
Douglas Gregor | 8f36aba | 2009-04-23 03:23:08 +0000 | [diff] [blame] | 3466 | llvm::SmallVector<ObjCIvarDecl *, 8> IVars; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3467 | if (!IDecl->ivar_empty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3468 | for (ObjCImplementationDecl::ivar_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3469 | IV = IDecl->ivar_begin(), IVEnd = IDecl->ivar_end(); |
Douglas Gregor | 8f36aba | 2009-04-23 03:23:08 +0000 | [diff] [blame] | 3470 | IV != IVEnd; ++IV) |
| 3471 | IVars.push_back(*IV); |
| 3472 | IVI = IVars.begin(); |
| 3473 | IVE = IVars.end(); |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3474 | } else { |
| 3475 | IVI = CDecl->ivar_begin(); |
| 3476 | IVE = CDecl->ivar_end(); |
| 3477 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3478 | Result += "\t,{{\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3479 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3480 | Result += "\", \""; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3481 | std::string TmpString, StrEncoding; |
| 3482 | Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI); |
| 3483 | QuoteDoublequotes(TmpString, StrEncoding); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3484 | Result += StrEncoding; |
| 3485 | Result += "\", "; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3486 | SynthesizeIvarOffsetComputation(IDecl, *IVI, Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3487 | Result += "}\n"; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3488 | for (++IVI; IVI != IVE; ++IVI) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3489 | Result += "\t ,{\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3490 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3491 | Result += "\", \""; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3492 | std::string TmpString, StrEncoding; |
| 3493 | Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI); |
| 3494 | QuoteDoublequotes(TmpString, StrEncoding); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3495 | Result += StrEncoding; |
| 3496 | Result += "\", "; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3497 | SynthesizeIvarOffsetComputation(IDecl, (*IVI), Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3498 | Result += "}\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3499 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3500 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3501 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3502 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3503 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3504 | // Build _objc_method_list for class's instance methods if needed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3505 | llvm::SmallVector<ObjCMethodDecl *, 32> |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3506 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3507 | |
| 3508 | // If any of our property implementations have associated getters or |
| 3509 | // setters, produce metadata for them as well. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3510 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 3511 | PropEnd = IDecl->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3512 | Prop != PropEnd; ++Prop) { |
| 3513 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 3514 | continue; |
| 3515 | if (!(*Prop)->getPropertyIvarDecl()) |
| 3516 | continue; |
| 3517 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 3518 | if (!PD) |
| 3519 | continue; |
| 3520 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 3521 | InstanceMethods.push_back(Getter); |
| 3522 | if (PD->isReadOnly()) |
| 3523 | continue; |
| 3524 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 3525 | InstanceMethods.push_back(Setter); |
| 3526 | } |
| 3527 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3528 | true, "", IDecl->getNameAsCString(), Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3529 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3530 | // Build _objc_method_list for class's class methods if needed |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3531 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3532 | false, "", IDecl->getNameAsCString(), Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3533 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3534 | // Protocols referenced in class declaration? |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3535 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), |
| 3536 | "CLASS", CDecl->getNameAsCString(), Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3537 | |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3538 | // Declaration of class/meta-class metadata |
| 3539 | /* struct _objc_class { |
| 3540 | struct _objc_class *isa; // or const char *root_class_name when metadata |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3541 | const char *super_class_name; |
| 3542 | char *name; |
| 3543 | long version; |
| 3544 | long info; |
| 3545 | long instance_size; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3546 | struct _objc_ivar_list *ivars; |
| 3547 | struct _objc_method_list *methods; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3548 | struct objc_cache *cache; |
| 3549 | struct objc_protocol_list *protocols; |
| 3550 | const char *ivar_layout; |
| 3551 | struct _objc_class_ext *ext; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3552 | }; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3553 | */ |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3554 | static bool objc_class = false; |
| 3555 | if (!objc_class) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3556 | Result += "\nstruct _objc_class {\n"; |
| 3557 | Result += "\tstruct _objc_class *isa;\n"; |
| 3558 | Result += "\tconst char *super_class_name;\n"; |
| 3559 | Result += "\tchar *name;\n"; |
| 3560 | Result += "\tlong version;\n"; |
| 3561 | Result += "\tlong info;\n"; |
| 3562 | Result += "\tlong instance_size;\n"; |
| 3563 | Result += "\tstruct _objc_ivar_list *ivars;\n"; |
| 3564 | Result += "\tstruct _objc_method_list *methods;\n"; |
| 3565 | Result += "\tstruct objc_cache *cache;\n"; |
| 3566 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 3567 | Result += "\tconst char *ivar_layout;\n"; |
| 3568 | Result += "\tstruct _objc_class_ext *ext;\n"; |
| 3569 | Result += "};\n"; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3570 | objc_class = true; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3571 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3572 | |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3573 | // Meta-class metadata generation. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3574 | ObjCInterfaceDecl *RootClass = 0; |
| 3575 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3576 | while (SuperClass) { |
| 3577 | RootClass = SuperClass; |
| 3578 | SuperClass = SuperClass->getSuperClass(); |
| 3579 | } |
| 3580 | SuperClass = CDecl->getSuperClass(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3581 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3582 | Result += "\nstatic struct _objc_class _OBJC_METACLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3583 | Result += CDecl->getNameAsString(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3584 | Result += " __attribute__ ((used, section (\"__OBJC, __meta_class\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3585 | "{\n\t(struct _objc_class *)\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3586 | Result += (RootClass ? RootClass->getNameAsString() : CDecl->getNameAsString()); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3587 | Result += "\""; |
| 3588 | |
| 3589 | if (SuperClass) { |
| 3590 | Result += ", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3591 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3592 | Result += "\", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3593 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3594 | Result += "\""; |
| 3595 | } |
| 3596 | else { |
| 3597 | Result += ", 0, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3598 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3599 | Result += "\""; |
| 3600 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3601 | // 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] | 3602 | // 'info' field is initialized to CLS_META(2) for metaclass |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3603 | Result += ", 0,2, sizeof(struct _objc_class), 0"; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3604 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 3605 | Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3606 | Result += IDecl->getNameAsString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3607 | Result += "\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3608 | } |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3609 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3610 | Result += ", 0\n"; |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3611 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3612 | Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3613 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3614 | Result += ",0,0\n"; |
| 3615 | } |
Fariborz Jahanian | 454cb01 | 2007-10-24 20:54:23 +0000 | [diff] [blame] | 3616 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3617 | Result += "\t,0,0,0,0\n"; |
| 3618 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3619 | |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3620 | // class metadata generation. |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3621 | Result += "\nstatic struct _objc_class _OBJC_CLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3622 | Result += CDecl->getNameAsString(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3623 | Result += " __attribute__ ((used, section (\"__OBJC, __class\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3624 | "{\n\t&_OBJC_METACLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3625 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3626 | if (SuperClass) { |
| 3627 | Result += ", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3628 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3629 | Result += "\", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3630 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3631 | Result += "\""; |
| 3632 | } |
| 3633 | else { |
| 3634 | Result += ", 0, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3635 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3636 | Result += "\""; |
| 3637 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3638 | // 'info' field is initialized to CLS_CLASS(1) for class |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3639 | Result += ", 0,1"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3640 | if (!ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3641 | Result += ",0"; |
| 3642 | else { |
| 3643 | // class has size. Must synthesize its size. |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 3644 | Result += ",sizeof(struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3645 | Result += CDecl->getNameAsString(); |
Steve Naroff | ba9ac4e | 2008-03-10 23:33:22 +0000 | [diff] [blame] | 3646 | if (LangOpts.Microsoft) |
| 3647 | Result += "_IMPL"; |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3648 | Result += ")"; |
| 3649 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3650 | if (NumIvars > 0) { |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 3651 | Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3652 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3653 | Result += "\n\t"; |
| 3654 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3655 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3656 | Result += ",0"; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3657 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3658 | Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3659 | Result += CDecl->getNameAsString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3660 | Result += ", 0\n\t"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3661 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3662 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3663 | Result += ",0,0"; |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3664 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3665 | Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3666 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3667 | Result += ", 0,0\n"; |
| 3668 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3669 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3670 | Result += ",0,0,0\n"; |
| 3671 | Result += "};\n"; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3672 | } |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3673 | |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3674 | /// RewriteImplementations - This routine rewrites all method implementations |
| 3675 | /// and emits meta-data. |
| 3676 | |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3677 | void RewriteObjC::RewriteImplementations() { |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3678 | int ClsDefCount = ClassImplementation.size(); |
| 3679 | int CatDefCount = CategoryImplementation.size(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3680 | |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3681 | // Rewrite implemented methods |
| 3682 | for (int i = 0; i < ClsDefCount; i++) |
| 3683 | RewriteImplementationDecl(ClassImplementation[i]); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3684 | |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 3685 | for (int i = 0; i < CatDefCount; i++) |
| 3686 | RewriteImplementationDecl(CategoryImplementation[i]); |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3687 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3688 | |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3689 | void RewriteObjC::SynthesizeMetaDataIntoBuffer(std::string &Result) { |
| 3690 | int ClsDefCount = ClassImplementation.size(); |
| 3691 | int CatDefCount = CategoryImplementation.size(); |
| 3692 | |
Steve Naroff | 5df5b76 | 2008-05-07 21:23:49 +0000 | [diff] [blame] | 3693 | // This is needed for determining instance variable offsets. |
Fariborz Jahanian | c98cbb4 | 2010-01-07 18:31:42 +0000 | [diff] [blame] | 3694 | Result += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((long) &((TYPE *)0)->MEMBER)\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3695 | // For each implemented class, write out all its meta data. |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3696 | for (int i = 0; i < ClsDefCount; i++) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3697 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3698 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3699 | // For each implemented category, write out all its meta data. |
| 3700 | for (int i = 0; i < CatDefCount; i++) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3701 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3702 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3703 | // Write objc_symtab metadata |
| 3704 | /* |
| 3705 | struct _objc_symtab |
| 3706 | { |
| 3707 | long sel_ref_cnt; |
| 3708 | SEL *refs; |
| 3709 | short cls_def_cnt; |
| 3710 | short cat_def_cnt; |
| 3711 | void *defs[cls_def_cnt + cat_def_cnt]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3712 | }; |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3713 | */ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3714 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3715 | Result += "\nstruct _objc_symtab {\n"; |
| 3716 | Result += "\tlong sel_ref_cnt;\n"; |
| 3717 | Result += "\tSEL *refs;\n"; |
| 3718 | Result += "\tshort cls_def_cnt;\n"; |
| 3719 | Result += "\tshort cat_def_cnt;\n"; |
| 3720 | Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n"; |
| 3721 | Result += "};\n\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3722 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3723 | Result += "static struct _objc_symtab " |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3724 | "_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3725 | Result += "\t0, 0, " + utostr(ClsDefCount) |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3726 | + ", " + utostr(CatDefCount) + "\n"; |
| 3727 | for (int i = 0; i < ClsDefCount; i++) { |
| 3728 | Result += "\t,&_OBJC_CLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3729 | Result += ClassImplementation[i]->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3730 | Result += "\n"; |
| 3731 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3732 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3733 | for (int i = 0; i < CatDefCount; i++) { |
| 3734 | Result += "\t,&_OBJC_CATEGORY_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3735 | Result += CategoryImplementation[i]->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3736 | Result += "_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3737 | Result += CategoryImplementation[i]->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3738 | Result += "\n"; |
| 3739 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3740 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3741 | Result += "};\n\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3742 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3743 | // Write objc_module metadata |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3744 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3745 | /* |
| 3746 | struct _objc_module { |
| 3747 | long version; |
| 3748 | long size; |
| 3749 | const char *name; |
| 3750 | struct _objc_symtab *symtab; |
| 3751 | } |
| 3752 | */ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3753 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3754 | Result += "\nstruct _objc_module {\n"; |
| 3755 | Result += "\tlong version;\n"; |
| 3756 | Result += "\tlong size;\n"; |
| 3757 | Result += "\tconst char *name;\n"; |
| 3758 | Result += "\tstruct _objc_symtab *symtab;\n"; |
| 3759 | Result += "};\n\n"; |
| 3760 | Result += "static struct _objc_module " |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3761 | "_OBJC_MODULES __attribute__ ((used, section (\"__OBJC, __module_info\")))= {\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3762 | Result += "\t" + utostr(OBJC_ABI_VERSION) + |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3763 | ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3764 | Result += "};\n\n"; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3765 | |
| 3766 | if (LangOpts.Microsoft) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3767 | if (ProtocolExprDecls.size()) { |
| 3768 | Result += "#pragma section(\".objc_protocol$B\",long,read,write)\n"; |
| 3769 | Result += "#pragma data_seg(push, \".objc_protocol$B\")\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3770 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3771 | E = ProtocolExprDecls.end(); I != E; ++I) { |
| 3772 | Result += "static struct _objc_protocol *_POINTER_OBJC_PROTOCOL_"; |
| 3773 | Result += (*I)->getNameAsString(); |
| 3774 | Result += " = &_OBJC_PROTOCOL_"; |
| 3775 | Result += (*I)->getNameAsString(); |
| 3776 | Result += ";\n"; |
| 3777 | } |
| 3778 | Result += "#pragma data_seg(pop)\n\n"; |
| 3779 | } |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3780 | Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n"; |
Steve Naroff | 1919032 | 2008-05-07 00:06:16 +0000 | [diff] [blame] | 3781 | Result += "#pragma data_seg(push, \".objc_module_info$B\")\n"; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3782 | Result += "static struct _objc_module *_POINTER_OBJC_MODULES = "; |
| 3783 | Result += "&_OBJC_MODULES;\n"; |
| 3784 | Result += "#pragma data_seg(pop)\n\n"; |
| 3785 | } |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3786 | } |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 3787 | |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3788 | void RewriteObjC::RewriteByRefString(std::string &ResultStr, |
| 3789 | const std::string &Name, |
| 3790 | ValueDecl *VD) { |
| 3791 | assert(BlockByRefDeclNo.count(VD) && |
| 3792 | "RewriteByRefString: ByRef decl missing"); |
| 3793 | ResultStr += "struct __Block_byref_" + Name + |
| 3794 | "_" + utostr(BlockByRefDeclNo[VD]) ; |
| 3795 | } |
| 3796 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3797 | std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, |
| 3798 | const char *funcName, |
| 3799 | std::string Tag) { |
| 3800 | const FunctionType *AFT = CE->getFunctionType(); |
| 3801 | QualType RT = AFT->getResultType(); |
| 3802 | std::string StructRef = "struct " + Tag; |
| 3803 | std::string S = "static " + RT.getAsString() + " __" + |
| 3804 | funcName + "_" + "block_func_" + utostr(i); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 3805 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3806 | BlockDecl *BD = CE->getBlockDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3807 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3808 | if (isa<FunctionNoProtoType>(AFT)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3809 | // 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] | 3810 | // block (to reference imported block decl refs). |
| 3811 | S += "(" + StructRef + " *__cself)"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3812 | } else if (BD->param_empty()) { |
| 3813 | S += "(" + StructRef + " *__cself)"; |
| 3814 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3815 | const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3816 | assert(FT && "SynthesizeBlockFunc: No function proto"); |
| 3817 | S += '('; |
| 3818 | // first add the implicit argument. |
| 3819 | S += StructRef + " *__cself, "; |
| 3820 | std::string ParamStr; |
| 3821 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 3822 | E = BD->param_end(); AI != E; ++AI) { |
| 3823 | if (AI != BD->param_begin()) S += ", "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3824 | ParamStr = (*AI)->getNameAsString(); |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 3825 | (*AI)->getType().getAsStringInternal(ParamStr, Context->PrintingPolicy); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3826 | S += ParamStr; |
| 3827 | } |
| 3828 | if (FT->isVariadic()) { |
| 3829 | if (!BD->param_empty()) S += ", "; |
| 3830 | S += "..."; |
| 3831 | } |
| 3832 | S += ')'; |
| 3833 | } |
| 3834 | S += " {\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3835 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3836 | // Create local declarations to avoid rewriting all closure decl ref exprs. |
| 3837 | // First, emit a declaration for all "by ref" decls. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3838 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3839 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3840 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3841 | std::string Name = (*I)->getNameAsString(); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3842 | std::string TypeString; |
| 3843 | RewriteByRefString(TypeString, Name, (*I)); |
| 3844 | TypeString += " *"; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 3845 | Name = TypeString + Name; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3846 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3847 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3848 | // Next, emit a declaration for all "by copy" declarations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3849 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3850 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3851 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3852 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3853 | // Handle nested closure invocation. For example: |
| 3854 | // |
| 3855 | // void (^myImportedClosure)(void); |
| 3856 | // myImportedClosure = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3857 | // |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3858 | // void (^anotherClosure)(void); |
| 3859 | // anotherClosure = ^(void) { |
| 3860 | // myImportedClosure(); // import and invoke the closure |
| 3861 | // }; |
| 3862 | // |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3863 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3864 | S += "struct __block_impl *"; |
| 3865 | else |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 3866 | (*I)->getType().getAsStringInternal(Name, Context->PrintingPolicy); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3867 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3868 | } |
| 3869 | std::string RewrittenStr = RewrittenBlockExprs[CE]; |
| 3870 | const char *cstr = RewrittenStr.c_str(); |
| 3871 | while (*cstr++ != '{') ; |
| 3872 | S += cstr; |
| 3873 | S += "\n"; |
| 3874 | return S; |
| 3875 | } |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 3876 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3877 | std::string RewriteObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
| 3878 | const char *funcName, |
| 3879 | std::string Tag) { |
| 3880 | std::string StructRef = "struct " + Tag; |
| 3881 | std::string S = "static void __"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3882 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3883 | S += funcName; |
| 3884 | S += "_block_copy_" + utostr(i); |
| 3885 | S += "(" + StructRef; |
| 3886 | S += "*dst, " + StructRef; |
| 3887 | S += "*src) {"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3888 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3889 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3890 | S += "_Block_object_assign((void*)&dst->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3891 | S += (*I)->getNameAsString(); |
Steve Naroff | 47a2422 | 2008-12-11 20:51:38 +0000 | [diff] [blame] | 3892 | S += ", (void*)src->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3893 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | d25d1b5 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 3894 | if (BlockByRefDecls.count((*I))) |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 3895 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | d25d1b5 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 3896 | else |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 3897 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3898 | } |
Fariborz Jahanian | d25d1b5 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 3899 | S += "}\n"; |
| 3900 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3901 | S += "\nstatic void __"; |
| 3902 | S += funcName; |
| 3903 | S += "_block_dispose_" + utostr(i); |
| 3904 | S += "(" + StructRef; |
| 3905 | S += "*src) {"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3906 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3907 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3908 | S += "_Block_object_dispose((void*)src->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3909 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | d25d1b5 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 3910 | if (BlockByRefDecls.count((*I))) |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 3911 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | d25d1b5 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 3912 | else |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 3913 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3914 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3915 | S += "}\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3916 | return S; |
| 3917 | } |
| 3918 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3919 | std::string RewriteObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 3920 | std::string Desc) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3921 | std::string S = "\nstruct " + Tag; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3922 | std::string Constructor = " " + Tag; |
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 | S += " {\n struct __block_impl impl;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3925 | S += " struct " + Desc; |
| 3926 | S += "* Desc;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3927 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3928 | Constructor += "(void *fp, "; // Invoke function pointer. |
| 3929 | Constructor += "struct " + Desc; // Descriptor pointer. |
| 3930 | Constructor += " *desc"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3931 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3932 | if (BlockDeclRefs.size()) { |
| 3933 | // Output all "by copy" declarations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3934 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3935 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3936 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3937 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3938 | std::string ArgName = "_" + FieldName; |
| 3939 | // Handle nested closure invocation. For example: |
| 3940 | // |
| 3941 | // void (^myImportedBlock)(void); |
| 3942 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3943 | // |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3944 | // void (^anotherBlock)(void); |
| 3945 | // anotherBlock = ^(void) { |
| 3946 | // myImportedBlock(); // import and invoke the closure |
| 3947 | // }; |
| 3948 | // |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3949 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3950 | S += "struct __block_impl *"; |
| 3951 | Constructor += ", void *" + ArgName; |
| 3952 | } else { |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 3953 | (*I)->getType().getAsStringInternal(FieldName, Context->PrintingPolicy); |
| 3954 | (*I)->getType().getAsStringInternal(ArgName, Context->PrintingPolicy); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3955 | Constructor += ", " + ArgName; |
| 3956 | } |
| 3957 | S += FieldName + ";\n"; |
| 3958 | } |
| 3959 | // Output all "by ref" declarations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3960 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3961 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3962 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3963 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3964 | std::string ArgName = "_" + FieldName; |
| 3965 | // Handle nested closure invocation. For example: |
| 3966 | // |
| 3967 | // void (^myImportedBlock)(void); |
| 3968 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3969 | // |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3970 | // void (^anotherBlock)(void); |
| 3971 | // anotherBlock = ^(void) { |
| 3972 | // myImportedBlock(); // import and invoke the closure |
| 3973 | // }; |
| 3974 | // |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3975 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3976 | S += "struct __block_impl *"; |
| 3977 | Constructor += ", void *" + ArgName; |
| 3978 | } else { |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3979 | std::string TypeString; |
| 3980 | RewriteByRefString(TypeString, FieldName, (*I)); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 3981 | TypeString += " *"; |
| 3982 | FieldName = TypeString + FieldName; |
| 3983 | ArgName = TypeString + ArgName; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3984 | Constructor += ", " + ArgName; |
| 3985 | } |
| 3986 | S += FieldName + "; // by ref\n"; |
| 3987 | } |
| 3988 | // Finish writing the constructor. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3989 | Constructor += ", int flags=0) {\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3990 | if (GlobalVarDecl) |
| 3991 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 3992 | else |
| 3993 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3994 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3995 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 3996 | Constructor += " Desc = desc;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3997 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3998 | // Initialize all "by copy" arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3999 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4000 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4001 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4002 | Constructor += " "; |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4003 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4004 | Constructor += Name + " = (struct __block_impl *)_"; |
| 4005 | else |
| 4006 | Constructor += Name + " = _"; |
| 4007 | Constructor += Name + ";\n"; |
| 4008 | } |
| 4009 | // Initialize all "by ref" arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4010 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4011 | E = BlockByRefDecls.end(); I != E; ++I) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4012 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4013 | Constructor += " "; |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4014 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4015 | Constructor += Name + " = (struct __block_impl *)_"; |
| 4016 | else |
| 4017 | Constructor += Name + " = _"; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4018 | Constructor += Name + "->__forwarding;\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4019 | } |
| 4020 | } else { |
| 4021 | // Finish writing the constructor. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4022 | Constructor += ", int flags=0) {\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4023 | if (GlobalVarDecl) |
| 4024 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 4025 | else |
| 4026 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4027 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 4028 | Constructor += " Desc = desc;\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4029 | } |
| 4030 | Constructor += " "; |
| 4031 | Constructor += "}\n"; |
| 4032 | S += Constructor; |
| 4033 | S += "};\n"; |
| 4034 | return S; |
| 4035 | } |
| 4036 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4037 | std::string RewriteObjC::SynthesizeBlockDescriptor(std::string DescTag, |
| 4038 | std::string ImplTag, int i, |
| 4039 | const char *FunName, |
| 4040 | unsigned hasCopy) { |
| 4041 | std::string S = "\nstatic struct " + DescTag; |
| 4042 | |
| 4043 | S += " {\n unsigned long reserved;\n"; |
| 4044 | S += " unsigned long Block_size;\n"; |
| 4045 | if (hasCopy) { |
Fariborz Jahanian | 4fcc4fd | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 4046 | S += " void (*copy)(struct "; |
| 4047 | S += ImplTag; S += "*, struct "; |
| 4048 | S += ImplTag; S += "*);\n"; |
| 4049 | |
| 4050 | S += " void (*dispose)(struct "; |
| 4051 | S += ImplTag; S += "*);\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4052 | } |
| 4053 | S += "} "; |
| 4054 | |
| 4055 | S += DescTag + "_DATA = { 0, sizeof(struct "; |
| 4056 | S += ImplTag + ")"; |
| 4057 | if (hasCopy) { |
| 4058 | S += ", __" + std::string(FunName) + "_block_copy_" + utostr(i); |
| 4059 | S += ", __" + std::string(FunName) + "_block_dispose_" + utostr(i); |
| 4060 | } |
| 4061 | S += "};\n"; |
| 4062 | return S; |
| 4063 | } |
| 4064 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4065 | void RewriteObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 4066 | const char *FunName) { |
| 4067 | // Insert declaration for the function in which block literal is used. |
Fariborz Jahanian | bf07012 | 2010-01-15 18:14:52 +0000 | [diff] [blame] | 4068 | if (CurFunctionDeclToDeclareForBlock && !Blocks.empty()) |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 4069 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4070 | // Insert closures that were part of the function. |
| 4071 | for (unsigned i = 0; i < Blocks.size(); i++) { |
| 4072 | |
| 4073 | CollectBlockDeclRefInfo(Blocks[i]); |
| 4074 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4075 | std::string ImplTag = "__" + std::string(FunName) + "_block_impl_" + utostr(i); |
| 4076 | std::string DescTag = "__" + std::string(FunName) + "_block_desc_" + utostr(i); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4077 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4078 | std::string CI = SynthesizeBlockImpl(Blocks[i], ImplTag, DescTag); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4079 | |
| 4080 | InsertText(FunLocStart, CI.c_str(), CI.size()); |
| 4081 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4082 | std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, ImplTag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4083 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4084 | InsertText(FunLocStart, CF.c_str(), CF.size()); |
| 4085 | |
| 4086 | if (ImportedBlockDecls.size()) { |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4087 | std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, ImplTag); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4088 | InsertText(FunLocStart, HF.c_str(), HF.size()); |
| 4089 | } |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4090 | std::string BD = SynthesizeBlockDescriptor(DescTag, ImplTag, i, FunName, |
| 4091 | ImportedBlockDecls.size() > 0); |
| 4092 | InsertText(FunLocStart, BD.c_str(), BD.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4093 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4094 | BlockDeclRefs.clear(); |
| 4095 | BlockByRefDecls.clear(); |
| 4096 | BlockByCopyDecls.clear(); |
| 4097 | BlockCallExprs.clear(); |
| 4098 | ImportedBlockDecls.clear(); |
| 4099 | } |
| 4100 | Blocks.clear(); |
| 4101 | RewrittenBlockExprs.clear(); |
| 4102 | } |
| 4103 | |
| 4104 | void RewriteObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { |
| 4105 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4106 | const char *FuncName = FD->getNameAsCString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4107 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4108 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 4109 | } |
| 4110 | |
| 4111 | void RewriteObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 4112 | //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n"); |
| 4113 | //SourceLocation FunLocStart = MD->getLocStart(); |
Fariborz Jahanian | 0e1c99a | 2010-01-29 01:55:49 +0000 | [diff] [blame] | 4114 | SourceLocation FunLocStart = MD->getLocStart(); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4115 | std::string FuncName = MD->getSelector().getAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4116 | // Convert colons to underscores. |
| 4117 | std::string::size_type loc = 0; |
| 4118 | while ((loc = FuncName.find(":", loc)) != std::string::npos) |
| 4119 | FuncName.replace(loc, 1, "_"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4120 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4121 | SynthesizeBlockLiterals(FunLocStart, FuncName.c_str()); |
| 4122 | } |
| 4123 | |
| 4124 | void RewriteObjC::GetBlockDeclRefExprs(Stmt *S) { |
| 4125 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4126 | CI != E; ++CI) |
| 4127 | if (*CI) { |
| 4128 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 4129 | GetBlockDeclRefExprs(CBE->getBody()); |
| 4130 | else |
| 4131 | GetBlockDeclRefExprs(*CI); |
| 4132 | } |
| 4133 | // Handle specific things. |
| 4134 | if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) |
| 4135 | // FIXME: Handle enums. |
| 4136 | if (!isa<FunctionDecl>(CDRE->getDecl())) |
| 4137 | BlockDeclRefs.push_back(CDRE); |
| 4138 | return; |
| 4139 | } |
| 4140 | |
| 4141 | void RewriteObjC::GetBlockCallExprs(Stmt *S) { |
| 4142 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4143 | CI != E; ++CI) |
| 4144 | if (*CI) { |
| 4145 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 4146 | GetBlockCallExprs(CBE->getBody()); |
| 4147 | else |
| 4148 | GetBlockCallExprs(*CI); |
| 4149 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4150 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4151 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
| 4152 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
| 4153 | BlockCallExprs[dyn_cast<BlockDeclRefExpr>(CE->getCallee())] = CE; |
| 4154 | } |
| 4155 | } |
| 4156 | return; |
| 4157 | } |
| 4158 | |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4159 | Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp, const Expr *BlockExp) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4160 | // Navigate to relevant type information. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4161 | const BlockPointerType *CPT = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4162 | |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4163 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BlockExp)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4164 | CPT = DRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4165 | } else if (const BlockDeclRefExpr *CDRE = |
| 4166 | dyn_cast<BlockDeclRefExpr>(BlockExp)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4167 | CPT = CDRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4168 | } else if (const MemberExpr *MExpr = dyn_cast<MemberExpr>(BlockExp)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4169 | CPT = MExpr->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4170 | } |
| 4171 | else if (const ParenExpr *PRE = dyn_cast<ParenExpr>(BlockExp)) { |
| 4172 | return SynthesizeBlockCall(Exp, PRE->getSubExpr()); |
| 4173 | } |
| 4174 | else if (const ImplicitCastExpr *IEXPR = dyn_cast<ImplicitCastExpr>(BlockExp)) |
| 4175 | CPT = IEXPR->getType()->getAs<BlockPointerType>(); |
| 4176 | else if (const ConditionalOperator *CEXPR = |
| 4177 | dyn_cast<ConditionalOperator>(BlockExp)) { |
| 4178 | Expr *LHSExp = CEXPR->getLHS(); |
| 4179 | Stmt *LHSStmt = SynthesizeBlockCall(Exp, LHSExp); |
| 4180 | Expr *RHSExp = CEXPR->getRHS(); |
| 4181 | Stmt *RHSStmt = SynthesizeBlockCall(Exp, RHSExp); |
| 4182 | Expr *CONDExp = CEXPR->getCond(); |
| 4183 | ConditionalOperator *CondExpr = |
| 4184 | new (Context) ConditionalOperator(CONDExp, |
| 4185 | SourceLocation(), cast<Expr>(LHSStmt), |
| 4186 | SourceLocation(), cast<Expr>(RHSStmt), |
| 4187 | Exp->getType()); |
| 4188 | return CondExpr; |
Fariborz Jahanian | e24b22b | 2009-12-18 01:15:21 +0000 | [diff] [blame] | 4189 | } else if (const ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(BlockExp)) { |
| 4190 | CPT = IRE->getType()->getAs<BlockPointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4191 | } else { |
| 4192 | assert(1 && "RewriteBlockClass: Bad type"); |
| 4193 | } |
| 4194 | assert(CPT && "RewriteBlockClass: Bad type"); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4195 | const FunctionType *FT = CPT->getPointeeType()->getAs<FunctionType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4196 | assert(FT && "RewriteBlockClass: Bad type"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4197 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4198 | // FTP will be null for closures that don't take arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4199 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4200 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
| 4201 | SourceLocation(), |
| 4202 | &Context->Idents.get("__block_impl")); |
| 4203 | QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD)); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4204 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4205 | // Generate a funky cast. |
| 4206 | llvm::SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4207 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4208 | // Push the block argument type. |
| 4209 | ArgTypes.push_back(PtrBlock); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4210 | if (FTP) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4211 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4212 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 4213 | QualType t = *I; |
| 4214 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4215 | if (isTopLevelBlockPointerType(t)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4216 | const BlockPointerType *BPT = t->getAs<BlockPointerType>(); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4217 | t = Context->getPointerType(BPT->getPointeeType()); |
| 4218 | } |
| 4219 | ArgTypes.push_back(t); |
| 4220 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4221 | } |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4222 | // Now do the pointer to function cast. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4223 | QualType PtrToFuncCastType = Context->getFunctionType(Exp->getType(), |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4224 | &ArgTypes[0], ArgTypes.size(), false/*no variadic*/, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4225 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4226 | PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4227 | |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4228 | CastExpr *BlkCast = NoTypeInfoCStyleCastExpr(Context, PtrBlock, |
| 4229 | CastExpr::CK_Unknown, |
| 4230 | const_cast<Expr*>(BlockExp)); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4231 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4232 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 4233 | BlkCast); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4234 | //PE->dump(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4235 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4236 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4237 | &Context->Idents.get("FuncPtr"), Context->VoidPtrTy, 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 4238 | /*BitWidth=*/0, /*Mutable=*/true); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4239 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
| 4240 | FD->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4241 | |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4242 | CastExpr *FunkCast = NoTypeInfoCStyleCastExpr(Context, PtrToFuncCastType, |
| 4243 | CastExpr::CK_Unknown, ME); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4244 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4245 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4246 | llvm::SmallVector<Expr*, 8> BlkExprs; |
| 4247 | // Add the implicit argument. |
| 4248 | BlkExprs.push_back(BlkCast); |
| 4249 | // Add the user arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4250 | for (CallExpr::arg_iterator I = Exp->arg_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4251 | E = Exp->arg_end(); I != E; ++I) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4252 | BlkExprs.push_back(*I); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4253 | } |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 4254 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &BlkExprs[0], |
| 4255 | BlkExprs.size(), |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4256 | Exp->getType(), SourceLocation()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4257 | return CE; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4258 | } |
| 4259 | |
| 4260 | void RewriteObjC::RewriteBlockCall(CallExpr *Exp) { |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4261 | Stmt *BlockCall = SynthesizeBlockCall(Exp, Exp->getCallee()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4262 | ReplaceStmt(Exp, BlockCall); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4263 | } |
| 4264 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4265 | // We need to return the rewritten expression to handle cases where the |
| 4266 | // BlockDeclRefExpr is embedded in another expression being rewritten. |
| 4267 | // For example: |
| 4268 | // |
| 4269 | // int main() { |
| 4270 | // __block Foo *f; |
| 4271 | // __block int i; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4272 | // |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4273 | // void (^myblock)() = ^() { |
| 4274 | // [f test]; // f is a BlockDeclRefExpr embedded in a message (which is being rewritten). |
| 4275 | // i = 77; |
| 4276 | // }; |
| 4277 | //} |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4278 | Stmt *RewriteObjC::RewriteBlockDeclRefExpr(Expr *DeclRefExp) { |
Fariborz Jahanian | bbf37e2 | 2009-12-23 19:26:34 +0000 | [diff] [blame] | 4279 | // Rewrite the byref variable into BYREFVAR->__forwarding->BYREFVAR |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4280 | // for each DeclRefExp where BYREFVAR is name of the variable. |
| 4281 | ValueDecl *VD; |
| 4282 | bool isArrow = true; |
| 4283 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(DeclRefExp)) |
| 4284 | VD = BDRE->getDecl(); |
| 4285 | else { |
| 4286 | VD = cast<DeclRefExpr>(DeclRefExp)->getDecl(); |
| 4287 | isArrow = false; |
| 4288 | } |
| 4289 | |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4290 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 4291 | &Context->Idents.get("__forwarding"), |
| 4292 | Context->VoidPtrTy, 0, |
| 4293 | /*BitWidth=*/0, /*Mutable=*/true); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4294 | MemberExpr *ME = new (Context) MemberExpr(DeclRefExp, isArrow, |
| 4295 | FD, SourceLocation(), |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4296 | FD->getType()); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4297 | |
| 4298 | const char *Name = VD->getNameAsCString(); |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4299 | FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 4300 | &Context->Idents.get(Name), |
| 4301 | Context->VoidPtrTy, 0, |
| 4302 | /*BitWidth=*/0, /*Mutable=*/true); |
| 4303 | ME = new (Context) MemberExpr(ME, true, FD, SourceLocation(), |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4304 | DeclRefExp->getType()); |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4305 | |
| 4306 | |
| 4307 | |
Steve Naroff | df8570d | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 4308 | // Need parens to enforce precedence. |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4309 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 4310 | ME); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4311 | ReplaceStmt(DeclRefExp, PE); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4312 | return PE; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4313 | } |
| 4314 | |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4315 | void RewriteObjC::RewriteCastExpr(CStyleCastExpr *CE) { |
| 4316 | SourceLocation LocStart = CE->getLParenLoc(); |
| 4317 | SourceLocation LocEnd = CE->getRParenLoc(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4318 | |
| 4319 | // Need to avoid trying to rewrite synthesized casts. |
| 4320 | if (LocStart.isInvalid()) |
| 4321 | return; |
Steve Naroff | 8f6ce57 | 2008-11-03 11:20:24 +0000 | [diff] [blame] | 4322 | // Need to avoid trying to rewrite casts contained in macros. |
| 4323 | if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) |
| 4324 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4325 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4326 | const char *startBuf = SM->getCharacterData(LocStart); |
| 4327 | const char *endBuf = SM->getCharacterData(LocEnd); |
Fariborz Jahanian | 1d4fca2 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4328 | QualType QT = CE->getType(); |
| 4329 | const Type* TypePtr = QT->getAs<Type>(); |
| 4330 | if (isa<TypeOfExprType>(TypePtr)) { |
| 4331 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 4332 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 4333 | std::string TypeAsString = "("; |
| 4334 | TypeAsString += QT.getAsString(); |
| 4335 | TypeAsString += ")"; |
| 4336 | ReplaceText(LocStart, endBuf-startBuf+1, |
| 4337 | TypeAsString.c_str(), TypeAsString.size()); |
| 4338 | return; |
| 4339 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4340 | // advance the location to startArgList. |
| 4341 | const char *argPtr = startBuf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4342 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4343 | while (*argPtr++ && (argPtr < endBuf)) { |
| 4344 | switch (*argPtr) { |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4345 | case '^': |
| 4346 | // Replace the '^' with '*'. |
| 4347 | LocStart = LocStart.getFileLocWithOffset(argPtr-startBuf); |
| 4348 | ReplaceText(LocStart, 1, "*", 1); |
| 4349 | break; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4350 | } |
| 4351 | } |
| 4352 | return; |
| 4353 | } |
| 4354 | |
| 4355 | void RewriteObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { |
| 4356 | SourceLocation DeclLoc = FD->getLocation(); |
| 4357 | unsigned parenCount = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4358 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4359 | // We have 1 or more arguments that have closure pointers. |
| 4360 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4361 | const char *startArgList = strchr(startBuf, '('); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4362 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4363 | assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); |
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 | parenCount++; |
| 4366 | // advance the location to startArgList. |
| 4367 | DeclLoc = DeclLoc.getFileLocWithOffset(startArgList-startBuf); |
| 4368 | assert((DeclLoc.isValid()) && "Invalid DeclLoc"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4369 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4370 | const char *argPtr = startArgList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4371 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4372 | while (*argPtr++ && parenCount) { |
| 4373 | switch (*argPtr) { |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4374 | case '^': |
| 4375 | // Replace the '^' with '*'. |
| 4376 | DeclLoc = DeclLoc.getFileLocWithOffset(argPtr-startArgList); |
| 4377 | ReplaceText(DeclLoc, 1, "*", 1); |
| 4378 | break; |
| 4379 | case '(': |
| 4380 | parenCount++; |
| 4381 | break; |
| 4382 | case ')': |
| 4383 | parenCount--; |
| 4384 | break; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4385 | } |
| 4386 | } |
| 4387 | return; |
| 4388 | } |
| 4389 | |
| 4390 | bool RewriteObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4391 | const FunctionProtoType *FTP; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4392 | const PointerType *PT = QT->getAs<PointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4393 | if (PT) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4394 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4395 | } else { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4396 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4397 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4398 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4399 | } |
| 4400 | if (FTP) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4401 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4402 | E = FTP->arg_type_end(); I != E; ++I) |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4403 | if (isTopLevelBlockPointerType(*I)) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4404 | return true; |
| 4405 | } |
| 4406 | return false; |
| 4407 | } |
| 4408 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4409 | void RewriteObjC::GetExtentOfArgList(const char *Name, const char *&LParen, |
| 4410 | const char *&RParen) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4411 | const char *argPtr = strchr(Name, '('); |
| 4412 | assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4413 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4414 | LParen = argPtr; // output the start. |
| 4415 | argPtr++; // skip past the left paren. |
| 4416 | unsigned parenCount = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4417 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4418 | while (*argPtr && parenCount) { |
| 4419 | switch (*argPtr) { |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4420 | case '(': parenCount++; break; |
| 4421 | case ')': parenCount--; break; |
| 4422 | default: break; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4423 | } |
| 4424 | if (parenCount) argPtr++; |
| 4425 | } |
| 4426 | assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); |
| 4427 | RParen = argPtr; // output the end |
| 4428 | } |
| 4429 | |
| 4430 | void RewriteObjC::RewriteBlockPointerDecl(NamedDecl *ND) { |
| 4431 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 4432 | RewriteBlockPointerFunctionArgs(FD); |
| 4433 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4434 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4435 | // Handle Variables and Typedefs. |
| 4436 | SourceLocation DeclLoc = ND->getLocation(); |
| 4437 | QualType DeclT; |
| 4438 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4439 | DeclT = VD->getType(); |
| 4440 | else if (TypedefDecl *TDD = dyn_cast<TypedefDecl>(ND)) |
| 4441 | DeclT = TDD->getUnderlyingType(); |
| 4442 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) |
| 4443 | DeclT = FD->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4444 | else |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4445 | assert(0 && "RewriteBlockPointerDecl(): Decl type not yet handled"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4446 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4447 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4448 | const char *endBuf = startBuf; |
| 4449 | // scan backward (from the decl location) for the end of the previous decl. |
| 4450 | while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) |
| 4451 | startBuf--; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4452 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4453 | // *startBuf != '^' if we are dealing with a pointer to function that |
| 4454 | // may take block argument types (which will be handled below). |
| 4455 | if (*startBuf == '^') { |
| 4456 | // Replace the '^' with '*', computing a negative offset. |
| 4457 | DeclLoc = DeclLoc.getFileLocWithOffset(startBuf-endBuf); |
| 4458 | ReplaceText(DeclLoc, 1, "*", 1); |
| 4459 | } |
| 4460 | if (PointerTypeTakesAnyBlockArguments(DeclT)) { |
| 4461 | // Replace the '^' with '*' for arguments. |
| 4462 | DeclLoc = ND->getLocation(); |
| 4463 | startBuf = SM->getCharacterData(DeclLoc); |
| 4464 | const char *argListBegin, *argListEnd; |
| 4465 | GetExtentOfArgList(startBuf, argListBegin, argListEnd); |
| 4466 | while (argListBegin < argListEnd) { |
| 4467 | if (*argListBegin == '^') { |
| 4468 | SourceLocation CaretLoc = DeclLoc.getFileLocWithOffset(argListBegin-startBuf); |
| 4469 | ReplaceText(CaretLoc, 1, "*", 1); |
| 4470 | } |
| 4471 | argListBegin++; |
| 4472 | } |
| 4473 | } |
| 4474 | return; |
| 4475 | } |
| 4476 | |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4477 | |
| 4478 | /// SynthesizeByrefCopyDestroyHelper - This routine synthesizes: |
| 4479 | /// void __Block_byref_id_object_copy(struct Block_byref_id_object *dst, |
| 4480 | /// struct Block_byref_id_object *src) { |
| 4481 | /// _Block_object_assign (&_dest->object, _src->object, |
| 4482 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4483 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4484 | /// _Block_object_assign(&_dest->object, _src->object, |
| 4485 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4486 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4487 | /// } |
| 4488 | /// And: |
| 4489 | /// void __Block_byref_id_object_dispose(struct Block_byref_id_object *_src) { |
| 4490 | /// _Block_object_dispose(_src->object, |
| 4491 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4492 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4493 | /// _Block_object_dispose(_src->object, |
| 4494 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4495 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4496 | /// } |
| 4497 | |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4498 | std::string RewriteObjC::SynthesizeByrefCopyDestroyHelper(VarDecl *VD, |
| 4499 | int flag) { |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4500 | std::string S; |
Benjamin Kramer | 1211a71 | 2010-01-10 19:57:50 +0000 | [diff] [blame] | 4501 | if (CopyDestroyCache.count(flag)) |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4502 | return S; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4503 | CopyDestroyCache.insert(flag); |
| 4504 | S = "static void __Block_byref_id_object_copy_"; |
| 4505 | S += utostr(flag); |
| 4506 | S += "(void *dst, void *src) {\n"; |
| 4507 | |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4508 | // offset into the object pointer is computed as: |
| 4509 | // void * + void* + int + int + void* + void * |
| 4510 | unsigned IntSize = |
| 4511 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 4512 | unsigned VoidPtrSize = |
| 4513 | static_cast<unsigned>(Context->getTypeSize(Context->VoidPtrTy)); |
| 4514 | |
| 4515 | unsigned offset = (VoidPtrSize*4 + IntSize + IntSize)/8; |
| 4516 | S += " _Block_object_assign((char*)dst + "; |
| 4517 | S += utostr(offset); |
| 4518 | S += ", *(void * *) ((char*)src + "; |
| 4519 | S += utostr(offset); |
| 4520 | S += "), "; |
| 4521 | S += utostr(flag); |
| 4522 | S += ");\n}\n"; |
| 4523 | |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4524 | S += "static void __Block_byref_id_object_dispose_"; |
| 4525 | S += utostr(flag); |
| 4526 | S += "(void *src) {\n"; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4527 | S += " _Block_object_dispose(*(void * *) ((char*)src + "; |
| 4528 | S += utostr(offset); |
| 4529 | S += "), "; |
| 4530 | S += utostr(flag); |
| 4531 | S += ");\n}\n"; |
| 4532 | return S; |
| 4533 | } |
| 4534 | |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4535 | /// RewriteByRefVar - For each __block typex ND variable this routine transforms |
| 4536 | /// the declaration into: |
| 4537 | /// struct __Block_byref_ND { |
| 4538 | /// void *__isa; // NULL for everything except __weak pointers |
| 4539 | /// struct __Block_byref_ND *__forwarding; |
| 4540 | /// int32_t __flags; |
| 4541 | /// int32_t __size; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4542 | /// void *__Block_byref_id_object_copy; // If variable is __block ObjC object |
| 4543 | /// void *__Block_byref_id_object_dispose; // If variable is __block ObjC object |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4544 | /// typex ND; |
| 4545 | /// }; |
| 4546 | /// |
| 4547 | /// It then replaces declaration of ND variable with: |
| 4548 | /// struct __Block_byref_ND ND = {__isa=0B, __forwarding=&ND, __flags=some_flag, |
| 4549 | /// __size=sizeof(struct __Block_byref_ND), |
| 4550 | /// ND=initializer-if-any}; |
| 4551 | /// |
| 4552 | /// |
| 4553 | void RewriteObjC::RewriteByRefVar(VarDecl *ND) { |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 4554 | // Insert declaration for the function in which block literal is |
| 4555 | // used. |
| 4556 | if (CurFunctionDeclToDeclareForBlock) |
| 4557 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4558 | int flag = 0; |
| 4559 | int isa = 0; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4560 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
| 4561 | const char *startBuf = SM->getCharacterData(DeclLoc); |
Fariborz Jahanian | 6f0a0a9 | 2009-12-30 20:38:08 +0000 | [diff] [blame] | 4562 | SourceLocation X = ND->getLocEnd(); |
| 4563 | X = SM->getInstantiationLoc(X); |
| 4564 | const char *endBuf = SM->getCharacterData(X); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4565 | std::string Name(ND->getNameAsString()); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4566 | std::string ByrefType; |
| 4567 | RewriteByRefString(ByrefType, Name, ND); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4568 | ByrefType += " {\n"; |
| 4569 | ByrefType += " void *__isa;\n"; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4570 | RewriteByRefString(ByrefType, Name, ND); |
| 4571 | ByrefType += " *__forwarding;\n"; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4572 | ByrefType += " int __flags;\n"; |
| 4573 | ByrefType += " int __size;\n"; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4574 | // Add void *__Block_byref_id_object_copy; |
| 4575 | // void *__Block_byref_id_object_dispose; if needed. |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4576 | QualType Ty = ND->getType(); |
| 4577 | bool HasCopyAndDispose = Context->BlockRequiresCopying(Ty); |
| 4578 | if (HasCopyAndDispose) { |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4579 | ByrefType += " void (*__Block_byref_id_object_copy)(void*, void*);\n"; |
| 4580 | ByrefType += " void (*__Block_byref_id_object_dispose)(void*);\n"; |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4581 | } |
| 4582 | |
| 4583 | Ty.getAsStringInternal(Name, Context->PrintingPolicy); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4584 | ByrefType += " " + Name + ";\n"; |
| 4585 | ByrefType += "};\n"; |
| 4586 | // Insert this type in global scope. It is needed by helper function. |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4587 | SourceLocation FunLocStart; |
| 4588 | if (CurFunctionDef) |
| 4589 | FunLocStart = CurFunctionDef->getTypeSpecStartLoc(); |
| 4590 | else { |
| 4591 | assert(CurMethodDef && "RewriteByRefVar - CurMethodDef is null"); |
| 4592 | FunLocStart = CurMethodDef->getLocStart(); |
| 4593 | } |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4594 | InsertText(FunLocStart, ByrefType.c_str(), ByrefType.size()); |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4595 | if (Ty.isObjCGCWeak()) { |
| 4596 | flag |= BLOCK_FIELD_IS_WEAK; |
| 4597 | isa = 1; |
| 4598 | } |
| 4599 | |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4600 | if (HasCopyAndDispose) { |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4601 | flag = BLOCK_BYREF_CALLER; |
| 4602 | QualType Ty = ND->getType(); |
| 4603 | // FIXME. Handle __weak variable (BLOCK_FIELD_IS_WEAK) as well. |
| 4604 | if (Ty->isBlockPointerType()) |
| 4605 | flag |= BLOCK_FIELD_IS_BLOCK; |
| 4606 | else |
| 4607 | flag |= BLOCK_FIELD_IS_OBJECT; |
| 4608 | std::string HF = SynthesizeByrefCopyDestroyHelper(ND, flag); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4609 | if (!HF.empty()) |
| 4610 | InsertText(FunLocStart, HF.c_str(), HF.size()); |
| 4611 | } |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4612 | |
| 4613 | // struct __Block_byref_ND ND = |
| 4614 | // {0, &ND, some_flag, __size=sizeof(struct __Block_byref_ND), |
| 4615 | // initializer-if-any}; |
| 4616 | bool hasInit = (ND->getInit() != 0); |
Fariborz Jahanian | e1f84f8 | 2010-01-05 18:15:57 +0000 | [diff] [blame] | 4617 | unsigned flags = 0; |
| 4618 | if (HasCopyAndDispose) |
| 4619 | flags |= BLOCK_HAS_COPY_DISPOSE; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4620 | Name = ND->getNameAsString(); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4621 | ByrefType.clear(); |
| 4622 | RewriteByRefString(ByrefType, Name, ND); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4623 | std::string ForwardingCastType("("); |
| 4624 | ForwardingCastType += ByrefType + " *)"; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4625 | if (!hasInit) { |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4626 | ByrefType += " " + Name + " = {(void*)"; |
| 4627 | ByrefType += utostr(isa); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4628 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4629 | ByrefType += utostr(flags); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4630 | ByrefType += ", "; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4631 | ByrefType += "sizeof("; |
| 4632 | RewriteByRefString(ByrefType, Name, ND); |
| 4633 | ByrefType += ")"; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4634 | if (HasCopyAndDispose) { |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4635 | ByrefType += ", __Block_byref_id_object_copy_"; |
| 4636 | ByrefType += utostr(flag); |
| 4637 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 4638 | ByrefType += utostr(flag); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4639 | } |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4640 | ByrefType += "};\n"; |
| 4641 | ReplaceText(DeclLoc, endBuf-startBuf+Name.size(), |
| 4642 | ByrefType.c_str(), ByrefType.size()); |
| 4643 | } |
| 4644 | else { |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4645 | SourceLocation startLoc; |
| 4646 | Expr *E = ND->getInit(); |
| 4647 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 4648 | startLoc = ECE->getLParenLoc(); |
| 4649 | else |
| 4650 | startLoc = E->getLocStart(); |
Fariborz Jahanian | 791b10d | 2010-01-05 23:06:29 +0000 | [diff] [blame] | 4651 | startLoc = SM->getInstantiationLoc(startLoc); |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4652 | endBuf = SM->getCharacterData(startLoc); |
| 4653 | |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4654 | ByrefType += " " + Name; |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4655 | ByrefType += " = {(void*)"; |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4656 | ByrefType += utostr(isa); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4657 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4658 | ByrefType += utostr(flags); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4659 | ByrefType += ", "; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4660 | ByrefType += "sizeof("; |
| 4661 | RewriteByRefString(ByrefType, Name, ND); |
| 4662 | ByrefType += "), "; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4663 | if (HasCopyAndDispose) { |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4664 | ByrefType += "__Block_byref_id_object_copy_"; |
| 4665 | ByrefType += utostr(flag); |
| 4666 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 4667 | ByrefType += utostr(flag); |
| 4668 | ByrefType += ", "; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4669 | } |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4670 | ReplaceText(DeclLoc, endBuf-startBuf, |
| 4671 | ByrefType.c_str(), ByrefType.size()); |
Steve Naroff | c5143c5 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 4672 | |
| 4673 | // Complete the newly synthesized compound expression by inserting a right |
| 4674 | // curly brace before the end of the declaration. |
| 4675 | // FIXME: This approach avoids rewriting the initializer expression. It |
| 4676 | // also assumes there is only one declarator. For example, the following |
| 4677 | // isn't currently supported by this routine (in general): |
| 4678 | // |
| 4679 | // double __block BYREFVAR = 1.34, BYREFVAR2 = 1.37; |
| 4680 | // |
| 4681 | const char *startBuf = SM->getCharacterData(startLoc); |
| 4682 | const char *semiBuf = strchr(startBuf, ';'); |
| 4683 | assert((*semiBuf == ';') && "RewriteByRefVar: can't find ';'"); |
| 4684 | SourceLocation semiLoc = |
| 4685 | startLoc.getFileLocWithOffset(semiBuf-startBuf); |
| 4686 | |
| 4687 | InsertText(semiLoc, "}", 1); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4688 | } |
Fariborz Jahanian | 1be6b46 | 2009-12-22 00:48:54 +0000 | [diff] [blame] | 4689 | return; |
| 4690 | } |
| 4691 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4692 | void RewriteObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4693 | // Add initializers for any closure decl refs. |
| 4694 | GetBlockDeclRefExprs(Exp->getBody()); |
| 4695 | if (BlockDeclRefs.size()) { |
| 4696 | // Unique all "by copy" declarations. |
| 4697 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 4698 | if (!BlockDeclRefs[i]->isByRef()) |
| 4699 | BlockByCopyDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 4700 | // Unique all "by ref" declarations. |
| 4701 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 4702 | if (BlockDeclRefs[i]->isByRef()) { |
| 4703 | BlockByRefDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 4704 | } |
| 4705 | // Find any imported blocks...they will need special attention. |
| 4706 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
Fariborz Jahanian | 4fcc4fd | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 4707 | if (BlockDeclRefs[i]->isByRef() || |
| 4708 | BlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
| 4709 | BlockDeclRefs[i]->getType()->isBlockPointerType()) { |
Steve Naroff | 0007268 | 2008-11-13 17:40:07 +0000 | [diff] [blame] | 4710 | GetBlockCallExprs(BlockDeclRefs[i]); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4711 | ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 4712 | } |
| 4713 | } |
| 4714 | } |
| 4715 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4716 | FunctionDecl *RewriteObjC::SynthBlockInitFunctionDecl(const char *name) { |
| 4717 | IdentifierInfo *ID = &Context->Idents.get(name); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4718 | QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4719 | return FunctionDecl::Create(*Context, TUDecl,SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 4720 | ID, FType, 0, FunctionDecl::Extern, false, |
Douglas Gregor | 2224f84 | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 4721 | false); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4722 | } |
| 4723 | |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4724 | Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4725 | Blocks.push_back(Exp); |
| 4726 | |
| 4727 | CollectBlockDeclRefInfo(Exp); |
| 4728 | std::string FuncName; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4729 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4730 | if (CurFunctionDef) |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4731 | FuncName = CurFunctionDef->getNameAsString(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4732 | else if (CurMethodDef) { |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4733 | FuncName = CurMethodDef->getSelector().getAsString(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4734 | // Convert colons to underscores. |
| 4735 | std::string::size_type loc = 0; |
| 4736 | while ((loc = FuncName.find(":", loc)) != std::string::npos) |
| 4737 | FuncName.replace(loc, 1, "_"); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4738 | } else if (GlobalVarDecl) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4739 | FuncName = std::string(GlobalVarDecl->getNameAsString()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4740 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4741 | std::string BlockNumber = utostr(Blocks.size()-1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4742 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4743 | std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber; |
| 4744 | std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4745 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4746 | // Get a pointer to the function type so we can cast appropriately. |
| 4747 | QualType FType = Context->getPointerType(QualType(Exp->getFunctionType(),0)); |
| 4748 | |
| 4749 | FunctionDecl *FD; |
| 4750 | Expr *NewRep; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4751 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4752 | // Simulate a contructor call... |
| 4753 | FD = SynthBlockInitFunctionDecl(Tag.c_str()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4754 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, FType, SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4755 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4756 | llvm::SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4757 | |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4758 | // Initialize the block function. |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4759 | FD = SynthBlockInitFunctionDecl(Func.c_str()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4760 | DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, FD->getType(), |
| 4761 | SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4762 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
| 4763 | CastExpr::CK_Unknown, Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4764 | InitExprs.push_back(castExpr); |
| 4765 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4766 | // Initialize the block descriptor. |
| 4767 | std::string DescData = "__" + FuncName + "_block_desc_" + BlockNumber + "_DATA"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4768 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4769 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
| 4770 | &Context->Idents.get(DescData.c_str()), |
| 4771 | Context->VoidPtrTy, 0, |
| 4772 | VarDecl::Static); |
| 4773 | UnaryOperator *DescRefExpr = new (Context) UnaryOperator( |
| 4774 | new (Context) DeclRefExpr(NewVD, |
| 4775 | Context->VoidPtrTy, SourceLocation()), |
| 4776 | UnaryOperator::AddrOf, |
| 4777 | Context->getPointerType(Context->VoidPtrTy), |
| 4778 | SourceLocation()); |
| 4779 | InitExprs.push_back(DescRefExpr); |
| 4780 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4781 | // Add initializers for any closure decl refs. |
| 4782 | if (BlockDeclRefs.size()) { |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4783 | Expr *Exp; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4784 | // Output all "by copy" declarations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4785 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4786 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4787 | if (isObjCType((*I)->getType())) { |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4788 | // FIXME: Conform to ABI ([[obj retain] autorelease]). |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4789 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4790 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4791 | } else if (isTopLevelBlockPointerType((*I)->getType())) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4792 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4793 | Arg = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4794 | Exp = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
| 4795 | CastExpr::CK_Unknown, Arg); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4796 | } else { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4797 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4798 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4799 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4800 | InitExprs.push_back(Exp); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4801 | } |
| 4802 | // Output all "by ref" declarations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4803 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4804 | E = BlockByRefDecls.end(); I != E; ++I) { |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4805 | ValueDecl *ND = (*I); |
| 4806 | std::string Name(ND->getNameAsString()); |
| 4807 | std::string RecName; |
| 4808 | RewriteByRefString(RecName, Name, ND); |
| 4809 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str() |
| 4810 | + sizeof("struct")); |
| 4811 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
| 4812 | SourceLocation(), II); |
| 4813 | assert(RD && "SynthBlockInitExpr(): Can't find RecordDecl"); |
| 4814 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 4815 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4816 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4817 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 4818 | Exp = new (Context) UnaryOperator(Exp, UnaryOperator::AddrOf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4819 | Context->getPointerType(Exp->getType()), |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4820 | SourceLocation()); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4821 | Exp = NoTypeInfoCStyleCastExpr(Context, castT, CastExpr::CK_Unknown, Exp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4822 | InitExprs.push_back(Exp); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4823 | } |
| 4824 | } |
Fariborz Jahanian | ff12788 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 4825 | if (ImportedBlockDecls.size()) { |
| 4826 | // generate BLOCK_HAS_COPY_DISPOSE(have helper funcs) | BLOCK_HAS_DESCRIPTOR |
| 4827 | int flag = (BLOCK_HAS_COPY_DISPOSE | BLOCK_HAS_DESCRIPTOR); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4828 | unsigned IntSize = |
| 4829 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Fariborz Jahanian | ff12788 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 4830 | Expr *FlagExp = new (Context) IntegerLiteral(llvm::APInt(IntSize, flag), |
| 4831 | Context->IntTy, SourceLocation()); |
| 4832 | InitExprs.push_back(FlagExp); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4833 | } |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 4834 | NewRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], InitExprs.size(), |
| 4835 | FType, SourceLocation()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4836 | NewRep = new (Context) UnaryOperator(NewRep, UnaryOperator::AddrOf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4837 | Context->getPointerType(NewRep->getType()), |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4838 | SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4839 | NewRep = NoTypeInfoCStyleCastExpr(Context, FType, CastExpr::CK_Unknown, |
| 4840 | NewRep); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4841 | BlockDeclRefs.clear(); |
| 4842 | BlockByRefDecls.clear(); |
| 4843 | BlockByCopyDecls.clear(); |
| 4844 | ImportedBlockDecls.clear(); |
| 4845 | return NewRep; |
| 4846 | } |
| 4847 | |
| 4848 | //===----------------------------------------------------------------------===// |
| 4849 | // Function Body / Expression rewriting |
| 4850 | //===----------------------------------------------------------------------===// |
| 4851 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4852 | // This is run as a first "pass" prior to RewriteFunctionBodyOrGlobalInitializer(). |
| 4853 | // The allows the main rewrite loop to associate all ObjCPropertyRefExprs with |
| 4854 | // their respective BinaryOperator. Without this knowledge, we'd need to rewrite |
| 4855 | // the ObjCPropertyRefExpr twice (once as a getter, and later as a setter). |
| 4856 | // Since the rewriter isn't capable of rewriting rewritten code, it's important |
| 4857 | // we get this right. |
| 4858 | void RewriteObjC::CollectPropertySetters(Stmt *S) { |
| 4859 | // Perform a bottom up traversal of all children. |
| 4860 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4861 | CI != E; ++CI) |
| 4862 | if (*CI) |
| 4863 | CollectPropertySetters(*CI); |
| 4864 | |
| 4865 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) { |
| 4866 | if (BinOp->isAssignmentOp()) { |
| 4867 | if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS())) |
| 4868 | PropSetters[PRE] = BinOp; |
| 4869 | } |
| 4870 | } |
| 4871 | } |
| 4872 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4873 | Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4874 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4875 | isa<DoStmt>(S) || isa<ForStmt>(S)) |
| 4876 | Stmts.push_back(S); |
| 4877 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 4878 | Stmts.push_back(S); |
Chris Lattner | 4824fcd | 2010-01-09 21:45:57 +0000 | [diff] [blame] | 4879 | ObjCBcLabelNo.push_back(++BcLabelCount); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 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 | SourceRange OrigStmtRange = S->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4883 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4884 | // Perform a bottom up rewrite of all children. |
| 4885 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4886 | CI != E; ++CI) |
| 4887 | if (*CI) { |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 4888 | Stmt *newStmt; |
| 4889 | Stmt *S = (*CI); |
| 4890 | if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) { |
| 4891 | Expr *OldBase = IvarRefExpr->getBase(); |
| 4892 | bool replaced = false; |
| 4893 | newStmt = RewriteObjCNestedIvarRefExpr(S, replaced); |
| 4894 | if (replaced) { |
| 4895 | if (ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(newStmt)) |
| 4896 | ReplaceStmt(OldBase, IRE->getBase()); |
| 4897 | else |
| 4898 | ReplaceStmt(S, newStmt); |
| 4899 | } |
| 4900 | } |
| 4901 | else |
| 4902 | newStmt = RewriteFunctionBodyOrGlobalInitializer(S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4903 | if (newStmt) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4904 | *CI = newStmt; |
| 4905 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4906 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4907 | if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
| 4908 | // Rewrite the block body in place. |
| 4909 | RewriteFunctionBodyOrGlobalInitializer(BE->getBody()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4910 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4911 | // 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] | 4912 | std::string Str = Rewrite.getRewrittenText(BE->getSourceRange()); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4913 | RewrittenBlockExprs[BE] = Str; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4914 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4915 | Stmt *blockTranscribed = SynthBlockInitExpr(BE); |
| 4916 | //blockTranscribed->dump(); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4917 | ReplaceStmt(S, blockTranscribed); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4918 | return blockTranscribed; |
| 4919 | } |
| 4920 | // Handle specific things. |
| 4921 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 4922 | return RewriteAtEncode(AtEncode); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4923 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4924 | if (ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(S)) { |
| 4925 | BinaryOperator *BinOp = PropSetters[PropRefExpr]; |
| 4926 | if (BinOp) { |
| 4927 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 4928 | // 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] | 4929 | DisableReplaceStmt = true; |
| 4930 | // Save the source range. Even if we disable the replacement, the |
| 4931 | // rewritten node will have been inserted into the tree. If the synthesized |
| 4932 | // node is at the 'end', the rewriter will fail. Consider this: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4933 | // self.errorHandler = handler ? handler : |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 4934 | // ^(NSURL *errorURL, NSError *error) { return (BOOL)1; }; |
| 4935 | SourceRange SrcRange = BinOp->getSourceRange(); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4936 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(BinOp->getRHS()); |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 4937 | DisableReplaceStmt = false; |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 4938 | // |
| 4939 | // Unlike the main iterator, we explicily avoid changing 'BinOp'. If |
| 4940 | // we changed the RHS of BinOp, the rewriter would fail (since it needs |
| 4941 | // to see the original expression). Consider this example: |
| 4942 | // |
| 4943 | // Foo *obj1, *obj2; |
| 4944 | // |
| 4945 | // obj1.i = [obj2 rrrr]; |
| 4946 | // |
| 4947 | // 'BinOp' for the previous expression looks like: |
| 4948 | // |
| 4949 | // (BinaryOperator 0x231ccf0 'int' '=' |
| 4950 | // (ObjCPropertyRefExpr 0x231cc70 'int' Kind=PropertyRef Property="i" |
| 4951 | // (DeclRefExpr 0x231cc50 'Foo *' Var='obj1' 0x231cbb0)) |
| 4952 | // (ObjCMessageExpr 0x231ccb0 'int' selector=rrrr |
| 4953 | // (DeclRefExpr 0x231cc90 'Foo *' Var='obj2' 0x231cbe0))) |
| 4954 | // |
| 4955 | // 'newStmt' represents the rewritten message expression. For example: |
| 4956 | // |
| 4957 | // (CallExpr 0x231d300 'id':'struct objc_object *' |
| 4958 | // (ParenExpr 0x231d2e0 'int (*)(id, SEL)' |
| 4959 | // (CStyleCastExpr 0x231d2c0 'int (*)(id, SEL)' |
| 4960 | // (CStyleCastExpr 0x231d220 'void *' |
| 4961 | // (DeclRefExpr 0x231d200 'id (id, SEL, ...)' FunctionDecl='objc_msgSend' 0x231cdc0)))) |
| 4962 | // |
| 4963 | // Note that 'newStmt' is passed to RewritePropertySetter so that it |
| 4964 | // can be used as the setter argument. ReplaceStmt() will still 'see' |
| 4965 | // the original RHS (since we haven't altered BinOp). |
| 4966 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4967 | // This implies the Rewrite* routines can no longer delete the original |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 4968 | // node. As a result, we now leak the original AST nodes. |
| 4969 | // |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 4970 | return RewritePropertySetter(BinOp, dyn_cast<Expr>(newStmt), SrcRange); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4971 | } else { |
| 4972 | return RewritePropertyGetter(PropRefExpr); |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 4973 | } |
| 4974 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4975 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 4976 | return RewriteAtSelector(AtSelector); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4977 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4978 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 4979 | return RewriteObjCStringLiteral(AtString); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4980 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4981 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4982 | #if 0 |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4983 | // Before we rewrite it, put the original message expression in a comment. |
| 4984 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 4985 | SourceLocation endLoc = MessExpr->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4986 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4987 | const char *startBuf = SM->getCharacterData(startLoc); |
| 4988 | const char *endBuf = SM->getCharacterData(endLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4989 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4990 | std::string messString; |
| 4991 | messString += "// "; |
| 4992 | messString.append(startBuf, endBuf-startBuf+1); |
| 4993 | messString += "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4994 | |
| 4995 | // FIXME: Missing definition of |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4996 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
| 4997 | // InsertText(startLoc, messString.c_str(), messString.size()); |
| 4998 | // Tried this, but it didn't work either... |
| 4999 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5000 | #endif |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5001 | return RewriteMessageExpr(MessExpr); |
| 5002 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5003 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5004 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 5005 | return RewriteObjCTryStmt(StmtTry); |
| 5006 | |
| 5007 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 5008 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 5009 | |
| 5010 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 5011 | return RewriteObjCThrowStmt(StmtThrow); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5012 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5013 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 5014 | return RewriteObjCProtocolExpr(ProtocolExp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5015 | |
| 5016 | if (ObjCForCollectionStmt *StmtForCollection = |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5017 | dyn_cast<ObjCForCollectionStmt>(S)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5018 | return RewriteObjCForCollectionStmt(StmtForCollection, |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5019 | OrigStmtRange.getEnd()); |
| 5020 | if (BreakStmt *StmtBreakStmt = |
| 5021 | dyn_cast<BreakStmt>(S)) |
| 5022 | return RewriteBreakStmt(StmtBreakStmt); |
| 5023 | if (ContinueStmt *StmtContinueStmt = |
| 5024 | dyn_cast<ContinueStmt>(S)) |
| 5025 | return RewriteContinueStmt(StmtContinueStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5026 | |
| 5027 | // 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] | 5028 | // and cast exprs. |
| 5029 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
| 5030 | // FIXME: What we're doing here is modifying the type-specifier that |
| 5031 | // precedes the first Decl. In the future the DeclGroup should have |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5032 | // a separate type-specifier that we can rewrite. |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5033 | // NOTE: We need to avoid rewriting the DeclStmt if it is within |
| 5034 | // the context of an ObjCForCollectionStmt. For example: |
| 5035 | // NSArray *someArray; |
| 5036 | // for (id <FooProtocol> index in someArray) ; |
| 5037 | // This is because RewriteObjCForCollectionStmt() does textual rewriting |
| 5038 | // and it depends on the original text locations/positions. |
Benjamin Kramer | b2041de | 2009-12-05 22:16:51 +0000 | [diff] [blame] | 5039 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5040 | RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5041 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5042 | // Blocks rewrite rules. |
| 5043 | for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); |
| 5044 | DI != DE; ++DI) { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 5045 | Decl *SD = *DI; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5046 | if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5047 | if (isTopLevelBlockPointerType(ND->getType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5048 | RewriteBlockPointerDecl(ND); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5049 | else if (ND->getType()->isFunctionPointerType()) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5050 | CheckFunctionPointerDecl(ND->getType(), ND); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5051 | if (VarDecl *VD = dyn_cast<VarDecl>(SD)) |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5052 | if (VD->hasAttr<BlocksAttr>()) { |
| 5053 | static unsigned uniqueByrefDeclCount = 0; |
| 5054 | assert(!BlockByRefDeclNo.count(ND) && |
| 5055 | "RewriteFunctionBodyOrGlobalInitializer: Duplicate byref decl"); |
| 5056 | BlockByRefDeclNo[ND] = uniqueByrefDeclCount++; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5057 | RewriteByRefVar(VD); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5058 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5059 | } |
| 5060 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5061 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5062 | RewriteBlockPointerDecl(TD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5063 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5064 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 5065 | } |
| 5066 | } |
| 5067 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5068 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5069 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) |
| 5070 | RewriteObjCQualifiedInterfaceTypes(CE); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5071 | |
| 5072 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5073 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 5074 | assert(!Stmts.empty() && "Statement stack is empty"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5075 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 5076 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5077 | && "Statement stack mismatch"); |
| 5078 | Stmts.pop_back(); |
| 5079 | } |
| 5080 | // Handle blocks rewriting. |
| 5081 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
| 5082 | if (BDRE->isByRef()) |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5083 | return RewriteBlockDeclRefExpr(BDRE); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5084 | } |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5085 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 5086 | ValueDecl *VD = DRE->getDecl(); |
| 5087 | if (VD->hasAttr<BlocksAttr>()) |
| 5088 | return RewriteBlockDeclRefExpr(DRE); |
| 5089 | } |
| 5090 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5091 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 5092 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 5093 | Stmt *BlockCall = SynthesizeBlockCall(CE, CE->getCallee()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 5094 | ReplaceStmt(S, BlockCall); |
| 5095 | return BlockCall; |
| 5096 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5097 | } |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 5098 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5099 | RewriteCastExpr(CE); |
| 5100 | } |
| 5101 | #if 0 |
| 5102 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5103 | CastExpr *Replacement = new (Context) CastExpr(ICE->getType(), ICE->getSubExpr(), SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5104 | // Get the new text. |
| 5105 | std::string SStr; |
| 5106 | llvm::raw_string_ostream Buf(SStr); |
Eli Friedman | 3a9eb44 | 2009-05-30 05:19:26 +0000 | [diff] [blame] | 5107 | Replacement->printPretty(Buf, *Context); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5108 | const std::string &Str = Buf.str(); |
| 5109 | |
| 5110 | printf("CAST = %s\n", &Str[0]); |
| 5111 | InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 5112 | delete S; |
| 5113 | return Replacement; |
| 5114 | } |
| 5115 | #endif |
| 5116 | // Return this stmt unmodified. |
| 5117 | return S; |
| 5118 | } |
| 5119 | |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5120 | void RewriteObjC::RewriteRecordBody(RecordDecl *RD) { |
| 5121 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 5122 | e = RD->field_end(); i != e; ++i) { |
| 5123 | FieldDecl *FD = *i; |
| 5124 | if (isTopLevelBlockPointerType(FD->getType())) |
| 5125 | RewriteBlockPointerDecl(FD); |
| 5126 | if (FD->getType()->isObjCQualifiedIdType() || |
| 5127 | FD->getType()->isObjCQualifiedInterfaceType()) |
| 5128 | RewriteObjCQualifiedInterfaceTypes(FD); |
| 5129 | } |
| 5130 | } |
| 5131 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5132 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 5133 | /// main file of the input. |
| 5134 | void RewriteObjC::HandleDeclInMainFile(Decl *D) { |
| 5135 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | cb73530 | 2008-12-17 00:20:22 +0000 | [diff] [blame] | 5136 | if (FD->isOverloadedOperator()) |
| 5137 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5138 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5139 | // Since function prototypes don't have ParmDecl's, we check the function |
| 5140 | // prototype. This enables us to rewrite function declarations and |
| 5141 | // definitions using the same code. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5142 | RewriteBlocksInFunctionProtoType(FD->getType(), FD); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5143 | |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 5144 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 5145 | if (CompoundStmt *Body = FD->getCompoundBody()) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5146 | CurFunctionDef = FD; |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 5147 | CurFunctionDeclToDeclareForBlock = FD; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5148 | CollectPropertySetters(Body); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5149 | CurrentBody = Body; |
Ted Kremenek | eaab206 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 5150 | Body = |
| 5151 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 5152 | FD->setBody(Body); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5153 | CurrentBody = 0; |
| 5154 | if (PropParentMap) { |
| 5155 | delete PropParentMap; |
| 5156 | PropParentMap = 0; |
| 5157 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5158 | // This synthesizes and inserts the block "impl" struct, invoke function, |
| 5159 | // and any copy/dispose helper functions. |
| 5160 | InsertBlockLiteralsWithinFunction(FD); |
| 5161 | CurFunctionDef = 0; |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 5162 | CurFunctionDeclToDeclareForBlock = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5163 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5164 | return; |
| 5165 | } |
| 5166 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 5167 | if (CompoundStmt *Body = MD->getCompoundBody()) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5168 | CurMethodDef = MD; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5169 | CollectPropertySetters(Body); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5170 | CurrentBody = Body; |
Ted Kremenek | eaab206 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 5171 | Body = |
| 5172 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 5173 | MD->setBody(Body); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5174 | CurrentBody = 0; |
| 5175 | if (PropParentMap) { |
| 5176 | delete PropParentMap; |
| 5177 | PropParentMap = 0; |
| 5178 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5179 | InsertBlockLiteralsWithinMethod(MD); |
| 5180 | CurMethodDef = 0; |
| 5181 | } |
| 5182 | } |
| 5183 | if (ObjCImplementationDecl *CI = dyn_cast<ObjCImplementationDecl>(D)) |
| 5184 | ClassImplementation.push_back(CI); |
| 5185 | else if (ObjCCategoryImplDecl *CI = dyn_cast<ObjCCategoryImplDecl>(D)) |
| 5186 | CategoryImplementation.push_back(CI); |
| 5187 | else if (ObjCClassDecl *CD = dyn_cast<ObjCClassDecl>(D)) |
| 5188 | RewriteForwardClassDecl(CD); |
| 5189 | else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 5190 | RewriteObjCQualifiedInterfaceTypes(VD); |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5191 | if (isTopLevelBlockPointerType(VD->getType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5192 | RewriteBlockPointerDecl(VD); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5193 | else if (VD->getType()->isFunctionPointerType()) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5194 | CheckFunctionPointerDecl(VD->getType(), VD); |
| 5195 | if (VD->getInit()) { |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 5196 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5197 | RewriteCastExpr(CE); |
| 5198 | } |
| 5199 | } |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5200 | } else if (VD->getType()->isRecordType()) { |
| 5201 | RecordDecl *RD = VD->getType()->getAs<RecordType>()->getDecl(); |
| 5202 | if (RD->isDefinition()) |
| 5203 | RewriteRecordBody(RD); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5204 | } |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5205 | if (VD->getInit()) { |
| 5206 | GlobalVarDecl = VD; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5207 | CollectPropertySetters(VD->getInit()); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5208 | CurrentBody = VD->getInit(); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5209 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5210 | CurrentBody = 0; |
| 5211 | if (PropParentMap) { |
| 5212 | delete PropParentMap; |
| 5213 | PropParentMap = 0; |
| 5214 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5215 | SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 5216 | VD->getNameAsCString()); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5217 | GlobalVarDecl = 0; |
| 5218 | |
| 5219 | // This is needed for blocks. |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 5220 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5221 | RewriteCastExpr(CE); |
| 5222 | } |
| 5223 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5224 | return; |
| 5225 | } |
| 5226 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5227 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5228 | RewriteBlockPointerDecl(TD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5229 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5230 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5231 | else if (TD->getUnderlyingType()->isRecordType()) { |
| 5232 | RecordDecl *RD = TD->getUnderlyingType()->getAs<RecordType>()->getDecl(); |
| 5233 | if (RD->isDefinition()) |
| 5234 | RewriteRecordBody(RD); |
| 5235 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5236 | return; |
| 5237 | } |
| 5238 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) { |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5239 | if (RD->isDefinition()) |
| 5240 | RewriteRecordBody(RD); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5241 | return; |
| 5242 | } |
| 5243 | // Nothing yet. |
| 5244 | } |
| 5245 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 5246 | void RewriteObjC::HandleTranslationUnit(ASTContext &C) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5247 | // Get the top-level buffer that this corresponds to. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5248 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5249 | // Rewrite tabs if we care. |
| 5250 | //RewriteTabs(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5251 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5252 | if (Diags.hasErrorOccurred()) |
| 5253 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5254 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5255 | RewriteInclude(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5256 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5257 | // Here's a great place to add any extra declarations that may be needed. |
| 5258 | // Write out meta data for each @protocol(<expr>). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5259 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5260 | E = ProtocolExprDecls.end(); I != E; ++I) |
| 5261 | RewriteObjCProtocolMetaData(*I, "", "", Preamble); |
| 5262 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5263 | InsertText(SM->getLocForStartOfFile(MainFileID), |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5264 | Preamble.c_str(), Preamble.size(), false); |
Steve Naroff | 0aab796 | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5265 | if (ClassImplementation.size() || CategoryImplementation.size()) |
| 5266 | RewriteImplementations(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5267 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5268 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 5269 | // we are done. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5270 | if (const RewriteBuffer *RewriteBuf = |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5271 | Rewrite.getRewriteBufferFor(MainFileID)) { |
| 5272 | //printf("Changed:\n"); |
| 5273 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 5274 | } else { |
| 5275 | fprintf(stderr, "No changes\n"); |
| 5276 | } |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 5277 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5278 | if (ClassImplementation.size() || CategoryImplementation.size() || |
| 5279 | ProtocolExprDecls.size()) { |
Steve Naroff | 0aab796 | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5280 | // Rewrite Objective-c meta data* |
| 5281 | std::string ResultStr; |
| 5282 | SynthesizeMetaDataIntoBuffer(ResultStr); |
| 5283 | // Emit metadata. |
| 5284 | *OutFile << ResultStr; |
| 5285 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5286 | OutFile->flush(); |
| 5287 | } |
| 5288 | |