Fariborz Jahanian | 64cb63a | 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 | 305c613 | 2012-09-01 05:09:24 +0000 | [diff] [blame] | 14 | #include "clang/Rewrite/Frontend/ASTConsumers.h" |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 15 | #include "clang/AST/AST.h" |
| 16 | #include "clang/AST/ASTConsumer.h" |
Benjamin Kramer | 2fa67ef | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 17 | #include "clang/AST/Attr.h" |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 18 | #include "clang/AST/ParentMap.h" |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 19 | #include "clang/Basic/CharInfo.h" |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 20 | #include "clang/Basic/Diagnostic.h" |
Benjamin Kramer | 2fa67ef | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 21 | #include "clang/Basic/IdentifierTable.h" |
| 22 | #include "clang/Basic/SourceManager.h" |
Benjamin Kramer | 9852f58 | 2012-12-01 16:35:25 +0000 | [diff] [blame] | 23 | #include "clang/Basic/TargetInfo.h" |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 24 | #include "clang/Lex/Lexer.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 25 | #include "clang/Rewrite/Core/Rewriter.h" |
Benjamin Kramer | 2fa67ef | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/DenseSet.h" |
| 27 | #include "llvm/ADT/OwningPtr.h" |
| 28 | #include "llvm/ADT/SmallPtrSet.h" |
| 29 | #include "llvm/ADT/StringExtras.h" |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 30 | #include "llvm/Support/MemoryBuffer.h" |
| 31 | #include "llvm/Support/raw_ostream.h" |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 32 | |
| 33 | using namespace clang; |
| 34 | using llvm::utostr; |
| 35 | |
| 36 | namespace { |
| 37 | class RewriteModernObjC : public ASTConsumer { |
| 38 | protected: |
| 39 | |
| 40 | enum { |
| 41 | BLOCK_FIELD_IS_OBJECT = 3, /* id, NSObject, __attribute__((NSObject)), |
| 42 | block, ... */ |
| 43 | BLOCK_FIELD_IS_BLOCK = 7, /* a block variable */ |
| 44 | BLOCK_FIELD_IS_BYREF = 8, /* the on stack structure holding the |
| 45 | __block variable */ |
| 46 | BLOCK_FIELD_IS_WEAK = 16, /* declared __weak, only used in byref copy |
| 47 | helpers */ |
| 48 | BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose |
| 49 | support routines */ |
| 50 | BLOCK_BYREF_CURRENT_MAX = 256 |
| 51 | }; |
| 52 | |
| 53 | enum { |
| 54 | BLOCK_NEEDS_FREE = (1 << 24), |
| 55 | BLOCK_HAS_COPY_DISPOSE = (1 << 25), |
| 56 | BLOCK_HAS_CXX_OBJ = (1 << 26), |
| 57 | BLOCK_IS_GC = (1 << 27), |
| 58 | BLOCK_IS_GLOBAL = (1 << 28), |
| 59 | BLOCK_HAS_DESCRIPTOR = (1 << 29) |
| 60 | }; |
| 61 | static const int OBJC_ABI_VERSION = 7; |
| 62 | |
| 63 | Rewriter Rewrite; |
| 64 | DiagnosticsEngine &Diags; |
| 65 | const LangOptions &LangOpts; |
| 66 | ASTContext *Context; |
| 67 | SourceManager *SM; |
| 68 | TranslationUnitDecl *TUDecl; |
| 69 | FileID MainFileID; |
| 70 | const char *MainFileStart, *MainFileEnd; |
| 71 | Stmt *CurrentBody; |
| 72 | ParentMap *PropParentMap; // created lazily. |
| 73 | std::string InFileName; |
| 74 | raw_ostream* OutFile; |
| 75 | std::string Preamble; |
| 76 | |
| 77 | TypeDecl *ProtocolTypeDecl; |
| 78 | VarDecl *GlobalVarDecl; |
Fariborz Jahanian | df474ec | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 79 | Expr *GlobalConstructionExp; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 80 | unsigned RewriteFailedDiag; |
Fariborz Jahanian | d13c2c2 | 2012-03-22 19:54:39 +0000 | [diff] [blame] | 81 | unsigned GlobalBlockRewriteFailedDiag; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 82 | // ObjC string constant support. |
| 83 | unsigned NumObjCStringLiterals; |
| 84 | VarDecl *ConstantStringClassReference; |
| 85 | RecordDecl *NSStringRecord; |
| 86 | |
| 87 | // ObjC foreach break/continue generation support. |
| 88 | int BcLabelCount; |
| 89 | |
| 90 | unsigned TryFinallyContainsReturnDiag; |
| 91 | // Needed for super. |
| 92 | ObjCMethodDecl *CurMethodDef; |
| 93 | RecordDecl *SuperStructDecl; |
| 94 | RecordDecl *ConstantStringDecl; |
| 95 | |
| 96 | FunctionDecl *MsgSendFunctionDecl; |
| 97 | FunctionDecl *MsgSendSuperFunctionDecl; |
| 98 | FunctionDecl *MsgSendStretFunctionDecl; |
| 99 | FunctionDecl *MsgSendSuperStretFunctionDecl; |
| 100 | FunctionDecl *MsgSendFpretFunctionDecl; |
| 101 | FunctionDecl *GetClassFunctionDecl; |
| 102 | FunctionDecl *GetMetaClassFunctionDecl; |
| 103 | FunctionDecl *GetSuperClassFunctionDecl; |
| 104 | FunctionDecl *SelGetUidFunctionDecl; |
| 105 | FunctionDecl *CFStringFunctionDecl; |
| 106 | FunctionDecl *SuperContructorFunctionDecl; |
| 107 | FunctionDecl *CurFunctionDef; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 108 | |
| 109 | /* Misc. containers needed for meta-data rewrite. */ |
| 110 | SmallVector<ObjCImplementationDecl *, 8> ClassImplementation; |
| 111 | SmallVector<ObjCCategoryImplDecl *, 8> CategoryImplementation; |
| 112 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCSynthesizedStructs; |
| 113 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> ObjCSynthesizedProtocols; |
Fariborz Jahanian | cf4c60f | 2012-02-17 22:20:12 +0000 | [diff] [blame] | 114 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCWrittenInterfaces; |
Fariborz Jahanian | 8fba894 | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 115 | llvm::SmallPtrSet<TagDecl*, 32> GlobalDefinedTags; |
Fariborz Jahanian | cf4c60f | 2012-02-17 22:20:12 +0000 | [diff] [blame] | 116 | SmallVector<ObjCInterfaceDecl*, 32> ObjCInterfacesSeen; |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 117 | /// DefinedNonLazyClasses - List of defined "non-lazy" classes. |
| 118 | SmallVector<ObjCInterfaceDecl*, 8> DefinedNonLazyClasses; |
| 119 | |
| 120 | /// DefinedNonLazyCategories - List of defined "non-lazy" categories. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 121 | SmallVector<ObjCCategoryDecl *, 8> DefinedNonLazyCategories; |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 122 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 123 | SmallVector<Stmt *, 32> Stmts; |
| 124 | SmallVector<int, 8> ObjCBcLabelNo; |
| 125 | // Remember all the @protocol(<expr>) expressions. |
| 126 | llvm::SmallPtrSet<ObjCProtocolDecl *, 32> ProtocolExprDecls; |
| 127 | |
| 128 | llvm::DenseSet<uint64_t> CopyDestroyCache; |
| 129 | |
| 130 | // Block expressions. |
| 131 | SmallVector<BlockExpr *, 32> Blocks; |
| 132 | SmallVector<int, 32> InnerDeclRefsCount; |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 133 | SmallVector<DeclRefExpr *, 32> InnerDeclRefs; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 134 | |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 135 | SmallVector<DeclRefExpr *, 32> BlockDeclRefs; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 136 | |
Fariborz Jahanian | cd3b036 | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 137 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 138 | // Block related declarations. |
| 139 | SmallVector<ValueDecl *, 8> BlockByCopyDecls; |
| 140 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByCopyDeclsPtrSet; |
| 141 | SmallVector<ValueDecl *, 8> BlockByRefDecls; |
| 142 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByRefDeclsPtrSet; |
| 143 | llvm::DenseMap<ValueDecl *, unsigned> BlockByRefDeclNo; |
| 144 | llvm::SmallPtrSet<ValueDecl *, 8> ImportedBlockDecls; |
| 145 | llvm::SmallPtrSet<VarDecl *, 8> ImportedLocalExternalDecls; |
| 146 | |
| 147 | llvm::DenseMap<BlockExpr *, std::string> RewrittenBlockExprs; |
Fariborz Jahanian | 72c88f1 | 2012-02-22 18:13:25 +0000 | [diff] [blame] | 148 | llvm::DenseMap<ObjCInterfaceDecl *, |
| 149 | llvm::SmallPtrSet<ObjCIvarDecl *, 8> > ReferencedIvars; |
| 150 | |
Fariborz Jahanian | cd3b036 | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 151 | // ivar bitfield grouping containers |
| 152 | llvm::DenseSet<const ObjCInterfaceDecl *> ObjCInterefaceHasBitfieldGroups; |
| 153 | llvm::DenseMap<const ObjCIvarDecl* , unsigned> IvarGroupNumber; |
| 154 | // This container maps an <class, group number for ivar> tuple to the type |
| 155 | // of the struct where the bitfield belongs. |
| 156 | llvm::DenseMap<std::pair<const ObjCInterfaceDecl*, unsigned>, QualType> GroupRecordType; |
Fariborz Jahanian | 31c4a4b | 2013-02-07 22:50:40 +0000 | [diff] [blame] | 157 | SmallVector<FunctionDecl*, 32> FunctionDefinitionsSeen; |
Fariborz Jahanian | cd3b036 | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 158 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 159 | // This maps an original source AST to it's rewritten form. This allows |
| 160 | // us to avoid rewriting the same node twice (which is very uncommon). |
| 161 | // This is needed to support some of the exotic property rewriting. |
| 162 | llvm::DenseMap<Stmt *, Stmt *> ReplacedNodes; |
| 163 | |
| 164 | // Needed for header files being rewritten |
| 165 | bool IsHeader; |
| 166 | bool SilenceRewriteMacroWarning; |
Fariborz Jahanian | ada7191 | 2013-02-08 00:27:34 +0000 | [diff] [blame] | 167 | bool GenerateLineInfo; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 168 | bool objc_impl_method; |
| 169 | |
| 170 | bool DisableReplaceStmt; |
| 171 | class DisableReplaceStmtScope { |
| 172 | RewriteModernObjC &R; |
| 173 | bool SavedValue; |
| 174 | |
| 175 | public: |
| 176 | DisableReplaceStmtScope(RewriteModernObjC &R) |
| 177 | : R(R), SavedValue(R.DisableReplaceStmt) { |
| 178 | R.DisableReplaceStmt = true; |
| 179 | } |
| 180 | ~DisableReplaceStmtScope() { |
| 181 | R.DisableReplaceStmt = SavedValue; |
| 182 | } |
| 183 | }; |
| 184 | void InitializeCommon(ASTContext &context); |
| 185 | |
| 186 | public: |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 187 | llvm::DenseMap<ObjCMethodDecl*, std::string> MethodInternalNames; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 188 | // Top Level Driver code. |
| 189 | virtual bool HandleTopLevelDecl(DeclGroupRef D) { |
| 190 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
| 191 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(*I)) { |
| 192 | if (!Class->isThisDeclarationADefinition()) { |
| 193 | RewriteForwardClassDecl(D); |
| 194 | break; |
Fariborz Jahanian | cf4c60f | 2012-02-17 22:20:12 +0000 | [diff] [blame] | 195 | } else { |
| 196 | // Keep track of all interface declarations seen. |
Fariborz Jahanian | f329527 | 2012-02-24 21:42:38 +0000 | [diff] [blame] | 197 | ObjCInterfacesSeen.push_back(Class); |
Fariborz Jahanian | cf4c60f | 2012-02-17 22:20:12 +0000 | [diff] [blame] | 198 | break; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
| 202 | if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(*I)) { |
| 203 | if (!Proto->isThisDeclarationADefinition()) { |
| 204 | RewriteForwardProtocolDecl(D); |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | |
Fariborz Jahanian | 31c4a4b | 2013-02-07 22:50:40 +0000 | [diff] [blame] | 209 | if (FunctionDecl *FDecl = dyn_cast<FunctionDecl>(*I)) { |
| 210 | // Under modern abi, we cannot translate body of the function |
| 211 | // yet until all class extensions and its implementation is seen. |
| 212 | // This is because they may introduce new bitfields which must go |
| 213 | // into their grouping struct. |
| 214 | if (FDecl->isThisDeclarationADefinition() && |
| 215 | // Not c functions defined inside an objc container. |
| 216 | !FDecl->isTopLevelDeclInObjCContainer()) { |
| 217 | FunctionDefinitionsSeen.push_back(FDecl); |
| 218 | break; |
| 219 | } |
| 220 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 221 | HandleTopLevelSingleDecl(*I); |
| 222 | } |
| 223 | return true; |
| 224 | } |
| 225 | void HandleTopLevelSingleDecl(Decl *D); |
| 226 | void HandleDeclInMainFile(Decl *D); |
| 227 | RewriteModernObjC(std::string inFile, raw_ostream *OS, |
| 228 | DiagnosticsEngine &D, const LangOptions &LOpts, |
Fariborz Jahanian | ada7191 | 2013-02-08 00:27:34 +0000 | [diff] [blame] | 229 | bool silenceMacroWarn, bool LineInfo); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 230 | |
| 231 | ~RewriteModernObjC() {} |
| 232 | |
| 233 | virtual void HandleTranslationUnit(ASTContext &C); |
| 234 | |
| 235 | void ReplaceStmt(Stmt *Old, Stmt *New) { |
| 236 | Stmt *ReplacingStmt = ReplacedNodes[Old]; |
| 237 | |
| 238 | if (ReplacingStmt) |
| 239 | return; // We can't rewrite the same node twice. |
| 240 | |
| 241 | if (DisableReplaceStmt) |
| 242 | return; |
| 243 | |
| 244 | // If replacement succeeded or warning disabled return with no warning. |
| 245 | if (!Rewrite.ReplaceStmt(Old, New)) { |
| 246 | ReplacedNodes[Old] = New; |
| 247 | return; |
| 248 | } |
| 249 | if (SilenceRewriteMacroWarning) |
| 250 | return; |
| 251 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 252 | << Old->getSourceRange(); |
| 253 | } |
| 254 | |
| 255 | void ReplaceStmtWithRange(Stmt *Old, Stmt *New, SourceRange SrcRange) { |
| 256 | if (DisableReplaceStmt) |
| 257 | return; |
| 258 | |
| 259 | // Measure the old text. |
| 260 | int Size = Rewrite.getRangeSize(SrcRange); |
| 261 | if (Size == -1) { |
| 262 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 263 | << Old->getSourceRange(); |
| 264 | return; |
| 265 | } |
| 266 | // Get the new text. |
| 267 | std::string SStr; |
| 268 | llvm::raw_string_ostream S(SStr); |
Richard Smith | d1420c6 | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 269 | New->printPretty(S, 0, PrintingPolicy(LangOpts)); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 270 | const std::string &Str = S.str(); |
| 271 | |
| 272 | // If replacement succeeded or warning disabled return with no warning. |
| 273 | if (!Rewrite.ReplaceText(SrcRange.getBegin(), Size, Str)) { |
| 274 | ReplacedNodes[Old] = New; |
| 275 | return; |
| 276 | } |
| 277 | if (SilenceRewriteMacroWarning) |
| 278 | return; |
| 279 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 280 | << Old->getSourceRange(); |
| 281 | } |
| 282 | |
| 283 | void InsertText(SourceLocation Loc, StringRef Str, |
| 284 | bool InsertAfter = true) { |
| 285 | // If insertion succeeded or warning disabled return with no warning. |
| 286 | if (!Rewrite.InsertText(Loc, Str, InsertAfter) || |
| 287 | SilenceRewriteMacroWarning) |
| 288 | return; |
| 289 | |
| 290 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 291 | } |
| 292 | |
| 293 | void ReplaceText(SourceLocation Start, unsigned OrigLength, |
| 294 | StringRef Str) { |
| 295 | // If removal succeeded or warning disabled return with no warning. |
| 296 | if (!Rewrite.ReplaceText(Start, OrigLength, Str) || |
| 297 | SilenceRewriteMacroWarning) |
| 298 | return; |
| 299 | |
| 300 | Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag); |
| 301 | } |
| 302 | |
| 303 | // Syntactic Rewriting. |
| 304 | void RewriteRecordBody(RecordDecl *RD); |
| 305 | void RewriteInclude(); |
Fariborz Jahanian | 9620596 | 2012-11-06 17:30:23 +0000 | [diff] [blame] | 306 | void RewriteLineDirective(const Decl *D); |
Fariborz Jahanian | f616ae2 | 2012-11-06 23:25:49 +0000 | [diff] [blame] | 307 | void ConvertSourceLocationToLineDirective(SourceLocation Loc, |
| 308 | std::string &LineString); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 309 | void RewriteForwardClassDecl(DeclGroupRef D); |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 310 | void RewriteForwardClassDecl(const SmallVector<Decl *, 8> &DG); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 311 | void RewriteForwardClassEpilogue(ObjCInterfaceDecl *ClassDecl, |
| 312 | const std::string &typedefString); |
| 313 | void RewriteImplementations(); |
| 314 | void RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 315 | ObjCImplementationDecl *IMD, |
| 316 | ObjCCategoryImplDecl *CID); |
| 317 | void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl); |
| 318 | void RewriteImplementationDecl(Decl *Dcl); |
| 319 | void RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl, |
| 320 | ObjCMethodDecl *MDecl, std::string &ResultStr); |
| 321 | void RewriteTypeIntoString(QualType T, std::string &ResultStr, |
| 322 | const FunctionType *&FPRetType); |
| 323 | void RewriteByRefString(std::string &ResultStr, const std::string &Name, |
| 324 | ValueDecl *VD, bool def=false); |
| 325 | void RewriteCategoryDecl(ObjCCategoryDecl *Dcl); |
| 326 | void RewriteProtocolDecl(ObjCProtocolDecl *Dcl); |
| 327 | void RewriteForwardProtocolDecl(DeclGroupRef D); |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 328 | void RewriteForwardProtocolDecl(const SmallVector<Decl *, 8> &DG); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 329 | void RewriteMethodDeclaration(ObjCMethodDecl *Method); |
| 330 | void RewriteProperty(ObjCPropertyDecl *prop); |
| 331 | void RewriteFunctionDecl(FunctionDecl *FD); |
| 332 | void RewriteBlockPointerType(std::string& Str, QualType Type); |
| 333 | void RewriteBlockPointerTypeVariable(std::string& Str, ValueDecl *VD); |
Fariborz Jahanian | b75f8de | 2012-04-19 00:50:01 +0000 | [diff] [blame] | 334 | void RewriteBlockLiteralFunctionDecl(FunctionDecl *FD); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 335 | void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl); |
| 336 | void RewriteTypeOfDecl(VarDecl *VD); |
| 337 | void RewriteObjCQualifiedInterfaceTypes(Expr *E); |
Fariborz Jahanian | 163d3ce | 2012-05-08 23:54:35 +0000 | [diff] [blame] | 338 | |
| 339 | std::string getIvarAccessString(ObjCIvarDecl *D); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 340 | |
| 341 | // Expression Rewriting. |
| 342 | Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S); |
| 343 | Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp); |
| 344 | Stmt *RewritePropertyOrImplicitGetter(PseudoObjectExpr *Pseudo); |
| 345 | Stmt *RewritePropertyOrImplicitSetter(PseudoObjectExpr *Pseudo); |
| 346 | Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp); |
| 347 | Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp); |
| 348 | Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp); |
Fariborz Jahanian | 5594704 | 2012-03-27 20:17:30 +0000 | [diff] [blame] | 349 | Stmt *RewriteObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Exp); |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 350 | Stmt *RewriteObjCBoxedExpr(ObjCBoxedExpr *Exp); |
Fariborz Jahanian | 86cff60 | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 351 | Stmt *RewriteObjCArrayLiteralExpr(ObjCArrayLiteral *Exp); |
Fariborz Jahanian | e35abe1 | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 352 | Stmt *RewriteObjCDictionaryLiteralExpr(ObjCDictionaryLiteral *Exp); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 353 | Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 354 | Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S); |
Fariborz Jahanian | 042b91d | 2012-05-23 23:47:20 +0000 | [diff] [blame] | 355 | Stmt *RewriteObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 356 | Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
| 357 | Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S); |
| 358 | Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
| 359 | SourceLocation OrigEnd); |
| 360 | Stmt *RewriteBreakStmt(BreakStmt *S); |
| 361 | Stmt *RewriteContinueStmt(ContinueStmt *S); |
| 362 | void RewriteCastExpr(CStyleCastExpr *CE); |
Fariborz Jahanian | f1ee687 | 2012-04-10 00:08:18 +0000 | [diff] [blame] | 363 | void RewriteImplicitCastObjCExpr(CastExpr *IE); |
Fariborz Jahanian | b3f904f | 2012-04-03 17:35:38 +0000 | [diff] [blame] | 364 | void RewriteLinkageSpec(LinkageSpecDecl *LSD); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 365 | |
Fariborz Jahanian | cd3b036 | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 366 | // Computes ivar bitfield group no. |
| 367 | unsigned ObjCIvarBitfieldGroupNo(ObjCIvarDecl *IV); |
| 368 | // Names field decl. for ivar bitfield group. |
| 369 | void ObjCIvarBitfieldGroupDecl(ObjCIvarDecl *IV, std::string &Result); |
| 370 | // Names struct type for ivar bitfield group. |
| 371 | void ObjCIvarBitfieldGroupType(ObjCIvarDecl *IV, std::string &Result); |
| 372 | // Names symbol for ivar bitfield group field offset. |
| 373 | void ObjCIvarBitfieldGroupOffset(ObjCIvarDecl *IV, std::string &Result); |
| 374 | // Given an ivar bitfield, it builds (or finds) its group record type. |
| 375 | QualType GetGroupRecordTypeForObjCIvarBitfield(ObjCIvarDecl *IV); |
| 376 | QualType SynthesizeBitfieldGroupStructType( |
| 377 | ObjCIvarDecl *IV, |
| 378 | SmallVectorImpl<ObjCIvarDecl *> &IVars); |
| 379 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 380 | // Block rewriting. |
| 381 | void RewriteBlocksInFunctionProtoType(QualType funcType, NamedDecl *D); |
| 382 | |
| 383 | // Block specific rewrite rules. |
| 384 | void RewriteBlockPointerDecl(NamedDecl *VD); |
Fariborz Jahanian | 4fe261c | 2012-04-24 19:38:45 +0000 | [diff] [blame] | 385 | void RewriteByRefVar(VarDecl *VD, bool firstDecl, bool lastDecl); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 386 | Stmt *RewriteBlockDeclRefExpr(DeclRefExpr *VD); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 387 | Stmt *RewriteLocalVariableExternalStorage(DeclRefExpr *DRE); |
| 388 | void RewriteBlockPointerFunctionArgs(FunctionDecl *FD); |
| 389 | |
| 390 | void RewriteObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
| 391 | std::string &Result); |
| 392 | |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 393 | void RewriteObjCFieldDecl(FieldDecl *fieldDecl, std::string &Result); |
Fariborz Jahanian | b68258f | 2012-05-01 17:46:45 +0000 | [diff] [blame] | 394 | bool IsTagDefinedInsideClass(ObjCContainerDecl *IDecl, TagDecl *Tag, |
Fariborz Jahanian | 8fba894 | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 395 | bool &IsNamedDefinition); |
| 396 | void RewriteLocallyDefinedNamedAggregates(FieldDecl *fieldDecl, |
| 397 | std::string &Result); |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 398 | |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 399 | bool RewriteObjCFieldDeclType(QualType &Type, std::string &Result); |
| 400 | |
Fariborz Jahanian | 72c88f1 | 2012-02-22 18:13:25 +0000 | [diff] [blame] | 401 | void RewriteIvarOffsetSymbols(ObjCInterfaceDecl *CDecl, |
| 402 | std::string &Result); |
| 403 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 404 | virtual void Initialize(ASTContext &context); |
| 405 | |
Benjamin Kramer | 48d798c | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 406 | // Misc. AST transformation routines. Sometimes they end up calling |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 407 | // rewriting routines on the new ASTs. |
| 408 | CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD, |
| 409 | Expr **args, unsigned nargs, |
| 410 | SourceLocation StartLoc=SourceLocation(), |
| 411 | SourceLocation EndLoc=SourceLocation()); |
Fariborz Jahanian | be8d55c | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 412 | |
| 413 | Expr *SynthMsgSendStretCallExpr(FunctionDecl *MsgSendStretFlavor, |
| 414 | QualType msgSendType, |
| 415 | QualType returnType, |
| 416 | SmallVectorImpl<QualType> &ArgTypes, |
| 417 | SmallVectorImpl<Expr*> &MsgExprs, |
| 418 | ObjCMethodDecl *Method); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 419 | |
| 420 | Stmt *SynthMessageExpr(ObjCMessageExpr *Exp, |
| 421 | SourceLocation StartLoc=SourceLocation(), |
| 422 | SourceLocation EndLoc=SourceLocation()); |
| 423 | |
| 424 | void SynthCountByEnumWithState(std::string &buf); |
| 425 | void SynthMsgSendFunctionDecl(); |
| 426 | void SynthMsgSendSuperFunctionDecl(); |
| 427 | void SynthMsgSendStretFunctionDecl(); |
| 428 | void SynthMsgSendFpretFunctionDecl(); |
| 429 | void SynthMsgSendSuperStretFunctionDecl(); |
| 430 | void SynthGetClassFunctionDecl(); |
| 431 | void SynthGetMetaClassFunctionDecl(); |
| 432 | void SynthGetSuperClassFunctionDecl(); |
| 433 | void SynthSelGetUidFunctionDecl(); |
| 434 | void SynthSuperContructorFunctionDecl(); |
| 435 | |
| 436 | // Rewriting metadata |
| 437 | template<typename MethodIterator> |
| 438 | void RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 439 | MethodIterator MethodEnd, |
| 440 | bool IsInstanceMethod, |
| 441 | StringRef prefix, |
| 442 | StringRef ClassName, |
| 443 | std::string &Result); |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 444 | void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol, |
| 445 | std::string &Result); |
Fariborz Jahanian | b4ee880 | 2012-04-30 16:57:52 +0000 | [diff] [blame] | 446 | void RewriteObjCProtocolListMetaData( |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 447 | const ObjCList<ObjCProtocolDecl> &Prots, |
| 448 | StringRef prefix, StringRef ClassName, std::string &Result); |
Fariborz Jahanian | b4ee880 | 2012-04-30 16:57:52 +0000 | [diff] [blame] | 449 | void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 450 | std::string &Result); |
Fariborz Jahanian | b4ee880 | 2012-04-30 16:57:52 +0000 | [diff] [blame] | 451 | void RewriteClassSetupInitHook(std::string &Result); |
Fariborz Jahanian | e033578 | 2012-03-27 18:41:05 +0000 | [diff] [blame] | 452 | |
Fariborz Jahanian | b4ee880 | 2012-04-30 16:57:52 +0000 | [diff] [blame] | 453 | void RewriteMetaDataIntoBuffer(std::string &Result); |
| 454 | void WriteImageInfo(std::string &Result); |
| 455 | void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 456 | std::string &Result); |
Fariborz Jahanian | b4ee880 | 2012-04-30 16:57:52 +0000 | [diff] [blame] | 457 | void RewriteCategorySetupInitHook(std::string &Result); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 458 | |
| 459 | // Rewriting ivar |
Fariborz Jahanian | b4ee880 | 2012-04-30 16:57:52 +0000 | [diff] [blame] | 460 | void RewriteIvarOffsetComputation(ObjCIvarDecl *ivar, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 461 | std::string &Result); |
Fariborz Jahanian | b4ee880 | 2012-04-30 16:57:52 +0000 | [diff] [blame] | 462 | Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 463 | |
| 464 | |
| 465 | std::string SynthesizeByrefCopyDestroyHelper(VarDecl *VD, int flag); |
| 466 | std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
| 467 | StringRef funcName, std::string Tag); |
| 468 | std::string SynthesizeBlockFunc(BlockExpr *CE, int i, |
| 469 | StringRef funcName, std::string Tag); |
| 470 | std::string SynthesizeBlockImpl(BlockExpr *CE, |
| 471 | std::string Tag, std::string Desc); |
| 472 | std::string SynthesizeBlockDescriptor(std::string DescTag, |
| 473 | std::string ImplTag, |
| 474 | int i, StringRef funcName, |
| 475 | unsigned hasCopy); |
| 476 | Stmt *SynthesizeBlockCall(CallExpr *Exp, const Expr* BlockExp); |
| 477 | void SynthesizeBlockLiterals(SourceLocation FunLocStart, |
| 478 | StringRef FunName); |
| 479 | FunctionDecl *SynthBlockInitFunctionDecl(StringRef name); |
| 480 | Stmt *SynthBlockInitExpr(BlockExpr *Exp, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 481 | const SmallVector<DeclRefExpr *, 8> &InnerBlockDeclRefs); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 482 | |
| 483 | // Misc. helper routines. |
| 484 | QualType getProtocolType(); |
| 485 | void WarnAboutReturnGotoStmts(Stmt *S); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 486 | void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND); |
| 487 | void InsertBlockLiteralsWithinFunction(FunctionDecl *FD); |
| 488 | void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD); |
| 489 | |
| 490 | bool IsDeclStmtInForeachHeader(DeclStmt *DS); |
| 491 | void CollectBlockDeclRefInfo(BlockExpr *Exp); |
| 492 | void GetBlockDeclRefExprs(Stmt *S); |
| 493 | void GetInnerBlockDeclRefExprs(Stmt *S, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 494 | SmallVector<DeclRefExpr *, 8> &InnerBlockDeclRefs, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 495 | llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts); |
| 496 | |
| 497 | // We avoid calling Type::isBlockPointerType(), since it operates on the |
| 498 | // canonical type. We only care if the top-level type is a closure pointer. |
| 499 | bool isTopLevelBlockPointerType(QualType T) { |
| 500 | return isa<BlockPointerType>(T); |
| 501 | } |
| 502 | |
| 503 | /// convertBlockPointerToFunctionPointer - Converts a block-pointer type |
| 504 | /// to a function pointer type and upon success, returns true; false |
| 505 | /// otherwise. |
| 506 | bool convertBlockPointerToFunctionPointer(QualType &T) { |
| 507 | if (isTopLevelBlockPointerType(T)) { |
| 508 | const BlockPointerType *BPT = T->getAs<BlockPointerType>(); |
| 509 | T = Context->getPointerType(BPT->getPointeeType()); |
| 510 | return true; |
| 511 | } |
| 512 | return false; |
| 513 | } |
| 514 | |
Fariborz Jahanian | 164d6f8 | 2012-02-13 18:57:49 +0000 | [diff] [blame] | 515 | bool convertObjCTypeToCStyleType(QualType &T); |
| 516 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 517 | bool needToScanForQualifiers(QualType T); |
| 518 | QualType getSuperStructType(); |
| 519 | QualType getConstantStringStructType(); |
| 520 | QualType convertFunctionTypeOfBlocks(const FunctionType *FT); |
| 521 | bool BufferContainsPPDirectives(const char *startBuf, const char *endBuf); |
| 522 | |
| 523 | void convertToUnqualifiedObjCType(QualType &T) { |
Fariborz Jahanian | e35abe1 | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 524 | if (T->isObjCQualifiedIdType()) { |
| 525 | bool isConst = T.isConstQualified(); |
| 526 | T = isConst ? Context->getObjCIdType().withConst() |
| 527 | : Context->getObjCIdType(); |
| 528 | } |
Fariborz Jahanian | 64cb63a | 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 | } |
| 541 | |
| 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 | } |
| 560 | bool PointerTypeTakesAnyBlockArguments(QualType QT); |
| 561 | bool PointerTypeTakesAnyObjCQualifiedType(QualType QT); |
| 562 | void GetExtentOfArgList(const char *Name, const char *&LParen, |
| 563 | const char *&RParen); |
| 564 | |
| 565 | void QuoteDoublequotes(std::string &From, std::string &To) { |
| 566 | for (unsigned i = 0; i < From.length(); i++) { |
| 567 | if (From[i] == '"') |
| 568 | To += "\\\""; |
| 569 | else |
| 570 | To += From[i]; |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | QualType getSimpleFunctionType(QualType result, |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame^] | 575 | ArrayRef<QualType> args, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 576 | bool variadic = false) { |
| 577 | if (result == Context->getObjCInstanceType()) |
| 578 | result = Context->getObjCIdType(); |
| 579 | FunctionProtoType::ExtProtoInfo fpi; |
| 580 | fpi.Variadic = variadic; |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame^] | 581 | return Context->getFunctionType(result, args, fpi); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | // Helper function: create a CStyleCastExpr with trivial type source info. |
| 585 | CStyleCastExpr* NoTypeInfoCStyleCastExpr(ASTContext *Ctx, QualType Ty, |
| 586 | CastKind Kind, Expr *E) { |
| 587 | TypeSourceInfo *TInfo = Ctx->getTrivialTypeSourceInfo(Ty, SourceLocation()); |
| 588 | return CStyleCastExpr::Create(*Ctx, Ty, VK_RValue, Kind, E, 0, TInfo, |
| 589 | SourceLocation(), SourceLocation()); |
| 590 | } |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 591 | |
| 592 | bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const { |
| 593 | IdentifierInfo* II = &Context->Idents.get("load"); |
| 594 | Selector LoadSel = Context->Selectors.getSelector(0, &II); |
| 595 | return OD->getClassMethod(LoadSel) != 0; |
| 596 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 597 | }; |
| 598 | |
| 599 | } |
| 600 | |
| 601 | void RewriteModernObjC::RewriteBlocksInFunctionProtoType(QualType funcType, |
| 602 | NamedDecl *D) { |
| 603 | if (const FunctionProtoType *fproto |
| 604 | = dyn_cast<FunctionProtoType>(funcType.IgnoreParens())) { |
| 605 | for (FunctionProtoType::arg_type_iterator I = fproto->arg_type_begin(), |
| 606 | E = fproto->arg_type_end(); I && (I != E); ++I) |
| 607 | if (isTopLevelBlockPointerType(*I)) { |
| 608 | // All the args are checked/rewritten. Don't call twice! |
| 609 | RewriteBlockPointerDecl(D); |
| 610 | break; |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | void RewriteModernObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) { |
| 616 | const PointerType *PT = funcType->getAs<PointerType>(); |
| 617 | if (PT && PointerTypeTakesAnyBlockArguments(funcType)) |
| 618 | RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND); |
| 619 | } |
| 620 | |
| 621 | static bool IsHeaderFile(const std::string &Filename) { |
| 622 | std::string::size_type DotPos = Filename.rfind('.'); |
| 623 | |
| 624 | if (DotPos == std::string::npos) { |
| 625 | // no file extension |
| 626 | return false; |
| 627 | } |
| 628 | |
| 629 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 630 | // C header: .h |
| 631 | // C++ header: .hh or .H; |
| 632 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
| 633 | } |
| 634 | |
| 635 | RewriteModernObjC::RewriteModernObjC(std::string inFile, raw_ostream* OS, |
| 636 | DiagnosticsEngine &D, const LangOptions &LOpts, |
Fariborz Jahanian | ada7191 | 2013-02-08 00:27:34 +0000 | [diff] [blame] | 637 | bool silenceMacroWarn, |
| 638 | bool LineInfo) |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 639 | : Diags(D), LangOpts(LOpts), InFileName(inFile), OutFile(OS), |
Fariborz Jahanian | ada7191 | 2013-02-08 00:27:34 +0000 | [diff] [blame] | 640 | SilenceRewriteMacroWarning(silenceMacroWarn), GenerateLineInfo(LineInfo) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 641 | IsHeader = IsHeaderFile(inFile); |
| 642 | RewriteFailedDiag = Diags.getCustomDiagID(DiagnosticsEngine::Warning, |
| 643 | "rewriting sub-expression within a macro (may not be correct)"); |
Fariborz Jahanian | d13c2c2 | 2012-03-22 19:54:39 +0000 | [diff] [blame] | 644 | // FIXME. This should be an error. But if block is not called, it is OK. And it |
| 645 | // may break including some headers. |
| 646 | GlobalBlockRewriteFailedDiag = Diags.getCustomDiagID(DiagnosticsEngine::Warning, |
| 647 | "rewriting block literal declared in global scope is not implemented"); |
| 648 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 649 | TryFinallyContainsReturnDiag = Diags.getCustomDiagID( |
| 650 | DiagnosticsEngine::Warning, |
| 651 | "rewriter doesn't support user-specified control flow semantics " |
| 652 | "for @try/@finally (code may not execute properly)"); |
| 653 | } |
| 654 | |
| 655 | ASTConsumer *clang::CreateModernObjCRewriter(const std::string& InFile, |
| 656 | raw_ostream* OS, |
| 657 | DiagnosticsEngine &Diags, |
| 658 | const LangOptions &LOpts, |
Fariborz Jahanian | ada7191 | 2013-02-08 00:27:34 +0000 | [diff] [blame] | 659 | bool SilenceRewriteMacroWarning, |
| 660 | bool LineInfo) { |
| 661 | return new RewriteModernObjC(InFile, OS, Diags, LOpts, |
| 662 | SilenceRewriteMacroWarning, LineInfo); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | void RewriteModernObjC::InitializeCommon(ASTContext &context) { |
| 666 | Context = &context; |
| 667 | SM = &Context->getSourceManager(); |
| 668 | TUDecl = Context->getTranslationUnitDecl(); |
| 669 | MsgSendFunctionDecl = 0; |
| 670 | MsgSendSuperFunctionDecl = 0; |
| 671 | MsgSendStretFunctionDecl = 0; |
| 672 | MsgSendSuperStretFunctionDecl = 0; |
| 673 | MsgSendFpretFunctionDecl = 0; |
| 674 | GetClassFunctionDecl = 0; |
| 675 | GetMetaClassFunctionDecl = 0; |
| 676 | GetSuperClassFunctionDecl = 0; |
| 677 | SelGetUidFunctionDecl = 0; |
| 678 | CFStringFunctionDecl = 0; |
| 679 | ConstantStringClassReference = 0; |
| 680 | NSStringRecord = 0; |
| 681 | CurMethodDef = 0; |
| 682 | CurFunctionDef = 0; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 683 | GlobalVarDecl = 0; |
Fariborz Jahanian | df474ec | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 684 | GlobalConstructionExp = 0; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 685 | SuperStructDecl = 0; |
| 686 | ProtocolTypeDecl = 0; |
| 687 | ConstantStringDecl = 0; |
| 688 | BcLabelCount = 0; |
| 689 | SuperContructorFunctionDecl = 0; |
| 690 | NumObjCStringLiterals = 0; |
| 691 | PropParentMap = 0; |
| 692 | CurrentBody = 0; |
| 693 | DisableReplaceStmt = false; |
| 694 | objc_impl_method = false; |
| 695 | |
| 696 | // Get the ID and start/end of the main file. |
| 697 | MainFileID = SM->getMainFileID(); |
| 698 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 699 | MainFileStart = MainBuf->getBufferStart(); |
| 700 | MainFileEnd = MainBuf->getBufferEnd(); |
| 701 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 702 | Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOpts()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | //===----------------------------------------------------------------------===// |
| 706 | // Top Level Driver Code |
| 707 | //===----------------------------------------------------------------------===// |
| 708 | |
| 709 | void RewriteModernObjC::HandleTopLevelSingleDecl(Decl *D) { |
| 710 | if (Diags.hasErrorOccurred()) |
| 711 | return; |
| 712 | |
| 713 | // Two cases: either the decl could be in the main file, or it could be in a |
| 714 | // #included file. If the former, rewrite it now. If the later, check to see |
| 715 | // if we rewrote the #include/#import. |
| 716 | SourceLocation Loc = D->getLocation(); |
| 717 | Loc = SM->getExpansionLoc(Loc); |
| 718 | |
| 719 | // If this is for a builtin, ignore it. |
| 720 | if (Loc.isInvalid()) return; |
| 721 | |
| 722 | // Look for built-in declarations that we need to refer during the rewrite. |
| 723 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 724 | RewriteFunctionDecl(FD); |
| 725 | } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) { |
| 726 | // declared in <Foundation/NSString.h> |
| 727 | if (FVD->getName() == "_NSConstantStringClassReference") { |
| 728 | ConstantStringClassReference = FVD; |
| 729 | return; |
| 730 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 731 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
| 732 | RewriteCategoryDecl(CD); |
| 733 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
| 734 | if (PD->isThisDeclarationADefinition()) |
| 735 | RewriteProtocolDecl(PD); |
| 736 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
Fariborz Jahanian | 8e86b2d | 2012-04-04 17:16:15 +0000 | [diff] [blame] | 737 | // FIXME. This will not work in all situations and leaving it out |
| 738 | // is harmless. |
| 739 | // RewriteLinkageSpec(LSD); |
| 740 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 741 | // Recurse into linkage specifications |
| 742 | for (DeclContext::decl_iterator DI = LSD->decls_begin(), |
| 743 | DIEnd = LSD->decls_end(); |
| 744 | DI != DIEnd; ) { |
| 745 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>((*DI))) { |
| 746 | if (!IFace->isThisDeclarationADefinition()) { |
| 747 | SmallVector<Decl *, 8> DG; |
| 748 | SourceLocation StartLoc = IFace->getLocStart(); |
| 749 | do { |
| 750 | if (isa<ObjCInterfaceDecl>(*DI) && |
| 751 | !cast<ObjCInterfaceDecl>(*DI)->isThisDeclarationADefinition() && |
| 752 | StartLoc == (*DI)->getLocStart()) |
| 753 | DG.push_back(*DI); |
| 754 | else |
| 755 | break; |
| 756 | |
| 757 | ++DI; |
| 758 | } while (DI != DIEnd); |
| 759 | RewriteForwardClassDecl(DG); |
| 760 | continue; |
| 761 | } |
Fariborz Jahanian | b3f904f | 2012-04-03 17:35:38 +0000 | [diff] [blame] | 762 | else { |
| 763 | // Keep track of all interface declarations seen. |
| 764 | ObjCInterfacesSeen.push_back(IFace); |
| 765 | ++DI; |
| 766 | continue; |
| 767 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>((*DI))) { |
| 771 | if (!Proto->isThisDeclarationADefinition()) { |
| 772 | SmallVector<Decl *, 8> DG; |
| 773 | SourceLocation StartLoc = Proto->getLocStart(); |
| 774 | do { |
| 775 | if (isa<ObjCProtocolDecl>(*DI) && |
| 776 | !cast<ObjCProtocolDecl>(*DI)->isThisDeclarationADefinition() && |
| 777 | StartLoc == (*DI)->getLocStart()) |
| 778 | DG.push_back(*DI); |
| 779 | else |
| 780 | break; |
| 781 | |
| 782 | ++DI; |
| 783 | } while (DI != DIEnd); |
| 784 | RewriteForwardProtocolDecl(DG); |
| 785 | continue; |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | HandleTopLevelSingleDecl(*DI); |
| 790 | ++DI; |
| 791 | } |
| 792 | } |
| 793 | // If we have a decl in the main file, see if we should rewrite it. |
| 794 | if (SM->isFromMainFile(Loc)) |
| 795 | return HandleDeclInMainFile(D); |
| 796 | } |
| 797 | |
| 798 | //===----------------------------------------------------------------------===// |
| 799 | // Syntactic (non-AST) Rewriting Code |
| 800 | //===----------------------------------------------------------------------===// |
| 801 | |
| 802 | void RewriteModernObjC::RewriteInclude() { |
| 803 | SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID); |
| 804 | StringRef MainBuf = SM->getBufferData(MainFileID); |
| 805 | const char *MainBufStart = MainBuf.begin(); |
| 806 | const char *MainBufEnd = MainBuf.end(); |
| 807 | size_t ImportLen = strlen("import"); |
| 808 | |
| 809 | // Loop over the whole file, looking for includes. |
| 810 | for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { |
| 811 | if (*BufPtr == '#') { |
| 812 | if (++BufPtr == MainBufEnd) |
| 813 | return; |
| 814 | while (*BufPtr == ' ' || *BufPtr == '\t') |
| 815 | if (++BufPtr == MainBufEnd) |
| 816 | return; |
| 817 | if (!strncmp(BufPtr, "import", ImportLen)) { |
| 818 | // replace import with include |
| 819 | SourceLocation ImportLoc = |
| 820 | LocStart.getLocWithOffset(BufPtr-MainBufStart); |
| 821 | ReplaceText(ImportLoc, ImportLen, "include"); |
| 822 | BufPtr += ImportLen; |
| 823 | } |
| 824 | } |
| 825 | } |
| 826 | } |
| 827 | |
Fariborz Jahanian | 163d3ce | 2012-05-08 23:54:35 +0000 | [diff] [blame] | 828 | static void WriteInternalIvarName(const ObjCInterfaceDecl *IDecl, |
| 829 | ObjCIvarDecl *IvarDecl, std::string &Result) { |
| 830 | Result += "OBJC_IVAR_$_"; |
| 831 | Result += IDecl->getName(); |
| 832 | Result += "$"; |
| 833 | Result += IvarDecl->getName(); |
| 834 | } |
| 835 | |
| 836 | std::string |
| 837 | RewriteModernObjC::getIvarAccessString(ObjCIvarDecl *D) { |
| 838 | const ObjCInterfaceDecl *ClassDecl = D->getContainingInterface(); |
| 839 | |
| 840 | // Build name of symbol holding ivar offset. |
| 841 | std::string IvarOffsetName; |
Fariborz Jahanian | cd3b036 | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 842 | if (D->isBitField()) |
| 843 | ObjCIvarBitfieldGroupOffset(D, IvarOffsetName); |
| 844 | else |
| 845 | WriteInternalIvarName(ClassDecl, D, IvarOffsetName); |
Fariborz Jahanian | 163d3ce | 2012-05-08 23:54:35 +0000 | [diff] [blame] | 846 | |
| 847 | |
| 848 | std::string S = "(*("; |
| 849 | QualType IvarT = D->getType(); |
Fariborz Jahanian | cd3b036 | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 850 | if (D->isBitField()) |
| 851 | IvarT = GetGroupRecordTypeForObjCIvarBitfield(D); |
Fariborz Jahanian | 163d3ce | 2012-05-08 23:54:35 +0000 | [diff] [blame] | 852 | |
| 853 | if (!isa<TypedefType>(IvarT) && IvarT->isRecordType()) { |
| 854 | RecordDecl *RD = IvarT->getAs<RecordType>()->getDecl(); |
| 855 | RD = RD->getDefinition(); |
| 856 | if (RD && !RD->getDeclName().getAsIdentifierInfo()) { |
| 857 | // decltype(((Foo_IMPL*)0)->bar) * |
| 858 | ObjCContainerDecl *CDecl = |
| 859 | dyn_cast<ObjCContainerDecl>(D->getDeclContext()); |
| 860 | // ivar in class extensions requires special treatment. |
| 861 | if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) |
| 862 | CDecl = CatDecl->getClassInterface(); |
| 863 | std::string RecName = CDecl->getName(); |
| 864 | RecName += "_IMPL"; |
| 865 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 866 | SourceLocation(), SourceLocation(), |
| 867 | &Context->Idents.get(RecName.c_str())); |
| 868 | QualType PtrStructIMPL = Context->getPointerType(Context->getTagDeclType(RD)); |
| 869 | unsigned UnsignedIntSize = |
| 870 | static_cast<unsigned>(Context->getTypeSize(Context->UnsignedIntTy)); |
| 871 | Expr *Zero = IntegerLiteral::Create(*Context, |
| 872 | llvm::APInt(UnsignedIntSize, 0), |
| 873 | Context->UnsignedIntTy, SourceLocation()); |
| 874 | Zero = NoTypeInfoCStyleCastExpr(Context, PtrStructIMPL, CK_BitCast, Zero); |
| 875 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 876 | Zero); |
| 877 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 878 | SourceLocation(), |
| 879 | &Context->Idents.get(D->getNameAsString()), |
| 880 | IvarT, 0, |
| 881 | /*BitWidth=*/0, /*Mutable=*/true, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 882 | ICIS_NoInit); |
Fariborz Jahanian | 163d3ce | 2012-05-08 23:54:35 +0000 | [diff] [blame] | 883 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
| 884 | FD->getType(), VK_LValue, |
| 885 | OK_Ordinary); |
| 886 | IvarT = Context->getDecltypeType(ME, ME->getType()); |
| 887 | } |
| 888 | } |
| 889 | convertObjCTypeToCStyleType(IvarT); |
| 890 | QualType castT = Context->getPointerType(IvarT); |
| 891 | std::string TypeString(castT.getAsString(Context->getPrintingPolicy())); |
| 892 | S += TypeString; |
| 893 | S += ")"; |
| 894 | |
| 895 | // ((char *)self + IVAR_OFFSET_SYMBOL_NAME) |
| 896 | S += "((char *)self + "; |
| 897 | S += IvarOffsetName; |
| 898 | S += "))"; |
Fariborz Jahanian | cd3b036 | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 899 | if (D->isBitField()) { |
| 900 | S += "."; |
| 901 | S += D->getNameAsString(); |
| 902 | } |
Fariborz Jahanian | 163d3ce | 2012-05-08 23:54:35 +0000 | [diff] [blame] | 903 | ReferencedIvars[const_cast<ObjCInterfaceDecl *>(ClassDecl)].insert(D); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 904 | return S; |
| 905 | } |
| 906 | |
Fariborz Jahanian | 301e2e4 | 2012-05-03 22:52:13 +0000 | [diff] [blame] | 907 | /// mustSynthesizeSetterGetterMethod - returns true if setter or getter has not |
| 908 | /// been found in the class implementation. In this case, it must be synthesized. |
| 909 | static bool mustSynthesizeSetterGetterMethod(ObjCImplementationDecl *IMP, |
| 910 | ObjCPropertyDecl *PD, |
| 911 | bool getter) { |
| 912 | return getter ? !IMP->getInstanceMethod(PD->getGetterName()) |
| 913 | : !IMP->getInstanceMethod(PD->getSetterName()); |
| 914 | |
| 915 | } |
| 916 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 917 | void RewriteModernObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 918 | ObjCImplementationDecl *IMD, |
| 919 | ObjCCategoryImplDecl *CID) { |
| 920 | static bool objcGetPropertyDefined = false; |
| 921 | static bool objcSetPropertyDefined = false; |
Fariborz Jahanian | 301e2e4 | 2012-05-03 22:52:13 +0000 | [diff] [blame] | 922 | SourceLocation startGetterSetterLoc; |
| 923 | |
| 924 | if (PID->getLocStart().isValid()) { |
| 925 | SourceLocation startLoc = PID->getLocStart(); |
| 926 | InsertText(startLoc, "// "); |
| 927 | const char *startBuf = SM->getCharacterData(startLoc); |
| 928 | assert((*startBuf == '@') && "bogus @synthesize location"); |
| 929 | const char *semiBuf = strchr(startBuf, ';'); |
| 930 | assert((*semiBuf == ';') && "@synthesize: can't find ';'"); |
| 931 | startGetterSetterLoc = startLoc.getLocWithOffset(semiBuf-startBuf+1); |
| 932 | } |
| 933 | else |
| 934 | startGetterSetterLoc = IMD ? IMD->getLocEnd() : CID->getLocEnd(); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 935 | |
| 936 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 937 | return; // FIXME: is this correct? |
| 938 | |
| 939 | // Generate the 'getter' function. |
| 940 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 941 | ObjCIvarDecl *OID = PID->getPropertyIvarDecl(); |
| 942 | |
| 943 | if (!OID) |
| 944 | return; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 945 | unsigned Attributes = PD->getPropertyAttributes(); |
Fariborz Jahanian | 301e2e4 | 2012-05-03 22:52:13 +0000 | [diff] [blame] | 946 | if (mustSynthesizeSetterGetterMethod(IMD, PD, true /*getter*/)) { |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 947 | bool GenGetProperty = !(Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) && |
| 948 | (Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
Fariborz Jahanian | 64cb63a | 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 | } |
| 957 | RewriteObjCMethodDecl(OID->getContainingInterface(), |
| 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 "; |
| 965 | const FunctionType *FPRetType = 0; |
| 966 | RewriteTypeIntoString(PD->getGetterMethodDecl()->getResultType(), Getr, |
| 967 | FPRetType); |
| 968 | Getr += " _TYPE"; |
| 969 | if (FPRetType) { |
| 970 | Getr += ")"; // close the precedence "scope" for "*". |
| 971 | |
| 972 | // Now, emit the argument types (if any). |
| 973 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)){ |
| 974 | Getr += "("; |
| 975 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 976 | if (i) Getr += ", "; |
| 977 | std::string ParamStr = FT->getArgType(i).getAsString( |
| 978 | Context->getPrintingPolicy()); |
| 979 | Getr += ParamStr; |
| 980 | } |
| 981 | if (FT->isVariadic()) { |
| 982 | if (FT->getNumArgs()) Getr += ", "; |
| 983 | Getr += "..."; |
| 984 | } |
| 985 | Getr += ")"; |
| 986 | } else |
| 987 | Getr += "()"; |
| 988 | } |
| 989 | Getr += ";\n"; |
| 990 | Getr += "return (_TYPE)"; |
| 991 | Getr += "objc_getProperty(self, _cmd, "; |
| 992 | RewriteIvarOffsetComputation(OID, Getr); |
| 993 | Getr += ", 1)"; |
| 994 | } |
| 995 | else |
| 996 | Getr += "return " + getIvarAccessString(OID); |
| 997 | Getr += "; }"; |
Fariborz Jahanian | 301e2e4 | 2012-05-03 22:52:13 +0000 | [diff] [blame] | 998 | InsertText(startGetterSetterLoc, Getr); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
Fariborz Jahanian | 301e2e4 | 2012-05-03 22:52:13 +0000 | [diff] [blame] | 1001 | if (PD->isReadOnly() || |
| 1002 | !mustSynthesizeSetterGetterMethod(IMD, PD, false /*setter*/)) |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1003 | return; |
| 1004 | |
| 1005 | // Generate the 'setter' function. |
| 1006 | std::string Setr; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1007 | bool GenSetProperty = Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1008 | ObjCPropertyDecl::OBJC_PR_copy); |
| 1009 | if (GenSetProperty && !objcSetPropertyDefined) { |
| 1010 | objcSetPropertyDefined = true; |
| 1011 | // FIXME. Is this attribute correct in all cases? |
| 1012 | Setr = "\nextern \"C\" __declspec(dllimport) " |
| 1013 | "void objc_setProperty (id, SEL, long, id, bool, bool);\n"; |
| 1014 | } |
| 1015 | |
| 1016 | RewriteObjCMethodDecl(OID->getContainingInterface(), |
| 1017 | PD->getSetterMethodDecl(), Setr); |
| 1018 | Setr += "{ "; |
| 1019 | // Synthesize an explicit cast to initialize the ivar. |
| 1020 | // See objc-act.c:objc_synthesize_new_setter() for details. |
| 1021 | if (GenSetProperty) { |
| 1022 | Setr += "objc_setProperty (self, _cmd, "; |
| 1023 | RewriteIvarOffsetComputation(OID, Setr); |
| 1024 | Setr += ", (id)"; |
| 1025 | Setr += PD->getName(); |
| 1026 | Setr += ", "; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1027 | if (Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1028 | Setr += "0, "; |
| 1029 | else |
| 1030 | Setr += "1, "; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1031 | if (Attributes & ObjCPropertyDecl::OBJC_PR_copy) |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1032 | Setr += "1)"; |
| 1033 | else |
| 1034 | Setr += "0)"; |
| 1035 | } |
| 1036 | else { |
| 1037 | Setr += getIvarAccessString(OID) + " = "; |
| 1038 | Setr += PD->getName(); |
| 1039 | } |
Fariborz Jahanian | 301e2e4 | 2012-05-03 22:52:13 +0000 | [diff] [blame] | 1040 | Setr += "; }\n"; |
| 1041 | InsertText(startGetterSetterLoc, Setr); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | static void RewriteOneForwardClassDecl(ObjCInterfaceDecl *ForwardDecl, |
| 1045 | std::string &typedefString) { |
Fariborz Jahanian | bfaa111 | 2013-02-08 17:15:07 +0000 | [diff] [blame] | 1046 | typedefString += "\n#ifndef _REWRITER_typedef_"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1047 | typedefString += ForwardDecl->getNameAsString(); |
| 1048 | typedefString += "\n"; |
| 1049 | typedefString += "#define _REWRITER_typedef_"; |
| 1050 | typedefString += ForwardDecl->getNameAsString(); |
| 1051 | typedefString += "\n"; |
| 1052 | typedefString += "typedef struct objc_object "; |
| 1053 | typedefString += ForwardDecl->getNameAsString(); |
Fariborz Jahanian | c38503b | 2012-03-12 23:58:28 +0000 | [diff] [blame] | 1054 | // typedef struct { } _objc_exc_Classname; |
| 1055 | typedefString += ";\ntypedef struct {} _objc_exc_"; |
| 1056 | typedefString += ForwardDecl->getNameAsString(); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1057 | typedefString += ";\n#endif\n"; |
| 1058 | } |
| 1059 | |
| 1060 | void RewriteModernObjC::RewriteForwardClassEpilogue(ObjCInterfaceDecl *ClassDecl, |
| 1061 | const std::string &typedefString) { |
| 1062 | SourceLocation startLoc = ClassDecl->getLocStart(); |
| 1063 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1064 | const char *semiPtr = strchr(startBuf, ';'); |
| 1065 | // Replace the @class with typedefs corresponding to the classes. |
| 1066 | ReplaceText(startLoc, semiPtr-startBuf+1, typedefString); |
| 1067 | } |
| 1068 | |
| 1069 | void RewriteModernObjC::RewriteForwardClassDecl(DeclGroupRef D) { |
| 1070 | std::string typedefString; |
| 1071 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
| 1072 | ObjCInterfaceDecl *ForwardDecl = cast<ObjCInterfaceDecl>(*I); |
| 1073 | if (I == D.begin()) { |
| 1074 | // Translate to typedef's that forward reference structs with the same name |
| 1075 | // as the class. As a convenience, we include the original declaration |
| 1076 | // as a comment. |
| 1077 | typedefString += "// @class "; |
| 1078 | typedefString += ForwardDecl->getNameAsString(); |
Fariborz Jahanian | bfaa111 | 2013-02-08 17:15:07 +0000 | [diff] [blame] | 1079 | typedefString += ";"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1080 | } |
| 1081 | RewriteOneForwardClassDecl(ForwardDecl, typedefString); |
| 1082 | } |
| 1083 | DeclGroupRef::iterator I = D.begin(); |
| 1084 | RewriteForwardClassEpilogue(cast<ObjCInterfaceDecl>(*I), typedefString); |
| 1085 | } |
| 1086 | |
| 1087 | void RewriteModernObjC::RewriteForwardClassDecl( |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1088 | const SmallVector<Decl *, 8> &D) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1089 | std::string typedefString; |
| 1090 | for (unsigned i = 0; i < D.size(); i++) { |
| 1091 | ObjCInterfaceDecl *ForwardDecl = cast<ObjCInterfaceDecl>(D[i]); |
| 1092 | if (i == 0) { |
| 1093 | typedefString += "// @class "; |
| 1094 | typedefString += ForwardDecl->getNameAsString(); |
Fariborz Jahanian | bfaa111 | 2013-02-08 17:15:07 +0000 | [diff] [blame] | 1095 | typedefString += ";"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1096 | } |
| 1097 | RewriteOneForwardClassDecl(ForwardDecl, typedefString); |
| 1098 | } |
| 1099 | RewriteForwardClassEpilogue(cast<ObjCInterfaceDecl>(D[0]), typedefString); |
| 1100 | } |
| 1101 | |
| 1102 | void RewriteModernObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) { |
| 1103 | // When method is a synthesized one, such as a getter/setter there is |
| 1104 | // nothing to rewrite. |
| 1105 | if (Method->isImplicit()) |
| 1106 | return; |
| 1107 | SourceLocation LocStart = Method->getLocStart(); |
| 1108 | SourceLocation LocEnd = Method->getLocEnd(); |
| 1109 | |
| 1110 | if (SM->getExpansionLineNumber(LocEnd) > |
| 1111 | SM->getExpansionLineNumber(LocStart)) { |
| 1112 | InsertText(LocStart, "#if 0\n"); |
| 1113 | ReplaceText(LocEnd, 1, ";\n#endif\n"); |
| 1114 | } else { |
| 1115 | InsertText(LocStart, "// "); |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | void RewriteModernObjC::RewriteProperty(ObjCPropertyDecl *prop) { |
| 1120 | SourceLocation Loc = prop->getAtLoc(); |
| 1121 | |
| 1122 | ReplaceText(Loc, 0, "// "); |
| 1123 | // FIXME: handle properties that are declared across multiple lines. |
| 1124 | } |
| 1125 | |
| 1126 | void RewriteModernObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
| 1127 | SourceLocation LocStart = CatDecl->getLocStart(); |
| 1128 | |
| 1129 | // FIXME: handle category headers that are declared across multiple lines. |
Fariborz Jahanian | b68258f | 2012-05-01 17:46:45 +0000 | [diff] [blame] | 1130 | if (CatDecl->getIvarRBraceLoc().isValid()) { |
| 1131 | ReplaceText(LocStart, 1, "/** "); |
| 1132 | ReplaceText(CatDecl->getIvarRBraceLoc(), 1, "**/ "); |
| 1133 | } |
| 1134 | else { |
Fariborz Jahanian | d2aea12 | 2012-02-19 19:00:05 +0000 | [diff] [blame] | 1135 | ReplaceText(LocStart, 0, "// "); |
Fariborz Jahanian | b68258f | 2012-05-01 17:46:45 +0000 | [diff] [blame] | 1136 | } |
Fariborz Jahanian | af30029 | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 1137 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1138 | for (ObjCCategoryDecl::prop_iterator I = CatDecl->prop_begin(), |
| 1139 | E = CatDecl->prop_end(); I != E; ++I) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1140 | RewriteProperty(*I); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1141 | |
| 1142 | for (ObjCCategoryDecl::instmeth_iterator |
| 1143 | I = CatDecl->instmeth_begin(), E = CatDecl->instmeth_end(); |
| 1144 | I != E; ++I) |
| 1145 | RewriteMethodDeclaration(*I); |
| 1146 | for (ObjCCategoryDecl::classmeth_iterator |
| 1147 | I = CatDecl->classmeth_begin(), E = CatDecl->classmeth_end(); |
| 1148 | I != E; ++I) |
| 1149 | RewriteMethodDeclaration(*I); |
| 1150 | |
| 1151 | // Lastly, comment out the @end. |
| 1152 | ReplaceText(CatDecl->getAtEndRange().getBegin(), |
| 1153 | strlen("@end"), "/* @end */"); |
| 1154 | } |
| 1155 | |
| 1156 | void RewriteModernObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
| 1157 | SourceLocation LocStart = PDecl->getLocStart(); |
| 1158 | assert(PDecl->isThisDeclarationADefinition()); |
| 1159 | |
| 1160 | // FIXME: handle protocol headers that are declared across multiple lines. |
| 1161 | ReplaceText(LocStart, 0, "// "); |
| 1162 | |
| 1163 | for (ObjCProtocolDecl::instmeth_iterator |
| 1164 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
| 1165 | I != E; ++I) |
| 1166 | RewriteMethodDeclaration(*I); |
| 1167 | for (ObjCProtocolDecl::classmeth_iterator |
| 1168 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
| 1169 | I != E; ++I) |
| 1170 | RewriteMethodDeclaration(*I); |
| 1171 | |
| 1172 | for (ObjCInterfaceDecl::prop_iterator I = PDecl->prop_begin(), |
| 1173 | E = PDecl->prop_end(); I != E; ++I) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1174 | RewriteProperty(*I); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1175 | |
| 1176 | // Lastly, comment out the @end. |
| 1177 | SourceLocation LocEnd = PDecl->getAtEndRange().getBegin(); |
| 1178 | ReplaceText(LocEnd, strlen("@end"), "/* @end */"); |
| 1179 | |
| 1180 | // Must comment out @optional/@required |
| 1181 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1182 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 1183 | for (const char *p = startBuf; p < endBuf; p++) { |
| 1184 | if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { |
| 1185 | SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf); |
| 1186 | ReplaceText(OptionalLoc, strlen("@optional"), "/* @optional */"); |
| 1187 | |
| 1188 | } |
| 1189 | else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { |
| 1190 | SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf); |
| 1191 | ReplaceText(OptionalLoc, strlen("@required"), "/* @required */"); |
| 1192 | |
| 1193 | } |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | void RewriteModernObjC::RewriteForwardProtocolDecl(DeclGroupRef D) { |
| 1198 | SourceLocation LocStart = (*D.begin())->getLocStart(); |
| 1199 | if (LocStart.isInvalid()) |
| 1200 | llvm_unreachable("Invalid SourceLocation"); |
| 1201 | // FIXME: handle forward protocol that are declared across multiple lines. |
| 1202 | ReplaceText(LocStart, 0, "// "); |
| 1203 | } |
| 1204 | |
| 1205 | void |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1206 | RewriteModernObjC::RewriteForwardProtocolDecl(const SmallVector<Decl *, 8> &DG) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1207 | SourceLocation LocStart = DG[0]->getLocStart(); |
| 1208 | if (LocStart.isInvalid()) |
| 1209 | llvm_unreachable("Invalid SourceLocation"); |
| 1210 | // FIXME: handle forward protocol that are declared across multiple lines. |
| 1211 | ReplaceText(LocStart, 0, "// "); |
| 1212 | } |
| 1213 | |
Fariborz Jahanian | b3f904f | 2012-04-03 17:35:38 +0000 | [diff] [blame] | 1214 | void |
| 1215 | RewriteModernObjC::RewriteLinkageSpec(LinkageSpecDecl *LSD) { |
| 1216 | SourceLocation LocStart = LSD->getExternLoc(); |
| 1217 | if (LocStart.isInvalid()) |
| 1218 | llvm_unreachable("Invalid extern SourceLocation"); |
| 1219 | |
| 1220 | ReplaceText(LocStart, 0, "// "); |
| 1221 | if (!LSD->hasBraces()) |
| 1222 | return; |
| 1223 | // FIXME. We don't rewrite well if '{' is not on same line as 'extern'. |
| 1224 | SourceLocation LocRBrace = LSD->getRBraceLoc(); |
| 1225 | if (LocRBrace.isInvalid()) |
| 1226 | llvm_unreachable("Invalid rbrace SourceLocation"); |
| 1227 | ReplaceText(LocRBrace, 0, "// "); |
| 1228 | } |
| 1229 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1230 | void RewriteModernObjC::RewriteTypeIntoString(QualType T, std::string &ResultStr, |
| 1231 | const FunctionType *&FPRetType) { |
| 1232 | if (T->isObjCQualifiedIdType()) |
| 1233 | ResultStr += "id"; |
| 1234 | else if (T->isFunctionPointerType() || |
| 1235 | T->isBlockPointerType()) { |
| 1236 | // needs special handling, since pointer-to-functions have special |
| 1237 | // syntax (where a decaration models use). |
| 1238 | QualType retType = T; |
| 1239 | QualType PointeeTy; |
| 1240 | if (const PointerType* PT = retType->getAs<PointerType>()) |
| 1241 | PointeeTy = PT->getPointeeType(); |
| 1242 | else if (const BlockPointerType *BPT = retType->getAs<BlockPointerType>()) |
| 1243 | PointeeTy = BPT->getPointeeType(); |
| 1244 | if ((FPRetType = PointeeTy->getAs<FunctionType>())) { |
| 1245 | ResultStr += FPRetType->getResultType().getAsString( |
| 1246 | Context->getPrintingPolicy()); |
| 1247 | ResultStr += "(*"; |
| 1248 | } |
| 1249 | } else |
| 1250 | ResultStr += T.getAsString(Context->getPrintingPolicy()); |
| 1251 | } |
| 1252 | |
| 1253 | void RewriteModernObjC::RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl, |
| 1254 | ObjCMethodDecl *OMD, |
| 1255 | std::string &ResultStr) { |
| 1256 | //fprintf(stderr,"In RewriteObjCMethodDecl\n"); |
| 1257 | const FunctionType *FPRetType = 0; |
| 1258 | ResultStr += "\nstatic "; |
| 1259 | RewriteTypeIntoString(OMD->getResultType(), ResultStr, FPRetType); |
| 1260 | ResultStr += " "; |
| 1261 | |
| 1262 | // Unique method name |
| 1263 | std::string NameStr; |
| 1264 | |
| 1265 | if (OMD->isInstanceMethod()) |
| 1266 | NameStr += "_I_"; |
| 1267 | else |
| 1268 | NameStr += "_C_"; |
| 1269 | |
| 1270 | NameStr += IDecl->getNameAsString(); |
| 1271 | NameStr += "_"; |
| 1272 | |
| 1273 | if (ObjCCategoryImplDecl *CID = |
| 1274 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
| 1275 | NameStr += CID->getNameAsString(); |
| 1276 | NameStr += "_"; |
| 1277 | } |
| 1278 | // Append selector names, replacing ':' with '_' |
| 1279 | { |
| 1280 | std::string selString = OMD->getSelector().getAsString(); |
| 1281 | int len = selString.size(); |
| 1282 | for (int i = 0; i < len; i++) |
| 1283 | if (selString[i] == ':') |
| 1284 | selString[i] = '_'; |
| 1285 | NameStr += selString; |
| 1286 | } |
| 1287 | // Remember this name for metadata emission |
| 1288 | MethodInternalNames[OMD] = NameStr; |
| 1289 | ResultStr += NameStr; |
| 1290 | |
| 1291 | // Rewrite arguments |
| 1292 | ResultStr += "("; |
| 1293 | |
| 1294 | // invisible arguments |
| 1295 | if (OMD->isInstanceMethod()) { |
| 1296 | QualType selfTy = Context->getObjCInterfaceType(IDecl); |
| 1297 | selfTy = Context->getPointerType(selfTy); |
| 1298 | if (!LangOpts.MicrosoftExt) { |
| 1299 | if (ObjCSynthesizedStructs.count(const_cast<ObjCInterfaceDecl*>(IDecl))) |
| 1300 | ResultStr += "struct "; |
| 1301 | } |
| 1302 | // When rewriting for Microsoft, explicitly omit the structure name. |
| 1303 | ResultStr += IDecl->getNameAsString(); |
| 1304 | ResultStr += " *"; |
| 1305 | } |
| 1306 | else |
| 1307 | ResultStr += Context->getObjCClassType().getAsString( |
| 1308 | Context->getPrintingPolicy()); |
| 1309 | |
| 1310 | ResultStr += " self, "; |
| 1311 | ResultStr += Context->getObjCSelType().getAsString(Context->getPrintingPolicy()); |
| 1312 | ResultStr += " _cmd"; |
| 1313 | |
| 1314 | // Method arguments. |
| 1315 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 1316 | E = OMD->param_end(); PI != E; ++PI) { |
| 1317 | ParmVarDecl *PDecl = *PI; |
| 1318 | ResultStr += ", "; |
| 1319 | if (PDecl->getType()->isObjCQualifiedIdType()) { |
| 1320 | ResultStr += "id "; |
| 1321 | ResultStr += PDecl->getNameAsString(); |
| 1322 | } else { |
| 1323 | std::string Name = PDecl->getNameAsString(); |
| 1324 | QualType QT = PDecl->getType(); |
| 1325 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 2610f90 | 2012-03-27 16:42:20 +0000 | [diff] [blame] | 1326 | (void)convertBlockPointerToFunctionPointer(QT); |
| 1327 | QT.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1328 | ResultStr += Name; |
| 1329 | } |
| 1330 | } |
| 1331 | if (OMD->isVariadic()) |
| 1332 | ResultStr += ", ..."; |
| 1333 | ResultStr += ") "; |
| 1334 | |
| 1335 | if (FPRetType) { |
| 1336 | ResultStr += ")"; // close the precedence "scope" for "*". |
| 1337 | |
| 1338 | // Now, emit the argument types (if any). |
| 1339 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) { |
| 1340 | ResultStr += "("; |
| 1341 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 1342 | if (i) ResultStr += ", "; |
| 1343 | std::string ParamStr = FT->getArgType(i).getAsString( |
| 1344 | Context->getPrintingPolicy()); |
| 1345 | ResultStr += ParamStr; |
| 1346 | } |
| 1347 | if (FT->isVariadic()) { |
| 1348 | if (FT->getNumArgs()) ResultStr += ", "; |
| 1349 | ResultStr += "..."; |
| 1350 | } |
| 1351 | ResultStr += ")"; |
| 1352 | } else { |
| 1353 | ResultStr += "()"; |
| 1354 | } |
| 1355 | } |
| 1356 | } |
| 1357 | void RewriteModernObjC::RewriteImplementationDecl(Decl *OID) { |
| 1358 | ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID); |
| 1359 | ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID); |
| 1360 | |
Fariborz Jahanian | d2aea12 | 2012-02-19 19:00:05 +0000 | [diff] [blame] | 1361 | if (IMD) { |
Fariborz Jahanian | b68258f | 2012-05-01 17:46:45 +0000 | [diff] [blame] | 1362 | if (IMD->getIvarRBraceLoc().isValid()) { |
| 1363 | ReplaceText(IMD->getLocStart(), 1, "/** "); |
| 1364 | ReplaceText(IMD->getIvarRBraceLoc(), 1, "**/ "); |
Fariborz Jahanian | d2aea12 | 2012-02-19 19:00:05 +0000 | [diff] [blame] | 1365 | } |
Fariborz Jahanian | b68258f | 2012-05-01 17:46:45 +0000 | [diff] [blame] | 1366 | else { |
| 1367 | InsertText(IMD->getLocStart(), "// "); |
| 1368 | } |
Fariborz Jahanian | d2aea12 | 2012-02-19 19:00:05 +0000 | [diff] [blame] | 1369 | } |
| 1370 | else |
| 1371 | InsertText(CID->getLocStart(), "// "); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1372 | |
| 1373 | for (ObjCCategoryImplDecl::instmeth_iterator |
| 1374 | I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(), |
| 1375 | E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); |
| 1376 | I != E; ++I) { |
| 1377 | std::string ResultStr; |
| 1378 | ObjCMethodDecl *OMD = *I; |
| 1379 | RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr); |
| 1380 | SourceLocation LocStart = OMD->getLocStart(); |
| 1381 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
| 1382 | |
| 1383 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1384 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 1385 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
| 1386 | } |
| 1387 | |
| 1388 | for (ObjCCategoryImplDecl::classmeth_iterator |
| 1389 | I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(), |
| 1390 | E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); |
| 1391 | I != E; ++I) { |
| 1392 | std::string ResultStr; |
| 1393 | ObjCMethodDecl *OMD = *I; |
| 1394 | RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr); |
| 1395 | SourceLocation LocStart = OMD->getLocStart(); |
| 1396 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
| 1397 | |
| 1398 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1399 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 1400 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
| 1401 | } |
| 1402 | for (ObjCCategoryImplDecl::propimpl_iterator |
| 1403 | I = IMD ? IMD->propimpl_begin() : CID->propimpl_begin(), |
| 1404 | E = IMD ? IMD->propimpl_end() : CID->propimpl_end(); |
| 1405 | I != E; ++I) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1406 | RewritePropertyImplDecl(*I, IMD, CID); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
| 1409 | InsertText(IMD ? IMD->getLocEnd() : CID->getLocEnd(), "// "); |
| 1410 | } |
| 1411 | |
| 1412 | void RewriteModernObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { |
Fariborz Jahanian | 8f1fed0 | 2012-02-11 20:10:52 +0000 | [diff] [blame] | 1413 | // Do not synthesize more than once. |
| 1414 | if (ObjCSynthesizedStructs.count(ClassDecl)) |
| 1415 | return; |
| 1416 | // Make sure super class's are written before current class is written. |
| 1417 | ObjCInterfaceDecl *SuperClass = ClassDecl->getSuperClass(); |
| 1418 | while (SuperClass) { |
| 1419 | RewriteInterfaceDecl(SuperClass); |
| 1420 | SuperClass = SuperClass->getSuperClass(); |
| 1421 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1422 | std::string ResultStr; |
Fariborz Jahanian | cf4c60f | 2012-02-17 22:20:12 +0000 | [diff] [blame] | 1423 | if (!ObjCWrittenInterfaces.count(ClassDecl->getCanonicalDecl())) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1424 | // we haven't seen a forward decl - generate a typedef. |
Fariborz Jahanian | c38503b | 2012-03-12 23:58:28 +0000 | [diff] [blame] | 1425 | RewriteOneForwardClassDecl(ClassDecl, ResultStr); |
Fariborz Jahanian | 72c88f1 | 2012-02-22 18:13:25 +0000 | [diff] [blame] | 1426 | RewriteIvarOffsetSymbols(ClassDecl, ResultStr); |
| 1427 | |
Fariborz Jahanian | 4339bb3 | 2012-02-15 22:01:47 +0000 | [diff] [blame] | 1428 | RewriteObjCInternalStruct(ClassDecl, ResultStr); |
Fariborz Jahanian | cf4c60f | 2012-02-17 22:20:12 +0000 | [diff] [blame] | 1429 | // Mark this typedef as having been written into its c++ equivalent. |
| 1430 | ObjCWrittenInterfaces.insert(ClassDecl->getCanonicalDecl()); |
Fariborz Jahanian | 4339bb3 | 2012-02-15 22:01:47 +0000 | [diff] [blame] | 1431 | |
| 1432 | for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(), |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1433 | E = ClassDecl->prop_end(); I != E; ++I) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1434 | RewriteProperty(*I); |
Fariborz Jahanian | 4339bb3 | 2012-02-15 22:01:47 +0000 | [diff] [blame] | 1435 | for (ObjCInterfaceDecl::instmeth_iterator |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1436 | I = ClassDecl->instmeth_begin(), E = ClassDecl->instmeth_end(); |
Fariborz Jahanian | 4339bb3 | 2012-02-15 22:01:47 +0000 | [diff] [blame] | 1437 | I != E; ++I) |
| 1438 | RewriteMethodDeclaration(*I); |
| 1439 | for (ObjCInterfaceDecl::classmeth_iterator |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1440 | I = ClassDecl->classmeth_begin(), E = ClassDecl->classmeth_end(); |
Fariborz Jahanian | 4339bb3 | 2012-02-15 22:01:47 +0000 | [diff] [blame] | 1441 | I != E; ++I) |
| 1442 | RewriteMethodDeclaration(*I); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1443 | |
Fariborz Jahanian | 4339bb3 | 2012-02-15 22:01:47 +0000 | [diff] [blame] | 1444 | // Lastly, comment out the @end. |
| 1445 | ReplaceText(ClassDecl->getAtEndRange().getBegin(), strlen("@end"), |
| 1446 | "/* @end */"); |
| 1447 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | Stmt *RewriteModernObjC::RewritePropertyOrImplicitSetter(PseudoObjectExpr *PseudoOp) { |
| 1451 | SourceRange OldRange = PseudoOp->getSourceRange(); |
| 1452 | |
| 1453 | // We just magically know some things about the structure of this |
| 1454 | // expression. |
| 1455 | ObjCMessageExpr *OldMsg = |
| 1456 | cast<ObjCMessageExpr>(PseudoOp->getSemanticExpr( |
| 1457 | PseudoOp->getNumSemanticExprs() - 1)); |
| 1458 | |
| 1459 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 1460 | // we need to suppress rewriting the sub-statements. |
Fariborz Jahanian | 88ec610 | 2012-04-10 22:06:54 +0000 | [diff] [blame] | 1461 | Expr *Base; |
| 1462 | SmallVector<Expr*, 2> Args; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1463 | { |
| 1464 | DisableReplaceStmtScope S(*this); |
| 1465 | |
| 1466 | // Rebuild the base expression if we have one. |
| 1467 | Base = 0; |
| 1468 | if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) { |
| 1469 | Base = OldMsg->getInstanceReceiver(); |
| 1470 | Base = cast<OpaqueValueExpr>(Base)->getSourceExpr(); |
| 1471 | Base = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Base)); |
| 1472 | } |
Fariborz Jahanian | 88ec610 | 2012-04-10 22:06:54 +0000 | [diff] [blame] | 1473 | |
| 1474 | unsigned numArgs = OldMsg->getNumArgs(); |
| 1475 | for (unsigned i = 0; i < numArgs; i++) { |
| 1476 | Expr *Arg = OldMsg->getArg(i); |
| 1477 | if (isa<OpaqueValueExpr>(Arg)) |
| 1478 | Arg = cast<OpaqueValueExpr>(Arg)->getSourceExpr(); |
| 1479 | Arg = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Arg)); |
| 1480 | Args.push_back(Arg); |
| 1481 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | // TODO: avoid this copy. |
| 1485 | SmallVector<SourceLocation, 1> SelLocs; |
| 1486 | OldMsg->getSelectorLocs(SelLocs); |
| 1487 | |
| 1488 | ObjCMessageExpr *NewMsg = 0; |
| 1489 | switch (OldMsg->getReceiverKind()) { |
| 1490 | case ObjCMessageExpr::Class: |
| 1491 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1492 | OldMsg->getValueKind(), |
| 1493 | OldMsg->getLeftLoc(), |
| 1494 | OldMsg->getClassReceiverTypeInfo(), |
| 1495 | OldMsg->getSelector(), |
| 1496 | SelLocs, |
| 1497 | OldMsg->getMethodDecl(), |
Fariborz Jahanian | 88ec610 | 2012-04-10 22:06:54 +0000 | [diff] [blame] | 1498 | Args, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1499 | OldMsg->getRightLoc(), |
| 1500 | OldMsg->isImplicit()); |
| 1501 | break; |
| 1502 | |
| 1503 | case ObjCMessageExpr::Instance: |
| 1504 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1505 | OldMsg->getValueKind(), |
| 1506 | OldMsg->getLeftLoc(), |
| 1507 | Base, |
| 1508 | OldMsg->getSelector(), |
| 1509 | SelLocs, |
| 1510 | OldMsg->getMethodDecl(), |
Fariborz Jahanian | 88ec610 | 2012-04-10 22:06:54 +0000 | [diff] [blame] | 1511 | Args, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1512 | OldMsg->getRightLoc(), |
| 1513 | OldMsg->isImplicit()); |
| 1514 | break; |
| 1515 | |
| 1516 | case ObjCMessageExpr::SuperClass: |
| 1517 | case ObjCMessageExpr::SuperInstance: |
| 1518 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1519 | OldMsg->getValueKind(), |
| 1520 | OldMsg->getLeftLoc(), |
| 1521 | OldMsg->getSuperLoc(), |
| 1522 | OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance, |
| 1523 | OldMsg->getSuperType(), |
| 1524 | OldMsg->getSelector(), |
| 1525 | SelLocs, |
| 1526 | OldMsg->getMethodDecl(), |
Fariborz Jahanian | 88ec610 | 2012-04-10 22:06:54 +0000 | [diff] [blame] | 1527 | Args, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1528 | OldMsg->getRightLoc(), |
| 1529 | OldMsg->isImplicit()); |
| 1530 | break; |
| 1531 | } |
| 1532 | |
| 1533 | Stmt *Replacement = SynthMessageExpr(NewMsg); |
| 1534 | ReplaceStmtWithRange(PseudoOp, Replacement, OldRange); |
| 1535 | return Replacement; |
| 1536 | } |
| 1537 | |
| 1538 | Stmt *RewriteModernObjC::RewritePropertyOrImplicitGetter(PseudoObjectExpr *PseudoOp) { |
| 1539 | SourceRange OldRange = PseudoOp->getSourceRange(); |
| 1540 | |
| 1541 | // We just magically know some things about the structure of this |
| 1542 | // expression. |
| 1543 | ObjCMessageExpr *OldMsg = |
| 1544 | cast<ObjCMessageExpr>(PseudoOp->getResultExpr()->IgnoreImplicit()); |
| 1545 | |
| 1546 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 1547 | // we need to suppress rewriting the sub-statements. |
| 1548 | Expr *Base = 0; |
Fariborz Jahanian | 88ec610 | 2012-04-10 22:06:54 +0000 | [diff] [blame] | 1549 | SmallVector<Expr*, 1> Args; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1550 | { |
| 1551 | DisableReplaceStmtScope S(*this); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1552 | // Rebuild the base expression if we have one. |
| 1553 | if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) { |
| 1554 | Base = OldMsg->getInstanceReceiver(); |
| 1555 | Base = cast<OpaqueValueExpr>(Base)->getSourceExpr(); |
| 1556 | Base = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Base)); |
| 1557 | } |
Fariborz Jahanian | 88ec610 | 2012-04-10 22:06:54 +0000 | [diff] [blame] | 1558 | unsigned numArgs = OldMsg->getNumArgs(); |
| 1559 | for (unsigned i = 0; i < numArgs; i++) { |
| 1560 | Expr *Arg = OldMsg->getArg(i); |
| 1561 | if (isa<OpaqueValueExpr>(Arg)) |
| 1562 | Arg = cast<OpaqueValueExpr>(Arg)->getSourceExpr(); |
| 1563 | Arg = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Arg)); |
| 1564 | Args.push_back(Arg); |
| 1565 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
| 1568 | // Intentionally empty. |
| 1569 | SmallVector<SourceLocation, 1> SelLocs; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1570 | |
| 1571 | ObjCMessageExpr *NewMsg = 0; |
| 1572 | switch (OldMsg->getReceiverKind()) { |
| 1573 | case ObjCMessageExpr::Class: |
| 1574 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1575 | OldMsg->getValueKind(), |
| 1576 | OldMsg->getLeftLoc(), |
| 1577 | OldMsg->getClassReceiverTypeInfo(), |
| 1578 | OldMsg->getSelector(), |
| 1579 | SelLocs, |
| 1580 | OldMsg->getMethodDecl(), |
| 1581 | Args, |
| 1582 | OldMsg->getRightLoc(), |
| 1583 | OldMsg->isImplicit()); |
| 1584 | break; |
| 1585 | |
| 1586 | case ObjCMessageExpr::Instance: |
| 1587 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1588 | OldMsg->getValueKind(), |
| 1589 | OldMsg->getLeftLoc(), |
| 1590 | Base, |
| 1591 | OldMsg->getSelector(), |
| 1592 | SelLocs, |
| 1593 | OldMsg->getMethodDecl(), |
| 1594 | Args, |
| 1595 | OldMsg->getRightLoc(), |
| 1596 | OldMsg->isImplicit()); |
| 1597 | break; |
| 1598 | |
| 1599 | case ObjCMessageExpr::SuperClass: |
| 1600 | case ObjCMessageExpr::SuperInstance: |
| 1601 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1602 | OldMsg->getValueKind(), |
| 1603 | OldMsg->getLeftLoc(), |
| 1604 | OldMsg->getSuperLoc(), |
| 1605 | OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance, |
| 1606 | OldMsg->getSuperType(), |
| 1607 | OldMsg->getSelector(), |
| 1608 | SelLocs, |
| 1609 | OldMsg->getMethodDecl(), |
| 1610 | Args, |
| 1611 | OldMsg->getRightLoc(), |
| 1612 | OldMsg->isImplicit()); |
| 1613 | break; |
| 1614 | } |
| 1615 | |
| 1616 | Stmt *Replacement = SynthMessageExpr(NewMsg); |
| 1617 | ReplaceStmtWithRange(PseudoOp, Replacement, OldRange); |
| 1618 | return Replacement; |
| 1619 | } |
| 1620 | |
| 1621 | /// SynthCountByEnumWithState - To print: |
| 1622 | /// ((unsigned int (*) |
| 1623 | /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
| 1624 | /// (void *)objc_msgSend)((id)l_collection, |
| 1625 | /// sel_registerName( |
| 1626 | /// "countByEnumeratingWithState:objects:count:"), |
| 1627 | /// &enumState, |
| 1628 | /// (id *)__rw_items, (unsigned int)16) |
| 1629 | /// |
| 1630 | void RewriteModernObjC::SynthCountByEnumWithState(std::string &buf) { |
| 1631 | buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, " |
| 1632 | "id *, unsigned int))(void *)objc_msgSend)"; |
| 1633 | buf += "\n\t\t"; |
| 1634 | buf += "((id)l_collection,\n\t\t"; |
| 1635 | buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; |
| 1636 | buf += "\n\t\t"; |
| 1637 | buf += "&enumState, " |
| 1638 | "(id *)__rw_items, (unsigned int)16)"; |
| 1639 | } |
| 1640 | |
| 1641 | /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach |
| 1642 | /// statement to exit to its outer synthesized loop. |
| 1643 | /// |
| 1644 | Stmt *RewriteModernObjC::RewriteBreakStmt(BreakStmt *S) { |
| 1645 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1646 | return S; |
| 1647 | // replace break with goto __break_label |
| 1648 | std::string buf; |
| 1649 | |
| 1650 | SourceLocation startLoc = S->getLocStart(); |
| 1651 | buf = "goto __break_label_"; |
| 1652 | buf += utostr(ObjCBcLabelNo.back()); |
| 1653 | ReplaceText(startLoc, strlen("break"), buf); |
| 1654 | |
| 1655 | return 0; |
| 1656 | } |
| 1657 | |
Fariborz Jahanian | f616ae2 | 2012-11-06 23:25:49 +0000 | [diff] [blame] | 1658 | void RewriteModernObjC::ConvertSourceLocationToLineDirective( |
| 1659 | SourceLocation Loc, |
| 1660 | std::string &LineString) { |
Fariborz Jahanian | ada7191 | 2013-02-08 00:27:34 +0000 | [diff] [blame] | 1661 | if (Loc.isFileID() && GenerateLineInfo) { |
Fariborz Jahanian | f616ae2 | 2012-11-06 23:25:49 +0000 | [diff] [blame] | 1662 | LineString += "\n#line "; |
| 1663 | PresumedLoc PLoc = SM->getPresumedLoc(Loc); |
| 1664 | LineString += utostr(PLoc.getLine()); |
| 1665 | LineString += " \""; |
| 1666 | LineString += Lexer::Stringify(PLoc.getFilename()); |
| 1667 | LineString += "\"\n"; |
| 1668 | } |
| 1669 | } |
| 1670 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1671 | /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach |
| 1672 | /// statement to continue with its inner synthesized loop. |
| 1673 | /// |
| 1674 | Stmt *RewriteModernObjC::RewriteContinueStmt(ContinueStmt *S) { |
| 1675 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1676 | return S; |
| 1677 | // replace continue with goto __continue_label |
| 1678 | std::string buf; |
| 1679 | |
| 1680 | SourceLocation startLoc = S->getLocStart(); |
| 1681 | buf = "goto __continue_label_"; |
| 1682 | buf += utostr(ObjCBcLabelNo.back()); |
| 1683 | ReplaceText(startLoc, strlen("continue"), buf); |
| 1684 | |
| 1685 | return 0; |
| 1686 | } |
| 1687 | |
| 1688 | /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. |
| 1689 | /// It rewrites: |
| 1690 | /// for ( type elem in collection) { stmts; } |
| 1691 | |
| 1692 | /// Into: |
| 1693 | /// { |
| 1694 | /// type elem; |
| 1695 | /// struct __objcFastEnumerationState enumState = { 0 }; |
| 1696 | /// id __rw_items[16]; |
| 1697 | /// id l_collection = (id)collection; |
| 1698 | /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1699 | /// objects:__rw_items count:16]; |
| 1700 | /// if (limit) { |
| 1701 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1702 | /// do { |
| 1703 | /// unsigned long counter = 0; |
| 1704 | /// do { |
| 1705 | /// if (startMutations != *enumState.mutationsPtr) |
| 1706 | /// objc_enumerationMutation(l_collection); |
| 1707 | /// elem = (type)enumState.itemsPtr[counter++]; |
| 1708 | /// stmts; |
| 1709 | /// __continue_label: ; |
| 1710 | /// } while (counter < limit); |
| 1711 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
| 1712 | /// objects:__rw_items count:16]); |
| 1713 | /// elem = nil; |
| 1714 | /// __break_label: ; |
| 1715 | /// } |
| 1716 | /// else |
| 1717 | /// elem = nil; |
| 1718 | /// } |
| 1719 | /// |
| 1720 | Stmt *RewriteModernObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
| 1721 | SourceLocation OrigEnd) { |
| 1722 | assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); |
| 1723 | assert(isa<ObjCForCollectionStmt>(Stmts.back()) && |
| 1724 | "ObjCForCollectionStmt Statement stack mismatch"); |
| 1725 | assert(!ObjCBcLabelNo.empty() && |
| 1726 | "ObjCForCollectionStmt - Label No stack empty"); |
| 1727 | |
| 1728 | SourceLocation startLoc = S->getLocStart(); |
| 1729 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1730 | StringRef elementName; |
| 1731 | std::string elementTypeAsString; |
| 1732 | std::string buf; |
Fariborz Jahanian | f616ae2 | 2012-11-06 23:25:49 +0000 | [diff] [blame] | 1733 | // line directive first. |
| 1734 | SourceLocation ForEachLoc = S->getForLoc(); |
| 1735 | ConvertSourceLocationToLineDirective(ForEachLoc, buf); |
| 1736 | buf += "{\n\t"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1737 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { |
| 1738 | // type elem; |
| 1739 | NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl()); |
| 1740 | QualType ElementType = cast<ValueDecl>(D)->getType(); |
| 1741 | if (ElementType->isObjCQualifiedIdType() || |
| 1742 | ElementType->isObjCQualifiedInterfaceType()) |
| 1743 | // Simply use 'id' for all qualified types. |
| 1744 | elementTypeAsString = "id"; |
| 1745 | else |
| 1746 | elementTypeAsString = ElementType.getAsString(Context->getPrintingPolicy()); |
| 1747 | buf += elementTypeAsString; |
| 1748 | buf += " "; |
| 1749 | elementName = D->getName(); |
| 1750 | buf += elementName; |
| 1751 | buf += ";\n\t"; |
| 1752 | } |
| 1753 | else { |
| 1754 | DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); |
| 1755 | elementName = DR->getDecl()->getName(); |
| 1756 | ValueDecl *VD = cast<ValueDecl>(DR->getDecl()); |
| 1757 | if (VD->getType()->isObjCQualifiedIdType() || |
| 1758 | VD->getType()->isObjCQualifiedInterfaceType()) |
| 1759 | // Simply use 'id' for all qualified types. |
| 1760 | elementTypeAsString = "id"; |
| 1761 | else |
| 1762 | elementTypeAsString = VD->getType().getAsString(Context->getPrintingPolicy()); |
| 1763 | } |
| 1764 | |
| 1765 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1766 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
| 1767 | // id __rw_items[16]; |
| 1768 | buf += "id __rw_items[16];\n\t"; |
| 1769 | // id l_collection = (id) |
| 1770 | buf += "id l_collection = (id)"; |
| 1771 | // Find start location of 'collection' the hard way! |
| 1772 | const char *startCollectionBuf = startBuf; |
| 1773 | startCollectionBuf += 3; // skip 'for' |
| 1774 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1775 | startCollectionBuf++; // skip '(' |
| 1776 | // find 'in' and skip it. |
| 1777 | while (*startCollectionBuf != ' ' || |
| 1778 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1779 | (*(startCollectionBuf+3) != ' ' && |
| 1780 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1781 | startCollectionBuf++; |
| 1782 | startCollectionBuf += 3; |
| 1783 | |
| 1784 | // Replace: "for (type element in" with string constructed thus far. |
| 1785 | ReplaceText(startLoc, startCollectionBuf - startBuf, buf); |
| 1786 | // Replace ')' in for '(' type elem in collection ')' with ';' |
| 1787 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1788 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
| 1789 | SourceLocation lparenLoc = startLoc.getLocWithOffset(rparenBuf-startBuf); |
| 1790 | buf = ";\n\t"; |
| 1791 | |
| 1792 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1793 | // objects:__rw_items count:16]; |
| 1794 | // which is synthesized into: |
| 1795 | // unsigned int limit = |
| 1796 | // ((unsigned int (*) |
| 1797 | // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
| 1798 | // (void *)objc_msgSend)((id)l_collection, |
| 1799 | // sel_registerName( |
| 1800 | // "countByEnumeratingWithState:objects:count:"), |
| 1801 | // (struct __objcFastEnumerationState *)&state, |
| 1802 | // (id *)__rw_items, (unsigned int)16); |
| 1803 | buf += "unsigned long limit =\n\t\t"; |
| 1804 | SynthCountByEnumWithState(buf); |
| 1805 | buf += ";\n\t"; |
| 1806 | /// if (limit) { |
| 1807 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1808 | /// do { |
| 1809 | /// unsigned long counter = 0; |
| 1810 | /// do { |
| 1811 | /// if (startMutations != *enumState.mutationsPtr) |
| 1812 | /// objc_enumerationMutation(l_collection); |
| 1813 | /// elem = (type)enumState.itemsPtr[counter++]; |
| 1814 | buf += "if (limit) {\n\t"; |
| 1815 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1816 | buf += "do {\n\t\t"; |
| 1817 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1818 | buf += "do {\n\t\t\t"; |
| 1819 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1820 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1821 | buf += elementName; |
| 1822 | buf += " = ("; |
| 1823 | buf += elementTypeAsString; |
| 1824 | buf += ")enumState.itemsPtr[counter++];"; |
| 1825 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
| 1826 | ReplaceText(lparenLoc, 1, buf); |
| 1827 | |
| 1828 | /// __continue_label: ; |
| 1829 | /// } while (counter < limit); |
| 1830 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
| 1831 | /// objects:__rw_items count:16]); |
| 1832 | /// elem = nil; |
| 1833 | /// __break_label: ; |
| 1834 | /// } |
| 1835 | /// else |
| 1836 | /// elem = nil; |
| 1837 | /// } |
| 1838 | /// |
| 1839 | buf = ";\n\t"; |
| 1840 | buf += "__continue_label_"; |
| 1841 | buf += utostr(ObjCBcLabelNo.back()); |
| 1842 | buf += ": ;"; |
| 1843 | buf += "\n\t\t"; |
| 1844 | buf += "} while (counter < limit);\n\t"; |
| 1845 | buf += "} while (limit = "; |
| 1846 | SynthCountByEnumWithState(buf); |
| 1847 | buf += ");\n\t"; |
| 1848 | buf += elementName; |
| 1849 | buf += " = (("; |
| 1850 | buf += elementTypeAsString; |
| 1851 | buf += ")0);\n\t"; |
| 1852 | buf += "__break_label_"; |
| 1853 | buf += utostr(ObjCBcLabelNo.back()); |
| 1854 | buf += ": ;\n\t"; |
| 1855 | buf += "}\n\t"; |
| 1856 | buf += "else\n\t\t"; |
| 1857 | buf += elementName; |
| 1858 | buf += " = (("; |
| 1859 | buf += elementTypeAsString; |
| 1860 | buf += ")0);\n\t"; |
| 1861 | buf += "}\n"; |
| 1862 | |
| 1863 | // Insert all these *after* the statement body. |
| 1864 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
| 1865 | if (isa<CompoundStmt>(S->getBody())) { |
| 1866 | SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(1); |
| 1867 | InsertText(endBodyLoc, buf); |
| 1868 | } else { |
| 1869 | /* Need to treat single statements specially. For example: |
| 1870 | * |
| 1871 | * for (A *a in b) if (stuff()) break; |
| 1872 | * for (A *a in b) xxxyy; |
| 1873 | * |
| 1874 | * The following code simply scans ahead to the semi to find the actual end. |
| 1875 | */ |
| 1876 | const char *stmtBuf = SM->getCharacterData(OrigEnd); |
| 1877 | const char *semiBuf = strchr(stmtBuf, ';'); |
| 1878 | assert(semiBuf && "Can't find ';'"); |
| 1879 | SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(semiBuf-stmtBuf+1); |
| 1880 | InsertText(endBodyLoc, buf); |
| 1881 | } |
| 1882 | Stmts.pop_back(); |
| 1883 | ObjCBcLabelNo.pop_back(); |
| 1884 | return 0; |
| 1885 | } |
| 1886 | |
Fariborz Jahanian | 542125f | 2012-03-16 21:33:16 +0000 | [diff] [blame] | 1887 | static void Write_RethrowObject(std::string &buf) { |
| 1888 | buf += "{ struct _FIN { _FIN(id reth) : rethrow(reth) {}\n"; |
| 1889 | buf += "\t~_FIN() { if (rethrow) objc_exception_throw(rethrow); }\n"; |
| 1890 | buf += "\tid rethrow;\n"; |
| 1891 | buf += "\t} _fin_force_rethow(_rethrow);"; |
| 1892 | } |
| 1893 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1894 | /// RewriteObjCSynchronizedStmt - |
| 1895 | /// This routine rewrites @synchronized(expr) stmt; |
| 1896 | /// into: |
| 1897 | /// objc_sync_enter(expr); |
| 1898 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1899 | /// |
| 1900 | Stmt *RewriteModernObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
| 1901 | // Get the start location and compute the semi location. |
| 1902 | SourceLocation startLoc = S->getLocStart(); |
| 1903 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1904 | |
| 1905 | assert((*startBuf == '@') && "bogus @synchronized location"); |
| 1906 | |
| 1907 | std::string buf; |
Fariborz Jahanian | 43f4f1e | 2012-11-07 00:43:05 +0000 | [diff] [blame] | 1908 | SourceLocation SynchLoc = S->getAtSynchronizedLoc(); |
| 1909 | ConvertSourceLocationToLineDirective(SynchLoc, buf); |
| 1910 | buf += "{ id _rethrow = 0; id _sync_obj = "; |
Fariborz Jahanian | 542125f | 2012-03-16 21:33:16 +0000 | [diff] [blame] | 1911 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1912 | const char *lparenBuf = startBuf; |
| 1913 | while (*lparenBuf != '(') lparenBuf++; |
| 1914 | ReplaceText(startLoc, lparenBuf-startBuf+1, buf); |
Fariborz Jahanian | 22e2f85 | 2012-03-17 17:46:02 +0000 | [diff] [blame] | 1915 | |
| 1916 | buf = "; objc_sync_enter(_sync_obj);\n"; |
| 1917 | buf += "try {\n\tstruct _SYNC_EXIT { _SYNC_EXIT(id arg) : sync_exit(arg) {}"; |
| 1918 | buf += "\n\t~_SYNC_EXIT() {objc_sync_exit(sync_exit);}"; |
| 1919 | buf += "\n\tid sync_exit;"; |
| 1920 | buf += "\n\t} _sync_exit(_sync_obj);\n"; |
| 1921 | |
| 1922 | // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since |
| 1923 | // the sync expression is typically a message expression that's already |
| 1924 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1925 | SourceLocation RParenExprLoc = S->getSynchBody()->getLocStart(); |
| 1926 | const char *RParenExprLocBuf = SM->getCharacterData(RParenExprLoc); |
| 1927 | while (*RParenExprLocBuf != ')') RParenExprLocBuf--; |
| 1928 | RParenExprLoc = startLoc.getLocWithOffset(RParenExprLocBuf-startBuf); |
| 1929 | |
| 1930 | SourceLocation LBranceLoc = S->getSynchBody()->getLocStart(); |
| 1931 | const char *LBraceLocBuf = SM->getCharacterData(LBranceLoc); |
| 1932 | assert (*LBraceLocBuf == '{'); |
| 1933 | ReplaceText(RParenExprLoc, (LBraceLocBuf - SM->getCharacterData(RParenExprLoc) + 1), buf); |
Fariborz Jahanian | 542125f | 2012-03-16 21:33:16 +0000 | [diff] [blame] | 1934 | |
Fariborz Jahanian | b655bf0 | 2012-03-16 21:43:45 +0000 | [diff] [blame] | 1935 | SourceLocation startRBraceLoc = S->getSynchBody()->getLocEnd(); |
Matt Beaumont-Gay | 9ab511c | 2012-03-16 22:20:39 +0000 | [diff] [blame] | 1936 | assert((*SM->getCharacterData(startRBraceLoc) == '}') && |
| 1937 | "bogus @synchronized block"); |
Fariborz Jahanian | 542125f | 2012-03-16 21:33:16 +0000 | [diff] [blame] | 1938 | |
| 1939 | buf = "} catch (id e) {_rethrow = e;}\n"; |
| 1940 | Write_RethrowObject(buf); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1941 | buf += "}\n"; |
Fariborz Jahanian | 542125f | 2012-03-16 21:33:16 +0000 | [diff] [blame] | 1942 | buf += "}\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1943 | |
Fariborz Jahanian | b655bf0 | 2012-03-16 21:43:45 +0000 | [diff] [blame] | 1944 | ReplaceText(startRBraceLoc, 1, buf); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1945 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1946 | return 0; |
| 1947 | } |
| 1948 | |
| 1949 | void RewriteModernObjC::WarnAboutReturnGotoStmts(Stmt *S) |
| 1950 | { |
| 1951 | // Perform a bottom up traversal of all children. |
| 1952 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
| 1953 | if (*CI) |
| 1954 | WarnAboutReturnGotoStmts(*CI); |
| 1955 | |
| 1956 | if (isa<ReturnStmt>(S) || isa<GotoStmt>(S)) { |
| 1957 | Diags.Report(Context->getFullLoc(S->getLocStart()), |
| 1958 | TryFinallyContainsReturnDiag); |
| 1959 | } |
| 1960 | return; |
| 1961 | } |
| 1962 | |
Fariborz Jahanian | 042b91d | 2012-05-23 23:47:20 +0000 | [diff] [blame] | 1963 | Stmt *RewriteModernObjC::RewriteObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S) { |
| 1964 | SourceLocation startLoc = S->getAtLoc(); |
| 1965 | ReplaceText(startLoc, strlen("@autoreleasepool"), "/* @autoreleasepool */"); |
Fariborz Jahanian | c9b72b6 | 2012-05-24 22:59:56 +0000 | [diff] [blame] | 1966 | ReplaceText(S->getSubStmt()->getLocStart(), 1, |
| 1967 | "{ __AtAutoreleasePool __autoreleasepool; "); |
Fariborz Jahanian | 042b91d | 2012-05-23 23:47:20 +0000 | [diff] [blame] | 1968 | |
| 1969 | return 0; |
| 1970 | } |
| 1971 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1972 | Stmt *RewriteModernObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Fariborz Jahanian | b122818 | 2012-03-15 22:42:15 +0000 | [diff] [blame] | 1973 | ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt(); |
Fariborz Jahanian | 220419a | 2012-03-15 23:50:33 +0000 | [diff] [blame] | 1974 | bool noCatch = S->getNumCatchStmts() == 0; |
Fariborz Jahanian | b122818 | 2012-03-15 22:42:15 +0000 | [diff] [blame] | 1975 | std::string buf; |
Fariborz Jahanian | f616ae2 | 2012-11-06 23:25:49 +0000 | [diff] [blame] | 1976 | SourceLocation TryLocation = S->getAtTryLoc(); |
| 1977 | ConvertSourceLocationToLineDirective(TryLocation, buf); |
Fariborz Jahanian | b122818 | 2012-03-15 22:42:15 +0000 | [diff] [blame] | 1978 | |
| 1979 | if (finalStmt) { |
Fariborz Jahanian | 220419a | 2012-03-15 23:50:33 +0000 | [diff] [blame] | 1980 | if (noCatch) |
Fariborz Jahanian | f616ae2 | 2012-11-06 23:25:49 +0000 | [diff] [blame] | 1981 | buf += "{ id volatile _rethrow = 0;\n"; |
Fariborz Jahanian | 220419a | 2012-03-15 23:50:33 +0000 | [diff] [blame] | 1982 | else { |
Fariborz Jahanian | f616ae2 | 2012-11-06 23:25:49 +0000 | [diff] [blame] | 1983 | buf += "{ id volatile _rethrow = 0;\ntry {\n"; |
Fariborz Jahanian | 220419a | 2012-03-15 23:50:33 +0000 | [diff] [blame] | 1984 | } |
Fariborz Jahanian | b122818 | 2012-03-15 22:42:15 +0000 | [diff] [blame] | 1985 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1986 | // Get the start location and compute the semi location. |
| 1987 | SourceLocation startLoc = S->getLocStart(); |
| 1988 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1989 | |
| 1990 | assert((*startBuf == '@') && "bogus @try location"); |
Fariborz Jahanian | b122818 | 2012-03-15 22:42:15 +0000 | [diff] [blame] | 1991 | if (finalStmt) |
| 1992 | ReplaceText(startLoc, 1, buf); |
| 1993 | else |
| 1994 | // @try -> try |
| 1995 | ReplaceText(startLoc, 1, ""); |
| 1996 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1997 | for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I) { |
| 1998 | ObjCAtCatchStmt *Catch = S->getCatchStmt(I); |
Fariborz Jahanian | 4c14881 | 2012-03-15 20:11:10 +0000 | [diff] [blame] | 1999 | VarDecl *catchDecl = Catch->getCatchParamDecl(); |
Fariborz Jahanian | c38503b | 2012-03-12 23:58:28 +0000 | [diff] [blame] | 2000 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2001 | startLoc = Catch->getLocStart(); |
Fariborz Jahanian | 4c14881 | 2012-03-15 20:11:10 +0000 | [diff] [blame] | 2002 | bool AtRemoved = false; |
| 2003 | if (catchDecl) { |
| 2004 | QualType t = catchDecl->getType(); |
| 2005 | if (const ObjCObjectPointerType *Ptr = t->getAs<ObjCObjectPointerType>()) { |
| 2006 | // Should be a pointer to a class. |
| 2007 | ObjCInterfaceDecl *IDecl = Ptr->getObjectType()->getInterface(); |
| 2008 | if (IDecl) { |
| 2009 | std::string Result; |
Fariborz Jahanian | f616ae2 | 2012-11-06 23:25:49 +0000 | [diff] [blame] | 2010 | ConvertSourceLocationToLineDirective(Catch->getLocStart(), Result); |
| 2011 | |
Fariborz Jahanian | 4c14881 | 2012-03-15 20:11:10 +0000 | [diff] [blame] | 2012 | startBuf = SM->getCharacterData(startLoc); |
| 2013 | assert((*startBuf == '@') && "bogus @catch location"); |
| 2014 | SourceLocation rParenLoc = Catch->getRParenLoc(); |
| 2015 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 2016 | |
| 2017 | // _objc_exc_Foo *_e as argument to catch. |
Fariborz Jahanian | f616ae2 | 2012-11-06 23:25:49 +0000 | [diff] [blame] | 2018 | Result += "catch (_objc_exc_"; Result += IDecl->getNameAsString(); |
Fariborz Jahanian | 4c14881 | 2012-03-15 20:11:10 +0000 | [diff] [blame] | 2019 | Result += " *_"; Result += catchDecl->getNameAsString(); |
| 2020 | Result += ")"; |
| 2021 | ReplaceText(startLoc, rParenBuf-startBuf+1, Result); |
| 2022 | // Foo *e = (Foo *)_e; |
| 2023 | Result.clear(); |
| 2024 | Result = "{ "; |
| 2025 | Result += IDecl->getNameAsString(); |
| 2026 | Result += " *"; Result += catchDecl->getNameAsString(); |
| 2027 | Result += " = ("; Result += IDecl->getNameAsString(); Result += "*)"; |
| 2028 | Result += "_"; Result += catchDecl->getNameAsString(); |
| 2029 | |
| 2030 | Result += "; "; |
| 2031 | SourceLocation lBraceLoc = Catch->getCatchBody()->getLocStart(); |
| 2032 | ReplaceText(lBraceLoc, 1, Result); |
| 2033 | AtRemoved = true; |
| 2034 | } |
| 2035 | } |
| 2036 | } |
| 2037 | if (!AtRemoved) |
| 2038 | // @catch -> catch |
| 2039 | ReplaceText(startLoc, 1, ""); |
Fariborz Jahanian | c38503b | 2012-03-12 23:58:28 +0000 | [diff] [blame] | 2040 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2041 | } |
Fariborz Jahanian | 220419a | 2012-03-15 23:50:33 +0000 | [diff] [blame] | 2042 | if (finalStmt) { |
| 2043 | buf.clear(); |
Fariborz Jahanian | f616ae2 | 2012-11-06 23:25:49 +0000 | [diff] [blame] | 2044 | SourceLocation FinallyLoc = finalStmt->getLocStart(); |
| 2045 | |
| 2046 | if (noCatch) { |
| 2047 | ConvertSourceLocationToLineDirective(FinallyLoc, buf); |
| 2048 | buf += "catch (id e) {_rethrow = e;}\n"; |
| 2049 | } |
| 2050 | else { |
| 2051 | buf += "}\n"; |
| 2052 | ConvertSourceLocationToLineDirective(FinallyLoc, buf); |
| 2053 | buf += "catch (id e) {_rethrow = e;}\n"; |
| 2054 | } |
| 2055 | |
Fariborz Jahanian | 220419a | 2012-03-15 23:50:33 +0000 | [diff] [blame] | 2056 | SourceLocation startFinalLoc = finalStmt->getLocStart(); |
| 2057 | ReplaceText(startFinalLoc, 8, buf); |
| 2058 | Stmt *body = finalStmt->getFinallyBody(); |
| 2059 | SourceLocation startFinalBodyLoc = body->getLocStart(); |
| 2060 | buf.clear(); |
Fariborz Jahanian | 542125f | 2012-03-16 21:33:16 +0000 | [diff] [blame] | 2061 | Write_RethrowObject(buf); |
Fariborz Jahanian | 220419a | 2012-03-15 23:50:33 +0000 | [diff] [blame] | 2062 | ReplaceText(startFinalBodyLoc, 1, buf); |
| 2063 | |
| 2064 | SourceLocation endFinalBodyLoc = body->getLocEnd(); |
| 2065 | ReplaceText(endFinalBodyLoc, 1, "}\n}"); |
Fariborz Jahanian | 22e2f85 | 2012-03-17 17:46:02 +0000 | [diff] [blame] | 2066 | // Now check for any return/continue/go statements within the @try. |
| 2067 | WarnAboutReturnGotoStmts(S->getTryBody()); |
Fariborz Jahanian | 220419a | 2012-03-15 23:50:33 +0000 | [diff] [blame] | 2068 | } |
| 2069 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2070 | return 0; |
| 2071 | } |
| 2072 | |
| 2073 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
| 2074 | // the throw expression is typically a message expression that's already |
| 2075 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 2076 | Stmt *RewriteModernObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
| 2077 | // Get the start location and compute the semi location. |
| 2078 | SourceLocation startLoc = S->getLocStart(); |
| 2079 | const char *startBuf = SM->getCharacterData(startLoc); |
| 2080 | |
| 2081 | assert((*startBuf == '@') && "bogus @throw location"); |
| 2082 | |
| 2083 | std::string buf; |
| 2084 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
| 2085 | if (S->getThrowExpr()) |
| 2086 | buf = "objc_exception_throw("; |
Fariborz Jahanian | 4053946 | 2012-03-16 16:52:06 +0000 | [diff] [blame] | 2087 | else |
| 2088 | buf = "throw"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2089 | |
| 2090 | // handle "@ throw" correctly. |
| 2091 | const char *wBuf = strchr(startBuf, 'w'); |
| 2092 | assert((*wBuf == 'w') && "@throw: can't find 'w'"); |
| 2093 | ReplaceText(startLoc, wBuf-startBuf+1, buf); |
| 2094 | |
Fariborz Jahanian | a09cd81 | 2013-02-11 19:30:33 +0000 | [diff] [blame] | 2095 | SourceLocation endLoc = S->getLocEnd(); |
| 2096 | const char *endBuf = SM->getCharacterData(endLoc); |
| 2097 | const char *semiBuf = strchr(endBuf, ';'); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2098 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
| 2099 | SourceLocation semiLoc = startLoc.getLocWithOffset(semiBuf-startBuf); |
Fariborz Jahanian | 4053946 | 2012-03-16 16:52:06 +0000 | [diff] [blame] | 2100 | if (S->getThrowExpr()) |
| 2101 | ReplaceText(semiLoc, 1, ");"); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2102 | return 0; |
| 2103 | } |
| 2104 | |
| 2105 | Stmt *RewriteModernObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
| 2106 | // Create a new string expression. |
| 2107 | QualType StrType = Context->getPointerType(Context->CharTy); |
| 2108 | std::string StrEncoding; |
| 2109 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); |
| 2110 | Expr *Replacement = StringLiteral::Create(*Context, StrEncoding, |
| 2111 | StringLiteral::Ascii, false, |
| 2112 | StrType, SourceLocation()); |
| 2113 | ReplaceStmt(Exp, Replacement); |
| 2114 | |
| 2115 | // Replace this subexpr in the parent. |
| 2116 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
| 2117 | return Replacement; |
| 2118 | } |
| 2119 | |
| 2120 | Stmt *RewriteModernObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
| 2121 | if (!SelGetUidFunctionDecl) |
| 2122 | SynthSelGetUidFunctionDecl(); |
| 2123 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 2124 | // Create a call to sel_registerName("selName"). |
| 2125 | SmallVector<Expr*, 8> SelExprs; |
| 2126 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2127 | SelExprs.push_back(StringLiteral::Create(*Context, |
| 2128 | Exp->getSelector().getAsString(), |
| 2129 | StringLiteral::Ascii, false, |
| 2130 | argType, SourceLocation())); |
| 2131 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2132 | &SelExprs[0], SelExprs.size()); |
| 2133 | ReplaceStmt(Exp, SelExp); |
| 2134 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
| 2135 | return SelExp; |
| 2136 | } |
| 2137 | |
| 2138 | CallExpr *RewriteModernObjC::SynthesizeCallToFunctionDecl( |
| 2139 | FunctionDecl *FD, Expr **args, unsigned nargs, SourceLocation StartLoc, |
| 2140 | SourceLocation EndLoc) { |
| 2141 | // Get the type, we will need to reference it in a couple spots. |
| 2142 | QualType msgSendType = FD->getType(); |
| 2143 | |
| 2144 | // Create a reference to the objc_msgSend() declaration. |
| 2145 | DeclRefExpr *DRE = |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2146 | new (Context) DeclRefExpr(FD, false, msgSendType, VK_LValue, SourceLocation()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2147 | |
| 2148 | // Now, we cast the reference to a pointer to the objc_msgSend type. |
| 2149 | QualType pToFunc = Context->getPointerType(msgSendType); |
| 2150 | ImplicitCastExpr *ICE = |
| 2151 | ImplicitCastExpr::Create(*Context, pToFunc, CK_FunctionToPointerDecay, |
| 2152 | DRE, 0, VK_RValue); |
| 2153 | |
| 2154 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
| 2155 | |
| 2156 | CallExpr *Exp = |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2157 | new (Context) CallExpr(*Context, ICE, llvm::makeArrayRef(args, nargs), |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2158 | FT->getCallResultType(*Context), |
| 2159 | VK_RValue, EndLoc); |
| 2160 | return Exp; |
| 2161 | } |
| 2162 | |
| 2163 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 2164 | const char *&startRef, const char *&endRef) { |
| 2165 | while (startBuf < endBuf) { |
| 2166 | if (*startBuf == '<') |
| 2167 | startRef = startBuf; // mark the start. |
| 2168 | if (*startBuf == '>') { |
| 2169 | if (startRef && *startRef == '<') { |
| 2170 | endRef = startBuf; // mark the end. |
| 2171 | return true; |
| 2172 | } |
| 2173 | return false; |
| 2174 | } |
| 2175 | startBuf++; |
| 2176 | } |
| 2177 | return false; |
| 2178 | } |
| 2179 | |
| 2180 | static void scanToNextArgument(const char *&argRef) { |
| 2181 | int angle = 0; |
| 2182 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 2183 | if (*argRef == '<') |
| 2184 | angle++; |
| 2185 | else if (*argRef == '>') |
| 2186 | angle--; |
| 2187 | argRef++; |
| 2188 | } |
| 2189 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 2190 | } |
| 2191 | |
| 2192 | bool RewriteModernObjC::needToScanForQualifiers(QualType T) { |
| 2193 | if (T->isObjCQualifiedIdType()) |
| 2194 | return true; |
| 2195 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 2196 | if (PT->getPointeeType()->isObjCQualifiedIdType()) |
| 2197 | return true; |
| 2198 | } |
| 2199 | if (T->isObjCObjectPointerType()) { |
| 2200 | T = T->getPointeeType(); |
| 2201 | return T->isObjCQualifiedInterfaceType(); |
| 2202 | } |
| 2203 | if (T->isArrayType()) { |
| 2204 | QualType ElemTy = Context->getBaseElementType(T); |
| 2205 | return needToScanForQualifiers(ElemTy); |
| 2206 | } |
| 2207 | return false; |
| 2208 | } |
| 2209 | |
| 2210 | void RewriteModernObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) { |
| 2211 | QualType Type = E->getType(); |
| 2212 | if (needToScanForQualifiers(Type)) { |
| 2213 | SourceLocation Loc, EndLoc; |
| 2214 | |
| 2215 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) { |
| 2216 | Loc = ECE->getLParenLoc(); |
| 2217 | EndLoc = ECE->getRParenLoc(); |
| 2218 | } else { |
| 2219 | Loc = E->getLocStart(); |
| 2220 | EndLoc = E->getLocEnd(); |
| 2221 | } |
| 2222 | // This will defend against trying to rewrite synthesized expressions. |
| 2223 | if (Loc.isInvalid() || EndLoc.isInvalid()) |
| 2224 | return; |
| 2225 | |
| 2226 | const char *startBuf = SM->getCharacterData(Loc); |
| 2227 | const char *endBuf = SM->getCharacterData(EndLoc); |
| 2228 | const char *startRef = 0, *endRef = 0; |
| 2229 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2230 | // Get the locations of the startRef, endRef. |
| 2231 | SourceLocation LessLoc = Loc.getLocWithOffset(startRef-startBuf); |
| 2232 | SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-startBuf+1); |
| 2233 | // Comment out the protocol references. |
| 2234 | InsertText(LessLoc, "/*"); |
| 2235 | InsertText(GreaterLoc, "*/"); |
| 2236 | } |
| 2237 | } |
| 2238 | } |
| 2239 | |
| 2240 | void RewriteModernObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
| 2241 | SourceLocation Loc; |
| 2242 | QualType Type; |
| 2243 | const FunctionProtoType *proto = 0; |
| 2244 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 2245 | Loc = VD->getLocation(); |
| 2246 | Type = VD->getType(); |
| 2247 | } |
| 2248 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 2249 | Loc = FD->getLocation(); |
| 2250 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 2251 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
| 2252 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
| 2253 | assert(funcType && "missing function type"); |
| 2254 | proto = dyn_cast<FunctionProtoType>(funcType); |
| 2255 | if (!proto) |
| 2256 | return; |
| 2257 | Type = proto->getResultType(); |
| 2258 | } |
| 2259 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(Dcl)) { |
| 2260 | Loc = FD->getLocation(); |
| 2261 | Type = FD->getType(); |
| 2262 | } |
| 2263 | else |
| 2264 | return; |
| 2265 | |
| 2266 | if (needToScanForQualifiers(Type)) { |
| 2267 | // Since types are unique, we need to scan the buffer. |
| 2268 | |
| 2269 | const char *endBuf = SM->getCharacterData(Loc); |
| 2270 | const char *startBuf = endBuf; |
| 2271 | while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) |
| 2272 | startBuf--; // scan backward (from the decl location) for return type. |
| 2273 | const char *startRef = 0, *endRef = 0; |
| 2274 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2275 | // Get the locations of the startRef, endRef. |
| 2276 | SourceLocation LessLoc = Loc.getLocWithOffset(startRef-endBuf); |
| 2277 | SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-endBuf+1); |
| 2278 | // Comment out the protocol references. |
| 2279 | InsertText(LessLoc, "/*"); |
| 2280 | InsertText(GreaterLoc, "*/"); |
| 2281 | } |
| 2282 | } |
| 2283 | if (!proto) |
| 2284 | return; // most likely, was a variable |
| 2285 | // Now check arguments. |
| 2286 | const char *startBuf = SM->getCharacterData(Loc); |
| 2287 | const char *startFuncBuf = startBuf; |
| 2288 | for (unsigned i = 0; i < proto->getNumArgs(); i++) { |
| 2289 | if (needToScanForQualifiers(proto->getArgType(i))) { |
| 2290 | // Since types are unique, we need to scan the buffer. |
| 2291 | |
| 2292 | const char *endBuf = startBuf; |
| 2293 | // scan forward (from the decl location) for argument types. |
| 2294 | scanToNextArgument(endBuf); |
| 2295 | const char *startRef = 0, *endRef = 0; |
| 2296 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2297 | // Get the locations of the startRef, endRef. |
| 2298 | SourceLocation LessLoc = |
| 2299 | Loc.getLocWithOffset(startRef-startFuncBuf); |
| 2300 | SourceLocation GreaterLoc = |
| 2301 | Loc.getLocWithOffset(endRef-startFuncBuf+1); |
| 2302 | // Comment out the protocol references. |
| 2303 | InsertText(LessLoc, "/*"); |
| 2304 | InsertText(GreaterLoc, "*/"); |
| 2305 | } |
| 2306 | startBuf = ++endBuf; |
| 2307 | } |
| 2308 | else { |
| 2309 | // If the function name is derived from a macro expansion, then the |
| 2310 | // argument buffer will not follow the name. Need to speak with Chris. |
| 2311 | while (*startBuf && *startBuf != ')' && *startBuf != ',') |
| 2312 | startBuf++; // scan forward (from the decl location) for argument types. |
| 2313 | startBuf++; |
| 2314 | } |
| 2315 | } |
| 2316 | } |
| 2317 | |
| 2318 | void RewriteModernObjC::RewriteTypeOfDecl(VarDecl *ND) { |
| 2319 | QualType QT = ND->getType(); |
| 2320 | const Type* TypePtr = QT->getAs<Type>(); |
| 2321 | if (!isa<TypeOfExprType>(TypePtr)) |
| 2322 | return; |
| 2323 | while (isa<TypeOfExprType>(TypePtr)) { |
| 2324 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 2325 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 2326 | TypePtr = QT->getAs<Type>(); |
| 2327 | } |
| 2328 | // FIXME. This will not work for multiple declarators; as in: |
| 2329 | // __typeof__(a) b,c,d; |
| 2330 | std::string TypeAsString(QT.getAsString(Context->getPrintingPolicy())); |
| 2331 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
| 2332 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 2333 | if (ND->getInit()) { |
| 2334 | std::string Name(ND->getNameAsString()); |
| 2335 | TypeAsString += " " + Name + " = "; |
| 2336 | Expr *E = ND->getInit(); |
| 2337 | SourceLocation startLoc; |
| 2338 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 2339 | startLoc = ECE->getLParenLoc(); |
| 2340 | else |
| 2341 | startLoc = E->getLocStart(); |
| 2342 | startLoc = SM->getExpansionLoc(startLoc); |
| 2343 | const char *endBuf = SM->getCharacterData(startLoc); |
| 2344 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
| 2345 | } |
| 2346 | else { |
| 2347 | SourceLocation X = ND->getLocEnd(); |
| 2348 | X = SM->getExpansionLoc(X); |
| 2349 | const char *endBuf = SM->getCharacterData(X); |
| 2350 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
| 2351 | } |
| 2352 | } |
| 2353 | |
| 2354 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
| 2355 | void RewriteModernObjC::SynthSelGetUidFunctionDecl() { |
| 2356 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
| 2357 | SmallVector<QualType, 16> ArgTys; |
| 2358 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
| 2359 | QualType getFuncType = |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame^] | 2360 | getSimpleFunctionType(Context->getObjCSelType(), ArgTys); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2361 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | e3b2988 | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2362 | SourceLocation(), |
| 2363 | SourceLocation(), |
| 2364 | SelGetUidIdent, getFuncType, 0, |
| 2365 | SC_Extern, SC_None); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2366 | } |
| 2367 | |
| 2368 | void RewriteModernObjC::RewriteFunctionDecl(FunctionDecl *FD) { |
| 2369 | // declared in <objc/objc.h> |
| 2370 | if (FD->getIdentifier() && |
| 2371 | FD->getName() == "sel_registerName") { |
| 2372 | SelGetUidFunctionDecl = FD; |
| 2373 | return; |
| 2374 | } |
| 2375 | RewriteObjCQualifiedInterfaceTypes(FD); |
| 2376 | } |
| 2377 | |
| 2378 | void RewriteModernObjC::RewriteBlockPointerType(std::string& Str, QualType Type) { |
| 2379 | std::string TypeString(Type.getAsString(Context->getPrintingPolicy())); |
| 2380 | const char *argPtr = TypeString.c_str(); |
| 2381 | if (!strchr(argPtr, '^')) { |
| 2382 | Str += TypeString; |
| 2383 | return; |
| 2384 | } |
| 2385 | while (*argPtr) { |
| 2386 | Str += (*argPtr == '^' ? '*' : *argPtr); |
| 2387 | argPtr++; |
| 2388 | } |
| 2389 | } |
| 2390 | |
| 2391 | // FIXME. Consolidate this routine with RewriteBlockPointerType. |
| 2392 | void RewriteModernObjC::RewriteBlockPointerTypeVariable(std::string& Str, |
| 2393 | ValueDecl *VD) { |
| 2394 | QualType Type = VD->getType(); |
| 2395 | std::string TypeString(Type.getAsString(Context->getPrintingPolicy())); |
| 2396 | const char *argPtr = TypeString.c_str(); |
| 2397 | int paren = 0; |
| 2398 | while (*argPtr) { |
| 2399 | switch (*argPtr) { |
| 2400 | case '(': |
| 2401 | Str += *argPtr; |
| 2402 | paren++; |
| 2403 | break; |
| 2404 | case ')': |
| 2405 | Str += *argPtr; |
| 2406 | paren--; |
| 2407 | break; |
| 2408 | case '^': |
| 2409 | Str += '*'; |
| 2410 | if (paren == 1) |
| 2411 | Str += VD->getNameAsString(); |
| 2412 | break; |
| 2413 | default: |
| 2414 | Str += *argPtr; |
| 2415 | break; |
| 2416 | } |
| 2417 | argPtr++; |
| 2418 | } |
| 2419 | } |
| 2420 | |
Fariborz Jahanian | b75f8de | 2012-04-19 00:50:01 +0000 | [diff] [blame] | 2421 | void RewriteModernObjC::RewriteBlockLiteralFunctionDecl(FunctionDecl *FD) { |
| 2422 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
| 2423 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
| 2424 | const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(funcType); |
| 2425 | if (!proto) |
| 2426 | return; |
| 2427 | QualType Type = proto->getResultType(); |
| 2428 | std::string FdStr = Type.getAsString(Context->getPrintingPolicy()); |
| 2429 | FdStr += " "; |
| 2430 | FdStr += FD->getName(); |
| 2431 | FdStr += "("; |
| 2432 | unsigned numArgs = proto->getNumArgs(); |
| 2433 | for (unsigned i = 0; i < numArgs; i++) { |
| 2434 | QualType ArgType = proto->getArgType(i); |
| 2435 | RewriteBlockPointerType(FdStr, ArgType); |
| 2436 | if (i+1 < numArgs) |
| 2437 | FdStr += ", "; |
| 2438 | } |
Fariborz Jahanian | b5863da | 2012-04-19 16:30:28 +0000 | [diff] [blame] | 2439 | if (FD->isVariadic()) { |
| 2440 | FdStr += (numArgs > 0) ? ", ...);\n" : "...);\n"; |
| 2441 | } |
| 2442 | else |
| 2443 | FdStr += ");\n"; |
Fariborz Jahanian | b75f8de | 2012-04-19 00:50:01 +0000 | [diff] [blame] | 2444 | InsertText(FunLocStart, FdStr); |
| 2445 | } |
| 2446 | |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 2447 | // SynthSuperContructorFunctionDecl - id __rw_objc_super(id obj, id super); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2448 | void RewriteModernObjC::SynthSuperContructorFunctionDecl() { |
| 2449 | if (SuperContructorFunctionDecl) |
| 2450 | return; |
| 2451 | IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super"); |
| 2452 | SmallVector<QualType, 16> ArgTys; |
| 2453 | QualType argT = Context->getObjCIdType(); |
| 2454 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2455 | ArgTys.push_back(argT); |
| 2456 | ArgTys.push_back(argT); |
| 2457 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame^] | 2458 | ArgTys); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2459 | SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | e3b2988 | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2460 | SourceLocation(), |
| 2461 | SourceLocation(), |
| 2462 | msgSendIdent, msgSendType, |
| 2463 | 0, SC_Extern, SC_None); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2464 | } |
| 2465 | |
| 2466 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
| 2467 | void RewriteModernObjC::SynthMsgSendFunctionDecl() { |
| 2468 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
| 2469 | SmallVector<QualType, 16> ArgTys; |
| 2470 | QualType argT = Context->getObjCIdType(); |
| 2471 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2472 | ArgTys.push_back(argT); |
| 2473 | argT = Context->getObjCSelType(); |
| 2474 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2475 | ArgTys.push_back(argT); |
| 2476 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame^] | 2477 | ArgTys, /*isVariadic=*/true); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2478 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | e3b2988 | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2479 | SourceLocation(), |
| 2480 | SourceLocation(), |
| 2481 | msgSendIdent, msgSendType, 0, |
| 2482 | SC_Extern, SC_None); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2483 | } |
| 2484 | |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 2485 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(void); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2486 | void RewriteModernObjC::SynthMsgSendSuperFunctionDecl() { |
| 2487 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 2488 | SmallVector<QualType, 2> ArgTys; |
| 2489 | ArgTys.push_back(Context->VoidTy); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2490 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame^] | 2491 | ArgTys, /*isVariadic=*/true); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2492 | MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | e3b2988 | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2493 | SourceLocation(), |
| 2494 | SourceLocation(), |
| 2495 | msgSendIdent, msgSendType, 0, |
| 2496 | SC_Extern, SC_None); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2497 | } |
| 2498 | |
| 2499 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
| 2500 | void RewriteModernObjC::SynthMsgSendStretFunctionDecl() { |
| 2501 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
| 2502 | SmallVector<QualType, 16> ArgTys; |
| 2503 | QualType argT = Context->getObjCIdType(); |
| 2504 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2505 | ArgTys.push_back(argT); |
| 2506 | argT = Context->getObjCSelType(); |
| 2507 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2508 | ArgTys.push_back(argT); |
| 2509 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame^] | 2510 | ArgTys, /*isVariadic=*/true); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2511 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | e3b2988 | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2512 | SourceLocation(), |
| 2513 | SourceLocation(), |
| 2514 | msgSendIdent, msgSendType, 0, |
| 2515 | SC_Extern, SC_None); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2516 | } |
| 2517 | |
| 2518 | // SynthMsgSendSuperStretFunctionDecl - |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 2519 | // id objc_msgSendSuper_stret(void); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2520 | void RewriteModernObjC::SynthMsgSendSuperStretFunctionDecl() { |
| 2521 | IdentifierInfo *msgSendIdent = |
| 2522 | &Context->Idents.get("objc_msgSendSuper_stret"); |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 2523 | SmallVector<QualType, 2> ArgTys; |
| 2524 | ArgTys.push_back(Context->VoidTy); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2525 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame^] | 2526 | ArgTys, /*isVariadic=*/true); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2527 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
| 2528 | SourceLocation(), |
| 2529 | SourceLocation(), |
Chad Rosier | e3b2988 | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2530 | msgSendIdent, |
| 2531 | msgSendType, 0, |
| 2532 | SC_Extern, SC_None); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2533 | } |
| 2534 | |
| 2535 | // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); |
| 2536 | void RewriteModernObjC::SynthMsgSendFpretFunctionDecl() { |
| 2537 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
| 2538 | SmallVector<QualType, 16> ArgTys; |
| 2539 | QualType argT = Context->getObjCIdType(); |
| 2540 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2541 | ArgTys.push_back(argT); |
| 2542 | argT = Context->getObjCSelType(); |
| 2543 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2544 | ArgTys.push_back(argT); |
| 2545 | QualType msgSendType = getSimpleFunctionType(Context->DoubleTy, |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame^] | 2546 | ArgTys, /*isVariadic=*/true); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2547 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | e3b2988 | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2548 | SourceLocation(), |
| 2549 | SourceLocation(), |
| 2550 | msgSendIdent, msgSendType, 0, |
| 2551 | SC_Extern, SC_None); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2552 | } |
| 2553 | |
Fariborz Jahanian | 20e181a | 2012-05-08 20:55:55 +0000 | [diff] [blame] | 2554 | // SynthGetClassFunctionDecl - Class objc_getClass(const char *name); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2555 | void RewriteModernObjC::SynthGetClassFunctionDecl() { |
| 2556 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
| 2557 | SmallVector<QualType, 16> ArgTys; |
| 2558 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
Fariborz Jahanian | 20e181a | 2012-05-08 20:55:55 +0000 | [diff] [blame] | 2559 | QualType getClassType = getSimpleFunctionType(Context->getObjCClassType(), |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame^] | 2560 | ArgTys); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2561 | GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | e3b2988 | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2562 | SourceLocation(), |
| 2563 | SourceLocation(), |
| 2564 | getClassIdent, getClassType, 0, |
| 2565 | SC_Extern, SC_None); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2566 | } |
| 2567 | |
| 2568 | // SynthGetSuperClassFunctionDecl - Class class_getSuperclass(Class cls); |
| 2569 | void RewriteModernObjC::SynthGetSuperClassFunctionDecl() { |
| 2570 | IdentifierInfo *getSuperClassIdent = |
| 2571 | &Context->Idents.get("class_getSuperclass"); |
| 2572 | SmallVector<QualType, 16> ArgTys; |
| 2573 | ArgTys.push_back(Context->getObjCClassType()); |
| 2574 | QualType getClassType = getSimpleFunctionType(Context->getObjCClassType(), |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame^] | 2575 | ArgTys); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2576 | GetSuperClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
| 2577 | SourceLocation(), |
| 2578 | SourceLocation(), |
| 2579 | getSuperClassIdent, |
| 2580 | getClassType, 0, |
Chad Rosier | e3b2988 | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2581 | SC_Extern, SC_None); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2582 | } |
| 2583 | |
Fariborz Jahanian | 20e181a | 2012-05-08 20:55:55 +0000 | [diff] [blame] | 2584 | // SynthGetMetaClassFunctionDecl - Class objc_getMetaClass(const char *name); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2585 | void RewriteModernObjC::SynthGetMetaClassFunctionDecl() { |
| 2586 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
| 2587 | SmallVector<QualType, 16> ArgTys; |
| 2588 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
Fariborz Jahanian | 20e181a | 2012-05-08 20:55:55 +0000 | [diff] [blame] | 2589 | QualType getClassType = getSimpleFunctionType(Context->getObjCClassType(), |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame^] | 2590 | ArgTys); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2591 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chad Rosier | e3b2988 | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 2592 | SourceLocation(), |
| 2593 | SourceLocation(), |
| 2594 | getClassIdent, getClassType, |
| 2595 | 0, SC_Extern, SC_None); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2596 | } |
| 2597 | |
| 2598 | Stmt *RewriteModernObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
| 2599 | QualType strType = getConstantStringStructType(); |
| 2600 | |
| 2601 | std::string S = "__NSConstantStringImpl_"; |
| 2602 | |
| 2603 | std::string tmpName = InFileName; |
| 2604 | unsigned i; |
| 2605 | for (i=0; i < tmpName.length(); i++) { |
| 2606 | char c = tmpName.at(i); |
| 2607 | // replace any non alphanumeric characters with '_'. |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 2608 | if (!isAlphanumeric(c)) |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2609 | tmpName[i] = '_'; |
| 2610 | } |
| 2611 | S += tmpName; |
| 2612 | S += "_"; |
| 2613 | S += utostr(NumObjCStringLiterals++); |
| 2614 | |
| 2615 | Preamble += "static __NSConstantStringImpl " + S; |
| 2616 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 2617 | Preamble += "0x000007c8,"; // utf8_str |
| 2618 | // The pretty printer for StringLiteral handles escape characters properly. |
| 2619 | std::string prettyBufS; |
| 2620 | llvm::raw_string_ostream prettyBuf(prettyBufS); |
Richard Smith | d1420c6 | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 2621 | Exp->getString()->printPretty(prettyBuf, 0, PrintingPolicy(LangOpts)); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2622 | Preamble += prettyBuf.str(); |
| 2623 | Preamble += ","; |
| 2624 | Preamble += utostr(Exp->getString()->getByteLength()) + "};\n"; |
| 2625 | |
| 2626 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
| 2627 | SourceLocation(), &Context->Idents.get(S), |
| 2628 | strType, 0, SC_Static, SC_None); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2629 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, false, strType, VK_LValue, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2630 | SourceLocation()); |
| 2631 | Expr *Unop = new (Context) UnaryOperator(DRE, UO_AddrOf, |
| 2632 | Context->getPointerType(DRE->getType()), |
| 2633 | VK_RValue, OK_Ordinary, |
| 2634 | SourceLocation()); |
| 2635 | // cast to NSConstantString * |
| 2636 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Exp->getType(), |
| 2637 | CK_CPointerToObjCPointerCast, Unop); |
| 2638 | ReplaceStmt(Exp, cast); |
| 2639 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
| 2640 | return cast; |
| 2641 | } |
| 2642 | |
Fariborz Jahanian | 5594704 | 2012-03-27 20:17:30 +0000 | [diff] [blame] | 2643 | Stmt *RewriteModernObjC::RewriteObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Exp) { |
| 2644 | unsigned IntSize = |
| 2645 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 2646 | |
| 2647 | Expr *FlagExp = IntegerLiteral::Create(*Context, |
| 2648 | llvm::APInt(IntSize, Exp->getValue()), |
| 2649 | Context->IntTy, Exp->getLocation()); |
| 2650 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Context->ObjCBuiltinBoolTy, |
| 2651 | CK_BitCast, FlagExp); |
| 2652 | ParenExpr *PE = new (Context) ParenExpr(Exp->getLocation(), Exp->getExprLoc(), |
| 2653 | cast); |
| 2654 | ReplaceStmt(Exp, PE); |
| 2655 | return PE; |
| 2656 | } |
| 2657 | |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2658 | Stmt *RewriteModernObjC::RewriteObjCBoxedExpr(ObjCBoxedExpr *Exp) { |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2659 | // synthesize declaration of helper functions needed in this routine. |
| 2660 | if (!SelGetUidFunctionDecl) |
| 2661 | SynthSelGetUidFunctionDecl(); |
| 2662 | // use objc_msgSend() for all. |
| 2663 | if (!MsgSendFunctionDecl) |
| 2664 | SynthMsgSendFunctionDecl(); |
| 2665 | if (!GetClassFunctionDecl) |
| 2666 | SynthGetClassFunctionDecl(); |
| 2667 | |
| 2668 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2669 | SourceLocation StartLoc = Exp->getLocStart(); |
| 2670 | SourceLocation EndLoc = Exp->getLocEnd(); |
| 2671 | |
| 2672 | // Synthesize a call to objc_msgSend(). |
| 2673 | SmallVector<Expr*, 4> MsgExprs; |
| 2674 | SmallVector<Expr*, 4> ClsExprs; |
| 2675 | QualType argType = Context->getPointerType(Context->CharTy); |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2676 | |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2677 | // Create a call to objc_getClass("<BoxingClass>"). It will be the 1st argument. |
| 2678 | ObjCMethodDecl *BoxingMethod = Exp->getBoxingMethod(); |
| 2679 | ObjCInterfaceDecl *BoxingClass = BoxingMethod->getClassInterface(); |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2680 | |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2681 | IdentifierInfo *clsName = BoxingClass->getIdentifier(); |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2682 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 2683 | clsName->getName(), |
| 2684 | StringLiteral::Ascii, false, |
| 2685 | argType, SourceLocation())); |
| 2686 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2687 | &ClsExprs[0], |
| 2688 | ClsExprs.size(), |
| 2689 | StartLoc, EndLoc); |
| 2690 | MsgExprs.push_back(Cls); |
| 2691 | |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2692 | // Create a call to sel_registerName("<BoxingMethod>:"), etc. |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2693 | // it will be the 2nd argument. |
| 2694 | SmallVector<Expr*, 4> SelExprs; |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2695 | SelExprs.push_back(StringLiteral::Create(*Context, |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2696 | BoxingMethod->getSelector().getAsString(), |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2697 | StringLiteral::Ascii, false, |
| 2698 | argType, SourceLocation())); |
| 2699 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2700 | &SelExprs[0], SelExprs.size(), |
| 2701 | StartLoc, EndLoc); |
| 2702 | MsgExprs.push_back(SelExp); |
| 2703 | |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2704 | // User provided sub-expression is the 3rd, and last, argument. |
| 2705 | Expr *subExpr = Exp->getSubExpr(); |
| 2706 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(subExpr)) { |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2707 | QualType type = ICE->getType(); |
| 2708 | const Expr *SubExpr = ICE->IgnoreParenImpCasts(); |
| 2709 | CastKind CK = CK_BitCast; |
| 2710 | if (SubExpr->getType()->isIntegralType(*Context) && type->isBooleanType()) |
| 2711 | CK = CK_IntegralToBoolean; |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2712 | subExpr = NoTypeInfoCStyleCastExpr(Context, type, CK, subExpr); |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2713 | } |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2714 | MsgExprs.push_back(subExpr); |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2715 | |
| 2716 | SmallVector<QualType, 4> ArgTypes; |
| 2717 | ArgTypes.push_back(Context->getObjCIdType()); |
| 2718 | ArgTypes.push_back(Context->getObjCSelType()); |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2719 | for (ObjCMethodDecl::param_iterator PI = BoxingMethod->param_begin(), |
| 2720 | E = BoxingMethod->param_end(); PI != E; ++PI) |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2721 | ArgTypes.push_back((*PI)->getType()); |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2722 | |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2723 | QualType returnType = Exp->getType(); |
| 2724 | // Get the type, we will need to reference it in a couple spots. |
| 2725 | QualType msgSendType = MsgSendFlavor->getType(); |
| 2726 | |
| 2727 | // Create a reference to the objc_msgSend() declaration. |
| 2728 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, false, msgSendType, |
| 2729 | VK_LValue, SourceLocation()); |
| 2730 | |
| 2731 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2732 | Context->getPointerType(Context->VoidTy), |
| 2733 | CK_BitCast, DRE); |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2734 | |
| 2735 | // Now do the "normal" pointer to function cast. |
| 2736 | QualType castType = |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame^] | 2737 | getSimpleFunctionType(returnType, ArgTypes, BoxingMethod->isVariadic()); |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2738 | castType = Context->getPointerType(castType); |
| 2739 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
| 2740 | cast); |
| 2741 | |
| 2742 | // Don't forget the parens to enforce the proper binding. |
| 2743 | ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast); |
| 2744 | |
| 2745 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2746 | CallExpr *CE = new (Context) CallExpr(*Context, PE, MsgExprs, |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2747 | FT->getResultType(), VK_RValue, |
| 2748 | EndLoc); |
| 2749 | ReplaceStmt(Exp, CE); |
| 2750 | return CE; |
| 2751 | } |
| 2752 | |
Fariborz Jahanian | 86cff60 | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2753 | Stmt *RewriteModernObjC::RewriteObjCArrayLiteralExpr(ObjCArrayLiteral *Exp) { |
| 2754 | // synthesize declaration of helper functions needed in this routine. |
| 2755 | if (!SelGetUidFunctionDecl) |
| 2756 | SynthSelGetUidFunctionDecl(); |
| 2757 | // use objc_msgSend() for all. |
| 2758 | if (!MsgSendFunctionDecl) |
| 2759 | SynthMsgSendFunctionDecl(); |
| 2760 | if (!GetClassFunctionDecl) |
| 2761 | SynthGetClassFunctionDecl(); |
| 2762 | |
| 2763 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2764 | SourceLocation StartLoc = Exp->getLocStart(); |
| 2765 | SourceLocation EndLoc = Exp->getLocEnd(); |
| 2766 | |
Fariborz Jahanian | e35abe1 | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2767 | // Build the expression: __NSContainer_literal(int, ...).arr |
Fariborz Jahanian | b0f245c | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 2768 | QualType IntQT = Context->IntTy; |
| 2769 | QualType NSArrayFType = |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame^] | 2770 | getSimpleFunctionType(Context->VoidTy, IntQT, true); |
Fariborz Jahanian | e35abe1 | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2771 | std::string NSArrayFName("__NSContainer_literal"); |
Fariborz Jahanian | b0f245c | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 2772 | FunctionDecl *NSArrayFD = SynthBlockInitFunctionDecl(NSArrayFName); |
| 2773 | DeclRefExpr *NSArrayDRE = |
| 2774 | new (Context) DeclRefExpr(NSArrayFD, false, NSArrayFType, VK_RValue, |
| 2775 | SourceLocation()); |
| 2776 | |
| 2777 | SmallVector<Expr*, 16> InitExprs; |
| 2778 | unsigned NumElements = Exp->getNumElements(); |
| 2779 | unsigned UnsignedIntSize = |
| 2780 | static_cast<unsigned>(Context->getTypeSize(Context->UnsignedIntTy)); |
| 2781 | Expr *count = IntegerLiteral::Create(*Context, |
| 2782 | llvm::APInt(UnsignedIntSize, NumElements), |
| 2783 | Context->UnsignedIntTy, SourceLocation()); |
| 2784 | InitExprs.push_back(count); |
| 2785 | for (unsigned i = 0; i < NumElements; i++) |
| 2786 | InitExprs.push_back(Exp->getElement(i)); |
| 2787 | Expr *NSArrayCallExpr = |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2788 | new (Context) CallExpr(*Context, NSArrayDRE, InitExprs, |
Fariborz Jahanian | b0f245c | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 2789 | NSArrayFType, VK_LValue, SourceLocation()); |
| 2790 | |
| 2791 | FieldDecl *ARRFD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 2792 | SourceLocation(), |
| 2793 | &Context->Idents.get("arr"), |
| 2794 | Context->getPointerType(Context->VoidPtrTy), 0, |
| 2795 | /*BitWidth=*/0, /*Mutable=*/true, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2796 | ICIS_NoInit); |
Fariborz Jahanian | b0f245c | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 2797 | MemberExpr *ArrayLiteralME = |
| 2798 | new (Context) MemberExpr(NSArrayCallExpr, false, ARRFD, |
| 2799 | SourceLocation(), |
| 2800 | ARRFD->getType(), VK_LValue, |
| 2801 | OK_Ordinary); |
| 2802 | QualType ConstIdT = Context->getObjCIdType().withConst(); |
| 2803 | CStyleCastExpr * ArrayLiteralObjects = |
| 2804 | NoTypeInfoCStyleCastExpr(Context, |
| 2805 | Context->getPointerType(ConstIdT), |
| 2806 | CK_BitCast, |
| 2807 | ArrayLiteralME); |
| 2808 | |
Fariborz Jahanian | 86cff60 | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2809 | // Synthesize a call to objc_msgSend(). |
| 2810 | SmallVector<Expr*, 32> MsgExprs; |
| 2811 | SmallVector<Expr*, 4> ClsExprs; |
| 2812 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2813 | QualType expType = Exp->getType(); |
| 2814 | |
| 2815 | // Create a call to objc_getClass("NSArray"). It will be th 1st argument. |
| 2816 | ObjCInterfaceDecl *Class = |
| 2817 | expType->getPointeeType()->getAs<ObjCObjectType>()->getInterface(); |
| 2818 | |
| 2819 | IdentifierInfo *clsName = Class->getIdentifier(); |
| 2820 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 2821 | clsName->getName(), |
| 2822 | StringLiteral::Ascii, false, |
| 2823 | argType, SourceLocation())); |
| 2824 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2825 | &ClsExprs[0], |
| 2826 | ClsExprs.size(), |
| 2827 | StartLoc, EndLoc); |
| 2828 | MsgExprs.push_back(Cls); |
| 2829 | |
| 2830 | // Create a call to sel_registerName("arrayWithObjects:count:"). |
| 2831 | // it will be the 2nd argument. |
| 2832 | SmallVector<Expr*, 4> SelExprs; |
| 2833 | ObjCMethodDecl *ArrayMethod = Exp->getArrayWithObjectsMethod(); |
| 2834 | SelExprs.push_back(StringLiteral::Create(*Context, |
| 2835 | ArrayMethod->getSelector().getAsString(), |
| 2836 | StringLiteral::Ascii, false, |
| 2837 | argType, SourceLocation())); |
| 2838 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2839 | &SelExprs[0], SelExprs.size(), |
| 2840 | StartLoc, EndLoc); |
| 2841 | MsgExprs.push_back(SelExp); |
| 2842 | |
Fariborz Jahanian | b0f245c | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 2843 | // (const id [])objects |
| 2844 | MsgExprs.push_back(ArrayLiteralObjects); |
Fariborz Jahanian | 86cff60 | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2845 | |
Fariborz Jahanian | b0f245c | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 2846 | // (NSUInteger)cnt |
| 2847 | Expr *cnt = IntegerLiteral::Create(*Context, |
| 2848 | llvm::APInt(UnsignedIntSize, NumElements), |
| 2849 | Context->UnsignedIntTy, SourceLocation()); |
| 2850 | MsgExprs.push_back(cnt); |
Fariborz Jahanian | 86cff60 | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2851 | |
| 2852 | |
| 2853 | SmallVector<QualType, 4> ArgTypes; |
| 2854 | ArgTypes.push_back(Context->getObjCIdType()); |
| 2855 | ArgTypes.push_back(Context->getObjCSelType()); |
| 2856 | for (ObjCMethodDecl::param_iterator PI = ArrayMethod->param_begin(), |
| 2857 | E = ArrayMethod->param_end(); PI != E; ++PI) |
| 2858 | ArgTypes.push_back((*PI)->getType()); |
| 2859 | |
| 2860 | QualType returnType = Exp->getType(); |
| 2861 | // Get the type, we will need to reference it in a couple spots. |
| 2862 | QualType msgSendType = MsgSendFlavor->getType(); |
| 2863 | |
| 2864 | // Create a reference to the objc_msgSend() declaration. |
| 2865 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, false, msgSendType, |
| 2866 | VK_LValue, SourceLocation()); |
| 2867 | |
| 2868 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, |
| 2869 | Context->getPointerType(Context->VoidTy), |
| 2870 | CK_BitCast, DRE); |
| 2871 | |
| 2872 | // Now do the "normal" pointer to function cast. |
| 2873 | QualType castType = |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame^] | 2874 | getSimpleFunctionType(returnType, ArgTypes, ArrayMethod->isVariadic()); |
Fariborz Jahanian | 86cff60 | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2875 | castType = Context->getPointerType(castType); |
| 2876 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
| 2877 | cast); |
| 2878 | |
| 2879 | // Don't forget the parens to enforce the proper binding. |
| 2880 | ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast); |
| 2881 | |
| 2882 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2883 | CallExpr *CE = new (Context) CallExpr(*Context, PE, MsgExprs, |
Fariborz Jahanian | 86cff60 | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2884 | FT->getResultType(), VK_RValue, |
| 2885 | EndLoc); |
| 2886 | ReplaceStmt(Exp, CE); |
| 2887 | return CE; |
| 2888 | } |
| 2889 | |
Fariborz Jahanian | e35abe1 | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2890 | Stmt *RewriteModernObjC::RewriteObjCDictionaryLiteralExpr(ObjCDictionaryLiteral *Exp) { |
| 2891 | // synthesize declaration of helper functions needed in this routine. |
| 2892 | if (!SelGetUidFunctionDecl) |
| 2893 | SynthSelGetUidFunctionDecl(); |
| 2894 | // use objc_msgSend() for all. |
| 2895 | if (!MsgSendFunctionDecl) |
| 2896 | SynthMsgSendFunctionDecl(); |
| 2897 | if (!GetClassFunctionDecl) |
| 2898 | SynthGetClassFunctionDecl(); |
| 2899 | |
| 2900 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2901 | SourceLocation StartLoc = Exp->getLocStart(); |
| 2902 | SourceLocation EndLoc = Exp->getLocEnd(); |
| 2903 | |
| 2904 | // Build the expression: __NSContainer_literal(int, ...).arr |
| 2905 | QualType IntQT = Context->IntTy; |
| 2906 | QualType NSDictFType = |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame^] | 2907 | getSimpleFunctionType(Context->VoidTy, IntQT, true); |
Fariborz Jahanian | e35abe1 | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2908 | std::string NSDictFName("__NSContainer_literal"); |
| 2909 | FunctionDecl *NSDictFD = SynthBlockInitFunctionDecl(NSDictFName); |
| 2910 | DeclRefExpr *NSDictDRE = |
| 2911 | new (Context) DeclRefExpr(NSDictFD, false, NSDictFType, VK_RValue, |
| 2912 | SourceLocation()); |
| 2913 | |
| 2914 | SmallVector<Expr*, 16> KeyExprs; |
| 2915 | SmallVector<Expr*, 16> ValueExprs; |
| 2916 | |
| 2917 | unsigned NumElements = Exp->getNumElements(); |
| 2918 | unsigned UnsignedIntSize = |
| 2919 | static_cast<unsigned>(Context->getTypeSize(Context->UnsignedIntTy)); |
| 2920 | Expr *count = IntegerLiteral::Create(*Context, |
| 2921 | llvm::APInt(UnsignedIntSize, NumElements), |
| 2922 | Context->UnsignedIntTy, SourceLocation()); |
| 2923 | KeyExprs.push_back(count); |
| 2924 | ValueExprs.push_back(count); |
| 2925 | for (unsigned i = 0; i < NumElements; i++) { |
| 2926 | ObjCDictionaryElement Element = Exp->getKeyValueElement(i); |
| 2927 | KeyExprs.push_back(Element.Key); |
| 2928 | ValueExprs.push_back(Element.Value); |
| 2929 | } |
| 2930 | |
| 2931 | // (const id [])objects |
| 2932 | Expr *NSValueCallExpr = |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2933 | new (Context) CallExpr(*Context, NSDictDRE, ValueExprs, |
Fariborz Jahanian | e35abe1 | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2934 | NSDictFType, VK_LValue, SourceLocation()); |
| 2935 | |
| 2936 | FieldDecl *ARRFD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 2937 | SourceLocation(), |
| 2938 | &Context->Idents.get("arr"), |
| 2939 | Context->getPointerType(Context->VoidPtrTy), 0, |
| 2940 | /*BitWidth=*/0, /*Mutable=*/true, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2941 | ICIS_NoInit); |
Fariborz Jahanian | e35abe1 | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2942 | MemberExpr *DictLiteralValueME = |
| 2943 | new (Context) MemberExpr(NSValueCallExpr, false, ARRFD, |
| 2944 | SourceLocation(), |
| 2945 | ARRFD->getType(), VK_LValue, |
| 2946 | OK_Ordinary); |
| 2947 | QualType ConstIdT = Context->getObjCIdType().withConst(); |
| 2948 | CStyleCastExpr * DictValueObjects = |
| 2949 | NoTypeInfoCStyleCastExpr(Context, |
| 2950 | Context->getPointerType(ConstIdT), |
| 2951 | CK_BitCast, |
| 2952 | DictLiteralValueME); |
| 2953 | // (const id <NSCopying> [])keys |
| 2954 | Expr *NSKeyCallExpr = |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2955 | new (Context) CallExpr(*Context, NSDictDRE, KeyExprs, |
Fariborz Jahanian | e35abe1 | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2956 | NSDictFType, VK_LValue, SourceLocation()); |
| 2957 | |
| 2958 | MemberExpr *DictLiteralKeyME = |
| 2959 | new (Context) MemberExpr(NSKeyCallExpr, false, ARRFD, |
| 2960 | SourceLocation(), |
| 2961 | ARRFD->getType(), VK_LValue, |
| 2962 | OK_Ordinary); |
| 2963 | |
| 2964 | CStyleCastExpr * DictKeyObjects = |
| 2965 | NoTypeInfoCStyleCastExpr(Context, |
| 2966 | Context->getPointerType(ConstIdT), |
| 2967 | CK_BitCast, |
| 2968 | DictLiteralKeyME); |
| 2969 | |
| 2970 | |
| 2971 | |
| 2972 | // Synthesize a call to objc_msgSend(). |
| 2973 | SmallVector<Expr*, 32> MsgExprs; |
| 2974 | SmallVector<Expr*, 4> ClsExprs; |
| 2975 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2976 | QualType expType = Exp->getType(); |
| 2977 | |
| 2978 | // Create a call to objc_getClass("NSArray"). It will be th 1st argument. |
| 2979 | ObjCInterfaceDecl *Class = |
| 2980 | expType->getPointeeType()->getAs<ObjCObjectType>()->getInterface(); |
| 2981 | |
| 2982 | IdentifierInfo *clsName = Class->getIdentifier(); |
| 2983 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 2984 | clsName->getName(), |
| 2985 | StringLiteral::Ascii, false, |
| 2986 | argType, SourceLocation())); |
| 2987 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2988 | &ClsExprs[0], |
| 2989 | ClsExprs.size(), |
| 2990 | StartLoc, EndLoc); |
| 2991 | MsgExprs.push_back(Cls); |
| 2992 | |
| 2993 | // Create a call to sel_registerName("arrayWithObjects:count:"). |
| 2994 | // it will be the 2nd argument. |
| 2995 | SmallVector<Expr*, 4> SelExprs; |
| 2996 | ObjCMethodDecl *DictMethod = Exp->getDictWithObjectsMethod(); |
| 2997 | SelExprs.push_back(StringLiteral::Create(*Context, |
| 2998 | DictMethod->getSelector().getAsString(), |
| 2999 | StringLiteral::Ascii, false, |
| 3000 | argType, SourceLocation())); |
| 3001 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 3002 | &SelExprs[0], SelExprs.size(), |
| 3003 | StartLoc, EndLoc); |
| 3004 | MsgExprs.push_back(SelExp); |
| 3005 | |
| 3006 | // (const id [])objects |
| 3007 | MsgExprs.push_back(DictValueObjects); |
| 3008 | |
| 3009 | // (const id <NSCopying> [])keys |
| 3010 | MsgExprs.push_back(DictKeyObjects); |
| 3011 | |
| 3012 | // (NSUInteger)cnt |
| 3013 | Expr *cnt = IntegerLiteral::Create(*Context, |
| 3014 | llvm::APInt(UnsignedIntSize, NumElements), |
| 3015 | Context->UnsignedIntTy, SourceLocation()); |
| 3016 | MsgExprs.push_back(cnt); |
| 3017 | |
| 3018 | |
| 3019 | SmallVector<QualType, 8> ArgTypes; |
| 3020 | ArgTypes.push_back(Context->getObjCIdType()); |
| 3021 | ArgTypes.push_back(Context->getObjCSelType()); |
| 3022 | for (ObjCMethodDecl::param_iterator PI = DictMethod->param_begin(), |
| 3023 | E = DictMethod->param_end(); PI != E; ++PI) { |
| 3024 | QualType T = (*PI)->getType(); |
| 3025 | if (const PointerType* PT = T->getAs<PointerType>()) { |
| 3026 | QualType PointeeTy = PT->getPointeeType(); |
| 3027 | convertToUnqualifiedObjCType(PointeeTy); |
| 3028 | T = Context->getPointerType(PointeeTy); |
| 3029 | } |
| 3030 | ArgTypes.push_back(T); |
| 3031 | } |
| 3032 | |
| 3033 | QualType returnType = Exp->getType(); |
| 3034 | // Get the type, we will need to reference it in a couple spots. |
| 3035 | QualType msgSendType = MsgSendFlavor->getType(); |
| 3036 | |
| 3037 | // Create a reference to the objc_msgSend() declaration. |
| 3038 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, false, msgSendType, |
| 3039 | VK_LValue, SourceLocation()); |
| 3040 | |
| 3041 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, |
| 3042 | Context->getPointerType(Context->VoidTy), |
| 3043 | CK_BitCast, DRE); |
| 3044 | |
| 3045 | // Now do the "normal" pointer to function cast. |
| 3046 | QualType castType = |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame^] | 3047 | getSimpleFunctionType(returnType, ArgTypes, DictMethod->isVariadic()); |
Fariborz Jahanian | e35abe1 | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 3048 | castType = Context->getPointerType(castType); |
| 3049 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
| 3050 | cast); |
| 3051 | |
| 3052 | // Don't forget the parens to enforce the proper binding. |
| 3053 | ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast); |
| 3054 | |
| 3055 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3056 | CallExpr *CE = new (Context) CallExpr(*Context, PE, MsgExprs, |
Fariborz Jahanian | e35abe1 | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 3057 | FT->getResultType(), VK_RValue, |
| 3058 | EndLoc); |
| 3059 | ReplaceStmt(Exp, CE); |
| 3060 | return CE; |
| 3061 | } |
| 3062 | |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3063 | // struct __rw_objc_super { |
| 3064 | // struct objc_object *object; struct objc_object *superClass; |
| 3065 | // }; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3066 | QualType RewriteModernObjC::getSuperStructType() { |
| 3067 | if (!SuperStructDecl) { |
| 3068 | SuperStructDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 3069 | SourceLocation(), SourceLocation(), |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3070 | &Context->Idents.get("__rw_objc_super")); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3071 | QualType FieldTypes[2]; |
| 3072 | |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3073 | // struct objc_object *object; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3074 | FieldTypes[0] = Context->getObjCIdType(); |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3075 | // struct objc_object *superClass; |
| 3076 | FieldTypes[1] = Context->getObjCIdType(); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3077 | |
| 3078 | // Create fields |
| 3079 | for (unsigned i = 0; i < 2; ++i) { |
| 3080 | SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl, |
| 3081 | SourceLocation(), |
| 3082 | SourceLocation(), 0, |
| 3083 | FieldTypes[i], 0, |
| 3084 | /*BitWidth=*/0, |
| 3085 | /*Mutable=*/false, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3086 | ICIS_NoInit)); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3087 | } |
| 3088 | |
| 3089 | SuperStructDecl->completeDefinition(); |
| 3090 | } |
| 3091 | return Context->getTagDeclType(SuperStructDecl); |
| 3092 | } |
| 3093 | |
| 3094 | QualType RewriteModernObjC::getConstantStringStructType() { |
| 3095 | if (!ConstantStringDecl) { |
| 3096 | ConstantStringDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 3097 | SourceLocation(), SourceLocation(), |
| 3098 | &Context->Idents.get("__NSConstantStringImpl")); |
| 3099 | QualType FieldTypes[4]; |
| 3100 | |
| 3101 | // struct objc_object *receiver; |
| 3102 | FieldTypes[0] = Context->getObjCIdType(); |
| 3103 | // int flags; |
| 3104 | FieldTypes[1] = Context->IntTy; |
| 3105 | // char *str; |
| 3106 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
| 3107 | // long length; |
| 3108 | FieldTypes[3] = Context->LongTy; |
| 3109 | |
| 3110 | // Create fields |
| 3111 | for (unsigned i = 0; i < 4; ++i) { |
| 3112 | ConstantStringDecl->addDecl(FieldDecl::Create(*Context, |
| 3113 | ConstantStringDecl, |
| 3114 | SourceLocation(), |
| 3115 | SourceLocation(), 0, |
| 3116 | FieldTypes[i], 0, |
| 3117 | /*BitWidth=*/0, |
| 3118 | /*Mutable=*/true, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3119 | ICIS_NoInit)); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3120 | } |
| 3121 | |
| 3122 | ConstantStringDecl->completeDefinition(); |
| 3123 | } |
| 3124 | return Context->getTagDeclType(ConstantStringDecl); |
| 3125 | } |
| 3126 | |
Fariborz Jahanian | be8d55c | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3127 | /// getFunctionSourceLocation - returns start location of a function |
| 3128 | /// definition. Complication arises when function has declared as |
| 3129 | /// extern "C" or extern "C" {...} |
| 3130 | static SourceLocation getFunctionSourceLocation (RewriteModernObjC &R, |
| 3131 | FunctionDecl *FD) { |
| 3132 | if (FD->isExternC() && !FD->isMain()) { |
| 3133 | const DeclContext *DC = FD->getDeclContext(); |
| 3134 | if (const LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(DC)) |
| 3135 | // if it is extern "C" {...}, return function decl's own location. |
| 3136 | if (!LSD->getRBraceLoc().isValid()) |
| 3137 | return LSD->getExternLoc(); |
| 3138 | } |
| 3139 | if (FD->getStorageClassAsWritten() != SC_None) |
| 3140 | R.RewriteBlockLiteralFunctionDecl(FD); |
| 3141 | return FD->getTypeSpecStartLoc(); |
| 3142 | } |
| 3143 | |
Fariborz Jahanian | 9620596 | 2012-11-06 17:30:23 +0000 | [diff] [blame] | 3144 | void RewriteModernObjC::RewriteLineDirective(const Decl *D) { |
| 3145 | |
| 3146 | SourceLocation Location = D->getLocation(); |
| 3147 | |
Fariborz Jahanian | ada7191 | 2013-02-08 00:27:34 +0000 | [diff] [blame] | 3148 | if (Location.isFileID() && GenerateLineInfo) { |
Fariborz Jahanian | 3b45ca9 | 2012-11-07 18:15:53 +0000 | [diff] [blame] | 3149 | std::string LineString("\n#line "); |
Fariborz Jahanian | 9620596 | 2012-11-06 17:30:23 +0000 | [diff] [blame] | 3150 | PresumedLoc PLoc = SM->getPresumedLoc(Location); |
| 3151 | LineString += utostr(PLoc.getLine()); |
| 3152 | LineString += " \""; |
NAKAMURA Takumi | ba529a9 | 2012-11-06 22:45:31 +0000 | [diff] [blame] | 3153 | LineString += Lexer::Stringify(PLoc.getFilename()); |
Fariborz Jahanian | 9620596 | 2012-11-06 17:30:23 +0000 | [diff] [blame] | 3154 | if (isa<ObjCMethodDecl>(D)) |
| 3155 | LineString += "\""; |
| 3156 | else LineString += "\"\n"; |
| 3157 | |
| 3158 | Location = D->getLocStart(); |
| 3159 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 3160 | if (FD->isExternC() && !FD->isMain()) { |
| 3161 | const DeclContext *DC = FD->getDeclContext(); |
| 3162 | if (const LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(DC)) |
| 3163 | // if it is extern "C" {...}, return function decl's own location. |
| 3164 | if (!LSD->getRBraceLoc().isValid()) |
| 3165 | Location = LSD->getExternLoc(); |
| 3166 | } |
| 3167 | } |
| 3168 | InsertText(Location, LineString); |
| 3169 | } |
| 3170 | } |
| 3171 | |
Fariborz Jahanian | be8d55c | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3172 | /// SynthMsgSendStretCallExpr - This routine translates message expression |
| 3173 | /// into a call to objc_msgSend_stret() entry point. Tricky part is that |
| 3174 | /// nil check on receiver must be performed before calling objc_msgSend_stret. |
| 3175 | /// MsgSendStretFlavor - function declaration objc_msgSend_stret(...) |
| 3176 | /// msgSendType - function type of objc_msgSend_stret(...) |
| 3177 | /// returnType - Result type of the method being synthesized. |
| 3178 | /// ArgTypes - type of the arguments passed to objc_msgSend_stret, starting with receiver type. |
| 3179 | /// MsgExprs - list of argument expressions being passed to objc_msgSend_stret, |
| 3180 | /// starting with receiver. |
| 3181 | /// Method - Method being rewritten. |
| 3182 | Expr *RewriteModernObjC::SynthMsgSendStretCallExpr(FunctionDecl *MsgSendStretFlavor, |
| 3183 | QualType msgSendType, |
| 3184 | QualType returnType, |
| 3185 | SmallVectorImpl<QualType> &ArgTypes, |
| 3186 | SmallVectorImpl<Expr*> &MsgExprs, |
| 3187 | ObjCMethodDecl *Method) { |
| 3188 | // Now do the "normal" pointer to function cast. |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame^] | 3189 | QualType castType = getSimpleFunctionType(returnType, ArgTypes, |
| 3190 | Method ? Method->isVariadic() |
| 3191 | : false); |
Fariborz Jahanian | be8d55c | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3192 | castType = Context->getPointerType(castType); |
| 3193 | |
| 3194 | // build type for containing the objc_msgSend_stret object. |
| 3195 | static unsigned stretCount=0; |
| 3196 | std::string name = "__Stret"; name += utostr(stretCount); |
Fariborz Jahanian | 2ca5af2 | 2012-07-25 21:48:36 +0000 | [diff] [blame] | 3197 | std::string str = |
| 3198 | "extern \"C\" void * __cdecl memset(void *_Dst, int _Val, size_t _Size);\n"; |
| 3199 | str += "struct "; str += name; |
Fariborz Jahanian | be8d55c | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3200 | str += " {\n\t"; |
| 3201 | str += name; |
| 3202 | str += "(id receiver, SEL sel"; |
| 3203 | for (unsigned i = 2; i < ArgTypes.size(); i++) { |
Fariborz Jahanian | 6734ec4 | 2012-06-29 19:55:46 +0000 | [diff] [blame] | 3204 | std::string ArgName = "arg"; ArgName += utostr(i); |
| 3205 | ArgTypes[i].getAsStringInternal(ArgName, Context->getPrintingPolicy()); |
| 3206 | str += ", "; str += ArgName; |
Fariborz Jahanian | be8d55c | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3207 | } |
| 3208 | // could be vararg. |
| 3209 | for (unsigned i = ArgTypes.size(); i < MsgExprs.size(); i++) { |
Fariborz Jahanian | 6734ec4 | 2012-06-29 19:55:46 +0000 | [diff] [blame] | 3210 | std::string ArgName = "arg"; ArgName += utostr(i); |
| 3211 | MsgExprs[i]->getType().getAsStringInternal(ArgName, |
| 3212 | Context->getPrintingPolicy()); |
| 3213 | str += ", "; str += ArgName; |
Fariborz Jahanian | be8d55c | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3214 | } |
| 3215 | |
| 3216 | str += ") {\n"; |
| 3217 | str += "\t if (receiver == 0)\n"; |
| 3218 | str += "\t memset((void*)&s, 0, sizeof(s));\n"; |
| 3219 | str += "\t else\n"; |
| 3220 | str += "\t s = (("; str += castType.getAsString(Context->getPrintingPolicy()); |
| 3221 | str += ")(void *)objc_msgSend_stret)(receiver, sel"; |
| 3222 | for (unsigned i = 2; i < ArgTypes.size(); i++) { |
| 3223 | str += ", arg"; str += utostr(i); |
| 3224 | } |
| 3225 | // could be vararg. |
| 3226 | for (unsigned i = ArgTypes.size(); i < MsgExprs.size(); i++) { |
| 3227 | str += ", arg"; str += utostr(i); |
| 3228 | } |
| 3229 | |
| 3230 | str += ");\n"; |
| 3231 | str += "\t}\n"; |
| 3232 | str += "\t"; str += returnType.getAsString(Context->getPrintingPolicy()); |
| 3233 | str += " s;\n"; |
| 3234 | str += "};\n\n"; |
Fariborz Jahanian | a6e5a6e | 2012-08-21 18:56:50 +0000 | [diff] [blame] | 3235 | SourceLocation FunLocStart; |
| 3236 | if (CurFunctionDef) |
| 3237 | FunLocStart = getFunctionSourceLocation(*this, CurFunctionDef); |
| 3238 | else { |
| 3239 | assert(CurMethodDef && "SynthMsgSendStretCallExpr - CurMethodDef is null"); |
| 3240 | FunLocStart = CurMethodDef->getLocStart(); |
| 3241 | } |
| 3242 | |
Fariborz Jahanian | be8d55c | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3243 | InsertText(FunLocStart, str); |
| 3244 | ++stretCount; |
| 3245 | |
| 3246 | // AST for __Stretn(receiver, args).s; |
| 3247 | IdentifierInfo *ID = &Context->Idents.get(name); |
| 3248 | FunctionDecl *FD = FunctionDecl::Create(*Context, TUDecl, SourceLocation(), |
Chad Rosier | e3b2988 | 2013-01-04 22:40:33 +0000 | [diff] [blame] | 3249 | SourceLocation(), ID, castType, 0, |
| 3250 | SC_Extern, SC_None, false, false); |
Fariborz Jahanian | be8d55c | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3251 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, false, castType, VK_RValue, |
| 3252 | SourceLocation()); |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3253 | CallExpr *STCE = new (Context) CallExpr(*Context, DRE, MsgExprs, |
Fariborz Jahanian | be8d55c | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3254 | castType, VK_LValue, SourceLocation()); |
| 3255 | |
| 3256 | FieldDecl *FieldD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 3257 | SourceLocation(), |
| 3258 | &Context->Idents.get("s"), |
| 3259 | returnType, 0, |
| 3260 | /*BitWidth=*/0, /*Mutable=*/true, |
| 3261 | ICIS_NoInit); |
| 3262 | MemberExpr *ME = new (Context) MemberExpr(STCE, false, FieldD, SourceLocation(), |
| 3263 | FieldD->getType(), VK_LValue, |
| 3264 | OK_Ordinary); |
| 3265 | |
| 3266 | return ME; |
| 3267 | } |
| 3268 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3269 | Stmt *RewriteModernObjC::SynthMessageExpr(ObjCMessageExpr *Exp, |
| 3270 | SourceLocation StartLoc, |
| 3271 | SourceLocation EndLoc) { |
| 3272 | if (!SelGetUidFunctionDecl) |
| 3273 | SynthSelGetUidFunctionDecl(); |
| 3274 | if (!MsgSendFunctionDecl) |
| 3275 | SynthMsgSendFunctionDecl(); |
| 3276 | if (!MsgSendSuperFunctionDecl) |
| 3277 | SynthMsgSendSuperFunctionDecl(); |
| 3278 | if (!MsgSendStretFunctionDecl) |
| 3279 | SynthMsgSendStretFunctionDecl(); |
| 3280 | if (!MsgSendSuperStretFunctionDecl) |
| 3281 | SynthMsgSendSuperStretFunctionDecl(); |
| 3282 | if (!MsgSendFpretFunctionDecl) |
| 3283 | SynthMsgSendFpretFunctionDecl(); |
| 3284 | if (!GetClassFunctionDecl) |
| 3285 | SynthGetClassFunctionDecl(); |
| 3286 | if (!GetSuperClassFunctionDecl) |
| 3287 | SynthGetSuperClassFunctionDecl(); |
| 3288 | if (!GetMetaClassFunctionDecl) |
| 3289 | SynthGetMetaClassFunctionDecl(); |
| 3290 | |
| 3291 | // default to objc_msgSend(). |
| 3292 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 3293 | // May need to use objc_msgSend_stret() as well. |
| 3294 | FunctionDecl *MsgSendStretFlavor = 0; |
| 3295 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
| 3296 | QualType resultType = mDecl->getResultType(); |
| 3297 | if (resultType->isRecordType()) |
| 3298 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
| 3299 | else if (resultType->isRealFloatingType()) |
| 3300 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
| 3301 | } |
| 3302 | |
| 3303 | // Synthesize a call to objc_msgSend(). |
| 3304 | SmallVector<Expr*, 8> MsgExprs; |
| 3305 | switch (Exp->getReceiverKind()) { |
| 3306 | case ObjCMessageExpr::SuperClass: { |
| 3307 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 3308 | if (MsgSendStretFlavor) |
| 3309 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 3310 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 3311 | |
| 3312 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
| 3313 | |
| 3314 | SmallVector<Expr*, 4> InitExprs; |
| 3315 | |
| 3316 | // set the receiver to self, the first argument to all methods. |
| 3317 | InitExprs.push_back( |
| 3318 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 3319 | CK_BitCast, |
| 3320 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3321 | false, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3322 | Context->getObjCIdType(), |
| 3323 | VK_RValue, |
| 3324 | SourceLocation())) |
| 3325 | ); // set the 'receiver'. |
| 3326 | |
| 3327 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 3328 | SmallVector<Expr*, 8> ClsExprs; |
| 3329 | QualType argType = Context->getPointerType(Context->CharTy); |
| 3330 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 3331 | ClassDecl->getIdentifier()->getName(), |
| 3332 | StringLiteral::Ascii, false, |
| 3333 | argType, SourceLocation())); |
Fariborz Jahanian | 20e181a | 2012-05-08 20:55:55 +0000 | [diff] [blame] | 3334 | // (Class)objc_getClass("CurrentClass") |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3335 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
| 3336 | &ClsExprs[0], |
| 3337 | ClsExprs.size(), |
| 3338 | StartLoc, |
| 3339 | EndLoc); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3340 | ClsExprs.clear(); |
Fariborz Jahanian | 20e181a | 2012-05-08 20:55:55 +0000 | [diff] [blame] | 3341 | ClsExprs.push_back(Cls); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3342 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 3343 | &ClsExprs[0], ClsExprs.size(), |
| 3344 | StartLoc, EndLoc); |
| 3345 | |
| 3346 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 3347 | // To turn off a warning, type-cast to 'id' |
| 3348 | InitExprs.push_back( // set 'super class', using class_getSuperclass(). |
| 3349 | NoTypeInfoCStyleCastExpr(Context, |
| 3350 | Context->getObjCIdType(), |
| 3351 | CK_BitCast, Cls)); |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3352 | // struct __rw_objc_super |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3353 | QualType superType = getSuperStructType(); |
| 3354 | Expr *SuperRep; |
| 3355 | |
| 3356 | if (LangOpts.MicrosoftExt) { |
| 3357 | SynthSuperContructorFunctionDecl(); |
| 3358 | // Simulate a contructor call... |
| 3359 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3360 | false, superType, VK_LValue, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3361 | SourceLocation()); |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3362 | SuperRep = new (Context) CallExpr(*Context, DRE, InitExprs, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3363 | superType, VK_LValue, |
| 3364 | SourceLocation()); |
| 3365 | // The code for super is a little tricky to prevent collision with |
| 3366 | // the structure definition in the header. The rewriter has it's own |
| 3367 | // internal definition (__rw_objc_super) that is uses. This is why |
| 3368 | // we need the cast below. For example: |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3369 | // (struct __rw_objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3370 | // |
| 3371 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
| 3372 | Context->getPointerType(SuperRep->getType()), |
| 3373 | VK_RValue, OK_Ordinary, |
| 3374 | SourceLocation()); |
| 3375 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 3376 | Context->getPointerType(superType), |
| 3377 | CK_BitCast, SuperRep); |
| 3378 | } else { |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3379 | // (struct __rw_objc_super) { <exprs from above> } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3380 | InitListExpr *ILE = |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3381 | new (Context) InitListExpr(*Context, SourceLocation(), InitExprs, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3382 | SourceLocation()); |
| 3383 | TypeSourceInfo *superTInfo |
| 3384 | = Context->getTrivialTypeSourceInfo(superType); |
| 3385 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
| 3386 | superType, VK_LValue, |
| 3387 | ILE, false); |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3388 | // struct __rw_objc_super * |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3389 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
| 3390 | Context->getPointerType(SuperRep->getType()), |
| 3391 | VK_RValue, OK_Ordinary, |
| 3392 | SourceLocation()); |
| 3393 | } |
| 3394 | MsgExprs.push_back(SuperRep); |
| 3395 | break; |
| 3396 | } |
| 3397 | |
| 3398 | case ObjCMessageExpr::Class: { |
| 3399 | SmallVector<Expr*, 8> ClsExprs; |
| 3400 | QualType argType = Context->getPointerType(Context->CharTy); |
| 3401 | ObjCInterfaceDecl *Class |
| 3402 | = Exp->getClassReceiver()->getAs<ObjCObjectType>()->getInterface(); |
| 3403 | IdentifierInfo *clsName = Class->getIdentifier(); |
| 3404 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 3405 | clsName->getName(), |
| 3406 | StringLiteral::Ascii, false, |
| 3407 | argType, SourceLocation())); |
| 3408 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 3409 | &ClsExprs[0], |
| 3410 | ClsExprs.size(), |
| 3411 | StartLoc, EndLoc); |
Fariborz Jahanian | 20e181a | 2012-05-08 20:55:55 +0000 | [diff] [blame] | 3412 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 3413 | Context->getObjCIdType(), |
| 3414 | CK_BitCast, Cls); |
| 3415 | MsgExprs.push_back(ArgExpr); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3416 | break; |
| 3417 | } |
| 3418 | |
| 3419 | case ObjCMessageExpr::SuperInstance:{ |
| 3420 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 3421 | if (MsgSendStretFlavor) |
| 3422 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 3423 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 3424 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
| 3425 | SmallVector<Expr*, 4> InitExprs; |
| 3426 | |
| 3427 | InitExprs.push_back( |
| 3428 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 3429 | CK_BitCast, |
| 3430 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3431 | false, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3432 | Context->getObjCIdType(), |
| 3433 | VK_RValue, SourceLocation())) |
| 3434 | ); // set the 'receiver'. |
| 3435 | |
| 3436 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 3437 | SmallVector<Expr*, 8> ClsExprs; |
| 3438 | QualType argType = Context->getPointerType(Context->CharTy); |
| 3439 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 3440 | ClassDecl->getIdentifier()->getName(), |
| 3441 | StringLiteral::Ascii, false, argType, |
| 3442 | SourceLocation())); |
Fariborz Jahanian | 20e181a | 2012-05-08 20:55:55 +0000 | [diff] [blame] | 3443 | // (Class)objc_getClass("CurrentClass") |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3444 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 3445 | &ClsExprs[0], |
| 3446 | ClsExprs.size(), |
| 3447 | StartLoc, EndLoc); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3448 | ClsExprs.clear(); |
Fariborz Jahanian | 20e181a | 2012-05-08 20:55:55 +0000 | [diff] [blame] | 3449 | ClsExprs.push_back(Cls); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3450 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 3451 | &ClsExprs[0], ClsExprs.size(), |
| 3452 | StartLoc, EndLoc); |
| 3453 | |
| 3454 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 3455 | // To turn off a warning, type-cast to 'id' |
| 3456 | InitExprs.push_back( |
| 3457 | // set 'super class', using class_getSuperclass(). |
| 3458 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 3459 | CK_BitCast, Cls)); |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3460 | // struct __rw_objc_super |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3461 | QualType superType = getSuperStructType(); |
| 3462 | Expr *SuperRep; |
| 3463 | |
| 3464 | if (LangOpts.MicrosoftExt) { |
| 3465 | SynthSuperContructorFunctionDecl(); |
| 3466 | // Simulate a contructor call... |
| 3467 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3468 | false, superType, VK_LValue, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3469 | SourceLocation()); |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3470 | SuperRep = new (Context) CallExpr(*Context, DRE, InitExprs, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3471 | superType, VK_LValue, SourceLocation()); |
| 3472 | // The code for super is a little tricky to prevent collision with |
| 3473 | // the structure definition in the header. The rewriter has it's own |
| 3474 | // internal definition (__rw_objc_super) that is uses. This is why |
| 3475 | // we need the cast below. For example: |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3476 | // (struct __rw_objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3477 | // |
| 3478 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
| 3479 | Context->getPointerType(SuperRep->getType()), |
| 3480 | VK_RValue, OK_Ordinary, |
| 3481 | SourceLocation()); |
| 3482 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 3483 | Context->getPointerType(superType), |
| 3484 | CK_BitCast, SuperRep); |
| 3485 | } else { |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3486 | // (struct __rw_objc_super) { <exprs from above> } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3487 | InitListExpr *ILE = |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3488 | new (Context) InitListExpr(*Context, SourceLocation(), InitExprs, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3489 | SourceLocation()); |
| 3490 | TypeSourceInfo *superTInfo |
| 3491 | = Context->getTrivialTypeSourceInfo(superType); |
| 3492 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
| 3493 | superType, VK_RValue, ILE, |
| 3494 | false); |
| 3495 | } |
| 3496 | MsgExprs.push_back(SuperRep); |
| 3497 | break; |
| 3498 | } |
| 3499 | |
| 3500 | case ObjCMessageExpr::Instance: { |
| 3501 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 3502 | // Foo<Proto> *. |
| 3503 | Expr *recExpr = Exp->getInstanceReceiver(); |
| 3504 | while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr)) |
| 3505 | recExpr = CE->getSubExpr(); |
| 3506 | CastKind CK = recExpr->getType()->isObjCObjectPointerType() |
| 3507 | ? CK_BitCast : recExpr->getType()->isBlockPointerType() |
| 3508 | ? CK_BlockPointerToObjCPointerCast |
| 3509 | : CK_CPointerToObjCPointerCast; |
| 3510 | |
| 3511 | recExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 3512 | CK, recExpr); |
| 3513 | MsgExprs.push_back(recExpr); |
| 3514 | break; |
| 3515 | } |
| 3516 | } |
| 3517 | |
| 3518 | // Create a call to sel_registerName("selName"), it will be the 2nd argument. |
| 3519 | SmallVector<Expr*, 8> SelExprs; |
| 3520 | QualType argType = Context->getPointerType(Context->CharTy); |
| 3521 | SelExprs.push_back(StringLiteral::Create(*Context, |
| 3522 | Exp->getSelector().getAsString(), |
| 3523 | StringLiteral::Ascii, false, |
| 3524 | argType, SourceLocation())); |
| 3525 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 3526 | &SelExprs[0], SelExprs.size(), |
| 3527 | StartLoc, |
| 3528 | EndLoc); |
| 3529 | MsgExprs.push_back(SelExp); |
| 3530 | |
| 3531 | // Now push any user supplied arguments. |
| 3532 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
| 3533 | Expr *userExpr = Exp->getArg(i); |
| 3534 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 3535 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 3536 | // Reuse the ICE type, it is exactly what the doctor ordered. |
| 3537 | QualType type = ICE->getType(); |
| 3538 | if (needToScanForQualifiers(type)) |
| 3539 | type = Context->getObjCIdType(); |
| 3540 | // Make sure we convert "type (^)(...)" to "type (*)(...)". |
| 3541 | (void)convertBlockPointerToFunctionPointer(type); |
| 3542 | const Expr *SubExpr = ICE->IgnoreParenImpCasts(); |
| 3543 | CastKind CK; |
| 3544 | if (SubExpr->getType()->isIntegralType(*Context) && |
| 3545 | type->isBooleanType()) { |
| 3546 | CK = CK_IntegralToBoolean; |
| 3547 | } else if (type->isObjCObjectPointerType()) { |
| 3548 | if (SubExpr->getType()->isBlockPointerType()) { |
| 3549 | CK = CK_BlockPointerToObjCPointerCast; |
| 3550 | } else if (SubExpr->getType()->isPointerType()) { |
| 3551 | CK = CK_CPointerToObjCPointerCast; |
| 3552 | } else { |
| 3553 | CK = CK_BitCast; |
| 3554 | } |
| 3555 | } else { |
| 3556 | CK = CK_BitCast; |
| 3557 | } |
| 3558 | |
| 3559 | userExpr = NoTypeInfoCStyleCastExpr(Context, type, CK, userExpr); |
| 3560 | } |
| 3561 | // Make id<P...> cast into an 'id' cast. |
| 3562 | else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) { |
| 3563 | if (CE->getType()->isObjCQualifiedIdType()) { |
| 3564 | while ((CE = dyn_cast<CStyleCastExpr>(userExpr))) |
| 3565 | userExpr = CE->getSubExpr(); |
| 3566 | CastKind CK; |
| 3567 | if (userExpr->getType()->isIntegralType(*Context)) { |
| 3568 | CK = CK_IntegralToPointer; |
| 3569 | } else if (userExpr->getType()->isBlockPointerType()) { |
| 3570 | CK = CK_BlockPointerToObjCPointerCast; |
| 3571 | } else if (userExpr->getType()->isPointerType()) { |
| 3572 | CK = CK_CPointerToObjCPointerCast; |
| 3573 | } else { |
| 3574 | CK = CK_BitCast; |
| 3575 | } |
| 3576 | userExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 3577 | CK, userExpr); |
| 3578 | } |
| 3579 | } |
| 3580 | MsgExprs.push_back(userExpr); |
| 3581 | // We've transferred the ownership to MsgExprs. For now, we *don't* null |
| 3582 | // out the argument in the original expression (since we aren't deleting |
| 3583 | // the ObjCMessageExpr). See RewritePropertyOrImplicitSetter() usage for more info. |
| 3584 | //Exp->setArg(i, 0); |
| 3585 | } |
| 3586 | // Generate the funky cast. |
| 3587 | CastExpr *cast; |
| 3588 | SmallVector<QualType, 8> ArgTypes; |
| 3589 | QualType returnType; |
| 3590 | |
| 3591 | // Push 'id' and 'SEL', the 2 implicit arguments. |
| 3592 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 3593 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 3594 | else |
| 3595 | ArgTypes.push_back(Context->getObjCIdType()); |
| 3596 | ArgTypes.push_back(Context->getObjCSelType()); |
| 3597 | if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) { |
| 3598 | // Push any user argument types. |
| 3599 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 3600 | E = OMD->param_end(); PI != E; ++PI) { |
| 3601 | QualType t = (*PI)->getType()->isObjCQualifiedIdType() |
| 3602 | ? Context->getObjCIdType() |
| 3603 | : (*PI)->getType(); |
| 3604 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
| 3605 | (void)convertBlockPointerToFunctionPointer(t); |
| 3606 | ArgTypes.push_back(t); |
| 3607 | } |
| 3608 | returnType = Exp->getType(); |
| 3609 | convertToUnqualifiedObjCType(returnType); |
| 3610 | (void)convertBlockPointerToFunctionPointer(returnType); |
| 3611 | } else { |
| 3612 | returnType = Context->getObjCIdType(); |
| 3613 | } |
| 3614 | // Get the type, we will need to reference it in a couple spots. |
| 3615 | QualType msgSendType = MsgSendFlavor->getType(); |
| 3616 | |
| 3617 | // Create a reference to the objc_msgSend() declaration. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3618 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, false, msgSendType, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3619 | VK_LValue, SourceLocation()); |
| 3620 | |
| 3621 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
| 3622 | // If we don't do this cast, we get the following bizarre warning/note: |
| 3623 | // xx.m:13: warning: function called through a non-compatible type |
| 3624 | // xx.m:13: note: if this code is reached, the program will abort |
| 3625 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 3626 | Context->getPointerType(Context->VoidTy), |
| 3627 | CK_BitCast, DRE); |
| 3628 | |
| 3629 | // Now do the "normal" pointer to function cast. |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame^] | 3630 | // If we don't have a method decl, force a variadic cast. |
| 3631 | const ObjCMethodDecl *MD = Exp->getMethodDecl(); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3632 | QualType castType = |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame^] | 3633 | getSimpleFunctionType(returnType, ArgTypes, MD ? MD->isVariadic() : true); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3634 | castType = Context->getPointerType(castType); |
| 3635 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
| 3636 | cast); |
| 3637 | |
| 3638 | // Don't forget the parens to enforce the proper binding. |
| 3639 | ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast); |
| 3640 | |
| 3641 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3642 | CallExpr *CE = new (Context) CallExpr(*Context, PE, MsgExprs, |
| 3643 | FT->getResultType(), VK_RValue, EndLoc); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3644 | Stmt *ReplacingStmt = CE; |
| 3645 | if (MsgSendStretFlavor) { |
| 3646 | // We have the method which returns a struct/union. Must also generate |
| 3647 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 3648 | // expression which dictate which one to envoke depending on size of |
| 3649 | // method's return type. |
| 3650 | |
Fariborz Jahanian | be8d55c | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3651 | Expr *STCE = SynthMsgSendStretCallExpr(MsgSendStretFlavor, |
| 3652 | msgSendType, returnType, |
| 3653 | ArgTypes, MsgExprs, |
| 3654 | Exp->getMethodDecl()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3655 | |
| 3656 | // Build sizeof(returnType) |
| 3657 | UnaryExprOrTypeTraitExpr *sizeofExpr = |
| 3658 | new (Context) UnaryExprOrTypeTraitExpr(UETT_SizeOf, |
| 3659 | Context->getTrivialTypeSourceInfo(returnType), |
| 3660 | Context->getSizeType(), SourceLocation(), |
| 3661 | SourceLocation()); |
| 3662 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 3663 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 3664 | // For X86 it is more complicated and some kind of target specific routine |
| 3665 | // is needed to decide what to do. |
| 3666 | unsigned IntSize = |
| 3667 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 3668 | IntegerLiteral *limit = IntegerLiteral::Create(*Context, |
| 3669 | llvm::APInt(IntSize, 8), |
| 3670 | Context->IntTy, |
| 3671 | SourceLocation()); |
| 3672 | BinaryOperator *lessThanExpr = |
| 3673 | new (Context) BinaryOperator(sizeofExpr, limit, BO_LE, Context->IntTy, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 3674 | VK_RValue, OK_Ordinary, SourceLocation(), |
| 3675 | false); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3676 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 3677 | ConditionalOperator *CondExpr = |
| 3678 | new (Context) ConditionalOperator(lessThanExpr, |
| 3679 | SourceLocation(), CE, |
| 3680 | SourceLocation(), STCE, |
| 3681 | returnType, VK_RValue, OK_Ordinary); |
| 3682 | ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3683 | CondExpr); |
| 3684 | } |
| 3685 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
| 3686 | return ReplacingStmt; |
| 3687 | } |
| 3688 | |
| 3689 | Stmt *RewriteModernObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
| 3690 | Stmt *ReplacingStmt = SynthMessageExpr(Exp, Exp->getLocStart(), |
| 3691 | Exp->getLocEnd()); |
| 3692 | |
| 3693 | // Now do the actual rewrite. |
| 3694 | ReplaceStmt(Exp, ReplacingStmt); |
| 3695 | |
| 3696 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
| 3697 | return ReplacingStmt; |
| 3698 | } |
| 3699 | |
| 3700 | // typedef struct objc_object Protocol; |
| 3701 | QualType RewriteModernObjC::getProtocolType() { |
| 3702 | if (!ProtocolTypeDecl) { |
| 3703 | TypeSourceInfo *TInfo |
| 3704 | = Context->getTrivialTypeSourceInfo(Context->getObjCIdType()); |
| 3705 | ProtocolTypeDecl = TypedefDecl::Create(*Context, TUDecl, |
| 3706 | SourceLocation(), SourceLocation(), |
| 3707 | &Context->Idents.get("Protocol"), |
| 3708 | TInfo); |
| 3709 | } |
| 3710 | return Context->getTypeDeclType(ProtocolTypeDecl); |
| 3711 | } |
| 3712 | |
| 3713 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
| 3714 | /// a synthesized/forward data reference (to the protocol's metadata). |
| 3715 | /// The forward references (and metadata) are generated in |
| 3716 | /// RewriteModernObjC::HandleTranslationUnit(). |
| 3717 | Stmt *RewriteModernObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
Fariborz Jahanian | 30650eb | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 3718 | std::string Name = "_OBJC_PROTOCOL_REFERENCE_$_" + |
| 3719 | Exp->getProtocol()->getNameAsString(); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3720 | IdentifierInfo *ID = &Context->Idents.get(Name); |
| 3721 | VarDecl *VD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
| 3722 | SourceLocation(), ID, getProtocolType(), 0, |
| 3723 | SC_Extern, SC_None); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3724 | DeclRefExpr *DRE = new (Context) DeclRefExpr(VD, false, getProtocolType(), |
| 3725 | VK_LValue, SourceLocation()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3726 | Expr *DerefExpr = new (Context) UnaryOperator(DRE, UO_AddrOf, |
| 3727 | Context->getPointerType(DRE->getType()), |
| 3728 | VK_RValue, OK_Ordinary, SourceLocation()); |
| 3729 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, DerefExpr->getType(), |
| 3730 | CK_BitCast, |
| 3731 | DerefExpr); |
| 3732 | ReplaceStmt(Exp, castExpr); |
| 3733 | ProtocolExprDecls.insert(Exp->getProtocol()->getCanonicalDecl()); |
| 3734 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
| 3735 | return castExpr; |
| 3736 | |
| 3737 | } |
| 3738 | |
| 3739 | bool RewriteModernObjC::BufferContainsPPDirectives(const char *startBuf, |
| 3740 | const char *endBuf) { |
| 3741 | while (startBuf < endBuf) { |
| 3742 | if (*startBuf == '#') { |
| 3743 | // Skip whitespace. |
| 3744 | for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf) |
| 3745 | ; |
| 3746 | if (!strncmp(startBuf, "if", strlen("if")) || |
| 3747 | !strncmp(startBuf, "ifdef", strlen("ifdef")) || |
| 3748 | !strncmp(startBuf, "ifndef", strlen("ifndef")) || |
| 3749 | !strncmp(startBuf, "define", strlen("define")) || |
| 3750 | !strncmp(startBuf, "undef", strlen("undef")) || |
| 3751 | !strncmp(startBuf, "else", strlen("else")) || |
| 3752 | !strncmp(startBuf, "elif", strlen("elif")) || |
| 3753 | !strncmp(startBuf, "endif", strlen("endif")) || |
| 3754 | !strncmp(startBuf, "pragma", strlen("pragma")) || |
| 3755 | !strncmp(startBuf, "include", strlen("include")) || |
| 3756 | !strncmp(startBuf, "import", strlen("import")) || |
| 3757 | !strncmp(startBuf, "include_next", strlen("include_next"))) |
| 3758 | return true; |
| 3759 | } |
| 3760 | startBuf++; |
| 3761 | } |
| 3762 | return false; |
| 3763 | } |
| 3764 | |
Fariborz Jahanian | 8fba894 | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3765 | /// IsTagDefinedInsideClass - This routine checks that a named tagged type |
| 3766 | /// is defined inside an objective-c class. If so, it returns true. |
Fariborz Jahanian | b68258f | 2012-05-01 17:46:45 +0000 | [diff] [blame] | 3767 | bool RewriteModernObjC::IsTagDefinedInsideClass(ObjCContainerDecl *IDecl, |
Fariborz Jahanian | 8fba894 | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3768 | TagDecl *Tag, |
| 3769 | bool &IsNamedDefinition) { |
Fariborz Jahanian | 89585e80 | 2012-04-30 19:46:53 +0000 | [diff] [blame] | 3770 | if (!IDecl) |
| 3771 | return false; |
| 3772 | SourceLocation TagLocation; |
| 3773 | if (RecordDecl *RD = dyn_cast<RecordDecl>(Tag)) { |
| 3774 | RD = RD->getDefinition(); |
Fariborz Jahanian | 8fba894 | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3775 | if (!RD || !RD->getDeclName().getAsIdentifierInfo()) |
Fariborz Jahanian | 89585e80 | 2012-04-30 19:46:53 +0000 | [diff] [blame] | 3776 | return false; |
Fariborz Jahanian | 8fba894 | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3777 | IsNamedDefinition = true; |
Fariborz Jahanian | 89585e80 | 2012-04-30 19:46:53 +0000 | [diff] [blame] | 3778 | TagLocation = RD->getLocation(); |
| 3779 | return Context->getSourceManager().isBeforeInTranslationUnit( |
Fariborz Jahanian | 8fba894 | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3780 | IDecl->getLocation(), TagLocation); |
| 3781 | } |
| 3782 | if (EnumDecl *ED = dyn_cast<EnumDecl>(Tag)) { |
| 3783 | if (!ED || !ED->getDeclName().getAsIdentifierInfo()) |
| 3784 | return false; |
| 3785 | IsNamedDefinition = true; |
| 3786 | TagLocation = ED->getLocation(); |
| 3787 | return Context->getSourceManager().isBeforeInTranslationUnit( |
| 3788 | IDecl->getLocation(), TagLocation); |
| 3789 | |
Fariborz Jahanian | 89585e80 | 2012-04-30 19:46:53 +0000 | [diff] [blame] | 3790 | } |
| 3791 | return false; |
| 3792 | } |
| 3793 | |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3794 | /// RewriteObjCFieldDeclType - This routine rewrites a type into the buffer. |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3795 | /// It handles elaborated types, as well as enum types in the process. |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3796 | bool RewriteModernObjC::RewriteObjCFieldDeclType(QualType &Type, |
| 3797 | std::string &Result) { |
Fariborz Jahanian | f5eac48 | 2012-05-02 17:34:59 +0000 | [diff] [blame] | 3798 | if (isa<TypedefType>(Type)) { |
| 3799 | Result += "\t"; |
| 3800 | return false; |
| 3801 | } |
| 3802 | |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3803 | if (Type->isArrayType()) { |
| 3804 | QualType ElemTy = Context->getBaseElementType(Type); |
| 3805 | return RewriteObjCFieldDeclType(ElemTy, Result); |
| 3806 | } |
| 3807 | else if (Type->isRecordType()) { |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3808 | RecordDecl *RD = Type->getAs<RecordType>()->getDecl(); |
| 3809 | if (RD->isCompleteDefinition()) { |
| 3810 | if (RD->isStruct()) |
| 3811 | Result += "\n\tstruct "; |
| 3812 | else if (RD->isUnion()) |
| 3813 | Result += "\n\tunion "; |
| 3814 | else |
| 3815 | assert(false && "class not allowed as an ivar type"); |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3816 | |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3817 | Result += RD->getName(); |
Fariborz Jahanian | 8fba894 | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3818 | if (GlobalDefinedTags.count(RD)) { |
| 3819 | // struct/union is defined globally, use it. |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3820 | Result += " "; |
| 3821 | return true; |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3822 | } |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3823 | Result += " {\n"; |
| 3824 | for (RecordDecl::field_iterator i = RD->field_begin(), |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3825 | e = RD->field_end(); i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3826 | FieldDecl *FD = *i; |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3827 | RewriteObjCFieldDecl(FD, Result); |
| 3828 | } |
| 3829 | Result += "\t} "; |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3830 | return true; |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3831 | } |
| 3832 | } |
| 3833 | else if (Type->isEnumeralType()) { |
| 3834 | EnumDecl *ED = Type->getAs<EnumType>()->getDecl(); |
| 3835 | if (ED->isCompleteDefinition()) { |
| 3836 | Result += "\n\tenum "; |
| 3837 | Result += ED->getName(); |
Fariborz Jahanian | 8fba894 | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3838 | if (GlobalDefinedTags.count(ED)) { |
| 3839 | // Enum is globall defined, use it. |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3840 | Result += " "; |
| 3841 | return true; |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3842 | } |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3843 | |
| 3844 | Result += " {\n"; |
| 3845 | for (EnumDecl::enumerator_iterator EC = ED->enumerator_begin(), |
| 3846 | ECEnd = ED->enumerator_end(); EC != ECEnd; ++EC) { |
| 3847 | Result += "\t"; Result += EC->getName(); Result += " = "; |
| 3848 | llvm::APSInt Val = EC->getInitVal(); |
| 3849 | Result += Val.toString(10); |
| 3850 | Result += ",\n"; |
| 3851 | } |
| 3852 | Result += "\t} "; |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3853 | return true; |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3854 | } |
| 3855 | } |
| 3856 | |
| 3857 | Result += "\t"; |
| 3858 | convertObjCTypeToCStyleType(Type); |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3859 | return false; |
| 3860 | } |
| 3861 | |
| 3862 | |
| 3863 | /// RewriteObjCFieldDecl - This routine rewrites a field into the buffer. |
| 3864 | /// It handles elaborated types, as well as enum types in the process. |
| 3865 | void RewriteModernObjC::RewriteObjCFieldDecl(FieldDecl *fieldDecl, |
| 3866 | std::string &Result) { |
| 3867 | QualType Type = fieldDecl->getType(); |
| 3868 | std::string Name = fieldDecl->getNameAsString(); |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3869 | |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3870 | bool EleboratedType = RewriteObjCFieldDeclType(Type, Result); |
| 3871 | if (!EleboratedType) |
| 3872 | Type.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3873 | Result += Name; |
| 3874 | if (fieldDecl->isBitField()) { |
| 3875 | Result += " : "; Result += utostr(fieldDecl->getBitWidthValue(*Context)); |
| 3876 | } |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3877 | else if (EleboratedType && Type->isArrayType()) { |
Eli Friedman | 6febf12 | 2012-12-13 01:43:21 +0000 | [diff] [blame] | 3878 | const ArrayType *AT = Context->getAsArrayType(Type); |
| 3879 | do { |
| 3880 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3881 | Result += "["; |
| 3882 | llvm::APInt Dim = CAT->getSize(); |
| 3883 | Result += utostr(Dim.getZExtValue()); |
| 3884 | Result += "]"; |
| 3885 | } |
Eli Friedman | 6febf12 | 2012-12-13 01:43:21 +0000 | [diff] [blame] | 3886 | AT = Context->getAsArrayType(AT->getElementType()); |
| 3887 | } while (AT); |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3888 | } |
| 3889 | |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3890 | Result += ";\n"; |
| 3891 | } |
| 3892 | |
Fariborz Jahanian | 8fba894 | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3893 | /// RewriteLocallyDefinedNamedAggregates - This routine rewrites locally defined |
| 3894 | /// named aggregate types into the input buffer. |
| 3895 | void RewriteModernObjC::RewriteLocallyDefinedNamedAggregates(FieldDecl *fieldDecl, |
| 3896 | std::string &Result) { |
| 3897 | QualType Type = fieldDecl->getType(); |
Fariborz Jahanian | f5eac48 | 2012-05-02 17:34:59 +0000 | [diff] [blame] | 3898 | if (isa<TypedefType>(Type)) |
| 3899 | return; |
Fariborz Jahanian | 8fba894 | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3900 | if (Type->isArrayType()) |
| 3901 | Type = Context->getBaseElementType(Type); |
Fariborz Jahanian | b68258f | 2012-05-01 17:46:45 +0000 | [diff] [blame] | 3902 | ObjCContainerDecl *IDecl = |
| 3903 | dyn_cast<ObjCContainerDecl>(fieldDecl->getDeclContext()); |
Fariborz Jahanian | 8fba894 | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3904 | |
| 3905 | TagDecl *TD = 0; |
| 3906 | if (Type->isRecordType()) { |
| 3907 | TD = Type->getAs<RecordType>()->getDecl(); |
| 3908 | } |
| 3909 | else if (Type->isEnumeralType()) { |
| 3910 | TD = Type->getAs<EnumType>()->getDecl(); |
| 3911 | } |
| 3912 | |
| 3913 | if (TD) { |
| 3914 | if (GlobalDefinedTags.count(TD)) |
| 3915 | return; |
| 3916 | |
| 3917 | bool IsNamedDefinition = false; |
| 3918 | if (IsTagDefinedInsideClass(IDecl, TD, IsNamedDefinition)) { |
| 3919 | RewriteObjCFieldDeclType(Type, Result); |
| 3920 | Result += ";"; |
| 3921 | } |
| 3922 | if (IsNamedDefinition) |
| 3923 | GlobalDefinedTags.insert(TD); |
| 3924 | } |
| 3925 | |
| 3926 | } |
| 3927 | |
Fariborz Jahanian | cd3b036 | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 3928 | unsigned RewriteModernObjC::ObjCIvarBitfieldGroupNo(ObjCIvarDecl *IV) { |
| 3929 | const ObjCInterfaceDecl *CDecl = IV->getContainingInterface(); |
| 3930 | if (ObjCInterefaceHasBitfieldGroups.count(CDecl)) { |
| 3931 | return IvarGroupNumber[IV]; |
| 3932 | } |
| 3933 | unsigned GroupNo = 0; |
| 3934 | SmallVector<const ObjCIvarDecl *, 8> IVars; |
| 3935 | for (const ObjCIvarDecl *IVD = CDecl->all_declared_ivar_begin(); |
| 3936 | IVD; IVD = IVD->getNextIvar()) |
| 3937 | IVars.push_back(IVD); |
| 3938 | |
| 3939 | for (unsigned i = 0, e = IVars.size(); i < e; i++) |
| 3940 | if (IVars[i]->isBitField()) { |
| 3941 | IvarGroupNumber[IVars[i++]] = ++GroupNo; |
| 3942 | while (i < e && IVars[i]->isBitField()) |
| 3943 | IvarGroupNumber[IVars[i++]] = GroupNo; |
| 3944 | if (i < e) |
| 3945 | --i; |
| 3946 | } |
| 3947 | |
| 3948 | ObjCInterefaceHasBitfieldGroups.insert(CDecl); |
| 3949 | return IvarGroupNumber[IV]; |
| 3950 | } |
| 3951 | |
| 3952 | QualType RewriteModernObjC::SynthesizeBitfieldGroupStructType( |
| 3953 | ObjCIvarDecl *IV, |
| 3954 | SmallVectorImpl<ObjCIvarDecl *> &IVars) { |
| 3955 | std::string StructTagName; |
| 3956 | ObjCIvarBitfieldGroupType(IV, StructTagName); |
| 3957 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, |
| 3958 | Context->getTranslationUnitDecl(), |
| 3959 | SourceLocation(), SourceLocation(), |
| 3960 | &Context->Idents.get(StructTagName)); |
| 3961 | for (unsigned i=0, e = IVars.size(); i < e; i++) { |
| 3962 | ObjCIvarDecl *Ivar = IVars[i]; |
| 3963 | RD->addDecl(FieldDecl::Create(*Context, RD, SourceLocation(), SourceLocation(), |
| 3964 | &Context->Idents.get(Ivar->getName()), |
| 3965 | Ivar->getType(), |
| 3966 | 0, /*Expr *BW */Ivar->getBitWidth(), false, |
| 3967 | ICIS_NoInit)); |
| 3968 | } |
| 3969 | RD->completeDefinition(); |
| 3970 | return Context->getTagDeclType(RD); |
| 3971 | } |
| 3972 | |
| 3973 | QualType RewriteModernObjC::GetGroupRecordTypeForObjCIvarBitfield(ObjCIvarDecl *IV) { |
| 3974 | const ObjCInterfaceDecl *CDecl = IV->getContainingInterface(); |
| 3975 | unsigned GroupNo = ObjCIvarBitfieldGroupNo(IV); |
| 3976 | std::pair<const ObjCInterfaceDecl*, unsigned> tuple = std::make_pair(CDecl, GroupNo); |
| 3977 | if (GroupRecordType.count(tuple)) |
| 3978 | return GroupRecordType[tuple]; |
| 3979 | |
| 3980 | SmallVector<ObjCIvarDecl *, 8> IVars; |
| 3981 | for (const ObjCIvarDecl *IVD = CDecl->all_declared_ivar_begin(); |
| 3982 | IVD; IVD = IVD->getNextIvar()) { |
| 3983 | if (IVD->isBitField()) |
| 3984 | IVars.push_back(const_cast<ObjCIvarDecl *>(IVD)); |
| 3985 | else { |
| 3986 | if (!IVars.empty()) { |
| 3987 | unsigned GroupNo = ObjCIvarBitfieldGroupNo(IVars[0]); |
| 3988 | // Generate the struct type for this group of bitfield ivars. |
| 3989 | GroupRecordType[std::make_pair(CDecl, GroupNo)] = |
| 3990 | SynthesizeBitfieldGroupStructType(IVars[0], IVars); |
| 3991 | IVars.clear(); |
| 3992 | } |
| 3993 | } |
| 3994 | } |
| 3995 | if (!IVars.empty()) { |
| 3996 | // Do the last one. |
| 3997 | unsigned GroupNo = ObjCIvarBitfieldGroupNo(IVars[0]); |
| 3998 | GroupRecordType[std::make_pair(CDecl, GroupNo)] = |
| 3999 | SynthesizeBitfieldGroupStructType(IVars[0], IVars); |
| 4000 | } |
| 4001 | QualType RetQT = GroupRecordType[tuple]; |
| 4002 | assert(!RetQT.isNull() && "GetGroupRecordTypeForObjCIvarBitfield struct type is NULL"); |
| 4003 | |
| 4004 | return RetQT; |
| 4005 | } |
| 4006 | |
| 4007 | /// ObjCIvarBitfieldGroupDecl - Names field decl. for ivar bitfield group. |
| 4008 | /// Name would be: classname__GRBF_n where n is the group number for this ivar. |
| 4009 | void RewriteModernObjC::ObjCIvarBitfieldGroupDecl(ObjCIvarDecl *IV, |
| 4010 | std::string &Result) { |
| 4011 | const ObjCInterfaceDecl *CDecl = IV->getContainingInterface(); |
| 4012 | Result += CDecl->getName(); |
| 4013 | Result += "__GRBF_"; |
| 4014 | unsigned GroupNo = ObjCIvarBitfieldGroupNo(IV); |
| 4015 | Result += utostr(GroupNo); |
| 4016 | return; |
| 4017 | } |
| 4018 | |
| 4019 | /// ObjCIvarBitfieldGroupType - Names struct type for ivar bitfield group. |
| 4020 | /// Name of the struct would be: classname__T_n where n is the group number for |
| 4021 | /// this ivar. |
| 4022 | void RewriteModernObjC::ObjCIvarBitfieldGroupType(ObjCIvarDecl *IV, |
| 4023 | std::string &Result) { |
| 4024 | const ObjCInterfaceDecl *CDecl = IV->getContainingInterface(); |
| 4025 | Result += CDecl->getName(); |
| 4026 | Result += "__T_"; |
| 4027 | unsigned GroupNo = ObjCIvarBitfieldGroupNo(IV); |
| 4028 | Result += utostr(GroupNo); |
| 4029 | return; |
| 4030 | } |
| 4031 | |
| 4032 | /// ObjCIvarBitfieldGroupOffset - Names symbol for ivar bitfield group field offset. |
| 4033 | /// Name would be: OBJC_IVAR_$_classname__GRBF_n where n is the group number for |
| 4034 | /// this ivar. |
| 4035 | void RewriteModernObjC::ObjCIvarBitfieldGroupOffset(ObjCIvarDecl *IV, |
| 4036 | std::string &Result) { |
| 4037 | Result += "OBJC_IVAR_$_"; |
| 4038 | ObjCIvarBitfieldGroupDecl(IV, Result); |
| 4039 | } |
| 4040 | |
| 4041 | #define SKIP_BITFIELDS(IX, ENDIX, VEC) { \ |
| 4042 | while ((IX < ENDIX) && VEC[IX]->isBitField()) \ |
| 4043 | ++IX; \ |
| 4044 | if (IX < ENDIX) \ |
| 4045 | --IX; \ |
| 4046 | } |
| 4047 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4048 | /// RewriteObjCInternalStruct - Rewrite one internal struct corresponding to |
| 4049 | /// an objective-c class with ivars. |
| 4050 | void RewriteModernObjC::RewriteObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
| 4051 | std::string &Result) { |
| 4052 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
| 4053 | assert(CDecl->getName() != "" && |
| 4054 | "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4055 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 4056 | SmallVector<ObjCIvarDecl *, 8> IVars; |
| 4057 | for (ObjCIvarDecl *IVD = CDecl->all_declared_ivar_begin(); |
Fariborz Jahanian | 9a2105b | 2012-03-06 17:16:27 +0000 | [diff] [blame] | 4058 | IVD; IVD = IVD->getNextIvar()) |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 4059 | IVars.push_back(IVD); |
Fariborz Jahanian | 9a2105b | 2012-03-06 17:16:27 +0000 | [diff] [blame] | 4060 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4061 | SourceLocation LocStart = CDecl->getLocStart(); |
| 4062 | SourceLocation LocEnd = CDecl->getEndOfDefinitionLoc(); |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 4063 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4064 | const char *startBuf = SM->getCharacterData(LocStart); |
| 4065 | const char *endBuf = SM->getCharacterData(LocEnd); |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 4066 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4067 | // If no ivars and no root or if its root, directly or indirectly, |
| 4068 | // have no ivars (thus not synthesized) then no need to synthesize this class. |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 4069 | if ((!CDecl->isThisDeclarationADefinition() || IVars.size() == 0) && |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4070 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
| 4071 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
| 4072 | ReplaceText(LocStart, endBuf-startBuf, Result); |
| 4073 | return; |
| 4074 | } |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 4075 | |
Fariborz Jahanian | 8fba894 | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 4076 | // Insert named struct/union definitions inside class to |
| 4077 | // outer scope. This follows semantics of locally defined |
| 4078 | // struct/unions in objective-c classes. |
| 4079 | for (unsigned i = 0, e = IVars.size(); i < e; i++) |
| 4080 | RewriteLocallyDefinedNamedAggregates(IVars[i], Result); |
Fariborz Jahanian | cd3b036 | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 4081 | |
| 4082 | // Insert named structs which are syntheized to group ivar bitfields |
| 4083 | // to outer scope as well. |
| 4084 | for (unsigned i = 0, e = IVars.size(); i < e; i++) |
| 4085 | if (IVars[i]->isBitField()) { |
| 4086 | ObjCIvarDecl *IV = IVars[i]; |
| 4087 | QualType QT = GetGroupRecordTypeForObjCIvarBitfield(IV); |
| 4088 | RewriteObjCFieldDeclType(QT, Result); |
| 4089 | Result += ";"; |
| 4090 | // skip over ivar bitfields in this group. |
| 4091 | SKIP_BITFIELDS(i , e, IVars); |
| 4092 | } |
| 4093 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4094 | Result += "\nstruct "; |
| 4095 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 4096 | Result += "_IMPL {\n"; |
| 4097 | |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 4098 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 4099 | Result += "\tstruct "; Result += RCDecl->getNameAsString(); |
| 4100 | Result += "_IMPL "; Result += RCDecl->getNameAsString(); |
| 4101 | Result += "_IVARS;\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4102 | } |
Fariborz Jahanian | 8fba894 | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 4103 | |
Fariborz Jahanian | cd3b036 | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 4104 | for (unsigned i = 0, e = IVars.size(); i < e; i++) { |
| 4105 | if (IVars[i]->isBitField()) { |
| 4106 | ObjCIvarDecl *IV = IVars[i]; |
| 4107 | Result += "\tstruct "; |
| 4108 | ObjCIvarBitfieldGroupType(IV, Result); Result += " "; |
| 4109 | ObjCIvarBitfieldGroupDecl(IV, Result); Result += ";\n"; |
| 4110 | // skip over ivar bitfields in this group. |
| 4111 | SKIP_BITFIELDS(i , e, IVars); |
| 4112 | } |
| 4113 | else |
| 4114 | RewriteObjCFieldDecl(IVars[i], Result); |
| 4115 | } |
Fariborz Jahanian | 0b17b9a | 2012-02-12 21:36:23 +0000 | [diff] [blame] | 4116 | |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 4117 | Result += "};\n"; |
| 4118 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
| 4119 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 4120 | // Mark this struct as having been generated. |
| 4121 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
| 4122 | llvm_unreachable("struct already synthesize- RewriteObjCInternalStruct"); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4123 | } |
| 4124 | |
Fariborz Jahanian | 72c88f1 | 2012-02-22 18:13:25 +0000 | [diff] [blame] | 4125 | /// RewriteIvarOffsetSymbols - Rewrite ivar offset symbols of those ivars which |
| 4126 | /// have been referenced in an ivar access expression. |
| 4127 | void RewriteModernObjC::RewriteIvarOffsetSymbols(ObjCInterfaceDecl *CDecl, |
| 4128 | std::string &Result) { |
| 4129 | // write out ivar offset symbols which have been referenced in an ivar |
| 4130 | // access expression. |
| 4131 | llvm::SmallPtrSet<ObjCIvarDecl *, 8> Ivars = ReferencedIvars[CDecl]; |
| 4132 | if (Ivars.empty()) |
| 4133 | return; |
Fariborz Jahanian | cd3b036 | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 4134 | |
| 4135 | llvm::DenseSet<std::pair<const ObjCInterfaceDecl*, unsigned> > GroupSymbolOutput; |
Fariborz Jahanian | 72c88f1 | 2012-02-22 18:13:25 +0000 | [diff] [blame] | 4136 | for (llvm::SmallPtrSet<ObjCIvarDecl *, 8>::iterator i = Ivars.begin(), |
| 4137 | e = Ivars.end(); i != e; i++) { |
| 4138 | ObjCIvarDecl *IvarDecl = (*i); |
Fariborz Jahanian | cd3b036 | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 4139 | const ObjCInterfaceDecl *IDecl = IvarDecl->getContainingInterface(); |
| 4140 | unsigned GroupNo = 0; |
| 4141 | if (IvarDecl->isBitField()) { |
| 4142 | GroupNo = ObjCIvarBitfieldGroupNo(IvarDecl); |
| 4143 | if (GroupSymbolOutput.count(std::make_pair(IDecl, GroupNo))) |
| 4144 | continue; |
| 4145 | } |
Fariborz Jahanian | 40a777a | 2012-03-12 16:46:58 +0000 | [diff] [blame] | 4146 | Result += "\n"; |
| 4147 | if (LangOpts.MicrosoftExt) |
| 4148 | Result += "__declspec(allocate(\".objc_ivar$B\")) "; |
Fariborz Jahanian | 297976d | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 4149 | Result += "extern \"C\" "; |
Fariborz Jahanian | 40a777a | 2012-03-12 16:46:58 +0000 | [diff] [blame] | 4150 | if (LangOpts.MicrosoftExt && |
| 4151 | IvarDecl->getAccessControl() != ObjCIvarDecl::Private && |
Fariborz Jahanian | 297976d | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 4152 | IvarDecl->getAccessControl() != ObjCIvarDecl::Package) |
| 4153 | Result += "__declspec(dllimport) "; |
| 4154 | |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 4155 | Result += "unsigned long "; |
Fariborz Jahanian | cd3b036 | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 4156 | if (IvarDecl->isBitField()) { |
| 4157 | ObjCIvarBitfieldGroupOffset(IvarDecl, Result); |
| 4158 | GroupSymbolOutput.insert(std::make_pair(IDecl, GroupNo)); |
| 4159 | } |
| 4160 | else |
| 4161 | WriteInternalIvarName(CDecl, IvarDecl, Result); |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 4162 | Result += ";"; |
Fariborz Jahanian | 72c88f1 | 2012-02-22 18:13:25 +0000 | [diff] [blame] | 4163 | } |
| 4164 | } |
| 4165 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4166 | //===----------------------------------------------------------------------===// |
| 4167 | // Meta Data Emission |
| 4168 | //===----------------------------------------------------------------------===// |
| 4169 | |
| 4170 | |
| 4171 | /// RewriteImplementations - This routine rewrites all method implementations |
| 4172 | /// and emits meta-data. |
| 4173 | |
| 4174 | void RewriteModernObjC::RewriteImplementations() { |
| 4175 | int ClsDefCount = ClassImplementation.size(); |
| 4176 | int CatDefCount = CategoryImplementation.size(); |
| 4177 | |
| 4178 | // Rewrite implemented methods |
Fariborz Jahanian | 8f1fed0 | 2012-02-11 20:10:52 +0000 | [diff] [blame] | 4179 | for (int i = 0; i < ClsDefCount; i++) { |
| 4180 | ObjCImplementationDecl *OIMP = ClassImplementation[i]; |
| 4181 | ObjCInterfaceDecl *CDecl = OIMP->getClassInterface(); |
| 4182 | if (CDecl->isImplicitInterfaceDecl()) |
Fariborz Jahanian | cf4c60f | 2012-02-17 22:20:12 +0000 | [diff] [blame] | 4183 | assert(false && |
| 4184 | "Legacy implicit interface rewriting not supported in moder abi"); |
Fariborz Jahanian | 8f1fed0 | 2012-02-11 20:10:52 +0000 | [diff] [blame] | 4185 | RewriteImplementationDecl(OIMP); |
| 4186 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4187 | |
Fariborz Jahanian | 8c00a1b | 2012-02-17 20:33:00 +0000 | [diff] [blame] | 4188 | for (int i = 0; i < CatDefCount; i++) { |
| 4189 | ObjCCategoryImplDecl *CIMP = CategoryImplementation[i]; |
| 4190 | ObjCInterfaceDecl *CDecl = CIMP->getClassInterface(); |
| 4191 | if (CDecl->isImplicitInterfaceDecl()) |
| 4192 | assert(false && |
| 4193 | "Legacy implicit interface rewriting not supported in moder abi"); |
Fariborz Jahanian | 8c00a1b | 2012-02-17 20:33:00 +0000 | [diff] [blame] | 4194 | RewriteImplementationDecl(CIMP); |
| 4195 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4196 | } |
| 4197 | |
| 4198 | void RewriteModernObjC::RewriteByRefString(std::string &ResultStr, |
| 4199 | const std::string &Name, |
| 4200 | ValueDecl *VD, bool def) { |
| 4201 | assert(BlockByRefDeclNo.count(VD) && |
| 4202 | "RewriteByRefString: ByRef decl missing"); |
| 4203 | if (def) |
| 4204 | ResultStr += "struct "; |
| 4205 | ResultStr += "__Block_byref_" + Name + |
| 4206 | "_" + utostr(BlockByRefDeclNo[VD]) ; |
| 4207 | } |
| 4208 | |
| 4209 | static bool HasLocalVariableExternalStorage(ValueDecl *VD) { |
| 4210 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 4211 | return (Var->isFunctionOrMethodVarDecl() && !Var->hasLocalStorage()); |
| 4212 | return false; |
| 4213 | } |
| 4214 | |
| 4215 | std::string RewriteModernObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, |
| 4216 | StringRef funcName, |
| 4217 | std::string Tag) { |
| 4218 | const FunctionType *AFT = CE->getFunctionType(); |
| 4219 | QualType RT = AFT->getResultType(); |
| 4220 | std::string StructRef = "struct " + Tag; |
Fariborz Jahanian | f616ae2 | 2012-11-06 23:25:49 +0000 | [diff] [blame] | 4221 | SourceLocation BlockLoc = CE->getExprLoc(); |
| 4222 | std::string S; |
| 4223 | ConvertSourceLocationToLineDirective(BlockLoc, S); |
| 4224 | |
| 4225 | S += "static " + RT.getAsString(Context->getPrintingPolicy()) + " __" + |
| 4226 | funcName.str() + "_block_func_" + utostr(i); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4227 | |
| 4228 | BlockDecl *BD = CE->getBlockDecl(); |
| 4229 | |
| 4230 | if (isa<FunctionNoProtoType>(AFT)) { |
| 4231 | // No user-supplied arguments. Still need to pass in a pointer to the |
| 4232 | // block (to reference imported block decl refs). |
| 4233 | S += "(" + StructRef + " *__cself)"; |
| 4234 | } else if (BD->param_empty()) { |
| 4235 | S += "(" + StructRef + " *__cself)"; |
| 4236 | } else { |
| 4237 | const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); |
| 4238 | assert(FT && "SynthesizeBlockFunc: No function proto"); |
| 4239 | S += '('; |
| 4240 | // first add the implicit argument. |
| 4241 | S += StructRef + " *__cself, "; |
| 4242 | std::string ParamStr; |
| 4243 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 4244 | E = BD->param_end(); AI != E; ++AI) { |
| 4245 | if (AI != BD->param_begin()) S += ", "; |
| 4246 | ParamStr = (*AI)->getNameAsString(); |
| 4247 | QualType QT = (*AI)->getType(); |
Fariborz Jahanian | 2610f90 | 2012-03-27 16:42:20 +0000 | [diff] [blame] | 4248 | (void)convertBlockPointerToFunctionPointer(QT); |
| 4249 | QT.getAsStringInternal(ParamStr, Context->getPrintingPolicy()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4250 | S += ParamStr; |
| 4251 | } |
| 4252 | if (FT->isVariadic()) { |
| 4253 | if (!BD->param_empty()) S += ", "; |
| 4254 | S += "..."; |
| 4255 | } |
| 4256 | S += ')'; |
| 4257 | } |
| 4258 | S += " {\n"; |
| 4259 | |
| 4260 | // Create local declarations to avoid rewriting all closure decl ref exprs. |
| 4261 | // First, emit a declaration for all "by ref" decls. |
| 4262 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 4263 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 4264 | S += " "; |
| 4265 | std::string Name = (*I)->getNameAsString(); |
| 4266 | std::string TypeString; |
| 4267 | RewriteByRefString(TypeString, Name, (*I)); |
| 4268 | TypeString += " *"; |
| 4269 | Name = TypeString + Name; |
| 4270 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; |
| 4271 | } |
| 4272 | // Next, emit a declaration for all "by copy" declarations. |
| 4273 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 4274 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 4275 | S += " "; |
| 4276 | // Handle nested closure invocation. For example: |
| 4277 | // |
| 4278 | // void (^myImportedClosure)(void); |
| 4279 | // myImportedClosure = ^(void) { setGlobalInt(x + y); }; |
| 4280 | // |
| 4281 | // void (^anotherClosure)(void); |
| 4282 | // anotherClosure = ^(void) { |
| 4283 | // myImportedClosure(); // import and invoke the closure |
| 4284 | // }; |
| 4285 | // |
| 4286 | if (isTopLevelBlockPointerType((*I)->getType())) { |
| 4287 | RewriteBlockPointerTypeVariable(S, (*I)); |
| 4288 | S += " = ("; |
| 4289 | RewriteBlockPointerType(S, (*I)->getType()); |
| 4290 | S += ")"; |
| 4291 | S += "__cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; |
| 4292 | } |
| 4293 | else { |
| 4294 | std::string Name = (*I)->getNameAsString(); |
| 4295 | QualType QT = (*I)->getType(); |
| 4296 | if (HasLocalVariableExternalStorage(*I)) |
| 4297 | QT = Context->getPointerType(QT); |
| 4298 | QT.getAsStringInternal(Name, Context->getPrintingPolicy()); |
| 4299 | S += Name + " = __cself->" + |
| 4300 | (*I)->getNameAsString() + "; // bound by copy\n"; |
| 4301 | } |
| 4302 | } |
| 4303 | std::string RewrittenStr = RewrittenBlockExprs[CE]; |
| 4304 | const char *cstr = RewrittenStr.c_str(); |
| 4305 | while (*cstr++ != '{') ; |
| 4306 | S += cstr; |
| 4307 | S += "\n"; |
| 4308 | return S; |
| 4309 | } |
| 4310 | |
| 4311 | std::string RewriteModernObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
| 4312 | StringRef funcName, |
| 4313 | std::string Tag) { |
| 4314 | std::string StructRef = "struct " + Tag; |
| 4315 | std::string S = "static void __"; |
| 4316 | |
| 4317 | S += funcName; |
| 4318 | S += "_block_copy_" + utostr(i); |
| 4319 | S += "(" + StructRef; |
| 4320 | S += "*dst, " + StructRef; |
| 4321 | S += "*src) {"; |
| 4322 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
| 4323 | E = ImportedBlockDecls.end(); I != E; ++I) { |
| 4324 | ValueDecl *VD = (*I); |
| 4325 | S += "_Block_object_assign((void*)&dst->"; |
| 4326 | S += (*I)->getNameAsString(); |
| 4327 | S += ", (void*)src->"; |
| 4328 | S += (*I)->getNameAsString(); |
| 4329 | if (BlockByRefDeclsPtrSet.count((*I))) |
| 4330 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
| 4331 | else if (VD->getType()->isBlockPointerType()) |
| 4332 | S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);"; |
| 4333 | else |
| 4334 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
| 4335 | } |
| 4336 | S += "}\n"; |
| 4337 | |
| 4338 | S += "\nstatic void __"; |
| 4339 | S += funcName; |
| 4340 | S += "_block_dispose_" + utostr(i); |
| 4341 | S += "(" + StructRef; |
| 4342 | S += "*src) {"; |
| 4343 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
| 4344 | E = ImportedBlockDecls.end(); I != E; ++I) { |
| 4345 | ValueDecl *VD = (*I); |
| 4346 | S += "_Block_object_dispose((void*)src->"; |
| 4347 | S += (*I)->getNameAsString(); |
| 4348 | if (BlockByRefDeclsPtrSet.count((*I))) |
| 4349 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
| 4350 | else if (VD->getType()->isBlockPointerType()) |
| 4351 | S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);"; |
| 4352 | else |
| 4353 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
| 4354 | } |
| 4355 | S += "}\n"; |
| 4356 | return S; |
| 4357 | } |
| 4358 | |
| 4359 | std::string RewriteModernObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 4360 | std::string Desc) { |
| 4361 | std::string S = "\nstruct " + Tag; |
| 4362 | std::string Constructor = " " + Tag; |
| 4363 | |
| 4364 | S += " {\n struct __block_impl impl;\n"; |
| 4365 | S += " struct " + Desc; |
| 4366 | S += "* Desc;\n"; |
| 4367 | |
| 4368 | Constructor += "(void *fp, "; // Invoke function pointer. |
| 4369 | Constructor += "struct " + Desc; // Descriptor pointer. |
| 4370 | Constructor += " *desc"; |
| 4371 | |
| 4372 | if (BlockDeclRefs.size()) { |
| 4373 | // Output all "by copy" declarations. |
| 4374 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 4375 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 4376 | S += " "; |
| 4377 | std::string FieldName = (*I)->getNameAsString(); |
| 4378 | std::string ArgName = "_" + FieldName; |
| 4379 | // Handle nested closure invocation. For example: |
| 4380 | // |
| 4381 | // void (^myImportedBlock)(void); |
| 4382 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
| 4383 | // |
| 4384 | // void (^anotherBlock)(void); |
| 4385 | // anotherBlock = ^(void) { |
| 4386 | // myImportedBlock(); // import and invoke the closure |
| 4387 | // }; |
| 4388 | // |
| 4389 | if (isTopLevelBlockPointerType((*I)->getType())) { |
| 4390 | S += "struct __block_impl *"; |
| 4391 | Constructor += ", void *" + ArgName; |
| 4392 | } else { |
| 4393 | QualType QT = (*I)->getType(); |
| 4394 | if (HasLocalVariableExternalStorage(*I)) |
| 4395 | QT = Context->getPointerType(QT); |
| 4396 | QT.getAsStringInternal(FieldName, Context->getPrintingPolicy()); |
| 4397 | QT.getAsStringInternal(ArgName, Context->getPrintingPolicy()); |
| 4398 | Constructor += ", " + ArgName; |
| 4399 | } |
| 4400 | S += FieldName + ";\n"; |
| 4401 | } |
| 4402 | // Output all "by ref" declarations. |
| 4403 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 4404 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 4405 | S += " "; |
| 4406 | std::string FieldName = (*I)->getNameAsString(); |
| 4407 | std::string ArgName = "_" + FieldName; |
| 4408 | { |
| 4409 | std::string TypeString; |
| 4410 | RewriteByRefString(TypeString, FieldName, (*I)); |
| 4411 | TypeString += " *"; |
| 4412 | FieldName = TypeString + FieldName; |
| 4413 | ArgName = TypeString + ArgName; |
| 4414 | Constructor += ", " + ArgName; |
| 4415 | } |
| 4416 | S += FieldName + "; // by ref\n"; |
| 4417 | } |
| 4418 | // Finish writing the constructor. |
| 4419 | Constructor += ", int flags=0)"; |
| 4420 | // Initialize all "by copy" arguments. |
| 4421 | bool firsTime = true; |
| 4422 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 4423 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 4424 | std::string Name = (*I)->getNameAsString(); |
| 4425 | if (firsTime) { |
| 4426 | Constructor += " : "; |
| 4427 | firsTime = false; |
| 4428 | } |
| 4429 | else |
| 4430 | Constructor += ", "; |
| 4431 | if (isTopLevelBlockPointerType((*I)->getType())) |
| 4432 | Constructor += Name + "((struct __block_impl *)_" + Name + ")"; |
| 4433 | else |
| 4434 | Constructor += Name + "(_" + Name + ")"; |
| 4435 | } |
| 4436 | // Initialize all "by ref" arguments. |
| 4437 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 4438 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 4439 | std::string Name = (*I)->getNameAsString(); |
| 4440 | if (firsTime) { |
| 4441 | Constructor += " : "; |
| 4442 | firsTime = false; |
| 4443 | } |
| 4444 | else |
| 4445 | Constructor += ", "; |
| 4446 | Constructor += Name + "(_" + Name + "->__forwarding)"; |
| 4447 | } |
| 4448 | |
| 4449 | Constructor += " {\n"; |
| 4450 | if (GlobalVarDecl) |
| 4451 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 4452 | else |
| 4453 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
| 4454 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 4455 | |
| 4456 | Constructor += " Desc = desc;\n"; |
| 4457 | } else { |
| 4458 | // Finish writing the constructor. |
| 4459 | Constructor += ", int flags=0) {\n"; |
| 4460 | if (GlobalVarDecl) |
| 4461 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 4462 | else |
| 4463 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
| 4464 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 4465 | Constructor += " Desc = desc;\n"; |
| 4466 | } |
| 4467 | Constructor += " "; |
| 4468 | Constructor += "}\n"; |
| 4469 | S += Constructor; |
| 4470 | S += "};\n"; |
| 4471 | return S; |
| 4472 | } |
| 4473 | |
| 4474 | std::string RewriteModernObjC::SynthesizeBlockDescriptor(std::string DescTag, |
| 4475 | std::string ImplTag, int i, |
| 4476 | StringRef FunName, |
| 4477 | unsigned hasCopy) { |
| 4478 | std::string S = "\nstatic struct " + DescTag; |
| 4479 | |
Fariborz Jahanian | 8b08adb | 2012-05-03 21:44:12 +0000 | [diff] [blame] | 4480 | S += " {\n size_t reserved;\n"; |
| 4481 | S += " size_t Block_size;\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4482 | if (hasCopy) { |
| 4483 | S += " void (*copy)(struct "; |
| 4484 | S += ImplTag; S += "*, struct "; |
| 4485 | S += ImplTag; S += "*);\n"; |
| 4486 | |
| 4487 | S += " void (*dispose)(struct "; |
| 4488 | S += ImplTag; S += "*);\n"; |
| 4489 | } |
| 4490 | S += "} "; |
| 4491 | |
| 4492 | S += DescTag + "_DATA = { 0, sizeof(struct "; |
| 4493 | S += ImplTag + ")"; |
| 4494 | if (hasCopy) { |
| 4495 | S += ", __" + FunName.str() + "_block_copy_" + utostr(i); |
| 4496 | S += ", __" + FunName.str() + "_block_dispose_" + utostr(i); |
| 4497 | } |
| 4498 | S += "};\n"; |
| 4499 | return S; |
| 4500 | } |
| 4501 | |
| 4502 | void RewriteModernObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, |
| 4503 | StringRef FunName) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4504 | bool RewriteSC = (GlobalVarDecl && |
| 4505 | !Blocks.empty() && |
| 4506 | GlobalVarDecl->getStorageClass() == SC_Static && |
| 4507 | GlobalVarDecl->getType().getCVRQualifiers()); |
| 4508 | if (RewriteSC) { |
| 4509 | std::string SC(" void __"); |
| 4510 | SC += GlobalVarDecl->getNameAsString(); |
| 4511 | SC += "() {}"; |
| 4512 | InsertText(FunLocStart, SC); |
| 4513 | } |
| 4514 | |
| 4515 | // Insert closures that were part of the function. |
| 4516 | for (unsigned i = 0, count=0; i < Blocks.size(); i++) { |
| 4517 | CollectBlockDeclRefInfo(Blocks[i]); |
| 4518 | // Need to copy-in the inner copied-in variables not actually used in this |
| 4519 | // block. |
| 4520 | for (int j = 0; j < InnerDeclRefsCount[i]; j++) { |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4521 | DeclRefExpr *Exp = InnerDeclRefs[count++]; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4522 | ValueDecl *VD = Exp->getDecl(); |
| 4523 | BlockDeclRefs.push_back(Exp); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4524 | if (!VD->hasAttr<BlocksAttr>()) { |
| 4525 | if (!BlockByCopyDeclsPtrSet.count(VD)) { |
| 4526 | BlockByCopyDeclsPtrSet.insert(VD); |
| 4527 | BlockByCopyDecls.push_back(VD); |
| 4528 | } |
| 4529 | continue; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4530 | } |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4531 | |
| 4532 | if (!BlockByRefDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4533 | BlockByRefDeclsPtrSet.insert(VD); |
| 4534 | BlockByRefDecls.push_back(VD); |
| 4535 | } |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4536 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4537 | // imported objects in the inner blocks not used in the outer |
| 4538 | // blocks must be copied/disposed in the outer block as well. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4539 | if (VD->getType()->isObjCObjectPointerType() || |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4540 | VD->getType()->isBlockPointerType()) |
| 4541 | ImportedBlockDecls.insert(VD); |
| 4542 | } |
| 4543 | |
| 4544 | std::string ImplTag = "__" + FunName.str() + "_block_impl_" + utostr(i); |
| 4545 | std::string DescTag = "__" + FunName.str() + "_block_desc_" + utostr(i); |
| 4546 | |
| 4547 | std::string CI = SynthesizeBlockImpl(Blocks[i], ImplTag, DescTag); |
| 4548 | |
| 4549 | InsertText(FunLocStart, CI); |
| 4550 | |
| 4551 | std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, ImplTag); |
| 4552 | |
| 4553 | InsertText(FunLocStart, CF); |
| 4554 | |
| 4555 | if (ImportedBlockDecls.size()) { |
| 4556 | std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, ImplTag); |
| 4557 | InsertText(FunLocStart, HF); |
| 4558 | } |
| 4559 | std::string BD = SynthesizeBlockDescriptor(DescTag, ImplTag, i, FunName, |
| 4560 | ImportedBlockDecls.size() > 0); |
| 4561 | InsertText(FunLocStart, BD); |
| 4562 | |
| 4563 | BlockDeclRefs.clear(); |
| 4564 | BlockByRefDecls.clear(); |
| 4565 | BlockByRefDeclsPtrSet.clear(); |
| 4566 | BlockByCopyDecls.clear(); |
| 4567 | BlockByCopyDeclsPtrSet.clear(); |
| 4568 | ImportedBlockDecls.clear(); |
| 4569 | } |
| 4570 | if (RewriteSC) { |
| 4571 | // Must insert any 'const/volatile/static here. Since it has been |
| 4572 | // removed as result of rewriting of block literals. |
| 4573 | std::string SC; |
| 4574 | if (GlobalVarDecl->getStorageClass() == SC_Static) |
| 4575 | SC = "static "; |
| 4576 | if (GlobalVarDecl->getType().isConstQualified()) |
| 4577 | SC += "const "; |
| 4578 | if (GlobalVarDecl->getType().isVolatileQualified()) |
| 4579 | SC += "volatile "; |
| 4580 | if (GlobalVarDecl->getType().isRestrictQualified()) |
| 4581 | SC += "restrict "; |
| 4582 | InsertText(FunLocStart, SC); |
| 4583 | } |
Fariborz Jahanian | df474ec | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 4584 | if (GlobalConstructionExp) { |
| 4585 | // extra fancy dance for global literal expression. |
| 4586 | |
| 4587 | // Always the latest block expression on the block stack. |
| 4588 | std::string Tag = "__"; |
| 4589 | Tag += FunName; |
| 4590 | Tag += "_block_impl_"; |
| 4591 | Tag += utostr(Blocks.size()-1); |
| 4592 | std::string globalBuf = "static "; |
| 4593 | globalBuf += Tag; globalBuf += " "; |
| 4594 | std::string SStr; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4595 | |
Fariborz Jahanian | df474ec | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 4596 | llvm::raw_string_ostream constructorExprBuf(SStr); |
Richard Smith | d1420c6 | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 4597 | GlobalConstructionExp->printPretty(constructorExprBuf, 0, |
Fariborz Jahanian | df474ec | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 4598 | PrintingPolicy(LangOpts)); |
| 4599 | globalBuf += constructorExprBuf.str(); |
| 4600 | globalBuf += ";\n"; |
| 4601 | InsertText(FunLocStart, globalBuf); |
| 4602 | GlobalConstructionExp = 0; |
| 4603 | } |
| 4604 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4605 | Blocks.clear(); |
| 4606 | InnerDeclRefsCount.clear(); |
| 4607 | InnerDeclRefs.clear(); |
| 4608 | RewrittenBlockExprs.clear(); |
| 4609 | } |
| 4610 | |
| 4611 | void RewriteModernObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { |
Fariborz Jahanian | 0418953 | 2012-04-25 17:56:48 +0000 | [diff] [blame] | 4612 | SourceLocation FunLocStart = |
| 4613 | (!Blocks.empty()) ? getFunctionSourceLocation(*this, FD) |
| 4614 | : FD->getTypeSpecStartLoc(); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4615 | StringRef FuncName = FD->getName(); |
| 4616 | |
| 4617 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 4618 | } |
| 4619 | |
| 4620 | static void BuildUniqueMethodName(std::string &Name, |
| 4621 | ObjCMethodDecl *MD) { |
| 4622 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
| 4623 | Name = IFace->getName(); |
| 4624 | Name += "__" + MD->getSelector().getAsString(); |
| 4625 | // Convert colons to underscores. |
| 4626 | std::string::size_type loc = 0; |
| 4627 | while ((loc = Name.find(":", loc)) != std::string::npos) |
| 4628 | Name.replace(loc, 1, "_"); |
| 4629 | } |
| 4630 | |
| 4631 | void RewriteModernObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { |
| 4632 | //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n"); |
| 4633 | //SourceLocation FunLocStart = MD->getLocStart(); |
| 4634 | SourceLocation FunLocStart = MD->getLocStart(); |
| 4635 | std::string FuncName; |
| 4636 | BuildUniqueMethodName(FuncName, MD); |
| 4637 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 4638 | } |
| 4639 | |
| 4640 | void RewriteModernObjC::GetBlockDeclRefExprs(Stmt *S) { |
| 4641 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
| 4642 | if (*CI) { |
| 4643 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 4644 | GetBlockDeclRefExprs(CBE->getBody()); |
| 4645 | else |
| 4646 | GetBlockDeclRefExprs(*CI); |
| 4647 | } |
| 4648 | // Handle specific things. |
Fariborz Jahanian | 0e97681 | 2012-04-16 23:00:57 +0000 | [diff] [blame] | 4649 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 4650 | if (DRE->refersToEnclosingLocal()) { |
| 4651 | // FIXME: Handle enums. |
| 4652 | if (!isa<FunctionDecl>(DRE->getDecl())) |
| 4653 | BlockDeclRefs.push_back(DRE); |
| 4654 | if (HasLocalVariableExternalStorage(DRE->getDecl())) |
| 4655 | BlockDeclRefs.push_back(DRE); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4656 | } |
Fariborz Jahanian | 0e97681 | 2012-04-16 23:00:57 +0000 | [diff] [blame] | 4657 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4658 | |
| 4659 | return; |
| 4660 | } |
| 4661 | |
| 4662 | void RewriteModernObjC::GetInnerBlockDeclRefExprs(Stmt *S, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4663 | SmallVector<DeclRefExpr *, 8> &InnerBlockDeclRefs, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4664 | llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts) { |
| 4665 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
| 4666 | if (*CI) { |
| 4667 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) { |
| 4668 | InnerContexts.insert(cast<DeclContext>(CBE->getBlockDecl())); |
| 4669 | GetInnerBlockDeclRefExprs(CBE->getBody(), |
| 4670 | InnerBlockDeclRefs, |
| 4671 | InnerContexts); |
| 4672 | } |
| 4673 | else |
| 4674 | GetInnerBlockDeclRefExprs(*CI, |
| 4675 | InnerBlockDeclRefs, |
| 4676 | InnerContexts); |
| 4677 | |
| 4678 | } |
| 4679 | // Handle specific things. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4680 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 4681 | if (DRE->refersToEnclosingLocal()) { |
| 4682 | if (!isa<FunctionDecl>(DRE->getDecl()) && |
| 4683 | !InnerContexts.count(DRE->getDecl()->getDeclContext())) |
| 4684 | InnerBlockDeclRefs.push_back(DRE); |
| 4685 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) |
| 4686 | if (Var->isFunctionOrMethodVarDecl()) |
| 4687 | ImportedLocalExternalDecls.insert(Var); |
| 4688 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4689 | } |
| 4690 | |
| 4691 | return; |
| 4692 | } |
| 4693 | |
Fariborz Jahanian | 164d6f8 | 2012-02-13 18:57:49 +0000 | [diff] [blame] | 4694 | /// convertObjCTypeToCStyleType - This routine converts such objc types |
| 4695 | /// as qualified objects, and blocks to their closest c/c++ types that |
| 4696 | /// it can. It returns true if input type was modified. |
| 4697 | bool RewriteModernObjC::convertObjCTypeToCStyleType(QualType &T) { |
| 4698 | QualType oldT = T; |
| 4699 | convertBlockPointerToFunctionPointer(T); |
| 4700 | if (T->isFunctionPointerType()) { |
| 4701 | QualType PointeeTy; |
| 4702 | if (const PointerType* PT = T->getAs<PointerType>()) { |
| 4703 | PointeeTy = PT->getPointeeType(); |
| 4704 | if (const FunctionType *FT = PointeeTy->getAs<FunctionType>()) { |
| 4705 | T = convertFunctionTypeOfBlocks(FT); |
| 4706 | T = Context->getPointerType(T); |
| 4707 | } |
| 4708 | } |
| 4709 | } |
| 4710 | |
| 4711 | convertToUnqualifiedObjCType(T); |
| 4712 | return T != oldT; |
| 4713 | } |
| 4714 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4715 | /// convertFunctionTypeOfBlocks - This routine converts a function type |
| 4716 | /// whose result type may be a block pointer or whose argument type(s) |
| 4717 | /// might be block pointers to an equivalent function type replacing |
| 4718 | /// all block pointers to function pointers. |
| 4719 | QualType RewriteModernObjC::convertFunctionTypeOfBlocks(const FunctionType *FT) { |
| 4720 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
| 4721 | // FTP will be null for closures that don't take arguments. |
| 4722 | // Generate a funky cast. |
| 4723 | SmallVector<QualType, 8> ArgTypes; |
| 4724 | QualType Res = FT->getResultType(); |
Fariborz Jahanian | 164d6f8 | 2012-02-13 18:57:49 +0000 | [diff] [blame] | 4725 | bool modified = convertObjCTypeToCStyleType(Res); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4726 | |
| 4727 | if (FTP) { |
| 4728 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
| 4729 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 4730 | QualType t = *I; |
| 4731 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 164d6f8 | 2012-02-13 18:57:49 +0000 | [diff] [blame] | 4732 | if (convertObjCTypeToCStyleType(t)) |
| 4733 | modified = true; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4734 | ArgTypes.push_back(t); |
| 4735 | } |
| 4736 | } |
| 4737 | QualType FuncType; |
Fariborz Jahanian | 164d6f8 | 2012-02-13 18:57:49 +0000 | [diff] [blame] | 4738 | if (modified) |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame^] | 4739 | FuncType = getSimpleFunctionType(Res, ArgTypes); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4740 | else FuncType = QualType(FT, 0); |
| 4741 | return FuncType; |
| 4742 | } |
| 4743 | |
| 4744 | Stmt *RewriteModernObjC::SynthesizeBlockCall(CallExpr *Exp, const Expr *BlockExp) { |
| 4745 | // Navigate to relevant type information. |
| 4746 | const BlockPointerType *CPT = 0; |
| 4747 | |
| 4748 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BlockExp)) { |
| 4749 | CPT = DRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4750 | } else if (const MemberExpr *MExpr = dyn_cast<MemberExpr>(BlockExp)) { |
| 4751 | CPT = MExpr->getType()->getAs<BlockPointerType>(); |
| 4752 | } |
| 4753 | else if (const ParenExpr *PRE = dyn_cast<ParenExpr>(BlockExp)) { |
| 4754 | return SynthesizeBlockCall(Exp, PRE->getSubExpr()); |
| 4755 | } |
| 4756 | else if (const ImplicitCastExpr *IEXPR = dyn_cast<ImplicitCastExpr>(BlockExp)) |
| 4757 | CPT = IEXPR->getType()->getAs<BlockPointerType>(); |
| 4758 | else if (const ConditionalOperator *CEXPR = |
| 4759 | dyn_cast<ConditionalOperator>(BlockExp)) { |
| 4760 | Expr *LHSExp = CEXPR->getLHS(); |
| 4761 | Stmt *LHSStmt = SynthesizeBlockCall(Exp, LHSExp); |
| 4762 | Expr *RHSExp = CEXPR->getRHS(); |
| 4763 | Stmt *RHSStmt = SynthesizeBlockCall(Exp, RHSExp); |
| 4764 | Expr *CONDExp = CEXPR->getCond(); |
| 4765 | ConditionalOperator *CondExpr = |
| 4766 | new (Context) ConditionalOperator(CONDExp, |
| 4767 | SourceLocation(), cast<Expr>(LHSStmt), |
| 4768 | SourceLocation(), cast<Expr>(RHSStmt), |
| 4769 | Exp->getType(), VK_RValue, OK_Ordinary); |
| 4770 | return CondExpr; |
| 4771 | } else if (const ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(BlockExp)) { |
| 4772 | CPT = IRE->getType()->getAs<BlockPointerType>(); |
| 4773 | } else if (const PseudoObjectExpr *POE |
| 4774 | = dyn_cast<PseudoObjectExpr>(BlockExp)) { |
| 4775 | CPT = POE->getType()->castAs<BlockPointerType>(); |
| 4776 | } else { |
| 4777 | assert(1 && "RewriteBlockClass: Bad type"); |
| 4778 | } |
| 4779 | assert(CPT && "RewriteBlockClass: Bad type"); |
| 4780 | const FunctionType *FT = CPT->getPointeeType()->getAs<FunctionType>(); |
| 4781 | assert(FT && "RewriteBlockClass: Bad type"); |
| 4782 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
| 4783 | // FTP will be null for closures that don't take arguments. |
| 4784 | |
| 4785 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 4786 | SourceLocation(), SourceLocation(), |
| 4787 | &Context->Idents.get("__block_impl")); |
| 4788 | QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD)); |
| 4789 | |
| 4790 | // Generate a funky cast. |
| 4791 | SmallVector<QualType, 8> ArgTypes; |
| 4792 | |
| 4793 | // Push the block argument type. |
| 4794 | ArgTypes.push_back(PtrBlock); |
| 4795 | if (FTP) { |
| 4796 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
| 4797 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 4798 | QualType t = *I; |
| 4799 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
| 4800 | if (!convertBlockPointerToFunctionPointer(t)) |
| 4801 | convertToUnqualifiedObjCType(t); |
| 4802 | ArgTypes.push_back(t); |
| 4803 | } |
| 4804 | } |
| 4805 | // Now do the pointer to function cast. |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame^] | 4806 | QualType PtrToFuncCastType = getSimpleFunctionType(Exp->getType(), ArgTypes); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4807 | |
| 4808 | PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); |
| 4809 | |
| 4810 | CastExpr *BlkCast = NoTypeInfoCStyleCastExpr(Context, PtrBlock, |
| 4811 | CK_BitCast, |
| 4812 | const_cast<Expr*>(BlockExp)); |
| 4813 | // Don't forget the parens to enforce the proper binding. |
| 4814 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 4815 | BlkCast); |
| 4816 | //PE->dump(); |
| 4817 | |
| 4818 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 4819 | SourceLocation(), |
| 4820 | &Context->Idents.get("FuncPtr"), |
| 4821 | Context->VoidPtrTy, 0, |
| 4822 | /*BitWidth=*/0, /*Mutable=*/true, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4823 | ICIS_NoInit); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4824 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
| 4825 | FD->getType(), VK_LValue, |
| 4826 | OK_Ordinary); |
| 4827 | |
| 4828 | |
| 4829 | CastExpr *FunkCast = NoTypeInfoCStyleCastExpr(Context, PtrToFuncCastType, |
| 4830 | CK_BitCast, ME); |
| 4831 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast); |
| 4832 | |
| 4833 | SmallVector<Expr*, 8> BlkExprs; |
| 4834 | // Add the implicit argument. |
| 4835 | BlkExprs.push_back(BlkCast); |
| 4836 | // Add the user arguments. |
| 4837 | for (CallExpr::arg_iterator I = Exp->arg_begin(), |
| 4838 | E = Exp->arg_end(); I != E; ++I) { |
| 4839 | BlkExprs.push_back(*I); |
| 4840 | } |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 4841 | CallExpr *CE = new (Context) CallExpr(*Context, PE, BlkExprs, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4842 | Exp->getType(), VK_RValue, |
| 4843 | SourceLocation()); |
| 4844 | return CE; |
| 4845 | } |
| 4846 | |
| 4847 | // We need to return the rewritten expression to handle cases where the |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4848 | // DeclRefExpr is embedded in another expression being rewritten. |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4849 | // For example: |
| 4850 | // |
| 4851 | // int main() { |
| 4852 | // __block Foo *f; |
| 4853 | // __block int i; |
| 4854 | // |
| 4855 | // void (^myblock)() = ^() { |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4856 | // [f test]; // f is a DeclRefExpr embedded in a message (which is being rewritten). |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4857 | // i = 77; |
| 4858 | // }; |
| 4859 | //} |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4860 | Stmt *RewriteModernObjC::RewriteBlockDeclRefExpr(DeclRefExpr *DeclRefExp) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4861 | // Rewrite the byref variable into BYREFVAR->__forwarding->BYREFVAR |
| 4862 | // for each DeclRefExp where BYREFVAR is name of the variable. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4863 | ValueDecl *VD = DeclRefExp->getDecl(); |
| 4864 | bool isArrow = DeclRefExp->refersToEnclosingLocal(); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4865 | |
| 4866 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 4867 | SourceLocation(), |
| 4868 | &Context->Idents.get("__forwarding"), |
| 4869 | Context->VoidPtrTy, 0, |
| 4870 | /*BitWidth=*/0, /*Mutable=*/true, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4871 | ICIS_NoInit); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4872 | MemberExpr *ME = new (Context) MemberExpr(DeclRefExp, isArrow, |
| 4873 | FD, SourceLocation(), |
| 4874 | FD->getType(), VK_LValue, |
| 4875 | OK_Ordinary); |
| 4876 | |
| 4877 | StringRef Name = VD->getName(); |
| 4878 | FD = FieldDecl::Create(*Context, 0, SourceLocation(), SourceLocation(), |
| 4879 | &Context->Idents.get(Name), |
| 4880 | Context->VoidPtrTy, 0, |
| 4881 | /*BitWidth=*/0, /*Mutable=*/true, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4882 | ICIS_NoInit); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4883 | ME = new (Context) MemberExpr(ME, true, FD, SourceLocation(), |
| 4884 | DeclRefExp->getType(), VK_LValue, OK_Ordinary); |
| 4885 | |
| 4886 | |
| 4887 | |
| 4888 | // Need parens to enforce precedence. |
| 4889 | ParenExpr *PE = new (Context) ParenExpr(DeclRefExp->getExprLoc(), |
| 4890 | DeclRefExp->getExprLoc(), |
| 4891 | ME); |
| 4892 | ReplaceStmt(DeclRefExp, PE); |
| 4893 | return PE; |
| 4894 | } |
| 4895 | |
| 4896 | // Rewrites the imported local variable V with external storage |
| 4897 | // (static, extern, etc.) as *V |
| 4898 | // |
| 4899 | Stmt *RewriteModernObjC::RewriteLocalVariableExternalStorage(DeclRefExpr *DRE) { |
| 4900 | ValueDecl *VD = DRE->getDecl(); |
| 4901 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 4902 | if (!ImportedLocalExternalDecls.count(Var)) |
| 4903 | return DRE; |
| 4904 | Expr *Exp = new (Context) UnaryOperator(DRE, UO_Deref, DRE->getType(), |
| 4905 | VK_LValue, OK_Ordinary, |
| 4906 | DRE->getLocation()); |
| 4907 | // Need parens to enforce precedence. |
| 4908 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 4909 | Exp); |
| 4910 | ReplaceStmt(DRE, PE); |
| 4911 | return PE; |
| 4912 | } |
| 4913 | |
| 4914 | void RewriteModernObjC::RewriteCastExpr(CStyleCastExpr *CE) { |
| 4915 | SourceLocation LocStart = CE->getLParenLoc(); |
| 4916 | SourceLocation LocEnd = CE->getRParenLoc(); |
| 4917 | |
| 4918 | // Need to avoid trying to rewrite synthesized casts. |
| 4919 | if (LocStart.isInvalid()) |
| 4920 | return; |
| 4921 | // Need to avoid trying to rewrite casts contained in macros. |
| 4922 | if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) |
| 4923 | return; |
| 4924 | |
| 4925 | const char *startBuf = SM->getCharacterData(LocStart); |
| 4926 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 4927 | QualType QT = CE->getType(); |
| 4928 | const Type* TypePtr = QT->getAs<Type>(); |
| 4929 | if (isa<TypeOfExprType>(TypePtr)) { |
| 4930 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 4931 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 4932 | std::string TypeAsString = "("; |
| 4933 | RewriteBlockPointerType(TypeAsString, QT); |
| 4934 | TypeAsString += ")"; |
| 4935 | ReplaceText(LocStart, endBuf-startBuf+1, TypeAsString); |
| 4936 | return; |
| 4937 | } |
| 4938 | // advance the location to startArgList. |
| 4939 | const char *argPtr = startBuf; |
| 4940 | |
| 4941 | while (*argPtr++ && (argPtr < endBuf)) { |
| 4942 | switch (*argPtr) { |
| 4943 | case '^': |
| 4944 | // Replace the '^' with '*'. |
| 4945 | LocStart = LocStart.getLocWithOffset(argPtr-startBuf); |
| 4946 | ReplaceText(LocStart, 1, "*"); |
| 4947 | break; |
| 4948 | } |
| 4949 | } |
| 4950 | return; |
| 4951 | } |
| 4952 | |
Fariborz Jahanian | f1ee687 | 2012-04-10 00:08:18 +0000 | [diff] [blame] | 4953 | void RewriteModernObjC::RewriteImplicitCastObjCExpr(CastExpr *IC) { |
| 4954 | CastKind CastKind = IC->getCastKind(); |
Fariborz Jahanian | 43aa1c3 | 2012-04-16 22:14:01 +0000 | [diff] [blame] | 4955 | if (CastKind != CK_BlockPointerToObjCPointerCast && |
| 4956 | CastKind != CK_AnyPointerToBlockPointerCast) |
| 4957 | return; |
Fariborz Jahanian | f1ee687 | 2012-04-10 00:08:18 +0000 | [diff] [blame] | 4958 | |
Fariborz Jahanian | 43aa1c3 | 2012-04-16 22:14:01 +0000 | [diff] [blame] | 4959 | QualType QT = IC->getType(); |
| 4960 | (void)convertBlockPointerToFunctionPointer(QT); |
| 4961 | std::string TypeString(QT.getAsString(Context->getPrintingPolicy())); |
| 4962 | std::string Str = "("; |
| 4963 | Str += TypeString; |
| 4964 | Str += ")"; |
| 4965 | InsertText(IC->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 4966 | |
Fariborz Jahanian | f1ee687 | 2012-04-10 00:08:18 +0000 | [diff] [blame] | 4967 | return; |
| 4968 | } |
| 4969 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4970 | void RewriteModernObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { |
| 4971 | SourceLocation DeclLoc = FD->getLocation(); |
| 4972 | unsigned parenCount = 0; |
| 4973 | |
| 4974 | // We have 1 or more arguments that have closure pointers. |
| 4975 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4976 | const char *startArgList = strchr(startBuf, '('); |
| 4977 | |
| 4978 | assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); |
| 4979 | |
| 4980 | parenCount++; |
| 4981 | // advance the location to startArgList. |
| 4982 | DeclLoc = DeclLoc.getLocWithOffset(startArgList-startBuf); |
| 4983 | assert((DeclLoc.isValid()) && "Invalid DeclLoc"); |
| 4984 | |
| 4985 | const char *argPtr = startArgList; |
| 4986 | |
| 4987 | while (*argPtr++ && parenCount) { |
| 4988 | switch (*argPtr) { |
| 4989 | case '^': |
| 4990 | // Replace the '^' with '*'. |
| 4991 | DeclLoc = DeclLoc.getLocWithOffset(argPtr-startArgList); |
| 4992 | ReplaceText(DeclLoc, 1, "*"); |
| 4993 | break; |
| 4994 | case '(': |
| 4995 | parenCount++; |
| 4996 | break; |
| 4997 | case ')': |
| 4998 | parenCount--; |
| 4999 | break; |
| 5000 | } |
| 5001 | } |
| 5002 | return; |
| 5003 | } |
| 5004 | |
| 5005 | bool RewriteModernObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { |
| 5006 | const FunctionProtoType *FTP; |
| 5007 | const PointerType *PT = QT->getAs<PointerType>(); |
| 5008 | if (PT) { |
| 5009 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
| 5010 | } else { |
| 5011 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
| 5012 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
| 5013 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 5014 | } |
| 5015 | if (FTP) { |
| 5016 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
| 5017 | E = FTP->arg_type_end(); I != E; ++I) |
| 5018 | if (isTopLevelBlockPointerType(*I)) |
| 5019 | return true; |
| 5020 | } |
| 5021 | return false; |
| 5022 | } |
| 5023 | |
| 5024 | bool RewriteModernObjC::PointerTypeTakesAnyObjCQualifiedType(QualType QT) { |
| 5025 | const FunctionProtoType *FTP; |
| 5026 | const PointerType *PT = QT->getAs<PointerType>(); |
| 5027 | if (PT) { |
| 5028 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
| 5029 | } else { |
| 5030 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
| 5031 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
| 5032 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 5033 | } |
| 5034 | if (FTP) { |
| 5035 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
| 5036 | E = FTP->arg_type_end(); I != E; ++I) { |
| 5037 | if ((*I)->isObjCQualifiedIdType()) |
| 5038 | return true; |
| 5039 | if ((*I)->isObjCObjectPointerType() && |
| 5040 | (*I)->getPointeeType()->isObjCQualifiedInterfaceType()) |
| 5041 | return true; |
| 5042 | } |
| 5043 | |
| 5044 | } |
| 5045 | return false; |
| 5046 | } |
| 5047 | |
| 5048 | void RewriteModernObjC::GetExtentOfArgList(const char *Name, const char *&LParen, |
| 5049 | const char *&RParen) { |
| 5050 | const char *argPtr = strchr(Name, '('); |
| 5051 | assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); |
| 5052 | |
| 5053 | LParen = argPtr; // output the start. |
| 5054 | argPtr++; // skip past the left paren. |
| 5055 | unsigned parenCount = 1; |
| 5056 | |
| 5057 | while (*argPtr && parenCount) { |
| 5058 | switch (*argPtr) { |
| 5059 | case '(': parenCount++; break; |
| 5060 | case ')': parenCount--; break; |
| 5061 | default: break; |
| 5062 | } |
| 5063 | if (parenCount) argPtr++; |
| 5064 | } |
| 5065 | assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); |
| 5066 | RParen = argPtr; // output the end |
| 5067 | } |
| 5068 | |
| 5069 | void RewriteModernObjC::RewriteBlockPointerDecl(NamedDecl *ND) { |
| 5070 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 5071 | RewriteBlockPointerFunctionArgs(FD); |
| 5072 | return; |
| 5073 | } |
| 5074 | // Handle Variables and Typedefs. |
| 5075 | SourceLocation DeclLoc = ND->getLocation(); |
| 5076 | QualType DeclT; |
| 5077 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 5078 | DeclT = VD->getType(); |
| 5079 | else if (TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(ND)) |
| 5080 | DeclT = TDD->getUnderlyingType(); |
| 5081 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) |
| 5082 | DeclT = FD->getType(); |
| 5083 | else |
| 5084 | llvm_unreachable("RewriteBlockPointerDecl(): Decl type not yet handled"); |
| 5085 | |
| 5086 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 5087 | const char *endBuf = startBuf; |
| 5088 | // scan backward (from the decl location) for the end of the previous decl. |
| 5089 | while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) |
| 5090 | startBuf--; |
| 5091 | SourceLocation Start = DeclLoc.getLocWithOffset(startBuf-endBuf); |
| 5092 | std::string buf; |
| 5093 | unsigned OrigLength=0; |
| 5094 | // *startBuf != '^' if we are dealing with a pointer to function that |
| 5095 | // may take block argument types (which will be handled below). |
| 5096 | if (*startBuf == '^') { |
| 5097 | // Replace the '^' with '*', computing a negative offset. |
| 5098 | buf = '*'; |
| 5099 | startBuf++; |
| 5100 | OrigLength++; |
| 5101 | } |
| 5102 | while (*startBuf != ')') { |
| 5103 | buf += *startBuf; |
| 5104 | startBuf++; |
| 5105 | OrigLength++; |
| 5106 | } |
| 5107 | buf += ')'; |
| 5108 | OrigLength++; |
| 5109 | |
| 5110 | if (PointerTypeTakesAnyBlockArguments(DeclT) || |
| 5111 | PointerTypeTakesAnyObjCQualifiedType(DeclT)) { |
| 5112 | // Replace the '^' with '*' for arguments. |
| 5113 | // Replace id<P> with id/*<>*/ |
| 5114 | DeclLoc = ND->getLocation(); |
| 5115 | startBuf = SM->getCharacterData(DeclLoc); |
| 5116 | const char *argListBegin, *argListEnd; |
| 5117 | GetExtentOfArgList(startBuf, argListBegin, argListEnd); |
| 5118 | while (argListBegin < argListEnd) { |
| 5119 | if (*argListBegin == '^') |
| 5120 | buf += '*'; |
| 5121 | else if (*argListBegin == '<') { |
| 5122 | buf += "/*"; |
| 5123 | buf += *argListBegin++; |
Dmitri Gribenko | 1ad23d6 | 2012-09-10 21:20:09 +0000 | [diff] [blame] | 5124 | OrigLength++; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5125 | while (*argListBegin != '>') { |
| 5126 | buf += *argListBegin++; |
| 5127 | OrigLength++; |
| 5128 | } |
| 5129 | buf += *argListBegin; |
| 5130 | buf += "*/"; |
| 5131 | } |
| 5132 | else |
| 5133 | buf += *argListBegin; |
| 5134 | argListBegin++; |
| 5135 | OrigLength++; |
| 5136 | } |
| 5137 | buf += ')'; |
| 5138 | OrigLength++; |
| 5139 | } |
| 5140 | ReplaceText(Start, OrigLength, buf); |
| 5141 | |
| 5142 | return; |
| 5143 | } |
| 5144 | |
| 5145 | |
| 5146 | /// SynthesizeByrefCopyDestroyHelper - This routine synthesizes: |
| 5147 | /// void __Block_byref_id_object_copy(struct Block_byref_id_object *dst, |
| 5148 | /// struct Block_byref_id_object *src) { |
| 5149 | /// _Block_object_assign (&_dest->object, _src->object, |
| 5150 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 5151 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 5152 | /// _Block_object_assign(&_dest->object, _src->object, |
| 5153 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 5154 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 5155 | /// } |
| 5156 | /// And: |
| 5157 | /// void __Block_byref_id_object_dispose(struct Block_byref_id_object *_src) { |
| 5158 | /// _Block_object_dispose(_src->object, |
| 5159 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 5160 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 5161 | /// _Block_object_dispose(_src->object, |
| 5162 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 5163 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 5164 | /// } |
| 5165 | |
| 5166 | std::string RewriteModernObjC::SynthesizeByrefCopyDestroyHelper(VarDecl *VD, |
| 5167 | int flag) { |
| 5168 | std::string S; |
| 5169 | if (CopyDestroyCache.count(flag)) |
| 5170 | return S; |
| 5171 | CopyDestroyCache.insert(flag); |
| 5172 | S = "static void __Block_byref_id_object_copy_"; |
| 5173 | S += utostr(flag); |
| 5174 | S += "(void *dst, void *src) {\n"; |
| 5175 | |
| 5176 | // offset into the object pointer is computed as: |
| 5177 | // void * + void* + int + int + void* + void * |
| 5178 | unsigned IntSize = |
| 5179 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 5180 | unsigned VoidPtrSize = |
| 5181 | static_cast<unsigned>(Context->getTypeSize(Context->VoidPtrTy)); |
| 5182 | |
| 5183 | unsigned offset = (VoidPtrSize*4 + IntSize + IntSize)/Context->getCharWidth(); |
| 5184 | S += " _Block_object_assign((char*)dst + "; |
| 5185 | S += utostr(offset); |
| 5186 | S += ", *(void * *) ((char*)src + "; |
| 5187 | S += utostr(offset); |
| 5188 | S += "), "; |
| 5189 | S += utostr(flag); |
| 5190 | S += ");\n}\n"; |
| 5191 | |
| 5192 | S += "static void __Block_byref_id_object_dispose_"; |
| 5193 | S += utostr(flag); |
| 5194 | S += "(void *src) {\n"; |
| 5195 | S += " _Block_object_dispose(*(void * *) ((char*)src + "; |
| 5196 | S += utostr(offset); |
| 5197 | S += "), "; |
| 5198 | S += utostr(flag); |
| 5199 | S += ");\n}\n"; |
| 5200 | return S; |
| 5201 | } |
| 5202 | |
| 5203 | /// RewriteByRefVar - For each __block typex ND variable this routine transforms |
| 5204 | /// the declaration into: |
| 5205 | /// struct __Block_byref_ND { |
| 5206 | /// void *__isa; // NULL for everything except __weak pointers |
| 5207 | /// struct __Block_byref_ND *__forwarding; |
| 5208 | /// int32_t __flags; |
| 5209 | /// int32_t __size; |
| 5210 | /// void *__Block_byref_id_object_copy; // If variable is __block ObjC object |
| 5211 | /// void *__Block_byref_id_object_dispose; // If variable is __block ObjC object |
| 5212 | /// typex ND; |
| 5213 | /// }; |
| 5214 | /// |
| 5215 | /// It then replaces declaration of ND variable with: |
| 5216 | /// struct __Block_byref_ND ND = {__isa=0B, __forwarding=&ND, __flags=some_flag, |
| 5217 | /// __size=sizeof(struct __Block_byref_ND), |
| 5218 | /// ND=initializer-if-any}; |
| 5219 | /// |
| 5220 | /// |
Fariborz Jahanian | 4fe261c | 2012-04-24 19:38:45 +0000 | [diff] [blame] | 5221 | void RewriteModernObjC::RewriteByRefVar(VarDecl *ND, bool firstDecl, |
| 5222 | bool lastDecl) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5223 | int flag = 0; |
| 5224 | int isa = 0; |
| 5225 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
| 5226 | if (DeclLoc.isInvalid()) |
| 5227 | // If type location is missing, it is because of missing type (a warning). |
| 5228 | // Use variable's location which is good for this case. |
| 5229 | DeclLoc = ND->getLocation(); |
| 5230 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 5231 | SourceLocation X = ND->getLocEnd(); |
| 5232 | X = SM->getExpansionLoc(X); |
| 5233 | const char *endBuf = SM->getCharacterData(X); |
| 5234 | std::string Name(ND->getNameAsString()); |
| 5235 | std::string ByrefType; |
| 5236 | RewriteByRefString(ByrefType, Name, ND, true); |
| 5237 | ByrefType += " {\n"; |
| 5238 | ByrefType += " void *__isa;\n"; |
| 5239 | RewriteByRefString(ByrefType, Name, ND); |
| 5240 | ByrefType += " *__forwarding;\n"; |
| 5241 | ByrefType += " int __flags;\n"; |
| 5242 | ByrefType += " int __size;\n"; |
| 5243 | // Add void *__Block_byref_id_object_copy; |
| 5244 | // void *__Block_byref_id_object_dispose; if needed. |
| 5245 | QualType Ty = ND->getType(); |
Fariborz Jahanian | b15c898 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 5246 | bool HasCopyAndDispose = Context->BlockRequiresCopying(Ty, ND); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5247 | if (HasCopyAndDispose) { |
| 5248 | ByrefType += " void (*__Block_byref_id_object_copy)(void*, void*);\n"; |
| 5249 | ByrefType += " void (*__Block_byref_id_object_dispose)(void*);\n"; |
| 5250 | } |
| 5251 | |
| 5252 | QualType T = Ty; |
| 5253 | (void)convertBlockPointerToFunctionPointer(T); |
| 5254 | T.getAsStringInternal(Name, Context->getPrintingPolicy()); |
| 5255 | |
| 5256 | ByrefType += " " + Name + ";\n"; |
| 5257 | ByrefType += "};\n"; |
| 5258 | // Insert this type in global scope. It is needed by helper function. |
| 5259 | SourceLocation FunLocStart; |
| 5260 | if (CurFunctionDef) |
Fariborz Jahanian | b75f8de | 2012-04-19 00:50:01 +0000 | [diff] [blame] | 5261 | FunLocStart = getFunctionSourceLocation(*this, CurFunctionDef); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5262 | else { |
| 5263 | assert(CurMethodDef && "RewriteByRefVar - CurMethodDef is null"); |
| 5264 | FunLocStart = CurMethodDef->getLocStart(); |
| 5265 | } |
| 5266 | InsertText(FunLocStart, ByrefType); |
Fariborz Jahanian | 8247c4e | 2012-04-24 16:45:27 +0000 | [diff] [blame] | 5267 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5268 | if (Ty.isObjCGCWeak()) { |
| 5269 | flag |= BLOCK_FIELD_IS_WEAK; |
| 5270 | isa = 1; |
| 5271 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5272 | if (HasCopyAndDispose) { |
| 5273 | flag = BLOCK_BYREF_CALLER; |
| 5274 | QualType Ty = ND->getType(); |
| 5275 | // FIXME. Handle __weak variable (BLOCK_FIELD_IS_WEAK) as well. |
| 5276 | if (Ty->isBlockPointerType()) |
| 5277 | flag |= BLOCK_FIELD_IS_BLOCK; |
| 5278 | else |
| 5279 | flag |= BLOCK_FIELD_IS_OBJECT; |
| 5280 | std::string HF = SynthesizeByrefCopyDestroyHelper(ND, flag); |
| 5281 | if (!HF.empty()) |
Fariborz Jahanian | 31c4a4b | 2013-02-07 22:50:40 +0000 | [diff] [blame] | 5282 | Preamble += HF; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5283 | } |
| 5284 | |
| 5285 | // struct __Block_byref_ND ND = |
| 5286 | // {0, &ND, some_flag, __size=sizeof(struct __Block_byref_ND), |
| 5287 | // initializer-if-any}; |
| 5288 | bool hasInit = (ND->getInit() != 0); |
Fariborz Jahanian | 104dbf9 | 2012-04-11 23:57:12 +0000 | [diff] [blame] | 5289 | // FIXME. rewriter does not support __block c++ objects which |
| 5290 | // require construction. |
Fariborz Jahanian | 65a7c68 | 2012-04-26 23:20:25 +0000 | [diff] [blame] | 5291 | if (hasInit) |
| 5292 | if (CXXConstructExpr *CExp = dyn_cast<CXXConstructExpr>(ND->getInit())) { |
| 5293 | CXXConstructorDecl *CXXDecl = CExp->getConstructor(); |
| 5294 | if (CXXDecl && CXXDecl->isDefaultConstructor()) |
| 5295 | hasInit = false; |
| 5296 | } |
| 5297 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5298 | unsigned flags = 0; |
| 5299 | if (HasCopyAndDispose) |
| 5300 | flags |= BLOCK_HAS_COPY_DISPOSE; |
| 5301 | Name = ND->getNameAsString(); |
| 5302 | ByrefType.clear(); |
| 5303 | RewriteByRefString(ByrefType, Name, ND); |
| 5304 | std::string ForwardingCastType("("); |
| 5305 | ForwardingCastType += ByrefType + " *)"; |
Fariborz Jahanian | 8247c4e | 2012-04-24 16:45:27 +0000 | [diff] [blame] | 5306 | ByrefType += " " + Name + " = {(void*)"; |
| 5307 | ByrefType += utostr(isa); |
| 5308 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
| 5309 | ByrefType += utostr(flags); |
| 5310 | ByrefType += ", "; |
| 5311 | ByrefType += "sizeof("; |
| 5312 | RewriteByRefString(ByrefType, Name, ND); |
| 5313 | ByrefType += ")"; |
| 5314 | if (HasCopyAndDispose) { |
| 5315 | ByrefType += ", __Block_byref_id_object_copy_"; |
| 5316 | ByrefType += utostr(flag); |
| 5317 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 5318 | ByrefType += utostr(flag); |
| 5319 | } |
| 5320 | |
Fariborz Jahanian | 4fe261c | 2012-04-24 19:38:45 +0000 | [diff] [blame] | 5321 | if (!firstDecl) { |
| 5322 | // In multiple __block declarations, and for all but 1st declaration, |
| 5323 | // find location of the separating comma. This would be start location |
| 5324 | // where new text is to be inserted. |
| 5325 | DeclLoc = ND->getLocation(); |
| 5326 | const char *startDeclBuf = SM->getCharacterData(DeclLoc); |
| 5327 | const char *commaBuf = startDeclBuf; |
| 5328 | while (*commaBuf != ',') |
| 5329 | commaBuf--; |
| 5330 | assert((*commaBuf == ',') && "RewriteByRefVar: can't find ','"); |
| 5331 | DeclLoc = DeclLoc.getLocWithOffset(commaBuf - startDeclBuf); |
| 5332 | startBuf = commaBuf; |
| 5333 | } |
| 5334 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5335 | if (!hasInit) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5336 | ByrefType += "};\n"; |
| 5337 | unsigned nameSize = Name.size(); |
| 5338 | // for block or function pointer declaration. Name is aleady |
| 5339 | // part of the declaration. |
| 5340 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) |
| 5341 | nameSize = 1; |
| 5342 | ReplaceText(DeclLoc, endBuf-startBuf+nameSize, ByrefType); |
| 5343 | } |
| 5344 | else { |
Fariborz Jahanian | 8247c4e | 2012-04-24 16:45:27 +0000 | [diff] [blame] | 5345 | ByrefType += ", "; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5346 | SourceLocation startLoc; |
| 5347 | Expr *E = ND->getInit(); |
| 5348 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 5349 | startLoc = ECE->getLParenLoc(); |
| 5350 | else |
| 5351 | startLoc = E->getLocStart(); |
| 5352 | startLoc = SM->getExpansionLoc(startLoc); |
| 5353 | endBuf = SM->getCharacterData(startLoc); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5354 | ReplaceText(DeclLoc, endBuf-startBuf, ByrefType); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5355 | |
Fariborz Jahanian | 4fe261c | 2012-04-24 19:38:45 +0000 | [diff] [blame] | 5356 | const char separator = lastDecl ? ';' : ','; |
| 5357 | const char *startInitializerBuf = SM->getCharacterData(startLoc); |
| 5358 | const char *separatorBuf = strchr(startInitializerBuf, separator); |
| 5359 | assert((*separatorBuf == separator) && |
| 5360 | "RewriteByRefVar: can't find ';' or ','"); |
| 5361 | SourceLocation separatorLoc = |
| 5362 | startLoc.getLocWithOffset(separatorBuf-startInitializerBuf); |
| 5363 | |
| 5364 | InsertText(separatorLoc, lastDecl ? "}" : "};\n"); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5365 | } |
| 5366 | return; |
| 5367 | } |
| 5368 | |
| 5369 | void RewriteModernObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { |
| 5370 | // Add initializers for any closure decl refs. |
| 5371 | GetBlockDeclRefExprs(Exp->getBody()); |
| 5372 | if (BlockDeclRefs.size()) { |
| 5373 | // Unique all "by copy" declarations. |
| 5374 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5375 | if (!BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>()) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5376 | if (!BlockByCopyDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 5377 | BlockByCopyDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 5378 | BlockByCopyDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 5379 | } |
| 5380 | } |
| 5381 | // Unique all "by ref" declarations. |
| 5382 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5383 | if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>()) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5384 | if (!BlockByRefDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 5385 | BlockByRefDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 5386 | BlockByRefDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 5387 | } |
| 5388 | } |
| 5389 | // Find any imported blocks...they will need special attention. |
| 5390 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5391 | if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>() || |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5392 | BlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
| 5393 | BlockDeclRefs[i]->getType()->isBlockPointerType()) |
| 5394 | ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 5395 | } |
| 5396 | } |
| 5397 | |
| 5398 | FunctionDecl *RewriteModernObjC::SynthBlockInitFunctionDecl(StringRef name) { |
| 5399 | IdentifierInfo *ID = &Context->Idents.get(name); |
| 5400 | QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy); |
| 5401 | return FunctionDecl::Create(*Context, TUDecl, SourceLocation(), |
| 5402 | SourceLocation(), ID, FType, 0, SC_Extern, |
| 5403 | SC_None, false, false); |
| 5404 | } |
| 5405 | |
| 5406 | Stmt *RewriteModernObjC::SynthBlockInitExpr(BlockExpr *Exp, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5407 | const SmallVector<DeclRefExpr *, 8> &InnerBlockDeclRefs) { |
Fariborz Jahanian | d13c2c2 | 2012-03-22 19:54:39 +0000 | [diff] [blame] | 5408 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5409 | const BlockDecl *block = Exp->getBlockDecl(); |
Fariborz Jahanian | d13c2c2 | 2012-03-22 19:54:39 +0000 | [diff] [blame] | 5410 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5411 | Blocks.push_back(Exp); |
| 5412 | |
| 5413 | CollectBlockDeclRefInfo(Exp); |
| 5414 | |
| 5415 | // Add inner imported variables now used in current block. |
| 5416 | int countOfInnerDecls = 0; |
| 5417 | if (!InnerBlockDeclRefs.empty()) { |
| 5418 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) { |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5419 | DeclRefExpr *Exp = InnerBlockDeclRefs[i]; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5420 | ValueDecl *VD = Exp->getDecl(); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5421 | if (!VD->hasAttr<BlocksAttr>() && !BlockByCopyDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5422 | // We need to save the copied-in variables in nested |
| 5423 | // blocks because it is needed at the end for some of the API generations. |
| 5424 | // See SynthesizeBlockLiterals routine. |
| 5425 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 5426 | BlockDeclRefs.push_back(Exp); |
| 5427 | BlockByCopyDeclsPtrSet.insert(VD); |
| 5428 | BlockByCopyDecls.push_back(VD); |
| 5429 | } |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5430 | if (VD->hasAttr<BlocksAttr>() && !BlockByRefDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5431 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 5432 | BlockDeclRefs.push_back(Exp); |
| 5433 | BlockByRefDeclsPtrSet.insert(VD); |
| 5434 | BlockByRefDecls.push_back(VD); |
| 5435 | } |
| 5436 | } |
| 5437 | // Find any imported blocks...they will need special attention. |
| 5438 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5439 | if (InnerBlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>() || |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5440 | InnerBlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
| 5441 | InnerBlockDeclRefs[i]->getType()->isBlockPointerType()) |
| 5442 | ImportedBlockDecls.insert(InnerBlockDeclRefs[i]->getDecl()); |
| 5443 | } |
| 5444 | InnerDeclRefsCount.push_back(countOfInnerDecls); |
| 5445 | |
| 5446 | std::string FuncName; |
| 5447 | |
| 5448 | if (CurFunctionDef) |
| 5449 | FuncName = CurFunctionDef->getNameAsString(); |
| 5450 | else if (CurMethodDef) |
| 5451 | BuildUniqueMethodName(FuncName, CurMethodDef); |
| 5452 | else if (GlobalVarDecl) |
| 5453 | FuncName = std::string(GlobalVarDecl->getNameAsString()); |
| 5454 | |
Fariborz Jahanian | df474ec | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 5455 | bool GlobalBlockExpr = |
| 5456 | block->getDeclContext()->getRedeclContext()->isFileContext(); |
| 5457 | |
| 5458 | if (GlobalBlockExpr && !GlobalVarDecl) { |
| 5459 | Diags.Report(block->getLocation(), GlobalBlockRewriteFailedDiag); |
| 5460 | GlobalBlockExpr = false; |
| 5461 | } |
| 5462 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5463 | std::string BlockNumber = utostr(Blocks.size()-1); |
| 5464 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5465 | std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; |
| 5466 | |
| 5467 | // Get a pointer to the function type so we can cast appropriately. |
| 5468 | QualType BFT = convertFunctionTypeOfBlocks(Exp->getFunctionType()); |
| 5469 | QualType FType = Context->getPointerType(BFT); |
| 5470 | |
| 5471 | FunctionDecl *FD; |
| 5472 | Expr *NewRep; |
| 5473 | |
| 5474 | // Simulate a contructor call... |
Fariborz Jahanian | df474ec | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 5475 | std::string Tag; |
| 5476 | |
| 5477 | if (GlobalBlockExpr) |
| 5478 | Tag = "__global_"; |
| 5479 | else |
| 5480 | Tag = "__"; |
| 5481 | Tag += FuncName + "_block_impl_" + BlockNumber; |
| 5482 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5483 | FD = SynthBlockInitFunctionDecl(Tag); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5484 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, false, FType, VK_RValue, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5485 | SourceLocation()); |
| 5486 | |
| 5487 | SmallVector<Expr*, 4> InitExprs; |
| 5488 | |
| 5489 | // Initialize the block function. |
| 5490 | FD = SynthBlockInitFunctionDecl(Func); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5491 | DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, false, FD->getType(), |
| 5492 | VK_LValue, SourceLocation()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5493 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
| 5494 | CK_BitCast, Arg); |
| 5495 | InitExprs.push_back(castExpr); |
| 5496 | |
| 5497 | // Initialize the block descriptor. |
| 5498 | std::string DescData = "__" + FuncName + "_block_desc_" + BlockNumber + "_DATA"; |
| 5499 | |
| 5500 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, |
| 5501 | SourceLocation(), SourceLocation(), |
| 5502 | &Context->Idents.get(DescData.c_str()), |
| 5503 | Context->VoidPtrTy, 0, |
| 5504 | SC_Static, SC_None); |
| 5505 | UnaryOperator *DescRefExpr = |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5506 | new (Context) UnaryOperator(new (Context) DeclRefExpr(NewVD, false, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5507 | Context->VoidPtrTy, |
| 5508 | VK_LValue, |
| 5509 | SourceLocation()), |
| 5510 | UO_AddrOf, |
| 5511 | Context->getPointerType(Context->VoidPtrTy), |
| 5512 | VK_RValue, OK_Ordinary, |
| 5513 | SourceLocation()); |
| 5514 | InitExprs.push_back(DescRefExpr); |
| 5515 | |
| 5516 | // Add initializers for any closure decl refs. |
| 5517 | if (BlockDeclRefs.size()) { |
| 5518 | Expr *Exp; |
| 5519 | // Output all "by copy" declarations. |
| 5520 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 5521 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 5522 | if (isObjCType((*I)->getType())) { |
| 5523 | // FIXME: Conform to ABI ([[obj retain] autorelease]). |
| 5524 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5525 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), |
| 5526 | VK_LValue, SourceLocation()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5527 | if (HasLocalVariableExternalStorage(*I)) { |
| 5528 | QualType QT = (*I)->getType(); |
| 5529 | QT = Context->getPointerType(QT); |
| 5530 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue, |
| 5531 | OK_Ordinary, SourceLocation()); |
| 5532 | } |
| 5533 | } else if (isTopLevelBlockPointerType((*I)->getType())) { |
| 5534 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5535 | Arg = new (Context) DeclRefExpr(FD, false, FD->getType(), |
| 5536 | VK_LValue, SourceLocation()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5537 | Exp = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
| 5538 | CK_BitCast, Arg); |
| 5539 | } else { |
| 5540 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5541 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), |
| 5542 | VK_LValue, SourceLocation()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5543 | if (HasLocalVariableExternalStorage(*I)) { |
| 5544 | QualType QT = (*I)->getType(); |
| 5545 | QT = Context->getPointerType(QT); |
| 5546 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue, |
| 5547 | OK_Ordinary, SourceLocation()); |
| 5548 | } |
| 5549 | |
| 5550 | } |
| 5551 | InitExprs.push_back(Exp); |
| 5552 | } |
| 5553 | // Output all "by ref" declarations. |
| 5554 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 5555 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 5556 | ValueDecl *ND = (*I); |
| 5557 | std::string Name(ND->getNameAsString()); |
| 5558 | std::string RecName; |
| 5559 | RewriteByRefString(RecName, Name, ND, true); |
| 5560 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str() |
| 5561 | + sizeof("struct")); |
| 5562 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 5563 | SourceLocation(), SourceLocation(), |
| 5564 | II); |
| 5565 | assert(RD && "SynthBlockInitExpr(): Can't find RecordDecl"); |
| 5566 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 5567 | |
| 5568 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5569 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5570 | SourceLocation()); |
| 5571 | bool isNestedCapturedVar = false; |
| 5572 | if (block) |
| 5573 | for (BlockDecl::capture_const_iterator ci = block->capture_begin(), |
| 5574 | ce = block->capture_end(); ci != ce; ++ci) { |
| 5575 | const VarDecl *variable = ci->getVariable(); |
| 5576 | if (variable == ND && ci->isNested()) { |
| 5577 | assert (ci->isByRef() && |
| 5578 | "SynthBlockInitExpr - captured block variable is not byref"); |
| 5579 | isNestedCapturedVar = true; |
| 5580 | break; |
| 5581 | } |
| 5582 | } |
| 5583 | // captured nested byref variable has its address passed. Do not take |
| 5584 | // its address again. |
| 5585 | if (!isNestedCapturedVar) |
| 5586 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, |
| 5587 | Context->getPointerType(Exp->getType()), |
| 5588 | VK_RValue, OK_Ordinary, SourceLocation()); |
| 5589 | Exp = NoTypeInfoCStyleCastExpr(Context, castT, CK_BitCast, Exp); |
| 5590 | InitExprs.push_back(Exp); |
| 5591 | } |
| 5592 | } |
| 5593 | if (ImportedBlockDecls.size()) { |
| 5594 | // generate BLOCK_HAS_COPY_DISPOSE(have helper funcs) | BLOCK_HAS_DESCRIPTOR |
| 5595 | int flag = (BLOCK_HAS_COPY_DISPOSE | BLOCK_HAS_DESCRIPTOR); |
| 5596 | unsigned IntSize = |
| 5597 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 5598 | Expr *FlagExp = IntegerLiteral::Create(*Context, llvm::APInt(IntSize, flag), |
| 5599 | Context->IntTy, SourceLocation()); |
| 5600 | InitExprs.push_back(FlagExp); |
| 5601 | } |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 5602 | NewRep = new (Context) CallExpr(*Context, DRE, InitExprs, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5603 | FType, VK_LValue, SourceLocation()); |
Fariborz Jahanian | df474ec | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 5604 | |
| 5605 | if (GlobalBlockExpr) { |
| 5606 | assert (GlobalConstructionExp == 0 && |
| 5607 | "SynthBlockInitExpr - GlobalConstructionExp must be null"); |
| 5608 | GlobalConstructionExp = NewRep; |
| 5609 | NewRep = DRE; |
| 5610 | } |
| 5611 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5612 | NewRep = new (Context) UnaryOperator(NewRep, UO_AddrOf, |
| 5613 | Context->getPointerType(NewRep->getType()), |
| 5614 | VK_RValue, OK_Ordinary, SourceLocation()); |
| 5615 | NewRep = NoTypeInfoCStyleCastExpr(Context, FType, CK_BitCast, |
| 5616 | NewRep); |
| 5617 | BlockDeclRefs.clear(); |
| 5618 | BlockByRefDecls.clear(); |
| 5619 | BlockByRefDeclsPtrSet.clear(); |
| 5620 | BlockByCopyDecls.clear(); |
| 5621 | BlockByCopyDeclsPtrSet.clear(); |
| 5622 | ImportedBlockDecls.clear(); |
| 5623 | return NewRep; |
| 5624 | } |
| 5625 | |
| 5626 | bool RewriteModernObjC::IsDeclStmtInForeachHeader(DeclStmt *DS) { |
| 5627 | if (const ObjCForCollectionStmt * CS = |
| 5628 | dyn_cast<ObjCForCollectionStmt>(Stmts.back())) |
| 5629 | return CS->getElement() == DS; |
| 5630 | return false; |
| 5631 | } |
| 5632 | |
| 5633 | //===----------------------------------------------------------------------===// |
| 5634 | // Function Body / Expression rewriting |
| 5635 | //===----------------------------------------------------------------------===// |
| 5636 | |
| 5637 | Stmt *RewriteModernObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
| 5638 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
| 5639 | isa<DoStmt>(S) || isa<ForStmt>(S)) |
| 5640 | Stmts.push_back(S); |
| 5641 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 5642 | Stmts.push_back(S); |
| 5643 | ObjCBcLabelNo.push_back(++BcLabelCount); |
| 5644 | } |
| 5645 | |
| 5646 | // Pseudo-object operations and ivar references need special |
| 5647 | // treatment because we're going to recursively rewrite them. |
| 5648 | if (PseudoObjectExpr *PseudoOp = dyn_cast<PseudoObjectExpr>(S)) { |
| 5649 | if (isa<BinaryOperator>(PseudoOp->getSyntacticForm())) { |
| 5650 | return RewritePropertyOrImplicitSetter(PseudoOp); |
| 5651 | } else { |
| 5652 | return RewritePropertyOrImplicitGetter(PseudoOp); |
| 5653 | } |
| 5654 | } else if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) { |
| 5655 | return RewriteObjCIvarRefExpr(IvarRefExpr); |
| 5656 | } |
Fariborz Jahanian | 9ffd1ae | 2013-02-08 18:57:50 +0000 | [diff] [blame] | 5657 | else if (isa<OpaqueValueExpr>(S)) |
| 5658 | S = cast<OpaqueValueExpr>(S)->getSourceExpr(); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5659 | |
| 5660 | SourceRange OrigStmtRange = S->getSourceRange(); |
| 5661 | |
| 5662 | // Perform a bottom up rewrite of all children. |
| 5663 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
| 5664 | if (*CI) { |
| 5665 | Stmt *childStmt = (*CI); |
| 5666 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(childStmt); |
| 5667 | if (newStmt) { |
| 5668 | *CI = newStmt; |
| 5669 | } |
| 5670 | } |
| 5671 | |
| 5672 | if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5673 | SmallVector<DeclRefExpr *, 8> InnerBlockDeclRefs; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5674 | llvm::SmallPtrSet<const DeclContext *, 8> InnerContexts; |
| 5675 | InnerContexts.insert(BE->getBlockDecl()); |
| 5676 | ImportedLocalExternalDecls.clear(); |
| 5677 | GetInnerBlockDeclRefExprs(BE->getBody(), |
| 5678 | InnerBlockDeclRefs, InnerContexts); |
| 5679 | // Rewrite the block body in place. |
| 5680 | Stmt *SaveCurrentBody = CurrentBody; |
| 5681 | CurrentBody = BE->getBody(); |
| 5682 | PropParentMap = 0; |
| 5683 | // block literal on rhs of a property-dot-sytax assignment |
| 5684 | // must be replaced by its synthesize ast so getRewrittenText |
| 5685 | // works as expected. In this case, what actually ends up on RHS |
| 5686 | // is the blockTranscribed which is the helper function for the |
| 5687 | // block literal; as in: self.c = ^() {[ace ARR];}; |
| 5688 | bool saveDisableReplaceStmt = DisableReplaceStmt; |
| 5689 | DisableReplaceStmt = false; |
| 5690 | RewriteFunctionBodyOrGlobalInitializer(BE->getBody()); |
| 5691 | DisableReplaceStmt = saveDisableReplaceStmt; |
| 5692 | CurrentBody = SaveCurrentBody; |
| 5693 | PropParentMap = 0; |
| 5694 | ImportedLocalExternalDecls.clear(); |
| 5695 | // Now we snarf the rewritten text and stash it away for later use. |
| 5696 | std::string Str = Rewrite.getRewrittenText(BE->getSourceRange()); |
| 5697 | RewrittenBlockExprs[BE] = Str; |
| 5698 | |
| 5699 | Stmt *blockTranscribed = SynthBlockInitExpr(BE, InnerBlockDeclRefs); |
| 5700 | |
| 5701 | //blockTranscribed->dump(); |
| 5702 | ReplaceStmt(S, blockTranscribed); |
| 5703 | return blockTranscribed; |
| 5704 | } |
| 5705 | // Handle specific things. |
| 5706 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 5707 | return RewriteAtEncode(AtEncode); |
| 5708 | |
| 5709 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 5710 | return RewriteAtSelector(AtSelector); |
| 5711 | |
| 5712 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 5713 | return RewriteObjCStringLiteral(AtString); |
Fariborz Jahanian | 5594704 | 2012-03-27 20:17:30 +0000 | [diff] [blame] | 5714 | |
| 5715 | if (ObjCBoolLiteralExpr *BoolLitExpr = dyn_cast<ObjCBoolLiteralExpr>(S)) |
| 5716 | return RewriteObjCBoolLiteralExpr(BoolLitExpr); |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 5717 | |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 5718 | if (ObjCBoxedExpr *BoxedExpr = dyn_cast<ObjCBoxedExpr>(S)) |
| 5719 | return RewriteObjCBoxedExpr(BoxedExpr); |
Fariborz Jahanian | 86cff60 | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 5720 | |
| 5721 | if (ObjCArrayLiteral *ArrayLitExpr = dyn_cast<ObjCArrayLiteral>(S)) |
| 5722 | return RewriteObjCArrayLiteralExpr(ArrayLitExpr); |
Fariborz Jahanian | e35abe1 | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 5723 | |
| 5724 | if (ObjCDictionaryLiteral *DictionaryLitExpr = |
| 5725 | dyn_cast<ObjCDictionaryLiteral>(S)) |
| 5726 | return RewriteObjCDictionaryLiteralExpr(DictionaryLitExpr); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5727 | |
| 5728 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
| 5729 | #if 0 |
| 5730 | // Before we rewrite it, put the original message expression in a comment. |
| 5731 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 5732 | SourceLocation endLoc = MessExpr->getLocEnd(); |
| 5733 | |
| 5734 | const char *startBuf = SM->getCharacterData(startLoc); |
| 5735 | const char *endBuf = SM->getCharacterData(endLoc); |
| 5736 | |
| 5737 | std::string messString; |
| 5738 | messString += "// "; |
| 5739 | messString.append(startBuf, endBuf-startBuf+1); |
| 5740 | messString += "\n"; |
| 5741 | |
| 5742 | // FIXME: Missing definition of |
| 5743 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
| 5744 | // InsertText(startLoc, messString.c_str(), messString.size()); |
| 5745 | // Tried this, but it didn't work either... |
| 5746 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
| 5747 | #endif |
| 5748 | return RewriteMessageExpr(MessExpr); |
| 5749 | } |
| 5750 | |
Fariborz Jahanian | 042b91d | 2012-05-23 23:47:20 +0000 | [diff] [blame] | 5751 | if (ObjCAutoreleasePoolStmt *StmtAutoRelease = |
| 5752 | dyn_cast<ObjCAutoreleasePoolStmt>(S)) { |
| 5753 | return RewriteObjCAutoreleasePoolStmt(StmtAutoRelease); |
| 5754 | } |
| 5755 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5756 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 5757 | return RewriteObjCTryStmt(StmtTry); |
| 5758 | |
| 5759 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 5760 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 5761 | |
| 5762 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 5763 | return RewriteObjCThrowStmt(StmtThrow); |
| 5764 | |
| 5765 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 5766 | return RewriteObjCProtocolExpr(ProtocolExp); |
| 5767 | |
| 5768 | if (ObjCForCollectionStmt *StmtForCollection = |
| 5769 | dyn_cast<ObjCForCollectionStmt>(S)) |
| 5770 | return RewriteObjCForCollectionStmt(StmtForCollection, |
| 5771 | OrigStmtRange.getEnd()); |
| 5772 | if (BreakStmt *StmtBreakStmt = |
| 5773 | dyn_cast<BreakStmt>(S)) |
| 5774 | return RewriteBreakStmt(StmtBreakStmt); |
| 5775 | if (ContinueStmt *StmtContinueStmt = |
| 5776 | dyn_cast<ContinueStmt>(S)) |
| 5777 | return RewriteContinueStmt(StmtContinueStmt); |
| 5778 | |
| 5779 | // Need to check for protocol refs (id <P>, Foo <P> *) in variable decls |
| 5780 | // and cast exprs. |
| 5781 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
| 5782 | // FIXME: What we're doing here is modifying the type-specifier that |
| 5783 | // precedes the first Decl. In the future the DeclGroup should have |
| 5784 | // a separate type-specifier that we can rewrite. |
| 5785 | // NOTE: We need to avoid rewriting the DeclStmt if it is within |
| 5786 | // the context of an ObjCForCollectionStmt. For example: |
| 5787 | // NSArray *someArray; |
| 5788 | // for (id <FooProtocol> index in someArray) ; |
| 5789 | // This is because RewriteObjCForCollectionStmt() does textual rewriting |
| 5790 | // and it depends on the original text locations/positions. |
| 5791 | if (Stmts.empty() || !IsDeclStmtInForeachHeader(DS)) |
| 5792 | RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin()); |
| 5793 | |
| 5794 | // Blocks rewrite rules. |
| 5795 | for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); |
| 5796 | DI != DE; ++DI) { |
| 5797 | Decl *SD = *DI; |
| 5798 | if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { |
| 5799 | if (isTopLevelBlockPointerType(ND->getType())) |
| 5800 | RewriteBlockPointerDecl(ND); |
| 5801 | else if (ND->getType()->isFunctionPointerType()) |
| 5802 | CheckFunctionPointerDecl(ND->getType(), ND); |
| 5803 | if (VarDecl *VD = dyn_cast<VarDecl>(SD)) { |
| 5804 | if (VD->hasAttr<BlocksAttr>()) { |
| 5805 | static unsigned uniqueByrefDeclCount = 0; |
| 5806 | assert(!BlockByRefDeclNo.count(ND) && |
| 5807 | "RewriteFunctionBodyOrGlobalInitializer: Duplicate byref decl"); |
| 5808 | BlockByRefDeclNo[ND] = uniqueByrefDeclCount++; |
Fariborz Jahanian | 4fe261c | 2012-04-24 19:38:45 +0000 | [diff] [blame] | 5809 | RewriteByRefVar(VD, (DI == DS->decl_begin()), ((DI+1) == DE)); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5810 | } |
| 5811 | else |
| 5812 | RewriteTypeOfDecl(VD); |
| 5813 | } |
| 5814 | } |
| 5815 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) { |
| 5816 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
| 5817 | RewriteBlockPointerDecl(TD); |
| 5818 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
| 5819 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 5820 | } |
| 5821 | } |
| 5822 | } |
| 5823 | |
| 5824 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) |
| 5825 | RewriteObjCQualifiedInterfaceTypes(CE); |
| 5826 | |
| 5827 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
| 5828 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 5829 | assert(!Stmts.empty() && "Statement stack is empty"); |
| 5830 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 5831 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
| 5832 | && "Statement stack mismatch"); |
| 5833 | Stmts.pop_back(); |
| 5834 | } |
| 5835 | // Handle blocks rewriting. |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5836 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 5837 | ValueDecl *VD = DRE->getDecl(); |
| 5838 | if (VD->hasAttr<BlocksAttr>()) |
| 5839 | return RewriteBlockDeclRefExpr(DRE); |
| 5840 | if (HasLocalVariableExternalStorage(VD)) |
| 5841 | return RewriteLocalVariableExternalStorage(DRE); |
| 5842 | } |
| 5843 | |
| 5844 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
| 5845 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
| 5846 | Stmt *BlockCall = SynthesizeBlockCall(CE, CE->getCallee()); |
| 5847 | ReplaceStmt(S, BlockCall); |
| 5848 | return BlockCall; |
| 5849 | } |
| 5850 | } |
| 5851 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) { |
| 5852 | RewriteCastExpr(CE); |
| 5853 | } |
Fariborz Jahanian | f1ee687 | 2012-04-10 00:08:18 +0000 | [diff] [blame] | 5854 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
| 5855 | RewriteImplicitCastObjCExpr(ICE); |
| 5856 | } |
Fariborz Jahanian | 43aa1c3 | 2012-04-16 22:14:01 +0000 | [diff] [blame] | 5857 | #if 0 |
Fariborz Jahanian | 653b7cf | 2012-04-13 18:00:54 +0000 | [diff] [blame] | 5858 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5859 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
| 5860 | CastExpr *Replacement = new (Context) CastExpr(ICE->getType(), |
| 5861 | ICE->getSubExpr(), |
| 5862 | SourceLocation()); |
| 5863 | // Get the new text. |
| 5864 | std::string SStr; |
| 5865 | llvm::raw_string_ostream Buf(SStr); |
Richard Smith | d1420c6 | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 5866 | Replacement->printPretty(Buf); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5867 | const std::string &Str = Buf.str(); |
| 5868 | |
| 5869 | printf("CAST = %s\n", &Str[0]); |
| 5870 | InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 5871 | delete S; |
| 5872 | return Replacement; |
| 5873 | } |
| 5874 | #endif |
| 5875 | // Return this stmt unmodified. |
| 5876 | return S; |
| 5877 | } |
| 5878 | |
| 5879 | void RewriteModernObjC::RewriteRecordBody(RecordDecl *RD) { |
| 5880 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 5881 | e = RD->field_end(); i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5882 | FieldDecl *FD = *i; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5883 | if (isTopLevelBlockPointerType(FD->getType())) |
| 5884 | RewriteBlockPointerDecl(FD); |
| 5885 | if (FD->getType()->isObjCQualifiedIdType() || |
| 5886 | FD->getType()->isObjCQualifiedInterfaceType()) |
| 5887 | RewriteObjCQualifiedInterfaceTypes(FD); |
| 5888 | } |
| 5889 | } |
| 5890 | |
| 5891 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 5892 | /// main file of the input. |
| 5893 | void RewriteModernObjC::HandleDeclInMainFile(Decl *D) { |
| 5894 | switch (D->getKind()) { |
| 5895 | case Decl::Function: { |
| 5896 | FunctionDecl *FD = cast<FunctionDecl>(D); |
| 5897 | if (FD->isOverloadedOperator()) |
| 5898 | return; |
| 5899 | |
| 5900 | // Since function prototypes don't have ParmDecl's, we check the function |
| 5901 | // prototype. This enables us to rewrite function declarations and |
| 5902 | // definitions using the same code. |
| 5903 | RewriteBlocksInFunctionProtoType(FD->getType(), FD); |
| 5904 | |
Argyrios Kyrtzidis | 9335df3 | 2012-02-12 04:48:45 +0000 | [diff] [blame] | 5905 | if (!FD->isThisDeclarationADefinition()) |
| 5906 | break; |
| 5907 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5908 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
| 5909 | if (CompoundStmt *Body = dyn_cast_or_null<CompoundStmt>(FD->getBody())) { |
| 5910 | CurFunctionDef = FD; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5911 | CurrentBody = Body; |
| 5912 | Body = |
| 5913 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 5914 | FD->setBody(Body); |
| 5915 | CurrentBody = 0; |
| 5916 | if (PropParentMap) { |
| 5917 | delete PropParentMap; |
| 5918 | PropParentMap = 0; |
| 5919 | } |
| 5920 | // This synthesizes and inserts the block "impl" struct, invoke function, |
| 5921 | // and any copy/dispose helper functions. |
| 5922 | InsertBlockLiteralsWithinFunction(FD); |
Fariborz Jahanian | 9620596 | 2012-11-06 17:30:23 +0000 | [diff] [blame] | 5923 | RewriteLineDirective(D); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5924 | CurFunctionDef = 0; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5925 | } |
| 5926 | break; |
| 5927 | } |
| 5928 | case Decl::ObjCMethod: { |
| 5929 | ObjCMethodDecl *MD = cast<ObjCMethodDecl>(D); |
| 5930 | if (CompoundStmt *Body = MD->getCompoundBody()) { |
| 5931 | CurMethodDef = MD; |
| 5932 | CurrentBody = Body; |
| 5933 | Body = |
| 5934 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 5935 | MD->setBody(Body); |
| 5936 | CurrentBody = 0; |
| 5937 | if (PropParentMap) { |
| 5938 | delete PropParentMap; |
| 5939 | PropParentMap = 0; |
| 5940 | } |
| 5941 | InsertBlockLiteralsWithinMethod(MD); |
Fariborz Jahanian | 9620596 | 2012-11-06 17:30:23 +0000 | [diff] [blame] | 5942 | RewriteLineDirective(D); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5943 | CurMethodDef = 0; |
| 5944 | } |
| 5945 | break; |
| 5946 | } |
| 5947 | case Decl::ObjCImplementation: { |
| 5948 | ObjCImplementationDecl *CI = cast<ObjCImplementationDecl>(D); |
| 5949 | ClassImplementation.push_back(CI); |
| 5950 | break; |
| 5951 | } |
| 5952 | case Decl::ObjCCategoryImpl: { |
| 5953 | ObjCCategoryImplDecl *CI = cast<ObjCCategoryImplDecl>(D); |
| 5954 | CategoryImplementation.push_back(CI); |
| 5955 | break; |
| 5956 | } |
| 5957 | case Decl::Var: { |
| 5958 | VarDecl *VD = cast<VarDecl>(D); |
| 5959 | RewriteObjCQualifiedInterfaceTypes(VD); |
| 5960 | if (isTopLevelBlockPointerType(VD->getType())) |
| 5961 | RewriteBlockPointerDecl(VD); |
| 5962 | else if (VD->getType()->isFunctionPointerType()) { |
| 5963 | CheckFunctionPointerDecl(VD->getType(), VD); |
| 5964 | if (VD->getInit()) { |
| 5965 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
| 5966 | RewriteCastExpr(CE); |
| 5967 | } |
| 5968 | } |
| 5969 | } else if (VD->getType()->isRecordType()) { |
| 5970 | RecordDecl *RD = VD->getType()->getAs<RecordType>()->getDecl(); |
| 5971 | if (RD->isCompleteDefinition()) |
| 5972 | RewriteRecordBody(RD); |
| 5973 | } |
| 5974 | if (VD->getInit()) { |
| 5975 | GlobalVarDecl = VD; |
| 5976 | CurrentBody = VD->getInit(); |
| 5977 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
| 5978 | CurrentBody = 0; |
| 5979 | if (PropParentMap) { |
| 5980 | delete PropParentMap; |
| 5981 | PropParentMap = 0; |
| 5982 | } |
| 5983 | SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), VD->getName()); |
| 5984 | GlobalVarDecl = 0; |
| 5985 | |
| 5986 | // This is needed for blocks. |
| 5987 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
| 5988 | RewriteCastExpr(CE); |
| 5989 | } |
| 5990 | } |
| 5991 | break; |
| 5992 | } |
| 5993 | case Decl::TypeAlias: |
| 5994 | case Decl::Typedef: { |
| 5995 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
| 5996 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
| 5997 | RewriteBlockPointerDecl(TD); |
| 5998 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
| 5999 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 6000 | } |
| 6001 | break; |
| 6002 | } |
| 6003 | case Decl::CXXRecord: |
| 6004 | case Decl::Record: { |
| 6005 | RecordDecl *RD = cast<RecordDecl>(D); |
| 6006 | if (RD->isCompleteDefinition()) |
| 6007 | RewriteRecordBody(RD); |
| 6008 | break; |
| 6009 | } |
| 6010 | default: |
| 6011 | break; |
| 6012 | } |
| 6013 | // Nothing yet. |
| 6014 | } |
| 6015 | |
Fariborz Jahanian | 30650eb | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 6016 | /// Write_ProtocolExprReferencedMetadata - This routine writer out the |
| 6017 | /// protocol reference symbols in the for of: |
| 6018 | /// struct _protocol_t *PROTOCOL_REF = &PROTOCOL_METADATA. |
| 6019 | static void Write_ProtocolExprReferencedMetadata(ASTContext *Context, |
| 6020 | ObjCProtocolDecl *PDecl, |
| 6021 | std::string &Result) { |
| 6022 | // Also output .objc_protorefs$B section and its meta-data. |
| 6023 | if (Context->getLangOpts().MicrosoftExt) |
Fariborz Jahanian | bd78cfa | 2012-04-27 21:39:49 +0000 | [diff] [blame] | 6024 | Result += "static "; |
Fariborz Jahanian | 30650eb | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 6025 | Result += "struct _protocol_t *"; |
| 6026 | Result += "_OBJC_PROTOCOL_REFERENCE_$_"; |
| 6027 | Result += PDecl->getNameAsString(); |
| 6028 | Result += " = &"; |
| 6029 | Result += "_OBJC_PROTOCOL_"; Result += PDecl->getNameAsString(); |
| 6030 | Result += ";\n"; |
| 6031 | } |
| 6032 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6033 | void RewriteModernObjC::HandleTranslationUnit(ASTContext &C) { |
| 6034 | if (Diags.hasErrorOccurred()) |
| 6035 | return; |
| 6036 | |
| 6037 | RewriteInclude(); |
| 6038 | |
Fariborz Jahanian | 31c4a4b | 2013-02-07 22:50:40 +0000 | [diff] [blame] | 6039 | for (unsigned i = 0, e = FunctionDefinitionsSeen.size(); i < e; i++) { |
| 6040 | // translation of function bodies were postponed untill all class and |
| 6041 | // their extensions and implementations are seen. This is because, we |
| 6042 | // cannot build grouping structs for bitfields untill they are all seen. |
| 6043 | FunctionDecl *FDecl = FunctionDefinitionsSeen[i]; |
| 6044 | HandleTopLevelSingleDecl(FDecl); |
| 6045 | } |
| 6046 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6047 | // Here's a great place to add any extra declarations that may be needed. |
| 6048 | // Write out meta data for each @protocol(<expr>). |
| 6049 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Fariborz Jahanian | 30650eb | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 6050 | E = ProtocolExprDecls.end(); I != E; ++I) { |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 6051 | RewriteObjCProtocolMetaData(*I, Preamble); |
Fariborz Jahanian | 30650eb | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 6052 | Write_ProtocolExprReferencedMetadata(Context, (*I), Preamble); |
| 6053 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6054 | |
| 6055 | InsertText(SM->getLocForStartOfFile(MainFileID), Preamble, false); |
Fariborz Jahanian | 163d3ce | 2012-05-08 23:54:35 +0000 | [diff] [blame] | 6056 | |
| 6057 | if (ClassImplementation.size() || CategoryImplementation.size()) |
| 6058 | RewriteImplementations(); |
| 6059 | |
Fariborz Jahanian | 5731778 | 2012-02-21 23:58:41 +0000 | [diff] [blame] | 6060 | for (unsigned i = 0, e = ObjCInterfacesSeen.size(); i < e; i++) { |
| 6061 | ObjCInterfaceDecl *CDecl = ObjCInterfacesSeen[i]; |
| 6062 | // Write struct declaration for the class matching its ivar declarations. |
| 6063 | // Note that for modern abi, this is postponed until the end of TU |
| 6064 | // because class extensions and the implementation might declare their own |
| 6065 | // private ivars. |
| 6066 | RewriteInterfaceDecl(CDecl); |
| 6067 | } |
Fariborz Jahanian | 31c4a4b | 2013-02-07 22:50:40 +0000 | [diff] [blame] | 6068 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6069 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 6070 | // we are done. |
| 6071 | if (const RewriteBuffer *RewriteBuf = |
| 6072 | Rewrite.getRewriteBufferFor(MainFileID)) { |
| 6073 | //printf("Changed:\n"); |
| 6074 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 6075 | } else { |
| 6076 | llvm::errs() << "No changes\n"; |
| 6077 | } |
| 6078 | |
| 6079 | if (ClassImplementation.size() || CategoryImplementation.size() || |
| 6080 | ProtocolExprDecls.size()) { |
| 6081 | // Rewrite Objective-c meta data* |
| 6082 | std::string ResultStr; |
| 6083 | RewriteMetaDataIntoBuffer(ResultStr); |
| 6084 | // Emit metadata. |
| 6085 | *OutFile << ResultStr; |
| 6086 | } |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 6087 | // Emit ImageInfo; |
| 6088 | { |
| 6089 | std::string ResultStr; |
| 6090 | WriteImageInfo(ResultStr); |
| 6091 | *OutFile << ResultStr; |
| 6092 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6093 | OutFile->flush(); |
| 6094 | } |
| 6095 | |
| 6096 | void RewriteModernObjC::Initialize(ASTContext &context) { |
| 6097 | InitializeCommon(context); |
| 6098 | |
Fariborz Jahanian | 6991bc5 | 2012-03-10 17:45:38 +0000 | [diff] [blame] | 6099 | Preamble += "#ifndef __OBJC2__\n"; |
| 6100 | Preamble += "#define __OBJC2__\n"; |
| 6101 | Preamble += "#endif\n"; |
| 6102 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6103 | // declaring objc_selector outside the parameter list removes a silly |
| 6104 | // scope related warning... |
| 6105 | if (IsHeader) |
| 6106 | Preamble = "#pragma once\n"; |
| 6107 | Preamble += "struct objc_selector; struct objc_class;\n"; |
Fariborz Jahanian | e2d87bc | 2012-04-12 23:52:52 +0000 | [diff] [blame] | 6108 | Preamble += "struct __rw_objc_super { \n\tstruct objc_object *object; "; |
| 6109 | Preamble += "\n\tstruct objc_object *superClass; "; |
| 6110 | // Add a constructor for creating temporary objects. |
| 6111 | Preamble += "\n\t__rw_objc_super(struct objc_object *o, struct objc_object *s) "; |
| 6112 | Preamble += ": object(o), superClass(s) {} "; |
| 6113 | Preamble += "\n};\n"; |
| 6114 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6115 | if (LangOpts.MicrosoftExt) { |
Fariborz Jahanian | 1ca052c | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 6116 | // Define all sections using syntax that makes sense. |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 6117 | // These are currently generated. |
| 6118 | Preamble += "\n#pragma section(\".objc_classlist$B\", long, read, write)\n"; |
Fariborz Jahanian | 1ca052c | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 6119 | Preamble += "#pragma section(\".objc_catlist$B\", long, read, write)\n"; |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 6120 | Preamble += "#pragma section(\".objc_imageinfo$B\", long, read, write)\n"; |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 6121 | Preamble += "#pragma section(\".objc_nlclslist$B\", long, read, write)\n"; |
| 6122 | Preamble += "#pragma section(\".objc_nlcatlist$B\", long, read, write)\n"; |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 6123 | // These are generated but not necessary for functionality. |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 6124 | Preamble += "#pragma section(\".cat_cls_meth$B\", long, read, write)\n"; |
Fariborz Jahanian | 1ca052c | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 6125 | Preamble += "#pragma section(\".inst_meth$B\", long, read, write)\n"; |
| 6126 | Preamble += "#pragma section(\".cls_meth$B\", long, read, write)\n"; |
Fariborz Jahanian | 40a777a | 2012-03-12 16:46:58 +0000 | [diff] [blame] | 6127 | Preamble += "#pragma section(\".objc_ivar$B\", long, read, write)\n"; |
Fariborz Jahanian | de5d946 | 2012-03-14 18:09:23 +0000 | [diff] [blame] | 6128 | |
Fariborz Jahanian | 30650eb | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 6129 | // These need be generated for performance. Currently they are not, |
| 6130 | // using API calls instead. |
| 6131 | Preamble += "#pragma section(\".objc_selrefs$B\", long, read, write)\n"; |
| 6132 | Preamble += "#pragma section(\".objc_classrefs$B\", long, read, write)\n"; |
| 6133 | Preamble += "#pragma section(\".objc_superrefs$B\", long, read, write)\n"; |
| 6134 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6135 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6136 | Preamble += "#ifndef _REWRITER_typedef_Protocol\n"; |
| 6137 | Preamble += "typedef struct objc_object Protocol;\n"; |
| 6138 | Preamble += "#define _REWRITER_typedef_Protocol\n"; |
| 6139 | Preamble += "#endif\n"; |
| 6140 | if (LangOpts.MicrosoftExt) { |
| 6141 | Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n"; |
| 6142 | Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n"; |
Fariborz Jahanian | 5cf6b6c | 2012-03-21 23:41:04 +0000 | [diff] [blame] | 6143 | } |
| 6144 | else |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6145 | Preamble += "#define __OBJC_RW_DLLIMPORT extern\n"; |
Fariborz Jahanian | 5cf6b6c | 2012-03-21 23:41:04 +0000 | [diff] [blame] | 6146 | |
| 6147 | Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSend(void);\n"; |
| 6148 | Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSendSuper(void);\n"; |
| 6149 | Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSend_stret(void);\n"; |
| 6150 | Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSendSuper_stret(void);\n"; |
| 6151 | Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSend_fpret(void);\n"; |
| 6152 | |
Fariborz Jahanian | 20e181a | 2012-05-08 20:55:55 +0000 | [diff] [blame] | 6153 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_class *objc_getClass"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6154 | Preamble += "(const char *);\n"; |
| 6155 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_class *class_getSuperclass"; |
| 6156 | Preamble += "(struct objc_class *);\n"; |
Fariborz Jahanian | 20e181a | 2012-05-08 20:55:55 +0000 | [diff] [blame] | 6157 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_class *objc_getMetaClass"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6158 | Preamble += "(const char *);\n"; |
Fariborz Jahanian | 55261af | 2012-03-19 18:11:32 +0000 | [diff] [blame] | 6159 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw( struct objc_object *);\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6160 | // @synchronized hooks. |
Aaron Ballman | 2d234d73 | 2012-09-06 16:44:16 +0000 | [diff] [blame] | 6161 | Preamble += "__OBJC_RW_DLLIMPORT int objc_sync_enter( struct objc_object *);\n"; |
| 6162 | Preamble += "__OBJC_RW_DLLIMPORT int objc_sync_exit( struct objc_object *);\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6163 | Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n"; |
| 6164 | Preamble += "#ifndef __FASTENUMERATIONSTATE\n"; |
| 6165 | Preamble += "struct __objcFastEnumerationState {\n\t"; |
| 6166 | Preamble += "unsigned long state;\n\t"; |
| 6167 | Preamble += "void **itemsPtr;\n\t"; |
| 6168 | Preamble += "unsigned long *mutationsPtr;\n\t"; |
| 6169 | Preamble += "unsigned long extra[5];\n};\n"; |
| 6170 | Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n"; |
| 6171 | Preamble += "#define __FASTENUMERATIONSTATE\n"; |
| 6172 | Preamble += "#endif\n"; |
| 6173 | Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n"; |
| 6174 | Preamble += "struct __NSConstantStringImpl {\n"; |
| 6175 | Preamble += " int *isa;\n"; |
| 6176 | Preamble += " int flags;\n"; |
| 6177 | Preamble += " char *str;\n"; |
| 6178 | Preamble += " long length;\n"; |
| 6179 | Preamble += "};\n"; |
| 6180 | Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n"; |
| 6181 | Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n"; |
| 6182 | Preamble += "#else\n"; |
| 6183 | Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n"; |
| 6184 | Preamble += "#endif\n"; |
| 6185 | Preamble += "#define __NSCONSTANTSTRINGIMPL\n"; |
| 6186 | Preamble += "#endif\n"; |
| 6187 | // Blocks preamble. |
| 6188 | Preamble += "#ifndef BLOCK_IMPL\n"; |
| 6189 | Preamble += "#define BLOCK_IMPL\n"; |
| 6190 | Preamble += "struct __block_impl {\n"; |
| 6191 | Preamble += " void *isa;\n"; |
| 6192 | Preamble += " int Flags;\n"; |
| 6193 | Preamble += " int Reserved;\n"; |
| 6194 | Preamble += " void *FuncPtr;\n"; |
| 6195 | Preamble += "};\n"; |
| 6196 | Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n"; |
| 6197 | Preamble += "#ifdef __OBJC_EXPORT_BLOCKS\n"; |
| 6198 | Preamble += "extern \"C\" __declspec(dllexport) " |
| 6199 | "void _Block_object_assign(void *, const void *, const int);\n"; |
| 6200 | Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_dispose(const void *, const int);\n"; |
| 6201 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteGlobalBlock[32];\n"; |
| 6202 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteStackBlock[32];\n"; |
| 6203 | Preamble += "#else\n"; |
| 6204 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_assign(void *, const void *, const int);\n"; |
| 6205 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_dispose(const void *, const int);\n"; |
| 6206 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteGlobalBlock[32];\n"; |
| 6207 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteStackBlock[32];\n"; |
| 6208 | Preamble += "#endif\n"; |
| 6209 | Preamble += "#endif\n"; |
| 6210 | if (LangOpts.MicrosoftExt) { |
| 6211 | Preamble += "#undef __OBJC_RW_DLLIMPORT\n"; |
| 6212 | Preamble += "#undef __OBJC_RW_STATICIMPORT\n"; |
| 6213 | Preamble += "#ifndef KEEP_ATTRIBUTES\n"; // We use this for clang tests. |
| 6214 | Preamble += "#define __attribute__(X)\n"; |
| 6215 | Preamble += "#endif\n"; |
Fariborz Jahanian | 5ce2827 | 2012-04-12 16:33:31 +0000 | [diff] [blame] | 6216 | Preamble += "#ifndef __weak\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6217 | Preamble += "#define __weak\n"; |
Fariborz Jahanian | 5ce2827 | 2012-04-12 16:33:31 +0000 | [diff] [blame] | 6218 | Preamble += "#endif\n"; |
| 6219 | Preamble += "#ifndef __block\n"; |
| 6220 | Preamble += "#define __block\n"; |
| 6221 | Preamble += "#endif\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6222 | } |
| 6223 | else { |
| 6224 | Preamble += "#define __block\n"; |
| 6225 | Preamble += "#define __weak\n"; |
| 6226 | } |
Fariborz Jahanian | be8d55c | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 6227 | |
Fariborz Jahanian | b0f245c | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 6228 | // Declarations required for modern objective-c array and dictionary literals. |
| 6229 | Preamble += "\n#include <stdarg.h>\n"; |
Fariborz Jahanian | e35abe1 | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 6230 | Preamble += "struct __NSContainer_literal {\n"; |
Fariborz Jahanian | b0f245c | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 6231 | Preamble += " void * *arr;\n"; |
Fariborz Jahanian | e35abe1 | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 6232 | Preamble += " __NSContainer_literal (unsigned int count, ...) {\n"; |
Fariborz Jahanian | b0f245c | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 6233 | Preamble += "\tva_list marker;\n"; |
| 6234 | Preamble += "\tva_start(marker, count);\n"; |
| 6235 | Preamble += "\tarr = new void *[count];\n"; |
| 6236 | Preamble += "\tfor (unsigned i = 0; i < count; i++)\n"; |
| 6237 | Preamble += "\t arr[i] = va_arg(marker, void *);\n"; |
| 6238 | Preamble += "\tva_end( marker );\n"; |
| 6239 | Preamble += " };\n"; |
Fariborz Jahanian | 13a9c02 | 2012-05-02 23:53:46 +0000 | [diff] [blame] | 6240 | Preamble += " ~__NSContainer_literal() {\n"; |
Fariborz Jahanian | b0f245c | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 6241 | Preamble += "\tdelete[] arr;\n"; |
| 6242 | Preamble += " }\n"; |
| 6243 | Preamble += "};\n"; |
| 6244 | |
Fariborz Jahanian | 042b91d | 2012-05-23 23:47:20 +0000 | [diff] [blame] | 6245 | // Declaration required for implementation of @autoreleasepool statement. |
| 6246 | Preamble += "extern \"C\" __declspec(dllimport) void * objc_autoreleasePoolPush(void);\n"; |
| 6247 | Preamble += "extern \"C\" __declspec(dllimport) void objc_autoreleasePoolPop(void *);\n\n"; |
| 6248 | Preamble += "struct __AtAutoreleasePool {\n"; |
| 6249 | Preamble += " __AtAutoreleasePool() {atautoreleasepoolobj = objc_autoreleasePoolPush();}\n"; |
| 6250 | Preamble += " ~__AtAutoreleasePool() {objc_autoreleasePoolPop(atautoreleasepoolobj);}\n"; |
| 6251 | Preamble += " void * atautoreleasepoolobj;\n"; |
| 6252 | Preamble += "};\n"; |
| 6253 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6254 | // NOTE! Windows uses LLP64 for 64bit mode. So, cast pointer to long long |
| 6255 | // as this avoids warning in any 64bit/32bit compilation model. |
| 6256 | Preamble += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((long long) &((TYPE *)0)->MEMBER)\n"; |
| 6257 | } |
| 6258 | |
| 6259 | /// RewriteIvarOffsetComputation - This rutine synthesizes computation of |
| 6260 | /// ivar offset. |
| 6261 | void RewriteModernObjC::RewriteIvarOffsetComputation(ObjCIvarDecl *ivar, |
| 6262 | std::string &Result) { |
Fariborz Jahanian | cd3b036 | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 6263 | Result += "__OFFSETOFIVAR__(struct "; |
| 6264 | Result += ivar->getContainingInterface()->getNameAsString(); |
| 6265 | if (LangOpts.MicrosoftExt) |
| 6266 | Result += "_IMPL"; |
| 6267 | Result += ", "; |
| 6268 | if (ivar->isBitField()) |
| 6269 | ObjCIvarBitfieldGroupDecl(ivar, Result); |
| 6270 | else |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6271 | Result += ivar->getNameAsString(); |
Fariborz Jahanian | cd3b036 | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 6272 | Result += ")"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6273 | } |
| 6274 | |
| 6275 | /// WriteModernMetadataDeclarations - Writes out metadata declarations for modern ABI. |
| 6276 | /// struct _prop_t { |
Fariborz Jahanian | da35eac | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 6277 | /// const char *name; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6278 | /// char *attributes; |
| 6279 | /// } |
| 6280 | |
| 6281 | /// struct _prop_list_t { |
| 6282 | /// uint32_t entsize; // sizeof(struct _prop_t) |
| 6283 | /// uint32_t count_of_properties; |
| 6284 | /// struct _prop_t prop_list[count_of_properties]; |
| 6285 | /// } |
| 6286 | |
| 6287 | /// struct _protocol_t; |
| 6288 | |
| 6289 | /// struct _protocol_list_t { |
| 6290 | /// long protocol_count; // Note, this is 32/64 bit |
| 6291 | /// struct _protocol_t * protocol_list[protocol_count]; |
| 6292 | /// } |
| 6293 | |
| 6294 | /// struct _objc_method { |
| 6295 | /// SEL _cmd; |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6296 | /// const char *method_type; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6297 | /// char *_imp; |
| 6298 | /// } |
| 6299 | |
| 6300 | /// struct _method_list_t { |
| 6301 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 6302 | /// uint32_t method_count; |
| 6303 | /// struct _objc_method method_list[method_count]; |
| 6304 | /// } |
| 6305 | |
| 6306 | /// struct _protocol_t { |
| 6307 | /// id isa; // NULL |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 6308 | /// const char *protocol_name; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6309 | /// const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 6310 | /// const struct method_list_t *instance_methods; |
| 6311 | /// const struct method_list_t *class_methods; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6312 | /// const struct method_list_t *optionalInstanceMethods; |
| 6313 | /// const struct method_list_t *optionalClassMethods; |
| 6314 | /// const struct _prop_list_t * properties; |
| 6315 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 6316 | /// const uint32_t flags; // = 0 |
| 6317 | /// const char ** extendedMethodTypes; |
| 6318 | /// } |
| 6319 | |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6320 | /// struct _ivar_t { |
| 6321 | /// unsigned long int *offset; // pointer to ivar offset location |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6322 | /// const char *name; |
| 6323 | /// const char *type; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6324 | /// uint32_t alignment; |
| 6325 | /// uint32_t size; |
| 6326 | /// } |
| 6327 | |
| 6328 | /// struct _ivar_list_t { |
| 6329 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 6330 | /// uint32 count; |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6331 | /// struct _ivar_t list[count]; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6332 | /// } |
| 6333 | |
| 6334 | /// struct _class_ro_t { |
Fariborz Jahanian | 249cd10 | 2012-03-24 16:53:16 +0000 | [diff] [blame] | 6335 | /// uint32_t flags; |
| 6336 | /// uint32_t instanceStart; |
| 6337 | /// uint32_t instanceSize; |
| 6338 | /// uint32_t reserved; // only when building for 64bit targets |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 6339 | /// const uint8_t *ivarLayout; |
| 6340 | /// const char *name; |
| 6341 | /// const struct _method_list_t *baseMethods; |
| 6342 | /// const struct _protocol_list_t *baseProtocols; |
| 6343 | /// const struct _ivar_list_t *ivars; |
| 6344 | /// const uint8_t *weakIvarLayout; |
| 6345 | /// const struct _prop_list_t *properties; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6346 | /// } |
| 6347 | |
| 6348 | /// struct _class_t { |
| 6349 | /// struct _class_t *isa; |
Fariborz Jahanian | fd4ce2c | 2012-03-20 17:34:50 +0000 | [diff] [blame] | 6350 | /// struct _class_t *superclass; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6351 | /// void *cache; |
| 6352 | /// IMP *vtable; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6353 | /// struct _class_ro_t *ro; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6354 | /// } |
| 6355 | |
| 6356 | /// struct _category_t { |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 6357 | /// const char *name; |
Fariborz Jahanian | 4b2fe6e | 2012-03-20 21:41:28 +0000 | [diff] [blame] | 6358 | /// struct _class_t *cls; |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 6359 | /// const struct _method_list_t *instance_methods; |
| 6360 | /// const struct _method_list_t *class_methods; |
| 6361 | /// const struct _protocol_list_t *protocols; |
| 6362 | /// const struct _prop_list_t *properties; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6363 | /// } |
| 6364 | |
| 6365 | /// MessageRefTy - LLVM for: |
| 6366 | /// struct _message_ref_t { |
| 6367 | /// IMP messenger; |
| 6368 | /// SEL name; |
| 6369 | /// }; |
| 6370 | |
| 6371 | /// SuperMessageRefTy - LLVM for: |
| 6372 | /// struct _super_message_ref_t { |
| 6373 | /// SUPER_IMP messenger; |
| 6374 | /// SEL name; |
| 6375 | /// }; |
| 6376 | |
Fariborz Jahanian | de5d946 | 2012-03-14 18:09:23 +0000 | [diff] [blame] | 6377 | static void WriteModernMetadataDeclarations(ASTContext *Context, std::string &Result) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6378 | static bool meta_data_declared = false; |
| 6379 | if (meta_data_declared) |
| 6380 | return; |
| 6381 | |
| 6382 | Result += "\nstruct _prop_t {\n"; |
Fariborz Jahanian | da35eac | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 6383 | Result += "\tconst char *name;\n"; |
| 6384 | Result += "\tconst char *attributes;\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6385 | Result += "};\n"; |
| 6386 | |
| 6387 | Result += "\nstruct _protocol_t;\n"; |
| 6388 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6389 | Result += "\nstruct _objc_method {\n"; |
| 6390 | Result += "\tstruct objc_selector * _cmd;\n"; |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6391 | Result += "\tconst char *method_type;\n"; |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6392 | Result += "\tvoid *_imp;\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6393 | Result += "};\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6394 | |
| 6395 | Result += "\nstruct _protocol_t {\n"; |
| 6396 | Result += "\tvoid * isa; // NULL\n"; |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 6397 | Result += "\tconst char *protocol_name;\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6398 | Result += "\tconst struct _protocol_list_t * protocol_list; // super protocols\n"; |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 6399 | Result += "\tconst struct method_list_t *instance_methods;\n"; |
| 6400 | Result += "\tconst struct method_list_t *class_methods;\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6401 | Result += "\tconst struct method_list_t *optionalInstanceMethods;\n"; |
| 6402 | Result += "\tconst struct method_list_t *optionalClassMethods;\n"; |
| 6403 | Result += "\tconst struct _prop_list_t * properties;\n"; |
| 6404 | Result += "\tconst unsigned int size; // sizeof(struct _protocol_t)\n"; |
| 6405 | Result += "\tconst unsigned int flags; // = 0\n"; |
| 6406 | Result += "\tconst char ** extendedMethodTypes;\n"; |
| 6407 | Result += "};\n"; |
| 6408 | |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6409 | Result += "\nstruct _ivar_t {\n"; |
| 6410 | Result += "\tunsigned long int *offset; // pointer to ivar offset location\n"; |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6411 | Result += "\tconst char *name;\n"; |
| 6412 | Result += "\tconst char *type;\n"; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6413 | Result += "\tunsigned int alignment;\n"; |
| 6414 | Result += "\tunsigned int size;\n"; |
| 6415 | Result += "};\n"; |
| 6416 | |
| 6417 | Result += "\nstruct _class_ro_t {\n"; |
Fariborz Jahanian | 249cd10 | 2012-03-24 16:53:16 +0000 | [diff] [blame] | 6418 | Result += "\tunsigned int flags;\n"; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6419 | Result += "\tunsigned int instanceStart;\n"; |
Fariborz Jahanian | 249cd10 | 2012-03-24 16:53:16 +0000 | [diff] [blame] | 6420 | Result += "\tunsigned int instanceSize;\n"; |
Fariborz Jahanian | de5d946 | 2012-03-14 18:09:23 +0000 | [diff] [blame] | 6421 | const llvm::Triple &Triple(Context->getTargetInfo().getTriple()); |
| 6422 | if (Triple.getArch() == llvm::Triple::x86_64) |
Fariborz Jahanian | 249cd10 | 2012-03-24 16:53:16 +0000 | [diff] [blame] | 6423 | Result += "\tunsigned int reserved;\n"; |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 6424 | Result += "\tconst unsigned char *ivarLayout;\n"; |
| 6425 | Result += "\tconst char *name;\n"; |
| 6426 | Result += "\tconst struct _method_list_t *baseMethods;\n"; |
| 6427 | Result += "\tconst struct _objc_protocol_list *baseProtocols;\n"; |
| 6428 | Result += "\tconst struct _ivar_list_t *ivars;\n"; |
| 6429 | Result += "\tconst unsigned char *weakIvarLayout;\n"; |
| 6430 | Result += "\tconst struct _prop_list_t *properties;\n"; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6431 | Result += "};\n"; |
| 6432 | |
| 6433 | Result += "\nstruct _class_t {\n"; |
| 6434 | Result += "\tstruct _class_t *isa;\n"; |
Fariborz Jahanian | fd4ce2c | 2012-03-20 17:34:50 +0000 | [diff] [blame] | 6435 | Result += "\tstruct _class_t *superclass;\n"; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6436 | Result += "\tvoid *cache;\n"; |
| 6437 | Result += "\tvoid *vtable;\n"; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6438 | Result += "\tstruct _class_ro_t *ro;\n"; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6439 | Result += "};\n"; |
| 6440 | |
| 6441 | Result += "\nstruct _category_t {\n"; |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 6442 | Result += "\tconst char *name;\n"; |
Fariborz Jahanian | 4b2fe6e | 2012-03-20 21:41:28 +0000 | [diff] [blame] | 6443 | Result += "\tstruct _class_t *cls;\n"; |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 6444 | Result += "\tconst struct _method_list_t *instance_methods;\n"; |
| 6445 | Result += "\tconst struct _method_list_t *class_methods;\n"; |
| 6446 | Result += "\tconst struct _protocol_list_t *protocols;\n"; |
| 6447 | Result += "\tconst struct _prop_list_t *properties;\n"; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6448 | Result += "};\n"; |
| 6449 | |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 6450 | Result += "extern \"C\" __declspec(dllimport) struct objc_cache _objc_empty_cache;\n"; |
Fariborz Jahanian | 297976d | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 6451 | Result += "#pragma warning(disable:4273)\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6452 | meta_data_declared = true; |
| 6453 | } |
| 6454 | |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 6455 | static void Write_protocol_list_t_TypeDecl(std::string &Result, |
| 6456 | long super_protocol_count) { |
| 6457 | Result += "struct /*_protocol_list_t*/"; Result += " {\n"; |
| 6458 | Result += "\tlong protocol_count; // Note, this is 32/64 bit\n"; |
| 6459 | Result += "\tstruct _protocol_t *super_protocols["; |
| 6460 | Result += utostr(super_protocol_count); Result += "];\n"; |
| 6461 | Result += "}"; |
| 6462 | } |
| 6463 | |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6464 | static void Write_method_list_t_TypeDecl(std::string &Result, |
| 6465 | unsigned int method_count) { |
| 6466 | Result += "struct /*_method_list_t*/"; Result += " {\n"; |
| 6467 | Result += "\tunsigned int entsize; // sizeof(struct _objc_method)\n"; |
| 6468 | Result += "\tunsigned int method_count;\n"; |
| 6469 | Result += "\tstruct _objc_method method_list["; |
| 6470 | Result += utostr(method_count); Result += "];\n"; |
| 6471 | Result += "}"; |
| 6472 | } |
| 6473 | |
Fariborz Jahanian | da35eac | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 6474 | static void Write__prop_list_t_TypeDecl(std::string &Result, |
| 6475 | unsigned int property_count) { |
| 6476 | Result += "struct /*_prop_list_t*/"; Result += " {\n"; |
| 6477 | Result += "\tunsigned int entsize; // sizeof(struct _prop_t)\n"; |
| 6478 | Result += "\tunsigned int count_of_properties;\n"; |
| 6479 | Result += "\tstruct _prop_t prop_list["; |
| 6480 | Result += utostr(property_count); Result += "];\n"; |
| 6481 | Result += "}"; |
| 6482 | } |
| 6483 | |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6484 | static void Write__ivar_list_t_TypeDecl(std::string &Result, |
| 6485 | unsigned int ivar_count) { |
| 6486 | Result += "struct /*_ivar_list_t*/"; Result += " {\n"; |
| 6487 | Result += "\tunsigned int entsize; // sizeof(struct _prop_t)\n"; |
| 6488 | Result += "\tunsigned int count;\n"; |
| 6489 | Result += "\tstruct _ivar_t ivar_list["; |
| 6490 | Result += utostr(ivar_count); Result += "];\n"; |
| 6491 | Result += "}"; |
| 6492 | } |
| 6493 | |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 6494 | static void Write_protocol_list_initializer(ASTContext *Context, std::string &Result, |
| 6495 | ArrayRef<ObjCProtocolDecl *> SuperProtocols, |
| 6496 | StringRef VarName, |
| 6497 | StringRef ProtocolName) { |
| 6498 | if (SuperProtocols.size() > 0) { |
| 6499 | Result += "\nstatic "; |
| 6500 | Write_protocol_list_t_TypeDecl(Result, SuperProtocols.size()); |
| 6501 | Result += " "; Result += VarName; |
| 6502 | Result += ProtocolName; |
| 6503 | Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n"; |
| 6504 | Result += "\t"; Result += utostr(SuperProtocols.size()); Result += ",\n"; |
| 6505 | for (unsigned i = 0, e = SuperProtocols.size(); i < e; i++) { |
| 6506 | ObjCProtocolDecl *SuperPD = SuperProtocols[i]; |
| 6507 | Result += "\t&"; Result += "_OBJC_PROTOCOL_"; |
| 6508 | Result += SuperPD->getNameAsString(); |
| 6509 | if (i == e-1) |
| 6510 | Result += "\n};\n"; |
| 6511 | else |
| 6512 | Result += ",\n"; |
| 6513 | } |
| 6514 | } |
| 6515 | } |
| 6516 | |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6517 | static void Write_method_list_t_initializer(RewriteModernObjC &RewriteObj, |
| 6518 | ASTContext *Context, std::string &Result, |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6519 | ArrayRef<ObjCMethodDecl *> Methods, |
| 6520 | StringRef VarName, |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6521 | StringRef TopLevelDeclName, |
| 6522 | bool MethodImpl) { |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6523 | if (Methods.size() > 0) { |
| 6524 | Result += "\nstatic "; |
| 6525 | Write_method_list_t_TypeDecl(Result, Methods.size()); |
| 6526 | Result += " "; Result += VarName; |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6527 | Result += TopLevelDeclName; |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6528 | Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n"; |
| 6529 | Result += "\t"; Result += "sizeof(_objc_method)"; Result += ",\n"; |
| 6530 | Result += "\t"; Result += utostr(Methods.size()); Result += ",\n"; |
| 6531 | for (unsigned i = 0, e = Methods.size(); i < e; i++) { |
| 6532 | ObjCMethodDecl *MD = Methods[i]; |
| 6533 | if (i == 0) |
| 6534 | Result += "\t{{(struct objc_selector *)\""; |
| 6535 | else |
| 6536 | Result += "\t{(struct objc_selector *)\""; |
| 6537 | Result += (MD)->getSelector().getAsString(); Result += "\""; |
| 6538 | Result += ", "; |
| 6539 | std::string MethodTypeString; |
| 6540 | Context->getObjCEncodingForMethodDecl(MD, MethodTypeString); |
| 6541 | Result += "\""; Result += MethodTypeString; Result += "\""; |
| 6542 | Result += ", "; |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6543 | if (!MethodImpl) |
| 6544 | Result += "0"; |
| 6545 | else { |
| 6546 | Result += "(void *)"; |
| 6547 | Result += RewriteObj.MethodInternalNames[MD]; |
| 6548 | } |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6549 | if (i == e-1) |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6550 | Result += "}}\n"; |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6551 | else |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6552 | Result += "},\n"; |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6553 | } |
| 6554 | Result += "};\n"; |
| 6555 | } |
| 6556 | } |
| 6557 | |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6558 | static void Write_prop_list_t_initializer(RewriteModernObjC &RewriteObj, |
Fariborz Jahanian | da35eac | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 6559 | ASTContext *Context, std::string &Result, |
| 6560 | ArrayRef<ObjCPropertyDecl *> Properties, |
| 6561 | const Decl *Container, |
| 6562 | StringRef VarName, |
| 6563 | StringRef ProtocolName) { |
| 6564 | if (Properties.size() > 0) { |
| 6565 | Result += "\nstatic "; |
| 6566 | Write__prop_list_t_TypeDecl(Result, Properties.size()); |
| 6567 | Result += " "; Result += VarName; |
| 6568 | Result += ProtocolName; |
| 6569 | Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n"; |
| 6570 | Result += "\t"; Result += "sizeof(_prop_t)"; Result += ",\n"; |
| 6571 | Result += "\t"; Result += utostr(Properties.size()); Result += ",\n"; |
| 6572 | for (unsigned i = 0, e = Properties.size(); i < e; i++) { |
| 6573 | ObjCPropertyDecl *PropDecl = Properties[i]; |
| 6574 | if (i == 0) |
| 6575 | Result += "\t{{\""; |
| 6576 | else |
| 6577 | Result += "\t{\""; |
| 6578 | Result += PropDecl->getName(); Result += "\","; |
| 6579 | std::string PropertyTypeString, QuotePropertyTypeString; |
| 6580 | Context->getObjCEncodingForPropertyDecl(PropDecl, Container, PropertyTypeString); |
| 6581 | RewriteObj.QuoteDoublequotes(PropertyTypeString, QuotePropertyTypeString); |
| 6582 | Result += "\""; Result += QuotePropertyTypeString; Result += "\""; |
| 6583 | if (i == e-1) |
| 6584 | Result += "}}\n"; |
| 6585 | else |
| 6586 | Result += "},\n"; |
| 6587 | } |
| 6588 | Result += "};\n"; |
| 6589 | } |
| 6590 | } |
| 6591 | |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 6592 | // Metadata flags |
| 6593 | enum MetaDataDlags { |
| 6594 | CLS = 0x0, |
| 6595 | CLS_META = 0x1, |
| 6596 | CLS_ROOT = 0x2, |
| 6597 | OBJC2_CLS_HIDDEN = 0x10, |
| 6598 | CLS_EXCEPTION = 0x20, |
| 6599 | |
| 6600 | /// (Obsolete) ARC-specific: this class has a .release_ivars method |
| 6601 | CLS_HAS_IVAR_RELEASER = 0x40, |
| 6602 | /// class was compiled with -fobjc-arr |
| 6603 | CLS_COMPILED_BY_ARC = 0x80 // (1<<7) |
| 6604 | }; |
| 6605 | |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6606 | static void Write__class_ro_t_initializer(ASTContext *Context, std::string &Result, |
| 6607 | unsigned int flags, |
| 6608 | const std::string &InstanceStart, |
| 6609 | const std::string &InstanceSize, |
| 6610 | ArrayRef<ObjCMethodDecl *>baseMethods, |
| 6611 | ArrayRef<ObjCProtocolDecl *>baseProtocols, |
| 6612 | ArrayRef<ObjCIvarDecl *>ivars, |
| 6613 | ArrayRef<ObjCPropertyDecl *>Properties, |
| 6614 | StringRef VarName, |
| 6615 | StringRef ClassName) { |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6616 | Result += "\nstatic struct _class_ro_t "; |
| 6617 | Result += VarName; Result += ClassName; |
| 6618 | Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n"; |
| 6619 | Result += "\t"; |
| 6620 | Result += llvm::utostr(flags); Result += ", "; |
| 6621 | Result += InstanceStart; Result += ", "; |
| 6622 | Result += InstanceSize; Result += ", \n"; |
| 6623 | Result += "\t"; |
Fariborz Jahanian | de5d946 | 2012-03-14 18:09:23 +0000 | [diff] [blame] | 6624 | const llvm::Triple &Triple(Context->getTargetInfo().getTriple()); |
| 6625 | if (Triple.getArch() == llvm::Triple::x86_64) |
| 6626 | // uint32_t const reserved; // only when building for 64bit targets |
| 6627 | Result += "(unsigned int)0, \n\t"; |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6628 | // const uint8_t * const ivarLayout; |
| 6629 | Result += "0, \n\t"; |
| 6630 | Result += "\""; Result += ClassName; Result += "\",\n\t"; |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 6631 | bool metaclass = ((flags & CLS_META) != 0); |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6632 | if (baseMethods.size() > 0) { |
| 6633 | Result += "(const struct _method_list_t *)&"; |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 6634 | if (metaclass) |
| 6635 | Result += "_OBJC_$_CLASS_METHODS_"; |
| 6636 | else |
| 6637 | Result += "_OBJC_$_INSTANCE_METHODS_"; |
| 6638 | Result += ClassName; |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6639 | Result += ",\n\t"; |
| 6640 | } |
| 6641 | else |
| 6642 | Result += "0, \n\t"; |
| 6643 | |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 6644 | if (!metaclass && baseProtocols.size() > 0) { |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6645 | Result += "(const struct _objc_protocol_list *)&"; |
| 6646 | Result += "_OBJC_CLASS_PROTOCOLS_$_"; Result += ClassName; |
| 6647 | Result += ",\n\t"; |
| 6648 | } |
| 6649 | else |
| 6650 | Result += "0, \n\t"; |
| 6651 | |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 6652 | if (!metaclass && ivars.size() > 0) { |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6653 | Result += "(const struct _ivar_list_t *)&"; |
| 6654 | Result += "_OBJC_$_INSTANCE_VARIABLES_"; Result += ClassName; |
| 6655 | Result += ",\n\t"; |
| 6656 | } |
| 6657 | else |
| 6658 | Result += "0, \n\t"; |
| 6659 | |
| 6660 | // weakIvarLayout |
| 6661 | Result += "0, \n\t"; |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 6662 | if (!metaclass && Properties.size() > 0) { |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6663 | Result += "(const struct _prop_list_t *)&"; |
Fariborz Jahanian | eeabf38 | 2012-02-16 21:57:59 +0000 | [diff] [blame] | 6664 | Result += "_OBJC_$_PROP_LIST_"; Result += ClassName; |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6665 | Result += ",\n"; |
| 6666 | } |
| 6667 | else |
| 6668 | Result += "0, \n"; |
| 6669 | |
| 6670 | Result += "};\n"; |
| 6671 | } |
| 6672 | |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6673 | static void Write_class_t(ASTContext *Context, std::string &Result, |
| 6674 | StringRef VarName, |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6675 | const ObjCInterfaceDecl *CDecl, bool metaclass) { |
| 6676 | bool rootClass = (!CDecl->getSuperClass()); |
| 6677 | const ObjCInterfaceDecl *RootClass = CDecl; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6678 | |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6679 | if (!rootClass) { |
| 6680 | // Find the Root class |
| 6681 | RootClass = CDecl->getSuperClass(); |
| 6682 | while (RootClass->getSuperClass()) { |
| 6683 | RootClass = RootClass->getSuperClass(); |
| 6684 | } |
| 6685 | } |
| 6686 | |
| 6687 | if (metaclass && rootClass) { |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6688 | // Need to handle a case of use of forward declaration. |
Fariborz Jahanian | ce0d897 | 2012-03-10 18:25:06 +0000 | [diff] [blame] | 6689 | Result += "\n"; |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 6690 | Result += "extern \"C\" "; |
Fariborz Jahanian | ce0d897 | 2012-03-10 18:25:06 +0000 | [diff] [blame] | 6691 | if (CDecl->getImplementation()) |
| 6692 | Result += "__declspec(dllexport) "; |
Fariborz Jahanian | 297976d | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 6693 | else |
| 6694 | Result += "__declspec(dllimport) "; |
| 6695 | |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 6696 | Result += "struct _class_t OBJC_CLASS_$_"; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6697 | Result += CDecl->getNameAsString(); |
| 6698 | Result += ";\n"; |
| 6699 | } |
| 6700 | // Also, for possibility of 'super' metadata class not having been defined yet. |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6701 | if (!rootClass) { |
Fariborz Jahanian | 868e985 | 2012-03-29 19:04:10 +0000 | [diff] [blame] | 6702 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
Fariborz Jahanian | ce0d897 | 2012-03-10 18:25:06 +0000 | [diff] [blame] | 6703 | Result += "\n"; |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 6704 | Result += "extern \"C\" "; |
Fariborz Jahanian | 868e985 | 2012-03-29 19:04:10 +0000 | [diff] [blame] | 6705 | if (SuperClass->getImplementation()) |
Fariborz Jahanian | ce0d897 | 2012-03-10 18:25:06 +0000 | [diff] [blame] | 6706 | Result += "__declspec(dllexport) "; |
Fariborz Jahanian | 297976d | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 6707 | else |
| 6708 | Result += "__declspec(dllimport) "; |
| 6709 | |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 6710 | Result += "struct _class_t "; |
Fariborz Jahanian | ce0d897 | 2012-03-10 18:25:06 +0000 | [diff] [blame] | 6711 | Result += VarName; |
Fariborz Jahanian | 868e985 | 2012-03-29 19:04:10 +0000 | [diff] [blame] | 6712 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6713 | Result += ";\n"; |
Fariborz Jahanian | 452eac1 | 2012-03-20 21:09:58 +0000 | [diff] [blame] | 6714 | |
Fariborz Jahanian | 868e985 | 2012-03-29 19:04:10 +0000 | [diff] [blame] | 6715 | if (metaclass && RootClass != SuperClass) { |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 6716 | Result += "extern \"C\" "; |
Fariborz Jahanian | 452eac1 | 2012-03-20 21:09:58 +0000 | [diff] [blame] | 6717 | if (RootClass->getImplementation()) |
| 6718 | Result += "__declspec(dllexport) "; |
Fariborz Jahanian | 297976d | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 6719 | else |
| 6720 | Result += "__declspec(dllimport) "; |
| 6721 | |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 6722 | Result += "struct _class_t "; |
Fariborz Jahanian | 452eac1 | 2012-03-20 21:09:58 +0000 | [diff] [blame] | 6723 | Result += VarName; |
| 6724 | Result += RootClass->getNameAsString(); |
| 6725 | Result += ";\n"; |
| 6726 | } |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6727 | } |
| 6728 | |
Fariborz Jahanian | 297976d | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 6729 | Result += "\nextern \"C\" __declspec(dllexport) struct _class_t "; |
| 6730 | Result += VarName; Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6731 | Result += " __attribute__ ((used, section (\"__DATA,__objc_data\"))) = {\n"; |
| 6732 | Result += "\t"; |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6733 | if (metaclass) { |
| 6734 | if (!rootClass) { |
| 6735 | Result += "0, // &"; Result += VarName; |
| 6736 | Result += RootClass->getNameAsString(); |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6737 | Result += ",\n\t"; |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6738 | Result += "0, // &"; Result += VarName; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6739 | Result += CDecl->getSuperClass()->getNameAsString(); |
| 6740 | Result += ",\n\t"; |
| 6741 | } |
| 6742 | else { |
Fariborz Jahanian | 452eac1 | 2012-03-20 21:09:58 +0000 | [diff] [blame] | 6743 | Result += "0, // &"; Result += VarName; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6744 | Result += CDecl->getNameAsString(); |
| 6745 | Result += ",\n\t"; |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6746 | Result += "0, // &OBJC_CLASS_$_"; Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6747 | Result += ",\n\t"; |
| 6748 | } |
| 6749 | } |
| 6750 | else { |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6751 | Result += "0, // &OBJC_METACLASS_$_"; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6752 | Result += CDecl->getNameAsString(); |
| 6753 | Result += ",\n\t"; |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6754 | if (!rootClass) { |
| 6755 | Result += "0, // &"; Result += VarName; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6756 | Result += CDecl->getSuperClass()->getNameAsString(); |
| 6757 | Result += ",\n\t"; |
| 6758 | } |
| 6759 | else |
| 6760 | Result += "0,\n\t"; |
| 6761 | } |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6762 | Result += "0, // (void *)&_objc_empty_cache,\n\t"; |
| 6763 | Result += "0, // unused, was (void *)&_objc_empty_vtable,\n\t"; |
| 6764 | if (metaclass) |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6765 | Result += "&_OBJC_METACLASS_RO_$_"; |
| 6766 | else |
| 6767 | Result += "&_OBJC_CLASS_RO_$_"; |
| 6768 | Result += CDecl->getNameAsString(); |
| 6769 | Result += ",\n};\n"; |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6770 | |
| 6771 | // Add static function to initialize some of the meta-data fields. |
| 6772 | // avoid doing it twice. |
| 6773 | if (metaclass) |
| 6774 | return; |
| 6775 | |
| 6776 | const ObjCInterfaceDecl *SuperClass = |
| 6777 | rootClass ? CDecl : CDecl->getSuperClass(); |
| 6778 | |
| 6779 | Result += "static void OBJC_CLASS_SETUP_$_"; |
| 6780 | Result += CDecl->getNameAsString(); |
| 6781 | Result += "(void ) {\n"; |
| 6782 | Result += "\tOBJC_METACLASS_$_"; Result += CDecl->getNameAsString(); |
| 6783 | Result += ".isa = "; Result += "&OBJC_METACLASS_$_"; |
Fariborz Jahanian | 452eac1 | 2012-03-20 21:09:58 +0000 | [diff] [blame] | 6784 | Result += RootClass->getNameAsString(); Result += ";\n"; |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6785 | |
| 6786 | Result += "\tOBJC_METACLASS_$_"; Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 452eac1 | 2012-03-20 21:09:58 +0000 | [diff] [blame] | 6787 | Result += ".superclass = "; |
| 6788 | if (rootClass) |
| 6789 | Result += "&OBJC_CLASS_$_"; |
| 6790 | else |
| 6791 | Result += "&OBJC_METACLASS_$_"; |
| 6792 | |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6793 | Result += SuperClass->getNameAsString(); Result += ";\n"; |
| 6794 | |
| 6795 | Result += "\tOBJC_METACLASS_$_"; Result += CDecl->getNameAsString(); |
| 6796 | Result += ".cache = "; Result += "&_objc_empty_cache"; Result += ";\n"; |
| 6797 | |
| 6798 | Result += "\tOBJC_CLASS_$_"; Result += CDecl->getNameAsString(); |
| 6799 | Result += ".isa = "; Result += "&OBJC_METACLASS_$_"; |
| 6800 | Result += CDecl->getNameAsString(); Result += ";\n"; |
| 6801 | |
| 6802 | if (!rootClass) { |
| 6803 | Result += "\tOBJC_CLASS_$_"; Result += CDecl->getNameAsString(); |
| 6804 | Result += ".superclass = "; Result += "&OBJC_CLASS_$_"; |
| 6805 | Result += SuperClass->getNameAsString(); Result += ";\n"; |
| 6806 | } |
| 6807 | |
| 6808 | Result += "\tOBJC_CLASS_$_"; Result += CDecl->getNameAsString(); |
| 6809 | Result += ".cache = "; Result += "&_objc_empty_cache"; Result += ";\n"; |
| 6810 | Result += "}\n"; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6811 | } |
| 6812 | |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6813 | static void Write_category_t(RewriteModernObjC &RewriteObj, ASTContext *Context, |
| 6814 | std::string &Result, |
Fariborz Jahanian | e033578 | 2012-03-27 18:41:05 +0000 | [diff] [blame] | 6815 | ObjCCategoryDecl *CatDecl, |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 6816 | ObjCInterfaceDecl *ClassDecl, |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6817 | ArrayRef<ObjCMethodDecl *> InstanceMethods, |
| 6818 | ArrayRef<ObjCMethodDecl *> ClassMethods, |
| 6819 | ArrayRef<ObjCProtocolDecl *> RefedProtocols, |
| 6820 | ArrayRef<ObjCPropertyDecl *> ClassProperties) { |
Fariborz Jahanian | e033578 | 2012-03-27 18:41:05 +0000 | [diff] [blame] | 6821 | StringRef CatName = CatDecl->getName(); |
NAKAMURA Takumi | 20f8939 | 2012-03-21 03:21:46 +0000 | [diff] [blame] | 6822 | StringRef ClassName = ClassDecl->getName(); |
Fariborz Jahanian | 8c00a1b | 2012-02-17 20:33:00 +0000 | [diff] [blame] | 6823 | // must declare an extern class object in case this class is not implemented |
| 6824 | // in this TU. |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 6825 | Result += "\n"; |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 6826 | Result += "extern \"C\" "; |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 6827 | if (ClassDecl->getImplementation()) |
| 6828 | Result += "__declspec(dllexport) "; |
Fariborz Jahanian | 297976d | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 6829 | else |
| 6830 | Result += "__declspec(dllimport) "; |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 6831 | |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 6832 | Result += "struct _class_t "; |
Fariborz Jahanian | 8c00a1b | 2012-02-17 20:33:00 +0000 | [diff] [blame] | 6833 | Result += "OBJC_CLASS_$_"; Result += ClassName; |
| 6834 | Result += ";\n"; |
| 6835 | |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6836 | Result += "\nstatic struct _category_t "; |
| 6837 | Result += "_OBJC_$_CATEGORY_"; |
| 6838 | Result += ClassName; Result += "_$_"; Result += CatName; |
| 6839 | Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = \n"; |
| 6840 | Result += "{\n"; |
| 6841 | Result += "\t\""; Result += ClassName; Result += "\",\n"; |
Fariborz Jahanian | 4b2fe6e | 2012-03-20 21:41:28 +0000 | [diff] [blame] | 6842 | Result += "\t0, // &"; Result += "OBJC_CLASS_$_"; Result += ClassName; |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6843 | Result += ",\n"; |
| 6844 | if (InstanceMethods.size() > 0) { |
| 6845 | Result += "\t(const struct _method_list_t *)&"; |
| 6846 | Result += "_OBJC_$_CATEGORY_INSTANCE_METHODS_"; |
| 6847 | Result += ClassName; Result += "_$_"; Result += CatName; |
| 6848 | Result += ",\n"; |
| 6849 | } |
| 6850 | else |
| 6851 | Result += "\t0,\n"; |
| 6852 | |
| 6853 | if (ClassMethods.size() > 0) { |
| 6854 | Result += "\t(const struct _method_list_t *)&"; |
| 6855 | Result += "_OBJC_$_CATEGORY_CLASS_METHODS_"; |
| 6856 | Result += ClassName; Result += "_$_"; Result += CatName; |
| 6857 | Result += ",\n"; |
| 6858 | } |
| 6859 | else |
| 6860 | Result += "\t0,\n"; |
| 6861 | |
| 6862 | if (RefedProtocols.size() > 0) { |
| 6863 | Result += "\t(const struct _protocol_list_t *)&"; |
| 6864 | Result += "_OBJC_CATEGORY_PROTOCOLS_$_"; |
| 6865 | Result += ClassName; Result += "_$_"; Result += CatName; |
| 6866 | Result += ",\n"; |
| 6867 | } |
| 6868 | else |
| 6869 | Result += "\t0,\n"; |
| 6870 | |
| 6871 | if (ClassProperties.size() > 0) { |
| 6872 | Result += "\t(const struct _prop_list_t *)&"; Result += "_OBJC_$_PROP_LIST_"; |
| 6873 | Result += ClassName; Result += "_$_"; Result += CatName; |
| 6874 | Result += ",\n"; |
| 6875 | } |
| 6876 | else |
| 6877 | Result += "\t0,\n"; |
| 6878 | |
| 6879 | Result += "};\n"; |
Fariborz Jahanian | 4b2fe6e | 2012-03-20 21:41:28 +0000 | [diff] [blame] | 6880 | |
| 6881 | // Add static function to initialize the class pointer in the category structure. |
| 6882 | Result += "static void OBJC_CATEGORY_SETUP_$_"; |
| 6883 | Result += ClassDecl->getNameAsString(); |
| 6884 | Result += "_$_"; |
| 6885 | Result += CatName; |
| 6886 | Result += "(void ) {\n"; |
| 6887 | Result += "\t_OBJC_$_CATEGORY_"; |
| 6888 | Result += ClassDecl->getNameAsString(); |
| 6889 | Result += "_$_"; |
| 6890 | Result += CatName; |
| 6891 | Result += ".cls = "; Result += "&OBJC_CLASS_$_"; Result += ClassName; |
| 6892 | Result += ";\n}\n"; |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6893 | } |
| 6894 | |
Fariborz Jahanian | e0adbd8 | 2012-02-08 22:23:26 +0000 | [diff] [blame] | 6895 | static void Write__extendedMethodTypes_initializer(RewriteModernObjC &RewriteObj, |
| 6896 | ASTContext *Context, std::string &Result, |
| 6897 | ArrayRef<ObjCMethodDecl *> Methods, |
| 6898 | StringRef VarName, |
| 6899 | StringRef ProtocolName) { |
| 6900 | if (Methods.size() == 0) |
| 6901 | return; |
| 6902 | |
| 6903 | Result += "\nstatic const char *"; |
| 6904 | Result += VarName; Result += ProtocolName; |
| 6905 | Result += " [] __attribute__ ((used, section (\"__DATA,__objc_const\"))) = \n"; |
| 6906 | Result += "{\n"; |
| 6907 | for (unsigned i = 0, e = Methods.size(); i < e; i++) { |
| 6908 | ObjCMethodDecl *MD = Methods[i]; |
| 6909 | std::string MethodTypeString, QuoteMethodTypeString; |
| 6910 | Context->getObjCEncodingForMethodDecl(MD, MethodTypeString, true); |
| 6911 | RewriteObj.QuoteDoublequotes(MethodTypeString, QuoteMethodTypeString); |
| 6912 | Result += "\t\""; Result += QuoteMethodTypeString; Result += "\""; |
| 6913 | if (i == e-1) |
| 6914 | Result += "\n};\n"; |
| 6915 | else { |
| 6916 | Result += ",\n"; |
| 6917 | } |
| 6918 | } |
| 6919 | } |
| 6920 | |
Fariborz Jahanian | acee1c9 | 2012-04-11 21:12:36 +0000 | [diff] [blame] | 6921 | static void Write_IvarOffsetVar(RewriteModernObjC &RewriteObj, |
| 6922 | ASTContext *Context, |
Fariborz Jahanian | 40a777a | 2012-03-12 16:46:58 +0000 | [diff] [blame] | 6923 | std::string &Result, |
Fariborz Jahanian | db64923 | 2012-02-13 20:59:02 +0000 | [diff] [blame] | 6924 | ArrayRef<ObjCIvarDecl *> Ivars, |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 6925 | ObjCInterfaceDecl *CDecl) { |
Fariborz Jahanian | db64923 | 2012-02-13 20:59:02 +0000 | [diff] [blame] | 6926 | // FIXME. visibilty of offset symbols may have to be set; for Darwin |
| 6927 | // this is what happens: |
| 6928 | /** |
| 6929 | if (Ivar->getAccessControl() == ObjCIvarDecl::Private || |
| 6930 | Ivar->getAccessControl() == ObjCIvarDecl::Package || |
| 6931 | Class->getVisibility() == HiddenVisibility) |
| 6932 | Visibility shoud be: HiddenVisibility; |
| 6933 | else |
| 6934 | Visibility shoud be: DefaultVisibility; |
| 6935 | */ |
| 6936 | |
Fariborz Jahanian | 07e5288 | 2012-02-13 21:34:45 +0000 | [diff] [blame] | 6937 | Result += "\n"; |
| 6938 | for (unsigned i =0, e = Ivars.size(); i < e; i++) { |
| 6939 | ObjCIvarDecl *IvarDecl = Ivars[i]; |
Fariborz Jahanian | 40a777a | 2012-03-12 16:46:58 +0000 | [diff] [blame] | 6940 | if (Context->getLangOpts().MicrosoftExt) |
| 6941 | Result += "__declspec(allocate(\".objc_ivar$B\")) "; |
| 6942 | |
| 6943 | if (!Context->getLangOpts().MicrosoftExt || |
| 6944 | IvarDecl->getAccessControl() == ObjCIvarDecl::Private || |
Fariborz Jahanian | 117591f | 2012-03-10 01:34:42 +0000 | [diff] [blame] | 6945 | IvarDecl->getAccessControl() == ObjCIvarDecl::Package) |
Fariborz Jahanian | 297976d | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 6946 | Result += "extern \"C\" unsigned long int "; |
Fariborz Jahanian | d1c84d3 | 2012-03-10 00:53:02 +0000 | [diff] [blame] | 6947 | else |
Fariborz Jahanian | 297976d | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 6948 | Result += "extern \"C\" __declspec(dllexport) unsigned long int "; |
Fariborz Jahanian | cd3b036 | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 6949 | if (Ivars[i]->isBitField()) |
| 6950 | RewriteObj.ObjCIvarBitfieldGroupOffset(IvarDecl, Result); |
| 6951 | else |
| 6952 | WriteInternalIvarName(CDecl, IvarDecl, Result); |
Fariborz Jahanian | 07e5288 | 2012-02-13 21:34:45 +0000 | [diff] [blame] | 6953 | Result += " __attribute__ ((used, section (\"__DATA,__objc_ivar\")))"; |
| 6954 | Result += " = "; |
Fariborz Jahanian | acee1c9 | 2012-04-11 21:12:36 +0000 | [diff] [blame] | 6955 | RewriteObj.RewriteIvarOffsetComputation(IvarDecl, Result); |
| 6956 | Result += ";\n"; |
Fariborz Jahanian | cd3b036 | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 6957 | if (Ivars[i]->isBitField()) { |
| 6958 | // skip over rest of the ivar bitfields. |
| 6959 | SKIP_BITFIELDS(i , e, Ivars); |
| 6960 | } |
Fariborz Jahanian | db64923 | 2012-02-13 20:59:02 +0000 | [diff] [blame] | 6961 | } |
| 6962 | } |
| 6963 | |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6964 | static void Write__ivar_list_t_initializer(RewriteModernObjC &RewriteObj, |
| 6965 | ASTContext *Context, std::string &Result, |
Fariborz Jahanian | cd3b036 | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 6966 | ArrayRef<ObjCIvarDecl *> OriginalIvars, |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6967 | StringRef VarName, |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 6968 | ObjCInterfaceDecl *CDecl) { |
Fariborz Jahanian | cd3b036 | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 6969 | if (OriginalIvars.size() > 0) { |
| 6970 | Write_IvarOffsetVar(RewriteObj, Context, Result, OriginalIvars, CDecl); |
| 6971 | SmallVector<ObjCIvarDecl *, 8> Ivars; |
| 6972 | // strip off all but the first ivar bitfield from each group of ivars. |
| 6973 | // Such ivars in the ivar list table will be replaced by their grouping struct |
| 6974 | // 'ivar'. |
| 6975 | for (unsigned i = 0, e = OriginalIvars.size(); i < e; i++) { |
| 6976 | if (OriginalIvars[i]->isBitField()) { |
| 6977 | Ivars.push_back(OriginalIvars[i]); |
| 6978 | // skip over rest of the ivar bitfields. |
| 6979 | SKIP_BITFIELDS(i , e, OriginalIvars); |
| 6980 | } |
| 6981 | else |
| 6982 | Ivars.push_back(OriginalIvars[i]); |
| 6983 | } |
Fariborz Jahanian | 07e5288 | 2012-02-13 21:34:45 +0000 | [diff] [blame] | 6984 | |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6985 | Result += "\nstatic "; |
| 6986 | Write__ivar_list_t_TypeDecl(Result, Ivars.size()); |
| 6987 | Result += " "; Result += VarName; |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 6988 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6989 | Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n"; |
| 6990 | Result += "\t"; Result += "sizeof(_ivar_t)"; Result += ",\n"; |
| 6991 | Result += "\t"; Result += utostr(Ivars.size()); Result += ",\n"; |
| 6992 | for (unsigned i =0, e = Ivars.size(); i < e; i++) { |
| 6993 | ObjCIvarDecl *IvarDecl = Ivars[i]; |
| 6994 | if (i == 0) |
| 6995 | Result += "\t{{"; |
| 6996 | else |
| 6997 | Result += "\t {"; |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 6998 | Result += "(unsigned long int *)&"; |
Fariborz Jahanian | cd3b036 | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 6999 | if (Ivars[i]->isBitField()) |
| 7000 | RewriteObj.ObjCIvarBitfieldGroupOffset(IvarDecl, Result); |
| 7001 | else |
| 7002 | WriteInternalIvarName(CDecl, IvarDecl, Result); |
Fariborz Jahanian | db64923 | 2012-02-13 20:59:02 +0000 | [diff] [blame] | 7003 | Result += ", "; |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 7004 | |
Fariborz Jahanian | cd3b036 | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 7005 | Result += "\""; |
| 7006 | if (Ivars[i]->isBitField()) |
| 7007 | RewriteObj.ObjCIvarBitfieldGroupDecl(Ivars[i], Result); |
| 7008 | else |
| 7009 | Result += IvarDecl->getName(); |
| 7010 | Result += "\", "; |
| 7011 | |
| 7012 | QualType IVQT = IvarDecl->getType(); |
| 7013 | if (IvarDecl->isBitField()) |
| 7014 | IVQT = RewriteObj.GetGroupRecordTypeForObjCIvarBitfield(IvarDecl); |
| 7015 | |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 7016 | std::string IvarTypeString, QuoteIvarTypeString; |
Fariborz Jahanian | cd3b036 | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 7017 | Context->getObjCEncodingForType(IVQT, IvarTypeString, |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 7018 | IvarDecl); |
| 7019 | RewriteObj.QuoteDoublequotes(IvarTypeString, QuoteIvarTypeString); |
| 7020 | Result += "\""; Result += QuoteIvarTypeString; Result += "\", "; |
| 7021 | |
Fariborz Jahanian | 8f1fed0 | 2012-02-11 20:10:52 +0000 | [diff] [blame] | 7022 | // FIXME. this alignment represents the host alignment and need be changed to |
| 7023 | // represent the target alignment. |
Fariborz Jahanian | cd3b036 | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 7024 | unsigned Align = Context->getTypeAlign(IVQT)/8; |
Fariborz Jahanian | 8f1fed0 | 2012-02-11 20:10:52 +0000 | [diff] [blame] | 7025 | Align = llvm::Log2_32(Align); |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 7026 | Result += llvm::utostr(Align); Result += ", "; |
Fariborz Jahanian | cd3b036 | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 7027 | CharUnits Size = Context->getTypeSizeInChars(IVQT); |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 7028 | Result += llvm::utostr(Size.getQuantity()); |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 7029 | if (i == e-1) |
| 7030 | Result += "}}\n"; |
| 7031 | else |
| 7032 | Result += "},\n"; |
| 7033 | } |
| 7034 | Result += "};\n"; |
| 7035 | } |
| 7036 | } |
| 7037 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7038 | /// RewriteObjCProtocolMetaData - Rewrite protocols meta-data. |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 7039 | void RewriteModernObjC::RewriteObjCProtocolMetaData(ObjCProtocolDecl *PDecl, |
| 7040 | std::string &Result) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7041 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7042 | // Do not synthesize the protocol more than once. |
| 7043 | if (ObjCSynthesizedProtocols.count(PDecl->getCanonicalDecl())) |
| 7044 | return; |
Fariborz Jahanian | de5d946 | 2012-03-14 18:09:23 +0000 | [diff] [blame] | 7045 | WriteModernMetadataDeclarations(Context, Result); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7046 | |
| 7047 | if (ObjCProtocolDecl *Def = PDecl->getDefinition()) |
| 7048 | PDecl = Def; |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 7049 | // Must write out all protocol definitions in current qualifier list, |
| 7050 | // and in their nested qualifiers before writing out current definition. |
| 7051 | for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(), |
| 7052 | E = PDecl->protocol_end(); I != E; ++I) |
| 7053 | RewriteObjCProtocolMetaData(*I, Result); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7054 | |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 7055 | // Construct method lists. |
| 7056 | std::vector<ObjCMethodDecl *> InstanceMethods, ClassMethods; |
| 7057 | std::vector<ObjCMethodDecl *> OptInstanceMethods, OptClassMethods; |
| 7058 | for (ObjCProtocolDecl::instmeth_iterator |
| 7059 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
| 7060 | I != E; ++I) { |
| 7061 | ObjCMethodDecl *MD = *I; |
| 7062 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 7063 | OptInstanceMethods.push_back(MD); |
| 7064 | } else { |
| 7065 | InstanceMethods.push_back(MD); |
| 7066 | } |
| 7067 | } |
| 7068 | |
| 7069 | for (ObjCProtocolDecl::classmeth_iterator |
| 7070 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
| 7071 | I != E; ++I) { |
| 7072 | ObjCMethodDecl *MD = *I; |
| 7073 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 7074 | OptClassMethods.push_back(MD); |
| 7075 | } else { |
| 7076 | ClassMethods.push_back(MD); |
| 7077 | } |
| 7078 | } |
Fariborz Jahanian | e0adbd8 | 2012-02-08 22:23:26 +0000 | [diff] [blame] | 7079 | std::vector<ObjCMethodDecl *> AllMethods; |
| 7080 | for (unsigned i = 0, e = InstanceMethods.size(); i < e; i++) |
| 7081 | AllMethods.push_back(InstanceMethods[i]); |
| 7082 | for (unsigned i = 0, e = ClassMethods.size(); i < e; i++) |
| 7083 | AllMethods.push_back(ClassMethods[i]); |
| 7084 | for (unsigned i = 0, e = OptInstanceMethods.size(); i < e; i++) |
| 7085 | AllMethods.push_back(OptInstanceMethods[i]); |
| 7086 | for (unsigned i = 0, e = OptClassMethods.size(); i < e; i++) |
| 7087 | AllMethods.push_back(OptClassMethods[i]); |
| 7088 | |
| 7089 | Write__extendedMethodTypes_initializer(*this, Context, Result, |
| 7090 | AllMethods, |
| 7091 | "_OBJC_PROTOCOL_METHOD_TYPES_", |
| 7092 | PDecl->getNameAsString()); |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 7093 | // Protocol's super protocol list |
| 7094 | std::vector<ObjCProtocolDecl *> SuperProtocols; |
| 7095 | for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(), |
| 7096 | E = PDecl->protocol_end(); I != E; ++I) |
| 7097 | SuperProtocols.push_back(*I); |
| 7098 | |
| 7099 | Write_protocol_list_initializer(Context, Result, SuperProtocols, |
| 7100 | "_OBJC_PROTOCOL_REFS_", |
| 7101 | PDecl->getNameAsString()); |
| 7102 | |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 7103 | Write_method_list_t_initializer(*this, Context, Result, InstanceMethods, |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 7104 | "_OBJC_PROTOCOL_INSTANCE_METHODS_", |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 7105 | PDecl->getNameAsString(), false); |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 7106 | |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 7107 | Write_method_list_t_initializer(*this, Context, Result, ClassMethods, |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 7108 | "_OBJC_PROTOCOL_CLASS_METHODS_", |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 7109 | PDecl->getNameAsString(), false); |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 7110 | |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 7111 | Write_method_list_t_initializer(*this, Context, Result, OptInstanceMethods, |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 7112 | "_OBJC_PROTOCOL_OPT_INSTANCE_METHODS_", |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 7113 | PDecl->getNameAsString(), false); |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 7114 | |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 7115 | Write_method_list_t_initializer(*this, Context, Result, OptClassMethods, |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 7116 | "_OBJC_PROTOCOL_OPT_CLASS_METHODS_", |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 7117 | PDecl->getNameAsString(), false); |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 7118 | |
Fariborz Jahanian | da35eac | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 7119 | // Protocol's property metadata. |
| 7120 | std::vector<ObjCPropertyDecl *> ProtocolProperties; |
| 7121 | for (ObjCContainerDecl::prop_iterator I = PDecl->prop_begin(), |
| 7122 | E = PDecl->prop_end(); I != E; ++I) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 7123 | ProtocolProperties.push_back(*I); |
Fariborz Jahanian | da35eac | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 7124 | |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 7125 | Write_prop_list_t_initializer(*this, Context, Result, ProtocolProperties, |
Fariborz Jahanian | da35eac | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 7126 | /* Container */0, |
| 7127 | "_OBJC_PROTOCOL_PROPERTIES_", |
| 7128 | PDecl->getNameAsString()); |
Fariborz Jahanian | da35eac | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 7129 | |
Fariborz Jahanian | 82848c2 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 7130 | // Writer out root metadata for current protocol: struct _protocol_t |
Fariborz Jahanian | 1ca052c | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 7131 | Result += "\n"; |
| 7132 | if (LangOpts.MicrosoftExt) |
Fariborz Jahanian | 8590d86 | 2012-04-14 17:13:08 +0000 | [diff] [blame] | 7133 | Result += "static "; |
Fariborz Jahanian | 30650eb | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 7134 | Result += "struct _protocol_t _OBJC_PROTOCOL_"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7135 | Result += PDecl->getNameAsString(); |
Fariborz Jahanian | 82848c2 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 7136 | Result += " __attribute__ ((used, section (\"__DATA,__datacoal_nt,coalesced\"))) = {\n"; |
| 7137 | Result += "\t0,\n"; // id is; is null |
| 7138 | Result += "\t\""; Result += PDecl->getNameAsString(); Result += "\",\n"; |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 7139 | if (SuperProtocols.size() > 0) { |
| 7140 | Result += "\t(const struct _protocol_list_t *)&"; Result += "_OBJC_PROTOCOL_REFS_"; |
| 7141 | Result += PDecl->getNameAsString(); Result += ",\n"; |
| 7142 | } |
| 7143 | else |
| 7144 | Result += "\t0,\n"; |
Fariborz Jahanian | 82848c2 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 7145 | if (InstanceMethods.size() > 0) { |
| 7146 | Result += "\t(const struct method_list_t *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 7147 | Result += PDecl->getNameAsString(); Result += ",\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7148 | } |
| 7149 | else |
Fariborz Jahanian | 82848c2 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 7150 | Result += "\t0,\n"; |
| 7151 | |
| 7152 | if (ClassMethods.size() > 0) { |
| 7153 | Result += "\t(const struct method_list_t *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
| 7154 | Result += PDecl->getNameAsString(); Result += ",\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7155 | } |
| 7156 | else |
Fariborz Jahanian | 82848c2 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 7157 | Result += "\t0,\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7158 | |
Fariborz Jahanian | 82848c2 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 7159 | if (OptInstanceMethods.size() > 0) { |
| 7160 | Result += "\t(const struct method_list_t *)&_OBJC_PROTOCOL_OPT_INSTANCE_METHODS_"; |
| 7161 | Result += PDecl->getNameAsString(); Result += ",\n"; |
| 7162 | } |
| 7163 | else |
| 7164 | Result += "\t0,\n"; |
| 7165 | |
| 7166 | if (OptClassMethods.size() > 0) { |
| 7167 | Result += "\t(const struct method_list_t *)&_OBJC_PROTOCOL_OPT_CLASS_METHODS_"; |
| 7168 | Result += PDecl->getNameAsString(); Result += ",\n"; |
| 7169 | } |
| 7170 | else |
| 7171 | Result += "\t0,\n"; |
| 7172 | |
| 7173 | if (ProtocolProperties.size() > 0) { |
| 7174 | Result += "\t(const struct _prop_list_t *)&_OBJC_PROTOCOL_PROPERTIES_"; |
| 7175 | Result += PDecl->getNameAsString(); Result += ",\n"; |
| 7176 | } |
| 7177 | else |
| 7178 | Result += "\t0,\n"; |
| 7179 | |
| 7180 | Result += "\t"; Result += "sizeof(_protocol_t)"; Result += ",\n"; |
| 7181 | Result += "\t0,\n"; |
| 7182 | |
Fariborz Jahanian | e0adbd8 | 2012-02-08 22:23:26 +0000 | [diff] [blame] | 7183 | if (AllMethods.size() > 0) { |
| 7184 | Result += "\t(const char **)&"; Result += "_OBJC_PROTOCOL_METHOD_TYPES_"; |
| 7185 | Result += PDecl->getNameAsString(); |
| 7186 | Result += "\n};\n"; |
| 7187 | } |
| 7188 | else |
| 7189 | Result += "\t0\n};\n"; |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 7190 | |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 7191 | if (LangOpts.MicrosoftExt) |
Fariborz Jahanian | 8590d86 | 2012-04-14 17:13:08 +0000 | [diff] [blame] | 7192 | Result += "static "; |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 7193 | Result += "struct _protocol_t *"; |
| 7194 | Result += "_OBJC_LABEL_PROTOCOL_$_"; Result += PDecl->getNameAsString(); |
| 7195 | Result += " = &_OBJC_PROTOCOL_"; Result += PDecl->getNameAsString(); |
| 7196 | Result += ";\n"; |
Fariborz Jahanian | 82848c2 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 7197 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7198 | // Mark this protocol as having been generated. |
| 7199 | if (!ObjCSynthesizedProtocols.insert(PDecl->getCanonicalDecl())) |
| 7200 | llvm_unreachable("protocol already synthesized"); |
| 7201 | |
| 7202 | } |
| 7203 | |
| 7204 | void RewriteModernObjC::RewriteObjCProtocolListMetaData( |
| 7205 | const ObjCList<ObjCProtocolDecl> &Protocols, |
| 7206 | StringRef prefix, StringRef ClassName, |
| 7207 | std::string &Result) { |
| 7208 | if (Protocols.empty()) return; |
| 7209 | |
| 7210 | for (unsigned i = 0; i != Protocols.size(); i++) |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 7211 | RewriteObjCProtocolMetaData(Protocols[i], Result); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7212 | |
| 7213 | // Output the top lovel protocol meta-data for the class. |
| 7214 | /* struct _objc_protocol_list { |
| 7215 | struct _objc_protocol_list *next; |
| 7216 | int protocol_count; |
| 7217 | struct _objc_protocol *class_protocols[]; |
| 7218 | } |
| 7219 | */ |
Fariborz Jahanian | 1ca052c | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 7220 | Result += "\n"; |
| 7221 | if (LangOpts.MicrosoftExt) |
| 7222 | Result += "__declspec(allocate(\".cat_cls_meth$B\")) "; |
| 7223 | Result += "static struct {\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7224 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 7225 | Result += "\tint protocol_count;\n"; |
| 7226 | Result += "\tstruct _objc_protocol *class_protocols["; |
| 7227 | Result += utostr(Protocols.size()); |
| 7228 | Result += "];\n} _OBJC_"; |
| 7229 | Result += prefix; |
| 7230 | Result += "_PROTOCOLS_"; |
| 7231 | Result += ClassName; |
| 7232 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 7233 | "{\n\t0, "; |
| 7234 | Result += utostr(Protocols.size()); |
| 7235 | Result += "\n"; |
| 7236 | |
| 7237 | Result += "\t,{&_OBJC_PROTOCOL_"; |
| 7238 | Result += Protocols[0]->getNameAsString(); |
| 7239 | Result += " \n"; |
| 7240 | |
| 7241 | for (unsigned i = 1; i != Protocols.size(); i++) { |
| 7242 | Result += "\t ,&_OBJC_PROTOCOL_"; |
| 7243 | Result += Protocols[i]->getNameAsString(); |
| 7244 | Result += "\n"; |
| 7245 | } |
| 7246 | Result += "\t }\n};\n"; |
| 7247 | } |
| 7248 | |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 7249 | /// hasObjCExceptionAttribute - Return true if this class or any super |
| 7250 | /// class has the __objc_exception__ attribute. |
| 7251 | /// FIXME. Move this to ASTContext.cpp as it is also used for IRGen. |
| 7252 | static bool hasObjCExceptionAttribute(ASTContext &Context, |
| 7253 | const ObjCInterfaceDecl *OID) { |
| 7254 | if (OID->hasAttr<ObjCExceptionAttr>()) |
| 7255 | return true; |
| 7256 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
| 7257 | return hasObjCExceptionAttribute(Context, Super); |
| 7258 | return false; |
| 7259 | } |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 7260 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7261 | void RewriteModernObjC::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
| 7262 | std::string &Result) { |
| 7263 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
| 7264 | |
| 7265 | // Explicitly declared @interface's are already synthesized. |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 7266 | if (CDecl->isImplicitInterfaceDecl()) |
| 7267 | assert(false && |
| 7268 | "Legacy implicit interface rewriting not supported in moder abi"); |
Fariborz Jahanian | 8f1fed0 | 2012-02-11 20:10:52 +0000 | [diff] [blame] | 7269 | |
Fariborz Jahanian | de5d946 | 2012-03-14 18:09:23 +0000 | [diff] [blame] | 7270 | WriteModernMetadataDeclarations(Context, Result); |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 7271 | SmallVector<ObjCIvarDecl *, 8> IVars; |
| 7272 | |
| 7273 | for (ObjCIvarDecl *IVD = CDecl->all_declared_ivar_begin(); |
| 7274 | IVD; IVD = IVD->getNextIvar()) { |
| 7275 | // Ignore unnamed bit-fields. |
| 7276 | if (!IVD->getDeclName()) |
| 7277 | continue; |
| 7278 | IVars.push_back(IVD); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7279 | } |
| 7280 | |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 7281 | Write__ivar_list_t_initializer(*this, Context, Result, IVars, |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 7282 | "_OBJC_$_INSTANCE_VARIABLES_", |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 7283 | CDecl); |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 7284 | |
| 7285 | // Build _objc_method_list for class's instance methods if needed |
| 7286 | SmallVector<ObjCMethodDecl *, 32> |
| 7287 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
| 7288 | |
| 7289 | // If any of our property implementations have associated getters or |
| 7290 | // setters, produce metadata for them as well. |
| 7291 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 7292 | PropEnd = IDecl->propimpl_end(); |
| 7293 | Prop != PropEnd; ++Prop) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7294 | if (Prop->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 7295 | continue; |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7296 | if (!Prop->getPropertyIvarDecl()) |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 7297 | continue; |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7298 | ObjCPropertyDecl *PD = Prop->getPropertyDecl(); |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 7299 | if (!PD) |
| 7300 | continue; |
| 7301 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
Fariborz Jahanian | 301e2e4 | 2012-05-03 22:52:13 +0000 | [diff] [blame] | 7302 | if (mustSynthesizeSetterGetterMethod(IDecl, PD, true /*getter*/)) |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 7303 | InstanceMethods.push_back(Getter); |
| 7304 | if (PD->isReadOnly()) |
| 7305 | continue; |
| 7306 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
Fariborz Jahanian | 301e2e4 | 2012-05-03 22:52:13 +0000 | [diff] [blame] | 7307 | if (mustSynthesizeSetterGetterMethod(IDecl, PD, false /*setter*/)) |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 7308 | InstanceMethods.push_back(Setter); |
| 7309 | } |
| 7310 | |
| 7311 | Write_method_list_t_initializer(*this, Context, Result, InstanceMethods, |
| 7312 | "_OBJC_$_INSTANCE_METHODS_", |
| 7313 | IDecl->getNameAsString(), true); |
| 7314 | |
| 7315 | SmallVector<ObjCMethodDecl *, 32> |
| 7316 | ClassMethods(IDecl->classmeth_begin(), IDecl->classmeth_end()); |
| 7317 | |
| 7318 | Write_method_list_t_initializer(*this, Context, Result, ClassMethods, |
| 7319 | "_OBJC_$_CLASS_METHODS_", |
| 7320 | IDecl->getNameAsString(), true); |
Fariborz Jahanian | 0a52534 | 2012-02-14 19:31:35 +0000 | [diff] [blame] | 7321 | |
| 7322 | // Protocols referenced in class declaration? |
| 7323 | // Protocol's super protocol list |
| 7324 | std::vector<ObjCProtocolDecl *> RefedProtocols; |
| 7325 | const ObjCList<ObjCProtocolDecl> &Protocols = CDecl->getReferencedProtocols(); |
| 7326 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 7327 | E = Protocols.end(); |
| 7328 | I != E; ++I) { |
| 7329 | RefedProtocols.push_back(*I); |
| 7330 | // Must write out all protocol definitions in current qualifier list, |
| 7331 | // and in their nested qualifiers before writing out current definition. |
| 7332 | RewriteObjCProtocolMetaData(*I, Result); |
| 7333 | } |
| 7334 | |
| 7335 | Write_protocol_list_initializer(Context, Result, |
| 7336 | RefedProtocols, |
| 7337 | "_OBJC_CLASS_PROTOCOLS_$_", |
| 7338 | IDecl->getNameAsString()); |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 7339 | |
| 7340 | // Protocol's property metadata. |
| 7341 | std::vector<ObjCPropertyDecl *> ClassProperties; |
| 7342 | for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(), |
| 7343 | E = CDecl->prop_end(); I != E; ++I) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 7344 | ClassProperties.push_back(*I); |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 7345 | |
| 7346 | Write_prop_list_t_initializer(*this, Context, Result, ClassProperties, |
Fariborz Jahanian | 2df089d | 2012-03-22 17:39:35 +0000 | [diff] [blame] | 7347 | /* Container */IDecl, |
Fariborz Jahanian | eeabf38 | 2012-02-16 21:57:59 +0000 | [diff] [blame] | 7348 | "_OBJC_$_PROP_LIST_", |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 7349 | CDecl->getNameAsString()); |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 7350 | |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 7351 | |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 7352 | // Data for initializing _class_ro_t metaclass meta-data |
| 7353 | uint32_t flags = CLS_META; |
| 7354 | std::string InstanceSize; |
| 7355 | std::string InstanceStart; |
| 7356 | |
| 7357 | |
| 7358 | bool classIsHidden = CDecl->getVisibility() == HiddenVisibility; |
| 7359 | if (classIsHidden) |
| 7360 | flags |= OBJC2_CLS_HIDDEN; |
| 7361 | |
| 7362 | if (!CDecl->getSuperClass()) |
| 7363 | // class is root |
| 7364 | flags |= CLS_ROOT; |
| 7365 | InstanceSize = "sizeof(struct _class_t)"; |
| 7366 | InstanceStart = InstanceSize; |
| 7367 | Write__class_ro_t_initializer(Context, Result, flags, |
| 7368 | InstanceStart, InstanceSize, |
| 7369 | ClassMethods, |
| 7370 | 0, |
| 7371 | 0, |
| 7372 | 0, |
| 7373 | "_OBJC_METACLASS_RO_$_", |
| 7374 | CDecl->getNameAsString()); |
| 7375 | |
| 7376 | |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 7377 | // Data for initializing _class_ro_t meta-data |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 7378 | flags = CLS; |
| 7379 | if (classIsHidden) |
| 7380 | flags |= OBJC2_CLS_HIDDEN; |
| 7381 | |
| 7382 | if (hasObjCExceptionAttribute(*Context, CDecl)) |
| 7383 | flags |= CLS_EXCEPTION; |
| 7384 | |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 7385 | if (!CDecl->getSuperClass()) |
| 7386 | // class is root |
| 7387 | flags |= CLS_ROOT; |
| 7388 | |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 7389 | InstanceSize.clear(); |
| 7390 | InstanceStart.clear(); |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 7391 | if (!ObjCSynthesizedStructs.count(CDecl)) { |
| 7392 | InstanceSize = "0"; |
| 7393 | InstanceStart = "0"; |
| 7394 | } |
| 7395 | else { |
| 7396 | InstanceSize = "sizeof(struct "; |
| 7397 | InstanceSize += CDecl->getNameAsString(); |
| 7398 | InstanceSize += "_IMPL)"; |
| 7399 | |
| 7400 | ObjCIvarDecl *IVD = CDecl->all_declared_ivar_begin(); |
| 7401 | if (IVD) { |
Fariborz Jahanian | acee1c9 | 2012-04-11 21:12:36 +0000 | [diff] [blame] | 7402 | RewriteIvarOffsetComputation(IVD, InstanceStart); |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 7403 | } |
| 7404 | else |
| 7405 | InstanceStart = InstanceSize; |
| 7406 | } |
| 7407 | Write__class_ro_t_initializer(Context, Result, flags, |
| 7408 | InstanceStart, InstanceSize, |
| 7409 | InstanceMethods, |
| 7410 | RefedProtocols, |
| 7411 | IVars, |
| 7412 | ClassProperties, |
| 7413 | "_OBJC_CLASS_RO_$_", |
| 7414 | CDecl->getNameAsString()); |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 7415 | |
| 7416 | Write_class_t(Context, Result, |
| 7417 | "OBJC_METACLASS_$_", |
| 7418 | CDecl, /*metaclass*/true); |
| 7419 | |
| 7420 | Write_class_t(Context, Result, |
| 7421 | "OBJC_CLASS_$_", |
| 7422 | CDecl, /*metaclass*/false); |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 7423 | |
| 7424 | if (ImplementationIsNonLazy(IDecl)) |
| 7425 | DefinedNonLazyClasses.push_back(CDecl); |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 7426 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7427 | } |
| 7428 | |
Fariborz Jahanian | e033578 | 2012-03-27 18:41:05 +0000 | [diff] [blame] | 7429 | void RewriteModernObjC::RewriteClassSetupInitHook(std::string &Result) { |
| 7430 | int ClsDefCount = ClassImplementation.size(); |
| 7431 | if (!ClsDefCount) |
| 7432 | return; |
| 7433 | Result += "#pragma section(\".objc_inithooks$B\", long, read, write)\n"; |
| 7434 | Result += "__declspec(allocate(\".objc_inithooks$B\")) "; |
| 7435 | Result += "static void *OBJC_CLASS_SETUP[] = {\n"; |
| 7436 | for (int i = 0; i < ClsDefCount; i++) { |
| 7437 | ObjCImplementationDecl *IDecl = ClassImplementation[i]; |
| 7438 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
| 7439 | Result += "\t(void *)&OBJC_CLASS_SETUP_$_"; |
| 7440 | Result += CDecl->getName(); Result += ",\n"; |
| 7441 | } |
| 7442 | Result += "};\n"; |
| 7443 | } |
| 7444 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7445 | void RewriteModernObjC::RewriteMetaDataIntoBuffer(std::string &Result) { |
| 7446 | int ClsDefCount = ClassImplementation.size(); |
| 7447 | int CatDefCount = CategoryImplementation.size(); |
| 7448 | |
| 7449 | // For each implemented class, write out all its meta data. |
| 7450 | for (int i = 0; i < ClsDefCount; i++) |
| 7451 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
| 7452 | |
Fariborz Jahanian | e033578 | 2012-03-27 18:41:05 +0000 | [diff] [blame] | 7453 | RewriteClassSetupInitHook(Result); |
| 7454 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7455 | // For each implemented category, write out all its meta data. |
| 7456 | for (int i = 0; i < CatDefCount; i++) |
| 7457 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
| 7458 | |
Fariborz Jahanian | e033578 | 2012-03-27 18:41:05 +0000 | [diff] [blame] | 7459 | RewriteCategorySetupInitHook(Result); |
| 7460 | |
Fariborz Jahanian | df79567 | 2012-02-17 00:06:14 +0000 | [diff] [blame] | 7461 | if (ClsDefCount > 0) { |
Fariborz Jahanian | 1ca052c | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 7462 | if (LangOpts.MicrosoftExt) |
| 7463 | Result += "__declspec(allocate(\".objc_classlist$B\")) "; |
Fariborz Jahanian | df79567 | 2012-02-17 00:06:14 +0000 | [diff] [blame] | 7464 | Result += "static struct _class_t *L_OBJC_LABEL_CLASS_$ ["; |
| 7465 | Result += llvm::utostr(ClsDefCount); Result += "]"; |
| 7466 | Result += |
| 7467 | " __attribute__((used, section (\"__DATA, __objc_classlist," |
| 7468 | "regular,no_dead_strip\")))= {\n"; |
| 7469 | for (int i = 0; i < ClsDefCount; i++) { |
| 7470 | Result += "\t&OBJC_CLASS_$_"; |
| 7471 | Result += ClassImplementation[i]->getNameAsString(); |
| 7472 | Result += ",\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7473 | } |
Fariborz Jahanian | df79567 | 2012-02-17 00:06:14 +0000 | [diff] [blame] | 7474 | Result += "};\n"; |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 7475 | |
| 7476 | if (!DefinedNonLazyClasses.empty()) { |
| 7477 | if (LangOpts.MicrosoftExt) |
| 7478 | Result += "__declspec(allocate(\".objc_nlclslist$B\")) \n"; |
| 7479 | Result += "static struct _class_t *_OBJC_LABEL_NONLAZY_CLASS_$[] = {\n\t"; |
| 7480 | for (unsigned i = 0, e = DefinedNonLazyClasses.size(); i < e; i++) { |
| 7481 | Result += "\t&OBJC_CLASS_$_"; Result += DefinedNonLazyClasses[i]->getNameAsString(); |
| 7482 | Result += ",\n"; |
| 7483 | } |
| 7484 | Result += "};\n"; |
| 7485 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7486 | } |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7487 | |
| 7488 | if (CatDefCount > 0) { |
Fariborz Jahanian | 1ca052c | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 7489 | if (LangOpts.MicrosoftExt) |
| 7490 | Result += "__declspec(allocate(\".objc_catlist$B\")) "; |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7491 | Result += "static struct _category_t *L_OBJC_LABEL_CATEGORY_$ ["; |
| 7492 | Result += llvm::utostr(CatDefCount); Result += "]"; |
| 7493 | Result += |
| 7494 | " __attribute__((used, section (\"__DATA, __objc_catlist," |
| 7495 | "regular,no_dead_strip\")))= {\n"; |
| 7496 | for (int i = 0; i < CatDefCount; i++) { |
| 7497 | Result += "\t&_OBJC_$_CATEGORY_"; |
| 7498 | Result += |
| 7499 | CategoryImplementation[i]->getClassInterface()->getNameAsString(); |
| 7500 | Result += "_$_"; |
| 7501 | Result += CategoryImplementation[i]->getNameAsString(); |
| 7502 | Result += ",\n"; |
| 7503 | } |
| 7504 | Result += "};\n"; |
| 7505 | } |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 7506 | |
| 7507 | if (!DefinedNonLazyCategories.empty()) { |
| 7508 | if (LangOpts.MicrosoftExt) |
| 7509 | Result += "__declspec(allocate(\".objc_nlcatlist$B\")) \n"; |
| 7510 | Result += "static struct _category_t *_OBJC_LABEL_NONLAZY_CATEGORY_$[] = {\n\t"; |
| 7511 | for (unsigned i = 0, e = DefinedNonLazyCategories.size(); i < e; i++) { |
| 7512 | Result += "\t&_OBJC_$_CATEGORY_"; |
| 7513 | Result += |
| 7514 | DefinedNonLazyCategories[i]->getClassInterface()->getNameAsString(); |
| 7515 | Result += "_$_"; |
| 7516 | Result += DefinedNonLazyCategories[i]->getNameAsString(); |
| 7517 | Result += ",\n"; |
| 7518 | } |
| 7519 | Result += "};\n"; |
| 7520 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7521 | } |
| 7522 | |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 7523 | void RewriteModernObjC::WriteImageInfo(std::string &Result) { |
| 7524 | if (LangOpts.MicrosoftExt) |
| 7525 | Result += "__declspec(allocate(\".objc_imageinfo$B\")) \n"; |
| 7526 | |
| 7527 | Result += "static struct IMAGE_INFO { unsigned version; unsigned flag; } "; |
| 7528 | // version 0, ObjCABI is 2 |
Fariborz Jahanian | 30650eb | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 7529 | Result += "_OBJC_IMAGE_INFO = { 0, 2 };\n"; |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 7530 | } |
| 7531 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7532 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
| 7533 | /// implementation. |
| 7534 | void RewriteModernObjC::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
| 7535 | std::string &Result) { |
Fariborz Jahanian | de5d946 | 2012-03-14 18:09:23 +0000 | [diff] [blame] | 7536 | WriteModernMetadataDeclarations(Context, Result); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7537 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
| 7538 | // Find category declaration for this implementation. |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 7539 | ObjCCategoryDecl *CDecl |
| 7540 | = ClassDecl->FindCategoryDeclaration(IDecl->getIdentifier()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7541 | |
| 7542 | std::string FullCategoryName = ClassDecl->getNameAsString(); |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7543 | FullCategoryName += "_$_"; |
| 7544 | FullCategoryName += CDecl->getNameAsString(); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7545 | |
| 7546 | // Build _objc_method_list for class's instance methods if needed |
| 7547 | SmallVector<ObjCMethodDecl *, 32> |
| 7548 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
| 7549 | |
| 7550 | // If any of our property implementations have associated getters or |
| 7551 | // setters, produce metadata for them as well. |
| 7552 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 7553 | PropEnd = IDecl->propimpl_end(); |
| 7554 | Prop != PropEnd; ++Prop) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7555 | if (Prop->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7556 | continue; |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7557 | if (!Prop->getPropertyIvarDecl()) |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7558 | continue; |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7559 | ObjCPropertyDecl *PD = Prop->getPropertyDecl(); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7560 | if (!PD) |
| 7561 | continue; |
| 7562 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 7563 | InstanceMethods.push_back(Getter); |
| 7564 | if (PD->isReadOnly()) |
| 7565 | continue; |
| 7566 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 7567 | InstanceMethods.push_back(Setter); |
| 7568 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7569 | |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7570 | Write_method_list_t_initializer(*this, Context, Result, InstanceMethods, |
| 7571 | "_OBJC_$_CATEGORY_INSTANCE_METHODS_", |
| 7572 | FullCategoryName, true); |
| 7573 | |
| 7574 | SmallVector<ObjCMethodDecl *, 32> |
| 7575 | ClassMethods(IDecl->classmeth_begin(), IDecl->classmeth_end()); |
| 7576 | |
| 7577 | Write_method_list_t_initializer(*this, Context, Result, ClassMethods, |
| 7578 | "_OBJC_$_CATEGORY_CLASS_METHODS_", |
| 7579 | FullCategoryName, true); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7580 | |
| 7581 | // Protocols referenced in class declaration? |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7582 | // Protocol's super protocol list |
| 7583 | std::vector<ObjCProtocolDecl *> RefedProtocols; |
Argyrios Kyrtzidis | a5f4441 | 2012-03-13 01:09:41 +0000 | [diff] [blame] | 7584 | for (ObjCInterfaceDecl::protocol_iterator I = CDecl->protocol_begin(), |
| 7585 | E = CDecl->protocol_end(); |
| 7586 | |
| 7587 | I != E; ++I) { |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7588 | RefedProtocols.push_back(*I); |
| 7589 | // Must write out all protocol definitions in current qualifier list, |
| 7590 | // and in their nested qualifiers before writing out current definition. |
| 7591 | RewriteObjCProtocolMetaData(*I, Result); |
| 7592 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7593 | |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7594 | Write_protocol_list_initializer(Context, Result, |
| 7595 | RefedProtocols, |
| 7596 | "_OBJC_CATEGORY_PROTOCOLS_$_", |
| 7597 | FullCategoryName); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7598 | |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7599 | // Protocol's property metadata. |
| 7600 | std::vector<ObjCPropertyDecl *> ClassProperties; |
| 7601 | for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(), |
| 7602 | E = CDecl->prop_end(); I != E; ++I) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 7603 | ClassProperties.push_back(*I); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7604 | |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7605 | Write_prop_list_t_initializer(*this, Context, Result, ClassProperties, |
Fariborz Jahanian | ebfa272 | 2012-05-03 23:19:33 +0000 | [diff] [blame] | 7606 | /* Container */IDecl, |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7607 | "_OBJC_$_PROP_LIST_", |
| 7608 | FullCategoryName); |
| 7609 | |
| 7610 | Write_category_t(*this, Context, Result, |
Fariborz Jahanian | e033578 | 2012-03-27 18:41:05 +0000 | [diff] [blame] | 7611 | CDecl, |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 7612 | ClassDecl, |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7613 | InstanceMethods, |
| 7614 | ClassMethods, |
| 7615 | RefedProtocols, |
| 7616 | ClassProperties); |
| 7617 | |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 7618 | // Determine if this category is also "non-lazy". |
| 7619 | if (ImplementationIsNonLazy(IDecl)) |
| 7620 | DefinedNonLazyCategories.push_back(CDecl); |
Fariborz Jahanian | e033578 | 2012-03-27 18:41:05 +0000 | [diff] [blame] | 7621 | |
| 7622 | } |
| 7623 | |
| 7624 | void RewriteModernObjC::RewriteCategorySetupInitHook(std::string &Result) { |
| 7625 | int CatDefCount = CategoryImplementation.size(); |
| 7626 | if (!CatDefCount) |
| 7627 | return; |
| 7628 | Result += "#pragma section(\".objc_inithooks$B\", long, read, write)\n"; |
| 7629 | Result += "__declspec(allocate(\".objc_inithooks$B\")) "; |
| 7630 | Result += "static void *OBJC_CATEGORY_SETUP[] = {\n"; |
| 7631 | for (int i = 0; i < CatDefCount; i++) { |
| 7632 | ObjCCategoryImplDecl *IDecl = CategoryImplementation[i]; |
| 7633 | ObjCCategoryDecl *CatDecl= IDecl->getCategoryDecl(); |
| 7634 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
| 7635 | Result += "\t(void *)&OBJC_CATEGORY_SETUP_$_"; |
| 7636 | Result += ClassDecl->getName(); |
| 7637 | Result += "_$_"; |
| 7638 | Result += CatDecl->getName(); |
| 7639 | Result += ",\n"; |
| 7640 | } |
| 7641 | Result += "};\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7642 | } |
| 7643 | |
| 7644 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
| 7645 | /// class methods. |
| 7646 | template<typename MethodIterator> |
| 7647 | void RewriteModernObjC::RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 7648 | MethodIterator MethodEnd, |
| 7649 | bool IsInstanceMethod, |
| 7650 | StringRef prefix, |
| 7651 | StringRef ClassName, |
| 7652 | std::string &Result) { |
| 7653 | if (MethodBegin == MethodEnd) return; |
| 7654 | |
| 7655 | if (!objc_impl_method) { |
| 7656 | /* struct _objc_method { |
| 7657 | SEL _cmd; |
| 7658 | char *method_types; |
| 7659 | void *_imp; |
| 7660 | } |
| 7661 | */ |
| 7662 | Result += "\nstruct _objc_method {\n"; |
| 7663 | Result += "\tSEL _cmd;\n"; |
| 7664 | Result += "\tchar *method_types;\n"; |
| 7665 | Result += "\tvoid *_imp;\n"; |
| 7666 | Result += "};\n"; |
| 7667 | |
| 7668 | objc_impl_method = true; |
| 7669 | } |
| 7670 | |
| 7671 | // Build _objc_method_list for class's methods if needed |
| 7672 | |
| 7673 | /* struct { |
| 7674 | struct _objc_method_list *next_method; |
| 7675 | int method_count; |
| 7676 | struct _objc_method method_list[]; |
| 7677 | } |
| 7678 | */ |
| 7679 | unsigned NumMethods = std::distance(MethodBegin, MethodEnd); |
Fariborz Jahanian | 1ca052c | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 7680 | Result += "\n"; |
| 7681 | if (LangOpts.MicrosoftExt) { |
| 7682 | if (IsInstanceMethod) |
| 7683 | Result += "__declspec(allocate(\".inst_meth$B\")) "; |
| 7684 | else |
| 7685 | Result += "__declspec(allocate(\".cls_meth$B\")) "; |
| 7686 | } |
| 7687 | Result += "static struct {\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7688 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 7689 | Result += "\tint method_count;\n"; |
| 7690 | Result += "\tstruct _objc_method method_list["; |
| 7691 | Result += utostr(NumMethods); |
| 7692 | Result += "];\n} _OBJC_"; |
| 7693 | Result += prefix; |
| 7694 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 7695 | Result += "_METHODS_"; |
| 7696 | Result += ClassName; |
| 7697 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
| 7698 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 7699 | Result += "_meth\")))= "; |
| 7700 | Result += "{\n\t0, " + utostr(NumMethods) + "\n"; |
| 7701 | |
| 7702 | Result += "\t,{{(SEL)\""; |
| 7703 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
| 7704 | std::string MethodTypeString; |
| 7705 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
| 7706 | Result += "\", \""; |
| 7707 | Result += MethodTypeString; |
| 7708 | Result += "\", (void *)"; |
| 7709 | Result += MethodInternalNames[*MethodBegin]; |
| 7710 | Result += "}\n"; |
| 7711 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 7712 | Result += "\t ,{(SEL)\""; |
| 7713 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
| 7714 | std::string MethodTypeString; |
| 7715 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
| 7716 | Result += "\", \""; |
| 7717 | Result += MethodTypeString; |
| 7718 | Result += "\", (void *)"; |
| 7719 | Result += MethodInternalNames[*MethodBegin]; |
| 7720 | Result += "}\n"; |
| 7721 | } |
| 7722 | Result += "\t }\n};\n"; |
| 7723 | } |
| 7724 | |
| 7725 | Stmt *RewriteModernObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV) { |
| 7726 | SourceRange OldRange = IV->getSourceRange(); |
| 7727 | Expr *BaseExpr = IV->getBase(); |
| 7728 | |
| 7729 | // Rewrite the base, but without actually doing replaces. |
| 7730 | { |
| 7731 | DisableReplaceStmtScope S(*this); |
| 7732 | BaseExpr = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(BaseExpr)); |
| 7733 | IV->setBase(BaseExpr); |
| 7734 | } |
| 7735 | |
| 7736 | ObjCIvarDecl *D = IV->getDecl(); |
| 7737 | |
| 7738 | Expr *Replacement = IV; |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7739 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7740 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
| 7741 | const ObjCInterfaceType *iFaceDecl = |
Fariborz Jahanian | 163d3ce | 2012-05-08 23:54:35 +0000 | [diff] [blame] | 7742 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7743 | assert(iFaceDecl && "RewriteObjCIvarRefExpr - iFaceDecl is null"); |
| 7744 | // lookup which class implements the instance variable. |
| 7745 | ObjCInterfaceDecl *clsDeclared = 0; |
| 7746 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
| 7747 | clsDeclared); |
| 7748 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
| 7749 | |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7750 | // Build name of symbol holding ivar offset. |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 7751 | std::string IvarOffsetName; |
Fariborz Jahanian | cd3b036 | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 7752 | if (D->isBitField()) |
| 7753 | ObjCIvarBitfieldGroupOffset(D, IvarOffsetName); |
| 7754 | else |
| 7755 | WriteInternalIvarName(clsDeclared, D, IvarOffsetName); |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 7756 | |
Fariborz Jahanian | 72c88f1 | 2012-02-22 18:13:25 +0000 | [diff] [blame] | 7757 | ReferencedIvars[clsDeclared].insert(D); |
| 7758 | |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7759 | // cast offset to "char *". |
| 7760 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, |
| 7761 | Context->getPointerType(Context->CharTy), |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7762 | CK_BitCast, |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7763 | BaseExpr); |
| 7764 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
| 7765 | SourceLocation(), &Context->Idents.get(IvarOffsetName), |
| 7766 | Context->UnsignedLongTy, 0, SC_Extern, SC_None); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 7767 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, false, |
| 7768 | Context->UnsignedLongTy, VK_LValue, |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7769 | SourceLocation()); |
| 7770 | BinaryOperator *addExpr = |
| 7771 | new (Context) BinaryOperator(castExpr, DRE, BO_Add, |
| 7772 | Context->getPointerType(Context->CharTy), |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 7773 | VK_RValue, OK_Ordinary, SourceLocation(), false); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7774 | // Don't forget the parens to enforce the proper binding. |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7775 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), |
| 7776 | SourceLocation(), |
| 7777 | addExpr); |
Fariborz Jahanian | 0d6e22a | 2012-02-24 17:35:35 +0000 | [diff] [blame] | 7778 | QualType IvarT = D->getType(); |
Fariborz Jahanian | cd3b036 | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 7779 | if (D->isBitField()) |
| 7780 | IvarT = GetGroupRecordTypeForObjCIvarBitfield(D); |
Fariborz Jahanian | 27fc81b | 2012-04-27 22:48:54 +0000 | [diff] [blame] | 7781 | |
Fariborz Jahanian | f5eac48 | 2012-05-02 17:34:59 +0000 | [diff] [blame] | 7782 | if (!isa<TypedefType>(IvarT) && IvarT->isRecordType()) { |
Fariborz Jahanian | 27fc81b | 2012-04-27 22:48:54 +0000 | [diff] [blame] | 7783 | RecordDecl *RD = IvarT->getAs<RecordType>()->getDecl(); |
Fariborz Jahanian | 8fba894 | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 7784 | RD = RD->getDefinition(); |
| 7785 | if (RD && !RD->getDeclName().getAsIdentifierInfo()) { |
Fariborz Jahanian | 27fc81b | 2012-04-27 22:48:54 +0000 | [diff] [blame] | 7786 | // decltype(((Foo_IMPL*)0)->bar) * |
Fariborz Jahanian | f5eac48 | 2012-05-02 17:34:59 +0000 | [diff] [blame] | 7787 | ObjCContainerDecl *CDecl = |
| 7788 | dyn_cast<ObjCContainerDecl>(D->getDeclContext()); |
| 7789 | // ivar in class extensions requires special treatment. |
| 7790 | if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) |
| 7791 | CDecl = CatDecl->getClassInterface(); |
| 7792 | std::string RecName = CDecl->getName(); |
Fariborz Jahanian | 27fc81b | 2012-04-27 22:48:54 +0000 | [diff] [blame] | 7793 | RecName += "_IMPL"; |
| 7794 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 7795 | SourceLocation(), SourceLocation(), |
| 7796 | &Context->Idents.get(RecName.c_str())); |
| 7797 | QualType PtrStructIMPL = Context->getPointerType(Context->getTagDeclType(RD)); |
| 7798 | unsigned UnsignedIntSize = |
| 7799 | static_cast<unsigned>(Context->getTypeSize(Context->UnsignedIntTy)); |
| 7800 | Expr *Zero = IntegerLiteral::Create(*Context, |
| 7801 | llvm::APInt(UnsignedIntSize, 0), |
| 7802 | Context->UnsignedIntTy, SourceLocation()); |
| 7803 | Zero = NoTypeInfoCStyleCastExpr(Context, PtrStructIMPL, CK_BitCast, Zero); |
| 7804 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 7805 | Zero); |
| 7806 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 7807 | SourceLocation(), |
| 7808 | &Context->Idents.get(D->getNameAsString()), |
| 7809 | IvarT, 0, |
| 7810 | /*BitWidth=*/0, /*Mutable=*/true, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 7811 | ICIS_NoInit); |
Fariborz Jahanian | 27fc81b | 2012-04-27 22:48:54 +0000 | [diff] [blame] | 7812 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
| 7813 | FD->getType(), VK_LValue, |
| 7814 | OK_Ordinary); |
| 7815 | IvarT = Context->getDecltypeType(ME, ME->getType()); |
| 7816 | } |
| 7817 | } |
Fariborz Jahanian | 8e0913d | 2012-02-29 00:26:20 +0000 | [diff] [blame] | 7818 | convertObjCTypeToCStyleType(IvarT); |
Fariborz Jahanian | 0d6e22a | 2012-02-24 17:35:35 +0000 | [diff] [blame] | 7819 | QualType castT = Context->getPointerType(IvarT); |
Fariborz Jahanian | 27fc81b | 2012-04-27 22:48:54 +0000 | [diff] [blame] | 7820 | |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7821 | castExpr = NoTypeInfoCStyleCastExpr(Context, |
| 7822 | castT, |
| 7823 | CK_BitCast, |
| 7824 | PE); |
Fariborz Jahanian | 27fc81b | 2012-04-27 22:48:54 +0000 | [diff] [blame] | 7825 | |
| 7826 | |
Fariborz Jahanian | 8e0913d | 2012-02-29 00:26:20 +0000 | [diff] [blame] | 7827 | Expr *Exp = new (Context) UnaryOperator(castExpr, UO_Deref, IvarT, |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7828 | VK_LValue, OK_Ordinary, |
| 7829 | SourceLocation()); |
| 7830 | PE = new (Context) ParenExpr(OldRange.getBegin(), |
| 7831 | OldRange.getEnd(), |
| 7832 | Exp); |
Fariborz Jahanian | cd3b036 | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 7833 | |
| 7834 | if (D->isBitField()) { |
| 7835 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 7836 | SourceLocation(), |
| 7837 | &Context->Idents.get(D->getNameAsString()), |
| 7838 | D->getType(), 0, |
| 7839 | /*BitWidth=*/D->getBitWidth(), |
| 7840 | /*Mutable=*/true, |
| 7841 | ICIS_NoInit); |
| 7842 | MemberExpr *ME = new (Context) MemberExpr(PE, /*isArrow*/false, FD, SourceLocation(), |
| 7843 | FD->getType(), VK_LValue, |
| 7844 | OK_Ordinary); |
| 7845 | Replacement = ME; |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7846 | |
Fariborz Jahanian | cd3b036 | 2013-02-07 01:53:15 +0000 | [diff] [blame] | 7847 | } |
| 7848 | else |
| 7849 | Replacement = PE; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7850 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7851 | |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7852 | ReplaceStmtWithRange(IV, Replacement, OldRange); |
| 7853 | return Replacement; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7854 | } |