Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1 | //===--- RewriteObjC.cpp - Playground for the code rewriter ---------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Hacks and fun related to the code rewriter. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Ted Kremenek | cdf8149 | 2012-09-01 05:09:24 +0000 | [diff] [blame] | 14 | #include "clang/Rewrite/Frontend/ASTConsumers.h" |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 15 | #include "clang/AST/AST.h" |
| 16 | #include "clang/AST/ASTConsumer.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 17 | #include "clang/AST/Attr.h" |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 18 | #include "clang/AST/ParentMap.h" |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 19 | #include "clang/Basic/CharInfo.h" |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 20 | #include "clang/Basic/Diagnostic.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 21 | #include "clang/Basic/IdentifierTable.h" |
| 22 | #include "clang/Basic/SourceManager.h" |
Benjamin Kramer | d7d2b1f | 2012-12-01 16:35:25 +0000 | [diff] [blame] | 23 | #include "clang/Basic/TargetInfo.h" |
NAKAMURA Takumi | 7f633df | 2017-07-18 08:55:03 +0000 | [diff] [blame] | 24 | #include "clang/Config/config.h" |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 25 | #include "clang/Lex/Lexer.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 26 | #include "clang/Rewrite/Core/Rewriter.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/DenseSet.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallPtrSet.h" |
| 29 | #include "llvm/ADT/StringExtras.h" |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 30 | #include "llvm/Support/MemoryBuffer.h" |
| 31 | #include "llvm/Support/raw_ostream.h" |
Ahmed Charles | dfca6f9 | 2014-03-09 11:36:40 +0000 | [diff] [blame] | 32 | #include <memory> |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 33 | |
NAKAMURA Takumi | d973982 | 2017-10-18 05:21:17 +0000 | [diff] [blame] | 34 | #if CLANG_ENABLE_OBJC_REWRITER |
Alp Toker | 0621cb2 | 2014-07-16 16:48:33 +0000 | [diff] [blame] | 35 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 36 | using namespace clang; |
| 37 | using llvm::utostr; |
| 38 | |
| 39 | namespace { |
| 40 | class RewriteModernObjC : public ASTConsumer { |
| 41 | protected: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 42 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 43 | enum { |
| 44 | BLOCK_FIELD_IS_OBJECT = 3, /* id, NSObject, __attribute__((NSObject)), |
| 45 | block, ... */ |
| 46 | BLOCK_FIELD_IS_BLOCK = 7, /* a block variable */ |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 47 | BLOCK_FIELD_IS_BYREF = 8, /* the on stack structure holding the |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 48 | __block variable */ |
| 49 | BLOCK_FIELD_IS_WEAK = 16, /* declared __weak, only used in byref copy |
| 50 | helpers */ |
| 51 | BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose |
| 52 | support routines */ |
| 53 | BLOCK_BYREF_CURRENT_MAX = 256 |
| 54 | }; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 55 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 56 | enum { |
| 57 | BLOCK_NEEDS_FREE = (1 << 24), |
| 58 | BLOCK_HAS_COPY_DISPOSE = (1 << 25), |
| 59 | BLOCK_HAS_CXX_OBJ = (1 << 26), |
| 60 | BLOCK_IS_GC = (1 << 27), |
| 61 | BLOCK_IS_GLOBAL = (1 << 28), |
| 62 | BLOCK_HAS_DESCRIPTOR = (1 << 29) |
| 63 | }; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 64 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 65 | Rewriter Rewrite; |
| 66 | DiagnosticsEngine &Diags; |
| 67 | const LangOptions &LangOpts; |
| 68 | ASTContext *Context; |
| 69 | SourceManager *SM; |
| 70 | TranslationUnitDecl *TUDecl; |
| 71 | FileID MainFileID; |
| 72 | const char *MainFileStart, *MainFileEnd; |
| 73 | Stmt *CurrentBody; |
| 74 | ParentMap *PropParentMap; // created lazily. |
| 75 | std::string InFileName; |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 76 | std::unique_ptr<raw_ostream> OutFile; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 77 | std::string Preamble; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 78 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 79 | TypeDecl *ProtocolTypeDecl; |
| 80 | VarDecl *GlobalVarDecl; |
Fariborz Jahanian | e005070 | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 81 | Expr *GlobalConstructionExp; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 82 | unsigned RewriteFailedDiag; |
Fariborz Jahanian | bdf975e | 2012-03-22 19:54:39 +0000 | [diff] [blame] | 83 | unsigned GlobalBlockRewriteFailedDiag; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 84 | // ObjC string constant support. |
| 85 | unsigned NumObjCStringLiterals; |
| 86 | VarDecl *ConstantStringClassReference; |
| 87 | RecordDecl *NSStringRecord; |
| 88 | |
| 89 | // ObjC foreach break/continue generation support. |
| 90 | int BcLabelCount; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 91 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 92 | unsigned TryFinallyContainsReturnDiag; |
| 93 | // Needed for super. |
| 94 | ObjCMethodDecl *CurMethodDef; |
| 95 | RecordDecl *SuperStructDecl; |
| 96 | RecordDecl *ConstantStringDecl; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 97 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 98 | FunctionDecl *MsgSendFunctionDecl; |
| 99 | FunctionDecl *MsgSendSuperFunctionDecl; |
| 100 | FunctionDecl *MsgSendStretFunctionDecl; |
| 101 | FunctionDecl *MsgSendSuperStretFunctionDecl; |
| 102 | FunctionDecl *MsgSendFpretFunctionDecl; |
| 103 | FunctionDecl *GetClassFunctionDecl; |
| 104 | FunctionDecl *GetMetaClassFunctionDecl; |
| 105 | FunctionDecl *GetSuperClassFunctionDecl; |
| 106 | FunctionDecl *SelGetUidFunctionDecl; |
| 107 | FunctionDecl *CFStringFunctionDecl; |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 108 | FunctionDecl *SuperConstructorFunctionDecl; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 109 | FunctionDecl *CurFunctionDef; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 110 | |
| 111 | /* Misc. containers needed for meta-data rewrite. */ |
| 112 | SmallVector<ObjCImplementationDecl *, 8> ClassImplementation; |
| 113 | SmallVector<ObjCCategoryImplDecl *, 8> CategoryImplementation; |
| 114 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCSynthesizedStructs; |
| 115 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> ObjCSynthesizedProtocols; |
Fariborz Jahanian | d268b0c | 2012-02-17 22:20:12 +0000 | [diff] [blame] | 116 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCWrittenInterfaces; |
Fariborz Jahanian | faded5b | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 117 | llvm::SmallPtrSet<TagDecl*, 32> GlobalDefinedTags; |
Fariborz Jahanian | d268b0c | 2012-02-17 22:20:12 +0000 | [diff] [blame] | 118 | SmallVector<ObjCInterfaceDecl*, 32> ObjCInterfacesSeen; |
Fariborz Jahanian | 07a423d | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 119 | /// DefinedNonLazyClasses - List of defined "non-lazy" classes. |
| 120 | SmallVector<ObjCInterfaceDecl*, 8> DefinedNonLazyClasses; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 121 | |
Fariborz Jahanian | 07a423d | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 122 | /// DefinedNonLazyCategories - List of defined "non-lazy" categories. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 123 | SmallVector<ObjCCategoryDecl *, 8> DefinedNonLazyCategories; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 124 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 125 | SmallVector<Stmt *, 32> Stmts; |
| 126 | SmallVector<int, 8> ObjCBcLabelNo; |
| 127 | // Remember all the @protocol(<expr>) expressions. |
| 128 | llvm::SmallPtrSet<ObjCProtocolDecl *, 32> ProtocolExprDecls; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 129 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 130 | llvm::DenseSet<uint64_t> CopyDestroyCache; |
| 131 | |
| 132 | // Block expressions. |
| 133 | SmallVector<BlockExpr *, 32> Blocks; |
| 134 | SmallVector<int, 32> InnerDeclRefsCount; |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 135 | SmallVector<DeclRefExpr *, 32> InnerDeclRefs; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 136 | |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 137 | SmallVector<DeclRefExpr *, 32> BlockDeclRefs; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 138 | |
| 139 | // Block related declarations. |
| 140 | SmallVector<ValueDecl *, 8> BlockByCopyDecls; |
| 141 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByCopyDeclsPtrSet; |
| 142 | SmallVector<ValueDecl *, 8> BlockByRefDecls; |
| 143 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByRefDeclsPtrSet; |
| 144 | llvm::DenseMap<ValueDecl *, unsigned> BlockByRefDeclNo; |
| 145 | llvm::SmallPtrSet<ValueDecl *, 8> ImportedBlockDecls; |
| 146 | llvm::SmallPtrSet<VarDecl *, 8> ImportedLocalExternalDecls; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 147 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 148 | llvm::DenseMap<BlockExpr *, std::string> RewrittenBlockExprs; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 149 | llvm::DenseMap<ObjCInterfaceDecl *, |
Mandeep Singh Grang | a2baff0 | 2017-07-06 18:49:57 +0000 | [diff] [blame] | 150 | llvm::SmallSetVector<ObjCIvarDecl *, 8> > ReferencedIvars; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 151 | |
Fariborz Jahanian | 57dd66b | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 152 | // ivar bitfield grouping containers |
| 153 | llvm::DenseSet<const ObjCInterfaceDecl *> ObjCInterefaceHasBitfieldGroups; |
| 154 | llvm::DenseMap<const ObjCIvarDecl* , unsigned> IvarGroupNumber; |
| 155 | // This container maps an <class, group number for ivar> tuple to the type |
| 156 | // of the struct where the bitfield belongs. |
| 157 | llvm::DenseMap<std::pair<const ObjCInterfaceDecl*, unsigned>, QualType> GroupRecordType; |
Fariborz Jahanian | e499613 | 2013-02-07 22:50:40 +0000 | [diff] [blame] | 158 | SmallVector<FunctionDecl*, 32> FunctionDefinitionsSeen; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 159 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 160 | // This maps an original source AST to it's rewritten form. This allows |
| 161 | // us to avoid rewriting the same node twice (which is very uncommon). |
| 162 | // This is needed to support some of the exotic property rewriting. |
| 163 | llvm::DenseMap<Stmt *, Stmt *> ReplacedNodes; |
| 164 | |
| 165 | // Needed for header files being rewritten |
| 166 | bool IsHeader; |
| 167 | bool SilenceRewriteMacroWarning; |
Fariborz Jahanian | e4c7e85 | 2013-02-08 00:27:34 +0000 | [diff] [blame] | 168 | bool GenerateLineInfo; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 169 | bool objc_impl_method; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 170 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 171 | bool DisableReplaceStmt; |
| 172 | class DisableReplaceStmtScope { |
| 173 | RewriteModernObjC &R; |
| 174 | bool SavedValue; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 175 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 176 | public: |
| 177 | DisableReplaceStmtScope(RewriteModernObjC &R) |
| 178 | : R(R), SavedValue(R.DisableReplaceStmt) { |
| 179 | R.DisableReplaceStmt = true; |
| 180 | } |
| 181 | ~DisableReplaceStmtScope() { |
| 182 | R.DisableReplaceStmt = SavedValue; |
| 183 | } |
| 184 | }; |
| 185 | void InitializeCommon(ASTContext &context); |
| 186 | |
| 187 | public: |
Fariborz Jahanian | b1ba885 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 188 | llvm::DenseMap<ObjCMethodDecl*, std::string> MethodInternalNames; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 189 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 190 | // Top Level Driver code. |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 191 | bool HandleTopLevelDecl(DeclGroupRef D) override { |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 192 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
| 193 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(*I)) { |
| 194 | if (!Class->isThisDeclarationADefinition()) { |
| 195 | RewriteForwardClassDecl(D); |
| 196 | break; |
Fariborz Jahanian | d268b0c | 2012-02-17 22:20:12 +0000 | [diff] [blame] | 197 | } else { |
| 198 | // Keep track of all interface declarations seen. |
Fariborz Jahanian | 0ed6cb7 | 2012-02-24 21:42:38 +0000 | [diff] [blame] | 199 | ObjCInterfacesSeen.push_back(Class); |
Fariborz Jahanian | d268b0c | 2012-02-17 22:20:12 +0000 | [diff] [blame] | 200 | break; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
| 204 | if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(*I)) { |
| 205 | if (!Proto->isThisDeclarationADefinition()) { |
| 206 | RewriteForwardProtocolDecl(D); |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | |
Fariborz Jahanian | e499613 | 2013-02-07 22:50:40 +0000 | [diff] [blame] | 211 | if (FunctionDecl *FDecl = dyn_cast<FunctionDecl>(*I)) { |
| 212 | // Under modern abi, we cannot translate body of the function |
| 213 | // yet until all class extensions and its implementation is seen. |
| 214 | // This is because they may introduce new bitfields which must go |
| 215 | // into their grouping struct. |
| 216 | if (FDecl->isThisDeclarationADefinition() && |
| 217 | // Not c functions defined inside an objc container. |
| 218 | !FDecl->isTopLevelDeclInObjCContainer()) { |
| 219 | FunctionDefinitionsSeen.push_back(FDecl); |
| 220 | break; |
| 221 | } |
| 222 | } |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 223 | HandleTopLevelSingleDecl(*I); |
| 224 | } |
| 225 | return true; |
| 226 | } |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 227 | |
| 228 | void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override { |
Fariborz Jahanian | d294062 | 2013-10-07 19:54:22 +0000 | [diff] [blame] | 229 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
| 230 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(*I)) { |
| 231 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
| 232 | RewriteBlockPointerDecl(TD); |
| 233 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
| 234 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 235 | else |
| 236 | RewriteObjCQualifiedInterfaceTypes(TD); |
| 237 | } |
| 238 | } |
Fariborz Jahanian | d294062 | 2013-10-07 19:54:22 +0000 | [diff] [blame] | 239 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 240 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 241 | void HandleTopLevelSingleDecl(Decl *D); |
| 242 | void HandleDeclInMainFile(Decl *D); |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 243 | RewriteModernObjC(std::string inFile, std::unique_ptr<raw_ostream> OS, |
| 244 | DiagnosticsEngine &D, const LangOptions &LOpts, |
| 245 | bool silenceMacroWarn, bool LineInfo); |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 246 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 247 | ~RewriteModernObjC() override {} |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 248 | |
| 249 | void HandleTranslationUnit(ASTContext &C) override; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 250 | |
| 251 | void ReplaceStmt(Stmt *Old, Stmt *New) { |
Daniel Jasper | 4475a24 | 2014-10-23 19:47:36 +0000 | [diff] [blame] | 252 | ReplaceStmtWithRange(Old, New, Old->getSourceRange()); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | void ReplaceStmtWithRange(Stmt *Old, Stmt *New, SourceRange SrcRange) { |
Richard Trieu | ddd01ce | 2014-06-09 22:53:25 +0000 | [diff] [blame] | 256 | assert(Old != nullptr && New != nullptr && "Expected non-null Stmt's"); |
Daniel Jasper | 4475a24 | 2014-10-23 19:47:36 +0000 | [diff] [blame] | 257 | |
| 258 | Stmt *ReplacingStmt = ReplacedNodes[Old]; |
| 259 | if (ReplacingStmt) |
| 260 | return; // We can't rewrite the same node twice. |
| 261 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 262 | if (DisableReplaceStmt) |
| 263 | return; |
| 264 | |
| 265 | // Measure the old text. |
| 266 | int Size = Rewrite.getRangeSize(SrcRange); |
| 267 | if (Size == -1) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 268 | Diags.Report(Context->getFullLoc(Old->getBeginLoc()), RewriteFailedDiag) |
| 269 | << Old->getSourceRange(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 270 | return; |
| 271 | } |
| 272 | // Get the new text. |
| 273 | std::string SStr; |
| 274 | llvm::raw_string_ostream S(SStr); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 275 | New->printPretty(S, nullptr, PrintingPolicy(LangOpts)); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 276 | const std::string &Str = S.str(); |
| 277 | |
| 278 | // If replacement succeeded or warning disabled return with no warning. |
| 279 | if (!Rewrite.ReplaceText(SrcRange.getBegin(), Size, Str)) { |
| 280 | ReplacedNodes[Old] = New; |
| 281 | return; |
| 282 | } |
| 283 | if (SilenceRewriteMacroWarning) |
| 284 | return; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 285 | Diags.Report(Context->getFullLoc(Old->getBeginLoc()), RewriteFailedDiag) |
| 286 | << Old->getSourceRange(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | void InsertText(SourceLocation Loc, StringRef Str, |
| 290 | bool InsertAfter = true) { |
| 291 | // If insertion succeeded or warning disabled return with no warning. |
| 292 | if (!Rewrite.InsertText(Loc, Str, InsertAfter) || |
| 293 | SilenceRewriteMacroWarning) |
| 294 | return; |
| 295 | |
| 296 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 297 | } |
| 298 | |
| 299 | void ReplaceText(SourceLocation Start, unsigned OrigLength, |
| 300 | StringRef Str) { |
| 301 | // If removal succeeded or warning disabled return with no warning. |
| 302 | if (!Rewrite.ReplaceText(Start, OrigLength, Str) || |
| 303 | SilenceRewriteMacroWarning) |
| 304 | return; |
| 305 | |
| 306 | Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag); |
| 307 | } |
| 308 | |
| 309 | // Syntactic Rewriting. |
| 310 | void RewriteRecordBody(RecordDecl *RD); |
| 311 | void RewriteInclude(); |
Fariborz Jahanian | aa4a242 | 2012-11-06 17:30:23 +0000 | [diff] [blame] | 312 | void RewriteLineDirective(const Decl *D); |
Fariborz Jahanian | b6933bc | 2012-11-06 23:25:49 +0000 | [diff] [blame] | 313 | void ConvertSourceLocationToLineDirective(SourceLocation Loc, |
| 314 | std::string &LineString); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 315 | void RewriteForwardClassDecl(DeclGroupRef D); |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 316 | void RewriteForwardClassDecl(const SmallVectorImpl<Decl *> &DG); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 317 | void RewriteForwardClassEpilogue(ObjCInterfaceDecl *ClassDecl, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 318 | const std::string &typedefString); |
| 319 | void RewriteImplementations(); |
| 320 | void RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 321 | ObjCImplementationDecl *IMD, |
| 322 | ObjCCategoryImplDecl *CID); |
| 323 | void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl); |
| 324 | void RewriteImplementationDecl(Decl *Dcl); |
| 325 | void RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl, |
| 326 | ObjCMethodDecl *MDecl, std::string &ResultStr); |
| 327 | void RewriteTypeIntoString(QualType T, std::string &ResultStr, |
| 328 | const FunctionType *&FPRetType); |
| 329 | void RewriteByRefString(std::string &ResultStr, const std::string &Name, |
| 330 | ValueDecl *VD, bool def=false); |
| 331 | void RewriteCategoryDecl(ObjCCategoryDecl *Dcl); |
| 332 | void RewriteProtocolDecl(ObjCProtocolDecl *Dcl); |
| 333 | void RewriteForwardProtocolDecl(DeclGroupRef D); |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 334 | void RewriteForwardProtocolDecl(const SmallVectorImpl<Decl *> &DG); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 335 | void RewriteMethodDeclaration(ObjCMethodDecl *Method); |
| 336 | void RewriteProperty(ObjCPropertyDecl *prop); |
| 337 | void RewriteFunctionDecl(FunctionDecl *FD); |
| 338 | void RewriteBlockPointerType(std::string& Str, QualType Type); |
| 339 | void RewriteBlockPointerTypeVariable(std::string& Str, ValueDecl *VD); |
Fariborz Jahanian | ca357d9 | 2012-04-19 00:50:01 +0000 | [diff] [blame] | 340 | void RewriteBlockLiteralFunctionDecl(FunctionDecl *FD); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 341 | void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl); |
| 342 | void RewriteTypeOfDecl(VarDecl *VD); |
| 343 | void RewriteObjCQualifiedInterfaceTypes(Expr *E); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 344 | |
Fariborz Jahanian | 89919cc | 2012-05-08 23:54:35 +0000 | [diff] [blame] | 345 | std::string getIvarAccessString(ObjCIvarDecl *D); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 346 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 347 | // Expression Rewriting. |
| 348 | Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S); |
| 349 | Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp); |
| 350 | Stmt *RewritePropertyOrImplicitGetter(PseudoObjectExpr *Pseudo); |
| 351 | Stmt *RewritePropertyOrImplicitSetter(PseudoObjectExpr *Pseudo); |
| 352 | Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp); |
| 353 | Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp); |
| 354 | Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp); |
Fariborz Jahanian | 307b7ad | 2012-03-27 20:17:30 +0000 | [diff] [blame] | 355 | Stmt *RewriteObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Exp); |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 356 | Stmt *RewriteObjCBoxedExpr(ObjCBoxedExpr *Exp); |
Fariborz Jahanian | 991a08d | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 357 | Stmt *RewriteObjCArrayLiteralExpr(ObjCArrayLiteral *Exp); |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 358 | Stmt *RewriteObjCDictionaryLiteralExpr(ObjCDictionaryLiteral *Exp); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 359 | Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 360 | Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S); |
Fariborz Jahanian | 9b43c3f | 2012-05-23 23:47:20 +0000 | [diff] [blame] | 361 | Stmt *RewriteObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 362 | Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
| 363 | Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S); |
| 364 | Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
| 365 | SourceLocation OrigEnd); |
| 366 | Stmt *RewriteBreakStmt(BreakStmt *S); |
| 367 | Stmt *RewriteContinueStmt(ContinueStmt *S); |
| 368 | void RewriteCastExpr(CStyleCastExpr *CE); |
Fariborz Jahanian | 2c00acd | 2012-04-10 00:08:18 +0000 | [diff] [blame] | 369 | void RewriteImplicitCastObjCExpr(CastExpr *IE); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 370 | |
Fariborz Jahanian | 57dd66b | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 371 | // Computes ivar bitfield group no. |
| 372 | unsigned ObjCIvarBitfieldGroupNo(ObjCIvarDecl *IV); |
| 373 | // Names field decl. for ivar bitfield group. |
| 374 | void ObjCIvarBitfieldGroupDecl(ObjCIvarDecl *IV, std::string &Result); |
| 375 | // Names struct type for ivar bitfield group. |
| 376 | void ObjCIvarBitfieldGroupType(ObjCIvarDecl *IV, std::string &Result); |
| 377 | // Names symbol for ivar bitfield group field offset. |
| 378 | void ObjCIvarBitfieldGroupOffset(ObjCIvarDecl *IV, std::string &Result); |
| 379 | // Given an ivar bitfield, it builds (or finds) its group record type. |
| 380 | QualType GetGroupRecordTypeForObjCIvarBitfield(ObjCIvarDecl *IV); |
| 381 | QualType SynthesizeBitfieldGroupStructType( |
| 382 | ObjCIvarDecl *IV, |
| 383 | SmallVectorImpl<ObjCIvarDecl *> &IVars); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 384 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 385 | // Block rewriting. |
| 386 | void RewriteBlocksInFunctionProtoType(QualType funcType, NamedDecl *D); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 387 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 388 | // Block specific rewrite rules. |
| 389 | void RewriteBlockPointerDecl(NamedDecl *VD); |
Fariborz Jahanian | 847713a | 2012-04-24 19:38:45 +0000 | [diff] [blame] | 390 | void RewriteByRefVar(VarDecl *VD, bool firstDecl, bool lastDecl); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 391 | Stmt *RewriteBlockDeclRefExpr(DeclRefExpr *VD); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 392 | Stmt *RewriteLocalVariableExternalStorage(DeclRefExpr *DRE); |
| 393 | void RewriteBlockPointerFunctionArgs(FunctionDecl *FD); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 394 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 395 | void RewriteObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
| 396 | std::string &Result); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 397 | |
Fariborz Jahanian | 265a421 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 398 | void RewriteObjCFieldDecl(FieldDecl *fieldDecl, std::string &Result); |
Fariborz Jahanian | 144b722 | 2012-05-01 17:46:45 +0000 | [diff] [blame] | 399 | bool IsTagDefinedInsideClass(ObjCContainerDecl *IDecl, TagDecl *Tag, |
Fariborz Jahanian | faded5b | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 400 | bool &IsNamedDefinition); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 401 | void RewriteLocallyDefinedNamedAggregates(FieldDecl *fieldDecl, |
Fariborz Jahanian | faded5b | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 402 | std::string &Result); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 403 | |
Fariborz Jahanian | c2e2ad6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 404 | bool RewriteObjCFieldDeclType(QualType &Type, std::string &Result); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 405 | |
Fariborz Jahanian | 5e49eb9 | 2012-02-22 18:13:25 +0000 | [diff] [blame] | 406 | void RewriteIvarOffsetSymbols(ObjCInterfaceDecl *CDecl, |
| 407 | std::string &Result); |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 408 | |
| 409 | void Initialize(ASTContext &context) override; |
| 410 | |
Benjamin Kramer | 474261a | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 411 | // Misc. AST transformation routines. Sometimes they end up calling |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 412 | // rewriting routines on the new ASTs. |
| 413 | CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD, |
Craig Topper | cf2126e | 2015-10-22 03:13:07 +0000 | [diff] [blame] | 414 | ArrayRef<Expr *> Args, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 415 | SourceLocation StartLoc=SourceLocation(), |
| 416 | SourceLocation EndLoc=SourceLocation()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 417 | |
Fariborz Jahanian | 4a031bd | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 418 | Expr *SynthMsgSendStretCallExpr(FunctionDecl *MsgSendStretFlavor, |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 419 | QualType returnType, |
Fariborz Jahanian | 4a031bd | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 420 | SmallVectorImpl<QualType> &ArgTypes, |
| 421 | SmallVectorImpl<Expr*> &MsgExprs, |
| 422 | ObjCMethodDecl *Method); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 423 | |
| 424 | Stmt *SynthMessageExpr(ObjCMessageExpr *Exp, |
| 425 | SourceLocation StartLoc=SourceLocation(), |
| 426 | SourceLocation EndLoc=SourceLocation()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 427 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 428 | void SynthCountByEnumWithState(std::string &buf); |
| 429 | void SynthMsgSendFunctionDecl(); |
| 430 | void SynthMsgSendSuperFunctionDecl(); |
| 431 | void SynthMsgSendStretFunctionDecl(); |
| 432 | void SynthMsgSendFpretFunctionDecl(); |
| 433 | void SynthMsgSendSuperStretFunctionDecl(); |
| 434 | void SynthGetClassFunctionDecl(); |
| 435 | void SynthGetMetaClassFunctionDecl(); |
| 436 | void SynthGetSuperClassFunctionDecl(); |
| 437 | void SynthSelGetUidFunctionDecl(); |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 438 | void SynthSuperConstructorFunctionDecl(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 439 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 440 | // Rewriting metadata |
| 441 | template<typename MethodIterator> |
| 442 | void RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 443 | MethodIterator MethodEnd, |
| 444 | bool IsInstanceMethod, |
| 445 | StringRef prefix, |
| 446 | StringRef ClassName, |
| 447 | std::string &Result); |
Fariborz Jahanian | e18961b | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 448 | void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol, |
| 449 | std::string &Result); |
Fariborz Jahanian | 95badad | 2012-04-30 16:57:52 +0000 | [diff] [blame] | 450 | void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 451 | std::string &Result); |
Fariborz Jahanian | 95badad | 2012-04-30 16:57:52 +0000 | [diff] [blame] | 452 | void RewriteClassSetupInitHook(std::string &Result); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 453 | |
Fariborz Jahanian | 95badad | 2012-04-30 16:57:52 +0000 | [diff] [blame] | 454 | void RewriteMetaDataIntoBuffer(std::string &Result); |
| 455 | void WriteImageInfo(std::string &Result); |
| 456 | void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 457 | std::string &Result); |
Fariborz Jahanian | 95badad | 2012-04-30 16:57:52 +0000 | [diff] [blame] | 458 | void RewriteCategorySetupInitHook(std::string &Result); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 459 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 460 | // Rewriting ivar |
Fariborz Jahanian | 95badad | 2012-04-30 16:57:52 +0000 | [diff] [blame] | 461 | void RewriteIvarOffsetComputation(ObjCIvarDecl *ivar, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 462 | std::string &Result); |
Fariborz Jahanian | 95badad | 2012-04-30 16:57:52 +0000 | [diff] [blame] | 463 | Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 464 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 465 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 466 | std::string SynthesizeByrefCopyDestroyHelper(VarDecl *VD, int flag); |
| 467 | std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
| 468 | StringRef funcName, std::string Tag); |
| 469 | std::string SynthesizeBlockFunc(BlockExpr *CE, int i, |
| 470 | StringRef funcName, std::string Tag); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 471 | std::string SynthesizeBlockImpl(BlockExpr *CE, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 472 | std::string Tag, std::string Desc); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 473 | std::string SynthesizeBlockDescriptor(std::string DescTag, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 474 | std::string ImplTag, |
| 475 | int i, StringRef funcName, |
| 476 | unsigned hasCopy); |
| 477 | Stmt *SynthesizeBlockCall(CallExpr *Exp, const Expr* BlockExp); |
| 478 | void SynthesizeBlockLiterals(SourceLocation FunLocStart, |
| 479 | StringRef FunName); |
| 480 | FunctionDecl *SynthBlockInitFunctionDecl(StringRef name); |
| 481 | Stmt *SynthBlockInitExpr(BlockExpr *Exp, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 482 | const SmallVectorImpl<DeclRefExpr *> &InnerBlockDeclRefs); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 483 | |
| 484 | // Misc. helper routines. |
| 485 | QualType getProtocolType(); |
| 486 | void WarnAboutReturnGotoStmts(Stmt *S); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 487 | void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND); |
| 488 | void InsertBlockLiteralsWithinFunction(FunctionDecl *FD); |
| 489 | void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD); |
| 490 | |
| 491 | bool IsDeclStmtInForeachHeader(DeclStmt *DS); |
| 492 | void CollectBlockDeclRefInfo(BlockExpr *Exp); |
| 493 | void GetBlockDeclRefExprs(Stmt *S); |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 494 | void GetInnerBlockDeclRefExprs(Stmt *S, |
| 495 | SmallVectorImpl<DeclRefExpr *> &InnerBlockDeclRefs, |
Craig Topper | 4dd9b43 | 2014-08-17 23:49:53 +0000 | [diff] [blame] | 496 | llvm::SmallPtrSetImpl<const DeclContext *> &InnerContexts); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 497 | |
| 498 | // We avoid calling Type::isBlockPointerType(), since it operates on the |
| 499 | // canonical type. We only care if the top-level type is a closure pointer. |
| 500 | bool isTopLevelBlockPointerType(QualType T) { |
| 501 | return isa<BlockPointerType>(T); |
| 502 | } |
| 503 | |
| 504 | /// convertBlockPointerToFunctionPointer - Converts a block-pointer type |
| 505 | /// to a function pointer type and upon success, returns true; false |
| 506 | /// otherwise. |
| 507 | bool convertBlockPointerToFunctionPointer(QualType &T) { |
| 508 | if (isTopLevelBlockPointerType(T)) { |
| 509 | const BlockPointerType *BPT = T->getAs<BlockPointerType>(); |
| 510 | T = Context->getPointerType(BPT->getPointeeType()); |
| 511 | return true; |
| 512 | } |
| 513 | return false; |
| 514 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 515 | |
Fariborz Jahanian | c3cdc41 | 2012-02-13 18:57:49 +0000 | [diff] [blame] | 516 | bool convertObjCTypeToCStyleType(QualType &T); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 517 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 518 | bool needToScanForQualifiers(QualType T); |
| 519 | QualType getSuperStructType(); |
| 520 | QualType getConstantStringStructType(); |
| 521 | QualType convertFunctionTypeOfBlocks(const FunctionType *FT); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 522 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 523 | void convertToUnqualifiedObjCType(QualType &T) { |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 524 | if (T->isObjCQualifiedIdType()) { |
| 525 | bool isConst = T.isConstQualified(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 526 | T = isConst ? Context->getObjCIdType().withConst() |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 527 | : Context->getObjCIdType(); |
| 528 | } |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 529 | else if (T->isObjCQualifiedClassType()) |
| 530 | T = Context->getObjCClassType(); |
| 531 | else if (T->isObjCObjectPointerType() && |
| 532 | T->getPointeeType()->isObjCQualifiedInterfaceType()) { |
| 533 | if (const ObjCObjectPointerType * OBJPT = |
| 534 | T->getAsObjCInterfacePointerType()) { |
| 535 | const ObjCInterfaceType *IFaceT = OBJPT->getInterfaceType(); |
| 536 | T = QualType(IFaceT, 0); |
| 537 | T = Context->getPointerType(T); |
| 538 | } |
| 539 | } |
| 540 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 541 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 542 | // FIXME: This predicate seems like it would be useful to add to ASTContext. |
| 543 | bool isObjCType(QualType T) { |
| 544 | if (!LangOpts.ObjC1 && !LangOpts.ObjC2) |
| 545 | return false; |
| 546 | |
| 547 | QualType OCT = Context->getCanonicalType(T).getUnqualifiedType(); |
| 548 | |
| 549 | if (OCT == Context->getCanonicalType(Context->getObjCIdType()) || |
| 550 | OCT == Context->getCanonicalType(Context->getObjCClassType())) |
| 551 | return true; |
| 552 | |
| 553 | if (const PointerType *PT = OCT->getAs<PointerType>()) { |
| 554 | if (isa<ObjCInterfaceType>(PT->getPointeeType()) || |
| 555 | PT->getPointeeType()->isObjCQualifiedIdType()) |
| 556 | return true; |
| 557 | } |
| 558 | return false; |
| 559 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 560 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 561 | bool PointerTypeTakesAnyBlockArguments(QualType QT); |
| 562 | bool PointerTypeTakesAnyObjCQualifiedType(QualType QT); |
| 563 | void GetExtentOfArgList(const char *Name, const char *&LParen, |
| 564 | const char *&RParen); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 565 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 566 | void QuoteDoublequotes(std::string &From, std::string &To) { |
| 567 | for (unsigned i = 0; i < From.length(); i++) { |
| 568 | if (From[i] == '"') |
| 569 | To += "\\\""; |
| 570 | else |
| 571 | To += From[i]; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | QualType getSimpleFunctionType(QualType result, |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 576 | ArrayRef<QualType> args, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 577 | bool variadic = false) { |
| 578 | if (result == Context->getObjCInstanceType()) |
| 579 | result = Context->getObjCIdType(); |
| 580 | FunctionProtoType::ExtProtoInfo fpi; |
| 581 | fpi.Variadic = variadic; |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 582 | return Context->getFunctionType(result, args, fpi); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | // Helper function: create a CStyleCastExpr with trivial type source info. |
| 586 | CStyleCastExpr* NoTypeInfoCStyleCastExpr(ASTContext *Ctx, QualType Ty, |
| 587 | CastKind Kind, Expr *E) { |
| 588 | TypeSourceInfo *TInfo = Ctx->getTrivialTypeSourceInfo(Ty, SourceLocation()); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 589 | return CStyleCastExpr::Create(*Ctx, Ty, VK_RValue, Kind, E, nullptr, |
| 590 | TInfo, SourceLocation(), SourceLocation()); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 591 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 592 | |
Fariborz Jahanian | 07a423d | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 593 | bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const { |
| 594 | IdentifierInfo* II = &Context->Idents.get("load"); |
| 595 | Selector LoadSel = Context->Selectors.getSelector(0, &II); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 596 | return OD->getClassMethod(LoadSel) != nullptr; |
Fariborz Jahanian | 07a423d | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 597 | } |
Benjamin Kramer | fc18842 | 2014-02-25 12:26:11 +0000 | [diff] [blame] | 598 | |
| 599 | StringLiteral *getStringLiteral(StringRef Str) { |
| 600 | QualType StrType = Context->getConstantArrayType( |
| 601 | Context->CharTy, llvm::APInt(32, Str.size() + 1), ArrayType::Normal, |
| 602 | 0); |
| 603 | return StringLiteral::Create(*Context, Str, StringLiteral::Ascii, |
| 604 | /*Pascal=*/false, StrType, SourceLocation()); |
| 605 | } |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 606 | }; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 607 | } // end anonymous namespace |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 608 | |
| 609 | void RewriteModernObjC::RewriteBlocksInFunctionProtoType(QualType funcType, |
| 610 | NamedDecl *D) { |
| 611 | if (const FunctionProtoType *fproto |
| 612 | = dyn_cast<FunctionProtoType>(funcType.IgnoreParens())) { |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 613 | for (const auto &I : fproto->param_types()) |
| 614 | if (isTopLevelBlockPointerType(I)) { |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 615 | // All the args are checked/rewritten. Don't call twice! |
| 616 | RewriteBlockPointerDecl(D); |
| 617 | break; |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | void RewriteModernObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) { |
| 623 | const PointerType *PT = funcType->getAs<PointerType>(); |
| 624 | if (PT && PointerTypeTakesAnyBlockArguments(funcType)) |
| 625 | RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND); |
| 626 | } |
| 627 | |
| 628 | static bool IsHeaderFile(const std::string &Filename) { |
| 629 | std::string::size_type DotPos = Filename.rfind('.'); |
| 630 | |
| 631 | if (DotPos == std::string::npos) { |
| 632 | // no file extension |
| 633 | return false; |
| 634 | } |
| 635 | |
| 636 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 637 | // C header: .h |
| 638 | // C++ header: .hh or .H; |
| 639 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
| 640 | } |
| 641 | |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 642 | RewriteModernObjC::RewriteModernObjC(std::string inFile, |
| 643 | std::unique_ptr<raw_ostream> OS, |
| 644 | DiagnosticsEngine &D, |
| 645 | const LangOptions &LOpts, |
| 646 | bool silenceMacroWarn, bool LineInfo) |
| 647 | : Diags(D), LangOpts(LOpts), InFileName(inFile), OutFile(std::move(OS)), |
| 648 | SilenceRewriteMacroWarning(silenceMacroWarn), GenerateLineInfo(LineInfo) { |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 649 | IsHeader = IsHeaderFile(inFile); |
| 650 | RewriteFailedDiag = Diags.getCustomDiagID(DiagnosticsEngine::Warning, |
| 651 | "rewriting sub-expression within a macro (may not be correct)"); |
Fariborz Jahanian | bdf975e | 2012-03-22 19:54:39 +0000 | [diff] [blame] | 652 | // FIXME. This should be an error. But if block is not called, it is OK. And it |
| 653 | // may break including some headers. |
| 654 | GlobalBlockRewriteFailedDiag = Diags.getCustomDiagID(DiagnosticsEngine::Warning, |
| 655 | "rewriting block literal declared in global scope is not implemented"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 656 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 657 | TryFinallyContainsReturnDiag = Diags.getCustomDiagID( |
| 658 | DiagnosticsEngine::Warning, |
| 659 | "rewriter doesn't support user-specified control flow semantics " |
| 660 | "for @try/@finally (code may not execute properly)"); |
| 661 | } |
| 662 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 663 | std::unique_ptr<ASTConsumer> clang::CreateModernObjCRewriter( |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 664 | const std::string &InFile, std::unique_ptr<raw_ostream> OS, |
| 665 | DiagnosticsEngine &Diags, const LangOptions &LOpts, |
| 666 | bool SilenceRewriteMacroWarning, bool LineInfo) { |
| 667 | return llvm::make_unique<RewriteModernObjC>(InFile, std::move(OS), Diags, |
| 668 | LOpts, SilenceRewriteMacroWarning, |
| 669 | LineInfo); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | void RewriteModernObjC::InitializeCommon(ASTContext &context) { |
| 673 | Context = &context; |
| 674 | SM = &Context->getSourceManager(); |
| 675 | TUDecl = Context->getTranslationUnitDecl(); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 676 | MsgSendFunctionDecl = nullptr; |
| 677 | MsgSendSuperFunctionDecl = nullptr; |
| 678 | MsgSendStretFunctionDecl = nullptr; |
| 679 | MsgSendSuperStretFunctionDecl = nullptr; |
| 680 | MsgSendFpretFunctionDecl = nullptr; |
| 681 | GetClassFunctionDecl = nullptr; |
| 682 | GetMetaClassFunctionDecl = nullptr; |
| 683 | GetSuperClassFunctionDecl = nullptr; |
| 684 | SelGetUidFunctionDecl = nullptr; |
| 685 | CFStringFunctionDecl = nullptr; |
| 686 | ConstantStringClassReference = nullptr; |
| 687 | NSStringRecord = nullptr; |
| 688 | CurMethodDef = nullptr; |
| 689 | CurFunctionDef = nullptr; |
| 690 | GlobalVarDecl = nullptr; |
| 691 | GlobalConstructionExp = nullptr; |
| 692 | SuperStructDecl = nullptr; |
| 693 | ProtocolTypeDecl = nullptr; |
| 694 | ConstantStringDecl = nullptr; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 695 | BcLabelCount = 0; |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 696 | SuperConstructorFunctionDecl = nullptr; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 697 | NumObjCStringLiterals = 0; |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 698 | PropParentMap = nullptr; |
| 699 | CurrentBody = nullptr; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 700 | DisableReplaceStmt = false; |
| 701 | objc_impl_method = false; |
| 702 | |
| 703 | // Get the ID and start/end of the main file. |
| 704 | MainFileID = SM->getMainFileID(); |
| 705 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 706 | MainFileStart = MainBuf->getBufferStart(); |
| 707 | MainFileEnd = MainBuf->getBufferEnd(); |
| 708 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 709 | Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOpts()); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | //===----------------------------------------------------------------------===// |
| 713 | // Top Level Driver Code |
| 714 | //===----------------------------------------------------------------------===// |
| 715 | |
| 716 | void RewriteModernObjC::HandleTopLevelSingleDecl(Decl *D) { |
| 717 | if (Diags.hasErrorOccurred()) |
| 718 | return; |
| 719 | |
| 720 | // Two cases: either the decl could be in the main file, or it could be in a |
| 721 | // #included file. If the former, rewrite it now. If the later, check to see |
| 722 | // if we rewrote the #include/#import. |
| 723 | SourceLocation Loc = D->getLocation(); |
| 724 | Loc = SM->getExpansionLoc(Loc); |
| 725 | |
| 726 | // If this is for a builtin, ignore it. |
| 727 | if (Loc.isInvalid()) return; |
| 728 | |
| 729 | // Look for built-in declarations that we need to refer during the rewrite. |
| 730 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 731 | RewriteFunctionDecl(FD); |
| 732 | } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) { |
| 733 | // declared in <Foundation/NSString.h> |
| 734 | if (FVD->getName() == "_NSConstantStringClassReference") { |
| 735 | ConstantStringClassReference = FVD; |
| 736 | return; |
| 737 | } |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 738 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
| 739 | RewriteCategoryDecl(CD); |
| 740 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
| 741 | if (PD->isThisDeclarationADefinition()) |
| 742 | RewriteProtocolDecl(PD); |
| 743 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
| 744 | // Recurse into linkage specifications |
| 745 | for (DeclContext::decl_iterator DI = LSD->decls_begin(), |
| 746 | DIEnd = LSD->decls_end(); |
| 747 | DI != DIEnd; ) { |
| 748 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>((*DI))) { |
| 749 | if (!IFace->isThisDeclarationADefinition()) { |
| 750 | SmallVector<Decl *, 8> DG; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 751 | SourceLocation StartLoc = IFace->getBeginLoc(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 752 | do { |
| 753 | if (isa<ObjCInterfaceDecl>(*DI) && |
| 754 | !cast<ObjCInterfaceDecl>(*DI)->isThisDeclarationADefinition() && |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 755 | StartLoc == (*DI)->getBeginLoc()) |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 756 | DG.push_back(*DI); |
| 757 | else |
| 758 | break; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 759 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 760 | ++DI; |
| 761 | } while (DI != DIEnd); |
| 762 | RewriteForwardClassDecl(DG); |
| 763 | continue; |
| 764 | } |
Fariborz Jahanian | 08ed892 | 2012-04-03 17:35:38 +0000 | [diff] [blame] | 765 | else { |
| 766 | // Keep track of all interface declarations seen. |
| 767 | ObjCInterfacesSeen.push_back(IFace); |
| 768 | ++DI; |
| 769 | continue; |
| 770 | } |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>((*DI))) { |
| 774 | if (!Proto->isThisDeclarationADefinition()) { |
| 775 | SmallVector<Decl *, 8> DG; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 776 | SourceLocation StartLoc = Proto->getBeginLoc(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 777 | do { |
| 778 | if (isa<ObjCProtocolDecl>(*DI) && |
| 779 | !cast<ObjCProtocolDecl>(*DI)->isThisDeclarationADefinition() && |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 780 | StartLoc == (*DI)->getBeginLoc()) |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 781 | DG.push_back(*DI); |
| 782 | else |
| 783 | break; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 784 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 785 | ++DI; |
| 786 | } while (DI != DIEnd); |
| 787 | RewriteForwardProtocolDecl(DG); |
| 788 | continue; |
| 789 | } |
| 790 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 791 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 792 | HandleTopLevelSingleDecl(*DI); |
| 793 | ++DI; |
| 794 | } |
| 795 | } |
| 796 | // If we have a decl in the main file, see if we should rewrite it. |
Eli Friedman | 5ba37d5 | 2013-08-22 00:27:10 +0000 | [diff] [blame] | 797 | if (SM->isWrittenInMainFile(Loc)) |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 798 | return HandleDeclInMainFile(D); |
| 799 | } |
| 800 | |
| 801 | //===----------------------------------------------------------------------===// |
| 802 | // Syntactic (non-AST) Rewriting Code |
| 803 | //===----------------------------------------------------------------------===// |
| 804 | |
| 805 | void RewriteModernObjC::RewriteInclude() { |
| 806 | SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID); |
| 807 | StringRef MainBuf = SM->getBufferData(MainFileID); |
| 808 | const char *MainBufStart = MainBuf.begin(); |
| 809 | const char *MainBufEnd = MainBuf.end(); |
| 810 | size_t ImportLen = strlen("import"); |
| 811 | |
| 812 | // Loop over the whole file, looking for includes. |
| 813 | for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { |
| 814 | if (*BufPtr == '#') { |
| 815 | if (++BufPtr == MainBufEnd) |
| 816 | return; |
| 817 | while (*BufPtr == ' ' || *BufPtr == '\t') |
| 818 | if (++BufPtr == MainBufEnd) |
| 819 | return; |
| 820 | if (!strncmp(BufPtr, "import", ImportLen)) { |
| 821 | // replace import with include |
| 822 | SourceLocation ImportLoc = |
| 823 | LocStart.getLocWithOffset(BufPtr-MainBufStart); |
| 824 | ReplaceText(ImportLoc, ImportLen, "include"); |
| 825 | BufPtr += ImportLen; |
| 826 | } |
| 827 | } |
| 828 | } |
| 829 | } |
| 830 | |
Fariborz Jahanian | 89919cc | 2012-05-08 23:54:35 +0000 | [diff] [blame] | 831 | static void WriteInternalIvarName(const ObjCInterfaceDecl *IDecl, |
| 832 | ObjCIvarDecl *IvarDecl, std::string &Result) { |
| 833 | Result += "OBJC_IVAR_$_"; |
| 834 | Result += IDecl->getName(); |
| 835 | Result += "$"; |
| 836 | Result += IvarDecl->getName(); |
| 837 | } |
| 838 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 839 | std::string |
Fariborz Jahanian | 89919cc | 2012-05-08 23:54:35 +0000 | [diff] [blame] | 840 | RewriteModernObjC::getIvarAccessString(ObjCIvarDecl *D) { |
| 841 | const ObjCInterfaceDecl *ClassDecl = D->getContainingInterface(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 842 | |
Fariborz Jahanian | 89919cc | 2012-05-08 23:54:35 +0000 | [diff] [blame] | 843 | // Build name of symbol holding ivar offset. |
| 844 | std::string IvarOffsetName; |
Fariborz Jahanian | 57dd66b | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 845 | if (D->isBitField()) |
| 846 | ObjCIvarBitfieldGroupOffset(D, IvarOffsetName); |
| 847 | else |
| 848 | WriteInternalIvarName(ClassDecl, D, IvarOffsetName); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 849 | |
Fariborz Jahanian | 89919cc | 2012-05-08 23:54:35 +0000 | [diff] [blame] | 850 | std::string S = "(*("; |
| 851 | QualType IvarT = D->getType(); |
Fariborz Jahanian | 57dd66b | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 852 | if (D->isBitField()) |
| 853 | IvarT = GetGroupRecordTypeForObjCIvarBitfield(D); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 854 | |
Fariborz Jahanian | 89919cc | 2012-05-08 23:54:35 +0000 | [diff] [blame] | 855 | if (!isa<TypedefType>(IvarT) && IvarT->isRecordType()) { |
| 856 | RecordDecl *RD = IvarT->getAs<RecordType>()->getDecl(); |
| 857 | RD = RD->getDefinition(); |
| 858 | if (RD && !RD->getDeclName().getAsIdentifierInfo()) { |
| 859 | // decltype(((Foo_IMPL*)0)->bar) * |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 860 | ObjCContainerDecl *CDecl = |
Fariborz Jahanian | 89919cc | 2012-05-08 23:54:35 +0000 | [diff] [blame] | 861 | dyn_cast<ObjCContainerDecl>(D->getDeclContext()); |
| 862 | // ivar in class extensions requires special treatment. |
| 863 | if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) |
| 864 | CDecl = CatDecl->getClassInterface(); |
| 865 | std::string RecName = CDecl->getName(); |
| 866 | RecName += "_IMPL"; |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 867 | RecordDecl *RD = |
| 868 | RecordDecl::Create(*Context, TTK_Struct, TUDecl, SourceLocation(), |
| 869 | SourceLocation(), &Context->Idents.get(RecName)); |
Fariborz Jahanian | 89919cc | 2012-05-08 23:54:35 +0000 | [diff] [blame] | 870 | QualType PtrStructIMPL = Context->getPointerType(Context->getTagDeclType(RD)); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 871 | unsigned UnsignedIntSize = |
Fariborz Jahanian | 89919cc | 2012-05-08 23:54:35 +0000 | [diff] [blame] | 872 | static_cast<unsigned>(Context->getTypeSize(Context->UnsignedIntTy)); |
| 873 | Expr *Zero = IntegerLiteral::Create(*Context, |
| 874 | llvm::APInt(UnsignedIntSize, 0), |
| 875 | Context->UnsignedIntTy, SourceLocation()); |
| 876 | Zero = NoTypeInfoCStyleCastExpr(Context, PtrStructIMPL, CK_BitCast, Zero); |
| 877 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 878 | Zero); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 879 | FieldDecl *FD = FieldDecl::Create(*Context, nullptr, SourceLocation(), |
Fariborz Jahanian | 89919cc | 2012-05-08 23:54:35 +0000 | [diff] [blame] | 880 | SourceLocation(), |
| 881 | &Context->Idents.get(D->getNameAsString()), |
Fariborz Jahanian | f8e68e2 | 2015-04-09 18:36:50 +0000 | [diff] [blame] | 882 | IvarT, nullptr, |
| 883 | /*BitWidth=*/nullptr, /*Mutable=*/true, |
| 884 | ICIS_NoInit); |
| 885 | MemberExpr *ME = new (Context) |
| 886 | MemberExpr(PE, true, SourceLocation(), FD, SourceLocation(), |
| 887 | FD->getType(), VK_LValue, OK_Ordinary); |
| 888 | IvarT = Context->getDecltypeType(ME, ME->getType()); |
| 889 | } |
| 890 | } |
Fariborz Jahanian | 89919cc | 2012-05-08 23:54:35 +0000 | [diff] [blame] | 891 | convertObjCTypeToCStyleType(IvarT); |
| 892 | QualType castT = Context->getPointerType(IvarT); |
| 893 | std::string TypeString(castT.getAsString(Context->getPrintingPolicy())); |
| 894 | S += TypeString; |
| 895 | S += ")"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 896 | |
Fariborz Jahanian | 89919cc | 2012-05-08 23:54:35 +0000 | [diff] [blame] | 897 | // ((char *)self + IVAR_OFFSET_SYMBOL_NAME) |
| 898 | S += "((char *)self + "; |
| 899 | S += IvarOffsetName; |
| 900 | S += "))"; |
Fariborz Jahanian | 57dd66b | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 901 | if (D->isBitField()) { |
| 902 | S += "."; |
| 903 | S += D->getNameAsString(); |
| 904 | } |
Fariborz Jahanian | 89919cc | 2012-05-08 23:54:35 +0000 | [diff] [blame] | 905 | ReferencedIvars[const_cast<ObjCInterfaceDecl *>(ClassDecl)].insert(D); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 906 | return S; |
| 907 | } |
| 908 | |
Fariborz Jahanian | f687e7b | 2012-05-03 22:52:13 +0000 | [diff] [blame] | 909 | /// mustSynthesizeSetterGetterMethod - returns true if setter or getter has not |
| 910 | /// been found in the class implementation. In this case, it must be synthesized. |
| 911 | static bool mustSynthesizeSetterGetterMethod(ObjCImplementationDecl *IMP, |
| 912 | ObjCPropertyDecl *PD, |
| 913 | bool getter) { |
| 914 | return getter ? !IMP->getInstanceMethod(PD->getGetterName()) |
| 915 | : !IMP->getInstanceMethod(PD->getSetterName()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 916 | |
Fariborz Jahanian | f687e7b | 2012-05-03 22:52:13 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 919 | void RewriteModernObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 920 | ObjCImplementationDecl *IMD, |
| 921 | ObjCCategoryImplDecl *CID) { |
| 922 | static bool objcGetPropertyDefined = false; |
| 923 | static bool objcSetPropertyDefined = false; |
Fariborz Jahanian | f687e7b | 2012-05-03 22:52:13 +0000 | [diff] [blame] | 924 | SourceLocation startGetterSetterLoc; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 925 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 926 | if (PID->getBeginLoc().isValid()) { |
| 927 | SourceLocation startLoc = PID->getBeginLoc(); |
Fariborz Jahanian | f687e7b | 2012-05-03 22:52:13 +0000 | [diff] [blame] | 928 | InsertText(startLoc, "// "); |
| 929 | const char *startBuf = SM->getCharacterData(startLoc); |
| 930 | assert((*startBuf == '@') && "bogus @synthesize location"); |
| 931 | const char *semiBuf = strchr(startBuf, ';'); |
| 932 | assert((*semiBuf == ';') && "@synthesize: can't find ';'"); |
| 933 | startGetterSetterLoc = startLoc.getLocWithOffset(semiBuf-startBuf+1); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 934 | } else |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 935 | startGetterSetterLoc = IMD ? IMD->getEndLoc() : CID->getEndLoc(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 936 | |
| 937 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 938 | return; // FIXME: is this correct? |
| 939 | |
| 940 | // Generate the 'getter' function. |
| 941 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 942 | ObjCIvarDecl *OID = PID->getPropertyIvarDecl(); |
Jordan Rose | 755a2ff | 2013-03-15 21:41:35 +0000 | [diff] [blame] | 943 | assert(IMD && OID && "Synthesized ivars must be attached to @implementation"); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 944 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 945 | unsigned Attributes = PD->getPropertyAttributes(); |
Fariborz Jahanian | f687e7b | 2012-05-03 22:52:13 +0000 | [diff] [blame] | 946 | if (mustSynthesizeSetterGetterMethod(IMD, PD, true /*getter*/)) { |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 947 | bool GenGetProperty = !(Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) && |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 948 | (Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 949 | ObjCPropertyDecl::OBJC_PR_copy)); |
| 950 | std::string Getr; |
| 951 | if (GenGetProperty && !objcGetPropertyDefined) { |
| 952 | objcGetPropertyDefined = true; |
| 953 | // FIXME. Is this attribute correct in all cases? |
| 954 | Getr = "\nextern \"C\" __declspec(dllimport) " |
| 955 | "id objc_getProperty(id, SEL, long, bool);\n"; |
| 956 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 957 | RewriteObjCMethodDecl(OID->getContainingInterface(), |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 958 | PD->getGetterMethodDecl(), Getr); |
| 959 | Getr += "{ "; |
| 960 | // Synthesize an explicit cast to gain access to the ivar. |
| 961 | // See objc-act.c:objc_synthesize_new_getter() for details. |
| 962 | if (GenGetProperty) { |
| 963 | // return objc_getProperty(self, _cmd, offsetof(ClassDecl, OID), 1) |
| 964 | Getr += "typedef "; |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 965 | const FunctionType *FPRetType = nullptr; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 966 | RewriteTypeIntoString(PD->getGetterMethodDecl()->getReturnType(), Getr, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 967 | FPRetType); |
| 968 | Getr += " _TYPE"; |
| 969 | if (FPRetType) { |
| 970 | Getr += ")"; // close the precedence "scope" for "*". |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 971 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 972 | // Now, emit the argument types (if any). |
| 973 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)){ |
| 974 | Getr += "("; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 975 | for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) { |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 976 | if (i) Getr += ", "; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 977 | std::string ParamStr = |
| 978 | FT->getParamType(i).getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 979 | Getr += ParamStr; |
| 980 | } |
| 981 | if (FT->isVariadic()) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 982 | if (FT->getNumParams()) |
| 983 | Getr += ", "; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 984 | Getr += "..."; |
| 985 | } |
| 986 | Getr += ")"; |
| 987 | } else |
| 988 | Getr += "()"; |
| 989 | } |
| 990 | Getr += ";\n"; |
| 991 | Getr += "return (_TYPE)"; |
| 992 | Getr += "objc_getProperty(self, _cmd, "; |
| 993 | RewriteIvarOffsetComputation(OID, Getr); |
| 994 | Getr += ", 1)"; |
| 995 | } |
| 996 | else |
| 997 | Getr += "return " + getIvarAccessString(OID); |
| 998 | Getr += "; }"; |
Fariborz Jahanian | f687e7b | 2012-05-03 22:52:13 +0000 | [diff] [blame] | 999 | InsertText(startGetterSetterLoc, Getr); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1000 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1001 | |
| 1002 | if (PD->isReadOnly() || |
Fariborz Jahanian | f687e7b | 2012-05-03 22:52:13 +0000 | [diff] [blame] | 1003 | !mustSynthesizeSetterGetterMethod(IMD, PD, false /*setter*/)) |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1004 | return; |
| 1005 | |
| 1006 | // Generate the 'setter' function. |
| 1007 | std::string Setr; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1008 | bool GenSetProperty = Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1009 | ObjCPropertyDecl::OBJC_PR_copy); |
| 1010 | if (GenSetProperty && !objcSetPropertyDefined) { |
| 1011 | objcSetPropertyDefined = true; |
| 1012 | // FIXME. Is this attribute correct in all cases? |
| 1013 | Setr = "\nextern \"C\" __declspec(dllimport) " |
| 1014 | "void objc_setProperty (id, SEL, long, id, bool, bool);\n"; |
| 1015 | } |
Mandeep Singh Grang | a2baff0 | 2017-07-06 18:49:57 +0000 | [diff] [blame] | 1016 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1017 | RewriteObjCMethodDecl(OID->getContainingInterface(), |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1018 | PD->getSetterMethodDecl(), Setr); |
| 1019 | Setr += "{ "; |
| 1020 | // Synthesize an explicit cast to initialize the ivar. |
| 1021 | // See objc-act.c:objc_synthesize_new_setter() for details. |
| 1022 | if (GenSetProperty) { |
| 1023 | Setr += "objc_setProperty (self, _cmd, "; |
| 1024 | RewriteIvarOffsetComputation(OID, Setr); |
| 1025 | Setr += ", (id)"; |
| 1026 | Setr += PD->getName(); |
| 1027 | Setr += ", "; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1028 | if (Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1029 | Setr += "0, "; |
| 1030 | else |
| 1031 | Setr += "1, "; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1032 | if (Attributes & ObjCPropertyDecl::OBJC_PR_copy) |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1033 | Setr += "1)"; |
| 1034 | else |
| 1035 | Setr += "0)"; |
| 1036 | } |
| 1037 | else { |
| 1038 | Setr += getIvarAccessString(OID) + " = "; |
| 1039 | Setr += PD->getName(); |
| 1040 | } |
Fariborz Jahanian | f687e7b | 2012-05-03 22:52:13 +0000 | [diff] [blame] | 1041 | Setr += "; }\n"; |
| 1042 | InsertText(startGetterSetterLoc, Setr); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | static void RewriteOneForwardClassDecl(ObjCInterfaceDecl *ForwardDecl, |
| 1046 | std::string &typedefString) { |
Fariborz Jahanian | e8730a3 | 2013-02-08 17:15:07 +0000 | [diff] [blame] | 1047 | typedefString += "\n#ifndef _REWRITER_typedef_"; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1048 | typedefString += ForwardDecl->getNameAsString(); |
| 1049 | typedefString += "\n"; |
| 1050 | typedefString += "#define _REWRITER_typedef_"; |
| 1051 | typedefString += ForwardDecl->getNameAsString(); |
| 1052 | typedefString += "\n"; |
| 1053 | typedefString += "typedef struct objc_object "; |
| 1054 | typedefString += ForwardDecl->getNameAsString(); |
Fariborz Jahanian | 2cc29af | 2012-03-12 23:58:28 +0000 | [diff] [blame] | 1055 | // typedef struct { } _objc_exc_Classname; |
| 1056 | typedefString += ";\ntypedef struct {} _objc_exc_"; |
| 1057 | typedefString += ForwardDecl->getNameAsString(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1058 | typedefString += ";\n#endif\n"; |
| 1059 | } |
| 1060 | |
| 1061 | void RewriteModernObjC::RewriteForwardClassEpilogue(ObjCInterfaceDecl *ClassDecl, |
| 1062 | const std::string &typedefString) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1063 | SourceLocation startLoc = ClassDecl->getBeginLoc(); |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 1064 | const char *startBuf = SM->getCharacterData(startLoc); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1065 | const char *semiPtr = strchr(startBuf, ';'); |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 1066 | // Replace the @class with typedefs corresponding to the classes. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1067 | ReplaceText(startLoc, semiPtr-startBuf+1, typedefString); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | void RewriteModernObjC::RewriteForwardClassDecl(DeclGroupRef D) { |
| 1071 | std::string typedefString; |
| 1072 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
Fariborz Jahanian | 0dded8a | 2013-09-24 17:03:07 +0000 | [diff] [blame] | 1073 | if (ObjCInterfaceDecl *ForwardDecl = dyn_cast<ObjCInterfaceDecl>(*I)) { |
| 1074 | if (I == D.begin()) { |
| 1075 | // Translate to typedef's that forward reference structs with the same name |
| 1076 | // as the class. As a convenience, we include the original declaration |
| 1077 | // as a comment. |
| 1078 | typedefString += "// @class "; |
| 1079 | typedefString += ForwardDecl->getNameAsString(); |
| 1080 | typedefString += ";"; |
| 1081 | } |
| 1082 | RewriteOneForwardClassDecl(ForwardDecl, typedefString); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1083 | } |
Fariborz Jahanian | 0dded8a | 2013-09-24 17:03:07 +0000 | [diff] [blame] | 1084 | else |
| 1085 | HandleTopLevelSingleDecl(*I); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1086 | } |
| 1087 | DeclGroupRef::iterator I = D.begin(); |
| 1088 | RewriteForwardClassEpilogue(cast<ObjCInterfaceDecl>(*I), typedefString); |
| 1089 | } |
| 1090 | |
| 1091 | void RewriteModernObjC::RewriteForwardClassDecl( |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 1092 | const SmallVectorImpl<Decl *> &D) { |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1093 | std::string typedefString; |
| 1094 | for (unsigned i = 0; i < D.size(); i++) { |
| 1095 | ObjCInterfaceDecl *ForwardDecl = cast<ObjCInterfaceDecl>(D[i]); |
| 1096 | if (i == 0) { |
| 1097 | typedefString += "// @class "; |
| 1098 | typedefString += ForwardDecl->getNameAsString(); |
Fariborz Jahanian | e8730a3 | 2013-02-08 17:15:07 +0000 | [diff] [blame] | 1099 | typedefString += ";"; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1100 | } |
| 1101 | RewriteOneForwardClassDecl(ForwardDecl, typedefString); |
| 1102 | } |
| 1103 | RewriteForwardClassEpilogue(cast<ObjCInterfaceDecl>(D[0]), typedefString); |
| 1104 | } |
| 1105 | |
| 1106 | void RewriteModernObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) { |
| 1107 | // When method is a synthesized one, such as a getter/setter there is |
| 1108 | // nothing to rewrite. |
| 1109 | if (Method->isImplicit()) |
| 1110 | return; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1111 | SourceLocation LocStart = Method->getBeginLoc(); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1112 | SourceLocation LocEnd = Method->getEndLoc(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1113 | |
| 1114 | if (SM->getExpansionLineNumber(LocEnd) > |
| 1115 | SM->getExpansionLineNumber(LocStart)) { |
| 1116 | InsertText(LocStart, "#if 0\n"); |
| 1117 | ReplaceText(LocEnd, 1, ";\n#endif\n"); |
| 1118 | } else { |
| 1119 | InsertText(LocStart, "// "); |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | void RewriteModernObjC::RewriteProperty(ObjCPropertyDecl *prop) { |
| 1124 | SourceLocation Loc = prop->getAtLoc(); |
| 1125 | |
| 1126 | ReplaceText(Loc, 0, "// "); |
| 1127 | // FIXME: handle properties that are declared across multiple lines. |
| 1128 | } |
| 1129 | |
| 1130 | void RewriteModernObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1131 | SourceLocation LocStart = CatDecl->getBeginLoc(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1132 | |
| 1133 | // FIXME: handle category headers that are declared across multiple lines. |
Fariborz Jahanian | 144b722 | 2012-05-01 17:46:45 +0000 | [diff] [blame] | 1134 | if (CatDecl->getIvarRBraceLoc().isValid()) { |
| 1135 | ReplaceText(LocStart, 1, "/** "); |
| 1136 | ReplaceText(CatDecl->getIvarRBraceLoc(), 1, "**/ "); |
| 1137 | } |
| 1138 | else { |
Fariborz Jahanian | 2b383d21 | 2012-02-19 19:00:05 +0000 | [diff] [blame] | 1139 | ReplaceText(LocStart, 0, "// "); |
Fariborz Jahanian | 144b722 | 2012-05-01 17:46:45 +0000 | [diff] [blame] | 1140 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1141 | |
Manman Ren | a7a8b1f | 2016-01-26 18:05:23 +0000 | [diff] [blame] | 1142 | for (auto *I : CatDecl->instance_properties()) |
Aaron Ballman | dc4bea4 | 2014-03-13 18:47:37 +0000 | [diff] [blame] | 1143 | RewriteProperty(I); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1144 | |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 1145 | for (auto *I : CatDecl->instance_methods()) |
| 1146 | RewriteMethodDeclaration(I); |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 1147 | for (auto *I : CatDecl->class_methods()) |
| 1148 | RewriteMethodDeclaration(I); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1149 | |
| 1150 | // Lastly, comment out the @end. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1151 | ReplaceText(CatDecl->getAtEndRange().getBegin(), |
Fariborz Jahanian | 3a65ce3 | 2013-04-03 19:11:21 +0000 | [diff] [blame] | 1152 | strlen("@end"), "/* @end */\n"); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | void RewriteModernObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1156 | SourceLocation LocStart = PDecl->getBeginLoc(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1157 | assert(PDecl->isThisDeclarationADefinition()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1158 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1159 | // FIXME: handle protocol headers that are declared across multiple lines. |
| 1160 | ReplaceText(LocStart, 0, "// "); |
| 1161 | |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 1162 | for (auto *I : PDecl->instance_methods()) |
| 1163 | RewriteMethodDeclaration(I); |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 1164 | for (auto *I : PDecl->class_methods()) |
| 1165 | RewriteMethodDeclaration(I); |
Manman Ren | a7a8b1f | 2016-01-26 18:05:23 +0000 | [diff] [blame] | 1166 | for (auto *I : PDecl->instance_properties()) |
Aaron Ballman | dc4bea4 | 2014-03-13 18:47:37 +0000 | [diff] [blame] | 1167 | RewriteProperty(I); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1168 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1169 | // Lastly, comment out the @end. |
| 1170 | SourceLocation LocEnd = PDecl->getAtEndRange().getBegin(); |
Fariborz Jahanian | 3a65ce3 | 2013-04-03 19:11:21 +0000 | [diff] [blame] | 1171 | ReplaceText(LocEnd, strlen("@end"), "/* @end */\n"); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1172 | |
| 1173 | // Must comment out @optional/@required |
| 1174 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1175 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 1176 | for (const char *p = startBuf; p < endBuf; p++) { |
| 1177 | if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { |
| 1178 | SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf); |
| 1179 | ReplaceText(OptionalLoc, strlen("@optional"), "/* @optional */"); |
| 1180 | |
| 1181 | } |
| 1182 | else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { |
| 1183 | SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf); |
| 1184 | ReplaceText(OptionalLoc, strlen("@required"), "/* @required */"); |
| 1185 | |
| 1186 | } |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | void RewriteModernObjC::RewriteForwardProtocolDecl(DeclGroupRef D) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1191 | SourceLocation LocStart = (*D.begin())->getBeginLoc(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1192 | if (LocStart.isInvalid()) |
| 1193 | llvm_unreachable("Invalid SourceLocation"); |
| 1194 | // FIXME: handle forward protocol that are declared across multiple lines. |
| 1195 | ReplaceText(LocStart, 0, "// "); |
| 1196 | } |
| 1197 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1198 | void |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 1199 | RewriteModernObjC::RewriteForwardProtocolDecl(const SmallVectorImpl<Decl *> &DG) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1200 | SourceLocation LocStart = DG[0]->getBeginLoc(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1201 | if (LocStart.isInvalid()) |
| 1202 | llvm_unreachable("Invalid SourceLocation"); |
| 1203 | // FIXME: handle forward protocol that are declared across multiple lines. |
| 1204 | ReplaceText(LocStart, 0, "// "); |
| 1205 | } |
| 1206 | |
| 1207 | void RewriteModernObjC::RewriteTypeIntoString(QualType T, std::string &ResultStr, |
| 1208 | const FunctionType *&FPRetType) { |
| 1209 | if (T->isObjCQualifiedIdType()) |
| 1210 | ResultStr += "id"; |
| 1211 | else if (T->isFunctionPointerType() || |
| 1212 | T->isBlockPointerType()) { |
| 1213 | // needs special handling, since pointer-to-functions have special |
| 1214 | // syntax (where a decaration models use). |
| 1215 | QualType retType = T; |
| 1216 | QualType PointeeTy; |
| 1217 | if (const PointerType* PT = retType->getAs<PointerType>()) |
| 1218 | PointeeTy = PT->getPointeeType(); |
| 1219 | else if (const BlockPointerType *BPT = retType->getAs<BlockPointerType>()) |
| 1220 | PointeeTy = BPT->getPointeeType(); |
| 1221 | if ((FPRetType = PointeeTy->getAs<FunctionType>())) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1222 | ResultStr += |
| 1223 | FPRetType->getReturnType().getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1224 | ResultStr += "(*"; |
| 1225 | } |
| 1226 | } else |
| 1227 | ResultStr += T.getAsString(Context->getPrintingPolicy()); |
| 1228 | } |
| 1229 | |
| 1230 | void RewriteModernObjC::RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl, |
| 1231 | ObjCMethodDecl *OMD, |
| 1232 | std::string &ResultStr) { |
| 1233 | //fprintf(stderr,"In RewriteObjCMethodDecl\n"); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 1234 | const FunctionType *FPRetType = nullptr; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1235 | ResultStr += "\nstatic "; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1236 | RewriteTypeIntoString(OMD->getReturnType(), ResultStr, FPRetType); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1237 | ResultStr += " "; |
| 1238 | |
| 1239 | // Unique method name |
| 1240 | std::string NameStr; |
| 1241 | |
| 1242 | if (OMD->isInstanceMethod()) |
| 1243 | NameStr += "_I_"; |
| 1244 | else |
| 1245 | NameStr += "_C_"; |
| 1246 | |
| 1247 | NameStr += IDecl->getNameAsString(); |
| 1248 | NameStr += "_"; |
| 1249 | |
| 1250 | if (ObjCCategoryImplDecl *CID = |
| 1251 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
| 1252 | NameStr += CID->getNameAsString(); |
| 1253 | NameStr += "_"; |
| 1254 | } |
| 1255 | // Append selector names, replacing ':' with '_' |
| 1256 | { |
| 1257 | std::string selString = OMD->getSelector().getAsString(); |
| 1258 | int len = selString.size(); |
| 1259 | for (int i = 0; i < len; i++) |
| 1260 | if (selString[i] == ':') |
| 1261 | selString[i] = '_'; |
| 1262 | NameStr += selString; |
| 1263 | } |
| 1264 | // Remember this name for metadata emission |
| 1265 | MethodInternalNames[OMD] = NameStr; |
| 1266 | ResultStr += NameStr; |
| 1267 | |
| 1268 | // Rewrite arguments |
| 1269 | ResultStr += "("; |
| 1270 | |
| 1271 | // invisible arguments |
| 1272 | if (OMD->isInstanceMethod()) { |
| 1273 | QualType selfTy = Context->getObjCInterfaceType(IDecl); |
| 1274 | selfTy = Context->getPointerType(selfTy); |
| 1275 | if (!LangOpts.MicrosoftExt) { |
| 1276 | if (ObjCSynthesizedStructs.count(const_cast<ObjCInterfaceDecl*>(IDecl))) |
| 1277 | ResultStr += "struct "; |
| 1278 | } |
| 1279 | // When rewriting for Microsoft, explicitly omit the structure name. |
| 1280 | ResultStr += IDecl->getNameAsString(); |
| 1281 | ResultStr += " *"; |
| 1282 | } |
| 1283 | else |
| 1284 | ResultStr += Context->getObjCClassType().getAsString( |
| 1285 | Context->getPrintingPolicy()); |
| 1286 | |
| 1287 | ResultStr += " self, "; |
| 1288 | ResultStr += Context->getObjCSelType().getAsString(Context->getPrintingPolicy()); |
| 1289 | ResultStr += " _cmd"; |
| 1290 | |
| 1291 | // Method arguments. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 1292 | for (const auto *PDecl : OMD->parameters()) { |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1293 | ResultStr += ", "; |
| 1294 | if (PDecl->getType()->isObjCQualifiedIdType()) { |
| 1295 | ResultStr += "id "; |
| 1296 | ResultStr += PDecl->getNameAsString(); |
| 1297 | } else { |
| 1298 | std::string Name = PDecl->getNameAsString(); |
| 1299 | QualType QT = PDecl->getType(); |
| 1300 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 835cabe | 2012-03-27 16:42:20 +0000 | [diff] [blame] | 1301 | (void)convertBlockPointerToFunctionPointer(QT); |
| 1302 | QT.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1303 | ResultStr += Name; |
| 1304 | } |
| 1305 | } |
| 1306 | if (OMD->isVariadic()) |
| 1307 | ResultStr += ", ..."; |
| 1308 | ResultStr += ") "; |
| 1309 | |
| 1310 | if (FPRetType) { |
| 1311 | ResultStr += ")"; // close the precedence "scope" for "*". |
| 1312 | |
| 1313 | // Now, emit the argument types (if any). |
| 1314 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) { |
| 1315 | ResultStr += "("; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1316 | for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) { |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1317 | if (i) ResultStr += ", "; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1318 | std::string ParamStr = |
| 1319 | FT->getParamType(i).getAsString(Context->getPrintingPolicy()); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1320 | ResultStr += ParamStr; |
| 1321 | } |
| 1322 | if (FT->isVariadic()) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1323 | if (FT->getNumParams()) |
| 1324 | ResultStr += ", "; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1325 | ResultStr += "..."; |
| 1326 | } |
| 1327 | ResultStr += ")"; |
| 1328 | } else { |
| 1329 | ResultStr += "()"; |
| 1330 | } |
| 1331 | } |
| 1332 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 1333 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1334 | void RewriteModernObjC::RewriteImplementationDecl(Decl *OID) { |
| 1335 | ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID); |
| 1336 | ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID); |
| 1337 | |
Fariborz Jahanian | 2b383d21 | 2012-02-19 19:00:05 +0000 | [diff] [blame] | 1338 | if (IMD) { |
Fariborz Jahanian | 144b722 | 2012-05-01 17:46:45 +0000 | [diff] [blame] | 1339 | if (IMD->getIvarRBraceLoc().isValid()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1340 | ReplaceText(IMD->getBeginLoc(), 1, "/** "); |
Fariborz Jahanian | 144b722 | 2012-05-01 17:46:45 +0000 | [diff] [blame] | 1341 | ReplaceText(IMD->getIvarRBraceLoc(), 1, "**/ "); |
Fariborz Jahanian | 2b383d21 | 2012-02-19 19:00:05 +0000 | [diff] [blame] | 1342 | } |
Fariborz Jahanian | 144b722 | 2012-05-01 17:46:45 +0000 | [diff] [blame] | 1343 | else { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1344 | InsertText(IMD->getBeginLoc(), "// "); |
Fariborz Jahanian | 144b722 | 2012-05-01 17:46:45 +0000 | [diff] [blame] | 1345 | } |
Fariborz Jahanian | 2b383d21 | 2012-02-19 19:00:05 +0000 | [diff] [blame] | 1346 | } |
| 1347 | else |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1348 | InsertText(CID->getBeginLoc(), "// "); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1349 | |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 1350 | for (auto *OMD : IMD ? IMD->instance_methods() : CID->instance_methods()) { |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1351 | std::string ResultStr; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1352 | RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1353 | SourceLocation LocStart = OMD->getBeginLoc(); |
| 1354 | SourceLocation LocEnd = OMD->getCompoundBody()->getBeginLoc(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1355 | |
| 1356 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1357 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 1358 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
| 1359 | } |
| 1360 | |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 1361 | for (auto *OMD : IMD ? IMD->class_methods() : CID->class_methods()) { |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1362 | std::string ResultStr; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1363 | RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1364 | SourceLocation LocStart = OMD->getBeginLoc(); |
| 1365 | SourceLocation LocEnd = OMD->getCompoundBody()->getBeginLoc(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1366 | |
| 1367 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1368 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 1369 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
| 1370 | } |
Aaron Ballman | d85eff4 | 2014-03-14 15:02:45 +0000 | [diff] [blame] | 1371 | for (auto *I : IMD ? IMD->property_impls() : CID->property_impls()) |
| 1372 | RewritePropertyImplDecl(I, IMD, CID); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1373 | |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1374 | InsertText(IMD ? IMD->getEndLoc() : CID->getEndLoc(), "// "); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
| 1377 | void RewriteModernObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { |
Fariborz Jahanian | 088959a | 2012-02-11 20:10:52 +0000 | [diff] [blame] | 1378 | // Do not synthesize more than once. |
| 1379 | if (ObjCSynthesizedStructs.count(ClassDecl)) |
| 1380 | return; |
| 1381 | // Make sure super class's are written before current class is written. |
| 1382 | ObjCInterfaceDecl *SuperClass = ClassDecl->getSuperClass(); |
| 1383 | while (SuperClass) { |
| 1384 | RewriteInterfaceDecl(SuperClass); |
| 1385 | SuperClass = SuperClass->getSuperClass(); |
| 1386 | } |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1387 | std::string ResultStr; |
Fariborz Jahanian | d268b0c | 2012-02-17 22:20:12 +0000 | [diff] [blame] | 1388 | if (!ObjCWrittenInterfaces.count(ClassDecl->getCanonicalDecl())) { |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1389 | // we haven't seen a forward decl - generate a typedef. |
Fariborz Jahanian | 2cc29af | 2012-03-12 23:58:28 +0000 | [diff] [blame] | 1390 | RewriteOneForwardClassDecl(ClassDecl, ResultStr); |
Fariborz Jahanian | 5e49eb9 | 2012-02-22 18:13:25 +0000 | [diff] [blame] | 1391 | RewriteIvarOffsetSymbols(ClassDecl, ResultStr); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1392 | |
Fariborz Jahanian | ff51338 | 2012-02-15 22:01:47 +0000 | [diff] [blame] | 1393 | RewriteObjCInternalStruct(ClassDecl, ResultStr); |
Fariborz Jahanian | d268b0c | 2012-02-17 22:20:12 +0000 | [diff] [blame] | 1394 | // Mark this typedef as having been written into its c++ equivalent. |
| 1395 | ObjCWrittenInterfaces.insert(ClassDecl->getCanonicalDecl()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1396 | |
Manman Ren | a7a8b1f | 2016-01-26 18:05:23 +0000 | [diff] [blame] | 1397 | for (auto *I : ClassDecl->instance_properties()) |
Aaron Ballman | dc4bea4 | 2014-03-13 18:47:37 +0000 | [diff] [blame] | 1398 | RewriteProperty(I); |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 1399 | for (auto *I : ClassDecl->instance_methods()) |
| 1400 | RewriteMethodDeclaration(I); |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 1401 | for (auto *I : ClassDecl->class_methods()) |
| 1402 | RewriteMethodDeclaration(I); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1403 | |
Fariborz Jahanian | ff51338 | 2012-02-15 22:01:47 +0000 | [diff] [blame] | 1404 | // Lastly, comment out the @end. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1405 | ReplaceText(ClassDecl->getAtEndRange().getBegin(), strlen("@end"), |
Fariborz Jahanian | 3a65ce3 | 2013-04-03 19:11:21 +0000 | [diff] [blame] | 1406 | "/* @end */\n"); |
Fariborz Jahanian | ff51338 | 2012-02-15 22:01:47 +0000 | [diff] [blame] | 1407 | } |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | Stmt *RewriteModernObjC::RewritePropertyOrImplicitSetter(PseudoObjectExpr *PseudoOp) { |
| 1411 | SourceRange OldRange = PseudoOp->getSourceRange(); |
| 1412 | |
| 1413 | // We just magically know some things about the structure of this |
| 1414 | // expression. |
| 1415 | ObjCMessageExpr *OldMsg = |
| 1416 | cast<ObjCMessageExpr>(PseudoOp->getSemanticExpr( |
| 1417 | PseudoOp->getNumSemanticExprs() - 1)); |
| 1418 | |
| 1419 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 1420 | // we need to suppress rewriting the sub-statements. |
Fariborz Jahanian | 31176b1 | 2012-04-10 22:06:54 +0000 | [diff] [blame] | 1421 | Expr *Base; |
| 1422 | SmallVector<Expr*, 2> Args; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1423 | { |
| 1424 | DisableReplaceStmtScope S(*this); |
| 1425 | |
| 1426 | // Rebuild the base expression if we have one. |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 1427 | Base = nullptr; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1428 | if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) { |
| 1429 | Base = OldMsg->getInstanceReceiver(); |
| 1430 | Base = cast<OpaqueValueExpr>(Base)->getSourceExpr(); |
| 1431 | Base = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Base)); |
| 1432 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1433 | |
Fariborz Jahanian | 31176b1 | 2012-04-10 22:06:54 +0000 | [diff] [blame] | 1434 | unsigned numArgs = OldMsg->getNumArgs(); |
| 1435 | for (unsigned i = 0; i < numArgs; i++) { |
| 1436 | Expr *Arg = OldMsg->getArg(i); |
| 1437 | if (isa<OpaqueValueExpr>(Arg)) |
| 1438 | Arg = cast<OpaqueValueExpr>(Arg)->getSourceExpr(); |
| 1439 | Arg = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Arg)); |
| 1440 | Args.push_back(Arg); |
| 1441 | } |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | // TODO: avoid this copy. |
| 1445 | SmallVector<SourceLocation, 1> SelLocs; |
| 1446 | OldMsg->getSelectorLocs(SelLocs); |
| 1447 | |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 1448 | ObjCMessageExpr *NewMsg = nullptr; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1449 | switch (OldMsg->getReceiverKind()) { |
| 1450 | case ObjCMessageExpr::Class: |
| 1451 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1452 | OldMsg->getValueKind(), |
| 1453 | OldMsg->getLeftLoc(), |
| 1454 | OldMsg->getClassReceiverTypeInfo(), |
| 1455 | OldMsg->getSelector(), |
| 1456 | SelLocs, |
| 1457 | OldMsg->getMethodDecl(), |
Fariborz Jahanian | 31176b1 | 2012-04-10 22:06:54 +0000 | [diff] [blame] | 1458 | Args, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1459 | OldMsg->getRightLoc(), |
| 1460 | OldMsg->isImplicit()); |
| 1461 | break; |
| 1462 | |
| 1463 | case ObjCMessageExpr::Instance: |
| 1464 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1465 | OldMsg->getValueKind(), |
| 1466 | OldMsg->getLeftLoc(), |
| 1467 | Base, |
| 1468 | OldMsg->getSelector(), |
| 1469 | SelLocs, |
| 1470 | OldMsg->getMethodDecl(), |
Fariborz Jahanian | 31176b1 | 2012-04-10 22:06:54 +0000 | [diff] [blame] | 1471 | Args, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1472 | OldMsg->getRightLoc(), |
| 1473 | OldMsg->isImplicit()); |
| 1474 | break; |
| 1475 | |
| 1476 | case ObjCMessageExpr::SuperClass: |
| 1477 | case ObjCMessageExpr::SuperInstance: |
| 1478 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1479 | OldMsg->getValueKind(), |
| 1480 | OldMsg->getLeftLoc(), |
| 1481 | OldMsg->getSuperLoc(), |
| 1482 | OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance, |
| 1483 | OldMsg->getSuperType(), |
| 1484 | OldMsg->getSelector(), |
| 1485 | SelLocs, |
| 1486 | OldMsg->getMethodDecl(), |
Fariborz Jahanian | 31176b1 | 2012-04-10 22:06:54 +0000 | [diff] [blame] | 1487 | Args, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1488 | OldMsg->getRightLoc(), |
| 1489 | OldMsg->isImplicit()); |
| 1490 | break; |
| 1491 | } |
| 1492 | |
| 1493 | Stmt *Replacement = SynthMessageExpr(NewMsg); |
| 1494 | ReplaceStmtWithRange(PseudoOp, Replacement, OldRange); |
| 1495 | return Replacement; |
| 1496 | } |
| 1497 | |
| 1498 | Stmt *RewriteModernObjC::RewritePropertyOrImplicitGetter(PseudoObjectExpr *PseudoOp) { |
| 1499 | SourceRange OldRange = PseudoOp->getSourceRange(); |
| 1500 | |
| 1501 | // We just magically know some things about the structure of this |
| 1502 | // expression. |
| 1503 | ObjCMessageExpr *OldMsg = |
| 1504 | cast<ObjCMessageExpr>(PseudoOp->getResultExpr()->IgnoreImplicit()); |
| 1505 | |
| 1506 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 1507 | // we need to suppress rewriting the sub-statements. |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 1508 | Expr *Base = nullptr; |
Fariborz Jahanian | 31176b1 | 2012-04-10 22:06:54 +0000 | [diff] [blame] | 1509 | SmallVector<Expr*, 1> Args; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1510 | { |
| 1511 | DisableReplaceStmtScope S(*this); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1512 | // Rebuild the base expression if we have one. |
| 1513 | if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) { |
| 1514 | Base = OldMsg->getInstanceReceiver(); |
| 1515 | Base = cast<OpaqueValueExpr>(Base)->getSourceExpr(); |
| 1516 | Base = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Base)); |
| 1517 | } |
Fariborz Jahanian | 31176b1 | 2012-04-10 22:06:54 +0000 | [diff] [blame] | 1518 | unsigned numArgs = OldMsg->getNumArgs(); |
| 1519 | for (unsigned i = 0; i < numArgs; i++) { |
| 1520 | Expr *Arg = OldMsg->getArg(i); |
| 1521 | if (isa<OpaqueValueExpr>(Arg)) |
| 1522 | Arg = cast<OpaqueValueExpr>(Arg)->getSourceExpr(); |
| 1523 | Arg = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Arg)); |
| 1524 | Args.push_back(Arg); |
| 1525 | } |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | // Intentionally empty. |
| 1529 | SmallVector<SourceLocation, 1> SelLocs; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1530 | |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 1531 | ObjCMessageExpr *NewMsg = nullptr; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1532 | switch (OldMsg->getReceiverKind()) { |
| 1533 | case ObjCMessageExpr::Class: |
| 1534 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1535 | OldMsg->getValueKind(), |
| 1536 | OldMsg->getLeftLoc(), |
| 1537 | OldMsg->getClassReceiverTypeInfo(), |
| 1538 | OldMsg->getSelector(), |
| 1539 | SelLocs, |
| 1540 | OldMsg->getMethodDecl(), |
| 1541 | Args, |
| 1542 | OldMsg->getRightLoc(), |
| 1543 | OldMsg->isImplicit()); |
| 1544 | break; |
| 1545 | |
| 1546 | case ObjCMessageExpr::Instance: |
| 1547 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1548 | OldMsg->getValueKind(), |
| 1549 | OldMsg->getLeftLoc(), |
| 1550 | Base, |
| 1551 | OldMsg->getSelector(), |
| 1552 | SelLocs, |
| 1553 | OldMsg->getMethodDecl(), |
| 1554 | Args, |
| 1555 | OldMsg->getRightLoc(), |
| 1556 | OldMsg->isImplicit()); |
| 1557 | break; |
| 1558 | |
| 1559 | case ObjCMessageExpr::SuperClass: |
| 1560 | case ObjCMessageExpr::SuperInstance: |
| 1561 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1562 | OldMsg->getValueKind(), |
| 1563 | OldMsg->getLeftLoc(), |
| 1564 | OldMsg->getSuperLoc(), |
| 1565 | OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance, |
| 1566 | OldMsg->getSuperType(), |
| 1567 | OldMsg->getSelector(), |
| 1568 | SelLocs, |
| 1569 | OldMsg->getMethodDecl(), |
| 1570 | Args, |
| 1571 | OldMsg->getRightLoc(), |
| 1572 | OldMsg->isImplicit()); |
| 1573 | break; |
| 1574 | } |
| 1575 | |
| 1576 | Stmt *Replacement = SynthMessageExpr(NewMsg); |
| 1577 | ReplaceStmtWithRange(PseudoOp, Replacement, OldRange); |
| 1578 | return Replacement; |
| 1579 | } |
| 1580 | |
| 1581 | /// SynthCountByEnumWithState - To print: |
Fariborz Jahanian | 6c0af64 | 2013-09-05 17:17:32 +0000 | [diff] [blame] | 1582 | /// ((NSUInteger (*) |
| 1583 | /// (id, SEL, struct __objcFastEnumerationState *, id *, NSUInteger)) |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1584 | /// (void *)objc_msgSend)((id)l_collection, |
| 1585 | /// sel_registerName( |
| 1586 | /// "countByEnumeratingWithState:objects:count:"), |
| 1587 | /// &enumState, |
Fariborz Jahanian | 6c0af64 | 2013-09-05 17:17:32 +0000 | [diff] [blame] | 1588 | /// (id *)__rw_items, (NSUInteger)16) |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1589 | /// |
| 1590 | void RewriteModernObjC::SynthCountByEnumWithState(std::string &buf) { |
Fariborz Jahanian | 6c0af64 | 2013-09-05 17:17:32 +0000 | [diff] [blame] | 1591 | buf += "((_WIN_NSUInteger (*) (id, SEL, struct __objcFastEnumerationState *, " |
| 1592 | "id *, _WIN_NSUInteger))(void *)objc_msgSend)"; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1593 | buf += "\n\t\t"; |
| 1594 | buf += "((id)l_collection,\n\t\t"; |
| 1595 | buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; |
| 1596 | buf += "\n\t\t"; |
| 1597 | buf += "&enumState, " |
Fariborz Jahanian | 6c0af64 | 2013-09-05 17:17:32 +0000 | [diff] [blame] | 1598 | "(id *)__rw_items, (_WIN_NSUInteger)16)"; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1599 | } |
| 1600 | |
| 1601 | /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach |
| 1602 | /// statement to exit to its outer synthesized loop. |
| 1603 | /// |
| 1604 | Stmt *RewriteModernObjC::RewriteBreakStmt(BreakStmt *S) { |
| 1605 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1606 | return S; |
| 1607 | // replace break with goto __break_label |
| 1608 | std::string buf; |
| 1609 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1610 | SourceLocation startLoc = S->getBeginLoc(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1611 | buf = "goto __break_label_"; |
| 1612 | buf += utostr(ObjCBcLabelNo.back()); |
| 1613 | ReplaceText(startLoc, strlen("break"), buf); |
| 1614 | |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 1615 | return nullptr; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
Fariborz Jahanian | b6933bc | 2012-11-06 23:25:49 +0000 | [diff] [blame] | 1618 | void RewriteModernObjC::ConvertSourceLocationToLineDirective( |
| 1619 | SourceLocation Loc, |
| 1620 | std::string &LineString) { |
Fariborz Jahanian | e4c7e85 | 2013-02-08 00:27:34 +0000 | [diff] [blame] | 1621 | if (Loc.isFileID() && GenerateLineInfo) { |
Fariborz Jahanian | b6933bc | 2012-11-06 23:25:49 +0000 | [diff] [blame] | 1622 | LineString += "\n#line "; |
| 1623 | PresumedLoc PLoc = SM->getPresumedLoc(Loc); |
| 1624 | LineString += utostr(PLoc.getLine()); |
| 1625 | LineString += " \""; |
| 1626 | LineString += Lexer::Stringify(PLoc.getFilename()); |
| 1627 | LineString += "\"\n"; |
| 1628 | } |
| 1629 | } |
| 1630 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1631 | /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach |
| 1632 | /// statement to continue with its inner synthesized loop. |
| 1633 | /// |
| 1634 | Stmt *RewriteModernObjC::RewriteContinueStmt(ContinueStmt *S) { |
| 1635 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1636 | return S; |
| 1637 | // replace continue with goto __continue_label |
| 1638 | std::string buf; |
| 1639 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1640 | SourceLocation startLoc = S->getBeginLoc(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1641 | buf = "goto __continue_label_"; |
| 1642 | buf += utostr(ObjCBcLabelNo.back()); |
| 1643 | ReplaceText(startLoc, strlen("continue"), buf); |
| 1644 | |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 1645 | return nullptr; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1646 | } |
| 1647 | |
| 1648 | /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. |
| 1649 | /// It rewrites: |
| 1650 | /// for ( type elem in collection) { stmts; } |
| 1651 | |
| 1652 | /// Into: |
| 1653 | /// { |
| 1654 | /// type elem; |
| 1655 | /// struct __objcFastEnumerationState enumState = { 0 }; |
| 1656 | /// id __rw_items[16]; |
| 1657 | /// id l_collection = (id)collection; |
Fariborz Jahanian | 6c0af64 | 2013-09-05 17:17:32 +0000 | [diff] [blame] | 1658 | /// NSUInteger limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1659 | /// objects:__rw_items count:16]; |
| 1660 | /// if (limit) { |
| 1661 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1662 | /// do { |
| 1663 | /// unsigned long counter = 0; |
| 1664 | /// do { |
| 1665 | /// if (startMutations != *enumState.mutationsPtr) |
| 1666 | /// objc_enumerationMutation(l_collection); |
| 1667 | /// elem = (type)enumState.itemsPtr[counter++]; |
| 1668 | /// stmts; |
| 1669 | /// __continue_label: ; |
| 1670 | /// } while (counter < limit); |
Fariborz Jahanian | 6c0af64 | 2013-09-05 17:17:32 +0000 | [diff] [blame] | 1671 | /// } while ((limit = [l_collection countByEnumeratingWithState:&enumState |
| 1672 | /// objects:__rw_items count:16])); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1673 | /// elem = nil; |
| 1674 | /// __break_label: ; |
| 1675 | /// } |
| 1676 | /// else |
| 1677 | /// elem = nil; |
| 1678 | /// } |
| 1679 | /// |
| 1680 | Stmt *RewriteModernObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
| 1681 | SourceLocation OrigEnd) { |
| 1682 | assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); |
| 1683 | assert(isa<ObjCForCollectionStmt>(Stmts.back()) && |
| 1684 | "ObjCForCollectionStmt Statement stack mismatch"); |
| 1685 | assert(!ObjCBcLabelNo.empty() && |
| 1686 | "ObjCForCollectionStmt - Label No stack empty"); |
| 1687 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1688 | SourceLocation startLoc = S->getBeginLoc(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1689 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1690 | StringRef elementName; |
| 1691 | std::string elementTypeAsString; |
| 1692 | std::string buf; |
Fariborz Jahanian | b6933bc | 2012-11-06 23:25:49 +0000 | [diff] [blame] | 1693 | // line directive first. |
| 1694 | SourceLocation ForEachLoc = S->getForLoc(); |
| 1695 | ConvertSourceLocationToLineDirective(ForEachLoc, buf); |
| 1696 | buf += "{\n\t"; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1697 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { |
| 1698 | // type elem; |
| 1699 | NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl()); |
| 1700 | QualType ElementType = cast<ValueDecl>(D)->getType(); |
| 1701 | if (ElementType->isObjCQualifiedIdType() || |
| 1702 | ElementType->isObjCQualifiedInterfaceType()) |
| 1703 | // Simply use 'id' for all qualified types. |
| 1704 | elementTypeAsString = "id"; |
| 1705 | else |
| 1706 | elementTypeAsString = ElementType.getAsString(Context->getPrintingPolicy()); |
| 1707 | buf += elementTypeAsString; |
| 1708 | buf += " "; |
| 1709 | elementName = D->getName(); |
| 1710 | buf += elementName; |
| 1711 | buf += ";\n\t"; |
| 1712 | } |
| 1713 | else { |
| 1714 | DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); |
| 1715 | elementName = DR->getDecl()->getName(); |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 1716 | ValueDecl *VD = DR->getDecl(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1717 | if (VD->getType()->isObjCQualifiedIdType() || |
| 1718 | VD->getType()->isObjCQualifiedInterfaceType()) |
| 1719 | // Simply use 'id' for all qualified types. |
| 1720 | elementTypeAsString = "id"; |
| 1721 | else |
| 1722 | elementTypeAsString = VD->getType().getAsString(Context->getPrintingPolicy()); |
| 1723 | } |
| 1724 | |
| 1725 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1726 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
| 1727 | // id __rw_items[16]; |
| 1728 | buf += "id __rw_items[16];\n\t"; |
| 1729 | // id l_collection = (id) |
| 1730 | buf += "id l_collection = (id)"; |
| 1731 | // Find start location of 'collection' the hard way! |
| 1732 | const char *startCollectionBuf = startBuf; |
| 1733 | startCollectionBuf += 3; // skip 'for' |
| 1734 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1735 | startCollectionBuf++; // skip '(' |
| 1736 | // find 'in' and skip it. |
| 1737 | while (*startCollectionBuf != ' ' || |
| 1738 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1739 | (*(startCollectionBuf+3) != ' ' && |
| 1740 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1741 | startCollectionBuf++; |
| 1742 | startCollectionBuf += 3; |
| 1743 | |
| 1744 | // Replace: "for (type element in" with string constructed thus far. |
| 1745 | ReplaceText(startLoc, startCollectionBuf - startBuf, buf); |
| 1746 | // Replace ')' in for '(' type elem in collection ')' with ';' |
| 1747 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1748 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
| 1749 | SourceLocation lparenLoc = startLoc.getLocWithOffset(rparenBuf-startBuf); |
| 1750 | buf = ";\n\t"; |
| 1751 | |
| 1752 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1753 | // objects:__rw_items count:16]; |
| 1754 | // which is synthesized into: |
Fariborz Jahanian | 6c0af64 | 2013-09-05 17:17:32 +0000 | [diff] [blame] | 1755 | // NSUInteger limit = |
| 1756 | // ((NSUInteger (*) |
| 1757 | // (id, SEL, struct __objcFastEnumerationState *, id *, NSUInteger)) |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1758 | // (void *)objc_msgSend)((id)l_collection, |
| 1759 | // sel_registerName( |
| 1760 | // "countByEnumeratingWithState:objects:count:"), |
| 1761 | // (struct __objcFastEnumerationState *)&state, |
Fariborz Jahanian | 6c0af64 | 2013-09-05 17:17:32 +0000 | [diff] [blame] | 1762 | // (id *)__rw_items, (NSUInteger)16); |
| 1763 | buf += "_WIN_NSUInteger limit =\n\t\t"; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1764 | SynthCountByEnumWithState(buf); |
| 1765 | buf += ";\n\t"; |
| 1766 | /// if (limit) { |
| 1767 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1768 | /// do { |
| 1769 | /// unsigned long counter = 0; |
| 1770 | /// do { |
| 1771 | /// if (startMutations != *enumState.mutationsPtr) |
| 1772 | /// objc_enumerationMutation(l_collection); |
| 1773 | /// elem = (type)enumState.itemsPtr[counter++]; |
| 1774 | buf += "if (limit) {\n\t"; |
| 1775 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1776 | buf += "do {\n\t\t"; |
| 1777 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1778 | buf += "do {\n\t\t\t"; |
| 1779 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1780 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1781 | buf += elementName; |
| 1782 | buf += " = ("; |
| 1783 | buf += elementTypeAsString; |
| 1784 | buf += ")enumState.itemsPtr[counter++];"; |
| 1785 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
| 1786 | ReplaceText(lparenLoc, 1, buf); |
| 1787 | |
| 1788 | /// __continue_label: ; |
| 1789 | /// } while (counter < limit); |
Fariborz Jahanian | 6c0af64 | 2013-09-05 17:17:32 +0000 | [diff] [blame] | 1790 | /// } while ((limit = [l_collection countByEnumeratingWithState:&enumState |
| 1791 | /// objects:__rw_items count:16])); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1792 | /// elem = nil; |
| 1793 | /// __break_label: ; |
| 1794 | /// } |
| 1795 | /// else |
| 1796 | /// elem = nil; |
| 1797 | /// } |
| 1798 | /// |
| 1799 | buf = ";\n\t"; |
| 1800 | buf += "__continue_label_"; |
| 1801 | buf += utostr(ObjCBcLabelNo.back()); |
| 1802 | buf += ": ;"; |
| 1803 | buf += "\n\t\t"; |
| 1804 | buf += "} while (counter < limit);\n\t"; |
Fariborz Jahanian | 6c0af64 | 2013-09-05 17:17:32 +0000 | [diff] [blame] | 1805 | buf += "} while ((limit = "; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1806 | SynthCountByEnumWithState(buf); |
Fariborz Jahanian | 6c0af64 | 2013-09-05 17:17:32 +0000 | [diff] [blame] | 1807 | buf += "));\n\t"; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1808 | buf += elementName; |
| 1809 | buf += " = (("; |
| 1810 | buf += elementTypeAsString; |
| 1811 | buf += ")0);\n\t"; |
| 1812 | buf += "__break_label_"; |
| 1813 | buf += utostr(ObjCBcLabelNo.back()); |
| 1814 | buf += ": ;\n\t"; |
| 1815 | buf += "}\n\t"; |
| 1816 | buf += "else\n\t\t"; |
| 1817 | buf += elementName; |
| 1818 | buf += " = (("; |
| 1819 | buf += elementTypeAsString; |
| 1820 | buf += ")0);\n\t"; |
| 1821 | buf += "}\n"; |
| 1822 | |
| 1823 | // Insert all these *after* the statement body. |
| 1824 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
| 1825 | if (isa<CompoundStmt>(S->getBody())) { |
| 1826 | SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(1); |
| 1827 | InsertText(endBodyLoc, buf); |
| 1828 | } else { |
| 1829 | /* Need to treat single statements specially. For example: |
| 1830 | * |
| 1831 | * for (A *a in b) if (stuff()) break; |
| 1832 | * for (A *a in b) xxxyy; |
| 1833 | * |
| 1834 | * The following code simply scans ahead to the semi to find the actual end. |
| 1835 | */ |
| 1836 | const char *stmtBuf = SM->getCharacterData(OrigEnd); |
| 1837 | const char *semiBuf = strchr(stmtBuf, ';'); |
| 1838 | assert(semiBuf && "Can't find ';'"); |
| 1839 | SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(semiBuf-stmtBuf+1); |
| 1840 | InsertText(endBodyLoc, buf); |
| 1841 | } |
| 1842 | Stmts.pop_back(); |
| 1843 | ObjCBcLabelNo.pop_back(); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 1844 | return nullptr; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1845 | } |
| 1846 | |
Fariborz Jahanian | fe6268e | 2012-03-16 21:33:16 +0000 | [diff] [blame] | 1847 | static void Write_RethrowObject(std::string &buf) { |
| 1848 | buf += "{ struct _FIN { _FIN(id reth) : rethrow(reth) {}\n"; |
| 1849 | buf += "\t~_FIN() { if (rethrow) objc_exception_throw(rethrow); }\n"; |
| 1850 | buf += "\tid rethrow;\n"; |
| 1851 | buf += "\t} _fin_force_rethow(_rethrow);"; |
| 1852 | } |
| 1853 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1854 | /// RewriteObjCSynchronizedStmt - |
| 1855 | /// This routine rewrites @synchronized(expr) stmt; |
| 1856 | /// into: |
| 1857 | /// objc_sync_enter(expr); |
| 1858 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1859 | /// |
| 1860 | Stmt *RewriteModernObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
| 1861 | // Get the start location and compute the semi location. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1862 | SourceLocation startLoc = S->getBeginLoc(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1863 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1864 | |
| 1865 | assert((*startBuf == '@') && "bogus @synchronized location"); |
| 1866 | |
| 1867 | std::string buf; |
Fariborz Jahanian | e030a63 | 2012-11-07 00:43:05 +0000 | [diff] [blame] | 1868 | SourceLocation SynchLoc = S->getAtSynchronizedLoc(); |
| 1869 | ConvertSourceLocationToLineDirective(SynchLoc, buf); |
Fariborz Jahanian | ff0c460 | 2013-09-17 17:51:48 +0000 | [diff] [blame] | 1870 | buf += "{ id _rethrow = 0; id _sync_obj = (id)"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1871 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1872 | const char *lparenBuf = startBuf; |
| 1873 | while (*lparenBuf != '(') lparenBuf++; |
| 1874 | ReplaceText(startLoc, lparenBuf-startBuf+1, buf); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1875 | |
Fariborz Jahanian | e881076 | 2012-03-17 17:46:02 +0000 | [diff] [blame] | 1876 | buf = "; objc_sync_enter(_sync_obj);\n"; |
| 1877 | buf += "try {\n\tstruct _SYNC_EXIT { _SYNC_EXIT(id arg) : sync_exit(arg) {}"; |
| 1878 | buf += "\n\t~_SYNC_EXIT() {objc_sync_exit(sync_exit);}"; |
| 1879 | buf += "\n\tid sync_exit;"; |
| 1880 | buf += "\n\t} _sync_exit(_sync_obj);\n"; |
| 1881 | |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1882 | // We can't use S->getSynchExpr()->getEndLoc() to find the end location, since |
Fariborz Jahanian | e881076 | 2012-03-17 17:46:02 +0000 | [diff] [blame] | 1883 | // the sync expression is typically a message expression that's already |
| 1884 | // been rewritten! (which implies the SourceLocation's are invalid). |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1885 | SourceLocation RParenExprLoc = S->getSynchBody()->getBeginLoc(); |
Fariborz Jahanian | e881076 | 2012-03-17 17:46:02 +0000 | [diff] [blame] | 1886 | const char *RParenExprLocBuf = SM->getCharacterData(RParenExprLoc); |
| 1887 | while (*RParenExprLocBuf != ')') RParenExprLocBuf--; |
| 1888 | RParenExprLoc = startLoc.getLocWithOffset(RParenExprLocBuf-startBuf); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1889 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1890 | SourceLocation LBranceLoc = S->getSynchBody()->getBeginLoc(); |
Fariborz Jahanian | e881076 | 2012-03-17 17:46:02 +0000 | [diff] [blame] | 1891 | const char *LBraceLocBuf = SM->getCharacterData(LBranceLoc); |
| 1892 | assert (*LBraceLocBuf == '{'); |
| 1893 | ReplaceText(RParenExprLoc, (LBraceLocBuf - SM->getCharacterData(RParenExprLoc) + 1), buf); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1894 | |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1895 | SourceLocation startRBraceLoc = S->getSynchBody()->getEndLoc(); |
Matt Beaumont-Gay | 6e177d3 | 2012-03-16 22:20:39 +0000 | [diff] [blame] | 1896 | assert((*SM->getCharacterData(startRBraceLoc) == '}') && |
| 1897 | "bogus @synchronized block"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1898 | |
Fariborz Jahanian | fe6268e | 2012-03-16 21:33:16 +0000 | [diff] [blame] | 1899 | buf = "} catch (id e) {_rethrow = e;}\n"; |
| 1900 | Write_RethrowObject(buf); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1901 | buf += "}\n"; |
Fariborz Jahanian | fe6268e | 2012-03-16 21:33:16 +0000 | [diff] [blame] | 1902 | buf += "}\n"; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1903 | |
Fariborz Jahanian | 1d24a025 | 2012-03-16 21:43:45 +0000 | [diff] [blame] | 1904 | ReplaceText(startRBraceLoc, 1, buf); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1905 | |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 1906 | return nullptr; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1907 | } |
| 1908 | |
| 1909 | void RewriteModernObjC::WarnAboutReturnGotoStmts(Stmt *S) |
| 1910 | { |
| 1911 | // Perform a bottom up traversal of all children. |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 1912 | for (Stmt *SubStmt : S->children()) |
| 1913 | if (SubStmt) |
| 1914 | WarnAboutReturnGotoStmts(SubStmt); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1915 | |
| 1916 | if (isa<ReturnStmt>(S) || isa<GotoStmt>(S)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1917 | Diags.Report(Context->getFullLoc(S->getBeginLoc()), |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1918 | TryFinallyContainsReturnDiag); |
| 1919 | } |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
Fariborz Jahanian | 9b43c3f | 2012-05-23 23:47:20 +0000 | [diff] [blame] | 1922 | Stmt *RewriteModernObjC::RewriteObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S) { |
| 1923 | SourceLocation startLoc = S->getAtLoc(); |
| 1924 | ReplaceText(startLoc, strlen("@autoreleasepool"), "/* @autoreleasepool */"); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1925 | ReplaceText(S->getSubStmt()->getBeginLoc(), 1, |
Fariborz Jahanian | c37a1d6 | 2012-05-24 22:59:56 +0000 | [diff] [blame] | 1926 | "{ __AtAutoreleasePool __autoreleasepool; "); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 1927 | |
| 1928 | return nullptr; |
Fariborz Jahanian | 9b43c3f | 2012-05-23 23:47:20 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1931 | Stmt *RewriteModernObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Fariborz Jahanian | 3b71b17 | 2012-03-15 22:42:15 +0000 | [diff] [blame] | 1932 | ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt(); |
Fariborz Jahanian | b960db4 | 2012-03-15 23:50:33 +0000 | [diff] [blame] | 1933 | bool noCatch = S->getNumCatchStmts() == 0; |
Fariborz Jahanian | 3b71b17 | 2012-03-15 22:42:15 +0000 | [diff] [blame] | 1934 | std::string buf; |
Fariborz Jahanian | b6933bc | 2012-11-06 23:25:49 +0000 | [diff] [blame] | 1935 | SourceLocation TryLocation = S->getAtTryLoc(); |
| 1936 | ConvertSourceLocationToLineDirective(TryLocation, buf); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1937 | |
Fariborz Jahanian | 3b71b17 | 2012-03-15 22:42:15 +0000 | [diff] [blame] | 1938 | if (finalStmt) { |
Fariborz Jahanian | b960db4 | 2012-03-15 23:50:33 +0000 | [diff] [blame] | 1939 | if (noCatch) |
Fariborz Jahanian | b6933bc | 2012-11-06 23:25:49 +0000 | [diff] [blame] | 1940 | buf += "{ id volatile _rethrow = 0;\n"; |
Fariborz Jahanian | b960db4 | 2012-03-15 23:50:33 +0000 | [diff] [blame] | 1941 | else { |
Fariborz Jahanian | b6933bc | 2012-11-06 23:25:49 +0000 | [diff] [blame] | 1942 | buf += "{ id volatile _rethrow = 0;\ntry {\n"; |
Fariborz Jahanian | b960db4 | 2012-03-15 23:50:33 +0000 | [diff] [blame] | 1943 | } |
Fariborz Jahanian | 3b71b17 | 2012-03-15 22:42:15 +0000 | [diff] [blame] | 1944 | } |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1945 | // Get the start location and compute the semi location. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1946 | SourceLocation startLoc = S->getBeginLoc(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1947 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1948 | |
| 1949 | assert((*startBuf == '@') && "bogus @try location"); |
Fariborz Jahanian | 3b71b17 | 2012-03-15 22:42:15 +0000 | [diff] [blame] | 1950 | if (finalStmt) |
| 1951 | ReplaceText(startLoc, 1, buf); |
| 1952 | else |
| 1953 | // @try -> try |
| 1954 | ReplaceText(startLoc, 1, ""); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1955 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1956 | for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I) { |
| 1957 | ObjCAtCatchStmt *Catch = S->getCatchStmt(I); |
Fariborz Jahanian | f232bb4 | 2012-03-15 20:11:10 +0000 | [diff] [blame] | 1958 | VarDecl *catchDecl = Catch->getCatchParamDecl(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1959 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1960 | startLoc = Catch->getBeginLoc(); |
Fariborz Jahanian | f232bb4 | 2012-03-15 20:11:10 +0000 | [diff] [blame] | 1961 | bool AtRemoved = false; |
| 1962 | if (catchDecl) { |
| 1963 | QualType t = catchDecl->getType(); |
| 1964 | if (const ObjCObjectPointerType *Ptr = t->getAs<ObjCObjectPointerType>()) { |
| 1965 | // Should be a pointer to a class. |
| 1966 | ObjCInterfaceDecl *IDecl = Ptr->getObjectType()->getInterface(); |
| 1967 | if (IDecl) { |
| 1968 | std::string Result; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1969 | ConvertSourceLocationToLineDirective(Catch->getBeginLoc(), Result); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1970 | |
Fariborz Jahanian | f232bb4 | 2012-03-15 20:11:10 +0000 | [diff] [blame] | 1971 | startBuf = SM->getCharacterData(startLoc); |
| 1972 | assert((*startBuf == '@') && "bogus @catch location"); |
| 1973 | SourceLocation rParenLoc = Catch->getRParenLoc(); |
| 1974 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1975 | |
Fariborz Jahanian | f232bb4 | 2012-03-15 20:11:10 +0000 | [diff] [blame] | 1976 | // _objc_exc_Foo *_e as argument to catch. |
Fariborz Jahanian | b6933bc | 2012-11-06 23:25:49 +0000 | [diff] [blame] | 1977 | Result += "catch (_objc_exc_"; Result += IDecl->getNameAsString(); |
Fariborz Jahanian | f232bb4 | 2012-03-15 20:11:10 +0000 | [diff] [blame] | 1978 | Result += " *_"; Result += catchDecl->getNameAsString(); |
| 1979 | Result += ")"; |
| 1980 | ReplaceText(startLoc, rParenBuf-startBuf+1, Result); |
| 1981 | // Foo *e = (Foo *)_e; |
| 1982 | Result.clear(); |
| 1983 | Result = "{ "; |
| 1984 | Result += IDecl->getNameAsString(); |
| 1985 | Result += " *"; Result += catchDecl->getNameAsString(); |
| 1986 | Result += " = ("; Result += IDecl->getNameAsString(); Result += "*)"; |
| 1987 | Result += "_"; Result += catchDecl->getNameAsString(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1988 | |
Fariborz Jahanian | f232bb4 | 2012-03-15 20:11:10 +0000 | [diff] [blame] | 1989 | Result += "; "; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1990 | SourceLocation lBraceLoc = Catch->getCatchBody()->getBeginLoc(); |
Fariborz Jahanian | f232bb4 | 2012-03-15 20:11:10 +0000 | [diff] [blame] | 1991 | ReplaceText(lBraceLoc, 1, Result); |
| 1992 | AtRemoved = true; |
| 1993 | } |
| 1994 | } |
| 1995 | } |
| 1996 | if (!AtRemoved) |
| 1997 | // @catch -> catch |
| 1998 | ReplaceText(startLoc, 1, ""); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1999 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2000 | } |
Fariborz Jahanian | b960db4 | 2012-03-15 23:50:33 +0000 | [diff] [blame] | 2001 | if (finalStmt) { |
| 2002 | buf.clear(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2003 | SourceLocation FinallyLoc = finalStmt->getBeginLoc(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2004 | |
Fariborz Jahanian | b6933bc | 2012-11-06 23:25:49 +0000 | [diff] [blame] | 2005 | if (noCatch) { |
| 2006 | ConvertSourceLocationToLineDirective(FinallyLoc, buf); |
| 2007 | buf += "catch (id e) {_rethrow = e;}\n"; |
| 2008 | } |
| 2009 | else { |
| 2010 | buf += "}\n"; |
| 2011 | ConvertSourceLocationToLineDirective(FinallyLoc, buf); |
| 2012 | buf += "catch (id e) {_rethrow = e;}\n"; |
| 2013 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2014 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2015 | SourceLocation startFinalLoc = finalStmt->getBeginLoc(); |
Fariborz Jahanian | b960db4 | 2012-03-15 23:50:33 +0000 | [diff] [blame] | 2016 | ReplaceText(startFinalLoc, 8, buf); |
| 2017 | Stmt *body = finalStmt->getFinallyBody(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2018 | SourceLocation startFinalBodyLoc = body->getBeginLoc(); |
Fariborz Jahanian | b960db4 | 2012-03-15 23:50:33 +0000 | [diff] [blame] | 2019 | buf.clear(); |
Fariborz Jahanian | fe6268e | 2012-03-16 21:33:16 +0000 | [diff] [blame] | 2020 | Write_RethrowObject(buf); |
Fariborz Jahanian | b960db4 | 2012-03-15 23:50:33 +0000 | [diff] [blame] | 2021 | ReplaceText(startFinalBodyLoc, 1, buf); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2022 | |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2023 | SourceLocation endFinalBodyLoc = body->getEndLoc(); |
Fariborz Jahanian | b960db4 | 2012-03-15 23:50:33 +0000 | [diff] [blame] | 2024 | ReplaceText(endFinalBodyLoc, 1, "}\n}"); |
Fariborz Jahanian | e881076 | 2012-03-17 17:46:02 +0000 | [diff] [blame] | 2025 | // Now check for any return/continue/go statements within the @try. |
| 2026 | WarnAboutReturnGotoStmts(S->getTryBody()); |
Fariborz Jahanian | b960db4 | 2012-03-15 23:50:33 +0000 | [diff] [blame] | 2027 | } |
| 2028 | |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 2029 | return nullptr; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
| 2032 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
| 2033 | // the throw expression is typically a message expression that's already |
| 2034 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 2035 | Stmt *RewriteModernObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
| 2036 | // Get the start location and compute the semi location. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2037 | SourceLocation startLoc = S->getBeginLoc(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2038 | const char *startBuf = SM->getCharacterData(startLoc); |
| 2039 | |
| 2040 | assert((*startBuf == '@') && "bogus @throw location"); |
| 2041 | |
| 2042 | std::string buf; |
| 2043 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
| 2044 | if (S->getThrowExpr()) |
| 2045 | buf = "objc_exception_throw("; |
Fariborz Jahanian | 52fe6a0 | 2012-03-16 16:52:06 +0000 | [diff] [blame] | 2046 | else |
| 2047 | buf = "throw"; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2048 | |
| 2049 | // handle "@ throw" correctly. |
| 2050 | const char *wBuf = strchr(startBuf, 'w'); |
| 2051 | assert((*wBuf == 'w') && "@throw: can't find 'w'"); |
| 2052 | ReplaceText(startLoc, wBuf-startBuf+1, buf); |
| 2053 | |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2054 | SourceLocation endLoc = S->getEndLoc(); |
Fariborz Jahanian | b0fdab2 | 2013-02-11 19:30:33 +0000 | [diff] [blame] | 2055 | const char *endBuf = SM->getCharacterData(endLoc); |
| 2056 | const char *semiBuf = strchr(endBuf, ';'); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2057 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
| 2058 | SourceLocation semiLoc = startLoc.getLocWithOffset(semiBuf-startBuf); |
Fariborz Jahanian | 52fe6a0 | 2012-03-16 16:52:06 +0000 | [diff] [blame] | 2059 | if (S->getThrowExpr()) |
| 2060 | ReplaceText(semiLoc, 1, ");"); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 2061 | return nullptr; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
| 2064 | Stmt *RewriteModernObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
| 2065 | // Create a new string expression. |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2066 | std::string StrEncoding; |
| 2067 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); |
Benjamin Kramer | fc18842 | 2014-02-25 12:26:11 +0000 | [diff] [blame] | 2068 | Expr *Replacement = getStringLiteral(StrEncoding); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2069 | ReplaceStmt(Exp, Replacement); |
| 2070 | |
| 2071 | // Replace this subexpr in the parent. |
| 2072 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
| 2073 | return Replacement; |
| 2074 | } |
| 2075 | |
| 2076 | Stmt *RewriteModernObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
| 2077 | if (!SelGetUidFunctionDecl) |
| 2078 | SynthSelGetUidFunctionDecl(); |
| 2079 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 2080 | // Create a call to sel_registerName("selName"). |
| 2081 | SmallVector<Expr*, 8> SelExprs; |
Benjamin Kramer | fc18842 | 2014-02-25 12:26:11 +0000 | [diff] [blame] | 2082 | SelExprs.push_back(getStringLiteral(Exp->getSelector().getAsString())); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2083 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
Craig Topper | cf2126e | 2015-10-22 03:13:07 +0000 | [diff] [blame] | 2084 | SelExprs); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2085 | ReplaceStmt(Exp, SelExp); |
| 2086 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
| 2087 | return SelExp; |
| 2088 | } |
| 2089 | |
Craig Topper | cf2126e | 2015-10-22 03:13:07 +0000 | [diff] [blame] | 2090 | CallExpr * |
| 2091 | RewriteModernObjC::SynthesizeCallToFunctionDecl(FunctionDecl *FD, |
| 2092 | ArrayRef<Expr *> Args, |
| 2093 | SourceLocation StartLoc, |
| 2094 | SourceLocation EndLoc) { |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2095 | // Get the type, we will need to reference it in a couple spots. |
| 2096 | QualType msgSendType = FD->getType(); |
| 2097 | |
| 2098 | // Create a reference to the objc_msgSend() declaration. |
| 2099 | DeclRefExpr *DRE = |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2100 | new (Context) DeclRefExpr(FD, false, msgSendType, VK_LValue, SourceLocation()); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2101 | |
| 2102 | // Now, we cast the reference to a pointer to the objc_msgSend type. |
| 2103 | QualType pToFunc = Context->getPointerType(msgSendType); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2104 | ImplicitCastExpr *ICE = |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2105 | ImplicitCastExpr::Create(*Context, pToFunc, CK_FunctionToPointerDecay, |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 2106 | DRE, nullptr, VK_RValue); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2107 | |
| 2108 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
| 2109 | |
Craig Topper | cf2126e | 2015-10-22 03:13:07 +0000 | [diff] [blame] | 2110 | CallExpr *Exp = new (Context) CallExpr(*Context, ICE, Args, |
| 2111 | FT->getCallResultType(*Context), |
| 2112 | VK_RValue, EndLoc); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2113 | return Exp; |
| 2114 | } |
| 2115 | |
| 2116 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 2117 | const char *&startRef, const char *&endRef) { |
| 2118 | while (startBuf < endBuf) { |
| 2119 | if (*startBuf == '<') |
| 2120 | startRef = startBuf; // mark the start. |
| 2121 | if (*startBuf == '>') { |
| 2122 | if (startRef && *startRef == '<') { |
| 2123 | endRef = startBuf; // mark the end. |
| 2124 | return true; |
| 2125 | } |
| 2126 | return false; |
| 2127 | } |
| 2128 | startBuf++; |
| 2129 | } |
| 2130 | return false; |
| 2131 | } |
| 2132 | |
| 2133 | static void scanToNextArgument(const char *&argRef) { |
| 2134 | int angle = 0; |
| 2135 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 2136 | if (*argRef == '<') |
| 2137 | angle++; |
| 2138 | else if (*argRef == '>') |
| 2139 | angle--; |
| 2140 | argRef++; |
| 2141 | } |
| 2142 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 2143 | } |
| 2144 | |
| 2145 | bool RewriteModernObjC::needToScanForQualifiers(QualType T) { |
| 2146 | if (T->isObjCQualifiedIdType()) |
| 2147 | return true; |
| 2148 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 2149 | if (PT->getPointeeType()->isObjCQualifiedIdType()) |
| 2150 | return true; |
| 2151 | } |
| 2152 | if (T->isObjCObjectPointerType()) { |
| 2153 | T = T->getPointeeType(); |
| 2154 | return T->isObjCQualifiedInterfaceType(); |
| 2155 | } |
| 2156 | if (T->isArrayType()) { |
| 2157 | QualType ElemTy = Context->getBaseElementType(T); |
| 2158 | return needToScanForQualifiers(ElemTy); |
| 2159 | } |
| 2160 | return false; |
| 2161 | } |
| 2162 | |
| 2163 | void RewriteModernObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) { |
| 2164 | QualType Type = E->getType(); |
| 2165 | if (needToScanForQualifiers(Type)) { |
| 2166 | SourceLocation Loc, EndLoc; |
| 2167 | |
| 2168 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) { |
| 2169 | Loc = ECE->getLParenLoc(); |
| 2170 | EndLoc = ECE->getRParenLoc(); |
| 2171 | } else { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2172 | Loc = E->getBeginLoc(); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2173 | EndLoc = E->getEndLoc(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2174 | } |
| 2175 | // This will defend against trying to rewrite synthesized expressions. |
| 2176 | if (Loc.isInvalid() || EndLoc.isInvalid()) |
| 2177 | return; |
| 2178 | |
| 2179 | const char *startBuf = SM->getCharacterData(Loc); |
| 2180 | const char *endBuf = SM->getCharacterData(EndLoc); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 2181 | const char *startRef = nullptr, *endRef = nullptr; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2182 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2183 | // Get the locations of the startRef, endRef. |
| 2184 | SourceLocation LessLoc = Loc.getLocWithOffset(startRef-startBuf); |
| 2185 | SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-startBuf+1); |
| 2186 | // Comment out the protocol references. |
| 2187 | InsertText(LessLoc, "/*"); |
| 2188 | InsertText(GreaterLoc, "*/"); |
| 2189 | } |
| 2190 | } |
| 2191 | } |
| 2192 | |
| 2193 | void RewriteModernObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
| 2194 | SourceLocation Loc; |
| 2195 | QualType Type; |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 2196 | const FunctionProtoType *proto = nullptr; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2197 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 2198 | Loc = VD->getLocation(); |
| 2199 | Type = VD->getType(); |
| 2200 | } |
| 2201 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 2202 | Loc = FD->getLocation(); |
| 2203 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 2204 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
| 2205 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
| 2206 | assert(funcType && "missing function type"); |
| 2207 | proto = dyn_cast<FunctionProtoType>(funcType); |
| 2208 | if (!proto) |
| 2209 | return; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2210 | Type = proto->getReturnType(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2211 | } |
| 2212 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(Dcl)) { |
| 2213 | Loc = FD->getLocation(); |
| 2214 | Type = FD->getType(); |
| 2215 | } |
Fariborz Jahanian | 3a65ce3 | 2013-04-03 19:11:21 +0000 | [diff] [blame] | 2216 | else if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(Dcl)) { |
| 2217 | Loc = TD->getLocation(); |
| 2218 | Type = TD->getUnderlyingType(); |
| 2219 | } |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2220 | else |
| 2221 | return; |
| 2222 | |
| 2223 | if (needToScanForQualifiers(Type)) { |
| 2224 | // Since types are unique, we need to scan the buffer. |
| 2225 | |
| 2226 | const char *endBuf = SM->getCharacterData(Loc); |
| 2227 | const char *startBuf = endBuf; |
| 2228 | while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) |
| 2229 | startBuf--; // scan backward (from the decl location) for return type. |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 2230 | const char *startRef = nullptr, *endRef = nullptr; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2231 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2232 | // Get the locations of the startRef, endRef. |
| 2233 | SourceLocation LessLoc = Loc.getLocWithOffset(startRef-endBuf); |
| 2234 | SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-endBuf+1); |
| 2235 | // Comment out the protocol references. |
| 2236 | InsertText(LessLoc, "/*"); |
| 2237 | InsertText(GreaterLoc, "*/"); |
| 2238 | } |
| 2239 | } |
| 2240 | if (!proto) |
| 2241 | return; // most likely, was a variable |
| 2242 | // Now check arguments. |
| 2243 | const char *startBuf = SM->getCharacterData(Loc); |
| 2244 | const char *startFuncBuf = startBuf; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2245 | for (unsigned i = 0; i < proto->getNumParams(); i++) { |
| 2246 | if (needToScanForQualifiers(proto->getParamType(i))) { |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2247 | // Since types are unique, we need to scan the buffer. |
| 2248 | |
| 2249 | const char *endBuf = startBuf; |
| 2250 | // scan forward (from the decl location) for argument types. |
| 2251 | scanToNextArgument(endBuf); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 2252 | const char *startRef = nullptr, *endRef = nullptr; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2253 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2254 | // Get the locations of the startRef, endRef. |
| 2255 | SourceLocation LessLoc = |
| 2256 | Loc.getLocWithOffset(startRef-startFuncBuf); |
| 2257 | SourceLocation GreaterLoc = |
| 2258 | Loc.getLocWithOffset(endRef-startFuncBuf+1); |
| 2259 | // Comment out the protocol references. |
| 2260 | InsertText(LessLoc, "/*"); |
| 2261 | InsertText(GreaterLoc, "*/"); |
| 2262 | } |
| 2263 | startBuf = ++endBuf; |
| 2264 | } |
| 2265 | else { |
| 2266 | // If the function name is derived from a macro expansion, then the |
| 2267 | // argument buffer will not follow the name. Need to speak with Chris. |
| 2268 | while (*startBuf && *startBuf != ')' && *startBuf != ',') |
| 2269 | startBuf++; // scan forward (from the decl location) for argument types. |
| 2270 | startBuf++; |
| 2271 | } |
| 2272 | } |
| 2273 | } |
| 2274 | |
| 2275 | void RewriteModernObjC::RewriteTypeOfDecl(VarDecl *ND) { |
| 2276 | QualType QT = ND->getType(); |
| 2277 | const Type* TypePtr = QT->getAs<Type>(); |
| 2278 | if (!isa<TypeOfExprType>(TypePtr)) |
| 2279 | return; |
| 2280 | while (isa<TypeOfExprType>(TypePtr)) { |
| 2281 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 2282 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 2283 | TypePtr = QT->getAs<Type>(); |
| 2284 | } |
| 2285 | // FIXME. This will not work for multiple declarators; as in: |
| 2286 | // __typeof__(a) b,c,d; |
| 2287 | std::string TypeAsString(QT.getAsString(Context->getPrintingPolicy())); |
| 2288 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
| 2289 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 2290 | if (ND->getInit()) { |
| 2291 | std::string Name(ND->getNameAsString()); |
| 2292 | TypeAsString += " " + Name + " = "; |
| 2293 | Expr *E = ND->getInit(); |
| 2294 | SourceLocation startLoc; |
| 2295 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 2296 | startLoc = ECE->getLParenLoc(); |
| 2297 | else |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2298 | startLoc = E->getBeginLoc(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2299 | startLoc = SM->getExpansionLoc(startLoc); |
| 2300 | const char *endBuf = SM->getCharacterData(startLoc); |
| 2301 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
| 2302 | } |
| 2303 | else { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2304 | SourceLocation X = ND->getEndLoc(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2305 | X = SM->getExpansionLoc(X); |
| 2306 | const char *endBuf = SM->getCharacterData(X); |
| 2307 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
| 2308 | } |
| 2309 | } |
| 2310 | |
| 2311 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
| 2312 | void RewriteModernObjC::SynthSelGetUidFunctionDecl() { |
| 2313 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
| 2314 | SmallVector<QualType, 16> ArgTys; |
| 2315 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
| 2316 | QualType getFuncType = |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2317 | getSimpleFunctionType(Context->getObjCSelType(), ArgTys); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2318 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2319 | SourceLocation(), |
| 2320 | SourceLocation(), |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 2321 | SelGetUidIdent, getFuncType, |
| 2322 | nullptr, SC_Extern); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2323 | } |
| 2324 | |
| 2325 | void RewriteModernObjC::RewriteFunctionDecl(FunctionDecl *FD) { |
| 2326 | // declared in <objc/objc.h> |
| 2327 | if (FD->getIdentifier() && |
| 2328 | FD->getName() == "sel_registerName") { |
| 2329 | SelGetUidFunctionDecl = FD; |
| 2330 | return; |
| 2331 | } |
| 2332 | RewriteObjCQualifiedInterfaceTypes(FD); |
| 2333 | } |
| 2334 | |
| 2335 | void RewriteModernObjC::RewriteBlockPointerType(std::string& Str, QualType Type) { |
| 2336 | std::string TypeString(Type.getAsString(Context->getPrintingPolicy())); |
| 2337 | const char *argPtr = TypeString.c_str(); |
| 2338 | if (!strchr(argPtr, '^')) { |
| 2339 | Str += TypeString; |
| 2340 | return; |
| 2341 | } |
| 2342 | while (*argPtr) { |
| 2343 | Str += (*argPtr == '^' ? '*' : *argPtr); |
| 2344 | argPtr++; |
| 2345 | } |
| 2346 | } |
| 2347 | |
| 2348 | // FIXME. Consolidate this routine with RewriteBlockPointerType. |
| 2349 | void RewriteModernObjC::RewriteBlockPointerTypeVariable(std::string& Str, |
| 2350 | ValueDecl *VD) { |
| 2351 | QualType Type = VD->getType(); |
| 2352 | std::string TypeString(Type.getAsString(Context->getPrintingPolicy())); |
| 2353 | const char *argPtr = TypeString.c_str(); |
| 2354 | int paren = 0; |
| 2355 | while (*argPtr) { |
| 2356 | switch (*argPtr) { |
| 2357 | case '(': |
| 2358 | Str += *argPtr; |
| 2359 | paren++; |
| 2360 | break; |
| 2361 | case ')': |
| 2362 | Str += *argPtr; |
| 2363 | paren--; |
| 2364 | break; |
| 2365 | case '^': |
| 2366 | Str += '*'; |
| 2367 | if (paren == 1) |
| 2368 | Str += VD->getNameAsString(); |
| 2369 | break; |
| 2370 | default: |
| 2371 | Str += *argPtr; |
| 2372 | break; |
| 2373 | } |
| 2374 | argPtr++; |
| 2375 | } |
| 2376 | } |
| 2377 | |
Fariborz Jahanian | ca357d9 | 2012-04-19 00:50:01 +0000 | [diff] [blame] | 2378 | void RewriteModernObjC::RewriteBlockLiteralFunctionDecl(FunctionDecl *FD) { |
| 2379 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
| 2380 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
| 2381 | const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(funcType); |
| 2382 | if (!proto) |
| 2383 | return; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2384 | QualType Type = proto->getReturnType(); |
Fariborz Jahanian | ca357d9 | 2012-04-19 00:50:01 +0000 | [diff] [blame] | 2385 | std::string FdStr = Type.getAsString(Context->getPrintingPolicy()); |
| 2386 | FdStr += " "; |
| 2387 | FdStr += FD->getName(); |
| 2388 | FdStr += "("; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2389 | unsigned numArgs = proto->getNumParams(); |
Fariborz Jahanian | ca357d9 | 2012-04-19 00:50:01 +0000 | [diff] [blame] | 2390 | for (unsigned i = 0; i < numArgs; i++) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2391 | QualType ArgType = proto->getParamType(i); |
Fariborz Jahanian | ca357d9 | 2012-04-19 00:50:01 +0000 | [diff] [blame] | 2392 | RewriteBlockPointerType(FdStr, ArgType); |
| 2393 | if (i+1 < numArgs) |
| 2394 | FdStr += ", "; |
| 2395 | } |
Fariborz Jahanian | df0577d | 2012-04-19 16:30:28 +0000 | [diff] [blame] | 2396 | if (FD->isVariadic()) { |
| 2397 | FdStr += (numArgs > 0) ? ", ...);\n" : "...);\n"; |
| 2398 | } |
| 2399 | else |
| 2400 | FdStr += ");\n"; |
Fariborz Jahanian | ca357d9 | 2012-04-19 00:50:01 +0000 | [diff] [blame] | 2401 | InsertText(FunLocStart, FdStr); |
| 2402 | } |
| 2403 | |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 2404 | // SynthSuperConstructorFunctionDecl - id __rw_objc_super(id obj, id super); |
| 2405 | void RewriteModernObjC::SynthSuperConstructorFunctionDecl() { |
| 2406 | if (SuperConstructorFunctionDecl) |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2407 | return; |
| 2408 | IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super"); |
| 2409 | SmallVector<QualType, 16> ArgTys; |
| 2410 | QualType argT = Context->getObjCIdType(); |
| 2411 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2412 | ArgTys.push_back(argT); |
| 2413 | ArgTys.push_back(argT); |
| 2414 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2415 | ArgTys); |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 2416 | SuperConstructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2417 | SourceLocation(), |
| 2418 | SourceLocation(), |
| 2419 | msgSendIdent, msgSendType, |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 2420 | nullptr, SC_Extern); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2421 | } |
| 2422 | |
| 2423 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
| 2424 | void RewriteModernObjC::SynthMsgSendFunctionDecl() { |
| 2425 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
| 2426 | SmallVector<QualType, 16> ArgTys; |
| 2427 | QualType argT = Context->getObjCIdType(); |
| 2428 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2429 | ArgTys.push_back(argT); |
| 2430 | argT = Context->getObjCSelType(); |
| 2431 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2432 | ArgTys.push_back(argT); |
| 2433 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2434 | ArgTys, /*isVariadic=*/true); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2435 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2436 | SourceLocation(), |
| 2437 | SourceLocation(), |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 2438 | msgSendIdent, msgSendType, nullptr, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2439 | SC_Extern); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2440 | } |
| 2441 | |
Fariborz Jahanian | 4af0e9e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 2442 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(void); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2443 | void RewriteModernObjC::SynthMsgSendSuperFunctionDecl() { |
| 2444 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
Fariborz Jahanian | 4af0e9e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 2445 | SmallVector<QualType, 2> ArgTys; |
| 2446 | ArgTys.push_back(Context->VoidTy); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2447 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2448 | ArgTys, /*isVariadic=*/true); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2449 | MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2450 | SourceLocation(), |
| 2451 | SourceLocation(), |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 2452 | msgSendIdent, msgSendType, |
| 2453 | nullptr, SC_Extern); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2454 | } |
| 2455 | |
| 2456 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
| 2457 | void RewriteModernObjC::SynthMsgSendStretFunctionDecl() { |
| 2458 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
| 2459 | SmallVector<QualType, 16> ArgTys; |
| 2460 | QualType argT = Context->getObjCIdType(); |
| 2461 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2462 | ArgTys.push_back(argT); |
| 2463 | argT = Context->getObjCSelType(); |
| 2464 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2465 | ArgTys.push_back(argT); |
| 2466 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2467 | ArgTys, /*isVariadic=*/true); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2468 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2469 | SourceLocation(), |
| 2470 | SourceLocation(), |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 2471 | msgSendIdent, msgSendType, |
| 2472 | nullptr, SC_Extern); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2473 | } |
| 2474 | |
| 2475 | // SynthMsgSendSuperStretFunctionDecl - |
Fariborz Jahanian | 4af0e9e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 2476 | // id objc_msgSendSuper_stret(void); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2477 | void RewriteModernObjC::SynthMsgSendSuperStretFunctionDecl() { |
| 2478 | IdentifierInfo *msgSendIdent = |
| 2479 | &Context->Idents.get("objc_msgSendSuper_stret"); |
Fariborz Jahanian | 4af0e9e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 2480 | SmallVector<QualType, 2> ArgTys; |
| 2481 | ArgTys.push_back(Context->VoidTy); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2482 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2483 | ArgTys, /*isVariadic=*/true); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2484 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
| 2485 | SourceLocation(), |
| 2486 | SourceLocation(), |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2487 | msgSendIdent, |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 2488 | msgSendType, nullptr, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2489 | SC_Extern); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2490 | } |
| 2491 | |
| 2492 | // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); |
| 2493 | void RewriteModernObjC::SynthMsgSendFpretFunctionDecl() { |
| 2494 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
| 2495 | SmallVector<QualType, 16> ArgTys; |
| 2496 | QualType argT = Context->getObjCIdType(); |
| 2497 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2498 | ArgTys.push_back(argT); |
| 2499 | argT = Context->getObjCSelType(); |
| 2500 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2501 | ArgTys.push_back(argT); |
| 2502 | QualType msgSendType = getSimpleFunctionType(Context->DoubleTy, |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2503 | ArgTys, /*isVariadic=*/true); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2504 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2505 | SourceLocation(), |
| 2506 | SourceLocation(), |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 2507 | msgSendIdent, msgSendType, |
| 2508 | nullptr, SC_Extern); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2509 | } |
| 2510 | |
Fariborz Jahanian | 9c0c050 | 2012-05-08 20:55:55 +0000 | [diff] [blame] | 2511 | // SynthGetClassFunctionDecl - Class objc_getClass(const char *name); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2512 | void RewriteModernObjC::SynthGetClassFunctionDecl() { |
| 2513 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
| 2514 | SmallVector<QualType, 16> ArgTys; |
| 2515 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
Fariborz Jahanian | 9c0c050 | 2012-05-08 20:55:55 +0000 | [diff] [blame] | 2516 | QualType getClassType = getSimpleFunctionType(Context->getObjCClassType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2517 | ArgTys); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2518 | GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2519 | SourceLocation(), |
| 2520 | SourceLocation(), |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 2521 | getClassIdent, getClassType, |
| 2522 | nullptr, SC_Extern); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2523 | } |
| 2524 | |
| 2525 | // SynthGetSuperClassFunctionDecl - Class class_getSuperclass(Class cls); |
| 2526 | void RewriteModernObjC::SynthGetSuperClassFunctionDecl() { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2527 | IdentifierInfo *getSuperClassIdent = |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2528 | &Context->Idents.get("class_getSuperclass"); |
| 2529 | SmallVector<QualType, 16> ArgTys; |
| 2530 | ArgTys.push_back(Context->getObjCClassType()); |
| 2531 | QualType getClassType = getSimpleFunctionType(Context->getObjCClassType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2532 | ArgTys); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2533 | GetSuperClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
| 2534 | SourceLocation(), |
| 2535 | SourceLocation(), |
| 2536 | getSuperClassIdent, |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 2537 | getClassType, nullptr, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2538 | SC_Extern); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2539 | } |
| 2540 | |
Fariborz Jahanian | 9c0c050 | 2012-05-08 20:55:55 +0000 | [diff] [blame] | 2541 | // SynthGetMetaClassFunctionDecl - Class objc_getMetaClass(const char *name); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2542 | void RewriteModernObjC::SynthGetMetaClassFunctionDecl() { |
| 2543 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
| 2544 | SmallVector<QualType, 16> ArgTys; |
| 2545 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
Fariborz Jahanian | 9c0c050 | 2012-05-08 20:55:55 +0000 | [diff] [blame] | 2546 | QualType getClassType = getSimpleFunctionType(Context->getObjCClassType(), |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2547 | ArgTys); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2548 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | ac00fbc | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2549 | SourceLocation(), |
| 2550 | SourceLocation(), |
| 2551 | getClassIdent, getClassType, |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 2552 | nullptr, SC_Extern); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2553 | } |
| 2554 | |
| 2555 | Stmt *RewriteModernObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
Richard Trieu | ddd01ce | 2014-06-09 22:53:25 +0000 | [diff] [blame] | 2556 | assert (Exp != nullptr && "Expected non-null ObjCStringLiteral"); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2557 | QualType strType = getConstantStringStructType(); |
| 2558 | |
| 2559 | std::string S = "__NSConstantStringImpl_"; |
| 2560 | |
| 2561 | std::string tmpName = InFileName; |
| 2562 | unsigned i; |
| 2563 | for (i=0; i < tmpName.length(); i++) { |
| 2564 | char c = tmpName.at(i); |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 2565 | // replace any non-alphanumeric characters with '_'. |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 2566 | if (!isAlphanumeric(c)) |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2567 | tmpName[i] = '_'; |
| 2568 | } |
| 2569 | S += tmpName; |
| 2570 | S += "_"; |
| 2571 | S += utostr(NumObjCStringLiterals++); |
| 2572 | |
| 2573 | Preamble += "static __NSConstantStringImpl " + S; |
| 2574 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 2575 | Preamble += "0x000007c8,"; // utf8_str |
| 2576 | // The pretty printer for StringLiteral handles escape characters properly. |
| 2577 | std::string prettyBufS; |
| 2578 | llvm::raw_string_ostream prettyBuf(prettyBufS); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 2579 | Exp->getString()->printPretty(prettyBuf, nullptr, PrintingPolicy(LangOpts)); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2580 | Preamble += prettyBuf.str(); |
| 2581 | Preamble += ","; |
| 2582 | Preamble += utostr(Exp->getString()->getByteLength()) + "};\n"; |
| 2583 | |
| 2584 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
| 2585 | SourceLocation(), &Context->Idents.get(S), |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 2586 | strType, nullptr, SC_Static); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2587 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, false, strType, VK_LValue, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2588 | SourceLocation()); |
| 2589 | Expr *Unop = new (Context) UnaryOperator(DRE, UO_AddrOf, |
| 2590 | Context->getPointerType(DRE->getType()), |
| 2591 | VK_RValue, OK_Ordinary, |
Aaron Ballman | a503855 | 2018-01-09 13:07:03 +0000 | [diff] [blame] | 2592 | SourceLocation(), false); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2593 | // cast to NSConstantString * |
| 2594 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Exp->getType(), |
| 2595 | CK_CPointerToObjCPointerCast, Unop); |
| 2596 | ReplaceStmt(Exp, cast); |
| 2597 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
| 2598 | return cast; |
| 2599 | } |
| 2600 | |
Fariborz Jahanian | 307b7ad | 2012-03-27 20:17:30 +0000 | [diff] [blame] | 2601 | Stmt *RewriteModernObjC::RewriteObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Exp) { |
| 2602 | unsigned IntSize = |
| 2603 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2604 | |
| 2605 | Expr *FlagExp = IntegerLiteral::Create(*Context, |
| 2606 | llvm::APInt(IntSize, Exp->getValue()), |
Fariborz Jahanian | 307b7ad | 2012-03-27 20:17:30 +0000 | [diff] [blame] | 2607 | Context->IntTy, Exp->getLocation()); |
| 2608 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Context->ObjCBuiltinBoolTy, |
| 2609 | CK_BitCast, FlagExp); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2610 | ParenExpr *PE = new (Context) ParenExpr(Exp->getLocation(), Exp->getExprLoc(), |
Fariborz Jahanian | 307b7ad | 2012-03-27 20:17:30 +0000 | [diff] [blame] | 2611 | cast); |
| 2612 | ReplaceStmt(Exp, PE); |
| 2613 | return PE; |
| 2614 | } |
| 2615 | |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2616 | Stmt *RewriteModernObjC::RewriteObjCBoxedExpr(ObjCBoxedExpr *Exp) { |
Fariborz Jahanian | 9c967fe | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2617 | // synthesize declaration of helper functions needed in this routine. |
| 2618 | if (!SelGetUidFunctionDecl) |
| 2619 | SynthSelGetUidFunctionDecl(); |
| 2620 | // use objc_msgSend() for all. |
| 2621 | if (!MsgSendFunctionDecl) |
| 2622 | SynthMsgSendFunctionDecl(); |
| 2623 | if (!GetClassFunctionDecl) |
| 2624 | SynthGetClassFunctionDecl(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2625 | |
Fariborz Jahanian | 9c967fe | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2626 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2627 | SourceLocation StartLoc = Exp->getBeginLoc(); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2628 | SourceLocation EndLoc = Exp->getEndLoc(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2629 | |
Fariborz Jahanian | 9c967fe | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2630 | // Synthesize a call to objc_msgSend(). |
| 2631 | SmallVector<Expr*, 4> MsgExprs; |
| 2632 | SmallVector<Expr*, 4> ClsExprs; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2633 | |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2634 | // Create a call to objc_getClass("<BoxingClass>"). It will be the 1st argument. |
| 2635 | ObjCMethodDecl *BoxingMethod = Exp->getBoxingMethod(); |
| 2636 | ObjCInterfaceDecl *BoxingClass = BoxingMethod->getClassInterface(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2637 | |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2638 | IdentifierInfo *clsName = BoxingClass->getIdentifier(); |
Benjamin Kramer | fc18842 | 2014-02-25 12:26:11 +0000 | [diff] [blame] | 2639 | ClsExprs.push_back(getStringLiteral(clsName->getName())); |
Craig Topper | cf2126e | 2015-10-22 03:13:07 +0000 | [diff] [blame] | 2640 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, ClsExprs, |
Fariborz Jahanian | 9c967fe | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2641 | StartLoc, EndLoc); |
| 2642 | MsgExprs.push_back(Cls); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2643 | |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2644 | // Create a call to sel_registerName("<BoxingMethod>:"), etc. |
Fariborz Jahanian | 9c967fe | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2645 | // it will be the 2nd argument. |
| 2646 | SmallVector<Expr*, 4> SelExprs; |
Benjamin Kramer | fc18842 | 2014-02-25 12:26:11 +0000 | [diff] [blame] | 2647 | SelExprs.push_back( |
| 2648 | getStringLiteral(BoxingMethod->getSelector().getAsString())); |
Fariborz Jahanian | 9c967fe | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2649 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
Craig Topper | cf2126e | 2015-10-22 03:13:07 +0000 | [diff] [blame] | 2650 | SelExprs, StartLoc, EndLoc); |
Fariborz Jahanian | 9c967fe | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2651 | MsgExprs.push_back(SelExp); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2652 | |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2653 | // User provided sub-expression is the 3rd, and last, argument. |
| 2654 | Expr *subExpr = Exp->getSubExpr(); |
| 2655 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(subExpr)) { |
Fariborz Jahanian | 9c967fe | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2656 | QualType type = ICE->getType(); |
| 2657 | const Expr *SubExpr = ICE->IgnoreParenImpCasts(); |
| 2658 | CastKind CK = CK_BitCast; |
| 2659 | if (SubExpr->getType()->isIntegralType(*Context) && type->isBooleanType()) |
| 2660 | CK = CK_IntegralToBoolean; |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2661 | subExpr = NoTypeInfoCStyleCastExpr(Context, type, CK, subExpr); |
Fariborz Jahanian | 9c967fe | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2662 | } |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2663 | MsgExprs.push_back(subExpr); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2664 | |
Fariborz Jahanian | 9c967fe | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2665 | SmallVector<QualType, 4> ArgTypes; |
Fariborz Jahanian | f8e68e2 | 2015-04-09 18:36:50 +0000 | [diff] [blame] | 2666 | ArgTypes.push_back(Context->getObjCClassType()); |
Fariborz Jahanian | 9c967fe | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2667 | ArgTypes.push_back(Context->getObjCSelType()); |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 2668 | for (const auto PI : BoxingMethod->parameters()) |
| 2669 | ArgTypes.push_back(PI->getType()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2670 | |
Fariborz Jahanian | 9c967fe | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2671 | QualType returnType = Exp->getType(); |
| 2672 | // Get the type, we will need to reference it in a couple spots. |
| 2673 | QualType msgSendType = MsgSendFlavor->getType(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2674 | |
Fariborz Jahanian | 9c967fe | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2675 | // Create a reference to the objc_msgSend() declaration. |
| 2676 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, false, msgSendType, |
| 2677 | VK_LValue, SourceLocation()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2678 | |
Fariborz Jahanian | 9c967fe | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2679 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2680 | Context->getPointerType(Context->VoidTy), |
| 2681 | CK_BitCast, DRE); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2682 | |
Fariborz Jahanian | 9c967fe | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2683 | // Now do the "normal" pointer to function cast. |
| 2684 | QualType castType = |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2685 | getSimpleFunctionType(returnType, ArgTypes, BoxingMethod->isVariadic()); |
Fariborz Jahanian | 9c967fe | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2686 | castType = Context->getPointerType(castType); |
| 2687 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
| 2688 | cast); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2689 | |
Fariborz Jahanian | 9c967fe | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2690 | // Don't forget the parens to enforce the proper binding. |
| 2691 | ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2692 | |
Fariborz Jahanian | 9c967fe | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2693 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2694 | CallExpr *CE = new (Context) |
| 2695 | CallExpr(*Context, PE, MsgExprs, FT->getReturnType(), VK_RValue, EndLoc); |
Fariborz Jahanian | 9c967fe | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2696 | ReplaceStmt(Exp, CE); |
| 2697 | return CE; |
| 2698 | } |
| 2699 | |
Fariborz Jahanian | 991a08d | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2700 | Stmt *RewriteModernObjC::RewriteObjCArrayLiteralExpr(ObjCArrayLiteral *Exp) { |
| 2701 | // synthesize declaration of helper functions needed in this routine. |
| 2702 | if (!SelGetUidFunctionDecl) |
| 2703 | SynthSelGetUidFunctionDecl(); |
| 2704 | // use objc_msgSend() for all. |
| 2705 | if (!MsgSendFunctionDecl) |
| 2706 | SynthMsgSendFunctionDecl(); |
| 2707 | if (!GetClassFunctionDecl) |
| 2708 | SynthGetClassFunctionDecl(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2709 | |
Fariborz Jahanian | 991a08d | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2710 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2711 | SourceLocation StartLoc = Exp->getBeginLoc(); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2712 | SourceLocation EndLoc = Exp->getEndLoc(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2713 | |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2714 | // Build the expression: __NSContainer_literal(int, ...).arr |
Fariborz Jahanian | e110fe4 | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 2715 | QualType IntQT = Context->IntTy; |
| 2716 | QualType NSArrayFType = |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2717 | getSimpleFunctionType(Context->VoidTy, IntQT, true); |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2718 | std::string NSArrayFName("__NSContainer_literal"); |
Fariborz Jahanian | e110fe4 | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 2719 | FunctionDecl *NSArrayFD = SynthBlockInitFunctionDecl(NSArrayFName); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2720 | DeclRefExpr *NSArrayDRE = |
Fariborz Jahanian | e110fe4 | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 2721 | new (Context) DeclRefExpr(NSArrayFD, false, NSArrayFType, VK_RValue, |
| 2722 | SourceLocation()); |
| 2723 | |
| 2724 | SmallVector<Expr*, 16> InitExprs; |
| 2725 | unsigned NumElements = Exp->getNumElements(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2726 | unsigned UnsignedIntSize = |
Fariborz Jahanian | e110fe4 | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 2727 | static_cast<unsigned>(Context->getTypeSize(Context->UnsignedIntTy)); |
| 2728 | Expr *count = IntegerLiteral::Create(*Context, |
| 2729 | llvm::APInt(UnsignedIntSize, NumElements), |
| 2730 | Context->UnsignedIntTy, SourceLocation()); |
| 2731 | InitExprs.push_back(count); |
| 2732 | for (unsigned i = 0; i < NumElements; i++) |
| 2733 | InitExprs.push_back(Exp->getElement(i)); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2734 | Expr *NSArrayCallExpr = |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2735 | new (Context) CallExpr(*Context, NSArrayDRE, InitExprs, |
Fariborz Jahanian | e110fe4 | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 2736 | NSArrayFType, VK_LValue, SourceLocation()); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 2737 | |
| 2738 | FieldDecl *ARRFD = FieldDecl::Create(*Context, nullptr, SourceLocation(), |
Fariborz Jahanian | e110fe4 | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 2739 | SourceLocation(), |
| 2740 | &Context->Idents.get("arr"), |
Fariborz Jahanian | f8e68e2 | 2015-04-09 18:36:50 +0000 | [diff] [blame] | 2741 | Context->getPointerType(Context->VoidPtrTy), |
| 2742 | nullptr, /*BitWidth=*/nullptr, |
| 2743 | /*Mutable=*/true, ICIS_NoInit); |
| 2744 | MemberExpr *ArrayLiteralME = new (Context) |
| 2745 | MemberExpr(NSArrayCallExpr, false, SourceLocation(), ARRFD, |
| 2746 | SourceLocation(), ARRFD->getType(), VK_LValue, OK_Ordinary); |
| 2747 | QualType ConstIdT = Context->getObjCIdType().withConst(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2748 | CStyleCastExpr * ArrayLiteralObjects = |
| 2749 | NoTypeInfoCStyleCastExpr(Context, |
Fariborz Jahanian | e110fe4 | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 2750 | Context->getPointerType(ConstIdT), |
| 2751 | CK_BitCast, |
| 2752 | ArrayLiteralME); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2753 | |
Fariborz Jahanian | 991a08d | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2754 | // Synthesize a call to objc_msgSend(). |
| 2755 | SmallVector<Expr*, 32> MsgExprs; |
| 2756 | SmallVector<Expr*, 4> ClsExprs; |
Fariborz Jahanian | 991a08d | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2757 | QualType expType = Exp->getType(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2758 | |
Fariborz Jahanian | 991a08d | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2759 | // Create a call to objc_getClass("NSArray"). It will be th 1st argument. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2760 | ObjCInterfaceDecl *Class = |
Fariborz Jahanian | 991a08d | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2761 | expType->getPointeeType()->getAs<ObjCObjectType>()->getInterface(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2762 | |
Fariborz Jahanian | 991a08d | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2763 | IdentifierInfo *clsName = Class->getIdentifier(); |
Benjamin Kramer | fc18842 | 2014-02-25 12:26:11 +0000 | [diff] [blame] | 2764 | ClsExprs.push_back(getStringLiteral(clsName->getName())); |
Craig Topper | cf2126e | 2015-10-22 03:13:07 +0000 | [diff] [blame] | 2765 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, ClsExprs, |
Fariborz Jahanian | 991a08d | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2766 | StartLoc, EndLoc); |
| 2767 | MsgExprs.push_back(Cls); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2768 | |
Fariborz Jahanian | 991a08d | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2769 | // Create a call to sel_registerName("arrayWithObjects:count:"). |
| 2770 | // it will be the 2nd argument. |
| 2771 | SmallVector<Expr*, 4> SelExprs; |
| 2772 | ObjCMethodDecl *ArrayMethod = Exp->getArrayWithObjectsMethod(); |
Benjamin Kramer | fc18842 | 2014-02-25 12:26:11 +0000 | [diff] [blame] | 2773 | SelExprs.push_back( |
| 2774 | getStringLiteral(ArrayMethod->getSelector().getAsString())); |
Fariborz Jahanian | 991a08d | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2775 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
Craig Topper | cf2126e | 2015-10-22 03:13:07 +0000 | [diff] [blame] | 2776 | SelExprs, StartLoc, EndLoc); |
Fariborz Jahanian | 991a08d | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2777 | MsgExprs.push_back(SelExp); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2778 | |
Fariborz Jahanian | e110fe4 | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 2779 | // (const id [])objects |
| 2780 | MsgExprs.push_back(ArrayLiteralObjects); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2781 | |
Fariborz Jahanian | e110fe4 | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 2782 | // (NSUInteger)cnt |
| 2783 | Expr *cnt = IntegerLiteral::Create(*Context, |
| 2784 | llvm::APInt(UnsignedIntSize, NumElements), |
| 2785 | Context->UnsignedIntTy, SourceLocation()); |
| 2786 | MsgExprs.push_back(cnt); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2787 | |
Fariborz Jahanian | 991a08d | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2788 | SmallVector<QualType, 4> ArgTypes; |
Fariborz Jahanian | f8e68e2 | 2015-04-09 18:36:50 +0000 | [diff] [blame] | 2789 | ArgTypes.push_back(Context->getObjCClassType()); |
Fariborz Jahanian | 991a08d | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2790 | ArgTypes.push_back(Context->getObjCSelType()); |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2791 | for (const auto *PI : ArrayMethod->parameters()) |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 2792 | ArgTypes.push_back(PI->getType()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2793 | |
Fariborz Jahanian | 991a08d | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2794 | QualType returnType = Exp->getType(); |
| 2795 | // Get the type, we will need to reference it in a couple spots. |
| 2796 | QualType msgSendType = MsgSendFlavor->getType(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2797 | |
Fariborz Jahanian | 991a08d | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2798 | // Create a reference to the objc_msgSend() declaration. |
| 2799 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, false, msgSendType, |
| 2800 | VK_LValue, SourceLocation()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2801 | |
Fariborz Jahanian | 991a08d | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2802 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, |
| 2803 | Context->getPointerType(Context->VoidTy), |
| 2804 | CK_BitCast, DRE); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2805 | |
Fariborz Jahanian | 991a08d | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2806 | // Now do the "normal" pointer to function cast. |
| 2807 | QualType castType = |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2808 | getSimpleFunctionType(returnType, ArgTypes, ArrayMethod->isVariadic()); |
Fariborz Jahanian | 991a08d | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2809 | castType = Context->getPointerType(castType); |
| 2810 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
| 2811 | cast); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2812 | |
Fariborz Jahanian | 991a08d | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2813 | // Don't forget the parens to enforce the proper binding. |
| 2814 | ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2815 | |
Fariborz Jahanian | 991a08d | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2816 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2817 | CallExpr *CE = new (Context) |
| 2818 | CallExpr(*Context, PE, MsgExprs, FT->getReturnType(), VK_RValue, EndLoc); |
Fariborz Jahanian | 991a08d | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2819 | ReplaceStmt(Exp, CE); |
| 2820 | return CE; |
| 2821 | } |
| 2822 | |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2823 | Stmt *RewriteModernObjC::RewriteObjCDictionaryLiteralExpr(ObjCDictionaryLiteral *Exp) { |
| 2824 | // synthesize declaration of helper functions needed in this routine. |
| 2825 | if (!SelGetUidFunctionDecl) |
| 2826 | SynthSelGetUidFunctionDecl(); |
| 2827 | // use objc_msgSend() for all. |
| 2828 | if (!MsgSendFunctionDecl) |
| 2829 | SynthMsgSendFunctionDecl(); |
| 2830 | if (!GetClassFunctionDecl) |
| 2831 | SynthGetClassFunctionDecl(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2832 | |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2833 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2834 | SourceLocation StartLoc = Exp->getBeginLoc(); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2835 | SourceLocation EndLoc = Exp->getEndLoc(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2836 | |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2837 | // Build the expression: __NSContainer_literal(int, ...).arr |
| 2838 | QualType IntQT = Context->IntTy; |
| 2839 | QualType NSDictFType = |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2840 | getSimpleFunctionType(Context->VoidTy, IntQT, true); |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2841 | std::string NSDictFName("__NSContainer_literal"); |
| 2842 | FunctionDecl *NSDictFD = SynthBlockInitFunctionDecl(NSDictFName); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2843 | DeclRefExpr *NSDictDRE = |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2844 | new (Context) DeclRefExpr(NSDictFD, false, NSDictFType, VK_RValue, |
| 2845 | SourceLocation()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2846 | |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2847 | SmallVector<Expr*, 16> KeyExprs; |
| 2848 | SmallVector<Expr*, 16> ValueExprs; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2849 | |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2850 | unsigned NumElements = Exp->getNumElements(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2851 | unsigned UnsignedIntSize = |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2852 | static_cast<unsigned>(Context->getTypeSize(Context->UnsignedIntTy)); |
| 2853 | Expr *count = IntegerLiteral::Create(*Context, |
| 2854 | llvm::APInt(UnsignedIntSize, NumElements), |
| 2855 | Context->UnsignedIntTy, SourceLocation()); |
| 2856 | KeyExprs.push_back(count); |
| 2857 | ValueExprs.push_back(count); |
| 2858 | for (unsigned i = 0; i < NumElements; i++) { |
| 2859 | ObjCDictionaryElement Element = Exp->getKeyValueElement(i); |
| 2860 | KeyExprs.push_back(Element.Key); |
| 2861 | ValueExprs.push_back(Element.Value); |
| 2862 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2863 | |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2864 | // (const id [])objects |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2865 | Expr *NSValueCallExpr = |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2866 | new (Context) CallExpr(*Context, NSDictDRE, ValueExprs, |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2867 | NSDictFType, VK_LValue, SourceLocation()); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 2868 | |
| 2869 | FieldDecl *ARRFD = FieldDecl::Create(*Context, nullptr, SourceLocation(), |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2870 | SourceLocation(), |
| 2871 | &Context->Idents.get("arr"), |
Fariborz Jahanian | f8e68e2 | 2015-04-09 18:36:50 +0000 | [diff] [blame] | 2872 | Context->getPointerType(Context->VoidPtrTy), |
| 2873 | nullptr, /*BitWidth=*/nullptr, |
| 2874 | /*Mutable=*/true, ICIS_NoInit); |
| 2875 | MemberExpr *DictLiteralValueME = new (Context) |
| 2876 | MemberExpr(NSValueCallExpr, false, SourceLocation(), ARRFD, |
| 2877 | SourceLocation(), ARRFD->getType(), VK_LValue, OK_Ordinary); |
| 2878 | QualType ConstIdT = Context->getObjCIdType().withConst(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2879 | CStyleCastExpr * DictValueObjects = |
| 2880 | NoTypeInfoCStyleCastExpr(Context, |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2881 | Context->getPointerType(ConstIdT), |
| 2882 | CK_BitCast, |
| 2883 | DictLiteralValueME); |
| 2884 | // (const id <NSCopying> [])keys |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2885 | Expr *NSKeyCallExpr = |
Fariborz Jahanian | f8e68e2 | 2015-04-09 18:36:50 +0000 | [diff] [blame] | 2886 | new (Context) CallExpr(*Context, NSDictDRE, KeyExprs, |
| 2887 | NSDictFType, VK_LValue, SourceLocation()); |
| 2888 | |
| 2889 | MemberExpr *DictLiteralKeyME = new (Context) |
| 2890 | MemberExpr(NSKeyCallExpr, false, SourceLocation(), ARRFD, |
| 2891 | SourceLocation(), ARRFD->getType(), VK_LValue, OK_Ordinary); |
| 2892 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2893 | CStyleCastExpr * DictKeyObjects = |
| 2894 | NoTypeInfoCStyleCastExpr(Context, |
Fariborz Jahanian | f8e68e2 | 2015-04-09 18:36:50 +0000 | [diff] [blame] | 2895 | Context->getPointerType(ConstIdT), |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2896 | CK_BitCast, |
| 2897 | DictLiteralKeyME); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2898 | |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2899 | // Synthesize a call to objc_msgSend(). |
| 2900 | SmallVector<Expr*, 32> MsgExprs; |
| 2901 | SmallVector<Expr*, 4> ClsExprs; |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2902 | QualType expType = Exp->getType(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2903 | |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2904 | // Create a call to objc_getClass("NSArray"). It will be th 1st argument. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2905 | ObjCInterfaceDecl *Class = |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2906 | expType->getPointeeType()->getAs<ObjCObjectType>()->getInterface(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2907 | |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2908 | IdentifierInfo *clsName = Class->getIdentifier(); |
Benjamin Kramer | fc18842 | 2014-02-25 12:26:11 +0000 | [diff] [blame] | 2909 | ClsExprs.push_back(getStringLiteral(clsName->getName())); |
Craig Topper | cf2126e | 2015-10-22 03:13:07 +0000 | [diff] [blame] | 2910 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, ClsExprs, |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2911 | StartLoc, EndLoc); |
| 2912 | MsgExprs.push_back(Cls); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2913 | |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2914 | // Create a call to sel_registerName("arrayWithObjects:count:"). |
| 2915 | // it will be the 2nd argument. |
| 2916 | SmallVector<Expr*, 4> SelExprs; |
| 2917 | ObjCMethodDecl *DictMethod = Exp->getDictWithObjectsMethod(); |
Benjamin Kramer | fc18842 | 2014-02-25 12:26:11 +0000 | [diff] [blame] | 2918 | SelExprs.push_back(getStringLiteral(DictMethod->getSelector().getAsString())); |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2919 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
Craig Topper | cf2126e | 2015-10-22 03:13:07 +0000 | [diff] [blame] | 2920 | SelExprs, StartLoc, EndLoc); |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2921 | MsgExprs.push_back(SelExp); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2922 | |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2923 | // (const id [])objects |
| 2924 | MsgExprs.push_back(DictValueObjects); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2925 | |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2926 | // (const id <NSCopying> [])keys |
| 2927 | MsgExprs.push_back(DictKeyObjects); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2928 | |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2929 | // (NSUInteger)cnt |
| 2930 | Expr *cnt = IntegerLiteral::Create(*Context, |
| 2931 | llvm::APInt(UnsignedIntSize, NumElements), |
| 2932 | Context->UnsignedIntTy, SourceLocation()); |
| 2933 | MsgExprs.push_back(cnt); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2934 | |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2935 | SmallVector<QualType, 8> ArgTypes; |
Fariborz Jahanian | f8e68e2 | 2015-04-09 18:36:50 +0000 | [diff] [blame] | 2936 | ArgTypes.push_back(Context->getObjCClassType()); |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2937 | ArgTypes.push_back(Context->getObjCSelType()); |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2938 | for (const auto *PI : DictMethod->parameters()) { |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 2939 | QualType T = PI->getType(); |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2940 | if (const PointerType* PT = T->getAs<PointerType>()) { |
| 2941 | QualType PointeeTy = PT->getPointeeType(); |
| 2942 | convertToUnqualifiedObjCType(PointeeTy); |
| 2943 | T = Context->getPointerType(PointeeTy); |
| 2944 | } |
| 2945 | ArgTypes.push_back(T); |
| 2946 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2947 | |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2948 | QualType returnType = Exp->getType(); |
| 2949 | // Get the type, we will need to reference it in a couple spots. |
| 2950 | QualType msgSendType = MsgSendFlavor->getType(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2951 | |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2952 | // Create a reference to the objc_msgSend() declaration. |
| 2953 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, false, msgSendType, |
| 2954 | VK_LValue, SourceLocation()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2955 | |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2956 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, |
| 2957 | Context->getPointerType(Context->VoidTy), |
| 2958 | CK_BitCast, DRE); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2959 | |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2960 | // Now do the "normal" pointer to function cast. |
| 2961 | QualType castType = |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2962 | getSimpleFunctionType(returnType, ArgTypes, DictMethod->isVariadic()); |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2963 | castType = Context->getPointerType(castType); |
| 2964 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
| 2965 | cast); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2966 | |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2967 | // Don't forget the parens to enforce the proper binding. |
| 2968 | ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2969 | |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2970 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2971 | CallExpr *CE = new (Context) |
| 2972 | CallExpr(*Context, PE, MsgExprs, FT->getReturnType(), VK_RValue, EndLoc); |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2973 | ReplaceStmt(Exp, CE); |
| 2974 | return CE; |
| 2975 | } |
| 2976 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2977 | // struct __rw_objc_super { |
| 2978 | // struct objc_object *object; struct objc_object *superClass; |
Fariborz Jahanian | 4af0e9e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 2979 | // }; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2980 | QualType RewriteModernObjC::getSuperStructType() { |
| 2981 | if (!SuperStructDecl) { |
| 2982 | SuperStructDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 2983 | SourceLocation(), SourceLocation(), |
Fariborz Jahanian | 4af0e9e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 2984 | &Context->Idents.get("__rw_objc_super")); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2985 | QualType FieldTypes[2]; |
| 2986 | |
Fariborz Jahanian | 4af0e9e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 2987 | // struct objc_object *object; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2988 | FieldTypes[0] = Context->getObjCIdType(); |
Fariborz Jahanian | 4af0e9e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 2989 | // struct objc_object *superClass; |
| 2990 | FieldTypes[1] = Context->getObjCIdType(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2991 | |
| 2992 | // Create fields |
| 2993 | for (unsigned i = 0; i < 2; ++i) { |
| 2994 | SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl, |
| 2995 | SourceLocation(), |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 2996 | SourceLocation(), nullptr, |
| 2997 | FieldTypes[i], nullptr, |
| 2998 | /*BitWidth=*/nullptr, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2999 | /*Mutable=*/false, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3000 | ICIS_NoInit)); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3001 | } |
| 3002 | |
| 3003 | SuperStructDecl->completeDefinition(); |
| 3004 | } |
| 3005 | return Context->getTagDeclType(SuperStructDecl); |
| 3006 | } |
| 3007 | |
| 3008 | QualType RewriteModernObjC::getConstantStringStructType() { |
| 3009 | if (!ConstantStringDecl) { |
| 3010 | ConstantStringDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 3011 | SourceLocation(), SourceLocation(), |
| 3012 | &Context->Idents.get("__NSConstantStringImpl")); |
| 3013 | QualType FieldTypes[4]; |
| 3014 | |
| 3015 | // struct objc_object *receiver; |
| 3016 | FieldTypes[0] = Context->getObjCIdType(); |
| 3017 | // int flags; |
| 3018 | FieldTypes[1] = Context->IntTy; |
| 3019 | // char *str; |
| 3020 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
| 3021 | // long length; |
| 3022 | FieldTypes[3] = Context->LongTy; |
| 3023 | |
| 3024 | // Create fields |
| 3025 | for (unsigned i = 0; i < 4; ++i) { |
| 3026 | ConstantStringDecl->addDecl(FieldDecl::Create(*Context, |
| 3027 | ConstantStringDecl, |
| 3028 | SourceLocation(), |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 3029 | SourceLocation(), nullptr, |
| 3030 | FieldTypes[i], nullptr, |
| 3031 | /*BitWidth=*/nullptr, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3032 | /*Mutable=*/true, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3033 | ICIS_NoInit)); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3034 | } |
| 3035 | |
| 3036 | ConstantStringDecl->completeDefinition(); |
| 3037 | } |
| 3038 | return Context->getTagDeclType(ConstantStringDecl); |
| 3039 | } |
| 3040 | |
Fariborz Jahanian | 4a031bd | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3041 | /// getFunctionSourceLocation - returns start location of a function |
| 3042 | /// definition. Complication arises when function has declared as |
| 3043 | /// extern "C" or extern "C" {...} |
| 3044 | static SourceLocation getFunctionSourceLocation (RewriteModernObjC &R, |
| 3045 | FunctionDecl *FD) { |
| 3046 | if (FD->isExternC() && !FD->isMain()) { |
| 3047 | const DeclContext *DC = FD->getDeclContext(); |
| 3048 | if (const LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(DC)) |
| 3049 | // if it is extern "C" {...}, return function decl's own location. |
| 3050 | if (!LSD->getRBraceLoc().isValid()) |
| 3051 | return LSD->getExternLoc(); |
| 3052 | } |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3053 | if (FD->getStorageClass() != SC_None) |
Fariborz Jahanian | 4a031bd | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3054 | R.RewriteBlockLiteralFunctionDecl(FD); |
| 3055 | return FD->getTypeSpecStartLoc(); |
| 3056 | } |
| 3057 | |
Fariborz Jahanian | aa4a242 | 2012-11-06 17:30:23 +0000 | [diff] [blame] | 3058 | void RewriteModernObjC::RewriteLineDirective(const Decl *D) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3059 | |
Fariborz Jahanian | aa4a242 | 2012-11-06 17:30:23 +0000 | [diff] [blame] | 3060 | SourceLocation Location = D->getLocation(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3061 | |
Fariborz Jahanian | e4c7e85 | 2013-02-08 00:27:34 +0000 | [diff] [blame] | 3062 | if (Location.isFileID() && GenerateLineInfo) { |
Fariborz Jahanian | 83dadc7 | 2012-11-07 18:15:53 +0000 | [diff] [blame] | 3063 | std::string LineString("\n#line "); |
Fariborz Jahanian | aa4a242 | 2012-11-06 17:30:23 +0000 | [diff] [blame] | 3064 | PresumedLoc PLoc = SM->getPresumedLoc(Location); |
| 3065 | LineString += utostr(PLoc.getLine()); |
| 3066 | LineString += " \""; |
NAKAMURA Takumi | b46a05c | 2012-11-06 22:45:31 +0000 | [diff] [blame] | 3067 | LineString += Lexer::Stringify(PLoc.getFilename()); |
Fariborz Jahanian | aa4a242 | 2012-11-06 17:30:23 +0000 | [diff] [blame] | 3068 | if (isa<ObjCMethodDecl>(D)) |
| 3069 | LineString += "\""; |
| 3070 | else LineString += "\"\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3071 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3072 | Location = D->getBeginLoc(); |
Fariborz Jahanian | aa4a242 | 2012-11-06 17:30:23 +0000 | [diff] [blame] | 3073 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 3074 | if (FD->isExternC() && !FD->isMain()) { |
| 3075 | const DeclContext *DC = FD->getDeclContext(); |
| 3076 | if (const LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(DC)) |
| 3077 | // if it is extern "C" {...}, return function decl's own location. |
| 3078 | if (!LSD->getRBraceLoc().isValid()) |
| 3079 | Location = LSD->getExternLoc(); |
| 3080 | } |
| 3081 | } |
| 3082 | InsertText(Location, LineString); |
| 3083 | } |
| 3084 | } |
| 3085 | |
Fariborz Jahanian | 4a031bd | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3086 | /// SynthMsgSendStretCallExpr - This routine translates message expression |
| 3087 | /// into a call to objc_msgSend_stret() entry point. Tricky part is that |
| 3088 | /// nil check on receiver must be performed before calling objc_msgSend_stret. |
| 3089 | /// MsgSendStretFlavor - function declaration objc_msgSend_stret(...) |
| 3090 | /// msgSendType - function type of objc_msgSend_stret(...) |
| 3091 | /// returnType - Result type of the method being synthesized. |
| 3092 | /// ArgTypes - type of the arguments passed to objc_msgSend_stret, starting with receiver type. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3093 | /// MsgExprs - list of argument expressions being passed to objc_msgSend_stret, |
Fariborz Jahanian | 4a031bd | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3094 | /// starting with receiver. |
| 3095 | /// Method - Method being rewritten. |
| 3096 | Expr *RewriteModernObjC::SynthMsgSendStretCallExpr(FunctionDecl *MsgSendStretFlavor, |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3097 | QualType returnType, |
Fariborz Jahanian | 4a031bd | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3098 | SmallVectorImpl<QualType> &ArgTypes, |
| 3099 | SmallVectorImpl<Expr*> &MsgExprs, |
| 3100 | ObjCMethodDecl *Method) { |
| 3101 | // Now do the "normal" pointer to function cast. |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 3102 | QualType castType = getSimpleFunctionType(returnType, ArgTypes, |
| 3103 | Method ? Method->isVariadic() |
| 3104 | : false); |
Fariborz Jahanian | 4a031bd | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3105 | castType = Context->getPointerType(castType); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3106 | |
Fariborz Jahanian | 4a031bd | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3107 | // build type for containing the objc_msgSend_stret object. |
| 3108 | static unsigned stretCount=0; |
| 3109 | std::string name = "__Stret"; name += utostr(stretCount); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3110 | std::string str = |
Fariborz Jahanian | 1a11252 | 2012-07-25 21:48:36 +0000 | [diff] [blame] | 3111 | "extern \"C\" void * __cdecl memset(void *_Dst, int _Val, size_t _Size);\n"; |
Fariborz Jahanian | b1a2124 | 2013-09-09 19:59:59 +0000 | [diff] [blame] | 3112 | str += "namespace {\n"; |
Fariborz Jahanian | 1a11252 | 2012-07-25 21:48:36 +0000 | [diff] [blame] | 3113 | str += "struct "; str += name; |
Fariborz Jahanian | 4a031bd | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3114 | str += " {\n\t"; |
| 3115 | str += name; |
| 3116 | str += "(id receiver, SEL sel"; |
| 3117 | for (unsigned i = 2; i < ArgTypes.size(); i++) { |
Fariborz Jahanian | 2794ad5 | 2012-06-29 19:55:46 +0000 | [diff] [blame] | 3118 | std::string ArgName = "arg"; ArgName += utostr(i); |
| 3119 | ArgTypes[i].getAsStringInternal(ArgName, Context->getPrintingPolicy()); |
| 3120 | str += ", "; str += ArgName; |
Fariborz Jahanian | 4a031bd | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3121 | } |
| 3122 | // could be vararg. |
| 3123 | for (unsigned i = ArgTypes.size(); i < MsgExprs.size(); i++) { |
Fariborz Jahanian | 2794ad5 | 2012-06-29 19:55:46 +0000 | [diff] [blame] | 3124 | std::string ArgName = "arg"; ArgName += utostr(i); |
| 3125 | MsgExprs[i]->getType().getAsStringInternal(ArgName, |
| 3126 | Context->getPrintingPolicy()); |
| 3127 | str += ", "; str += ArgName; |
Fariborz Jahanian | 4a031bd | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3128 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3129 | |
Fariborz Jahanian | 4a031bd | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3130 | str += ") {\n"; |
Fariborz Jahanian | b1a2124 | 2013-09-09 19:59:59 +0000 | [diff] [blame] | 3131 | str += "\t unsigned size = sizeof("; |
| 3132 | str += returnType.getAsString(Context->getPrintingPolicy()); str += ");\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3133 | |
Fariborz Jahanian | b1a2124 | 2013-09-09 19:59:59 +0000 | [diff] [blame] | 3134 | str += "\t if (size == 1 || size == 2 || size == 4 || size == 8)\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3135 | |
Fariborz Jahanian | b1a2124 | 2013-09-09 19:59:59 +0000 | [diff] [blame] | 3136 | str += "\t s = (("; str += castType.getAsString(Context->getPrintingPolicy()); |
| 3137 | str += ")(void *)objc_msgSend)(receiver, sel"; |
| 3138 | for (unsigned i = 2; i < ArgTypes.size(); i++) { |
| 3139 | str += ", arg"; str += utostr(i); |
| 3140 | } |
| 3141 | // could be vararg. |
| 3142 | for (unsigned i = ArgTypes.size(); i < MsgExprs.size(); i++) { |
| 3143 | str += ", arg"; str += utostr(i); |
| 3144 | } |
| 3145 | str+= ");\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3146 | |
Fariborz Jahanian | b1a2124 | 2013-09-09 19:59:59 +0000 | [diff] [blame] | 3147 | str += "\t else if (receiver == 0)\n"; |
Fariborz Jahanian | 4a031bd | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3148 | str += "\t memset((void*)&s, 0, sizeof(s));\n"; |
| 3149 | str += "\t else\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3150 | |
Fariborz Jahanian | 4a031bd | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3151 | str += "\t s = (("; str += castType.getAsString(Context->getPrintingPolicy()); |
| 3152 | str += ")(void *)objc_msgSend_stret)(receiver, sel"; |
| 3153 | for (unsigned i = 2; i < ArgTypes.size(); i++) { |
| 3154 | str += ", arg"; str += utostr(i); |
| 3155 | } |
| 3156 | // could be vararg. |
| 3157 | for (unsigned i = ArgTypes.size(); i < MsgExprs.size(); i++) { |
| 3158 | str += ", arg"; str += utostr(i); |
| 3159 | } |
Fariborz Jahanian | 4a031bd | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3160 | str += ");\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3161 | |
Fariborz Jahanian | 4a031bd | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3162 | str += "\t}\n"; |
| 3163 | str += "\t"; str += returnType.getAsString(Context->getPrintingPolicy()); |
| 3164 | str += " s;\n"; |
Fariborz Jahanian | b1a2124 | 2013-09-09 19:59:59 +0000 | [diff] [blame] | 3165 | str += "};\n};\n\n"; |
Fariborz Jahanian | f1f36c6 | 2012-08-21 18:56:50 +0000 | [diff] [blame] | 3166 | SourceLocation FunLocStart; |
| 3167 | if (CurFunctionDef) |
| 3168 | FunLocStart = getFunctionSourceLocation(*this, CurFunctionDef); |
| 3169 | else { |
| 3170 | assert(CurMethodDef && "SynthMsgSendStretCallExpr - CurMethodDef is null"); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3171 | FunLocStart = CurMethodDef->getBeginLoc(); |
Fariborz Jahanian | f1f36c6 | 2012-08-21 18:56:50 +0000 | [diff] [blame] | 3172 | } |
| 3173 | |
Fariborz Jahanian | 4a031bd | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3174 | InsertText(FunLocStart, str); |
| 3175 | ++stretCount; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3176 | |
Fariborz Jahanian | 4a031bd | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3177 | // AST for __Stretn(receiver, args).s; |
| 3178 | IdentifierInfo *ID = &Context->Idents.get(name); |
| 3179 | FunctionDecl *FD = FunctionDecl::Create(*Context, TUDecl, SourceLocation(), |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 3180 | SourceLocation(), ID, castType, |
| 3181 | nullptr, SC_Extern, false, false); |
Fariborz Jahanian | 4a031bd | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3182 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, false, castType, VK_RValue, |
| 3183 | SourceLocation()); |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3184 | CallExpr *STCE = new (Context) CallExpr(*Context, DRE, MsgExprs, |
Fariborz Jahanian | 4a031bd | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3185 | castType, VK_LValue, SourceLocation()); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 3186 | |
| 3187 | FieldDecl *FieldD = FieldDecl::Create(*Context, nullptr, SourceLocation(), |
Fariborz Jahanian | 4a031bd | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3188 | SourceLocation(), |
| 3189 | &Context->Idents.get("s"), |
Fariborz Jahanian | f8e68e2 | 2015-04-09 18:36:50 +0000 | [diff] [blame] | 3190 | returnType, nullptr, |
| 3191 | /*BitWidth=*/nullptr, |
| 3192 | /*Mutable=*/true, ICIS_NoInit); |
| 3193 | MemberExpr *ME = new (Context) |
| 3194 | MemberExpr(STCE, false, SourceLocation(), FieldD, SourceLocation(), |
| 3195 | FieldD->getType(), VK_LValue, OK_Ordinary); |
| 3196 | |
| 3197 | return ME; |
| 3198 | } |
Fariborz Jahanian | 4a031bd | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3199 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3200 | Stmt *RewriteModernObjC::SynthMessageExpr(ObjCMessageExpr *Exp, |
| 3201 | SourceLocation StartLoc, |
| 3202 | SourceLocation EndLoc) { |
| 3203 | if (!SelGetUidFunctionDecl) |
| 3204 | SynthSelGetUidFunctionDecl(); |
| 3205 | if (!MsgSendFunctionDecl) |
| 3206 | SynthMsgSendFunctionDecl(); |
| 3207 | if (!MsgSendSuperFunctionDecl) |
| 3208 | SynthMsgSendSuperFunctionDecl(); |
| 3209 | if (!MsgSendStretFunctionDecl) |
| 3210 | SynthMsgSendStretFunctionDecl(); |
| 3211 | if (!MsgSendSuperStretFunctionDecl) |
| 3212 | SynthMsgSendSuperStretFunctionDecl(); |
| 3213 | if (!MsgSendFpretFunctionDecl) |
| 3214 | SynthMsgSendFpretFunctionDecl(); |
| 3215 | if (!GetClassFunctionDecl) |
| 3216 | SynthGetClassFunctionDecl(); |
| 3217 | if (!GetSuperClassFunctionDecl) |
| 3218 | SynthGetSuperClassFunctionDecl(); |
| 3219 | if (!GetMetaClassFunctionDecl) |
| 3220 | SynthGetMetaClassFunctionDecl(); |
| 3221 | |
| 3222 | // default to objc_msgSend(). |
| 3223 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 3224 | // May need to use objc_msgSend_stret() as well. |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 3225 | FunctionDecl *MsgSendStretFlavor = nullptr; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3226 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3227 | QualType resultType = mDecl->getReturnType(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3228 | if (resultType->isRecordType()) |
| 3229 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
| 3230 | else if (resultType->isRealFloatingType()) |
| 3231 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
| 3232 | } |
| 3233 | |
| 3234 | // Synthesize a call to objc_msgSend(). |
| 3235 | SmallVector<Expr*, 8> MsgExprs; |
| 3236 | switch (Exp->getReceiverKind()) { |
| 3237 | case ObjCMessageExpr::SuperClass: { |
| 3238 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 3239 | if (MsgSendStretFlavor) |
| 3240 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 3241 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 3242 | |
| 3243 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
| 3244 | |
| 3245 | SmallVector<Expr*, 4> InitExprs; |
| 3246 | |
| 3247 | // set the receiver to self, the first argument to all methods. |
| 3248 | InitExprs.push_back( |
| 3249 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 3250 | CK_BitCast, |
| 3251 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3252 | false, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3253 | Context->getObjCIdType(), |
| 3254 | VK_RValue, |
| 3255 | SourceLocation())) |
| 3256 | ); // set the 'receiver'. |
| 3257 | |
| 3258 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 3259 | SmallVector<Expr*, 8> ClsExprs; |
Benjamin Kramer | fc18842 | 2014-02-25 12:26:11 +0000 | [diff] [blame] | 3260 | ClsExprs.push_back(getStringLiteral(ClassDecl->getIdentifier()->getName())); |
Fariborz Jahanian | 9c0c050 | 2012-05-08 20:55:55 +0000 | [diff] [blame] | 3261 | // (Class)objc_getClass("CurrentClass") |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3262 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
Craig Topper | cf2126e | 2015-10-22 03:13:07 +0000 | [diff] [blame] | 3263 | ClsExprs, StartLoc, EndLoc); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3264 | ClsExprs.clear(); |
Fariborz Jahanian | 9c0c050 | 2012-05-08 20:55:55 +0000 | [diff] [blame] | 3265 | ClsExprs.push_back(Cls); |
Craig Topper | cf2126e | 2015-10-22 03:13:07 +0000 | [diff] [blame] | 3266 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, ClsExprs, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3267 | StartLoc, EndLoc); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3268 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3269 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 3270 | // To turn off a warning, type-cast to 'id' |
| 3271 | InitExprs.push_back( // set 'super class', using class_getSuperclass(). |
| 3272 | NoTypeInfoCStyleCastExpr(Context, |
| 3273 | Context->getObjCIdType(), |
| 3274 | CK_BitCast, Cls)); |
Fariborz Jahanian | 4af0e9e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3275 | // struct __rw_objc_super |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3276 | QualType superType = getSuperStructType(); |
| 3277 | Expr *SuperRep; |
| 3278 | |
| 3279 | if (LangOpts.MicrosoftExt) { |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 3280 | SynthSuperConstructorFunctionDecl(); |
| 3281 | // Simulate a constructor call... |
| 3282 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperConstructorFunctionDecl, |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3283 | false, superType, VK_LValue, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3284 | SourceLocation()); |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3285 | SuperRep = new (Context) CallExpr(*Context, DRE, InitExprs, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3286 | superType, VK_LValue, |
| 3287 | SourceLocation()); |
| 3288 | // The code for super is a little tricky to prevent collision with |
| 3289 | // the structure definition in the header. The rewriter has it's own |
| 3290 | // internal definition (__rw_objc_super) that is uses. This is why |
| 3291 | // we need the cast below. For example: |
Fariborz Jahanian | 4af0e9e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3292 | // (struct __rw_objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3293 | // |
| 3294 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
| 3295 | Context->getPointerType(SuperRep->getType()), |
| 3296 | VK_RValue, OK_Ordinary, |
Aaron Ballman | a503855 | 2018-01-09 13:07:03 +0000 | [diff] [blame] | 3297 | SourceLocation(), false); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3298 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 3299 | Context->getPointerType(superType), |
| 3300 | CK_BitCast, SuperRep); |
| 3301 | } else { |
Fariborz Jahanian | 4af0e9e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3302 | // (struct __rw_objc_super) { <exprs from above> } |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3303 | InitListExpr *ILE = |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3304 | new (Context) InitListExpr(*Context, SourceLocation(), InitExprs, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3305 | SourceLocation()); |
| 3306 | TypeSourceInfo *superTInfo |
| 3307 | = Context->getTrivialTypeSourceInfo(superType); |
| 3308 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
| 3309 | superType, VK_LValue, |
| 3310 | ILE, false); |
Fariborz Jahanian | 4af0e9e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3311 | // struct __rw_objc_super * |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3312 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
| 3313 | Context->getPointerType(SuperRep->getType()), |
| 3314 | VK_RValue, OK_Ordinary, |
Aaron Ballman | a503855 | 2018-01-09 13:07:03 +0000 | [diff] [blame] | 3315 | SourceLocation(), false); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3316 | } |
| 3317 | MsgExprs.push_back(SuperRep); |
| 3318 | break; |
| 3319 | } |
| 3320 | |
| 3321 | case ObjCMessageExpr::Class: { |
| 3322 | SmallVector<Expr*, 8> ClsExprs; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3323 | ObjCInterfaceDecl *Class |
| 3324 | = Exp->getClassReceiver()->getAs<ObjCObjectType>()->getInterface(); |
| 3325 | IdentifierInfo *clsName = Class->getIdentifier(); |
Benjamin Kramer | fc18842 | 2014-02-25 12:26:11 +0000 | [diff] [blame] | 3326 | ClsExprs.push_back(getStringLiteral(clsName->getName())); |
Craig Topper | cf2126e | 2015-10-22 03:13:07 +0000 | [diff] [blame] | 3327 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, ClsExprs, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3328 | StartLoc, EndLoc); |
Fariborz Jahanian | 9c0c050 | 2012-05-08 20:55:55 +0000 | [diff] [blame] | 3329 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 3330 | Context->getObjCIdType(), |
| 3331 | CK_BitCast, Cls); |
| 3332 | MsgExprs.push_back(ArgExpr); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3333 | break; |
| 3334 | } |
| 3335 | |
| 3336 | case ObjCMessageExpr::SuperInstance:{ |
| 3337 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 3338 | if (MsgSendStretFlavor) |
| 3339 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 3340 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 3341 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
| 3342 | SmallVector<Expr*, 4> InitExprs; |
| 3343 | |
| 3344 | InitExprs.push_back( |
| 3345 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 3346 | CK_BitCast, |
| 3347 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3348 | false, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3349 | Context->getObjCIdType(), |
| 3350 | VK_RValue, SourceLocation())) |
| 3351 | ); // set the 'receiver'. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3352 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3353 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 3354 | SmallVector<Expr*, 8> ClsExprs; |
Benjamin Kramer | fc18842 | 2014-02-25 12:26:11 +0000 | [diff] [blame] | 3355 | ClsExprs.push_back(getStringLiteral(ClassDecl->getIdentifier()->getName())); |
Fariborz Jahanian | 9c0c050 | 2012-05-08 20:55:55 +0000 | [diff] [blame] | 3356 | // (Class)objc_getClass("CurrentClass") |
Craig Topper | cf2126e | 2015-10-22 03:13:07 +0000 | [diff] [blame] | 3357 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, ClsExprs, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3358 | StartLoc, EndLoc); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3359 | ClsExprs.clear(); |
Fariborz Jahanian | 9c0c050 | 2012-05-08 20:55:55 +0000 | [diff] [blame] | 3360 | ClsExprs.push_back(Cls); |
Craig Topper | cf2126e | 2015-10-22 03:13:07 +0000 | [diff] [blame] | 3361 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, ClsExprs, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3362 | StartLoc, EndLoc); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3363 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3364 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 3365 | // To turn off a warning, type-cast to 'id' |
| 3366 | InitExprs.push_back( |
| 3367 | // set 'super class', using class_getSuperclass(). |
| 3368 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 3369 | CK_BitCast, Cls)); |
Fariborz Jahanian | 4af0e9e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3370 | // struct __rw_objc_super |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3371 | QualType superType = getSuperStructType(); |
| 3372 | Expr *SuperRep; |
| 3373 | |
| 3374 | if (LangOpts.MicrosoftExt) { |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 3375 | SynthSuperConstructorFunctionDecl(); |
| 3376 | // Simulate a constructor call... |
| 3377 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperConstructorFunctionDecl, |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3378 | false, superType, VK_LValue, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3379 | SourceLocation()); |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3380 | SuperRep = new (Context) CallExpr(*Context, DRE, InitExprs, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3381 | superType, VK_LValue, SourceLocation()); |
| 3382 | // The code for super is a little tricky to prevent collision with |
| 3383 | // the structure definition in the header. The rewriter has it's own |
| 3384 | // internal definition (__rw_objc_super) that is uses. This is why |
| 3385 | // we need the cast below. For example: |
Fariborz Jahanian | 4af0e9e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3386 | // (struct __rw_objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3387 | // |
| 3388 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
| 3389 | Context->getPointerType(SuperRep->getType()), |
| 3390 | VK_RValue, OK_Ordinary, |
Aaron Ballman | a503855 | 2018-01-09 13:07:03 +0000 | [diff] [blame] | 3391 | SourceLocation(), false); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3392 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 3393 | Context->getPointerType(superType), |
| 3394 | CK_BitCast, SuperRep); |
| 3395 | } else { |
Fariborz Jahanian | 4af0e9e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3396 | // (struct __rw_objc_super) { <exprs from above> } |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3397 | InitListExpr *ILE = |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3398 | new (Context) InitListExpr(*Context, SourceLocation(), InitExprs, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3399 | SourceLocation()); |
| 3400 | TypeSourceInfo *superTInfo |
| 3401 | = Context->getTrivialTypeSourceInfo(superType); |
| 3402 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
| 3403 | superType, VK_RValue, ILE, |
| 3404 | false); |
| 3405 | } |
| 3406 | MsgExprs.push_back(SuperRep); |
| 3407 | break; |
| 3408 | } |
| 3409 | |
| 3410 | case ObjCMessageExpr::Instance: { |
| 3411 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 3412 | // Foo<Proto> *. |
| 3413 | Expr *recExpr = Exp->getInstanceReceiver(); |
| 3414 | while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr)) |
| 3415 | recExpr = CE->getSubExpr(); |
| 3416 | CastKind CK = recExpr->getType()->isObjCObjectPointerType() |
| 3417 | ? CK_BitCast : recExpr->getType()->isBlockPointerType() |
| 3418 | ? CK_BlockPointerToObjCPointerCast |
| 3419 | : CK_CPointerToObjCPointerCast; |
| 3420 | |
| 3421 | recExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 3422 | CK, recExpr); |
| 3423 | MsgExprs.push_back(recExpr); |
| 3424 | break; |
| 3425 | } |
| 3426 | } |
| 3427 | |
| 3428 | // Create a call to sel_registerName("selName"), it will be the 2nd argument. |
| 3429 | SmallVector<Expr*, 8> SelExprs; |
Benjamin Kramer | fc18842 | 2014-02-25 12:26:11 +0000 | [diff] [blame] | 3430 | SelExprs.push_back(getStringLiteral(Exp->getSelector().getAsString())); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3431 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
Craig Topper | cf2126e | 2015-10-22 03:13:07 +0000 | [diff] [blame] | 3432 | SelExprs, StartLoc, EndLoc); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3433 | MsgExprs.push_back(SelExp); |
| 3434 | |
| 3435 | // Now push any user supplied arguments. |
| 3436 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
| 3437 | Expr *userExpr = Exp->getArg(i); |
| 3438 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 3439 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 3440 | // Reuse the ICE type, it is exactly what the doctor ordered. |
| 3441 | QualType type = ICE->getType(); |
| 3442 | if (needToScanForQualifiers(type)) |
| 3443 | type = Context->getObjCIdType(); |
| 3444 | // Make sure we convert "type (^)(...)" to "type (*)(...)". |
| 3445 | (void)convertBlockPointerToFunctionPointer(type); |
| 3446 | const Expr *SubExpr = ICE->IgnoreParenImpCasts(); |
| 3447 | CastKind CK; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3448 | if (SubExpr->getType()->isIntegralType(*Context) && |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3449 | type->isBooleanType()) { |
| 3450 | CK = CK_IntegralToBoolean; |
| 3451 | } else if (type->isObjCObjectPointerType()) { |
| 3452 | if (SubExpr->getType()->isBlockPointerType()) { |
| 3453 | CK = CK_BlockPointerToObjCPointerCast; |
| 3454 | } else if (SubExpr->getType()->isPointerType()) { |
| 3455 | CK = CK_CPointerToObjCPointerCast; |
| 3456 | } else { |
| 3457 | CK = CK_BitCast; |
| 3458 | } |
| 3459 | } else { |
| 3460 | CK = CK_BitCast; |
| 3461 | } |
| 3462 | |
| 3463 | userExpr = NoTypeInfoCStyleCastExpr(Context, type, CK, userExpr); |
| 3464 | } |
| 3465 | // Make id<P...> cast into an 'id' cast. |
| 3466 | else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) { |
| 3467 | if (CE->getType()->isObjCQualifiedIdType()) { |
| 3468 | while ((CE = dyn_cast<CStyleCastExpr>(userExpr))) |
| 3469 | userExpr = CE->getSubExpr(); |
| 3470 | CastKind CK; |
| 3471 | if (userExpr->getType()->isIntegralType(*Context)) { |
| 3472 | CK = CK_IntegralToPointer; |
| 3473 | } else if (userExpr->getType()->isBlockPointerType()) { |
| 3474 | CK = CK_BlockPointerToObjCPointerCast; |
| 3475 | } else if (userExpr->getType()->isPointerType()) { |
| 3476 | CK = CK_CPointerToObjCPointerCast; |
| 3477 | } else { |
| 3478 | CK = CK_BitCast; |
| 3479 | } |
| 3480 | userExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 3481 | CK, userExpr); |
| 3482 | } |
| 3483 | } |
| 3484 | MsgExprs.push_back(userExpr); |
| 3485 | // We've transferred the ownership to MsgExprs. For now, we *don't* null |
| 3486 | // out the argument in the original expression (since we aren't deleting |
| 3487 | // the ObjCMessageExpr). See RewritePropertyOrImplicitSetter() usage for more info. |
| 3488 | //Exp->setArg(i, 0); |
| 3489 | } |
| 3490 | // Generate the funky cast. |
| 3491 | CastExpr *cast; |
| 3492 | SmallVector<QualType, 8> ArgTypes; |
| 3493 | QualType returnType; |
| 3494 | |
| 3495 | // Push 'id' and 'SEL', the 2 implicit arguments. |
| 3496 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 3497 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 3498 | else |
| 3499 | ArgTypes.push_back(Context->getObjCIdType()); |
| 3500 | ArgTypes.push_back(Context->getObjCSelType()); |
| 3501 | if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) { |
| 3502 | // Push any user argument types. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 3503 | for (const auto *PI : OMD->parameters()) { |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 3504 | QualType t = PI->getType()->isObjCQualifiedIdType() |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3505 | ? Context->getObjCIdType() |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 3506 | : PI->getType(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3507 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
| 3508 | (void)convertBlockPointerToFunctionPointer(t); |
| 3509 | ArgTypes.push_back(t); |
| 3510 | } |
| 3511 | returnType = Exp->getType(); |
| 3512 | convertToUnqualifiedObjCType(returnType); |
| 3513 | (void)convertBlockPointerToFunctionPointer(returnType); |
| 3514 | } else { |
| 3515 | returnType = Context->getObjCIdType(); |
| 3516 | } |
| 3517 | // Get the type, we will need to reference it in a couple spots. |
| 3518 | QualType msgSendType = MsgSendFlavor->getType(); |
| 3519 | |
| 3520 | // Create a reference to the objc_msgSend() declaration. |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3521 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, false, msgSendType, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3522 | VK_LValue, SourceLocation()); |
| 3523 | |
| 3524 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
| 3525 | // If we don't do this cast, we get the following bizarre warning/note: |
| 3526 | // xx.m:13: warning: function called through a non-compatible type |
| 3527 | // xx.m:13: note: if this code is reached, the program will abort |
| 3528 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 3529 | Context->getPointerType(Context->VoidTy), |
| 3530 | CK_BitCast, DRE); |
| 3531 | |
| 3532 | // Now do the "normal" pointer to function cast. |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 3533 | // If we don't have a method decl, force a variadic cast. |
| 3534 | const ObjCMethodDecl *MD = Exp->getMethodDecl(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3535 | QualType castType = |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 3536 | getSimpleFunctionType(returnType, ArgTypes, MD ? MD->isVariadic() : true); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3537 | castType = Context->getPointerType(castType); |
| 3538 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
| 3539 | cast); |
| 3540 | |
| 3541 | // Don't forget the parens to enforce the proper binding. |
| 3542 | ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast); |
| 3543 | |
| 3544 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3545 | CallExpr *CE = new (Context) |
| 3546 | CallExpr(*Context, PE, MsgExprs, FT->getReturnType(), VK_RValue, EndLoc); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3547 | Stmt *ReplacingStmt = CE; |
| 3548 | if (MsgSendStretFlavor) { |
| 3549 | // We have the method which returns a struct/union. Must also generate |
| 3550 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 3551 | // expression which dictate which one to envoke depending on size of |
| 3552 | // method's return type. |
| 3553 | |
Fariborz Jahanian | b1a2124 | 2013-09-09 19:59:59 +0000 | [diff] [blame] | 3554 | Expr *STCE = SynthMsgSendStretCallExpr(MsgSendStretFlavor, |
| 3555 | returnType, |
Fariborz Jahanian | 4a031bd | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3556 | ArgTypes, MsgExprs, |
| 3557 | Exp->getMethodDecl()); |
Fariborz Jahanian | b1a2124 | 2013-09-09 19:59:59 +0000 | [diff] [blame] | 3558 | ReplacingStmt = STCE; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3559 | } |
| 3560 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
| 3561 | return ReplacingStmt; |
| 3562 | } |
| 3563 | |
| 3564 | Stmt *RewriteModernObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3565 | Stmt *ReplacingStmt = |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 3566 | SynthMessageExpr(Exp, Exp->getBeginLoc(), Exp->getEndLoc()); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3567 | |
| 3568 | // Now do the actual rewrite. |
| 3569 | ReplaceStmt(Exp, ReplacingStmt); |
| 3570 | |
| 3571 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
| 3572 | return ReplacingStmt; |
| 3573 | } |
| 3574 | |
| 3575 | // typedef struct objc_object Protocol; |
| 3576 | QualType RewriteModernObjC::getProtocolType() { |
| 3577 | if (!ProtocolTypeDecl) { |
| 3578 | TypeSourceInfo *TInfo |
| 3579 | = Context->getTrivialTypeSourceInfo(Context->getObjCIdType()); |
| 3580 | ProtocolTypeDecl = TypedefDecl::Create(*Context, TUDecl, |
| 3581 | SourceLocation(), SourceLocation(), |
| 3582 | &Context->Idents.get("Protocol"), |
| 3583 | TInfo); |
| 3584 | } |
| 3585 | return Context->getTypeDeclType(ProtocolTypeDecl); |
| 3586 | } |
| 3587 | |
| 3588 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
| 3589 | /// a synthesized/forward data reference (to the protocol's metadata). |
| 3590 | /// The forward references (and metadata) are generated in |
| 3591 | /// RewriteModernObjC::HandleTranslationUnit(). |
| 3592 | Stmt *RewriteModernObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3593 | std::string Name = "_OBJC_PROTOCOL_REFERENCE_$_" + |
Fariborz Jahanian | b31e3af | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 3594 | Exp->getProtocol()->getNameAsString(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3595 | IdentifierInfo *ID = &Context->Idents.get(Name); |
| 3596 | VarDecl *VD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 3597 | SourceLocation(), ID, getProtocolType(), |
| 3598 | nullptr, SC_Extern); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3599 | DeclRefExpr *DRE = new (Context) DeclRefExpr(VD, false, getProtocolType(), |
| 3600 | VK_LValue, SourceLocation()); |
Fariborz Jahanian | d38951a | 2013-11-22 18:43:41 +0000 | [diff] [blame] | 3601 | CastExpr *castExpr = |
| 3602 | NoTypeInfoCStyleCastExpr( |
| 3603 | Context, Context->getPointerType(DRE->getType()), CK_BitCast, DRE); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3604 | ReplaceStmt(Exp, castExpr); |
| 3605 | ProtocolExprDecls.insert(Exp->getProtocol()->getCanonicalDecl()); |
| 3606 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
| 3607 | return castExpr; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3608 | } |
| 3609 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3610 | /// IsTagDefinedInsideClass - This routine checks that a named tagged type |
| 3611 | /// is defined inside an objective-c class. If so, it returns true. |
| 3612 | bool RewriteModernObjC::IsTagDefinedInsideClass(ObjCContainerDecl *IDecl, |
Fariborz Jahanian | faded5b | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3613 | TagDecl *Tag, |
| 3614 | bool &IsNamedDefinition) { |
Fariborz Jahanian | 5979c31 | 2012-04-30 19:46:53 +0000 | [diff] [blame] | 3615 | if (!IDecl) |
| 3616 | return false; |
| 3617 | SourceLocation TagLocation; |
| 3618 | if (RecordDecl *RD = dyn_cast<RecordDecl>(Tag)) { |
| 3619 | RD = RD->getDefinition(); |
Fariborz Jahanian | faded5b | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3620 | if (!RD || !RD->getDeclName().getAsIdentifierInfo()) |
Fariborz Jahanian | 5979c31 | 2012-04-30 19:46:53 +0000 | [diff] [blame] | 3621 | return false; |
Fariborz Jahanian | faded5b | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3622 | IsNamedDefinition = true; |
Fariborz Jahanian | 5979c31 | 2012-04-30 19:46:53 +0000 | [diff] [blame] | 3623 | TagLocation = RD->getLocation(); |
| 3624 | return Context->getSourceManager().isBeforeInTranslationUnit( |
Fariborz Jahanian | faded5b | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3625 | IDecl->getLocation(), TagLocation); |
| 3626 | } |
| 3627 | if (EnumDecl *ED = dyn_cast<EnumDecl>(Tag)) { |
| 3628 | if (!ED || !ED->getDeclName().getAsIdentifierInfo()) |
| 3629 | return false; |
| 3630 | IsNamedDefinition = true; |
| 3631 | TagLocation = ED->getLocation(); |
| 3632 | return Context->getSourceManager().isBeforeInTranslationUnit( |
| 3633 | IDecl->getLocation(), TagLocation); |
Fariborz Jahanian | 5979c31 | 2012-04-30 19:46:53 +0000 | [diff] [blame] | 3634 | } |
| 3635 | return false; |
| 3636 | } |
| 3637 | |
Fariborz Jahanian | c2e2ad6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3638 | /// RewriteObjCFieldDeclType - This routine rewrites a type into the buffer. |
Fariborz Jahanian | 265a421 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3639 | /// It handles elaborated types, as well as enum types in the process. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3640 | bool RewriteModernObjC::RewriteObjCFieldDeclType(QualType &Type, |
Fariborz Jahanian | c2e2ad6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3641 | std::string &Result) { |
Fariborz Jahanian | d7c6777 | 2012-05-02 17:34:59 +0000 | [diff] [blame] | 3642 | if (isa<TypedefType>(Type)) { |
| 3643 | Result += "\t"; |
| 3644 | return false; |
| 3645 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3646 | |
Fariborz Jahanian | c2e2ad6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3647 | if (Type->isArrayType()) { |
| 3648 | QualType ElemTy = Context->getBaseElementType(Type); |
| 3649 | return RewriteObjCFieldDeclType(ElemTy, Result); |
| 3650 | } |
| 3651 | else if (Type->isRecordType()) { |
Fariborz Jahanian | 265a421 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3652 | RecordDecl *RD = Type->getAs<RecordType>()->getDecl(); |
| 3653 | if (RD->isCompleteDefinition()) { |
| 3654 | if (RD->isStruct()) |
| 3655 | Result += "\n\tstruct "; |
| 3656 | else if (RD->isUnion()) |
| 3657 | Result += "\n\tunion "; |
| 3658 | else |
| 3659 | assert(false && "class not allowed as an ivar type"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3660 | |
Fariborz Jahanian | 265a421 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3661 | Result += RD->getName(); |
Fariborz Jahanian | faded5b | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3662 | if (GlobalDefinedTags.count(RD)) { |
| 3663 | // struct/union is defined globally, use it. |
Fariborz Jahanian | c2e2ad6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3664 | Result += " "; |
| 3665 | return true; |
Fariborz Jahanian | 265a421 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3666 | } |
Fariborz Jahanian | 265a421 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3667 | Result += " {\n"; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 3668 | for (auto *FD : RD->fields()) |
Fariborz Jahanian | 265a421 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3669 | RewriteObjCFieldDecl(FD, Result); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3670 | Result += "\t} "; |
Fariborz Jahanian | c2e2ad6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3671 | return true; |
Fariborz Jahanian | 265a421 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3672 | } |
| 3673 | } |
| 3674 | else if (Type->isEnumeralType()) { |
| 3675 | EnumDecl *ED = Type->getAs<EnumType>()->getDecl(); |
| 3676 | if (ED->isCompleteDefinition()) { |
| 3677 | Result += "\n\tenum "; |
| 3678 | Result += ED->getName(); |
Fariborz Jahanian | faded5b | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3679 | if (GlobalDefinedTags.count(ED)) { |
| 3680 | // Enum is globall defined, use it. |
Fariborz Jahanian | c2e2ad6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3681 | Result += " "; |
| 3682 | return true; |
Fariborz Jahanian | 265a421 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3683 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3684 | |
Fariborz Jahanian | 265a421 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3685 | Result += " {\n"; |
Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 3686 | for (const auto *EC : ED->enumerators()) { |
Fariborz Jahanian | 265a421 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3687 | Result += "\t"; Result += EC->getName(); Result += " = "; |
| 3688 | llvm::APSInt Val = EC->getInitVal(); |
| 3689 | Result += Val.toString(10); |
| 3690 | Result += ",\n"; |
| 3691 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3692 | Result += "\t} "; |
Fariborz Jahanian | c2e2ad6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3693 | return true; |
Fariborz Jahanian | 265a421 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3694 | } |
| 3695 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3696 | |
Fariborz Jahanian | 265a421 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3697 | Result += "\t"; |
| 3698 | convertObjCTypeToCStyleType(Type); |
Fariborz Jahanian | c2e2ad6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3699 | return false; |
| 3700 | } |
| 3701 | |
| 3702 | |
| 3703 | /// RewriteObjCFieldDecl - This routine rewrites a field into the buffer. |
| 3704 | /// It handles elaborated types, as well as enum types in the process. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3705 | void RewriteModernObjC::RewriteObjCFieldDecl(FieldDecl *fieldDecl, |
Fariborz Jahanian | c2e2ad6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3706 | std::string &Result) { |
| 3707 | QualType Type = fieldDecl->getType(); |
| 3708 | std::string Name = fieldDecl->getNameAsString(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3709 | |
| 3710 | bool EleboratedType = RewriteObjCFieldDeclType(Type, Result); |
Fariborz Jahanian | c2e2ad6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3711 | if (!EleboratedType) |
| 3712 | Type.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Fariborz Jahanian | 265a421 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3713 | Result += Name; |
| 3714 | if (fieldDecl->isBitField()) { |
| 3715 | Result += " : "; Result += utostr(fieldDecl->getBitWidthValue(*Context)); |
| 3716 | } |
Fariborz Jahanian | c2e2ad6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3717 | else if (EleboratedType && Type->isArrayType()) { |
Eli Friedman | 07bab73 | 2012-12-13 01:43:21 +0000 | [diff] [blame] | 3718 | const ArrayType *AT = Context->getAsArrayType(Type); |
| 3719 | do { |
| 3720 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
Fariborz Jahanian | c2e2ad6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3721 | Result += "["; |
| 3722 | llvm::APInt Dim = CAT->getSize(); |
| 3723 | Result += utostr(Dim.getZExtValue()); |
| 3724 | Result += "]"; |
| 3725 | } |
Eli Friedman | 07bab73 | 2012-12-13 01:43:21 +0000 | [diff] [blame] | 3726 | AT = Context->getAsArrayType(AT->getElementType()); |
| 3727 | } while (AT); |
Fariborz Jahanian | c2e2ad6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3728 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3729 | |
Fariborz Jahanian | 265a421 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3730 | Result += ";\n"; |
| 3731 | } |
| 3732 | |
Fariborz Jahanian | faded5b | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3733 | /// RewriteLocallyDefinedNamedAggregates - This routine rewrites locally defined |
| 3734 | /// named aggregate types into the input buffer. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3735 | void RewriteModernObjC::RewriteLocallyDefinedNamedAggregates(FieldDecl *fieldDecl, |
Fariborz Jahanian | faded5b | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3736 | std::string &Result) { |
| 3737 | QualType Type = fieldDecl->getType(); |
Fariborz Jahanian | d7c6777 | 2012-05-02 17:34:59 +0000 | [diff] [blame] | 3738 | if (isa<TypedefType>(Type)) |
| 3739 | return; |
Fariborz Jahanian | faded5b | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3740 | if (Type->isArrayType()) |
| 3741 | Type = Context->getBaseElementType(Type); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3742 | ObjCContainerDecl *IDecl = |
Fariborz Jahanian | 144b722 | 2012-05-01 17:46:45 +0000 | [diff] [blame] | 3743 | dyn_cast<ObjCContainerDecl>(fieldDecl->getDeclContext()); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 3744 | |
| 3745 | TagDecl *TD = nullptr; |
Fariborz Jahanian | faded5b | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3746 | if (Type->isRecordType()) { |
| 3747 | TD = Type->getAs<RecordType>()->getDecl(); |
| 3748 | } |
| 3749 | else if (Type->isEnumeralType()) { |
| 3750 | TD = Type->getAs<EnumType>()->getDecl(); |
| 3751 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3752 | |
Fariborz Jahanian | faded5b | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3753 | if (TD) { |
| 3754 | if (GlobalDefinedTags.count(TD)) |
| 3755 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3756 | |
Fariborz Jahanian | faded5b | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3757 | bool IsNamedDefinition = false; |
| 3758 | if (IsTagDefinedInsideClass(IDecl, TD, IsNamedDefinition)) { |
| 3759 | RewriteObjCFieldDeclType(Type, Result); |
| 3760 | Result += ";"; |
| 3761 | } |
| 3762 | if (IsNamedDefinition) |
| 3763 | GlobalDefinedTags.insert(TD); |
| 3764 | } |
Fariborz Jahanian | faded5b | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3765 | } |
| 3766 | |
Fariborz Jahanian | 57dd66b | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 3767 | unsigned RewriteModernObjC::ObjCIvarBitfieldGroupNo(ObjCIvarDecl *IV) { |
| 3768 | const ObjCInterfaceDecl *CDecl = IV->getContainingInterface(); |
| 3769 | if (ObjCInterefaceHasBitfieldGroups.count(CDecl)) { |
| 3770 | return IvarGroupNumber[IV]; |
| 3771 | } |
| 3772 | unsigned GroupNo = 0; |
| 3773 | SmallVector<const ObjCIvarDecl *, 8> IVars; |
| 3774 | for (const ObjCIvarDecl *IVD = CDecl->all_declared_ivar_begin(); |
| 3775 | IVD; IVD = IVD->getNextIvar()) |
| 3776 | IVars.push_back(IVD); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3777 | |
Fariborz Jahanian | 57dd66b | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 3778 | for (unsigned i = 0, e = IVars.size(); i < e; i++) |
| 3779 | if (IVars[i]->isBitField()) { |
| 3780 | IvarGroupNumber[IVars[i++]] = ++GroupNo; |
| 3781 | while (i < e && IVars[i]->isBitField()) |
| 3782 | IvarGroupNumber[IVars[i++]] = GroupNo; |
| 3783 | if (i < e) |
| 3784 | --i; |
| 3785 | } |
| 3786 | |
| 3787 | ObjCInterefaceHasBitfieldGroups.insert(CDecl); |
| 3788 | return IvarGroupNumber[IV]; |
| 3789 | } |
| 3790 | |
| 3791 | QualType RewriteModernObjC::SynthesizeBitfieldGroupStructType( |
| 3792 | ObjCIvarDecl *IV, |
| 3793 | SmallVectorImpl<ObjCIvarDecl *> &IVars) { |
| 3794 | std::string StructTagName; |
| 3795 | ObjCIvarBitfieldGroupType(IV, StructTagName); |
| 3796 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, |
| 3797 | Context->getTranslationUnitDecl(), |
| 3798 | SourceLocation(), SourceLocation(), |
| 3799 | &Context->Idents.get(StructTagName)); |
| 3800 | for (unsigned i=0, e = IVars.size(); i < e; i++) { |
| 3801 | ObjCIvarDecl *Ivar = IVars[i]; |
| 3802 | RD->addDecl(FieldDecl::Create(*Context, RD, SourceLocation(), SourceLocation(), |
| 3803 | &Context->Idents.get(Ivar->getName()), |
| 3804 | Ivar->getType(), |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 3805 | nullptr, /*Expr *BW */Ivar->getBitWidth(), |
| 3806 | false, ICIS_NoInit)); |
Fariborz Jahanian | 57dd66b | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 3807 | } |
| 3808 | RD->completeDefinition(); |
| 3809 | return Context->getTagDeclType(RD); |
| 3810 | } |
| 3811 | |
| 3812 | QualType RewriteModernObjC::GetGroupRecordTypeForObjCIvarBitfield(ObjCIvarDecl *IV) { |
| 3813 | const ObjCInterfaceDecl *CDecl = IV->getContainingInterface(); |
| 3814 | unsigned GroupNo = ObjCIvarBitfieldGroupNo(IV); |
| 3815 | std::pair<const ObjCInterfaceDecl*, unsigned> tuple = std::make_pair(CDecl, GroupNo); |
| 3816 | if (GroupRecordType.count(tuple)) |
| 3817 | return GroupRecordType[tuple]; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3818 | |
Fariborz Jahanian | 57dd66b | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 3819 | SmallVector<ObjCIvarDecl *, 8> IVars; |
| 3820 | for (const ObjCIvarDecl *IVD = CDecl->all_declared_ivar_begin(); |
| 3821 | IVD; IVD = IVD->getNextIvar()) { |
| 3822 | if (IVD->isBitField()) |
| 3823 | IVars.push_back(const_cast<ObjCIvarDecl *>(IVD)); |
| 3824 | else { |
| 3825 | if (!IVars.empty()) { |
| 3826 | unsigned GroupNo = ObjCIvarBitfieldGroupNo(IVars[0]); |
| 3827 | // Generate the struct type for this group of bitfield ivars. |
| 3828 | GroupRecordType[std::make_pair(CDecl, GroupNo)] = |
| 3829 | SynthesizeBitfieldGroupStructType(IVars[0], IVars); |
| 3830 | IVars.clear(); |
| 3831 | } |
| 3832 | } |
| 3833 | } |
| 3834 | if (!IVars.empty()) { |
| 3835 | // Do the last one. |
| 3836 | unsigned GroupNo = ObjCIvarBitfieldGroupNo(IVars[0]); |
| 3837 | GroupRecordType[std::make_pair(CDecl, GroupNo)] = |
| 3838 | SynthesizeBitfieldGroupStructType(IVars[0], IVars); |
| 3839 | } |
| 3840 | QualType RetQT = GroupRecordType[tuple]; |
| 3841 | assert(!RetQT.isNull() && "GetGroupRecordTypeForObjCIvarBitfield struct type is NULL"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3842 | |
Fariborz Jahanian | 57dd66b | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 3843 | return RetQT; |
| 3844 | } |
| 3845 | |
| 3846 | /// ObjCIvarBitfieldGroupDecl - Names field decl. for ivar bitfield group. |
| 3847 | /// Name would be: classname__GRBF_n where n is the group number for this ivar. |
| 3848 | void RewriteModernObjC::ObjCIvarBitfieldGroupDecl(ObjCIvarDecl *IV, |
| 3849 | std::string &Result) { |
| 3850 | const ObjCInterfaceDecl *CDecl = IV->getContainingInterface(); |
| 3851 | Result += CDecl->getName(); |
| 3852 | Result += "__GRBF_"; |
| 3853 | unsigned GroupNo = ObjCIvarBitfieldGroupNo(IV); |
| 3854 | Result += utostr(GroupNo); |
Fariborz Jahanian | 57dd66b | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 3855 | } |
| 3856 | |
| 3857 | /// ObjCIvarBitfieldGroupType - Names struct type for ivar bitfield group. |
| 3858 | /// Name of the struct would be: classname__T_n where n is the group number for |
| 3859 | /// this ivar. |
| 3860 | void RewriteModernObjC::ObjCIvarBitfieldGroupType(ObjCIvarDecl *IV, |
| 3861 | std::string &Result) { |
| 3862 | const ObjCInterfaceDecl *CDecl = IV->getContainingInterface(); |
| 3863 | Result += CDecl->getName(); |
| 3864 | Result += "__T_"; |
| 3865 | unsigned GroupNo = ObjCIvarBitfieldGroupNo(IV); |
| 3866 | Result += utostr(GroupNo); |
Fariborz Jahanian | 57dd66b | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 3867 | } |
| 3868 | |
| 3869 | /// ObjCIvarBitfieldGroupOffset - Names symbol for ivar bitfield group field offset. |
| 3870 | /// Name would be: OBJC_IVAR_$_classname__GRBF_n where n is the group number for |
| 3871 | /// this ivar. |
| 3872 | void RewriteModernObjC::ObjCIvarBitfieldGroupOffset(ObjCIvarDecl *IV, |
| 3873 | std::string &Result) { |
| 3874 | Result += "OBJC_IVAR_$_"; |
| 3875 | ObjCIvarBitfieldGroupDecl(IV, Result); |
| 3876 | } |
| 3877 | |
| 3878 | #define SKIP_BITFIELDS(IX, ENDIX, VEC) { \ |
| 3879 | while ((IX < ENDIX) && VEC[IX]->isBitField()) \ |
| 3880 | ++IX; \ |
| 3881 | if (IX < ENDIX) \ |
| 3882 | --IX; \ |
| 3883 | } |
| 3884 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3885 | /// RewriteObjCInternalStruct - Rewrite one internal struct corresponding to |
| 3886 | /// an objective-c class with ivars. |
| 3887 | void RewriteModernObjC::RewriteObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
| 3888 | std::string &Result) { |
| 3889 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
| 3890 | assert(CDecl->getName() != "" && |
| 3891 | "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3892 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Fariborz Jahanian | cb1d9f3 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 3893 | SmallVector<ObjCIvarDecl *, 8> IVars; |
| 3894 | for (ObjCIvarDecl *IVD = CDecl->all_declared_ivar_begin(); |
Fariborz Jahanian | d7a3261 | 2012-03-06 17:16:27 +0000 | [diff] [blame] | 3895 | IVD; IVD = IVD->getNextIvar()) |
Fariborz Jahanian | cb1d9f3 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 3896 | IVars.push_back(IVD); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3897 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3898 | SourceLocation LocStart = CDecl->getBeginLoc(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3899 | SourceLocation LocEnd = CDecl->getEndOfDefinitionLoc(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3900 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3901 | const char *startBuf = SM->getCharacterData(LocStart); |
| 3902 | const char *endBuf = SM->getCharacterData(LocEnd); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3903 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3904 | // If no ivars and no root or if its root, directly or indirectly, |
| 3905 | // have no ivars (thus not synthesized) then no need to synthesize this class. |
Fariborz Jahanian | cb1d9f3 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 3906 | if ((!CDecl->isThisDeclarationADefinition() || IVars.size() == 0) && |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3907 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
| 3908 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
| 3909 | ReplaceText(LocStart, endBuf-startBuf, Result); |
| 3910 | return; |
| 3911 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3912 | |
Fariborz Jahanian | faded5b | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3913 | // Insert named struct/union definitions inside class to |
| 3914 | // outer scope. This follows semantics of locally defined |
| 3915 | // struct/unions in objective-c classes. |
| 3916 | for (unsigned i = 0, e = IVars.size(); i < e; i++) |
| 3917 | RewriteLocallyDefinedNamedAggregates(IVars[i], Result); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3918 | |
Fariborz Jahanian | 57dd66b | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 3919 | // Insert named structs which are syntheized to group ivar bitfields |
| 3920 | // to outer scope as well. |
| 3921 | for (unsigned i = 0, e = IVars.size(); i < e; i++) |
| 3922 | if (IVars[i]->isBitField()) { |
| 3923 | ObjCIvarDecl *IV = IVars[i]; |
| 3924 | QualType QT = GetGroupRecordTypeForObjCIvarBitfield(IV); |
| 3925 | RewriteObjCFieldDeclType(QT, Result); |
| 3926 | Result += ";"; |
| 3927 | // skip over ivar bitfields in this group. |
| 3928 | SKIP_BITFIELDS(i , e, IVars); |
| 3929 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3930 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3931 | Result += "\nstruct "; |
| 3932 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | cb1d9f3 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 3933 | Result += "_IMPL {\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3934 | |
Fariborz Jahanian | fdc06e3 | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 3935 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Fariborz Jahanian | cb1d9f3 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 3936 | Result += "\tstruct "; Result += RCDecl->getNameAsString(); |
| 3937 | Result += "_IMPL "; Result += RCDecl->getNameAsString(); |
| 3938 | Result += "_IVARS;\n"; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3939 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3940 | |
Fariborz Jahanian | 57dd66b | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 3941 | for (unsigned i = 0, e = IVars.size(); i < e; i++) { |
| 3942 | if (IVars[i]->isBitField()) { |
| 3943 | ObjCIvarDecl *IV = IVars[i]; |
| 3944 | Result += "\tstruct "; |
| 3945 | ObjCIvarBitfieldGroupType(IV, Result); Result += " "; |
| 3946 | ObjCIvarBitfieldGroupDecl(IV, Result); Result += ";\n"; |
| 3947 | // skip over ivar bitfields in this group. |
| 3948 | SKIP_BITFIELDS(i , e, IVars); |
| 3949 | } |
| 3950 | else |
| 3951 | RewriteObjCFieldDecl(IVars[i], Result); |
| 3952 | } |
Fariborz Jahanian | 245534d | 2012-02-12 21:36:23 +0000 | [diff] [blame] | 3953 | |
Fariborz Jahanian | cb1d9f3 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 3954 | Result += "};\n"; |
| 3955 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
| 3956 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | fdc06e3 | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 3957 | // Mark this struct as having been generated. |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 3958 | if (!ObjCSynthesizedStructs.insert(CDecl).second) |
Fariborz Jahanian | fdc06e3 | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 3959 | llvm_unreachable("struct already synthesize- RewriteObjCInternalStruct"); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3960 | } |
| 3961 | |
Fariborz Jahanian | 5e49eb9 | 2012-02-22 18:13:25 +0000 | [diff] [blame] | 3962 | /// RewriteIvarOffsetSymbols - Rewrite ivar offset symbols of those ivars which |
| 3963 | /// have been referenced in an ivar access expression. |
| 3964 | void RewriteModernObjC::RewriteIvarOffsetSymbols(ObjCInterfaceDecl *CDecl, |
| 3965 | std::string &Result) { |
| 3966 | // write out ivar offset symbols which have been referenced in an ivar |
| 3967 | // access expression. |
Mandeep Singh Grang | a2baff0 | 2017-07-06 18:49:57 +0000 | [diff] [blame] | 3968 | llvm::SmallSetVector<ObjCIvarDecl *, 8> Ivars = ReferencedIvars[CDecl]; |
| 3969 | |
Fariborz Jahanian | 5e49eb9 | 2012-02-22 18:13:25 +0000 | [diff] [blame] | 3970 | if (Ivars.empty()) |
| 3971 | return; |
Mandeep Singh Grang | a2baff0 | 2017-07-06 18:49:57 +0000 | [diff] [blame] | 3972 | |
Fariborz Jahanian | 57dd66b | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 3973 | llvm::DenseSet<std::pair<const ObjCInterfaceDecl*, unsigned> > GroupSymbolOutput; |
Craig Topper | c6914d0 | 2014-08-25 04:15:02 +0000 | [diff] [blame] | 3974 | for (ObjCIvarDecl *IvarDecl : Ivars) { |
Fariborz Jahanian | 57dd66b | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 3975 | const ObjCInterfaceDecl *IDecl = IvarDecl->getContainingInterface(); |
| 3976 | unsigned GroupNo = 0; |
| 3977 | if (IvarDecl->isBitField()) { |
| 3978 | GroupNo = ObjCIvarBitfieldGroupNo(IvarDecl); |
| 3979 | if (GroupSymbolOutput.count(std::make_pair(IDecl, GroupNo))) |
| 3980 | continue; |
| 3981 | } |
Fariborz Jahanian | e47bf2b | 2012-03-12 16:46:58 +0000 | [diff] [blame] | 3982 | Result += "\n"; |
| 3983 | if (LangOpts.MicrosoftExt) |
| 3984 | Result += "__declspec(allocate(\".objc_ivar$B\")) "; |
Fariborz Jahanian | f35e020 | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 3985 | Result += "extern \"C\" "; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3986 | if (LangOpts.MicrosoftExt && |
Fariborz Jahanian | e47bf2b | 2012-03-12 16:46:58 +0000 | [diff] [blame] | 3987 | IvarDecl->getAccessControl() != ObjCIvarDecl::Private && |
Fariborz Jahanian | f35e020 | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 3988 | IvarDecl->getAccessControl() != ObjCIvarDecl::Package) |
| 3989 | Result += "__declspec(dllimport) "; |
| 3990 | |
Fariborz Jahanian | 38c5910 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 3991 | Result += "unsigned long "; |
Fariborz Jahanian | 57dd66b | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 3992 | if (IvarDecl->isBitField()) { |
| 3993 | ObjCIvarBitfieldGroupOffset(IvarDecl, Result); |
| 3994 | GroupSymbolOutput.insert(std::make_pair(IDecl, GroupNo)); |
| 3995 | } |
| 3996 | else |
| 3997 | WriteInternalIvarName(CDecl, IvarDecl, Result); |
Fariborz Jahanian | a854174a | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 3998 | Result += ";"; |
Fariborz Jahanian | 5e49eb9 | 2012-02-22 18:13:25 +0000 | [diff] [blame] | 3999 | } |
| 4000 | } |
| 4001 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4002 | //===----------------------------------------------------------------------===// |
| 4003 | // Meta Data Emission |
| 4004 | //===----------------------------------------------------------------------===// |
| 4005 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4006 | /// RewriteImplementations - This routine rewrites all method implementations |
| 4007 | /// and emits meta-data. |
| 4008 | |
| 4009 | void RewriteModernObjC::RewriteImplementations() { |
| 4010 | int ClsDefCount = ClassImplementation.size(); |
| 4011 | int CatDefCount = CategoryImplementation.size(); |
| 4012 | |
| 4013 | // Rewrite implemented methods |
Fariborz Jahanian | 088959a | 2012-02-11 20:10:52 +0000 | [diff] [blame] | 4014 | for (int i = 0; i < ClsDefCount; i++) { |
| 4015 | ObjCImplementationDecl *OIMP = ClassImplementation[i]; |
| 4016 | ObjCInterfaceDecl *CDecl = OIMP->getClassInterface(); |
| 4017 | if (CDecl->isImplicitInterfaceDecl()) |
Fariborz Jahanian | d268b0c | 2012-02-17 22:20:12 +0000 | [diff] [blame] | 4018 | assert(false && |
| 4019 | "Legacy implicit interface rewriting not supported in moder abi"); |
Fariborz Jahanian | 088959a | 2012-02-11 20:10:52 +0000 | [diff] [blame] | 4020 | RewriteImplementationDecl(OIMP); |
| 4021 | } |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4022 | |
Fariborz Jahanian | 320c88a | 2012-02-17 20:33:00 +0000 | [diff] [blame] | 4023 | for (int i = 0; i < CatDefCount; i++) { |
| 4024 | ObjCCategoryImplDecl *CIMP = CategoryImplementation[i]; |
| 4025 | ObjCInterfaceDecl *CDecl = CIMP->getClassInterface(); |
| 4026 | if (CDecl->isImplicitInterfaceDecl()) |
| 4027 | assert(false && |
| 4028 | "Legacy implicit interface rewriting not supported in moder abi"); |
Fariborz Jahanian | 320c88a | 2012-02-17 20:33:00 +0000 | [diff] [blame] | 4029 | RewriteImplementationDecl(CIMP); |
| 4030 | } |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4031 | } |
| 4032 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4033 | void RewriteModernObjC::RewriteByRefString(std::string &ResultStr, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4034 | const std::string &Name, |
| 4035 | ValueDecl *VD, bool def) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4036 | assert(BlockByRefDeclNo.count(VD) && |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4037 | "RewriteByRefString: ByRef decl missing"); |
| 4038 | if (def) |
| 4039 | ResultStr += "struct "; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4040 | ResultStr += "__Block_byref_" + Name + |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4041 | "_" + utostr(BlockByRefDeclNo[VD]) ; |
| 4042 | } |
| 4043 | |
| 4044 | static bool HasLocalVariableExternalStorage(ValueDecl *VD) { |
| 4045 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 4046 | return (Var->isFunctionOrMethodVarDecl() && !Var->hasLocalStorage()); |
| 4047 | return false; |
| 4048 | } |
| 4049 | |
| 4050 | std::string RewriteModernObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, |
| 4051 | StringRef funcName, |
| 4052 | std::string Tag) { |
| 4053 | const FunctionType *AFT = CE->getFunctionType(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4054 | QualType RT = AFT->getReturnType(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4055 | std::string StructRef = "struct " + Tag; |
Fariborz Jahanian | b6933bc | 2012-11-06 23:25:49 +0000 | [diff] [blame] | 4056 | SourceLocation BlockLoc = CE->getExprLoc(); |
| 4057 | std::string S; |
| 4058 | ConvertSourceLocationToLineDirective(BlockLoc, S); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4059 | |
Fariborz Jahanian | b6933bc | 2012-11-06 23:25:49 +0000 | [diff] [blame] | 4060 | S += "static " + RT.getAsString(Context->getPrintingPolicy()) + " __" + |
| 4061 | funcName.str() + "_block_func_" + utostr(i); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4062 | |
| 4063 | BlockDecl *BD = CE->getBlockDecl(); |
| 4064 | |
| 4065 | if (isa<FunctionNoProtoType>(AFT)) { |
| 4066 | // No user-supplied arguments. Still need to pass in a pointer to the |
| 4067 | // block (to reference imported block decl refs). |
| 4068 | S += "(" + StructRef + " *__cself)"; |
| 4069 | } else if (BD->param_empty()) { |
| 4070 | S += "(" + StructRef + " *__cself)"; |
| 4071 | } else { |
| 4072 | const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); |
| 4073 | assert(FT && "SynthesizeBlockFunc: No function proto"); |
| 4074 | S += '('; |
| 4075 | // first add the implicit argument. |
| 4076 | S += StructRef + " *__cself, "; |
| 4077 | std::string ParamStr; |
| 4078 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 4079 | E = BD->param_end(); AI != E; ++AI) { |
| 4080 | if (AI != BD->param_begin()) S += ", "; |
| 4081 | ParamStr = (*AI)->getNameAsString(); |
| 4082 | QualType QT = (*AI)->getType(); |
Fariborz Jahanian | 835cabe | 2012-03-27 16:42:20 +0000 | [diff] [blame] | 4083 | (void)convertBlockPointerToFunctionPointer(QT); |
| 4084 | QT.getAsStringInternal(ParamStr, Context->getPrintingPolicy()); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4085 | S += ParamStr; |
| 4086 | } |
| 4087 | if (FT->isVariadic()) { |
| 4088 | if (!BD->param_empty()) S += ", "; |
| 4089 | S += "..."; |
| 4090 | } |
| 4091 | S += ')'; |
| 4092 | } |
| 4093 | S += " {\n"; |
| 4094 | |
| 4095 | // Create local declarations to avoid rewriting all closure decl ref exprs. |
| 4096 | // First, emit a declaration for all "by ref" decls. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 4097 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByRefDecls.begin(), |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4098 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 4099 | S += " "; |
| 4100 | std::string Name = (*I)->getNameAsString(); |
| 4101 | std::string TypeString; |
| 4102 | RewriteByRefString(TypeString, Name, (*I)); |
| 4103 | TypeString += " *"; |
| 4104 | Name = TypeString + Name; |
| 4105 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; |
| 4106 | } |
| 4107 | // Next, emit a declaration for all "by copy" declarations. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 4108 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByCopyDecls.begin(), |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4109 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 4110 | S += " "; |
| 4111 | // Handle nested closure invocation. For example: |
| 4112 | // |
| 4113 | // void (^myImportedClosure)(void); |
| 4114 | // myImportedClosure = ^(void) { setGlobalInt(x + y); }; |
| 4115 | // |
| 4116 | // void (^anotherClosure)(void); |
| 4117 | // anotherClosure = ^(void) { |
| 4118 | // myImportedClosure(); // import and invoke the closure |
| 4119 | // }; |
| 4120 | // |
| 4121 | if (isTopLevelBlockPointerType((*I)->getType())) { |
| 4122 | RewriteBlockPointerTypeVariable(S, (*I)); |
| 4123 | S += " = ("; |
| 4124 | RewriteBlockPointerType(S, (*I)->getType()); |
| 4125 | S += ")"; |
| 4126 | S += "__cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; |
| 4127 | } |
| 4128 | else { |
| 4129 | std::string Name = (*I)->getNameAsString(); |
| 4130 | QualType QT = (*I)->getType(); |
| 4131 | if (HasLocalVariableExternalStorage(*I)) |
| 4132 | QT = Context->getPointerType(QT); |
| 4133 | QT.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4134 | S += Name + " = __cself->" + |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4135 | (*I)->getNameAsString() + "; // bound by copy\n"; |
| 4136 | } |
| 4137 | } |
| 4138 | std::string RewrittenStr = RewrittenBlockExprs[CE]; |
| 4139 | const char *cstr = RewrittenStr.c_str(); |
| 4140 | while (*cstr++ != '{') ; |
| 4141 | S += cstr; |
| 4142 | S += "\n"; |
| 4143 | return S; |
| 4144 | } |
| 4145 | |
| 4146 | std::string RewriteModernObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
| 4147 | StringRef funcName, |
| 4148 | std::string Tag) { |
| 4149 | std::string StructRef = "struct " + Tag; |
| 4150 | std::string S = "static void __"; |
| 4151 | |
| 4152 | S += funcName; |
| 4153 | S += "_block_copy_" + utostr(i); |
| 4154 | S += "(" + StructRef; |
| 4155 | S += "*dst, " + StructRef; |
| 4156 | S += "*src) {"; |
Craig Topper | c6914d0 | 2014-08-25 04:15:02 +0000 | [diff] [blame] | 4157 | for (ValueDecl *VD : ImportedBlockDecls) { |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4158 | S += "_Block_object_assign((void*)&dst->"; |
Craig Topper | c6914d0 | 2014-08-25 04:15:02 +0000 | [diff] [blame] | 4159 | S += VD->getNameAsString(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4160 | S += ", (void*)src->"; |
Craig Topper | c6914d0 | 2014-08-25 04:15:02 +0000 | [diff] [blame] | 4161 | S += VD->getNameAsString(); |
| 4162 | if (BlockByRefDeclsPtrSet.count(VD)) |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4163 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
| 4164 | else if (VD->getType()->isBlockPointerType()) |
| 4165 | S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);"; |
| 4166 | else |
| 4167 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
| 4168 | } |
| 4169 | S += "}\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4170 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4171 | S += "\nstatic void __"; |
| 4172 | S += funcName; |
| 4173 | S += "_block_dispose_" + utostr(i); |
| 4174 | S += "(" + StructRef; |
| 4175 | S += "*src) {"; |
Craig Topper | c6914d0 | 2014-08-25 04:15:02 +0000 | [diff] [blame] | 4176 | for (ValueDecl *VD : ImportedBlockDecls) { |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4177 | S += "_Block_object_dispose((void*)src->"; |
Craig Topper | c6914d0 | 2014-08-25 04:15:02 +0000 | [diff] [blame] | 4178 | S += VD->getNameAsString(); |
| 4179 | if (BlockByRefDeclsPtrSet.count(VD)) |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4180 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
| 4181 | else if (VD->getType()->isBlockPointerType()) |
| 4182 | S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);"; |
| 4183 | else |
| 4184 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
| 4185 | } |
| 4186 | S += "}\n"; |
| 4187 | return S; |
| 4188 | } |
| 4189 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4190 | std::string RewriteModernObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4191 | std::string Desc) { |
| 4192 | std::string S = "\nstruct " + Tag; |
| 4193 | std::string Constructor = " " + Tag; |
| 4194 | |
| 4195 | S += " {\n struct __block_impl impl;\n"; |
| 4196 | S += " struct " + Desc; |
| 4197 | S += "* Desc;\n"; |
| 4198 | |
| 4199 | Constructor += "(void *fp, "; // Invoke function pointer. |
| 4200 | Constructor += "struct " + Desc; // Descriptor pointer. |
| 4201 | Constructor += " *desc"; |
| 4202 | |
| 4203 | if (BlockDeclRefs.size()) { |
| 4204 | // Output all "by copy" declarations. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 4205 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByCopyDecls.begin(), |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4206 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 4207 | S += " "; |
| 4208 | std::string FieldName = (*I)->getNameAsString(); |
| 4209 | std::string ArgName = "_" + FieldName; |
| 4210 | // Handle nested closure invocation. For example: |
| 4211 | // |
| 4212 | // void (^myImportedBlock)(void); |
| 4213 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
| 4214 | // |
| 4215 | // void (^anotherBlock)(void); |
| 4216 | // anotherBlock = ^(void) { |
| 4217 | // myImportedBlock(); // import and invoke the closure |
| 4218 | // }; |
| 4219 | // |
| 4220 | if (isTopLevelBlockPointerType((*I)->getType())) { |
| 4221 | S += "struct __block_impl *"; |
| 4222 | Constructor += ", void *" + ArgName; |
| 4223 | } else { |
| 4224 | QualType QT = (*I)->getType(); |
| 4225 | if (HasLocalVariableExternalStorage(*I)) |
| 4226 | QT = Context->getPointerType(QT); |
| 4227 | QT.getAsStringInternal(FieldName, Context->getPrintingPolicy()); |
| 4228 | QT.getAsStringInternal(ArgName, Context->getPrintingPolicy()); |
| 4229 | Constructor += ", " + ArgName; |
| 4230 | } |
| 4231 | S += FieldName + ";\n"; |
| 4232 | } |
| 4233 | // Output all "by ref" declarations. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 4234 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByRefDecls.begin(), |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4235 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 4236 | S += " "; |
| 4237 | std::string FieldName = (*I)->getNameAsString(); |
| 4238 | std::string ArgName = "_" + FieldName; |
| 4239 | { |
| 4240 | std::string TypeString; |
| 4241 | RewriteByRefString(TypeString, FieldName, (*I)); |
| 4242 | TypeString += " *"; |
| 4243 | FieldName = TypeString + FieldName; |
| 4244 | ArgName = TypeString + ArgName; |
| 4245 | Constructor += ", " + ArgName; |
| 4246 | } |
| 4247 | S += FieldName + "; // by ref\n"; |
| 4248 | } |
| 4249 | // Finish writing the constructor. |
| 4250 | Constructor += ", int flags=0)"; |
| 4251 | // Initialize all "by copy" arguments. |
| 4252 | bool firsTime = true; |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 4253 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByCopyDecls.begin(), |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4254 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 4255 | std::string Name = (*I)->getNameAsString(); |
| 4256 | if (firsTime) { |
| 4257 | Constructor += " : "; |
| 4258 | firsTime = false; |
| 4259 | } |
| 4260 | else |
| 4261 | Constructor += ", "; |
| 4262 | if (isTopLevelBlockPointerType((*I)->getType())) |
| 4263 | Constructor += Name + "((struct __block_impl *)_" + Name + ")"; |
| 4264 | else |
| 4265 | Constructor += Name + "(_" + Name + ")"; |
| 4266 | } |
| 4267 | // Initialize all "by ref" arguments. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 4268 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByRefDecls.begin(), |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4269 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 4270 | std::string Name = (*I)->getNameAsString(); |
| 4271 | if (firsTime) { |
| 4272 | Constructor += " : "; |
| 4273 | firsTime = false; |
| 4274 | } |
| 4275 | else |
| 4276 | Constructor += ", "; |
| 4277 | Constructor += Name + "(_" + Name + "->__forwarding)"; |
| 4278 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4279 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4280 | Constructor += " {\n"; |
| 4281 | if (GlobalVarDecl) |
| 4282 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 4283 | else |
| 4284 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
| 4285 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 4286 | |
| 4287 | Constructor += " Desc = desc;\n"; |
| 4288 | } else { |
| 4289 | // Finish writing the constructor. |
| 4290 | Constructor += ", int flags=0) {\n"; |
| 4291 | if (GlobalVarDecl) |
| 4292 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 4293 | else |
| 4294 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
| 4295 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 4296 | Constructor += " Desc = desc;\n"; |
| 4297 | } |
| 4298 | Constructor += " "; |
| 4299 | Constructor += "}\n"; |
| 4300 | S += Constructor; |
| 4301 | S += "};\n"; |
| 4302 | return S; |
| 4303 | } |
| 4304 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4305 | std::string RewriteModernObjC::SynthesizeBlockDescriptor(std::string DescTag, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4306 | std::string ImplTag, int i, |
| 4307 | StringRef FunName, |
| 4308 | unsigned hasCopy) { |
| 4309 | std::string S = "\nstatic struct " + DescTag; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4310 | |
Fariborz Jahanian | 2e7f638 | 2012-05-03 21:44:12 +0000 | [diff] [blame] | 4311 | S += " {\n size_t reserved;\n"; |
| 4312 | S += " size_t Block_size;\n"; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4313 | if (hasCopy) { |
| 4314 | S += " void (*copy)(struct "; |
| 4315 | S += ImplTag; S += "*, struct "; |
| 4316 | S += ImplTag; S += "*);\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4317 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4318 | S += " void (*dispose)(struct "; |
| 4319 | S += ImplTag; S += "*);\n"; |
| 4320 | } |
| 4321 | S += "} "; |
| 4322 | |
| 4323 | S += DescTag + "_DATA = { 0, sizeof(struct "; |
| 4324 | S += ImplTag + ")"; |
| 4325 | if (hasCopy) { |
| 4326 | S += ", __" + FunName.str() + "_block_copy_" + utostr(i); |
| 4327 | S += ", __" + FunName.str() + "_block_dispose_" + utostr(i); |
| 4328 | } |
| 4329 | S += "};\n"; |
| 4330 | return S; |
| 4331 | } |
| 4332 | |
| 4333 | void RewriteModernObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, |
| 4334 | StringRef FunName) { |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4335 | bool RewriteSC = (GlobalVarDecl && |
| 4336 | !Blocks.empty() && |
| 4337 | GlobalVarDecl->getStorageClass() == SC_Static && |
| 4338 | GlobalVarDecl->getType().getCVRQualifiers()); |
| 4339 | if (RewriteSC) { |
| 4340 | std::string SC(" void __"); |
| 4341 | SC += GlobalVarDecl->getNameAsString(); |
| 4342 | SC += "() {}"; |
| 4343 | InsertText(FunLocStart, SC); |
| 4344 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4345 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4346 | // Insert closures that were part of the function. |
| 4347 | for (unsigned i = 0, count=0; i < Blocks.size(); i++) { |
| 4348 | CollectBlockDeclRefInfo(Blocks[i]); |
| 4349 | // Need to copy-in the inner copied-in variables not actually used in this |
| 4350 | // block. |
| 4351 | for (int j = 0; j < InnerDeclRefsCount[i]; j++) { |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4352 | DeclRefExpr *Exp = InnerDeclRefs[count++]; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4353 | ValueDecl *VD = Exp->getDecl(); |
| 4354 | BlockDeclRefs.push_back(Exp); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4355 | if (!VD->hasAttr<BlocksAttr>()) { |
| 4356 | if (!BlockByCopyDeclsPtrSet.count(VD)) { |
| 4357 | BlockByCopyDeclsPtrSet.insert(VD); |
| 4358 | BlockByCopyDecls.push_back(VD); |
| 4359 | } |
| 4360 | continue; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4361 | } |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4362 | |
| 4363 | if (!BlockByRefDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4364 | BlockByRefDeclsPtrSet.insert(VD); |
| 4365 | BlockByRefDecls.push_back(VD); |
| 4366 | } |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4367 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4368 | // imported objects in the inner blocks not used in the outer |
| 4369 | // blocks must be copied/disposed in the outer block as well. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4370 | if (VD->getType()->isObjCObjectPointerType() || |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4371 | VD->getType()->isBlockPointerType()) |
| 4372 | ImportedBlockDecls.insert(VD); |
| 4373 | } |
| 4374 | |
| 4375 | std::string ImplTag = "__" + FunName.str() + "_block_impl_" + utostr(i); |
| 4376 | std::string DescTag = "__" + FunName.str() + "_block_desc_" + utostr(i); |
| 4377 | |
| 4378 | std::string CI = SynthesizeBlockImpl(Blocks[i], ImplTag, DescTag); |
| 4379 | |
| 4380 | InsertText(FunLocStart, CI); |
| 4381 | |
| 4382 | std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, ImplTag); |
| 4383 | |
| 4384 | InsertText(FunLocStart, CF); |
| 4385 | |
| 4386 | if (ImportedBlockDecls.size()) { |
| 4387 | std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, ImplTag); |
| 4388 | InsertText(FunLocStart, HF); |
| 4389 | } |
| 4390 | std::string BD = SynthesizeBlockDescriptor(DescTag, ImplTag, i, FunName, |
| 4391 | ImportedBlockDecls.size() > 0); |
| 4392 | InsertText(FunLocStart, BD); |
| 4393 | |
| 4394 | BlockDeclRefs.clear(); |
| 4395 | BlockByRefDecls.clear(); |
| 4396 | BlockByRefDeclsPtrSet.clear(); |
| 4397 | BlockByCopyDecls.clear(); |
| 4398 | BlockByCopyDeclsPtrSet.clear(); |
| 4399 | ImportedBlockDecls.clear(); |
| 4400 | } |
| 4401 | if (RewriteSC) { |
| 4402 | // Must insert any 'const/volatile/static here. Since it has been |
| 4403 | // removed as result of rewriting of block literals. |
| 4404 | std::string SC; |
| 4405 | if (GlobalVarDecl->getStorageClass() == SC_Static) |
| 4406 | SC = "static "; |
| 4407 | if (GlobalVarDecl->getType().isConstQualified()) |
| 4408 | SC += "const "; |
| 4409 | if (GlobalVarDecl->getType().isVolatileQualified()) |
| 4410 | SC += "volatile "; |
| 4411 | if (GlobalVarDecl->getType().isRestrictQualified()) |
| 4412 | SC += "restrict "; |
| 4413 | InsertText(FunLocStart, SC); |
| 4414 | } |
Fariborz Jahanian | e005070 | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 4415 | if (GlobalConstructionExp) { |
| 4416 | // extra fancy dance for global literal expression. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4417 | |
Fariborz Jahanian | e005070 | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 4418 | // Always the latest block expression on the block stack. |
| 4419 | std::string Tag = "__"; |
| 4420 | Tag += FunName; |
| 4421 | Tag += "_block_impl_"; |
| 4422 | Tag += utostr(Blocks.size()-1); |
| 4423 | std::string globalBuf = "static "; |
| 4424 | globalBuf += Tag; globalBuf += " "; |
| 4425 | std::string SStr; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4426 | |
Fariborz Jahanian | e005070 | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 4427 | llvm::raw_string_ostream constructorExprBuf(SStr); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 4428 | GlobalConstructionExp->printPretty(constructorExprBuf, nullptr, |
| 4429 | PrintingPolicy(LangOpts)); |
Fariborz Jahanian | e005070 | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 4430 | globalBuf += constructorExprBuf.str(); |
| 4431 | globalBuf += ";\n"; |
| 4432 | InsertText(FunLocStart, globalBuf); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 4433 | GlobalConstructionExp = nullptr; |
Fariborz Jahanian | e005070 | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 4434 | } |
| 4435 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4436 | Blocks.clear(); |
| 4437 | InnerDeclRefsCount.clear(); |
| 4438 | InnerDeclRefs.clear(); |
| 4439 | RewrittenBlockExprs.clear(); |
| 4440 | } |
| 4441 | |
| 4442 | void RewriteModernObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4443 | SourceLocation FunLocStart = |
Fariborz Jahanian | e49a42c | 2012-04-25 17:56:48 +0000 | [diff] [blame] | 4444 | (!Blocks.empty()) ? getFunctionSourceLocation(*this, FD) |
| 4445 | : FD->getTypeSpecStartLoc(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4446 | StringRef FuncName = FD->getName(); |
| 4447 | |
| 4448 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 4449 | } |
| 4450 | |
| 4451 | static void BuildUniqueMethodName(std::string &Name, |
| 4452 | ObjCMethodDecl *MD) { |
| 4453 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
| 4454 | Name = IFace->getName(); |
| 4455 | Name += "__" + MD->getSelector().getAsString(); |
| 4456 | // Convert colons to underscores. |
| 4457 | std::string::size_type loc = 0; |
Sylvestre Ledru | d8650cd | 2017-01-28 13:36:34 +0000 | [diff] [blame] | 4458 | while ((loc = Name.find(':', loc)) != std::string::npos) |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4459 | Name.replace(loc, 1, "_"); |
| 4460 | } |
| 4461 | |
| 4462 | void RewriteModernObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4463 | // fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n"); |
| 4464 | // SourceLocation FunLocStart = MD->getBeginLoc(); |
| 4465 | SourceLocation FunLocStart = MD->getBeginLoc(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4466 | std::string FuncName; |
| 4467 | BuildUniqueMethodName(FuncName, MD); |
| 4468 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 4469 | } |
| 4470 | |
| 4471 | void RewriteModernObjC::GetBlockDeclRefExprs(Stmt *S) { |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 4472 | for (Stmt *SubStmt : S->children()) |
| 4473 | if (SubStmt) { |
| 4474 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(SubStmt)) |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4475 | GetBlockDeclRefExprs(CBE->getBody()); |
| 4476 | else |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 4477 | GetBlockDeclRefExprs(SubStmt); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4478 | } |
| 4479 | // Handle specific things. |
Alexey Bataev | f841bd9 | 2014-12-16 07:00:22 +0000 | [diff] [blame] | 4480 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) |
Alexey Bataev | 19acc3d | 2015-01-12 10:17:46 +0000 | [diff] [blame] | 4481 | if (DRE->refersToEnclosingVariableOrCapture() || |
Alexey Bataev | f841bd9 | 2014-12-16 07:00:22 +0000 | [diff] [blame] | 4482 | HasLocalVariableExternalStorage(DRE->getDecl())) |
Fariborz Jahanian | 35f6e12 | 2012-04-16 23:00:57 +0000 | [diff] [blame] | 4483 | // FIXME: Handle enums. |
Alexey Bataev | f841bd9 | 2014-12-16 07:00:22 +0000 | [diff] [blame] | 4484 | BlockDeclRefs.push_back(DRE); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4485 | } |
| 4486 | |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 4487 | void RewriteModernObjC::GetInnerBlockDeclRefExprs(Stmt *S, |
| 4488 | SmallVectorImpl<DeclRefExpr *> &InnerBlockDeclRefs, |
Craig Topper | 4dd9b43 | 2014-08-17 23:49:53 +0000 | [diff] [blame] | 4489 | llvm::SmallPtrSetImpl<const DeclContext *> &InnerContexts) { |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 4490 | for (Stmt *SubStmt : S->children()) |
| 4491 | if (SubStmt) { |
| 4492 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(SubStmt)) { |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4493 | InnerContexts.insert(cast<DeclContext>(CBE->getBlockDecl())); |
| 4494 | GetInnerBlockDeclRefExprs(CBE->getBody(), |
| 4495 | InnerBlockDeclRefs, |
| 4496 | InnerContexts); |
| 4497 | } |
| 4498 | else |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 4499 | GetInnerBlockDeclRefExprs(SubStmt, InnerBlockDeclRefs, InnerContexts); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4500 | } |
| 4501 | // Handle specific things. |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4502 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
Alexey Bataev | 19acc3d | 2015-01-12 10:17:46 +0000 | [diff] [blame] | 4503 | if (DRE->refersToEnclosingVariableOrCapture() || |
Alexey Bataev | f841bd9 | 2014-12-16 07:00:22 +0000 | [diff] [blame] | 4504 | HasLocalVariableExternalStorage(DRE->getDecl())) { |
| 4505 | if (!InnerContexts.count(DRE->getDecl()->getDeclContext())) |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4506 | InnerBlockDeclRefs.push_back(DRE); |
Alexey Bataev | f841bd9 | 2014-12-16 07:00:22 +0000 | [diff] [blame] | 4507 | if (VarDecl *Var = cast<VarDecl>(DRE->getDecl())) |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4508 | if (Var->isFunctionOrMethodVarDecl()) |
| 4509 | ImportedLocalExternalDecls.insert(Var); |
| 4510 | } |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4511 | } |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4512 | } |
| 4513 | |
Fariborz Jahanian | c3cdc41 | 2012-02-13 18:57:49 +0000 | [diff] [blame] | 4514 | /// convertObjCTypeToCStyleType - This routine converts such objc types |
| 4515 | /// as qualified objects, and blocks to their closest c/c++ types that |
| 4516 | /// it can. It returns true if input type was modified. |
| 4517 | bool RewriteModernObjC::convertObjCTypeToCStyleType(QualType &T) { |
| 4518 | QualType oldT = T; |
| 4519 | convertBlockPointerToFunctionPointer(T); |
| 4520 | if (T->isFunctionPointerType()) { |
| 4521 | QualType PointeeTy; |
| 4522 | if (const PointerType* PT = T->getAs<PointerType>()) { |
| 4523 | PointeeTy = PT->getPointeeType(); |
| 4524 | if (const FunctionType *FT = PointeeTy->getAs<FunctionType>()) { |
| 4525 | T = convertFunctionTypeOfBlocks(FT); |
| 4526 | T = Context->getPointerType(T); |
| 4527 | } |
| 4528 | } |
| 4529 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4530 | |
Fariborz Jahanian | c3cdc41 | 2012-02-13 18:57:49 +0000 | [diff] [blame] | 4531 | convertToUnqualifiedObjCType(T); |
| 4532 | return T != oldT; |
| 4533 | } |
| 4534 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4535 | /// convertFunctionTypeOfBlocks - This routine converts a function type |
| 4536 | /// whose result type may be a block pointer or whose argument type(s) |
| 4537 | /// might be block pointers to an equivalent function type replacing |
| 4538 | /// all block pointers to function pointers. |
| 4539 | QualType RewriteModernObjC::convertFunctionTypeOfBlocks(const FunctionType *FT) { |
| 4540 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
| 4541 | // FTP will be null for closures that don't take arguments. |
| 4542 | // Generate a funky cast. |
| 4543 | SmallVector<QualType, 8> ArgTypes; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4544 | QualType Res = FT->getReturnType(); |
Fariborz Jahanian | c3cdc41 | 2012-02-13 18:57:49 +0000 | [diff] [blame] | 4545 | bool modified = convertObjCTypeToCStyleType(Res); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4546 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4547 | if (FTP) { |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 4548 | for (auto &I : FTP->param_types()) { |
| 4549 | QualType t = I; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4550 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | c3cdc41 | 2012-02-13 18:57:49 +0000 | [diff] [blame] | 4551 | if (convertObjCTypeToCStyleType(t)) |
| 4552 | modified = true; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4553 | ArgTypes.push_back(t); |
| 4554 | } |
| 4555 | } |
| 4556 | QualType FuncType; |
Fariborz Jahanian | c3cdc41 | 2012-02-13 18:57:49 +0000 | [diff] [blame] | 4557 | if (modified) |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 4558 | FuncType = getSimpleFunctionType(Res, ArgTypes); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4559 | else FuncType = QualType(FT, 0); |
| 4560 | return FuncType; |
| 4561 | } |
| 4562 | |
| 4563 | Stmt *RewriteModernObjC::SynthesizeBlockCall(CallExpr *Exp, const Expr *BlockExp) { |
| 4564 | // Navigate to relevant type information. |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 4565 | const BlockPointerType *CPT = nullptr; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4566 | |
| 4567 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BlockExp)) { |
| 4568 | CPT = DRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4569 | } else if (const MemberExpr *MExpr = dyn_cast<MemberExpr>(BlockExp)) { |
| 4570 | CPT = MExpr->getType()->getAs<BlockPointerType>(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4571 | } |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4572 | else if (const ParenExpr *PRE = dyn_cast<ParenExpr>(BlockExp)) { |
| 4573 | return SynthesizeBlockCall(Exp, PRE->getSubExpr()); |
| 4574 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4575 | else if (const ImplicitCastExpr *IEXPR = dyn_cast<ImplicitCastExpr>(BlockExp)) |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4576 | CPT = IEXPR->getType()->getAs<BlockPointerType>(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4577 | else if (const ConditionalOperator *CEXPR = |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4578 | dyn_cast<ConditionalOperator>(BlockExp)) { |
| 4579 | Expr *LHSExp = CEXPR->getLHS(); |
| 4580 | Stmt *LHSStmt = SynthesizeBlockCall(Exp, LHSExp); |
| 4581 | Expr *RHSExp = CEXPR->getRHS(); |
| 4582 | Stmt *RHSStmt = SynthesizeBlockCall(Exp, RHSExp); |
| 4583 | Expr *CONDExp = CEXPR->getCond(); |
| 4584 | ConditionalOperator *CondExpr = |
| 4585 | new (Context) ConditionalOperator(CONDExp, |
| 4586 | SourceLocation(), cast<Expr>(LHSStmt), |
| 4587 | SourceLocation(), cast<Expr>(RHSStmt), |
| 4588 | Exp->getType(), VK_RValue, OK_Ordinary); |
| 4589 | return CondExpr; |
| 4590 | } else if (const ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(BlockExp)) { |
| 4591 | CPT = IRE->getType()->getAs<BlockPointerType>(); |
| 4592 | } else if (const PseudoObjectExpr *POE |
| 4593 | = dyn_cast<PseudoObjectExpr>(BlockExp)) { |
| 4594 | CPT = POE->getType()->castAs<BlockPointerType>(); |
| 4595 | } else { |
Craig Topper | 0da2076 | 2016-04-24 02:08:22 +0000 | [diff] [blame] | 4596 | assert(false && "RewriteBlockClass: Bad type"); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4597 | } |
| 4598 | assert(CPT && "RewriteBlockClass: Bad type"); |
| 4599 | const FunctionType *FT = CPT->getPointeeType()->getAs<FunctionType>(); |
| 4600 | assert(FT && "RewriteBlockClass: Bad type"); |
| 4601 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
| 4602 | // FTP will be null for closures that don't take arguments. |
| 4603 | |
| 4604 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 4605 | SourceLocation(), SourceLocation(), |
| 4606 | &Context->Idents.get("__block_impl")); |
| 4607 | QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD)); |
| 4608 | |
| 4609 | // Generate a funky cast. |
| 4610 | SmallVector<QualType, 8> ArgTypes; |
| 4611 | |
| 4612 | // Push the block argument type. |
| 4613 | ArgTypes.push_back(PtrBlock); |
| 4614 | if (FTP) { |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 4615 | for (auto &I : FTP->param_types()) { |
| 4616 | QualType t = I; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4617 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
| 4618 | if (!convertBlockPointerToFunctionPointer(t)) |
| 4619 | convertToUnqualifiedObjCType(t); |
| 4620 | ArgTypes.push_back(t); |
| 4621 | } |
| 4622 | } |
| 4623 | // Now do the pointer to function cast. |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 4624 | QualType PtrToFuncCastType = getSimpleFunctionType(Exp->getType(), ArgTypes); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4625 | |
| 4626 | PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); |
| 4627 | |
| 4628 | CastExpr *BlkCast = NoTypeInfoCStyleCastExpr(Context, PtrBlock, |
| 4629 | CK_BitCast, |
| 4630 | const_cast<Expr*>(BlockExp)); |
| 4631 | // Don't forget the parens to enforce the proper binding. |
| 4632 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 4633 | BlkCast); |
| 4634 | //PE->dump(); |
| 4635 | |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 4636 | FieldDecl *FD = FieldDecl::Create(*Context, nullptr, SourceLocation(), |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4637 | SourceLocation(), |
| 4638 | &Context->Idents.get("FuncPtr"), |
Fariborz Jahanian | f8e68e2 | 2015-04-09 18:36:50 +0000 | [diff] [blame] | 4639 | Context->VoidPtrTy, nullptr, |
| 4640 | /*BitWidth=*/nullptr, /*Mutable=*/true, |
| 4641 | ICIS_NoInit); |
| 4642 | MemberExpr *ME = |
| 4643 | new (Context) MemberExpr(PE, true, SourceLocation(), FD, SourceLocation(), |
| 4644 | FD->getType(), VK_LValue, OK_Ordinary); |
| 4645 | |
| 4646 | CastExpr *FunkCast = NoTypeInfoCStyleCastExpr(Context, PtrToFuncCastType, |
| 4647 | CK_BitCast, ME); |
| 4648 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4649 | |
| 4650 | SmallVector<Expr*, 8> BlkExprs; |
| 4651 | // Add the implicit argument. |
| 4652 | BlkExprs.push_back(BlkCast); |
| 4653 | // Add the user arguments. |
| 4654 | for (CallExpr::arg_iterator I = Exp->arg_begin(), |
| 4655 | E = Exp->arg_end(); I != E; ++I) { |
| 4656 | BlkExprs.push_back(*I); |
| 4657 | } |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 4658 | CallExpr *CE = new (Context) CallExpr(*Context, PE, BlkExprs, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4659 | Exp->getType(), VK_RValue, |
| 4660 | SourceLocation()); |
| 4661 | return CE; |
| 4662 | } |
| 4663 | |
| 4664 | // We need to return the rewritten expression to handle cases where the |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4665 | // DeclRefExpr is embedded in another expression being rewritten. |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4666 | // For example: |
| 4667 | // |
| 4668 | // int main() { |
| 4669 | // __block Foo *f; |
| 4670 | // __block int i; |
| 4671 | // |
| 4672 | // void (^myblock)() = ^() { |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4673 | // [f test]; // f is a DeclRefExpr embedded in a message (which is being rewritten). |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4674 | // i = 77; |
| 4675 | // }; |
| 4676 | //} |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4677 | Stmt *RewriteModernObjC::RewriteBlockDeclRefExpr(DeclRefExpr *DeclRefExp) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4678 | // Rewrite the byref variable into BYREFVAR->__forwarding->BYREFVAR |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4679 | // for each DeclRefExp where BYREFVAR is name of the variable. |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4680 | ValueDecl *VD = DeclRefExp->getDecl(); |
Alexey Bataev | 19acc3d | 2015-01-12 10:17:46 +0000 | [diff] [blame] | 4681 | bool isArrow = DeclRefExp->refersToEnclosingVariableOrCapture() || |
Alexey Bataev | f841bd9 | 2014-12-16 07:00:22 +0000 | [diff] [blame] | 4682 | HasLocalVariableExternalStorage(DeclRefExp->getDecl()); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 4683 | |
| 4684 | FieldDecl *FD = FieldDecl::Create(*Context, nullptr, SourceLocation(), |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4685 | SourceLocation(), |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4686 | &Context->Idents.get("__forwarding"), |
Fariborz Jahanian | f8e68e2 | 2015-04-09 18:36:50 +0000 | [diff] [blame] | 4687 | Context->VoidPtrTy, nullptr, |
| 4688 | /*BitWidth=*/nullptr, /*Mutable=*/true, |
| 4689 | ICIS_NoInit); |
| 4690 | MemberExpr *ME = new (Context) |
| 4691 | MemberExpr(DeclRefExp, isArrow, SourceLocation(), FD, SourceLocation(), |
| 4692 | FD->getType(), VK_LValue, OK_Ordinary); |
| 4693 | |
| 4694 | StringRef Name = VD->getName(); |
| 4695 | FD = FieldDecl::Create(*Context, nullptr, SourceLocation(), SourceLocation(), |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4696 | &Context->Idents.get(Name), |
Fariborz Jahanian | f8e68e2 | 2015-04-09 18:36:50 +0000 | [diff] [blame] | 4697 | Context->VoidPtrTy, nullptr, |
| 4698 | /*BitWidth=*/nullptr, /*Mutable=*/true, |
| 4699 | ICIS_NoInit); |
| 4700 | ME = |
| 4701 | new (Context) MemberExpr(ME, true, SourceLocation(), FD, SourceLocation(), |
| 4702 | DeclRefExp->getType(), VK_LValue, OK_Ordinary); |
| 4703 | |
| 4704 | // Need parens to enforce precedence. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4705 | ParenExpr *PE = new (Context) ParenExpr(DeclRefExp->getExprLoc(), |
| 4706 | DeclRefExp->getExprLoc(), |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4707 | ME); |
| 4708 | ReplaceStmt(DeclRefExp, PE); |
| 4709 | return PE; |
| 4710 | } |
| 4711 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4712 | // Rewrites the imported local variable V with external storage |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4713 | // (static, extern, etc.) as *V |
| 4714 | // |
| 4715 | Stmt *RewriteModernObjC::RewriteLocalVariableExternalStorage(DeclRefExpr *DRE) { |
| 4716 | ValueDecl *VD = DRE->getDecl(); |
| 4717 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 4718 | if (!ImportedLocalExternalDecls.count(Var)) |
| 4719 | return DRE; |
| 4720 | Expr *Exp = new (Context) UnaryOperator(DRE, UO_Deref, DRE->getType(), |
| 4721 | VK_LValue, OK_Ordinary, |
Aaron Ballman | a503855 | 2018-01-09 13:07:03 +0000 | [diff] [blame] | 4722 | DRE->getLocation(), false); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4723 | // Need parens to enforce precedence. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4724 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4725 | Exp); |
| 4726 | ReplaceStmt(DRE, PE); |
| 4727 | return PE; |
| 4728 | } |
| 4729 | |
| 4730 | void RewriteModernObjC::RewriteCastExpr(CStyleCastExpr *CE) { |
| 4731 | SourceLocation LocStart = CE->getLParenLoc(); |
| 4732 | SourceLocation LocEnd = CE->getRParenLoc(); |
| 4733 | |
| 4734 | // Need to avoid trying to rewrite synthesized casts. |
| 4735 | if (LocStart.isInvalid()) |
| 4736 | return; |
| 4737 | // Need to avoid trying to rewrite casts contained in macros. |
| 4738 | if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) |
| 4739 | return; |
| 4740 | |
| 4741 | const char *startBuf = SM->getCharacterData(LocStart); |
| 4742 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 4743 | QualType QT = CE->getType(); |
| 4744 | const Type* TypePtr = QT->getAs<Type>(); |
| 4745 | if (isa<TypeOfExprType>(TypePtr)) { |
| 4746 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 4747 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 4748 | std::string TypeAsString = "("; |
| 4749 | RewriteBlockPointerType(TypeAsString, QT); |
| 4750 | TypeAsString += ")"; |
| 4751 | ReplaceText(LocStart, endBuf-startBuf+1, TypeAsString); |
| 4752 | return; |
| 4753 | } |
| 4754 | // advance the location to startArgList. |
| 4755 | const char *argPtr = startBuf; |
| 4756 | |
| 4757 | while (*argPtr++ && (argPtr < endBuf)) { |
| 4758 | switch (*argPtr) { |
| 4759 | case '^': |
| 4760 | // Replace the '^' with '*'. |
| 4761 | LocStart = LocStart.getLocWithOffset(argPtr-startBuf); |
| 4762 | ReplaceText(LocStart, 1, "*"); |
| 4763 | break; |
| 4764 | } |
| 4765 | } |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4766 | } |
| 4767 | |
Fariborz Jahanian | 2c00acd | 2012-04-10 00:08:18 +0000 | [diff] [blame] | 4768 | void RewriteModernObjC::RewriteImplicitCastObjCExpr(CastExpr *IC) { |
| 4769 | CastKind CastKind = IC->getCastKind(); |
Fariborz Jahanian | cc17228 | 2012-04-16 22:14:01 +0000 | [diff] [blame] | 4770 | if (CastKind != CK_BlockPointerToObjCPointerCast && |
| 4771 | CastKind != CK_AnyPointerToBlockPointerCast) |
| 4772 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4773 | |
Fariborz Jahanian | cc17228 | 2012-04-16 22:14:01 +0000 | [diff] [blame] | 4774 | QualType QT = IC->getType(); |
| 4775 | (void)convertBlockPointerToFunctionPointer(QT); |
| 4776 | std::string TypeString(QT.getAsString(Context->getPrintingPolicy())); |
| 4777 | std::string Str = "("; |
| 4778 | Str += TypeString; |
| 4779 | Str += ")"; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4780 | InsertText(IC->getSubExpr()->getBeginLoc(), Str); |
Fariborz Jahanian | 2c00acd | 2012-04-10 00:08:18 +0000 | [diff] [blame] | 4781 | } |
| 4782 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4783 | void RewriteModernObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { |
| 4784 | SourceLocation DeclLoc = FD->getLocation(); |
| 4785 | unsigned parenCount = 0; |
| 4786 | |
| 4787 | // We have 1 or more arguments that have closure pointers. |
| 4788 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4789 | const char *startArgList = strchr(startBuf, '('); |
| 4790 | |
| 4791 | assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); |
| 4792 | |
| 4793 | parenCount++; |
| 4794 | // advance the location to startArgList. |
| 4795 | DeclLoc = DeclLoc.getLocWithOffset(startArgList-startBuf); |
| 4796 | assert((DeclLoc.isValid()) && "Invalid DeclLoc"); |
| 4797 | |
| 4798 | const char *argPtr = startArgList; |
| 4799 | |
| 4800 | while (*argPtr++ && parenCount) { |
| 4801 | switch (*argPtr) { |
| 4802 | case '^': |
| 4803 | // Replace the '^' with '*'. |
| 4804 | DeclLoc = DeclLoc.getLocWithOffset(argPtr-startArgList); |
| 4805 | ReplaceText(DeclLoc, 1, "*"); |
| 4806 | break; |
| 4807 | case '(': |
| 4808 | parenCount++; |
| 4809 | break; |
| 4810 | case ')': |
| 4811 | parenCount--; |
| 4812 | break; |
| 4813 | } |
| 4814 | } |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4815 | } |
| 4816 | |
| 4817 | bool RewriteModernObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { |
| 4818 | const FunctionProtoType *FTP; |
| 4819 | const PointerType *PT = QT->getAs<PointerType>(); |
| 4820 | if (PT) { |
| 4821 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4822 | } else { |
| 4823 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
| 4824 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
| 4825 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4826 | } |
| 4827 | if (FTP) { |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 4828 | for (const auto &I : FTP->param_types()) |
| 4829 | if (isTopLevelBlockPointerType(I)) |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4830 | return true; |
| 4831 | } |
| 4832 | return false; |
| 4833 | } |
| 4834 | |
| 4835 | bool RewriteModernObjC::PointerTypeTakesAnyObjCQualifiedType(QualType QT) { |
| 4836 | const FunctionProtoType *FTP; |
| 4837 | const PointerType *PT = QT->getAs<PointerType>(); |
| 4838 | if (PT) { |
| 4839 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4840 | } else { |
| 4841 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
| 4842 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
| 4843 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4844 | } |
| 4845 | if (FTP) { |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 4846 | for (const auto &I : FTP->param_types()) { |
| 4847 | if (I->isObjCQualifiedIdType()) |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4848 | return true; |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 4849 | if (I->isObjCObjectPointerType() && |
| 4850 | I->getPointeeType()->isObjCQualifiedInterfaceType()) |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4851 | return true; |
| 4852 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4853 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4854 | } |
| 4855 | return false; |
| 4856 | } |
| 4857 | |
| 4858 | void RewriteModernObjC::GetExtentOfArgList(const char *Name, const char *&LParen, |
| 4859 | const char *&RParen) { |
| 4860 | const char *argPtr = strchr(Name, '('); |
| 4861 | assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); |
| 4862 | |
| 4863 | LParen = argPtr; // output the start. |
| 4864 | argPtr++; // skip past the left paren. |
| 4865 | unsigned parenCount = 1; |
| 4866 | |
| 4867 | while (*argPtr && parenCount) { |
| 4868 | switch (*argPtr) { |
| 4869 | case '(': parenCount++; break; |
| 4870 | case ')': parenCount--; break; |
| 4871 | default: break; |
| 4872 | } |
| 4873 | if (parenCount) argPtr++; |
| 4874 | } |
| 4875 | assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); |
| 4876 | RParen = argPtr; // output the end |
| 4877 | } |
| 4878 | |
| 4879 | void RewriteModernObjC::RewriteBlockPointerDecl(NamedDecl *ND) { |
| 4880 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 4881 | RewriteBlockPointerFunctionArgs(FD); |
| 4882 | return; |
| 4883 | } |
| 4884 | // Handle Variables and Typedefs. |
| 4885 | SourceLocation DeclLoc = ND->getLocation(); |
| 4886 | QualType DeclT; |
| 4887 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4888 | DeclT = VD->getType(); |
| 4889 | else if (TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(ND)) |
| 4890 | DeclT = TDD->getUnderlyingType(); |
| 4891 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) |
| 4892 | DeclT = FD->getType(); |
| 4893 | else |
| 4894 | llvm_unreachable("RewriteBlockPointerDecl(): Decl type not yet handled"); |
| 4895 | |
| 4896 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4897 | const char *endBuf = startBuf; |
| 4898 | // scan backward (from the decl location) for the end of the previous decl. |
| 4899 | while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) |
| 4900 | startBuf--; |
| 4901 | SourceLocation Start = DeclLoc.getLocWithOffset(startBuf-endBuf); |
| 4902 | std::string buf; |
| 4903 | unsigned OrigLength=0; |
| 4904 | // *startBuf != '^' if we are dealing with a pointer to function that |
| 4905 | // may take block argument types (which will be handled below). |
| 4906 | if (*startBuf == '^') { |
| 4907 | // Replace the '^' with '*', computing a negative offset. |
| 4908 | buf = '*'; |
| 4909 | startBuf++; |
| 4910 | OrigLength++; |
| 4911 | } |
| 4912 | while (*startBuf != ')') { |
| 4913 | buf += *startBuf; |
| 4914 | startBuf++; |
| 4915 | OrigLength++; |
| 4916 | } |
| 4917 | buf += ')'; |
| 4918 | OrigLength++; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4919 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4920 | if (PointerTypeTakesAnyBlockArguments(DeclT) || |
| 4921 | PointerTypeTakesAnyObjCQualifiedType(DeclT)) { |
| 4922 | // Replace the '^' with '*' for arguments. |
| 4923 | // Replace id<P> with id/*<>*/ |
| 4924 | DeclLoc = ND->getLocation(); |
| 4925 | startBuf = SM->getCharacterData(DeclLoc); |
| 4926 | const char *argListBegin, *argListEnd; |
| 4927 | GetExtentOfArgList(startBuf, argListBegin, argListEnd); |
| 4928 | while (argListBegin < argListEnd) { |
| 4929 | if (*argListBegin == '^') |
| 4930 | buf += '*'; |
| 4931 | else if (*argListBegin == '<') { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4932 | buf += "/*"; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4933 | buf += *argListBegin++; |
Dmitri Gribenko | 76bb5cabfa | 2012-09-10 21:20:09 +0000 | [diff] [blame] | 4934 | OrigLength++; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4935 | while (*argListBegin != '>') { |
| 4936 | buf += *argListBegin++; |
| 4937 | OrigLength++; |
| 4938 | } |
| 4939 | buf += *argListBegin; |
| 4940 | buf += "*/"; |
| 4941 | } |
| 4942 | else |
| 4943 | buf += *argListBegin; |
| 4944 | argListBegin++; |
| 4945 | OrigLength++; |
| 4946 | } |
| 4947 | buf += ')'; |
| 4948 | OrigLength++; |
| 4949 | } |
| 4950 | ReplaceText(Start, OrigLength, buf); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4951 | } |
| 4952 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4953 | /// SynthesizeByrefCopyDestroyHelper - This routine synthesizes: |
| 4954 | /// void __Block_byref_id_object_copy(struct Block_byref_id_object *dst, |
| 4955 | /// struct Block_byref_id_object *src) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4956 | /// _Block_object_assign (&_dest->object, _src->object, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4957 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4958 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4959 | /// _Block_object_assign(&_dest->object, _src->object, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4960 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4961 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4962 | /// } |
| 4963 | /// And: |
| 4964 | /// void __Block_byref_id_object_dispose(struct Block_byref_id_object *_src) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4965 | /// _Block_object_dispose(_src->object, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4966 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4967 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4968 | /// _Block_object_dispose(_src->object, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4969 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4970 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4971 | /// } |
| 4972 | |
| 4973 | std::string RewriteModernObjC::SynthesizeByrefCopyDestroyHelper(VarDecl *VD, |
| 4974 | int flag) { |
| 4975 | std::string S; |
| 4976 | if (CopyDestroyCache.count(flag)) |
| 4977 | return S; |
| 4978 | CopyDestroyCache.insert(flag); |
| 4979 | S = "static void __Block_byref_id_object_copy_"; |
| 4980 | S += utostr(flag); |
| 4981 | S += "(void *dst, void *src) {\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4982 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4983 | // offset into the object pointer is computed as: |
| 4984 | // void * + void* + int + int + void* + void * |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4985 | unsigned IntSize = |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4986 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4987 | unsigned VoidPtrSize = |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4988 | static_cast<unsigned>(Context->getTypeSize(Context->VoidPtrTy)); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4989 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4990 | unsigned offset = (VoidPtrSize*4 + IntSize + IntSize)/Context->getCharWidth(); |
| 4991 | S += " _Block_object_assign((char*)dst + "; |
| 4992 | S += utostr(offset); |
| 4993 | S += ", *(void * *) ((char*)src + "; |
| 4994 | S += utostr(offset); |
| 4995 | S += "), "; |
| 4996 | S += utostr(flag); |
| 4997 | S += ");\n}\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4998 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4999 | S += "static void __Block_byref_id_object_dispose_"; |
| 5000 | S += utostr(flag); |
| 5001 | S += "(void *src) {\n"; |
| 5002 | S += " _Block_object_dispose(*(void * *) ((char*)src + "; |
| 5003 | S += utostr(offset); |
| 5004 | S += "), "; |
| 5005 | S += utostr(flag); |
| 5006 | S += ");\n}\n"; |
| 5007 | return S; |
| 5008 | } |
| 5009 | |
| 5010 | /// RewriteByRefVar - For each __block typex ND variable this routine transforms |
| 5011 | /// the declaration into: |
| 5012 | /// struct __Block_byref_ND { |
| 5013 | /// void *__isa; // NULL for everything except __weak pointers |
| 5014 | /// struct __Block_byref_ND *__forwarding; |
| 5015 | /// int32_t __flags; |
| 5016 | /// int32_t __size; |
| 5017 | /// void *__Block_byref_id_object_copy; // If variable is __block ObjC object |
| 5018 | /// void *__Block_byref_id_object_dispose; // If variable is __block ObjC object |
| 5019 | /// typex ND; |
| 5020 | /// }; |
| 5021 | /// |
| 5022 | /// It then replaces declaration of ND variable with: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5023 | /// struct __Block_byref_ND ND = {__isa=0B, __forwarding=&ND, __flags=some_flag, |
| 5024 | /// __size=sizeof(struct __Block_byref_ND), |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5025 | /// ND=initializer-if-any}; |
| 5026 | /// |
| 5027 | /// |
Fariborz Jahanian | 847713a | 2012-04-24 19:38:45 +0000 | [diff] [blame] | 5028 | void RewriteModernObjC::RewriteByRefVar(VarDecl *ND, bool firstDecl, |
| 5029 | bool lastDecl) { |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5030 | int flag = 0; |
| 5031 | int isa = 0; |
| 5032 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
| 5033 | if (DeclLoc.isInvalid()) |
| 5034 | // If type location is missing, it is because of missing type (a warning). |
| 5035 | // Use variable's location which is good for this case. |
| 5036 | DeclLoc = ND->getLocation(); |
| 5037 | const char *startBuf = SM->getCharacterData(DeclLoc); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 5038 | SourceLocation X = ND->getEndLoc(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5039 | X = SM->getExpansionLoc(X); |
| 5040 | const char *endBuf = SM->getCharacterData(X); |
| 5041 | std::string Name(ND->getNameAsString()); |
| 5042 | std::string ByrefType; |
| 5043 | RewriteByRefString(ByrefType, Name, ND, true); |
| 5044 | ByrefType += " {\n"; |
| 5045 | ByrefType += " void *__isa;\n"; |
| 5046 | RewriteByRefString(ByrefType, Name, ND); |
| 5047 | ByrefType += " *__forwarding;\n"; |
| 5048 | ByrefType += " int __flags;\n"; |
| 5049 | ByrefType += " int __size;\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5050 | // Add void *__Block_byref_id_object_copy; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5051 | // void *__Block_byref_id_object_dispose; if needed. |
| 5052 | QualType Ty = ND->getType(); |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 5053 | bool HasCopyAndDispose = Context->BlockRequiresCopying(Ty, ND); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5054 | if (HasCopyAndDispose) { |
| 5055 | ByrefType += " void (*__Block_byref_id_object_copy)(void*, void*);\n"; |
| 5056 | ByrefType += " void (*__Block_byref_id_object_dispose)(void*);\n"; |
| 5057 | } |
| 5058 | |
| 5059 | QualType T = Ty; |
| 5060 | (void)convertBlockPointerToFunctionPointer(T); |
| 5061 | T.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5062 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5063 | ByrefType += " " + Name + ";\n"; |
| 5064 | ByrefType += "};\n"; |
| 5065 | // Insert this type in global scope. It is needed by helper function. |
| 5066 | SourceLocation FunLocStart; |
| 5067 | if (CurFunctionDef) |
Fariborz Jahanian | ca357d9 | 2012-04-19 00:50:01 +0000 | [diff] [blame] | 5068 | FunLocStart = getFunctionSourceLocation(*this, CurFunctionDef); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5069 | else { |
| 5070 | assert(CurMethodDef && "RewriteByRefVar - CurMethodDef is null"); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5071 | FunLocStart = CurMethodDef->getBeginLoc(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5072 | } |
| 5073 | InsertText(FunLocStart, ByrefType); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5074 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5075 | if (Ty.isObjCGCWeak()) { |
| 5076 | flag |= BLOCK_FIELD_IS_WEAK; |
| 5077 | isa = 1; |
| 5078 | } |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5079 | if (HasCopyAndDispose) { |
| 5080 | flag = BLOCK_BYREF_CALLER; |
| 5081 | QualType Ty = ND->getType(); |
| 5082 | // FIXME. Handle __weak variable (BLOCK_FIELD_IS_WEAK) as well. |
| 5083 | if (Ty->isBlockPointerType()) |
| 5084 | flag |= BLOCK_FIELD_IS_BLOCK; |
| 5085 | else |
| 5086 | flag |= BLOCK_FIELD_IS_OBJECT; |
| 5087 | std::string HF = SynthesizeByrefCopyDestroyHelper(ND, flag); |
| 5088 | if (!HF.empty()) |
Fariborz Jahanian | e499613 | 2013-02-07 22:50:40 +0000 | [diff] [blame] | 5089 | Preamble += HF; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5090 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5091 | |
| 5092 | // struct __Block_byref_ND ND = |
| 5093 | // {0, &ND, some_flag, __size=sizeof(struct __Block_byref_ND), |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5094 | // initializer-if-any}; |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 5095 | bool hasInit = (ND->getInit() != nullptr); |
Fariborz Jahanian | 5811fd6 | 2012-04-11 23:57:12 +0000 | [diff] [blame] | 5096 | // FIXME. rewriter does not support __block c++ objects which |
| 5097 | // require construction. |
Fariborz Jahanian | 16d0d6c | 2012-04-26 23:20:25 +0000 | [diff] [blame] | 5098 | if (hasInit) |
| 5099 | if (CXXConstructExpr *CExp = dyn_cast<CXXConstructExpr>(ND->getInit())) { |
| 5100 | CXXConstructorDecl *CXXDecl = CExp->getConstructor(); |
| 5101 | if (CXXDecl && CXXDecl->isDefaultConstructor()) |
| 5102 | hasInit = false; |
| 5103 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5104 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5105 | unsigned flags = 0; |
| 5106 | if (HasCopyAndDispose) |
| 5107 | flags |= BLOCK_HAS_COPY_DISPOSE; |
| 5108 | Name = ND->getNameAsString(); |
| 5109 | ByrefType.clear(); |
| 5110 | RewriteByRefString(ByrefType, Name, ND); |
| 5111 | std::string ForwardingCastType("("); |
| 5112 | ForwardingCastType += ByrefType + " *)"; |
Fariborz Jahanian | 3fd9bbd | 2012-04-24 16:45:27 +0000 | [diff] [blame] | 5113 | ByrefType += " " + Name + " = {(void*)"; |
| 5114 | ByrefType += utostr(isa); |
| 5115 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
| 5116 | ByrefType += utostr(flags); |
| 5117 | ByrefType += ", "; |
| 5118 | ByrefType += "sizeof("; |
| 5119 | RewriteByRefString(ByrefType, Name, ND); |
| 5120 | ByrefType += ")"; |
| 5121 | if (HasCopyAndDispose) { |
| 5122 | ByrefType += ", __Block_byref_id_object_copy_"; |
| 5123 | ByrefType += utostr(flag); |
| 5124 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 5125 | ByrefType += utostr(flag); |
| 5126 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5127 | |
Fariborz Jahanian | 847713a | 2012-04-24 19:38:45 +0000 | [diff] [blame] | 5128 | if (!firstDecl) { |
| 5129 | // In multiple __block declarations, and for all but 1st declaration, |
| 5130 | // find location of the separating comma. This would be start location |
| 5131 | // where new text is to be inserted. |
| 5132 | DeclLoc = ND->getLocation(); |
| 5133 | const char *startDeclBuf = SM->getCharacterData(DeclLoc); |
| 5134 | const char *commaBuf = startDeclBuf; |
| 5135 | while (*commaBuf != ',') |
| 5136 | commaBuf--; |
| 5137 | assert((*commaBuf == ',') && "RewriteByRefVar: can't find ','"); |
| 5138 | DeclLoc = DeclLoc.getLocWithOffset(commaBuf - startDeclBuf); |
| 5139 | startBuf = commaBuf; |
| 5140 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5141 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5142 | if (!hasInit) { |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5143 | ByrefType += "};\n"; |
| 5144 | unsigned nameSize = Name.size(); |
Simon Pilgrim | 2c51880 | 2017-03-30 14:13:19 +0000 | [diff] [blame] | 5145 | // for block or function pointer declaration. Name is already |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5146 | // part of the declaration. |
| 5147 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) |
| 5148 | nameSize = 1; |
| 5149 | ReplaceText(DeclLoc, endBuf-startBuf+nameSize, ByrefType); |
| 5150 | } |
| 5151 | else { |
Fariborz Jahanian | 3fd9bbd | 2012-04-24 16:45:27 +0000 | [diff] [blame] | 5152 | ByrefType += ", "; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5153 | SourceLocation startLoc; |
| 5154 | Expr *E = ND->getInit(); |
| 5155 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 5156 | startLoc = ECE->getLParenLoc(); |
| 5157 | else |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5158 | startLoc = E->getBeginLoc(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5159 | startLoc = SM->getExpansionLoc(startLoc); |
| 5160 | endBuf = SM->getCharacterData(startLoc); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5161 | ReplaceText(DeclLoc, endBuf-startBuf, ByrefType); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5162 | |
Fariborz Jahanian | 847713a | 2012-04-24 19:38:45 +0000 | [diff] [blame] | 5163 | const char separator = lastDecl ? ';' : ','; |
| 5164 | const char *startInitializerBuf = SM->getCharacterData(startLoc); |
| 5165 | const char *separatorBuf = strchr(startInitializerBuf, separator); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5166 | assert((*separatorBuf == separator) && |
Fariborz Jahanian | 847713a | 2012-04-24 19:38:45 +0000 | [diff] [blame] | 5167 | "RewriteByRefVar: can't find ';' or ','"); |
| 5168 | SourceLocation separatorLoc = |
| 5169 | startLoc.getLocWithOffset(separatorBuf-startInitializerBuf); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5170 | |
Fariborz Jahanian | 847713a | 2012-04-24 19:38:45 +0000 | [diff] [blame] | 5171 | InsertText(separatorLoc, lastDecl ? "}" : "};\n"); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5172 | } |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5173 | } |
| 5174 | |
| 5175 | void RewriteModernObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { |
| 5176 | // Add initializers for any closure decl refs. |
| 5177 | GetBlockDeclRefExprs(Exp->getBody()); |
| 5178 | if (BlockDeclRefs.size()) { |
| 5179 | // Unique all "by copy" declarations. |
| 5180 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5181 | if (!BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>()) { |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5182 | if (!BlockByCopyDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 5183 | BlockByCopyDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 5184 | BlockByCopyDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 5185 | } |
| 5186 | } |
| 5187 | // Unique all "by ref" declarations. |
| 5188 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5189 | if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>()) { |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5190 | if (!BlockByRefDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 5191 | BlockByRefDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 5192 | BlockByRefDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 5193 | } |
| 5194 | } |
| 5195 | // Find any imported blocks...they will need special attention. |
| 5196 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5197 | if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>() || |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5198 | BlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5199 | BlockDeclRefs[i]->getType()->isBlockPointerType()) |
| 5200 | ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 5201 | } |
| 5202 | } |
| 5203 | |
| 5204 | FunctionDecl *RewriteModernObjC::SynthBlockInitFunctionDecl(StringRef name) { |
| 5205 | IdentifierInfo *ID = &Context->Idents.get(name); |
| 5206 | QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy); |
| 5207 | return FunctionDecl::Create(*Context, TUDecl, SourceLocation(), |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 5208 | SourceLocation(), ID, FType, nullptr, SC_Extern, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 5209 | false, false); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5210 | } |
| 5211 | |
| 5212 | Stmt *RewriteModernObjC::SynthBlockInitExpr(BlockExpr *Exp, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5213 | const SmallVectorImpl<DeclRefExpr *> &InnerBlockDeclRefs) { |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5214 | const BlockDecl *block = Exp->getBlockDecl(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5215 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5216 | Blocks.push_back(Exp); |
| 5217 | |
| 5218 | CollectBlockDeclRefInfo(Exp); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5219 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5220 | // Add inner imported variables now used in current block. |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 5221 | int countOfInnerDecls = 0; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5222 | if (!InnerBlockDeclRefs.empty()) { |
| 5223 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) { |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5224 | DeclRefExpr *Exp = InnerBlockDeclRefs[i]; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5225 | ValueDecl *VD = Exp->getDecl(); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5226 | if (!VD->hasAttr<BlocksAttr>() && !BlockByCopyDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5227 | // We need to save the copied-in variables in nested |
| 5228 | // blocks because it is needed at the end for some of the API generations. |
| 5229 | // See SynthesizeBlockLiterals routine. |
| 5230 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 5231 | BlockDeclRefs.push_back(Exp); |
| 5232 | BlockByCopyDeclsPtrSet.insert(VD); |
| 5233 | BlockByCopyDecls.push_back(VD); |
| 5234 | } |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5235 | if (VD->hasAttr<BlocksAttr>() && !BlockByRefDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5236 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 5237 | BlockDeclRefs.push_back(Exp); |
| 5238 | BlockByRefDeclsPtrSet.insert(VD); |
| 5239 | BlockByRefDecls.push_back(VD); |
| 5240 | } |
| 5241 | } |
| 5242 | // Find any imported blocks...they will need special attention. |
| 5243 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5244 | if (InnerBlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>() || |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5245 | InnerBlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5246 | InnerBlockDeclRefs[i]->getType()->isBlockPointerType()) |
| 5247 | ImportedBlockDecls.insert(InnerBlockDeclRefs[i]->getDecl()); |
| 5248 | } |
| 5249 | InnerDeclRefsCount.push_back(countOfInnerDecls); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5250 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5251 | std::string FuncName; |
| 5252 | |
| 5253 | if (CurFunctionDef) |
| 5254 | FuncName = CurFunctionDef->getNameAsString(); |
| 5255 | else if (CurMethodDef) |
| 5256 | BuildUniqueMethodName(FuncName, CurMethodDef); |
| 5257 | else if (GlobalVarDecl) |
| 5258 | FuncName = std::string(GlobalVarDecl->getNameAsString()); |
| 5259 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5260 | bool GlobalBlockExpr = |
Fariborz Jahanian | e005070 | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 5261 | block->getDeclContext()->getRedeclContext()->isFileContext(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5262 | |
Fariborz Jahanian | e005070 | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 5263 | if (GlobalBlockExpr && !GlobalVarDecl) { |
| 5264 | Diags.Report(block->getLocation(), GlobalBlockRewriteFailedDiag); |
| 5265 | GlobalBlockExpr = false; |
| 5266 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5267 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5268 | std::string BlockNumber = utostr(Blocks.size()-1); |
| 5269 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5270 | std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; |
| 5271 | |
| 5272 | // Get a pointer to the function type so we can cast appropriately. |
| 5273 | QualType BFT = convertFunctionTypeOfBlocks(Exp->getFunctionType()); |
| 5274 | QualType FType = Context->getPointerType(BFT); |
| 5275 | |
| 5276 | FunctionDecl *FD; |
| 5277 | Expr *NewRep; |
| 5278 | |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 5279 | // Simulate a constructor call... |
Fariborz Jahanian | e005070 | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 5280 | std::string Tag; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5281 | |
Fariborz Jahanian | e005070 | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 5282 | if (GlobalBlockExpr) |
| 5283 | Tag = "__global_"; |
| 5284 | else |
| 5285 | Tag = "__"; |
| 5286 | Tag += FuncName + "_block_impl_" + BlockNumber; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5287 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5288 | FD = SynthBlockInitFunctionDecl(Tag); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5289 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, false, FType, VK_RValue, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5290 | SourceLocation()); |
| 5291 | |
| 5292 | SmallVector<Expr*, 4> InitExprs; |
| 5293 | |
| 5294 | // Initialize the block function. |
| 5295 | FD = SynthBlockInitFunctionDecl(Func); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5296 | DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, false, FD->getType(), |
| 5297 | VK_LValue, SourceLocation()); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5298 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
| 5299 | CK_BitCast, Arg); |
| 5300 | InitExprs.push_back(castExpr); |
| 5301 | |
| 5302 | // Initialize the block descriptor. |
| 5303 | std::string DescData = "__" + FuncName + "_block_desc_" + BlockNumber + "_DATA"; |
| 5304 | |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 5305 | VarDecl *NewVD = VarDecl::Create( |
| 5306 | *Context, TUDecl, SourceLocation(), SourceLocation(), |
| 5307 | &Context->Idents.get(DescData), Context->VoidPtrTy, nullptr, SC_Static); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5308 | UnaryOperator *DescRefExpr = |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5309 | new (Context) UnaryOperator(new (Context) DeclRefExpr(NewVD, false, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5310 | Context->VoidPtrTy, |
| 5311 | VK_LValue, |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5312 | SourceLocation()), |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5313 | UO_AddrOf, |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5314 | Context->getPointerType(Context->VoidPtrTy), |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5315 | VK_RValue, OK_Ordinary, |
Aaron Ballman | a503855 | 2018-01-09 13:07:03 +0000 | [diff] [blame] | 5316 | SourceLocation(), false); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5317 | InitExprs.push_back(DescRefExpr); |
| 5318 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5319 | // Add initializers for any closure decl refs. |
| 5320 | if (BlockDeclRefs.size()) { |
| 5321 | Expr *Exp; |
| 5322 | // Output all "by copy" declarations. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 5323 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByCopyDecls.begin(), |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5324 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 5325 | if (isObjCType((*I)->getType())) { |
| 5326 | // FIXME: Conform to ABI ([[obj retain] autorelease]). |
| 5327 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5328 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), |
| 5329 | VK_LValue, SourceLocation()); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5330 | if (HasLocalVariableExternalStorage(*I)) { |
| 5331 | QualType QT = (*I)->getType(); |
| 5332 | QT = Context->getPointerType(QT); |
| 5333 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue, |
Aaron Ballman | a503855 | 2018-01-09 13:07:03 +0000 | [diff] [blame] | 5334 | OK_Ordinary, SourceLocation(), |
| 5335 | false); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5336 | } |
| 5337 | } else if (isTopLevelBlockPointerType((*I)->getType())) { |
| 5338 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5339 | Arg = new (Context) DeclRefExpr(FD, false, FD->getType(), |
| 5340 | VK_LValue, SourceLocation()); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5341 | Exp = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
| 5342 | CK_BitCast, Arg); |
| 5343 | } else { |
| 5344 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5345 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), |
| 5346 | VK_LValue, SourceLocation()); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5347 | if (HasLocalVariableExternalStorage(*I)) { |
| 5348 | QualType QT = (*I)->getType(); |
| 5349 | QT = Context->getPointerType(QT); |
| 5350 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue, |
Aaron Ballman | a503855 | 2018-01-09 13:07:03 +0000 | [diff] [blame] | 5351 | OK_Ordinary, SourceLocation(), |
| 5352 | false); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5353 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5354 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5355 | } |
| 5356 | InitExprs.push_back(Exp); |
| 5357 | } |
| 5358 | // Output all "by ref" declarations. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 5359 | for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByRefDecls.begin(), |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5360 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 5361 | ValueDecl *ND = (*I); |
| 5362 | std::string Name(ND->getNameAsString()); |
| 5363 | std::string RecName; |
| 5364 | RewriteByRefString(RecName, Name, ND, true); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5365 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str() |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5366 | + sizeof("struct")); |
| 5367 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 5368 | SourceLocation(), SourceLocation(), |
| 5369 | II); |
| 5370 | assert(RD && "SynthBlockInitExpr(): Can't find RecordDecl"); |
| 5371 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5372 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5373 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5374 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5375 | SourceLocation()); |
| 5376 | bool isNestedCapturedVar = false; |
| 5377 | if (block) |
Aaron Ballman | 9371dd2 | 2014-03-14 18:34:04 +0000 | [diff] [blame] | 5378 | for (const auto &CI : block->captures()) { |
| 5379 | const VarDecl *variable = CI.getVariable(); |
| 5380 | if (variable == ND && CI.isNested()) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5381 | assert (CI.isByRef() && |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5382 | "SynthBlockInitExpr - captured block variable is not byref"); |
| 5383 | isNestedCapturedVar = true; |
| 5384 | break; |
| 5385 | } |
| 5386 | } |
| 5387 | // captured nested byref variable has its address passed. Do not take |
| 5388 | // its address again. |
| 5389 | if (!isNestedCapturedVar) |
| 5390 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, |
| 5391 | Context->getPointerType(Exp->getType()), |
Aaron Ballman | a503855 | 2018-01-09 13:07:03 +0000 | [diff] [blame] | 5392 | VK_RValue, OK_Ordinary, SourceLocation(), |
| 5393 | false); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5394 | Exp = NoTypeInfoCStyleCastExpr(Context, castT, CK_BitCast, Exp); |
| 5395 | InitExprs.push_back(Exp); |
| 5396 | } |
| 5397 | } |
| 5398 | if (ImportedBlockDecls.size()) { |
| 5399 | // generate BLOCK_HAS_COPY_DISPOSE(have helper funcs) | BLOCK_HAS_DESCRIPTOR |
| 5400 | int flag = (BLOCK_HAS_COPY_DISPOSE | BLOCK_HAS_DESCRIPTOR); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5401 | unsigned IntSize = |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5402 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5403 | Expr *FlagExp = IntegerLiteral::Create(*Context, llvm::APInt(IntSize, flag), |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5404 | Context->IntTy, SourceLocation()); |
| 5405 | InitExprs.push_back(FlagExp); |
| 5406 | } |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 5407 | NewRep = new (Context) CallExpr(*Context, DRE, InitExprs, |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5408 | FType, VK_LValue, SourceLocation()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5409 | |
Fariborz Jahanian | e005070 | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 5410 | if (GlobalBlockExpr) { |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 5411 | assert (!GlobalConstructionExp && |
Fariborz Jahanian | e005070 | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 5412 | "SynthBlockInitExpr - GlobalConstructionExp must be null"); |
| 5413 | GlobalConstructionExp = NewRep; |
| 5414 | NewRep = DRE; |
| 5415 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5416 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5417 | NewRep = new (Context) UnaryOperator(NewRep, UO_AddrOf, |
| 5418 | Context->getPointerType(NewRep->getType()), |
Aaron Ballman | a503855 | 2018-01-09 13:07:03 +0000 | [diff] [blame] | 5419 | VK_RValue, OK_Ordinary, SourceLocation(), false); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5420 | NewRep = NoTypeInfoCStyleCastExpr(Context, FType, CK_BitCast, |
| 5421 | NewRep); |
Fariborz Jahanian | 93722477 | 2014-10-28 23:46:58 +0000 | [diff] [blame] | 5422 | // Put Paren around the call. |
| 5423 | NewRep = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 5424 | NewRep); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5425 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5426 | BlockDeclRefs.clear(); |
| 5427 | BlockByRefDecls.clear(); |
| 5428 | BlockByRefDeclsPtrSet.clear(); |
| 5429 | BlockByCopyDecls.clear(); |
| 5430 | BlockByCopyDeclsPtrSet.clear(); |
| 5431 | ImportedBlockDecls.clear(); |
| 5432 | return NewRep; |
| 5433 | } |
| 5434 | |
| 5435 | bool RewriteModernObjC::IsDeclStmtInForeachHeader(DeclStmt *DS) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5436 | if (const ObjCForCollectionStmt * CS = |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5437 | dyn_cast<ObjCForCollectionStmt>(Stmts.back())) |
| 5438 | return CS->getElement() == DS; |
| 5439 | return false; |
| 5440 | } |
| 5441 | |
| 5442 | //===----------------------------------------------------------------------===// |
| 5443 | // Function Body / Expression rewriting |
| 5444 | //===----------------------------------------------------------------------===// |
| 5445 | |
| 5446 | Stmt *RewriteModernObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
| 5447 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
| 5448 | isa<DoStmt>(S) || isa<ForStmt>(S)) |
| 5449 | Stmts.push_back(S); |
| 5450 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 5451 | Stmts.push_back(S); |
| 5452 | ObjCBcLabelNo.push_back(++BcLabelCount); |
| 5453 | } |
| 5454 | |
| 5455 | // Pseudo-object operations and ivar references need special |
| 5456 | // treatment because we're going to recursively rewrite them. |
| 5457 | if (PseudoObjectExpr *PseudoOp = dyn_cast<PseudoObjectExpr>(S)) { |
| 5458 | if (isa<BinaryOperator>(PseudoOp->getSyntacticForm())) { |
| 5459 | return RewritePropertyOrImplicitSetter(PseudoOp); |
| 5460 | } else { |
| 5461 | return RewritePropertyOrImplicitGetter(PseudoOp); |
| 5462 | } |
| 5463 | } else if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) { |
| 5464 | return RewriteObjCIvarRefExpr(IvarRefExpr); |
| 5465 | } |
Fariborz Jahanian | 4254cdb | 2013-02-08 18:57:50 +0000 | [diff] [blame] | 5466 | else if (isa<OpaqueValueExpr>(S)) |
| 5467 | S = cast<OpaqueValueExpr>(S)->getSourceExpr(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5468 | |
| 5469 | SourceRange OrigStmtRange = S->getSourceRange(); |
| 5470 | |
| 5471 | // Perform a bottom up rewrite of all children. |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 5472 | for (Stmt *&childStmt : S->children()) |
| 5473 | if (childStmt) { |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5474 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(childStmt); |
| 5475 | if (newStmt) { |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 5476 | childStmt = newStmt; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5477 | } |
| 5478 | } |
| 5479 | |
| 5480 | if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5481 | SmallVector<DeclRefExpr *, 8> InnerBlockDeclRefs; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5482 | llvm::SmallPtrSet<const DeclContext *, 8> InnerContexts; |
| 5483 | InnerContexts.insert(BE->getBlockDecl()); |
| 5484 | ImportedLocalExternalDecls.clear(); |
| 5485 | GetInnerBlockDeclRefExprs(BE->getBody(), |
| 5486 | InnerBlockDeclRefs, InnerContexts); |
| 5487 | // Rewrite the block body in place. |
| 5488 | Stmt *SaveCurrentBody = CurrentBody; |
| 5489 | CurrentBody = BE->getBody(); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 5490 | PropParentMap = nullptr; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5491 | // block literal on rhs of a property-dot-sytax assignment |
| 5492 | // must be replaced by its synthesize ast so getRewrittenText |
| 5493 | // works as expected. In this case, what actually ends up on RHS |
| 5494 | // is the blockTranscribed which is the helper function for the |
| 5495 | // block literal; as in: self.c = ^() {[ace ARR];}; |
| 5496 | bool saveDisableReplaceStmt = DisableReplaceStmt; |
| 5497 | DisableReplaceStmt = false; |
| 5498 | RewriteFunctionBodyOrGlobalInitializer(BE->getBody()); |
| 5499 | DisableReplaceStmt = saveDisableReplaceStmt; |
| 5500 | CurrentBody = SaveCurrentBody; |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 5501 | PropParentMap = nullptr; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5502 | ImportedLocalExternalDecls.clear(); |
| 5503 | // Now we snarf the rewritten text and stash it away for later use. |
| 5504 | std::string Str = Rewrite.getRewrittenText(BE->getSourceRange()); |
| 5505 | RewrittenBlockExprs[BE] = Str; |
| 5506 | |
| 5507 | Stmt *blockTranscribed = SynthBlockInitExpr(BE, InnerBlockDeclRefs); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5508 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5509 | //blockTranscribed->dump(); |
| 5510 | ReplaceStmt(S, blockTranscribed); |
| 5511 | return blockTranscribed; |
| 5512 | } |
| 5513 | // Handle specific things. |
| 5514 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 5515 | return RewriteAtEncode(AtEncode); |
| 5516 | |
| 5517 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 5518 | return RewriteAtSelector(AtSelector); |
| 5519 | |
| 5520 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 5521 | return RewriteObjCStringLiteral(AtString); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5522 | |
Fariborz Jahanian | 307b7ad | 2012-03-27 20:17:30 +0000 | [diff] [blame] | 5523 | if (ObjCBoolLiteralExpr *BoolLitExpr = dyn_cast<ObjCBoolLiteralExpr>(S)) |
| 5524 | return RewriteObjCBoolLiteralExpr(BoolLitExpr); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5525 | |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 5526 | if (ObjCBoxedExpr *BoxedExpr = dyn_cast<ObjCBoxedExpr>(S)) |
| 5527 | return RewriteObjCBoxedExpr(BoxedExpr); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5528 | |
Fariborz Jahanian | 991a08d | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 5529 | if (ObjCArrayLiteral *ArrayLitExpr = dyn_cast<ObjCArrayLiteral>(S)) |
| 5530 | return RewriteObjCArrayLiteralExpr(ArrayLitExpr); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5531 | |
| 5532 | if (ObjCDictionaryLiteral *DictionaryLitExpr = |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 5533 | dyn_cast<ObjCDictionaryLiteral>(S)) |
| 5534 | return RewriteObjCDictionaryLiteralExpr(DictionaryLitExpr); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5535 | |
| 5536 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
| 5537 | #if 0 |
| 5538 | // Before we rewrite it, put the original message expression in a comment. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5539 | SourceLocation startLoc = MessExpr->getBeginLoc(); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 5540 | SourceLocation endLoc = MessExpr->getEndLoc(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5541 | |
| 5542 | const char *startBuf = SM->getCharacterData(startLoc); |
| 5543 | const char *endBuf = SM->getCharacterData(endLoc); |
| 5544 | |
| 5545 | std::string messString; |
| 5546 | messString += "// "; |
| 5547 | messString.append(startBuf, endBuf-startBuf+1); |
| 5548 | messString += "\n"; |
| 5549 | |
| 5550 | // FIXME: Missing definition of |
| 5551 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
Craig Topper | a2a8d9c | 2015-10-22 03:13:10 +0000 | [diff] [blame] | 5552 | // InsertText(startLoc, messString); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5553 | // Tried this, but it didn't work either... |
| 5554 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
| 5555 | #endif |
| 5556 | return RewriteMessageExpr(MessExpr); |
| 5557 | } |
| 5558 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5559 | if (ObjCAutoreleasePoolStmt *StmtAutoRelease = |
Fariborz Jahanian | 9b43c3f | 2012-05-23 23:47:20 +0000 | [diff] [blame] | 5560 | dyn_cast<ObjCAutoreleasePoolStmt>(S)) { |
| 5561 | return RewriteObjCAutoreleasePoolStmt(StmtAutoRelease); |
| 5562 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5563 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5564 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 5565 | return RewriteObjCTryStmt(StmtTry); |
| 5566 | |
| 5567 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 5568 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 5569 | |
| 5570 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 5571 | return RewriteObjCThrowStmt(StmtThrow); |
| 5572 | |
| 5573 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 5574 | return RewriteObjCProtocolExpr(ProtocolExp); |
| 5575 | |
| 5576 | if (ObjCForCollectionStmt *StmtForCollection = |
| 5577 | dyn_cast<ObjCForCollectionStmt>(S)) |
| 5578 | return RewriteObjCForCollectionStmt(StmtForCollection, |
| 5579 | OrigStmtRange.getEnd()); |
| 5580 | if (BreakStmt *StmtBreakStmt = |
| 5581 | dyn_cast<BreakStmt>(S)) |
| 5582 | return RewriteBreakStmt(StmtBreakStmt); |
| 5583 | if (ContinueStmt *StmtContinueStmt = |
| 5584 | dyn_cast<ContinueStmt>(S)) |
| 5585 | return RewriteContinueStmt(StmtContinueStmt); |
| 5586 | |
| 5587 | // Need to check for protocol refs (id <P>, Foo <P> *) in variable decls |
| 5588 | // and cast exprs. |
| 5589 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
| 5590 | // FIXME: What we're doing here is modifying the type-specifier that |
| 5591 | // precedes the first Decl. In the future the DeclGroup should have |
| 5592 | // a separate type-specifier that we can rewrite. |
| 5593 | // NOTE: We need to avoid rewriting the DeclStmt if it is within |
| 5594 | // the context of an ObjCForCollectionStmt. For example: |
| 5595 | // NSArray *someArray; |
| 5596 | // for (id <FooProtocol> index in someArray) ; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5597 | // This is because RewriteObjCForCollectionStmt() does textual rewriting |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5598 | // and it depends on the original text locations/positions. |
| 5599 | if (Stmts.empty() || !IsDeclStmtInForeachHeader(DS)) |
| 5600 | RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin()); |
| 5601 | |
| 5602 | // Blocks rewrite rules. |
| 5603 | for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); |
| 5604 | DI != DE; ++DI) { |
| 5605 | Decl *SD = *DI; |
| 5606 | if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { |
| 5607 | if (isTopLevelBlockPointerType(ND->getType())) |
| 5608 | RewriteBlockPointerDecl(ND); |
| 5609 | else if (ND->getType()->isFunctionPointerType()) |
| 5610 | CheckFunctionPointerDecl(ND->getType(), ND); |
| 5611 | if (VarDecl *VD = dyn_cast<VarDecl>(SD)) { |
| 5612 | if (VD->hasAttr<BlocksAttr>()) { |
| 5613 | static unsigned uniqueByrefDeclCount = 0; |
| 5614 | assert(!BlockByRefDeclNo.count(ND) && |
| 5615 | "RewriteFunctionBodyOrGlobalInitializer: Duplicate byref decl"); |
| 5616 | BlockByRefDeclNo[ND] = uniqueByrefDeclCount++; |
Fariborz Jahanian | 847713a | 2012-04-24 19:38:45 +0000 | [diff] [blame] | 5617 | RewriteByRefVar(VD, (DI == DS->decl_begin()), ((DI+1) == DE)); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5618 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5619 | else |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5620 | RewriteTypeOfDecl(VD); |
| 5621 | } |
| 5622 | } |
| 5623 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) { |
| 5624 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
| 5625 | RewriteBlockPointerDecl(TD); |
| 5626 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
| 5627 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 5628 | } |
| 5629 | } |
| 5630 | } |
| 5631 | |
| 5632 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) |
| 5633 | RewriteObjCQualifiedInterfaceTypes(CE); |
| 5634 | |
| 5635 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
| 5636 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 5637 | assert(!Stmts.empty() && "Statement stack is empty"); |
| 5638 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 5639 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
| 5640 | && "Statement stack mismatch"); |
| 5641 | Stmts.pop_back(); |
| 5642 | } |
| 5643 | // Handle blocks rewriting. |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5644 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5645 | ValueDecl *VD = DRE->getDecl(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5646 | if (VD->hasAttr<BlocksAttr>()) |
| 5647 | return RewriteBlockDeclRefExpr(DRE); |
| 5648 | if (HasLocalVariableExternalStorage(VD)) |
| 5649 | return RewriteLocalVariableExternalStorage(DRE); |
| 5650 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5651 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5652 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
| 5653 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
| 5654 | Stmt *BlockCall = SynthesizeBlockCall(CE, CE->getCallee()); |
| 5655 | ReplaceStmt(S, BlockCall); |
| 5656 | return BlockCall; |
| 5657 | } |
| 5658 | } |
| 5659 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) { |
| 5660 | RewriteCastExpr(CE); |
| 5661 | } |
Fariborz Jahanian | 2c00acd | 2012-04-10 00:08:18 +0000 | [diff] [blame] | 5662 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
| 5663 | RewriteImplicitCastObjCExpr(ICE); |
| 5664 | } |
Fariborz Jahanian | cc17228 | 2012-04-16 22:14:01 +0000 | [diff] [blame] | 5665 | #if 0 |
Fariborz Jahanian | 3a5d552 | 2012-04-13 18:00:54 +0000 | [diff] [blame] | 5666 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5667 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
| 5668 | CastExpr *Replacement = new (Context) CastExpr(ICE->getType(), |
| 5669 | ICE->getSubExpr(), |
| 5670 | SourceLocation()); |
| 5671 | // Get the new text. |
| 5672 | std::string SStr; |
| 5673 | llvm::raw_string_ostream Buf(SStr); |
Richard Smith | 235341b | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 5674 | Replacement->printPretty(Buf); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5675 | const std::string &Str = Buf.str(); |
| 5676 | |
| 5677 | printf("CAST = %s\n", &Str[0]); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5678 | InsertText(ICE->getSubExpr()->getBeginLoc(), Str); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5679 | delete S; |
| 5680 | return Replacement; |
| 5681 | } |
| 5682 | #endif |
| 5683 | // Return this stmt unmodified. |
| 5684 | return S; |
| 5685 | } |
| 5686 | |
| 5687 | void RewriteModernObjC::RewriteRecordBody(RecordDecl *RD) { |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5688 | for (auto *FD : RD->fields()) { |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5689 | if (isTopLevelBlockPointerType(FD->getType())) |
| 5690 | RewriteBlockPointerDecl(FD); |
| 5691 | if (FD->getType()->isObjCQualifiedIdType() || |
| 5692 | FD->getType()->isObjCQualifiedInterfaceType()) |
| 5693 | RewriteObjCQualifiedInterfaceTypes(FD); |
| 5694 | } |
| 5695 | } |
| 5696 | |
| 5697 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 5698 | /// main file of the input. |
| 5699 | void RewriteModernObjC::HandleDeclInMainFile(Decl *D) { |
| 5700 | switch (D->getKind()) { |
| 5701 | case Decl::Function: { |
| 5702 | FunctionDecl *FD = cast<FunctionDecl>(D); |
| 5703 | if (FD->isOverloadedOperator()) |
| 5704 | return; |
| 5705 | |
| 5706 | // Since function prototypes don't have ParmDecl's, we check the function |
| 5707 | // prototype. This enables us to rewrite function declarations and |
| 5708 | // definitions using the same code. |
| 5709 | RewriteBlocksInFunctionProtoType(FD->getType(), FD); |
| 5710 | |
Argyrios Kyrtzidis | 75627ad | 2012-02-12 04:48:45 +0000 | [diff] [blame] | 5711 | if (!FD->isThisDeclarationADefinition()) |
| 5712 | break; |
| 5713 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5714 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
| 5715 | if (CompoundStmt *Body = dyn_cast_or_null<CompoundStmt>(FD->getBody())) { |
| 5716 | CurFunctionDef = FD; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5717 | CurrentBody = Body; |
| 5718 | Body = |
| 5719 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 5720 | FD->setBody(Body); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 5721 | CurrentBody = nullptr; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5722 | if (PropParentMap) { |
| 5723 | delete PropParentMap; |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 5724 | PropParentMap = nullptr; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5725 | } |
| 5726 | // This synthesizes and inserts the block "impl" struct, invoke function, |
| 5727 | // and any copy/dispose helper functions. |
| 5728 | InsertBlockLiteralsWithinFunction(FD); |
Fariborz Jahanian | aa4a242 | 2012-11-06 17:30:23 +0000 | [diff] [blame] | 5729 | RewriteLineDirective(D); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 5730 | CurFunctionDef = nullptr; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5731 | } |
| 5732 | break; |
| 5733 | } |
| 5734 | case Decl::ObjCMethod: { |
| 5735 | ObjCMethodDecl *MD = cast<ObjCMethodDecl>(D); |
| 5736 | if (CompoundStmt *Body = MD->getCompoundBody()) { |
| 5737 | CurMethodDef = MD; |
| 5738 | CurrentBody = Body; |
| 5739 | Body = |
| 5740 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 5741 | MD->setBody(Body); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 5742 | CurrentBody = nullptr; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5743 | if (PropParentMap) { |
| 5744 | delete PropParentMap; |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 5745 | PropParentMap = nullptr; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5746 | } |
| 5747 | InsertBlockLiteralsWithinMethod(MD); |
Fariborz Jahanian | aa4a242 | 2012-11-06 17:30:23 +0000 | [diff] [blame] | 5748 | RewriteLineDirective(D); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 5749 | CurMethodDef = nullptr; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5750 | } |
| 5751 | break; |
| 5752 | } |
| 5753 | case Decl::ObjCImplementation: { |
| 5754 | ObjCImplementationDecl *CI = cast<ObjCImplementationDecl>(D); |
| 5755 | ClassImplementation.push_back(CI); |
| 5756 | break; |
| 5757 | } |
| 5758 | case Decl::ObjCCategoryImpl: { |
| 5759 | ObjCCategoryImplDecl *CI = cast<ObjCCategoryImplDecl>(D); |
| 5760 | CategoryImplementation.push_back(CI); |
| 5761 | break; |
| 5762 | } |
| 5763 | case Decl::Var: { |
| 5764 | VarDecl *VD = cast<VarDecl>(D); |
| 5765 | RewriteObjCQualifiedInterfaceTypes(VD); |
| 5766 | if (isTopLevelBlockPointerType(VD->getType())) |
| 5767 | RewriteBlockPointerDecl(VD); |
| 5768 | else if (VD->getType()->isFunctionPointerType()) { |
| 5769 | CheckFunctionPointerDecl(VD->getType(), VD); |
| 5770 | if (VD->getInit()) { |
| 5771 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
| 5772 | RewriteCastExpr(CE); |
| 5773 | } |
| 5774 | } |
| 5775 | } else if (VD->getType()->isRecordType()) { |
| 5776 | RecordDecl *RD = VD->getType()->getAs<RecordType>()->getDecl(); |
| 5777 | if (RD->isCompleteDefinition()) |
| 5778 | RewriteRecordBody(RD); |
| 5779 | } |
| 5780 | if (VD->getInit()) { |
| 5781 | GlobalVarDecl = VD; |
| 5782 | CurrentBody = VD->getInit(); |
| 5783 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 5784 | CurrentBody = nullptr; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5785 | if (PropParentMap) { |
| 5786 | delete PropParentMap; |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 5787 | PropParentMap = nullptr; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5788 | } |
| 5789 | SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), VD->getName()); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 5790 | GlobalVarDecl = nullptr; |
| 5791 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5792 | // This is needed for blocks. |
| 5793 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
| 5794 | RewriteCastExpr(CE); |
| 5795 | } |
| 5796 | } |
| 5797 | break; |
| 5798 | } |
| 5799 | case Decl::TypeAlias: |
| 5800 | case Decl::Typedef: { |
| 5801 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
| 5802 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
| 5803 | RewriteBlockPointerDecl(TD); |
| 5804 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
| 5805 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
Fariborz Jahanian | 3a65ce3 | 2013-04-03 19:11:21 +0000 | [diff] [blame] | 5806 | else |
| 5807 | RewriteObjCQualifiedInterfaceTypes(TD); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5808 | } |
| 5809 | break; |
| 5810 | } |
| 5811 | case Decl::CXXRecord: |
| 5812 | case Decl::Record: { |
| 5813 | RecordDecl *RD = cast<RecordDecl>(D); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5814 | if (RD->isCompleteDefinition()) |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5815 | RewriteRecordBody(RD); |
| 5816 | break; |
| 5817 | } |
| 5818 | default: |
| 5819 | break; |
| 5820 | } |
| 5821 | // Nothing yet. |
| 5822 | } |
| 5823 | |
Fariborz Jahanian | b31e3af | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 5824 | /// Write_ProtocolExprReferencedMetadata - This routine writer out the |
| 5825 | /// protocol reference symbols in the for of: |
| 5826 | /// struct _protocol_t *PROTOCOL_REF = &PROTOCOL_METADATA. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5827 | static void Write_ProtocolExprReferencedMetadata(ASTContext *Context, |
Fariborz Jahanian | b31e3af | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 5828 | ObjCProtocolDecl *PDecl, |
| 5829 | std::string &Result) { |
| 5830 | // Also output .objc_protorefs$B section and its meta-data. |
| 5831 | if (Context->getLangOpts().MicrosoftExt) |
Fariborz Jahanian | 75f2e3c | 2012-04-27 21:39:49 +0000 | [diff] [blame] | 5832 | Result += "static "; |
Fariborz Jahanian | b31e3af | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 5833 | Result += "struct _protocol_t *"; |
| 5834 | Result += "_OBJC_PROTOCOL_REFERENCE_$_"; |
| 5835 | Result += PDecl->getNameAsString(); |
| 5836 | Result += " = &"; |
| 5837 | Result += "_OBJC_PROTOCOL_"; Result += PDecl->getNameAsString(); |
| 5838 | Result += ";\n"; |
| 5839 | } |
| 5840 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5841 | void RewriteModernObjC::HandleTranslationUnit(ASTContext &C) { |
| 5842 | if (Diags.hasErrorOccurred()) |
| 5843 | return; |
| 5844 | |
| 5845 | RewriteInclude(); |
| 5846 | |
Fariborz Jahanian | e499613 | 2013-02-07 22:50:40 +0000 | [diff] [blame] | 5847 | for (unsigned i = 0, e = FunctionDefinitionsSeen.size(); i < e; i++) { |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 5848 | // translation of function bodies were postponed until all class and |
Fariborz Jahanian | e499613 | 2013-02-07 22:50:40 +0000 | [diff] [blame] | 5849 | // their extensions and implementations are seen. This is because, we |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 5850 | // cannot build grouping structs for bitfields until they are all seen. |
Fariborz Jahanian | e499613 | 2013-02-07 22:50:40 +0000 | [diff] [blame] | 5851 | FunctionDecl *FDecl = FunctionDefinitionsSeen[i]; |
| 5852 | HandleTopLevelSingleDecl(FDecl); |
| 5853 | } |
| 5854 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5855 | // Here's a great place to add any extra declarations that may be needed. |
| 5856 | // Write out meta data for each @protocol(<expr>). |
Craig Topper | c6914d0 | 2014-08-25 04:15:02 +0000 | [diff] [blame] | 5857 | for (ObjCProtocolDecl *ProtDecl : ProtocolExprDecls) { |
| 5858 | RewriteObjCProtocolMetaData(ProtDecl, Preamble); |
| 5859 | Write_ProtocolExprReferencedMetadata(Context, ProtDecl, Preamble); |
Fariborz Jahanian | b31e3af | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 5860 | } |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5861 | |
| 5862 | InsertText(SM->getLocForStartOfFile(MainFileID), Preamble, false); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5863 | |
Fariborz Jahanian | 89919cc | 2012-05-08 23:54:35 +0000 | [diff] [blame] | 5864 | if (ClassImplementation.size() || CategoryImplementation.size()) |
| 5865 | RewriteImplementations(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5866 | |
Fariborz Jahanian | 8e1118cbd | 2012-02-21 23:58:41 +0000 | [diff] [blame] | 5867 | for (unsigned i = 0, e = ObjCInterfacesSeen.size(); i < e; i++) { |
| 5868 | ObjCInterfaceDecl *CDecl = ObjCInterfacesSeen[i]; |
| 5869 | // Write struct declaration for the class matching its ivar declarations. |
| 5870 | // Note that for modern abi, this is postponed until the end of TU |
| 5871 | // because class extensions and the implementation might declare their own |
| 5872 | // private ivars. |
| 5873 | RewriteInterfaceDecl(CDecl); |
| 5874 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5875 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5876 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 5877 | // we are done. |
| 5878 | if (const RewriteBuffer *RewriteBuf = |
| 5879 | Rewrite.getRewriteBufferFor(MainFileID)) { |
| 5880 | //printf("Changed:\n"); |
| 5881 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 5882 | } else { |
| 5883 | llvm::errs() << "No changes\n"; |
| 5884 | } |
| 5885 | |
| 5886 | if (ClassImplementation.size() || CategoryImplementation.size() || |
| 5887 | ProtocolExprDecls.size()) { |
| 5888 | // Rewrite Objective-c meta data* |
| 5889 | std::string ResultStr; |
| 5890 | RewriteMetaDataIntoBuffer(ResultStr); |
| 5891 | // Emit metadata. |
| 5892 | *OutFile << ResultStr; |
| 5893 | } |
Fariborz Jahanian | ddddca3 | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 5894 | // Emit ImageInfo; |
| 5895 | { |
| 5896 | std::string ResultStr; |
| 5897 | WriteImageInfo(ResultStr); |
| 5898 | *OutFile << ResultStr; |
| 5899 | } |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5900 | OutFile->flush(); |
| 5901 | } |
| 5902 | |
| 5903 | void RewriteModernObjC::Initialize(ASTContext &context) { |
| 5904 | InitializeCommon(context); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5905 | |
Fariborz Jahanian | b52221e | 2012-03-10 17:45:38 +0000 | [diff] [blame] | 5906 | Preamble += "#ifndef __OBJC2__\n"; |
| 5907 | Preamble += "#define __OBJC2__\n"; |
| 5908 | Preamble += "#endif\n"; |
| 5909 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5910 | // declaring objc_selector outside the parameter list removes a silly |
| 5911 | // scope related warning... |
| 5912 | if (IsHeader) |
| 5913 | Preamble = "#pragma once\n"; |
| 5914 | Preamble += "struct objc_selector; struct objc_class;\n"; |
Fariborz Jahanian | 27db0b3 | 2012-04-12 23:52:52 +0000 | [diff] [blame] | 5915 | Preamble += "struct __rw_objc_super { \n\tstruct objc_object *object; "; |
| 5916 | Preamble += "\n\tstruct objc_object *superClass; "; |
| 5917 | // Add a constructor for creating temporary objects. |
| 5918 | Preamble += "\n\t__rw_objc_super(struct objc_object *o, struct objc_object *s) "; |
| 5919 | Preamble += ": object(o), superClass(s) {} "; |
| 5920 | Preamble += "\n};\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5921 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5922 | if (LangOpts.MicrosoftExt) { |
Fariborz Jahanian | 008dfe2 | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 5923 | // Define all sections using syntax that makes sense. |
Fariborz Jahanian | ddddca3 | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 5924 | // These are currently generated. |
| 5925 | Preamble += "\n#pragma section(\".objc_classlist$B\", long, read, write)\n"; |
Fariborz Jahanian | 008dfe2 | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 5926 | Preamble += "#pragma section(\".objc_catlist$B\", long, read, write)\n"; |
Fariborz Jahanian | ddddca3 | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 5927 | Preamble += "#pragma section(\".objc_imageinfo$B\", long, read, write)\n"; |
Fariborz Jahanian | 07a423d | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 5928 | Preamble += "#pragma section(\".objc_nlclslist$B\", long, read, write)\n"; |
| 5929 | Preamble += "#pragma section(\".objc_nlcatlist$B\", long, read, write)\n"; |
Fariborz Jahanian | ddddca3 | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 5930 | // These are generated but not necessary for functionality. |
Fariborz Jahanian | ddddca3 | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 5931 | Preamble += "#pragma section(\".cat_cls_meth$B\", long, read, write)\n"; |
Fariborz Jahanian | 008dfe2 | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 5932 | Preamble += "#pragma section(\".inst_meth$B\", long, read, write)\n"; |
| 5933 | Preamble += "#pragma section(\".cls_meth$B\", long, read, write)\n"; |
Fariborz Jahanian | e47bf2b | 2012-03-12 16:46:58 +0000 | [diff] [blame] | 5934 | Preamble += "#pragma section(\".objc_ivar$B\", long, read, write)\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5935 | |
Fariborz Jahanian | b31e3af | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 5936 | // These need be generated for performance. Currently they are not, |
| 5937 | // using API calls instead. |
| 5938 | Preamble += "#pragma section(\".objc_selrefs$B\", long, read, write)\n"; |
| 5939 | Preamble += "#pragma section(\".objc_classrefs$B\", long, read, write)\n"; |
| 5940 | Preamble += "#pragma section(\".objc_superrefs$B\", long, read, write)\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5941 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5942 | } |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5943 | Preamble += "#ifndef _REWRITER_typedef_Protocol\n"; |
| 5944 | Preamble += "typedef struct objc_object Protocol;\n"; |
| 5945 | Preamble += "#define _REWRITER_typedef_Protocol\n"; |
| 5946 | Preamble += "#endif\n"; |
| 5947 | if (LangOpts.MicrosoftExt) { |
| 5948 | Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n"; |
| 5949 | Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5950 | } |
Fariborz Jahanian | 167384d | 2012-03-21 23:41:04 +0000 | [diff] [blame] | 5951 | else |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5952 | Preamble += "#define __OBJC_RW_DLLIMPORT extern\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5953 | |
Fariborz Jahanian | 167384d | 2012-03-21 23:41:04 +0000 | [diff] [blame] | 5954 | Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSend(void);\n"; |
| 5955 | Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSendSuper(void);\n"; |
| 5956 | Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSend_stret(void);\n"; |
| 5957 | Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSendSuper_stret(void);\n"; |
| 5958 | Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSend_fpret(void);\n"; |
| 5959 | |
Fariborz Jahanian | 9c0c050 | 2012-05-08 20:55:55 +0000 | [diff] [blame] | 5960 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_class *objc_getClass"; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5961 | Preamble += "(const char *);\n"; |
| 5962 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_class *class_getSuperclass"; |
| 5963 | Preamble += "(struct objc_class *);\n"; |
Fariborz Jahanian | 9c0c050 | 2012-05-08 20:55:55 +0000 | [diff] [blame] | 5964 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_class *objc_getMetaClass"; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5965 | Preamble += "(const char *);\n"; |
Fariborz Jahanian | 3466059 | 2012-03-19 18:11:32 +0000 | [diff] [blame] | 5966 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw( struct objc_object *);\n"; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5967 | // @synchronized hooks. |
Aaron Ballman | 9c00446 | 2012-09-06 16:44:16 +0000 | [diff] [blame] | 5968 | Preamble += "__OBJC_RW_DLLIMPORT int objc_sync_enter( struct objc_object *);\n"; |
| 5969 | Preamble += "__OBJC_RW_DLLIMPORT int objc_sync_exit( struct objc_object *);\n"; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5970 | Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n"; |
Fariborz Jahanian | 6c0af64 | 2013-09-05 17:17:32 +0000 | [diff] [blame] | 5971 | Preamble += "#ifdef _WIN64\n"; |
| 5972 | Preamble += "typedef unsigned long long _WIN_NSUInteger;\n"; |
| 5973 | Preamble += "#else\n"; |
| 5974 | Preamble += "typedef unsigned int _WIN_NSUInteger;\n"; |
| 5975 | Preamble += "#endif\n"; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5976 | Preamble += "#ifndef __FASTENUMERATIONSTATE\n"; |
| 5977 | Preamble += "struct __objcFastEnumerationState {\n\t"; |
| 5978 | Preamble += "unsigned long state;\n\t"; |
| 5979 | Preamble += "void **itemsPtr;\n\t"; |
| 5980 | Preamble += "unsigned long *mutationsPtr;\n\t"; |
| 5981 | Preamble += "unsigned long extra[5];\n};\n"; |
| 5982 | Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n"; |
| 5983 | Preamble += "#define __FASTENUMERATIONSTATE\n"; |
| 5984 | Preamble += "#endif\n"; |
| 5985 | Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n"; |
| 5986 | Preamble += "struct __NSConstantStringImpl {\n"; |
| 5987 | Preamble += " int *isa;\n"; |
| 5988 | Preamble += " int flags;\n"; |
| 5989 | Preamble += " char *str;\n"; |
Fariborz Jahanian | db3a5dc | 2014-04-16 17:03:06 +0000 | [diff] [blame] | 5990 | Preamble += "#if _WIN64\n"; |
Fariborz Jahanian | 287e79a | 2014-04-01 19:32:35 +0000 | [diff] [blame] | 5991 | Preamble += " long long length;\n"; |
| 5992 | Preamble += "#else\n"; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5993 | Preamble += " long length;\n"; |
Fariborz Jahanian | 287e79a | 2014-04-01 19:32:35 +0000 | [diff] [blame] | 5994 | Preamble += "#endif\n"; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5995 | Preamble += "};\n"; |
| 5996 | Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n"; |
| 5997 | Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n"; |
| 5998 | Preamble += "#else\n"; |
| 5999 | Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n"; |
| 6000 | Preamble += "#endif\n"; |
| 6001 | Preamble += "#define __NSCONSTANTSTRINGIMPL\n"; |
| 6002 | Preamble += "#endif\n"; |
| 6003 | // Blocks preamble. |
| 6004 | Preamble += "#ifndef BLOCK_IMPL\n"; |
| 6005 | Preamble += "#define BLOCK_IMPL\n"; |
| 6006 | Preamble += "struct __block_impl {\n"; |
| 6007 | Preamble += " void *isa;\n"; |
| 6008 | Preamble += " int Flags;\n"; |
| 6009 | Preamble += " int Reserved;\n"; |
| 6010 | Preamble += " void *FuncPtr;\n"; |
| 6011 | Preamble += "};\n"; |
| 6012 | Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n"; |
| 6013 | Preamble += "#ifdef __OBJC_EXPORT_BLOCKS\n"; |
| 6014 | Preamble += "extern \"C\" __declspec(dllexport) " |
| 6015 | "void _Block_object_assign(void *, const void *, const int);\n"; |
| 6016 | Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_dispose(const void *, const int);\n"; |
| 6017 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteGlobalBlock[32];\n"; |
| 6018 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteStackBlock[32];\n"; |
| 6019 | Preamble += "#else\n"; |
| 6020 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_assign(void *, const void *, const int);\n"; |
| 6021 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_dispose(const void *, const int);\n"; |
| 6022 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteGlobalBlock[32];\n"; |
| 6023 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteStackBlock[32];\n"; |
| 6024 | Preamble += "#endif\n"; |
| 6025 | Preamble += "#endif\n"; |
| 6026 | if (LangOpts.MicrosoftExt) { |
| 6027 | Preamble += "#undef __OBJC_RW_DLLIMPORT\n"; |
| 6028 | Preamble += "#undef __OBJC_RW_STATICIMPORT\n"; |
| 6029 | Preamble += "#ifndef KEEP_ATTRIBUTES\n"; // We use this for clang tests. |
| 6030 | Preamble += "#define __attribute__(X)\n"; |
| 6031 | Preamble += "#endif\n"; |
Fariborz Jahanian | e1240fe | 2012-04-12 16:33:31 +0000 | [diff] [blame] | 6032 | Preamble += "#ifndef __weak\n"; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6033 | Preamble += "#define __weak\n"; |
Fariborz Jahanian | e1240fe | 2012-04-12 16:33:31 +0000 | [diff] [blame] | 6034 | Preamble += "#endif\n"; |
| 6035 | Preamble += "#ifndef __block\n"; |
| 6036 | Preamble += "#define __block\n"; |
| 6037 | Preamble += "#endif\n"; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6038 | } |
| 6039 | else { |
| 6040 | Preamble += "#define __block\n"; |
| 6041 | Preamble += "#define __weak\n"; |
| 6042 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6043 | |
Fariborz Jahanian | e110fe4 | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 6044 | // Declarations required for modern objective-c array and dictionary literals. |
| 6045 | Preamble += "\n#include <stdarg.h>\n"; |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 6046 | Preamble += "struct __NSContainer_literal {\n"; |
Fariborz Jahanian | e110fe4 | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 6047 | Preamble += " void * *arr;\n"; |
Fariborz Jahanian | 4460e0f | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 6048 | Preamble += " __NSContainer_literal (unsigned int count, ...) {\n"; |
Fariborz Jahanian | e110fe4 | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 6049 | Preamble += "\tva_list marker;\n"; |
| 6050 | Preamble += "\tva_start(marker, count);\n"; |
| 6051 | Preamble += "\tarr = new void *[count];\n"; |
| 6052 | Preamble += "\tfor (unsigned i = 0; i < count; i++)\n"; |
| 6053 | Preamble += "\t arr[i] = va_arg(marker, void *);\n"; |
| 6054 | Preamble += "\tva_end( marker );\n"; |
| 6055 | Preamble += " };\n"; |
Fariborz Jahanian | 70ef929 | 2012-05-02 23:53:46 +0000 | [diff] [blame] | 6056 | Preamble += " ~__NSContainer_literal() {\n"; |
Fariborz Jahanian | e110fe4 | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 6057 | Preamble += "\tdelete[] arr;\n"; |
| 6058 | Preamble += " }\n"; |
| 6059 | Preamble += "};\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6060 | |
Fariborz Jahanian | 9b43c3f | 2012-05-23 23:47:20 +0000 | [diff] [blame] | 6061 | // Declaration required for implementation of @autoreleasepool statement. |
| 6062 | Preamble += "extern \"C\" __declspec(dllimport) void * objc_autoreleasePoolPush(void);\n"; |
| 6063 | Preamble += "extern \"C\" __declspec(dllimport) void objc_autoreleasePoolPop(void *);\n\n"; |
| 6064 | Preamble += "struct __AtAutoreleasePool {\n"; |
| 6065 | Preamble += " __AtAutoreleasePool() {atautoreleasepoolobj = objc_autoreleasePoolPush();}\n"; |
| 6066 | Preamble += " ~__AtAutoreleasePool() {objc_autoreleasePoolPop(atautoreleasepoolobj);}\n"; |
| 6067 | Preamble += " void * atautoreleasepoolobj;\n"; |
| 6068 | Preamble += "};\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6069 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6070 | // NOTE! Windows uses LLP64 for 64bit mode. So, cast pointer to long long |
| 6071 | // as this avoids warning in any 64bit/32bit compilation model. |
| 6072 | Preamble += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((long long) &((TYPE *)0)->MEMBER)\n"; |
| 6073 | } |
| 6074 | |
Hiroshi Inoue | c5e54dd | 2017-07-03 08:49:44 +0000 | [diff] [blame] | 6075 | /// RewriteIvarOffsetComputation - This routine synthesizes computation of |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6076 | /// ivar offset. |
| 6077 | void RewriteModernObjC::RewriteIvarOffsetComputation(ObjCIvarDecl *ivar, |
| 6078 | std::string &Result) { |
Fariborz Jahanian | 57dd66b | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 6079 | Result += "__OFFSETOFIVAR__(struct "; |
| 6080 | Result += ivar->getContainingInterface()->getNameAsString(); |
| 6081 | if (LangOpts.MicrosoftExt) |
| 6082 | Result += "_IMPL"; |
| 6083 | Result += ", "; |
| 6084 | if (ivar->isBitField()) |
| 6085 | ObjCIvarBitfieldGroupDecl(ivar, Result); |
| 6086 | else |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6087 | Result += ivar->getNameAsString(); |
Fariborz Jahanian | 57dd66b | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 6088 | Result += ")"; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6089 | } |
| 6090 | |
| 6091 | /// WriteModernMetadataDeclarations - Writes out metadata declarations for modern ABI. |
| 6092 | /// struct _prop_t { |
Fariborz Jahanian | 67f7c55 | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 6093 | /// const char *name; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6094 | /// char *attributes; |
| 6095 | /// } |
| 6096 | |
| 6097 | /// struct _prop_list_t { |
| 6098 | /// uint32_t entsize; // sizeof(struct _prop_t) |
| 6099 | /// uint32_t count_of_properties; |
| 6100 | /// struct _prop_t prop_list[count_of_properties]; |
| 6101 | /// } |
| 6102 | |
| 6103 | /// struct _protocol_t; |
| 6104 | |
| 6105 | /// struct _protocol_list_t { |
| 6106 | /// long protocol_count; // Note, this is 32/64 bit |
| 6107 | /// struct _protocol_t * protocol_list[protocol_count]; |
| 6108 | /// } |
| 6109 | |
| 6110 | /// struct _objc_method { |
| 6111 | /// SEL _cmd; |
Fariborz Jahanian | 4aaf8b1 | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6112 | /// const char *method_type; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6113 | /// char *_imp; |
| 6114 | /// } |
| 6115 | |
| 6116 | /// struct _method_list_t { |
| 6117 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 6118 | /// uint32_t method_count; |
| 6119 | /// struct _objc_method method_list[method_count]; |
| 6120 | /// } |
| 6121 | |
| 6122 | /// struct _protocol_t { |
| 6123 | /// id isa; // NULL |
Fariborz Jahanian | eb4eb5c | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 6124 | /// const char *protocol_name; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6125 | /// const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | eb4eb5c | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 6126 | /// const struct method_list_t *instance_methods; |
| 6127 | /// const struct method_list_t *class_methods; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6128 | /// const struct method_list_t *optionalInstanceMethods; |
| 6129 | /// const struct method_list_t *optionalClassMethods; |
| 6130 | /// const struct _prop_list_t * properties; |
| 6131 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 6132 | /// const uint32_t flags; // = 0 |
| 6133 | /// const char ** extendedMethodTypes; |
| 6134 | /// } |
| 6135 | |
Fariborz Jahanian | 591b7de | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6136 | /// struct _ivar_t { |
| 6137 | /// unsigned long int *offset; // pointer to ivar offset location |
Fariborz Jahanian | 6b83dae | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6138 | /// const char *name; |
| 6139 | /// const char *type; |
Fariborz Jahanian | 591b7de | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6140 | /// uint32_t alignment; |
| 6141 | /// uint32_t size; |
| 6142 | /// } |
| 6143 | |
| 6144 | /// struct _ivar_list_t { |
| 6145 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 6146 | /// uint32 count; |
Fariborz Jahanian | 6b83dae | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6147 | /// struct _ivar_t list[count]; |
Fariborz Jahanian | 591b7de | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6148 | /// } |
| 6149 | |
| 6150 | /// struct _class_ro_t { |
Fariborz Jahanian | 3413481 | 2012-03-24 16:53:16 +0000 | [diff] [blame] | 6151 | /// uint32_t flags; |
| 6152 | /// uint32_t instanceStart; |
| 6153 | /// uint32_t instanceSize; |
| 6154 | /// uint32_t reserved; // only when building for 64bit targets |
Fariborz Jahanian | eb4eb5c | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 6155 | /// const uint8_t *ivarLayout; |
| 6156 | /// const char *name; |
| 6157 | /// const struct _method_list_t *baseMethods; |
| 6158 | /// const struct _protocol_list_t *baseProtocols; |
| 6159 | /// const struct _ivar_list_t *ivars; |
| 6160 | /// const uint8_t *weakIvarLayout; |
| 6161 | /// const struct _prop_list_t *properties; |
Fariborz Jahanian | 591b7de | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6162 | /// } |
| 6163 | |
| 6164 | /// struct _class_t { |
| 6165 | /// struct _class_t *isa; |
Fariborz Jahanian | 6e60c13 | 2012-03-20 17:34:50 +0000 | [diff] [blame] | 6166 | /// struct _class_t *superclass; |
Fariborz Jahanian | 591b7de | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6167 | /// void *cache; |
| 6168 | /// IMP *vtable; |
Fariborz Jahanian | 638252f | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6169 | /// struct _class_ro_t *ro; |
Fariborz Jahanian | 591b7de | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6170 | /// } |
| 6171 | |
| 6172 | /// struct _category_t { |
Fariborz Jahanian | eb4eb5c | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 6173 | /// const char *name; |
Fariborz Jahanian | cd79a49 | 2012-03-20 21:41:28 +0000 | [diff] [blame] | 6174 | /// struct _class_t *cls; |
Fariborz Jahanian | eb4eb5c | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 6175 | /// const struct _method_list_t *instance_methods; |
| 6176 | /// const struct _method_list_t *class_methods; |
| 6177 | /// const struct _protocol_list_t *protocols; |
| 6178 | /// const struct _prop_list_t *properties; |
Fariborz Jahanian | 591b7de | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6179 | /// } |
| 6180 | |
| 6181 | /// MessageRefTy - LLVM for: |
| 6182 | /// struct _message_ref_t { |
| 6183 | /// IMP messenger; |
| 6184 | /// SEL name; |
| 6185 | /// }; |
| 6186 | |
| 6187 | /// SuperMessageRefTy - LLVM for: |
| 6188 | /// struct _super_message_ref_t { |
| 6189 | /// SUPER_IMP messenger; |
| 6190 | /// SEL name; |
| 6191 | /// }; |
| 6192 | |
Fariborz Jahanian | 4548962 | 2012-03-14 18:09:23 +0000 | [diff] [blame] | 6193 | static void WriteModernMetadataDeclarations(ASTContext *Context, std::string &Result) { |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6194 | static bool meta_data_declared = false; |
| 6195 | if (meta_data_declared) |
| 6196 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6197 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6198 | Result += "\nstruct _prop_t {\n"; |
Fariborz Jahanian | 67f7c55 | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 6199 | Result += "\tconst char *name;\n"; |
| 6200 | Result += "\tconst char *attributes;\n"; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6201 | Result += "};\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6202 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6203 | Result += "\nstruct _protocol_t;\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6204 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6205 | Result += "\nstruct _objc_method {\n"; |
| 6206 | Result += "\tstruct objc_selector * _cmd;\n"; |
Fariborz Jahanian | 4aaf8b1 | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6207 | Result += "\tconst char *method_type;\n"; |
Fariborz Jahanian | b1ba885 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6208 | Result += "\tvoid *_imp;\n"; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6209 | Result += "};\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6210 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6211 | Result += "\nstruct _protocol_t {\n"; |
| 6212 | Result += "\tvoid * isa; // NULL\n"; |
Fariborz Jahanian | eb4eb5c | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 6213 | Result += "\tconst char *protocol_name;\n"; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6214 | Result += "\tconst struct _protocol_list_t * protocol_list; // super protocols\n"; |
Fariborz Jahanian | eb4eb5c | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 6215 | Result += "\tconst struct method_list_t *instance_methods;\n"; |
| 6216 | Result += "\tconst struct method_list_t *class_methods;\n"; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6217 | Result += "\tconst struct method_list_t *optionalInstanceMethods;\n"; |
| 6218 | Result += "\tconst struct method_list_t *optionalClassMethods;\n"; |
| 6219 | Result += "\tconst struct _prop_list_t * properties;\n"; |
| 6220 | Result += "\tconst unsigned int size; // sizeof(struct _protocol_t)\n"; |
| 6221 | Result += "\tconst unsigned int flags; // = 0\n"; |
| 6222 | Result += "\tconst char ** extendedMethodTypes;\n"; |
| 6223 | Result += "};\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6224 | |
Fariborz Jahanian | 591b7de | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6225 | Result += "\nstruct _ivar_t {\n"; |
| 6226 | Result += "\tunsigned long int *offset; // pointer to ivar offset location\n"; |
Fariborz Jahanian | 6b83dae | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6227 | Result += "\tconst char *name;\n"; |
| 6228 | Result += "\tconst char *type;\n"; |
Fariborz Jahanian | 591b7de | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6229 | Result += "\tunsigned int alignment;\n"; |
| 6230 | Result += "\tunsigned int size;\n"; |
| 6231 | Result += "};\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6232 | |
Fariborz Jahanian | 591b7de | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6233 | Result += "\nstruct _class_ro_t {\n"; |
Fariborz Jahanian | 3413481 | 2012-03-24 16:53:16 +0000 | [diff] [blame] | 6234 | Result += "\tunsigned int flags;\n"; |
Fariborz Jahanian | 591b7de | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6235 | Result += "\tunsigned int instanceStart;\n"; |
Fariborz Jahanian | 3413481 | 2012-03-24 16:53:16 +0000 | [diff] [blame] | 6236 | Result += "\tunsigned int instanceSize;\n"; |
Fariborz Jahanian | 4548962 | 2012-03-14 18:09:23 +0000 | [diff] [blame] | 6237 | const llvm::Triple &Triple(Context->getTargetInfo().getTriple()); |
| 6238 | if (Triple.getArch() == llvm::Triple::x86_64) |
Fariborz Jahanian | 3413481 | 2012-03-24 16:53:16 +0000 | [diff] [blame] | 6239 | Result += "\tunsigned int reserved;\n"; |
Fariborz Jahanian | eb4eb5c | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 6240 | Result += "\tconst unsigned char *ivarLayout;\n"; |
| 6241 | Result += "\tconst char *name;\n"; |
| 6242 | Result += "\tconst struct _method_list_t *baseMethods;\n"; |
| 6243 | Result += "\tconst struct _objc_protocol_list *baseProtocols;\n"; |
| 6244 | Result += "\tconst struct _ivar_list_t *ivars;\n"; |
| 6245 | Result += "\tconst unsigned char *weakIvarLayout;\n"; |
| 6246 | Result += "\tconst struct _prop_list_t *properties;\n"; |
Fariborz Jahanian | 591b7de | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6247 | Result += "};\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6248 | |
Fariborz Jahanian | 591b7de | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6249 | Result += "\nstruct _class_t {\n"; |
| 6250 | Result += "\tstruct _class_t *isa;\n"; |
Fariborz Jahanian | 6e60c13 | 2012-03-20 17:34:50 +0000 | [diff] [blame] | 6251 | Result += "\tstruct _class_t *superclass;\n"; |
Fariborz Jahanian | 591b7de | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6252 | Result += "\tvoid *cache;\n"; |
| 6253 | Result += "\tvoid *vtable;\n"; |
Fariborz Jahanian | 638252f | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6254 | Result += "\tstruct _class_ro_t *ro;\n"; |
Fariborz Jahanian | 591b7de | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6255 | Result += "};\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6256 | |
Fariborz Jahanian | 591b7de | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6257 | Result += "\nstruct _category_t {\n"; |
Fariborz Jahanian | eb4eb5c | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 6258 | Result += "\tconst char *name;\n"; |
Fariborz Jahanian | cd79a49 | 2012-03-20 21:41:28 +0000 | [diff] [blame] | 6259 | Result += "\tstruct _class_t *cls;\n"; |
Fariborz Jahanian | eb4eb5c | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 6260 | Result += "\tconst struct _method_list_t *instance_methods;\n"; |
| 6261 | Result += "\tconst struct _method_list_t *class_methods;\n"; |
| 6262 | Result += "\tconst struct _protocol_list_t *protocols;\n"; |
| 6263 | Result += "\tconst struct _prop_list_t *properties;\n"; |
Fariborz Jahanian | 591b7de | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6264 | Result += "};\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6265 | |
Fariborz Jahanian | 38c5910 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 6266 | Result += "extern \"C\" __declspec(dllimport) struct objc_cache _objc_empty_cache;\n"; |
Fariborz Jahanian | f35e020 | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 6267 | Result += "#pragma warning(disable:4273)\n"; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6268 | meta_data_declared = true; |
| 6269 | } |
| 6270 | |
Fariborz Jahanian | e18961b | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 6271 | static void Write_protocol_list_t_TypeDecl(std::string &Result, |
| 6272 | long super_protocol_count) { |
| 6273 | Result += "struct /*_protocol_list_t*/"; Result += " {\n"; |
| 6274 | Result += "\tlong protocol_count; // Note, this is 32/64 bit\n"; |
| 6275 | Result += "\tstruct _protocol_t *super_protocols["; |
| 6276 | Result += utostr(super_protocol_count); Result += "];\n"; |
| 6277 | Result += "}"; |
| 6278 | } |
| 6279 | |
Fariborz Jahanian | 4aaf8b1 | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6280 | static void Write_method_list_t_TypeDecl(std::string &Result, |
| 6281 | unsigned int method_count) { |
| 6282 | Result += "struct /*_method_list_t*/"; Result += " {\n"; |
| 6283 | Result += "\tunsigned int entsize; // sizeof(struct _objc_method)\n"; |
| 6284 | Result += "\tunsigned int method_count;\n"; |
| 6285 | Result += "\tstruct _objc_method method_list["; |
| 6286 | Result += utostr(method_count); Result += "];\n"; |
| 6287 | Result += "}"; |
| 6288 | } |
| 6289 | |
Fariborz Jahanian | 67f7c55 | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 6290 | static void Write__prop_list_t_TypeDecl(std::string &Result, |
| 6291 | unsigned int property_count) { |
| 6292 | Result += "struct /*_prop_list_t*/"; Result += " {\n"; |
| 6293 | Result += "\tunsigned int entsize; // sizeof(struct _prop_t)\n"; |
| 6294 | Result += "\tunsigned int count_of_properties;\n"; |
| 6295 | Result += "\tstruct _prop_t prop_list["; |
| 6296 | Result += utostr(property_count); Result += "];\n"; |
| 6297 | Result += "}"; |
| 6298 | } |
| 6299 | |
Fariborz Jahanian | 6b83dae | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6300 | static void Write__ivar_list_t_TypeDecl(std::string &Result, |
| 6301 | unsigned int ivar_count) { |
| 6302 | Result += "struct /*_ivar_list_t*/"; Result += " {\n"; |
| 6303 | Result += "\tunsigned int entsize; // sizeof(struct _prop_t)\n"; |
| 6304 | Result += "\tunsigned int count;\n"; |
| 6305 | Result += "\tstruct _ivar_t ivar_list["; |
| 6306 | Result += utostr(ivar_count); Result += "];\n"; |
| 6307 | Result += "}"; |
| 6308 | } |
| 6309 | |
Fariborz Jahanian | e18961b | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 6310 | static void Write_protocol_list_initializer(ASTContext *Context, std::string &Result, |
| 6311 | ArrayRef<ObjCProtocolDecl *> SuperProtocols, |
| 6312 | StringRef VarName, |
| 6313 | StringRef ProtocolName) { |
| 6314 | if (SuperProtocols.size() > 0) { |
| 6315 | Result += "\nstatic "; |
| 6316 | Write_protocol_list_t_TypeDecl(Result, SuperProtocols.size()); |
| 6317 | Result += " "; Result += VarName; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6318 | Result += ProtocolName; |
Fariborz Jahanian | e18961b | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 6319 | Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n"; |
| 6320 | Result += "\t"; Result += utostr(SuperProtocols.size()); Result += ",\n"; |
| 6321 | for (unsigned i = 0, e = SuperProtocols.size(); i < e; i++) { |
| 6322 | ObjCProtocolDecl *SuperPD = SuperProtocols[i]; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6323 | Result += "\t&"; Result += "_OBJC_PROTOCOL_"; |
Fariborz Jahanian | e18961b | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 6324 | Result += SuperPD->getNameAsString(); |
| 6325 | if (i == e-1) |
| 6326 | Result += "\n};\n"; |
| 6327 | else |
| 6328 | Result += ",\n"; |
| 6329 | } |
| 6330 | } |
| 6331 | } |
| 6332 | |
Fariborz Jahanian | b1ba885 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6333 | static void Write_method_list_t_initializer(RewriteModernObjC &RewriteObj, |
| 6334 | ASTContext *Context, std::string &Result, |
Fariborz Jahanian | 4aaf8b1 | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6335 | ArrayRef<ObjCMethodDecl *> Methods, |
| 6336 | StringRef VarName, |
Fariborz Jahanian | b1ba885 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6337 | StringRef TopLevelDeclName, |
| 6338 | bool MethodImpl) { |
Fariborz Jahanian | 4aaf8b1 | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6339 | if (Methods.size() > 0) { |
| 6340 | Result += "\nstatic "; |
| 6341 | Write_method_list_t_TypeDecl(Result, Methods.size()); |
| 6342 | Result += " "; Result += VarName; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6343 | Result += TopLevelDeclName; |
Fariborz Jahanian | 4aaf8b1 | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6344 | Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n"; |
| 6345 | Result += "\t"; Result += "sizeof(_objc_method)"; Result += ",\n"; |
| 6346 | Result += "\t"; Result += utostr(Methods.size()); Result += ",\n"; |
| 6347 | for (unsigned i = 0, e = Methods.size(); i < e; i++) { |
| 6348 | ObjCMethodDecl *MD = Methods[i]; |
| 6349 | if (i == 0) |
| 6350 | Result += "\t{{(struct objc_selector *)\""; |
| 6351 | else |
| 6352 | Result += "\t{(struct objc_selector *)\""; |
| 6353 | Result += (MD)->getSelector().getAsString(); Result += "\""; |
| 6354 | Result += ", "; |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 6355 | std::string MethodTypeString = Context->getObjCEncodingForMethodDecl(MD); |
Fariborz Jahanian | 4aaf8b1 | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6356 | Result += "\""; Result += MethodTypeString; Result += "\""; |
| 6357 | Result += ", "; |
Fariborz Jahanian | b1ba885 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6358 | if (!MethodImpl) |
| 6359 | Result += "0"; |
| 6360 | else { |
| 6361 | Result += "(void *)"; |
| 6362 | Result += RewriteObj.MethodInternalNames[MD]; |
| 6363 | } |
Fariborz Jahanian | 4aaf8b1 | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6364 | if (i == e-1) |
Fariborz Jahanian | b1ba885 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6365 | Result += "}}\n"; |
Fariborz Jahanian | 4aaf8b1 | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6366 | else |
Fariborz Jahanian | b1ba885 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6367 | Result += "},\n"; |
Fariborz Jahanian | 4aaf8b1 | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6368 | } |
| 6369 | Result += "};\n"; |
| 6370 | } |
| 6371 | } |
| 6372 | |
Fariborz Jahanian | fdc06e3 | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6373 | static void Write_prop_list_t_initializer(RewriteModernObjC &RewriteObj, |
Fariborz Jahanian | 67f7c55 | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 6374 | ASTContext *Context, std::string &Result, |
| 6375 | ArrayRef<ObjCPropertyDecl *> Properties, |
| 6376 | const Decl *Container, |
| 6377 | StringRef VarName, |
| 6378 | StringRef ProtocolName) { |
| 6379 | if (Properties.size() > 0) { |
| 6380 | Result += "\nstatic "; |
| 6381 | Write__prop_list_t_TypeDecl(Result, Properties.size()); |
| 6382 | Result += " "; Result += VarName; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6383 | Result += ProtocolName; |
Fariborz Jahanian | 67f7c55 | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 6384 | Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n"; |
| 6385 | Result += "\t"; Result += "sizeof(_prop_t)"; Result += ",\n"; |
| 6386 | Result += "\t"; Result += utostr(Properties.size()); Result += ",\n"; |
| 6387 | for (unsigned i = 0, e = Properties.size(); i < e; i++) { |
| 6388 | ObjCPropertyDecl *PropDecl = Properties[i]; |
| 6389 | if (i == 0) |
| 6390 | Result += "\t{{\""; |
| 6391 | else |
| 6392 | Result += "\t{\""; |
| 6393 | Result += PropDecl->getName(); Result += "\","; |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 6394 | std::string PropertyTypeString = |
| 6395 | Context->getObjCEncodingForPropertyDecl(PropDecl, Container); |
| 6396 | std::string QuotePropertyTypeString; |
Fariborz Jahanian | 67f7c55 | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 6397 | RewriteObj.QuoteDoublequotes(PropertyTypeString, QuotePropertyTypeString); |
| 6398 | Result += "\""; Result += QuotePropertyTypeString; Result += "\""; |
| 6399 | if (i == e-1) |
| 6400 | Result += "}}\n"; |
| 6401 | else |
| 6402 | Result += "},\n"; |
| 6403 | } |
| 6404 | Result += "};\n"; |
| 6405 | } |
| 6406 | } |
| 6407 | |
Fariborz Jahanian | 0a25688 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 6408 | // Metadata flags |
| 6409 | enum MetaDataDlags { |
| 6410 | CLS = 0x0, |
| 6411 | CLS_META = 0x1, |
| 6412 | CLS_ROOT = 0x2, |
| 6413 | OBJC2_CLS_HIDDEN = 0x10, |
| 6414 | CLS_EXCEPTION = 0x20, |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6415 | |
Fariborz Jahanian | 0a25688 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 6416 | /// (Obsolete) ARC-specific: this class has a .release_ivars method |
| 6417 | CLS_HAS_IVAR_RELEASER = 0x40, |
| 6418 | /// class was compiled with -fobjc-arr |
| 6419 | CLS_COMPILED_BY_ARC = 0x80 // (1<<7) |
| 6420 | }; |
| 6421 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6422 | static void Write__class_ro_t_initializer(ASTContext *Context, std::string &Result, |
| 6423 | unsigned int flags, |
| 6424 | const std::string &InstanceStart, |
Fariborz Jahanian | fdc06e3 | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6425 | const std::string &InstanceSize, |
| 6426 | ArrayRef<ObjCMethodDecl *>baseMethods, |
| 6427 | ArrayRef<ObjCProtocolDecl *>baseProtocols, |
| 6428 | ArrayRef<ObjCIvarDecl *>ivars, |
| 6429 | ArrayRef<ObjCPropertyDecl *>Properties, |
| 6430 | StringRef VarName, |
| 6431 | StringRef ClassName) { |
Fariborz Jahanian | fdc06e3 | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6432 | Result += "\nstatic struct _class_ro_t "; |
| 6433 | Result += VarName; Result += ClassName; |
| 6434 | Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6435 | Result += "\t"; |
| 6436 | Result += llvm::utostr(flags); Result += ", "; |
Fariborz Jahanian | fdc06e3 | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6437 | Result += InstanceStart; Result += ", "; |
| 6438 | Result += InstanceSize; Result += ", \n"; |
| 6439 | Result += "\t"; |
Fariborz Jahanian | 4548962 | 2012-03-14 18:09:23 +0000 | [diff] [blame] | 6440 | const llvm::Triple &Triple(Context->getTargetInfo().getTriple()); |
| 6441 | if (Triple.getArch() == llvm::Triple::x86_64) |
| 6442 | // uint32_t const reserved; // only when building for 64bit targets |
| 6443 | Result += "(unsigned int)0, \n\t"; |
Fariborz Jahanian | fdc06e3 | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6444 | // const uint8_t * const ivarLayout; |
| 6445 | Result += "0, \n\t"; |
| 6446 | Result += "\""; Result += ClassName; Result += "\",\n\t"; |
Fariborz Jahanian | 0a25688 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 6447 | bool metaclass = ((flags & CLS_META) != 0); |
Fariborz Jahanian | fdc06e3 | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6448 | if (baseMethods.size() > 0) { |
| 6449 | Result += "(const struct _method_list_t *)&"; |
Fariborz Jahanian | 0a25688 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 6450 | if (metaclass) |
| 6451 | Result += "_OBJC_$_CLASS_METHODS_"; |
| 6452 | else |
| 6453 | Result += "_OBJC_$_INSTANCE_METHODS_"; |
| 6454 | Result += ClassName; |
Fariborz Jahanian | fdc06e3 | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6455 | Result += ",\n\t"; |
| 6456 | } |
| 6457 | else |
| 6458 | Result += "0, \n\t"; |
| 6459 | |
Fariborz Jahanian | 0a25688 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 6460 | if (!metaclass && baseProtocols.size() > 0) { |
Fariborz Jahanian | fdc06e3 | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6461 | Result += "(const struct _objc_protocol_list *)&"; |
| 6462 | Result += "_OBJC_CLASS_PROTOCOLS_$_"; Result += ClassName; |
| 6463 | Result += ",\n\t"; |
| 6464 | } |
| 6465 | else |
| 6466 | Result += "0, \n\t"; |
| 6467 | |
Fariborz Jahanian | 0a25688 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 6468 | if (!metaclass && ivars.size() > 0) { |
Fariborz Jahanian | fdc06e3 | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6469 | Result += "(const struct _ivar_list_t *)&"; |
| 6470 | Result += "_OBJC_$_INSTANCE_VARIABLES_"; Result += ClassName; |
| 6471 | Result += ",\n\t"; |
| 6472 | } |
| 6473 | else |
| 6474 | Result += "0, \n\t"; |
| 6475 | |
| 6476 | // weakIvarLayout |
| 6477 | Result += "0, \n\t"; |
Fariborz Jahanian | 0a25688 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 6478 | if (!metaclass && Properties.size() > 0) { |
Fariborz Jahanian | fdc06e3 | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6479 | Result += "(const struct _prop_list_t *)&"; |
Fariborz Jahanian | c41d828 | 2012-02-16 21:57:59 +0000 | [diff] [blame] | 6480 | Result += "_OBJC_$_PROP_LIST_"; Result += ClassName; |
Fariborz Jahanian | fdc06e3 | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6481 | Result += ",\n"; |
| 6482 | } |
| 6483 | else |
| 6484 | Result += "0, \n"; |
| 6485 | |
| 6486 | Result += "};\n"; |
| 6487 | } |
| 6488 | |
Fariborz Jahanian | 638252f | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6489 | static void Write_class_t(ASTContext *Context, std::string &Result, |
| 6490 | StringRef VarName, |
Fariborz Jahanian | 40ca00d | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6491 | const ObjCInterfaceDecl *CDecl, bool metaclass) { |
| 6492 | bool rootClass = (!CDecl->getSuperClass()); |
| 6493 | const ObjCInterfaceDecl *RootClass = CDecl; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6494 | |
Fariborz Jahanian | 40ca00d | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6495 | if (!rootClass) { |
| 6496 | // Find the Root class |
| 6497 | RootClass = CDecl->getSuperClass(); |
| 6498 | while (RootClass->getSuperClass()) { |
| 6499 | RootClass = RootClass->getSuperClass(); |
| 6500 | } |
| 6501 | } |
| 6502 | |
| 6503 | if (metaclass && rootClass) { |
Fariborz Jahanian | 638252f | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6504 | // Need to handle a case of use of forward declaration. |
Fariborz Jahanian | fca6510 | 2012-03-10 18:25:06 +0000 | [diff] [blame] | 6505 | Result += "\n"; |
Fariborz Jahanian | 38c5910 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 6506 | Result += "extern \"C\" "; |
Fariborz Jahanian | fca6510 | 2012-03-10 18:25:06 +0000 | [diff] [blame] | 6507 | if (CDecl->getImplementation()) |
| 6508 | Result += "__declspec(dllexport) "; |
Fariborz Jahanian | f35e020 | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 6509 | else |
| 6510 | Result += "__declspec(dllimport) "; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6511 | |
Fariborz Jahanian | 38c5910 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 6512 | Result += "struct _class_t OBJC_CLASS_$_"; |
Fariborz Jahanian | 638252f | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6513 | Result += CDecl->getNameAsString(); |
| 6514 | Result += ";\n"; |
| 6515 | } |
| 6516 | // Also, for possibility of 'super' metadata class not having been defined yet. |
Fariborz Jahanian | 40ca00d | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6517 | if (!rootClass) { |
Fariborz Jahanian | 064b538 | 2012-03-29 19:04:10 +0000 | [diff] [blame] | 6518 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
Fariborz Jahanian | fca6510 | 2012-03-10 18:25:06 +0000 | [diff] [blame] | 6519 | Result += "\n"; |
Fariborz Jahanian | 38c5910 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 6520 | Result += "extern \"C\" "; |
Fariborz Jahanian | 064b538 | 2012-03-29 19:04:10 +0000 | [diff] [blame] | 6521 | if (SuperClass->getImplementation()) |
Fariborz Jahanian | fca6510 | 2012-03-10 18:25:06 +0000 | [diff] [blame] | 6522 | Result += "__declspec(dllexport) "; |
Fariborz Jahanian | f35e020 | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 6523 | else |
| 6524 | Result += "__declspec(dllimport) "; |
| 6525 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6526 | Result += "struct _class_t "; |
Fariborz Jahanian | fca6510 | 2012-03-10 18:25:06 +0000 | [diff] [blame] | 6527 | Result += VarName; |
Fariborz Jahanian | 064b538 | 2012-03-29 19:04:10 +0000 | [diff] [blame] | 6528 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | 638252f | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6529 | Result += ";\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6530 | |
Fariborz Jahanian | 064b538 | 2012-03-29 19:04:10 +0000 | [diff] [blame] | 6531 | if (metaclass && RootClass != SuperClass) { |
Fariborz Jahanian | 38c5910 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 6532 | Result += "extern \"C\" "; |
Fariborz Jahanian | 3546559 | 2012-03-20 21:09:58 +0000 | [diff] [blame] | 6533 | if (RootClass->getImplementation()) |
| 6534 | Result += "__declspec(dllexport) "; |
Fariborz Jahanian | f35e020 | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 6535 | else |
| 6536 | Result += "__declspec(dllimport) "; |
| 6537 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6538 | Result += "struct _class_t "; |
Fariborz Jahanian | 3546559 | 2012-03-20 21:09:58 +0000 | [diff] [blame] | 6539 | Result += VarName; |
| 6540 | Result += RootClass->getNameAsString(); |
| 6541 | Result += ";\n"; |
| 6542 | } |
Fariborz Jahanian | 638252f | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6543 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6544 | |
| 6545 | Result += "\nextern \"C\" __declspec(dllexport) struct _class_t "; |
Fariborz Jahanian | f35e020 | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 6546 | Result += VarName; Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 638252f | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6547 | Result += " __attribute__ ((used, section (\"__DATA,__objc_data\"))) = {\n"; |
| 6548 | Result += "\t"; |
Fariborz Jahanian | 40ca00d | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6549 | if (metaclass) { |
| 6550 | if (!rootClass) { |
| 6551 | Result += "0, // &"; Result += VarName; |
| 6552 | Result += RootClass->getNameAsString(); |
Fariborz Jahanian | 638252f | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6553 | Result += ",\n\t"; |
Fariborz Jahanian | 40ca00d | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6554 | Result += "0, // &"; Result += VarName; |
Fariborz Jahanian | 638252f | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6555 | Result += CDecl->getSuperClass()->getNameAsString(); |
| 6556 | Result += ",\n\t"; |
| 6557 | } |
| 6558 | else { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6559 | Result += "0, // &"; Result += VarName; |
Fariborz Jahanian | 638252f | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6560 | Result += CDecl->getNameAsString(); |
| 6561 | Result += ",\n\t"; |
Fariborz Jahanian | 40ca00d | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6562 | Result += "0, // &OBJC_CLASS_$_"; Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 638252f | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6563 | Result += ",\n\t"; |
| 6564 | } |
| 6565 | } |
| 6566 | else { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6567 | Result += "0, // &OBJC_METACLASS_$_"; |
Fariborz Jahanian | 638252f | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6568 | Result += CDecl->getNameAsString(); |
| 6569 | Result += ",\n\t"; |
Fariborz Jahanian | 40ca00d | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6570 | if (!rootClass) { |
| 6571 | Result += "0, // &"; Result += VarName; |
Fariborz Jahanian | 638252f | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6572 | Result += CDecl->getSuperClass()->getNameAsString(); |
| 6573 | Result += ",\n\t"; |
| 6574 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6575 | else |
Fariborz Jahanian | 638252f | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6576 | Result += "0,\n\t"; |
| 6577 | } |
Fariborz Jahanian | 40ca00d | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6578 | Result += "0, // (void *)&_objc_empty_cache,\n\t"; |
| 6579 | Result += "0, // unused, was (void *)&_objc_empty_vtable,\n\t"; |
| 6580 | if (metaclass) |
Fariborz Jahanian | 638252f | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6581 | Result += "&_OBJC_METACLASS_RO_$_"; |
| 6582 | else |
| 6583 | Result += "&_OBJC_CLASS_RO_$_"; |
| 6584 | Result += CDecl->getNameAsString(); |
| 6585 | Result += ",\n};\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6586 | |
Fariborz Jahanian | 40ca00d | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6587 | // Add static function to initialize some of the meta-data fields. |
| 6588 | // avoid doing it twice. |
| 6589 | if (metaclass) |
| 6590 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6591 | |
| 6592 | const ObjCInterfaceDecl *SuperClass = |
Fariborz Jahanian | 40ca00d | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6593 | rootClass ? CDecl : CDecl->getSuperClass(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6594 | |
Fariborz Jahanian | 40ca00d | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6595 | Result += "static void OBJC_CLASS_SETUP_$_"; |
| 6596 | Result += CDecl->getNameAsString(); |
| 6597 | Result += "(void ) {\n"; |
| 6598 | Result += "\tOBJC_METACLASS_$_"; Result += CDecl->getNameAsString(); |
| 6599 | Result += ".isa = "; Result += "&OBJC_METACLASS_$_"; |
Fariborz Jahanian | 3546559 | 2012-03-20 21:09:58 +0000 | [diff] [blame] | 6600 | Result += RootClass->getNameAsString(); Result += ";\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6601 | |
Fariborz Jahanian | 40ca00d | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6602 | Result += "\tOBJC_METACLASS_$_"; Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 3546559 | 2012-03-20 21:09:58 +0000 | [diff] [blame] | 6603 | Result += ".superclass = "; |
| 6604 | if (rootClass) |
| 6605 | Result += "&OBJC_CLASS_$_"; |
| 6606 | else |
| 6607 | Result += "&OBJC_METACLASS_$_"; |
| 6608 | |
Fariborz Jahanian | 40ca00d | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6609 | Result += SuperClass->getNameAsString(); Result += ";\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6610 | |
Fariborz Jahanian | 40ca00d | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6611 | Result += "\tOBJC_METACLASS_$_"; Result += CDecl->getNameAsString(); |
| 6612 | Result += ".cache = "; Result += "&_objc_empty_cache"; Result += ";\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6613 | |
Fariborz Jahanian | 40ca00d | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6614 | Result += "\tOBJC_CLASS_$_"; Result += CDecl->getNameAsString(); |
| 6615 | Result += ".isa = "; Result += "&OBJC_METACLASS_$_"; |
| 6616 | Result += CDecl->getNameAsString(); Result += ";\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6617 | |
Fariborz Jahanian | 40ca00d | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6618 | if (!rootClass) { |
| 6619 | Result += "\tOBJC_CLASS_$_"; Result += CDecl->getNameAsString(); |
| 6620 | Result += ".superclass = "; Result += "&OBJC_CLASS_$_"; |
| 6621 | Result += SuperClass->getNameAsString(); Result += ";\n"; |
| 6622 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6623 | |
Fariborz Jahanian | 40ca00d | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6624 | Result += "\tOBJC_CLASS_$_"; Result += CDecl->getNameAsString(); |
| 6625 | Result += ".cache = "; Result += "&_objc_empty_cache"; Result += ";\n"; |
| 6626 | Result += "}\n"; |
Fariborz Jahanian | 638252f | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6627 | } |
| 6628 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6629 | static void Write_category_t(RewriteModernObjC &RewriteObj, ASTContext *Context, |
Fariborz Jahanian | f205505 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6630 | std::string &Result, |
Fariborz Jahanian | 5ed21c3 | 2012-03-27 18:41:05 +0000 | [diff] [blame] | 6631 | ObjCCategoryDecl *CatDecl, |
Fariborz Jahanian | 07a423d | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 6632 | ObjCInterfaceDecl *ClassDecl, |
Fariborz Jahanian | f205505 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6633 | ArrayRef<ObjCMethodDecl *> InstanceMethods, |
| 6634 | ArrayRef<ObjCMethodDecl *> ClassMethods, |
| 6635 | ArrayRef<ObjCProtocolDecl *> RefedProtocols, |
| 6636 | ArrayRef<ObjCPropertyDecl *> ClassProperties) { |
Fariborz Jahanian | 5ed21c3 | 2012-03-27 18:41:05 +0000 | [diff] [blame] | 6637 | StringRef CatName = CatDecl->getName(); |
NAKAMURA Takumi | 3eb0edd | 2012-03-21 03:21:46 +0000 | [diff] [blame] | 6638 | StringRef ClassName = ClassDecl->getName(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6639 | // must declare an extern class object in case this class is not implemented |
Fariborz Jahanian | 320c88a | 2012-02-17 20:33:00 +0000 | [diff] [blame] | 6640 | // in this TU. |
Fariborz Jahanian | 07a423d | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 6641 | Result += "\n"; |
Fariborz Jahanian | 38c5910 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 6642 | Result += "extern \"C\" "; |
Fariborz Jahanian | 07a423d | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 6643 | if (ClassDecl->getImplementation()) |
| 6644 | Result += "__declspec(dllexport) "; |
Fariborz Jahanian | f35e020 | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 6645 | else |
| 6646 | Result += "__declspec(dllimport) "; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6647 | |
Fariborz Jahanian | 38c5910 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 6648 | Result += "struct _class_t "; |
Fariborz Jahanian | 320c88a | 2012-02-17 20:33:00 +0000 | [diff] [blame] | 6649 | Result += "OBJC_CLASS_$_"; Result += ClassName; |
| 6650 | Result += ";\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6651 | |
Fariborz Jahanian | f205505 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6652 | Result += "\nstatic struct _category_t "; |
| 6653 | Result += "_OBJC_$_CATEGORY_"; |
| 6654 | Result += ClassName; Result += "_$_"; Result += CatName; |
| 6655 | Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = \n"; |
| 6656 | Result += "{\n"; |
| 6657 | Result += "\t\""; Result += ClassName; Result += "\",\n"; |
Fariborz Jahanian | cd79a49 | 2012-03-20 21:41:28 +0000 | [diff] [blame] | 6658 | Result += "\t0, // &"; Result += "OBJC_CLASS_$_"; Result += ClassName; |
Fariborz Jahanian | f205505 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6659 | Result += ",\n"; |
| 6660 | if (InstanceMethods.size() > 0) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6661 | Result += "\t(const struct _method_list_t *)&"; |
Fariborz Jahanian | f205505 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6662 | Result += "_OBJC_$_CATEGORY_INSTANCE_METHODS_"; |
| 6663 | Result += ClassName; Result += "_$_"; Result += CatName; |
| 6664 | Result += ",\n"; |
| 6665 | } |
| 6666 | else |
| 6667 | Result += "\t0,\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6668 | |
Fariborz Jahanian | f205505 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6669 | if (ClassMethods.size() > 0) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6670 | Result += "\t(const struct _method_list_t *)&"; |
Fariborz Jahanian | f205505 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6671 | Result += "_OBJC_$_CATEGORY_CLASS_METHODS_"; |
| 6672 | Result += ClassName; Result += "_$_"; Result += CatName; |
| 6673 | Result += ",\n"; |
| 6674 | } |
| 6675 | else |
| 6676 | Result += "\t0,\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6677 | |
Fariborz Jahanian | f205505 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6678 | if (RefedProtocols.size() > 0) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6679 | Result += "\t(const struct _protocol_list_t *)&"; |
Fariborz Jahanian | f205505 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6680 | Result += "_OBJC_CATEGORY_PROTOCOLS_$_"; |
| 6681 | Result += ClassName; Result += "_$_"; Result += CatName; |
| 6682 | Result += ",\n"; |
| 6683 | } |
| 6684 | else |
| 6685 | Result += "\t0,\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6686 | |
Fariborz Jahanian | f205505 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6687 | if (ClassProperties.size() > 0) { |
| 6688 | Result += "\t(const struct _prop_list_t *)&"; Result += "_OBJC_$_PROP_LIST_"; |
| 6689 | Result += ClassName; Result += "_$_"; Result += CatName; |
| 6690 | Result += ",\n"; |
| 6691 | } |
| 6692 | else |
| 6693 | Result += "\t0,\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6694 | |
Fariborz Jahanian | f205505 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6695 | Result += "};\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6696 | |
Fariborz Jahanian | cd79a49 | 2012-03-20 21:41:28 +0000 | [diff] [blame] | 6697 | // Add static function to initialize the class pointer in the category structure. |
| 6698 | Result += "static void OBJC_CATEGORY_SETUP_$_"; |
| 6699 | Result += ClassDecl->getNameAsString(); |
| 6700 | Result += "_$_"; |
| 6701 | Result += CatName; |
| 6702 | Result += "(void ) {\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6703 | Result += "\t_OBJC_$_CATEGORY_"; |
Fariborz Jahanian | cd79a49 | 2012-03-20 21:41:28 +0000 | [diff] [blame] | 6704 | Result += ClassDecl->getNameAsString(); |
| 6705 | Result += "_$_"; |
| 6706 | Result += CatName; |
| 6707 | Result += ".cls = "; Result += "&OBJC_CLASS_$_"; Result += ClassName; |
| 6708 | Result += ";\n}\n"; |
Fariborz Jahanian | f205505 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6709 | } |
| 6710 | |
Fariborz Jahanian | a8395a6 | 2012-02-08 22:23:26 +0000 | [diff] [blame] | 6711 | static void Write__extendedMethodTypes_initializer(RewriteModernObjC &RewriteObj, |
| 6712 | ASTContext *Context, std::string &Result, |
| 6713 | ArrayRef<ObjCMethodDecl *> Methods, |
| 6714 | StringRef VarName, |
| 6715 | StringRef ProtocolName) { |
| 6716 | if (Methods.size() == 0) |
| 6717 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6718 | |
Fariborz Jahanian | a8395a6 | 2012-02-08 22:23:26 +0000 | [diff] [blame] | 6719 | Result += "\nstatic const char *"; |
| 6720 | Result += VarName; Result += ProtocolName; |
| 6721 | Result += " [] __attribute__ ((used, section (\"__DATA,__objc_const\"))) = \n"; |
| 6722 | Result += "{\n"; |
| 6723 | for (unsigned i = 0, e = Methods.size(); i < e; i++) { |
| 6724 | ObjCMethodDecl *MD = Methods[i]; |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 6725 | std::string MethodTypeString = |
| 6726 | Context->getObjCEncodingForMethodDecl(MD, true); |
| 6727 | std::string QuoteMethodTypeString; |
Fariborz Jahanian | a8395a6 | 2012-02-08 22:23:26 +0000 | [diff] [blame] | 6728 | RewriteObj.QuoteDoublequotes(MethodTypeString, QuoteMethodTypeString); |
| 6729 | Result += "\t\""; Result += QuoteMethodTypeString; Result += "\""; |
| 6730 | if (i == e-1) |
| 6731 | Result += "\n};\n"; |
| 6732 | else { |
| 6733 | Result += ",\n"; |
| 6734 | } |
| 6735 | } |
| 6736 | } |
| 6737 | |
Fariborz Jahanian | aaf4d69 | 2012-04-11 21:12:36 +0000 | [diff] [blame] | 6738 | static void Write_IvarOffsetVar(RewriteModernObjC &RewriteObj, |
| 6739 | ASTContext *Context, |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6740 | std::string &Result, |
| 6741 | ArrayRef<ObjCIvarDecl *> Ivars, |
Fariborz Jahanian | a854174a | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 6742 | ObjCInterfaceDecl *CDecl) { |
Fariborz Jahanian | 75e71b9 | 2012-02-13 20:59:02 +0000 | [diff] [blame] | 6743 | // FIXME. visibilty of offset symbols may have to be set; for Darwin |
| 6744 | // this is what happens: |
| 6745 | /** |
| 6746 | if (Ivar->getAccessControl() == ObjCIvarDecl::Private || |
| 6747 | Ivar->getAccessControl() == ObjCIvarDecl::Package || |
| 6748 | Class->getVisibility() == HiddenVisibility) |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 6749 | Visibility should be: HiddenVisibility; |
Fariborz Jahanian | 75e71b9 | 2012-02-13 20:59:02 +0000 | [diff] [blame] | 6750 | else |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 6751 | Visibility should be: DefaultVisibility; |
Fariborz Jahanian | 75e71b9 | 2012-02-13 20:59:02 +0000 | [diff] [blame] | 6752 | */ |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6753 | |
Fariborz Jahanian | 1c1da17 | 2012-02-13 21:34:45 +0000 | [diff] [blame] | 6754 | Result += "\n"; |
| 6755 | for (unsigned i =0, e = Ivars.size(); i < e; i++) { |
| 6756 | ObjCIvarDecl *IvarDecl = Ivars[i]; |
Fariborz Jahanian | e47bf2b | 2012-03-12 16:46:58 +0000 | [diff] [blame] | 6757 | if (Context->getLangOpts().MicrosoftExt) |
| 6758 | Result += "__declspec(allocate(\".objc_ivar$B\")) "; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6759 | |
Fariborz Jahanian | e47bf2b | 2012-03-12 16:46:58 +0000 | [diff] [blame] | 6760 | if (!Context->getLangOpts().MicrosoftExt || |
| 6761 | IvarDecl->getAccessControl() == ObjCIvarDecl::Private || |
Fariborz Jahanian | c9295ec | 2012-03-10 01:34:42 +0000 | [diff] [blame] | 6762 | IvarDecl->getAccessControl() == ObjCIvarDecl::Package) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6763 | Result += "extern \"C\" unsigned long int "; |
Fariborz Jahanian | 2677ded | 2012-03-10 00:53:02 +0000 | [diff] [blame] | 6764 | else |
Fariborz Jahanian | f35e020 | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 6765 | Result += "extern \"C\" __declspec(dllexport) unsigned long int "; |
Fariborz Jahanian | 57dd66b | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 6766 | if (Ivars[i]->isBitField()) |
| 6767 | RewriteObj.ObjCIvarBitfieldGroupOffset(IvarDecl, Result); |
| 6768 | else |
| 6769 | WriteInternalIvarName(CDecl, IvarDecl, Result); |
Fariborz Jahanian | 1c1da17 | 2012-02-13 21:34:45 +0000 | [diff] [blame] | 6770 | Result += " __attribute__ ((used, section (\"__DATA,__objc_ivar\")))"; |
| 6771 | Result += " = "; |
Fariborz Jahanian | aaf4d69 | 2012-04-11 21:12:36 +0000 | [diff] [blame] | 6772 | RewriteObj.RewriteIvarOffsetComputation(IvarDecl, Result); |
| 6773 | Result += ";\n"; |
Fariborz Jahanian | 57dd66b | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 6774 | if (Ivars[i]->isBitField()) { |
| 6775 | // skip over rest of the ivar bitfields. |
| 6776 | SKIP_BITFIELDS(i , e, Ivars); |
| 6777 | } |
Fariborz Jahanian | 75e71b9 | 2012-02-13 20:59:02 +0000 | [diff] [blame] | 6778 | } |
| 6779 | } |
| 6780 | |
Fariborz Jahanian | 6b83dae | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6781 | static void Write__ivar_list_t_initializer(RewriteModernObjC &RewriteObj, |
| 6782 | ASTContext *Context, std::string &Result, |
Fariborz Jahanian | 57dd66b | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 6783 | ArrayRef<ObjCIvarDecl *> OriginalIvars, |
Fariborz Jahanian | 6b83dae | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6784 | StringRef VarName, |
Fariborz Jahanian | a854174a | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 6785 | ObjCInterfaceDecl *CDecl) { |
Fariborz Jahanian | 57dd66b | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 6786 | if (OriginalIvars.size() > 0) { |
| 6787 | Write_IvarOffsetVar(RewriteObj, Context, Result, OriginalIvars, CDecl); |
| 6788 | SmallVector<ObjCIvarDecl *, 8> Ivars; |
| 6789 | // strip off all but the first ivar bitfield from each group of ivars. |
| 6790 | // Such ivars in the ivar list table will be replaced by their grouping struct |
| 6791 | // 'ivar'. |
| 6792 | for (unsigned i = 0, e = OriginalIvars.size(); i < e; i++) { |
| 6793 | if (OriginalIvars[i]->isBitField()) { |
| 6794 | Ivars.push_back(OriginalIvars[i]); |
| 6795 | // skip over rest of the ivar bitfields. |
| 6796 | SKIP_BITFIELDS(i , e, OriginalIvars); |
| 6797 | } |
| 6798 | else |
| 6799 | Ivars.push_back(OriginalIvars[i]); |
| 6800 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6801 | |
Fariborz Jahanian | 6b83dae | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6802 | Result += "\nstatic "; |
| 6803 | Write__ivar_list_t_TypeDecl(Result, Ivars.size()); |
| 6804 | Result += " "; Result += VarName; |
Fariborz Jahanian | a854174a | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 6805 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 6b83dae | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6806 | Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n"; |
| 6807 | Result += "\t"; Result += "sizeof(_ivar_t)"; Result += ",\n"; |
| 6808 | Result += "\t"; Result += utostr(Ivars.size()); Result += ",\n"; |
| 6809 | for (unsigned i =0, e = Ivars.size(); i < e; i++) { |
| 6810 | ObjCIvarDecl *IvarDecl = Ivars[i]; |
| 6811 | if (i == 0) |
| 6812 | Result += "\t{{"; |
| 6813 | else |
| 6814 | Result += "\t {"; |
Fariborz Jahanian | a854174a | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 6815 | Result += "(unsigned long int *)&"; |
Fariborz Jahanian | 57dd66b | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 6816 | if (Ivars[i]->isBitField()) |
| 6817 | RewriteObj.ObjCIvarBitfieldGroupOffset(IvarDecl, Result); |
| 6818 | else |
| 6819 | WriteInternalIvarName(CDecl, IvarDecl, Result); |
Fariborz Jahanian | 75e71b9 | 2012-02-13 20:59:02 +0000 | [diff] [blame] | 6820 | Result += ", "; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6821 | |
Fariborz Jahanian | 57dd66b | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 6822 | Result += "\""; |
| 6823 | if (Ivars[i]->isBitField()) |
| 6824 | RewriteObj.ObjCIvarBitfieldGroupDecl(Ivars[i], Result); |
| 6825 | else |
| 6826 | Result += IvarDecl->getName(); |
| 6827 | Result += "\", "; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6828 | |
Fariborz Jahanian | 57dd66b | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 6829 | QualType IVQT = IvarDecl->getType(); |
| 6830 | if (IvarDecl->isBitField()) |
| 6831 | IVQT = RewriteObj.GetGroupRecordTypeForObjCIvarBitfield(IvarDecl); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6832 | |
Fariborz Jahanian | 6b83dae | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6833 | std::string IvarTypeString, QuoteIvarTypeString; |
Fariborz Jahanian | 57dd66b | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 6834 | Context->getObjCEncodingForType(IVQT, IvarTypeString, |
Fariborz Jahanian | 6b83dae | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6835 | IvarDecl); |
| 6836 | RewriteObj.QuoteDoublequotes(IvarTypeString, QuoteIvarTypeString); |
| 6837 | Result += "\""; Result += QuoteIvarTypeString; Result += "\", "; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6838 | |
Fariborz Jahanian | 088959a | 2012-02-11 20:10:52 +0000 | [diff] [blame] | 6839 | // FIXME. this alignment represents the host alignment and need be changed to |
| 6840 | // represent the target alignment. |
Fariborz Jahanian | 57dd66b | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 6841 | unsigned Align = Context->getTypeAlign(IVQT)/8; |
Fariborz Jahanian | 088959a | 2012-02-11 20:10:52 +0000 | [diff] [blame] | 6842 | Align = llvm::Log2_32(Align); |
Fariborz Jahanian | 6b83dae | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6843 | Result += llvm::utostr(Align); Result += ", "; |
Fariborz Jahanian | 57dd66b | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 6844 | CharUnits Size = Context->getTypeSizeInChars(IVQT); |
Fariborz Jahanian | cb1d9f3 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 6845 | Result += llvm::utostr(Size.getQuantity()); |
Fariborz Jahanian | 6b83dae | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6846 | if (i == e-1) |
| 6847 | Result += "}}\n"; |
| 6848 | else |
| 6849 | Result += "},\n"; |
| 6850 | } |
| 6851 | Result += "};\n"; |
| 6852 | } |
| 6853 | } |
| 6854 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6855 | /// RewriteObjCProtocolMetaData - Rewrite protocols meta-data. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6856 | void RewriteModernObjC::RewriteObjCProtocolMetaData(ObjCProtocolDecl *PDecl, |
Fariborz Jahanian | e18961b | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 6857 | std::string &Result) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6858 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6859 | // Do not synthesize the protocol more than once. |
| 6860 | if (ObjCSynthesizedProtocols.count(PDecl->getCanonicalDecl())) |
| 6861 | return; |
Fariborz Jahanian | 4548962 | 2012-03-14 18:09:23 +0000 | [diff] [blame] | 6862 | WriteModernMetadataDeclarations(Context, Result); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6863 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6864 | if (ObjCProtocolDecl *Def = PDecl->getDefinition()) |
| 6865 | PDecl = Def; |
Fariborz Jahanian | e18961b | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 6866 | // Must write out all protocol definitions in current qualifier list, |
| 6867 | // and in their nested qualifiers before writing out current definition. |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 6868 | for (auto *I : PDecl->protocols()) |
| 6869 | RewriteObjCProtocolMetaData(I, Result); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6870 | |
Fariborz Jahanian | 4aaf8b1 | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6871 | // Construct method lists. |
| 6872 | std::vector<ObjCMethodDecl *> InstanceMethods, ClassMethods; |
| 6873 | std::vector<ObjCMethodDecl *> OptInstanceMethods, OptClassMethods; |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 6874 | for (auto *MD : PDecl->instance_methods()) { |
Fariborz Jahanian | 4aaf8b1 | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6875 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 6876 | OptInstanceMethods.push_back(MD); |
| 6877 | } else { |
| 6878 | InstanceMethods.push_back(MD); |
| 6879 | } |
| 6880 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6881 | |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 6882 | for (auto *MD : PDecl->class_methods()) { |
Fariborz Jahanian | 4aaf8b1 | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6883 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 6884 | OptClassMethods.push_back(MD); |
| 6885 | } else { |
| 6886 | ClassMethods.push_back(MD); |
| 6887 | } |
| 6888 | } |
Fariborz Jahanian | a8395a6 | 2012-02-08 22:23:26 +0000 | [diff] [blame] | 6889 | std::vector<ObjCMethodDecl *> AllMethods; |
| 6890 | for (unsigned i = 0, e = InstanceMethods.size(); i < e; i++) |
| 6891 | AllMethods.push_back(InstanceMethods[i]); |
| 6892 | for (unsigned i = 0, e = ClassMethods.size(); i < e; i++) |
| 6893 | AllMethods.push_back(ClassMethods[i]); |
| 6894 | for (unsigned i = 0, e = OptInstanceMethods.size(); i < e; i++) |
| 6895 | AllMethods.push_back(OptInstanceMethods[i]); |
| 6896 | for (unsigned i = 0, e = OptClassMethods.size(); i < e; i++) |
| 6897 | AllMethods.push_back(OptClassMethods[i]); |
| 6898 | |
| 6899 | Write__extendedMethodTypes_initializer(*this, Context, Result, |
| 6900 | AllMethods, |
| 6901 | "_OBJC_PROTOCOL_METHOD_TYPES_", |
| 6902 | PDecl->getNameAsString()); |
Fariborz Jahanian | e18961b | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 6903 | // Protocol's super protocol list |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6904 | SmallVector<ObjCProtocolDecl *, 8> SuperProtocols(PDecl->protocols()); |
Fariborz Jahanian | e18961b | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 6905 | Write_protocol_list_initializer(Context, Result, SuperProtocols, |
| 6906 | "_OBJC_PROTOCOL_REFS_", |
| 6907 | PDecl->getNameAsString()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6908 | |
| 6909 | Write_method_list_t_initializer(*this, Context, Result, InstanceMethods, |
Fariborz Jahanian | 4aaf8b1 | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6910 | "_OBJC_PROTOCOL_INSTANCE_METHODS_", |
Fariborz Jahanian | b1ba885 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6911 | PDecl->getNameAsString(), false); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6912 | |
| 6913 | Write_method_list_t_initializer(*this, Context, Result, ClassMethods, |
Fariborz Jahanian | 4aaf8b1 | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6914 | "_OBJC_PROTOCOL_CLASS_METHODS_", |
Fariborz Jahanian | b1ba885 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6915 | PDecl->getNameAsString(), false); |
Fariborz Jahanian | 4aaf8b1 | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6916 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6917 | Write_method_list_t_initializer(*this, Context, Result, OptInstanceMethods, |
Fariborz Jahanian | 4aaf8b1 | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6918 | "_OBJC_PROTOCOL_OPT_INSTANCE_METHODS_", |
Fariborz Jahanian | b1ba885 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6919 | PDecl->getNameAsString(), false); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6920 | |
| 6921 | Write_method_list_t_initializer(*this, Context, Result, OptClassMethods, |
Fariborz Jahanian | 4aaf8b1 | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6922 | "_OBJC_PROTOCOL_OPT_CLASS_METHODS_", |
Fariborz Jahanian | b1ba885 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6923 | PDecl->getNameAsString(), false); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6924 | |
Fariborz Jahanian | 67f7c55 | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 6925 | // Protocol's property metadata. |
Manman Ren | a7a8b1f | 2016-01-26 18:05:23 +0000 | [diff] [blame] | 6926 | SmallVector<ObjCPropertyDecl *, 8> ProtocolProperties( |
| 6927 | PDecl->instance_properties()); |
Fariborz Jahanian | fdc06e3 | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6928 | Write_prop_list_t_initializer(*this, Context, Result, ProtocolProperties, |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 6929 | /* Container */nullptr, |
Fariborz Jahanian | 67f7c55 | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 6930 | "_OBJC_PROTOCOL_PROPERTIES_", |
| 6931 | PDecl->getNameAsString()); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 6932 | |
Fariborz Jahanian | 4898580 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6933 | // Writer out root metadata for current protocol: struct _protocol_t |
Fariborz Jahanian | 008dfe2 | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 6934 | Result += "\n"; |
| 6935 | if (LangOpts.MicrosoftExt) |
Fariborz Jahanian | 1b08542 | 2012-04-14 17:13:08 +0000 | [diff] [blame] | 6936 | Result += "static "; |
Fariborz Jahanian | b31e3af | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 6937 | Result += "struct _protocol_t _OBJC_PROTOCOL_"; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6938 | Result += PDecl->getNameAsString(); |
Akira Hatanaka | 7f550f3 | 2016-02-11 06:36:35 +0000 | [diff] [blame] | 6939 | Result += " __attribute__ ((used)) = {\n"; |
Fariborz Jahanian | 4898580 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6940 | Result += "\t0,\n"; // id is; is null |
| 6941 | Result += "\t\""; Result += PDecl->getNameAsString(); Result += "\",\n"; |
Fariborz Jahanian | e18961b | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 6942 | if (SuperProtocols.size() > 0) { |
| 6943 | Result += "\t(const struct _protocol_list_t *)&"; Result += "_OBJC_PROTOCOL_REFS_"; |
| 6944 | Result += PDecl->getNameAsString(); Result += ",\n"; |
| 6945 | } |
| 6946 | else |
| 6947 | Result += "\t0,\n"; |
Fariborz Jahanian | 4898580 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6948 | if (InstanceMethods.size() > 0) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6949 | Result += "\t(const struct method_list_t *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
Fariborz Jahanian | 4898580 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6950 | Result += PDecl->getNameAsString(); Result += ",\n"; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6951 | } |
| 6952 | else |
Fariborz Jahanian | 4898580 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6953 | Result += "\t0,\n"; |
| 6954 | |
| 6955 | if (ClassMethods.size() > 0) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6956 | Result += "\t(const struct method_list_t *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
Fariborz Jahanian | 4898580 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6957 | Result += PDecl->getNameAsString(); Result += ",\n"; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6958 | } |
| 6959 | else |
Fariborz Jahanian | 4898580 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6960 | Result += "\t0,\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6961 | |
Fariborz Jahanian | 4898580 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6962 | if (OptInstanceMethods.size() > 0) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6963 | Result += "\t(const struct method_list_t *)&_OBJC_PROTOCOL_OPT_INSTANCE_METHODS_"; |
Fariborz Jahanian | 4898580 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6964 | Result += PDecl->getNameAsString(); Result += ",\n"; |
| 6965 | } |
| 6966 | else |
| 6967 | Result += "\t0,\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6968 | |
Fariborz Jahanian | 4898580 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6969 | if (OptClassMethods.size() > 0) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6970 | Result += "\t(const struct method_list_t *)&_OBJC_PROTOCOL_OPT_CLASS_METHODS_"; |
Fariborz Jahanian | 4898580 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6971 | Result += PDecl->getNameAsString(); Result += ",\n"; |
| 6972 | } |
| 6973 | else |
| 6974 | Result += "\t0,\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6975 | |
Fariborz Jahanian | 4898580 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6976 | if (ProtocolProperties.size() > 0) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6977 | Result += "\t(const struct _prop_list_t *)&_OBJC_PROTOCOL_PROPERTIES_"; |
Fariborz Jahanian | 4898580 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6978 | Result += PDecl->getNameAsString(); Result += ",\n"; |
| 6979 | } |
| 6980 | else |
| 6981 | Result += "\t0,\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6982 | |
Fariborz Jahanian | 4898580 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6983 | Result += "\t"; Result += "sizeof(_protocol_t)"; Result += ",\n"; |
| 6984 | Result += "\t0,\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6985 | |
Fariborz Jahanian | a8395a6 | 2012-02-08 22:23:26 +0000 | [diff] [blame] | 6986 | if (AllMethods.size() > 0) { |
| 6987 | Result += "\t(const char **)&"; Result += "_OBJC_PROTOCOL_METHOD_TYPES_"; |
| 6988 | Result += PDecl->getNameAsString(); |
| 6989 | Result += "\n};\n"; |
| 6990 | } |
| 6991 | else |
| 6992 | Result += "\t0\n};\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6993 | |
Fariborz Jahanian | ddddca3 | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 6994 | if (LangOpts.MicrosoftExt) |
Fariborz Jahanian | 1b08542 | 2012-04-14 17:13:08 +0000 | [diff] [blame] | 6995 | Result += "static "; |
Fariborz Jahanian | ddddca3 | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 6996 | Result += "struct _protocol_t *"; |
| 6997 | Result += "_OBJC_LABEL_PROTOCOL_$_"; Result += PDecl->getNameAsString(); |
| 6998 | Result += " = &_OBJC_PROTOCOL_"; Result += PDecl->getNameAsString(); |
| 6999 | Result += ";\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7000 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7001 | // Mark this protocol as having been generated. |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7002 | if (!ObjCSynthesizedProtocols.insert(PDecl->getCanonicalDecl()).second) |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7003 | llvm_unreachable("protocol already synthesized"); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7004 | } |
| 7005 | |
Fariborz Jahanian | 0a25688 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 7006 | /// hasObjCExceptionAttribute - Return true if this class or any super |
| 7007 | /// class has the __objc_exception__ attribute. |
| 7008 | /// FIXME. Move this to ASTContext.cpp as it is also used for IRGen. |
| 7009 | static bool hasObjCExceptionAttribute(ASTContext &Context, |
| 7010 | const ObjCInterfaceDecl *OID) { |
| 7011 | if (OID->hasAttr<ObjCExceptionAttr>()) |
| 7012 | return true; |
| 7013 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
| 7014 | return hasObjCExceptionAttribute(Context, Super); |
| 7015 | return false; |
| 7016 | } |
Fariborz Jahanian | fdc06e3 | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 7017 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7018 | void RewriteModernObjC::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
| 7019 | std::string &Result) { |
| 7020 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7021 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7022 | // Explicitly declared @interface's are already synthesized. |
Fariborz Jahanian | 6b83dae | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 7023 | if (CDecl->isImplicitInterfaceDecl()) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7024 | assert(false && |
Fariborz Jahanian | 6b83dae | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 7025 | "Legacy implicit interface rewriting not supported in moder abi"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7026 | |
Fariborz Jahanian | 4548962 | 2012-03-14 18:09:23 +0000 | [diff] [blame] | 7027 | WriteModernMetadataDeclarations(Context, Result); |
Fariborz Jahanian | 6b83dae | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 7028 | SmallVector<ObjCIvarDecl *, 8> IVars; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7029 | |
Fariborz Jahanian | 6b83dae | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 7030 | for (ObjCIvarDecl *IVD = CDecl->all_declared_ivar_begin(); |
| 7031 | IVD; IVD = IVD->getNextIvar()) { |
| 7032 | // Ignore unnamed bit-fields. |
| 7033 | if (!IVD->getDeclName()) |
| 7034 | continue; |
| 7035 | IVars.push_back(IVD); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7036 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7037 | |
| 7038 | Write__ivar_list_t_initializer(*this, Context, Result, IVars, |
Fariborz Jahanian | fdc06e3 | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 7039 | "_OBJC_$_INSTANCE_VARIABLES_", |
Fariborz Jahanian | a854174a | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 7040 | CDecl); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7041 | |
Fariborz Jahanian | b1ba885 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 7042 | // Build _objc_method_list for class's instance methods if needed |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 7043 | SmallVector<ObjCMethodDecl *, 32> InstanceMethods(IDecl->instance_methods()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7044 | |
Fariborz Jahanian | b1ba885 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 7045 | // If any of our property implementations have associated getters or |
| 7046 | // setters, produce metadata for them as well. |
Aaron Ballman | d85eff4 | 2014-03-14 15:02:45 +0000 | [diff] [blame] | 7047 | for (const auto *Prop : IDecl->property_impls()) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7048 | if (Prop->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
Fariborz Jahanian | b1ba885 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 7049 | continue; |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7050 | if (!Prop->getPropertyIvarDecl()) |
Fariborz Jahanian | b1ba885 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 7051 | continue; |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7052 | ObjCPropertyDecl *PD = Prop->getPropertyDecl(); |
Fariborz Jahanian | b1ba885 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 7053 | if (!PD) |
| 7054 | continue; |
| 7055 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
Fariborz Jahanian | f687e7b | 2012-05-03 22:52:13 +0000 | [diff] [blame] | 7056 | if (mustSynthesizeSetterGetterMethod(IDecl, PD, true /*getter*/)) |
Fariborz Jahanian | b1ba885 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 7057 | InstanceMethods.push_back(Getter); |
| 7058 | if (PD->isReadOnly()) |
| 7059 | continue; |
| 7060 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
Fariborz Jahanian | f687e7b | 2012-05-03 22:52:13 +0000 | [diff] [blame] | 7061 | if (mustSynthesizeSetterGetterMethod(IDecl, PD, false /*setter*/)) |
Fariborz Jahanian | b1ba885 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 7062 | InstanceMethods.push_back(Setter); |
| 7063 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7064 | |
Fariborz Jahanian | b1ba885 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 7065 | Write_method_list_t_initializer(*this, Context, Result, InstanceMethods, |
| 7066 | "_OBJC_$_INSTANCE_METHODS_", |
| 7067 | IDecl->getNameAsString(), true); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7068 | |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 7069 | SmallVector<ObjCMethodDecl *, 32> ClassMethods(IDecl->class_methods()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7070 | |
Fariborz Jahanian | b1ba885 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 7071 | Write_method_list_t_initializer(*this, Context, Result, ClassMethods, |
| 7072 | "_OBJC_$_CLASS_METHODS_", |
| 7073 | IDecl->getNameAsString(), true); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7074 | |
Fariborz Jahanian | bce36774 | 2012-02-14 19:31:35 +0000 | [diff] [blame] | 7075 | // Protocols referenced in class declaration? |
| 7076 | // Protocol's super protocol list |
| 7077 | std::vector<ObjCProtocolDecl *> RefedProtocols; |
| 7078 | const ObjCList<ObjCProtocolDecl> &Protocols = CDecl->getReferencedProtocols(); |
| 7079 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 7080 | E = Protocols.end(); |
| 7081 | I != E; ++I) { |
| 7082 | RefedProtocols.push_back(*I); |
| 7083 | // Must write out all protocol definitions in current qualifier list, |
| 7084 | // and in their nested qualifiers before writing out current definition. |
| 7085 | RewriteObjCProtocolMetaData(*I, Result); |
| 7086 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7087 | |
| 7088 | Write_protocol_list_initializer(Context, Result, |
Fariborz Jahanian | bce36774 | 2012-02-14 19:31:35 +0000 | [diff] [blame] | 7089 | RefedProtocols, |
| 7090 | "_OBJC_CLASS_PROTOCOLS_$_", |
| 7091 | IDecl->getNameAsString()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7092 | |
Fariborz Jahanian | fdc06e3 | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 7093 | // Protocol's property metadata. |
Manman Ren | a7a8b1f | 2016-01-26 18:05:23 +0000 | [diff] [blame] | 7094 | SmallVector<ObjCPropertyDecl *, 8> ClassProperties( |
| 7095 | CDecl->instance_properties()); |
Fariborz Jahanian | fdc06e3 | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 7096 | Write_prop_list_t_initializer(*this, Context, Result, ClassProperties, |
Fariborz Jahanian | ee1db7a | 2012-03-22 17:39:35 +0000 | [diff] [blame] | 7097 | /* Container */IDecl, |
Fariborz Jahanian | c41d828 | 2012-02-16 21:57:59 +0000 | [diff] [blame] | 7098 | "_OBJC_$_PROP_LIST_", |
Fariborz Jahanian | fdc06e3 | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 7099 | CDecl->getNameAsString()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7100 | |
Fariborz Jahanian | 0a25688 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 7101 | // Data for initializing _class_ro_t metaclass meta-data |
| 7102 | uint32_t flags = CLS_META; |
| 7103 | std::string InstanceSize; |
| 7104 | std::string InstanceStart; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7105 | |
Fariborz Jahanian | 0a25688 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 7106 | bool classIsHidden = CDecl->getVisibility() == HiddenVisibility; |
| 7107 | if (classIsHidden) |
| 7108 | flags |= OBJC2_CLS_HIDDEN; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7109 | |
Fariborz Jahanian | 0a25688 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 7110 | if (!CDecl->getSuperClass()) |
| 7111 | // class is root |
| 7112 | flags |= CLS_ROOT; |
| 7113 | InstanceSize = "sizeof(struct _class_t)"; |
| 7114 | InstanceStart = InstanceSize; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7115 | Write__class_ro_t_initializer(Context, Result, flags, |
Fariborz Jahanian | 0a25688 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 7116 | InstanceStart, InstanceSize, |
| 7117 | ClassMethods, |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 7118 | nullptr, |
| 7119 | nullptr, |
| 7120 | nullptr, |
Fariborz Jahanian | 0a25688 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 7121 | "_OBJC_METACLASS_RO_$_", |
| 7122 | CDecl->getNameAsString()); |
| 7123 | |
Fariborz Jahanian | fdc06e3 | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 7124 | // Data for initializing _class_ro_t meta-data |
Fariborz Jahanian | 0a25688 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 7125 | flags = CLS; |
| 7126 | if (classIsHidden) |
| 7127 | flags |= OBJC2_CLS_HIDDEN; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7128 | |
Fariborz Jahanian | 0a25688 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 7129 | if (hasObjCExceptionAttribute(*Context, CDecl)) |
| 7130 | flags |= CLS_EXCEPTION; |
| 7131 | |
Fariborz Jahanian | fdc06e3 | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 7132 | if (!CDecl->getSuperClass()) |
| 7133 | // class is root |
| 7134 | flags |= CLS_ROOT; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7135 | |
Fariborz Jahanian | 0a25688 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 7136 | InstanceSize.clear(); |
| 7137 | InstanceStart.clear(); |
Fariborz Jahanian | fdc06e3 | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 7138 | if (!ObjCSynthesizedStructs.count(CDecl)) { |
| 7139 | InstanceSize = "0"; |
| 7140 | InstanceStart = "0"; |
| 7141 | } |
| 7142 | else { |
| 7143 | InstanceSize = "sizeof(struct "; |
| 7144 | InstanceSize += CDecl->getNameAsString(); |
| 7145 | InstanceSize += "_IMPL)"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7146 | |
Fariborz Jahanian | fdc06e3 | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 7147 | ObjCIvarDecl *IVD = CDecl->all_declared_ivar_begin(); |
| 7148 | if (IVD) { |
Fariborz Jahanian | aaf4d69 | 2012-04-11 21:12:36 +0000 | [diff] [blame] | 7149 | RewriteIvarOffsetComputation(IVD, InstanceStart); |
Fariborz Jahanian | fdc06e3 | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 7150 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7151 | else |
Fariborz Jahanian | fdc06e3 | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 7152 | InstanceStart = InstanceSize; |
| 7153 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7154 | Write__class_ro_t_initializer(Context, Result, flags, |
Fariborz Jahanian | fdc06e3 | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 7155 | InstanceStart, InstanceSize, |
| 7156 | InstanceMethods, |
| 7157 | RefedProtocols, |
| 7158 | IVars, |
| 7159 | ClassProperties, |
| 7160 | "_OBJC_CLASS_RO_$_", |
| 7161 | CDecl->getNameAsString()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7162 | |
Fariborz Jahanian | 638252f | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 7163 | Write_class_t(Context, Result, |
| 7164 | "OBJC_METACLASS_$_", |
| 7165 | CDecl, /*metaclass*/true); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7166 | |
Fariborz Jahanian | 638252f | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 7167 | Write_class_t(Context, Result, |
| 7168 | "OBJC_CLASS_$_", |
| 7169 | CDecl, /*metaclass*/false); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7170 | |
Fariborz Jahanian | 07a423d | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 7171 | if (ImplementationIsNonLazy(IDecl)) |
| 7172 | DefinedNonLazyClasses.push_back(CDecl); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7173 | } |
| 7174 | |
Fariborz Jahanian | 5ed21c3 | 2012-03-27 18:41:05 +0000 | [diff] [blame] | 7175 | void RewriteModernObjC::RewriteClassSetupInitHook(std::string &Result) { |
| 7176 | int ClsDefCount = ClassImplementation.size(); |
| 7177 | if (!ClsDefCount) |
| 7178 | return; |
| 7179 | Result += "#pragma section(\".objc_inithooks$B\", long, read, write)\n"; |
| 7180 | Result += "__declspec(allocate(\".objc_inithooks$B\")) "; |
| 7181 | Result += "static void *OBJC_CLASS_SETUP[] = {\n"; |
| 7182 | for (int i = 0; i < ClsDefCount; i++) { |
| 7183 | ObjCImplementationDecl *IDecl = ClassImplementation[i]; |
| 7184 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
| 7185 | Result += "\t(void *)&OBJC_CLASS_SETUP_$_"; |
| 7186 | Result += CDecl->getName(); Result += ",\n"; |
| 7187 | } |
| 7188 | Result += "};\n"; |
| 7189 | } |
| 7190 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7191 | void RewriteModernObjC::RewriteMetaDataIntoBuffer(std::string &Result) { |
| 7192 | int ClsDefCount = ClassImplementation.size(); |
| 7193 | int CatDefCount = CategoryImplementation.size(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7194 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7195 | // For each implemented class, write out all its meta data. |
| 7196 | for (int i = 0; i < ClsDefCount; i++) |
| 7197 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7198 | |
Fariborz Jahanian | 5ed21c3 | 2012-03-27 18:41:05 +0000 | [diff] [blame] | 7199 | RewriteClassSetupInitHook(Result); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7200 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7201 | // For each implemented category, write out all its meta data. |
| 7202 | for (int i = 0; i < CatDefCount; i++) |
| 7203 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7204 | |
Fariborz Jahanian | 5ed21c3 | 2012-03-27 18:41:05 +0000 | [diff] [blame] | 7205 | RewriteCategorySetupInitHook(Result); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7206 | |
Fariborz Jahanian | e7a0c93 | 2012-02-17 00:06:14 +0000 | [diff] [blame] | 7207 | if (ClsDefCount > 0) { |
Fariborz Jahanian | 008dfe2 | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 7208 | if (LangOpts.MicrosoftExt) |
| 7209 | Result += "__declspec(allocate(\".objc_classlist$B\")) "; |
Fariborz Jahanian | e7a0c93 | 2012-02-17 00:06:14 +0000 | [diff] [blame] | 7210 | Result += "static struct _class_t *L_OBJC_LABEL_CLASS_$ ["; |
| 7211 | Result += llvm::utostr(ClsDefCount); Result += "]"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7212 | Result += |
Fariborz Jahanian | e7a0c93 | 2012-02-17 00:06:14 +0000 | [diff] [blame] | 7213 | " __attribute__((used, section (\"__DATA, __objc_classlist," |
| 7214 | "regular,no_dead_strip\")))= {\n"; |
| 7215 | for (int i = 0; i < ClsDefCount; i++) { |
| 7216 | Result += "\t&OBJC_CLASS_$_"; |
| 7217 | Result += ClassImplementation[i]->getNameAsString(); |
| 7218 | Result += ",\n"; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7219 | } |
Fariborz Jahanian | e7a0c93 | 2012-02-17 00:06:14 +0000 | [diff] [blame] | 7220 | Result += "};\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7221 | |
Fariborz Jahanian | 07a423d | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 7222 | if (!DefinedNonLazyClasses.empty()) { |
| 7223 | if (LangOpts.MicrosoftExt) |
| 7224 | Result += "__declspec(allocate(\".objc_nlclslist$B\")) \n"; |
| 7225 | Result += "static struct _class_t *_OBJC_LABEL_NONLAZY_CLASS_$[] = {\n\t"; |
| 7226 | for (unsigned i = 0, e = DefinedNonLazyClasses.size(); i < e; i++) { |
| 7227 | Result += "\t&OBJC_CLASS_$_"; Result += DefinedNonLazyClasses[i]->getNameAsString(); |
| 7228 | Result += ",\n"; |
| 7229 | } |
| 7230 | Result += "};\n"; |
| 7231 | } |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7232 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7233 | |
Fariborz Jahanian | f205505 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7234 | if (CatDefCount > 0) { |
Fariborz Jahanian | 008dfe2 | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 7235 | if (LangOpts.MicrosoftExt) |
| 7236 | Result += "__declspec(allocate(\".objc_catlist$B\")) "; |
Fariborz Jahanian | f205505 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7237 | Result += "static struct _category_t *L_OBJC_LABEL_CATEGORY_$ ["; |
| 7238 | Result += llvm::utostr(CatDefCount); Result += "]"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7239 | Result += |
Fariborz Jahanian | f205505 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7240 | " __attribute__((used, section (\"__DATA, __objc_catlist," |
| 7241 | "regular,no_dead_strip\")))= {\n"; |
| 7242 | for (int i = 0; i < CatDefCount; i++) { |
| 7243 | Result += "\t&_OBJC_$_CATEGORY_"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7244 | Result += |
| 7245 | CategoryImplementation[i]->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | f205505 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7246 | Result += "_$_"; |
| 7247 | Result += CategoryImplementation[i]->getNameAsString(); |
| 7248 | Result += ",\n"; |
| 7249 | } |
| 7250 | Result += "};\n"; |
| 7251 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7252 | |
Fariborz Jahanian | 07a423d | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 7253 | if (!DefinedNonLazyCategories.empty()) { |
| 7254 | if (LangOpts.MicrosoftExt) |
| 7255 | Result += "__declspec(allocate(\".objc_nlcatlist$B\")) \n"; |
| 7256 | Result += "static struct _category_t *_OBJC_LABEL_NONLAZY_CATEGORY_$[] = {\n\t"; |
| 7257 | for (unsigned i = 0, e = DefinedNonLazyCategories.size(); i < e; i++) { |
| 7258 | Result += "\t&_OBJC_$_CATEGORY_"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7259 | Result += |
| 7260 | DefinedNonLazyCategories[i]->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | 07a423d | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 7261 | Result += "_$_"; |
| 7262 | Result += DefinedNonLazyCategories[i]->getNameAsString(); |
| 7263 | Result += ",\n"; |
| 7264 | } |
| 7265 | Result += "};\n"; |
| 7266 | } |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7267 | } |
| 7268 | |
Fariborz Jahanian | ddddca3 | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 7269 | void RewriteModernObjC::WriteImageInfo(std::string &Result) { |
| 7270 | if (LangOpts.MicrosoftExt) |
| 7271 | Result += "__declspec(allocate(\".objc_imageinfo$B\")) \n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7272 | |
Fariborz Jahanian | ddddca3 | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 7273 | Result += "static struct IMAGE_INFO { unsigned version; unsigned flag; } "; |
| 7274 | // version 0, ObjCABI is 2 |
Fariborz Jahanian | b31e3af | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 7275 | Result += "_OBJC_IMAGE_INFO = { 0, 2 };\n"; |
Fariborz Jahanian | ddddca3 | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 7276 | } |
| 7277 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7278 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
| 7279 | /// implementation. |
| 7280 | void RewriteModernObjC::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
| 7281 | std::string &Result) { |
Fariborz Jahanian | 4548962 | 2012-03-14 18:09:23 +0000 | [diff] [blame] | 7282 | WriteModernMetadataDeclarations(Context, Result); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7283 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
| 7284 | // Find category declaration for this implementation. |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 7285 | ObjCCategoryDecl *CDecl |
| 7286 | = ClassDecl->FindCategoryDeclaration(IDecl->getIdentifier()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7287 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7288 | std::string FullCategoryName = ClassDecl->getNameAsString(); |
Fariborz Jahanian | f205505 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7289 | FullCategoryName += "_$_"; |
| 7290 | FullCategoryName += CDecl->getNameAsString(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7291 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7292 | // Build _objc_method_list for class's instance methods if needed |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 7293 | SmallVector<ObjCMethodDecl *, 32> InstanceMethods(IDecl->instance_methods()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7294 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7295 | // If any of our property implementations have associated getters or |
| 7296 | // setters, produce metadata for them as well. |
Aaron Ballman | d85eff4 | 2014-03-14 15:02:45 +0000 | [diff] [blame] | 7297 | for (const auto *Prop : IDecl->property_impls()) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7298 | if (Prop->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7299 | continue; |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7300 | if (!Prop->getPropertyIvarDecl()) |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7301 | continue; |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7302 | ObjCPropertyDecl *PD = Prop->getPropertyDecl(); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7303 | if (!PD) |
| 7304 | continue; |
| 7305 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 7306 | InstanceMethods.push_back(Getter); |
| 7307 | if (PD->isReadOnly()) |
| 7308 | continue; |
| 7309 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 7310 | InstanceMethods.push_back(Setter); |
| 7311 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7312 | |
Fariborz Jahanian | f205505 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7313 | Write_method_list_t_initializer(*this, Context, Result, InstanceMethods, |
| 7314 | "_OBJC_$_CATEGORY_INSTANCE_METHODS_", |
| 7315 | FullCategoryName, true); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7316 | |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 7317 | SmallVector<ObjCMethodDecl *, 32> ClassMethods(IDecl->class_methods()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7318 | |
Fariborz Jahanian | f205505 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7319 | Write_method_list_t_initializer(*this, Context, Result, ClassMethods, |
| 7320 | "_OBJC_$_CATEGORY_CLASS_METHODS_", |
| 7321 | FullCategoryName, true); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7322 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7323 | // Protocols referenced in class declaration? |
Fariborz Jahanian | f205505 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7324 | // Protocol's super protocol list |
Aaron Ballman | 19a4176 | 2014-03-14 12:55:57 +0000 | [diff] [blame] | 7325 | SmallVector<ObjCProtocolDecl *, 8> RefedProtocols(CDecl->protocols()); |
| 7326 | for (auto *I : CDecl->protocols()) |
Fariborz Jahanian | f205505 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7327 | // Must write out all protocol definitions in current qualifier list, |
| 7328 | // and in their nested qualifiers before writing out current definition. |
Aaron Ballman | 19a4176 | 2014-03-14 12:55:57 +0000 | [diff] [blame] | 7329 | RewriteObjCProtocolMetaData(I, Result); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7330 | |
| 7331 | Write_protocol_list_initializer(Context, Result, |
Fariborz Jahanian | f205505 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7332 | RefedProtocols, |
| 7333 | "_OBJC_CATEGORY_PROTOCOLS_$_", |
| 7334 | FullCategoryName); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7335 | |
Fariborz Jahanian | f205505 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7336 | // Protocol's property metadata. |
Manman Ren | a7a8b1f | 2016-01-26 18:05:23 +0000 | [diff] [blame] | 7337 | SmallVector<ObjCPropertyDecl *, 8> ClassProperties( |
| 7338 | CDecl->instance_properties()); |
Fariborz Jahanian | f205505 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7339 | Write_prop_list_t_initializer(*this, Context, Result, ClassProperties, |
Fariborz Jahanian | e9863b5 | 2012-05-03 23:19:33 +0000 | [diff] [blame] | 7340 | /* Container */IDecl, |
Fariborz Jahanian | f205505 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7341 | "_OBJC_$_PROP_LIST_", |
| 7342 | FullCategoryName); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7343 | |
Fariborz Jahanian | f205505 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7344 | Write_category_t(*this, Context, Result, |
Fariborz Jahanian | 5ed21c3 | 2012-03-27 18:41:05 +0000 | [diff] [blame] | 7345 | CDecl, |
Fariborz Jahanian | 07a423d | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 7346 | ClassDecl, |
Fariborz Jahanian | f205505 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7347 | InstanceMethods, |
| 7348 | ClassMethods, |
| 7349 | RefedProtocols, |
| 7350 | ClassProperties); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7351 | |
Fariborz Jahanian | 07a423d | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 7352 | // Determine if this category is also "non-lazy". |
| 7353 | if (ImplementationIsNonLazy(IDecl)) |
| 7354 | DefinedNonLazyCategories.push_back(CDecl); |
Fariborz Jahanian | 5ed21c3 | 2012-03-27 18:41:05 +0000 | [diff] [blame] | 7355 | } |
| 7356 | |
| 7357 | void RewriteModernObjC::RewriteCategorySetupInitHook(std::string &Result) { |
| 7358 | int CatDefCount = CategoryImplementation.size(); |
| 7359 | if (!CatDefCount) |
| 7360 | return; |
| 7361 | Result += "#pragma section(\".objc_inithooks$B\", long, read, write)\n"; |
| 7362 | Result += "__declspec(allocate(\".objc_inithooks$B\")) "; |
| 7363 | Result += "static void *OBJC_CATEGORY_SETUP[] = {\n"; |
| 7364 | for (int i = 0; i < CatDefCount; i++) { |
| 7365 | ObjCCategoryImplDecl *IDecl = CategoryImplementation[i]; |
| 7366 | ObjCCategoryDecl *CatDecl= IDecl->getCategoryDecl(); |
| 7367 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
| 7368 | Result += "\t(void *)&OBJC_CATEGORY_SETUP_$_"; |
| 7369 | Result += ClassDecl->getName(); |
| 7370 | Result += "_$_"; |
| 7371 | Result += CatDecl->getName(); |
| 7372 | Result += ",\n"; |
| 7373 | } |
| 7374 | Result += "};\n"; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7375 | } |
| 7376 | |
| 7377 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
| 7378 | /// class methods. |
| 7379 | template<typename MethodIterator> |
| 7380 | void RewriteModernObjC::RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 7381 | MethodIterator MethodEnd, |
| 7382 | bool IsInstanceMethod, |
| 7383 | StringRef prefix, |
| 7384 | StringRef ClassName, |
| 7385 | std::string &Result) { |
| 7386 | if (MethodBegin == MethodEnd) return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7387 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7388 | if (!objc_impl_method) { |
| 7389 | /* struct _objc_method { |
| 7390 | SEL _cmd; |
| 7391 | char *method_types; |
| 7392 | void *_imp; |
| 7393 | } |
| 7394 | */ |
| 7395 | Result += "\nstruct _objc_method {\n"; |
| 7396 | Result += "\tSEL _cmd;\n"; |
| 7397 | Result += "\tchar *method_types;\n"; |
| 7398 | Result += "\tvoid *_imp;\n"; |
| 7399 | Result += "};\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7400 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7401 | objc_impl_method = true; |
| 7402 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7403 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7404 | // Build _objc_method_list for class's methods if needed |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7405 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7406 | /* struct { |
| 7407 | struct _objc_method_list *next_method; |
| 7408 | int method_count; |
| 7409 | struct _objc_method method_list[]; |
| 7410 | } |
| 7411 | */ |
| 7412 | unsigned NumMethods = std::distance(MethodBegin, MethodEnd); |
Fariborz Jahanian | 008dfe2 | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 7413 | Result += "\n"; |
| 7414 | if (LangOpts.MicrosoftExt) { |
| 7415 | if (IsInstanceMethod) |
| 7416 | Result += "__declspec(allocate(\".inst_meth$B\")) "; |
| 7417 | else |
| 7418 | Result += "__declspec(allocate(\".cls_meth$B\")) "; |
| 7419 | } |
| 7420 | Result += "static struct {\n"; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7421 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 7422 | Result += "\tint method_count;\n"; |
| 7423 | Result += "\tstruct _objc_method method_list["; |
| 7424 | Result += utostr(NumMethods); |
| 7425 | Result += "];\n} _OBJC_"; |
| 7426 | Result += prefix; |
| 7427 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 7428 | Result += "_METHODS_"; |
| 7429 | Result += ClassName; |
| 7430 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
| 7431 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 7432 | Result += "_meth\")))= "; |
| 7433 | Result += "{\n\t0, " + utostr(NumMethods) + "\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7434 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7435 | Result += "\t,{{(SEL)\""; |
| 7436 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
| 7437 | std::string MethodTypeString; |
| 7438 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
| 7439 | Result += "\", \""; |
| 7440 | Result += MethodTypeString; |
| 7441 | Result += "\", (void *)"; |
| 7442 | Result += MethodInternalNames[*MethodBegin]; |
| 7443 | Result += "}\n"; |
| 7444 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 7445 | Result += "\t ,{(SEL)\""; |
| 7446 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
| 7447 | std::string MethodTypeString; |
| 7448 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
| 7449 | Result += "\", \""; |
| 7450 | Result += MethodTypeString; |
| 7451 | Result += "\", (void *)"; |
| 7452 | Result += MethodInternalNames[*MethodBegin]; |
| 7453 | Result += "}\n"; |
| 7454 | } |
| 7455 | Result += "\t }\n};\n"; |
| 7456 | } |
| 7457 | |
| 7458 | Stmt *RewriteModernObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV) { |
| 7459 | SourceRange OldRange = IV->getSourceRange(); |
| 7460 | Expr *BaseExpr = IV->getBase(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7461 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7462 | // Rewrite the base, but without actually doing replaces. |
| 7463 | { |
| 7464 | DisableReplaceStmtScope S(*this); |
| 7465 | BaseExpr = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(BaseExpr)); |
| 7466 | IV->setBase(BaseExpr); |
| 7467 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7468 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7469 | ObjCIvarDecl *D = IV->getDecl(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7470 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7471 | Expr *Replacement = IV; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7472 | |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7473 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
| 7474 | const ObjCInterfaceType *iFaceDecl = |
Fariborz Jahanian | 89919cc | 2012-05-08 23:54:35 +0000 | [diff] [blame] | 7475 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7476 | assert(iFaceDecl && "RewriteObjCIvarRefExpr - iFaceDecl is null"); |
| 7477 | // lookup which class implements the instance variable. |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 7478 | ObjCInterfaceDecl *clsDeclared = nullptr; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7479 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
| 7480 | clsDeclared); |
| 7481 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7482 | |
Fariborz Jahanian | c481c0c | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7483 | // Build name of symbol holding ivar offset. |
Fariborz Jahanian | a854174a | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 7484 | std::string IvarOffsetName; |
Fariborz Jahanian | 57dd66b | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 7485 | if (D->isBitField()) |
| 7486 | ObjCIvarBitfieldGroupOffset(D, IvarOffsetName); |
| 7487 | else |
| 7488 | WriteInternalIvarName(clsDeclared, D, IvarOffsetName); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7489 | |
Fariborz Jahanian | 5e49eb9 | 2012-02-22 18:13:25 +0000 | [diff] [blame] | 7490 | ReferencedIvars[clsDeclared].insert(D); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7491 | |
Fariborz Jahanian | c481c0c | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7492 | // cast offset to "char *". |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7493 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, |
Fariborz Jahanian | c481c0c | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7494 | Context->getPointerType(Context->CharTy), |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7495 | CK_BitCast, |
Fariborz Jahanian | c481c0c | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7496 | BaseExpr); |
| 7497 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
| 7498 | SourceLocation(), &Context->Idents.get(IvarOffsetName), |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 7499 | Context->UnsignedLongTy, nullptr, |
| 7500 | SC_Extern); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 7501 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, false, |
| 7502 | Context->UnsignedLongTy, VK_LValue, |
Fariborz Jahanian | c481c0c | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7503 | SourceLocation()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7504 | BinaryOperator *addExpr = |
| 7505 | new (Context) BinaryOperator(castExpr, DRE, BO_Add, |
Fariborz Jahanian | c481c0c | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7506 | Context->getPointerType(Context->CharTy), |
Adam Nemet | 484aa45 | 2017-03-27 19:17:25 +0000 | [diff] [blame] | 7507 | VK_RValue, OK_Ordinary, SourceLocation(), FPOptions()); |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7508 | // Don't forget the parens to enforce the proper binding. |
Fariborz Jahanian | c481c0c | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7509 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), |
| 7510 | SourceLocation(), |
| 7511 | addExpr); |
Fariborz Jahanian | dd5a59b | 2012-02-24 17:35:35 +0000 | [diff] [blame] | 7512 | QualType IvarT = D->getType(); |
Fariborz Jahanian | 57dd66b | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 7513 | if (D->isBitField()) |
| 7514 | IvarT = GetGroupRecordTypeForObjCIvarBitfield(D); |
Fariborz Jahanian | bf217c8 | 2012-04-27 22:48:54 +0000 | [diff] [blame] | 7515 | |
Fariborz Jahanian | d7c6777 | 2012-05-02 17:34:59 +0000 | [diff] [blame] | 7516 | if (!isa<TypedefType>(IvarT) && IvarT->isRecordType()) { |
Fariborz Jahanian | bf217c8 | 2012-04-27 22:48:54 +0000 | [diff] [blame] | 7517 | RecordDecl *RD = IvarT->getAs<RecordType>()->getDecl(); |
Fariborz Jahanian | faded5b | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 7518 | RD = RD->getDefinition(); |
| 7519 | if (RD && !RD->getDeclName().getAsIdentifierInfo()) { |
Fariborz Jahanian | bf217c8 | 2012-04-27 22:48:54 +0000 | [diff] [blame] | 7520 | // decltype(((Foo_IMPL*)0)->bar) * |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7521 | ObjCContainerDecl *CDecl = |
Fariborz Jahanian | d7c6777 | 2012-05-02 17:34:59 +0000 | [diff] [blame] | 7522 | dyn_cast<ObjCContainerDecl>(D->getDeclContext()); |
| 7523 | // ivar in class extensions requires special treatment. |
| 7524 | if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) |
| 7525 | CDecl = CatDecl->getClassInterface(); |
| 7526 | std::string RecName = CDecl->getName(); |
Fariborz Jahanian | bf217c8 | 2012-04-27 22:48:54 +0000 | [diff] [blame] | 7527 | RecName += "_IMPL"; |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 7528 | RecordDecl *RD = RecordDecl::Create( |
| 7529 | *Context, TTK_Struct, TUDecl, SourceLocation(), SourceLocation(), |
| 7530 | &Context->Idents.get(RecName)); |
Fariborz Jahanian | bf217c8 | 2012-04-27 22:48:54 +0000 | [diff] [blame] | 7531 | QualType PtrStructIMPL = Context->getPointerType(Context->getTagDeclType(RD)); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7532 | unsigned UnsignedIntSize = |
Fariborz Jahanian | bf217c8 | 2012-04-27 22:48:54 +0000 | [diff] [blame] | 7533 | static_cast<unsigned>(Context->getTypeSize(Context->UnsignedIntTy)); |
| 7534 | Expr *Zero = IntegerLiteral::Create(*Context, |
| 7535 | llvm::APInt(UnsignedIntSize, 0), |
| 7536 | Context->UnsignedIntTy, SourceLocation()); |
| 7537 | Zero = NoTypeInfoCStyleCastExpr(Context, PtrStructIMPL, CK_BitCast, Zero); |
| 7538 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 7539 | Zero); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 7540 | FieldDecl *FD = FieldDecl::Create(*Context, nullptr, SourceLocation(), |
Fariborz Jahanian | bf217c8 | 2012-04-27 22:48:54 +0000 | [diff] [blame] | 7541 | SourceLocation(), |
| 7542 | &Context->Idents.get(D->getNameAsString()), |
Fariborz Jahanian | f8e68e2 | 2015-04-09 18:36:50 +0000 | [diff] [blame] | 7543 | IvarT, nullptr, |
| 7544 | /*BitWidth=*/nullptr, |
| 7545 | /*Mutable=*/true, ICIS_NoInit); |
| 7546 | MemberExpr *ME = new (Context) |
| 7547 | MemberExpr(PE, true, SourceLocation(), FD, SourceLocation(), |
| 7548 | FD->getType(), VK_LValue, OK_Ordinary); |
| 7549 | IvarT = Context->getDecltypeType(ME, ME->getType()); |
| 7550 | } |
| 7551 | } |
Fariborz Jahanian | 1dc712f | 2012-02-29 00:26:20 +0000 | [diff] [blame] | 7552 | convertObjCTypeToCStyleType(IvarT); |
Fariborz Jahanian | dd5a59b | 2012-02-24 17:35:35 +0000 | [diff] [blame] | 7553 | QualType castT = Context->getPointerType(IvarT); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7554 | |
| 7555 | castExpr = NoTypeInfoCStyleCastExpr(Context, |
Fariborz Jahanian | c481c0c | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7556 | castT, |
| 7557 | CK_BitCast, |
| 7558 | PE); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7559 | |
| 7560 | |
Fariborz Jahanian | 1dc712f | 2012-02-29 00:26:20 +0000 | [diff] [blame] | 7561 | Expr *Exp = new (Context) UnaryOperator(castExpr, UO_Deref, IvarT, |
Fariborz Jahanian | c481c0c | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7562 | VK_LValue, OK_Ordinary, |
Aaron Ballman | a503855 | 2018-01-09 13:07:03 +0000 | [diff] [blame] | 7563 | SourceLocation(), false); |
Fariborz Jahanian | c481c0c | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7564 | PE = new (Context) ParenExpr(OldRange.getBegin(), |
| 7565 | OldRange.getEnd(), |
| 7566 | Exp); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7567 | |
Fariborz Jahanian | 57dd66b | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 7568 | if (D->isBitField()) { |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 7569 | FieldDecl *FD = FieldDecl::Create(*Context, nullptr, SourceLocation(), |
Fariborz Jahanian | 57dd66b | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 7570 | SourceLocation(), |
| 7571 | &Context->Idents.get(D->getNameAsString()), |
Fariborz Jahanian | f8e68e2 | 2015-04-09 18:36:50 +0000 | [diff] [blame] | 7572 | D->getType(), nullptr, |
| 7573 | /*BitWidth=*/D->getBitWidth(), |
| 7574 | /*Mutable=*/true, ICIS_NoInit); |
| 7575 | MemberExpr *ME = new (Context) |
| 7576 | MemberExpr(PE, /*isArrow*/ false, SourceLocation(), FD, |
| 7577 | SourceLocation(), FD->getType(), VK_LValue, OK_Ordinary); |
| 7578 | Replacement = ME; |
| 7579 | |
| 7580 | } |
Fariborz Jahanian | 57dd66b | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 7581 | else |
| 7582 | Replacement = PE; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7583 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7584 | |
Fariborz Jahanian | c481c0c | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7585 | ReplaceStmtWithRange(IV, Replacement, OldRange); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7586 | return Replacement; |
Fariborz Jahanian | 1167190 | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7587 | } |
Alp Toker | 0621cb2 | 2014-07-16 16:48:33 +0000 | [diff] [blame] | 7588 | |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 7589 | #endif // CLANG_ENABLE_OBJC_REWRITER |