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; |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 123 | llvm::SmallVector<int, 32> InnerDeclRefsCount; |
| 124 | llvm::SmallVector<BlockDeclRefExpr *, 32> InnerDeclRefs; |
| 125 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 126 | llvm::SmallVector<BlockDeclRefExpr *, 32> BlockDeclRefs; |
| 127 | llvm::DenseMap<BlockDeclRefExpr *, CallExpr *> BlockCallExprs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 128 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 129 | // Block related declarations. |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 130 | llvm::SmallVector<ValueDecl *, 8> BlockByCopyDecls; |
| 131 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByCopyDeclsPtrSet; |
| 132 | llvm::SmallVector<ValueDecl *, 8> BlockByRefDecls; |
| 133 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByRefDeclsPtrSet; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 134 | llvm::DenseMap<ValueDecl *, unsigned> BlockByRefDeclNo; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 135 | llvm::SmallPtrSet<ValueDecl *, 8> ImportedBlockDecls; |
| 136 | |
| 137 | llvm::DenseMap<BlockExpr *, std::string> RewrittenBlockExprs; |
| 138 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 139 | // This maps a property to it's assignment statement. |
| 140 | llvm::DenseMap<ObjCPropertyRefExpr *, BinaryOperator *> PropSetters; |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 141 | // This maps a property to it's synthesied message expression. |
| 142 | // This allows us to rewrite chained getters (e.g. o.a.b.c). |
| 143 | llvm::DenseMap<ObjCPropertyRefExpr *, Stmt *> PropGetters; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 144 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 145 | // This maps an original source AST to it's rewritten form. This allows |
| 146 | // us to avoid rewriting the same node twice (which is very uncommon). |
| 147 | // This is needed to support some of the exotic property rewriting. |
| 148 | llvm::DenseMap<Stmt *, Stmt *> ReplacedNodes; |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 149 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 150 | FunctionDecl *CurFunctionDef; |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 151 | FunctionDecl *CurFunctionDeclToDeclareForBlock; |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 152 | VarDecl *GlobalVarDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 153 | |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 154 | bool DisableReplaceStmt; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 156 | static const int OBJC_ABI_VERSION =7 ; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 157 | public: |
Ted Kremenek | e3a6198 | 2008-05-31 20:11:04 +0000 | [diff] [blame] | 158 | virtual void Initialize(ASTContext &context); |
| 159 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 160 | // Top Level Driver code. |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 161 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
| 162 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) |
| 163 | HandleTopLevelSingleDecl(*I); |
| 164 | } |
| 165 | void HandleTopLevelSingleDecl(Decl *D); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 166 | void HandleDeclInMainFile(Decl *D); |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 167 | RewriteObjC(std::string inFile, llvm::raw_ostream *OS, |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 168 | Diagnostic &D, const LangOptions &LOpts, |
| 169 | bool silenceMacroWarn); |
Ted Kremenek | e452e0f | 2008-08-08 04:15:52 +0000 | [diff] [blame] | 170 | |
| 171 | ~RewriteObjC() {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 172 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 173 | virtual void HandleTranslationUnit(ASTContext &C); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 174 | |
Fariborz Jahanian | 88906cd | 2010-02-05 16:43:40 +0000 | [diff] [blame] | 175 | void ReplaceStmt(Stmt *Old, Stmt *New) { |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 176 | Stmt *ReplacingStmt = ReplacedNodes[Old]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 177 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 178 | if (ReplacingStmt) |
| 179 | return; // We can't rewrite the same node twice. |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 180 | |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 181 | if (DisableReplaceStmt) |
| 182 | return; // Used when rewriting the assignment of a property setter. |
| 183 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 184 | // If replacement succeeded or warning disabled return with no warning. |
Fariborz Jahanian | 88906cd | 2010-02-05 16:43:40 +0000 | [diff] [blame] | 185 | if (!Rewrite.ReplaceStmt(Old, New)) { |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 186 | ReplacedNodes[Old] = New; |
| 187 | return; |
| 188 | } |
| 189 | if (SilenceRewriteMacroWarning) |
| 190 | return; |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 191 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 192 | << Old->getSourceRange(); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 193 | } |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 194 | |
| 195 | void ReplaceStmtWithRange(Stmt *Old, Stmt *New, SourceRange SrcRange) { |
| 196 | // Measaure the old text. |
| 197 | int Size = Rewrite.getRangeSize(SrcRange); |
| 198 | if (Size == -1) { |
| 199 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 200 | << Old->getSourceRange(); |
| 201 | return; |
| 202 | } |
| 203 | // Get the new text. |
| 204 | std::string SStr; |
| 205 | llvm::raw_string_ostream S(SStr); |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 206 | New->printPretty(S, *Context, 0, PrintingPolicy(LangOpts)); |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 207 | const std::string &Str = S.str(); |
| 208 | |
| 209 | // If replacement succeeded or warning disabled return with no warning. |
Daniel Dunbar | d7407dc | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 210 | if (!Rewrite.ReplaceText(SrcRange.getBegin(), Size, Str)) { |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 211 | ReplacedNodes[Old] = New; |
| 212 | return; |
| 213 | } |
| 214 | if (SilenceRewriteMacroWarning) |
| 215 | return; |
| 216 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 217 | << Old->getSourceRange(); |
| 218 | } |
| 219 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 220 | void InsertText(SourceLocation Loc, llvm::StringRef Str, |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 221 | bool InsertAfter = true) { |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 222 | // If insertion succeeded or warning disabled return with no warning. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 223 | if (!Rewrite.InsertText(Loc, Str, InsertAfter) || |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 224 | SilenceRewriteMacroWarning) |
| 225 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 226 | |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 227 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 228 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 229 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 230 | void RemoveText(SourceLocation Loc, unsigned StrLen) { |
| 231 | // If removal succeeded or warning disabled return with no warning. |
| 232 | if (!Rewrite.RemoveText(Loc, StrLen) || SilenceRewriteMacroWarning) |
| 233 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 234 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 235 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 236 | } |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 237 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 238 | void ReplaceText(SourceLocation Start, unsigned OrigLength, |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 239 | llvm::StringRef Str) { |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 240 | // If removal succeeded or warning disabled return with no warning. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 241 | if (!Rewrite.ReplaceText(Start, OrigLength, Str) || |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 242 | SilenceRewriteMacroWarning) |
| 243 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 244 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 245 | Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag); |
| 246 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 247 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 248 | // Syntactic Rewriting. |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 249 | void RewritePrologue(SourceLocation Loc); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 250 | void RewriteInclude(); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 251 | void RewriteTabs(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 252 | void RewriteForwardClassDecl(ObjCClassDecl *Dcl); |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 253 | void RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 254 | ObjCImplementationDecl *IMD, |
| 255 | ObjCCategoryImplDecl *CID); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 256 | void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 257 | void RewriteImplementationDecl(Decl *Dcl); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 258 | void RewriteObjCMethodDecl(ObjCMethodDecl *MDecl, std::string &ResultStr); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 259 | void RewriteByRefString(std::string &ResultStr, const std::string &Name, |
| 260 | ValueDecl *VD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 261 | void RewriteCategoryDecl(ObjCCategoryDecl *Dcl); |
| 262 | void RewriteProtocolDecl(ObjCProtocolDecl *Dcl); |
| 263 | void RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *Dcl); |
| 264 | void RewriteMethodDeclaration(ObjCMethodDecl *Method); |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 265 | void RewriteProperty(ObjCPropertyDecl *prop); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 266 | void RewriteFunctionDecl(FunctionDecl *FD); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 267 | void RewriteBlockLiteralFunctionDecl(FunctionDecl *FD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 268 | void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 269 | void RewriteTypeOfDecl(VarDecl *VD); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 270 | void RewriteObjCQualifiedInterfaceTypes(Expr *E); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 271 | bool needToScanForQualifiers(QualType T); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 272 | ObjCInterfaceDecl *isSuperReceiver(Expr *recExpr); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 273 | QualType getSuperStructType(); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 274 | QualType getConstantStringStructType(); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 275 | bool BufferContainsPPDirectives(const char *startBuf, const char *endBuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 276 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 277 | // Expression Rewriting. |
Steve Naroff | f3473a7 | 2007-11-09 15:20:18 +0000 | [diff] [blame] | 278 | Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 279 | void CollectPropertySetters(Stmt *S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 280 | |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 281 | Stmt *CurrentBody; |
| 282 | ParentMap *PropParentMap; // created lazily. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 283 | |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 284 | Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp); |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 285 | Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, SourceLocation OrigStart, |
| 286 | bool &replaced); |
| 287 | Stmt *RewriteObjCNestedIvarRefExpr(Stmt *S, bool &replaced); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 288 | Stmt *RewritePropertyGetter(ObjCPropertyRefExpr *PropRefExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 289 | Stmt *RewritePropertySetter(BinaryOperator *BinOp, Expr *newStmt, |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 290 | SourceRange SrcRange); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 291 | Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp); |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 292 | Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp); |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 293 | Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp); |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 294 | Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 295 | void WarnAboutReturnGotoStmts(Stmt *S); |
| 296 | void HasReturnStmts(Stmt *S, bool &hasReturns); |
| 297 | void RewriteTryReturnStmts(Stmt *S); |
| 298 | void RewriteSyncReturnStmts(Stmt *S, std::string buf); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 299 | Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 300 | Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 301 | Stmt *RewriteObjCCatchStmt(ObjCAtCatchStmt *S); |
| 302 | Stmt *RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S); |
| 303 | Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S); |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 304 | Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
| 305 | SourceLocation OrigEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 306 | CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD, |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 307 | Expr **args, unsigned nargs, |
| 308 | SourceLocation StartLoc=SourceLocation(), |
| 309 | SourceLocation EndLoc=SourceLocation()); |
| 310 | Stmt *SynthMessageExpr(ObjCMessageExpr *Exp, |
| 311 | SourceLocation StartLoc=SourceLocation(), |
| 312 | SourceLocation EndLoc=SourceLocation()); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 313 | Stmt *RewriteBreakStmt(BreakStmt *S); |
| 314 | Stmt *RewriteContinueStmt(ContinueStmt *S); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 315 | void SynthCountByEnumWithState(std::string &buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 316 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 317 | void SynthMsgSendFunctionDecl(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 318 | void SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 319 | void SynthMsgSendStretFunctionDecl(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 320 | void SynthMsgSendFpretFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 321 | void SynthMsgSendSuperStretFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 322 | void SynthGetClassFunctionDecl(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 323 | void SynthGetMetaClassFunctionDecl(); |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 324 | void SynthSelGetUidFunctionDecl(); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 325 | void SynthSuperContructorFunctionDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 326 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 327 | // Metadata emission. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 328 | void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 329 | std::string &Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 330 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 331 | void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 332 | std::string &Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 333 | |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 334 | template<typename MethodIterator> |
| 335 | void RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 336 | MethodIterator MethodEnd, |
Fariborz Jahanian | 8e991ba | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 337 | bool IsInstanceMethod, |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 338 | const char *prefix, |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 339 | const char *ClassName, |
| 340 | std::string &Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 341 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 342 | void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol, |
| 343 | const char *prefix, |
| 344 | const char *ClassName, |
| 345 | std::string &Result); |
| 346 | void RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Prots, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 347 | const char *prefix, |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 348 | const char *ClassName, |
| 349 | std::string &Result); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 350 | void SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 351 | std::string &Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 352 | void SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl, |
| 353 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 354 | std::string &Result); |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 355 | void RewriteImplementations(); |
| 356 | void SynthesizeMetaDataIntoBuffer(std::string &Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 357 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 358 | // Block rewriting. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 359 | void RewriteBlocksInFunctionProtoType(QualType funcType, NamedDecl *D); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 360 | void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 361 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 362 | void InsertBlockLiteralsWithinFunction(FunctionDecl *FD); |
| 363 | void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 364 | |
| 365 | // Block specific rewrite rules. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 366 | void RewriteBlockCall(CallExpr *Exp); |
| 367 | void RewriteBlockPointerDecl(NamedDecl *VD); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 368 | void RewriteByRefVar(VarDecl *VD); |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 369 | std::string SynthesizeByrefCopyDestroyHelper(VarDecl *VD, int flag); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 370 | Stmt *RewriteBlockDeclRefExpr(Expr *VD); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 371 | void RewriteBlockPointerFunctionArgs(FunctionDecl *FD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 372 | |
| 373 | std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 374 | const char *funcName, std::string Tag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 375 | std::string SynthesizeBlockFunc(BlockExpr *CE, int i, |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 376 | const char *funcName, std::string Tag); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 377 | std::string SynthesizeBlockImpl(BlockExpr *CE, |
| 378 | std::string Tag, std::string Desc); |
| 379 | std::string SynthesizeBlockDescriptor(std::string DescTag, |
| 380 | std::string ImplTag, |
| 381 | int i, const char *funcName, |
| 382 | unsigned hasCopy); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 383 | Stmt *SynthesizeBlockCall(CallExpr *Exp, const Expr* BlockExp); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 384 | void SynthesizeBlockLiterals(SourceLocation FunLocStart, |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 385 | const char *FunName); |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 386 | void RewriteRecordBody(RecordDecl *RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 387 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 388 | void CollectBlockDeclRefInfo(BlockExpr *Exp); |
| 389 | void GetBlockCallExprs(Stmt *S); |
| 390 | void GetBlockDeclRefExprs(Stmt *S); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 391 | void GetInnerBlockDeclRefExprs(Stmt *S, |
| 392 | llvm::SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs, |
| 393 | llvm::SmallPtrSet<ValueDecl *, 8> &InnerBlockValueDecls); |
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 | // We avoid calling Type::isBlockPointerType(), since it operates on the |
| 396 | // 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] | 397 | bool isTopLevelBlockPointerType(QualType T) { |
| 398 | return isa<BlockPointerType>(T); |
| 399 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 400 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 401 | // FIXME: This predicate seems like it would be useful to add to ASTContext. |
| 402 | bool isObjCType(QualType T) { |
| 403 | if (!LangOpts.ObjC1 && !LangOpts.ObjC2) |
| 404 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 405 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 406 | QualType OCT = Context->getCanonicalType(T).getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 407 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 408 | if (OCT == Context->getCanonicalType(Context->getObjCIdType()) || |
| 409 | OCT == Context->getCanonicalType(Context->getObjCClassType())) |
| 410 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 411 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 412 | if (const PointerType *PT = OCT->getAs<PointerType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 413 | if (isa<ObjCInterfaceType>(PT->getPointeeType()) || |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 414 | PT->getPointeeType()->isObjCQualifiedIdType()) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 415 | return true; |
| 416 | } |
| 417 | return false; |
| 418 | } |
| 419 | bool PointerTypeTakesAnyBlockArguments(QualType QT); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 420 | void GetExtentOfArgList(const char *Name, const char *&LParen, |
| 421 | const char *&RParen); |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 422 | void RewriteCastExpr(CStyleCastExpr *CE); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 423 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 424 | FunctionDecl *SynthBlockInitFunctionDecl(const char *name); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 425 | Stmt *SynthBlockInitExpr(BlockExpr *Exp, |
| 426 | const llvm::SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 427 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 428 | void QuoteDoublequotes(std::string &From, std::string &To) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 429 | for (unsigned i = 0; i < From.length(); i++) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 430 | if (From[i] == '"') |
| 431 | To += "\\\""; |
| 432 | else |
| 433 | To += From[i]; |
| 434 | } |
| 435 | } |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 436 | }; |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 437 | |
| 438 | // Helper function: create a CStyleCastExpr with trivial type source info. |
| 439 | CStyleCastExpr* NoTypeInfoCStyleCastExpr(ASTContext *Ctx, QualType Ty, |
| 440 | CastExpr::CastKind Kind, Expr *E) { |
| 441 | TypeSourceInfo *TInfo = Ctx->getTrivialTypeSourceInfo(Ty, SourceLocation()); |
| 442 | return new (Ctx) CStyleCastExpr(Ty, Kind, E, TInfo, |
| 443 | SourceLocation(), SourceLocation()); |
| 444 | } |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 447 | void RewriteObjC::RewriteBlocksInFunctionProtoType(QualType funcType, |
| 448 | NamedDecl *D) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 449 | if (FunctionProtoType *fproto = dyn_cast<FunctionProtoType>(funcType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 450 | for (FunctionProtoType::arg_type_iterator I = fproto->arg_type_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 451 | E = fproto->arg_type_end(); I && (I != E); ++I) |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 452 | if (isTopLevelBlockPointerType(*I)) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 453 | // All the args are checked/rewritten. Don't call twice! |
| 454 | RewriteBlockPointerDecl(D); |
| 455 | break; |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | void RewriteObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 461 | const PointerType *PT = funcType->getAs<PointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 462 | if (PT && PointerTypeTakesAnyBlockArguments(funcType)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 463 | RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 466 | static bool IsHeaderFile(const std::string &Filename) { |
| 467 | std::string::size_type DotPos = Filename.rfind('.'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 468 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 469 | if (DotPos == std::string::npos) { |
| 470 | // no file extension |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 471 | return false; |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 472 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 473 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 474 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 475 | // C header: .h |
| 476 | // C++ header: .hh or .H; |
| 477 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 478 | } |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 479 | |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 480 | RewriteObjC::RewriteObjC(std::string inFile, llvm::raw_ostream* OS, |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 481 | Diagnostic &D, const LangOptions &LOpts, |
| 482 | bool silenceMacroWarn) |
| 483 | : Diags(D), LangOpts(LOpts), InFileName(inFile), OutFile(OS), |
| 484 | SilenceRewriteMacroWarning(silenceMacroWarn) { |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 485 | IsHeader = IsHeaderFile(inFile); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 486 | RewriteFailedDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 487 | "rewriting sub-expression within a macro (may not be correct)"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 488 | TryFinallyContainsReturnDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 489 | "rewriter doesn't support user-specified control flow semantics " |
| 490 | "for @try/@finally (code may not execute properly)"); |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Eli Friedman | bce831b | 2009-05-18 22:29:17 +0000 | [diff] [blame] | 493 | ASTConsumer *clang::CreateObjCRewriter(const std::string& InFile, |
| 494 | llvm::raw_ostream* OS, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 495 | Diagnostic &Diags, |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 496 | const LangOptions &LOpts, |
| 497 | bool SilenceRewriteMacroWarning) { |
| 498 | return new RewriteObjC(InFile, OS, Diags, LOpts, SilenceRewriteMacroWarning); |
Chris Lattner | e365c50 | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 499 | } |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 500 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 501 | void RewriteObjC::Initialize(ASTContext &context) { |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 502 | Context = &context; |
| 503 | SM = &Context->getSourceManager(); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 504 | TUDecl = Context->getTranslationUnitDecl(); |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 505 | MsgSendFunctionDecl = 0; |
| 506 | MsgSendSuperFunctionDecl = 0; |
| 507 | MsgSendStretFunctionDecl = 0; |
| 508 | MsgSendSuperStretFunctionDecl = 0; |
| 509 | MsgSendFpretFunctionDecl = 0; |
| 510 | GetClassFunctionDecl = 0; |
| 511 | GetMetaClassFunctionDecl = 0; |
| 512 | SelGetUidFunctionDecl = 0; |
| 513 | CFStringFunctionDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 514 | ConstantStringClassReference = 0; |
| 515 | NSStringRecord = 0; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 516 | CurMethodDef = 0; |
| 517 | CurFunctionDef = 0; |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 518 | CurFunctionDeclToDeclareForBlock = 0; |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 519 | GlobalVarDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 520 | SuperStructDecl = 0; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 521 | ProtocolTypeDecl = 0; |
Steve Naroff | 9630ec5 | 2008-03-27 22:59:54 +0000 | [diff] [blame] | 522 | ConstantStringDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 523 | BcLabelCount = 0; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 524 | SuperContructorFunctionDecl = 0; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 525 | NumObjCStringLiterals = 0; |
Steve Naroff | 68272b8 | 2008-12-08 20:01:41 +0000 | [diff] [blame] | 526 | PropParentMap = 0; |
| 527 | CurrentBody = 0; |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 528 | DisableReplaceStmt = false; |
Fariborz Jahanian | f292fcf | 2010-01-07 22:51:18 +0000 | [diff] [blame] | 529 | objc_impl_method = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 530 | |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 531 | // Get the ID and start/end of the main file. |
| 532 | MainFileID = SM->getMainFileID(); |
| 533 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 534 | MainFileStart = MainBuf->getBufferStart(); |
| 535 | MainFileEnd = MainBuf->getBufferEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 536 | |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 537 | Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOptions()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 538 | |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 539 | // declaring objc_selector outside the parameter list removes a silly |
| 540 | // scope related warning... |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 541 | if (IsHeader) |
Steve Naroff | 62c2632 | 2009-02-03 20:39:18 +0000 | [diff] [blame] | 542 | Preamble = "#pragma once\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 543 | Preamble += "struct objc_selector; struct objc_class;\n"; |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 544 | Preamble += "struct __rw_objc_super { struct objc_object *object; "; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 545 | Preamble += "struct objc_object *superClass; "; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 546 | if (LangOpts.Microsoft) { |
| 547 | // Add a constructor for creating temporary objects. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 548 | Preamble += "__rw_objc_super(struct objc_object *o, struct objc_object *s) " |
| 549 | ": "; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 550 | Preamble += "object(o), superClass(s) {} "; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 551 | } |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 552 | Preamble += "};\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 553 | Preamble += "#ifndef _REWRITER_typedef_Protocol\n"; |
| 554 | Preamble += "typedef struct objc_object Protocol;\n"; |
| 555 | Preamble += "#define _REWRITER_typedef_Protocol\n"; |
| 556 | Preamble += "#endif\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 557 | if (LangOpts.Microsoft) { |
| 558 | Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n"; |
| 559 | Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n"; |
| 560 | } else |
| 561 | Preamble += "#define __OBJC_RW_DLLIMPORT extern\n"; |
| 562 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 563 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 564 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 565 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 566 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend_stret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 567 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 568 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper_stret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 569 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 570 | Preamble += "__OBJC_RW_DLLIMPORT double objc_msgSend_fpret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 571 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 572 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getClass"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 573 | Preamble += "(const char *);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 574 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getMetaClass"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 575 | Preamble += "(const char *);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 576 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw(struct objc_object *);\n"; |
| 577 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_enter(void *);\n"; |
| 578 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_exit(void *);\n"; |
| 579 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_exception_extract(void *);\n"; |
| 580 | Preamble += "__OBJC_RW_DLLIMPORT int objc_exception_match"; |
Steve Naroff | 580ca78 | 2008-05-09 21:17:56 +0000 | [diff] [blame] | 581 | Preamble += "(struct objc_class *, struct objc_object *);\n"; |
Steve Naroff | 59f05a4 | 2008-07-16 18:58:11 +0000 | [diff] [blame] | 582 | // @synchronized hooks. |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 583 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_enter(struct objc_object *);\n"; |
| 584 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_exit(struct objc_object *);\n"; |
| 585 | Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 586 | Preamble += "#ifndef __FASTENUMERATIONSTATE\n"; |
| 587 | Preamble += "struct __objcFastEnumerationState {\n\t"; |
| 588 | Preamble += "unsigned long state;\n\t"; |
Steve Naroff | b10f273 | 2008-04-04 22:58:22 +0000 | [diff] [blame] | 589 | Preamble += "void **itemsPtr;\n\t"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 590 | Preamble += "unsigned long *mutationsPtr;\n\t"; |
| 591 | Preamble += "unsigned long extra[5];\n};\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 592 | Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 593 | Preamble += "#define __FASTENUMERATIONSTATE\n"; |
| 594 | Preamble += "#endif\n"; |
| 595 | Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n"; |
| 596 | Preamble += "struct __NSConstantStringImpl {\n"; |
| 597 | Preamble += " int *isa;\n"; |
| 598 | Preamble += " int flags;\n"; |
| 599 | Preamble += " char *str;\n"; |
| 600 | Preamble += " long length;\n"; |
| 601 | Preamble += "};\n"; |
Steve Naroff | 88bee74 | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 602 | Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n"; |
| 603 | Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n"; |
| 604 | Preamble += "#else\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 605 | Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n"; |
Steve Naroff | 88bee74 | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 606 | Preamble += "#endif\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 607 | Preamble += "#define __NSCONSTANTSTRINGIMPL\n"; |
| 608 | Preamble += "#endif\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 609 | // Blocks preamble. |
| 610 | Preamble += "#ifndef BLOCK_IMPL\n"; |
| 611 | Preamble += "#define BLOCK_IMPL\n"; |
| 612 | Preamble += "struct __block_impl {\n"; |
| 613 | Preamble += " void *isa;\n"; |
| 614 | Preamble += " int Flags;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 615 | Preamble += " int Reserved;\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 616 | Preamble += " void *FuncPtr;\n"; |
| 617 | Preamble += "};\n"; |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 618 | Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n"; |
Steve Naroff | c9c1e9c | 2009-12-06 01:52:22 +0000 | [diff] [blame] | 619 | Preamble += "#ifdef __OBJC_EXPORT_BLOCKS\n"; |
Steve Naroff | a851e60 | 2009-12-06 01:33:56 +0000 | [diff] [blame] | 620 | Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_assign(void *, const void *, const int);\n"; |
| 621 | Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_dispose(const void *, const int);\n"; |
| 622 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteGlobalBlock[32];\n"; |
| 623 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteStackBlock[32];\n"; |
| 624 | Preamble += "#else\n"; |
Steve Naroff | cd82637 | 2010-01-05 18:09:31 +0000 | [diff] [blame] | 625 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_assign(void *, const void *, const int);\n"; |
| 626 | 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] | 627 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteGlobalBlock[32];\n"; |
| 628 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteStackBlock[32];\n"; |
| 629 | Preamble += "#endif\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 630 | Preamble += "#endif\n"; |
Steve Naroff | a48396e | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 631 | if (LangOpts.Microsoft) { |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 632 | Preamble += "#undef __OBJC_RW_DLLIMPORT\n"; |
| 633 | Preamble += "#undef __OBJC_RW_STATICIMPORT\n"; |
Steve Naroff | a48396e | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 634 | Preamble += "#define __attribute__(X)\n"; |
Fariborz Jahanian | 3420419 | 2010-01-15 22:29:39 +0000 | [diff] [blame] | 635 | Preamble += "#define __weak\n"; |
Steve Naroff | a48396e | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 636 | } |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 637 | else { |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 638 | Preamble += "#define __block\n"; |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 639 | Preamble += "#define __weak\n"; |
| 640 | } |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 644 | //===----------------------------------------------------------------------===// |
| 645 | // Top Level Driver Code |
| 646 | //===----------------------------------------------------------------------===// |
| 647 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 648 | void RewriteObjC::HandleTopLevelSingleDecl(Decl *D) { |
Ted Kremenek | e50187a | 2010-02-05 21:28:51 +0000 | [diff] [blame] | 649 | if (Diags.hasErrorOccurred()) |
| 650 | return; |
| 651 | |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 652 | // Two cases: either the decl could be in the main file, or it could be in a |
| 653 | // #included file. If the former, rewrite it now. If the later, check to see |
| 654 | // if we rewrote the #include/#import. |
| 655 | SourceLocation Loc = D->getLocation(); |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 656 | Loc = SM->getInstantiationLoc(Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 657 | |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 658 | // If this is for a builtin, ignore it. |
| 659 | if (Loc.isInvalid()) return; |
| 660 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 661 | // Look for built-in declarations that we need to refer during the rewrite. |
| 662 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 663 | RewriteFunctionDecl(FD); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 664 | } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) { |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 665 | // declared in <Foundation/NSString.h> |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 666 | if (strcmp(FVD->getNameAsCString(), "_NSConstantStringClassReference") == 0) { |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 667 | ConstantStringClassReference = FVD; |
| 668 | return; |
| 669 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 670 | } else if (ObjCInterfaceDecl *MD = dyn_cast<ObjCInterfaceDecl>(D)) { |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 671 | RewriteInterfaceDecl(MD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 672 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 673 | RewriteCategoryDecl(CD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 674 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 675 | RewriteProtocolDecl(PD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 676 | } else if (ObjCForwardProtocolDecl *FP = |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 677 | dyn_cast<ObjCForwardProtocolDecl>(D)){ |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 678 | RewriteForwardProtocolDecl(FP); |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 679 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
| 680 | // Recurse into linkage specifications |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 681 | for (DeclContext::decl_iterator DI = LSD->decls_begin(), |
| 682 | DIEnd = LSD->decls_end(); |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 683 | DI != DIEnd; ++DI) |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 684 | HandleTopLevelSingleDecl(*DI); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 685 | } |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 686 | // 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] | 687 | if (SM->isFromMainFile(Loc)) |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 688 | return HandleDeclInMainFile(D); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 691 | //===----------------------------------------------------------------------===// |
| 692 | // Syntactic (non-AST) Rewriting Code |
| 693 | //===----------------------------------------------------------------------===// |
| 694 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 695 | void RewriteObjC::RewriteInclude() { |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 696 | SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 697 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
| 698 | const char *MainBufStart = MainBuf.first; |
| 699 | const char *MainBufEnd = MainBuf.second; |
| 700 | size_t ImportLen = strlen("import"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 701 | |
Fariborz Jahanian | af57b46 | 2008-01-19 01:03:17 +0000 | [diff] [blame] | 702 | // Loop over the whole file, looking for includes. |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 703 | for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { |
| 704 | if (*BufPtr == '#') { |
| 705 | if (++BufPtr == MainBufEnd) |
| 706 | return; |
| 707 | while (*BufPtr == ' ' || *BufPtr == '\t') |
| 708 | if (++BufPtr == MainBufEnd) |
| 709 | return; |
| 710 | if (!strncmp(BufPtr, "import", ImportLen)) { |
| 711 | // replace import with include |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 712 | SourceLocation ImportLoc = |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 713 | LocStart.getFileLocWithOffset(BufPtr-MainBufStart); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 714 | ReplaceText(ImportLoc, ImportLen, "include"); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 715 | BufPtr += ImportLen; |
| 716 | } |
| 717 | } |
| 718 | } |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 719 | } |
| 720 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 721 | void RewriteObjC::RewriteTabs() { |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 722 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
| 723 | const char *MainBufStart = MainBuf.first; |
| 724 | const char *MainBufEnd = MainBuf.second; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 725 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 726 | // Loop over the whole file, looking for tabs. |
| 727 | for (const char *BufPtr = MainBufStart; BufPtr != MainBufEnd; ++BufPtr) { |
| 728 | if (*BufPtr != '\t') |
| 729 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 730 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 731 | // Okay, we found a tab. This tab will turn into at least one character, |
| 732 | // but it depends on which 'virtual column' it is in. Compute that now. |
| 733 | unsigned VCol = 0; |
| 734 | while (BufPtr-VCol != MainBufStart && BufPtr[-VCol-1] != '\t' && |
| 735 | BufPtr[-VCol-1] != '\n' && BufPtr[-VCol-1] != '\r') |
| 736 | ++VCol; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 737 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 738 | // Okay, now that we know the virtual column, we know how many spaces to |
| 739 | // insert. We assume 8-character tab-stops. |
| 740 | unsigned Spaces = 8-(VCol & 7); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 741 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 742 | // Get the location of the tab. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 743 | SourceLocation TabLoc = SM->getLocForStartOfFile(MainFileID); |
| 744 | TabLoc = TabLoc.getFileLocWithOffset(BufPtr-MainBufStart); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 745 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 746 | // Rewrite the single tab character into a sequence of spaces. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 747 | ReplaceText(TabLoc, 1, llvm::StringRef(" ", Spaces)); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 748 | } |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 751 | static std::string getIvarAccessString(ObjCInterfaceDecl *ClassDecl, |
| 752 | ObjCIvarDecl *OID) { |
| 753 | std::string S; |
| 754 | S = "((struct "; |
| 755 | S += ClassDecl->getIdentifier()->getName(); |
| 756 | S += "_IMPL *)self)->"; |
Daniel Dunbar | 5ffe14c | 2009-10-18 20:26:27 +0000 | [diff] [blame] | 757 | S += OID->getName(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 758 | return S; |
| 759 | } |
| 760 | |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 761 | void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 762 | ObjCImplementationDecl *IMD, |
| 763 | ObjCCategoryImplDecl *CID) { |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 764 | SourceLocation startLoc = PID->getLocStart(); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 765 | InsertText(startLoc, "// "); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 766 | const char *startBuf = SM->getCharacterData(startLoc); |
| 767 | assert((*startBuf == '@') && "bogus @synthesize location"); |
| 768 | const char *semiBuf = strchr(startBuf, ';'); |
| 769 | assert((*semiBuf == ';') && "@synthesize: can't find ';'"); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 770 | SourceLocation onePastSemiLoc = |
| 771 | startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 772 | |
| 773 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 774 | return; // FIXME: is this correct? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 775 | |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 776 | // Generate the 'getter' function. |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 777 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 778 | ObjCInterfaceDecl *ClassDecl = PD->getGetterMethodDecl()->getClassInterface(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 779 | ObjCIvarDecl *OID = PID->getPropertyIvarDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 780 | |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 781 | if (!OID) |
| 782 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 783 | |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 784 | std::string Getr; |
| 785 | RewriteObjCMethodDecl(PD->getGetterMethodDecl(), Getr); |
| 786 | Getr += "{ "; |
| 787 | // Synthesize an explicit cast to gain access to 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 | 3539cdb | 2008-12-02 17:54:50 +0000 | [diff] [blame] | 790 | // See objc-act.c:objc_synthesize_new_getter() for details. |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 791 | Getr += "return " + getIvarAccessString(ClassDecl, OID); |
| 792 | Getr += "; }"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 793 | InsertText(onePastSemiLoc, Getr); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 794 | if (PD->isReadOnly()) |
| 795 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 796 | |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 797 | // Generate the 'setter' function. |
| 798 | std::string Setr; |
| 799 | RewriteObjCMethodDecl(PD->getSetterMethodDecl(), Setr); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 800 | Setr += "{ "; |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 801 | // Synthesize an explicit cast to initialize the ivar. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 802 | // FIXME: deal with code generation implications for various property |
| 803 | // attributes (copy, retain, nonatomic). |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 804 | // See objc-act.c:objc_synthesize_new_setter() for details. |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 805 | Setr += getIvarAccessString(ClassDecl, OID) + " = "; |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 806 | Setr += PD->getNameAsCString(); |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 807 | Setr += "; }"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 808 | InsertText(onePastSemiLoc, Setr); |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 809 | } |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 810 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 811 | void RewriteObjC::RewriteForwardClassDecl(ObjCClassDecl *ClassDecl) { |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 812 | // Get the start location and compute the semi location. |
| 813 | SourceLocation startLoc = ClassDecl->getLocation(); |
| 814 | const char *startBuf = SM->getCharacterData(startLoc); |
| 815 | const char *semiPtr = strchr(startBuf, ';'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 816 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 817 | // Translate to typedef's that forward reference structs with the same name |
| 818 | // as the class. As a convenience, we include the original declaration |
| 819 | // as a comment. |
| 820 | std::string typedefString; |
Fariborz Jahanian | 91fbd12 | 2010-01-11 22:48:40 +0000 | [diff] [blame] | 821 | typedefString += "// @class "; |
| 822 | for (ObjCClassDecl::iterator I = ClassDecl->begin(), E = ClassDecl->end(); |
| 823 | I != E; ++I) { |
| 824 | ObjCInterfaceDecl *ForwardDecl = I->getInterface(); |
| 825 | typedefString += ForwardDecl->getNameAsString(); |
| 826 | if (I+1 != E) |
| 827 | typedefString += ", "; |
| 828 | else |
| 829 | typedefString += ";\n"; |
| 830 | } |
| 831 | |
Chris Lattner | 6795605 | 2009-02-20 18:04:31 +0000 | [diff] [blame] | 832 | for (ObjCClassDecl::iterator I = ClassDecl->begin(), E = ClassDecl->end(); |
| 833 | I != E; ++I) { |
Ted Kremenek | 321c22f | 2009-11-18 00:28:11 +0000 | [diff] [blame] | 834 | ObjCInterfaceDecl *ForwardDecl = I->getInterface(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 835 | typedefString += "#ifndef _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 836 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 837 | typedefString += "\n"; |
| 838 | typedefString += "#define _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 839 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 840 | typedefString += "\n"; |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 841 | typedefString += "typedef struct objc_object "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 842 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 843 | typedefString += ";\n#endif\n"; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 844 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 845 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 846 | // Replace the @class with typedefs corresponding to the classes. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 847 | ReplaceText(startLoc, semiPtr-startBuf+1, typedefString); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 850 | void RewriteObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) { |
Fariborz Jahanian | d050240 | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 851 | // When method is a synthesized one, such as a getter/setter there is |
| 852 | // nothing to rewrite. |
| 853 | if (Method->isSynthesized()) |
| 854 | return; |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 855 | SourceLocation LocStart = Method->getLocStart(); |
| 856 | SourceLocation LocEnd = Method->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 857 | |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 858 | if (SM->getInstantiationLineNumber(LocEnd) > |
| 859 | SM->getInstantiationLineNumber(LocStart)) { |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 860 | InsertText(LocStart, "#if 0\n"); |
| 861 | ReplaceText(LocEnd, 1, ";\n#endif\n"); |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 862 | } else { |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 863 | InsertText(LocStart, "// "); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 864 | } |
| 865 | } |
| 866 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 867 | void RewriteObjC::RewriteProperty(ObjCPropertyDecl *prop) { |
Fariborz Jahanian | d050240 | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 868 | SourceLocation Loc = prop->getAtLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 869 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 870 | ReplaceText(Loc, 0, "// "); |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 871 | // FIXME: handle properties that are declared across multiple lines. |
Fariborz Jahanian | 957cf65 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 874 | void RewriteObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 875 | SourceLocation LocStart = CatDecl->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 876 | |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 877 | // FIXME: handle category headers that are declared across multiple lines. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 878 | ReplaceText(LocStart, 0, "// "); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 879 | |
Fariborz Jahanian | 13751e3 | 2010-02-10 01:15:09 +0000 | [diff] [blame] | 880 | for (ObjCCategoryDecl::prop_iterator I = CatDecl->prop_begin(), |
| 881 | E = CatDecl->prop_end(); I != E; ++I) |
| 882 | RewriteProperty(*I); |
| 883 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 884 | for (ObjCCategoryDecl::instmeth_iterator |
| 885 | I = CatDecl->instmeth_begin(), E = CatDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 886 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 887 | RewriteMethodDeclaration(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 888 | for (ObjCCategoryDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 889 | I = CatDecl->classmeth_begin(), E = CatDecl->classmeth_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); |
| 892 | |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 893 | // Lastly, comment out the @end. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 894 | ReplaceText(CatDecl->getAtEndRange().getBegin(), 0, "// "); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 895 | } |
| 896 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 897 | void RewriteObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 898 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 899 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 900 | SourceLocation LocStart = PDecl->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 901 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 902 | // FIXME: handle protocol headers that are declared across multiple lines. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 903 | ReplaceText(LocStart, 0, "// "); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 904 | |
| 905 | for (ObjCProtocolDecl::instmeth_iterator |
| 906 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 907 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 908 | RewriteMethodDeclaration(*I); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 909 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 910 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 911 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 912 | RewriteMethodDeclaration(*I); |
| 913 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 914 | // Lastly, comment out the @end. |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 915 | SourceLocation LocEnd = PDecl->getAtEndRange().getBegin(); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 916 | ReplaceText(LocEnd, 0, "// "); |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 917 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 918 | // Must comment out @optional/@required |
| 919 | const char *startBuf = SM->getCharacterData(LocStart); |
| 920 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 921 | for (const char *p = startBuf; p < endBuf; p++) { |
| 922 | if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 923 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 924 | ReplaceText(OptionalLoc, strlen("@optional"), "/* @optional */"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 925 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 926 | } |
| 927 | else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 928 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 929 | ReplaceText(OptionalLoc, strlen("@required"), "/* @required */"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 930 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 931 | } |
| 932 | } |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 933 | } |
| 934 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 935 | void RewriteObjC::RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *PDecl) { |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 936 | SourceLocation LocStart = PDecl->getLocation(); |
Steve Naroff | b7fa992 | 2007-11-14 03:37:28 +0000 | [diff] [blame] | 937 | if (LocStart.isInvalid()) |
| 938 | assert(false && "Invalid SourceLocation"); |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 939 | // FIXME: handle forward protocol that are declared across multiple lines. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 940 | ReplaceText(LocStart, 0, "// "); |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 941 | } |
| 942 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 943 | void RewriteObjC::RewriteObjCMethodDecl(ObjCMethodDecl *OMD, |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 944 | std::string &ResultStr) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 945 | //fprintf(stderr,"In RewriteObjCMethodDecl\n"); |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 946 | const FunctionType *FPRetType = 0; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 947 | ResultStr += "\nstatic "; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 948 | if (OMD->getResultType()->isObjCQualifiedIdType()) |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 949 | ResultStr += "id"; |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 950 | else if (OMD->getResultType()->isFunctionPointerType() || |
| 951 | OMD->getResultType()->isBlockPointerType()) { |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 952 | // needs special handling, since pointer-to-functions have special |
| 953 | // syntax (where a decaration models use). |
| 954 | QualType retType = OMD->getResultType(); |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 955 | QualType PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 956 | if (const PointerType* PT = retType->getAs<PointerType>()) |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 957 | PointeeTy = PT->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 958 | else if (const BlockPointerType *BPT = retType->getAs<BlockPointerType>()) |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 959 | PointeeTy = BPT->getPointeeType(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 960 | if ((FPRetType = PointeeTy->getAs<FunctionType>())) { |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 961 | ResultStr += FPRetType->getResultType().getAsString(); |
| 962 | ResultStr += "(*"; |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 963 | } |
| 964 | } else |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 965 | ResultStr += OMD->getResultType().getAsString(); |
Fariborz Jahanian | 531a1ea | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 966 | ResultStr += " "; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 967 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 968 | // Unique method name |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 969 | std::string NameStr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 970 | |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 971 | if (OMD->isInstanceMethod()) |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 972 | NameStr += "_I_"; |
| 973 | else |
| 974 | NameStr += "_C_"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 975 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 976 | NameStr += OMD->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 977 | NameStr += "_"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 978 | |
| 979 | if (ObjCCategoryImplDecl *CID = |
Steve Naroff | 3e0a540 | 2009-01-08 19:41:02 +0000 | [diff] [blame] | 980 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 981 | NameStr += CID->getNameAsString(); |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 982 | NameStr += "_"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 983 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 984 | // Append selector names, replacing ':' with '_' |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 985 | { |
| 986 | std::string selString = OMD->getSelector().getAsString(); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 987 | int len = selString.size(); |
| 988 | for (int i = 0; i < len; i++) |
| 989 | if (selString[i] == ':') |
| 990 | selString[i] = '_'; |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 991 | NameStr += selString; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 992 | } |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 993 | // Remember this name for metadata emission |
| 994 | MethodInternalNames[OMD] = NameStr; |
| 995 | ResultStr += NameStr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 996 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 997 | // Rewrite arguments |
| 998 | ResultStr += "("; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 999 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1000 | // invisible arguments |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1001 | if (OMD->isInstanceMethod()) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1002 | QualType selfTy = Context->getObjCInterfaceType(OMD->getClassInterface()); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1003 | selfTy = Context->getPointerType(selfTy); |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 1004 | if (!LangOpts.Microsoft) { |
| 1005 | if (ObjCSynthesizedStructs.count(OMD->getClassInterface())) |
| 1006 | ResultStr += "struct "; |
| 1007 | } |
| 1008 | // When rewriting for Microsoft, explicitly omit the structure name. |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1009 | ResultStr += OMD->getClassInterface()->getNameAsString(); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1010 | ResultStr += " *"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1011 | } |
| 1012 | else |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 1013 | ResultStr += Context->getObjCClassType().getAsString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1014 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1015 | ResultStr += " self, "; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1016 | ResultStr += Context->getObjCSelType().getAsString(); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1017 | ResultStr += " _cmd"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1018 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1019 | // Method arguments. |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1020 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 1021 | E = OMD->param_end(); PI != E; ++PI) { |
| 1022 | ParmVarDecl *PDecl = *PI; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1023 | ResultStr += ", "; |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1024 | if (PDecl->getType()->isObjCQualifiedIdType()) { |
| 1025 | ResultStr += "id "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1026 | ResultStr += PDecl->getNameAsString(); |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1027 | } else { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1028 | std::string Name = PDecl->getNameAsString(); |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 1029 | if (isTopLevelBlockPointerType(PDecl->getType())) { |
Steve Naroff | c8ad87b | 2008-10-30 14:45:29 +0000 | [diff] [blame] | 1030 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1031 | const BlockPointerType *BPT = PDecl->getType()->getAs<BlockPointerType>(); |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1032 | Context->getPointerType(BPT->getPointeeType()).getAsStringInternal(Name, |
| 1033 | Context->PrintingPolicy); |
Steve Naroff | c8ad87b | 2008-10-30 14:45:29 +0000 | [diff] [blame] | 1034 | } else |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1035 | PDecl->getType().getAsStringInternal(Name, Context->PrintingPolicy); |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1036 | ResultStr += Name; |
| 1037 | } |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1038 | } |
Fariborz Jahanian | 7c39ff7 | 2008-01-21 20:14:23 +0000 | [diff] [blame] | 1039 | if (OMD->isVariadic()) |
| 1040 | ResultStr += ", ..."; |
Fariborz Jahanian | 531a1ea | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 1041 | ResultStr += ") "; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1042 | |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1043 | if (FPRetType) { |
| 1044 | ResultStr += ")"; // close the precedence "scope" for "*". |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1045 | |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1046 | // Now, emit the argument types (if any). |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1047 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) { |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1048 | ResultStr += "("; |
| 1049 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 1050 | if (i) ResultStr += ", "; |
| 1051 | std::string ParamStr = FT->getArgType(i).getAsString(); |
| 1052 | ResultStr += ParamStr; |
| 1053 | } |
| 1054 | if (FT->isVariadic()) { |
| 1055 | if (FT->getNumArgs()) ResultStr += ", "; |
| 1056 | ResultStr += "..."; |
| 1057 | } |
| 1058 | ResultStr += ")"; |
| 1059 | } else { |
| 1060 | ResultStr += "()"; |
| 1061 | } |
| 1062 | } |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1063 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1064 | void RewriteObjC::RewriteImplementationDecl(Decl *OID) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1065 | ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID); |
| 1066 | ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1067 | |
Fariborz Jahanian | a135216 | 2010-02-15 21:11:41 +0000 | [diff] [blame] | 1068 | InsertText(IMD ? IMD->getLocStart() : CID->getLocStart(), "// "); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1069 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1070 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1071 | I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(), |
| 1072 | E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1073 | I != E; ++I) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1074 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1075 | ObjCMethodDecl *OMD = *I; |
| 1076 | RewriteObjCMethodDecl(OMD, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1077 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1078 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1079 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1080 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1081 | const char *endBuf = SM->getCharacterData(LocEnd); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1082 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1083 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1084 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1085 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1086 | I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(), |
| 1087 | E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1088 | I != E; ++I) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1089 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1090 | ObjCMethodDecl *OMD = *I; |
| 1091 | RewriteObjCMethodDecl(OMD, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1092 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1093 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1094 | |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1095 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1096 | const char *endBuf = SM->getCharacterData(LocEnd); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1097 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1098 | } |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1099 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1100 | I = IMD ? IMD->propimpl_begin() : CID->propimpl_begin(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1101 | E = IMD ? IMD->propimpl_end() : CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1102 | I != E; ++I) { |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 1103 | RewritePropertyImplDecl(*I, IMD, CID); |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1106 | InsertText(IMD ? IMD->getLocEnd() : CID->getLocEnd(), "// "); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1109 | void RewriteObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { |
Steve Naroff | f908a87 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 1110 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1111 | if (!ObjCForwardDecls.count(ClassDecl)) { |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1112 | // we haven't seen a forward decl - generate a typedef. |
Steve Naroff | 5086a8d | 2007-11-14 23:02:56 +0000 | [diff] [blame] | 1113 | ResultStr = "#ifndef _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1114 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1115 | ResultStr += "\n"; |
| 1116 | ResultStr += "#define _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1117 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1118 | ResultStr += "\n"; |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1119 | ResultStr += "typedef struct objc_object "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1120 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1121 | ResultStr += ";\n#endif\n"; |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1122 | // Mark this typedef as having been generated. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1123 | ObjCForwardDecls.insert(ClassDecl); |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1124 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1125 | SynthesizeObjCInternalStruct(ClassDecl, ResultStr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1126 | |
| 1127 | for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1128 | E = ClassDecl->prop_end(); I != E; ++I) |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 1129 | RewriteProperty(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1130 | for (ObjCInterfaceDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1131 | I = ClassDecl->instmeth_begin(), E = ClassDecl->instmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1132 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1133 | RewriteMethodDeclaration(*I); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1134 | for (ObjCInterfaceDecl::classmeth_iterator |
| 1135 | I = ClassDecl->classmeth_begin(), E = ClassDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1136 | I != E; ++I) |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1137 | RewriteMethodDeclaration(*I); |
| 1138 | |
Steve Naroff | 2feac5e | 2007-10-30 03:43:13 +0000 | [diff] [blame] | 1139 | // Lastly, comment out the @end. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1140 | ReplaceText(ClassDecl->getAtEndRange().getBegin(), 0, "// "); |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 1143 | Stmt *RewriteObjC::RewritePropertySetter(BinaryOperator *BinOp, Expr *newStmt, |
| 1144 | SourceRange SrcRange) { |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1145 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr. |
| 1146 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
| 1147 | ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS()); |
| 1148 | ObjCMessageExpr *MsgExpr; |
| 1149 | ObjCPropertyDecl *PDecl = PropRefExpr->getProperty(); |
| 1150 | llvm::SmallVector<Expr *, 1> ExprVec; |
| 1151 | ExprVec.push_back(newStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1152 | |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1153 | Stmt *Receiver = PropRefExpr->getBase(); |
| 1154 | ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(Receiver); |
| 1155 | if (PRE && PropGetters[PRE]) { |
| 1156 | // This allows us to handle chain/nested property getters. |
| 1157 | Receiver = PropGetters[PRE]; |
| 1158 | } |
Ted Kremenek | eb3b324 | 2010-02-11 22:41:21 +0000 | [diff] [blame] | 1159 | MsgExpr = new (Context) ObjCMessageExpr(*Context, dyn_cast<Expr>(Receiver), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1160 | PDecl->getSetterName(), PDecl->getType(), |
| 1161 | PDecl->getSetterMethodDecl(), |
| 1162 | SourceLocation(), SourceLocation(), |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1163 | &ExprVec[0], 1); |
| 1164 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1165 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1166 | // Now do the actual rewrite. |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 1167 | ReplaceStmtWithRange(BinOp, ReplacingStmt, SrcRange); |
Steve Naroff | e58ee0c | 2008-12-10 14:53:27 +0000 | [diff] [blame] | 1168 | //delete BinOp; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1169 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1170 | // to things that stay around. |
| 1171 | Context->Deallocate(MsgExpr); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1172 | return ReplacingStmt; |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1175 | Stmt *RewriteObjC::RewritePropertyGetter(ObjCPropertyRefExpr *PropRefExpr) { |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1176 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr. |
| 1177 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
| 1178 | ObjCMessageExpr *MsgExpr; |
| 1179 | ObjCPropertyDecl *PDecl = PropRefExpr->getProperty(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1180 | |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1181 | Stmt *Receiver = PropRefExpr->getBase(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1182 | |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1183 | ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(Receiver); |
| 1184 | if (PRE && PropGetters[PRE]) { |
| 1185 | // This allows us to handle chain/nested property getters. |
| 1186 | Receiver = PropGetters[PRE]; |
| 1187 | } |
Ted Kremenek | eb3b324 | 2010-02-11 22:41:21 +0000 | [diff] [blame] | 1188 | MsgExpr = new (Context) ObjCMessageExpr(*Context, dyn_cast<Expr>(Receiver), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1189 | PDecl->getGetterName(), PDecl->getType(), |
| 1190 | PDecl->getGetterMethodDecl(), |
| 1191 | SourceLocation(), SourceLocation(), |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1192 | 0, 0); |
| 1193 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1194 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1195 | |
| 1196 | if (!PropParentMap) |
| 1197 | PropParentMap = new ParentMap(CurrentBody); |
| 1198 | |
| 1199 | Stmt *Parent = PropParentMap->getParent(PropRefExpr); |
| 1200 | if (Parent && isa<ObjCPropertyRefExpr>(Parent)) { |
| 1201 | // We stash away the ReplacingStmt since actually doing the |
| 1202 | // replacement/rewrite won't work for nested getters (e.g. obj.p.i) |
| 1203 | PropGetters[PropRefExpr] = ReplacingStmt; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1204 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1205 | // to things that stay around. |
| 1206 | Context->Deallocate(MsgExpr); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1207 | return PropRefExpr; // return the original... |
| 1208 | } else { |
| 1209 | ReplaceStmt(PropRefExpr, ReplacingStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1210 | // delete PropRefExpr; elsewhere... |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1211 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1212 | // to things that stay around. |
| 1213 | Context->Deallocate(MsgExpr); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1214 | return ReplacingStmt; |
| 1215 | } |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1218 | Stmt *RewriteObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1219 | SourceLocation OrigStart, |
| 1220 | bool &replaced) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1221 | ObjCIvarDecl *D = IV->getDecl(); |
Fariborz Jahanian | 84ed600 | 2010-01-07 18:18:32 +0000 | [diff] [blame] | 1222 | const Expr *BaseExpr = IV->getBase(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1223 | if (CurMethodDef) { |
Fariborz Jahanian | 8f09543 | 2010-01-26 18:28:51 +0000 | [diff] [blame] | 1224 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1225 | ObjCInterfaceType *iFaceDecl = |
Fariborz Jahanian | 84ed600 | 2010-01-07 18:18:32 +0000 | [diff] [blame] | 1226 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
Fariborz Jahanian | ffbdead | 2010-01-26 20:37:44 +0000 | [diff] [blame] | 1227 | assert(iFaceDecl && "RewriteObjCIvarRefExpr - iFaceDecl is null"); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1228 | // lookup which class implements the instance variable. |
| 1229 | ObjCInterfaceDecl *clsDeclared = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1230 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1231 | clsDeclared); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1232 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1233 | |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1234 | // Synthesize an explicit cast to gain access to the ivar. |
| 1235 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1236 | RecName += "_IMPL"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1237 | IdentifierInfo *II = &Context->Idents.get(RecName); |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1238 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1239 | SourceLocation(), II); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1240 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1241 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1242 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, |
| 1243 | CastExpr::CK_Unknown, |
| 1244 | IV->getBase()); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1245 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1246 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
| 1247 | IV->getBase()->getLocEnd(), |
| 1248 | castExpr); |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1249 | replaced = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1250 | if (IV->isFreeIvar() && |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1251 | CurMethodDef->getClassInterface() == iFaceDecl->getDecl()) { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1252 | MemberExpr *ME = new (Context) MemberExpr(PE, true, D, |
| 1253 | IV->getLocation(), |
| 1254 | D->getType()); |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1255 | // delete IV; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1256 | return ME; |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1257 | } |
Fariborz Jahanian | 7e20ffe | 2010-01-28 01:41:20 +0000 | [diff] [blame] | 1258 | // Get the new text |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1259 | // Cannot delete IV->getBase(), since PE points to it. |
| 1260 | // Replace the old base with the cast. This is important when doing |
| 1261 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1262 | IV->setBase(PE); |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1263 | return IV; |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1264 | } |
Steve Naroff | 84472a8 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1265 | } else { // we are outside a method. |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1266 | assert(!IV->isFreeIvar() && "Cannot have a free standing ivar outside a method"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1267 | |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1268 | // Explicit ivar refs need to have a cast inserted. |
| 1269 | // FIXME: consider sharing some of this code with the code above. |
Fariborz Jahanian | 26337b2 | 2010-01-12 17:31:23 +0000 | [diff] [blame] | 1270 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
Fariborz Jahanian | c374cd9 | 2010-01-11 17:50:35 +0000 | [diff] [blame] | 1271 | ObjCInterfaceType *iFaceDecl = |
| 1272 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1273 | // lookup which class implements the instance variable. |
| 1274 | ObjCInterfaceDecl *clsDeclared = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1275 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1276 | clsDeclared); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1277 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1278 | |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1279 | // Synthesize an explicit cast to gain access to the ivar. |
| 1280 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1281 | RecName += "_IMPL"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1282 | IdentifierInfo *II = &Context->Idents.get(RecName); |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1283 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1284 | SourceLocation(), II); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1285 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1286 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1287 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, |
| 1288 | CastExpr::CK_Unknown, |
| 1289 | IV->getBase()); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1290 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1291 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
Chris Lattner | 8d36616 | 2008-05-28 16:38:23 +0000 | [diff] [blame] | 1292 | IV->getBase()->getLocEnd(), castExpr); |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1293 | replaced = true; |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1294 | // Cannot delete IV->getBase(), since PE points to it. |
| 1295 | // Replace the old base with the cast. This is important when doing |
| 1296 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1297 | IV->setBase(PE); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1298 | return IV; |
| 1299 | } |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1300 | } |
Steve Naroff | 84472a8 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1301 | return IV; |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1304 | Stmt *RewriteObjC::RewriteObjCNestedIvarRefExpr(Stmt *S, bool &replaced) { |
| 1305 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1306 | CI != E; ++CI) { |
| 1307 | if (*CI) { |
| 1308 | Stmt *newStmt = RewriteObjCNestedIvarRefExpr(*CI, replaced); |
| 1309 | if (newStmt) |
| 1310 | *CI = newStmt; |
| 1311 | } |
| 1312 | } |
| 1313 | if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) { |
| 1314 | SourceRange OrigStmtRange = S->getSourceRange(); |
| 1315 | Stmt *newStmt = RewriteObjCIvarRefExpr(IvarRefExpr, OrigStmtRange.getBegin(), |
| 1316 | replaced); |
| 1317 | return newStmt; |
Fariborz Jahanian | 376338a | 2010-02-05 17:48:10 +0000 | [diff] [blame] | 1318 | } |
| 1319 | if (ObjCMessageExpr *MsgRefExpr = dyn_cast<ObjCMessageExpr>(S)) { |
| 1320 | Stmt *newStmt = SynthMessageExpr(MsgRefExpr); |
| 1321 | return newStmt; |
| 1322 | } |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1323 | return S; |
| 1324 | } |
| 1325 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1326 | /// SynthCountByEnumWithState - To print: |
| 1327 | /// ((unsigned int (*) |
| 1328 | /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1329 | /// (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1330 | /// sel_registerName( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1331 | /// "countByEnumeratingWithState:objects:count:"), |
| 1332 | /// &enumState, |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1333 | /// (id *)items, (unsigned int)16) |
| 1334 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1335 | void RewriteObjC::SynthCountByEnumWithState(std::string &buf) { |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1336 | buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, " |
| 1337 | "id *, unsigned int))(void *)objc_msgSend)"; |
| 1338 | buf += "\n\t\t"; |
| 1339 | buf += "((id)l_collection,\n\t\t"; |
| 1340 | buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; |
| 1341 | buf += "\n\t\t"; |
| 1342 | buf += "&enumState, " |
| 1343 | "(id *)items, (unsigned int)16)"; |
| 1344 | } |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1345 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1346 | /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach |
| 1347 | /// statement to exit to its outer synthesized loop. |
| 1348 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1349 | Stmt *RewriteObjC::RewriteBreakStmt(BreakStmt *S) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1350 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1351 | return S; |
| 1352 | // replace break with goto __break_label |
| 1353 | std::string buf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1354 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1355 | SourceLocation startLoc = S->getLocStart(); |
| 1356 | buf = "goto __break_label_"; |
| 1357 | buf += utostr(ObjCBcLabelNo.back()); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1358 | ReplaceText(startLoc, strlen("break"), buf); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1359 | |
| 1360 | return 0; |
| 1361 | } |
| 1362 | |
| 1363 | /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach |
| 1364 | /// statement to continue with its inner synthesized loop. |
| 1365 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1366 | Stmt *RewriteObjC::RewriteContinueStmt(ContinueStmt *S) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1367 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1368 | return S; |
| 1369 | // replace continue with goto __continue_label |
| 1370 | std::string buf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1371 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1372 | SourceLocation startLoc = S->getLocStart(); |
| 1373 | buf = "goto __continue_label_"; |
| 1374 | buf += utostr(ObjCBcLabelNo.back()); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1375 | ReplaceText(startLoc, strlen("continue"), buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1376 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1377 | return 0; |
| 1378 | } |
| 1379 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1380 | /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1381 | /// It rewrites: |
| 1382 | /// for ( type elem in collection) { stmts; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1383 | |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1384 | /// Into: |
| 1385 | /// { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1386 | /// type elem; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1387 | /// struct __objcFastEnumerationState enumState = { 0 }; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1388 | /// id items[16]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1389 | /// id l_collection = (id)collection; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1390 | /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1391 | /// objects:items count:16]; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1392 | /// if (limit) { |
| 1393 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1394 | /// do { |
| 1395 | /// unsigned long counter = 0; |
| 1396 | /// do { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1397 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1398 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1399 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1400 | /// stmts; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1401 | /// __continue_label: ; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1402 | /// } while (counter < limit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1403 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1404 | /// objects:items count:16]); |
| 1405 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1406 | /// __break_label: ; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1407 | /// } |
| 1408 | /// else |
| 1409 | /// elem = nil; |
| 1410 | /// } |
| 1411 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1412 | Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 1413 | SourceLocation OrigEnd) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1414 | assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1415 | assert(isa<ObjCForCollectionStmt>(Stmts.back()) && |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1416 | "ObjCForCollectionStmt Statement stack mismatch"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1417 | assert(!ObjCBcLabelNo.empty() && |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1418 | "ObjCForCollectionStmt - Label No stack empty"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1419 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1420 | SourceLocation startLoc = S->getLocStart(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1421 | const char *startBuf = SM->getCharacterData(startLoc); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1422 | const char *elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1423 | std::string elementTypeAsString; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1424 | std::string buf; |
| 1425 | buf = "\n{\n\t"; |
| 1426 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { |
| 1427 | // type elem; |
Chris Lattner | 7e24e82 | 2009-03-28 06:33:19 +0000 | [diff] [blame] | 1428 | NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl()); |
Ted Kremenek | 1ed8e2a | 2008-10-06 22:16:13 +0000 | [diff] [blame] | 1429 | QualType ElementType = cast<ValueDecl>(D)->getType(); |
Steve Naroff | e89b8e7 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1430 | if (ElementType->isObjCQualifiedIdType() || |
| 1431 | ElementType->isObjCQualifiedInterfaceType()) |
| 1432 | // Simply use 'id' for all qualified types. |
| 1433 | elementTypeAsString = "id"; |
| 1434 | else |
| 1435 | elementTypeAsString = ElementType.getAsString(); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1436 | buf += elementTypeAsString; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1437 | buf += " "; |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1438 | elementName = D->getNameAsCString(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1439 | buf += elementName; |
| 1440 | buf += ";\n\t"; |
| 1441 | } |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1442 | else { |
| 1443 | DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1444 | elementName = DR->getDecl()->getNameAsCString(); |
Steve Naroff | e89b8e7 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1445 | ValueDecl *VD = cast<ValueDecl>(DR->getDecl()); |
| 1446 | if (VD->getType()->isObjCQualifiedIdType() || |
| 1447 | VD->getType()->isObjCQualifiedInterfaceType()) |
| 1448 | // Simply use 'id' for all qualified types. |
| 1449 | elementTypeAsString = "id"; |
| 1450 | else |
| 1451 | elementTypeAsString = VD->getType().getAsString(); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1452 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1453 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1454 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1455 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
| 1456 | // id items[16]; |
| 1457 | buf += "id items[16];\n\t"; |
| 1458 | // id l_collection = (id) |
| 1459 | buf += "id l_collection = (id)"; |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1460 | // Find start location of 'collection' the hard way! |
| 1461 | const char *startCollectionBuf = startBuf; |
| 1462 | startCollectionBuf += 3; // skip 'for' |
| 1463 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1464 | startCollectionBuf++; // skip '(' |
| 1465 | // find 'in' and skip it. |
| 1466 | while (*startCollectionBuf != ' ' || |
| 1467 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1468 | (*(startCollectionBuf+3) != ' ' && |
| 1469 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1470 | startCollectionBuf++; |
| 1471 | startCollectionBuf += 3; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1472 | |
| 1473 | // Replace: "for (type element in" with string constructed thus far. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1474 | ReplaceText(startLoc, startCollectionBuf - startBuf, buf); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1475 | // Replace ')' in for '(' type elem in collection ')' with ';' |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1476 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1477 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
| 1478 | SourceLocation lparenLoc = startLoc.getFileLocWithOffset(rparenBuf-startBuf); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1479 | buf = ";\n\t"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1480 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1481 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1482 | // objects:items count:16]; |
| 1483 | // which is synthesized into: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1484 | // unsigned int limit = |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1485 | // ((unsigned int (*) |
| 1486 | // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1487 | // (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1488 | // sel_registerName( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1489 | // "countByEnumeratingWithState:objects:count:"), |
| 1490 | // (struct __objcFastEnumerationState *)&state, |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1491 | // (id *)items, (unsigned int)16); |
| 1492 | buf += "unsigned long limit =\n\t\t"; |
| 1493 | SynthCountByEnumWithState(buf); |
| 1494 | buf += ";\n\t"; |
| 1495 | /// if (limit) { |
| 1496 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1497 | /// do { |
| 1498 | /// unsigned long counter = 0; |
| 1499 | /// do { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1500 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1501 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1502 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1503 | buf += "if (limit) {\n\t"; |
| 1504 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1505 | buf += "do {\n\t\t"; |
| 1506 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1507 | buf += "do {\n\t\t\t"; |
| 1508 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1509 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1510 | buf += elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1511 | buf += " = ("; |
| 1512 | buf += elementTypeAsString; |
| 1513 | buf += ")enumState.itemsPtr[counter++];"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1514 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1515 | ReplaceText(lparenLoc, 1, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1516 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1517 | /// __continue_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1518 | /// } while (counter < limit); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1519 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1520 | /// objects:items count:16]); |
| 1521 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1522 | /// __break_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1523 | /// } |
| 1524 | /// else |
| 1525 | /// elem = nil; |
| 1526 | /// } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1527 | /// |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1528 | buf = ";\n\t"; |
| 1529 | buf += "__continue_label_"; |
| 1530 | buf += utostr(ObjCBcLabelNo.back()); |
| 1531 | buf += ": ;"; |
| 1532 | buf += "\n\t\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1533 | buf += "} while (counter < limit);\n\t"; |
| 1534 | buf += "} while (limit = "; |
| 1535 | SynthCountByEnumWithState(buf); |
| 1536 | buf += ");\n\t"; |
| 1537 | buf += elementName; |
Fariborz Jahanian | 65b0aa5 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1538 | buf += " = (("; |
| 1539 | buf += elementTypeAsString; |
| 1540 | buf += ")0);\n\t"; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1541 | buf += "__break_label_"; |
| 1542 | buf += utostr(ObjCBcLabelNo.back()); |
| 1543 | buf += ": ;\n\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1544 | buf += "}\n\t"; |
| 1545 | buf += "else\n\t\t"; |
| 1546 | buf += elementName; |
Fariborz Jahanian | 65b0aa5 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1547 | buf += " = (("; |
| 1548 | buf += elementTypeAsString; |
| 1549 | buf += ")0);\n\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1550 | buf += "}\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1551 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1552 | // Insert all these *after* the statement body. |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1553 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1554 | if (isa<CompoundStmt>(S->getBody())) { |
| 1555 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1556 | InsertText(endBodyLoc, buf); |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1557 | } else { |
| 1558 | /* Need to treat single statements specially. For example: |
| 1559 | * |
| 1560 | * for (A *a in b) if (stuff()) break; |
| 1561 | * for (A *a in b) xxxyy; |
| 1562 | * |
| 1563 | * The following code simply scans ahead to the semi to find the actual end. |
| 1564 | */ |
| 1565 | const char *stmtBuf = SM->getCharacterData(OrigEnd); |
| 1566 | const char *semiBuf = strchr(stmtBuf, ';'); |
| 1567 | assert(semiBuf && "Can't find ';'"); |
| 1568 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(semiBuf-stmtBuf+1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1569 | InsertText(endBodyLoc, buf); |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1570 | } |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1571 | Stmts.pop_back(); |
| 1572 | ObjCBcLabelNo.pop_back(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1573 | return 0; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1574 | } |
| 1575 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1576 | /// RewriteObjCSynchronizedStmt - |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1577 | /// This routine rewrites @synchronized(expr) stmt; |
| 1578 | /// into: |
| 1579 | /// objc_sync_enter(expr); |
| 1580 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1581 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1582 | Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1583 | // Get the start location and compute the semi location. |
| 1584 | SourceLocation startLoc = S->getLocStart(); |
| 1585 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1586 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1587 | assert((*startBuf == '@') && "bogus @synchronized location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1588 | |
| 1589 | std::string buf; |
Steve Naroff | 3498cc9 | 2008-08-21 13:03:03 +0000 | [diff] [blame] | 1590 | buf = "objc_sync_enter((id)"; |
| 1591 | const char *lparenBuf = startBuf; |
| 1592 | while (*lparenBuf != '(') lparenBuf++; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1593 | ReplaceText(startLoc, lparenBuf-startBuf+1, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1594 | // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since |
| 1595 | // the sync expression is typically a message expression that's already |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1596 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1597 | SourceLocation endLoc = S->getSynchBody()->getLocStart(); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1598 | const char *endBuf = SM->getCharacterData(endLoc); |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1599 | while (*endBuf != ')') endBuf--; |
| 1600 | SourceLocation rparenLoc = startLoc.getFileLocWithOffset(endBuf-startBuf); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1601 | buf = ");\n"; |
| 1602 | // declare a new scope with two variables, _stack and _rethrow. |
| 1603 | buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n"; |
| 1604 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1605 | buf += "char *pointers[4];} _stack;\n"; |
| 1606 | buf += "id volatile _rethrow = 0;\n"; |
| 1607 | buf += "objc_exception_try_enter(&_stack);\n"; |
| 1608 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1609 | ReplaceText(rparenLoc, 1, buf); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1610 | startLoc = S->getSynchBody()->getLocEnd(); |
| 1611 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1612 | |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1613 | assert((*startBuf == '}') && "bogus @synchronized block"); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1614 | SourceLocation lastCurlyLoc = startLoc; |
| 1615 | buf = "}\nelse {\n"; |
| 1616 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 1617 | buf += "}\n"; |
| 1618 | buf += "{ /* implicit finally clause */\n"; |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1619 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1620 | |
| 1621 | std::string syncBuf; |
| 1622 | syncBuf += " objc_sync_exit("; |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1623 | Expr *syncExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 1624 | CastExpr::CK_Unknown, |
| 1625 | S->getSynchExpr()); |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1626 | std::string syncExprBufS; |
| 1627 | llvm::raw_string_ostream syncExprBuf(syncExprBufS); |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1628 | syncExpr->printPretty(syncExprBuf, *Context, 0, |
| 1629 | PrintingPolicy(LangOpts)); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1630 | syncBuf += syncExprBuf.str(); |
| 1631 | syncBuf += ");"; |
| 1632 | |
| 1633 | buf += syncBuf; |
| 1634 | buf += "\n if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1635 | buf += "}\n"; |
| 1636 | buf += "}"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1637 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1638 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1639 | |
| 1640 | bool hasReturns = false; |
| 1641 | HasReturnStmts(S->getSynchBody(), hasReturns); |
| 1642 | if (hasReturns) |
| 1643 | RewriteSyncReturnStmts(S->getSynchBody(), syncBuf); |
| 1644 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1645 | return 0; |
| 1646 | } |
| 1647 | |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1648 | void RewriteObjC::WarnAboutReturnGotoStmts(Stmt *S) |
| 1649 | { |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1650 | // Perform a bottom up traversal of all children. |
| 1651 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1652 | CI != E; ++CI) |
| 1653 | if (*CI) |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1654 | WarnAboutReturnGotoStmts(*CI); |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1655 | |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1656 | if (isa<ReturnStmt>(S) || isa<GotoStmt>(S)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1657 | Diags.Report(Context->getFullLoc(S->getLocStart()), |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1658 | TryFinallyContainsReturnDiag); |
| 1659 | } |
| 1660 | return; |
| 1661 | } |
| 1662 | |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1663 | void RewriteObjC::HasReturnStmts(Stmt *S, bool &hasReturns) |
| 1664 | { |
| 1665 | // Perform a bottom up traversal of all children. |
| 1666 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1667 | CI != E; ++CI) |
| 1668 | if (*CI) |
| 1669 | HasReturnStmts(*CI, hasReturns); |
| 1670 | |
| 1671 | if (isa<ReturnStmt>(S)) |
| 1672 | hasReturns = true; |
| 1673 | return; |
| 1674 | } |
| 1675 | |
| 1676 | void RewriteObjC::RewriteTryReturnStmts(Stmt *S) { |
| 1677 | // Perform a bottom up traversal of all children. |
| 1678 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1679 | CI != E; ++CI) |
| 1680 | if (*CI) { |
| 1681 | RewriteTryReturnStmts(*CI); |
| 1682 | } |
| 1683 | if (isa<ReturnStmt>(S)) { |
| 1684 | SourceLocation startLoc = S->getLocStart(); |
| 1685 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1686 | |
| 1687 | const char *semiBuf = strchr(startBuf, ';'); |
| 1688 | assert((*semiBuf == ';') && "RewriteTryReturnStmts: can't find ';'"); |
| 1689 | SourceLocation onePastSemiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
| 1690 | |
| 1691 | std::string buf; |
| 1692 | buf = "{ objc_exception_try_exit(&_stack); return"; |
| 1693 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1694 | ReplaceText(startLoc, 6, buf); |
| 1695 | InsertText(onePastSemiLoc, "}"); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1696 | } |
| 1697 | return; |
| 1698 | } |
| 1699 | |
| 1700 | void RewriteObjC::RewriteSyncReturnStmts(Stmt *S, std::string syncExitBuf) { |
| 1701 | // Perform a bottom up traversal of all children. |
| 1702 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1703 | CI != E; ++CI) |
| 1704 | if (*CI) { |
| 1705 | RewriteSyncReturnStmts(*CI, syncExitBuf); |
| 1706 | } |
| 1707 | if (isa<ReturnStmt>(S)) { |
| 1708 | SourceLocation startLoc = S->getLocStart(); |
| 1709 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1710 | |
| 1711 | const char *semiBuf = strchr(startBuf, ';'); |
| 1712 | assert((*semiBuf == ';') && "RewriteSyncReturnStmts: can't find ';'"); |
| 1713 | SourceLocation onePastSemiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
| 1714 | |
| 1715 | std::string buf; |
| 1716 | buf = "{ objc_exception_try_exit(&_stack);"; |
| 1717 | buf += syncExitBuf; |
| 1718 | buf += " return"; |
| 1719 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1720 | ReplaceText(startLoc, 6, buf); |
| 1721 | InsertText(onePastSemiLoc, "}"); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1722 | } |
| 1723 | return; |
| 1724 | } |
| 1725 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1726 | Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1727 | // Get the start location and compute the semi location. |
| 1728 | SourceLocation startLoc = S->getLocStart(); |
| 1729 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1730 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1731 | assert((*startBuf == '@') && "bogus @try location"); |
| 1732 | |
| 1733 | std::string buf; |
| 1734 | // declare a new scope with two variables, _stack and _rethrow. |
| 1735 | buf = "/* @try scope begin */ { struct _objc_exception_data {\n"; |
| 1736 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1737 | buf += "char *pointers[4];} _stack;\n"; |
| 1738 | buf += "id volatile _rethrow = 0;\n"; |
| 1739 | buf += "objc_exception_try_enter(&_stack);\n"; |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1740 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1741 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1742 | ReplaceText(startLoc, 4, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1743 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1744 | startLoc = S->getTryBody()->getLocEnd(); |
| 1745 | startBuf = SM->getCharacterData(startLoc); |
| 1746 | |
| 1747 | assert((*startBuf == '}') && "bogus @try block"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1748 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1749 | SourceLocation lastCurlyLoc = startLoc; |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1750 | ObjCAtCatchStmt *catchList = S->getCatchStmts(); |
| 1751 | if (catchList) { |
| 1752 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1753 | buf = " /* @catch begin */ else {\n"; |
| 1754 | buf += " id _caught = objc_exception_extract(&_stack);\n"; |
| 1755 | buf += " objc_exception_try_enter (&_stack);\n"; |
| 1756 | buf += " if (_setjmp(_stack.buf))\n"; |
| 1757 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1758 | buf += " else { /* @catch continue */"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1759 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1760 | InsertText(startLoc, buf); |
Steve Naroff | 8bd3dc6 | 2008-09-09 19:59:12 +0000 | [diff] [blame] | 1761 | } else { /* no catch list */ |
| 1762 | buf = "}\nelse {\n"; |
| 1763 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1764 | buf += "}"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1765 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1766 | } |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1767 | bool sawIdTypedCatch = false; |
| 1768 | Stmt *lastCatchBody = 0; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1769 | while (catchList) { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1770 | ParmVarDecl *catchDecl = catchList->getCatchParamDecl(); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1771 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1772 | if (catchList == S->getCatchStmts()) |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1773 | buf = "if ("; // we are generating code for the first catch clause |
| 1774 | else |
| 1775 | buf = "else if ("; |
| 1776 | startLoc = catchList->getLocStart(); |
| 1777 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1778 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1779 | assert((*startBuf == '@') && "bogus @catch location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1780 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1781 | const char *lParenLoc = strchr(startBuf, '('); |
| 1782 | |
Steve Naroff | be4b333 | 2008-02-01 22:08:12 +0000 | [diff] [blame] | 1783 | if (catchList->hasEllipsis()) { |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1784 | // Now rewrite the body... |
| 1785 | lastCatchBody = catchList->getCatchBody(); |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1786 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1787 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1788 | assert(*SM->getCharacterData(catchList->getRParenLoc()) == ')' && |
| 1789 | "bogus @catch paren location"); |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1790 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1791 | |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1792 | buf += "1) { id _tmp = _caught;"; |
Daniel Dunbar | d7407dc | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 1793 | Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, buf); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1794 | } else if (catchDecl) { |
| 1795 | QualType t = catchDecl->getType(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1796 | if (t == Context->getObjCIdType()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1797 | buf += "1) { "; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1798 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1799 | sawIdTypedCatch = true; |
Fariborz Jahanian | 66867c5 | 2010-01-12 01:22:23 +0000 | [diff] [blame] | 1800 | } else if (t->isObjCObjectPointerType()) { |
| 1801 | QualType InterfaceTy = t->getPointeeType(); |
| 1802 | const ObjCInterfaceType *cls = // Should be a pointer to a class. |
| 1803 | InterfaceTy->getAs<ObjCInterfaceType>(); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1804 | if (cls) { |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1805 | buf += "objc_exception_match((struct objc_class *)objc_getClass(\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1806 | buf += cls->getDecl()->getNameAsString(); |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1807 | buf += "\"), (struct objc_object *)_caught)) { "; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1808 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1809 | } |
| 1810 | } |
| 1811 | // Now rewrite the body... |
| 1812 | lastCatchBody = catchList->getCatchBody(); |
| 1813 | SourceLocation rParenLoc = catchList->getRParenLoc(); |
| 1814 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1815 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 1816 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 1817 | assert((*rParenBuf == ')') && "bogus @catch paren location"); |
| 1818 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1819 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1820 | // Here we replace ") {" with "= _caught;" (which initializes and |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1821 | // declares the @catch parameter). |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1822 | ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, " = _caught;"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1823 | } else { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1824 | assert(false && "@catch rewrite bug"); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1825 | } |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1826 | // make sure all the catch bodies get rewritten! |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1827 | catchList = catchList->getNextCatchStmt(); |
| 1828 | } |
| 1829 | // Complete the catch list... |
| 1830 | if (lastCatchBody) { |
| 1831 | SourceLocation bodyLoc = lastCatchBody->getLocEnd(); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1832 | assert(*SM->getCharacterData(bodyLoc) == '}' && |
| 1833 | "bogus @catch body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1834 | |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1835 | // Insert the last (implicit) else clause *before* the right curly brace. |
| 1836 | bodyLoc = bodyLoc.getFileLocWithOffset(-1); |
| 1837 | buf = "} /* last catch end */\n"; |
| 1838 | buf += "else {\n"; |
| 1839 | buf += " _rethrow = _caught;\n"; |
| 1840 | buf += " objc_exception_try_exit(&_stack);\n"; |
| 1841 | buf += "} } /* @catch end */\n"; |
| 1842 | if (!S->getFinallyStmt()) |
| 1843 | buf += "}\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1844 | InsertText(bodyLoc, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1845 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1846 | // Set lastCurlyLoc |
| 1847 | lastCurlyLoc = lastCatchBody->getLocEnd(); |
| 1848 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1849 | if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1850 | startLoc = finalStmt->getLocStart(); |
| 1851 | startBuf = SM->getCharacterData(startLoc); |
| 1852 | assert((*startBuf == '@') && "bogus @finally start"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1853 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1854 | ReplaceText(startLoc, 8, "/* @finally */"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1855 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1856 | Stmt *body = finalStmt->getFinallyBody(); |
| 1857 | SourceLocation startLoc = body->getLocStart(); |
| 1858 | SourceLocation endLoc = body->getLocEnd(); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1859 | assert(*SM->getCharacterData(startLoc) == '{' && |
| 1860 | "bogus @finally body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1861 | assert(*SM->getCharacterData(endLoc) == '}' && |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1862 | "bogus @finally body location"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1863 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1864 | startLoc = startLoc.getFileLocWithOffset(1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1865 | InsertText(startLoc, " if (!_rethrow) objc_exception_try_exit(&_stack);\n"); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1866 | endLoc = endLoc.getFileLocWithOffset(-1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1867 | InsertText(endLoc, " if (_rethrow) objc_exception_throw(_rethrow);\n"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1868 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1869 | // Set lastCurlyLoc |
| 1870 | lastCurlyLoc = body->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1871 | |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1872 | // Now check for any return/continue/go statements within the @try. |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1873 | WarnAboutReturnGotoStmts(S->getTryBody()); |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1874 | } else { /* no finally clause - make sure we synthesize an implicit one */ |
| 1875 | buf = "{ /* implicit finally clause */\n"; |
| 1876 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
| 1877 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 1878 | buf += "}"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1879 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | b85e77a | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1880 | |
| 1881 | // Now check for any return/continue/go statements within the @try. |
| 1882 | // The implicit finally clause won't called if the @try contains any |
| 1883 | // jump statements. |
| 1884 | bool hasReturns = false; |
| 1885 | HasReturnStmts(S->getTryBody(), hasReturns); |
| 1886 | if (hasReturns) |
| 1887 | RewriteTryReturnStmts(S->getTryBody()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1888 | } |
| 1889 | // Now emit the final closing curly brace... |
| 1890 | lastCurlyLoc = lastCurlyLoc.getFileLocWithOffset(1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1891 | InsertText(lastCurlyLoc, " } /* @try scope end */\n"); |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1892 | return 0; |
| 1893 | } |
| 1894 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1895 | Stmt *RewriteObjC::RewriteObjCCatchStmt(ObjCAtCatchStmt *S) { |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1896 | return 0; |
| 1897 | } |
| 1898 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1899 | Stmt *RewriteObjC::RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S) { |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1900 | return 0; |
| 1901 | } |
| 1902 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1903 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
| 1904 | // the throw expression is typically a message expression that's already |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1905 | // been rewritten! (which implies the SourceLocation's are invalid). |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1906 | Stmt *RewriteObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1907 | // Get the start location and compute the semi location. |
| 1908 | SourceLocation startLoc = S->getLocStart(); |
| 1909 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1910 | |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1911 | assert((*startBuf == '@') && "bogus @throw location"); |
| 1912 | |
| 1913 | std::string buf; |
| 1914 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
Steve Naroff | 20ebf8f | 2008-01-19 00:42:38 +0000 | [diff] [blame] | 1915 | if (S->getThrowExpr()) |
| 1916 | buf = "objc_exception_throw("; |
| 1917 | else // add an implicit argument |
| 1918 | buf = "objc_exception_throw(_caught"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1919 | |
Steve Naroff | 4ba0acb | 2008-07-25 15:41:30 +0000 | [diff] [blame] | 1920 | // handle "@ throw" correctly. |
| 1921 | const char *wBuf = strchr(startBuf, 'w'); |
| 1922 | assert((*wBuf == 'w') && "@throw: can't find 'w'"); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1923 | ReplaceText(startLoc, wBuf-startBuf+1, buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1924 | |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1925 | const char *semiBuf = strchr(startBuf, ';'); |
| 1926 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
| 1927 | SourceLocation semiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1928 | ReplaceText(semiLoc, 1, ");"); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1929 | return 0; |
| 1930 | } |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1931 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1932 | Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 1933 | // Create a new string expression. |
| 1934 | QualType StrType = Context->getPointerType(Context->CharTy); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1935 | std::string StrEncoding; |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1936 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 1937 | Expr *Replacement = StringLiteral::Create(*Context,StrEncoding.c_str(), |
| 1938 | StrEncoding.length(), false,StrType, |
| 1939 | SourceLocation()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1940 | ReplaceStmt(Exp, Replacement); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1941 | |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 1942 | // Replace this subexpr in the parent. |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1943 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 1944 | return Replacement; |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 1945 | } |
| 1946 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1947 | Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
Steve Naroff | 1a93764 | 2008-12-22 22:16:07 +0000 | [diff] [blame] | 1948 | if (!SelGetUidFunctionDecl) |
| 1949 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1950 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 1951 | // Create a call to sel_registerName("selName"). |
| 1952 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 1953 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1954 | SelExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 6e94ef5 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 1955 | Exp->getSelector().getAsString().c_str(), |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1956 | Exp->getSelector().getAsString().size(), |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 1957 | false, argType, SourceLocation())); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1958 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 1959 | &SelExprs[0], SelExprs.size()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1960 | ReplaceStmt(Exp, SelExp); |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1961 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1962 | return SelExp; |
| 1963 | } |
| 1964 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1965 | CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl( |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 1966 | FunctionDecl *FD, Expr **args, unsigned nargs, SourceLocation StartLoc, |
| 1967 | SourceLocation EndLoc) { |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1968 | // 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] | 1969 | QualType msgSendType = FD->getType(); |
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 | // Create a reference to the objc_msgSend() declaration. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1972 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, msgSendType, SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1973 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1974 | // 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] | 1975 | QualType pToFunc = Context->getPointerType(msgSendType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1976 | ImplicitCastExpr *ICE = new (Context) ImplicitCastExpr(pToFunc, |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 1977 | CastExpr::CK_Unknown, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1978 | DRE, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1979 | /*isLvalue=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1980 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1981 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1982 | |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 1983 | CallExpr *Exp = |
| 1984 | new (Context) CallExpr(*Context, ICE, args, nargs, FT->getResultType(), |
| 1985 | EndLoc); |
| 1986 | return Exp; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1989 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 1990 | const char *&startRef, const char *&endRef) { |
| 1991 | while (startBuf < endBuf) { |
| 1992 | if (*startBuf == '<') |
| 1993 | startRef = startBuf; // mark the start. |
| 1994 | if (*startBuf == '>') { |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1995 | if (startRef && *startRef == '<') { |
| 1996 | endRef = startBuf; // mark the end. |
| 1997 | return true; |
| 1998 | } |
| 1999 | return false; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2000 | } |
| 2001 | startBuf++; |
| 2002 | } |
| 2003 | return false; |
| 2004 | } |
| 2005 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2006 | static void scanToNextArgument(const char *&argRef) { |
| 2007 | int angle = 0; |
| 2008 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 2009 | if (*argRef == '<') |
| 2010 | angle++; |
| 2011 | else if (*argRef == '>') |
| 2012 | angle--; |
| 2013 | argRef++; |
| 2014 | } |
| 2015 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 2016 | } |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2017 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2018 | bool RewriteObjC::needToScanForQualifiers(QualType T) { |
Fariborz Jahanian | 32132a0 | 2010-02-03 21:29:28 +0000 | [diff] [blame] | 2019 | if (T->isObjCQualifiedIdType()) |
| 2020 | return true; |
Fariborz Jahanian | 84aa946 | 2010-02-02 18:35:07 +0000 | [diff] [blame] | 2021 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 2022 | if (PT->getPointeeType()->isObjCQualifiedIdType()) |
| 2023 | return true; |
| 2024 | } |
| 2025 | if (T->isObjCObjectPointerType()) { |
| 2026 | T = T->getPointeeType(); |
| 2027 | return T->isObjCQualifiedInterfaceType(); |
| 2028 | } |
| 2029 | return false; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2032 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) { |
| 2033 | QualType Type = E->getType(); |
| 2034 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2035 | SourceLocation Loc, EndLoc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2036 | |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2037 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) { |
| 2038 | Loc = ECE->getLParenLoc(); |
| 2039 | EndLoc = ECE->getRParenLoc(); |
| 2040 | } else { |
| 2041 | Loc = E->getLocStart(); |
| 2042 | EndLoc = E->getLocEnd(); |
| 2043 | } |
| 2044 | // This will defend against trying to rewrite synthesized expressions. |
| 2045 | if (Loc.isInvalid() || EndLoc.isInvalid()) |
| 2046 | return; |
| 2047 | |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2048 | const char *startBuf = SM->getCharacterData(Loc); |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2049 | const char *endBuf = SM->getCharacterData(EndLoc); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2050 | const char *startRef = 0, *endRef = 0; |
| 2051 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2052 | // Get the locations of the startRef, endRef. |
| 2053 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-startBuf); |
| 2054 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-startBuf+1); |
| 2055 | // Comment out the protocol references. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2056 | InsertText(LessLoc, "/*"); |
| 2057 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2058 | } |
| 2059 | } |
| 2060 | } |
| 2061 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2062 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2063 | SourceLocation Loc; |
| 2064 | QualType Type; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2065 | const FunctionProtoType *proto = 0; |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2066 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 2067 | Loc = VD->getLocation(); |
| 2068 | Type = VD->getType(); |
| 2069 | } |
| 2070 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 2071 | Loc = FD->getLocation(); |
| 2072 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 2073 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2074 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2075 | assert(funcType && "missing function type"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2076 | proto = dyn_cast<FunctionProtoType>(funcType); |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2077 | if (!proto) |
| 2078 | return; |
| 2079 | Type = proto->getResultType(); |
| 2080 | } |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 2081 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(Dcl)) { |
| 2082 | Loc = FD->getLocation(); |
| 2083 | Type = FD->getType(); |
| 2084 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2085 | else |
| 2086 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2087 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2088 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2089 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2090 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2091 | const char *endBuf = SM->getCharacterData(Loc); |
| 2092 | const char *startBuf = endBuf; |
Steve Naroff | 6cafbf2 | 2008-05-31 05:02:17 +0000 | [diff] [blame] | 2093 | while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2094 | startBuf--; // scan backward (from the decl location) for return type. |
| 2095 | const char *startRef = 0, *endRef = 0; |
| 2096 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2097 | // Get the locations of the startRef, endRef. |
| 2098 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-endBuf); |
| 2099 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-endBuf+1); |
| 2100 | // Comment out the protocol references. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2101 | InsertText(LessLoc, "/*"); |
| 2102 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2103 | } |
| 2104 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2105 | if (!proto) |
| 2106 | return; // most likely, was a variable |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2107 | // Now check arguments. |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2108 | const char *startBuf = SM->getCharacterData(Loc); |
| 2109 | const char *startFuncBuf = startBuf; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2110 | for (unsigned i = 0; i < proto->getNumArgs(); i++) { |
| 2111 | if (needToScanForQualifiers(proto->getArgType(i))) { |
| 2112 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2113 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2114 | const char *endBuf = startBuf; |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2115 | // scan forward (from the decl location) for argument types. |
| 2116 | scanToNextArgument(endBuf); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2117 | const char *startRef = 0, *endRef = 0; |
| 2118 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2119 | // Get the locations of the startRef, endRef. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2120 | SourceLocation LessLoc = |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2121 | Loc.getFileLocWithOffset(startRef-startFuncBuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2122 | SourceLocation GreaterLoc = |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2123 | Loc.getFileLocWithOffset(endRef-startFuncBuf+1); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2124 | // Comment out the protocol references. |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2125 | InsertText(LessLoc, "/*"); |
| 2126 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2127 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2128 | startBuf = ++endBuf; |
| 2129 | } |
| 2130 | else { |
Steve Naroff | aba49d1 | 2008-08-06 15:58:23 +0000 | [diff] [blame] | 2131 | // If the function name is derived from a macro expansion, then the |
| 2132 | // argument buffer will not follow the name. Need to speak with Chris. |
| 2133 | while (*startBuf && *startBuf != ')' && *startBuf != ',') |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2134 | startBuf++; // scan forward (from the decl location) for argument types. |
| 2135 | startBuf++; |
| 2136 | } |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2137 | } |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2138 | } |
| 2139 | |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2140 | void RewriteObjC::RewriteTypeOfDecl(VarDecl *ND) { |
| 2141 | QualType QT = ND->getType(); |
| 2142 | const Type* TypePtr = QT->getAs<Type>(); |
| 2143 | if (!isa<TypeOfExprType>(TypePtr)) |
| 2144 | return; |
| 2145 | while (isa<TypeOfExprType>(TypePtr)) { |
| 2146 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 2147 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 2148 | TypePtr = QT->getAs<Type>(); |
| 2149 | } |
| 2150 | // FIXME. This will not work for multiple declarators; as in: |
| 2151 | // __typeof__(a) b,c,d; |
| 2152 | std::string TypeAsString(QT.getAsString()); |
| 2153 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
| 2154 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 2155 | if (ND->getInit()) { |
| 2156 | std::string Name(ND->getNameAsString()); |
| 2157 | TypeAsString += " " + Name + " = "; |
| 2158 | Expr *E = ND->getInit(); |
| 2159 | SourceLocation startLoc; |
| 2160 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 2161 | startLoc = ECE->getLParenLoc(); |
| 2162 | else |
| 2163 | startLoc = E->getLocStart(); |
| 2164 | startLoc = SM->getInstantiationLoc(startLoc); |
| 2165 | const char *endBuf = SM->getCharacterData(startLoc); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2166 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2167 | } |
| 2168 | else { |
| 2169 | SourceLocation X = ND->getLocEnd(); |
| 2170 | X = SM->getInstantiationLoc(X); |
| 2171 | const char *endBuf = SM->getCharacterData(X); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2172 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2173 | } |
| 2174 | } |
| 2175 | |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2176 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2177 | void RewriteObjC::SynthSelGetUidFunctionDecl() { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2178 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
| 2179 | llvm::SmallVector<QualType, 16> ArgTys; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2180 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2181 | QualType getFuncType = Context->getFunctionType(Context->getObjCSelType(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2182 | &ArgTys[0], ArgTys.size(), |
| 2183 | false /*isVariadic*/, 0, |
| 2184 | false, false, 0, 0, false, |
| 2185 | CC_Default); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2186 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2187 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2188 | SelGetUidIdent, getFuncType, 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2189 | FunctionDecl::Extern, false); |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2190 | } |
| 2191 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2192 | void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2193 | // declared in <objc/objc.h> |
Douglas Gregor | 51efe56 | 2009-01-09 01:47:02 +0000 | [diff] [blame] | 2194 | if (FD->getIdentifier() && |
| 2195 | strcmp(FD->getNameAsCString(), "sel_registerName") == 0) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2196 | SelGetUidFunctionDecl = FD; |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2197 | return; |
| 2198 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2199 | RewriteObjCQualifiedInterfaceTypes(FD); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2200 | } |
| 2201 | |
Fariborz Jahanian | 52b2e1e | 2010-02-12 17:52:31 +0000 | [diff] [blame] | 2202 | static void RewriteBlockPointerType(std::string& Str, QualType Type) { |
| 2203 | std::string TypeString(Type.getAsString()); |
| 2204 | const char *argPtr = TypeString.c_str(); |
| 2205 | if (!strchr(argPtr, '^')) { |
| 2206 | Str += TypeString; |
| 2207 | return; |
| 2208 | } |
| 2209 | while (*argPtr) { |
| 2210 | Str += (*argPtr == '^' ? '*' : *argPtr); |
| 2211 | argPtr++; |
| 2212 | } |
| 2213 | } |
| 2214 | |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2215 | // FIXME. Consolidate this routine with RewriteBlockPointerType. |
| 2216 | static void RewriteBlockPointerTypeVariable(std::string& Str, ValueDecl *VD) { |
| 2217 | QualType Type = VD->getType(); |
| 2218 | std::string TypeString(Type.getAsString()); |
| 2219 | const char *argPtr = TypeString.c_str(); |
| 2220 | int paren = 0; |
| 2221 | while (*argPtr) { |
| 2222 | switch (*argPtr) { |
| 2223 | case '(': |
| 2224 | Str += *argPtr; |
| 2225 | paren++; |
| 2226 | break; |
| 2227 | case ')': |
| 2228 | Str += *argPtr; |
| 2229 | paren--; |
| 2230 | break; |
| 2231 | case '^': |
| 2232 | Str += '*'; |
| 2233 | if (paren == 1) |
| 2234 | Str += VD->getNameAsString(); |
| 2235 | break; |
| 2236 | default: |
| 2237 | Str += *argPtr; |
| 2238 | break; |
| 2239 | } |
| 2240 | argPtr++; |
| 2241 | } |
| 2242 | } |
| 2243 | |
| 2244 | |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2245 | void RewriteObjC::RewriteBlockLiteralFunctionDecl(FunctionDecl *FD) { |
| 2246 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
| 2247 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
| 2248 | const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(funcType); |
| 2249 | if (!proto) |
| 2250 | return; |
| 2251 | QualType Type = proto->getResultType(); |
| 2252 | std::string FdStr = Type.getAsString(); |
| 2253 | FdStr += " "; |
| 2254 | FdStr += FD->getNameAsCString(); |
| 2255 | FdStr += "("; |
| 2256 | unsigned numArgs = proto->getNumArgs(); |
| 2257 | for (unsigned i = 0; i < numArgs; i++) { |
| 2258 | QualType ArgType = proto->getArgType(i); |
Fariborz Jahanian | 52b2e1e | 2010-02-12 17:52:31 +0000 | [diff] [blame] | 2259 | RewriteBlockPointerType(FdStr, ArgType); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2260 | if (i+1 < numArgs) |
| 2261 | FdStr += ", "; |
| 2262 | } |
| 2263 | FdStr += ");\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2264 | InsertText(FunLocStart, FdStr); |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2265 | CurFunctionDeclToDeclareForBlock = 0; |
| 2266 | } |
| 2267 | |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2268 | // SynthSuperContructorFunctionDecl - id objc_super(id obj, id super); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2269 | void RewriteObjC::SynthSuperContructorFunctionDecl() { |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2270 | if (SuperContructorFunctionDecl) |
| 2271 | return; |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2272 | IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super"); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2273 | llvm::SmallVector<QualType, 16> ArgTys; |
| 2274 | QualType argT = Context->getObjCIdType(); |
| 2275 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2276 | ArgTys.push_back(argT); |
| 2277 | ArgTys.push_back(argT); |
| 2278 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
| 2279 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2280 | false, 0, |
| 2281 | false, false, 0, 0, false, |
| 2282 | CC_Default); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2283 | SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2284 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2285 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2286 | FunctionDecl::Extern, false); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2287 | } |
| 2288 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2289 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2290 | void RewriteObjC::SynthMsgSendFunctionDecl() { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2291 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
| 2292 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2293 | QualType argT = Context->getObjCIdType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2294 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2295 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2296 | argT = Context->getObjCSelType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2297 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2298 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2299 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2300 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2301 | true /*isVariadic*/, 0, |
| 2302 | false, false, 0, 0, false, |
| 2303 | CC_Default); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2304 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2305 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2306 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2307 | FunctionDecl::Extern, false); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2308 | } |
| 2309 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2310 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2311 | void RewriteObjC::SynthMsgSendSuperFunctionDecl() { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2312 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
| 2313 | llvm::SmallVector<QualType, 16> ArgTys; |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2314 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2315 | SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2316 | &Context->Idents.get("objc_super")); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2317 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2318 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2319 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2320 | argT = Context->getObjCSelType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2321 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2322 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2323 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2324 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2325 | true /*isVariadic*/, 0, |
| 2326 | false, false, 0, 0, false, |
| 2327 | CC_Default); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2328 | MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2329 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2330 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2331 | FunctionDecl::Extern, false); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2332 | } |
| 2333 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2334 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2335 | void RewriteObjC::SynthMsgSendStretFunctionDecl() { |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2336 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
| 2337 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2338 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2339 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2340 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2341 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2342 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2343 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2344 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2345 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2346 | true /*isVariadic*/, 0, |
| 2347 | false, false, 0, 0, false, |
| 2348 | CC_Default); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2349 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2350 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2351 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2352 | FunctionDecl::Extern, false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2353 | } |
| 2354 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2355 | // SynthMsgSendSuperStretFunctionDecl - |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2356 | // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2357 | void RewriteObjC::SynthMsgSendSuperStretFunctionDecl() { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2358 | IdentifierInfo *msgSendIdent = |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2359 | &Context->Idents.get("objc_msgSendSuper_stret"); |
| 2360 | llvm::SmallVector<QualType, 16> ArgTys; |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2361 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2362 | SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2363 | &Context->Idents.get("objc_super")); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2364 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2365 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2366 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2367 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2368 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2369 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2370 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2371 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2372 | true /*isVariadic*/, 0, |
| 2373 | false, false, 0, 0, false, |
| 2374 | CC_Default); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2375 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2376 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2377 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2378 | FunctionDecl::Extern, false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2379 | } |
| 2380 | |
Steve Naroff | 1284db8 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2381 | // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2382 | void RewriteObjC::SynthMsgSendFpretFunctionDecl() { |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2383 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
| 2384 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2385 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2386 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2387 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2388 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2389 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2390 | ArgTys.push_back(argT); |
Steve Naroff | 1284db8 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2391 | QualType msgSendType = Context->getFunctionType(Context->DoubleTy, |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2392 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2393 | true /*isVariadic*/, 0, |
| 2394 | false, false, 0, 0, false, |
| 2395 | CC_Default); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2396 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2397 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2398 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2399 | FunctionDecl::Extern, false); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2400 | } |
| 2401 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2402 | // SynthGetClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2403 | void RewriteObjC::SynthGetClassFunctionDecl() { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2404 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
| 2405 | llvm::SmallVector<QualType, 16> ArgTys; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2406 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2407 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2408 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2409 | false /*isVariadic*/, 0, |
| 2410 | false, false, 0, 0, false, |
| 2411 | CC_Default); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2412 | GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2413 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2414 | getClassIdent, getClassType, 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2415 | FunctionDecl::Extern, false); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2416 | } |
| 2417 | |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2418 | // SynthGetMetaClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2419 | void RewriteObjC::SynthGetMetaClassFunctionDecl() { |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2420 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
| 2421 | llvm::SmallVector<QualType, 16> ArgTys; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2422 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2423 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2424 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2425 | false /*isVariadic*/, 0, |
| 2426 | false, false, 0, 0, false, |
| 2427 | CC_Default); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2428 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2429 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2430 | getClassIdent, getClassType, 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2431 | FunctionDecl::Extern, false); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2432 | } |
| 2433 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2434 | Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2435 | QualType strType = getConstantStringStructType(); |
| 2436 | |
| 2437 | std::string S = "__NSConstantStringImpl_"; |
Steve Naroff | 7691d9b | 2008-05-31 03:35:42 +0000 | [diff] [blame] | 2438 | |
| 2439 | std::string tmpName = InFileName; |
| 2440 | unsigned i; |
| 2441 | for (i=0; i < tmpName.length(); i++) { |
| 2442 | char c = tmpName.at(i); |
| 2443 | // replace any non alphanumeric characters with '_'. |
| 2444 | if (!isalpha(c) && (c < '0' || c > '9')) |
| 2445 | tmpName[i] = '_'; |
| 2446 | } |
| 2447 | S += tmpName; |
| 2448 | S += "_"; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2449 | S += utostr(NumObjCStringLiterals++); |
| 2450 | |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2451 | Preamble += "static __NSConstantStringImpl " + S; |
| 2452 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 2453 | Preamble += "0x000007c8,"; // utf8_str |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2454 | // The pretty printer for StringLiteral handles escape characters properly. |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2455 | std::string prettyBufS; |
| 2456 | llvm::raw_string_ostream prettyBuf(prettyBufS); |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2457 | Exp->getString()->printPretty(prettyBuf, *Context, 0, |
| 2458 | PrintingPolicy(LangOpts)); |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2459 | Preamble += prettyBuf.str(); |
| 2460 | Preamble += ","; |
Steve Naroff | fd5b76f | 2009-12-06 01:48:44 +0000 | [diff] [blame] | 2461 | Preamble += utostr(Exp->getString()->getByteLength()) + "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2462 | |
| 2463 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2464 | &Context->Idents.get(S), strType, 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2465 | VarDecl::Static); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2466 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, strType, SourceLocation()); |
| 2467 | Expr *Unop = new (Context) UnaryOperator(DRE, UnaryOperator::AddrOf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2468 | Context->getPointerType(DRE->getType()), |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2469 | SourceLocation()); |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2470 | // cast to NSConstantString * |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2471 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Exp->getType(), |
| 2472 | CastExpr::CK_Unknown, Unop); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2473 | ReplaceStmt(Exp, cast); |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 2474 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2475 | return cast; |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2476 | } |
| 2477 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2478 | ObjCInterfaceDecl *RewriteObjC::isSuperReceiver(Expr *recExpr) { |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2479 | // check if we are sending a message to 'super' |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 2480 | if (!CurMethodDef || !CurMethodDef->isInstanceMethod()) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2481 | |
Douglas Gregor | cd9b46e | 2008-11-04 14:56:14 +0000 | [diff] [blame] | 2482 | if (ObjCSuperExpr *Super = dyn_cast<ObjCSuperExpr>(recExpr)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2483 | const ObjCObjectPointerType *OPT = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2484 | Super->getType()->getAs<ObjCObjectPointerType>(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2485 | assert(OPT); |
| 2486 | const ObjCInterfaceType *IT = OPT->getInterfaceType(); |
Chris Lattner | 0d17f6f | 2008-06-21 18:04:54 +0000 | [diff] [blame] | 2487 | return IT->getDecl(); |
| 2488 | } |
| 2489 | return 0; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2490 | } |
| 2491 | |
| 2492 | // struct objc_super { struct objc_object *receiver; struct objc_class *super; }; |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2493 | QualType RewriteObjC::getSuperStructType() { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2494 | if (!SuperStructDecl) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2495 | SuperStructDecl = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2496 | SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2497 | &Context->Idents.get("objc_super")); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2498 | QualType FieldTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2499 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2500 | // struct objc_object *receiver; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2501 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2502 | // struct objc_class *super; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2503 | FieldTypes[1] = Context->getObjCClassType(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2504 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2505 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2506 | for (unsigned i = 0; i < 2; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2507 | SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl, |
| 2508 | SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2509 | FieldTypes[i], 0, |
| 2510 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2511 | /*Mutable=*/false)); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2512 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2513 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2514 | SuperStructDecl->completeDefinition(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2515 | } |
| 2516 | return Context->getTagDeclType(SuperStructDecl); |
| 2517 | } |
| 2518 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2519 | QualType RewriteObjC::getConstantStringStructType() { |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2520 | if (!ConstantStringDecl) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2521 | ConstantStringDecl = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2522 | SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2523 | &Context->Idents.get("__NSConstantStringImpl")); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2524 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2525 | |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2526 | // struct objc_object *receiver; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2527 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2528 | // int flags; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2529 | FieldTypes[1] = Context->IntTy; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2530 | // char *str; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2531 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2532 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2533 | FieldTypes[3] = Context->LongTy; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2534 | |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2535 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2536 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2537 | ConstantStringDecl->addDecl(FieldDecl::Create(*Context, |
| 2538 | ConstantStringDecl, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2539 | SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2540 | FieldTypes[i], 0, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2541 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2542 | /*Mutable=*/true)); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2543 | } |
| 2544 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2545 | ConstantStringDecl->completeDefinition(); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2546 | } |
| 2547 | return Context->getTagDeclType(ConstantStringDecl); |
| 2548 | } |
| 2549 | |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2550 | Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp, |
| 2551 | SourceLocation StartLoc, |
| 2552 | SourceLocation EndLoc) { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2553 | if (!SelGetUidFunctionDecl) |
| 2554 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2555 | if (!MsgSendFunctionDecl) |
| 2556 | SynthMsgSendFunctionDecl(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2557 | if (!MsgSendSuperFunctionDecl) |
| 2558 | SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2559 | if (!MsgSendStretFunctionDecl) |
| 2560 | SynthMsgSendStretFunctionDecl(); |
| 2561 | if (!MsgSendSuperStretFunctionDecl) |
| 2562 | SynthMsgSendSuperStretFunctionDecl(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2563 | if (!MsgSendFpretFunctionDecl) |
| 2564 | SynthMsgSendFpretFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2565 | if (!GetClassFunctionDecl) |
| 2566 | SynthGetClassFunctionDecl(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2567 | if (!GetMetaClassFunctionDecl) |
| 2568 | SynthGetMetaClassFunctionDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2569 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2570 | // default to objc_msgSend(). |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2571 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2572 | // May need to use objc_msgSend_stret() as well. |
| 2573 | FunctionDecl *MsgSendStretFlavor = 0; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2574 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
| 2575 | QualType resultType = mDecl->getResultType(); |
Chris Lattner | 8b51fd7 | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2576 | if (resultType->isStructureType() || resultType->isUnionType()) |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2577 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
Chris Lattner | 8b51fd7 | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2578 | else if (resultType->isRealFloatingType()) |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2579 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2580 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2581 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2582 | // Synthesize a call to objc_msgSend(). |
| 2583 | llvm::SmallVector<Expr*, 8> MsgExprs; |
| 2584 | IdentifierInfo *clsName = Exp->getClassName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2585 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2586 | // Derive/push the receiver/selector, 2 implicit arguments to objc_msgSend(). |
| 2587 | if (clsName) { // class message. |
Steve Naroff | fc93d52 | 2008-07-24 19:44:33 +0000 | [diff] [blame] | 2588 | // FIXME: We need to fix Sema (and the AST for ObjCMessageExpr) to handle |
| 2589 | // the 'super' idiom within a class method. |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 2590 | if (clsName->getName() == "super") { |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2591 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2592 | if (MsgSendStretFlavor) |
| 2593 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2594 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2595 | |
| 2596 | ObjCInterfaceDecl *SuperDecl = |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 2597 | CurMethodDef->getClassInterface()->getSuperClass(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2598 | |
| 2599 | llvm::SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2600 | |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2601 | // set the receiver to self, the first argument to all methods. |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2602 | InitExprs.push_back( |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2603 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 2604 | CastExpr::CK_Unknown, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2605 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2606 | Context->getObjCIdType(), |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2607 | SourceLocation())) |
| 2608 | ); // set the 'receiver'. |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2609 | |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2610 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2611 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2612 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2613 | SuperDecl->getIdentifier()->getNameStart(), |
| 2614 | SuperDecl->getIdentifier()->getLength(), |
| 2615 | false, argType, SourceLocation())); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2616 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2617 | &ClsExprs[0], |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2618 | ClsExprs.size(), |
| 2619 | StartLoc, |
| 2620 | EndLoc); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2621 | // To turn off a warning, type-cast to 'id' |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2622 | InitExprs.push_back( // set 'super class', using objc_getClass(). |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2623 | NoTypeInfoCStyleCastExpr(Context, |
| 2624 | Context->getObjCIdType(), |
| 2625 | CastExpr::CK_Unknown, Cls)); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2626 | // struct objc_super |
| 2627 | QualType superType = getSuperStructType(); |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2628 | Expr *SuperRep; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2629 | |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2630 | if (LangOpts.Microsoft) { |
| 2631 | SynthSuperContructorFunctionDecl(); |
| 2632 | // Simulate a contructor call... |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2633 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2634 | superType, SourceLocation()); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2635 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2636 | InitExprs.size(), |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2637 | superType, SourceLocation()); |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2638 | // The code for super is a little tricky to prevent collision with |
| 2639 | // the structure definition in the header. The rewriter has it's own |
| 2640 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2641 | // we need the cast below. For example: |
| 2642 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2643 | // |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2644 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2645 | Context->getPointerType(SuperRep->getType()), |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2646 | SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2647 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2648 | Context->getPointerType(superType), |
| 2649 | CastExpr::CK_Unknown, SuperRep); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2650 | } else { |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2651 | // (struct objc_super) { <exprs from above> } |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 2652 | InitListExpr *ILE = new (Context) InitListExpr(SourceLocation(), |
| 2653 | &InitExprs[0], InitExprs.size(), |
| 2654 | SourceLocation()); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 2655 | TypeSourceInfo *superTInfo |
| 2656 | = Context->getTrivialTypeSourceInfo(superType); |
John McCall | 1d7d8d6 | 2010-01-19 22:33:45 +0000 | [diff] [blame] | 2657 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
| 2658 | superType, ILE, false); |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2659 | // struct objc_super * |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2660 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2661 | Context->getPointerType(SuperRep->getType()), |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2662 | SourceLocation()); |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2663 | } |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2664 | MsgExprs.push_back(SuperRep); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2665 | } else { |
| 2666 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2667 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2668 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2669 | clsName->getNameStart(), |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2670 | clsName->getLength(), |
| 2671 | false, argType, |
| 2672 | SourceLocation())); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2673 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2674 | &ClsExprs[0], |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2675 | ClsExprs.size(), |
| 2676 | StartLoc, EndLoc); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2677 | MsgExprs.push_back(Cls); |
| 2678 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2679 | } else { // instance message. |
| 2680 | Expr *recExpr = Exp->getReceiver(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2681 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2682 | if (ObjCInterfaceDecl *SuperDecl = isSuperReceiver(recExpr)) { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2683 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2684 | if (MsgSendStretFlavor) |
| 2685 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2686 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2687 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2688 | llvm::SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2689 | |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2690 | InitExprs.push_back( |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2691 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 2692 | CastExpr::CK_Unknown, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2693 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
Steve Naroff | f616ebb | 2008-07-16 22:35:27 +0000 | [diff] [blame] | 2694 | Context->getObjCIdType(), |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2695 | SourceLocation())) |
| 2696 | ); // set the 'receiver'. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2697 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2698 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2699 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2700 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2701 | SuperDecl->getIdentifier()->getNameStart(), |
| 2702 | SuperDecl->getIdentifier()->getLength(), |
| 2703 | false, argType, SourceLocation())); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2704 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2705 | &ClsExprs[0], |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2706 | ClsExprs.size(), |
| 2707 | StartLoc, EndLoc); |
Fariborz Jahanian | 7127431 | 2007-12-05 17:29:46 +0000 | [diff] [blame] | 2708 | // To turn off a warning, type-cast to 'id' |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2709 | InitExprs.push_back( |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2710 | // set 'super class', using objc_getClass(). |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2711 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 2712 | CastExpr::CK_Unknown, Cls)); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2713 | // struct objc_super |
| 2714 | QualType superType = getSuperStructType(); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2715 | Expr *SuperRep; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2716 | |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2717 | if (LangOpts.Microsoft) { |
| 2718 | SynthSuperContructorFunctionDecl(); |
| 2719 | // Simulate a contructor call... |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2720 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2721 | superType, SourceLocation()); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2722 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2723 | InitExprs.size(), |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2724 | superType, SourceLocation()); |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2725 | // The code for super is a little tricky to prevent collision with |
| 2726 | // the structure definition in the header. The rewriter has it's own |
| 2727 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2728 | // we need the cast below. For example: |
| 2729 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2730 | // |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2731 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2732 | Context->getPointerType(SuperRep->getType()), |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2733 | SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2734 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2735 | Context->getPointerType(superType), |
| 2736 | CastExpr::CK_Unknown, SuperRep); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2737 | } else { |
| 2738 | // (struct objc_super) { <exprs from above> } |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 2739 | InitListExpr *ILE = new (Context) InitListExpr(SourceLocation(), |
| 2740 | &InitExprs[0], InitExprs.size(), |
| 2741 | SourceLocation()); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 2742 | TypeSourceInfo *superTInfo |
| 2743 | = Context->getTrivialTypeSourceInfo(superType); |
John McCall | 1d7d8d6 | 2010-01-19 22:33:45 +0000 | [diff] [blame] | 2744 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
| 2745 | superType, ILE, false); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2746 | } |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2747 | MsgExprs.push_back(SuperRep); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2748 | } else { |
Fariborz Jahanian | 7dd8283 | 2007-12-07 21:21:21 +0000 | [diff] [blame] | 2749 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 2750 | // Foo<Proto> *. |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2751 | while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr)) |
Fariborz Jahanian | 7dd8283 | 2007-12-07 21:21:21 +0000 | [diff] [blame] | 2752 | recExpr = CE->getSubExpr(); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2753 | recExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 2754 | CastExpr::CK_Unknown, recExpr); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2755 | MsgExprs.push_back(recExpr); |
| 2756 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2757 | } |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2758 | // 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] | 2759 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 2760 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2761 | SelExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 6e94ef5 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2762 | Exp->getSelector().getAsString().c_str(), |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2763 | Exp->getSelector().getAsString().size(), |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 2764 | false, argType, SourceLocation())); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2765 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2766 | &SelExprs[0], SelExprs.size(), |
| 2767 | StartLoc, |
| 2768 | EndLoc); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2769 | MsgExprs.push_back(SelExp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2770 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2771 | // Now push any user supplied arguments. |
| 2772 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2773 | Expr *userExpr = Exp->getArg(i); |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2774 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 2775 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 2776 | // Reuse the ICE type, it is exactly what the doctor ordered. |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2777 | QualType type = ICE->getType()->isObjCQualifiedIdType() |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2778 | ? Context->getObjCIdType() |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2779 | : ICE->getType(); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2780 | userExpr = NoTypeInfoCStyleCastExpr(Context, type, CastExpr::CK_Unknown, |
| 2781 | userExpr); |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2782 | } |
| 2783 | // Make id<P...> cast into an 'id' cast. |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2784 | else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2785 | if (CE->getType()->isObjCQualifiedIdType()) { |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2786 | while ((CE = dyn_cast<CStyleCastExpr>(userExpr))) |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2787 | userExpr = CE->getSubExpr(); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2788 | userExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 2789 | CastExpr::CK_Unknown, userExpr); |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2790 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2791 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2792 | MsgExprs.push_back(userExpr); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2793 | // We've transferred the ownership to MsgExprs. For now, we *don't* null |
| 2794 | // out the argument in the original expression (since we aren't deleting |
| 2795 | // the ObjCMessageExpr). See RewritePropertySetter() usage for more info. |
| 2796 | //Exp->setArg(i, 0); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2797 | } |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2798 | // Generate the funky cast. |
| 2799 | CastExpr *cast; |
| 2800 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 2801 | QualType returnType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2802 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2803 | // Push 'id' and 'SEL', the 2 implicit arguments. |
Steve Naroff | c3a438c | 2007-11-15 10:43:57 +0000 | [diff] [blame] | 2804 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 2805 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 2806 | else |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2807 | ArgTypes.push_back(Context->getObjCIdType()); |
| 2808 | ArgTypes.push_back(Context->getObjCSelType()); |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2809 | if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) { |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2810 | // Push any user argument types. |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2811 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 2812 | E = OMD->param_end(); PI != E; ++PI) { |
| 2813 | QualType t = (*PI)->getType()->isObjCQualifiedIdType() |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2814 | ? Context->getObjCIdType() |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2815 | : (*PI)->getType(); |
Steve Naroff | a206b06 | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 2816 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 2817 | if (isTopLevelBlockPointerType(t)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2818 | const BlockPointerType *BPT = t->getAs<BlockPointerType>(); |
Steve Naroff | a206b06 | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 2819 | t = Context->getPointerType(BPT->getPointeeType()); |
| 2820 | } |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 2821 | ArgTypes.push_back(t); |
| 2822 | } |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2823 | returnType = OMD->getResultType()->isObjCQualifiedIdType() |
| 2824 | ? Context->getObjCIdType() : OMD->getResultType(); |
Fariborz Jahanian | 86aa9fd | 2010-02-24 01:25:40 +0000 | [diff] [blame] | 2825 | if (isTopLevelBlockPointerType(returnType)) { |
| 2826 | const BlockPointerType *BPT = returnType->getAs<BlockPointerType>(); |
| 2827 | returnType = Context->getPointerType(BPT->getPointeeType()); |
| 2828 | } |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2829 | } else { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2830 | returnType = Context->getObjCIdType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2831 | } |
| 2832 | // 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] | 2833 | QualType msgSendType = MsgSendFlavor->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2834 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2835 | // Create a reference to the objc_msgSend() declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2836 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, msgSendType, |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2837 | SourceLocation()); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2838 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2839 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2840 | // If we don't do this cast, we get the following bizarre warning/note: |
| 2841 | // xx.m:13: warning: function called through a non-compatible type |
| 2842 | // 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] | 2843 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 2844 | Context->getPointerType(Context->VoidTy), |
| 2845 | CastExpr::CK_Unknown, DRE); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2846 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2847 | // Now do the "normal" pointer to function cast. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2848 | QualType castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | d0ee6f9 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 2849 | &ArgTypes[0], ArgTypes.size(), |
Steve Naroff | 2679e48 | 2008-03-18 02:02:04 +0000 | [diff] [blame] | 2850 | // If we don't have a method decl, force a variadic cast. |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2851 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true, 0, |
| 2852 | false, false, 0, 0, false, |
| 2853 | CC_Default); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2854 | castType = Context->getPointerType(castType); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2855 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CastExpr::CK_Unknown, |
| 2856 | cast); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2857 | |
| 2858 | // Don't forget the parens to enforce the proper binding. |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2859 | ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2860 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2861 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2862 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2863 | MsgExprs.size(), |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2864 | FT->getResultType(), EndLoc); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2865 | Stmt *ReplacingStmt = CE; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2866 | if (MsgSendStretFlavor) { |
| 2867 | // We have the method which returns a struct/union. Must also generate |
| 2868 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 2869 | // expression which dictate which one to envoke depending on size of |
| 2870 | // method's return type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2871 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2872 | // Create a reference to the objc_msgSend_stret() declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2873 | DeclRefExpr *STDRE = new (Context) DeclRefExpr(MsgSendStretFlavor, msgSendType, |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2874 | SourceLocation()); |
| 2875 | // Need to cast objc_msgSend_stret to "void *" (see above comment). |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2876 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 2877 | Context->getPointerType(Context->VoidTy), |
| 2878 | CastExpr::CK_Unknown, STDRE); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2879 | // Now do the "normal" pointer to function cast. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2880 | castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | d0ee6f9 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 2881 | &ArgTypes[0], ArgTypes.size(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2882 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false, 0, |
| 2883 | false, false, 0, 0, false, |
| 2884 | CC_Default); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2885 | castType = Context->getPointerType(castType); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2886 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CastExpr::CK_Unknown, |
| 2887 | cast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2888 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2889 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2890 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2891 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2892 | FT = msgSendType->getAs<FunctionType>(); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2893 | CallExpr *STCE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2894 | MsgExprs.size(), |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2895 | FT->getResultType(), SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2896 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2897 | // Build sizeof(returnType) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2898 | SizeOfAlignOfExpr *sizeofExpr = new (Context) SizeOfAlignOfExpr(true, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2899 | Context->getTrivialTypeSourceInfo(returnType), |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2900 | Context->getSizeType(), |
| 2901 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2902 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 2903 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 2904 | // For X86 it is more complicated and some kind of target specific routine |
| 2905 | // is needed to decide what to do. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2906 | unsigned IntSize = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2907 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2908 | IntegerLiteral *limit = new (Context) IntegerLiteral(llvm::APInt(IntSize, 8), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2909 | Context->IntTy, |
| 2910 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2911 | BinaryOperator *lessThanExpr = new (Context) BinaryOperator(sizeofExpr, limit, |
| 2912 | BinaryOperator::LE, |
| 2913 | Context->IntTy, |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2914 | SourceLocation()); |
| 2915 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2916 | ConditionalOperator *CondExpr = |
Douglas Gregor | 47e1f7c | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 2917 | new (Context) ConditionalOperator(lessThanExpr, |
| 2918 | SourceLocation(), CE, |
| 2919 | SourceLocation(), STCE, returnType); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2920 | ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), CondExpr); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2921 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2922 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2923 | return ReplacingStmt; |
| 2924 | } |
| 2925 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2926 | Stmt *RewriteObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | 1d35b16 | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2927 | Stmt *ReplacingStmt = SynthMessageExpr(Exp, Exp->getLocStart(), |
| 2928 | Exp->getLocEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2929 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2930 | // Now do the actual rewrite. |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2931 | ReplaceStmt(Exp, ReplacingStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2932 | |
| 2933 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2934 | return ReplacingStmt; |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2935 | } |
| 2936 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2937 | // typedef struct objc_object Protocol; |
| 2938 | QualType RewriteObjC::getProtocolType() { |
| 2939 | if (!ProtocolTypeDecl) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2940 | TypeSourceInfo *TInfo |
| 2941 | = Context->getTrivialTypeSourceInfo(Context->getObjCIdType()); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2942 | ProtocolTypeDecl = TypedefDecl::Create(*Context, TUDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2943 | SourceLocation(), |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2944 | &Context->Idents.get("Protocol"), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2945 | TInfo); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2946 | } |
| 2947 | return Context->getTypeDeclType(ProtocolTypeDecl); |
| 2948 | } |
| 2949 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2950 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2951 | /// a synthesized/forward data reference (to the protocol's metadata). |
| 2952 | /// The forward references (and metadata) are generated in |
| 2953 | /// RewriteObjC::HandleTranslationUnit(). |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2954 | Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2955 | std::string Name = "_OBJC_PROTOCOL_" + Exp->getProtocol()->getNameAsString(); |
| 2956 | IdentifierInfo *ID = &Context->Idents.get(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2957 | VarDecl *VD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Douglas Gregor | 0da76df | 2009-11-23 11:41:28 +0000 | [diff] [blame] | 2958 | ID, getProtocolType(), 0, VarDecl::Extern); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2959 | DeclRefExpr *DRE = new (Context) DeclRefExpr(VD, getProtocolType(), SourceLocation()); |
| 2960 | Expr *DerefExpr = new (Context) UnaryOperator(DRE, UnaryOperator::AddrOf, |
| 2961 | Context->getPointerType(DRE->getType()), |
| 2962 | SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2963 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, DerefExpr->getType(), |
| 2964 | CastExpr::CK_Unknown, |
| 2965 | DerefExpr); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2966 | ReplaceStmt(Exp, castExpr); |
| 2967 | ProtocolExprDecls.insert(Exp->getProtocol()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2968 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2969 | return castExpr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2970 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2971 | } |
| 2972 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2973 | bool RewriteObjC::BufferContainsPPDirectives(const char *startBuf, |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2974 | const char *endBuf) { |
| 2975 | while (startBuf < endBuf) { |
| 2976 | if (*startBuf == '#') { |
| 2977 | // Skip whitespace. |
| 2978 | for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf) |
| 2979 | ; |
| 2980 | if (!strncmp(startBuf, "if", strlen("if")) || |
| 2981 | !strncmp(startBuf, "ifdef", strlen("ifdef")) || |
| 2982 | !strncmp(startBuf, "ifndef", strlen("ifndef")) || |
| 2983 | !strncmp(startBuf, "define", strlen("define")) || |
| 2984 | !strncmp(startBuf, "undef", strlen("undef")) || |
| 2985 | !strncmp(startBuf, "else", strlen("else")) || |
| 2986 | !strncmp(startBuf, "elif", strlen("elif")) || |
| 2987 | !strncmp(startBuf, "endif", strlen("endif")) || |
| 2988 | !strncmp(startBuf, "pragma", strlen("pragma")) || |
| 2989 | !strncmp(startBuf, "include", strlen("include")) || |
| 2990 | !strncmp(startBuf, "import", strlen("import")) || |
| 2991 | !strncmp(startBuf, "include_next", strlen("include_next"))) |
| 2992 | return true; |
| 2993 | } |
| 2994 | startBuf++; |
| 2995 | } |
| 2996 | return false; |
| 2997 | } |
| 2998 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2999 | /// SynthesizeObjCInternalStruct - Rewrite one internal struct corresponding to |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3000 | /// an objective-c class with ivars. |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3001 | void RewriteObjC::SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3002 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3003 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3004 | assert(CDecl->getNameAsCString() && |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 3005 | "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 3006 | // Do not synthesize more than once. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3007 | if (ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 3008 | return; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3009 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3010 | int NumIvars = CDecl->ivar_size(); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3011 | SourceLocation LocStart = CDecl->getLocStart(); |
| 3012 | SourceLocation LocEnd = CDecl->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3013 | |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3014 | const char *startBuf = SM->getCharacterData(LocStart); |
| 3015 | const char *endBuf = SM->getCharacterData(LocEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3016 | |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3017 | // If no ivars and no root or if its root, directly or indirectly, |
| 3018 | // 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] | 3019 | if ((CDecl->isForwardDecl() || NumIvars == 0) && |
| 3020 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3021 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3022 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3023 | return; |
| 3024 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3025 | |
| 3026 | // FIXME: This has potential of causing problem. If |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3027 | // SynthesizeObjCInternalStruct is ever called recursively. |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3028 | Result += "\nstruct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3029 | Result += CDecl->getNameAsString(); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 3030 | if (LangOpts.Microsoft) |
| 3031 | Result += "_IMPL"; |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 3032 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3033 | if (NumIvars > 0) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3034 | const char *cursor = strchr(startBuf, '{'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3035 | assert((cursor && endBuf) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3036 | && "SynthesizeObjCInternalStruct - malformed @interface"); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3037 | // If the buffer contains preprocessor directives, we do more fine-grained |
| 3038 | // rewrites. This is intended to fix code that looks like (which occurs in |
| 3039 | // NSURL.h, for example): |
| 3040 | // |
| 3041 | // #ifdef XYZ |
| 3042 | // @interface Foo : NSObject |
| 3043 | // #else |
| 3044 | // @interface FooBar : NSObject |
| 3045 | // #endif |
| 3046 | // { |
| 3047 | // int i; |
| 3048 | // } |
| 3049 | // @end |
| 3050 | // |
| 3051 | // This clause is segregated to avoid breaking the common case. |
| 3052 | if (BufferContainsPPDirectives(startBuf, cursor)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3053 | SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() : |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3054 | CDecl->getClassLoc(); |
| 3055 | const char *endHeader = SM->getCharacterData(L); |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3056 | endHeader += Lexer::MeasureTokenLength(L, *SM, LangOpts); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3057 | |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3058 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3059 | // advance to the end of the referenced protocols. |
| 3060 | while (endHeader < cursor && *endHeader != '>') endHeader++; |
| 3061 | endHeader++; |
| 3062 | } |
| 3063 | // rewrite the original header |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3064 | ReplaceText(LocStart, endHeader-startBuf, Result); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3065 | } else { |
| 3066 | // rewrite the original header *without* disturbing the '{' |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3067 | ReplaceText(LocStart, cursor-startBuf, Result); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3068 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3069 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3070 | Result = "\n struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3071 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 39bbd9f | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 3072 | Result += "_IMPL "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3073 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 819173c | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 3074 | Result += "_IVARS;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3075 | |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3076 | // insert the super class structure definition. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 3077 | SourceLocation OnePastCurly = |
| 3078 | LocStart.getFileLocWithOffset(cursor-startBuf+1); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3079 | InsertText(OnePastCurly, Result); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3080 | } |
| 3081 | cursor++; // past '{' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3082 | |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3083 | // Now comment out any visibility specifiers. |
| 3084 | while (cursor < endBuf) { |
| 3085 | if (*cursor == '@') { |
| 3086 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | df6a51b | 2007-11-14 22:57:51 +0000 | [diff] [blame] | 3087 | // Skip whitespace. |
| 3088 | for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor) |
| 3089 | /*scan*/; |
| 3090 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3091 | // FIXME: presence of @public, etc. inside comment results in |
| 3092 | // this transformation as well, which is still correct c-code. |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3093 | if (!strncmp(cursor, "public", strlen("public")) || |
| 3094 | !strncmp(cursor, "private", strlen("private")) || |
Steve Naroff | c5e3277 | 2008-04-04 22:34:24 +0000 | [diff] [blame] | 3095 | !strncmp(cursor, "package", strlen("package")) || |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3096 | !strncmp(cursor, "protected", strlen("protected"))) |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3097 | InsertText(atLoc, "// "); |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3098 | } |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3099 | // FIXME: If there are cases where '<' is used in ivar declaration part |
| 3100 | // of user code, then scan the ivar list and use needToScanForQualifiers |
| 3101 | // for type checking. |
| 3102 | else if (*cursor == '<') { |
| 3103 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3104 | InsertText(atLoc, "/* "); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3105 | cursor = strchr(cursor, '>'); |
| 3106 | cursor++; |
| 3107 | atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3108 | InsertText(atLoc, " */"); |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3109 | } else if (*cursor == '^') { // rewrite block specifier. |
| 3110 | SourceLocation caretLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3111 | ReplaceText(caretLoc, 1, "*"); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3112 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3113 | cursor++; |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3114 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3115 | // Don't forget to add a ';'!! |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3116 | InsertText(LocEnd.getFileLocWithOffset(1), ";"); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3117 | } else { // we don't have any instance variables - insert super struct. |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3118 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3119 | Result += " {\n struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3120 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 39bbd9f | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 3121 | Result += "_IMPL "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3122 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 819173c | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 3123 | Result += "_IVARS;\n};\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3124 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3125 | } |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3126 | // Mark this struct as having been generated. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3127 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
Steve Naroff | fbfe825 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 3128 | assert(false && "struct already synthesize- SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3129 | } |
| 3130 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3131 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3132 | /// class methods. |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3133 | template<typename MethodIterator> |
| 3134 | void RewriteObjC::RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 3135 | MethodIterator MethodEnd, |
Fariborz Jahanian | 8e991ba | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 3136 | bool IsInstanceMethod, |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3137 | const char *prefix, |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 3138 | const char *ClassName, |
| 3139 | std::string &Result) { |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3140 | if (MethodBegin == MethodEnd) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3141 | |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3142 | if (!objc_impl_method) { |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3143 | /* struct _objc_method { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3144 | SEL _cmd; |
| 3145 | char *method_types; |
| 3146 | void *_imp; |
| 3147 | } |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3148 | */ |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 3149 | Result += "\nstruct _objc_method {\n"; |
| 3150 | Result += "\tSEL _cmd;\n"; |
| 3151 | Result += "\tchar *method_types;\n"; |
| 3152 | Result += "\tvoid *_imp;\n"; |
| 3153 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3154 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3155 | objc_impl_method = true; |
Fariborz Jahanian | 776d6ff | 2007-10-19 00:36:46 +0000 | [diff] [blame] | 3156 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3157 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3158 | // Build _objc_method_list for class's methods if needed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3159 | |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3160 | /* struct { |
| 3161 | struct _objc_method_list *next_method; |
| 3162 | int method_count; |
| 3163 | struct _objc_method method_list[]; |
| 3164 | } |
| 3165 | */ |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3166 | unsigned NumMethods = std::distance(MethodBegin, MethodEnd); |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3167 | Result += "\nstatic struct {\n"; |
| 3168 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 3169 | Result += "\tint method_count;\n"; |
| 3170 | Result += "\tstruct _objc_method method_list["; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3171 | Result += utostr(NumMethods); |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3172 | Result += "];\n} _OBJC_"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3173 | Result += prefix; |
| 3174 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 3175 | Result += "_METHODS_"; |
| 3176 | Result += ClassName; |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3177 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3178 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 3179 | Result += "_meth\")))= "; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3180 | Result += "{\n\t0, " + utostr(NumMethods) + "\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3181 | |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3182 | Result += "\t,{{(SEL)\""; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3183 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3184 | std::string MethodTypeString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3185 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3186 | Result += "\", \""; |
| 3187 | Result += MethodTypeString; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3188 | Result += "\", (void *)"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3189 | Result += MethodInternalNames[*MethodBegin]; |
| 3190 | Result += "}\n"; |
| 3191 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 3192 | Result += "\t ,{(SEL)\""; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3193 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3194 | std::string MethodTypeString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3195 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3196 | Result += "\", \""; |
| 3197 | Result += MethodTypeString; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3198 | Result += "\", (void *)"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3199 | Result += MethodInternalNames[*MethodBegin]; |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 3200 | Result += "}\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3201 | } |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3202 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3203 | } |
| 3204 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3205 | /// RewriteObjCProtocolMetaData - Rewrite protocols meta-data. |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 3206 | void RewriteObjC:: |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3207 | RewriteObjCProtocolMetaData(ObjCProtocolDecl *PDecl, const char *prefix, |
| 3208 | const char *ClassName, std::string &Result) { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3209 | static bool objc_protocol_methods = false; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3210 | |
| 3211 | // Output struct protocol_methods holder of method selector and type. |
| 3212 | if (!objc_protocol_methods && !PDecl->isForwardDecl()) { |
| 3213 | /* struct protocol_methods { |
| 3214 | SEL _cmd; |
| 3215 | char *method_types; |
| 3216 | } |
| 3217 | */ |
| 3218 | Result += "\nstruct _protocol_methods {\n"; |
| 3219 | Result += "\tstruct objc_selector *_cmd;\n"; |
| 3220 | Result += "\tchar *method_types;\n"; |
| 3221 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3222 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3223 | objc_protocol_methods = true; |
| 3224 | } |
| 3225 | // Do not synthesize the protocol more than once. |
| 3226 | if (ObjCSynthesizedProtocols.count(PDecl)) |
| 3227 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3228 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3229 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 3230 | unsigned NumMethods = std::distance(PDecl->instmeth_begin(), |
| 3231 | PDecl->instmeth_end()); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3232 | /* struct _objc_protocol_method_list { |
| 3233 | int protocol_method_count; |
| 3234 | struct protocol_methods protocols[]; |
| 3235 | } |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3236 | */ |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3237 | Result += "\nstatic struct {\n"; |
| 3238 | Result += "\tint protocol_method_count;\n"; |
| 3239 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 3240 | Result += utostr(NumMethods); |
| 3241 | Result += "];\n} _OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 3242 | Result += PDecl->getNameAsString(); |
| 3243 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= " |
| 3244 | "{\n\t" + utostr(NumMethods) + "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3245 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3246 | // Output instance methods declared in this protocol. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3247 | for (ObjCProtocolDecl::instmeth_iterator |
| 3248 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3249 | I != E; ++I) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3250 | if (I == PDecl->instmeth_begin()) |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3251 | Result += "\t ,{{(struct objc_selector *)\""; |
| 3252 | else |
| 3253 | Result += "\t ,{(struct objc_selector *)\""; |
| 3254 | Result += (*I)->getSelector().getAsString().c_str(); |
| 3255 | std::string MethodTypeString; |
| 3256 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 3257 | Result += "\", \""; |
| 3258 | Result += MethodTypeString; |
| 3259 | Result += "\"}\n"; |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3260 | } |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3261 | Result += "\t }\n};\n"; |
| 3262 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3263 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3264 | // Output class methods declared in this protocol. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3265 | unsigned NumMethods = std::distance(PDecl->classmeth_begin(), |
| 3266 | PDecl->classmeth_end()); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3267 | if (NumMethods > 0) { |
| 3268 | /* struct _objc_protocol_method_list { |
| 3269 | int protocol_method_count; |
| 3270 | struct protocol_methods protocols[]; |
| 3271 | } |
| 3272 | */ |
| 3273 | Result += "\nstatic struct {\n"; |
| 3274 | Result += "\tint protocol_method_count;\n"; |
| 3275 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 3276 | Result += utostr(NumMethods); |
| 3277 | Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_"; |
| 3278 | Result += PDecl->getNameAsString(); |
| 3279 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 3280 | "{\n\t"; |
| 3281 | Result += utostr(NumMethods); |
| 3282 | Result += "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3283 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3284 | // Output instance methods declared in this protocol. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3285 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3286 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3287 | I != E; ++I) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3288 | if (I == PDecl->classmeth_begin()) |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3289 | Result += "\t ,{{(struct objc_selector *)\""; |
| 3290 | else |
| 3291 | Result += "\t ,{(struct objc_selector *)\""; |
| 3292 | Result += (*I)->getSelector().getAsString().c_str(); |
| 3293 | std::string MethodTypeString; |
| 3294 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 3295 | Result += "\", \""; |
| 3296 | Result += MethodTypeString; |
| 3297 | Result += "\"}\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3298 | } |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3299 | Result += "\t }\n};\n"; |
| 3300 | } |
| 3301 | |
| 3302 | // Output: |
| 3303 | /* struct _objc_protocol { |
| 3304 | // Objective-C 1.0 extensions |
| 3305 | struct _objc_protocol_extension *isa; |
| 3306 | char *protocol_name; |
| 3307 | struct _objc_protocol **protocol_list; |
| 3308 | struct _objc_protocol_method_list *instance_methods; |
| 3309 | struct _objc_protocol_method_list *class_methods; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3310 | }; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3311 | */ |
| 3312 | static bool objc_protocol = false; |
| 3313 | if (!objc_protocol) { |
| 3314 | Result += "\nstruct _objc_protocol {\n"; |
| 3315 | Result += "\tstruct _objc_protocol_extension *isa;\n"; |
| 3316 | Result += "\tchar *protocol_name;\n"; |
| 3317 | Result += "\tstruct _objc_protocol **protocol_list;\n"; |
| 3318 | Result += "\tstruct _objc_protocol_method_list *instance_methods;\n"; |
| 3319 | Result += "\tstruct _objc_protocol_method_list *class_methods;\n"; |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3320 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3321 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3322 | objc_protocol = true; |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3323 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3324 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3325 | Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_"; |
| 3326 | Result += PDecl->getNameAsString(); |
| 3327 | Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= " |
| 3328 | "{\n\t0, \""; |
| 3329 | Result += PDecl->getNameAsString(); |
| 3330 | Result += "\", 0, "; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3331 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3332 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 3333 | Result += PDecl->getNameAsString(); |
| 3334 | Result += ", "; |
| 3335 | } |
| 3336 | else |
| 3337 | Result += "0, "; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3338 | if (PDecl->classmeth_begin() != PDecl->classmeth_end()) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3339 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
| 3340 | Result += PDecl->getNameAsString(); |
| 3341 | Result += "\n"; |
| 3342 | } |
| 3343 | else |
| 3344 | Result += "0\n"; |
| 3345 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3346 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3347 | // Mark this protocol as having been generated. |
| 3348 | if (!ObjCSynthesizedProtocols.insert(PDecl)) |
| 3349 | assert(false && "protocol already synthesized"); |
| 3350 | |
| 3351 | } |
| 3352 | |
| 3353 | void RewriteObjC:: |
| 3354 | RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Protocols, |
| 3355 | const char *prefix, const char *ClassName, |
| 3356 | std::string &Result) { |
| 3357 | if (Protocols.empty()) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3358 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3359 | for (unsigned i = 0; i != Protocols.size(); i++) |
| 3360 | RewriteObjCProtocolMetaData(Protocols[i], prefix, ClassName, Result); |
| 3361 | |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3362 | // Output the top lovel protocol meta-data for the class. |
| 3363 | /* struct _objc_protocol_list { |
| 3364 | struct _objc_protocol_list *next; |
| 3365 | int protocol_count; |
| 3366 | struct _objc_protocol *class_protocols[]; |
| 3367 | } |
| 3368 | */ |
| 3369 | Result += "\nstatic struct {\n"; |
| 3370 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 3371 | Result += "\tint protocol_count;\n"; |
| 3372 | Result += "\tstruct _objc_protocol *class_protocols["; |
| 3373 | Result += utostr(Protocols.size()); |
| 3374 | Result += "];\n} _OBJC_"; |
| 3375 | Result += prefix; |
| 3376 | Result += "_PROTOCOLS_"; |
| 3377 | Result += ClassName; |
| 3378 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 3379 | "{\n\t0, "; |
| 3380 | Result += utostr(Protocols.size()); |
| 3381 | Result += "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3382 | |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3383 | Result += "\t,{&_OBJC_PROTOCOL_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3384 | Result += Protocols[0]->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3385 | Result += " \n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3386 | |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3387 | for (unsigned i = 1; i != Protocols.size(); i++) { |
| 3388 | Result += "\t ,&_OBJC_PROTOCOL_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3389 | Result += Protocols[i]->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3390 | Result += "\n"; |
| 3391 | } |
| 3392 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3393 | } |
| 3394 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3395 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3396 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3397 | /// implementation. |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3398 | void RewriteObjC::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3399 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3400 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3401 | // Find category declaration for this implementation. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3402 | ObjCCategoryDecl *CDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3403 | for (CDecl = ClassDecl->getCategoryList(); CDecl; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3404 | CDecl = CDecl->getNextClassCategory()) |
| 3405 | if (CDecl->getIdentifier() == IDecl->getIdentifier()) |
| 3406 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3407 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3408 | std::string FullCategoryName = ClassDecl->getNameAsString(); |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3409 | FullCategoryName += '_'; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3410 | FullCategoryName += IDecl->getNameAsString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3411 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3412 | // Build _objc_method_list for class's instance methods if needed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3413 | llvm::SmallVector<ObjCMethodDecl *, 32> |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3414 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3415 | |
| 3416 | // If any of our property implementations have associated getters or |
| 3417 | // setters, produce metadata for them as well. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3418 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 3419 | PropEnd = IDecl->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3420 | Prop != PropEnd; ++Prop) { |
| 3421 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 3422 | continue; |
| 3423 | if (!(*Prop)->getPropertyIvarDecl()) |
| 3424 | continue; |
| 3425 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 3426 | if (!PD) |
| 3427 | continue; |
| 3428 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 3429 | InstanceMethods.push_back(Getter); |
| 3430 | if (PD->isReadOnly()) |
| 3431 | continue; |
| 3432 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 3433 | InstanceMethods.push_back(Setter); |
| 3434 | } |
| 3435 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3436 | true, "CATEGORY_", FullCategoryName.c_str(), |
| 3437 | Result); |
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 | // Build _objc_method_list for class's class methods if needed |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3440 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3441 | false, "CATEGORY_", FullCategoryName.c_str(), |
| 3442 | Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3443 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3444 | // Protocols referenced in class declaration? |
Fariborz Jahanian | bac97d4 | 2007-11-13 22:09:49 +0000 | [diff] [blame] | 3445 | // Null CDecl is case of a category implementation with no category interface |
| 3446 | if (CDecl) |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3447 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), "CATEGORY", |
| 3448 | FullCategoryName.c_str(), Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3449 | /* struct _objc_category { |
| 3450 | char *category_name; |
| 3451 | char *class_name; |
| 3452 | struct _objc_method_list *instance_methods; |
| 3453 | struct _objc_method_list *class_methods; |
| 3454 | struct _objc_protocol_list *protocols; |
| 3455 | // Objective-C 1.0 extensions |
| 3456 | uint32_t size; // sizeof (struct _objc_category) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3457 | struct _objc_property_list *instance_properties; // category's own |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3458 | // @property decl. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3459 | }; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3460 | */ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3461 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3462 | static bool objc_category = false; |
| 3463 | if (!objc_category) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3464 | Result += "\nstruct _objc_category {\n"; |
| 3465 | Result += "\tchar *category_name;\n"; |
| 3466 | Result += "\tchar *class_name;\n"; |
| 3467 | Result += "\tstruct _objc_method_list *instance_methods;\n"; |
| 3468 | Result += "\tstruct _objc_method_list *class_methods;\n"; |
| 3469 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3470 | Result += "\tunsigned int size;\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3471 | Result += "\tstruct _objc_property_list *instance_properties;\n"; |
| 3472 | Result += "};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3473 | objc_category = true; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3474 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3475 | Result += "\nstatic struct _objc_category _OBJC_CATEGORY_"; |
| 3476 | Result += FullCategoryName; |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3477 | Result += " __attribute__ ((used, section (\"__OBJC, __category\")))= {\n\t\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3478 | Result += IDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3479 | Result += "\"\n\t, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3480 | Result += ClassDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3481 | Result += "\"\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3482 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3483 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3484 | Result += "\t, (struct _objc_method_list *)" |
| 3485 | "&_OBJC_CATEGORY_INSTANCE_METHODS_"; |
| 3486 | Result += FullCategoryName; |
| 3487 | Result += "\n"; |
| 3488 | } |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3489 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3490 | Result += "\t, 0\n"; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3491 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3492 | Result += "\t, (struct _objc_method_list *)" |
| 3493 | "&_OBJC_CATEGORY_CLASS_METHODS_"; |
| 3494 | Result += FullCategoryName; |
| 3495 | Result += "\n"; |
| 3496 | } |
| 3497 | else |
| 3498 | Result += "\t, 0\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3499 | |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3500 | if (CDecl && CDecl->protocol_begin() != CDecl->protocol_end()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3501 | Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3502 | Result += FullCategoryName; |
| 3503 | Result += "\n"; |
| 3504 | } |
| 3505 | else |
| 3506 | Result += "\t, 0\n"; |
| 3507 | Result += "\t, sizeof(struct _objc_category), 0\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3508 | } |
| 3509 | |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3510 | /// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of |
| 3511 | /// ivar offset. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3512 | void RewriteObjC::SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl, |
| 3513 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3514 | std::string &Result) { |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3515 | if (ivar->isBitField()) { |
| 3516 | // FIXME: The hack below doesn't work for bitfields. For now, we simply |
| 3517 | // place all bitfields at offset 0. |
| 3518 | Result += "0"; |
| 3519 | } else { |
| 3520 | Result += "__OFFSETOFIVAR__(struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3521 | Result += IDecl->getNameAsString(); |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3522 | if (LangOpts.Microsoft) |
| 3523 | Result += "_IMPL"; |
| 3524 | Result += ", "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3525 | Result += ivar->getNameAsString(); |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3526 | Result += ")"; |
| 3527 | } |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3528 | } |
| 3529 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3530 | //===----------------------------------------------------------------------===// |
| 3531 | // Meta Data Emission |
| 3532 | //===----------------------------------------------------------------------===// |
| 3533 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3534 | void RewriteObjC::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3535 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3536 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3537 | |
Fariborz Jahanian | ebe668f | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3538 | // Explictly declared @interface's are already synthesized. |
Steve Naroff | 33feeb0 | 2009-04-20 20:09:33 +0000 | [diff] [blame] | 3539 | if (CDecl->isImplicitInterfaceDecl()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3540 | // FIXME: Implementation of a class with no @interface (legacy) doese not |
Fariborz Jahanian | ebe668f | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3541 | // produce correct synthesis as yet. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3542 | SynthesizeObjCInternalStruct(CDecl, Result); |
Fariborz Jahanian | ebe668f | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3543 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3544 | |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3545 | // Build _objc_ivar_list metadata for classes ivars if needed |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3546 | unsigned NumIvars = !IDecl->ivar_empty() |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3547 | ? IDecl->ivar_size() |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3548 | : (CDecl ? CDecl->ivar_size() : 0); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3549 | if (NumIvars > 0) { |
| 3550 | static bool objc_ivar = false; |
| 3551 | if (!objc_ivar) { |
| 3552 | /* struct _objc_ivar { |
| 3553 | char *ivar_name; |
| 3554 | char *ivar_type; |
| 3555 | int ivar_offset; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3556 | }; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3557 | */ |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3558 | Result += "\nstruct _objc_ivar {\n"; |
| 3559 | Result += "\tchar *ivar_name;\n"; |
| 3560 | Result += "\tchar *ivar_type;\n"; |
| 3561 | Result += "\tint ivar_offset;\n"; |
| 3562 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3563 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3564 | objc_ivar = true; |
| 3565 | } |
| 3566 | |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3567 | /* struct { |
| 3568 | int ivar_count; |
| 3569 | struct _objc_ivar ivar_list[nIvars]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3570 | }; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3571 | */ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3572 | Result += "\nstatic struct {\n"; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3573 | Result += "\tint ivar_count;\n"; |
| 3574 | Result += "\tstruct _objc_ivar ivar_list["; |
| 3575 | Result += utostr(NumIvars); |
| 3576 | Result += "];\n} _OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3577 | Result += IDecl->getNameAsString(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3578 | Result += " __attribute__ ((used, section (\"__OBJC, __instance_vars\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3579 | "{\n\t"; |
| 3580 | Result += utostr(NumIvars); |
| 3581 | Result += "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3582 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3583 | ObjCInterfaceDecl::ivar_iterator IVI, IVE; |
Douglas Gregor | 8f36aba | 2009-04-23 03:23:08 +0000 | [diff] [blame] | 3584 | llvm::SmallVector<ObjCIvarDecl *, 8> IVars; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3585 | if (!IDecl->ivar_empty()) { |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 3586 | for (ObjCInterfaceDecl::ivar_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3587 | IV = IDecl->ivar_begin(), IVEnd = IDecl->ivar_end(); |
Douglas Gregor | 8f36aba | 2009-04-23 03:23:08 +0000 | [diff] [blame] | 3588 | IV != IVEnd; ++IV) |
| 3589 | IVars.push_back(*IV); |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 3590 | IVI = IDecl->ivar_begin(); |
| 3591 | IVE = IDecl->ivar_end(); |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3592 | } else { |
| 3593 | IVI = CDecl->ivar_begin(); |
| 3594 | IVE = CDecl->ivar_end(); |
| 3595 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3596 | Result += "\t,{{\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3597 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3598 | Result += "\", \""; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3599 | std::string TmpString, StrEncoding; |
| 3600 | Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI); |
| 3601 | QuoteDoublequotes(TmpString, StrEncoding); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3602 | Result += StrEncoding; |
| 3603 | Result += "\", "; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3604 | SynthesizeIvarOffsetComputation(IDecl, *IVI, Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3605 | Result += "}\n"; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3606 | for (++IVI; IVI != IVE; ++IVI) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3607 | Result += "\t ,{\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3608 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3609 | Result += "\", \""; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3610 | std::string TmpString, StrEncoding; |
| 3611 | Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI); |
| 3612 | QuoteDoublequotes(TmpString, StrEncoding); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3613 | Result += StrEncoding; |
| 3614 | Result += "\", "; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3615 | SynthesizeIvarOffsetComputation(IDecl, (*IVI), Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3616 | Result += "}\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3617 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3618 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3619 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3620 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3621 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3622 | // Build _objc_method_list for class's instance methods if needed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3623 | llvm::SmallVector<ObjCMethodDecl *, 32> |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3624 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3625 | |
| 3626 | // If any of our property implementations have associated getters or |
| 3627 | // setters, produce metadata for them as well. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3628 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 3629 | PropEnd = IDecl->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3630 | Prop != PropEnd; ++Prop) { |
| 3631 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 3632 | continue; |
| 3633 | if (!(*Prop)->getPropertyIvarDecl()) |
| 3634 | continue; |
| 3635 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 3636 | if (!PD) |
| 3637 | continue; |
| 3638 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 3639 | InstanceMethods.push_back(Getter); |
| 3640 | if (PD->isReadOnly()) |
| 3641 | continue; |
| 3642 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 3643 | InstanceMethods.push_back(Setter); |
| 3644 | } |
| 3645 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3646 | true, "", IDecl->getNameAsCString(), Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3647 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3648 | // Build _objc_method_list for class's class methods if needed |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3649 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3650 | false, "", IDecl->getNameAsCString(), Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3651 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3652 | // Protocols referenced in class declaration? |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3653 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), |
| 3654 | "CLASS", CDecl->getNameAsCString(), Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3655 | |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3656 | // Declaration of class/meta-class metadata |
| 3657 | /* struct _objc_class { |
| 3658 | struct _objc_class *isa; // or const char *root_class_name when metadata |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3659 | const char *super_class_name; |
| 3660 | char *name; |
| 3661 | long version; |
| 3662 | long info; |
| 3663 | long instance_size; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3664 | struct _objc_ivar_list *ivars; |
| 3665 | struct _objc_method_list *methods; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3666 | struct objc_cache *cache; |
| 3667 | struct objc_protocol_list *protocols; |
| 3668 | const char *ivar_layout; |
| 3669 | struct _objc_class_ext *ext; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3670 | }; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3671 | */ |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3672 | static bool objc_class = false; |
| 3673 | if (!objc_class) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3674 | Result += "\nstruct _objc_class {\n"; |
| 3675 | Result += "\tstruct _objc_class *isa;\n"; |
| 3676 | Result += "\tconst char *super_class_name;\n"; |
| 3677 | Result += "\tchar *name;\n"; |
| 3678 | Result += "\tlong version;\n"; |
| 3679 | Result += "\tlong info;\n"; |
| 3680 | Result += "\tlong instance_size;\n"; |
| 3681 | Result += "\tstruct _objc_ivar_list *ivars;\n"; |
| 3682 | Result += "\tstruct _objc_method_list *methods;\n"; |
| 3683 | Result += "\tstruct objc_cache *cache;\n"; |
| 3684 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 3685 | Result += "\tconst char *ivar_layout;\n"; |
| 3686 | Result += "\tstruct _objc_class_ext *ext;\n"; |
| 3687 | Result += "};\n"; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3688 | objc_class = true; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3689 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3690 | |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3691 | // Meta-class metadata generation. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3692 | ObjCInterfaceDecl *RootClass = 0; |
| 3693 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3694 | while (SuperClass) { |
| 3695 | RootClass = SuperClass; |
| 3696 | SuperClass = SuperClass->getSuperClass(); |
| 3697 | } |
| 3698 | SuperClass = CDecl->getSuperClass(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3699 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3700 | Result += "\nstatic struct _objc_class _OBJC_METACLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3701 | Result += CDecl->getNameAsString(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3702 | Result += " __attribute__ ((used, section (\"__OBJC, __meta_class\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3703 | "{\n\t(struct _objc_class *)\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3704 | Result += (RootClass ? RootClass->getNameAsString() : CDecl->getNameAsString()); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3705 | Result += "\""; |
| 3706 | |
| 3707 | if (SuperClass) { |
| 3708 | Result += ", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3709 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3710 | Result += "\", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3711 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3712 | Result += "\""; |
| 3713 | } |
| 3714 | else { |
| 3715 | Result += ", 0, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3716 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3717 | Result += "\""; |
| 3718 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3719 | // 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] | 3720 | // 'info' field is initialized to CLS_META(2) for metaclass |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3721 | Result += ", 0,2, sizeof(struct _objc_class), 0"; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3722 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 3723 | Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3724 | Result += IDecl->getNameAsString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3725 | Result += "\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3726 | } |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3727 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3728 | Result += ", 0\n"; |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3729 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3730 | Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3731 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3732 | Result += ",0,0\n"; |
| 3733 | } |
Fariborz Jahanian | 454cb01 | 2007-10-24 20:54:23 +0000 | [diff] [blame] | 3734 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3735 | Result += "\t,0,0,0,0\n"; |
| 3736 | Result += "};\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3737 | |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3738 | // class metadata generation. |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3739 | Result += "\nstatic struct _objc_class _OBJC_CLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3740 | Result += CDecl->getNameAsString(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3741 | Result += " __attribute__ ((used, section (\"__OBJC, __class\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3742 | "{\n\t&_OBJC_METACLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3743 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3744 | if (SuperClass) { |
| 3745 | Result += ", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3746 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3747 | Result += "\", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3748 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3749 | Result += "\""; |
| 3750 | } |
| 3751 | else { |
| 3752 | Result += ", 0, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3753 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3754 | Result += "\""; |
| 3755 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3756 | // 'info' field is initialized to CLS_CLASS(1) for class |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3757 | Result += ", 0,1"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3758 | if (!ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3759 | Result += ",0"; |
| 3760 | else { |
| 3761 | // class has size. Must synthesize its size. |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 3762 | Result += ",sizeof(struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3763 | Result += CDecl->getNameAsString(); |
Steve Naroff | ba9ac4e | 2008-03-10 23:33:22 +0000 | [diff] [blame] | 3764 | if (LangOpts.Microsoft) |
| 3765 | Result += "_IMPL"; |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3766 | Result += ")"; |
| 3767 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3768 | if (NumIvars > 0) { |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 3769 | Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3770 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3771 | Result += "\n\t"; |
| 3772 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3773 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3774 | Result += ",0"; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3775 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3776 | Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3777 | Result += CDecl->getNameAsString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3778 | Result += ", 0\n\t"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3779 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3780 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3781 | Result += ",0,0"; |
Chris Lattner | cafeb35 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3782 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3783 | Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3784 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3785 | Result += ", 0,0\n"; |
| 3786 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3787 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3788 | Result += ",0,0,0\n"; |
| 3789 | Result += "};\n"; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3790 | } |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3791 | |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3792 | /// RewriteImplementations - This routine rewrites all method implementations |
| 3793 | /// and emits meta-data. |
| 3794 | |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3795 | void RewriteObjC::RewriteImplementations() { |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3796 | int ClsDefCount = ClassImplementation.size(); |
| 3797 | int CatDefCount = CategoryImplementation.size(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3798 | |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3799 | // Rewrite implemented methods |
| 3800 | for (int i = 0; i < ClsDefCount; i++) |
| 3801 | RewriteImplementationDecl(ClassImplementation[i]); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3802 | |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 3803 | for (int i = 0; i < CatDefCount; i++) |
| 3804 | RewriteImplementationDecl(CategoryImplementation[i]); |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3805 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3806 | |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3807 | void RewriteObjC::SynthesizeMetaDataIntoBuffer(std::string &Result) { |
| 3808 | int ClsDefCount = ClassImplementation.size(); |
| 3809 | int CatDefCount = CategoryImplementation.size(); |
| 3810 | |
Steve Naroff | 5df5b76 | 2008-05-07 21:23:49 +0000 | [diff] [blame] | 3811 | // This is needed for determining instance variable offsets. |
Fariborz Jahanian | c98cbb4 | 2010-01-07 18:31:42 +0000 | [diff] [blame] | 3812 | Result += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((long) &((TYPE *)0)->MEMBER)\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3813 | // For each implemented class, write out all its meta data. |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3814 | for (int i = 0; i < ClsDefCount; i++) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3815 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3816 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3817 | // For each implemented category, write out all its meta data. |
| 3818 | for (int i = 0; i < CatDefCount; i++) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3819 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3820 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3821 | // Write objc_symtab metadata |
| 3822 | /* |
| 3823 | struct _objc_symtab |
| 3824 | { |
| 3825 | long sel_ref_cnt; |
| 3826 | SEL *refs; |
| 3827 | short cls_def_cnt; |
| 3828 | short cat_def_cnt; |
| 3829 | void *defs[cls_def_cnt + cat_def_cnt]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3830 | }; |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3831 | */ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3832 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3833 | Result += "\nstruct _objc_symtab {\n"; |
| 3834 | Result += "\tlong sel_ref_cnt;\n"; |
| 3835 | Result += "\tSEL *refs;\n"; |
| 3836 | Result += "\tshort cls_def_cnt;\n"; |
| 3837 | Result += "\tshort cat_def_cnt;\n"; |
| 3838 | Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n"; |
| 3839 | Result += "};\n\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3840 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3841 | Result += "static struct _objc_symtab " |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3842 | "_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3843 | Result += "\t0, 0, " + utostr(ClsDefCount) |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3844 | + ", " + utostr(CatDefCount) + "\n"; |
| 3845 | for (int i = 0; i < ClsDefCount; i++) { |
| 3846 | Result += "\t,&_OBJC_CLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3847 | Result += ClassImplementation[i]->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3848 | Result += "\n"; |
| 3849 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3850 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3851 | for (int i = 0; i < CatDefCount; i++) { |
| 3852 | Result += "\t,&_OBJC_CATEGORY_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3853 | Result += CategoryImplementation[i]->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3854 | Result += "_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3855 | Result += CategoryImplementation[i]->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3856 | Result += "\n"; |
| 3857 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3858 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3859 | Result += "};\n\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3860 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3861 | // Write objc_module metadata |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3862 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3863 | /* |
| 3864 | struct _objc_module { |
| 3865 | long version; |
| 3866 | long size; |
| 3867 | const char *name; |
| 3868 | struct _objc_symtab *symtab; |
| 3869 | } |
| 3870 | */ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3871 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3872 | Result += "\nstruct _objc_module {\n"; |
| 3873 | Result += "\tlong version;\n"; |
| 3874 | Result += "\tlong size;\n"; |
| 3875 | Result += "\tconst char *name;\n"; |
| 3876 | Result += "\tstruct _objc_symtab *symtab;\n"; |
| 3877 | Result += "};\n\n"; |
| 3878 | Result += "static struct _objc_module " |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3879 | "_OBJC_MODULES __attribute__ ((used, section (\"__OBJC, __module_info\")))= {\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3880 | Result += "\t" + utostr(OBJC_ABI_VERSION) + |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3881 | ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3882 | Result += "};\n\n"; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3883 | |
| 3884 | if (LangOpts.Microsoft) { |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3885 | if (ProtocolExprDecls.size()) { |
| 3886 | Result += "#pragma section(\".objc_protocol$B\",long,read,write)\n"; |
| 3887 | Result += "#pragma data_seg(push, \".objc_protocol$B\")\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3888 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3889 | E = ProtocolExprDecls.end(); I != E; ++I) { |
| 3890 | Result += "static struct _objc_protocol *_POINTER_OBJC_PROTOCOL_"; |
| 3891 | Result += (*I)->getNameAsString(); |
| 3892 | Result += " = &_OBJC_PROTOCOL_"; |
| 3893 | Result += (*I)->getNameAsString(); |
| 3894 | Result += ";\n"; |
| 3895 | } |
| 3896 | Result += "#pragma data_seg(pop)\n\n"; |
| 3897 | } |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3898 | Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n"; |
Steve Naroff | 1919032 | 2008-05-07 00:06:16 +0000 | [diff] [blame] | 3899 | Result += "#pragma data_seg(push, \".objc_module_info$B\")\n"; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3900 | Result += "static struct _objc_module *_POINTER_OBJC_MODULES = "; |
| 3901 | Result += "&_OBJC_MODULES;\n"; |
| 3902 | Result += "#pragma data_seg(pop)\n\n"; |
| 3903 | } |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3904 | } |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 3905 | |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3906 | void RewriteObjC::RewriteByRefString(std::string &ResultStr, |
| 3907 | const std::string &Name, |
| 3908 | ValueDecl *VD) { |
| 3909 | assert(BlockByRefDeclNo.count(VD) && |
| 3910 | "RewriteByRefString: ByRef decl missing"); |
| 3911 | ResultStr += "struct __Block_byref_" + Name + |
| 3912 | "_" + utostr(BlockByRefDeclNo[VD]) ; |
| 3913 | } |
| 3914 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3915 | std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, |
| 3916 | const char *funcName, |
| 3917 | std::string Tag) { |
| 3918 | const FunctionType *AFT = CE->getFunctionType(); |
| 3919 | QualType RT = AFT->getResultType(); |
| 3920 | std::string StructRef = "struct " + Tag; |
| 3921 | std::string S = "static " + RT.getAsString() + " __" + |
| 3922 | funcName + "_" + "block_func_" + utostr(i); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 3923 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3924 | BlockDecl *BD = CE->getBlockDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3925 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3926 | if (isa<FunctionNoProtoType>(AFT)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3927 | // 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] | 3928 | // block (to reference imported block decl refs). |
| 3929 | S += "(" + StructRef + " *__cself)"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3930 | } else if (BD->param_empty()) { |
| 3931 | S += "(" + StructRef + " *__cself)"; |
| 3932 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3933 | const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3934 | assert(FT && "SynthesizeBlockFunc: No function proto"); |
| 3935 | S += '('; |
| 3936 | // first add the implicit argument. |
| 3937 | S += StructRef + " *__cself, "; |
| 3938 | std::string ParamStr; |
| 3939 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 3940 | E = BD->param_end(); AI != E; ++AI) { |
| 3941 | if (AI != BD->param_begin()) S += ", "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3942 | ParamStr = (*AI)->getNameAsString(); |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 3943 | (*AI)->getType().getAsStringInternal(ParamStr, Context->PrintingPolicy); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3944 | S += ParamStr; |
| 3945 | } |
| 3946 | if (FT->isVariadic()) { |
| 3947 | if (!BD->param_empty()) S += ", "; |
| 3948 | S += "..."; |
| 3949 | } |
| 3950 | S += ')'; |
| 3951 | } |
| 3952 | S += " {\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3953 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3954 | // Create local declarations to avoid rewriting all closure decl ref exprs. |
| 3955 | // First, emit a declaration for all "by ref" decls. |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 3956 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3957 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3958 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3959 | std::string Name = (*I)->getNameAsString(); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3960 | std::string TypeString; |
| 3961 | RewriteByRefString(TypeString, Name, (*I)); |
| 3962 | TypeString += " *"; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 3963 | Name = TypeString + Name; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3964 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3965 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3966 | // Next, emit a declaration for all "by copy" declarations. |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 3967 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3968 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3969 | S += " "; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3970 | // Handle nested closure invocation. For example: |
| 3971 | // |
| 3972 | // void (^myImportedClosure)(void); |
| 3973 | // myImportedClosure = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3974 | // |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3975 | // void (^anotherClosure)(void); |
| 3976 | // anotherClosure = ^(void) { |
| 3977 | // myImportedClosure(); // import and invoke the closure |
| 3978 | // }; |
| 3979 | // |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 3980 | if (isTopLevelBlockPointerType((*I)->getType())) { |
| 3981 | RewriteBlockPointerTypeVariable(S, (*I)); |
| 3982 | S += " = ("; |
| 3983 | RewriteBlockPointerType(S, (*I)->getType()); |
| 3984 | S += ")"; |
| 3985 | S += "__cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; |
| 3986 | } |
| 3987 | else { |
Fariborz Jahanian | 210c248 | 2010-02-16 17:26:03 +0000 | [diff] [blame] | 3988 | std::string Name = (*I)->getNameAsString(); |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 3989 | (*I)->getType().getAsStringInternal(Name, Context->PrintingPolicy); |
Fariborz Jahanian | e8c28df | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 3990 | S += Name + " = __cself->" + |
| 3991 | (*I)->getNameAsString() + "; // bound by copy\n"; |
| 3992 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3993 | } |
| 3994 | std::string RewrittenStr = RewrittenBlockExprs[CE]; |
| 3995 | const char *cstr = RewrittenStr.c_str(); |
| 3996 | while (*cstr++ != '{') ; |
| 3997 | S += cstr; |
| 3998 | S += "\n"; |
| 3999 | return S; |
| 4000 | } |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 4001 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4002 | std::string RewriteObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
| 4003 | const char *funcName, |
| 4004 | std::string Tag) { |
| 4005 | std::string StructRef = "struct " + Tag; |
| 4006 | std::string S = "static void __"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4007 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4008 | S += funcName; |
| 4009 | S += "_block_copy_" + utostr(i); |
| 4010 | S += "(" + StructRef; |
| 4011 | S += "*dst, " + StructRef; |
| 4012 | S += "*src) {"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4013 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4014 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 4015 | S += "_Block_object_assign((void*)&dst->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4016 | S += (*I)->getNameAsString(); |
Steve Naroff | 47a2422 | 2008-12-11 20:51:38 +0000 | [diff] [blame] | 4017 | S += ", (void*)src->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4018 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4019 | if (BlockByRefDeclsPtrSet.count((*I))) |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 4020 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | d25d1b5 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 4021 | else |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 4022 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4023 | } |
Fariborz Jahanian | d25d1b5 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 4024 | S += "}\n"; |
| 4025 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4026 | S += "\nstatic void __"; |
| 4027 | S += funcName; |
| 4028 | S += "_block_dispose_" + utostr(i); |
| 4029 | S += "(" + StructRef; |
| 4030 | S += "*src) {"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4031 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4032 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 4033 | S += "_Block_object_dispose((void*)src->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4034 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4035 | if (BlockByRefDeclsPtrSet.count((*I))) |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 4036 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | d25d1b5 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 4037 | else |
Fariborz Jahanian | 73e437b | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 4038 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4039 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4040 | S += "}\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4041 | return S; |
| 4042 | } |
| 4043 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4044 | std::string RewriteObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 4045 | std::string Desc) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 4046 | std::string S = "\nstruct " + Tag; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4047 | std::string Constructor = " " + Tag; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4048 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4049 | S += " {\n struct __block_impl impl;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4050 | S += " struct " + Desc; |
| 4051 | S += "* Desc;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4052 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4053 | Constructor += "(void *fp, "; // Invoke function pointer. |
| 4054 | Constructor += "struct " + Desc; // Descriptor pointer. |
| 4055 | Constructor += " *desc"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4056 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4057 | if (BlockDeclRefs.size()) { |
| 4058 | // Output all "by copy" declarations. |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4059 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4060 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 4061 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4062 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4063 | std::string ArgName = "_" + FieldName; |
| 4064 | // Handle nested closure invocation. For example: |
| 4065 | // |
| 4066 | // void (^myImportedBlock)(void); |
| 4067 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4068 | // |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4069 | // void (^anotherBlock)(void); |
| 4070 | // anotherBlock = ^(void) { |
| 4071 | // myImportedBlock(); // import and invoke the closure |
| 4072 | // }; |
| 4073 | // |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4074 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4075 | S += "struct __block_impl *"; |
| 4076 | Constructor += ", void *" + ArgName; |
| 4077 | } else { |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 4078 | (*I)->getType().getAsStringInternal(FieldName, Context->PrintingPolicy); |
| 4079 | (*I)->getType().getAsStringInternal(ArgName, Context->PrintingPolicy); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4080 | Constructor += ", " + ArgName; |
| 4081 | } |
| 4082 | S += FieldName + ";\n"; |
| 4083 | } |
| 4084 | // Output all "by ref" declarations. |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4085 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4086 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 4087 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4088 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4089 | std::string ArgName = "_" + FieldName; |
| 4090 | // Handle nested closure invocation. For example: |
| 4091 | // |
| 4092 | // void (^myImportedBlock)(void); |
| 4093 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4094 | // |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4095 | // void (^anotherBlock)(void); |
| 4096 | // anotherBlock = ^(void) { |
| 4097 | // myImportedBlock(); // import and invoke the closure |
| 4098 | // }; |
| 4099 | // |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4100 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4101 | S += "struct __block_impl *"; |
| 4102 | Constructor += ", void *" + ArgName; |
| 4103 | } else { |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4104 | std::string TypeString; |
| 4105 | RewriteByRefString(TypeString, FieldName, (*I)); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4106 | TypeString += " *"; |
| 4107 | FieldName = TypeString + FieldName; |
| 4108 | ArgName = TypeString + ArgName; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4109 | Constructor += ", " + ArgName; |
| 4110 | } |
| 4111 | S += FieldName + "; // by ref\n"; |
| 4112 | } |
| 4113 | // Finish writing the constructor. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4114 | Constructor += ", int flags=0) {\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4115 | if (GlobalVarDecl) |
| 4116 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 4117 | else |
| 4118 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4119 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4120 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4121 | Constructor += " Desc = desc;\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4122 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4123 | // Initialize all "by copy" arguments. |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4124 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4125 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4126 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4127 | Constructor += " "; |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4128 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4129 | Constructor += Name + " = (struct __block_impl *)_"; |
| 4130 | else |
| 4131 | Constructor += Name + " = _"; |
| 4132 | Constructor += Name + ";\n"; |
| 4133 | } |
| 4134 | // Initialize all "by ref" arguments. |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4135 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4136 | E = BlockByRefDecls.end(); I != E; ++I) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4137 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4138 | Constructor += " "; |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4139 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4140 | Constructor += Name + " = (struct __block_impl *)_"; |
| 4141 | else |
| 4142 | Constructor += Name + " = _"; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4143 | Constructor += Name + "->__forwarding;\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4144 | } |
| 4145 | } else { |
| 4146 | // Finish writing the constructor. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4147 | Constructor += ", int flags=0) {\n"; |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4148 | if (GlobalVarDecl) |
| 4149 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 4150 | else |
| 4151 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4152 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 4153 | Constructor += " Desc = desc;\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4154 | } |
| 4155 | Constructor += " "; |
| 4156 | Constructor += "}\n"; |
| 4157 | S += Constructor; |
| 4158 | S += "};\n"; |
| 4159 | return S; |
| 4160 | } |
| 4161 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4162 | std::string RewriteObjC::SynthesizeBlockDescriptor(std::string DescTag, |
| 4163 | std::string ImplTag, int i, |
| 4164 | const char *FunName, |
| 4165 | unsigned hasCopy) { |
| 4166 | std::string S = "\nstatic struct " + DescTag; |
| 4167 | |
| 4168 | S += " {\n unsigned long reserved;\n"; |
| 4169 | S += " unsigned long Block_size;\n"; |
| 4170 | if (hasCopy) { |
Fariborz Jahanian | 4fcc4fd | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 4171 | S += " void (*copy)(struct "; |
| 4172 | S += ImplTag; S += "*, struct "; |
| 4173 | S += ImplTag; S += "*);\n"; |
| 4174 | |
| 4175 | S += " void (*dispose)(struct "; |
| 4176 | S += ImplTag; S += "*);\n"; |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4177 | } |
| 4178 | S += "} "; |
| 4179 | |
| 4180 | S += DescTag + "_DATA = { 0, sizeof(struct "; |
| 4181 | S += ImplTag + ")"; |
| 4182 | if (hasCopy) { |
| 4183 | S += ", __" + std::string(FunName) + "_block_copy_" + utostr(i); |
| 4184 | S += ", __" + std::string(FunName) + "_block_dispose_" + utostr(i); |
| 4185 | } |
| 4186 | S += "};\n"; |
| 4187 | return S; |
| 4188 | } |
| 4189 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4190 | void RewriteObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 4191 | const char *FunName) { |
| 4192 | // Insert declaration for the function in which block literal is used. |
Fariborz Jahanian | bf07012 | 2010-01-15 18:14:52 +0000 | [diff] [blame] | 4193 | if (CurFunctionDeclToDeclareForBlock && !Blocks.empty()) |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 4194 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4195 | // Insert closures that were part of the function. |
| 4196 | for (unsigned i = 0; i < Blocks.size(); i++) { |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4197 | // Need to copy-in the inner copied-in variables not actually used in this |
| 4198 | // block. |
| 4199 | for (int j = 0; j < InnerDeclRefsCount[i]; j++) |
| 4200 | BlockDeclRefs.push_back(InnerDeclRefs[j]); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4201 | CollectBlockDeclRefInfo(Blocks[i]); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4202 | llvm::SmallPtrSet<ValueDecl *, 8> InnerBlockValueDecls; |
| 4203 | llvm::SmallVector<BlockDeclRefExpr *, 8> InnerBlockDeclRefs; |
| 4204 | GetInnerBlockDeclRefExprs(Blocks[i]->getBody(), |
| 4205 | InnerBlockDeclRefs, InnerBlockValueDecls); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4206 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4207 | std::string ImplTag = "__" + std::string(FunName) + "_block_impl_" + utostr(i); |
| 4208 | std::string DescTag = "__" + std::string(FunName) + "_block_desc_" + utostr(i); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4209 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4210 | std::string CI = SynthesizeBlockImpl(Blocks[i], ImplTag, DescTag); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4211 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4212 | InsertText(FunLocStart, CI); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4213 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4214 | std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, ImplTag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4215 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4216 | InsertText(FunLocStart, CF); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4217 | |
| 4218 | if (ImportedBlockDecls.size()) { |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4219 | std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, ImplTag); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4220 | InsertText(FunLocStart, HF); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4221 | } |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4222 | std::string BD = SynthesizeBlockDescriptor(DescTag, ImplTag, i, FunName, |
| 4223 | ImportedBlockDecls.size() > 0); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4224 | InsertText(FunLocStart, BD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4225 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4226 | BlockDeclRefs.clear(); |
| 4227 | BlockByRefDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4228 | BlockByRefDeclsPtrSet.clear(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4229 | BlockByCopyDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4230 | BlockByCopyDeclsPtrSet.clear(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4231 | BlockCallExprs.clear(); |
| 4232 | ImportedBlockDecls.clear(); |
| 4233 | } |
| 4234 | Blocks.clear(); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4235 | InnerDeclRefsCount.clear(); |
| 4236 | InnerDeclRefs.clear(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4237 | RewrittenBlockExprs.clear(); |
| 4238 | } |
| 4239 | |
| 4240 | void RewriteObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { |
| 4241 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4242 | const char *FuncName = FD->getNameAsCString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4243 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4244 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 4245 | } |
| 4246 | |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 4247 | static void BuildUniqueMethodName(std::string &Name, |
| 4248 | ObjCMethodDecl *MD) { |
| 4249 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
| 4250 | Name = IFace->getNameAsCString(); |
| 4251 | Name += "__" + MD->getSelector().getAsString(); |
| 4252 | // Convert colons to underscores. |
| 4253 | std::string::size_type loc = 0; |
| 4254 | while ((loc = Name.find(":", loc)) != std::string::npos) |
| 4255 | Name.replace(loc, 1, "_"); |
| 4256 | } |
| 4257 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4258 | void RewriteObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 4259 | //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n"); |
| 4260 | //SourceLocation FunLocStart = MD->getLocStart(); |
Fariborz Jahanian | 0e1c99a | 2010-01-29 01:55:49 +0000 | [diff] [blame] | 4261 | SourceLocation FunLocStart = MD->getLocStart(); |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 4262 | std::string FuncName; |
| 4263 | BuildUniqueMethodName(FuncName, MD); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4264 | SynthesizeBlockLiterals(FunLocStart, FuncName.c_str()); |
| 4265 | } |
| 4266 | |
| 4267 | void RewriteObjC::GetBlockDeclRefExprs(Stmt *S) { |
| 4268 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4269 | CI != E; ++CI) |
| 4270 | if (*CI) { |
| 4271 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 4272 | GetBlockDeclRefExprs(CBE->getBody()); |
| 4273 | else |
| 4274 | GetBlockDeclRefExprs(*CI); |
| 4275 | } |
| 4276 | // Handle specific things. |
| 4277 | if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) |
| 4278 | // FIXME: Handle enums. |
| 4279 | if (!isa<FunctionDecl>(CDRE->getDecl())) |
| 4280 | BlockDeclRefs.push_back(CDRE); |
| 4281 | return; |
| 4282 | } |
| 4283 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4284 | void RewriteObjC::GetInnerBlockDeclRefExprs(Stmt *S, |
| 4285 | llvm::SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs, |
| 4286 | llvm::SmallPtrSet<ValueDecl *, 8> &InnerBlockValueDecls) { |
| 4287 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4288 | CI != E; ++CI) |
| 4289 | if (*CI) { |
| 4290 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 4291 | GetInnerBlockDeclRefExprs(CBE->getBody(), |
| 4292 | InnerBlockDeclRefs, |
| 4293 | InnerBlockValueDecls); |
| 4294 | else |
| 4295 | GetInnerBlockDeclRefExprs(*CI, |
| 4296 | InnerBlockDeclRefs, |
| 4297 | InnerBlockValueDecls); |
| 4298 | |
| 4299 | } |
| 4300 | // Handle specific things. |
| 4301 | if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) |
| 4302 | if (!isa<FunctionDecl>(CDRE->getDecl()) && |
| 4303 | !CDRE->isByRef() && |
| 4304 | !InnerBlockValueDecls.count(CDRE->getDecl())) { |
| 4305 | InnerBlockValueDecls.insert(CDRE->getDecl()); |
| 4306 | InnerBlockDeclRefs.push_back(CDRE); |
| 4307 | } |
| 4308 | return; |
| 4309 | } |
| 4310 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4311 | void RewriteObjC::GetBlockCallExprs(Stmt *S) { |
| 4312 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4313 | CI != E; ++CI) |
| 4314 | if (*CI) { |
| 4315 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 4316 | GetBlockCallExprs(CBE->getBody()); |
| 4317 | else |
| 4318 | GetBlockCallExprs(*CI); |
| 4319 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4320 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4321 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
| 4322 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
| 4323 | BlockCallExprs[dyn_cast<BlockDeclRefExpr>(CE->getCallee())] = CE; |
| 4324 | } |
| 4325 | } |
| 4326 | return; |
| 4327 | } |
| 4328 | |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4329 | Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp, const Expr *BlockExp) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4330 | // Navigate to relevant type information. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4331 | const BlockPointerType *CPT = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4332 | |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4333 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BlockExp)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4334 | CPT = DRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4335 | } else if (const BlockDeclRefExpr *CDRE = |
| 4336 | dyn_cast<BlockDeclRefExpr>(BlockExp)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4337 | CPT = CDRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4338 | } else if (const MemberExpr *MExpr = dyn_cast<MemberExpr>(BlockExp)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4339 | CPT = MExpr->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4340 | } |
| 4341 | else if (const ParenExpr *PRE = dyn_cast<ParenExpr>(BlockExp)) { |
| 4342 | return SynthesizeBlockCall(Exp, PRE->getSubExpr()); |
| 4343 | } |
| 4344 | else if (const ImplicitCastExpr *IEXPR = dyn_cast<ImplicitCastExpr>(BlockExp)) |
| 4345 | CPT = IEXPR->getType()->getAs<BlockPointerType>(); |
| 4346 | else if (const ConditionalOperator *CEXPR = |
| 4347 | dyn_cast<ConditionalOperator>(BlockExp)) { |
| 4348 | Expr *LHSExp = CEXPR->getLHS(); |
| 4349 | Stmt *LHSStmt = SynthesizeBlockCall(Exp, LHSExp); |
| 4350 | Expr *RHSExp = CEXPR->getRHS(); |
| 4351 | Stmt *RHSStmt = SynthesizeBlockCall(Exp, RHSExp); |
| 4352 | Expr *CONDExp = CEXPR->getCond(); |
| 4353 | ConditionalOperator *CondExpr = |
| 4354 | new (Context) ConditionalOperator(CONDExp, |
| 4355 | SourceLocation(), cast<Expr>(LHSStmt), |
| 4356 | SourceLocation(), cast<Expr>(RHSStmt), |
| 4357 | Exp->getType()); |
| 4358 | return CondExpr; |
Fariborz Jahanian | e24b22b | 2009-12-18 01:15:21 +0000 | [diff] [blame] | 4359 | } else if (const ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(BlockExp)) { |
| 4360 | CPT = IRE->getType()->getAs<BlockPointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4361 | } else { |
| 4362 | assert(1 && "RewriteBlockClass: Bad type"); |
| 4363 | } |
| 4364 | assert(CPT && "RewriteBlockClass: Bad type"); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4365 | const FunctionType *FT = CPT->getPointeeType()->getAs<FunctionType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4366 | assert(FT && "RewriteBlockClass: Bad type"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4367 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4368 | // FTP will be null for closures that don't take arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4369 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4370 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
| 4371 | SourceLocation(), |
| 4372 | &Context->Idents.get("__block_impl")); |
| 4373 | QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD)); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4374 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4375 | // Generate a funky cast. |
| 4376 | llvm::SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4377 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4378 | // Push the block argument type. |
| 4379 | ArgTypes.push_back(PtrBlock); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4380 | if (FTP) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4381 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4382 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 4383 | QualType t = *I; |
| 4384 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4385 | if (isTopLevelBlockPointerType(t)) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4386 | const BlockPointerType *BPT = t->getAs<BlockPointerType>(); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4387 | t = Context->getPointerType(BPT->getPointeeType()); |
| 4388 | } |
| 4389 | ArgTypes.push_back(t); |
| 4390 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4391 | } |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4392 | // Now do the pointer to function cast. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4393 | QualType PtrToFuncCastType = Context->getFunctionType(Exp->getType(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 4394 | &ArgTypes[0], ArgTypes.size(), false/*no variadic*/, 0, |
| 4395 | false, false, 0, 0, |
| 4396 | false, CC_Default); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4397 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4398 | PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4399 | |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4400 | CastExpr *BlkCast = NoTypeInfoCStyleCastExpr(Context, PtrBlock, |
| 4401 | CastExpr::CK_Unknown, |
| 4402 | const_cast<Expr*>(BlockExp)); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4403 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4404 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 4405 | BlkCast); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4406 | //PE->dump(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4407 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4408 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4409 | &Context->Idents.get("FuncPtr"), Context->VoidPtrTy, 0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 4410 | /*BitWidth=*/0, /*Mutable=*/true); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4411 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
| 4412 | FD->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4413 | |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4414 | CastExpr *FunkCast = NoTypeInfoCStyleCastExpr(Context, PtrToFuncCastType, |
| 4415 | CastExpr::CK_Unknown, ME); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4416 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4417 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4418 | llvm::SmallVector<Expr*, 8> BlkExprs; |
| 4419 | // Add the implicit argument. |
| 4420 | BlkExprs.push_back(BlkCast); |
| 4421 | // Add the user arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4422 | for (CallExpr::arg_iterator I = Exp->arg_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4423 | E = Exp->arg_end(); I != E; ++I) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4424 | BlkExprs.push_back(*I); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4425 | } |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 4426 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &BlkExprs[0], |
| 4427 | BlkExprs.size(), |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4428 | Exp->getType(), SourceLocation()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4429 | return CE; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4430 | } |
| 4431 | |
| 4432 | void RewriteObjC::RewriteBlockCall(CallExpr *Exp) { |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4433 | Stmt *BlockCall = SynthesizeBlockCall(Exp, Exp->getCallee()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4434 | ReplaceStmt(Exp, BlockCall); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4435 | } |
| 4436 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4437 | // We need to return the rewritten expression to handle cases where the |
| 4438 | // BlockDeclRefExpr is embedded in another expression being rewritten. |
| 4439 | // For example: |
| 4440 | // |
| 4441 | // int main() { |
| 4442 | // __block Foo *f; |
| 4443 | // __block int i; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4444 | // |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4445 | // void (^myblock)() = ^() { |
| 4446 | // [f test]; // f is a BlockDeclRefExpr embedded in a message (which is being rewritten). |
| 4447 | // i = 77; |
| 4448 | // }; |
| 4449 | //} |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4450 | Stmt *RewriteObjC::RewriteBlockDeclRefExpr(Expr *DeclRefExp) { |
Fariborz Jahanian | bbf37e2 | 2009-12-23 19:26:34 +0000 | [diff] [blame] | 4451 | // Rewrite the byref variable into BYREFVAR->__forwarding->BYREFVAR |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4452 | // for each DeclRefExp where BYREFVAR is name of the variable. |
| 4453 | ValueDecl *VD; |
| 4454 | bool isArrow = true; |
| 4455 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(DeclRefExp)) |
| 4456 | VD = BDRE->getDecl(); |
| 4457 | else { |
| 4458 | VD = cast<DeclRefExpr>(DeclRefExp)->getDecl(); |
| 4459 | isArrow = false; |
| 4460 | } |
| 4461 | |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4462 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 4463 | &Context->Idents.get("__forwarding"), |
| 4464 | Context->VoidPtrTy, 0, |
| 4465 | /*BitWidth=*/0, /*Mutable=*/true); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4466 | MemberExpr *ME = new (Context) MemberExpr(DeclRefExp, isArrow, |
| 4467 | FD, SourceLocation(), |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4468 | FD->getType()); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4469 | |
| 4470 | const char *Name = VD->getNameAsCString(); |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4471 | FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 4472 | &Context->Idents.get(Name), |
| 4473 | Context->VoidPtrTy, 0, |
| 4474 | /*BitWidth=*/0, /*Mutable=*/true); |
| 4475 | ME = new (Context) MemberExpr(ME, true, FD, SourceLocation(), |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4476 | DeclRefExp->getType()); |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4477 | |
| 4478 | |
| 4479 | |
Steve Naroff | df8570d | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 4480 | // Need parens to enforce precedence. |
Fariborz Jahanian | ec878f2 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4481 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 4482 | ME); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4483 | ReplaceStmt(DeclRefExp, PE); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4484 | return PE; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4485 | } |
| 4486 | |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4487 | void RewriteObjC::RewriteCastExpr(CStyleCastExpr *CE) { |
| 4488 | SourceLocation LocStart = CE->getLParenLoc(); |
| 4489 | SourceLocation LocEnd = CE->getRParenLoc(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4490 | |
| 4491 | // Need to avoid trying to rewrite synthesized casts. |
| 4492 | if (LocStart.isInvalid()) |
| 4493 | return; |
Steve Naroff | 8f6ce57 | 2008-11-03 11:20:24 +0000 | [diff] [blame] | 4494 | // Need to avoid trying to rewrite casts contained in macros. |
| 4495 | if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) |
| 4496 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4497 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4498 | const char *startBuf = SM->getCharacterData(LocStart); |
| 4499 | const char *endBuf = SM->getCharacterData(LocEnd); |
Fariborz Jahanian | 1d4fca2 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4500 | QualType QT = CE->getType(); |
| 4501 | const Type* TypePtr = QT->getAs<Type>(); |
| 4502 | if (isa<TypeOfExprType>(TypePtr)) { |
| 4503 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 4504 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 4505 | std::string TypeAsString = "("; |
Fariborz Jahanian | afad76f | 2010-02-18 01:20:22 +0000 | [diff] [blame] | 4506 | RewriteBlockPointerType(TypeAsString, QT); |
Fariborz Jahanian | 1d4fca2 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4507 | TypeAsString += ")"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4508 | ReplaceText(LocStart, endBuf-startBuf+1, TypeAsString); |
Fariborz Jahanian | 1d4fca2 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4509 | return; |
| 4510 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4511 | // advance the location to startArgList. |
| 4512 | const char *argPtr = startBuf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4513 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4514 | while (*argPtr++ && (argPtr < endBuf)) { |
| 4515 | switch (*argPtr) { |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4516 | case '^': |
| 4517 | // Replace the '^' with '*'. |
| 4518 | LocStart = LocStart.getFileLocWithOffset(argPtr-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4519 | ReplaceText(LocStart, 1, "*"); |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4520 | break; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4521 | } |
| 4522 | } |
| 4523 | return; |
| 4524 | } |
| 4525 | |
| 4526 | void RewriteObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { |
| 4527 | SourceLocation DeclLoc = FD->getLocation(); |
| 4528 | unsigned parenCount = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4529 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4530 | // We have 1 or more arguments that have closure pointers. |
| 4531 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4532 | const char *startArgList = strchr(startBuf, '('); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4533 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4534 | assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4535 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4536 | parenCount++; |
| 4537 | // advance the location to startArgList. |
| 4538 | DeclLoc = DeclLoc.getFileLocWithOffset(startArgList-startBuf); |
| 4539 | assert((DeclLoc.isValid()) && "Invalid DeclLoc"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4540 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4541 | const char *argPtr = startArgList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4542 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4543 | while (*argPtr++ && parenCount) { |
| 4544 | switch (*argPtr) { |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4545 | case '^': |
| 4546 | // Replace the '^' with '*'. |
| 4547 | DeclLoc = DeclLoc.getFileLocWithOffset(argPtr-startArgList); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4548 | ReplaceText(DeclLoc, 1, "*"); |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4549 | break; |
| 4550 | case '(': |
| 4551 | parenCount++; |
| 4552 | break; |
| 4553 | case ')': |
| 4554 | parenCount--; |
| 4555 | break; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4556 | } |
| 4557 | } |
| 4558 | return; |
| 4559 | } |
| 4560 | |
| 4561 | bool RewriteObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4562 | const FunctionProtoType *FTP; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4563 | const PointerType *PT = QT->getAs<PointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4564 | if (PT) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4565 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4566 | } else { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4567 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4568 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4569 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4570 | } |
| 4571 | if (FTP) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4572 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4573 | E = FTP->arg_type_end(); I != E; ++I) |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4574 | if (isTopLevelBlockPointerType(*I)) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4575 | return true; |
| 4576 | } |
| 4577 | return false; |
| 4578 | } |
| 4579 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4580 | void RewriteObjC::GetExtentOfArgList(const char *Name, const char *&LParen, |
| 4581 | const char *&RParen) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4582 | const char *argPtr = strchr(Name, '('); |
| 4583 | assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4584 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4585 | LParen = argPtr; // output the start. |
| 4586 | argPtr++; // skip past the left paren. |
| 4587 | unsigned parenCount = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4588 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4589 | while (*argPtr && parenCount) { |
| 4590 | switch (*argPtr) { |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4591 | case '(': parenCount++; break; |
| 4592 | case ')': parenCount--; break; |
| 4593 | default: break; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4594 | } |
| 4595 | if (parenCount) argPtr++; |
| 4596 | } |
| 4597 | assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); |
| 4598 | RParen = argPtr; // output the end |
| 4599 | } |
| 4600 | |
| 4601 | void RewriteObjC::RewriteBlockPointerDecl(NamedDecl *ND) { |
| 4602 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 4603 | RewriteBlockPointerFunctionArgs(FD); |
| 4604 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4605 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4606 | // Handle Variables and Typedefs. |
| 4607 | SourceLocation DeclLoc = ND->getLocation(); |
| 4608 | QualType DeclT; |
| 4609 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4610 | DeclT = VD->getType(); |
| 4611 | else if (TypedefDecl *TDD = dyn_cast<TypedefDecl>(ND)) |
| 4612 | DeclT = TDD->getUnderlyingType(); |
| 4613 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) |
| 4614 | DeclT = FD->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4615 | else |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4616 | assert(0 && "RewriteBlockPointerDecl(): Decl type not yet handled"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4617 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4618 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4619 | const char *endBuf = startBuf; |
| 4620 | // scan backward (from the decl location) for the end of the previous decl. |
| 4621 | while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) |
| 4622 | startBuf--; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4623 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4624 | // *startBuf != '^' if we are dealing with a pointer to function that |
| 4625 | // may take block argument types (which will be handled below). |
| 4626 | if (*startBuf == '^') { |
| 4627 | // Replace the '^' with '*', computing a negative offset. |
| 4628 | DeclLoc = DeclLoc.getFileLocWithOffset(startBuf-endBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4629 | ReplaceText(DeclLoc, 1, "*"); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4630 | } |
| 4631 | if (PointerTypeTakesAnyBlockArguments(DeclT)) { |
| 4632 | // Replace the '^' with '*' for arguments. |
| 4633 | DeclLoc = ND->getLocation(); |
| 4634 | startBuf = SM->getCharacterData(DeclLoc); |
| 4635 | const char *argListBegin, *argListEnd; |
| 4636 | GetExtentOfArgList(startBuf, argListBegin, argListEnd); |
| 4637 | while (argListBegin < argListEnd) { |
| 4638 | if (*argListBegin == '^') { |
| 4639 | SourceLocation CaretLoc = DeclLoc.getFileLocWithOffset(argListBegin-startBuf); |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4640 | ReplaceText(CaretLoc, 1, "*"); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4641 | } |
| 4642 | argListBegin++; |
| 4643 | } |
| 4644 | } |
| 4645 | return; |
| 4646 | } |
| 4647 | |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4648 | |
| 4649 | /// SynthesizeByrefCopyDestroyHelper - This routine synthesizes: |
| 4650 | /// void __Block_byref_id_object_copy(struct Block_byref_id_object *dst, |
| 4651 | /// struct Block_byref_id_object *src) { |
| 4652 | /// _Block_object_assign (&_dest->object, _src->object, |
| 4653 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4654 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4655 | /// _Block_object_assign(&_dest->object, _src->object, |
| 4656 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4657 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4658 | /// } |
| 4659 | /// And: |
| 4660 | /// void __Block_byref_id_object_dispose(struct Block_byref_id_object *_src) { |
| 4661 | /// _Block_object_dispose(_src->object, |
| 4662 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4663 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4664 | /// _Block_object_dispose(_src->object, |
| 4665 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4666 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4667 | /// } |
| 4668 | |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4669 | std::string RewriteObjC::SynthesizeByrefCopyDestroyHelper(VarDecl *VD, |
| 4670 | int flag) { |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4671 | std::string S; |
Benjamin Kramer | 1211a71 | 2010-01-10 19:57:50 +0000 | [diff] [blame] | 4672 | if (CopyDestroyCache.count(flag)) |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4673 | return S; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4674 | CopyDestroyCache.insert(flag); |
| 4675 | S = "static void __Block_byref_id_object_copy_"; |
| 4676 | S += utostr(flag); |
| 4677 | S += "(void *dst, void *src) {\n"; |
| 4678 | |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4679 | // offset into the object pointer is computed as: |
| 4680 | // void * + void* + int + int + void* + void * |
| 4681 | unsigned IntSize = |
| 4682 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 4683 | unsigned VoidPtrSize = |
| 4684 | static_cast<unsigned>(Context->getTypeSize(Context->VoidPtrTy)); |
| 4685 | |
| 4686 | unsigned offset = (VoidPtrSize*4 + IntSize + IntSize)/8; |
| 4687 | S += " _Block_object_assign((char*)dst + "; |
| 4688 | S += utostr(offset); |
| 4689 | S += ", *(void * *) ((char*)src + "; |
| 4690 | S += utostr(offset); |
| 4691 | S += "), "; |
| 4692 | S += utostr(flag); |
| 4693 | S += ");\n}\n"; |
| 4694 | |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4695 | S += "static void __Block_byref_id_object_dispose_"; |
| 4696 | S += utostr(flag); |
| 4697 | S += "(void *src) {\n"; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4698 | S += " _Block_object_dispose(*(void * *) ((char*)src + "; |
| 4699 | S += utostr(offset); |
| 4700 | S += "), "; |
| 4701 | S += utostr(flag); |
| 4702 | S += ");\n}\n"; |
| 4703 | return S; |
| 4704 | } |
| 4705 | |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4706 | /// RewriteByRefVar - For each __block typex ND variable this routine transforms |
| 4707 | /// the declaration into: |
| 4708 | /// struct __Block_byref_ND { |
| 4709 | /// void *__isa; // NULL for everything except __weak pointers |
| 4710 | /// struct __Block_byref_ND *__forwarding; |
| 4711 | /// int32_t __flags; |
| 4712 | /// int32_t __size; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4713 | /// void *__Block_byref_id_object_copy; // If variable is __block ObjC object |
| 4714 | /// void *__Block_byref_id_object_dispose; // If variable is __block ObjC object |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4715 | /// typex ND; |
| 4716 | /// }; |
| 4717 | /// |
| 4718 | /// It then replaces declaration of ND variable with: |
| 4719 | /// struct __Block_byref_ND ND = {__isa=0B, __forwarding=&ND, __flags=some_flag, |
| 4720 | /// __size=sizeof(struct __Block_byref_ND), |
| 4721 | /// ND=initializer-if-any}; |
| 4722 | /// |
| 4723 | /// |
| 4724 | void RewriteObjC::RewriteByRefVar(VarDecl *ND) { |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 4725 | // Insert declaration for the function in which block literal is |
| 4726 | // used. |
| 4727 | if (CurFunctionDeclToDeclareForBlock) |
| 4728 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4729 | int flag = 0; |
| 4730 | int isa = 0; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4731 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
| 4732 | const char *startBuf = SM->getCharacterData(DeclLoc); |
Fariborz Jahanian | 6f0a0a9 | 2009-12-30 20:38:08 +0000 | [diff] [blame] | 4733 | SourceLocation X = ND->getLocEnd(); |
| 4734 | X = SM->getInstantiationLoc(X); |
| 4735 | const char *endBuf = SM->getCharacterData(X); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4736 | std::string Name(ND->getNameAsString()); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4737 | std::string ByrefType; |
| 4738 | RewriteByRefString(ByrefType, Name, ND); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4739 | ByrefType += " {\n"; |
| 4740 | ByrefType += " void *__isa;\n"; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4741 | RewriteByRefString(ByrefType, Name, ND); |
| 4742 | ByrefType += " *__forwarding;\n"; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4743 | ByrefType += " int __flags;\n"; |
| 4744 | ByrefType += " int __size;\n"; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4745 | // Add void *__Block_byref_id_object_copy; |
| 4746 | // void *__Block_byref_id_object_dispose; if needed. |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4747 | QualType Ty = ND->getType(); |
| 4748 | bool HasCopyAndDispose = Context->BlockRequiresCopying(Ty); |
| 4749 | if (HasCopyAndDispose) { |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4750 | ByrefType += " void (*__Block_byref_id_object_copy)(void*, void*);\n"; |
| 4751 | ByrefType += " void (*__Block_byref_id_object_dispose)(void*);\n"; |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4752 | } |
| 4753 | |
| 4754 | Ty.getAsStringInternal(Name, Context->PrintingPolicy); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4755 | ByrefType += " " + Name + ";\n"; |
| 4756 | ByrefType += "};\n"; |
| 4757 | // Insert this type in global scope. It is needed by helper function. |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4758 | SourceLocation FunLocStart; |
| 4759 | if (CurFunctionDef) |
| 4760 | FunLocStart = CurFunctionDef->getTypeSpecStartLoc(); |
| 4761 | else { |
| 4762 | assert(CurMethodDef && "RewriteByRefVar - CurMethodDef is null"); |
| 4763 | FunLocStart = CurMethodDef->getLocStart(); |
| 4764 | } |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4765 | InsertText(FunLocStart, ByrefType); |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4766 | if (Ty.isObjCGCWeak()) { |
| 4767 | flag |= BLOCK_FIELD_IS_WEAK; |
| 4768 | isa = 1; |
| 4769 | } |
| 4770 | |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4771 | if (HasCopyAndDispose) { |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4772 | flag = BLOCK_BYREF_CALLER; |
| 4773 | QualType Ty = ND->getType(); |
| 4774 | // FIXME. Handle __weak variable (BLOCK_FIELD_IS_WEAK) as well. |
| 4775 | if (Ty->isBlockPointerType()) |
| 4776 | flag |= BLOCK_FIELD_IS_BLOCK; |
| 4777 | else |
| 4778 | flag |= BLOCK_FIELD_IS_OBJECT; |
| 4779 | std::string HF = SynthesizeByrefCopyDestroyHelper(ND, flag); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4780 | if (!HF.empty()) |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4781 | InsertText(FunLocStart, HF); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4782 | } |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4783 | |
| 4784 | // struct __Block_byref_ND ND = |
| 4785 | // {0, &ND, some_flag, __size=sizeof(struct __Block_byref_ND), |
| 4786 | // initializer-if-any}; |
| 4787 | bool hasInit = (ND->getInit() != 0); |
Fariborz Jahanian | e1f84f8 | 2010-01-05 18:15:57 +0000 | [diff] [blame] | 4788 | unsigned flags = 0; |
| 4789 | if (HasCopyAndDispose) |
| 4790 | flags |= BLOCK_HAS_COPY_DISPOSE; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4791 | Name = ND->getNameAsString(); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4792 | ByrefType.clear(); |
| 4793 | RewriteByRefString(ByrefType, Name, ND); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4794 | std::string ForwardingCastType("("); |
| 4795 | ForwardingCastType += ByrefType + " *)"; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4796 | if (!hasInit) { |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4797 | ByrefType += " " + Name + " = {(void*)"; |
| 4798 | ByrefType += utostr(isa); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4799 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4800 | ByrefType += utostr(flags); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4801 | ByrefType += ", "; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4802 | ByrefType += "sizeof("; |
| 4803 | RewriteByRefString(ByrefType, Name, ND); |
| 4804 | ByrefType += ")"; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4805 | if (HasCopyAndDispose) { |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4806 | ByrefType += ", __Block_byref_id_object_copy_"; |
| 4807 | ByrefType += utostr(flag); |
| 4808 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 4809 | ByrefType += utostr(flag); |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4810 | } |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4811 | ByrefType += "};\n"; |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4812 | ReplaceText(DeclLoc, endBuf-startBuf+Name.size(), ByrefType); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4813 | } |
| 4814 | else { |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4815 | SourceLocation startLoc; |
| 4816 | Expr *E = ND->getInit(); |
| 4817 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 4818 | startLoc = ECE->getLParenLoc(); |
| 4819 | else |
| 4820 | startLoc = E->getLocStart(); |
Fariborz Jahanian | 791b10d | 2010-01-05 23:06:29 +0000 | [diff] [blame] | 4821 | startLoc = SM->getInstantiationLoc(startLoc); |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4822 | endBuf = SM->getCharacterData(startLoc); |
| 4823 | |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4824 | ByrefType += " " + Name; |
Fariborz Jahanian | dfa4fa0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4825 | ByrefType += " = {(void*)"; |
Fariborz Jahanian | 2086d54 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4826 | ByrefType += utostr(isa); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4827 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4828 | ByrefType += utostr(flags); |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4829 | ByrefType += ", "; |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4830 | ByrefType += "sizeof("; |
| 4831 | RewriteByRefString(ByrefType, Name, ND); |
| 4832 | ByrefType += "), "; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4833 | if (HasCopyAndDispose) { |
Fariborz Jahanian | ab10b2e | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4834 | ByrefType += "__Block_byref_id_object_copy_"; |
| 4835 | ByrefType += utostr(flag); |
| 4836 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 4837 | ByrefType += utostr(flag); |
| 4838 | ByrefType += ", "; |
Fariborz Jahanian | d2eb1fd | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4839 | } |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4840 | ReplaceText(DeclLoc, endBuf-startBuf, ByrefType); |
Steve Naroff | c5143c5 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 4841 | |
| 4842 | // Complete the newly synthesized compound expression by inserting a right |
| 4843 | // curly brace before the end of the declaration. |
| 4844 | // FIXME: This approach avoids rewriting the initializer expression. It |
| 4845 | // also assumes there is only one declarator. For example, the following |
| 4846 | // isn't currently supported by this routine (in general): |
| 4847 | // |
| 4848 | // double __block BYREFVAR = 1.34, BYREFVAR2 = 1.37; |
| 4849 | // |
| 4850 | const char *startBuf = SM->getCharacterData(startLoc); |
| 4851 | const char *semiBuf = strchr(startBuf, ';'); |
| 4852 | assert((*semiBuf == ';') && "RewriteByRefVar: can't find ';'"); |
| 4853 | SourceLocation semiLoc = |
| 4854 | startLoc.getFileLocWithOffset(semiBuf-startBuf); |
| 4855 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4856 | InsertText(semiLoc, "}"); |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4857 | } |
Fariborz Jahanian | 1be6b46 | 2009-12-22 00:48:54 +0000 | [diff] [blame] | 4858 | return; |
| 4859 | } |
| 4860 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4861 | void RewriteObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4862 | // Add initializers for any closure decl refs. |
| 4863 | GetBlockDeclRefExprs(Exp->getBody()); |
| 4864 | if (BlockDeclRefs.size()) { |
| 4865 | // Unique all "by copy" declarations. |
| 4866 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4867 | if (!BlockDeclRefs[i]->isByRef()) { |
| 4868 | if (!BlockByCopyDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 4869 | BlockByCopyDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 4870 | BlockByCopyDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 4871 | } |
| 4872 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4873 | // Unique all "by ref" declarations. |
| 4874 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 4875 | if (BlockDeclRefs[i]->isByRef()) { |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4876 | if (!BlockByRefDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 4877 | BlockByRefDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 4878 | BlockByRefDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 4879 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4880 | } |
| 4881 | // Find any imported blocks...they will need special attention. |
| 4882 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
Fariborz Jahanian | 4fcc4fd | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 4883 | if (BlockDeclRefs[i]->isByRef() || |
| 4884 | BlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
| 4885 | BlockDeclRefs[i]->getType()->isBlockPointerType()) { |
Steve Naroff | 0007268 | 2008-11-13 17:40:07 +0000 | [diff] [blame] | 4886 | GetBlockCallExprs(BlockDeclRefs[i]); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4887 | ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 4888 | } |
| 4889 | } |
| 4890 | } |
| 4891 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4892 | FunctionDecl *RewriteObjC::SynthBlockInitFunctionDecl(const char *name) { |
| 4893 | IdentifierInfo *ID = &Context->Idents.get(name); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4894 | QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4895 | return FunctionDecl::Create(*Context, TUDecl,SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 4896 | ID, FType, 0, FunctionDecl::Extern, false, |
Douglas Gregor | 2224f84 | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 4897 | false); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4898 | } |
| 4899 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4900 | Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp, |
| 4901 | const llvm::SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4902 | Blocks.push_back(Exp); |
| 4903 | |
| 4904 | CollectBlockDeclRefInfo(Exp); |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4905 | |
| 4906 | // Add inner imported variables now used in current block. |
| 4907 | int countOfInnerDecls = 0; |
| 4908 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) { |
| 4909 | BlockDeclRefExpr *Exp = InnerBlockDeclRefs[i]; |
| 4910 | ValueDecl *VD = Exp->getDecl(); |
| 4911 | if (!BlockByCopyDeclsPtrSet.count(VD)) { |
| 4912 | // We need to save the copied-in variables in nested |
| 4913 | // blocks because it is needed at the end for some of the API generations. |
| 4914 | // See SynthesizeBlockLiterals routine. |
| 4915 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 4916 | BlockDeclRefs.push_back(Exp); |
| 4917 | BlockByCopyDeclsPtrSet.insert(VD); |
| 4918 | BlockByCopyDecls.push_back(VD); |
| 4919 | if (Exp->getType()->isObjCObjectPointerType() || |
| 4920 | Exp->getType()->isBlockPointerType()) { |
| 4921 | GetBlockCallExprs(Exp); |
| 4922 | ImportedBlockDecls.insert(VD); |
| 4923 | } |
| 4924 | } |
| 4925 | } |
| 4926 | InnerDeclRefsCount.push_back(countOfInnerDecls); |
| 4927 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4928 | std::string FuncName; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4929 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4930 | if (CurFunctionDef) |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4931 | FuncName = CurFunctionDef->getNameAsString(); |
Fariborz Jahanian | e61a1d4 | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 4932 | else if (CurMethodDef) |
| 4933 | BuildUniqueMethodName(FuncName, CurMethodDef); |
| 4934 | else if (GlobalVarDecl) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4935 | FuncName = std::string(GlobalVarDecl->getNameAsString()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4936 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4937 | std::string BlockNumber = utostr(Blocks.size()-1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4938 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4939 | std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber; |
| 4940 | std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4941 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4942 | // Get a pointer to the function type so we can cast appropriately. |
| 4943 | QualType FType = Context->getPointerType(QualType(Exp->getFunctionType(),0)); |
| 4944 | |
| 4945 | FunctionDecl *FD; |
| 4946 | Expr *NewRep; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4947 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4948 | // Simulate a contructor call... |
| 4949 | FD = SynthBlockInitFunctionDecl(Tag.c_str()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4950 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, FType, SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4951 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4952 | llvm::SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4953 | |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4954 | // Initialize the block function. |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4955 | FD = SynthBlockInitFunctionDecl(Func.c_str()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4956 | DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, FD->getType(), |
| 4957 | SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4958 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
| 4959 | CastExpr::CK_Unknown, Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4960 | InitExprs.push_back(castExpr); |
| 4961 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4962 | // Initialize the block descriptor. |
| 4963 | std::string DescData = "__" + FuncName + "_block_desc_" + BlockNumber + "_DATA"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4964 | |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4965 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
| 4966 | &Context->Idents.get(DescData.c_str()), |
| 4967 | Context->VoidPtrTy, 0, |
| 4968 | VarDecl::Static); |
| 4969 | UnaryOperator *DescRefExpr = new (Context) UnaryOperator( |
| 4970 | new (Context) DeclRefExpr(NewVD, |
| 4971 | Context->VoidPtrTy, SourceLocation()), |
| 4972 | UnaryOperator::AddrOf, |
| 4973 | Context->getPointerType(Context->VoidPtrTy), |
| 4974 | SourceLocation()); |
| 4975 | InitExprs.push_back(DescRefExpr); |
| 4976 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4977 | // Add initializers for any closure decl refs. |
| 4978 | if (BlockDeclRefs.size()) { |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4979 | Expr *Exp; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4980 | // Output all "by copy" declarations. |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4981 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4982 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4983 | if (isObjCType((*I)->getType())) { |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4984 | // FIXME: Conform to ABI ([[obj retain] autorelease]). |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4985 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4986 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4987 | } else if (isTopLevelBlockPointerType((*I)->getType())) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4988 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4989 | Arg = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4990 | Exp = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
| 4991 | CastExpr::CK_Unknown, Arg); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4992 | } else { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4993 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4994 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4995 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4996 | InitExprs.push_back(Exp); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4997 | } |
| 4998 | // Output all "by ref" declarations. |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4999 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5000 | E = BlockByRefDecls.end(); I != E; ++I) { |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5001 | ValueDecl *ND = (*I); |
| 5002 | std::string Name(ND->getNameAsString()); |
| 5003 | std::string RecName; |
| 5004 | RewriteByRefString(RecName, Name, ND); |
| 5005 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str() |
| 5006 | + sizeof("struct")); |
| 5007 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
| 5008 | SourceLocation(), II); |
| 5009 | assert(RD && "SynthBlockInitExpr(): Can't find RecordDecl"); |
| 5010 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 5011 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 5012 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5013 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 5014 | Exp = new (Context) UnaryOperator(Exp, UnaryOperator::AddrOf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5015 | Context->getPointerType(Exp->getType()), |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 5016 | SourceLocation()); |
Fariborz Jahanian | 2663f52 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5017 | Exp = NoTypeInfoCStyleCastExpr(Context, castT, CastExpr::CK_Unknown, Exp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5018 | InitExprs.push_back(Exp); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5019 | } |
| 5020 | } |
Fariborz Jahanian | ff12788 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 5021 | if (ImportedBlockDecls.size()) { |
| 5022 | // generate BLOCK_HAS_COPY_DISPOSE(have helper funcs) | BLOCK_HAS_DESCRIPTOR |
| 5023 | int flag = (BLOCK_HAS_COPY_DISPOSE | BLOCK_HAS_DESCRIPTOR); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5024 | unsigned IntSize = |
| 5025 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Fariborz Jahanian | ff12788 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 5026 | Expr *FlagExp = new (Context) IntegerLiteral(llvm::APInt(IntSize, flag), |
| 5027 | Context->IntTy, SourceLocation()); |
| 5028 | InitExprs.push_back(FlagExp); |
Steve Naroff | 01aec11 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5029 | } |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 5030 | NewRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], InitExprs.size(), |
| 5031 | FType, SourceLocation()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5032 | NewRep = new (Context) UnaryOperator(NewRep, UnaryOperator::AddrOf, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5033 | Context->getPointerType(NewRep->getType()), |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5034 | SourceLocation()); |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 5035 | NewRep = NoTypeInfoCStyleCastExpr(Context, FType, CastExpr::CK_Unknown, |
| 5036 | NewRep); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5037 | BlockDeclRefs.clear(); |
| 5038 | BlockByRefDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5039 | BlockByRefDeclsPtrSet.clear(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5040 | BlockByCopyDecls.clear(); |
Fariborz Jahanian | bab7168 | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5041 | BlockByCopyDeclsPtrSet.clear(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5042 | ImportedBlockDecls.clear(); |
| 5043 | return NewRep; |
| 5044 | } |
| 5045 | |
| 5046 | //===----------------------------------------------------------------------===// |
| 5047 | // Function Body / Expression rewriting |
| 5048 | //===----------------------------------------------------------------------===// |
| 5049 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5050 | // This is run as a first "pass" prior to RewriteFunctionBodyOrGlobalInitializer(). |
| 5051 | // The allows the main rewrite loop to associate all ObjCPropertyRefExprs with |
| 5052 | // their respective BinaryOperator. Without this knowledge, we'd need to rewrite |
| 5053 | // the ObjCPropertyRefExpr twice (once as a getter, and later as a setter). |
| 5054 | // Since the rewriter isn't capable of rewriting rewritten code, it's important |
| 5055 | // we get this right. |
| 5056 | void RewriteObjC::CollectPropertySetters(Stmt *S) { |
| 5057 | // Perform a bottom up traversal of all children. |
| 5058 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 5059 | CI != E; ++CI) |
| 5060 | if (*CI) |
| 5061 | CollectPropertySetters(*CI); |
| 5062 | |
| 5063 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) { |
| 5064 | if (BinOp->isAssignmentOp()) { |
| 5065 | if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS())) |
| 5066 | PropSetters[PRE] = BinOp; |
| 5067 | } |
| 5068 | } |
| 5069 | } |
| 5070 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5071 | Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 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 | Stmts.push_back(S); |
| 5075 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 5076 | Stmts.push_back(S); |
Chris Lattner | 4824fcd | 2010-01-09 21:45:57 +0000 | [diff] [blame] | 5077 | ObjCBcLabelNo.push_back(++BcLabelCount); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5078 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5079 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5080 | SourceRange OrigStmtRange = S->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5081 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5082 | // Perform a bottom up rewrite of all children. |
| 5083 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 5084 | CI != E; ++CI) |
| 5085 | if (*CI) { |
Fariborz Jahanian | 2b9b0b2 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 5086 | Stmt *newStmt; |
| 5087 | Stmt *S = (*CI); |
| 5088 | if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) { |
| 5089 | Expr *OldBase = IvarRefExpr->getBase(); |
| 5090 | bool replaced = false; |
| 5091 | newStmt = RewriteObjCNestedIvarRefExpr(S, replaced); |
| 5092 | if (replaced) { |
| 5093 | if (ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(newStmt)) |
| 5094 | ReplaceStmt(OldBase, IRE->getBase()); |
| 5095 | else |
| 5096 | ReplaceStmt(S, newStmt); |
| 5097 | } |
| 5098 | } |
| 5099 | else |
| 5100 | newStmt = RewriteFunctionBodyOrGlobalInitializer(S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5101 | if (newStmt) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5102 | *CI = newStmt; |
| 5103 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5104 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5105 | if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5106 | llvm::SmallPtrSet<ValueDecl *, 8> InnerBlockValueDecls; |
| 5107 | llvm::SmallVector<BlockDeclRefExpr *, 8> InnerBlockDeclRefs; |
| 5108 | GetInnerBlockDeclRefExprs(BE->getBody(), |
| 5109 | InnerBlockDeclRefs, InnerBlockValueDecls); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5110 | // Rewrite the block body in place. |
| 5111 | RewriteFunctionBodyOrGlobalInitializer(BE->getBody()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5112 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5113 | // 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] | 5114 | std::string Str = Rewrite.getRewrittenText(BE->getSourceRange()); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5115 | RewrittenBlockExprs[BE] = Str; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5116 | |
Fariborz Jahanian | 5e49b2f | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5117 | Stmt *blockTranscribed = SynthBlockInitExpr(BE, InnerBlockDeclRefs); |
| 5118 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5119 | //blockTranscribed->dump(); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5120 | ReplaceStmt(S, blockTranscribed); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5121 | return blockTranscribed; |
| 5122 | } |
| 5123 | // Handle specific things. |
| 5124 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 5125 | return RewriteAtEncode(AtEncode); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5126 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5127 | if (ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(S)) { |
| 5128 | BinaryOperator *BinOp = PropSetters[PropRefExpr]; |
| 5129 | if (BinOp) { |
| 5130 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 5131 | // 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] | 5132 | DisableReplaceStmt = true; |
| 5133 | // Save the source range. Even if we disable the replacement, the |
| 5134 | // rewritten node will have been inserted into the tree. If the synthesized |
| 5135 | // node is at the 'end', the rewriter will fail. Consider this: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5136 | // self.errorHandler = handler ? handler : |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 5137 | // ^(NSURL *errorURL, NSError *error) { return (BOOL)1; }; |
| 5138 | SourceRange SrcRange = BinOp->getSourceRange(); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5139 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(BinOp->getRHS()); |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 5140 | DisableReplaceStmt = false; |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 5141 | // |
| 5142 | // Unlike the main iterator, we explicily avoid changing 'BinOp'. If |
| 5143 | // we changed the RHS of BinOp, the rewriter would fail (since it needs |
| 5144 | // to see the original expression). Consider this example: |
| 5145 | // |
| 5146 | // Foo *obj1, *obj2; |
| 5147 | // |
| 5148 | // obj1.i = [obj2 rrrr]; |
| 5149 | // |
| 5150 | // 'BinOp' for the previous expression looks like: |
| 5151 | // |
| 5152 | // (BinaryOperator 0x231ccf0 'int' '=' |
| 5153 | // (ObjCPropertyRefExpr 0x231cc70 'int' Kind=PropertyRef Property="i" |
| 5154 | // (DeclRefExpr 0x231cc50 'Foo *' Var='obj1' 0x231cbb0)) |
| 5155 | // (ObjCMessageExpr 0x231ccb0 'int' selector=rrrr |
| 5156 | // (DeclRefExpr 0x231cc90 'Foo *' Var='obj2' 0x231cbe0))) |
| 5157 | // |
| 5158 | // 'newStmt' represents the rewritten message expression. For example: |
| 5159 | // |
| 5160 | // (CallExpr 0x231d300 'id':'struct objc_object *' |
| 5161 | // (ParenExpr 0x231d2e0 'int (*)(id, SEL)' |
| 5162 | // (CStyleCastExpr 0x231d2c0 'int (*)(id, SEL)' |
| 5163 | // (CStyleCastExpr 0x231d220 'void *' |
| 5164 | // (DeclRefExpr 0x231d200 'id (id, SEL, ...)' FunctionDecl='objc_msgSend' 0x231cdc0)))) |
| 5165 | // |
| 5166 | // Note that 'newStmt' is passed to RewritePropertySetter so that it |
| 5167 | // can be used as the setter argument. ReplaceStmt() will still 'see' |
| 5168 | // the original RHS (since we haven't altered BinOp). |
| 5169 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5170 | // This implies the Rewrite* routines can no longer delete the original |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 5171 | // node. As a result, we now leak the original AST nodes. |
| 5172 | // |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 5173 | return RewritePropertySetter(BinOp, dyn_cast<Expr>(newStmt), SrcRange); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5174 | } else { |
| 5175 | return RewritePropertyGetter(PropRefExpr); |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 5176 | } |
| 5177 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5178 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 5179 | return RewriteAtSelector(AtSelector); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5180 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5181 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 5182 | return RewriteObjCStringLiteral(AtString); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5183 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5184 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5185 | #if 0 |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5186 | // Before we rewrite it, put the original message expression in a comment. |
| 5187 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 5188 | SourceLocation endLoc = MessExpr->getLocEnd(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5189 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5190 | const char *startBuf = SM->getCharacterData(startLoc); |
| 5191 | const char *endBuf = SM->getCharacterData(endLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5192 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5193 | std::string messString; |
| 5194 | messString += "// "; |
| 5195 | messString.append(startBuf, endBuf-startBuf+1); |
| 5196 | messString += "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5197 | |
| 5198 | // FIXME: Missing definition of |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5199 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
| 5200 | // InsertText(startLoc, messString.c_str(), messString.size()); |
| 5201 | // Tried this, but it didn't work either... |
| 5202 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5203 | #endif |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5204 | return RewriteMessageExpr(MessExpr); |
| 5205 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5206 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5207 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 5208 | return RewriteObjCTryStmt(StmtTry); |
| 5209 | |
| 5210 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 5211 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 5212 | |
| 5213 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 5214 | return RewriteObjCThrowStmt(StmtThrow); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5215 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5216 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 5217 | return RewriteObjCProtocolExpr(ProtocolExp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5218 | |
| 5219 | if (ObjCForCollectionStmt *StmtForCollection = |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5220 | dyn_cast<ObjCForCollectionStmt>(S)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5221 | return RewriteObjCForCollectionStmt(StmtForCollection, |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5222 | OrigStmtRange.getEnd()); |
| 5223 | if (BreakStmt *StmtBreakStmt = |
| 5224 | dyn_cast<BreakStmt>(S)) |
| 5225 | return RewriteBreakStmt(StmtBreakStmt); |
| 5226 | if (ContinueStmt *StmtContinueStmt = |
| 5227 | dyn_cast<ContinueStmt>(S)) |
| 5228 | return RewriteContinueStmt(StmtContinueStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5229 | |
| 5230 | // 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] | 5231 | // and cast exprs. |
| 5232 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
| 5233 | // FIXME: What we're doing here is modifying the type-specifier that |
| 5234 | // precedes the first Decl. In the future the DeclGroup should have |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5235 | // a separate type-specifier that we can rewrite. |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5236 | // NOTE: We need to avoid rewriting the DeclStmt if it is within |
| 5237 | // the context of an ObjCForCollectionStmt. For example: |
| 5238 | // NSArray *someArray; |
| 5239 | // for (id <FooProtocol> index in someArray) ; |
| 5240 | // This is because RewriteObjCForCollectionStmt() does textual rewriting |
| 5241 | // and it depends on the original text locations/positions. |
Benjamin Kramer | b2041de | 2009-12-05 22:16:51 +0000 | [diff] [blame] | 5242 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5243 | RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5244 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5245 | // Blocks rewrite rules. |
| 5246 | for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); |
| 5247 | DI != DE; ++DI) { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 5248 | Decl *SD = *DI; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5249 | if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5250 | if (isTopLevelBlockPointerType(ND->getType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5251 | RewriteBlockPointerDecl(ND); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5252 | else if (ND->getType()->isFunctionPointerType()) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5253 | CheckFunctionPointerDecl(ND->getType(), ND); |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 5254 | if (VarDecl *VD = dyn_cast<VarDecl>(SD)) { |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5255 | if (VD->hasAttr<BlocksAttr>()) { |
| 5256 | static unsigned uniqueByrefDeclCount = 0; |
| 5257 | assert(!BlockByRefDeclNo.count(ND) && |
| 5258 | "RewriteFunctionBodyOrGlobalInitializer: Duplicate byref decl"); |
| 5259 | BlockByRefDeclNo[ND] = uniqueByrefDeclCount++; |
Fariborz Jahanian | 52b08f2 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5260 | RewriteByRefVar(VD); |
Fariborz Jahanian | a73165e | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5261 | } |
Fariborz Jahanian | 4c863ef | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 5262 | else |
| 5263 | RewriteTypeOfDecl(VD); |
| 5264 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5265 | } |
| 5266 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5267 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5268 | RewriteBlockPointerDecl(TD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5269 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5270 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 5271 | } |
| 5272 | } |
| 5273 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5274 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5275 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) |
| 5276 | RewriteObjCQualifiedInterfaceTypes(CE); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5277 | |
| 5278 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5279 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 5280 | assert(!Stmts.empty() && "Statement stack is empty"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5281 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 5282 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5283 | && "Statement stack mismatch"); |
| 5284 | Stmts.pop_back(); |
| 5285 | } |
| 5286 | // Handle blocks rewriting. |
| 5287 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
| 5288 | if (BDRE->isByRef()) |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5289 | return RewriteBlockDeclRefExpr(BDRE); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5290 | } |
Fariborz Jahanian | f381cc9 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5291 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 5292 | ValueDecl *VD = DRE->getDecl(); |
| 5293 | if (VD->hasAttr<BlocksAttr>()) |
| 5294 | return RewriteBlockDeclRefExpr(DRE); |
| 5295 | } |
| 5296 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5297 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 5298 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
Fariborz Jahanian | 8a9e170 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 5299 | Stmt *BlockCall = SynthesizeBlockCall(CE, CE->getCallee()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 5300 | ReplaceStmt(S, BlockCall); |
| 5301 | return BlockCall; |
| 5302 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5303 | } |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 5304 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5305 | RewriteCastExpr(CE); |
| 5306 | } |
| 5307 | #if 0 |
| 5308 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5309 | CastExpr *Replacement = new (Context) CastExpr(ICE->getType(), ICE->getSubExpr(), SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5310 | // Get the new text. |
| 5311 | std::string SStr; |
| 5312 | llvm::raw_string_ostream Buf(SStr); |
Eli Friedman | 3a9eb44 | 2009-05-30 05:19:26 +0000 | [diff] [blame] | 5313 | Replacement->printPretty(Buf, *Context); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5314 | const std::string &Str = Buf.str(); |
| 5315 | |
| 5316 | printf("CAST = %s\n", &Str[0]); |
| 5317 | InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 5318 | delete S; |
| 5319 | return Replacement; |
| 5320 | } |
| 5321 | #endif |
| 5322 | // Return this stmt unmodified. |
| 5323 | return S; |
| 5324 | } |
| 5325 | |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5326 | void RewriteObjC::RewriteRecordBody(RecordDecl *RD) { |
| 5327 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 5328 | e = RD->field_end(); i != e; ++i) { |
| 5329 | FieldDecl *FD = *i; |
| 5330 | if (isTopLevelBlockPointerType(FD->getType())) |
| 5331 | RewriteBlockPointerDecl(FD); |
| 5332 | if (FD->getType()->isObjCQualifiedIdType() || |
| 5333 | FD->getType()->isObjCQualifiedInterfaceType()) |
| 5334 | RewriteObjCQualifiedInterfaceTypes(FD); |
| 5335 | } |
| 5336 | } |
| 5337 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5338 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 5339 | /// main file of the input. |
| 5340 | void RewriteObjC::HandleDeclInMainFile(Decl *D) { |
| 5341 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | cb73530 | 2008-12-17 00:20:22 +0000 | [diff] [blame] | 5342 | if (FD->isOverloadedOperator()) |
| 5343 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5344 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5345 | // Since function prototypes don't have ParmDecl's, we check the function |
| 5346 | // prototype. This enables us to rewrite function declarations and |
| 5347 | // definitions using the same code. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5348 | RewriteBlocksInFunctionProtoType(FD->getType(), FD); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5349 | |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 5350 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 5351 | if (CompoundStmt *Body = FD->getCompoundBody()) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5352 | CurFunctionDef = FD; |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 5353 | CurFunctionDeclToDeclareForBlock = FD; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5354 | CollectPropertySetters(Body); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5355 | CurrentBody = Body; |
Ted Kremenek | eaab206 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 5356 | Body = |
| 5357 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 5358 | FD->setBody(Body); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5359 | CurrentBody = 0; |
| 5360 | if (PropParentMap) { |
| 5361 | delete PropParentMap; |
| 5362 | PropParentMap = 0; |
| 5363 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5364 | // This synthesizes and inserts the block "impl" struct, invoke function, |
| 5365 | // and any copy/dispose helper functions. |
| 5366 | InsertBlockLiteralsWithinFunction(FD); |
| 5367 | CurFunctionDef = 0; |
Fariborz Jahanian | abfd83e | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 5368 | CurFunctionDeclToDeclareForBlock = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5369 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5370 | return; |
| 5371 | } |
| 5372 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 5373 | if (CompoundStmt *Body = MD->getCompoundBody()) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5374 | CurMethodDef = MD; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5375 | CollectPropertySetters(Body); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5376 | CurrentBody = Body; |
Ted Kremenek | eaab206 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 5377 | Body = |
| 5378 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 5379 | MD->setBody(Body); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5380 | CurrentBody = 0; |
| 5381 | if (PropParentMap) { |
| 5382 | delete PropParentMap; |
| 5383 | PropParentMap = 0; |
| 5384 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5385 | InsertBlockLiteralsWithinMethod(MD); |
| 5386 | CurMethodDef = 0; |
| 5387 | } |
| 5388 | } |
| 5389 | if (ObjCImplementationDecl *CI = dyn_cast<ObjCImplementationDecl>(D)) |
| 5390 | ClassImplementation.push_back(CI); |
| 5391 | else if (ObjCCategoryImplDecl *CI = dyn_cast<ObjCCategoryImplDecl>(D)) |
| 5392 | CategoryImplementation.push_back(CI); |
| 5393 | else if (ObjCClassDecl *CD = dyn_cast<ObjCClassDecl>(D)) |
| 5394 | RewriteForwardClassDecl(CD); |
| 5395 | else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 5396 | RewriteObjCQualifiedInterfaceTypes(VD); |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5397 | if (isTopLevelBlockPointerType(VD->getType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5398 | RewriteBlockPointerDecl(VD); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5399 | else if (VD->getType()->isFunctionPointerType()) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5400 | CheckFunctionPointerDecl(VD->getType(), VD); |
| 5401 | if (VD->getInit()) { |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 5402 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5403 | RewriteCastExpr(CE); |
| 5404 | } |
| 5405 | } |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5406 | } else if (VD->getType()->isRecordType()) { |
| 5407 | RecordDecl *RD = VD->getType()->getAs<RecordType>()->getDecl(); |
| 5408 | if (RD->isDefinition()) |
| 5409 | RewriteRecordBody(RD); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5410 | } |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5411 | if (VD->getInit()) { |
| 5412 | GlobalVarDecl = VD; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5413 | CollectPropertySetters(VD->getInit()); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5414 | CurrentBody = VD->getInit(); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5415 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5416 | CurrentBody = 0; |
| 5417 | if (PropParentMap) { |
| 5418 | delete PropParentMap; |
| 5419 | PropParentMap = 0; |
| 5420 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5421 | SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 5422 | VD->getNameAsCString()); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5423 | GlobalVarDecl = 0; |
| 5424 | |
| 5425 | // This is needed for blocks. |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 5426 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5427 | RewriteCastExpr(CE); |
| 5428 | } |
| 5429 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5430 | return; |
| 5431 | } |
| 5432 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5433 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5434 | RewriteBlockPointerDecl(TD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5435 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5436 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 5437 | return; |
| 5438 | } |
| 5439 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) { |
Steve Naroff | 3d7e786 | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5440 | if (RD->isDefinition()) |
| 5441 | RewriteRecordBody(RD); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5442 | return; |
| 5443 | } |
| 5444 | // Nothing yet. |
| 5445 | } |
| 5446 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 5447 | void RewriteObjC::HandleTranslationUnit(ASTContext &C) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5448 | if (Diags.hasErrorOccurred()) |
| 5449 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5450 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5451 | RewriteInclude(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5452 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5453 | // Here's a great place to add any extra declarations that may be needed. |
| 5454 | // Write out meta data for each @protocol(<expr>). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5455 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5456 | E = ProtocolExprDecls.end(); I != E; ++I) |
| 5457 | RewriteObjCProtocolMetaData(*I, "", "", Preamble); |
| 5458 | |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5459 | InsertText(SM->getLocForStartOfFile(MainFileID), Preamble, false); |
Steve Naroff | 0aab796 | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5460 | if (ClassImplementation.size() || CategoryImplementation.size()) |
| 5461 | RewriteImplementations(); |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5462 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5463 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 5464 | // we are done. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5465 | if (const RewriteBuffer *RewriteBuf = |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5466 | Rewrite.getRewriteBufferFor(MainFileID)) { |
| 5467 | //printf("Changed:\n"); |
| 5468 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 5469 | } else { |
Benjamin Kramer | d999b37 | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5470 | llvm::errs() << "No changes\n"; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5471 | } |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 5472 | |
Steve Naroff | 621edce | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5473 | if (ClassImplementation.size() || CategoryImplementation.size() || |
| 5474 | ProtocolExprDecls.size()) { |
Steve Naroff | 0aab796 | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5475 | // Rewrite Objective-c meta data* |
| 5476 | std::string ResultStr; |
| 5477 | SynthesizeMetaDataIntoBuffer(ResultStr); |
| 5478 | // Emit metadata. |
| 5479 | *OutFile << ResultStr; |
| 5480 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5481 | OutFile->flush(); |
| 5482 | } |
| 5483 | |