Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1 | //===--- RewriteObjC.cpp - Playground for the code rewriter ---------------===// |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 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 | e99c832 | 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 | 9f30fc3 | 2009-05-18 22:50:54 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/ASTConsumers.h" |
Chris Lattner | 16a0de4 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 15 | #include "clang/Rewrite/Rewriter.h" |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 16 | #include "clang/AST/AST.h" |
| 17 | #include "clang/AST/ASTConsumer.h" |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 18 | #include "clang/AST/ParentMap.h" |
Chris Lattner | 16a0de4 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 19 | #include "clang/Basic/SourceManager.h" |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 20 | #include "clang/Basic/IdentifierTable.h" |
Chris Lattner | 4431a1b | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 21 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | f3a59a1 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 22 | #include "clang/Lex/Lexer.h" |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MemoryBuffer.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 211f8b8 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringExtras.h" |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallPtrSet.h" |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/OwningPtr.h" |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/DenseSet.h" |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 29 | |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 30 | using namespace clang; |
Chris Lattner | 211f8b8 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 31 | using llvm::utostr; |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 32 | |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 33 | namespace { |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 34 | class RewriteObjC : public ASTConsumer { |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 35 | enum { |
| 36 | BLOCK_FIELD_IS_OBJECT = 3, /* id, NSObject, __attribute__((NSObject)), |
| 37 | block, ... */ |
| 38 | BLOCK_FIELD_IS_BLOCK = 7, /* a block variable */ |
| 39 | BLOCK_FIELD_IS_BYREF = 8, /* the on stack structure holding the |
| 40 | __block variable */ |
| 41 | BLOCK_FIELD_IS_WEAK = 16, /* declared __weak, only used in byref copy |
| 42 | helpers */ |
| 43 | BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose |
| 44 | support routines */ |
| 45 | BLOCK_BYREF_CURRENT_MAX = 256 |
| 46 | }; |
| 47 | |
| 48 | enum { |
| 49 | BLOCK_NEEDS_FREE = (1 << 24), |
| 50 | BLOCK_HAS_COPY_DISPOSE = (1 << 25), |
| 51 | BLOCK_HAS_CXX_OBJ = (1 << 26), |
| 52 | BLOCK_IS_GC = (1 << 27), |
| 53 | BLOCK_IS_GLOBAL = (1 << 28), |
| 54 | BLOCK_HAS_DESCRIPTOR = (1 << 29) |
| 55 | }; |
| 56 | |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 57 | Rewriter Rewrite; |
Chris Lattner | e9c810c | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 58 | Diagnostic &Diags; |
Steve Naroff | 945a3b1 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 59 | const LangOptions &LangOpts; |
Steve Naroff | 7b3579b | 2008-01-30 19:17:43 +0000 | [diff] [blame] | 60 | unsigned RewriteFailedDiag; |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 61 | unsigned TryFinallyContainsReturnDiag; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | |
Chris Lattner | c6d91c0 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 63 | ASTContext *Context; |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 64 | SourceManager *SM; |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 65 | TranslationUnitDecl *TUDecl; |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 66 | FileID MainFileID; |
Chris Lattner | f3a59a1 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 67 | const char *MainFileStart, *MainFileEnd; |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 68 | SourceLocation LastIncLoc; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 69 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 70 | llvm::SmallVector<ObjCImplementationDecl *, 8> ClassImplementation; |
| 71 | llvm::SmallVector<ObjCCategoryImplDecl *, 8> CategoryImplementation; |
| 72 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCSynthesizedStructs; |
Steve Naroff | 13e7487 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 73 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> ObjCSynthesizedProtocols; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 74 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCForwardDecls; |
| 75 | llvm::DenseMap<ObjCMethodDecl*, std::string> MethodInternalNames; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 76 | llvm::SmallVector<Stmt *, 32> Stmts; |
| 77 | llvm::SmallVector<int, 8> ObjCBcLabelNo; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 78 | // Remember all the @protocol(<expr>) expressions. |
| 79 | llvm::SmallPtrSet<ObjCProtocolDecl *, 32> ProtocolExprDecls; |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 80 | |
| 81 | llvm::DenseSet<uint64_t> CopyDestroyCache; |
| 82 | |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 83 | unsigned NumObjCStringLiterals; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 84 | |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 85 | FunctionDecl *MsgSendFunctionDecl; |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 86 | FunctionDecl *MsgSendSuperFunctionDecl; |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 87 | FunctionDecl *MsgSendStretFunctionDecl; |
| 88 | FunctionDecl *MsgSendSuperStretFunctionDecl; |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 89 | FunctionDecl *MsgSendFpretFunctionDecl; |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 90 | FunctionDecl *GetClassFunctionDecl; |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 91 | FunctionDecl *GetMetaClassFunctionDecl; |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 92 | FunctionDecl *SelGetUidFunctionDecl; |
Steve Naroff | 265a6b9 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 93 | FunctionDecl *CFStringFunctionDecl; |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 94 | FunctionDecl *SuperContructorFunctionDecl; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 95 | |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 96 | // ObjC string constant support. |
Steve Naroff | 08899ff | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 97 | VarDecl *ConstantStringClassReference; |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 98 | RecordDecl *NSStringRecord; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 99 | |
Fariborz Jahanian | 19d42bf | 2008-01-16 00:09:11 +0000 | [diff] [blame] | 100 | // ObjC foreach break/continue generation support. |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 101 | int BcLabelCount; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 103 | // Needed for super. |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 104 | ObjCMethodDecl *CurMethodDef; |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 105 | RecordDecl *SuperStructDecl; |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 106 | RecordDecl *ConstantStringDecl; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 108 | TypeDecl *ProtocolTypeDecl; |
| 109 | QualType getProtocolType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 110 | |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 111 | // Needed for header files being rewritten |
| 112 | bool IsHeader; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 113 | |
Steve Naroff | f9e7c90 | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 114 | std::string InFileName; |
Eli Friedman | 94cf21e | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 115 | llvm::raw_ostream* OutFile; |
Eli Friedman | f22439a | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 116 | |
| 117 | bool SilenceRewriteMacroWarning; |
Fariborz Jahanian | bc6811c | 2010-01-07 22:51:18 +0000 | [diff] [blame] | 118 | bool objc_impl_method; |
Eli Friedman | f22439a | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 119 | |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 120 | std::string Preamble; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 121 | |
| 122 | // Block expressions. |
| 123 | llvm::SmallVector<BlockExpr *, 32> Blocks; |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 124 | llvm::SmallVector<int, 32> InnerDeclRefsCount; |
| 125 | llvm::SmallVector<BlockDeclRefExpr *, 32> InnerDeclRefs; |
| 126 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 127 | llvm::SmallVector<BlockDeclRefExpr *, 32> BlockDeclRefs; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 128 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 129 | // Block related declarations. |
Fariborz Jahanian | 4c4ca5a | 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 | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 134 | llvm::DenseMap<ValueDecl *, unsigned> BlockByRefDeclNo; |
Steve Naroff | 677ab3a | 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 | 4588d0f | 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 | 1042ff3 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 144 | |
Steve Naroff | 22216db | 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 | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 149 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 150 | FunctionDecl *CurFunctionDef; |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 151 | FunctionDecl *CurFunctionDeclToDeclareForBlock; |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 152 | VarDecl *GlobalVarDecl; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 153 | |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 154 | bool DisableReplaceStmt; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 156 | static const int OBJC_ABI_VERSION =7 ; |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 157 | public: |
Ted Kremenek | 380df93 | 2008-05-31 20:11:04 +0000 | [diff] [blame] | 158 | virtual void Initialize(ASTContext &context); |
| 159 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 160 | // Top Level Driver code. |
Chris Lattner | 5bbb3c8 | 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 | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 166 | void HandleDeclInMainFile(Decl *D); |
Eli Friedman | 94cf21e | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 167 | RewriteObjC(std::string inFile, llvm::raw_ostream *OS, |
Eli Friedman | f22439a | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 168 | Diagnostic &D, const LangOptions &LOpts, |
| 169 | bool silenceMacroWarn); |
Ted Kremenek | 6231e7e | 2008-08-08 04:15:52 +0000 | [diff] [blame] | 170 | |
| 171 | ~RewriteObjC() {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 172 | |
Chris Lattner | cf16983 | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 173 | virtual void HandleTranslationUnit(ASTContext &C); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 174 | |
Fariborz Jahanian | a7e1dcd | 2010-02-05 16:43:40 +0000 | [diff] [blame] | 175 | void ReplaceStmt(Stmt *Old, Stmt *New) { |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 176 | Stmt *ReplacingStmt = ReplacedNodes[Old]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 177 | |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 178 | if (ReplacingStmt) |
| 179 | return; // We can't rewrite the same node twice. |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 180 | |
Steve Naroff | 08628db | 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 | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 184 | // If replacement succeeded or warning disabled return with no warning. |
Fariborz Jahanian | a7e1dcd | 2010-02-05 16:43:40 +0000 | [diff] [blame] | 185 | if (!Rewrite.ReplaceStmt(Old, New)) { |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 186 | ReplacedNodes[Old] = New; |
| 187 | return; |
| 188 | } |
| 189 | if (SilenceRewriteMacroWarning) |
| 190 | return; |
Chris Lattner | 8488c82 | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 191 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 192 | << Old->getSourceRange(); |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 193 | } |
Steve Naroff | 08628db | 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 | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 206 | New->printPretty(S, *Context, 0, PrintingPolicy(LangOpts)); |
Steve Naroff | 08628db | 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 | dec484a | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 210 | if (!Rewrite.ReplaceText(SrcRange.getBegin(), Size, Str)) { |
Steve Naroff | 08628db | 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 | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 220 | void InsertText(SourceLocation Loc, llvm::StringRef Str, |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 221 | bool InsertAfter = true) { |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 222 | // If insertion succeeded or warning disabled return with no warning. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 223 | if (!Rewrite.InsertText(Loc, Str, InsertAfter) || |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 224 | SilenceRewriteMacroWarning) |
| 225 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 226 | |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 227 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 228 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 229 | |
Chris Lattner | 9cc55f5 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 234 | |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 235 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 236 | } |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 237 | |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 238 | void ReplaceText(SourceLocation Start, unsigned OrigLength, |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 239 | llvm::StringRef Str) { |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 240 | // If removal succeeded or warning disabled return with no warning. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 241 | if (!Rewrite.ReplaceText(Start, OrigLength, Str) || |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 242 | SilenceRewriteMacroWarning) |
| 243 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 244 | |
Chris Lattner | 9cc55f5 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 245 | Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag); |
| 246 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 247 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 248 | // Syntactic Rewriting. |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 249 | void RewritePrologue(SourceLocation Loc); |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 250 | void RewriteInclude(); |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 251 | void RewriteTabs(); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 252 | void RewriteForwardClassDecl(ObjCClassDecl *Dcl); |
Steve Naroff | c038b3a | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 253 | void RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 254 | ObjCImplementationDecl *IMD, |
| 255 | ObjCCategoryImplDecl *CID); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 256 | void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl); |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 257 | void RewriteImplementationDecl(Decl *Dcl); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 258 | void RewriteObjCMethodDecl(ObjCMethodDecl *MDecl, std::string &ResultStr); |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 259 | void RewriteTypeIntoString(QualType T, std::string &ResultStr, |
| 260 | const FunctionType *&FPRetType); |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 261 | void RewriteByRefString(std::string &ResultStr, const std::string &Name, |
| 262 | ValueDecl *VD); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 263 | void RewriteCategoryDecl(ObjCCategoryDecl *Dcl); |
| 264 | void RewriteProtocolDecl(ObjCProtocolDecl *Dcl); |
| 265 | void RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *Dcl); |
| 266 | void RewriteMethodDeclaration(ObjCMethodDecl *Method); |
Steve Naroff | 0c0f5ba | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 267 | void RewriteProperty(ObjCPropertyDecl *prop); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 268 | void RewriteFunctionDecl(FunctionDecl *FD); |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 269 | void RewriteBlockLiteralFunctionDecl(FunctionDecl *FD); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 270 | void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 271 | void RewriteTypeOfDecl(VarDecl *VD); |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 272 | void RewriteObjCQualifiedInterfaceTypes(Expr *E); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 273 | bool needToScanForQualifiers(QualType T); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 274 | ObjCInterfaceDecl *isSuperReceiver(Expr *recExpr); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 275 | QualType getSuperStructType(); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 276 | QualType getConstantStringStructType(); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 277 | bool BufferContainsPPDirectives(const char *startBuf, const char *endBuf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 278 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 279 | // Expression Rewriting. |
Steve Naroff | 2011338 | 2007-11-09 15:20:18 +0000 | [diff] [blame] | 280 | Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 281 | void CollectPropertySetters(Stmt *S); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 282 | |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 283 | Stmt *CurrentBody; |
| 284 | ParentMap *PropParentMap; // created lazily. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 285 | |
Chris Lattner | 6953469 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 286 | Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp); |
Fariborz Jahanian | 80fadb5 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 287 | Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, SourceLocation OrigStart, |
| 288 | bool &replaced); |
| 289 | Stmt *RewriteObjCNestedIvarRefExpr(Stmt *S, bool &replaced); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 290 | Stmt *RewritePropertyGetter(ObjCPropertyRefExpr *PropRefExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 291 | Stmt *RewritePropertySetter(BinaryOperator *BinOp, Expr *newStmt, |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 292 | SourceRange SrcRange); |
Steve Naroff | e4f9b23 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 293 | Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp); |
Chris Lattner | 6953469 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 294 | Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp); |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 295 | Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp); |
Fariborz Jahanian | 33c0e81 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 296 | Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 297 | void WarnAboutReturnGotoStmts(Stmt *S); |
| 298 | void HasReturnStmts(Stmt *S, bool &hasReturns); |
| 299 | void RewriteTryReturnStmts(Stmt *S); |
| 300 | void RewriteSyncReturnStmts(Stmt *S, std::string buf); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 301 | Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 302 | Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 303 | Stmt *RewriteObjCCatchStmt(ObjCAtCatchStmt *S); |
| 304 | Stmt *RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S); |
| 305 | Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S); |
Chris Lattner | a779d69 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 306 | Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
| 307 | SourceLocation OrigEnd); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 308 | CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD, |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 309 | Expr **args, unsigned nargs, |
| 310 | SourceLocation StartLoc=SourceLocation(), |
| 311 | SourceLocation EndLoc=SourceLocation()); |
| 312 | Stmt *SynthMessageExpr(ObjCMessageExpr *Exp, |
| 313 | SourceLocation StartLoc=SourceLocation(), |
| 314 | SourceLocation EndLoc=SourceLocation()); |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 315 | Stmt *RewriteBreakStmt(BreakStmt *S); |
| 316 | Stmt *RewriteContinueStmt(ContinueStmt *S); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 317 | void SynthCountByEnumWithState(std::string &buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 318 | |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 319 | void SynthMsgSendFunctionDecl(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 320 | void SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 321 | void SynthMsgSendStretFunctionDecl(); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 322 | void SynthMsgSendFpretFunctionDecl(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 323 | void SynthMsgSendSuperStretFunctionDecl(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 324 | void SynthGetClassFunctionDecl(); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 325 | void SynthGetMetaClassFunctionDecl(); |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 326 | void SynthSelGetUidFunctionDecl(); |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 327 | void SynthSuperContructorFunctionDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 328 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 329 | // Metadata emission. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 330 | void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 331 | std::string &Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 332 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 333 | void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 334 | std::string &Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 335 | |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 336 | template<typename MethodIterator> |
| 337 | void RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 338 | MethodIterator MethodEnd, |
Fariborz Jahanian | 3df412a | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 339 | bool IsInstanceMethod, |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 340 | const char *prefix, |
Chris Lattner | 211f8b8 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 341 | const char *ClassName, |
| 342 | std::string &Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 343 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 344 | void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol, |
| 345 | const char *prefix, |
| 346 | const char *ClassName, |
| 347 | std::string &Result); |
| 348 | void RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Prots, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 349 | const char *prefix, |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 350 | const char *ClassName, |
| 351 | std::string &Result); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 352 | void SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 353 | std::string &Result); |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 354 | void SynthesizeIvarOffsetComputation(ObjCContainerDecl *IDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 355 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 356 | std::string &Result); |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 357 | void RewriteImplementations(); |
| 358 | void SynthesizeMetaDataIntoBuffer(std::string &Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 359 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 360 | // Block rewriting. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 361 | void RewriteBlocksInFunctionProtoType(QualType funcType, NamedDecl *D); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 362 | void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 363 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 364 | void InsertBlockLiteralsWithinFunction(FunctionDecl *FD); |
| 365 | void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 366 | |
| 367 | // Block specific rewrite rules. |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 368 | void RewriteBlockCall(CallExpr *Exp); |
| 369 | void RewriteBlockPointerDecl(NamedDecl *VD); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 370 | void RewriteByRefVar(VarDecl *VD); |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 371 | std::string SynthesizeByrefCopyDestroyHelper(VarDecl *VD, int flag); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 372 | Stmt *RewriteBlockDeclRefExpr(Expr *VD); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 373 | void RewriteBlockPointerFunctionArgs(FunctionDecl *FD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 | |
| 375 | std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 376 | const char *funcName, std::string Tag); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 377 | std::string SynthesizeBlockFunc(BlockExpr *CE, int i, |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 378 | const char *funcName, std::string Tag); |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 379 | std::string SynthesizeBlockImpl(BlockExpr *CE, |
| 380 | std::string Tag, std::string Desc); |
| 381 | std::string SynthesizeBlockDescriptor(std::string DescTag, |
| 382 | std::string ImplTag, |
| 383 | int i, const char *funcName, |
| 384 | unsigned hasCopy); |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 385 | Stmt *SynthesizeBlockCall(CallExpr *Exp, const Expr* BlockExp); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 386 | void SynthesizeBlockLiterals(SourceLocation FunLocStart, |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 387 | const char *FunName); |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 388 | void RewriteRecordBody(RecordDecl *RD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 389 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 390 | void CollectBlockDeclRefInfo(BlockExpr *Exp); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 391 | void GetBlockDeclRefExprs(Stmt *S); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 392 | void GetInnerBlockDeclRefExprs(Stmt *S, |
| 393 | llvm::SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs, |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 394 | llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 395 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 396 | // We avoid calling Type::isBlockPointerType(), since it operates on the |
| 397 | // canonical type. We only care if the top-level type is a closure pointer. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 398 | bool isTopLevelBlockPointerType(QualType T) { |
| 399 | return isa<BlockPointerType>(T); |
| 400 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 401 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 402 | // FIXME: This predicate seems like it would be useful to add to ASTContext. |
| 403 | bool isObjCType(QualType T) { |
| 404 | if (!LangOpts.ObjC1 && !LangOpts.ObjC2) |
| 405 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 406 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 407 | QualType OCT = Context->getCanonicalType(T).getUnqualifiedType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 408 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 409 | if (OCT == Context->getCanonicalType(Context->getObjCIdType()) || |
| 410 | OCT == Context->getCanonicalType(Context->getObjCClassType())) |
| 411 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 412 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 413 | if (const PointerType *PT = OCT->getAs<PointerType>()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 414 | if (isa<ObjCInterfaceType>(PT->getPointeeType()) || |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 415 | PT->getPointeeType()->isObjCQualifiedIdType()) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 416 | return true; |
| 417 | } |
| 418 | return false; |
| 419 | } |
| 420 | bool PointerTypeTakesAnyBlockArguments(QualType QT); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 421 | void GetExtentOfArgList(const char *Name, const char *&LParen, |
| 422 | const char *&RParen); |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 423 | void RewriteCastExpr(CStyleCastExpr *CE); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 424 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 425 | FunctionDecl *SynthBlockInitFunctionDecl(const char *name); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 426 | Stmt *SynthBlockInitExpr(BlockExpr *Exp, |
| 427 | const llvm::SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 428 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 429 | void QuoteDoublequotes(std::string &From, std::string &To) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 430 | for (unsigned i = 0; i < From.length(); i++) { |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 431 | if (From[i] == '"') |
| 432 | To += "\\\""; |
| 433 | else |
| 434 | To += From[i]; |
| 435 | } |
| 436 | } |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 437 | }; |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 438 | |
| 439 | // Helper function: create a CStyleCastExpr with trivial type source info. |
| 440 | CStyleCastExpr* NoTypeInfoCStyleCastExpr(ASTContext *Ctx, QualType Ty, |
| 441 | CastExpr::CastKind Kind, Expr *E) { |
| 442 | TypeSourceInfo *TInfo = Ctx->getTrivialTypeSourceInfo(Ty, SourceLocation()); |
| 443 | return new (Ctx) CStyleCastExpr(Ty, Kind, E, TInfo, |
| 444 | SourceLocation(), SourceLocation()); |
| 445 | } |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 448 | void RewriteObjC::RewriteBlocksInFunctionProtoType(QualType funcType, |
| 449 | NamedDecl *D) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 450 | if (FunctionProtoType *fproto = dyn_cast<FunctionProtoType>(funcType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 451 | for (FunctionProtoType::arg_type_iterator I = fproto->arg_type_begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 452 | E = fproto->arg_type_end(); I && (I != E); ++I) |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 453 | if (isTopLevelBlockPointerType(*I)) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 454 | // All the args are checked/rewritten. Don't call twice! |
| 455 | RewriteBlockPointerDecl(D); |
| 456 | break; |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | void RewriteObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 462 | const PointerType *PT = funcType->getAs<PointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 463 | if (PT && PointerTypeTakesAnyBlockArguments(funcType)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 464 | RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 467 | static bool IsHeaderFile(const std::string &Filename) { |
| 468 | std::string::size_type DotPos = Filename.rfind('.'); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 469 | |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 470 | if (DotPos == std::string::npos) { |
| 471 | // no file extension |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 472 | return false; |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 473 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 474 | |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 475 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 476 | // C header: .h |
| 477 | // C++ header: .hh or .H; |
| 478 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 479 | } |
Fariborz Jahanian | 159ee39 | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 480 | |
Eli Friedman | 94cf21e | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 481 | RewriteObjC::RewriteObjC(std::string inFile, llvm::raw_ostream* OS, |
Eli Friedman | f22439a | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 482 | Diagnostic &D, const LangOptions &LOpts, |
| 483 | bool silenceMacroWarn) |
| 484 | : Diags(D), LangOpts(LOpts), InFileName(inFile), OutFile(OS), |
| 485 | SilenceRewriteMacroWarning(silenceMacroWarn) { |
Steve Naroff | f9e7c90 | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 486 | IsHeader = IsHeaderFile(inFile); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 487 | RewriteFailedDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
Steve Naroff | f9e7c90 | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 488 | "rewriting sub-expression within a macro (may not be correct)"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 489 | TryFinallyContainsReturnDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 490 | "rewriter doesn't support user-specified control flow semantics " |
| 491 | "for @try/@finally (code may not execute properly)"); |
Steve Naroff | f9e7c90 | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Eli Friedman | a63ab2d | 2009-05-18 22:29:17 +0000 | [diff] [blame] | 494 | ASTConsumer *clang::CreateObjCRewriter(const std::string& InFile, |
| 495 | llvm::raw_ostream* OS, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 496 | Diagnostic &Diags, |
Eli Friedman | f22439a | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 497 | const LangOptions &LOpts, |
| 498 | bool SilenceRewriteMacroWarning) { |
| 499 | return new RewriteObjC(InFile, OS, Diags, LOpts, SilenceRewriteMacroWarning); |
Chris Lattner | e9c810c | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 500 | } |
Chris Lattner | e99c832 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 501 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 502 | void RewriteObjC::Initialize(ASTContext &context) { |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 503 | Context = &context; |
| 504 | SM = &Context->getSourceManager(); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 505 | TUDecl = Context->getTranslationUnitDecl(); |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 506 | MsgSendFunctionDecl = 0; |
| 507 | MsgSendSuperFunctionDecl = 0; |
| 508 | MsgSendStretFunctionDecl = 0; |
| 509 | MsgSendSuperStretFunctionDecl = 0; |
| 510 | MsgSendFpretFunctionDecl = 0; |
| 511 | GetClassFunctionDecl = 0; |
| 512 | GetMetaClassFunctionDecl = 0; |
| 513 | SelGetUidFunctionDecl = 0; |
| 514 | CFStringFunctionDecl = 0; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 515 | ConstantStringClassReference = 0; |
| 516 | NSStringRecord = 0; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 517 | CurMethodDef = 0; |
| 518 | CurFunctionDef = 0; |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 519 | CurFunctionDeclToDeclareForBlock = 0; |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 520 | GlobalVarDecl = 0; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 521 | SuperStructDecl = 0; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 522 | ProtocolTypeDecl = 0; |
Steve Naroff | 60a9ef6 | 2008-03-27 22:59:54 +0000 | [diff] [blame] | 523 | ConstantStringDecl = 0; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 524 | BcLabelCount = 0; |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 525 | SuperContructorFunctionDecl = 0; |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 526 | NumObjCStringLiterals = 0; |
Steve Naroff | f1ab600 | 2008-12-08 20:01:41 +0000 | [diff] [blame] | 527 | PropParentMap = 0; |
| 528 | CurrentBody = 0; |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 529 | DisableReplaceStmt = false; |
Fariborz Jahanian | bc6811c | 2010-01-07 22:51:18 +0000 | [diff] [blame] | 530 | objc_impl_method = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 531 | |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 532 | // Get the ID and start/end of the main file. |
| 533 | MainFileID = SM->getMainFileID(); |
| 534 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 535 | MainFileStart = MainBuf->getBufferStart(); |
| 536 | MainFileEnd = MainBuf->getBufferEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 537 | |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 538 | Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOptions()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 539 | |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 540 | // declaring objc_selector outside the parameter list removes a silly |
| 541 | // scope related warning... |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 542 | if (IsHeader) |
Steve Naroff | fcc6fd5 | 2009-02-03 20:39:18 +0000 | [diff] [blame] | 543 | Preamble = "#pragma once\n"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 544 | Preamble += "struct objc_selector; struct objc_class;\n"; |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 545 | Preamble += "struct __rw_objc_super { struct objc_object *object; "; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 546 | Preamble += "struct objc_object *superClass; "; |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 547 | if (LangOpts.Microsoft) { |
| 548 | // Add a constructor for creating temporary objects. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 549 | Preamble += "__rw_objc_super(struct objc_object *o, struct objc_object *s) " |
| 550 | ": "; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 551 | Preamble += "object(o), superClass(s) {} "; |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 552 | } |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 553 | Preamble += "};\n"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 554 | Preamble += "#ifndef _REWRITER_typedef_Protocol\n"; |
| 555 | Preamble += "typedef struct objc_object Protocol;\n"; |
| 556 | Preamble += "#define _REWRITER_typedef_Protocol\n"; |
| 557 | Preamble += "#endif\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 558 | if (LangOpts.Microsoft) { |
| 559 | Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n"; |
| 560 | Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n"; |
| 561 | } else |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 562 | Preamble += "#define __OBJC_RW_DLLIMPORT extern\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 563 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 564 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 565 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 566 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 567 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend_stret"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 568 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 569 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper_stret"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 570 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 571 | Preamble += "__OBJC_RW_DLLIMPORT double objc_msgSend_fpret"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 572 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 573 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getClass"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 574 | Preamble += "(const char *);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 575 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getMetaClass"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 576 | Preamble += "(const char *);\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 577 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw(struct objc_object *);\n"; |
| 578 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_enter(void *);\n"; |
| 579 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_exit(void *);\n"; |
| 580 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_exception_extract(void *);\n"; |
| 581 | Preamble += "__OBJC_RW_DLLIMPORT int objc_exception_match"; |
Steve Naroff | d30f8c5 | 2008-05-09 21:17:56 +0000 | [diff] [blame] | 582 | Preamble += "(struct objc_class *, struct objc_object *);\n"; |
Steve Naroff | 8dd1525 | 2008-07-16 18:58:11 +0000 | [diff] [blame] | 583 | // @synchronized hooks. |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 584 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_enter(struct objc_object *);\n"; |
| 585 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_exit(struct objc_object *);\n"; |
| 586 | Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 587 | Preamble += "#ifndef __FASTENUMERATIONSTATE\n"; |
| 588 | Preamble += "struct __objcFastEnumerationState {\n\t"; |
| 589 | Preamble += "unsigned long state;\n\t"; |
Steve Naroff | 4dbab8a | 2008-04-04 22:58:22 +0000 | [diff] [blame] | 590 | Preamble += "void **itemsPtr;\n\t"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 591 | Preamble += "unsigned long *mutationsPtr;\n\t"; |
| 592 | Preamble += "unsigned long extra[5];\n};\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 593 | Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 594 | Preamble += "#define __FASTENUMERATIONSTATE\n"; |
| 595 | Preamble += "#endif\n"; |
| 596 | Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n"; |
| 597 | Preamble += "struct __NSConstantStringImpl {\n"; |
| 598 | Preamble += " int *isa;\n"; |
| 599 | Preamble += " int flags;\n"; |
| 600 | Preamble += " char *str;\n"; |
| 601 | Preamble += " long length;\n"; |
| 602 | Preamble += "};\n"; |
Steve Naroff | dd514e0 | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 603 | Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n"; |
| 604 | Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n"; |
| 605 | Preamble += "#else\n"; |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 606 | Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n"; |
Steve Naroff | dd514e0 | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 607 | Preamble += "#endif\n"; |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 608 | Preamble += "#define __NSCONSTANTSTRINGIMPL\n"; |
| 609 | Preamble += "#endif\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 610 | // Blocks preamble. |
| 611 | Preamble += "#ifndef BLOCK_IMPL\n"; |
| 612 | Preamble += "#define BLOCK_IMPL\n"; |
| 613 | Preamble += "struct __block_impl {\n"; |
| 614 | Preamble += " void *isa;\n"; |
| 615 | Preamble += " int Flags;\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 616 | Preamble += " int Reserved;\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 617 | Preamble += " void *FuncPtr;\n"; |
| 618 | Preamble += "};\n"; |
Steve Naroff | 61d879e | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 619 | Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n"; |
Steve Naroff | 287a2bf | 2009-12-06 01:52:22 +0000 | [diff] [blame] | 620 | Preamble += "#ifdef __OBJC_EXPORT_BLOCKS\n"; |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 621 | Preamble += "extern \"C\" __declspec(dllexport) " |
| 622 | "void _Block_object_assign(void *, const void *, const int);\n"; |
Steve Naroff | 2b3843d | 2009-12-06 01:33:56 +0000 | [diff] [blame] | 623 | Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_dispose(const void *, const int);\n"; |
| 624 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteGlobalBlock[32];\n"; |
| 625 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteStackBlock[32];\n"; |
| 626 | Preamble += "#else\n"; |
Steve Naroff | 7bf01ea | 2010-01-05 18:09:31 +0000 | [diff] [blame] | 627 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_assign(void *, const void *, const int);\n"; |
| 628 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_dispose(const void *, const int);\n"; |
Steve Naroff | 2b3843d | 2009-12-06 01:33:56 +0000 | [diff] [blame] | 629 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteGlobalBlock[32];\n"; |
| 630 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteStackBlock[32];\n"; |
| 631 | Preamble += "#endif\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 632 | Preamble += "#endif\n"; |
Steve Naroff | cb04e88 | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 633 | if (LangOpts.Microsoft) { |
Steve Naroff | f122ff0 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 634 | Preamble += "#undef __OBJC_RW_DLLIMPORT\n"; |
| 635 | Preamble += "#undef __OBJC_RW_STATICIMPORT\n"; |
Steve Naroff | cb04e88 | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 636 | Preamble += "#define __attribute__(X)\n"; |
Fariborz Jahanian | f0462ff | 2010-01-15 22:29:39 +0000 | [diff] [blame] | 637 | Preamble += "#define __weak\n"; |
Steve Naroff | cb04e88 | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 638 | } |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 639 | else { |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 640 | Preamble += "#define __block\n"; |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 641 | Preamble += "#define __weak\n"; |
| 642 | } |
Fariborz Jahanian | db217c4 | 2010-03-02 01:19:04 +0000 | [diff] [blame^] | 643 | // NOTE! Windows uses LLP64 for 64bit mode. So, cast pointer to long long |
| 644 | // as this avoids warning in any 64bit/32bit compilation model. |
| 645 | Preamble += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((long long) &((TYPE *)0)->MEMBER)\n"; |
Chris Lattner | 187f626 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 649 | //===----------------------------------------------------------------------===// |
| 650 | // Top Level Driver Code |
| 651 | //===----------------------------------------------------------------------===// |
| 652 | |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 653 | void RewriteObjC::HandleTopLevelSingleDecl(Decl *D) { |
Ted Kremenek | 31e7f0f | 2010-02-05 21:28:51 +0000 | [diff] [blame] | 654 | if (Diags.hasErrorOccurred()) |
| 655 | return; |
| 656 | |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 657 | // Two cases: either the decl could be in the main file, or it could be in a |
| 658 | // #included file. If the former, rewrite it now. If the later, check to see |
| 659 | // if we rewrote the #include/#import. |
| 660 | SourceLocation Loc = D->getLocation(); |
Chris Lattner | 8a42586 | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 661 | Loc = SM->getInstantiationLoc(Loc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 662 | |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 663 | // If this is for a builtin, ignore it. |
| 664 | if (Loc.isInvalid()) return; |
| 665 | |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 666 | // Look for built-in declarations that we need to refer during the rewrite. |
| 667 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 668 | RewriteFunctionDecl(FD); |
Steve Naroff | 08899ff | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 669 | } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) { |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 670 | // declared in <Foundation/NSString.h> |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 671 | if (strcmp(FVD->getNameAsCString(), "_NSConstantStringClassReference") == 0) { |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 672 | ConstantStringClassReference = FVD; |
| 673 | return; |
| 674 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 675 | } else if (ObjCInterfaceDecl *MD = dyn_cast<ObjCInterfaceDecl>(D)) { |
Steve Naroff | 161a92b | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 676 | RewriteInterfaceDecl(MD); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 677 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 678 | RewriteCategoryDecl(CD); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 679 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 680 | RewriteProtocolDecl(PD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 681 | } else if (ObjCForwardProtocolDecl *FP = |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 682 | dyn_cast<ObjCForwardProtocolDecl>(D)){ |
Fariborz Jahanian | da6165c | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 683 | RewriteForwardProtocolDecl(FP); |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 684 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
| 685 | // Recurse into linkage specifications |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 686 | for (DeclContext::decl_iterator DI = LSD->decls_begin(), |
| 687 | DIEnd = LSD->decls_end(); |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 688 | DI != DIEnd; ++DI) |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 689 | HandleTopLevelSingleDecl(*DI); |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 690 | } |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 691 | // If we have a decl in the main file, see if we should rewrite it. |
Ted Kremenek | d61ed3b | 2008-04-14 21:24:13 +0000 | [diff] [blame] | 692 | if (SM->isFromMainFile(Loc)) |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 693 | return HandleDeclInMainFile(D); |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 696 | //===----------------------------------------------------------------------===// |
| 697 | // Syntactic (non-AST) Rewriting Code |
| 698 | //===----------------------------------------------------------------------===// |
| 699 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 700 | void RewriteObjC::RewriteInclude() { |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 701 | SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID); |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 702 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
| 703 | const char *MainBufStart = MainBuf.first; |
| 704 | const char *MainBufEnd = MainBuf.second; |
| 705 | size_t ImportLen = strlen("import"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 706 | |
Fariborz Jahanian | 137d693 | 2008-01-19 01:03:17 +0000 | [diff] [blame] | 707 | // Loop over the whole file, looking for includes. |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 708 | for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { |
| 709 | if (*BufPtr == '#') { |
| 710 | if (++BufPtr == MainBufEnd) |
| 711 | return; |
| 712 | while (*BufPtr == ' ' || *BufPtr == '\t') |
| 713 | if (++BufPtr == MainBufEnd) |
| 714 | return; |
| 715 | if (!strncmp(BufPtr, "import", ImportLen)) { |
| 716 | // replace import with include |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 717 | SourceLocation ImportLoc = |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 718 | LocStart.getFileLocWithOffset(BufPtr-MainBufStart); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 719 | ReplaceText(ImportLoc, ImportLen, "include"); |
Fariborz Jahanian | 8025836 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 720 | BufPtr += ImportLen; |
| 721 | } |
| 722 | } |
| 723 | } |
Chris Lattner | 0bd1c97 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 726 | void RewriteObjC::RewriteTabs() { |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 727 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
| 728 | const char *MainBufStart = MainBuf.first; |
| 729 | const char *MainBufEnd = MainBuf.second; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 730 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 731 | // Loop over the whole file, looking for tabs. |
| 732 | for (const char *BufPtr = MainBufStart; BufPtr != MainBufEnd; ++BufPtr) { |
| 733 | if (*BufPtr != '\t') |
| 734 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 735 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 736 | // Okay, we found a tab. This tab will turn into at least one character, |
| 737 | // but it depends on which 'virtual column' it is in. Compute that now. |
| 738 | unsigned VCol = 0; |
| 739 | while (BufPtr-VCol != MainBufStart && BufPtr[-VCol-1] != '\t' && |
| 740 | BufPtr[-VCol-1] != '\n' && BufPtr[-VCol-1] != '\r') |
| 741 | ++VCol; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 742 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 743 | // Okay, now that we know the virtual column, we know how many spaces to |
| 744 | // insert. We assume 8-character tab-stops. |
| 745 | unsigned Spaces = 8-(VCol & 7); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 746 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 747 | // Get the location of the tab. |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 748 | SourceLocation TabLoc = SM->getLocForStartOfFile(MainFileID); |
| 749 | TabLoc = TabLoc.getFileLocWithOffset(BufPtr-MainBufStart); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 750 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 751 | // Rewrite the single tab character into a sequence of spaces. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 752 | ReplaceText(TabLoc, 1, llvm::StringRef(" ", Spaces)); |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 753 | } |
Chris Lattner | 16a0de4 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 754 | } |
| 755 | |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 756 | static std::string getIvarAccessString(ObjCInterfaceDecl *ClassDecl, |
| 757 | ObjCIvarDecl *OID) { |
| 758 | std::string S; |
| 759 | S = "((struct "; |
| 760 | S += ClassDecl->getIdentifier()->getName(); |
| 761 | S += "_IMPL *)self)->"; |
Daniel Dunbar | 70e7ead | 2009-10-18 20:26:27 +0000 | [diff] [blame] | 762 | S += OID->getName(); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 763 | return S; |
| 764 | } |
| 765 | |
Steve Naroff | c038b3a | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 766 | void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 767 | ObjCImplementationDecl *IMD, |
| 768 | ObjCCategoryImplDecl *CID) { |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 769 | static bool objcGetPropertyDefined = false; |
| 770 | static bool objcSetPropertyDefined = false; |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 771 | SourceLocation startLoc = PID->getLocStart(); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 772 | InsertText(startLoc, "// "); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 773 | const char *startBuf = SM->getCharacterData(startLoc); |
| 774 | assert((*startBuf == '@') && "bogus @synthesize location"); |
| 775 | const char *semiBuf = strchr(startBuf, ';'); |
| 776 | assert((*semiBuf == ';') && "@synthesize: can't find ';'"); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 777 | SourceLocation onePastSemiLoc = |
| 778 | startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 779 | |
| 780 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 781 | return; // FIXME: is this correct? |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 782 | |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 783 | // Generate the 'getter' function. |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 784 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 785 | ObjCInterfaceDecl *ClassDecl = PD->getGetterMethodDecl()->getClassInterface(); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 786 | ObjCIvarDecl *OID = PID->getPropertyIvarDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 787 | |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 788 | if (!OID) |
| 789 | return; |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 790 | unsigned Attributes = PD->getPropertyAttributes(); |
| 791 | bool GenGetProperty = !(Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) && |
| 792 | (Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
| 793 | ObjCPropertyDecl::OBJC_PR_copy)); |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 794 | std::string Getr; |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 795 | if (GenGetProperty && !objcGetPropertyDefined) { |
| 796 | objcGetPropertyDefined = true; |
| 797 | // FIXME. Is this attribute correct in all cases? |
| 798 | Getr = "\nextern \"C\" __declspec(dllimport) " |
| 799 | "id objc_getProperty(id, SEL, long, bool);\n"; |
| 800 | } |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 801 | RewriteObjCMethodDecl(PD->getGetterMethodDecl(), Getr); |
| 802 | Getr += "{ "; |
| 803 | // Synthesize an explicit cast to gain access to the ivar. |
Steve Naroff | 0629704 | 2008-12-02 17:54:50 +0000 | [diff] [blame] | 804 | // See objc-act.c:objc_synthesize_new_getter() for details. |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 805 | if (GenGetProperty) { |
| 806 | // return objc_getProperty(self, _cmd, offsetof(ClassDecl, OID), 1) |
| 807 | Getr += "typedef "; |
| 808 | const FunctionType *FPRetType = 0; |
| 809 | RewriteTypeIntoString(PD->getGetterMethodDecl()->getResultType(), Getr, |
| 810 | FPRetType); |
| 811 | Getr += " _TYPE"; |
| 812 | if (FPRetType) { |
| 813 | Getr += ")"; // close the precedence "scope" for "*". |
| 814 | |
| 815 | // Now, emit the argument types (if any). |
| 816 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) { |
| 817 | Getr += "("; |
| 818 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 819 | if (i) Getr += ", "; |
| 820 | std::string ParamStr = FT->getArgType(i).getAsString(); |
| 821 | Getr += ParamStr; |
| 822 | } |
| 823 | if (FT->isVariadic()) { |
| 824 | if (FT->getNumArgs()) Getr += ", "; |
| 825 | Getr += "..."; |
| 826 | } |
| 827 | Getr += ")"; |
| 828 | } else |
| 829 | Getr += "()"; |
| 830 | } |
| 831 | Getr += ";\n"; |
| 832 | Getr += "return (_TYPE)"; |
| 833 | Getr += "objc_getProperty(self, _cmd, "; |
| 834 | SynthesizeIvarOffsetComputation(ClassDecl, OID, Getr); |
| 835 | Getr += ", 1)"; |
| 836 | } |
| 837 | else |
| 838 | Getr += "return " + getIvarAccessString(ClassDecl, OID); |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 839 | Getr += "; }"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 840 | InsertText(onePastSemiLoc, Getr); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 841 | if (PD->isReadOnly()) |
| 842 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 843 | |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 844 | // Generate the 'setter' function. |
| 845 | std::string Setr; |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 846 | bool GenSetProperty = Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
| 847 | ObjCPropertyDecl::OBJC_PR_copy); |
| 848 | if (GenSetProperty && !objcSetPropertyDefined) { |
| 849 | objcSetPropertyDefined = true; |
| 850 | // FIXME. Is this attribute correct in all cases? |
| 851 | Setr = "\nextern \"C\" __declspec(dllimport) " |
| 852 | "void objc_setProperty (id, SEL, long, id, bool, bool);\n"; |
| 853 | } |
| 854 | |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 855 | RewriteObjCMethodDecl(PD->getSetterMethodDecl(), Setr); |
Steve Naroff | 9af9491 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 856 | Setr += "{ "; |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 857 | // Synthesize an explicit cast to initialize the ivar. |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 858 | // See objc-act.c:objc_synthesize_new_setter() for details. |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 859 | if (GenSetProperty) { |
| 860 | Setr += "objc_setProperty (self, _cmd, "; |
| 861 | SynthesizeIvarOffsetComputation(ClassDecl, OID, Setr); |
| 862 | Setr += ", (id)"; |
| 863 | Setr += PD->getNameAsCString(); |
| 864 | Setr += ", "; |
| 865 | if (Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 866 | Setr += "0, "; |
| 867 | else |
| 868 | Setr += "1, "; |
| 869 | if (Attributes & ObjCPropertyDecl::OBJC_PR_copy) |
| 870 | Setr += "1)"; |
| 871 | else |
| 872 | Setr += "0)"; |
| 873 | } |
| 874 | else { |
| 875 | Setr += getIvarAccessString(ClassDecl, OID) + " = "; |
| 876 | Setr += PD->getNameAsCString(); |
| 877 | } |
Steve Naroff | 003d00e | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 878 | Setr += "; }"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 879 | InsertText(onePastSemiLoc, Setr); |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 880 | } |
Chris Lattner | 16a0de4 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 881 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 882 | void RewriteObjC::RewriteForwardClassDecl(ObjCClassDecl *ClassDecl) { |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 883 | // Get the start location and compute the semi location. |
| 884 | SourceLocation startLoc = ClassDecl->getLocation(); |
| 885 | const char *startBuf = SM->getCharacterData(startLoc); |
| 886 | const char *semiPtr = strchr(startBuf, ';'); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 887 | |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 888 | // Translate to typedef's that forward reference structs with the same name |
| 889 | // as the class. As a convenience, we include the original declaration |
| 890 | // as a comment. |
| 891 | std::string typedefString; |
Fariborz Jahanian | 1c2cb6d | 2010-01-11 22:48:40 +0000 | [diff] [blame] | 892 | typedefString += "// @class "; |
| 893 | for (ObjCClassDecl::iterator I = ClassDecl->begin(), E = ClassDecl->end(); |
| 894 | I != E; ++I) { |
| 895 | ObjCInterfaceDecl *ForwardDecl = I->getInterface(); |
| 896 | typedefString += ForwardDecl->getNameAsString(); |
| 897 | if (I+1 != E) |
| 898 | typedefString += ", "; |
| 899 | else |
| 900 | typedefString += ";\n"; |
| 901 | } |
| 902 | |
Chris Lattner | 9ee23b7 | 2009-02-20 18:04:31 +0000 | [diff] [blame] | 903 | for (ObjCClassDecl::iterator I = ClassDecl->begin(), E = ClassDecl->end(); |
| 904 | I != E; ++I) { |
Ted Kremenek | 9b124e1 | 2009-11-18 00:28:11 +0000 | [diff] [blame] | 905 | ObjCInterfaceDecl *ForwardDecl = I->getInterface(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 906 | typedefString += "#ifndef _REWRITER_typedef_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 907 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 908 | typedefString += "\n"; |
| 909 | typedefString += "#define _REWRITER_typedef_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 910 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 911 | typedefString += "\n"; |
Steve Naroff | 98eb8d1 | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 912 | typedefString += "typedef struct objc_object "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 913 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 914 | typedefString += ";\n#endif\n"; |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 915 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 916 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 917 | // Replace the @class with typedefs corresponding to the classes. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 918 | ReplaceText(startLoc, semiPtr-startBuf+1, typedefString); |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 919 | } |
| 920 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 921 | void RewriteObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) { |
Fariborz Jahanian | da8ec2b | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 922 | // When method is a synthesized one, such as a getter/setter there is |
| 923 | // nothing to rewrite. |
| 924 | if (Method->isSynthesized()) |
| 925 | return; |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 926 | SourceLocation LocStart = Method->getLocStart(); |
| 927 | SourceLocation LocEnd = Method->getLocEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 928 | |
Chris Lattner | 88ea93e | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 929 | if (SM->getInstantiationLineNumber(LocEnd) > |
| 930 | SM->getInstantiationLineNumber(LocStart)) { |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 931 | InsertText(LocStart, "#if 0\n"); |
| 932 | ReplaceText(LocEnd, 1, ";\n#endif\n"); |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 933 | } else { |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 934 | InsertText(LocStart, "// "); |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 935 | } |
| 936 | } |
| 937 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 938 | void RewriteObjC::RewriteProperty(ObjCPropertyDecl *prop) { |
Fariborz Jahanian | da8ec2b | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 939 | SourceLocation Loc = prop->getAtLoc(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 940 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 941 | ReplaceText(Loc, 0, "// "); |
Steve Naroff | 0c0f5ba | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 942 | // FIXME: handle properties that are declared across multiple lines. |
Fariborz Jahanian | e8a3016 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 945 | void RewriteObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 946 | SourceLocation LocStart = CatDecl->getLocStart(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 947 | |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 948 | // FIXME: handle category headers that are declared across multiple lines. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 949 | ReplaceText(LocStart, 0, "// "); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 950 | |
Fariborz Jahanian | 68ebe63 | 2010-02-10 01:15:09 +0000 | [diff] [blame] | 951 | for (ObjCCategoryDecl::prop_iterator I = CatDecl->prop_begin(), |
| 952 | E = CatDecl->prop_end(); I != E; ++I) |
| 953 | RewriteProperty(*I); |
| 954 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 955 | for (ObjCCategoryDecl::instmeth_iterator |
| 956 | I = CatDecl->instmeth_begin(), E = CatDecl->instmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 957 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 958 | RewriteMethodDeclaration(*I); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 959 | for (ObjCCategoryDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 960 | I = CatDecl->classmeth_begin(), E = CatDecl->classmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 961 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 962 | RewriteMethodDeclaration(*I); |
| 963 | |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 964 | // Lastly, comment out the @end. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 965 | ReplaceText(CatDecl->getAtEndRange().getBegin(), 0, "// "); |
Steve Naroff | 5448cf6 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 966 | } |
| 967 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 968 | void RewriteObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
Fariborz Jahanian | fe38ba2 | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 969 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 970 | |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 971 | SourceLocation LocStart = PDecl->getLocStart(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 972 | |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 973 | // FIXME: handle protocol headers that are declared across multiple lines. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 974 | ReplaceText(LocStart, 0, "// "); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 975 | |
| 976 | for (ObjCProtocolDecl::instmeth_iterator |
| 977 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 978 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 979 | RewriteMethodDeclaration(*I); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 980 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 981 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 982 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 983 | RewriteMethodDeclaration(*I); |
| 984 | |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 985 | // Lastly, comment out the @end. |
Ted Kremenek | c7c6431 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 986 | SourceLocation LocEnd = PDecl->getAtEndRange().getBegin(); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 987 | ReplaceText(LocEnd, 0, "// "); |
Steve Naroff | a509f04 | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 988 | |
Fariborz Jahanian | fe38ba2 | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 989 | // Must comment out @optional/@required |
| 990 | const char *startBuf = SM->getCharacterData(LocStart); |
| 991 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 992 | for (const char *p = startBuf; p < endBuf; p++) { |
| 993 | if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { |
Steve Naroff | a509f04 | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 994 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 995 | ReplaceText(OptionalLoc, strlen("@optional"), "/* @optional */"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 996 | |
Fariborz Jahanian | fe38ba2 | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 997 | } |
| 998 | else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { |
Steve Naroff | a509f04 | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 999 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1000 | ReplaceText(OptionalLoc, strlen("@required"), "/* @required */"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1001 | |
Fariborz Jahanian | fe38ba2 | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 1002 | } |
| 1003 | } |
Steve Naroff | f921385f | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1006 | void RewriteObjC::RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *PDecl) { |
Fariborz Jahanian | da6165c | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 1007 | SourceLocation LocStart = PDecl->getLocation(); |
Steve Naroff | c17b056 | 2007-11-14 03:37:28 +0000 | [diff] [blame] | 1008 | if (LocStart.isInvalid()) |
| 1009 | assert(false && "Invalid SourceLocation"); |
Fariborz Jahanian | da6165c | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 1010 | // FIXME: handle forward protocol that are declared across multiple lines. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1011 | ReplaceText(LocStart, 0, "// "); |
Fariborz Jahanian | da6165c | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1014 | void RewriteObjC::RewriteTypeIntoString(QualType T, std::string &ResultStr, |
| 1015 | const FunctionType *&FPRetType) { |
| 1016 | if (T->isObjCQualifiedIdType()) |
Fariborz Jahanian | 24cb52c | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1017 | ResultStr += "id"; |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1018 | else if (T->isFunctionPointerType() || |
| 1019 | T->isBlockPointerType()) { |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1020 | // needs special handling, since pointer-to-functions have special |
| 1021 | // syntax (where a decaration models use). |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1022 | QualType retType = T; |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1023 | QualType PointeeTy; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1024 | if (const PointerType* PT = retType->getAs<PointerType>()) |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1025 | PointeeTy = PT->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1026 | else if (const BlockPointerType *BPT = retType->getAs<BlockPointerType>()) |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1027 | PointeeTy = BPT->getPointeeType(); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1028 | if ((FPRetType = PointeeTy->getAs<FunctionType>())) { |
Steve Naroff | 1fa7bd1 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 1029 | ResultStr += FPRetType->getResultType().getAsString(); |
| 1030 | ResultStr += "(*"; |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1031 | } |
| 1032 | } else |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 1033 | ResultStr += T.getAsString(); |
| 1034 | } |
| 1035 | |
| 1036 | void RewriteObjC::RewriteObjCMethodDecl(ObjCMethodDecl *OMD, |
| 1037 | std::string &ResultStr) { |
| 1038 | //fprintf(stderr,"In RewriteObjCMethodDecl\n"); |
| 1039 | const FunctionType *FPRetType = 0; |
| 1040 | ResultStr += "\nstatic "; |
| 1041 | RewriteTypeIntoString(OMD->getResultType(), ResultStr, FPRetType); |
Fariborz Jahanian | 7262fca | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 1042 | ResultStr += " "; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1043 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1044 | // Unique method name |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1045 | std::string NameStr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1046 | |
Douglas Gregor | ffca3a2 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1047 | if (OMD->isInstanceMethod()) |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1048 | NameStr += "_I_"; |
| 1049 | else |
| 1050 | NameStr += "_C_"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1051 | |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1052 | NameStr += OMD->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1053 | NameStr += "_"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1054 | |
| 1055 | if (ObjCCategoryImplDecl *CID = |
Steve Naroff | 11b387f | 2009-01-08 19:41:02 +0000 | [diff] [blame] | 1056 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1057 | NameStr += CID->getNameAsString(); |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1058 | NameStr += "_"; |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1059 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1060 | // Append selector names, replacing ':' with '_' |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1061 | { |
| 1062 | std::string selString = OMD->getSelector().getAsString(); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1063 | int len = selString.size(); |
| 1064 | for (int i = 0; i < len; i++) |
| 1065 | if (selString[i] == ':') |
| 1066 | selString[i] = '_'; |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1067 | NameStr += selString; |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1068 | } |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1069 | // Remember this name for metadata emission |
| 1070 | MethodInternalNames[OMD] = NameStr; |
| 1071 | ResultStr += NameStr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1072 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1073 | // Rewrite arguments |
| 1074 | ResultStr += "("; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1075 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1076 | // invisible arguments |
Douglas Gregor | ffca3a2 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1077 | if (OMD->isInstanceMethod()) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1078 | QualType selfTy = Context->getObjCInterfaceType(OMD->getClassInterface()); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1079 | selfTy = Context->getPointerType(selfTy); |
Steve Naroff | dc5b6b2 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 1080 | if (!LangOpts.Microsoft) { |
| 1081 | if (ObjCSynthesizedStructs.count(OMD->getClassInterface())) |
| 1082 | ResultStr += "struct "; |
| 1083 | } |
| 1084 | // When rewriting for Microsoft, explicitly omit the structure name. |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1085 | ResultStr += OMD->getClassInterface()->getNameAsString(); |
Steve Naroff | a1e115e | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1086 | ResultStr += " *"; |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1087 | } |
| 1088 | else |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 1089 | ResultStr += Context->getObjCClassType().getAsString(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1090 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1091 | ResultStr += " self, "; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1092 | ResultStr += Context->getObjCSelType().getAsString(); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1093 | ResultStr += " _cmd"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1094 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1095 | // Method arguments. |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1096 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 1097 | E = OMD->param_end(); PI != E; ++PI) { |
| 1098 | ParmVarDecl *PDecl = *PI; |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1099 | ResultStr += ", "; |
Steve Naroff | dcdcdcd | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1100 | if (PDecl->getType()->isObjCQualifiedIdType()) { |
| 1101 | ResultStr += "id "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1102 | ResultStr += PDecl->getNameAsString(); |
Steve Naroff | dcdcdcd | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1103 | } else { |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1104 | std::string Name = PDecl->getNameAsString(); |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 1105 | if (isTopLevelBlockPointerType(PDecl->getType())) { |
Steve Naroff | 44df6a2 | 2008-10-30 14:45:29 +0000 | [diff] [blame] | 1106 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1107 | const BlockPointerType *BPT = PDecl->getType()->getAs<BlockPointerType>(); |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1108 | Context->getPointerType(BPT->getPointeeType()).getAsStringInternal(Name, |
| 1109 | Context->PrintingPolicy); |
Steve Naroff | 44df6a2 | 2008-10-30 14:45:29 +0000 | [diff] [blame] | 1110 | } else |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1111 | PDecl->getType().getAsStringInternal(Name, Context->PrintingPolicy); |
Steve Naroff | dcdcdcd | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 1112 | ResultStr += Name; |
| 1113 | } |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1114 | } |
Fariborz Jahanian | eab81cd | 2008-01-21 20:14:23 +0000 | [diff] [blame] | 1115 | if (OMD->isVariadic()) |
| 1116 | ResultStr += ", ..."; |
Fariborz Jahanian | 7262fca | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 1117 | ResultStr += ") "; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1118 | |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1119 | if (FPRetType) { |
| 1120 | ResultStr += ")"; // close the precedence "scope" for "*". |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1121 | |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1122 | // Now, emit the argument types (if any). |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1123 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) { |
Steve Naroff | b067bbd | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 1124 | ResultStr += "("; |
| 1125 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 1126 | if (i) ResultStr += ", "; |
| 1127 | std::string ParamStr = FT->getArgType(i).getAsString(); |
| 1128 | ResultStr += ParamStr; |
| 1129 | } |
| 1130 | if (FT->isVariadic()) { |
| 1131 | if (FT->getNumArgs()) ResultStr += ", "; |
| 1132 | ResultStr += "..."; |
| 1133 | } |
| 1134 | ResultStr += ")"; |
| 1135 | } else { |
| 1136 | ResultStr += "()"; |
| 1137 | } |
| 1138 | } |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1139 | } |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1140 | void RewriteObjC::RewriteImplementationDecl(Decl *OID) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1141 | ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID); |
| 1142 | ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1143 | |
Fariborz Jahanian | 02d964b | 2010-02-15 21:11:41 +0000 | [diff] [blame] | 1144 | InsertText(IMD ? IMD->getLocStart() : CID->getLocStart(), "// "); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1145 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1146 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1147 | I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(), |
| 1148 | E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1149 | I != E; ++I) { |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1150 | std::string ResultStr; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1151 | ObjCMethodDecl *OMD = *I; |
| 1152 | RewriteObjCMethodDecl(OMD, ResultStr); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1153 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1154 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1155 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1156 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1157 | const char *endBuf = SM->getCharacterData(LocEnd); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1158 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1159 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1160 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1161 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1162 | I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(), |
| 1163 | E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1164 | I != E; ++I) { |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1165 | std::string ResultStr; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1166 | ObjCMethodDecl *OMD = *I; |
| 1167 | RewriteObjCMethodDecl(OMD, ResultStr); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1168 | SourceLocation LocStart = OMD->getLocStart(); |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1169 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1170 | |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1171 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1172 | const char *endBuf = SM->getCharacterData(LocEnd); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1173 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1174 | } |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1175 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1176 | I = IMD ? IMD->propimpl_begin() : CID->propimpl_begin(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1177 | E = IMD ? IMD->propimpl_end() : CID->propimpl_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1178 | I != E; ++I) { |
Steve Naroff | c038b3a | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 1179 | RewritePropertyImplDecl(*I, IMD, CID); |
Steve Naroff | e1908e3 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1182 | InsertText(IMD ? IMD->getLocEnd() : CID->getLocEnd(), "// "); |
Fariborz Jahanian | 1e5f64e | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1185 | void RewriteObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { |
Steve Naroff | c548404 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 1186 | std::string ResultStr; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1187 | if (!ObjCForwardDecls.count(ClassDecl)) { |
Steve Naroff | 2f55b98 | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1188 | // we haven't seen a forward decl - generate a typedef. |
Steve Naroff | 03f2767 | 2007-11-14 23:02:56 +0000 | [diff] [blame] | 1189 | ResultStr = "#ifndef _REWRITER_typedef_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1190 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1191 | ResultStr += "\n"; |
| 1192 | ResultStr += "#define _REWRITER_typedef_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1193 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1194 | ResultStr += "\n"; |
Steve Naroff | a1e115e | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1195 | ResultStr += "typedef struct objc_object "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1196 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1197 | ResultStr += ";\n#endif\n"; |
Steve Naroff | 2f55b98 | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1198 | // Mark this typedef as having been generated. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1199 | ObjCForwardDecls.insert(ClassDecl); |
Steve Naroff | 2f55b98 | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1200 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1201 | SynthesizeObjCInternalStruct(ClassDecl, ResultStr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1202 | |
| 1203 | for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(), |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1204 | E = ClassDecl->prop_end(); I != E; ++I) |
Steve Naroff | 0c0f5ba | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 1205 | RewriteProperty(*I); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1206 | for (ObjCInterfaceDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1207 | I = ClassDecl->instmeth_begin(), E = ClassDecl->instmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1208 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1209 | RewriteMethodDeclaration(*I); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1210 | for (ObjCInterfaceDecl::classmeth_iterator |
| 1211 | I = ClassDecl->classmeth_begin(), E = ClassDecl->classmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1212 | I != E; ++I) |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1213 | RewriteMethodDeclaration(*I); |
| 1214 | |
Steve Naroff | 4cd61ac | 2007-10-30 03:43:13 +0000 | [diff] [blame] | 1215 | // Lastly, comment out the @end. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1216 | ReplaceText(ClassDecl->getAtEndRange().getBegin(), 0, "// "); |
Steve Naroff | 161a92b | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 1219 | Stmt *RewriteObjC::RewritePropertySetter(BinaryOperator *BinOp, Expr *newStmt, |
| 1220 | SourceRange SrcRange) { |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1221 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr. |
| 1222 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
| 1223 | ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS()); |
| 1224 | ObjCMessageExpr *MsgExpr; |
| 1225 | ObjCPropertyDecl *PDecl = PropRefExpr->getProperty(); |
| 1226 | llvm::SmallVector<Expr *, 1> ExprVec; |
| 1227 | ExprVec.push_back(newStmt); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1228 | |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1229 | Stmt *Receiver = PropRefExpr->getBase(); |
| 1230 | ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(Receiver); |
| 1231 | if (PRE && PropGetters[PRE]) { |
| 1232 | // This allows us to handle chain/nested property getters. |
| 1233 | Receiver = PropGetters[PRE]; |
| 1234 | } |
Ted Kremenek | 2c80930 | 2010-02-11 22:41:21 +0000 | [diff] [blame] | 1235 | MsgExpr = new (Context) ObjCMessageExpr(*Context, dyn_cast<Expr>(Receiver), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1236 | PDecl->getSetterName(), PDecl->getType(), |
| 1237 | PDecl->getSetterMethodDecl(), |
| 1238 | SourceLocation(), SourceLocation(), |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1239 | &ExprVec[0], 1); |
| 1240 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1241 | |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1242 | // Now do the actual rewrite. |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 1243 | ReplaceStmtWithRange(BinOp, ReplacingStmt, SrcRange); |
Steve Naroff | df70577 | 2008-12-10 14:53:27 +0000 | [diff] [blame] | 1244 | //delete BinOp; |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1245 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1246 | // to things that stay around. |
| 1247 | Context->Deallocate(MsgExpr); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1248 | return ReplacingStmt; |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1251 | Stmt *RewriteObjC::RewritePropertyGetter(ObjCPropertyRefExpr *PropRefExpr) { |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1252 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr. |
| 1253 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
| 1254 | ObjCMessageExpr *MsgExpr; |
| 1255 | ObjCPropertyDecl *PDecl = PropRefExpr->getProperty(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1256 | |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1257 | Stmt *Receiver = PropRefExpr->getBase(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1258 | |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1259 | ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(Receiver); |
| 1260 | if (PRE && PropGetters[PRE]) { |
| 1261 | // This allows us to handle chain/nested property getters. |
| 1262 | Receiver = PropGetters[PRE]; |
| 1263 | } |
Ted Kremenek | 2c80930 | 2010-02-11 22:41:21 +0000 | [diff] [blame] | 1264 | MsgExpr = new (Context) ObjCMessageExpr(*Context, dyn_cast<Expr>(Receiver), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1265 | PDecl->getGetterName(), PDecl->getType(), |
| 1266 | PDecl->getGetterMethodDecl(), |
| 1267 | SourceLocation(), SourceLocation(), |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1268 | 0, 0); |
| 1269 | |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1270 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1271 | |
| 1272 | if (!PropParentMap) |
| 1273 | PropParentMap = new ParentMap(CurrentBody); |
| 1274 | |
| 1275 | Stmt *Parent = PropParentMap->getParent(PropRefExpr); |
| 1276 | if (Parent && isa<ObjCPropertyRefExpr>(Parent)) { |
| 1277 | // We stash away the ReplacingStmt since actually doing the |
| 1278 | // replacement/rewrite won't work for nested getters (e.g. obj.p.i) |
| 1279 | PropGetters[PropRefExpr] = ReplacingStmt; |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1280 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1281 | // to things that stay around. |
| 1282 | Context->Deallocate(MsgExpr); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1283 | return PropRefExpr; // return the original... |
| 1284 | } else { |
| 1285 | ReplaceStmt(PropRefExpr, ReplacingStmt); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1286 | // delete PropRefExpr; elsewhere... |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1287 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1288 | // to things that stay around. |
| 1289 | Context->Deallocate(MsgExpr); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1290 | return ReplacingStmt; |
| 1291 | } |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1294 | Stmt *RewriteObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, |
Fariborz Jahanian | 80fadb5 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1295 | SourceLocation OrigStart, |
| 1296 | bool &replaced) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1297 | ObjCIvarDecl *D = IV->getDecl(); |
Fariborz Jahanian | 0f3aecf | 2010-01-07 18:18:32 +0000 | [diff] [blame] | 1298 | const Expr *BaseExpr = IV->getBase(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1299 | if (CurMethodDef) { |
Fariborz Jahanian | f9e8c2b | 2010-01-26 18:28:51 +0000 | [diff] [blame] | 1300 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1301 | ObjCInterfaceType *iFaceDecl = |
Fariborz Jahanian | 0f3aecf | 2010-01-07 18:18:32 +0000 | [diff] [blame] | 1302 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
Fariborz Jahanian | f0ed69c | 2010-01-26 20:37:44 +0000 | [diff] [blame] | 1303 | assert(iFaceDecl && "RewriteObjCIvarRefExpr - iFaceDecl is null"); |
Steve Naroff | b1c0237 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1304 | // lookup which class implements the instance variable. |
| 1305 | ObjCInterfaceDecl *clsDeclared = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1306 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1307 | clsDeclared); |
Steve Naroff | b1c0237 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1308 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1309 | |
Steve Naroff | b1c0237 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1310 | // Synthesize an explicit cast to gain access to the ivar. |
| 1311 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1312 | RecName += "_IMPL"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1313 | IdentifierInfo *II = &Context->Idents.get(RecName); |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1314 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1315 | SourceLocation(), II); |
Steve Naroff | b1c0237 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1316 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1317 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1318 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, |
| 1319 | CastExpr::CK_Unknown, |
| 1320 | IV->getBase()); |
Steve Naroff | b1c0237 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1321 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1322 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
| 1323 | IV->getBase()->getLocEnd(), |
| 1324 | castExpr); |
Fariborz Jahanian | 80fadb5 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1325 | replaced = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1326 | if (IV->isFreeIvar() && |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1327 | CurMethodDef->getClassInterface() == iFaceDecl->getDecl()) { |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1328 | MemberExpr *ME = new (Context) MemberExpr(PE, true, D, |
| 1329 | IV->getLocation(), |
| 1330 | D->getType()); |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1331 | // delete IV; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | b1c0237 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1332 | return ME; |
Steve Naroff | 05caa48 | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1333 | } |
Fariborz Jahanian | 8131081 | 2010-01-28 01:41:20 +0000 | [diff] [blame] | 1334 | // Get the new text |
Chris Lattner | 37f5b7d | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1335 | // Cannot delete IV->getBase(), since PE points to it. |
| 1336 | // Replace the old base with the cast. This is important when doing |
| 1337 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1338 | IV->setBase(PE); |
Chris Lattner | 37f5b7d | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1339 | return IV; |
Steve Naroff | 05caa48 | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1340 | } |
Steve Naroff | 24840f6 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1341 | } else { // we are outside a method. |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1342 | assert(!IV->isFreeIvar() && "Cannot have a free standing ivar outside a method"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1343 | |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1344 | // Explicit ivar refs need to have a cast inserted. |
| 1345 | // FIXME: consider sharing some of this code with the code above. |
Fariborz Jahanian | 12e2e86 | 2010-01-12 17:31:23 +0000 | [diff] [blame] | 1346 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
Fariborz Jahanian | 9146e44 | 2010-01-11 17:50:35 +0000 | [diff] [blame] | 1347 | ObjCInterfaceType *iFaceDecl = |
| 1348 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1349 | // lookup which class implements the instance variable. |
| 1350 | ObjCInterfaceDecl *clsDeclared = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1351 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1352 | clsDeclared); |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1353 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1354 | |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1355 | // Synthesize an explicit cast to gain access to the ivar. |
| 1356 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1357 | RecName += "_IMPL"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1358 | IdentifierInfo *II = &Context->Idents.get(RecName); |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1359 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1360 | SourceLocation(), II); |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1361 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1362 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1363 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, |
| 1364 | CastExpr::CK_Unknown, |
| 1365 | IV->getBase()); |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1366 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1367 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
Chris Lattner | 34873d2 | 2008-05-28 16:38:23 +0000 | [diff] [blame] | 1368 | IV->getBase()->getLocEnd(), castExpr); |
Fariborz Jahanian | 80fadb5 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1369 | replaced = true; |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1370 | // Cannot delete IV->getBase(), since PE points to it. |
| 1371 | // Replace the old base with the cast. This is important when doing |
| 1372 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1373 | IV->setBase(PE); |
Steve Naroff | 29ce4e5 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1374 | return IV; |
| 1375 | } |
Steve Naroff | 05caa48 | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1376 | } |
Steve Naroff | 24840f6 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1377 | return IV; |
Steve Naroff | f60782b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
Fariborz Jahanian | 80fadb5 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1380 | Stmt *RewriteObjC::RewriteObjCNestedIvarRefExpr(Stmt *S, bool &replaced) { |
| 1381 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1382 | CI != E; ++CI) { |
| 1383 | if (*CI) { |
| 1384 | Stmt *newStmt = RewriteObjCNestedIvarRefExpr(*CI, replaced); |
| 1385 | if (newStmt) |
| 1386 | *CI = newStmt; |
| 1387 | } |
| 1388 | } |
| 1389 | if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) { |
| 1390 | SourceRange OrigStmtRange = S->getSourceRange(); |
| 1391 | Stmt *newStmt = RewriteObjCIvarRefExpr(IvarRefExpr, OrigStmtRange.getBegin(), |
| 1392 | replaced); |
| 1393 | return newStmt; |
Fariborz Jahanian | 3143338 | 2010-02-05 17:48:10 +0000 | [diff] [blame] | 1394 | } |
| 1395 | if (ObjCMessageExpr *MsgRefExpr = dyn_cast<ObjCMessageExpr>(S)) { |
| 1396 | Stmt *newStmt = SynthMessageExpr(MsgRefExpr); |
| 1397 | return newStmt; |
| 1398 | } |
Fariborz Jahanian | 80fadb5 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 1399 | return S; |
| 1400 | } |
| 1401 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1402 | /// SynthCountByEnumWithState - To print: |
| 1403 | /// ((unsigned int (*) |
| 1404 | /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1405 | /// (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1406 | /// sel_registerName( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1407 | /// "countByEnumeratingWithState:objects:count:"), |
| 1408 | /// &enumState, |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1409 | /// (id *)items, (unsigned int)16) |
| 1410 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1411 | void RewriteObjC::SynthCountByEnumWithState(std::string &buf) { |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1412 | buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, " |
| 1413 | "id *, unsigned int))(void *)objc_msgSend)"; |
| 1414 | buf += "\n\t\t"; |
| 1415 | buf += "((id)l_collection,\n\t\t"; |
| 1416 | buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; |
| 1417 | buf += "\n\t\t"; |
| 1418 | buf += "&enumState, " |
| 1419 | "(id *)items, (unsigned int)16)"; |
| 1420 | } |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1421 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1422 | /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach |
| 1423 | /// statement to exit to its outer synthesized loop. |
| 1424 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1425 | Stmt *RewriteObjC::RewriteBreakStmt(BreakStmt *S) { |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1426 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1427 | return S; |
| 1428 | // replace break with goto __break_label |
| 1429 | std::string buf; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1430 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1431 | SourceLocation startLoc = S->getLocStart(); |
| 1432 | buf = "goto __break_label_"; |
| 1433 | buf += utostr(ObjCBcLabelNo.back()); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1434 | ReplaceText(startLoc, strlen("break"), buf); |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1435 | |
| 1436 | return 0; |
| 1437 | } |
| 1438 | |
| 1439 | /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach |
| 1440 | /// statement to continue with its inner synthesized loop. |
| 1441 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1442 | Stmt *RewriteObjC::RewriteContinueStmt(ContinueStmt *S) { |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1443 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1444 | return S; |
| 1445 | // replace continue with goto __continue_label |
| 1446 | std::string buf; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1447 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1448 | SourceLocation startLoc = S->getLocStart(); |
| 1449 | buf = "goto __continue_label_"; |
| 1450 | buf += utostr(ObjCBcLabelNo.back()); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1451 | ReplaceText(startLoc, strlen("continue"), buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1452 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1453 | return 0; |
| 1454 | } |
| 1455 | |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1456 | /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1457 | /// It rewrites: |
| 1458 | /// for ( type elem in collection) { stmts; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1459 | |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1460 | /// Into: |
| 1461 | /// { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1462 | /// type elem; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1463 | /// struct __objcFastEnumerationState enumState = { 0 }; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1464 | /// id items[16]; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1465 | /// id l_collection = (id)collection; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1466 | /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1467 | /// objects:items count:16]; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1468 | /// if (limit) { |
| 1469 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1470 | /// do { |
| 1471 | /// unsigned long counter = 0; |
| 1472 | /// do { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1473 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1474 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1475 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1476 | /// stmts; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1477 | /// __continue_label: ; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1478 | /// } while (counter < limit); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1479 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1480 | /// objects:items count:16]); |
| 1481 | /// elem = nil; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1482 | /// __break_label: ; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1483 | /// } |
| 1484 | /// else |
| 1485 | /// elem = nil; |
| 1486 | /// } |
| 1487 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1488 | Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
Chris Lattner | a779d69 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 1489 | SourceLocation OrigEnd) { |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1490 | assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1491 | assert(isa<ObjCForCollectionStmt>(Stmts.back()) && |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1492 | "ObjCForCollectionStmt Statement stack mismatch"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1493 | assert(!ObjCBcLabelNo.empty() && |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1494 | "ObjCForCollectionStmt - Label No stack empty"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1495 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1496 | SourceLocation startLoc = S->getLocStart(); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1497 | const char *startBuf = SM->getCharacterData(startLoc); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1498 | const char *elementName; |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1499 | std::string elementTypeAsString; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1500 | std::string buf; |
| 1501 | buf = "\n{\n\t"; |
| 1502 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { |
| 1503 | // type elem; |
Chris Lattner | 529efc7 | 2009-03-28 06:33:19 +0000 | [diff] [blame] | 1504 | NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl()); |
Ted Kremenek | 292b384 | 2008-10-06 22:16:13 +0000 | [diff] [blame] | 1505 | QualType ElementType = cast<ValueDecl>(D)->getType(); |
Steve Naroff | 3ce3af2 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1506 | if (ElementType->isObjCQualifiedIdType() || |
| 1507 | ElementType->isObjCQualifiedInterfaceType()) |
| 1508 | // Simply use 'id' for all qualified types. |
| 1509 | elementTypeAsString = "id"; |
| 1510 | else |
| 1511 | elementTypeAsString = ElementType.getAsString(); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1512 | buf += elementTypeAsString; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1513 | buf += " "; |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1514 | elementName = D->getNameAsCString(); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1515 | buf += elementName; |
| 1516 | buf += ";\n\t"; |
| 1517 | } |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1518 | else { |
| 1519 | DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1520 | elementName = DR->getDecl()->getNameAsCString(); |
Steve Naroff | 3ce3af2 | 2009-12-04 21:18:19 +0000 | [diff] [blame] | 1521 | ValueDecl *VD = cast<ValueDecl>(DR->getDecl()); |
| 1522 | if (VD->getType()->isObjCQualifiedIdType() || |
| 1523 | VD->getType()->isObjCQualifiedInterfaceType()) |
| 1524 | // Simply use 'id' for all qualified types. |
| 1525 | elementTypeAsString = "id"; |
| 1526 | else |
| 1527 | elementTypeAsString = VD->getType().getAsString(); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1528 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1529 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1530 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1531 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
| 1532 | // id items[16]; |
| 1533 | buf += "id items[16];\n\t"; |
| 1534 | // id l_collection = (id) |
| 1535 | buf += "id l_collection = (id)"; |
Fariborz Jahanian | 82ae015 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1536 | // Find start location of 'collection' the hard way! |
| 1537 | const char *startCollectionBuf = startBuf; |
| 1538 | startCollectionBuf += 3; // skip 'for' |
| 1539 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1540 | startCollectionBuf++; // skip '(' |
| 1541 | // find 'in' and skip it. |
| 1542 | while (*startCollectionBuf != ' ' || |
| 1543 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1544 | (*(startCollectionBuf+3) != ' ' && |
| 1545 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1546 | startCollectionBuf++; |
| 1547 | startCollectionBuf += 3; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1548 | |
| 1549 | // Replace: "for (type element in" with string constructed thus far. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1550 | ReplaceText(startLoc, startCollectionBuf - startBuf, buf); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1551 | // Replace ')' in for '(' type elem in collection ')' with ';' |
Fariborz Jahanian | 82ae015 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1552 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1553 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
| 1554 | SourceLocation lparenLoc = startLoc.getFileLocWithOffset(rparenBuf-startBuf); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1555 | buf = ";\n\t"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1556 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1557 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1558 | // objects:items count:16]; |
| 1559 | // which is synthesized into: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1560 | // unsigned int limit = |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1561 | // ((unsigned int (*) |
| 1562 | // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1563 | // (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1564 | // sel_registerName( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1565 | // "countByEnumeratingWithState:objects:count:"), |
| 1566 | // (struct __objcFastEnumerationState *)&state, |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1567 | // (id *)items, (unsigned int)16); |
| 1568 | buf += "unsigned long limit =\n\t\t"; |
| 1569 | SynthCountByEnumWithState(buf); |
| 1570 | buf += ";\n\t"; |
| 1571 | /// if (limit) { |
| 1572 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1573 | /// do { |
| 1574 | /// unsigned long counter = 0; |
| 1575 | /// do { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1576 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1577 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1578 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1579 | buf += "if (limit) {\n\t"; |
| 1580 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1581 | buf += "do {\n\t\t"; |
| 1582 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1583 | buf += "do {\n\t\t\t"; |
| 1584 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1585 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1586 | buf += elementName; |
Fariborz Jahanian | 6fa7516 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1587 | buf += " = ("; |
| 1588 | buf += elementTypeAsString; |
| 1589 | buf += ")enumState.itemsPtr[counter++];"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1590 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1591 | ReplaceText(lparenLoc, 1, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1592 | |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1593 | /// __continue_label: ; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1594 | /// } while (counter < limit); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1595 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1596 | /// objects:items count:16]); |
| 1597 | /// elem = nil; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1598 | /// __break_label: ; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1599 | /// } |
| 1600 | /// else |
| 1601 | /// elem = nil; |
| 1602 | /// } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1603 | /// |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1604 | buf = ";\n\t"; |
| 1605 | buf += "__continue_label_"; |
| 1606 | buf += utostr(ObjCBcLabelNo.back()); |
| 1607 | buf += ": ;"; |
| 1608 | buf += "\n\t\t"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1609 | buf += "} while (counter < limit);\n\t"; |
| 1610 | buf += "} while (limit = "; |
| 1611 | SynthCountByEnumWithState(buf); |
| 1612 | buf += ");\n\t"; |
| 1613 | buf += elementName; |
Fariborz Jahanian | 39d7094 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1614 | buf += " = (("; |
| 1615 | buf += elementTypeAsString; |
| 1616 | buf += ")0);\n\t"; |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1617 | buf += "__break_label_"; |
| 1618 | buf += utostr(ObjCBcLabelNo.back()); |
| 1619 | buf += ": ;\n\t"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1620 | buf += "}\n\t"; |
| 1621 | buf += "else\n\t\t"; |
| 1622 | buf += elementName; |
Fariborz Jahanian | 39d7094 | 2010-01-08 01:29:44 +0000 | [diff] [blame] | 1623 | buf += " = (("; |
| 1624 | buf += elementTypeAsString; |
| 1625 | buf += ")0);\n\t"; |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1626 | buf += "}\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1627 | |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1628 | // Insert all these *after* the statement body. |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1629 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Steve Naroff | f0ff879 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1630 | if (isa<CompoundStmt>(S->getBody())) { |
| 1631 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1632 | InsertText(endBodyLoc, buf); |
Steve Naroff | f0ff879 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1633 | } else { |
| 1634 | /* Need to treat single statements specially. For example: |
| 1635 | * |
| 1636 | * for (A *a in b) if (stuff()) break; |
| 1637 | * for (A *a in b) xxxyy; |
| 1638 | * |
| 1639 | * The following code simply scans ahead to the semi to find the actual end. |
| 1640 | */ |
| 1641 | const char *stmtBuf = SM->getCharacterData(OrigEnd); |
| 1642 | const char *semiBuf = strchr(stmtBuf, ';'); |
| 1643 | assert(semiBuf && "Can't find ';'"); |
| 1644 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(semiBuf-stmtBuf+1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1645 | InsertText(endBodyLoc, buf); |
Steve Naroff | f0ff879 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1646 | } |
Fariborz Jahanian | b860cbf | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1647 | Stmts.pop_back(); |
| 1648 | ObjCBcLabelNo.pop_back(); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1649 | return 0; |
Fariborz Jahanian | dc917b9 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1652 | /// RewriteObjCSynchronizedStmt - |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1653 | /// This routine rewrites @synchronized(expr) stmt; |
| 1654 | /// into: |
| 1655 | /// objc_sync_enter(expr); |
| 1656 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1657 | /// |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1658 | Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1659 | // Get the start location and compute the semi location. |
| 1660 | SourceLocation startLoc = S->getLocStart(); |
| 1661 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1662 | |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1663 | assert((*startBuf == '@') && "bogus @synchronized location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1664 | |
| 1665 | std::string buf; |
Steve Naroff | b2fc052 | 2008-08-21 13:03:03 +0000 | [diff] [blame] | 1666 | buf = "objc_sync_enter((id)"; |
| 1667 | const char *lparenBuf = startBuf; |
| 1668 | while (*lparenBuf != '(') lparenBuf++; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1669 | ReplaceText(startLoc, lparenBuf-startBuf+1, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1670 | // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since |
| 1671 | // the sync expression is typically a message expression that's already |
Steve Naroff | ad7013b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1672 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1673 | SourceLocation endLoc = S->getSynchBody()->getLocStart(); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1674 | const char *endBuf = SM->getCharacterData(endLoc); |
Steve Naroff | ad7013b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1675 | while (*endBuf != ')') endBuf--; |
| 1676 | SourceLocation rparenLoc = startLoc.getFileLocWithOffset(endBuf-startBuf); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1677 | buf = ");\n"; |
| 1678 | // declare a new scope with two variables, _stack and _rethrow. |
| 1679 | buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n"; |
| 1680 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1681 | buf += "char *pointers[4];} _stack;\n"; |
| 1682 | buf += "id volatile _rethrow = 0;\n"; |
| 1683 | buf += "objc_exception_try_enter(&_stack);\n"; |
| 1684 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1685 | ReplaceText(rparenLoc, 1, buf); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1686 | startLoc = S->getSynchBody()->getLocEnd(); |
| 1687 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1688 | |
Steve Naroff | ad7013b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1689 | assert((*startBuf == '}') && "bogus @synchronized block"); |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1690 | SourceLocation lastCurlyLoc = startLoc; |
| 1691 | buf = "}\nelse {\n"; |
| 1692 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 1693 | buf += "}\n"; |
| 1694 | buf += "{ /* implicit finally clause */\n"; |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1695 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1696 | |
| 1697 | std::string syncBuf; |
| 1698 | syncBuf += " objc_sync_exit("; |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1699 | Expr *syncExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 1700 | CastExpr::CK_Unknown, |
| 1701 | S->getSynchExpr()); |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1702 | std::string syncExprBufS; |
| 1703 | llvm::raw_string_ostream syncExprBuf(syncExprBufS); |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1704 | syncExpr->printPretty(syncExprBuf, *Context, 0, |
| 1705 | PrintingPolicy(LangOpts)); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1706 | syncBuf += syncExprBuf.str(); |
| 1707 | syncBuf += ");"; |
| 1708 | |
| 1709 | buf += syncBuf; |
| 1710 | buf += "\n if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1711 | buf += "}\n"; |
| 1712 | buf += "}"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1713 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1714 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1715 | |
| 1716 | bool hasReturns = false; |
| 1717 | HasReturnStmts(S->getSynchBody(), hasReturns); |
| 1718 | if (hasReturns) |
| 1719 | RewriteSyncReturnStmts(S->getSynchBody(), syncBuf); |
| 1720 | |
Fariborz Jahanian | 284011b | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1721 | return 0; |
| 1722 | } |
| 1723 | |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1724 | void RewriteObjC::WarnAboutReturnGotoStmts(Stmt *S) |
| 1725 | { |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1726 | // Perform a bottom up traversal of all children. |
| 1727 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1728 | CI != E; ++CI) |
| 1729 | if (*CI) |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1730 | WarnAboutReturnGotoStmts(*CI); |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1731 | |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1732 | if (isa<ReturnStmt>(S) || isa<GotoStmt>(S)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1733 | Diags.Report(Context->getFullLoc(S->getLocStart()), |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1734 | TryFinallyContainsReturnDiag); |
| 1735 | } |
| 1736 | return; |
| 1737 | } |
| 1738 | |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1739 | void RewriteObjC::HasReturnStmts(Stmt *S, bool &hasReturns) |
| 1740 | { |
| 1741 | // Perform a bottom up traversal of all children. |
| 1742 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1743 | CI != E; ++CI) |
| 1744 | if (*CI) |
| 1745 | HasReturnStmts(*CI, hasReturns); |
| 1746 | |
| 1747 | if (isa<ReturnStmt>(S)) |
| 1748 | hasReturns = true; |
| 1749 | return; |
| 1750 | } |
| 1751 | |
| 1752 | void RewriteObjC::RewriteTryReturnStmts(Stmt *S) { |
| 1753 | // Perform a bottom up traversal of all children. |
| 1754 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1755 | CI != E; ++CI) |
| 1756 | if (*CI) { |
| 1757 | RewriteTryReturnStmts(*CI); |
| 1758 | } |
| 1759 | if (isa<ReturnStmt>(S)) { |
| 1760 | SourceLocation startLoc = S->getLocStart(); |
| 1761 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1762 | |
| 1763 | const char *semiBuf = strchr(startBuf, ';'); |
| 1764 | assert((*semiBuf == ';') && "RewriteTryReturnStmts: can't find ';'"); |
| 1765 | SourceLocation onePastSemiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
| 1766 | |
| 1767 | std::string buf; |
| 1768 | buf = "{ objc_exception_try_exit(&_stack); return"; |
| 1769 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1770 | ReplaceText(startLoc, 6, buf); |
| 1771 | InsertText(onePastSemiLoc, "}"); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1772 | } |
| 1773 | return; |
| 1774 | } |
| 1775 | |
| 1776 | void RewriteObjC::RewriteSyncReturnStmts(Stmt *S, std::string syncExitBuf) { |
| 1777 | // Perform a bottom up traversal of all children. |
| 1778 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1779 | CI != E; ++CI) |
| 1780 | if (*CI) { |
| 1781 | RewriteSyncReturnStmts(*CI, syncExitBuf); |
| 1782 | } |
| 1783 | if (isa<ReturnStmt>(S)) { |
| 1784 | SourceLocation startLoc = S->getLocStart(); |
| 1785 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1786 | |
| 1787 | const char *semiBuf = strchr(startBuf, ';'); |
| 1788 | assert((*semiBuf == ';') && "RewriteSyncReturnStmts: can't find ';'"); |
| 1789 | SourceLocation onePastSemiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
| 1790 | |
| 1791 | std::string buf; |
| 1792 | buf = "{ objc_exception_try_exit(&_stack);"; |
| 1793 | buf += syncExitBuf; |
| 1794 | buf += " return"; |
| 1795 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1796 | ReplaceText(startLoc, 6, buf); |
| 1797 | InsertText(onePastSemiLoc, "}"); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1798 | } |
| 1799 | return; |
| 1800 | } |
| 1801 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1802 | Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1803 | // Get the start location and compute the semi location. |
| 1804 | SourceLocation startLoc = S->getLocStart(); |
| 1805 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1806 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1807 | assert((*startBuf == '@') && "bogus @try location"); |
| 1808 | |
| 1809 | std::string buf; |
| 1810 | // declare a new scope with two variables, _stack and _rethrow. |
| 1811 | buf = "/* @try scope begin */ { struct _objc_exception_data {\n"; |
| 1812 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1813 | buf += "char *pointers[4];} _stack;\n"; |
| 1814 | buf += "id volatile _rethrow = 0;\n"; |
| 1815 | buf += "objc_exception_try_enter(&_stack);\n"; |
Steve Naroff | 1601858 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1816 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1817 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1818 | ReplaceText(startLoc, 4, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1819 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1820 | startLoc = S->getTryBody()->getLocEnd(); |
| 1821 | startBuf = SM->getCharacterData(startLoc); |
| 1822 | |
| 1823 | assert((*startBuf == '}') && "bogus @try block"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1824 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1825 | SourceLocation lastCurlyLoc = startLoc; |
Steve Naroff | ce2dca1 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1826 | ObjCAtCatchStmt *catchList = S->getCatchStmts(); |
| 1827 | if (catchList) { |
| 1828 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1829 | buf = " /* @catch begin */ else {\n"; |
| 1830 | buf += " id _caught = objc_exception_extract(&_stack);\n"; |
| 1831 | buf += " objc_exception_try_enter (&_stack);\n"; |
| 1832 | buf += " if (_setjmp(_stack.buf))\n"; |
| 1833 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1834 | buf += " else { /* @catch continue */"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1835 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1836 | InsertText(startLoc, buf); |
Steve Naroff | fac18fe | 2008-09-09 19:59:12 +0000 | [diff] [blame] | 1837 | } else { /* no catch list */ |
| 1838 | buf = "}\nelse {\n"; |
| 1839 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1840 | buf += "}"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1841 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | ce2dca1 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1842 | } |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1843 | bool sawIdTypedCatch = false; |
| 1844 | Stmt *lastCatchBody = 0; |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1845 | while (catchList) { |
Steve Naroff | 371b8fb | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1846 | ParmVarDecl *catchDecl = catchList->getCatchParamDecl(); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1847 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1848 | if (catchList == S->getCatchStmts()) |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1849 | buf = "if ("; // we are generating code for the first catch clause |
| 1850 | else |
| 1851 | buf = "else if ("; |
| 1852 | startLoc = catchList->getLocStart(); |
| 1853 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1854 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1855 | assert((*startBuf == '@') && "bogus @catch location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1856 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1857 | const char *lParenLoc = strchr(startBuf, '('); |
| 1858 | |
Steve Naroff | e6b7ffd | 2008-02-01 22:08:12 +0000 | [diff] [blame] | 1859 | if (catchList->hasEllipsis()) { |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1860 | // Now rewrite the body... |
| 1861 | lastCatchBody = catchList->getCatchBody(); |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1862 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1863 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1864 | assert(*SM->getCharacterData(catchList->getRParenLoc()) == ')' && |
| 1865 | "bogus @catch paren location"); |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1866 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1867 | |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1868 | buf += "1) { id _tmp = _caught;"; |
Daniel Dunbar | dec484a | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 1869 | Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, buf); |
Steve Naroff | 371b8fb | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1870 | } else if (catchDecl) { |
| 1871 | QualType t = catchDecl->getType(); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1872 | if (t == Context->getObjCIdType()) { |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1873 | buf += "1) { "; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1874 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1875 | sawIdTypedCatch = true; |
Fariborz Jahanian | 5951609 | 2010-01-12 01:22:23 +0000 | [diff] [blame] | 1876 | } else if (t->isObjCObjectPointerType()) { |
| 1877 | QualType InterfaceTy = t->getPointeeType(); |
| 1878 | const ObjCInterfaceType *cls = // Should be a pointer to a class. |
| 1879 | InterfaceTy->getAs<ObjCInterfaceType>(); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1880 | if (cls) { |
Steve Naroff | 1601858 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1881 | buf += "objc_exception_match((struct objc_class *)objc_getClass(\""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1882 | buf += cls->getDecl()->getNameAsString(); |
Steve Naroff | 1601858 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1883 | buf += "\"), (struct objc_object *)_caught)) { "; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1884 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1885 | } |
| 1886 | } |
| 1887 | // Now rewrite the body... |
| 1888 | lastCatchBody = catchList->getCatchBody(); |
| 1889 | SourceLocation rParenLoc = catchList->getRParenLoc(); |
| 1890 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1891 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 1892 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 1893 | assert((*rParenBuf == ')') && "bogus @catch paren location"); |
| 1894 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1895 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1896 | // Here we replace ") {" with "= _caught;" (which initializes and |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1897 | // declares the @catch parameter). |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1898 | ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, " = _caught;"); |
Steve Naroff | 371b8fb | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1899 | } else { |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1900 | assert(false && "@catch rewrite bug"); |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1901 | } |
Steve Naroff | edb5bc6 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1902 | // make sure all the catch bodies get rewritten! |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1903 | catchList = catchList->getNextCatchStmt(); |
| 1904 | } |
| 1905 | // Complete the catch list... |
| 1906 | if (lastCatchBody) { |
| 1907 | SourceLocation bodyLoc = lastCatchBody->getLocEnd(); |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1908 | assert(*SM->getCharacterData(bodyLoc) == '}' && |
| 1909 | "bogus @catch body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1910 | |
Steve Naroff | 4adbe31 | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1911 | // Insert the last (implicit) else clause *before* the right curly brace. |
| 1912 | bodyLoc = bodyLoc.getFileLocWithOffset(-1); |
| 1913 | buf = "} /* last catch end */\n"; |
| 1914 | buf += "else {\n"; |
| 1915 | buf += " _rethrow = _caught;\n"; |
| 1916 | buf += " objc_exception_try_exit(&_stack);\n"; |
| 1917 | buf += "} } /* @catch end */\n"; |
| 1918 | if (!S->getFinallyStmt()) |
| 1919 | buf += "}\n"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1920 | InsertText(bodyLoc, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1921 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1922 | // Set lastCurlyLoc |
| 1923 | lastCurlyLoc = lastCatchBody->getLocEnd(); |
| 1924 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1925 | if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) { |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1926 | startLoc = finalStmt->getLocStart(); |
| 1927 | startBuf = SM->getCharacterData(startLoc); |
| 1928 | assert((*startBuf == '@') && "bogus @finally start"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1929 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1930 | ReplaceText(startLoc, 8, "/* @finally */"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1931 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1932 | Stmt *body = finalStmt->getFinallyBody(); |
| 1933 | SourceLocation startLoc = body->getLocStart(); |
| 1934 | SourceLocation endLoc = body->getLocEnd(); |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1935 | assert(*SM->getCharacterData(startLoc) == '{' && |
| 1936 | "bogus @finally body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1937 | assert(*SM->getCharacterData(endLoc) == '}' && |
Chris Lattner | 4fdfbf7 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1938 | "bogus @finally body location"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1939 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1940 | startLoc = startLoc.getFileLocWithOffset(1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1941 | InsertText(startLoc, " if (!_rethrow) objc_exception_try_exit(&_stack);\n"); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1942 | endLoc = endLoc.getFileLocWithOffset(-1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1943 | InsertText(endLoc, " if (_rethrow) objc_exception_throw(_rethrow);\n"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1944 | |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1945 | // Set lastCurlyLoc |
| 1946 | lastCurlyLoc = body->getLocEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1947 | |
Steve Naroff | 6d6da25 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1948 | // Now check for any return/continue/go statements within the @try. |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1949 | WarnAboutReturnGotoStmts(S->getTryBody()); |
Steve Naroff | 4adbe31 | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1950 | } else { /* no finally clause - make sure we synthesize an implicit one */ |
| 1951 | buf = "{ /* implicit finally clause */\n"; |
| 1952 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
| 1953 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 1954 | buf += "}"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1955 | ReplaceText(lastCurlyLoc, 1, buf); |
Steve Naroff | ec60b43 | 2009-12-05 21:43:12 +0000 | [diff] [blame] | 1956 | |
| 1957 | // Now check for any return/continue/go statements within the @try. |
| 1958 | // The implicit finally clause won't called if the @try contains any |
| 1959 | // jump statements. |
| 1960 | bool hasReturns = false; |
| 1961 | HasReturnStmts(S->getTryBody(), hasReturns); |
| 1962 | if (hasReturns) |
| 1963 | RewriteTryReturnStmts(S->getTryBody()); |
Steve Naroff | bf478ec | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1964 | } |
| 1965 | // Now emit the final closing curly brace... |
| 1966 | lastCurlyLoc = lastCurlyLoc.getFileLocWithOffset(1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1967 | InsertText(lastCurlyLoc, " } /* @try scope end */\n"); |
Fariborz Jahanian | 63ac80e | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1968 | return 0; |
| 1969 | } |
| 1970 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1971 | Stmt *RewriteObjC::RewriteObjCCatchStmt(ObjCAtCatchStmt *S) { |
Fariborz Jahanian | 63ac80e | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1972 | return 0; |
| 1973 | } |
| 1974 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1975 | Stmt *RewriteObjC::RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S) { |
Fariborz Jahanian | 63ac80e | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1976 | return 0; |
| 1977 | } |
| 1978 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1979 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
| 1980 | // the throw expression is typically a message expression that's already |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1981 | // been rewritten! (which implies the SourceLocation's are invalid). |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1982 | Stmt *RewriteObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1983 | // Get the start location and compute the semi location. |
| 1984 | SourceLocation startLoc = S->getLocStart(); |
| 1985 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1986 | |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1987 | assert((*startBuf == '@') && "bogus @throw location"); |
| 1988 | |
| 1989 | std::string buf; |
| 1990 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
Steve Naroff | c7d2df2 | 2008-01-19 00:42:38 +0000 | [diff] [blame] | 1991 | if (S->getThrowExpr()) |
| 1992 | buf = "objc_exception_throw("; |
| 1993 | else // add an implicit argument |
| 1994 | buf = "objc_exception_throw(_caught"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1995 | |
Steve Naroff | 2978834 | 2008-07-25 15:41:30 +0000 | [diff] [blame] | 1996 | // handle "@ throw" correctly. |
| 1997 | const char *wBuf = strchr(startBuf, 'w'); |
| 1998 | assert((*wBuf == 'w') && "@throw: can't find 'w'"); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 1999 | ReplaceText(startLoc, wBuf-startBuf+1, buf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2000 | |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2001 | const char *semiBuf = strchr(startBuf, ';'); |
| 2002 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
| 2003 | SourceLocation semiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2004 | ReplaceText(semiLoc, 1, ");"); |
Steve Naroff | a733c7f | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 2005 | return 0; |
| 2006 | } |
Fariborz Jahanian | 63ac80e | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 2007 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2008 | Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
Chris Lattner | c6d91c0 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 2009 | // Create a new string expression. |
| 2010 | QualType StrType = Context->getPointerType(Context->CharTy); |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2011 | std::string StrEncoding; |
Daniel Dunbar | fc1066d | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 2012 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); |
Chris Lattner | f83b5af | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2013 | Expr *Replacement = StringLiteral::Create(*Context,StrEncoding.c_str(), |
| 2014 | StrEncoding.length(), false,StrType, |
| 2015 | SourceLocation()); |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2016 | ReplaceStmt(Exp, Replacement); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2017 | |
Chris Lattner | 4431a1b | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 2018 | // Replace this subexpr in the parent. |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 2019 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Chris Lattner | 6953469 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 2020 | return Replacement; |
Chris Lattner | a7c19fe | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 2021 | } |
| 2022 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2023 | Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
Steve Naroff | 2654e18 | 2008-12-22 22:16:07 +0000 | [diff] [blame] | 2024 | if (!SelGetUidFunctionDecl) |
| 2025 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | e4f9b23 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2026 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 2027 | // Create a call to sel_registerName("selName"). |
| 2028 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 2029 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2030 | SelExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 6b7ecf6 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2031 | Exp->getSelector().getAsString().c_str(), |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2032 | Exp->getSelector().getAsString().size(), |
Chris Lattner | 630970d | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 2033 | false, argType, SourceLocation())); |
Steve Naroff | e4f9b23 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2034 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2035 | &SelExprs[0], SelExprs.size()); |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2036 | ReplaceStmt(Exp, SelExp); |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 2037 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | e4f9b23 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 2038 | return SelExp; |
| 2039 | } |
| 2040 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2041 | CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl( |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2042 | FunctionDecl *FD, Expr **args, unsigned nargs, SourceLocation StartLoc, |
| 2043 | SourceLocation EndLoc) { |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2044 | // Get the type, we will need to reference it in a couple spots. |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2045 | QualType msgSendType = FD->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2046 | |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2047 | // Create a reference to the objc_msgSend() declaration. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2048 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, msgSendType, SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2049 | |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2050 | // Now, we cast the reference to a pointer to the objc_msgSend type. |
Chris Lattner | 3c799d7 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 2051 | QualType pToFunc = Context->getPointerType(msgSendType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2052 | ImplicitCastExpr *ICE = new (Context) ImplicitCastExpr(pToFunc, |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2053 | CastExpr::CK_Unknown, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2054 | DRE, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2055 | /*isLvalue=*/false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2056 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2057 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2058 | |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2059 | CallExpr *Exp = |
| 2060 | new (Context) CallExpr(*Context, ICE, args, nargs, FT->getResultType(), |
| 2061 | EndLoc); |
| 2062 | return Exp; |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2063 | } |
| 2064 | |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2065 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 2066 | const char *&startRef, const char *&endRef) { |
| 2067 | while (startBuf < endBuf) { |
| 2068 | if (*startBuf == '<') |
| 2069 | startRef = startBuf; // mark the start. |
| 2070 | if (*startBuf == '>') { |
Steve Naroff | 1b23213 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 2071 | if (startRef && *startRef == '<') { |
| 2072 | endRef = startBuf; // mark the end. |
| 2073 | return true; |
| 2074 | } |
| 2075 | return false; |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2076 | } |
| 2077 | startBuf++; |
| 2078 | } |
| 2079 | return false; |
| 2080 | } |
| 2081 | |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2082 | static void scanToNextArgument(const char *&argRef) { |
| 2083 | int angle = 0; |
| 2084 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 2085 | if (*argRef == '<') |
| 2086 | angle++; |
| 2087 | else if (*argRef == '>') |
| 2088 | angle--; |
| 2089 | argRef++; |
| 2090 | } |
| 2091 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 2092 | } |
Fariborz Jahanian | 16e703a | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2093 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2094 | bool RewriteObjC::needToScanForQualifiers(QualType T) { |
Fariborz Jahanian | 80c54b0 | 2010-02-03 21:29:28 +0000 | [diff] [blame] | 2095 | if (T->isObjCQualifiedIdType()) |
| 2096 | return true; |
Fariborz Jahanian | 06769f9 | 2010-02-02 18:35:07 +0000 | [diff] [blame] | 2097 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 2098 | if (PT->getPointeeType()->isObjCQualifiedIdType()) |
| 2099 | return true; |
| 2100 | } |
| 2101 | if (T->isObjCObjectPointerType()) { |
| 2102 | T = T->getPointeeType(); |
| 2103 | return T->isObjCQualifiedInterfaceType(); |
| 2104 | } |
| 2105 | return false; |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2106 | } |
| 2107 | |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2108 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) { |
| 2109 | QualType Type = E->getType(); |
| 2110 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | dbfc693 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2111 | SourceLocation Loc, EndLoc; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2112 | |
Steve Naroff | dbfc693 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2113 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) { |
| 2114 | Loc = ECE->getLParenLoc(); |
| 2115 | EndLoc = ECE->getRParenLoc(); |
| 2116 | } else { |
| 2117 | Loc = E->getLocStart(); |
| 2118 | EndLoc = E->getLocEnd(); |
| 2119 | } |
| 2120 | // This will defend against trying to rewrite synthesized expressions. |
| 2121 | if (Loc.isInvalid() || EndLoc.isInvalid()) |
| 2122 | return; |
| 2123 | |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2124 | const char *startBuf = SM->getCharacterData(Loc); |
Steve Naroff | dbfc693 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 2125 | const char *endBuf = SM->getCharacterData(EndLoc); |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2126 | const char *startRef = 0, *endRef = 0; |
| 2127 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2128 | // Get the locations of the startRef, endRef. |
| 2129 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-startBuf); |
| 2130 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-startBuf+1); |
| 2131 | // Comment out the protocol references. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2132 | InsertText(LessLoc, "/*"); |
| 2133 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 873bd84 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 2134 | } |
| 2135 | } |
| 2136 | } |
| 2137 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2138 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2139 | SourceLocation Loc; |
| 2140 | QualType Type; |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2141 | const FunctionProtoType *proto = 0; |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2142 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 2143 | Loc = VD->getLocation(); |
| 2144 | Type = VD->getType(); |
| 2145 | } |
| 2146 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 2147 | Loc = FD->getLocation(); |
| 2148 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 2149 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2150 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2151 | assert(funcType && "missing function type"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2152 | proto = dyn_cast<FunctionProtoType>(funcType); |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2153 | if (!proto) |
| 2154 | return; |
| 2155 | Type = proto->getResultType(); |
| 2156 | } |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 2157 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(Dcl)) { |
| 2158 | Loc = FD->getLocation(); |
| 2159 | Type = FD->getType(); |
| 2160 | } |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2161 | else |
| 2162 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2163 | |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2164 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2165 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2166 | |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2167 | const char *endBuf = SM->getCharacterData(Loc); |
| 2168 | const char *startBuf = endBuf; |
Steve Naroff | 930e099 | 2008-05-31 05:02:17 +0000 | [diff] [blame] | 2169 | while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2170 | startBuf--; // scan backward (from the decl location) for return type. |
| 2171 | const char *startRef = 0, *endRef = 0; |
| 2172 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2173 | // Get the locations of the startRef, endRef. |
| 2174 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-endBuf); |
| 2175 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-endBuf+1); |
| 2176 | // Comment out the protocol references. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2177 | InsertText(LessLoc, "/*"); |
| 2178 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 37e011c | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2179 | } |
| 2180 | } |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2181 | if (!proto) |
| 2182 | return; // most likely, was a variable |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2183 | // Now check arguments. |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2184 | const char *startBuf = SM->getCharacterData(Loc); |
| 2185 | const char *startFuncBuf = startBuf; |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2186 | for (unsigned i = 0; i < proto->getNumArgs(); i++) { |
| 2187 | if (needToScanForQualifiers(proto->getArgType(i))) { |
| 2188 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2189 | |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2190 | const char *endBuf = startBuf; |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2191 | // scan forward (from the decl location) for argument types. |
| 2192 | scanToNextArgument(endBuf); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2193 | const char *startRef = 0, *endRef = 0; |
| 2194 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2195 | // Get the locations of the startRef, endRef. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2196 | SourceLocation LessLoc = |
Fariborz Jahanian | 16e703a | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2197 | Loc.getFileLocWithOffset(startRef-startFuncBuf); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2198 | SourceLocation GreaterLoc = |
Fariborz Jahanian | 16e703a | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 2199 | Loc.getFileLocWithOffset(endRef-startFuncBuf+1); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2200 | // Comment out the protocol references. |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2201 | InsertText(LessLoc, "/*"); |
| 2202 | InsertText(GreaterLoc, "*/"); |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2203 | } |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2204 | startBuf = ++endBuf; |
| 2205 | } |
| 2206 | else { |
Steve Naroff | c884aa8 | 2008-08-06 15:58:23 +0000 | [diff] [blame] | 2207 | // If the function name is derived from a macro expansion, then the |
| 2208 | // argument buffer will not follow the name. Need to speak with Chris. |
| 2209 | while (*startBuf && *startBuf != ')' && *startBuf != ',') |
Fariborz Jahanian | 4e56ed5 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 2210 | startBuf++; // scan forward (from the decl location) for argument types. |
| 2211 | startBuf++; |
| 2212 | } |
Steve Naroff | 50d4205 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 2213 | } |
Steve Naroff | 37e011c | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2214 | } |
| 2215 | |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2216 | void RewriteObjC::RewriteTypeOfDecl(VarDecl *ND) { |
| 2217 | QualType QT = ND->getType(); |
| 2218 | const Type* TypePtr = QT->getAs<Type>(); |
| 2219 | if (!isa<TypeOfExprType>(TypePtr)) |
| 2220 | return; |
| 2221 | while (isa<TypeOfExprType>(TypePtr)) { |
| 2222 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 2223 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 2224 | TypePtr = QT->getAs<Type>(); |
| 2225 | } |
| 2226 | // FIXME. This will not work for multiple declarators; as in: |
| 2227 | // __typeof__(a) b,c,d; |
| 2228 | std::string TypeAsString(QT.getAsString()); |
| 2229 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
| 2230 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 2231 | if (ND->getInit()) { |
| 2232 | std::string Name(ND->getNameAsString()); |
| 2233 | TypeAsString += " " + Name + " = "; |
| 2234 | Expr *E = ND->getInit(); |
| 2235 | SourceLocation startLoc; |
| 2236 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 2237 | startLoc = ECE->getLParenLoc(); |
| 2238 | else |
| 2239 | startLoc = E->getLocStart(); |
| 2240 | startLoc = SM->getInstantiationLoc(startLoc); |
| 2241 | const char *endBuf = SM->getCharacterData(startLoc); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2242 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2243 | } |
| 2244 | else { |
| 2245 | SourceLocation X = ND->getLocEnd(); |
| 2246 | X = SM->getInstantiationLoc(X); |
| 2247 | const char *endBuf = SM->getCharacterData(X); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2248 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 2249 | } |
| 2250 | } |
| 2251 | |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2252 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2253 | void RewriteObjC::SynthSelGetUidFunctionDecl() { |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2254 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
| 2255 | llvm::SmallVector<QualType, 16> ArgTys; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2256 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2257 | QualType getFuncType = Context->getFunctionType(Context->getObjCSelType(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2258 | &ArgTys[0], ArgTys.size(), |
| 2259 | false /*isVariadic*/, 0, |
| 2260 | false, false, 0, 0, false, |
| 2261 | CC_Default); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2262 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2263 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2264 | SelGetUidIdent, getFuncType, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2265 | FunctionDecl::Extern, false); |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2268 | void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2269 | // declared in <objc/objc.h> |
Douglas Gregor | 1e21c19 | 2009-01-09 01:47:02 +0000 | [diff] [blame] | 2270 | if (FD->getIdentifier() && |
| 2271 | strcmp(FD->getNameAsCString(), "sel_registerName") == 0) { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2272 | SelGetUidFunctionDecl = FD; |
Steve Naroff | 37e011c | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 2273 | return; |
| 2274 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2275 | RewriteObjCQualifiedInterfaceTypes(FD); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2276 | } |
| 2277 | |
Fariborz Jahanian | a459c44 | 2010-02-12 17:52:31 +0000 | [diff] [blame] | 2278 | static void RewriteBlockPointerType(std::string& Str, QualType Type) { |
| 2279 | std::string TypeString(Type.getAsString()); |
| 2280 | const char *argPtr = TypeString.c_str(); |
| 2281 | if (!strchr(argPtr, '^')) { |
| 2282 | Str += TypeString; |
| 2283 | return; |
| 2284 | } |
| 2285 | while (*argPtr) { |
| 2286 | Str += (*argPtr == '^' ? '*' : *argPtr); |
| 2287 | argPtr++; |
| 2288 | } |
| 2289 | } |
| 2290 | |
Fariborz Jahanian | e1ff123 | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 2291 | // FIXME. Consolidate this routine with RewriteBlockPointerType. |
| 2292 | static void RewriteBlockPointerTypeVariable(std::string& Str, ValueDecl *VD) { |
| 2293 | QualType Type = VD->getType(); |
| 2294 | std::string TypeString(Type.getAsString()); |
| 2295 | const char *argPtr = TypeString.c_str(); |
| 2296 | int paren = 0; |
| 2297 | while (*argPtr) { |
| 2298 | switch (*argPtr) { |
| 2299 | case '(': |
| 2300 | Str += *argPtr; |
| 2301 | paren++; |
| 2302 | break; |
| 2303 | case ')': |
| 2304 | Str += *argPtr; |
| 2305 | paren--; |
| 2306 | break; |
| 2307 | case '^': |
| 2308 | Str += '*'; |
| 2309 | if (paren == 1) |
| 2310 | Str += VD->getNameAsString(); |
| 2311 | break; |
| 2312 | default: |
| 2313 | Str += *argPtr; |
| 2314 | break; |
| 2315 | } |
| 2316 | argPtr++; |
| 2317 | } |
| 2318 | } |
| 2319 | |
| 2320 | |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2321 | void RewriteObjC::RewriteBlockLiteralFunctionDecl(FunctionDecl *FD) { |
| 2322 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
| 2323 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
| 2324 | const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(funcType); |
| 2325 | if (!proto) |
| 2326 | return; |
| 2327 | QualType Type = proto->getResultType(); |
| 2328 | std::string FdStr = Type.getAsString(); |
| 2329 | FdStr += " "; |
| 2330 | FdStr += FD->getNameAsCString(); |
| 2331 | FdStr += "("; |
| 2332 | unsigned numArgs = proto->getNumArgs(); |
| 2333 | for (unsigned i = 0; i < numArgs; i++) { |
| 2334 | QualType ArgType = proto->getArgType(i); |
Fariborz Jahanian | a459c44 | 2010-02-12 17:52:31 +0000 | [diff] [blame] | 2335 | RewriteBlockPointerType(FdStr, ArgType); |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2336 | if (i+1 < numArgs) |
| 2337 | FdStr += ", "; |
| 2338 | } |
| 2339 | FdStr += ");\n"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2340 | InsertText(FunLocStart, FdStr); |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 2341 | CurFunctionDeclToDeclareForBlock = 0; |
| 2342 | } |
| 2343 | |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2344 | // SynthSuperContructorFunctionDecl - id objc_super(id obj, id super); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2345 | void RewriteObjC::SynthSuperContructorFunctionDecl() { |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2346 | if (SuperContructorFunctionDecl) |
| 2347 | return; |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2348 | IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super"); |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2349 | llvm::SmallVector<QualType, 16> ArgTys; |
| 2350 | QualType argT = Context->getObjCIdType(); |
| 2351 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2352 | ArgTys.push_back(argT); |
| 2353 | ArgTys.push_back(argT); |
| 2354 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
| 2355 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2356 | false, 0, |
| 2357 | false, false, 0, 0, false, |
| 2358 | CC_Default); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2359 | SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2360 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2361 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2362 | FunctionDecl::Extern, false); |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2363 | } |
| 2364 | |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2365 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2366 | void RewriteObjC::SynthMsgSendFunctionDecl() { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2367 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
| 2368 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2369 | QualType argT = Context->getObjCIdType(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2370 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2371 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2372 | argT = Context->getObjCSelType(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2373 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2374 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2375 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2376 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2377 | true /*isVariadic*/, 0, |
| 2378 | false, false, 0, 0, false, |
| 2379 | CC_Default); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2380 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2381 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2382 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2383 | FunctionDecl::Extern, false); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2384 | } |
| 2385 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2386 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2387 | void RewriteObjC::SynthMsgSendSuperFunctionDecl() { |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2388 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
| 2389 | llvm::SmallVector<QualType, 16> ArgTys; |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2390 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2391 | SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2392 | &Context->Idents.get("objc_super")); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2393 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2394 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2395 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2396 | argT = Context->getObjCSelType(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2397 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2398 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2399 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2400 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2401 | true /*isVariadic*/, 0, |
| 2402 | false, false, 0, 0, false, |
| 2403 | CC_Default); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2404 | MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2405 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2406 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2407 | FunctionDecl::Extern, false); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2408 | } |
| 2409 | |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2410 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2411 | void RewriteObjC::SynthMsgSendStretFunctionDecl() { |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2412 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
| 2413 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2414 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2415 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2416 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2417 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2418 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2419 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2420 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2421 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2422 | true /*isVariadic*/, 0, |
| 2423 | false, false, 0, 0, false, |
| 2424 | CC_Default); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2425 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2426 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2427 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2428 | FunctionDecl::Extern, false); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2429 | } |
| 2430 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2431 | // SynthMsgSendSuperStretFunctionDecl - |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2432 | // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2433 | void RewriteObjC::SynthMsgSendSuperStretFunctionDecl() { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2434 | IdentifierInfo *msgSendIdent = |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2435 | &Context->Idents.get("objc_msgSendSuper_stret"); |
| 2436 | llvm::SmallVector<QualType, 16> ArgTys; |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2437 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2438 | SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2439 | &Context->Idents.get("objc_super")); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2440 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2441 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2442 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2443 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2444 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2445 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2446 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2447 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2448 | true /*isVariadic*/, 0, |
| 2449 | false, false, 0, 0, false, |
| 2450 | CC_Default); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2451 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2452 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2453 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2454 | FunctionDecl::Extern, false); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2455 | } |
| 2456 | |
Steve Naroff | 2e4e385 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2457 | // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2458 | void RewriteObjC::SynthMsgSendFpretFunctionDecl() { |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2459 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
| 2460 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2461 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2462 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2463 | ArgTys.push_back(argT); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2464 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2465 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2466 | ArgTys.push_back(argT); |
Steve Naroff | 2e4e385 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2467 | QualType msgSendType = Context->getFunctionType(Context->DoubleTy, |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2468 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2469 | true /*isVariadic*/, 0, |
| 2470 | false, false, 0, 0, false, |
| 2471 | CC_Default); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2472 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2473 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2474 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2475 | FunctionDecl::Extern, false); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2476 | } |
| 2477 | |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2478 | // SynthGetClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2479 | void RewriteObjC::SynthGetClassFunctionDecl() { |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2480 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
| 2481 | llvm::SmallVector<QualType, 16> ArgTys; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2482 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2483 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2484 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2485 | false /*isVariadic*/, 0, |
| 2486 | false, false, 0, 0, false, |
| 2487 | CC_Default); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2488 | GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2489 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2490 | getClassIdent, getClassType, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2491 | FunctionDecl::Extern, false); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2492 | } |
| 2493 | |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2494 | // SynthGetMetaClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2495 | void RewriteObjC::SynthGetMetaClassFunctionDecl() { |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2496 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
| 2497 | llvm::SmallVector<QualType, 16> ArgTys; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2498 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2499 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2500 | &ArgTys[0], ArgTys.size(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2501 | false /*isVariadic*/, 0, |
| 2502 | false, false, 0, 0, false, |
| 2503 | CC_Default); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2504 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2505 | SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2506 | getClassIdent, getClassType, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2507 | FunctionDecl::Extern, false); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2508 | } |
| 2509 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2510 | Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2511 | QualType strType = getConstantStringStructType(); |
| 2512 | |
| 2513 | std::string S = "__NSConstantStringImpl_"; |
Steve Naroff | a6141f0 | 2008-05-31 03:35:42 +0000 | [diff] [blame] | 2514 | |
| 2515 | std::string tmpName = InFileName; |
| 2516 | unsigned i; |
| 2517 | for (i=0; i < tmpName.length(); i++) { |
| 2518 | char c = tmpName.at(i); |
| 2519 | // replace any non alphanumeric characters with '_'. |
| 2520 | if (!isalpha(c) && (c < '0' || c > '9')) |
| 2521 | tmpName[i] = '_'; |
| 2522 | } |
| 2523 | S += tmpName; |
| 2524 | S += "_"; |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2525 | S += utostr(NumObjCStringLiterals++); |
| 2526 | |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2527 | Preamble += "static __NSConstantStringImpl " + S; |
| 2528 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 2529 | Preamble += "0x000007c8,"; // utf8_str |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2530 | // The pretty printer for StringLiteral handles escape characters properly. |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2531 | std::string prettyBufS; |
| 2532 | llvm::raw_string_ostream prettyBuf(prettyBufS); |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2533 | Exp->getString()->printPretty(prettyBuf, *Context, 0, |
| 2534 | PrintingPolicy(LangOpts)); |
Steve Naroff | 00a3176 | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2535 | Preamble += prettyBuf.str(); |
| 2536 | Preamble += ","; |
Steve Naroff | 94ed6dc | 2009-12-06 01:48:44 +0000 | [diff] [blame] | 2537 | Preamble += utostr(Exp->getString()->getByteLength()) + "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2538 | |
| 2539 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 2540 | &Context->Idents.get(S), strType, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2541 | VarDecl::Static); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2542 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, strType, SourceLocation()); |
| 2543 | Expr *Unop = new (Context) UnaryOperator(DRE, UnaryOperator::AddrOf, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2544 | Context->getPointerType(DRE->getType()), |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2545 | SourceLocation()); |
Steve Naroff | 265a6b9 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2546 | // cast to NSConstantString * |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2547 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Exp->getType(), |
| 2548 | CastExpr::CK_Unknown, Unop); |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2549 | ReplaceStmt(Exp, cast); |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 2550 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | 265a6b9 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2551 | return cast; |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2552 | } |
| 2553 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2554 | ObjCInterfaceDecl *RewriteObjC::isSuperReceiver(Expr *recExpr) { |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2555 | // check if we are sending a message to 'super' |
Douglas Gregor | ffca3a2 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 2556 | if (!CurMethodDef || !CurMethodDef->isInstanceMethod()) return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2557 | |
Douglas Gregor | 8ea1f53 | 2008-11-04 14:56:14 +0000 | [diff] [blame] | 2558 | if (ObjCSuperExpr *Super = dyn_cast<ObjCSuperExpr>(recExpr)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2559 | const ObjCObjectPointerType *OPT = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2560 | Super->getType()->getAs<ObjCObjectPointerType>(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2561 | assert(OPT); |
| 2562 | const ObjCInterfaceType *IT = OPT->getInterfaceType(); |
Chris Lattner | a9b3cae | 2008-06-21 18:04:54 +0000 | [diff] [blame] | 2563 | return IT->getDecl(); |
| 2564 | } |
| 2565 | return 0; |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2566 | } |
| 2567 | |
| 2568 | // struct objc_super { struct objc_object *receiver; struct objc_class *super; }; |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2569 | QualType RewriteObjC::getSuperStructType() { |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2570 | if (!SuperStructDecl) { |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2571 | SuperStructDecl = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2572 | SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2573 | &Context->Idents.get("objc_super")); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2574 | QualType FieldTypes[2]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2575 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2576 | // struct objc_object *receiver; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2577 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2578 | // struct objc_class *super; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2579 | FieldTypes[1] = Context->getObjCClassType(); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2580 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2581 | // Create fields |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2582 | for (unsigned i = 0; i < 2; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2583 | SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl, |
| 2584 | SourceLocation(), 0, |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2585 | FieldTypes[i], 0, |
| 2586 | /*BitWidth=*/0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2587 | /*Mutable=*/false)); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2588 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2589 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2590 | SuperStructDecl->completeDefinition(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2591 | } |
| 2592 | return Context->getTagDeclType(SuperStructDecl); |
| 2593 | } |
| 2594 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2595 | QualType RewriteObjC::getConstantStringStructType() { |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2596 | if (!ConstantStringDecl) { |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2597 | ConstantStringDecl = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2598 | SourceLocation(), |
Ted Kremenek | 47923c7 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2599 | &Context->Idents.get("__NSConstantStringImpl")); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2600 | QualType FieldTypes[4]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2601 | |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2602 | // struct objc_object *receiver; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2603 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2604 | // int flags; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2605 | FieldTypes[1] = Context->IntTy; |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2606 | // char *str; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2607 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2608 | // long length; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2609 | FieldTypes[3] = Context->LongTy; |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2610 | |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2611 | // Create fields |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2612 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2613 | ConstantStringDecl->addDecl(FieldDecl::Create(*Context, |
| 2614 | ConstantStringDecl, |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2615 | SourceLocation(), 0, |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2616 | FieldTypes[i], 0, |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2617 | /*BitWidth=*/0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2618 | /*Mutable=*/true)); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2619 | } |
| 2620 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2621 | ConstantStringDecl->completeDefinition(); |
Steve Naroff | ce8e886 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2622 | } |
| 2623 | return Context->getTagDeclType(ConstantStringDecl); |
| 2624 | } |
| 2625 | |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2626 | Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp, |
| 2627 | SourceLocation StartLoc, |
| 2628 | SourceLocation EndLoc) { |
Fariborz Jahanian | 31e1850 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2629 | if (!SelGetUidFunctionDecl) |
| 2630 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2631 | if (!MsgSendFunctionDecl) |
| 2632 | SynthMsgSendFunctionDecl(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2633 | if (!MsgSendSuperFunctionDecl) |
| 2634 | SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2635 | if (!MsgSendStretFunctionDecl) |
| 2636 | SynthMsgSendStretFunctionDecl(); |
| 2637 | if (!MsgSendSuperStretFunctionDecl) |
| 2638 | SynthMsgSendSuperStretFunctionDecl(); |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2639 | if (!MsgSendFpretFunctionDecl) |
| 2640 | SynthMsgSendFpretFunctionDecl(); |
Steve Naroff | 5cdcd9b | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2641 | if (!GetClassFunctionDecl) |
| 2642 | SynthGetClassFunctionDecl(); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2643 | if (!GetMetaClassFunctionDecl) |
| 2644 | SynthGetMetaClassFunctionDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2645 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2646 | // default to objc_msgSend(). |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2647 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2648 | // May need to use objc_msgSend_stret() as well. |
| 2649 | FunctionDecl *MsgSendStretFlavor = 0; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2650 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
| 2651 | QualType resultType = mDecl->getResultType(); |
Chris Lattner | 3f6cd0b | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2652 | if (resultType->isStructureType() || resultType->isUnionType()) |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2653 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
Chris Lattner | 3f6cd0b | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2654 | else if (resultType->isRealFloatingType()) |
Fariborz Jahanian | 4f76f22 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2655 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2656 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2657 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2658 | // Synthesize a call to objc_msgSend(). |
| 2659 | llvm::SmallVector<Expr*, 8> MsgExprs; |
| 2660 | IdentifierInfo *clsName = Exp->getClassName(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2661 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2662 | // Derive/push the receiver/selector, 2 implicit arguments to objc_msgSend(). |
| 2663 | if (clsName) { // class message. |
Steve Naroff | 6c79f97 | 2008-07-24 19:44:33 +0000 | [diff] [blame] | 2664 | // FIXME: We need to fix Sema (and the AST for ObjCMessageExpr) to handle |
| 2665 | // the 'super' idiom within a class method. |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 2666 | if (clsName->getName() == "super") { |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2667 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2668 | if (MsgSendStretFlavor) |
| 2669 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2670 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2671 | |
| 2672 | ObjCInterfaceDecl *SuperDecl = |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 2673 | CurMethodDef->getClassInterface()->getSuperClass(); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2674 | |
| 2675 | llvm::SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2676 | |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2677 | // set the receiver to self, the first argument to all methods. |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2678 | InitExprs.push_back( |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2679 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 2680 | CastExpr::CK_Unknown, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2681 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2682 | Context->getObjCIdType(), |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2683 | SourceLocation())) |
| 2684 | ); // set the 'receiver'. |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2685 | |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2686 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2687 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | f83b5af | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2688 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2689 | SuperDecl->getIdentifier()->getNameStart(), |
| 2690 | SuperDecl->getIdentifier()->getLength(), |
| 2691 | false, argType, SourceLocation())); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2692 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2693 | &ClsExprs[0], |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2694 | ClsExprs.size(), |
| 2695 | StartLoc, |
| 2696 | EndLoc); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2697 | // To turn off a warning, type-cast to 'id' |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2698 | InitExprs.push_back( // set 'super class', using objc_getClass(). |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2699 | NoTypeInfoCStyleCastExpr(Context, |
| 2700 | Context->getObjCIdType(), |
| 2701 | CastExpr::CK_Unknown, Cls)); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2702 | // struct objc_super |
| 2703 | QualType superType = getSuperStructType(); |
Steve Naroff | 0b844f0 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2704 | Expr *SuperRep; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2705 | |
Steve Naroff | 0b844f0 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2706 | if (LangOpts.Microsoft) { |
| 2707 | SynthSuperContructorFunctionDecl(); |
| 2708 | // Simulate a contructor call... |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2709 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
Steve Naroff | 0b844f0 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2710 | superType, SourceLocation()); |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2711 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2712 | InitExprs.size(), |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2713 | superType, SourceLocation()); |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2714 | // The code for super is a little tricky to prevent collision with |
| 2715 | // the structure definition in the header. The rewriter has it's own |
| 2716 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2717 | // we need the cast below. For example: |
| 2718 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2719 | // |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2720 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2721 | Context->getPointerType(SuperRep->getType()), |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2722 | SourceLocation()); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2723 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2724 | Context->getPointerType(superType), |
| 2725 | CastExpr::CK_Unknown, SuperRep); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2726 | } else { |
Steve Naroff | 0b844f0 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2727 | // (struct objc_super) { <exprs from above> } |
Ted Kremenek | 013041e | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 2728 | InitListExpr *ILE = new (Context) InitListExpr(SourceLocation(), |
| 2729 | &InitExprs[0], InitExprs.size(), |
| 2730 | SourceLocation()); |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 2731 | TypeSourceInfo *superTInfo |
| 2732 | = Context->getTrivialTypeSourceInfo(superType); |
John McCall | 5d7aa7f | 2010-01-19 22:33:45 +0000 | [diff] [blame] | 2733 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
| 2734 | superType, ILE, false); |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2735 | // struct objc_super * |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2736 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2737 | Context->getPointerType(SuperRep->getType()), |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2738 | SourceLocation()); |
Steve Naroff | 0b844f0 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2739 | } |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2740 | MsgExprs.push_back(SuperRep); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2741 | } else { |
| 2742 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2743 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | f83b5af | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2744 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2745 | clsName->getNameStart(), |
Chris Lattner | f83b5af | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2746 | clsName->getLength(), |
| 2747 | false, argType, |
| 2748 | SourceLocation())); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2749 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2750 | &ClsExprs[0], |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2751 | ClsExprs.size(), |
| 2752 | StartLoc, EndLoc); |
Steve Naroff | b2f8ff1 | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2753 | MsgExprs.push_back(Cls); |
| 2754 | } |
Steve Naroff | e7f1819 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2755 | } else { // instance message. |
| 2756 | Expr *recExpr = Exp->getReceiver(); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2757 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2758 | if (ObjCInterfaceDecl *SuperDecl = isSuperReceiver(recExpr)) { |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2759 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2760 | if (MsgSendStretFlavor) |
| 2761 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2762 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2763 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2764 | llvm::SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2765 | |
Fariborz Jahanian | 1e34ce1 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2766 | InitExprs.push_back( |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2767 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 2768 | CastExpr::CK_Unknown, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2769 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
Steve Naroff | 97adf60 | 2008-07-16 22:35:27 +0000 | [diff] [blame] | 2770 | Context->getObjCIdType(), |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2771 | SourceLocation())) |
| 2772 | ); // set the 'receiver'. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2773 | |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2774 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2775 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2776 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2777 | SuperDecl->getIdentifier()->getNameStart(), |
| 2778 | SuperDecl->getIdentifier()->getLength(), |
| 2779 | false, argType, SourceLocation())); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2780 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2781 | &ClsExprs[0], |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2782 | ClsExprs.size(), |
| 2783 | StartLoc, EndLoc); |
Fariborz Jahanian | d5db92b | 2007-12-05 17:29:46 +0000 | [diff] [blame] | 2784 | // To turn off a warning, type-cast to 'id' |
Fariborz Jahanian | 1e34ce1 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2785 | InitExprs.push_back( |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2786 | // set 'super class', using objc_getClass(). |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2787 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 2788 | CastExpr::CK_Unknown, Cls)); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2789 | // struct objc_super |
| 2790 | QualType superType = getSuperStructType(); |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2791 | Expr *SuperRep; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2792 | |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2793 | if (LangOpts.Microsoft) { |
| 2794 | SynthSuperContructorFunctionDecl(); |
| 2795 | // Simulate a contructor call... |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2796 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2797 | superType, SourceLocation()); |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2798 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2799 | InitExprs.size(), |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2800 | superType, SourceLocation()); |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2801 | // The code for super is a little tricky to prevent collision with |
| 2802 | // the structure definition in the header. The rewriter has it's own |
| 2803 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2804 | // we need the cast below. For example: |
| 2805 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2806 | // |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2807 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2808 | Context->getPointerType(SuperRep->getType()), |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2809 | SourceLocation()); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2810 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2811 | Context->getPointerType(superType), |
| 2812 | CastExpr::CK_Unknown, SuperRep); |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2813 | } else { |
| 2814 | // (struct objc_super) { <exprs from above> } |
Ted Kremenek | 013041e | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 2815 | InitListExpr *ILE = new (Context) InitListExpr(SourceLocation(), |
| 2816 | &InitExprs[0], InitExprs.size(), |
| 2817 | SourceLocation()); |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 2818 | TypeSourceInfo *superTInfo |
| 2819 | = Context->getTrivialTypeSourceInfo(superType); |
John McCall | 5d7aa7f | 2010-01-19 22:33:45 +0000 | [diff] [blame] | 2820 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
| 2821 | superType, ILE, false); |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2822 | } |
Steve Naroff | 6ab6dc7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2823 | MsgExprs.push_back(SuperRep); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2824 | } else { |
Fariborz Jahanian | ff6a455 | 2007-12-07 21:21:21 +0000 | [diff] [blame] | 2825 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 2826 | // Foo<Proto> *. |
Douglas Gregor | f19b231 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2827 | while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr)) |
Fariborz Jahanian | ff6a455 | 2007-12-07 21:21:21 +0000 | [diff] [blame] | 2828 | recExpr = CE->getSubExpr(); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2829 | recExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 2830 | CastExpr::CK_Unknown, recExpr); |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2831 | MsgExprs.push_back(recExpr); |
| 2832 | } |
Steve Naroff | e7f1819 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2833 | } |
Steve Naroff | a397efd | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2834 | // Create a call to sel_registerName("selName"), it will be the 2nd argument. |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2835 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 2836 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2837 | SelExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 6b7ecf6 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2838 | Exp->getSelector().getAsString().c_str(), |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2839 | Exp->getSelector().getAsString().size(), |
Chris Lattner | 630970d | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 2840 | false, argType, SourceLocation())); |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2841 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2842 | &SelExprs[0], SelExprs.size(), |
| 2843 | StartLoc, |
| 2844 | EndLoc); |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2845 | MsgExprs.push_back(SelExp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2846 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2847 | // Now push any user supplied arguments. |
| 2848 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
Steve Naroff | e7f1819 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2849 | Expr *userExpr = Exp->getArg(i); |
Steve Naroff | f60782b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2850 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 2851 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 2852 | // Reuse the ICE type, it is exactly what the doctor ordered. |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2853 | QualType type = ICE->getType()->isObjCQualifiedIdType() |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2854 | ? Context->getObjCIdType() |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2855 | : ICE->getType(); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2856 | userExpr = NoTypeInfoCStyleCastExpr(Context, type, CastExpr::CK_Unknown, |
| 2857 | userExpr); |
Fariborz Jahanian | 9f0e310 | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2858 | } |
| 2859 | // Make id<P...> cast into an 'id' cast. |
Douglas Gregor | f19b231 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2860 | else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2861 | if (CE->getType()->isObjCQualifiedIdType()) { |
Douglas Gregor | f19b231 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2862 | while ((CE = dyn_cast<CStyleCastExpr>(userExpr))) |
Fariborz Jahanian | 9f0e310 | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2863 | userExpr = CE->getSubExpr(); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2864 | userExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 2865 | CastExpr::CK_Unknown, userExpr); |
Fariborz Jahanian | 9f0e310 | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2866 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2867 | } |
Steve Naroff | e7f1819 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2868 | MsgExprs.push_back(userExpr); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2869 | // We've transferred the ownership to MsgExprs. For now, we *don't* null |
| 2870 | // out the argument in the original expression (since we aren't deleting |
| 2871 | // the ObjCMessageExpr). See RewritePropertySetter() usage for more info. |
| 2872 | //Exp->setArg(i, 0); |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2873 | } |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2874 | // Generate the funky cast. |
| 2875 | CastExpr *cast; |
| 2876 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 2877 | QualType returnType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2878 | |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2879 | // Push 'id' and 'SEL', the 2 implicit arguments. |
Steve Naroff | 44864e4 | 2007-11-15 10:43:57 +0000 | [diff] [blame] | 2880 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 2881 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 2882 | else |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2883 | ArgTypes.push_back(Context->getObjCIdType()); |
| 2884 | ArgTypes.push_back(Context->getObjCSelType()); |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2885 | if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) { |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2886 | // Push any user argument types. |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2887 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 2888 | E = OMD->param_end(); PI != E; ++PI) { |
| 2889 | QualType t = (*PI)->getType()->isObjCQualifiedIdType() |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2890 | ? Context->getObjCIdType() |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2891 | : (*PI)->getType(); |
Steve Naroff | 52c65fa | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 2892 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 2893 | if (isTopLevelBlockPointerType(t)) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2894 | const BlockPointerType *BPT = t->getAs<BlockPointerType>(); |
Steve Naroff | 52c65fa | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 2895 | t = Context->getPointerType(BPT->getPointeeType()); |
| 2896 | } |
Steve Naroff | 98eb8d1 | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 2897 | ArgTypes.push_back(t); |
| 2898 | } |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2899 | returnType = OMD->getResultType()->isObjCQualifiedIdType() |
| 2900 | ? Context->getObjCIdType() : OMD->getResultType(); |
Fariborz Jahanian | f89eb2b | 2010-02-24 01:25:40 +0000 | [diff] [blame] | 2901 | if (isTopLevelBlockPointerType(returnType)) { |
| 2902 | const BlockPointerType *BPT = returnType->getAs<BlockPointerType>(); |
| 2903 | returnType = Context->getPointerType(BPT->getPointeeType()); |
| 2904 | } |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2905 | } else { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2906 | returnType = Context->getObjCIdType(); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2907 | } |
| 2908 | // Get the type, we will need to reference it in a couple spots. |
Steve Naroff | 7fa2f04 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2909 | QualType msgSendType = MsgSendFlavor->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2910 | |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2911 | // Create a reference to the objc_msgSend() declaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2912 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, msgSendType, |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2913 | SourceLocation()); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2914 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2915 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2916 | // If we don't do this cast, we get the following bizarre warning/note: |
| 2917 | // xx.m:13: warning: function called through a non-compatible type |
| 2918 | // xx.m:13: note: if this code is reached, the program will abort |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2919 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 2920 | Context->getPointerType(Context->VoidTy), |
| 2921 | CastExpr::CK_Unknown, DRE); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2922 | |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2923 | // Now do the "normal" pointer to function cast. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2924 | QualType castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | 227c0d1 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 2925 | &ArgTypes[0], ArgTypes.size(), |
Steve Naroff | 327f0f4 | 2008-03-18 02:02:04 +0000 | [diff] [blame] | 2926 | // If we don't have a method decl, force a variadic cast. |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2927 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true, 0, |
| 2928 | false, false, 0, 0, false, |
| 2929 | CC_Default); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2930 | castType = Context->getPointerType(castType); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2931 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CastExpr::CK_Unknown, |
| 2932 | cast); |
Steve Naroff | f36987c | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2933 | |
| 2934 | // Don't forget the parens to enforce the proper binding. |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2935 | ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2936 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2937 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2938 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2939 | MsgExprs.size(), |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 2940 | FT->getResultType(), EndLoc); |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2941 | Stmt *ReplacingStmt = CE; |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2942 | if (MsgSendStretFlavor) { |
| 2943 | // We have the method which returns a struct/union. Must also generate |
| 2944 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 2945 | // expression which dictate which one to envoke depending on size of |
| 2946 | // method's return type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2947 | |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2948 | // Create a reference to the objc_msgSend_stret() declaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2949 | DeclRefExpr *STDRE = new (Context) DeclRefExpr(MsgSendStretFlavor, msgSendType, |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2950 | SourceLocation()); |
| 2951 | // Need to cast objc_msgSend_stret to "void *" (see above comment). |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2952 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 2953 | Context->getPointerType(Context->VoidTy), |
| 2954 | CastExpr::CK_Unknown, STDRE); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2955 | // Now do the "normal" pointer to function cast. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2956 | castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | 227c0d1 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 2957 | &ArgTypes[0], ArgTypes.size(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2958 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false, 0, |
| 2959 | false, false, 0, 0, false, |
| 2960 | CC_Default); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2961 | castType = Context->getPointerType(castType); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 2962 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CastExpr::CK_Unknown, |
| 2963 | cast); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2964 | |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2965 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2966 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2967 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2968 | FT = msgSendType->getAs<FunctionType>(); |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2969 | CallExpr *STCE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2970 | MsgExprs.size(), |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2971 | FT->getResultType(), SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2972 | |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2973 | // Build sizeof(returnType) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2974 | SizeOfAlignOfExpr *sizeofExpr = new (Context) SizeOfAlignOfExpr(true, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2975 | Context->getTrivialTypeSourceInfo(returnType), |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2976 | Context->getSizeType(), |
| 2977 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2978 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 2979 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 2980 | // For X86 it is more complicated and some kind of target specific routine |
| 2981 | // is needed to decide what to do. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2982 | unsigned IntSize = |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2983 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2984 | IntegerLiteral *limit = new (Context) IntegerLiteral(llvm::APInt(IntSize, 8), |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2985 | Context->IntTy, |
| 2986 | SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2987 | BinaryOperator *lessThanExpr = new (Context) BinaryOperator(sizeofExpr, limit, |
| 2988 | BinaryOperator::LE, |
| 2989 | Context->IntTy, |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2990 | SourceLocation()); |
| 2991 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2992 | ConditionalOperator *CondExpr = |
Douglas Gregor | 7e112b0 | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 2993 | new (Context) ConditionalOperator(lessThanExpr, |
| 2994 | SourceLocation(), CE, |
| 2995 | SourceLocation(), STCE, returnType); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2996 | ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), CondExpr); |
Fariborz Jahanian | 9e7b848 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2997 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2998 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2999 | return ReplacingStmt; |
| 3000 | } |
| 3001 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3002 | Stmt *RewriteObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | b8f018d | 2010-02-22 20:48:10 +0000 | [diff] [blame] | 3003 | Stmt *ReplacingStmt = SynthMessageExpr(Exp, Exp->getLocStart(), |
| 3004 | Exp->getLocEnd()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3005 | |
Steve Naroff | 574440f | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 3006 | // Now do the actual rewrite. |
Chris Lattner | 2e0d260 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 3007 | ReplaceStmt(Exp, ReplacingStmt); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3008 | |
| 3009 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Fariborz Jahanian | 965a896 | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 3010 | return ReplacingStmt; |
Steve Naroff | db1ab1c | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 3011 | } |
| 3012 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3013 | // typedef struct objc_object Protocol; |
| 3014 | QualType RewriteObjC::getProtocolType() { |
| 3015 | if (!ProtocolTypeDecl) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3016 | TypeSourceInfo *TInfo |
| 3017 | = Context->getTrivialTypeSourceInfo(Context->getObjCIdType()); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3018 | ProtocolTypeDecl = TypedefDecl::Create(*Context, TUDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3019 | SourceLocation(), |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3020 | &Context->Idents.get("Protocol"), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3021 | TInfo); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3022 | } |
| 3023 | return Context->getTypeDeclType(ProtocolTypeDecl); |
| 3024 | } |
| 3025 | |
Fariborz Jahanian | 33c0e81 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 3026 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3027 | /// a synthesized/forward data reference (to the protocol's metadata). |
| 3028 | /// The forward references (and metadata) are generated in |
| 3029 | /// RewriteObjC::HandleTranslationUnit(). |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3030 | Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3031 | std::string Name = "_OBJC_PROTOCOL_" + Exp->getProtocol()->getNameAsString(); |
| 3032 | IdentifierInfo *ID = &Context->Idents.get(Name); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3033 | VarDecl *VD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Douglas Gregor | ed6c744 | 2009-11-23 11:41:28 +0000 | [diff] [blame] | 3034 | ID, getProtocolType(), 0, VarDecl::Extern); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3035 | DeclRefExpr *DRE = new (Context) DeclRefExpr(VD, getProtocolType(), SourceLocation()); |
| 3036 | Expr *DerefExpr = new (Context) UnaryOperator(DRE, UnaryOperator::AddrOf, |
| 3037 | Context->getPointerType(DRE->getType()), |
| 3038 | SourceLocation()); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3039 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, DerefExpr->getType(), |
| 3040 | CastExpr::CK_Unknown, |
| 3041 | DerefExpr); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3042 | ReplaceStmt(Exp, castExpr); |
| 3043 | ProtocolExprDecls.insert(Exp->getProtocol()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3044 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3045 | return castExpr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3046 | |
Fariborz Jahanian | 33c0e81 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 3047 | } |
| 3048 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3049 | bool RewriteObjC::BufferContainsPPDirectives(const char *startBuf, |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3050 | const char *endBuf) { |
| 3051 | while (startBuf < endBuf) { |
| 3052 | if (*startBuf == '#') { |
| 3053 | // Skip whitespace. |
| 3054 | for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf) |
| 3055 | ; |
| 3056 | if (!strncmp(startBuf, "if", strlen("if")) || |
| 3057 | !strncmp(startBuf, "ifdef", strlen("ifdef")) || |
| 3058 | !strncmp(startBuf, "ifndef", strlen("ifndef")) || |
| 3059 | !strncmp(startBuf, "define", strlen("define")) || |
| 3060 | !strncmp(startBuf, "undef", strlen("undef")) || |
| 3061 | !strncmp(startBuf, "else", strlen("else")) || |
| 3062 | !strncmp(startBuf, "elif", strlen("elif")) || |
| 3063 | !strncmp(startBuf, "endif", strlen("endif")) || |
| 3064 | !strncmp(startBuf, "pragma", strlen("pragma")) || |
| 3065 | !strncmp(startBuf, "include", strlen("include")) || |
| 3066 | !strncmp(startBuf, "import", strlen("import")) || |
| 3067 | !strncmp(startBuf, "include_next", strlen("include_next"))) |
| 3068 | return true; |
| 3069 | } |
| 3070 | startBuf++; |
| 3071 | } |
| 3072 | return false; |
| 3073 | } |
| 3074 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3075 | /// SynthesizeObjCInternalStruct - Rewrite one internal struct corresponding to |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3076 | /// an objective-c class with ivars. |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3077 | void RewriteObjC::SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3078 | std::string &Result) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3079 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3080 | assert(CDecl->getNameAsCString() && |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 3081 | "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | c3cda76 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 3082 | // Do not synthesize more than once. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3083 | if (ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | c3cda76 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 3084 | return; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3085 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Chris Lattner | 8d1c04f | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3086 | int NumIvars = CDecl->ivar_size(); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3087 | SourceLocation LocStart = CDecl->getLocStart(); |
| 3088 | SourceLocation LocEnd = CDecl->getLocEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3089 | |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3090 | const char *startBuf = SM->getCharacterData(LocStart); |
| 3091 | const char *endBuf = SM->getCharacterData(LocEnd); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3092 | |
Fariborz Jahanian | a883d6e | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3093 | // If no ivars and no root or if its root, directly or indirectly, |
| 3094 | // have no ivars (thus not synthesized) then no need to synthesize this class. |
Chris Lattner | 8d1c04f | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3095 | if ((CDecl->isForwardDecl() || NumIvars == 0) && |
| 3096 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3097 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3098 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | a883d6e | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3099 | return; |
| 3100 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3101 | |
| 3102 | // FIXME: This has potential of causing problem. If |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3103 | // SynthesizeObjCInternalStruct is ever called recursively. |
Fariborz Jahanian | a883d6e | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 3104 | Result += "\nstruct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3105 | Result += CDecl->getNameAsString(); |
Steve Naroff | a1e115e | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 3106 | if (LangOpts.Microsoft) |
| 3107 | Result += "_IMPL"; |
Steve Naroff | dc5b6b2 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 3108 | |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3109 | if (NumIvars > 0) { |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3110 | const char *cursor = strchr(startBuf, '{'); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3111 | assert((cursor && endBuf) |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3112 | && "SynthesizeObjCInternalStruct - malformed @interface"); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3113 | // If the buffer contains preprocessor directives, we do more fine-grained |
| 3114 | // rewrites. This is intended to fix code that looks like (which occurs in |
| 3115 | // NSURL.h, for example): |
| 3116 | // |
| 3117 | // #ifdef XYZ |
| 3118 | // @interface Foo : NSObject |
| 3119 | // #else |
| 3120 | // @interface FooBar : NSObject |
| 3121 | // #endif |
| 3122 | // { |
| 3123 | // int i; |
| 3124 | // } |
| 3125 | // @end |
| 3126 | // |
| 3127 | // This clause is segregated to avoid breaking the common case. |
| 3128 | if (BufferContainsPPDirectives(startBuf, cursor)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3129 | SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() : |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3130 | CDecl->getClassLoc(); |
| 3131 | const char *endHeader = SM->getCharacterData(L); |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3132 | endHeader += Lexer::MeasureTokenLength(L, *SM, LangOpts); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3133 | |
Chris Lattner | f5b7751 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3134 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3135 | // advance to the end of the referenced protocols. |
| 3136 | while (endHeader < cursor && *endHeader != '>') endHeader++; |
| 3137 | endHeader++; |
| 3138 | } |
| 3139 | // rewrite the original header |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3140 | ReplaceText(LocStart, endHeader-startBuf, Result); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3141 | } else { |
| 3142 | // rewrite the original header *without* disturbing the '{' |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3143 | ReplaceText(LocStart, cursor-startBuf, Result); |
Steve Naroff | cd92aeb | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 3144 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3145 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3146 | Result = "\n struct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3147 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 9f33bd2 | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 3148 | Result += "_IMPL "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3149 | Result += RCDecl->getNameAsString(); |
Steve Naroff | ffb5f9a | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 3150 | Result += "_IVARS;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3151 | |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3152 | // insert the super class structure definition. |
Chris Lattner | 1780a85 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 3153 | SourceLocation OnePastCurly = |
| 3154 | LocStart.getFileLocWithOffset(cursor-startBuf+1); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3155 | InsertText(OnePastCurly, Result); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3156 | } |
| 3157 | cursor++; // past '{' |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3158 | |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3159 | // Now comment out any visibility specifiers. |
| 3160 | while (cursor < endBuf) { |
| 3161 | if (*cursor == '@') { |
| 3162 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | 174a825 | 2007-11-14 22:57:51 +0000 | [diff] [blame] | 3163 | // Skip whitespace. |
| 3164 | for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor) |
| 3165 | /*scan*/; |
| 3166 | |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3167 | // FIXME: presence of @public, etc. inside comment results in |
| 3168 | // this transformation as well, which is still correct c-code. |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3169 | if (!strncmp(cursor, "public", strlen("public")) || |
| 3170 | !strncmp(cursor, "private", strlen("private")) || |
Steve Naroff | af91b9a | 2008-04-04 22:34:24 +0000 | [diff] [blame] | 3171 | !strncmp(cursor, "package", strlen("package")) || |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3172 | !strncmp(cursor, "protected", strlen("protected"))) |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3173 | InsertText(atLoc, "// "); |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3174 | } |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3175 | // FIXME: If there are cases where '<' is used in ivar declaration part |
| 3176 | // of user code, then scan the ivar list and use needToScanForQualifiers |
| 3177 | // for type checking. |
| 3178 | else if (*cursor == '<') { |
| 3179 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3180 | InsertText(atLoc, "/* "); |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3181 | cursor = strchr(cursor, '>'); |
| 3182 | cursor++; |
| 3183 | atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3184 | InsertText(atLoc, " */"); |
Steve Naroff | 295570a | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3185 | } else if (*cursor == '^') { // rewrite block specifier. |
| 3186 | SourceLocation caretLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3187 | ReplaceText(caretLoc, 1, "*"); |
Fariborz Jahanian | c622553 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 3188 | } |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3189 | cursor++; |
Fariborz Jahanian | aff228df | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 3190 | } |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3191 | // Don't forget to add a ';'!! |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3192 | InsertText(LocEnd.getFileLocWithOffset(1), ";"); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3193 | } else { // we don't have any instance variables - insert super struct. |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 3194 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Steve Naroff | dde7898 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 3195 | Result += " {\n struct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3196 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 9f33bd2 | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 3197 | Result += "_IMPL "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3198 | Result += RCDecl->getNameAsString(); |
Steve Naroff | ffb5f9a | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 3199 | Result += "_IVARS;\n};\n"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 3200 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3201 | } |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3202 | // Mark this struct as having been generated. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3203 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
Steve Naroff | 13e7487 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 3204 | assert(false && "struct already synthesize- SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3205 | } |
| 3206 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3207 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3208 | /// class methods. |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3209 | template<typename MethodIterator> |
| 3210 | void RewriteObjC::RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 3211 | MethodIterator MethodEnd, |
Fariborz Jahanian | 3df412a | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 3212 | bool IsInstanceMethod, |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3213 | const char *prefix, |
Chris Lattner | 211f8b8 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 3214 | const char *ClassName, |
| 3215 | std::string &Result) { |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3216 | if (MethodBegin == MethodEnd) return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3217 | |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3218 | if (!objc_impl_method) { |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3219 | /* struct _objc_method { |
Fariborz Jahanian | 6eafb03 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3220 | SEL _cmd; |
| 3221 | char *method_types; |
| 3222 | void *_imp; |
| 3223 | } |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3224 | */ |
Chris Lattner | 211f8b8 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 3225 | Result += "\nstruct _objc_method {\n"; |
| 3226 | Result += "\tSEL _cmd;\n"; |
| 3227 | Result += "\tchar *method_types;\n"; |
| 3228 | Result += "\tvoid *_imp;\n"; |
| 3229 | Result += "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3230 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3231 | objc_impl_method = true; |
Fariborz Jahanian | 74a1cfa | 2007-10-19 00:36:46 +0000 | [diff] [blame] | 3232 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3233 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3234 | // Build _objc_method_list for class's methods if needed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3235 | |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3236 | /* struct { |
| 3237 | struct _objc_method_list *next_method; |
| 3238 | int method_count; |
| 3239 | struct _objc_method method_list[]; |
| 3240 | } |
| 3241 | */ |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3242 | unsigned NumMethods = std::distance(MethodBegin, MethodEnd); |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3243 | Result += "\nstatic struct {\n"; |
| 3244 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 3245 | Result += "\tint method_count;\n"; |
| 3246 | Result += "\tstruct _objc_method method_list["; |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3247 | Result += utostr(NumMethods); |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3248 | Result += "];\n} _OBJC_"; |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3249 | Result += prefix; |
| 3250 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 3251 | Result += "_METHODS_"; |
| 3252 | Result += ClassName; |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3253 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3254 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 3255 | Result += "_meth\")))= "; |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3256 | Result += "{\n\t0, " + utostr(NumMethods) + "\n"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3257 | |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3258 | Result += "\t,{{(SEL)\""; |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3259 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3260 | std::string MethodTypeString; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3261 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3262 | Result += "\", \""; |
| 3263 | Result += MethodTypeString; |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3264 | Result += "\", (void *)"; |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3265 | Result += MethodInternalNames[*MethodBegin]; |
| 3266 | Result += "}\n"; |
| 3267 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 3268 | Result += "\t ,{(SEL)\""; |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3269 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3270 | std::string MethodTypeString; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3271 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3272 | Result += "\", \""; |
| 3273 | Result += MethodTypeString; |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3274 | Result += "\", (void *)"; |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3275 | Result += MethodInternalNames[*MethodBegin]; |
Fariborz Jahanian | 5633835 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 3276 | Result += "}\n"; |
Fariborz Jahanian | 6eafb03 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3277 | } |
Chris Lattner | 31bc07e | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 3278 | Result += "\t }\n};\n"; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3279 | } |
| 3280 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3281 | /// RewriteObjCProtocolMetaData - Rewrite protocols meta-data. |
Chris Lattner | 390d39a | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 3282 | void RewriteObjC:: |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3283 | RewriteObjCProtocolMetaData(ObjCProtocolDecl *PDecl, const char *prefix, |
| 3284 | const char *ClassName, std::string &Result) { |
Fariborz Jahanian | 6eafb03 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3285 | static bool objc_protocol_methods = false; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3286 | |
| 3287 | // Output struct protocol_methods holder of method selector and type. |
| 3288 | if (!objc_protocol_methods && !PDecl->isForwardDecl()) { |
| 3289 | /* struct protocol_methods { |
| 3290 | SEL _cmd; |
| 3291 | char *method_types; |
| 3292 | } |
| 3293 | */ |
| 3294 | Result += "\nstruct _protocol_methods {\n"; |
| 3295 | Result += "\tstruct objc_selector *_cmd;\n"; |
| 3296 | Result += "\tchar *method_types;\n"; |
| 3297 | Result += "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3298 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3299 | objc_protocol_methods = true; |
| 3300 | } |
| 3301 | // Do not synthesize the protocol more than once. |
| 3302 | if (ObjCSynthesizedProtocols.count(PDecl)) |
| 3303 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3304 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3305 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 3306 | unsigned NumMethods = std::distance(PDecl->instmeth_begin(), |
| 3307 | PDecl->instmeth_end()); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3308 | /* struct _objc_protocol_method_list { |
| 3309 | int protocol_method_count; |
| 3310 | struct protocol_methods protocols[]; |
| 3311 | } |
Steve Naroff | 251084d | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3312 | */ |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3313 | Result += "\nstatic struct {\n"; |
| 3314 | Result += "\tint protocol_method_count;\n"; |
| 3315 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 3316 | Result += utostr(NumMethods); |
| 3317 | Result += "];\n} _OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 3318 | Result += PDecl->getNameAsString(); |
| 3319 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= " |
| 3320 | "{\n\t" + utostr(NumMethods) + "\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3321 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3322 | // Output instance methods declared in this protocol. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3323 | for (ObjCProtocolDecl::instmeth_iterator |
| 3324 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3325 | I != E; ++I) { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3326 | if (I == PDecl->instmeth_begin()) |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3327 | Result += "\t ,{{(struct objc_selector *)\""; |
| 3328 | else |
| 3329 | Result += "\t ,{(struct objc_selector *)\""; |
| 3330 | Result += (*I)->getSelector().getAsString().c_str(); |
| 3331 | std::string MethodTypeString; |
| 3332 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 3333 | Result += "\", \""; |
| 3334 | Result += MethodTypeString; |
| 3335 | Result += "\"}\n"; |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3336 | } |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3337 | Result += "\t }\n};\n"; |
| 3338 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3339 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3340 | // Output class methods declared in this protocol. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3341 | unsigned NumMethods = std::distance(PDecl->classmeth_begin(), |
| 3342 | PDecl->classmeth_end()); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3343 | if (NumMethods > 0) { |
| 3344 | /* struct _objc_protocol_method_list { |
| 3345 | int protocol_method_count; |
| 3346 | struct protocol_methods protocols[]; |
| 3347 | } |
| 3348 | */ |
| 3349 | Result += "\nstatic struct {\n"; |
| 3350 | Result += "\tint protocol_method_count;\n"; |
| 3351 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 3352 | Result += utostr(NumMethods); |
| 3353 | Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_"; |
| 3354 | Result += PDecl->getNameAsString(); |
| 3355 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 3356 | "{\n\t"; |
| 3357 | Result += utostr(NumMethods); |
| 3358 | Result += "\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3359 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3360 | // Output instance methods declared in this protocol. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3361 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3362 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3363 | I != E; ++I) { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3364 | if (I == PDecl->classmeth_begin()) |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3365 | Result += "\t ,{{(struct objc_selector *)\""; |
| 3366 | else |
| 3367 | Result += "\t ,{(struct objc_selector *)\""; |
| 3368 | Result += (*I)->getSelector().getAsString().c_str(); |
| 3369 | std::string MethodTypeString; |
| 3370 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 3371 | Result += "\", \""; |
| 3372 | Result += MethodTypeString; |
| 3373 | Result += "\"}\n"; |
Fariborz Jahanian | 6eafb03 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3374 | } |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3375 | Result += "\t }\n};\n"; |
| 3376 | } |
| 3377 | |
| 3378 | // Output: |
| 3379 | /* struct _objc_protocol { |
| 3380 | // Objective-C 1.0 extensions |
| 3381 | struct _objc_protocol_extension *isa; |
| 3382 | char *protocol_name; |
| 3383 | struct _objc_protocol **protocol_list; |
| 3384 | struct _objc_protocol_method_list *instance_methods; |
| 3385 | struct _objc_protocol_method_list *class_methods; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3386 | }; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3387 | */ |
| 3388 | static bool objc_protocol = false; |
| 3389 | if (!objc_protocol) { |
| 3390 | Result += "\nstruct _objc_protocol {\n"; |
| 3391 | Result += "\tstruct _objc_protocol_extension *isa;\n"; |
| 3392 | Result += "\tchar *protocol_name;\n"; |
| 3393 | Result += "\tstruct _objc_protocol **protocol_list;\n"; |
| 3394 | Result += "\tstruct _objc_protocol_method_list *instance_methods;\n"; |
| 3395 | Result += "\tstruct _objc_protocol_method_list *class_methods;\n"; |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3396 | Result += "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3397 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3398 | objc_protocol = true; |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3399 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3400 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3401 | Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_"; |
| 3402 | Result += PDecl->getNameAsString(); |
| 3403 | Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= " |
| 3404 | "{\n\t0, \""; |
| 3405 | Result += PDecl->getNameAsString(); |
| 3406 | Result += "\", 0, "; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3407 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3408 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 3409 | Result += PDecl->getNameAsString(); |
| 3410 | Result += ", "; |
| 3411 | } |
| 3412 | else |
| 3413 | Result += "0, "; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3414 | if (PDecl->classmeth_begin() != PDecl->classmeth_end()) { |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3415 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
| 3416 | Result += PDecl->getNameAsString(); |
| 3417 | Result += "\n"; |
| 3418 | } |
| 3419 | else |
| 3420 | Result += "0\n"; |
| 3421 | Result += "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3422 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3423 | // Mark this protocol as having been generated. |
| 3424 | if (!ObjCSynthesizedProtocols.insert(PDecl)) |
| 3425 | assert(false && "protocol already synthesized"); |
| 3426 | |
| 3427 | } |
| 3428 | |
| 3429 | void RewriteObjC:: |
| 3430 | RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Protocols, |
| 3431 | const char *prefix, const char *ClassName, |
| 3432 | std::string &Result) { |
| 3433 | if (Protocols.empty()) return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3434 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3435 | for (unsigned i = 0; i != Protocols.size(); i++) |
| 3436 | RewriteObjCProtocolMetaData(Protocols[i], prefix, ClassName, Result); |
| 3437 | |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3438 | // Output the top lovel protocol meta-data for the class. |
| 3439 | /* struct _objc_protocol_list { |
| 3440 | struct _objc_protocol_list *next; |
| 3441 | int protocol_count; |
| 3442 | struct _objc_protocol *class_protocols[]; |
| 3443 | } |
| 3444 | */ |
| 3445 | Result += "\nstatic struct {\n"; |
| 3446 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 3447 | Result += "\tint protocol_count;\n"; |
| 3448 | Result += "\tstruct _objc_protocol *class_protocols["; |
| 3449 | Result += utostr(Protocols.size()); |
| 3450 | Result += "];\n} _OBJC_"; |
| 3451 | Result += prefix; |
| 3452 | Result += "_PROTOCOLS_"; |
| 3453 | Result += ClassName; |
| 3454 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 3455 | "{\n\t0, "; |
| 3456 | Result += utostr(Protocols.size()); |
| 3457 | Result += "\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3458 | |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3459 | Result += "\t,{&_OBJC_PROTOCOL_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3460 | Result += Protocols[0]->getNameAsString(); |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3461 | Result += " \n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3462 | |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3463 | for (unsigned i = 1; i != Protocols.size(); i++) { |
| 3464 | Result += "\t ,&_OBJC_PROTOCOL_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3465 | Result += Protocols[i]->getNameAsString(); |
Chris Lattner | 388f6e9 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3466 | Result += "\n"; |
| 3467 | } |
| 3468 | Result += "\t }\n};\n"; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3469 | } |
| 3470 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3471 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3472 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3473 | /// implementation. |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3474 | void RewriteObjC::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3475 | std::string &Result) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3476 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3477 | // Find category declaration for this implementation. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3478 | ObjCCategoryDecl *CDecl; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3479 | for (CDecl = ClassDecl->getCategoryList(); CDecl; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3480 | CDecl = CDecl->getNextClassCategory()) |
| 3481 | if (CDecl->getIdentifier() == IDecl->getIdentifier()) |
| 3482 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3483 | |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3484 | std::string FullCategoryName = ClassDecl->getNameAsString(); |
Chris Lattner | b907c3f | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3485 | FullCategoryName += '_'; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3486 | FullCategoryName += IDecl->getNameAsString(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3487 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3488 | // Build _objc_method_list for class's instance methods if needed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3489 | llvm::SmallVector<ObjCMethodDecl *, 32> |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3490 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3491 | |
| 3492 | // If any of our property implementations have associated getters or |
| 3493 | // setters, produce metadata for them as well. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3494 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 3495 | PropEnd = IDecl->propimpl_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3496 | Prop != PropEnd; ++Prop) { |
| 3497 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 3498 | continue; |
| 3499 | if (!(*Prop)->getPropertyIvarDecl()) |
| 3500 | continue; |
| 3501 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 3502 | if (!PD) |
| 3503 | continue; |
| 3504 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 3505 | InstanceMethods.push_back(Getter); |
| 3506 | if (PD->isReadOnly()) |
| 3507 | continue; |
| 3508 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 3509 | InstanceMethods.push_back(Setter); |
| 3510 | } |
| 3511 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
Chris Lattner | b907c3f | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3512 | true, "CATEGORY_", FullCategoryName.c_str(), |
| 3513 | Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3514 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3515 | // Build _objc_method_list for class's class methods if needed |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3516 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | b907c3f | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3517 | false, "CATEGORY_", FullCategoryName.c_str(), |
| 3518 | Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3519 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3520 | // Protocols referenced in class declaration? |
Fariborz Jahanian | 989e039 | 2007-11-13 22:09:49 +0000 | [diff] [blame] | 3521 | // Null CDecl is case of a category implementation with no category interface |
| 3522 | if (CDecl) |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3523 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), "CATEGORY", |
| 3524 | FullCategoryName.c_str(), Result); |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3525 | /* struct _objc_category { |
| 3526 | char *category_name; |
| 3527 | char *class_name; |
| 3528 | struct _objc_method_list *instance_methods; |
| 3529 | struct _objc_method_list *class_methods; |
| 3530 | struct _objc_protocol_list *protocols; |
| 3531 | // Objective-C 1.0 extensions |
| 3532 | uint32_t size; // sizeof (struct _objc_category) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3533 | struct _objc_property_list *instance_properties; // category's own |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3534 | // @property decl. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3535 | }; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3536 | */ |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3537 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3538 | static bool objc_category = false; |
| 3539 | if (!objc_category) { |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3540 | Result += "\nstruct _objc_category {\n"; |
| 3541 | Result += "\tchar *category_name;\n"; |
| 3542 | Result += "\tchar *class_name;\n"; |
| 3543 | Result += "\tstruct _objc_method_list *instance_methods;\n"; |
| 3544 | Result += "\tstruct _objc_method_list *class_methods;\n"; |
| 3545 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3546 | Result += "\tunsigned int size;\n"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3547 | Result += "\tstruct _objc_property_list *instance_properties;\n"; |
| 3548 | Result += "};\n"; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3549 | objc_category = true; |
Fariborz Jahanian | 6eafb03 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3550 | } |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3551 | Result += "\nstatic struct _objc_category _OBJC_CATEGORY_"; |
| 3552 | Result += FullCategoryName; |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3553 | Result += " __attribute__ ((used, section (\"__OBJC, __category\")))= {\n\t\""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3554 | Result += IDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3555 | Result += "\"\n\t, \""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3556 | Result += ClassDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3557 | Result += "\"\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3558 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3559 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3560 | Result += "\t, (struct _objc_method_list *)" |
| 3561 | "&_OBJC_CATEGORY_INSTANCE_METHODS_"; |
| 3562 | Result += FullCategoryName; |
| 3563 | Result += "\n"; |
| 3564 | } |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3565 | else |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3566 | Result += "\t, 0\n"; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3567 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3568 | Result += "\t, (struct _objc_method_list *)" |
| 3569 | "&_OBJC_CATEGORY_CLASS_METHODS_"; |
| 3570 | Result += FullCategoryName; |
| 3571 | Result += "\n"; |
| 3572 | } |
| 3573 | else |
| 3574 | Result += "\t, 0\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3575 | |
Chris Lattner | f5b7751 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3576 | if (CDecl && CDecl->protocol_begin() != CDecl->protocol_end()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3577 | Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3578 | Result += FullCategoryName; |
| 3579 | Result += "\n"; |
| 3580 | } |
| 3581 | else |
| 3582 | Result += "\t, 0\n"; |
| 3583 | Result += "\t, sizeof(struct _objc_category), 0\n};\n"; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3584 | } |
| 3585 | |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3586 | /// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of |
| 3587 | /// ivar offset. |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 3588 | void RewriteObjC::SynthesizeIvarOffsetComputation(ObjCContainerDecl *IDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3589 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3590 | std::string &Result) { |
Steve Naroff | de7d0f6 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3591 | if (ivar->isBitField()) { |
| 3592 | // FIXME: The hack below doesn't work for bitfields. For now, we simply |
| 3593 | // place all bitfields at offset 0. |
| 3594 | Result += "0"; |
| 3595 | } else { |
| 3596 | Result += "__OFFSETOFIVAR__(struct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3597 | Result += IDecl->getNameAsString(); |
Steve Naroff | de7d0f6 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3598 | if (LangOpts.Microsoft) |
| 3599 | Result += "_IMPL"; |
| 3600 | Result += ", "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3601 | Result += ivar->getNameAsString(); |
Steve Naroff | de7d0f6 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3602 | Result += ")"; |
| 3603 | } |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3604 | } |
| 3605 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3606 | //===----------------------------------------------------------------------===// |
| 3607 | // Meta Data Emission |
| 3608 | //===----------------------------------------------------------------------===// |
| 3609 | |
Steve Naroff | 1dc53ef | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3610 | void RewriteObjC::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3611 | std::string &Result) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3612 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3613 | |
Fariborz Jahanian | 96502af | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3614 | // Explictly declared @interface's are already synthesized. |
Steve Naroff | aac654a | 2009-04-20 20:09:33 +0000 | [diff] [blame] | 3615 | if (CDecl->isImplicitInterfaceDecl()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3616 | // FIXME: Implementation of a class with no @interface (legacy) doese not |
Fariborz Jahanian | 96502af | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3617 | // produce correct synthesis as yet. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3618 | SynthesizeObjCInternalStruct(CDecl, Result); |
Fariborz Jahanian | 96502af | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3619 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3620 | |
Chris Lattner | 30d23e8 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3621 | // Build _objc_ivar_list metadata for classes ivars if needed |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3622 | unsigned NumIvars = !IDecl->ivar_empty() |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3623 | ? IDecl->ivar_size() |
Chris Lattner | 8d1c04f | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3624 | : (CDecl ? CDecl->ivar_size() : 0); |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3625 | if (NumIvars > 0) { |
| 3626 | static bool objc_ivar = false; |
| 3627 | if (!objc_ivar) { |
| 3628 | /* struct _objc_ivar { |
| 3629 | char *ivar_name; |
| 3630 | char *ivar_type; |
| 3631 | int ivar_offset; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3632 | }; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3633 | */ |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3634 | Result += "\nstruct _objc_ivar {\n"; |
| 3635 | Result += "\tchar *ivar_name;\n"; |
| 3636 | Result += "\tchar *ivar_type;\n"; |
| 3637 | Result += "\tint ivar_offset;\n"; |
| 3638 | Result += "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3639 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3640 | objc_ivar = true; |
| 3641 | } |
| 3642 | |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3643 | /* struct { |
| 3644 | int ivar_count; |
| 3645 | struct _objc_ivar ivar_list[nIvars]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3646 | }; |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3647 | */ |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3648 | Result += "\nstatic struct {\n"; |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3649 | Result += "\tint ivar_count;\n"; |
| 3650 | Result += "\tstruct _objc_ivar ivar_list["; |
| 3651 | Result += utostr(NumIvars); |
| 3652 | Result += "];\n} _OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3653 | Result += IDecl->getNameAsString(); |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3654 | Result += " __attribute__ ((used, section (\"__OBJC, __instance_vars\")))= " |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3655 | "{\n\t"; |
| 3656 | Result += utostr(NumIvars); |
| 3657 | Result += "\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3658 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3659 | ObjCInterfaceDecl::ivar_iterator IVI, IVE; |
Douglas Gregor | 5f66205 | 2009-04-23 03:23:08 +0000 | [diff] [blame] | 3660 | llvm::SmallVector<ObjCIvarDecl *, 8> IVars; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3661 | if (!IDecl->ivar_empty()) { |
Fariborz Jahanian | aef6622 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 3662 | for (ObjCInterfaceDecl::ivar_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3663 | IV = IDecl->ivar_begin(), IVEnd = IDecl->ivar_end(); |
Douglas Gregor | 5f66205 | 2009-04-23 03:23:08 +0000 | [diff] [blame] | 3664 | IV != IVEnd; ++IV) |
| 3665 | IVars.push_back(*IV); |
Fariborz Jahanian | aef6622 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 3666 | IVI = IDecl->ivar_begin(); |
| 3667 | IVE = IDecl->ivar_end(); |
Chris Lattner | 30d23e8 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3668 | } else { |
| 3669 | IVI = CDecl->ivar_begin(); |
| 3670 | IVE = CDecl->ivar_end(); |
| 3671 | } |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3672 | Result += "\t,{{\""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3673 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | bc27593 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3674 | Result += "\", \""; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3675 | std::string TmpString, StrEncoding; |
| 3676 | Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI); |
| 3677 | QuoteDoublequotes(TmpString, StrEncoding); |
Fariborz Jahanian | bc27593 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3678 | Result += StrEncoding; |
| 3679 | Result += "\", "; |
Chris Lattner | 30d23e8 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3680 | SynthesizeIvarOffsetComputation(IDecl, *IVI, Result); |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3681 | Result += "}\n"; |
Chris Lattner | 30d23e8 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3682 | for (++IVI; IVI != IVE; ++IVI) { |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3683 | Result += "\t ,{\""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3684 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | bc27593 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3685 | Result += "\", \""; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3686 | std::string TmpString, StrEncoding; |
| 3687 | Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI); |
| 3688 | QuoteDoublequotes(TmpString, StrEncoding); |
Fariborz Jahanian | bc27593 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3689 | Result += StrEncoding; |
| 3690 | Result += "\", "; |
Chris Lattner | 30d23e8 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3691 | SynthesizeIvarOffsetComputation(IDecl, (*IVI), Result); |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3692 | Result += "}\n"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3693 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3694 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3695 | Result += "\t }\n};\n"; |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3696 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3697 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3698 | // Build _objc_method_list for class's instance methods if needed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3699 | llvm::SmallVector<ObjCMethodDecl *, 32> |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3700 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3701 | |
| 3702 | // If any of our property implementations have associated getters or |
| 3703 | // setters, produce metadata for them as well. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3704 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 3705 | PropEnd = IDecl->propimpl_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3706 | Prop != PropEnd; ++Prop) { |
| 3707 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 3708 | continue; |
| 3709 | if (!(*Prop)->getPropertyIvarDecl()) |
| 3710 | continue; |
| 3711 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 3712 | if (!PD) |
| 3713 | continue; |
| 3714 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 3715 | InstanceMethods.push_back(Getter); |
| 3716 | if (PD->isReadOnly()) |
| 3717 | continue; |
| 3718 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 3719 | InstanceMethods.push_back(Setter); |
| 3720 | } |
| 3721 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3722 | true, "", IDecl->getNameAsCString(), Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3723 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3724 | // Build _objc_method_list for class's class methods if needed |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3725 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3726 | false, "", IDecl->getNameAsCString(), Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3727 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3728 | // Protocols referenced in class declaration? |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3729 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), |
| 3730 | "CLASS", CDecl->getNameAsCString(), Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3731 | |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3732 | // Declaration of class/meta-class metadata |
| 3733 | /* struct _objc_class { |
| 3734 | struct _objc_class *isa; // or const char *root_class_name when metadata |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3735 | const char *super_class_name; |
| 3736 | char *name; |
| 3737 | long version; |
| 3738 | long info; |
| 3739 | long instance_size; |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3740 | struct _objc_ivar_list *ivars; |
| 3741 | struct _objc_method_list *methods; |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3742 | struct objc_cache *cache; |
| 3743 | struct objc_protocol_list *protocols; |
| 3744 | const char *ivar_layout; |
| 3745 | struct _objc_class_ext *ext; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3746 | }; |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3747 | */ |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3748 | static bool objc_class = false; |
| 3749 | if (!objc_class) { |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3750 | Result += "\nstruct _objc_class {\n"; |
| 3751 | Result += "\tstruct _objc_class *isa;\n"; |
| 3752 | Result += "\tconst char *super_class_name;\n"; |
| 3753 | Result += "\tchar *name;\n"; |
| 3754 | Result += "\tlong version;\n"; |
| 3755 | Result += "\tlong info;\n"; |
| 3756 | Result += "\tlong instance_size;\n"; |
| 3757 | Result += "\tstruct _objc_ivar_list *ivars;\n"; |
| 3758 | Result += "\tstruct _objc_method_list *methods;\n"; |
| 3759 | Result += "\tstruct objc_cache *cache;\n"; |
| 3760 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 3761 | Result += "\tconst char *ivar_layout;\n"; |
| 3762 | Result += "\tstruct _objc_class_ext *ext;\n"; |
| 3763 | Result += "};\n"; |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3764 | objc_class = true; |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3765 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3766 | |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3767 | // Meta-class metadata generation. |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3768 | ObjCInterfaceDecl *RootClass = 0; |
| 3769 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3770 | while (SuperClass) { |
| 3771 | RootClass = SuperClass; |
| 3772 | SuperClass = SuperClass->getSuperClass(); |
| 3773 | } |
| 3774 | SuperClass = CDecl->getSuperClass(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3775 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3776 | Result += "\nstatic struct _objc_class _OBJC_METACLASS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3777 | Result += CDecl->getNameAsString(); |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3778 | Result += " __attribute__ ((used, section (\"__OBJC, __meta_class\")))= " |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3779 | "{\n\t(struct _objc_class *)\""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3780 | Result += (RootClass ? RootClass->getNameAsString() : CDecl->getNameAsString()); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3781 | Result += "\""; |
| 3782 | |
| 3783 | if (SuperClass) { |
| 3784 | Result += ", \""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3785 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3786 | Result += "\", \""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3787 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3788 | Result += "\""; |
| 3789 | } |
| 3790 | else { |
| 3791 | Result += ", 0, \""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3792 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3793 | Result += "\""; |
| 3794 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3795 | // Set 'ivars' field for root class to 0. ObjC1 runtime does not use it. |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3796 | // 'info' field is initialized to CLS_META(2) for metaclass |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3797 | Result += ", 0,2, sizeof(struct _objc_class), 0"; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3798 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
Steve Naroff | 0b844f0 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 3799 | Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3800 | Result += IDecl->getNameAsString(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3801 | Result += "\n"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3802 | } |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3803 | else |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3804 | Result += ", 0\n"; |
Chris Lattner | f5b7751 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3805 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | 251084d | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3806 | Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3807 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3808 | Result += ",0,0\n"; |
| 3809 | } |
Fariborz Jahanian | 486f718 | 2007-10-24 20:54:23 +0000 | [diff] [blame] | 3810 | else |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3811 | Result += "\t,0,0,0,0\n"; |
| 3812 | Result += "};\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3813 | |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3814 | // class metadata generation. |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3815 | Result += "\nstatic struct _objc_class _OBJC_CLASS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3816 | Result += CDecl->getNameAsString(); |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3817 | Result += " __attribute__ ((used, section (\"__OBJC, __class\")))= " |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3818 | "{\n\t&_OBJC_METACLASS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3819 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3820 | if (SuperClass) { |
| 3821 | Result += ", \""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3822 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3823 | Result += "\", \""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3824 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3825 | Result += "\""; |
| 3826 | } |
| 3827 | else { |
| 3828 | Result += ", 0, \""; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3829 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3830 | Result += "\""; |
| 3831 | } |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3832 | // 'info' field is initialized to CLS_CLASS(1) for class |
Fariborz Jahanian | 801b635 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3833 | Result += ", 0,1"; |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3834 | if (!ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 801b635 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3835 | Result += ",0"; |
| 3836 | else { |
| 3837 | // class has size. Must synthesize its size. |
Fariborz Jahanian | 63ac80e | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 3838 | Result += ",sizeof(struct "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3839 | Result += CDecl->getNameAsString(); |
Steve Naroff | 14a0746 | 2008-03-10 23:33:22 +0000 | [diff] [blame] | 3840 | if (LangOpts.Microsoft) |
| 3841 | Result += "_IMPL"; |
Fariborz Jahanian | 801b635 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3842 | Result += ")"; |
| 3843 | } |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3844 | if (NumIvars > 0) { |
Steve Naroff | 17978c4 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 3845 | Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3846 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3847 | Result += "\n\t"; |
| 3848 | } |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3849 | else |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3850 | Result += ",0"; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3851 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
Steve Naroff | c5b9cc7 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3852 | Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3853 | Result += CDecl->getNameAsString(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3854 | Result += ", 0\n\t"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3855 | } |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3856 | else |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3857 | Result += ",0,0"; |
Chris Lattner | f5b7751 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3858 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | 251084d | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3859 | Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3860 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3861 | Result += ", 0,0\n"; |
| 3862 | } |
Fariborz Jahanian | f3d5a54 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3863 | else |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3864 | Result += ",0,0,0\n"; |
| 3865 | Result += "};\n"; |
Fariborz Jahanian | d752eae | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3866 | } |
Fariborz Jahanian | c34409c | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3867 | |
Fariborz Jahanian | 98ba6cd | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3868 | /// RewriteImplementations - This routine rewrites all method implementations |
| 3869 | /// and emits meta-data. |
| 3870 | |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3871 | void RewriteObjC::RewriteImplementations() { |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3872 | int ClsDefCount = ClassImplementation.size(); |
| 3873 | int CatDefCount = CategoryImplementation.size(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3874 | |
Fariborz Jahanian | 98ba6cd | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3875 | // Rewrite implemented methods |
| 3876 | for (int i = 0; i < ClsDefCount; i++) |
| 3877 | RewriteImplementationDecl(ClassImplementation[i]); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3878 | |
Fariborz Jahanian | c54d846 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 3879 | for (int i = 0; i < CatDefCount; i++) |
| 3880 | RewriteImplementationDecl(CategoryImplementation[i]); |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3881 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3882 | |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3883 | void RewriteObjC::SynthesizeMetaDataIntoBuffer(std::string &Result) { |
| 3884 | int ClsDefCount = ClassImplementation.size(); |
| 3885 | int CatDefCount = CategoryImplementation.size(); |
Fariborz Jahanian | ec201dc | 2010-02-26 01:42:20 +0000 | [diff] [blame] | 3886 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3887 | // For each implemented class, write out all its meta data. |
Fariborz Jahanian | c34409c | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3888 | for (int i = 0; i < ClsDefCount; i++) |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3889 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3890 | |
Fariborz Jahanian | b2f525d | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3891 | // For each implemented category, write out all its meta data. |
| 3892 | for (int i = 0; i < CatDefCount; i++) |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3893 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3894 | |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3895 | // Write objc_symtab metadata |
| 3896 | /* |
| 3897 | struct _objc_symtab |
| 3898 | { |
| 3899 | long sel_ref_cnt; |
| 3900 | SEL *refs; |
| 3901 | short cls_def_cnt; |
| 3902 | short cat_def_cnt; |
| 3903 | void *defs[cls_def_cnt + cat_def_cnt]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3904 | }; |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3905 | */ |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3906 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3907 | Result += "\nstruct _objc_symtab {\n"; |
| 3908 | Result += "\tlong sel_ref_cnt;\n"; |
| 3909 | Result += "\tSEL *refs;\n"; |
| 3910 | Result += "\tshort cls_def_cnt;\n"; |
| 3911 | Result += "\tshort cat_def_cnt;\n"; |
| 3912 | Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n"; |
| 3913 | Result += "};\n\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3914 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3915 | Result += "static struct _objc_symtab " |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3916 | "_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3917 | Result += "\t0, 0, " + utostr(ClsDefCount) |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3918 | + ", " + utostr(CatDefCount) + "\n"; |
| 3919 | for (int i = 0; i < ClsDefCount; i++) { |
| 3920 | Result += "\t,&_OBJC_CLASS_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3921 | Result += ClassImplementation[i]->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3922 | Result += "\n"; |
| 3923 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3924 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3925 | for (int i = 0; i < CatDefCount; i++) { |
| 3926 | Result += "\t,&_OBJC_CATEGORY_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3927 | Result += CategoryImplementation[i]->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3928 | Result += "_"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3929 | Result += CategoryImplementation[i]->getNameAsString(); |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3930 | Result += "\n"; |
| 3931 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3932 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3933 | Result += "};\n\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3934 | |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3935 | // Write objc_module metadata |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3936 | |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3937 | /* |
| 3938 | struct _objc_module { |
| 3939 | long version; |
| 3940 | long size; |
| 3941 | const char *name; |
| 3942 | struct _objc_symtab *symtab; |
| 3943 | } |
| 3944 | */ |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3945 | |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3946 | Result += "\nstruct _objc_module {\n"; |
| 3947 | Result += "\tlong version;\n"; |
| 3948 | Result += "\tlong size;\n"; |
| 3949 | Result += "\tconst char *name;\n"; |
| 3950 | Result += "\tstruct _objc_symtab *symtab;\n"; |
| 3951 | Result += "};\n\n"; |
| 3952 | Result += "static struct _objc_module " |
Steve Naroff | b327e49 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3953 | "_OBJC_MODULES __attribute__ ((used, section (\"__OBJC, __module_info\")))= {\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3954 | Result += "\t" + utostr(OBJC_ABI_VERSION) + |
Fariborz Jahanian | 99e96b0 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3955 | ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n"; |
Fariborz Jahanian | 51f2182 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3956 | Result += "};\n\n"; |
Steve Naroff | 945a3b1 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3957 | |
| 3958 | if (LangOpts.Microsoft) { |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3959 | if (ProtocolExprDecls.size()) { |
| 3960 | Result += "#pragma section(\".objc_protocol$B\",long,read,write)\n"; |
| 3961 | Result += "#pragma data_seg(push, \".objc_protocol$B\")\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3962 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3963 | E = ProtocolExprDecls.end(); I != E; ++I) { |
| 3964 | Result += "static struct _objc_protocol *_POINTER_OBJC_PROTOCOL_"; |
| 3965 | Result += (*I)->getNameAsString(); |
| 3966 | Result += " = &_OBJC_PROTOCOL_"; |
| 3967 | Result += (*I)->getNameAsString(); |
| 3968 | Result += ";\n"; |
| 3969 | } |
| 3970 | Result += "#pragma data_seg(pop)\n\n"; |
| 3971 | } |
Steve Naroff | 945a3b1 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3972 | Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n"; |
Steve Naroff | cab93d5 | 2008-05-07 00:06:16 +0000 | [diff] [blame] | 3973 | Result += "#pragma data_seg(push, \".objc_module_info$B\")\n"; |
Steve Naroff | 945a3b1 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3974 | Result += "static struct _objc_module *_POINTER_OBJC_MODULES = "; |
| 3975 | Result += "&_OBJC_MODULES;\n"; |
| 3976 | Result += "#pragma data_seg(pop)\n\n"; |
| 3977 | } |
Fariborz Jahanian | 93191af | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3978 | } |
Chris Lattner | a7c19fe | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 3979 | |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 3980 | void RewriteObjC::RewriteByRefString(std::string &ResultStr, |
| 3981 | const std::string &Name, |
| 3982 | ValueDecl *VD) { |
| 3983 | assert(BlockByRefDeclNo.count(VD) && |
| 3984 | "RewriteByRefString: ByRef decl missing"); |
| 3985 | ResultStr += "struct __Block_byref_" + Name + |
| 3986 | "_" + utostr(BlockByRefDeclNo[VD]) ; |
| 3987 | } |
| 3988 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3989 | std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, |
| 3990 | const char *funcName, |
| 3991 | std::string Tag) { |
| 3992 | const FunctionType *AFT = CE->getFunctionType(); |
| 3993 | QualType RT = AFT->getResultType(); |
| 3994 | std::string StructRef = "struct " + Tag; |
| 3995 | std::string S = "static " + RT.getAsString() + " __" + |
| 3996 | funcName + "_" + "block_func_" + utostr(i); |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 3997 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3998 | BlockDecl *BD = CE->getBlockDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3999 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4000 | if (isa<FunctionNoProtoType>(AFT)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4001 | // No user-supplied arguments. Still need to pass in a pointer to the |
Steve Naroff | f26a1d4 | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 4002 | // block (to reference imported block decl refs). |
| 4003 | S += "(" + StructRef + " *__cself)"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4004 | } else if (BD->param_empty()) { |
| 4005 | S += "(" + StructRef + " *__cself)"; |
| 4006 | } else { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4007 | const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4008 | assert(FT && "SynthesizeBlockFunc: No function proto"); |
| 4009 | S += '('; |
| 4010 | // first add the implicit argument. |
| 4011 | S += StructRef + " *__cself, "; |
| 4012 | std::string ParamStr; |
| 4013 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 4014 | E = BD->param_end(); AI != E; ++AI) { |
| 4015 | if (AI != BD->param_begin()) S += ", "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4016 | ParamStr = (*AI)->getNameAsString(); |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 4017 | (*AI)->getType().getAsStringInternal(ParamStr, Context->PrintingPolicy); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4018 | S += ParamStr; |
| 4019 | } |
| 4020 | if (FT->isVariadic()) { |
| 4021 | if (!BD->param_empty()) S += ", "; |
| 4022 | S += "..."; |
| 4023 | } |
| 4024 | S += ')'; |
| 4025 | } |
| 4026 | S += " {\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4027 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4028 | // Create local declarations to avoid rewriting all closure decl ref exprs. |
| 4029 | // First, emit a declaration for all "by ref" decls. |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4030 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4031 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 4032 | S += " "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4033 | std::string Name = (*I)->getNameAsString(); |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4034 | std::string TypeString; |
| 4035 | RewriteByRefString(TypeString, Name, (*I)); |
| 4036 | TypeString += " *"; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4037 | Name = TypeString + Name; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4038 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4039 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4040 | // Next, emit a declaration for all "by copy" declarations. |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4041 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4042 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 4043 | S += " "; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4044 | // Handle nested closure invocation. For example: |
| 4045 | // |
| 4046 | // void (^myImportedClosure)(void); |
| 4047 | // myImportedClosure = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4048 | // |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4049 | // void (^anotherClosure)(void); |
| 4050 | // anotherClosure = ^(void) { |
| 4051 | // myImportedClosure(); // import and invoke the closure |
| 4052 | // }; |
| 4053 | // |
Fariborz Jahanian | e1ff123 | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 4054 | if (isTopLevelBlockPointerType((*I)->getType())) { |
| 4055 | RewriteBlockPointerTypeVariable(S, (*I)); |
| 4056 | S += " = ("; |
| 4057 | RewriteBlockPointerType(S, (*I)->getType()); |
| 4058 | S += ")"; |
| 4059 | S += "__cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; |
| 4060 | } |
| 4061 | else { |
Fariborz Jahanian | b6a68c0 | 2010-02-16 17:26:03 +0000 | [diff] [blame] | 4062 | std::string Name = (*I)->getNameAsString(); |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 4063 | (*I)->getType().getAsStringInternal(Name, Context->PrintingPolicy); |
Fariborz Jahanian | e1ff123 | 2010-02-16 16:21:26 +0000 | [diff] [blame] | 4064 | S += Name + " = __cself->" + |
| 4065 | (*I)->getNameAsString() + "; // bound by copy\n"; |
| 4066 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4067 | } |
| 4068 | std::string RewrittenStr = RewrittenBlockExprs[CE]; |
| 4069 | const char *cstr = RewrittenStr.c_str(); |
| 4070 | while (*cstr++ != '{') ; |
| 4071 | S += cstr; |
| 4072 | S += "\n"; |
| 4073 | return S; |
| 4074 | } |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 4075 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4076 | std::string RewriteObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
| 4077 | const char *funcName, |
| 4078 | std::string Tag) { |
| 4079 | std::string StructRef = "struct " + Tag; |
| 4080 | std::string S = "static void __"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4081 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4082 | S += funcName; |
| 4083 | S += "_block_copy_" + utostr(i); |
| 4084 | S += "(" + StructRef; |
| 4085 | S += "*dst, " + StructRef; |
| 4086 | S += "*src) {"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4087 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4088 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Steve Naroff | 61d879e | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 4089 | S += "_Block_object_assign((void*)&dst->"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4090 | S += (*I)->getNameAsString(); |
Steve Naroff | 5ac4eac | 2008-12-11 20:51:38 +0000 | [diff] [blame] | 4091 | S += ", (void*)src->"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4092 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4093 | if (BlockByRefDeclsPtrSet.count((*I))) |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 4094 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | cbdcfe8 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 4095 | else |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 4096 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4097 | } |
Fariborz Jahanian | cbdcfe8 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 4098 | S += "}\n"; |
| 4099 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4100 | S += "\nstatic void __"; |
| 4101 | S += funcName; |
| 4102 | S += "_block_dispose_" + utostr(i); |
| 4103 | S += "(" + StructRef; |
| 4104 | S += "*src) {"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4105 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4106 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Steve Naroff | 61d879e | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 4107 | S += "_Block_object_dispose((void*)src->"; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4108 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4109 | if (BlockByRefDeclsPtrSet.count((*I))) |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 4110 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
Fariborz Jahanian | cbdcfe8 | 2009-12-23 20:32:38 +0000 | [diff] [blame] | 4111 | else |
Fariborz Jahanian | 4bf727d | 2009-12-23 21:18:41 +0000 | [diff] [blame] | 4112 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4113 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4114 | S += "}\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4115 | return S; |
| 4116 | } |
| 4117 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4118 | std::string RewriteObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 4119 | std::string Desc) { |
Steve Naroff | 295570a | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 4120 | std::string S = "\nstruct " + Tag; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4121 | std::string Constructor = " " + Tag; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4122 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4123 | S += " {\n struct __block_impl impl;\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4124 | S += " struct " + Desc; |
| 4125 | S += "* Desc;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4126 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4127 | Constructor += "(void *fp, "; // Invoke function pointer. |
| 4128 | Constructor += "struct " + Desc; // Descriptor pointer. |
| 4129 | Constructor += " *desc"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4130 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4131 | if (BlockDeclRefs.size()) { |
| 4132 | // Output all "by copy" declarations. |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4133 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4134 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 4135 | S += " "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4136 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4137 | std::string ArgName = "_" + FieldName; |
| 4138 | // Handle nested closure invocation. For example: |
| 4139 | // |
| 4140 | // void (^myImportedBlock)(void); |
| 4141 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4142 | // |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4143 | // void (^anotherBlock)(void); |
| 4144 | // anotherBlock = ^(void) { |
| 4145 | // myImportedBlock(); // import and invoke the closure |
| 4146 | // }; |
| 4147 | // |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4148 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4149 | S += "struct __block_impl *"; |
| 4150 | Constructor += ", void *" + ArgName; |
| 4151 | } else { |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 4152 | (*I)->getType().getAsStringInternal(FieldName, Context->PrintingPolicy); |
| 4153 | (*I)->getType().getAsStringInternal(ArgName, Context->PrintingPolicy); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4154 | Constructor += ", " + ArgName; |
| 4155 | } |
| 4156 | S += FieldName + ";\n"; |
| 4157 | } |
| 4158 | // Output all "by ref" declarations. |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4159 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4160 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 4161 | S += " "; |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4162 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4163 | std::string ArgName = "_" + FieldName; |
| 4164 | // Handle nested closure invocation. For example: |
| 4165 | // |
| 4166 | // void (^myImportedBlock)(void); |
| 4167 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4168 | // |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4169 | // void (^anotherBlock)(void); |
| 4170 | // anotherBlock = ^(void) { |
| 4171 | // myImportedBlock(); // import and invoke the closure |
| 4172 | // }; |
| 4173 | // |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4174 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4175 | S += "struct __block_impl *"; |
| 4176 | Constructor += ", void *" + ArgName; |
| 4177 | } else { |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4178 | std::string TypeString; |
| 4179 | RewriteByRefString(TypeString, FieldName, (*I)); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4180 | TypeString += " *"; |
| 4181 | FieldName = TypeString + FieldName; |
| 4182 | ArgName = TypeString + ArgName; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4183 | Constructor += ", " + ArgName; |
| 4184 | } |
| 4185 | S += FieldName + "; // by ref\n"; |
| 4186 | } |
| 4187 | // Finish writing the constructor. |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4188 | Constructor += ", int flags=0) {\n"; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4189 | if (GlobalVarDecl) |
| 4190 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 4191 | else |
| 4192 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4193 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4194 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4195 | Constructor += " Desc = desc;\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4196 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4197 | // Initialize all "by copy" arguments. |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4198 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4199 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4200 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4201 | Constructor += " "; |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4202 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4203 | Constructor += Name + " = (struct __block_impl *)_"; |
| 4204 | else |
| 4205 | Constructor += Name + " = _"; |
| 4206 | Constructor += Name + ";\n"; |
| 4207 | } |
| 4208 | // Initialize all "by ref" arguments. |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4209 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4210 | E = BlockByRefDecls.end(); I != E; ++I) { |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4211 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4212 | Constructor += " "; |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4213 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4214 | Constructor += Name + " = (struct __block_impl *)_"; |
| 4215 | else |
| 4216 | Constructor += Name + " = _"; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4217 | Constructor += Name + "->__forwarding;\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4218 | } |
| 4219 | } else { |
| 4220 | // Finish writing the constructor. |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4221 | Constructor += ", int flags=0) {\n"; |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4222 | if (GlobalVarDecl) |
| 4223 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 4224 | else |
| 4225 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4226 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 4227 | Constructor += " Desc = desc;\n"; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4228 | } |
| 4229 | Constructor += " "; |
| 4230 | Constructor += "}\n"; |
| 4231 | S += Constructor; |
| 4232 | S += "};\n"; |
| 4233 | return S; |
| 4234 | } |
| 4235 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4236 | std::string RewriteObjC::SynthesizeBlockDescriptor(std::string DescTag, |
| 4237 | std::string ImplTag, int i, |
| 4238 | const char *FunName, |
| 4239 | unsigned hasCopy) { |
| 4240 | std::string S = "\nstatic struct " + DescTag; |
| 4241 | |
| 4242 | S += " {\n unsigned long reserved;\n"; |
| 4243 | S += " unsigned long Block_size;\n"; |
| 4244 | if (hasCopy) { |
Fariborz Jahanian | e175eeb | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 4245 | S += " void (*copy)(struct "; |
| 4246 | S += ImplTag; S += "*, struct "; |
| 4247 | S += ImplTag; S += "*);\n"; |
| 4248 | |
| 4249 | S += " void (*dispose)(struct "; |
| 4250 | S += ImplTag; S += "*);\n"; |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4251 | } |
| 4252 | S += "} "; |
| 4253 | |
| 4254 | S += DescTag + "_DATA = { 0, sizeof(struct "; |
| 4255 | S += ImplTag + ")"; |
| 4256 | if (hasCopy) { |
| 4257 | S += ", __" + std::string(FunName) + "_block_copy_" + utostr(i); |
| 4258 | S += ", __" + std::string(FunName) + "_block_dispose_" + utostr(i); |
| 4259 | } |
| 4260 | S += "};\n"; |
| 4261 | return S; |
| 4262 | } |
| 4263 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4264 | void RewriteObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 4265 | const char *FunName) { |
| 4266 | // Insert declaration for the function in which block literal is used. |
Fariborz Jahanian | 5c26eee | 2010-01-15 18:14:52 +0000 | [diff] [blame] | 4267 | if (CurFunctionDeclToDeclareForBlock && !Blocks.empty()) |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 4268 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4269 | // Insert closures that were part of the function. |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4270 | for (unsigned i = 0, count=0; i < Blocks.size(); i++) { |
| 4271 | CollectBlockDeclRefInfo(Blocks[i]); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4272 | // Need to copy-in the inner copied-in variables not actually used in this |
| 4273 | // block. |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4274 | for (int j = 0; j < InnerDeclRefsCount[i]; j++) { |
| 4275 | BlockDeclRefExpr *Exp = InnerDeclRefs[count++]; |
| 4276 | ValueDecl *VD = Exp->getDecl(); |
| 4277 | BlockDeclRefs.push_back(Exp); |
| 4278 | if (!Exp->isByRef() && !BlockByCopyDeclsPtrSet.count(VD)) { |
| 4279 | BlockByCopyDeclsPtrSet.insert(VD); |
| 4280 | BlockByCopyDecls.push_back(VD); |
| 4281 | } |
| 4282 | if (Exp->isByRef() && !BlockByRefDeclsPtrSet.count(VD)) { |
| 4283 | BlockByRefDeclsPtrSet.insert(VD); |
| 4284 | BlockByRefDecls.push_back(VD); |
| 4285 | } |
| 4286 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4287 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4288 | std::string ImplTag = "__" + std::string(FunName) + "_block_impl_" + utostr(i); |
| 4289 | std::string DescTag = "__" + std::string(FunName) + "_block_desc_" + utostr(i); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4290 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4291 | std::string CI = SynthesizeBlockImpl(Blocks[i], ImplTag, DescTag); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4292 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4293 | InsertText(FunLocStart, CI); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4294 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4295 | std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, ImplTag); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4296 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4297 | InsertText(FunLocStart, CF); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4298 | |
| 4299 | if (ImportedBlockDecls.size()) { |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4300 | std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, ImplTag); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4301 | InsertText(FunLocStart, HF); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4302 | } |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 4303 | std::string BD = SynthesizeBlockDescriptor(DescTag, ImplTag, i, FunName, |
| 4304 | ImportedBlockDecls.size() > 0); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4305 | InsertText(FunLocStart, BD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4306 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4307 | BlockDeclRefs.clear(); |
| 4308 | BlockByRefDecls.clear(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4309 | BlockByRefDeclsPtrSet.clear(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4310 | BlockByCopyDecls.clear(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4311 | BlockByCopyDeclsPtrSet.clear(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4312 | ImportedBlockDecls.clear(); |
| 4313 | } |
| 4314 | Blocks.clear(); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4315 | InnerDeclRefsCount.clear(); |
| 4316 | InnerDeclRefs.clear(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4317 | RewrittenBlockExprs.clear(); |
| 4318 | } |
| 4319 | |
| 4320 | void RewriteObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { |
| 4321 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4322 | const char *FuncName = FD->getNameAsCString(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4323 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4324 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 4325 | } |
| 4326 | |
Fariborz Jahanian | c3bdefa | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 4327 | static void BuildUniqueMethodName(std::string &Name, |
| 4328 | ObjCMethodDecl *MD) { |
| 4329 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
| 4330 | Name = IFace->getNameAsCString(); |
| 4331 | Name += "__" + MD->getSelector().getAsString(); |
| 4332 | // Convert colons to underscores. |
| 4333 | std::string::size_type loc = 0; |
| 4334 | while ((loc = Name.find(":", loc)) != std::string::npos) |
| 4335 | Name.replace(loc, 1, "_"); |
| 4336 | } |
| 4337 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4338 | void RewriteObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { |
Steve Naroff | 295570a | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 4339 | //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n"); |
| 4340 | //SourceLocation FunLocStart = MD->getLocStart(); |
Fariborz Jahanian | b5f99c3 | 2010-01-29 01:55:49 +0000 | [diff] [blame] | 4341 | SourceLocation FunLocStart = MD->getLocStart(); |
Fariborz Jahanian | c3bdefa | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 4342 | std::string FuncName; |
| 4343 | BuildUniqueMethodName(FuncName, MD); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4344 | SynthesizeBlockLiterals(FunLocStart, FuncName.c_str()); |
| 4345 | } |
| 4346 | |
| 4347 | void RewriteObjC::GetBlockDeclRefExprs(Stmt *S) { |
| 4348 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4349 | CI != E; ++CI) |
| 4350 | if (*CI) { |
| 4351 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 4352 | GetBlockDeclRefExprs(CBE->getBody()); |
| 4353 | else |
| 4354 | GetBlockDeclRefExprs(*CI); |
| 4355 | } |
| 4356 | // Handle specific things. |
| 4357 | if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) |
| 4358 | // FIXME: Handle enums. |
| 4359 | if (!isa<FunctionDecl>(CDRE->getDecl())) |
| 4360 | BlockDeclRefs.push_back(CDRE); |
| 4361 | return; |
| 4362 | } |
| 4363 | |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4364 | void RewriteObjC::GetInnerBlockDeclRefExprs(Stmt *S, |
| 4365 | llvm::SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs, |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4366 | llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts) { |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4367 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4368 | CI != E; ++CI) |
| 4369 | if (*CI) { |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4370 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) { |
| 4371 | InnerContexts.insert(cast<DeclContext>(CBE->getBlockDecl())); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4372 | GetInnerBlockDeclRefExprs(CBE->getBody(), |
| 4373 | InnerBlockDeclRefs, |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4374 | InnerContexts); |
| 4375 | } |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4376 | else |
| 4377 | GetInnerBlockDeclRefExprs(*CI, |
| 4378 | InnerBlockDeclRefs, |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4379 | InnerContexts); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4380 | |
| 4381 | } |
| 4382 | // Handle specific things. |
| 4383 | if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) |
| 4384 | if (!isa<FunctionDecl>(CDRE->getDecl()) && |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4385 | !InnerContexts.count(CDRE->getDecl()->getDeclContext())) |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4386 | InnerBlockDeclRefs.push_back(CDRE); |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 4387 | |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4388 | return; |
| 4389 | } |
| 4390 | |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4391 | Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp, const Expr *BlockExp) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4392 | // Navigate to relevant type information. |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4393 | const BlockPointerType *CPT = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4394 | |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4395 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BlockExp)) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4396 | CPT = DRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4397 | } else if (const BlockDeclRefExpr *CDRE = |
| 4398 | dyn_cast<BlockDeclRefExpr>(BlockExp)) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4399 | CPT = CDRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4400 | } else if (const MemberExpr *MExpr = dyn_cast<MemberExpr>(BlockExp)) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4401 | CPT = MExpr->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4402 | } |
| 4403 | else if (const ParenExpr *PRE = dyn_cast<ParenExpr>(BlockExp)) { |
| 4404 | return SynthesizeBlockCall(Exp, PRE->getSubExpr()); |
| 4405 | } |
| 4406 | else if (const ImplicitCastExpr *IEXPR = dyn_cast<ImplicitCastExpr>(BlockExp)) |
| 4407 | CPT = IEXPR->getType()->getAs<BlockPointerType>(); |
| 4408 | else if (const ConditionalOperator *CEXPR = |
| 4409 | dyn_cast<ConditionalOperator>(BlockExp)) { |
| 4410 | Expr *LHSExp = CEXPR->getLHS(); |
| 4411 | Stmt *LHSStmt = SynthesizeBlockCall(Exp, LHSExp); |
| 4412 | Expr *RHSExp = CEXPR->getRHS(); |
| 4413 | Stmt *RHSStmt = SynthesizeBlockCall(Exp, RHSExp); |
| 4414 | Expr *CONDExp = CEXPR->getCond(); |
| 4415 | ConditionalOperator *CondExpr = |
| 4416 | new (Context) ConditionalOperator(CONDExp, |
| 4417 | SourceLocation(), cast<Expr>(LHSStmt), |
| 4418 | SourceLocation(), cast<Expr>(RHSStmt), |
| 4419 | Exp->getType()); |
| 4420 | return CondExpr; |
Fariborz Jahanian | 6ab7ed4 | 2009-12-18 01:15:21 +0000 | [diff] [blame] | 4421 | } else if (const ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(BlockExp)) { |
| 4422 | CPT = IRE->getType()->getAs<BlockPointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4423 | } else { |
| 4424 | assert(1 && "RewriteBlockClass: Bad type"); |
| 4425 | } |
| 4426 | assert(CPT && "RewriteBlockClass: Bad type"); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4427 | const FunctionType *FT = CPT->getPointeeType()->getAs<FunctionType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4428 | assert(FT && "RewriteBlockClass: Bad type"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4429 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4430 | // FTP will be null for closures that don't take arguments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4431 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4432 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
| 4433 | SourceLocation(), |
| 4434 | &Context->Idents.get("__block_impl")); |
| 4435 | QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD)); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4436 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4437 | // Generate a funky cast. |
| 4438 | llvm::SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4439 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4440 | // Push the block argument type. |
| 4441 | ArgTypes.push_back(PtrBlock); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4442 | if (FTP) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4443 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4444 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 4445 | QualType t = *I; |
| 4446 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4447 | if (isTopLevelBlockPointerType(t)) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4448 | const BlockPointerType *BPT = t->getAs<BlockPointerType>(); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4449 | t = Context->getPointerType(BPT->getPointeeType()); |
| 4450 | } |
| 4451 | ArgTypes.push_back(t); |
| 4452 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4453 | } |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4454 | // Now do the pointer to function cast. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4455 | QualType PtrToFuncCastType = Context->getFunctionType(Exp->getType(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 4456 | &ArgTypes[0], ArgTypes.size(), false/*no variadic*/, 0, |
| 4457 | false, false, 0, 0, |
| 4458 | false, CC_Default); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4459 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4460 | PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4461 | |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4462 | CastExpr *BlkCast = NoTypeInfoCStyleCastExpr(Context, PtrBlock, |
| 4463 | CastExpr::CK_Unknown, |
| 4464 | const_cast<Expr*>(BlockExp)); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4465 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4466 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 4467 | BlkCast); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4468 | //PE->dump(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4469 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4470 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4471 | &Context->Idents.get("FuncPtr"), Context->VoidPtrTy, 0, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 4472 | /*BitWidth=*/0, /*Mutable=*/true); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4473 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
| 4474 | FD->getType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4475 | |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4476 | CastExpr *FunkCast = NoTypeInfoCStyleCastExpr(Context, PtrToFuncCastType, |
| 4477 | CastExpr::CK_Unknown, ME); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4478 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4479 | |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4480 | llvm::SmallVector<Expr*, 8> BlkExprs; |
| 4481 | // Add the implicit argument. |
| 4482 | BlkExprs.push_back(BlkCast); |
| 4483 | // Add the user arguments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4484 | for (CallExpr::arg_iterator I = Exp->arg_begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4485 | E = Exp->arg_end(); I != E; ++I) { |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4486 | BlkExprs.push_back(*I); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4487 | } |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 4488 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &BlkExprs[0], |
| 4489 | BlkExprs.size(), |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4490 | Exp->getType(), SourceLocation()); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4491 | return CE; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4492 | } |
| 4493 | |
| 4494 | void RewriteObjC::RewriteBlockCall(CallExpr *Exp) { |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 4495 | Stmt *BlockCall = SynthesizeBlockCall(Exp, Exp->getCallee()); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4496 | ReplaceStmt(Exp, BlockCall); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4497 | } |
| 4498 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4499 | // We need to return the rewritten expression to handle cases where the |
| 4500 | // BlockDeclRefExpr is embedded in another expression being rewritten. |
| 4501 | // For example: |
| 4502 | // |
| 4503 | // int main() { |
| 4504 | // __block Foo *f; |
| 4505 | // __block int i; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4506 | // |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4507 | // void (^myblock)() = ^() { |
| 4508 | // [f test]; // f is a BlockDeclRefExpr embedded in a message (which is being rewritten). |
| 4509 | // i = 77; |
| 4510 | // }; |
| 4511 | //} |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4512 | Stmt *RewriteObjC::RewriteBlockDeclRefExpr(Expr *DeclRefExp) { |
Fariborz Jahanian | 25c07fa | 2009-12-23 19:26:34 +0000 | [diff] [blame] | 4513 | // Rewrite the byref variable into BYREFVAR->__forwarding->BYREFVAR |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4514 | // for each DeclRefExp where BYREFVAR is name of the variable. |
| 4515 | ValueDecl *VD; |
| 4516 | bool isArrow = true; |
| 4517 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(DeclRefExp)) |
| 4518 | VD = BDRE->getDecl(); |
| 4519 | else { |
| 4520 | VD = cast<DeclRefExpr>(DeclRefExp)->getDecl(); |
| 4521 | isArrow = false; |
| 4522 | } |
| 4523 | |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4524 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 4525 | &Context->Idents.get("__forwarding"), |
| 4526 | Context->VoidPtrTy, 0, |
| 4527 | /*BitWidth=*/0, /*Mutable=*/true); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4528 | MemberExpr *ME = new (Context) MemberExpr(DeclRefExp, isArrow, |
| 4529 | FD, SourceLocation(), |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4530 | FD->getType()); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4531 | |
| 4532 | const char *Name = VD->getNameAsCString(); |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4533 | FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 4534 | &Context->Idents.get(Name), |
| 4535 | Context->VoidPtrTy, 0, |
| 4536 | /*BitWidth=*/0, /*Mutable=*/true); |
| 4537 | ME = new (Context) MemberExpr(ME, true, FD, SourceLocation(), |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4538 | DeclRefExp->getType()); |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4539 | |
| 4540 | |
| 4541 | |
Steve Naroff | f26a1d4 | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 4542 | // Need parens to enforce precedence. |
Fariborz Jahanian | 7df3980 | 2009-12-23 19:22:33 +0000 | [diff] [blame] | 4543 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 4544 | ME); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4545 | ReplaceStmt(DeclRefExp, PE); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4546 | return PE; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4547 | } |
| 4548 | |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4549 | void RewriteObjC::RewriteCastExpr(CStyleCastExpr *CE) { |
| 4550 | SourceLocation LocStart = CE->getLParenLoc(); |
| 4551 | SourceLocation LocEnd = CE->getRParenLoc(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4552 | |
| 4553 | // Need to avoid trying to rewrite synthesized casts. |
| 4554 | if (LocStart.isInvalid()) |
| 4555 | return; |
Steve Naroff | 3e7ced1 | 2008-11-03 11:20:24 +0000 | [diff] [blame] | 4556 | // Need to avoid trying to rewrite casts contained in macros. |
| 4557 | if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) |
| 4558 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4559 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4560 | const char *startBuf = SM->getCharacterData(LocStart); |
| 4561 | const char *endBuf = SM->getCharacterData(LocEnd); |
Fariborz Jahanian | f3b9b95 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4562 | QualType QT = CE->getType(); |
| 4563 | const Type* TypePtr = QT->getAs<Type>(); |
| 4564 | if (isa<TypeOfExprType>(TypePtr)) { |
| 4565 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 4566 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 4567 | std::string TypeAsString = "("; |
Fariborz Jahanian | f506791 | 2010-02-18 01:20:22 +0000 | [diff] [blame] | 4568 | RewriteBlockPointerType(TypeAsString, QT); |
Fariborz Jahanian | f3b9b95 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4569 | TypeAsString += ")"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4570 | ReplaceText(LocStart, endBuf-startBuf+1, TypeAsString); |
Fariborz Jahanian | f3b9b95 | 2010-01-19 21:48:35 +0000 | [diff] [blame] | 4571 | return; |
| 4572 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4573 | // advance the location to startArgList. |
| 4574 | const char *argPtr = startBuf; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4575 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4576 | while (*argPtr++ && (argPtr < endBuf)) { |
| 4577 | switch (*argPtr) { |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4578 | case '^': |
| 4579 | // Replace the '^' with '*'. |
| 4580 | LocStart = LocStart.getFileLocWithOffset(argPtr-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4581 | ReplaceText(LocStart, 1, "*"); |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4582 | break; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4583 | } |
| 4584 | } |
| 4585 | return; |
| 4586 | } |
| 4587 | |
| 4588 | void RewriteObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { |
| 4589 | SourceLocation DeclLoc = FD->getLocation(); |
| 4590 | unsigned parenCount = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4591 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4592 | // We have 1 or more arguments that have closure pointers. |
| 4593 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4594 | const char *startArgList = strchr(startBuf, '('); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4595 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4596 | assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4597 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4598 | parenCount++; |
| 4599 | // advance the location to startArgList. |
| 4600 | DeclLoc = DeclLoc.getFileLocWithOffset(startArgList-startBuf); |
| 4601 | assert((DeclLoc.isValid()) && "Invalid DeclLoc"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4602 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4603 | const char *argPtr = startArgList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4604 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4605 | while (*argPtr++ && parenCount) { |
| 4606 | switch (*argPtr) { |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4607 | case '^': |
| 4608 | // Replace the '^' with '*'. |
| 4609 | DeclLoc = DeclLoc.getFileLocWithOffset(argPtr-startArgList); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4610 | ReplaceText(DeclLoc, 1, "*"); |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4611 | break; |
| 4612 | case '(': |
| 4613 | parenCount++; |
| 4614 | break; |
| 4615 | case ')': |
| 4616 | parenCount--; |
| 4617 | break; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4618 | } |
| 4619 | } |
| 4620 | return; |
| 4621 | } |
| 4622 | |
| 4623 | bool RewriteObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4624 | const FunctionProtoType *FTP; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4625 | const PointerType *PT = QT->getAs<PointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4626 | if (PT) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4627 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4628 | } else { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4629 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4630 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4631 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4632 | } |
| 4633 | if (FTP) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4634 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4635 | E = FTP->arg_type_end(); I != E; ++I) |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4636 | if (isTopLevelBlockPointerType(*I)) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4637 | return true; |
| 4638 | } |
| 4639 | return false; |
| 4640 | } |
| 4641 | |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4642 | void RewriteObjC::GetExtentOfArgList(const char *Name, const char *&LParen, |
| 4643 | const char *&RParen) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4644 | const char *argPtr = strchr(Name, '('); |
| 4645 | assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4646 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4647 | LParen = argPtr; // output the start. |
| 4648 | argPtr++; // skip past the left paren. |
| 4649 | unsigned parenCount = 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4650 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4651 | while (*argPtr && parenCount) { |
| 4652 | switch (*argPtr) { |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 4653 | case '(': parenCount++; break; |
| 4654 | case ')': parenCount--; break; |
| 4655 | default: break; |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4656 | } |
| 4657 | if (parenCount) argPtr++; |
| 4658 | } |
| 4659 | assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); |
| 4660 | RParen = argPtr; // output the end |
| 4661 | } |
| 4662 | |
| 4663 | void RewriteObjC::RewriteBlockPointerDecl(NamedDecl *ND) { |
| 4664 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 4665 | RewriteBlockPointerFunctionArgs(FD); |
| 4666 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4667 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4668 | // Handle Variables and Typedefs. |
| 4669 | SourceLocation DeclLoc = ND->getLocation(); |
| 4670 | QualType DeclT; |
| 4671 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4672 | DeclT = VD->getType(); |
| 4673 | else if (TypedefDecl *TDD = dyn_cast<TypedefDecl>(ND)) |
| 4674 | DeclT = TDD->getUnderlyingType(); |
| 4675 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) |
| 4676 | DeclT = FD->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4677 | else |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4678 | assert(0 && "RewriteBlockPointerDecl(): Decl type not yet handled"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4679 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4680 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4681 | const char *endBuf = startBuf; |
| 4682 | // scan backward (from the decl location) for the end of the previous decl. |
| 4683 | while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) |
| 4684 | startBuf--; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4685 | |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4686 | // *startBuf != '^' if we are dealing with a pointer to function that |
| 4687 | // may take block argument types (which will be handled below). |
| 4688 | if (*startBuf == '^') { |
| 4689 | // Replace the '^' with '*', computing a negative offset. |
| 4690 | DeclLoc = DeclLoc.getFileLocWithOffset(startBuf-endBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4691 | ReplaceText(DeclLoc, 1, "*"); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4692 | } |
| 4693 | if (PointerTypeTakesAnyBlockArguments(DeclT)) { |
| 4694 | // Replace the '^' with '*' for arguments. |
| 4695 | DeclLoc = ND->getLocation(); |
| 4696 | startBuf = SM->getCharacterData(DeclLoc); |
| 4697 | const char *argListBegin, *argListEnd; |
| 4698 | GetExtentOfArgList(startBuf, argListBegin, argListEnd); |
| 4699 | while (argListBegin < argListEnd) { |
| 4700 | if (*argListBegin == '^') { |
| 4701 | SourceLocation CaretLoc = DeclLoc.getFileLocWithOffset(argListBegin-startBuf); |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4702 | ReplaceText(CaretLoc, 1, "*"); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4703 | } |
| 4704 | argListBegin++; |
| 4705 | } |
| 4706 | } |
| 4707 | return; |
| 4708 | } |
| 4709 | |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4710 | |
| 4711 | /// SynthesizeByrefCopyDestroyHelper - This routine synthesizes: |
| 4712 | /// void __Block_byref_id_object_copy(struct Block_byref_id_object *dst, |
| 4713 | /// struct Block_byref_id_object *src) { |
| 4714 | /// _Block_object_assign (&_dest->object, _src->object, |
| 4715 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4716 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4717 | /// _Block_object_assign(&_dest->object, _src->object, |
| 4718 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4719 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4720 | /// } |
| 4721 | /// And: |
| 4722 | /// void __Block_byref_id_object_dispose(struct Block_byref_id_object *_src) { |
| 4723 | /// _Block_object_dispose(_src->object, |
| 4724 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4725 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4726 | /// _Block_object_dispose(_src->object, |
| 4727 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4728 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4729 | /// } |
| 4730 | |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4731 | std::string RewriteObjC::SynthesizeByrefCopyDestroyHelper(VarDecl *VD, |
| 4732 | int flag) { |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4733 | std::string S; |
Benjamin Kramer | e056cea | 2010-01-10 19:57:50 +0000 | [diff] [blame] | 4734 | if (CopyDestroyCache.count(flag)) |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4735 | return S; |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4736 | CopyDestroyCache.insert(flag); |
| 4737 | S = "static void __Block_byref_id_object_copy_"; |
| 4738 | S += utostr(flag); |
| 4739 | S += "(void *dst, void *src) {\n"; |
| 4740 | |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4741 | // offset into the object pointer is computed as: |
| 4742 | // void * + void* + int + int + void* + void * |
| 4743 | unsigned IntSize = |
| 4744 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 4745 | unsigned VoidPtrSize = |
| 4746 | static_cast<unsigned>(Context->getTypeSize(Context->VoidPtrTy)); |
| 4747 | |
| 4748 | unsigned offset = (VoidPtrSize*4 + IntSize + IntSize)/8; |
| 4749 | S += " _Block_object_assign((char*)dst + "; |
| 4750 | S += utostr(offset); |
| 4751 | S += ", *(void * *) ((char*)src + "; |
| 4752 | S += utostr(offset); |
| 4753 | S += "), "; |
| 4754 | S += utostr(flag); |
| 4755 | S += ");\n}\n"; |
| 4756 | |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4757 | S += "static void __Block_byref_id_object_dispose_"; |
| 4758 | S += utostr(flag); |
| 4759 | S += "(void *src) {\n"; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4760 | S += " _Block_object_dispose(*(void * *) ((char*)src + "; |
| 4761 | S += utostr(offset); |
| 4762 | S += "), "; |
| 4763 | S += utostr(flag); |
| 4764 | S += ");\n}\n"; |
| 4765 | return S; |
| 4766 | } |
| 4767 | |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4768 | /// RewriteByRefVar - For each __block typex ND variable this routine transforms |
| 4769 | /// the declaration into: |
| 4770 | /// struct __Block_byref_ND { |
| 4771 | /// void *__isa; // NULL for everything except __weak pointers |
| 4772 | /// struct __Block_byref_ND *__forwarding; |
| 4773 | /// int32_t __flags; |
| 4774 | /// int32_t __size; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4775 | /// void *__Block_byref_id_object_copy; // If variable is __block ObjC object |
| 4776 | /// void *__Block_byref_id_object_dispose; // If variable is __block ObjC object |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4777 | /// typex ND; |
| 4778 | /// }; |
| 4779 | /// |
| 4780 | /// It then replaces declaration of ND variable with: |
| 4781 | /// struct __Block_byref_ND ND = {__isa=0B, __forwarding=&ND, __flags=some_flag, |
| 4782 | /// __size=sizeof(struct __Block_byref_ND), |
| 4783 | /// ND=initializer-if-any}; |
| 4784 | /// |
| 4785 | /// |
| 4786 | void RewriteObjC::RewriteByRefVar(VarDecl *ND) { |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 4787 | // Insert declaration for the function in which block literal is |
| 4788 | // used. |
| 4789 | if (CurFunctionDeclToDeclareForBlock) |
| 4790 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4791 | int flag = 0; |
| 4792 | int isa = 0; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4793 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
Fariborz Jahanian | 6005bd8 | 2010-02-26 22:49:11 +0000 | [diff] [blame] | 4794 | if (DeclLoc.isInvalid()) |
| 4795 | // If type location is missing, it is because of missing type (a warning). |
| 4796 | // Use variable's location which is good for this case. |
| 4797 | DeclLoc = ND->getLocation(); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4798 | const char *startBuf = SM->getCharacterData(DeclLoc); |
Fariborz Jahanian | 92368a1 | 2009-12-30 20:38:08 +0000 | [diff] [blame] | 4799 | SourceLocation X = ND->getLocEnd(); |
| 4800 | X = SM->getInstantiationLoc(X); |
| 4801 | const char *endBuf = SM->getCharacterData(X); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4802 | std::string Name(ND->getNameAsString()); |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4803 | std::string ByrefType; |
| 4804 | RewriteByRefString(ByrefType, Name, ND); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4805 | ByrefType += " {\n"; |
| 4806 | ByrefType += " void *__isa;\n"; |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4807 | RewriteByRefString(ByrefType, Name, ND); |
| 4808 | ByrefType += " *__forwarding;\n"; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4809 | ByrefType += " int __flags;\n"; |
| 4810 | ByrefType += " int __size;\n"; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4811 | // Add void *__Block_byref_id_object_copy; |
| 4812 | // void *__Block_byref_id_object_dispose; if needed. |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4813 | QualType Ty = ND->getType(); |
| 4814 | bool HasCopyAndDispose = Context->BlockRequiresCopying(Ty); |
| 4815 | if (HasCopyAndDispose) { |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4816 | ByrefType += " void (*__Block_byref_id_object_copy)(void*, void*);\n"; |
| 4817 | ByrefType += " void (*__Block_byref_id_object_dispose)(void*);\n"; |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4818 | } |
| 4819 | |
| 4820 | Ty.getAsStringInternal(Name, Context->PrintingPolicy); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4821 | ByrefType += " " + Name + ";\n"; |
| 4822 | ByrefType += "};\n"; |
| 4823 | // Insert this type in global scope. It is needed by helper function. |
Fariborz Jahanian | faf85c0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4824 | SourceLocation FunLocStart; |
| 4825 | if (CurFunctionDef) |
| 4826 | FunLocStart = CurFunctionDef->getTypeSpecStartLoc(); |
| 4827 | else { |
| 4828 | assert(CurMethodDef && "RewriteByRefVar - CurMethodDef is null"); |
| 4829 | FunLocStart = CurMethodDef->getLocStart(); |
| 4830 | } |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4831 | InsertText(FunLocStart, ByrefType); |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4832 | if (Ty.isObjCGCWeak()) { |
| 4833 | flag |= BLOCK_FIELD_IS_WEAK; |
| 4834 | isa = 1; |
| 4835 | } |
| 4836 | |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4837 | if (HasCopyAndDispose) { |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4838 | flag = BLOCK_BYREF_CALLER; |
| 4839 | QualType Ty = ND->getType(); |
| 4840 | // FIXME. Handle __weak variable (BLOCK_FIELD_IS_WEAK) as well. |
| 4841 | if (Ty->isBlockPointerType()) |
| 4842 | flag |= BLOCK_FIELD_IS_BLOCK; |
| 4843 | else |
| 4844 | flag |= BLOCK_FIELD_IS_OBJECT; |
| 4845 | std::string HF = SynthesizeByrefCopyDestroyHelper(ND, flag); |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4846 | if (!HF.empty()) |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4847 | InsertText(FunLocStart, HF); |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4848 | } |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4849 | |
| 4850 | // struct __Block_byref_ND ND = |
| 4851 | // {0, &ND, some_flag, __size=sizeof(struct __Block_byref_ND), |
| 4852 | // initializer-if-any}; |
| 4853 | bool hasInit = (ND->getInit() != 0); |
Fariborz Jahanian | f794543 | 2010-01-05 18:15:57 +0000 | [diff] [blame] | 4854 | unsigned flags = 0; |
| 4855 | if (HasCopyAndDispose) |
| 4856 | flags |= BLOCK_HAS_COPY_DISPOSE; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4857 | Name = ND->getNameAsString(); |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4858 | ByrefType.clear(); |
| 4859 | RewriteByRefString(ByrefType, Name, ND); |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4860 | std::string ForwardingCastType("("); |
| 4861 | ForwardingCastType += ByrefType + " *)"; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4862 | if (!hasInit) { |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4863 | ByrefType += " " + Name + " = {(void*)"; |
| 4864 | ByrefType += utostr(isa); |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4865 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4866 | ByrefType += utostr(flags); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4867 | ByrefType += ", "; |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4868 | ByrefType += "sizeof("; |
| 4869 | RewriteByRefString(ByrefType, Name, ND); |
| 4870 | ByrefType += ")"; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4871 | if (HasCopyAndDispose) { |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4872 | ByrefType += ", __Block_byref_id_object_copy_"; |
| 4873 | ByrefType += utostr(flag); |
| 4874 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 4875 | ByrefType += utostr(flag); |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4876 | } |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4877 | ByrefType += "};\n"; |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4878 | ReplaceText(DeclLoc, endBuf-startBuf+Name.size(), ByrefType); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4879 | } |
| 4880 | else { |
Fariborz Jahanian | faf85c0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4881 | SourceLocation startLoc; |
| 4882 | Expr *E = ND->getInit(); |
| 4883 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 4884 | startLoc = ECE->getLParenLoc(); |
| 4885 | else |
| 4886 | startLoc = E->getLocStart(); |
Fariborz Jahanian | b8646ed | 2010-01-05 23:06:29 +0000 | [diff] [blame] | 4887 | startLoc = SM->getInstantiationLoc(startLoc); |
Fariborz Jahanian | faf85c0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4888 | endBuf = SM->getCharacterData(startLoc); |
| 4889 | |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4890 | ByrefType += " " + Name; |
Fariborz Jahanian | faf85c0 | 2010-01-16 19:36:43 +0000 | [diff] [blame] | 4891 | ByrefType += " = {(void*)"; |
Fariborz Jahanian | 7fac655 | 2010-01-05 19:21:35 +0000 | [diff] [blame] | 4892 | ByrefType += utostr(isa); |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 4893 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4894 | ByrefType += utostr(flags); |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 4895 | ByrefType += ", "; |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 4896 | ByrefType += "sizeof("; |
| 4897 | RewriteByRefString(ByrefType, Name, ND); |
| 4898 | ByrefType += "), "; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4899 | if (HasCopyAndDispose) { |
Fariborz Jahanian | e389158 | 2010-01-05 18:04:40 +0000 | [diff] [blame] | 4900 | ByrefType += "__Block_byref_id_object_copy_"; |
| 4901 | ByrefType += utostr(flag); |
| 4902 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 4903 | ByrefType += utostr(flag); |
| 4904 | ByrefType += ", "; |
Fariborz Jahanian | 8c07e75 | 2010-01-05 01:16:51 +0000 | [diff] [blame] | 4905 | } |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4906 | ReplaceText(DeclLoc, endBuf-startBuf, ByrefType); |
Steve Naroff | 1346837 | 2009-12-23 17:24:33 +0000 | [diff] [blame] | 4907 | |
| 4908 | // Complete the newly synthesized compound expression by inserting a right |
| 4909 | // curly brace before the end of the declaration. |
| 4910 | // FIXME: This approach avoids rewriting the initializer expression. It |
| 4911 | // also assumes there is only one declarator. For example, the following |
| 4912 | // isn't currently supported by this routine (in general): |
| 4913 | // |
| 4914 | // double __block BYREFVAR = 1.34, BYREFVAR2 = 1.37; |
| 4915 | // |
| 4916 | const char *startBuf = SM->getCharacterData(startLoc); |
| 4917 | const char *semiBuf = strchr(startBuf, ';'); |
| 4918 | assert((*semiBuf == ';') && "RewriteByRefVar: can't find ';'"); |
| 4919 | SourceLocation semiLoc = |
| 4920 | startLoc.getFileLocWithOffset(semiBuf-startBuf); |
| 4921 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 4922 | InsertText(semiLoc, "}"); |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 4923 | } |
Fariborz Jahanian | 8120346 | 2009-12-22 00:48:54 +0000 | [diff] [blame] | 4924 | return; |
| 4925 | } |
| 4926 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4927 | void RewriteObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4928 | // Add initializers for any closure decl refs. |
| 4929 | GetBlockDeclRefExprs(Exp->getBody()); |
| 4930 | if (BlockDeclRefs.size()) { |
| 4931 | // Unique all "by copy" declarations. |
| 4932 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4933 | if (!BlockDeclRefs[i]->isByRef()) { |
| 4934 | if (!BlockByCopyDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 4935 | BlockByCopyDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 4936 | BlockByCopyDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 4937 | } |
| 4938 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4939 | // Unique all "by ref" declarations. |
| 4940 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 4941 | if (BlockDeclRefs[i]->isByRef()) { |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 4942 | if (!BlockByRefDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 4943 | BlockByRefDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 4944 | BlockByRefDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 4945 | } |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4946 | } |
| 4947 | // Find any imported blocks...they will need special attention. |
| 4948 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
Fariborz Jahanian | e175eeb | 2009-12-21 23:31:42 +0000 | [diff] [blame] | 4949 | if (BlockDeclRefs[i]->isByRef() || |
| 4950 | BlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
Fariborz Jahanian | 9bbc148 | 2010-02-26 21:46:27 +0000 | [diff] [blame] | 4951 | BlockDeclRefs[i]->getType()->isBlockPointerType()) |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4952 | ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); |
Steve Naroff | 677ab3a | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4953 | } |
| 4954 | } |
| 4955 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4956 | FunctionDecl *RewriteObjC::SynthBlockInitFunctionDecl(const char *name) { |
| 4957 | IdentifierInfo *ID = &Context->Idents.get(name); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4958 | QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4959 | return FunctionDecl::Create(*Context, TUDecl,SourceLocation(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 4960 | ID, FType, 0, FunctionDecl::Extern, false, |
Douglas Gregor | 739ef0c | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 4961 | false); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4962 | } |
| 4963 | |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4964 | Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp, |
| 4965 | const llvm::SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4966 | Blocks.push_back(Exp); |
| 4967 | |
| 4968 | CollectBlockDeclRefInfo(Exp); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4969 | |
| 4970 | // Add inner imported variables now used in current block. |
| 4971 | int countOfInnerDecls = 0; |
Fariborz Jahanian | be730c9 | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4972 | if (!InnerBlockDeclRefs.empty()) { |
| 4973 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) { |
| 4974 | BlockDeclRefExpr *Exp = InnerBlockDeclRefs[i]; |
| 4975 | ValueDecl *VD = Exp->getDecl(); |
| 4976 | if (!Exp->isByRef() && !BlockByCopyDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4977 | // We need to save the copied-in variables in nested |
| 4978 | // blocks because it is needed at the end for some of the API generations. |
| 4979 | // See SynthesizeBlockLiterals routine. |
Fariborz Jahanian | be730c9 | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4980 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 4981 | BlockDeclRefs.push_back(Exp); |
| 4982 | BlockByCopyDeclsPtrSet.insert(VD); |
| 4983 | BlockByCopyDecls.push_back(VD); |
| 4984 | } |
| 4985 | if (Exp->isByRef() && !BlockByRefDeclsPtrSet.count(VD)) { |
| 4986 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 4987 | BlockDeclRefs.push_back(Exp); |
| 4988 | BlockByRefDeclsPtrSet.insert(VD); |
| 4989 | BlockByRefDecls.push_back(VD); |
| 4990 | } |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4991 | } |
Fariborz Jahanian | be730c9 | 2010-02-26 22:36:30 +0000 | [diff] [blame] | 4992 | // Find any imported blocks...they will need special attention. |
| 4993 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) |
| 4994 | if (InnerBlockDeclRefs[i]->isByRef() || |
| 4995 | InnerBlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
| 4996 | InnerBlockDeclRefs[i]->getType()->isBlockPointerType()) |
| 4997 | ImportedBlockDecls.insert(InnerBlockDeclRefs[i]->getDecl()); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 4998 | } |
| 4999 | InnerDeclRefsCount.push_back(countOfInnerDecls); |
| 5000 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5001 | std::string FuncName; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5002 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5003 | if (CurFunctionDef) |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 5004 | FuncName = CurFunctionDef->getNameAsString(); |
Fariborz Jahanian | c3bdefa | 2010-02-10 20:18:25 +0000 | [diff] [blame] | 5005 | else if (CurMethodDef) |
| 5006 | BuildUniqueMethodName(FuncName, CurMethodDef); |
| 5007 | else if (GlobalVarDecl) |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5008 | FuncName = std::string(GlobalVarDecl->getNameAsString()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5009 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5010 | std::string BlockNumber = utostr(Blocks.size()-1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5011 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5012 | std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber; |
| 5013 | std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5014 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5015 | // Get a pointer to the function type so we can cast appropriately. |
| 5016 | QualType FType = Context->getPointerType(QualType(Exp->getFunctionType(),0)); |
| 5017 | |
| 5018 | FunctionDecl *FD; |
| 5019 | Expr *NewRep; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5020 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5021 | // Simulate a contructor call... |
| 5022 | FD = SynthBlockInitFunctionDecl(Tag.c_str()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5023 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, FType, SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5024 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5025 | llvm::SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5026 | |
Steve Naroff | e251423 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 5027 | // Initialize the block function. |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5028 | FD = SynthBlockInitFunctionDecl(Func.c_str()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5029 | DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, FD->getType(), |
| 5030 | SourceLocation()); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 5031 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
| 5032 | CastExpr::CK_Unknown, Arg); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5033 | InitExprs.push_back(castExpr); |
| 5034 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5035 | // Initialize the block descriptor. |
| 5036 | std::string DescData = "__" + FuncName + "_block_desc_" + BlockNumber + "_DATA"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5037 | |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5038 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
| 5039 | &Context->Idents.get(DescData.c_str()), |
| 5040 | Context->VoidPtrTy, 0, |
| 5041 | VarDecl::Static); |
| 5042 | UnaryOperator *DescRefExpr = new (Context) UnaryOperator( |
| 5043 | new (Context) DeclRefExpr(NewVD, |
| 5044 | Context->VoidPtrTy, SourceLocation()), |
| 5045 | UnaryOperator::AddrOf, |
| 5046 | Context->getPointerType(Context->VoidPtrTy), |
| 5047 | SourceLocation()); |
| 5048 | InitExprs.push_back(DescRefExpr); |
| 5049 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5050 | // Add initializers for any closure decl refs. |
| 5051 | if (BlockDeclRefs.size()) { |
Steve Naroff | e251423 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 5052 | Expr *Exp; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5053 | // Output all "by copy" declarations. |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5054 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5055 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5056 | if (isObjCType((*I)->getType())) { |
Steve Naroff | e251423 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 5057 | // FIXME: Conform to ABI ([[obj retain] autorelease]). |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 5058 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5059 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5060 | } else if (isTopLevelBlockPointerType((*I)->getType())) { |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 5061 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5062 | Arg = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 5063 | Exp = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
| 5064 | CastExpr::CK_Unknown, Arg); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5065 | } else { |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 5066 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5067 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5068 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5069 | InitExprs.push_back(Exp); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5070 | } |
| 5071 | // Output all "by ref" declarations. |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5072 | for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5073 | E = BlockByRefDecls.end(); I != E; ++I) { |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5074 | ValueDecl *ND = (*I); |
| 5075 | std::string Name(ND->getNameAsString()); |
| 5076 | std::string RecName; |
| 5077 | RewriteByRefString(RecName, Name, ND); |
| 5078 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str() |
| 5079 | + sizeof("struct")); |
| 5080 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
| 5081 | SourceLocation(), II); |
| 5082 | assert(RD && "SynthBlockInitExpr(): Can't find RecordDecl"); |
| 5083 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 5084 | |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 5085 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5086 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 5087 | Exp = new (Context) UnaryOperator(Exp, UnaryOperator::AddrOf, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5088 | Context->getPointerType(Exp->getType()), |
Steve Naroff | e251423 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 5089 | SourceLocation()); |
Fariborz Jahanian | b8355e3 | 2010-02-04 00:07:58 +0000 | [diff] [blame] | 5090 | Exp = NoTypeInfoCStyleCastExpr(Context, castT, CastExpr::CK_Unknown, Exp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5091 | InitExprs.push_back(Exp); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5092 | } |
| 5093 | } |
Fariborz Jahanian | 65e6bd6 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 5094 | if (ImportedBlockDecls.size()) { |
| 5095 | // generate BLOCK_HAS_COPY_DISPOSE(have helper funcs) | BLOCK_HAS_DESCRIPTOR |
| 5096 | int flag = (BLOCK_HAS_COPY_DISPOSE | BLOCK_HAS_DESCRIPTOR); |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5097 | unsigned IntSize = |
| 5098 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Fariborz Jahanian | 65e6bd6 | 2009-12-23 21:52:32 +0000 | [diff] [blame] | 5099 | Expr *FlagExp = new (Context) IntegerLiteral(llvm::APInt(IntSize, flag), |
| 5100 | Context->IntTy, SourceLocation()); |
| 5101 | InitExprs.push_back(FlagExp); |
Steve Naroff | 3048470 | 2009-12-06 21:14:13 +0000 | [diff] [blame] | 5102 | } |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 5103 | NewRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], InitExprs.size(), |
| 5104 | FType, SourceLocation()); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5105 | NewRep = new (Context) UnaryOperator(NewRep, UnaryOperator::AddrOf, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5106 | Context->getPointerType(NewRep->getType()), |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5107 | SourceLocation()); |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 5108 | NewRep = NoTypeInfoCStyleCastExpr(Context, FType, CastExpr::CK_Unknown, |
| 5109 | NewRep); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5110 | BlockDeclRefs.clear(); |
| 5111 | BlockByRefDecls.clear(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5112 | BlockByRefDeclsPtrSet.clear(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5113 | BlockByCopyDecls.clear(); |
Fariborz Jahanian | 4c4ca5a | 2010-02-11 23:35:57 +0000 | [diff] [blame] | 5114 | BlockByCopyDeclsPtrSet.clear(); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5115 | ImportedBlockDecls.clear(); |
| 5116 | return NewRep; |
| 5117 | } |
| 5118 | |
| 5119 | //===----------------------------------------------------------------------===// |
| 5120 | // Function Body / Expression rewriting |
| 5121 | //===----------------------------------------------------------------------===// |
| 5122 | |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5123 | // This is run as a first "pass" prior to RewriteFunctionBodyOrGlobalInitializer(). |
| 5124 | // The allows the main rewrite loop to associate all ObjCPropertyRefExprs with |
| 5125 | // their respective BinaryOperator. Without this knowledge, we'd need to rewrite |
| 5126 | // the ObjCPropertyRefExpr twice (once as a getter, and later as a setter). |
| 5127 | // Since the rewriter isn't capable of rewriting rewritten code, it's important |
| 5128 | // we get this right. |
| 5129 | void RewriteObjC::CollectPropertySetters(Stmt *S) { |
| 5130 | // Perform a bottom up traversal of all children. |
| 5131 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 5132 | CI != E; ++CI) |
| 5133 | if (*CI) |
| 5134 | CollectPropertySetters(*CI); |
| 5135 | |
| 5136 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) { |
| 5137 | if (BinOp->isAssignmentOp()) { |
| 5138 | if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS())) |
| 5139 | PropSetters[PRE] = BinOp; |
| 5140 | } |
| 5141 | } |
| 5142 | } |
| 5143 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5144 | Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5145 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5146 | isa<DoStmt>(S) || isa<ForStmt>(S)) |
| 5147 | Stmts.push_back(S); |
| 5148 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 5149 | Stmts.push_back(S); |
Chris Lattner | b71980f | 2010-01-09 21:45:57 +0000 | [diff] [blame] | 5150 | ObjCBcLabelNo.push_back(++BcLabelCount); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5151 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5152 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5153 | SourceRange OrigStmtRange = S->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5154 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5155 | // Perform a bottom up rewrite of all children. |
| 5156 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 5157 | CI != E; ++CI) |
| 5158 | if (*CI) { |
Fariborz Jahanian | 80fadb5 | 2010-02-05 01:35:00 +0000 | [diff] [blame] | 5159 | Stmt *newStmt; |
| 5160 | Stmt *S = (*CI); |
| 5161 | if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) { |
| 5162 | Expr *OldBase = IvarRefExpr->getBase(); |
| 5163 | bool replaced = false; |
| 5164 | newStmt = RewriteObjCNestedIvarRefExpr(S, replaced); |
| 5165 | if (replaced) { |
| 5166 | if (ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(newStmt)) |
| 5167 | ReplaceStmt(OldBase, IRE->getBase()); |
| 5168 | else |
| 5169 | ReplaceStmt(S, newStmt); |
| 5170 | } |
| 5171 | } |
| 5172 | else |
| 5173 | newStmt = RewriteFunctionBodyOrGlobalInitializer(S); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5174 | if (newStmt) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5175 | *CI = newStmt; |
| 5176 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5177 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5178 | if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5179 | llvm::SmallVector<BlockDeclRefExpr *, 8> InnerBlockDeclRefs; |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 5180 | llvm::SmallPtrSet<const DeclContext *, 8> InnerContexts; |
| 5181 | InnerContexts.insert(BE->getBlockDecl()); |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5182 | GetInnerBlockDeclRefExprs(BE->getBody(), |
Fariborz Jahanian | f4609d4 | 2010-03-01 23:36:21 +0000 | [diff] [blame] | 5183 | InnerBlockDeclRefs, InnerContexts); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5184 | // Rewrite the block body in place. |
| 5185 | RewriteFunctionBodyOrGlobalInitializer(BE->getBody()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5186 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5187 | // Now we snarf the rewritten text and stash it away for later use. |
Ted Kremenek | db2ef37 | 2010-01-07 18:00:35 +0000 | [diff] [blame] | 5188 | std::string Str = Rewrite.getRewrittenText(BE->getSourceRange()); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5189 | RewrittenBlockExprs[BE] = Str; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5190 | |
Fariborz Jahanian | 8652be0 | 2010-02-24 22:48:18 +0000 | [diff] [blame] | 5191 | Stmt *blockTranscribed = SynthBlockInitExpr(BE, InnerBlockDeclRefs); |
| 5192 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5193 | //blockTranscribed->dump(); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5194 | ReplaceStmt(S, blockTranscribed); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5195 | return blockTranscribed; |
| 5196 | } |
| 5197 | // Handle specific things. |
| 5198 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 5199 | return RewriteAtEncode(AtEncode); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5200 | |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5201 | if (ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(S)) { |
| 5202 | BinaryOperator *BinOp = PropSetters[PropRefExpr]; |
| 5203 | if (BinOp) { |
| 5204 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 5205 | // we need to rewrite the right hand side prior to rewriting the setter. |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 5206 | DisableReplaceStmt = true; |
| 5207 | // Save the source range. Even if we disable the replacement, the |
| 5208 | // rewritten node will have been inserted into the tree. If the synthesized |
| 5209 | // node is at the 'end', the rewriter will fail. Consider this: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5210 | // self.errorHandler = handler ? handler : |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 5211 | // ^(NSURL *errorURL, NSError *error) { return (BOOL)1; }; |
| 5212 | SourceRange SrcRange = BinOp->getSourceRange(); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5213 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(BinOp->getRHS()); |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 5214 | DisableReplaceStmt = false; |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 5215 | // |
| 5216 | // Unlike the main iterator, we explicily avoid changing 'BinOp'. If |
| 5217 | // we changed the RHS of BinOp, the rewriter would fail (since it needs |
| 5218 | // to see the original expression). Consider this example: |
| 5219 | // |
| 5220 | // Foo *obj1, *obj2; |
| 5221 | // |
| 5222 | // obj1.i = [obj2 rrrr]; |
| 5223 | // |
| 5224 | // 'BinOp' for the previous expression looks like: |
| 5225 | // |
| 5226 | // (BinaryOperator 0x231ccf0 'int' '=' |
| 5227 | // (ObjCPropertyRefExpr 0x231cc70 'int' Kind=PropertyRef Property="i" |
| 5228 | // (DeclRefExpr 0x231cc50 'Foo *' Var='obj1' 0x231cbb0)) |
| 5229 | // (ObjCMessageExpr 0x231ccb0 'int' selector=rrrr |
| 5230 | // (DeclRefExpr 0x231cc90 'Foo *' Var='obj2' 0x231cbe0))) |
| 5231 | // |
| 5232 | // 'newStmt' represents the rewritten message expression. For example: |
| 5233 | // |
| 5234 | // (CallExpr 0x231d300 'id':'struct objc_object *' |
| 5235 | // (ParenExpr 0x231d2e0 'int (*)(id, SEL)' |
| 5236 | // (CStyleCastExpr 0x231d2c0 'int (*)(id, SEL)' |
| 5237 | // (CStyleCastExpr 0x231d220 'void *' |
| 5238 | // (DeclRefExpr 0x231d200 'id (id, SEL, ...)' FunctionDecl='objc_msgSend' 0x231cdc0)))) |
| 5239 | // |
| 5240 | // Note that 'newStmt' is passed to RewritePropertySetter so that it |
| 5241 | // can be used as the setter argument. ReplaceStmt() will still 'see' |
| 5242 | // the original RHS (since we haven't altered BinOp). |
| 5243 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5244 | // This implies the Rewrite* routines can no longer delete the original |
Steve Naroff | 22216db | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 5245 | // node. As a result, we now leak the original AST nodes. |
| 5246 | // |
Steve Naroff | 08628db | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 5247 | return RewritePropertySetter(BinOp, dyn_cast<Expr>(newStmt), SrcRange); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5248 | } else { |
| 5249 | return RewritePropertyGetter(PropRefExpr); |
Steve Naroff | f326f40 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 5250 | } |
| 5251 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5252 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 5253 | return RewriteAtSelector(AtSelector); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5254 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5255 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 5256 | return RewriteObjCStringLiteral(AtString); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5257 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5258 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5259 | #if 0 |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5260 | // Before we rewrite it, put the original message expression in a comment. |
| 5261 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 5262 | SourceLocation endLoc = MessExpr->getLocEnd(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5263 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5264 | const char *startBuf = SM->getCharacterData(startLoc); |
| 5265 | const char *endBuf = SM->getCharacterData(endLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5266 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5267 | std::string messString; |
| 5268 | messString += "// "; |
| 5269 | messString.append(startBuf, endBuf-startBuf+1); |
| 5270 | messString += "\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5271 | |
| 5272 | // FIXME: Missing definition of |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5273 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
| 5274 | // InsertText(startLoc, messString.c_str(), messString.size()); |
| 5275 | // Tried this, but it didn't work either... |
| 5276 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5277 | #endif |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5278 | return RewriteMessageExpr(MessExpr); |
| 5279 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5280 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5281 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 5282 | return RewriteObjCTryStmt(StmtTry); |
| 5283 | |
| 5284 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 5285 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 5286 | |
| 5287 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 5288 | return RewriteObjCThrowStmt(StmtThrow); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5289 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5290 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 5291 | return RewriteObjCProtocolExpr(ProtocolExp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5292 | |
| 5293 | if (ObjCForCollectionStmt *StmtForCollection = |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5294 | dyn_cast<ObjCForCollectionStmt>(S)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5295 | return RewriteObjCForCollectionStmt(StmtForCollection, |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5296 | OrigStmtRange.getEnd()); |
| 5297 | if (BreakStmt *StmtBreakStmt = |
| 5298 | dyn_cast<BreakStmt>(S)) |
| 5299 | return RewriteBreakStmt(StmtBreakStmt); |
| 5300 | if (ContinueStmt *StmtContinueStmt = |
| 5301 | dyn_cast<ContinueStmt>(S)) |
| 5302 | return RewriteContinueStmt(StmtContinueStmt); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5303 | |
| 5304 | // Need to check for protocol refs (id <P>, Foo <P> *) in variable decls |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5305 | // and cast exprs. |
| 5306 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
| 5307 | // FIXME: What we're doing here is modifying the type-specifier that |
| 5308 | // precedes the first Decl. In the future the DeclGroup should have |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5309 | // a separate type-specifier that we can rewrite. |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5310 | // NOTE: We need to avoid rewriting the DeclStmt if it is within |
| 5311 | // the context of an ObjCForCollectionStmt. For example: |
| 5312 | // NSArray *someArray; |
| 5313 | // for (id <FooProtocol> index in someArray) ; |
| 5314 | // This is because RewriteObjCForCollectionStmt() does textual rewriting |
| 5315 | // and it depends on the original text locations/positions. |
Benjamin Kramer | acc5fa1 | 2009-12-05 22:16:51 +0000 | [diff] [blame] | 5316 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5317 | RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5318 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5319 | // Blocks rewrite rules. |
| 5320 | for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); |
| 5321 | DI != DE; ++DI) { |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 5322 | Decl *SD = *DI; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5323 | if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5324 | if (isTopLevelBlockPointerType(ND->getType())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5325 | RewriteBlockPointerDecl(ND); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5326 | else if (ND->getType()->isFunctionPointerType()) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5327 | CheckFunctionPointerDecl(ND->getType(), ND); |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 5328 | if (VarDecl *VD = dyn_cast<VarDecl>(SD)) { |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5329 | if (VD->hasAttr<BlocksAttr>()) { |
| 5330 | static unsigned uniqueByrefDeclCount = 0; |
| 5331 | assert(!BlockByRefDeclNo.count(ND) && |
| 5332 | "RewriteFunctionBodyOrGlobalInitializer: Duplicate byref decl"); |
| 5333 | BlockByRefDeclNo[ND] = uniqueByrefDeclCount++; |
Fariborz Jahanian | 02e0773 | 2009-12-23 02:07:37 +0000 | [diff] [blame] | 5334 | RewriteByRefVar(VD); |
Fariborz Jahanian | 195ac2d | 2010-01-14 23:05:52 +0000 | [diff] [blame] | 5335 | } |
Fariborz Jahanian | bbf4320 | 2010-02-10 18:54:22 +0000 | [diff] [blame] | 5336 | else |
| 5337 | RewriteTypeOfDecl(VD); |
| 5338 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5339 | } |
| 5340 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) { |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5341 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5342 | RewriteBlockPointerDecl(TD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5343 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5344 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 5345 | } |
| 5346 | } |
| 5347 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5348 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5349 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) |
| 5350 | RewriteObjCQualifiedInterfaceTypes(CE); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5351 | |
| 5352 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5353 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 5354 | assert(!Stmts.empty() && "Statement stack is empty"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5355 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 5356 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5357 | && "Statement stack mismatch"); |
| 5358 | Stmts.pop_back(); |
| 5359 | } |
| 5360 | // Handle blocks rewriting. |
| 5361 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
| 5362 | if (BDRE->isByRef()) |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5363 | return RewriteBlockDeclRefExpr(BDRE); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5364 | } |
Fariborz Jahanian | d6cba50 | 2010-01-04 19:50:07 +0000 | [diff] [blame] | 5365 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 5366 | ValueDecl *VD = DRE->getDecl(); |
| 5367 | if (VD->hasAttr<BlocksAttr>()) |
| 5368 | return RewriteBlockDeclRefExpr(DRE); |
| 5369 | } |
| 5370 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5371 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 5372 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
Fariborz Jahanian | d1a2d57 | 2009-12-15 17:30:20 +0000 | [diff] [blame] | 5373 | Stmt *BlockCall = SynthesizeBlockCall(CE, CE->getCallee()); |
Steve Naroff | 350b665 | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 5374 | ReplaceStmt(S, BlockCall); |
| 5375 | return BlockCall; |
| 5376 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5377 | } |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 5378 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5379 | RewriteCastExpr(CE); |
| 5380 | } |
| 5381 | #if 0 |
| 5382 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5383 | CastExpr *Replacement = new (Context) CastExpr(ICE->getType(), ICE->getSubExpr(), SourceLocation()); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5384 | // Get the new text. |
| 5385 | std::string SStr; |
| 5386 | llvm::raw_string_ostream Buf(SStr); |
Eli Friedman | 0905f14 | 2009-05-30 05:19:26 +0000 | [diff] [blame] | 5387 | Replacement->printPretty(Buf, *Context); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5388 | const std::string &Str = Buf.str(); |
| 5389 | |
| 5390 | printf("CAST = %s\n", &Str[0]); |
| 5391 | InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 5392 | delete S; |
| 5393 | return Replacement; |
| 5394 | } |
| 5395 | #endif |
| 5396 | // Return this stmt unmodified. |
| 5397 | return S; |
| 5398 | } |
| 5399 | |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5400 | void RewriteObjC::RewriteRecordBody(RecordDecl *RD) { |
| 5401 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 5402 | e = RD->field_end(); i != e; ++i) { |
| 5403 | FieldDecl *FD = *i; |
| 5404 | if (isTopLevelBlockPointerType(FD->getType())) |
| 5405 | RewriteBlockPointerDecl(FD); |
| 5406 | if (FD->getType()->isObjCQualifiedIdType() || |
| 5407 | FD->getType()->isObjCQualifiedInterfaceType()) |
| 5408 | RewriteObjCQualifiedInterfaceTypes(FD); |
| 5409 | } |
| 5410 | } |
| 5411 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5412 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 5413 | /// main file of the input. |
| 5414 | void RewriteObjC::HandleDeclInMainFile(Decl *D) { |
| 5415 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | b136888 | 2008-12-17 00:20:22 +0000 | [diff] [blame] | 5416 | if (FD->isOverloadedOperator()) |
| 5417 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5418 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5419 | // Since function prototypes don't have ParmDecl's, we check the function |
| 5420 | // prototype. This enables us to rewrite function declarations and |
| 5421 | // definitions using the same code. |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5422 | RewriteBlocksInFunctionProtoType(FD->getType(), FD); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5423 | |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 5424 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 5425 | if (CompoundStmt *Body = FD->getCompoundBody()) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5426 | CurFunctionDef = FD; |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 5427 | CurFunctionDeclToDeclareForBlock = FD; |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5428 | CollectPropertySetters(Body); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5429 | CurrentBody = Body; |
Ted Kremenek | 7398059 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 5430 | Body = |
| 5431 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 5432 | FD->setBody(Body); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5433 | CurrentBody = 0; |
| 5434 | if (PropParentMap) { |
| 5435 | delete PropParentMap; |
| 5436 | PropParentMap = 0; |
| 5437 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5438 | // This synthesizes and inserts the block "impl" struct, invoke function, |
| 5439 | // and any copy/dispose helper functions. |
| 5440 | InsertBlockLiteralsWithinFunction(FD); |
| 5441 | CurFunctionDef = 0; |
Fariborz Jahanian | e2dd542 | 2010-01-14 00:35:56 +0000 | [diff] [blame] | 5442 | CurFunctionDeclToDeclareForBlock = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5443 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5444 | return; |
| 5445 | } |
| 5446 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 5447 | if (CompoundStmt *Body = MD->getCompoundBody()) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5448 | CurMethodDef = MD; |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5449 | CollectPropertySetters(Body); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5450 | CurrentBody = Body; |
Ted Kremenek | 7398059 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 5451 | Body = |
| 5452 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 5453 | MD->setBody(Body); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5454 | CurrentBody = 0; |
| 5455 | if (PropParentMap) { |
| 5456 | delete PropParentMap; |
| 5457 | PropParentMap = 0; |
| 5458 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5459 | InsertBlockLiteralsWithinMethod(MD); |
| 5460 | CurMethodDef = 0; |
| 5461 | } |
| 5462 | } |
| 5463 | if (ObjCImplementationDecl *CI = dyn_cast<ObjCImplementationDecl>(D)) |
| 5464 | ClassImplementation.push_back(CI); |
| 5465 | else if (ObjCCategoryImplDecl *CI = dyn_cast<ObjCCategoryImplDecl>(D)) |
| 5466 | CategoryImplementation.push_back(CI); |
| 5467 | else if (ObjCClassDecl *CD = dyn_cast<ObjCClassDecl>(D)) |
| 5468 | RewriteForwardClassDecl(CD); |
| 5469 | else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 5470 | RewriteObjCQualifiedInterfaceTypes(VD); |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5471 | if (isTopLevelBlockPointerType(VD->getType())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5472 | RewriteBlockPointerDecl(VD); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5473 | else if (VD->getType()->isFunctionPointerType()) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5474 | CheckFunctionPointerDecl(VD->getType(), VD); |
| 5475 | if (VD->getInit()) { |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 5476 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5477 | RewriteCastExpr(CE); |
| 5478 | } |
| 5479 | } |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5480 | } else if (VD->getType()->isRecordType()) { |
| 5481 | RecordDecl *RD = VD->getType()->getAs<RecordType>()->getDecl(); |
| 5482 | if (RD->isDefinition()) |
| 5483 | RewriteRecordBody(RD); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5484 | } |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5485 | if (VD->getInit()) { |
| 5486 | GlobalVarDecl = VD; |
Steve Naroff | 4588d0f | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 5487 | CollectPropertySetters(VD->getInit()); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5488 | CurrentBody = VD->getInit(); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5489 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
Steve Naroff | 1042ff3 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 5490 | CurrentBody = 0; |
| 5491 | if (PropParentMap) { |
| 5492 | delete PropParentMap; |
| 5493 | PropParentMap = 0; |
| 5494 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5495 | SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 5496 | VD->getNameAsCString()); |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5497 | GlobalVarDecl = 0; |
| 5498 | |
| 5499 | // This is needed for blocks. |
Steve Naroff | c989a7b | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 5500 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | d8907b7 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 5501 | RewriteCastExpr(CE); |
| 5502 | } |
| 5503 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5504 | return; |
| 5505 | } |
| 5506 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
Steve Naroff | a5c0db8 | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 5507 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5508 | RewriteBlockPointerDecl(TD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5509 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5510 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 5511 | return; |
| 5512 | } |
| 5513 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) { |
Steve Naroff | e70a52a | 2009-12-05 15:55:59 +0000 | [diff] [blame] | 5514 | if (RD->isDefinition()) |
| 5515 | RewriteRecordBody(RD); |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5516 | return; |
| 5517 | } |
| 5518 | // Nothing yet. |
| 5519 | } |
| 5520 | |
Chris Lattner | cf16983 | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 5521 | void RewriteObjC::HandleTranslationUnit(ASTContext &C) { |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5522 | if (Diags.hasErrorOccurred()) |
| 5523 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5524 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5525 | RewriteInclude(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5526 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5527 | // Here's a great place to add any extra declarations that may be needed. |
| 5528 | // Write out meta data for each @protocol(<expr>). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5529 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5530 | E = ProtocolExprDecls.end(); I != E; ++I) |
| 5531 | RewriteObjCProtocolMetaData(*I, "", "", Preamble); |
| 5532 | |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5533 | InsertText(SM->getLocForStartOfFile(MainFileID), Preamble, false); |
Steve Naroff | 2a2a41f | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5534 | if (ClassImplementation.size() || CategoryImplementation.size()) |
| 5535 | RewriteImplementations(); |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5536 | |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5537 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 5538 | // we are done. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5539 | if (const RewriteBuffer *RewriteBuf = |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5540 | Rewrite.getRewriteBufferFor(MainFileID)) { |
| 5541 | //printf("Changed:\n"); |
| 5542 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 5543 | } else { |
Benjamin Kramer | 88ab94e | 2010-02-14 14:14:16 +0000 | [diff] [blame] | 5544 | llvm::errs() << "No changes\n"; |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5545 | } |
Steve Naroff | f8cfd16 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 5546 | |
Steve Naroff | d980371 | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 5547 | if (ClassImplementation.size() || CategoryImplementation.size() || |
| 5548 | ProtocolExprDecls.size()) { |
Steve Naroff | 2a2a41f | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 5549 | // Rewrite Objective-c meta data* |
| 5550 | std::string ResultStr; |
| 5551 | SynthesizeMetaDataIntoBuffer(ResultStr); |
| 5552 | // Emit metadata. |
| 5553 | *OutFile << ResultStr; |
| 5554 | } |
Steve Naroff | f4b992a | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 5555 | OutFile->flush(); |
| 5556 | } |
| 5557 | |