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 | |
| 14 | #include "clang/Rewrite/ASTConsumers.h" |
| 15 | #include "clang/Rewrite/Rewriter.h" |
| 16 | #include "clang/AST/AST.h" |
| 17 | #include "clang/AST/ASTConsumer.h" |
| 18 | #include "clang/AST/ParentMap.h" |
| 19 | #include "clang/Basic/SourceManager.h" |
| 20 | #include "clang/Basic/IdentifierTable.h" |
| 21 | #include "clang/Basic/Diagnostic.h" |
| 22 | #include "clang/Lex/Lexer.h" |
| 23 | #include "llvm/Support/MemoryBuffer.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
| 25 | #include "llvm/ADT/StringExtras.h" |
| 26 | #include "llvm/ADT/SmallPtrSet.h" |
| 27 | #include "llvm/ADT/OwningPtr.h" |
| 28 | #include "llvm/ADT/DenseSet.h" |
| 29 | |
| 30 | using namespace clang; |
| 31 | using llvm::utostr; |
| 32 | |
| 33 | namespace { |
| 34 | class RewriteModernObjC : public ASTConsumer { |
| 35 | protected: |
| 36 | |
| 37 | enum { |
| 38 | BLOCK_FIELD_IS_OBJECT = 3, /* id, NSObject, __attribute__((NSObject)), |
| 39 | block, ... */ |
| 40 | BLOCK_FIELD_IS_BLOCK = 7, /* a block variable */ |
| 41 | BLOCK_FIELD_IS_BYREF = 8, /* the on stack structure holding the |
| 42 | __block variable */ |
| 43 | BLOCK_FIELD_IS_WEAK = 16, /* declared __weak, only used in byref copy |
| 44 | helpers */ |
| 45 | BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose |
| 46 | support routines */ |
| 47 | BLOCK_BYREF_CURRENT_MAX = 256 |
| 48 | }; |
| 49 | |
| 50 | enum { |
| 51 | BLOCK_NEEDS_FREE = (1 << 24), |
| 52 | BLOCK_HAS_COPY_DISPOSE = (1 << 25), |
| 53 | BLOCK_HAS_CXX_OBJ = (1 << 26), |
| 54 | BLOCK_IS_GC = (1 << 27), |
| 55 | BLOCK_IS_GLOBAL = (1 << 28), |
| 56 | BLOCK_HAS_DESCRIPTOR = (1 << 29) |
| 57 | }; |
| 58 | static const int OBJC_ABI_VERSION = 7; |
| 59 | |
| 60 | Rewriter Rewrite; |
| 61 | DiagnosticsEngine &Diags; |
| 62 | const LangOptions &LangOpts; |
| 63 | ASTContext *Context; |
| 64 | SourceManager *SM; |
| 65 | TranslationUnitDecl *TUDecl; |
| 66 | FileID MainFileID; |
| 67 | const char *MainFileStart, *MainFileEnd; |
| 68 | Stmt *CurrentBody; |
| 69 | ParentMap *PropParentMap; // created lazily. |
| 70 | std::string InFileName; |
| 71 | raw_ostream* OutFile; |
| 72 | std::string Preamble; |
| 73 | |
| 74 | TypeDecl *ProtocolTypeDecl; |
| 75 | VarDecl *GlobalVarDecl; |
Fariborz Jahanian | df474ec | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 76 | Expr *GlobalConstructionExp; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 77 | unsigned RewriteFailedDiag; |
Fariborz Jahanian | d13c2c2 | 2012-03-22 19:54:39 +0000 | [diff] [blame] | 78 | unsigned GlobalBlockRewriteFailedDiag; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 79 | // ObjC string constant support. |
| 80 | unsigned NumObjCStringLiterals; |
| 81 | VarDecl *ConstantStringClassReference; |
| 82 | RecordDecl *NSStringRecord; |
| 83 | |
| 84 | // ObjC foreach break/continue generation support. |
| 85 | int BcLabelCount; |
| 86 | |
| 87 | unsigned TryFinallyContainsReturnDiag; |
| 88 | // Needed for super. |
| 89 | ObjCMethodDecl *CurMethodDef; |
| 90 | RecordDecl *SuperStructDecl; |
| 91 | RecordDecl *ConstantStringDecl; |
| 92 | |
| 93 | FunctionDecl *MsgSendFunctionDecl; |
| 94 | FunctionDecl *MsgSendSuperFunctionDecl; |
| 95 | FunctionDecl *MsgSendStretFunctionDecl; |
| 96 | FunctionDecl *MsgSendSuperStretFunctionDecl; |
| 97 | FunctionDecl *MsgSendFpretFunctionDecl; |
| 98 | FunctionDecl *GetClassFunctionDecl; |
| 99 | FunctionDecl *GetMetaClassFunctionDecl; |
| 100 | FunctionDecl *GetSuperClassFunctionDecl; |
| 101 | FunctionDecl *SelGetUidFunctionDecl; |
| 102 | FunctionDecl *CFStringFunctionDecl; |
| 103 | FunctionDecl *SuperContructorFunctionDecl; |
| 104 | FunctionDecl *CurFunctionDef; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 105 | |
| 106 | /* Misc. containers needed for meta-data rewrite. */ |
| 107 | SmallVector<ObjCImplementationDecl *, 8> ClassImplementation; |
| 108 | SmallVector<ObjCCategoryImplDecl *, 8> CategoryImplementation; |
| 109 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCSynthesizedStructs; |
| 110 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> ObjCSynthesizedProtocols; |
Fariborz Jahanian | cf4c60f | 2012-02-17 22:20:12 +0000 | [diff] [blame] | 111 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCWrittenInterfaces; |
Fariborz Jahanian | 8fba894 | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 112 | llvm::SmallPtrSet<TagDecl*, 32> GlobalDefinedTags; |
Fariborz Jahanian | cf4c60f | 2012-02-17 22:20:12 +0000 | [diff] [blame] | 113 | SmallVector<ObjCInterfaceDecl*, 32> ObjCInterfacesSeen; |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 114 | /// DefinedNonLazyClasses - List of defined "non-lazy" classes. |
| 115 | SmallVector<ObjCInterfaceDecl*, 8> DefinedNonLazyClasses; |
| 116 | |
| 117 | /// DefinedNonLazyCategories - List of defined "non-lazy" categories. |
| 118 | llvm::SmallVector<ObjCCategoryDecl*, 8> DefinedNonLazyCategories; |
| 119 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 120 | SmallVector<Stmt *, 32> Stmts; |
| 121 | SmallVector<int, 8> ObjCBcLabelNo; |
| 122 | // Remember all the @protocol(<expr>) expressions. |
| 123 | llvm::SmallPtrSet<ObjCProtocolDecl *, 32> ProtocolExprDecls; |
| 124 | |
| 125 | llvm::DenseSet<uint64_t> CopyDestroyCache; |
| 126 | |
| 127 | // Block expressions. |
| 128 | SmallVector<BlockExpr *, 32> Blocks; |
| 129 | SmallVector<int, 32> InnerDeclRefsCount; |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 130 | SmallVector<DeclRefExpr *, 32> InnerDeclRefs; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 131 | |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 132 | SmallVector<DeclRefExpr *, 32> BlockDeclRefs; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 133 | |
| 134 | // Block related declarations. |
| 135 | SmallVector<ValueDecl *, 8> BlockByCopyDecls; |
| 136 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByCopyDeclsPtrSet; |
| 137 | SmallVector<ValueDecl *, 8> BlockByRefDecls; |
| 138 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByRefDeclsPtrSet; |
| 139 | llvm::DenseMap<ValueDecl *, unsigned> BlockByRefDeclNo; |
| 140 | llvm::SmallPtrSet<ValueDecl *, 8> ImportedBlockDecls; |
| 141 | llvm::SmallPtrSet<VarDecl *, 8> ImportedLocalExternalDecls; |
| 142 | |
| 143 | llvm::DenseMap<BlockExpr *, std::string> RewrittenBlockExprs; |
Fariborz Jahanian | 72c88f1 | 2012-02-22 18:13:25 +0000 | [diff] [blame] | 144 | llvm::DenseMap<ObjCInterfaceDecl *, |
| 145 | llvm::SmallPtrSet<ObjCIvarDecl *, 8> > ReferencedIvars; |
| 146 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 147 | // This maps an original source AST to it's rewritten form. This allows |
| 148 | // us to avoid rewriting the same node twice (which is very uncommon). |
| 149 | // This is needed to support some of the exotic property rewriting. |
| 150 | llvm::DenseMap<Stmt *, Stmt *> ReplacedNodes; |
| 151 | |
| 152 | // Needed for header files being rewritten |
| 153 | bool IsHeader; |
| 154 | bool SilenceRewriteMacroWarning; |
| 155 | bool objc_impl_method; |
| 156 | |
| 157 | bool DisableReplaceStmt; |
| 158 | class DisableReplaceStmtScope { |
| 159 | RewriteModernObjC &R; |
| 160 | bool SavedValue; |
| 161 | |
| 162 | public: |
| 163 | DisableReplaceStmtScope(RewriteModernObjC &R) |
| 164 | : R(R), SavedValue(R.DisableReplaceStmt) { |
| 165 | R.DisableReplaceStmt = true; |
| 166 | } |
| 167 | ~DisableReplaceStmtScope() { |
| 168 | R.DisableReplaceStmt = SavedValue; |
| 169 | } |
| 170 | }; |
| 171 | void InitializeCommon(ASTContext &context); |
| 172 | |
| 173 | public: |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 174 | llvm::DenseMap<ObjCMethodDecl*, std::string> MethodInternalNames; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 175 | // Top Level Driver code. |
| 176 | virtual bool HandleTopLevelDecl(DeclGroupRef D) { |
| 177 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
| 178 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(*I)) { |
| 179 | if (!Class->isThisDeclarationADefinition()) { |
| 180 | RewriteForwardClassDecl(D); |
| 181 | break; |
Fariborz Jahanian | cf4c60f | 2012-02-17 22:20:12 +0000 | [diff] [blame] | 182 | } else { |
| 183 | // Keep track of all interface declarations seen. |
Fariborz Jahanian | f329527 | 2012-02-24 21:42:38 +0000 | [diff] [blame] | 184 | ObjCInterfacesSeen.push_back(Class); |
Fariborz Jahanian | cf4c60f | 2012-02-17 22:20:12 +0000 | [diff] [blame] | 185 | break; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
| 189 | if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(*I)) { |
| 190 | if (!Proto->isThisDeclarationADefinition()) { |
| 191 | RewriteForwardProtocolDecl(D); |
| 192 | break; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | HandleTopLevelSingleDecl(*I); |
| 197 | } |
| 198 | return true; |
| 199 | } |
| 200 | void HandleTopLevelSingleDecl(Decl *D); |
| 201 | void HandleDeclInMainFile(Decl *D); |
| 202 | RewriteModernObjC(std::string inFile, raw_ostream *OS, |
| 203 | DiagnosticsEngine &D, const LangOptions &LOpts, |
| 204 | bool silenceMacroWarn); |
| 205 | |
| 206 | ~RewriteModernObjC() {} |
| 207 | |
| 208 | virtual void HandleTranslationUnit(ASTContext &C); |
| 209 | |
| 210 | void ReplaceStmt(Stmt *Old, Stmt *New) { |
| 211 | Stmt *ReplacingStmt = ReplacedNodes[Old]; |
| 212 | |
| 213 | if (ReplacingStmt) |
| 214 | return; // We can't rewrite the same node twice. |
| 215 | |
| 216 | if (DisableReplaceStmt) |
| 217 | return; |
| 218 | |
| 219 | // If replacement succeeded or warning disabled return with no warning. |
| 220 | if (!Rewrite.ReplaceStmt(Old, New)) { |
| 221 | ReplacedNodes[Old] = New; |
| 222 | return; |
| 223 | } |
| 224 | if (SilenceRewriteMacroWarning) |
| 225 | return; |
| 226 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 227 | << Old->getSourceRange(); |
| 228 | } |
| 229 | |
| 230 | void ReplaceStmtWithRange(Stmt *Old, Stmt *New, SourceRange SrcRange) { |
| 231 | if (DisableReplaceStmt) |
| 232 | return; |
| 233 | |
| 234 | // Measure the old text. |
| 235 | int Size = Rewrite.getRangeSize(SrcRange); |
| 236 | if (Size == -1) { |
| 237 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 238 | << Old->getSourceRange(); |
| 239 | return; |
| 240 | } |
| 241 | // Get the new text. |
| 242 | std::string SStr; |
| 243 | llvm::raw_string_ostream S(SStr); |
| 244 | New->printPretty(S, *Context, 0, PrintingPolicy(LangOpts)); |
| 245 | const std::string &Str = S.str(); |
| 246 | |
| 247 | // If replacement succeeded or warning disabled return with no warning. |
| 248 | if (!Rewrite.ReplaceText(SrcRange.getBegin(), Size, Str)) { |
| 249 | ReplacedNodes[Old] = New; |
| 250 | return; |
| 251 | } |
| 252 | if (SilenceRewriteMacroWarning) |
| 253 | return; |
| 254 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 255 | << Old->getSourceRange(); |
| 256 | } |
| 257 | |
| 258 | void InsertText(SourceLocation Loc, StringRef Str, |
| 259 | bool InsertAfter = true) { |
| 260 | // If insertion succeeded or warning disabled return with no warning. |
| 261 | if (!Rewrite.InsertText(Loc, Str, InsertAfter) || |
| 262 | SilenceRewriteMacroWarning) |
| 263 | return; |
| 264 | |
| 265 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 266 | } |
| 267 | |
| 268 | void ReplaceText(SourceLocation Start, unsigned OrigLength, |
| 269 | StringRef Str) { |
| 270 | // If removal succeeded or warning disabled return with no warning. |
| 271 | if (!Rewrite.ReplaceText(Start, OrigLength, Str) || |
| 272 | SilenceRewriteMacroWarning) |
| 273 | return; |
| 274 | |
| 275 | Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag); |
| 276 | } |
| 277 | |
| 278 | // Syntactic Rewriting. |
| 279 | void RewriteRecordBody(RecordDecl *RD); |
| 280 | void RewriteInclude(); |
| 281 | void RewriteForwardClassDecl(DeclGroupRef D); |
| 282 | void RewriteForwardClassDecl(const llvm::SmallVector<Decl*, 8> &DG); |
| 283 | void RewriteForwardClassEpilogue(ObjCInterfaceDecl *ClassDecl, |
| 284 | const std::string &typedefString); |
| 285 | void RewriteImplementations(); |
| 286 | void RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 287 | ObjCImplementationDecl *IMD, |
| 288 | ObjCCategoryImplDecl *CID); |
| 289 | void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl); |
| 290 | void RewriteImplementationDecl(Decl *Dcl); |
| 291 | void RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl, |
| 292 | ObjCMethodDecl *MDecl, std::string &ResultStr); |
| 293 | void RewriteTypeIntoString(QualType T, std::string &ResultStr, |
| 294 | const FunctionType *&FPRetType); |
| 295 | void RewriteByRefString(std::string &ResultStr, const std::string &Name, |
| 296 | ValueDecl *VD, bool def=false); |
| 297 | void RewriteCategoryDecl(ObjCCategoryDecl *Dcl); |
| 298 | void RewriteProtocolDecl(ObjCProtocolDecl *Dcl); |
| 299 | void RewriteForwardProtocolDecl(DeclGroupRef D); |
| 300 | void RewriteForwardProtocolDecl(const llvm::SmallVector<Decl*, 8> &DG); |
| 301 | void RewriteMethodDeclaration(ObjCMethodDecl *Method); |
| 302 | void RewriteProperty(ObjCPropertyDecl *prop); |
| 303 | void RewriteFunctionDecl(FunctionDecl *FD); |
| 304 | void RewriteBlockPointerType(std::string& Str, QualType Type); |
| 305 | void RewriteBlockPointerTypeVariable(std::string& Str, ValueDecl *VD); |
Fariborz Jahanian | b75f8de | 2012-04-19 00:50:01 +0000 | [diff] [blame] | 306 | void RewriteBlockLiteralFunctionDecl(FunctionDecl *FD); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 307 | void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl); |
| 308 | void RewriteTypeOfDecl(VarDecl *VD); |
| 309 | void RewriteObjCQualifiedInterfaceTypes(Expr *E); |
Fariborz Jahanian | 163d3ce | 2012-05-08 23:54:35 +0000 | [diff] [blame] | 310 | |
| 311 | std::string getIvarAccessString(ObjCIvarDecl *D); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 312 | |
| 313 | // Expression Rewriting. |
| 314 | Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S); |
| 315 | Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp); |
| 316 | Stmt *RewritePropertyOrImplicitGetter(PseudoObjectExpr *Pseudo); |
| 317 | Stmt *RewritePropertyOrImplicitSetter(PseudoObjectExpr *Pseudo); |
| 318 | Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp); |
| 319 | Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp); |
| 320 | Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp); |
Fariborz Jahanian | 5594704 | 2012-03-27 20:17:30 +0000 | [diff] [blame] | 321 | Stmt *RewriteObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Exp); |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 322 | Stmt *RewriteObjCBoxedExpr(ObjCBoxedExpr *Exp); |
Fariborz Jahanian | 86cff60 | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 323 | Stmt *RewriteObjCArrayLiteralExpr(ObjCArrayLiteral *Exp); |
Fariborz Jahanian | e35abe1 | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 324 | Stmt *RewriteObjCDictionaryLiteralExpr(ObjCDictionaryLiteral *Exp); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 325 | Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 326 | Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S); |
Fariborz Jahanian | 042b91d | 2012-05-23 23:47:20 +0000 | [diff] [blame] | 327 | Stmt *RewriteObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 328 | Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
| 329 | Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S); |
| 330 | Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
| 331 | SourceLocation OrigEnd); |
| 332 | Stmt *RewriteBreakStmt(BreakStmt *S); |
| 333 | Stmt *RewriteContinueStmt(ContinueStmt *S); |
| 334 | void RewriteCastExpr(CStyleCastExpr *CE); |
Fariborz Jahanian | f1ee687 | 2012-04-10 00:08:18 +0000 | [diff] [blame] | 335 | void RewriteImplicitCastObjCExpr(CastExpr *IE); |
Fariborz Jahanian | b3f904f | 2012-04-03 17:35:38 +0000 | [diff] [blame] | 336 | void RewriteLinkageSpec(LinkageSpecDecl *LSD); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 337 | |
| 338 | // Block rewriting. |
| 339 | void RewriteBlocksInFunctionProtoType(QualType funcType, NamedDecl *D); |
| 340 | |
| 341 | // Block specific rewrite rules. |
| 342 | void RewriteBlockPointerDecl(NamedDecl *VD); |
Fariborz Jahanian | 4fe261c | 2012-04-24 19:38:45 +0000 | [diff] [blame] | 343 | void RewriteByRefVar(VarDecl *VD, bool firstDecl, bool lastDecl); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 344 | Stmt *RewriteBlockDeclRefExpr(DeclRefExpr *VD); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 345 | Stmt *RewriteLocalVariableExternalStorage(DeclRefExpr *DRE); |
| 346 | void RewriteBlockPointerFunctionArgs(FunctionDecl *FD); |
| 347 | |
| 348 | void RewriteObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
| 349 | std::string &Result); |
| 350 | |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 351 | void RewriteObjCFieldDecl(FieldDecl *fieldDecl, std::string &Result); |
Fariborz Jahanian | b68258f | 2012-05-01 17:46:45 +0000 | [diff] [blame] | 352 | bool IsTagDefinedInsideClass(ObjCContainerDecl *IDecl, TagDecl *Tag, |
Fariborz Jahanian | 8fba894 | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 353 | bool &IsNamedDefinition); |
| 354 | void RewriteLocallyDefinedNamedAggregates(FieldDecl *fieldDecl, |
| 355 | std::string &Result); |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 356 | |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 357 | bool RewriteObjCFieldDeclType(QualType &Type, std::string &Result); |
| 358 | |
Fariborz Jahanian | 72c88f1 | 2012-02-22 18:13:25 +0000 | [diff] [blame] | 359 | void RewriteIvarOffsetSymbols(ObjCInterfaceDecl *CDecl, |
| 360 | std::string &Result); |
| 361 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 362 | virtual void Initialize(ASTContext &context); |
| 363 | |
Benjamin Kramer | 48d798c | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 364 | // Misc. AST transformation routines. Sometimes they end up calling |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 365 | // rewriting routines on the new ASTs. |
| 366 | CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD, |
| 367 | Expr **args, unsigned nargs, |
| 368 | SourceLocation StartLoc=SourceLocation(), |
| 369 | SourceLocation EndLoc=SourceLocation()); |
Fariborz Jahanian | be8d55c | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 370 | |
| 371 | Expr *SynthMsgSendStretCallExpr(FunctionDecl *MsgSendStretFlavor, |
| 372 | QualType msgSendType, |
| 373 | QualType returnType, |
| 374 | SmallVectorImpl<QualType> &ArgTypes, |
| 375 | SmallVectorImpl<Expr*> &MsgExprs, |
| 376 | ObjCMethodDecl *Method); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 377 | |
| 378 | Stmt *SynthMessageExpr(ObjCMessageExpr *Exp, |
| 379 | SourceLocation StartLoc=SourceLocation(), |
| 380 | SourceLocation EndLoc=SourceLocation()); |
| 381 | |
| 382 | void SynthCountByEnumWithState(std::string &buf); |
| 383 | void SynthMsgSendFunctionDecl(); |
| 384 | void SynthMsgSendSuperFunctionDecl(); |
| 385 | void SynthMsgSendStretFunctionDecl(); |
| 386 | void SynthMsgSendFpretFunctionDecl(); |
| 387 | void SynthMsgSendSuperStretFunctionDecl(); |
| 388 | void SynthGetClassFunctionDecl(); |
| 389 | void SynthGetMetaClassFunctionDecl(); |
| 390 | void SynthGetSuperClassFunctionDecl(); |
| 391 | void SynthSelGetUidFunctionDecl(); |
| 392 | void SynthSuperContructorFunctionDecl(); |
| 393 | |
| 394 | // Rewriting metadata |
| 395 | template<typename MethodIterator> |
| 396 | void RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 397 | MethodIterator MethodEnd, |
| 398 | bool IsInstanceMethod, |
| 399 | StringRef prefix, |
| 400 | StringRef ClassName, |
| 401 | std::string &Result); |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 402 | void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol, |
| 403 | std::string &Result); |
Fariborz Jahanian | b4ee880 | 2012-04-30 16:57:52 +0000 | [diff] [blame] | 404 | void RewriteObjCProtocolListMetaData( |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 405 | const ObjCList<ObjCProtocolDecl> &Prots, |
| 406 | StringRef prefix, StringRef ClassName, std::string &Result); |
Fariborz Jahanian | b4ee880 | 2012-04-30 16:57:52 +0000 | [diff] [blame] | 407 | void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 408 | std::string &Result); |
Fariborz Jahanian | b4ee880 | 2012-04-30 16:57:52 +0000 | [diff] [blame] | 409 | void RewriteClassSetupInitHook(std::string &Result); |
Fariborz Jahanian | e033578 | 2012-03-27 18:41:05 +0000 | [diff] [blame] | 410 | |
Fariborz Jahanian | b4ee880 | 2012-04-30 16:57:52 +0000 | [diff] [blame] | 411 | void RewriteMetaDataIntoBuffer(std::string &Result); |
| 412 | void WriteImageInfo(std::string &Result); |
| 413 | void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 414 | std::string &Result); |
Fariborz Jahanian | b4ee880 | 2012-04-30 16:57:52 +0000 | [diff] [blame] | 415 | void RewriteCategorySetupInitHook(std::string &Result); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 416 | |
| 417 | // Rewriting ivar |
Fariborz Jahanian | b4ee880 | 2012-04-30 16:57:52 +0000 | [diff] [blame] | 418 | void RewriteIvarOffsetComputation(ObjCIvarDecl *ivar, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 419 | std::string &Result); |
Fariborz Jahanian | b4ee880 | 2012-04-30 16:57:52 +0000 | [diff] [blame] | 420 | Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 421 | |
| 422 | |
| 423 | std::string SynthesizeByrefCopyDestroyHelper(VarDecl *VD, int flag); |
| 424 | std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
| 425 | StringRef funcName, std::string Tag); |
| 426 | std::string SynthesizeBlockFunc(BlockExpr *CE, int i, |
| 427 | StringRef funcName, std::string Tag); |
| 428 | std::string SynthesizeBlockImpl(BlockExpr *CE, |
| 429 | std::string Tag, std::string Desc); |
| 430 | std::string SynthesizeBlockDescriptor(std::string DescTag, |
| 431 | std::string ImplTag, |
| 432 | int i, StringRef funcName, |
| 433 | unsigned hasCopy); |
| 434 | Stmt *SynthesizeBlockCall(CallExpr *Exp, const Expr* BlockExp); |
| 435 | void SynthesizeBlockLiterals(SourceLocation FunLocStart, |
| 436 | StringRef FunName); |
| 437 | FunctionDecl *SynthBlockInitFunctionDecl(StringRef name); |
| 438 | Stmt *SynthBlockInitExpr(BlockExpr *Exp, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 439 | const SmallVector<DeclRefExpr *, 8> &InnerBlockDeclRefs); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 440 | |
| 441 | // Misc. helper routines. |
| 442 | QualType getProtocolType(); |
| 443 | void WarnAboutReturnGotoStmts(Stmt *S); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 444 | void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND); |
| 445 | void InsertBlockLiteralsWithinFunction(FunctionDecl *FD); |
| 446 | void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD); |
| 447 | |
| 448 | bool IsDeclStmtInForeachHeader(DeclStmt *DS); |
| 449 | void CollectBlockDeclRefInfo(BlockExpr *Exp); |
| 450 | void GetBlockDeclRefExprs(Stmt *S); |
| 451 | void GetInnerBlockDeclRefExprs(Stmt *S, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 452 | SmallVector<DeclRefExpr *, 8> &InnerBlockDeclRefs, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 453 | llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts); |
| 454 | |
| 455 | // We avoid calling Type::isBlockPointerType(), since it operates on the |
| 456 | // canonical type. We only care if the top-level type is a closure pointer. |
| 457 | bool isTopLevelBlockPointerType(QualType T) { |
| 458 | return isa<BlockPointerType>(T); |
| 459 | } |
| 460 | |
| 461 | /// convertBlockPointerToFunctionPointer - Converts a block-pointer type |
| 462 | /// to a function pointer type and upon success, returns true; false |
| 463 | /// otherwise. |
| 464 | bool convertBlockPointerToFunctionPointer(QualType &T) { |
| 465 | if (isTopLevelBlockPointerType(T)) { |
| 466 | const BlockPointerType *BPT = T->getAs<BlockPointerType>(); |
| 467 | T = Context->getPointerType(BPT->getPointeeType()); |
| 468 | return true; |
| 469 | } |
| 470 | return false; |
| 471 | } |
| 472 | |
Fariborz Jahanian | 164d6f8 | 2012-02-13 18:57:49 +0000 | [diff] [blame] | 473 | bool convertObjCTypeToCStyleType(QualType &T); |
| 474 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 475 | bool needToScanForQualifiers(QualType T); |
| 476 | QualType getSuperStructType(); |
| 477 | QualType getConstantStringStructType(); |
| 478 | QualType convertFunctionTypeOfBlocks(const FunctionType *FT); |
| 479 | bool BufferContainsPPDirectives(const char *startBuf, const char *endBuf); |
| 480 | |
| 481 | void convertToUnqualifiedObjCType(QualType &T) { |
Fariborz Jahanian | e35abe1 | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 482 | if (T->isObjCQualifiedIdType()) { |
| 483 | bool isConst = T.isConstQualified(); |
| 484 | T = isConst ? Context->getObjCIdType().withConst() |
| 485 | : Context->getObjCIdType(); |
| 486 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 487 | else if (T->isObjCQualifiedClassType()) |
| 488 | T = Context->getObjCClassType(); |
| 489 | else if (T->isObjCObjectPointerType() && |
| 490 | T->getPointeeType()->isObjCQualifiedInterfaceType()) { |
| 491 | if (const ObjCObjectPointerType * OBJPT = |
| 492 | T->getAsObjCInterfacePointerType()) { |
| 493 | const ObjCInterfaceType *IFaceT = OBJPT->getInterfaceType(); |
| 494 | T = QualType(IFaceT, 0); |
| 495 | T = Context->getPointerType(T); |
| 496 | } |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | // FIXME: This predicate seems like it would be useful to add to ASTContext. |
| 501 | bool isObjCType(QualType T) { |
| 502 | if (!LangOpts.ObjC1 && !LangOpts.ObjC2) |
| 503 | return false; |
| 504 | |
| 505 | QualType OCT = Context->getCanonicalType(T).getUnqualifiedType(); |
| 506 | |
| 507 | if (OCT == Context->getCanonicalType(Context->getObjCIdType()) || |
| 508 | OCT == Context->getCanonicalType(Context->getObjCClassType())) |
| 509 | return true; |
| 510 | |
| 511 | if (const PointerType *PT = OCT->getAs<PointerType>()) { |
| 512 | if (isa<ObjCInterfaceType>(PT->getPointeeType()) || |
| 513 | PT->getPointeeType()->isObjCQualifiedIdType()) |
| 514 | return true; |
| 515 | } |
| 516 | return false; |
| 517 | } |
| 518 | bool PointerTypeTakesAnyBlockArguments(QualType QT); |
| 519 | bool PointerTypeTakesAnyObjCQualifiedType(QualType QT); |
| 520 | void GetExtentOfArgList(const char *Name, const char *&LParen, |
| 521 | const char *&RParen); |
| 522 | |
| 523 | void QuoteDoublequotes(std::string &From, std::string &To) { |
| 524 | for (unsigned i = 0; i < From.length(); i++) { |
| 525 | if (From[i] == '"') |
| 526 | To += "\\\""; |
| 527 | else |
| 528 | To += From[i]; |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | QualType getSimpleFunctionType(QualType result, |
| 533 | const QualType *args, |
| 534 | unsigned numArgs, |
| 535 | bool variadic = false) { |
| 536 | if (result == Context->getObjCInstanceType()) |
| 537 | result = Context->getObjCIdType(); |
| 538 | FunctionProtoType::ExtProtoInfo fpi; |
| 539 | fpi.Variadic = variadic; |
| 540 | return Context->getFunctionType(result, args, numArgs, fpi); |
| 541 | } |
| 542 | |
| 543 | // Helper function: create a CStyleCastExpr with trivial type source info. |
| 544 | CStyleCastExpr* NoTypeInfoCStyleCastExpr(ASTContext *Ctx, QualType Ty, |
| 545 | CastKind Kind, Expr *E) { |
| 546 | TypeSourceInfo *TInfo = Ctx->getTrivialTypeSourceInfo(Ty, SourceLocation()); |
| 547 | return CStyleCastExpr::Create(*Ctx, Ty, VK_RValue, Kind, E, 0, TInfo, |
| 548 | SourceLocation(), SourceLocation()); |
| 549 | } |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 550 | |
| 551 | bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const { |
| 552 | IdentifierInfo* II = &Context->Idents.get("load"); |
| 553 | Selector LoadSel = Context->Selectors.getSelector(0, &II); |
| 554 | return OD->getClassMethod(LoadSel) != 0; |
| 555 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 556 | }; |
| 557 | |
| 558 | } |
| 559 | |
| 560 | void RewriteModernObjC::RewriteBlocksInFunctionProtoType(QualType funcType, |
| 561 | NamedDecl *D) { |
| 562 | if (const FunctionProtoType *fproto |
| 563 | = dyn_cast<FunctionProtoType>(funcType.IgnoreParens())) { |
| 564 | for (FunctionProtoType::arg_type_iterator I = fproto->arg_type_begin(), |
| 565 | E = fproto->arg_type_end(); I && (I != E); ++I) |
| 566 | if (isTopLevelBlockPointerType(*I)) { |
| 567 | // All the args are checked/rewritten. Don't call twice! |
| 568 | RewriteBlockPointerDecl(D); |
| 569 | break; |
| 570 | } |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | void RewriteModernObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) { |
| 575 | const PointerType *PT = funcType->getAs<PointerType>(); |
| 576 | if (PT && PointerTypeTakesAnyBlockArguments(funcType)) |
| 577 | RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND); |
| 578 | } |
| 579 | |
| 580 | static bool IsHeaderFile(const std::string &Filename) { |
| 581 | std::string::size_type DotPos = Filename.rfind('.'); |
| 582 | |
| 583 | if (DotPos == std::string::npos) { |
| 584 | // no file extension |
| 585 | return false; |
| 586 | } |
| 587 | |
| 588 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 589 | // C header: .h |
| 590 | // C++ header: .hh or .H; |
| 591 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
| 592 | } |
| 593 | |
| 594 | RewriteModernObjC::RewriteModernObjC(std::string inFile, raw_ostream* OS, |
| 595 | DiagnosticsEngine &D, const LangOptions &LOpts, |
| 596 | bool silenceMacroWarn) |
| 597 | : Diags(D), LangOpts(LOpts), InFileName(inFile), OutFile(OS), |
| 598 | SilenceRewriteMacroWarning(silenceMacroWarn) { |
| 599 | IsHeader = IsHeaderFile(inFile); |
| 600 | RewriteFailedDiag = Diags.getCustomDiagID(DiagnosticsEngine::Warning, |
| 601 | "rewriting sub-expression within a macro (may not be correct)"); |
Fariborz Jahanian | d13c2c2 | 2012-03-22 19:54:39 +0000 | [diff] [blame] | 602 | // FIXME. This should be an error. But if block is not called, it is OK. And it |
| 603 | // may break including some headers. |
| 604 | GlobalBlockRewriteFailedDiag = Diags.getCustomDiagID(DiagnosticsEngine::Warning, |
| 605 | "rewriting block literal declared in global scope is not implemented"); |
| 606 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 607 | TryFinallyContainsReturnDiag = Diags.getCustomDiagID( |
| 608 | DiagnosticsEngine::Warning, |
| 609 | "rewriter doesn't support user-specified control flow semantics " |
| 610 | "for @try/@finally (code may not execute properly)"); |
| 611 | } |
| 612 | |
| 613 | ASTConsumer *clang::CreateModernObjCRewriter(const std::string& InFile, |
| 614 | raw_ostream* OS, |
| 615 | DiagnosticsEngine &Diags, |
| 616 | const LangOptions &LOpts, |
| 617 | bool SilenceRewriteMacroWarning) { |
| 618 | return new RewriteModernObjC(InFile, OS, Diags, LOpts, SilenceRewriteMacroWarning); |
| 619 | } |
| 620 | |
| 621 | void RewriteModernObjC::InitializeCommon(ASTContext &context) { |
| 622 | Context = &context; |
| 623 | SM = &Context->getSourceManager(); |
| 624 | TUDecl = Context->getTranslationUnitDecl(); |
| 625 | MsgSendFunctionDecl = 0; |
| 626 | MsgSendSuperFunctionDecl = 0; |
| 627 | MsgSendStretFunctionDecl = 0; |
| 628 | MsgSendSuperStretFunctionDecl = 0; |
| 629 | MsgSendFpretFunctionDecl = 0; |
| 630 | GetClassFunctionDecl = 0; |
| 631 | GetMetaClassFunctionDecl = 0; |
| 632 | GetSuperClassFunctionDecl = 0; |
| 633 | SelGetUidFunctionDecl = 0; |
| 634 | CFStringFunctionDecl = 0; |
| 635 | ConstantStringClassReference = 0; |
| 636 | NSStringRecord = 0; |
| 637 | CurMethodDef = 0; |
| 638 | CurFunctionDef = 0; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 639 | GlobalVarDecl = 0; |
Fariborz Jahanian | df474ec | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 640 | GlobalConstructionExp = 0; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 641 | SuperStructDecl = 0; |
| 642 | ProtocolTypeDecl = 0; |
| 643 | ConstantStringDecl = 0; |
| 644 | BcLabelCount = 0; |
| 645 | SuperContructorFunctionDecl = 0; |
| 646 | NumObjCStringLiterals = 0; |
| 647 | PropParentMap = 0; |
| 648 | CurrentBody = 0; |
| 649 | DisableReplaceStmt = false; |
| 650 | objc_impl_method = false; |
| 651 | |
| 652 | // Get the ID and start/end of the main file. |
| 653 | MainFileID = SM->getMainFileID(); |
| 654 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 655 | MainFileStart = MainBuf->getBufferStart(); |
| 656 | MainFileEnd = MainBuf->getBufferEnd(); |
| 657 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 658 | Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOpts()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | //===----------------------------------------------------------------------===// |
| 662 | // Top Level Driver Code |
| 663 | //===----------------------------------------------------------------------===// |
| 664 | |
| 665 | void RewriteModernObjC::HandleTopLevelSingleDecl(Decl *D) { |
| 666 | if (Diags.hasErrorOccurred()) |
| 667 | return; |
| 668 | |
| 669 | // Two cases: either the decl could be in the main file, or it could be in a |
| 670 | // #included file. If the former, rewrite it now. If the later, check to see |
| 671 | // if we rewrote the #include/#import. |
| 672 | SourceLocation Loc = D->getLocation(); |
| 673 | Loc = SM->getExpansionLoc(Loc); |
| 674 | |
| 675 | // If this is for a builtin, ignore it. |
| 676 | if (Loc.isInvalid()) return; |
| 677 | |
| 678 | // Look for built-in declarations that we need to refer during the rewrite. |
| 679 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 680 | RewriteFunctionDecl(FD); |
| 681 | } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) { |
| 682 | // declared in <Foundation/NSString.h> |
| 683 | if (FVD->getName() == "_NSConstantStringClassReference") { |
| 684 | ConstantStringClassReference = FVD; |
| 685 | return; |
| 686 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 687 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
| 688 | RewriteCategoryDecl(CD); |
| 689 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
| 690 | if (PD->isThisDeclarationADefinition()) |
| 691 | RewriteProtocolDecl(PD); |
| 692 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
Fariborz Jahanian | 8e86b2d | 2012-04-04 17:16:15 +0000 | [diff] [blame] | 693 | // FIXME. This will not work in all situations and leaving it out |
| 694 | // is harmless. |
| 695 | // RewriteLinkageSpec(LSD); |
| 696 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 697 | // Recurse into linkage specifications |
| 698 | for (DeclContext::decl_iterator DI = LSD->decls_begin(), |
| 699 | DIEnd = LSD->decls_end(); |
| 700 | DI != DIEnd; ) { |
| 701 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>((*DI))) { |
| 702 | if (!IFace->isThisDeclarationADefinition()) { |
| 703 | SmallVector<Decl *, 8> DG; |
| 704 | SourceLocation StartLoc = IFace->getLocStart(); |
| 705 | do { |
| 706 | if (isa<ObjCInterfaceDecl>(*DI) && |
| 707 | !cast<ObjCInterfaceDecl>(*DI)->isThisDeclarationADefinition() && |
| 708 | StartLoc == (*DI)->getLocStart()) |
| 709 | DG.push_back(*DI); |
| 710 | else |
| 711 | break; |
| 712 | |
| 713 | ++DI; |
| 714 | } while (DI != DIEnd); |
| 715 | RewriteForwardClassDecl(DG); |
| 716 | continue; |
| 717 | } |
Fariborz Jahanian | b3f904f | 2012-04-03 17:35:38 +0000 | [diff] [blame] | 718 | else { |
| 719 | // Keep track of all interface declarations seen. |
| 720 | ObjCInterfacesSeen.push_back(IFace); |
| 721 | ++DI; |
| 722 | continue; |
| 723 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>((*DI))) { |
| 727 | if (!Proto->isThisDeclarationADefinition()) { |
| 728 | SmallVector<Decl *, 8> DG; |
| 729 | SourceLocation StartLoc = Proto->getLocStart(); |
| 730 | do { |
| 731 | if (isa<ObjCProtocolDecl>(*DI) && |
| 732 | !cast<ObjCProtocolDecl>(*DI)->isThisDeclarationADefinition() && |
| 733 | StartLoc == (*DI)->getLocStart()) |
| 734 | DG.push_back(*DI); |
| 735 | else |
| 736 | break; |
| 737 | |
| 738 | ++DI; |
| 739 | } while (DI != DIEnd); |
| 740 | RewriteForwardProtocolDecl(DG); |
| 741 | continue; |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | HandleTopLevelSingleDecl(*DI); |
| 746 | ++DI; |
| 747 | } |
| 748 | } |
| 749 | // If we have a decl in the main file, see if we should rewrite it. |
| 750 | if (SM->isFromMainFile(Loc)) |
| 751 | return HandleDeclInMainFile(D); |
| 752 | } |
| 753 | |
| 754 | //===----------------------------------------------------------------------===// |
| 755 | // Syntactic (non-AST) Rewriting Code |
| 756 | //===----------------------------------------------------------------------===// |
| 757 | |
| 758 | void RewriteModernObjC::RewriteInclude() { |
| 759 | SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID); |
| 760 | StringRef MainBuf = SM->getBufferData(MainFileID); |
| 761 | const char *MainBufStart = MainBuf.begin(); |
| 762 | const char *MainBufEnd = MainBuf.end(); |
| 763 | size_t ImportLen = strlen("import"); |
| 764 | |
| 765 | // Loop over the whole file, looking for includes. |
| 766 | for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { |
| 767 | if (*BufPtr == '#') { |
| 768 | if (++BufPtr == MainBufEnd) |
| 769 | return; |
| 770 | while (*BufPtr == ' ' || *BufPtr == '\t') |
| 771 | if (++BufPtr == MainBufEnd) |
| 772 | return; |
| 773 | if (!strncmp(BufPtr, "import", ImportLen)) { |
| 774 | // replace import with include |
| 775 | SourceLocation ImportLoc = |
| 776 | LocStart.getLocWithOffset(BufPtr-MainBufStart); |
| 777 | ReplaceText(ImportLoc, ImportLen, "include"); |
| 778 | BufPtr += ImportLen; |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | } |
| 783 | |
Fariborz Jahanian | 163d3ce | 2012-05-08 23:54:35 +0000 | [diff] [blame] | 784 | static void WriteInternalIvarName(const ObjCInterfaceDecl *IDecl, |
| 785 | ObjCIvarDecl *IvarDecl, std::string &Result) { |
| 786 | Result += "OBJC_IVAR_$_"; |
| 787 | Result += IDecl->getName(); |
| 788 | Result += "$"; |
| 789 | Result += IvarDecl->getName(); |
| 790 | } |
| 791 | |
| 792 | std::string |
| 793 | RewriteModernObjC::getIvarAccessString(ObjCIvarDecl *D) { |
| 794 | const ObjCInterfaceDecl *ClassDecl = D->getContainingInterface(); |
| 795 | |
| 796 | // Build name of symbol holding ivar offset. |
| 797 | std::string IvarOffsetName; |
| 798 | WriteInternalIvarName(ClassDecl, D, IvarOffsetName); |
| 799 | |
| 800 | |
| 801 | std::string S = "(*("; |
| 802 | QualType IvarT = D->getType(); |
| 803 | |
| 804 | if (!isa<TypedefType>(IvarT) && IvarT->isRecordType()) { |
| 805 | RecordDecl *RD = IvarT->getAs<RecordType>()->getDecl(); |
| 806 | RD = RD->getDefinition(); |
| 807 | if (RD && !RD->getDeclName().getAsIdentifierInfo()) { |
| 808 | // decltype(((Foo_IMPL*)0)->bar) * |
| 809 | ObjCContainerDecl *CDecl = |
| 810 | dyn_cast<ObjCContainerDecl>(D->getDeclContext()); |
| 811 | // ivar in class extensions requires special treatment. |
| 812 | if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) |
| 813 | CDecl = CatDecl->getClassInterface(); |
| 814 | std::string RecName = CDecl->getName(); |
| 815 | RecName += "_IMPL"; |
| 816 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 817 | SourceLocation(), SourceLocation(), |
| 818 | &Context->Idents.get(RecName.c_str())); |
| 819 | QualType PtrStructIMPL = Context->getPointerType(Context->getTagDeclType(RD)); |
| 820 | unsigned UnsignedIntSize = |
| 821 | static_cast<unsigned>(Context->getTypeSize(Context->UnsignedIntTy)); |
| 822 | Expr *Zero = IntegerLiteral::Create(*Context, |
| 823 | llvm::APInt(UnsignedIntSize, 0), |
| 824 | Context->UnsignedIntTy, SourceLocation()); |
| 825 | Zero = NoTypeInfoCStyleCastExpr(Context, PtrStructIMPL, CK_BitCast, Zero); |
| 826 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 827 | Zero); |
| 828 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 829 | SourceLocation(), |
| 830 | &Context->Idents.get(D->getNameAsString()), |
| 831 | IvarT, 0, |
| 832 | /*BitWidth=*/0, /*Mutable=*/true, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 833 | ICIS_NoInit); |
Fariborz Jahanian | 163d3ce | 2012-05-08 23:54:35 +0000 | [diff] [blame] | 834 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
| 835 | FD->getType(), VK_LValue, |
| 836 | OK_Ordinary); |
| 837 | IvarT = Context->getDecltypeType(ME, ME->getType()); |
| 838 | } |
| 839 | } |
| 840 | convertObjCTypeToCStyleType(IvarT); |
| 841 | QualType castT = Context->getPointerType(IvarT); |
| 842 | std::string TypeString(castT.getAsString(Context->getPrintingPolicy())); |
| 843 | S += TypeString; |
| 844 | S += ")"; |
| 845 | |
| 846 | // ((char *)self + IVAR_OFFSET_SYMBOL_NAME) |
| 847 | S += "((char *)self + "; |
| 848 | S += IvarOffsetName; |
| 849 | S += "))"; |
| 850 | ReferencedIvars[const_cast<ObjCInterfaceDecl *>(ClassDecl)].insert(D); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 851 | return S; |
| 852 | } |
| 853 | |
Fariborz Jahanian | 301e2e4 | 2012-05-03 22:52:13 +0000 | [diff] [blame] | 854 | /// mustSynthesizeSetterGetterMethod - returns true if setter or getter has not |
| 855 | /// been found in the class implementation. In this case, it must be synthesized. |
| 856 | static bool mustSynthesizeSetterGetterMethod(ObjCImplementationDecl *IMP, |
| 857 | ObjCPropertyDecl *PD, |
| 858 | bool getter) { |
| 859 | return getter ? !IMP->getInstanceMethod(PD->getGetterName()) |
| 860 | : !IMP->getInstanceMethod(PD->getSetterName()); |
| 861 | |
| 862 | } |
| 863 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 864 | void RewriteModernObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 865 | ObjCImplementationDecl *IMD, |
| 866 | ObjCCategoryImplDecl *CID) { |
| 867 | static bool objcGetPropertyDefined = false; |
| 868 | static bool objcSetPropertyDefined = false; |
Fariborz Jahanian | 301e2e4 | 2012-05-03 22:52:13 +0000 | [diff] [blame] | 869 | SourceLocation startGetterSetterLoc; |
| 870 | |
| 871 | if (PID->getLocStart().isValid()) { |
| 872 | SourceLocation startLoc = PID->getLocStart(); |
| 873 | InsertText(startLoc, "// "); |
| 874 | const char *startBuf = SM->getCharacterData(startLoc); |
| 875 | assert((*startBuf == '@') && "bogus @synthesize location"); |
| 876 | const char *semiBuf = strchr(startBuf, ';'); |
| 877 | assert((*semiBuf == ';') && "@synthesize: can't find ';'"); |
| 878 | startGetterSetterLoc = startLoc.getLocWithOffset(semiBuf-startBuf+1); |
| 879 | } |
| 880 | else |
| 881 | startGetterSetterLoc = IMD ? IMD->getLocEnd() : CID->getLocEnd(); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 882 | |
| 883 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 884 | return; // FIXME: is this correct? |
| 885 | |
| 886 | // Generate the 'getter' function. |
| 887 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 888 | ObjCIvarDecl *OID = PID->getPropertyIvarDecl(); |
| 889 | |
| 890 | if (!OID) |
| 891 | return; |
| 892 | unsigned Attributes = PD->getPropertyAttributes(); |
Fariborz Jahanian | 301e2e4 | 2012-05-03 22:52:13 +0000 | [diff] [blame] | 893 | if (mustSynthesizeSetterGetterMethod(IMD, PD, true /*getter*/)) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 894 | bool GenGetProperty = !(Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) && |
| 895 | (Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
| 896 | ObjCPropertyDecl::OBJC_PR_copy)); |
| 897 | std::string Getr; |
| 898 | if (GenGetProperty && !objcGetPropertyDefined) { |
| 899 | objcGetPropertyDefined = true; |
| 900 | // FIXME. Is this attribute correct in all cases? |
| 901 | Getr = "\nextern \"C\" __declspec(dllimport) " |
| 902 | "id objc_getProperty(id, SEL, long, bool);\n"; |
| 903 | } |
| 904 | RewriteObjCMethodDecl(OID->getContainingInterface(), |
| 905 | PD->getGetterMethodDecl(), Getr); |
| 906 | Getr += "{ "; |
| 907 | // Synthesize an explicit cast to gain access to the ivar. |
| 908 | // See objc-act.c:objc_synthesize_new_getter() for details. |
| 909 | if (GenGetProperty) { |
| 910 | // return objc_getProperty(self, _cmd, offsetof(ClassDecl, OID), 1) |
| 911 | Getr += "typedef "; |
| 912 | const FunctionType *FPRetType = 0; |
| 913 | RewriteTypeIntoString(PD->getGetterMethodDecl()->getResultType(), Getr, |
| 914 | FPRetType); |
| 915 | Getr += " _TYPE"; |
| 916 | if (FPRetType) { |
| 917 | Getr += ")"; // close the precedence "scope" for "*". |
| 918 | |
| 919 | // Now, emit the argument types (if any). |
| 920 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)){ |
| 921 | Getr += "("; |
| 922 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 923 | if (i) Getr += ", "; |
| 924 | std::string ParamStr = FT->getArgType(i).getAsString( |
| 925 | Context->getPrintingPolicy()); |
| 926 | Getr += ParamStr; |
| 927 | } |
| 928 | if (FT->isVariadic()) { |
| 929 | if (FT->getNumArgs()) Getr += ", "; |
| 930 | Getr += "..."; |
| 931 | } |
| 932 | Getr += ")"; |
| 933 | } else |
| 934 | Getr += "()"; |
| 935 | } |
| 936 | Getr += ";\n"; |
| 937 | Getr += "return (_TYPE)"; |
| 938 | Getr += "objc_getProperty(self, _cmd, "; |
| 939 | RewriteIvarOffsetComputation(OID, Getr); |
| 940 | Getr += ", 1)"; |
| 941 | } |
| 942 | else |
| 943 | Getr += "return " + getIvarAccessString(OID); |
| 944 | Getr += "; }"; |
Fariborz Jahanian | 301e2e4 | 2012-05-03 22:52:13 +0000 | [diff] [blame] | 945 | InsertText(startGetterSetterLoc, Getr); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 946 | } |
| 947 | |
Fariborz Jahanian | 301e2e4 | 2012-05-03 22:52:13 +0000 | [diff] [blame] | 948 | if (PD->isReadOnly() || |
| 949 | !mustSynthesizeSetterGetterMethod(IMD, PD, false /*setter*/)) |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 950 | return; |
| 951 | |
| 952 | // Generate the 'setter' function. |
| 953 | std::string Setr; |
| 954 | bool GenSetProperty = Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
| 955 | ObjCPropertyDecl::OBJC_PR_copy); |
| 956 | if (GenSetProperty && !objcSetPropertyDefined) { |
| 957 | objcSetPropertyDefined = true; |
| 958 | // FIXME. Is this attribute correct in all cases? |
| 959 | Setr = "\nextern \"C\" __declspec(dllimport) " |
| 960 | "void objc_setProperty (id, SEL, long, id, bool, bool);\n"; |
| 961 | } |
| 962 | |
| 963 | RewriteObjCMethodDecl(OID->getContainingInterface(), |
| 964 | PD->getSetterMethodDecl(), Setr); |
| 965 | Setr += "{ "; |
| 966 | // Synthesize an explicit cast to initialize the ivar. |
| 967 | // See objc-act.c:objc_synthesize_new_setter() for details. |
| 968 | if (GenSetProperty) { |
| 969 | Setr += "objc_setProperty (self, _cmd, "; |
| 970 | RewriteIvarOffsetComputation(OID, Setr); |
| 971 | Setr += ", (id)"; |
| 972 | Setr += PD->getName(); |
| 973 | Setr += ", "; |
| 974 | if (Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 975 | Setr += "0, "; |
| 976 | else |
| 977 | Setr += "1, "; |
| 978 | if (Attributes & ObjCPropertyDecl::OBJC_PR_copy) |
| 979 | Setr += "1)"; |
| 980 | else |
| 981 | Setr += "0)"; |
| 982 | } |
| 983 | else { |
| 984 | Setr += getIvarAccessString(OID) + " = "; |
| 985 | Setr += PD->getName(); |
| 986 | } |
Fariborz Jahanian | 301e2e4 | 2012-05-03 22:52:13 +0000 | [diff] [blame] | 987 | Setr += "; }\n"; |
| 988 | InsertText(startGetterSetterLoc, Setr); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | static void RewriteOneForwardClassDecl(ObjCInterfaceDecl *ForwardDecl, |
| 992 | std::string &typedefString) { |
| 993 | typedefString += "#ifndef _REWRITER_typedef_"; |
| 994 | typedefString += ForwardDecl->getNameAsString(); |
| 995 | typedefString += "\n"; |
| 996 | typedefString += "#define _REWRITER_typedef_"; |
| 997 | typedefString += ForwardDecl->getNameAsString(); |
| 998 | typedefString += "\n"; |
| 999 | typedefString += "typedef struct objc_object "; |
| 1000 | typedefString += ForwardDecl->getNameAsString(); |
Fariborz Jahanian | c38503b | 2012-03-12 23:58:28 +0000 | [diff] [blame] | 1001 | // typedef struct { } _objc_exc_Classname; |
| 1002 | typedefString += ";\ntypedef struct {} _objc_exc_"; |
| 1003 | typedefString += ForwardDecl->getNameAsString(); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1004 | typedefString += ";\n#endif\n"; |
| 1005 | } |
| 1006 | |
| 1007 | void RewriteModernObjC::RewriteForwardClassEpilogue(ObjCInterfaceDecl *ClassDecl, |
| 1008 | const std::string &typedefString) { |
| 1009 | SourceLocation startLoc = ClassDecl->getLocStart(); |
| 1010 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1011 | const char *semiPtr = strchr(startBuf, ';'); |
| 1012 | // Replace the @class with typedefs corresponding to the classes. |
| 1013 | ReplaceText(startLoc, semiPtr-startBuf+1, typedefString); |
| 1014 | } |
| 1015 | |
| 1016 | void RewriteModernObjC::RewriteForwardClassDecl(DeclGroupRef D) { |
| 1017 | std::string typedefString; |
| 1018 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
| 1019 | ObjCInterfaceDecl *ForwardDecl = cast<ObjCInterfaceDecl>(*I); |
| 1020 | if (I == D.begin()) { |
| 1021 | // Translate to typedef's that forward reference structs with the same name |
| 1022 | // as the class. As a convenience, we include the original declaration |
| 1023 | // as a comment. |
| 1024 | typedefString += "// @class "; |
| 1025 | typedefString += ForwardDecl->getNameAsString(); |
| 1026 | typedefString += ";\n"; |
| 1027 | } |
| 1028 | RewriteOneForwardClassDecl(ForwardDecl, typedefString); |
| 1029 | } |
| 1030 | DeclGroupRef::iterator I = D.begin(); |
| 1031 | RewriteForwardClassEpilogue(cast<ObjCInterfaceDecl>(*I), typedefString); |
| 1032 | } |
| 1033 | |
| 1034 | void RewriteModernObjC::RewriteForwardClassDecl( |
| 1035 | const llvm::SmallVector<Decl*, 8> &D) { |
| 1036 | std::string typedefString; |
| 1037 | for (unsigned i = 0; i < D.size(); i++) { |
| 1038 | ObjCInterfaceDecl *ForwardDecl = cast<ObjCInterfaceDecl>(D[i]); |
| 1039 | if (i == 0) { |
| 1040 | typedefString += "// @class "; |
| 1041 | typedefString += ForwardDecl->getNameAsString(); |
| 1042 | typedefString += ";\n"; |
| 1043 | } |
| 1044 | RewriteOneForwardClassDecl(ForwardDecl, typedefString); |
| 1045 | } |
| 1046 | RewriteForwardClassEpilogue(cast<ObjCInterfaceDecl>(D[0]), typedefString); |
| 1047 | } |
| 1048 | |
| 1049 | void RewriteModernObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) { |
| 1050 | // When method is a synthesized one, such as a getter/setter there is |
| 1051 | // nothing to rewrite. |
| 1052 | if (Method->isImplicit()) |
| 1053 | return; |
| 1054 | SourceLocation LocStart = Method->getLocStart(); |
| 1055 | SourceLocation LocEnd = Method->getLocEnd(); |
| 1056 | |
| 1057 | if (SM->getExpansionLineNumber(LocEnd) > |
| 1058 | SM->getExpansionLineNumber(LocStart)) { |
| 1059 | InsertText(LocStart, "#if 0\n"); |
| 1060 | ReplaceText(LocEnd, 1, ";\n#endif\n"); |
| 1061 | } else { |
| 1062 | InsertText(LocStart, "// "); |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | void RewriteModernObjC::RewriteProperty(ObjCPropertyDecl *prop) { |
| 1067 | SourceLocation Loc = prop->getAtLoc(); |
| 1068 | |
| 1069 | ReplaceText(Loc, 0, "// "); |
| 1070 | // FIXME: handle properties that are declared across multiple lines. |
| 1071 | } |
| 1072 | |
| 1073 | void RewriteModernObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
| 1074 | SourceLocation LocStart = CatDecl->getLocStart(); |
| 1075 | |
| 1076 | // FIXME: handle category headers that are declared across multiple lines. |
Fariborz Jahanian | b68258f | 2012-05-01 17:46:45 +0000 | [diff] [blame] | 1077 | if (CatDecl->getIvarRBraceLoc().isValid()) { |
| 1078 | ReplaceText(LocStart, 1, "/** "); |
| 1079 | ReplaceText(CatDecl->getIvarRBraceLoc(), 1, "**/ "); |
| 1080 | } |
| 1081 | else { |
Fariborz Jahanian | d2aea12 | 2012-02-19 19:00:05 +0000 | [diff] [blame] | 1082 | ReplaceText(LocStart, 0, "// "); |
Fariborz Jahanian | b68258f | 2012-05-01 17:46:45 +0000 | [diff] [blame] | 1083 | } |
Fariborz Jahanian | af30029 | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 1084 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1085 | for (ObjCCategoryDecl::prop_iterator I = CatDecl->prop_begin(), |
| 1086 | E = CatDecl->prop_end(); I != E; ++I) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1087 | RewriteProperty(*I); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1088 | |
| 1089 | for (ObjCCategoryDecl::instmeth_iterator |
| 1090 | I = CatDecl->instmeth_begin(), E = CatDecl->instmeth_end(); |
| 1091 | I != E; ++I) |
| 1092 | RewriteMethodDeclaration(*I); |
| 1093 | for (ObjCCategoryDecl::classmeth_iterator |
| 1094 | I = CatDecl->classmeth_begin(), E = CatDecl->classmeth_end(); |
| 1095 | I != E; ++I) |
| 1096 | RewriteMethodDeclaration(*I); |
| 1097 | |
| 1098 | // Lastly, comment out the @end. |
| 1099 | ReplaceText(CatDecl->getAtEndRange().getBegin(), |
| 1100 | strlen("@end"), "/* @end */"); |
| 1101 | } |
| 1102 | |
| 1103 | void RewriteModernObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
| 1104 | SourceLocation LocStart = PDecl->getLocStart(); |
| 1105 | assert(PDecl->isThisDeclarationADefinition()); |
| 1106 | |
| 1107 | // FIXME: handle protocol headers that are declared across multiple lines. |
| 1108 | ReplaceText(LocStart, 0, "// "); |
| 1109 | |
| 1110 | for (ObjCProtocolDecl::instmeth_iterator |
| 1111 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
| 1112 | I != E; ++I) |
| 1113 | RewriteMethodDeclaration(*I); |
| 1114 | for (ObjCProtocolDecl::classmeth_iterator |
| 1115 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
| 1116 | I != E; ++I) |
| 1117 | RewriteMethodDeclaration(*I); |
| 1118 | |
| 1119 | for (ObjCInterfaceDecl::prop_iterator I = PDecl->prop_begin(), |
| 1120 | E = PDecl->prop_end(); I != E; ++I) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1121 | RewriteProperty(*I); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1122 | |
| 1123 | // Lastly, comment out the @end. |
| 1124 | SourceLocation LocEnd = PDecl->getAtEndRange().getBegin(); |
| 1125 | ReplaceText(LocEnd, strlen("@end"), "/* @end */"); |
| 1126 | |
| 1127 | // Must comment out @optional/@required |
| 1128 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1129 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 1130 | for (const char *p = startBuf; p < endBuf; p++) { |
| 1131 | if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { |
| 1132 | SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf); |
| 1133 | ReplaceText(OptionalLoc, strlen("@optional"), "/* @optional */"); |
| 1134 | |
| 1135 | } |
| 1136 | else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { |
| 1137 | SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf); |
| 1138 | ReplaceText(OptionalLoc, strlen("@required"), "/* @required */"); |
| 1139 | |
| 1140 | } |
| 1141 | } |
| 1142 | } |
| 1143 | |
| 1144 | void RewriteModernObjC::RewriteForwardProtocolDecl(DeclGroupRef D) { |
| 1145 | SourceLocation LocStart = (*D.begin())->getLocStart(); |
| 1146 | if (LocStart.isInvalid()) |
| 1147 | llvm_unreachable("Invalid SourceLocation"); |
| 1148 | // FIXME: handle forward protocol that are declared across multiple lines. |
| 1149 | ReplaceText(LocStart, 0, "// "); |
| 1150 | } |
| 1151 | |
| 1152 | void |
| 1153 | RewriteModernObjC::RewriteForwardProtocolDecl(const llvm::SmallVector<Decl*, 8> &DG) { |
| 1154 | SourceLocation LocStart = DG[0]->getLocStart(); |
| 1155 | if (LocStart.isInvalid()) |
| 1156 | llvm_unreachable("Invalid SourceLocation"); |
| 1157 | // FIXME: handle forward protocol that are declared across multiple lines. |
| 1158 | ReplaceText(LocStart, 0, "// "); |
| 1159 | } |
| 1160 | |
Fariborz Jahanian | b3f904f | 2012-04-03 17:35:38 +0000 | [diff] [blame] | 1161 | void |
| 1162 | RewriteModernObjC::RewriteLinkageSpec(LinkageSpecDecl *LSD) { |
| 1163 | SourceLocation LocStart = LSD->getExternLoc(); |
| 1164 | if (LocStart.isInvalid()) |
| 1165 | llvm_unreachable("Invalid extern SourceLocation"); |
| 1166 | |
| 1167 | ReplaceText(LocStart, 0, "// "); |
| 1168 | if (!LSD->hasBraces()) |
| 1169 | return; |
| 1170 | // FIXME. We don't rewrite well if '{' is not on same line as 'extern'. |
| 1171 | SourceLocation LocRBrace = LSD->getRBraceLoc(); |
| 1172 | if (LocRBrace.isInvalid()) |
| 1173 | llvm_unreachable("Invalid rbrace SourceLocation"); |
| 1174 | ReplaceText(LocRBrace, 0, "// "); |
| 1175 | } |
| 1176 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1177 | void RewriteModernObjC::RewriteTypeIntoString(QualType T, std::string &ResultStr, |
| 1178 | const FunctionType *&FPRetType) { |
| 1179 | if (T->isObjCQualifiedIdType()) |
| 1180 | ResultStr += "id"; |
| 1181 | else if (T->isFunctionPointerType() || |
| 1182 | T->isBlockPointerType()) { |
| 1183 | // needs special handling, since pointer-to-functions have special |
| 1184 | // syntax (where a decaration models use). |
| 1185 | QualType retType = T; |
| 1186 | QualType PointeeTy; |
| 1187 | if (const PointerType* PT = retType->getAs<PointerType>()) |
| 1188 | PointeeTy = PT->getPointeeType(); |
| 1189 | else if (const BlockPointerType *BPT = retType->getAs<BlockPointerType>()) |
| 1190 | PointeeTy = BPT->getPointeeType(); |
| 1191 | if ((FPRetType = PointeeTy->getAs<FunctionType>())) { |
| 1192 | ResultStr += FPRetType->getResultType().getAsString( |
| 1193 | Context->getPrintingPolicy()); |
| 1194 | ResultStr += "(*"; |
| 1195 | } |
| 1196 | } else |
| 1197 | ResultStr += T.getAsString(Context->getPrintingPolicy()); |
| 1198 | } |
| 1199 | |
| 1200 | void RewriteModernObjC::RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl, |
| 1201 | ObjCMethodDecl *OMD, |
| 1202 | std::string &ResultStr) { |
| 1203 | //fprintf(stderr,"In RewriteObjCMethodDecl\n"); |
| 1204 | const FunctionType *FPRetType = 0; |
| 1205 | ResultStr += "\nstatic "; |
| 1206 | RewriteTypeIntoString(OMD->getResultType(), ResultStr, FPRetType); |
| 1207 | ResultStr += " "; |
| 1208 | |
| 1209 | // Unique method name |
| 1210 | std::string NameStr; |
| 1211 | |
| 1212 | if (OMD->isInstanceMethod()) |
| 1213 | NameStr += "_I_"; |
| 1214 | else |
| 1215 | NameStr += "_C_"; |
| 1216 | |
| 1217 | NameStr += IDecl->getNameAsString(); |
| 1218 | NameStr += "_"; |
| 1219 | |
| 1220 | if (ObjCCategoryImplDecl *CID = |
| 1221 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
| 1222 | NameStr += CID->getNameAsString(); |
| 1223 | NameStr += "_"; |
| 1224 | } |
| 1225 | // Append selector names, replacing ':' with '_' |
| 1226 | { |
| 1227 | std::string selString = OMD->getSelector().getAsString(); |
| 1228 | int len = selString.size(); |
| 1229 | for (int i = 0; i < len; i++) |
| 1230 | if (selString[i] == ':') |
| 1231 | selString[i] = '_'; |
| 1232 | NameStr += selString; |
| 1233 | } |
| 1234 | // Remember this name for metadata emission |
| 1235 | MethodInternalNames[OMD] = NameStr; |
| 1236 | ResultStr += NameStr; |
| 1237 | |
| 1238 | // Rewrite arguments |
| 1239 | ResultStr += "("; |
| 1240 | |
| 1241 | // invisible arguments |
| 1242 | if (OMD->isInstanceMethod()) { |
| 1243 | QualType selfTy = Context->getObjCInterfaceType(IDecl); |
| 1244 | selfTy = Context->getPointerType(selfTy); |
| 1245 | if (!LangOpts.MicrosoftExt) { |
| 1246 | if (ObjCSynthesizedStructs.count(const_cast<ObjCInterfaceDecl*>(IDecl))) |
| 1247 | ResultStr += "struct "; |
| 1248 | } |
| 1249 | // When rewriting for Microsoft, explicitly omit the structure name. |
| 1250 | ResultStr += IDecl->getNameAsString(); |
| 1251 | ResultStr += " *"; |
| 1252 | } |
| 1253 | else |
| 1254 | ResultStr += Context->getObjCClassType().getAsString( |
| 1255 | Context->getPrintingPolicy()); |
| 1256 | |
| 1257 | ResultStr += " self, "; |
| 1258 | ResultStr += Context->getObjCSelType().getAsString(Context->getPrintingPolicy()); |
| 1259 | ResultStr += " _cmd"; |
| 1260 | |
| 1261 | // Method arguments. |
| 1262 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 1263 | E = OMD->param_end(); PI != E; ++PI) { |
| 1264 | ParmVarDecl *PDecl = *PI; |
| 1265 | ResultStr += ", "; |
| 1266 | if (PDecl->getType()->isObjCQualifiedIdType()) { |
| 1267 | ResultStr += "id "; |
| 1268 | ResultStr += PDecl->getNameAsString(); |
| 1269 | } else { |
| 1270 | std::string Name = PDecl->getNameAsString(); |
| 1271 | QualType QT = PDecl->getType(); |
| 1272 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 2610f90 | 2012-03-27 16:42:20 +0000 | [diff] [blame] | 1273 | (void)convertBlockPointerToFunctionPointer(QT); |
| 1274 | QT.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1275 | ResultStr += Name; |
| 1276 | } |
| 1277 | } |
| 1278 | if (OMD->isVariadic()) |
| 1279 | ResultStr += ", ..."; |
| 1280 | ResultStr += ") "; |
| 1281 | |
| 1282 | if (FPRetType) { |
| 1283 | ResultStr += ")"; // close the precedence "scope" for "*". |
| 1284 | |
| 1285 | // Now, emit the argument types (if any). |
| 1286 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) { |
| 1287 | ResultStr += "("; |
| 1288 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 1289 | if (i) ResultStr += ", "; |
| 1290 | std::string ParamStr = FT->getArgType(i).getAsString( |
| 1291 | Context->getPrintingPolicy()); |
| 1292 | ResultStr += ParamStr; |
| 1293 | } |
| 1294 | if (FT->isVariadic()) { |
| 1295 | if (FT->getNumArgs()) ResultStr += ", "; |
| 1296 | ResultStr += "..."; |
| 1297 | } |
| 1298 | ResultStr += ")"; |
| 1299 | } else { |
| 1300 | ResultStr += "()"; |
| 1301 | } |
| 1302 | } |
| 1303 | } |
| 1304 | void RewriteModernObjC::RewriteImplementationDecl(Decl *OID) { |
| 1305 | ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID); |
| 1306 | ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID); |
| 1307 | |
Fariborz Jahanian | d2aea12 | 2012-02-19 19:00:05 +0000 | [diff] [blame] | 1308 | if (IMD) { |
Fariborz Jahanian | b68258f | 2012-05-01 17:46:45 +0000 | [diff] [blame] | 1309 | if (IMD->getIvarRBraceLoc().isValid()) { |
| 1310 | ReplaceText(IMD->getLocStart(), 1, "/** "); |
| 1311 | ReplaceText(IMD->getIvarRBraceLoc(), 1, "**/ "); |
Fariborz Jahanian | d2aea12 | 2012-02-19 19:00:05 +0000 | [diff] [blame] | 1312 | } |
Fariborz Jahanian | b68258f | 2012-05-01 17:46:45 +0000 | [diff] [blame] | 1313 | else { |
| 1314 | InsertText(IMD->getLocStart(), "// "); |
| 1315 | } |
Fariborz Jahanian | d2aea12 | 2012-02-19 19:00:05 +0000 | [diff] [blame] | 1316 | } |
| 1317 | else |
| 1318 | InsertText(CID->getLocStart(), "// "); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1319 | |
| 1320 | for (ObjCCategoryImplDecl::instmeth_iterator |
| 1321 | I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(), |
| 1322 | E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); |
| 1323 | I != E; ++I) { |
| 1324 | std::string ResultStr; |
| 1325 | ObjCMethodDecl *OMD = *I; |
| 1326 | RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr); |
| 1327 | SourceLocation LocStart = OMD->getLocStart(); |
| 1328 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
| 1329 | |
| 1330 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1331 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 1332 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
| 1333 | } |
| 1334 | |
| 1335 | for (ObjCCategoryImplDecl::classmeth_iterator |
| 1336 | I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(), |
| 1337 | E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); |
| 1338 | I != E; ++I) { |
| 1339 | std::string ResultStr; |
| 1340 | ObjCMethodDecl *OMD = *I; |
| 1341 | RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr); |
| 1342 | SourceLocation LocStart = OMD->getLocStart(); |
| 1343 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
| 1344 | |
| 1345 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1346 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 1347 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
| 1348 | } |
| 1349 | for (ObjCCategoryImplDecl::propimpl_iterator |
| 1350 | I = IMD ? IMD->propimpl_begin() : CID->propimpl_begin(), |
| 1351 | E = IMD ? IMD->propimpl_end() : CID->propimpl_end(); |
| 1352 | I != E; ++I) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1353 | RewritePropertyImplDecl(*I, IMD, CID); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | InsertText(IMD ? IMD->getLocEnd() : CID->getLocEnd(), "// "); |
| 1357 | } |
| 1358 | |
| 1359 | void RewriteModernObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { |
Fariborz Jahanian | 8f1fed0 | 2012-02-11 20:10:52 +0000 | [diff] [blame] | 1360 | // Do not synthesize more than once. |
| 1361 | if (ObjCSynthesizedStructs.count(ClassDecl)) |
| 1362 | return; |
| 1363 | // Make sure super class's are written before current class is written. |
| 1364 | ObjCInterfaceDecl *SuperClass = ClassDecl->getSuperClass(); |
| 1365 | while (SuperClass) { |
| 1366 | RewriteInterfaceDecl(SuperClass); |
| 1367 | SuperClass = SuperClass->getSuperClass(); |
| 1368 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1369 | std::string ResultStr; |
Fariborz Jahanian | cf4c60f | 2012-02-17 22:20:12 +0000 | [diff] [blame] | 1370 | if (!ObjCWrittenInterfaces.count(ClassDecl->getCanonicalDecl())) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1371 | // we haven't seen a forward decl - generate a typedef. |
Fariborz Jahanian | c38503b | 2012-03-12 23:58:28 +0000 | [diff] [blame] | 1372 | RewriteOneForwardClassDecl(ClassDecl, ResultStr); |
Fariborz Jahanian | 72c88f1 | 2012-02-22 18:13:25 +0000 | [diff] [blame] | 1373 | RewriteIvarOffsetSymbols(ClassDecl, ResultStr); |
| 1374 | |
Fariborz Jahanian | 4339bb3 | 2012-02-15 22:01:47 +0000 | [diff] [blame] | 1375 | RewriteObjCInternalStruct(ClassDecl, ResultStr); |
Fariborz Jahanian | cf4c60f | 2012-02-17 22:20:12 +0000 | [diff] [blame] | 1376 | // Mark this typedef as having been written into its c++ equivalent. |
| 1377 | ObjCWrittenInterfaces.insert(ClassDecl->getCanonicalDecl()); |
Fariborz Jahanian | 4339bb3 | 2012-02-15 22:01:47 +0000 | [diff] [blame] | 1378 | |
| 1379 | for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(), |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1380 | E = ClassDecl->prop_end(); I != E; ++I) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1381 | RewriteProperty(*I); |
Fariborz Jahanian | 4339bb3 | 2012-02-15 22:01:47 +0000 | [diff] [blame] | 1382 | for (ObjCInterfaceDecl::instmeth_iterator |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1383 | I = ClassDecl->instmeth_begin(), E = ClassDecl->instmeth_end(); |
Fariborz Jahanian | 4339bb3 | 2012-02-15 22:01:47 +0000 | [diff] [blame] | 1384 | I != E; ++I) |
| 1385 | RewriteMethodDeclaration(*I); |
| 1386 | for (ObjCInterfaceDecl::classmeth_iterator |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1387 | I = ClassDecl->classmeth_begin(), E = ClassDecl->classmeth_end(); |
Fariborz Jahanian | 4339bb3 | 2012-02-15 22:01:47 +0000 | [diff] [blame] | 1388 | I != E; ++I) |
| 1389 | RewriteMethodDeclaration(*I); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1390 | |
Fariborz Jahanian | 4339bb3 | 2012-02-15 22:01:47 +0000 | [diff] [blame] | 1391 | // Lastly, comment out the @end. |
| 1392 | ReplaceText(ClassDecl->getAtEndRange().getBegin(), strlen("@end"), |
| 1393 | "/* @end */"); |
| 1394 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | Stmt *RewriteModernObjC::RewritePropertyOrImplicitSetter(PseudoObjectExpr *PseudoOp) { |
| 1398 | SourceRange OldRange = PseudoOp->getSourceRange(); |
| 1399 | |
| 1400 | // We just magically know some things about the structure of this |
| 1401 | // expression. |
| 1402 | ObjCMessageExpr *OldMsg = |
| 1403 | cast<ObjCMessageExpr>(PseudoOp->getSemanticExpr( |
| 1404 | PseudoOp->getNumSemanticExprs() - 1)); |
| 1405 | |
| 1406 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 1407 | // we need to suppress rewriting the sub-statements. |
Fariborz Jahanian | 88ec610 | 2012-04-10 22:06:54 +0000 | [diff] [blame] | 1408 | Expr *Base; |
| 1409 | SmallVector<Expr*, 2> Args; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1410 | { |
| 1411 | DisableReplaceStmtScope S(*this); |
| 1412 | |
| 1413 | // Rebuild the base expression if we have one. |
| 1414 | Base = 0; |
| 1415 | if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) { |
| 1416 | Base = OldMsg->getInstanceReceiver(); |
| 1417 | Base = cast<OpaqueValueExpr>(Base)->getSourceExpr(); |
| 1418 | Base = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Base)); |
| 1419 | } |
Fariborz Jahanian | 88ec610 | 2012-04-10 22:06:54 +0000 | [diff] [blame] | 1420 | |
| 1421 | unsigned numArgs = OldMsg->getNumArgs(); |
| 1422 | for (unsigned i = 0; i < numArgs; i++) { |
| 1423 | Expr *Arg = OldMsg->getArg(i); |
| 1424 | if (isa<OpaqueValueExpr>(Arg)) |
| 1425 | Arg = cast<OpaqueValueExpr>(Arg)->getSourceExpr(); |
| 1426 | Arg = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Arg)); |
| 1427 | Args.push_back(Arg); |
| 1428 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | // TODO: avoid this copy. |
| 1432 | SmallVector<SourceLocation, 1> SelLocs; |
| 1433 | OldMsg->getSelectorLocs(SelLocs); |
| 1434 | |
| 1435 | ObjCMessageExpr *NewMsg = 0; |
| 1436 | switch (OldMsg->getReceiverKind()) { |
| 1437 | case ObjCMessageExpr::Class: |
| 1438 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1439 | OldMsg->getValueKind(), |
| 1440 | OldMsg->getLeftLoc(), |
| 1441 | OldMsg->getClassReceiverTypeInfo(), |
| 1442 | OldMsg->getSelector(), |
| 1443 | SelLocs, |
| 1444 | OldMsg->getMethodDecl(), |
Fariborz Jahanian | 88ec610 | 2012-04-10 22:06:54 +0000 | [diff] [blame] | 1445 | Args, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1446 | OldMsg->getRightLoc(), |
| 1447 | OldMsg->isImplicit()); |
| 1448 | break; |
| 1449 | |
| 1450 | case ObjCMessageExpr::Instance: |
| 1451 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1452 | OldMsg->getValueKind(), |
| 1453 | OldMsg->getLeftLoc(), |
| 1454 | Base, |
| 1455 | OldMsg->getSelector(), |
| 1456 | SelLocs, |
| 1457 | OldMsg->getMethodDecl(), |
Fariborz Jahanian | 88ec610 | 2012-04-10 22:06:54 +0000 | [diff] [blame] | 1458 | Args, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1459 | OldMsg->getRightLoc(), |
| 1460 | OldMsg->isImplicit()); |
| 1461 | break; |
| 1462 | |
| 1463 | case ObjCMessageExpr::SuperClass: |
| 1464 | case ObjCMessageExpr::SuperInstance: |
| 1465 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1466 | OldMsg->getValueKind(), |
| 1467 | OldMsg->getLeftLoc(), |
| 1468 | OldMsg->getSuperLoc(), |
| 1469 | OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance, |
| 1470 | OldMsg->getSuperType(), |
| 1471 | OldMsg->getSelector(), |
| 1472 | SelLocs, |
| 1473 | OldMsg->getMethodDecl(), |
Fariborz Jahanian | 88ec610 | 2012-04-10 22:06:54 +0000 | [diff] [blame] | 1474 | Args, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1475 | OldMsg->getRightLoc(), |
| 1476 | OldMsg->isImplicit()); |
| 1477 | break; |
| 1478 | } |
| 1479 | |
| 1480 | Stmt *Replacement = SynthMessageExpr(NewMsg); |
| 1481 | ReplaceStmtWithRange(PseudoOp, Replacement, OldRange); |
| 1482 | return Replacement; |
| 1483 | } |
| 1484 | |
| 1485 | Stmt *RewriteModernObjC::RewritePropertyOrImplicitGetter(PseudoObjectExpr *PseudoOp) { |
| 1486 | SourceRange OldRange = PseudoOp->getSourceRange(); |
| 1487 | |
| 1488 | // We just magically know some things about the structure of this |
| 1489 | // expression. |
| 1490 | ObjCMessageExpr *OldMsg = |
| 1491 | cast<ObjCMessageExpr>(PseudoOp->getResultExpr()->IgnoreImplicit()); |
| 1492 | |
| 1493 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 1494 | // we need to suppress rewriting the sub-statements. |
| 1495 | Expr *Base = 0; |
Fariborz Jahanian | 88ec610 | 2012-04-10 22:06:54 +0000 | [diff] [blame] | 1496 | SmallVector<Expr*, 1> Args; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1497 | { |
| 1498 | DisableReplaceStmtScope S(*this); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1499 | // Rebuild the base expression if we have one. |
| 1500 | if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) { |
| 1501 | Base = OldMsg->getInstanceReceiver(); |
| 1502 | Base = cast<OpaqueValueExpr>(Base)->getSourceExpr(); |
| 1503 | Base = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Base)); |
| 1504 | } |
Fariborz Jahanian | 88ec610 | 2012-04-10 22:06:54 +0000 | [diff] [blame] | 1505 | unsigned numArgs = OldMsg->getNumArgs(); |
| 1506 | for (unsigned i = 0; i < numArgs; i++) { |
| 1507 | Expr *Arg = OldMsg->getArg(i); |
| 1508 | if (isa<OpaqueValueExpr>(Arg)) |
| 1509 | Arg = cast<OpaqueValueExpr>(Arg)->getSourceExpr(); |
| 1510 | Arg = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Arg)); |
| 1511 | Args.push_back(Arg); |
| 1512 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | // Intentionally empty. |
| 1516 | SmallVector<SourceLocation, 1> SelLocs; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1517 | |
| 1518 | ObjCMessageExpr *NewMsg = 0; |
| 1519 | switch (OldMsg->getReceiverKind()) { |
| 1520 | case ObjCMessageExpr::Class: |
| 1521 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1522 | OldMsg->getValueKind(), |
| 1523 | OldMsg->getLeftLoc(), |
| 1524 | OldMsg->getClassReceiverTypeInfo(), |
| 1525 | OldMsg->getSelector(), |
| 1526 | SelLocs, |
| 1527 | OldMsg->getMethodDecl(), |
| 1528 | Args, |
| 1529 | OldMsg->getRightLoc(), |
| 1530 | OldMsg->isImplicit()); |
| 1531 | break; |
| 1532 | |
| 1533 | case ObjCMessageExpr::Instance: |
| 1534 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1535 | OldMsg->getValueKind(), |
| 1536 | OldMsg->getLeftLoc(), |
| 1537 | Base, |
| 1538 | OldMsg->getSelector(), |
| 1539 | SelLocs, |
| 1540 | OldMsg->getMethodDecl(), |
| 1541 | Args, |
| 1542 | OldMsg->getRightLoc(), |
| 1543 | OldMsg->isImplicit()); |
| 1544 | break; |
| 1545 | |
| 1546 | case ObjCMessageExpr::SuperClass: |
| 1547 | case ObjCMessageExpr::SuperInstance: |
| 1548 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1549 | OldMsg->getValueKind(), |
| 1550 | OldMsg->getLeftLoc(), |
| 1551 | OldMsg->getSuperLoc(), |
| 1552 | OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance, |
| 1553 | OldMsg->getSuperType(), |
| 1554 | OldMsg->getSelector(), |
| 1555 | SelLocs, |
| 1556 | OldMsg->getMethodDecl(), |
| 1557 | Args, |
| 1558 | OldMsg->getRightLoc(), |
| 1559 | OldMsg->isImplicit()); |
| 1560 | break; |
| 1561 | } |
| 1562 | |
| 1563 | Stmt *Replacement = SynthMessageExpr(NewMsg); |
| 1564 | ReplaceStmtWithRange(PseudoOp, Replacement, OldRange); |
| 1565 | return Replacement; |
| 1566 | } |
| 1567 | |
| 1568 | /// SynthCountByEnumWithState - To print: |
| 1569 | /// ((unsigned int (*) |
| 1570 | /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
| 1571 | /// (void *)objc_msgSend)((id)l_collection, |
| 1572 | /// sel_registerName( |
| 1573 | /// "countByEnumeratingWithState:objects:count:"), |
| 1574 | /// &enumState, |
| 1575 | /// (id *)__rw_items, (unsigned int)16) |
| 1576 | /// |
| 1577 | void RewriteModernObjC::SynthCountByEnumWithState(std::string &buf) { |
| 1578 | buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, " |
| 1579 | "id *, unsigned int))(void *)objc_msgSend)"; |
| 1580 | buf += "\n\t\t"; |
| 1581 | buf += "((id)l_collection,\n\t\t"; |
| 1582 | buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; |
| 1583 | buf += "\n\t\t"; |
| 1584 | buf += "&enumState, " |
| 1585 | "(id *)__rw_items, (unsigned int)16)"; |
| 1586 | } |
| 1587 | |
| 1588 | /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach |
| 1589 | /// statement to exit to its outer synthesized loop. |
| 1590 | /// |
| 1591 | Stmt *RewriteModernObjC::RewriteBreakStmt(BreakStmt *S) { |
| 1592 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1593 | return S; |
| 1594 | // replace break with goto __break_label |
| 1595 | std::string buf; |
| 1596 | |
| 1597 | SourceLocation startLoc = S->getLocStart(); |
| 1598 | buf = "goto __break_label_"; |
| 1599 | buf += utostr(ObjCBcLabelNo.back()); |
| 1600 | ReplaceText(startLoc, strlen("break"), buf); |
| 1601 | |
| 1602 | return 0; |
| 1603 | } |
| 1604 | |
| 1605 | /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach |
| 1606 | /// statement to continue with its inner synthesized loop. |
| 1607 | /// |
| 1608 | Stmt *RewriteModernObjC::RewriteContinueStmt(ContinueStmt *S) { |
| 1609 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1610 | return S; |
| 1611 | // replace continue with goto __continue_label |
| 1612 | std::string buf; |
| 1613 | |
| 1614 | SourceLocation startLoc = S->getLocStart(); |
| 1615 | buf = "goto __continue_label_"; |
| 1616 | buf += utostr(ObjCBcLabelNo.back()); |
| 1617 | ReplaceText(startLoc, strlen("continue"), buf); |
| 1618 | |
| 1619 | return 0; |
| 1620 | } |
| 1621 | |
| 1622 | /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. |
| 1623 | /// It rewrites: |
| 1624 | /// for ( type elem in collection) { stmts; } |
| 1625 | |
| 1626 | /// Into: |
| 1627 | /// { |
| 1628 | /// type elem; |
| 1629 | /// struct __objcFastEnumerationState enumState = { 0 }; |
| 1630 | /// id __rw_items[16]; |
| 1631 | /// id l_collection = (id)collection; |
| 1632 | /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1633 | /// objects:__rw_items count:16]; |
| 1634 | /// if (limit) { |
| 1635 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1636 | /// do { |
| 1637 | /// unsigned long counter = 0; |
| 1638 | /// do { |
| 1639 | /// if (startMutations != *enumState.mutationsPtr) |
| 1640 | /// objc_enumerationMutation(l_collection); |
| 1641 | /// elem = (type)enumState.itemsPtr[counter++]; |
| 1642 | /// stmts; |
| 1643 | /// __continue_label: ; |
| 1644 | /// } while (counter < limit); |
| 1645 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
| 1646 | /// objects:__rw_items count:16]); |
| 1647 | /// elem = nil; |
| 1648 | /// __break_label: ; |
| 1649 | /// } |
| 1650 | /// else |
| 1651 | /// elem = nil; |
| 1652 | /// } |
| 1653 | /// |
| 1654 | Stmt *RewriteModernObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
| 1655 | SourceLocation OrigEnd) { |
| 1656 | assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); |
| 1657 | assert(isa<ObjCForCollectionStmt>(Stmts.back()) && |
| 1658 | "ObjCForCollectionStmt Statement stack mismatch"); |
| 1659 | assert(!ObjCBcLabelNo.empty() && |
| 1660 | "ObjCForCollectionStmt - Label No stack empty"); |
| 1661 | |
| 1662 | SourceLocation startLoc = S->getLocStart(); |
| 1663 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1664 | StringRef elementName; |
| 1665 | std::string elementTypeAsString; |
| 1666 | std::string buf; |
| 1667 | buf = "\n{\n\t"; |
| 1668 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { |
| 1669 | // type elem; |
| 1670 | NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl()); |
| 1671 | QualType ElementType = cast<ValueDecl>(D)->getType(); |
| 1672 | if (ElementType->isObjCQualifiedIdType() || |
| 1673 | ElementType->isObjCQualifiedInterfaceType()) |
| 1674 | // Simply use 'id' for all qualified types. |
| 1675 | elementTypeAsString = "id"; |
| 1676 | else |
| 1677 | elementTypeAsString = ElementType.getAsString(Context->getPrintingPolicy()); |
| 1678 | buf += elementTypeAsString; |
| 1679 | buf += " "; |
| 1680 | elementName = D->getName(); |
| 1681 | buf += elementName; |
| 1682 | buf += ";\n\t"; |
| 1683 | } |
| 1684 | else { |
| 1685 | DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); |
| 1686 | elementName = DR->getDecl()->getName(); |
| 1687 | ValueDecl *VD = cast<ValueDecl>(DR->getDecl()); |
| 1688 | if (VD->getType()->isObjCQualifiedIdType() || |
| 1689 | VD->getType()->isObjCQualifiedInterfaceType()) |
| 1690 | // Simply use 'id' for all qualified types. |
| 1691 | elementTypeAsString = "id"; |
| 1692 | else |
| 1693 | elementTypeAsString = VD->getType().getAsString(Context->getPrintingPolicy()); |
| 1694 | } |
| 1695 | |
| 1696 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1697 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
| 1698 | // id __rw_items[16]; |
| 1699 | buf += "id __rw_items[16];\n\t"; |
| 1700 | // id l_collection = (id) |
| 1701 | buf += "id l_collection = (id)"; |
| 1702 | // Find start location of 'collection' the hard way! |
| 1703 | const char *startCollectionBuf = startBuf; |
| 1704 | startCollectionBuf += 3; // skip 'for' |
| 1705 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1706 | startCollectionBuf++; // skip '(' |
| 1707 | // find 'in' and skip it. |
| 1708 | while (*startCollectionBuf != ' ' || |
| 1709 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1710 | (*(startCollectionBuf+3) != ' ' && |
| 1711 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1712 | startCollectionBuf++; |
| 1713 | startCollectionBuf += 3; |
| 1714 | |
| 1715 | // Replace: "for (type element in" with string constructed thus far. |
| 1716 | ReplaceText(startLoc, startCollectionBuf - startBuf, buf); |
| 1717 | // Replace ')' in for '(' type elem in collection ')' with ';' |
| 1718 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1719 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
| 1720 | SourceLocation lparenLoc = startLoc.getLocWithOffset(rparenBuf-startBuf); |
| 1721 | buf = ";\n\t"; |
| 1722 | |
| 1723 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1724 | // objects:__rw_items count:16]; |
| 1725 | // which is synthesized into: |
| 1726 | // unsigned int limit = |
| 1727 | // ((unsigned int (*) |
| 1728 | // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
| 1729 | // (void *)objc_msgSend)((id)l_collection, |
| 1730 | // sel_registerName( |
| 1731 | // "countByEnumeratingWithState:objects:count:"), |
| 1732 | // (struct __objcFastEnumerationState *)&state, |
| 1733 | // (id *)__rw_items, (unsigned int)16); |
| 1734 | buf += "unsigned long limit =\n\t\t"; |
| 1735 | SynthCountByEnumWithState(buf); |
| 1736 | buf += ";\n\t"; |
| 1737 | /// if (limit) { |
| 1738 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1739 | /// do { |
| 1740 | /// unsigned long counter = 0; |
| 1741 | /// do { |
| 1742 | /// if (startMutations != *enumState.mutationsPtr) |
| 1743 | /// objc_enumerationMutation(l_collection); |
| 1744 | /// elem = (type)enumState.itemsPtr[counter++]; |
| 1745 | buf += "if (limit) {\n\t"; |
| 1746 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1747 | buf += "do {\n\t\t"; |
| 1748 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1749 | buf += "do {\n\t\t\t"; |
| 1750 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1751 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1752 | buf += elementName; |
| 1753 | buf += " = ("; |
| 1754 | buf += elementTypeAsString; |
| 1755 | buf += ")enumState.itemsPtr[counter++];"; |
| 1756 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
| 1757 | ReplaceText(lparenLoc, 1, buf); |
| 1758 | |
| 1759 | /// __continue_label: ; |
| 1760 | /// } while (counter < limit); |
| 1761 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
| 1762 | /// objects:__rw_items count:16]); |
| 1763 | /// elem = nil; |
| 1764 | /// __break_label: ; |
| 1765 | /// } |
| 1766 | /// else |
| 1767 | /// elem = nil; |
| 1768 | /// } |
| 1769 | /// |
| 1770 | buf = ";\n\t"; |
| 1771 | buf += "__continue_label_"; |
| 1772 | buf += utostr(ObjCBcLabelNo.back()); |
| 1773 | buf += ": ;"; |
| 1774 | buf += "\n\t\t"; |
| 1775 | buf += "} while (counter < limit);\n\t"; |
| 1776 | buf += "} while (limit = "; |
| 1777 | SynthCountByEnumWithState(buf); |
| 1778 | buf += ");\n\t"; |
| 1779 | buf += elementName; |
| 1780 | buf += " = (("; |
| 1781 | buf += elementTypeAsString; |
| 1782 | buf += ")0);\n\t"; |
| 1783 | buf += "__break_label_"; |
| 1784 | buf += utostr(ObjCBcLabelNo.back()); |
| 1785 | buf += ": ;\n\t"; |
| 1786 | buf += "}\n\t"; |
| 1787 | buf += "else\n\t\t"; |
| 1788 | buf += elementName; |
| 1789 | buf += " = (("; |
| 1790 | buf += elementTypeAsString; |
| 1791 | buf += ")0);\n\t"; |
| 1792 | buf += "}\n"; |
| 1793 | |
| 1794 | // Insert all these *after* the statement body. |
| 1795 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
| 1796 | if (isa<CompoundStmt>(S->getBody())) { |
| 1797 | SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(1); |
| 1798 | InsertText(endBodyLoc, buf); |
| 1799 | } else { |
| 1800 | /* Need to treat single statements specially. For example: |
| 1801 | * |
| 1802 | * for (A *a in b) if (stuff()) break; |
| 1803 | * for (A *a in b) xxxyy; |
| 1804 | * |
| 1805 | * The following code simply scans ahead to the semi to find the actual end. |
| 1806 | */ |
| 1807 | const char *stmtBuf = SM->getCharacterData(OrigEnd); |
| 1808 | const char *semiBuf = strchr(stmtBuf, ';'); |
| 1809 | assert(semiBuf && "Can't find ';'"); |
| 1810 | SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(semiBuf-stmtBuf+1); |
| 1811 | InsertText(endBodyLoc, buf); |
| 1812 | } |
| 1813 | Stmts.pop_back(); |
| 1814 | ObjCBcLabelNo.pop_back(); |
| 1815 | return 0; |
| 1816 | } |
| 1817 | |
Fariborz Jahanian | 542125f | 2012-03-16 21:33:16 +0000 | [diff] [blame] | 1818 | static void Write_RethrowObject(std::string &buf) { |
| 1819 | buf += "{ struct _FIN { _FIN(id reth) : rethrow(reth) {}\n"; |
| 1820 | buf += "\t~_FIN() { if (rethrow) objc_exception_throw(rethrow); }\n"; |
| 1821 | buf += "\tid rethrow;\n"; |
| 1822 | buf += "\t} _fin_force_rethow(_rethrow);"; |
| 1823 | } |
| 1824 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1825 | /// RewriteObjCSynchronizedStmt - |
| 1826 | /// This routine rewrites @synchronized(expr) stmt; |
| 1827 | /// into: |
| 1828 | /// objc_sync_enter(expr); |
| 1829 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1830 | /// |
| 1831 | Stmt *RewriteModernObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
| 1832 | // Get the start location and compute the semi location. |
| 1833 | SourceLocation startLoc = S->getLocStart(); |
| 1834 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1835 | |
| 1836 | assert((*startBuf == '@') && "bogus @synchronized location"); |
| 1837 | |
| 1838 | std::string buf; |
Fariborz Jahanian | 22e2f85 | 2012-03-17 17:46:02 +0000 | [diff] [blame] | 1839 | buf = "{ id _rethrow = 0; id _sync_obj = "; |
Fariborz Jahanian | 542125f | 2012-03-16 21:33:16 +0000 | [diff] [blame] | 1840 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1841 | const char *lparenBuf = startBuf; |
| 1842 | while (*lparenBuf != '(') lparenBuf++; |
| 1843 | ReplaceText(startLoc, lparenBuf-startBuf+1, buf); |
Fariborz Jahanian | 22e2f85 | 2012-03-17 17:46:02 +0000 | [diff] [blame] | 1844 | |
| 1845 | buf = "; objc_sync_enter(_sync_obj);\n"; |
| 1846 | buf += "try {\n\tstruct _SYNC_EXIT { _SYNC_EXIT(id arg) : sync_exit(arg) {}"; |
| 1847 | buf += "\n\t~_SYNC_EXIT() {objc_sync_exit(sync_exit);}"; |
| 1848 | buf += "\n\tid sync_exit;"; |
| 1849 | buf += "\n\t} _sync_exit(_sync_obj);\n"; |
| 1850 | |
| 1851 | // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since |
| 1852 | // the sync expression is typically a message expression that's already |
| 1853 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1854 | SourceLocation RParenExprLoc = S->getSynchBody()->getLocStart(); |
| 1855 | const char *RParenExprLocBuf = SM->getCharacterData(RParenExprLoc); |
| 1856 | while (*RParenExprLocBuf != ')') RParenExprLocBuf--; |
| 1857 | RParenExprLoc = startLoc.getLocWithOffset(RParenExprLocBuf-startBuf); |
| 1858 | |
| 1859 | SourceLocation LBranceLoc = S->getSynchBody()->getLocStart(); |
| 1860 | const char *LBraceLocBuf = SM->getCharacterData(LBranceLoc); |
| 1861 | assert (*LBraceLocBuf == '{'); |
| 1862 | ReplaceText(RParenExprLoc, (LBraceLocBuf - SM->getCharacterData(RParenExprLoc) + 1), buf); |
Fariborz Jahanian | 542125f | 2012-03-16 21:33:16 +0000 | [diff] [blame] | 1863 | |
Fariborz Jahanian | b655bf0 | 2012-03-16 21:43:45 +0000 | [diff] [blame] | 1864 | SourceLocation startRBraceLoc = S->getSynchBody()->getLocEnd(); |
Matt Beaumont-Gay | 9ab511c | 2012-03-16 22:20:39 +0000 | [diff] [blame] | 1865 | assert((*SM->getCharacterData(startRBraceLoc) == '}') && |
| 1866 | "bogus @synchronized block"); |
Fariborz Jahanian | 542125f | 2012-03-16 21:33:16 +0000 | [diff] [blame] | 1867 | |
| 1868 | buf = "} catch (id e) {_rethrow = e;}\n"; |
| 1869 | Write_RethrowObject(buf); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1870 | buf += "}\n"; |
Fariborz Jahanian | 542125f | 2012-03-16 21:33:16 +0000 | [diff] [blame] | 1871 | buf += "}\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1872 | |
Fariborz Jahanian | b655bf0 | 2012-03-16 21:43:45 +0000 | [diff] [blame] | 1873 | ReplaceText(startRBraceLoc, 1, buf); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1874 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1875 | return 0; |
| 1876 | } |
| 1877 | |
| 1878 | void RewriteModernObjC::WarnAboutReturnGotoStmts(Stmt *S) |
| 1879 | { |
| 1880 | // Perform a bottom up traversal of all children. |
| 1881 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
| 1882 | if (*CI) |
| 1883 | WarnAboutReturnGotoStmts(*CI); |
| 1884 | |
| 1885 | if (isa<ReturnStmt>(S) || isa<GotoStmt>(S)) { |
| 1886 | Diags.Report(Context->getFullLoc(S->getLocStart()), |
| 1887 | TryFinallyContainsReturnDiag); |
| 1888 | } |
| 1889 | return; |
| 1890 | } |
| 1891 | |
Fariborz Jahanian | 042b91d | 2012-05-23 23:47:20 +0000 | [diff] [blame] | 1892 | Stmt *RewriteModernObjC::RewriteObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S) { |
| 1893 | SourceLocation startLoc = S->getAtLoc(); |
| 1894 | ReplaceText(startLoc, strlen("@autoreleasepool"), "/* @autoreleasepool */"); |
Fariborz Jahanian | c9b72b6 | 2012-05-24 22:59:56 +0000 | [diff] [blame] | 1895 | ReplaceText(S->getSubStmt()->getLocStart(), 1, |
| 1896 | "{ __AtAutoreleasePool __autoreleasepool; "); |
Fariborz Jahanian | 042b91d | 2012-05-23 23:47:20 +0000 | [diff] [blame] | 1897 | |
| 1898 | return 0; |
| 1899 | } |
| 1900 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1901 | Stmt *RewriteModernObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Fariborz Jahanian | b122818 | 2012-03-15 22:42:15 +0000 | [diff] [blame] | 1902 | ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt(); |
Fariborz Jahanian | 220419a | 2012-03-15 23:50:33 +0000 | [diff] [blame] | 1903 | bool noCatch = S->getNumCatchStmts() == 0; |
Fariborz Jahanian | b122818 | 2012-03-15 22:42:15 +0000 | [diff] [blame] | 1904 | std::string buf; |
| 1905 | |
| 1906 | if (finalStmt) { |
Fariborz Jahanian | 220419a | 2012-03-15 23:50:33 +0000 | [diff] [blame] | 1907 | if (noCatch) |
| 1908 | buf = "{ id volatile _rethrow = 0;\n"; |
| 1909 | else { |
| 1910 | buf = "{ id volatile _rethrow = 0;\ntry {\n"; |
| 1911 | } |
Fariborz Jahanian | b122818 | 2012-03-15 22:42:15 +0000 | [diff] [blame] | 1912 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1913 | // Get the start location and compute the semi location. |
| 1914 | SourceLocation startLoc = S->getLocStart(); |
| 1915 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1916 | |
| 1917 | assert((*startBuf == '@') && "bogus @try location"); |
Fariborz Jahanian | b122818 | 2012-03-15 22:42:15 +0000 | [diff] [blame] | 1918 | if (finalStmt) |
| 1919 | ReplaceText(startLoc, 1, buf); |
| 1920 | else |
| 1921 | // @try -> try |
| 1922 | ReplaceText(startLoc, 1, ""); |
| 1923 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1924 | for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I) { |
| 1925 | ObjCAtCatchStmt *Catch = S->getCatchStmt(I); |
Fariborz Jahanian | 4c14881 | 2012-03-15 20:11:10 +0000 | [diff] [blame] | 1926 | VarDecl *catchDecl = Catch->getCatchParamDecl(); |
Fariborz Jahanian | c38503b | 2012-03-12 23:58:28 +0000 | [diff] [blame] | 1927 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1928 | startLoc = Catch->getLocStart(); |
Fariborz Jahanian | 4c14881 | 2012-03-15 20:11:10 +0000 | [diff] [blame] | 1929 | bool AtRemoved = false; |
| 1930 | if (catchDecl) { |
| 1931 | QualType t = catchDecl->getType(); |
| 1932 | if (const ObjCObjectPointerType *Ptr = t->getAs<ObjCObjectPointerType>()) { |
| 1933 | // Should be a pointer to a class. |
| 1934 | ObjCInterfaceDecl *IDecl = Ptr->getObjectType()->getInterface(); |
| 1935 | if (IDecl) { |
| 1936 | std::string Result; |
| 1937 | startBuf = SM->getCharacterData(startLoc); |
| 1938 | assert((*startBuf == '@') && "bogus @catch location"); |
| 1939 | SourceLocation rParenLoc = Catch->getRParenLoc(); |
| 1940 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 1941 | |
| 1942 | // _objc_exc_Foo *_e as argument to catch. |
| 1943 | Result = "catch (_objc_exc_"; Result += IDecl->getNameAsString(); |
| 1944 | Result += " *_"; Result += catchDecl->getNameAsString(); |
| 1945 | Result += ")"; |
| 1946 | ReplaceText(startLoc, rParenBuf-startBuf+1, Result); |
| 1947 | // Foo *e = (Foo *)_e; |
| 1948 | Result.clear(); |
| 1949 | Result = "{ "; |
| 1950 | Result += IDecl->getNameAsString(); |
| 1951 | Result += " *"; Result += catchDecl->getNameAsString(); |
| 1952 | Result += " = ("; Result += IDecl->getNameAsString(); Result += "*)"; |
| 1953 | Result += "_"; Result += catchDecl->getNameAsString(); |
| 1954 | |
| 1955 | Result += "; "; |
| 1956 | SourceLocation lBraceLoc = Catch->getCatchBody()->getLocStart(); |
| 1957 | ReplaceText(lBraceLoc, 1, Result); |
| 1958 | AtRemoved = true; |
| 1959 | } |
| 1960 | } |
| 1961 | } |
| 1962 | if (!AtRemoved) |
| 1963 | // @catch -> catch |
| 1964 | ReplaceText(startLoc, 1, ""); |
Fariborz Jahanian | c38503b | 2012-03-12 23:58:28 +0000 | [diff] [blame] | 1965 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1966 | } |
Fariborz Jahanian | 220419a | 2012-03-15 23:50:33 +0000 | [diff] [blame] | 1967 | if (finalStmt) { |
| 1968 | buf.clear(); |
| 1969 | if (noCatch) |
| 1970 | buf = "catch (id e) {_rethrow = e;}\n"; |
| 1971 | else |
| 1972 | buf = "}\ncatch (id e) {_rethrow = e;}\n"; |
| 1973 | |
| 1974 | SourceLocation startFinalLoc = finalStmt->getLocStart(); |
| 1975 | ReplaceText(startFinalLoc, 8, buf); |
| 1976 | Stmt *body = finalStmt->getFinallyBody(); |
| 1977 | SourceLocation startFinalBodyLoc = body->getLocStart(); |
| 1978 | buf.clear(); |
Fariborz Jahanian | 542125f | 2012-03-16 21:33:16 +0000 | [diff] [blame] | 1979 | Write_RethrowObject(buf); |
Fariborz Jahanian | 220419a | 2012-03-15 23:50:33 +0000 | [diff] [blame] | 1980 | ReplaceText(startFinalBodyLoc, 1, buf); |
| 1981 | |
| 1982 | SourceLocation endFinalBodyLoc = body->getLocEnd(); |
| 1983 | ReplaceText(endFinalBodyLoc, 1, "}\n}"); |
Fariborz Jahanian | 22e2f85 | 2012-03-17 17:46:02 +0000 | [diff] [blame] | 1984 | // Now check for any return/continue/go statements within the @try. |
| 1985 | WarnAboutReturnGotoStmts(S->getTryBody()); |
Fariborz Jahanian | 220419a | 2012-03-15 23:50:33 +0000 | [diff] [blame] | 1986 | } |
| 1987 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1988 | return 0; |
| 1989 | } |
| 1990 | |
| 1991 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
| 1992 | // the throw expression is typically a message expression that's already |
| 1993 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1994 | Stmt *RewriteModernObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
| 1995 | // Get the start location and compute the semi location. |
| 1996 | SourceLocation startLoc = S->getLocStart(); |
| 1997 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1998 | |
| 1999 | assert((*startBuf == '@') && "bogus @throw location"); |
| 2000 | |
| 2001 | std::string buf; |
| 2002 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
| 2003 | if (S->getThrowExpr()) |
| 2004 | buf = "objc_exception_throw("; |
Fariborz Jahanian | 4053946 | 2012-03-16 16:52:06 +0000 | [diff] [blame] | 2005 | else |
| 2006 | buf = "throw"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2007 | |
| 2008 | // handle "@ throw" correctly. |
| 2009 | const char *wBuf = strchr(startBuf, 'w'); |
| 2010 | assert((*wBuf == 'w') && "@throw: can't find 'w'"); |
| 2011 | ReplaceText(startLoc, wBuf-startBuf+1, buf); |
| 2012 | |
| 2013 | const char *semiBuf = strchr(startBuf, ';'); |
| 2014 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
| 2015 | SourceLocation semiLoc = startLoc.getLocWithOffset(semiBuf-startBuf); |
Fariborz Jahanian | 4053946 | 2012-03-16 16:52:06 +0000 | [diff] [blame] | 2016 | if (S->getThrowExpr()) |
| 2017 | ReplaceText(semiLoc, 1, ");"); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2018 | return 0; |
| 2019 | } |
| 2020 | |
| 2021 | Stmt *RewriteModernObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
| 2022 | // Create a new string expression. |
| 2023 | QualType StrType = Context->getPointerType(Context->CharTy); |
| 2024 | std::string StrEncoding; |
| 2025 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); |
| 2026 | Expr *Replacement = StringLiteral::Create(*Context, StrEncoding, |
| 2027 | StringLiteral::Ascii, false, |
| 2028 | StrType, SourceLocation()); |
| 2029 | ReplaceStmt(Exp, Replacement); |
| 2030 | |
| 2031 | // Replace this subexpr in the parent. |
| 2032 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
| 2033 | return Replacement; |
| 2034 | } |
| 2035 | |
| 2036 | Stmt *RewriteModernObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
| 2037 | if (!SelGetUidFunctionDecl) |
| 2038 | SynthSelGetUidFunctionDecl(); |
| 2039 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 2040 | // Create a call to sel_registerName("selName"). |
| 2041 | SmallVector<Expr*, 8> SelExprs; |
| 2042 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2043 | SelExprs.push_back(StringLiteral::Create(*Context, |
| 2044 | Exp->getSelector().getAsString(), |
| 2045 | StringLiteral::Ascii, false, |
| 2046 | argType, SourceLocation())); |
| 2047 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2048 | &SelExprs[0], SelExprs.size()); |
| 2049 | ReplaceStmt(Exp, SelExp); |
| 2050 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
| 2051 | return SelExp; |
| 2052 | } |
| 2053 | |
| 2054 | CallExpr *RewriteModernObjC::SynthesizeCallToFunctionDecl( |
| 2055 | FunctionDecl *FD, Expr **args, unsigned nargs, SourceLocation StartLoc, |
| 2056 | SourceLocation EndLoc) { |
| 2057 | // Get the type, we will need to reference it in a couple spots. |
| 2058 | QualType msgSendType = FD->getType(); |
| 2059 | |
| 2060 | // Create a reference to the objc_msgSend() declaration. |
| 2061 | DeclRefExpr *DRE = |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2062 | new (Context) DeclRefExpr(FD, false, msgSendType, VK_LValue, SourceLocation()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2063 | |
| 2064 | // Now, we cast the reference to a pointer to the objc_msgSend type. |
| 2065 | QualType pToFunc = Context->getPointerType(msgSendType); |
| 2066 | ImplicitCastExpr *ICE = |
| 2067 | ImplicitCastExpr::Create(*Context, pToFunc, CK_FunctionToPointerDecay, |
| 2068 | DRE, 0, VK_RValue); |
| 2069 | |
| 2070 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
| 2071 | |
| 2072 | CallExpr *Exp = |
| 2073 | new (Context) CallExpr(*Context, ICE, args, nargs, |
| 2074 | FT->getCallResultType(*Context), |
| 2075 | VK_RValue, EndLoc); |
| 2076 | return Exp; |
| 2077 | } |
| 2078 | |
| 2079 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 2080 | const char *&startRef, const char *&endRef) { |
| 2081 | while (startBuf < endBuf) { |
| 2082 | if (*startBuf == '<') |
| 2083 | startRef = startBuf; // mark the start. |
| 2084 | if (*startBuf == '>') { |
| 2085 | if (startRef && *startRef == '<') { |
| 2086 | endRef = startBuf; // mark the end. |
| 2087 | return true; |
| 2088 | } |
| 2089 | return false; |
| 2090 | } |
| 2091 | startBuf++; |
| 2092 | } |
| 2093 | return false; |
| 2094 | } |
| 2095 | |
| 2096 | static void scanToNextArgument(const char *&argRef) { |
| 2097 | int angle = 0; |
| 2098 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 2099 | if (*argRef == '<') |
| 2100 | angle++; |
| 2101 | else if (*argRef == '>') |
| 2102 | angle--; |
| 2103 | argRef++; |
| 2104 | } |
| 2105 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 2106 | } |
| 2107 | |
| 2108 | bool RewriteModernObjC::needToScanForQualifiers(QualType T) { |
| 2109 | if (T->isObjCQualifiedIdType()) |
| 2110 | return true; |
| 2111 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 2112 | if (PT->getPointeeType()->isObjCQualifiedIdType()) |
| 2113 | return true; |
| 2114 | } |
| 2115 | if (T->isObjCObjectPointerType()) { |
| 2116 | T = T->getPointeeType(); |
| 2117 | return T->isObjCQualifiedInterfaceType(); |
| 2118 | } |
| 2119 | if (T->isArrayType()) { |
| 2120 | QualType ElemTy = Context->getBaseElementType(T); |
| 2121 | return needToScanForQualifiers(ElemTy); |
| 2122 | } |
| 2123 | return false; |
| 2124 | } |
| 2125 | |
| 2126 | void RewriteModernObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) { |
| 2127 | QualType Type = E->getType(); |
| 2128 | if (needToScanForQualifiers(Type)) { |
| 2129 | SourceLocation Loc, EndLoc; |
| 2130 | |
| 2131 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) { |
| 2132 | Loc = ECE->getLParenLoc(); |
| 2133 | EndLoc = ECE->getRParenLoc(); |
| 2134 | } else { |
| 2135 | Loc = E->getLocStart(); |
| 2136 | EndLoc = E->getLocEnd(); |
| 2137 | } |
| 2138 | // This will defend against trying to rewrite synthesized expressions. |
| 2139 | if (Loc.isInvalid() || EndLoc.isInvalid()) |
| 2140 | return; |
| 2141 | |
| 2142 | const char *startBuf = SM->getCharacterData(Loc); |
| 2143 | const char *endBuf = SM->getCharacterData(EndLoc); |
| 2144 | const char *startRef = 0, *endRef = 0; |
| 2145 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2146 | // Get the locations of the startRef, endRef. |
| 2147 | SourceLocation LessLoc = Loc.getLocWithOffset(startRef-startBuf); |
| 2148 | SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-startBuf+1); |
| 2149 | // Comment out the protocol references. |
| 2150 | InsertText(LessLoc, "/*"); |
| 2151 | InsertText(GreaterLoc, "*/"); |
| 2152 | } |
| 2153 | } |
| 2154 | } |
| 2155 | |
| 2156 | void RewriteModernObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
| 2157 | SourceLocation Loc; |
| 2158 | QualType Type; |
| 2159 | const FunctionProtoType *proto = 0; |
| 2160 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 2161 | Loc = VD->getLocation(); |
| 2162 | Type = VD->getType(); |
| 2163 | } |
| 2164 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 2165 | Loc = FD->getLocation(); |
| 2166 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 2167 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
| 2168 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
| 2169 | assert(funcType && "missing function type"); |
| 2170 | proto = dyn_cast<FunctionProtoType>(funcType); |
| 2171 | if (!proto) |
| 2172 | return; |
| 2173 | Type = proto->getResultType(); |
| 2174 | } |
| 2175 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(Dcl)) { |
| 2176 | Loc = FD->getLocation(); |
| 2177 | Type = FD->getType(); |
| 2178 | } |
| 2179 | else |
| 2180 | return; |
| 2181 | |
| 2182 | if (needToScanForQualifiers(Type)) { |
| 2183 | // Since types are unique, we need to scan the buffer. |
| 2184 | |
| 2185 | const char *endBuf = SM->getCharacterData(Loc); |
| 2186 | const char *startBuf = endBuf; |
| 2187 | while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) |
| 2188 | startBuf--; // scan backward (from the decl location) for return type. |
| 2189 | const char *startRef = 0, *endRef = 0; |
| 2190 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2191 | // Get the locations of the startRef, endRef. |
| 2192 | SourceLocation LessLoc = Loc.getLocWithOffset(startRef-endBuf); |
| 2193 | SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-endBuf+1); |
| 2194 | // Comment out the protocol references. |
| 2195 | InsertText(LessLoc, "/*"); |
| 2196 | InsertText(GreaterLoc, "*/"); |
| 2197 | } |
| 2198 | } |
| 2199 | if (!proto) |
| 2200 | return; // most likely, was a variable |
| 2201 | // Now check arguments. |
| 2202 | const char *startBuf = SM->getCharacterData(Loc); |
| 2203 | const char *startFuncBuf = startBuf; |
| 2204 | for (unsigned i = 0; i < proto->getNumArgs(); i++) { |
| 2205 | if (needToScanForQualifiers(proto->getArgType(i))) { |
| 2206 | // Since types are unique, we need to scan the buffer. |
| 2207 | |
| 2208 | const char *endBuf = startBuf; |
| 2209 | // scan forward (from the decl location) for argument types. |
| 2210 | scanToNextArgument(endBuf); |
| 2211 | const char *startRef = 0, *endRef = 0; |
| 2212 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2213 | // Get the locations of the startRef, endRef. |
| 2214 | SourceLocation LessLoc = |
| 2215 | Loc.getLocWithOffset(startRef-startFuncBuf); |
| 2216 | SourceLocation GreaterLoc = |
| 2217 | Loc.getLocWithOffset(endRef-startFuncBuf+1); |
| 2218 | // Comment out the protocol references. |
| 2219 | InsertText(LessLoc, "/*"); |
| 2220 | InsertText(GreaterLoc, "*/"); |
| 2221 | } |
| 2222 | startBuf = ++endBuf; |
| 2223 | } |
| 2224 | else { |
| 2225 | // If the function name is derived from a macro expansion, then the |
| 2226 | // argument buffer will not follow the name. Need to speak with Chris. |
| 2227 | while (*startBuf && *startBuf != ')' && *startBuf != ',') |
| 2228 | startBuf++; // scan forward (from the decl location) for argument types. |
| 2229 | startBuf++; |
| 2230 | } |
| 2231 | } |
| 2232 | } |
| 2233 | |
| 2234 | void RewriteModernObjC::RewriteTypeOfDecl(VarDecl *ND) { |
| 2235 | QualType QT = ND->getType(); |
| 2236 | const Type* TypePtr = QT->getAs<Type>(); |
| 2237 | if (!isa<TypeOfExprType>(TypePtr)) |
| 2238 | return; |
| 2239 | while (isa<TypeOfExprType>(TypePtr)) { |
| 2240 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 2241 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 2242 | TypePtr = QT->getAs<Type>(); |
| 2243 | } |
| 2244 | // FIXME. This will not work for multiple declarators; as in: |
| 2245 | // __typeof__(a) b,c,d; |
| 2246 | std::string TypeAsString(QT.getAsString(Context->getPrintingPolicy())); |
| 2247 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
| 2248 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 2249 | if (ND->getInit()) { |
| 2250 | std::string Name(ND->getNameAsString()); |
| 2251 | TypeAsString += " " + Name + " = "; |
| 2252 | Expr *E = ND->getInit(); |
| 2253 | SourceLocation startLoc; |
| 2254 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 2255 | startLoc = ECE->getLParenLoc(); |
| 2256 | else |
| 2257 | startLoc = E->getLocStart(); |
| 2258 | startLoc = SM->getExpansionLoc(startLoc); |
| 2259 | const char *endBuf = SM->getCharacterData(startLoc); |
| 2260 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
| 2261 | } |
| 2262 | else { |
| 2263 | SourceLocation X = ND->getLocEnd(); |
| 2264 | X = SM->getExpansionLoc(X); |
| 2265 | const char *endBuf = SM->getCharacterData(X); |
| 2266 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
| 2267 | } |
| 2268 | } |
| 2269 | |
| 2270 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
| 2271 | void RewriteModernObjC::SynthSelGetUidFunctionDecl() { |
| 2272 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
| 2273 | SmallVector<QualType, 16> ArgTys; |
| 2274 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
| 2275 | QualType getFuncType = |
| 2276 | getSimpleFunctionType(Context->getObjCSelType(), &ArgTys[0], ArgTys.size()); |
| 2277 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
| 2278 | SourceLocation(), |
| 2279 | SourceLocation(), |
| 2280 | SelGetUidIdent, getFuncType, 0, |
| 2281 | SC_Extern, |
| 2282 | SC_None, false); |
| 2283 | } |
| 2284 | |
| 2285 | void RewriteModernObjC::RewriteFunctionDecl(FunctionDecl *FD) { |
| 2286 | // declared in <objc/objc.h> |
| 2287 | if (FD->getIdentifier() && |
| 2288 | FD->getName() == "sel_registerName") { |
| 2289 | SelGetUidFunctionDecl = FD; |
| 2290 | return; |
| 2291 | } |
| 2292 | RewriteObjCQualifiedInterfaceTypes(FD); |
| 2293 | } |
| 2294 | |
| 2295 | void RewriteModernObjC::RewriteBlockPointerType(std::string& Str, QualType Type) { |
| 2296 | std::string TypeString(Type.getAsString(Context->getPrintingPolicy())); |
| 2297 | const char *argPtr = TypeString.c_str(); |
| 2298 | if (!strchr(argPtr, '^')) { |
| 2299 | Str += TypeString; |
| 2300 | return; |
| 2301 | } |
| 2302 | while (*argPtr) { |
| 2303 | Str += (*argPtr == '^' ? '*' : *argPtr); |
| 2304 | argPtr++; |
| 2305 | } |
| 2306 | } |
| 2307 | |
| 2308 | // FIXME. Consolidate this routine with RewriteBlockPointerType. |
| 2309 | void RewriteModernObjC::RewriteBlockPointerTypeVariable(std::string& Str, |
| 2310 | ValueDecl *VD) { |
| 2311 | QualType Type = VD->getType(); |
| 2312 | std::string TypeString(Type.getAsString(Context->getPrintingPolicy())); |
| 2313 | const char *argPtr = TypeString.c_str(); |
| 2314 | int paren = 0; |
| 2315 | while (*argPtr) { |
| 2316 | switch (*argPtr) { |
| 2317 | case '(': |
| 2318 | Str += *argPtr; |
| 2319 | paren++; |
| 2320 | break; |
| 2321 | case ')': |
| 2322 | Str += *argPtr; |
| 2323 | paren--; |
| 2324 | break; |
| 2325 | case '^': |
| 2326 | Str += '*'; |
| 2327 | if (paren == 1) |
| 2328 | Str += VD->getNameAsString(); |
| 2329 | break; |
| 2330 | default: |
| 2331 | Str += *argPtr; |
| 2332 | break; |
| 2333 | } |
| 2334 | argPtr++; |
| 2335 | } |
| 2336 | } |
| 2337 | |
Fariborz Jahanian | b75f8de | 2012-04-19 00:50:01 +0000 | [diff] [blame] | 2338 | void RewriteModernObjC::RewriteBlockLiteralFunctionDecl(FunctionDecl *FD) { |
| 2339 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
| 2340 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
| 2341 | const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(funcType); |
| 2342 | if (!proto) |
| 2343 | return; |
| 2344 | QualType Type = proto->getResultType(); |
| 2345 | std::string FdStr = Type.getAsString(Context->getPrintingPolicy()); |
| 2346 | FdStr += " "; |
| 2347 | FdStr += FD->getName(); |
| 2348 | FdStr += "("; |
| 2349 | unsigned numArgs = proto->getNumArgs(); |
| 2350 | for (unsigned i = 0; i < numArgs; i++) { |
| 2351 | QualType ArgType = proto->getArgType(i); |
| 2352 | RewriteBlockPointerType(FdStr, ArgType); |
| 2353 | if (i+1 < numArgs) |
| 2354 | FdStr += ", "; |
| 2355 | } |
Fariborz Jahanian | b5863da | 2012-04-19 16:30:28 +0000 | [diff] [blame] | 2356 | if (FD->isVariadic()) { |
| 2357 | FdStr += (numArgs > 0) ? ", ...);\n" : "...);\n"; |
| 2358 | } |
| 2359 | else |
| 2360 | FdStr += ");\n"; |
Fariborz Jahanian | b75f8de | 2012-04-19 00:50:01 +0000 | [diff] [blame] | 2361 | InsertText(FunLocStart, FdStr); |
| 2362 | } |
| 2363 | |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 2364 | // SynthSuperContructorFunctionDecl - id __rw_objc_super(id obj, id super); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2365 | void RewriteModernObjC::SynthSuperContructorFunctionDecl() { |
| 2366 | if (SuperContructorFunctionDecl) |
| 2367 | return; |
| 2368 | IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super"); |
| 2369 | SmallVector<QualType, 16> ArgTys; |
| 2370 | QualType argT = Context->getObjCIdType(); |
| 2371 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2372 | ArgTys.push_back(argT); |
| 2373 | ArgTys.push_back(argT); |
| 2374 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2375 | &ArgTys[0], ArgTys.size()); |
| 2376 | SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
| 2377 | SourceLocation(), |
| 2378 | SourceLocation(), |
| 2379 | msgSendIdent, msgSendType, 0, |
| 2380 | SC_Extern, |
| 2381 | SC_None, false); |
| 2382 | } |
| 2383 | |
| 2384 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
| 2385 | void RewriteModernObjC::SynthMsgSendFunctionDecl() { |
| 2386 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
| 2387 | SmallVector<QualType, 16> ArgTys; |
| 2388 | QualType argT = Context->getObjCIdType(); |
| 2389 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2390 | ArgTys.push_back(argT); |
| 2391 | argT = Context->getObjCSelType(); |
| 2392 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2393 | ArgTys.push_back(argT); |
| 2394 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2395 | &ArgTys[0], ArgTys.size(), |
| 2396 | true /*isVariadic*/); |
| 2397 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
| 2398 | SourceLocation(), |
| 2399 | SourceLocation(), |
| 2400 | msgSendIdent, msgSendType, 0, |
| 2401 | SC_Extern, |
| 2402 | SC_None, false); |
| 2403 | } |
| 2404 | |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 2405 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(void); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2406 | void RewriteModernObjC::SynthMsgSendSuperFunctionDecl() { |
| 2407 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 2408 | SmallVector<QualType, 2> ArgTys; |
| 2409 | ArgTys.push_back(Context->VoidTy); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2410 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 2411 | &ArgTys[0], 1, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2412 | true /*isVariadic*/); |
| 2413 | MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
| 2414 | SourceLocation(), |
| 2415 | SourceLocation(), |
| 2416 | msgSendIdent, msgSendType, 0, |
| 2417 | SC_Extern, |
| 2418 | SC_None, false); |
| 2419 | } |
| 2420 | |
| 2421 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
| 2422 | void RewriteModernObjC::SynthMsgSendStretFunctionDecl() { |
| 2423 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
| 2424 | SmallVector<QualType, 16> ArgTys; |
| 2425 | QualType argT = Context->getObjCIdType(); |
| 2426 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2427 | ArgTys.push_back(argT); |
| 2428 | argT = Context->getObjCSelType(); |
| 2429 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2430 | ArgTys.push_back(argT); |
| 2431 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2432 | &ArgTys[0], ArgTys.size(), |
| 2433 | true /*isVariadic*/); |
| 2434 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
| 2435 | SourceLocation(), |
| 2436 | SourceLocation(), |
| 2437 | msgSendIdent, msgSendType, 0, |
| 2438 | SC_Extern, |
| 2439 | SC_None, false); |
| 2440 | } |
| 2441 | |
| 2442 | // SynthMsgSendSuperStretFunctionDecl - |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 2443 | // id objc_msgSendSuper_stret(void); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2444 | void RewriteModernObjC::SynthMsgSendSuperStretFunctionDecl() { |
| 2445 | IdentifierInfo *msgSendIdent = |
| 2446 | &Context->Idents.get("objc_msgSendSuper_stret"); |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 2447 | SmallVector<QualType, 2> ArgTys; |
| 2448 | ArgTys.push_back(Context->VoidTy); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2449 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 2450 | &ArgTys[0], 1, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2451 | true /*isVariadic*/); |
| 2452 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
| 2453 | SourceLocation(), |
| 2454 | SourceLocation(), |
| 2455 | msgSendIdent, msgSendType, 0, |
| 2456 | SC_Extern, |
| 2457 | SC_None, false); |
| 2458 | } |
| 2459 | |
| 2460 | // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); |
| 2461 | void RewriteModernObjC::SynthMsgSendFpretFunctionDecl() { |
| 2462 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
| 2463 | SmallVector<QualType, 16> ArgTys; |
| 2464 | QualType argT = Context->getObjCIdType(); |
| 2465 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2466 | ArgTys.push_back(argT); |
| 2467 | argT = Context->getObjCSelType(); |
| 2468 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2469 | ArgTys.push_back(argT); |
| 2470 | QualType msgSendType = getSimpleFunctionType(Context->DoubleTy, |
| 2471 | &ArgTys[0], ArgTys.size(), |
| 2472 | true /*isVariadic*/); |
| 2473 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
| 2474 | SourceLocation(), |
| 2475 | SourceLocation(), |
| 2476 | msgSendIdent, msgSendType, 0, |
| 2477 | SC_Extern, |
| 2478 | SC_None, false); |
| 2479 | } |
| 2480 | |
Fariborz Jahanian | 20e181a | 2012-05-08 20:55:55 +0000 | [diff] [blame] | 2481 | // SynthGetClassFunctionDecl - Class objc_getClass(const char *name); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2482 | void RewriteModernObjC::SynthGetClassFunctionDecl() { |
| 2483 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
| 2484 | SmallVector<QualType, 16> ArgTys; |
| 2485 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
Fariborz Jahanian | 20e181a | 2012-05-08 20:55:55 +0000 | [diff] [blame] | 2486 | QualType getClassType = getSimpleFunctionType(Context->getObjCClassType(), |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2487 | &ArgTys[0], ArgTys.size()); |
| 2488 | GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
| 2489 | SourceLocation(), |
| 2490 | SourceLocation(), |
| 2491 | getClassIdent, getClassType, 0, |
| 2492 | SC_Extern, |
| 2493 | SC_None, false); |
| 2494 | } |
| 2495 | |
| 2496 | // SynthGetSuperClassFunctionDecl - Class class_getSuperclass(Class cls); |
| 2497 | void RewriteModernObjC::SynthGetSuperClassFunctionDecl() { |
| 2498 | IdentifierInfo *getSuperClassIdent = |
| 2499 | &Context->Idents.get("class_getSuperclass"); |
| 2500 | SmallVector<QualType, 16> ArgTys; |
| 2501 | ArgTys.push_back(Context->getObjCClassType()); |
| 2502 | QualType getClassType = getSimpleFunctionType(Context->getObjCClassType(), |
| 2503 | &ArgTys[0], ArgTys.size()); |
| 2504 | GetSuperClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
| 2505 | SourceLocation(), |
| 2506 | SourceLocation(), |
| 2507 | getSuperClassIdent, |
| 2508 | getClassType, 0, |
| 2509 | SC_Extern, |
| 2510 | SC_None, |
| 2511 | false); |
| 2512 | } |
| 2513 | |
Fariborz Jahanian | 20e181a | 2012-05-08 20:55:55 +0000 | [diff] [blame] | 2514 | // SynthGetMetaClassFunctionDecl - Class objc_getMetaClass(const char *name); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2515 | void RewriteModernObjC::SynthGetMetaClassFunctionDecl() { |
| 2516 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
| 2517 | SmallVector<QualType, 16> ArgTys; |
| 2518 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
Fariborz Jahanian | 20e181a | 2012-05-08 20:55:55 +0000 | [diff] [blame] | 2519 | QualType getClassType = getSimpleFunctionType(Context->getObjCClassType(), |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2520 | &ArgTys[0], ArgTys.size()); |
| 2521 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
| 2522 | SourceLocation(), |
| 2523 | SourceLocation(), |
| 2524 | getClassIdent, getClassType, 0, |
| 2525 | SC_Extern, |
| 2526 | SC_None, false); |
| 2527 | } |
| 2528 | |
| 2529 | Stmt *RewriteModernObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
| 2530 | QualType strType = getConstantStringStructType(); |
| 2531 | |
| 2532 | std::string S = "__NSConstantStringImpl_"; |
| 2533 | |
| 2534 | std::string tmpName = InFileName; |
| 2535 | unsigned i; |
| 2536 | for (i=0; i < tmpName.length(); i++) { |
| 2537 | char c = tmpName.at(i); |
| 2538 | // replace any non alphanumeric characters with '_'. |
| 2539 | if (!isalpha(c) && (c < '0' || c > '9')) |
| 2540 | tmpName[i] = '_'; |
| 2541 | } |
| 2542 | S += tmpName; |
| 2543 | S += "_"; |
| 2544 | S += utostr(NumObjCStringLiterals++); |
| 2545 | |
| 2546 | Preamble += "static __NSConstantStringImpl " + S; |
| 2547 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 2548 | Preamble += "0x000007c8,"; // utf8_str |
| 2549 | // The pretty printer for StringLiteral handles escape characters properly. |
| 2550 | std::string prettyBufS; |
| 2551 | llvm::raw_string_ostream prettyBuf(prettyBufS); |
| 2552 | Exp->getString()->printPretty(prettyBuf, *Context, 0, |
| 2553 | PrintingPolicy(LangOpts)); |
| 2554 | Preamble += prettyBuf.str(); |
| 2555 | Preamble += ","; |
| 2556 | Preamble += utostr(Exp->getString()->getByteLength()) + "};\n"; |
| 2557 | |
| 2558 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
| 2559 | SourceLocation(), &Context->Idents.get(S), |
| 2560 | strType, 0, SC_Static, SC_None); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2561 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, false, strType, VK_LValue, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2562 | SourceLocation()); |
| 2563 | Expr *Unop = new (Context) UnaryOperator(DRE, UO_AddrOf, |
| 2564 | Context->getPointerType(DRE->getType()), |
| 2565 | VK_RValue, OK_Ordinary, |
| 2566 | SourceLocation()); |
| 2567 | // cast to NSConstantString * |
| 2568 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Exp->getType(), |
| 2569 | CK_CPointerToObjCPointerCast, Unop); |
| 2570 | ReplaceStmt(Exp, cast); |
| 2571 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
| 2572 | return cast; |
| 2573 | } |
| 2574 | |
Fariborz Jahanian | 5594704 | 2012-03-27 20:17:30 +0000 | [diff] [blame] | 2575 | Stmt *RewriteModernObjC::RewriteObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Exp) { |
| 2576 | unsigned IntSize = |
| 2577 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 2578 | |
| 2579 | Expr *FlagExp = IntegerLiteral::Create(*Context, |
| 2580 | llvm::APInt(IntSize, Exp->getValue()), |
| 2581 | Context->IntTy, Exp->getLocation()); |
| 2582 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Context->ObjCBuiltinBoolTy, |
| 2583 | CK_BitCast, FlagExp); |
| 2584 | ParenExpr *PE = new (Context) ParenExpr(Exp->getLocation(), Exp->getExprLoc(), |
| 2585 | cast); |
| 2586 | ReplaceStmt(Exp, PE); |
| 2587 | return PE; |
| 2588 | } |
| 2589 | |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2590 | Stmt *RewriteModernObjC::RewriteObjCBoxedExpr(ObjCBoxedExpr *Exp) { |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2591 | // synthesize declaration of helper functions needed in this routine. |
| 2592 | if (!SelGetUidFunctionDecl) |
| 2593 | SynthSelGetUidFunctionDecl(); |
| 2594 | // use objc_msgSend() for all. |
| 2595 | if (!MsgSendFunctionDecl) |
| 2596 | SynthMsgSendFunctionDecl(); |
| 2597 | if (!GetClassFunctionDecl) |
| 2598 | SynthGetClassFunctionDecl(); |
| 2599 | |
| 2600 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2601 | SourceLocation StartLoc = Exp->getLocStart(); |
| 2602 | SourceLocation EndLoc = Exp->getLocEnd(); |
| 2603 | |
| 2604 | // Synthesize a call to objc_msgSend(). |
| 2605 | SmallVector<Expr*, 4> MsgExprs; |
| 2606 | SmallVector<Expr*, 4> ClsExprs; |
| 2607 | QualType argType = Context->getPointerType(Context->CharTy); |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2608 | |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2609 | // Create a call to objc_getClass("<BoxingClass>"). It will be the 1st argument. |
| 2610 | ObjCMethodDecl *BoxingMethod = Exp->getBoxingMethod(); |
| 2611 | ObjCInterfaceDecl *BoxingClass = BoxingMethod->getClassInterface(); |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2612 | |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2613 | IdentifierInfo *clsName = BoxingClass->getIdentifier(); |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2614 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 2615 | clsName->getName(), |
| 2616 | StringLiteral::Ascii, false, |
| 2617 | argType, SourceLocation())); |
| 2618 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2619 | &ClsExprs[0], |
| 2620 | ClsExprs.size(), |
| 2621 | StartLoc, EndLoc); |
| 2622 | MsgExprs.push_back(Cls); |
| 2623 | |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2624 | // Create a call to sel_registerName("<BoxingMethod>:"), etc. |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2625 | // it will be the 2nd argument. |
| 2626 | SmallVector<Expr*, 4> SelExprs; |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2627 | SelExprs.push_back(StringLiteral::Create(*Context, |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2628 | BoxingMethod->getSelector().getAsString(), |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2629 | StringLiteral::Ascii, false, |
| 2630 | argType, SourceLocation())); |
| 2631 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2632 | &SelExprs[0], SelExprs.size(), |
| 2633 | StartLoc, EndLoc); |
| 2634 | MsgExprs.push_back(SelExp); |
| 2635 | |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2636 | // User provided sub-expression is the 3rd, and last, argument. |
| 2637 | Expr *subExpr = Exp->getSubExpr(); |
| 2638 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(subExpr)) { |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2639 | QualType type = ICE->getType(); |
| 2640 | const Expr *SubExpr = ICE->IgnoreParenImpCasts(); |
| 2641 | CastKind CK = CK_BitCast; |
| 2642 | if (SubExpr->getType()->isIntegralType(*Context) && type->isBooleanType()) |
| 2643 | CK = CK_IntegralToBoolean; |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2644 | subExpr = NoTypeInfoCStyleCastExpr(Context, type, CK, subExpr); |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2645 | } |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2646 | MsgExprs.push_back(subExpr); |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2647 | |
| 2648 | SmallVector<QualType, 4> ArgTypes; |
| 2649 | ArgTypes.push_back(Context->getObjCIdType()); |
| 2650 | ArgTypes.push_back(Context->getObjCSelType()); |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2651 | for (ObjCMethodDecl::param_iterator PI = BoxingMethod->param_begin(), |
| 2652 | E = BoxingMethod->param_end(); PI != E; ++PI) |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2653 | ArgTypes.push_back((*PI)->getType()); |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2654 | |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2655 | QualType returnType = Exp->getType(); |
| 2656 | // Get the type, we will need to reference it in a couple spots. |
| 2657 | QualType msgSendType = MsgSendFlavor->getType(); |
| 2658 | |
| 2659 | // Create a reference to the objc_msgSend() declaration. |
| 2660 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, false, msgSendType, |
| 2661 | VK_LValue, SourceLocation()); |
| 2662 | |
| 2663 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2664 | Context->getPointerType(Context->VoidTy), |
| 2665 | CK_BitCast, DRE); |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2666 | |
| 2667 | // Now do the "normal" pointer to function cast. |
| 2668 | QualType castType = |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2669 | getSimpleFunctionType(returnType, &ArgTypes[0], ArgTypes.size(), |
| 2670 | BoxingMethod->isVariadic()); |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2671 | castType = Context->getPointerType(castType); |
| 2672 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
| 2673 | cast); |
| 2674 | |
| 2675 | // Don't forget the parens to enforce the proper binding. |
| 2676 | ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast); |
| 2677 | |
| 2678 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
| 2679 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
| 2680 | MsgExprs.size(), |
| 2681 | FT->getResultType(), VK_RValue, |
| 2682 | EndLoc); |
| 2683 | ReplaceStmt(Exp, CE); |
| 2684 | return CE; |
| 2685 | } |
| 2686 | |
Fariborz Jahanian | 86cff60 | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2687 | Stmt *RewriteModernObjC::RewriteObjCArrayLiteralExpr(ObjCArrayLiteral *Exp) { |
| 2688 | // synthesize declaration of helper functions needed in this routine. |
| 2689 | if (!SelGetUidFunctionDecl) |
| 2690 | SynthSelGetUidFunctionDecl(); |
| 2691 | // use objc_msgSend() for all. |
| 2692 | if (!MsgSendFunctionDecl) |
| 2693 | SynthMsgSendFunctionDecl(); |
| 2694 | if (!GetClassFunctionDecl) |
| 2695 | SynthGetClassFunctionDecl(); |
| 2696 | |
| 2697 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2698 | SourceLocation StartLoc = Exp->getLocStart(); |
| 2699 | SourceLocation EndLoc = Exp->getLocEnd(); |
| 2700 | |
Fariborz Jahanian | e35abe1 | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2701 | // Build the expression: __NSContainer_literal(int, ...).arr |
Fariborz Jahanian | b0f245c | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 2702 | QualType IntQT = Context->IntTy; |
| 2703 | QualType NSArrayFType = |
| 2704 | getSimpleFunctionType(Context->VoidTy, &IntQT, 1, true); |
Fariborz Jahanian | e35abe1 | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2705 | std::string NSArrayFName("__NSContainer_literal"); |
Fariborz Jahanian | b0f245c | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 2706 | FunctionDecl *NSArrayFD = SynthBlockInitFunctionDecl(NSArrayFName); |
| 2707 | DeclRefExpr *NSArrayDRE = |
| 2708 | new (Context) DeclRefExpr(NSArrayFD, false, NSArrayFType, VK_RValue, |
| 2709 | SourceLocation()); |
| 2710 | |
| 2711 | SmallVector<Expr*, 16> InitExprs; |
| 2712 | unsigned NumElements = Exp->getNumElements(); |
| 2713 | unsigned UnsignedIntSize = |
| 2714 | static_cast<unsigned>(Context->getTypeSize(Context->UnsignedIntTy)); |
| 2715 | Expr *count = IntegerLiteral::Create(*Context, |
| 2716 | llvm::APInt(UnsignedIntSize, NumElements), |
| 2717 | Context->UnsignedIntTy, SourceLocation()); |
| 2718 | InitExprs.push_back(count); |
| 2719 | for (unsigned i = 0; i < NumElements; i++) |
| 2720 | InitExprs.push_back(Exp->getElement(i)); |
| 2721 | Expr *NSArrayCallExpr = |
| 2722 | new (Context) CallExpr(*Context, NSArrayDRE, &InitExprs[0], InitExprs.size(), |
| 2723 | NSArrayFType, VK_LValue, SourceLocation()); |
| 2724 | |
| 2725 | FieldDecl *ARRFD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 2726 | SourceLocation(), |
| 2727 | &Context->Idents.get("arr"), |
| 2728 | Context->getPointerType(Context->VoidPtrTy), 0, |
| 2729 | /*BitWidth=*/0, /*Mutable=*/true, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2730 | ICIS_NoInit); |
Fariborz Jahanian | b0f245c | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 2731 | MemberExpr *ArrayLiteralME = |
| 2732 | new (Context) MemberExpr(NSArrayCallExpr, false, ARRFD, |
| 2733 | SourceLocation(), |
| 2734 | ARRFD->getType(), VK_LValue, |
| 2735 | OK_Ordinary); |
| 2736 | QualType ConstIdT = Context->getObjCIdType().withConst(); |
| 2737 | CStyleCastExpr * ArrayLiteralObjects = |
| 2738 | NoTypeInfoCStyleCastExpr(Context, |
| 2739 | Context->getPointerType(ConstIdT), |
| 2740 | CK_BitCast, |
| 2741 | ArrayLiteralME); |
| 2742 | |
Fariborz Jahanian | 86cff60 | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2743 | // Synthesize a call to objc_msgSend(). |
| 2744 | SmallVector<Expr*, 32> MsgExprs; |
| 2745 | SmallVector<Expr*, 4> ClsExprs; |
| 2746 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2747 | QualType expType = Exp->getType(); |
| 2748 | |
| 2749 | // Create a call to objc_getClass("NSArray"). It will be th 1st argument. |
| 2750 | ObjCInterfaceDecl *Class = |
| 2751 | expType->getPointeeType()->getAs<ObjCObjectType>()->getInterface(); |
| 2752 | |
| 2753 | IdentifierInfo *clsName = Class->getIdentifier(); |
| 2754 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 2755 | clsName->getName(), |
| 2756 | StringLiteral::Ascii, false, |
| 2757 | argType, SourceLocation())); |
| 2758 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2759 | &ClsExprs[0], |
| 2760 | ClsExprs.size(), |
| 2761 | StartLoc, EndLoc); |
| 2762 | MsgExprs.push_back(Cls); |
| 2763 | |
| 2764 | // Create a call to sel_registerName("arrayWithObjects:count:"). |
| 2765 | // it will be the 2nd argument. |
| 2766 | SmallVector<Expr*, 4> SelExprs; |
| 2767 | ObjCMethodDecl *ArrayMethod = Exp->getArrayWithObjectsMethod(); |
| 2768 | SelExprs.push_back(StringLiteral::Create(*Context, |
| 2769 | ArrayMethod->getSelector().getAsString(), |
| 2770 | StringLiteral::Ascii, false, |
| 2771 | argType, SourceLocation())); |
| 2772 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2773 | &SelExprs[0], SelExprs.size(), |
| 2774 | StartLoc, EndLoc); |
| 2775 | MsgExprs.push_back(SelExp); |
| 2776 | |
Fariborz Jahanian | b0f245c | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 2777 | // (const id [])objects |
| 2778 | MsgExprs.push_back(ArrayLiteralObjects); |
Fariborz Jahanian | 86cff60 | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2779 | |
Fariborz Jahanian | b0f245c | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 2780 | // (NSUInteger)cnt |
| 2781 | Expr *cnt = IntegerLiteral::Create(*Context, |
| 2782 | llvm::APInt(UnsignedIntSize, NumElements), |
| 2783 | Context->UnsignedIntTy, SourceLocation()); |
| 2784 | MsgExprs.push_back(cnt); |
Fariborz Jahanian | 86cff60 | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2785 | |
| 2786 | |
| 2787 | SmallVector<QualType, 4> ArgTypes; |
| 2788 | ArgTypes.push_back(Context->getObjCIdType()); |
| 2789 | ArgTypes.push_back(Context->getObjCSelType()); |
| 2790 | for (ObjCMethodDecl::param_iterator PI = ArrayMethod->param_begin(), |
| 2791 | E = ArrayMethod->param_end(); PI != E; ++PI) |
| 2792 | ArgTypes.push_back((*PI)->getType()); |
| 2793 | |
| 2794 | QualType returnType = Exp->getType(); |
| 2795 | // Get the type, we will need to reference it in a couple spots. |
| 2796 | QualType msgSendType = MsgSendFlavor->getType(); |
| 2797 | |
| 2798 | // Create a reference to the objc_msgSend() declaration. |
| 2799 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, false, msgSendType, |
| 2800 | VK_LValue, SourceLocation()); |
| 2801 | |
| 2802 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, |
| 2803 | Context->getPointerType(Context->VoidTy), |
| 2804 | CK_BitCast, DRE); |
| 2805 | |
| 2806 | // Now do the "normal" pointer to function cast. |
| 2807 | QualType castType = |
| 2808 | getSimpleFunctionType(returnType, &ArgTypes[0], ArgTypes.size(), |
| 2809 | ArrayMethod->isVariadic()); |
| 2810 | castType = Context->getPointerType(castType); |
| 2811 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
| 2812 | cast); |
| 2813 | |
| 2814 | // Don't forget the parens to enforce the proper binding. |
| 2815 | ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast); |
| 2816 | |
| 2817 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
| 2818 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
| 2819 | MsgExprs.size(), |
| 2820 | FT->getResultType(), VK_RValue, |
| 2821 | EndLoc); |
| 2822 | ReplaceStmt(Exp, CE); |
| 2823 | return CE; |
| 2824 | } |
| 2825 | |
Fariborz Jahanian | e35abe1 | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2826 | Stmt *RewriteModernObjC::RewriteObjCDictionaryLiteralExpr(ObjCDictionaryLiteral *Exp) { |
| 2827 | // synthesize declaration of helper functions needed in this routine. |
| 2828 | if (!SelGetUidFunctionDecl) |
| 2829 | SynthSelGetUidFunctionDecl(); |
| 2830 | // use objc_msgSend() for all. |
| 2831 | if (!MsgSendFunctionDecl) |
| 2832 | SynthMsgSendFunctionDecl(); |
| 2833 | if (!GetClassFunctionDecl) |
| 2834 | SynthGetClassFunctionDecl(); |
| 2835 | |
| 2836 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2837 | SourceLocation StartLoc = Exp->getLocStart(); |
| 2838 | SourceLocation EndLoc = Exp->getLocEnd(); |
| 2839 | |
| 2840 | // Build the expression: __NSContainer_literal(int, ...).arr |
| 2841 | QualType IntQT = Context->IntTy; |
| 2842 | QualType NSDictFType = |
| 2843 | getSimpleFunctionType(Context->VoidTy, &IntQT, 1, true); |
| 2844 | std::string NSDictFName("__NSContainer_literal"); |
| 2845 | FunctionDecl *NSDictFD = SynthBlockInitFunctionDecl(NSDictFName); |
| 2846 | DeclRefExpr *NSDictDRE = |
| 2847 | new (Context) DeclRefExpr(NSDictFD, false, NSDictFType, VK_RValue, |
| 2848 | SourceLocation()); |
| 2849 | |
| 2850 | SmallVector<Expr*, 16> KeyExprs; |
| 2851 | SmallVector<Expr*, 16> ValueExprs; |
| 2852 | |
| 2853 | unsigned NumElements = Exp->getNumElements(); |
| 2854 | unsigned UnsignedIntSize = |
| 2855 | static_cast<unsigned>(Context->getTypeSize(Context->UnsignedIntTy)); |
| 2856 | Expr *count = IntegerLiteral::Create(*Context, |
| 2857 | llvm::APInt(UnsignedIntSize, NumElements), |
| 2858 | Context->UnsignedIntTy, SourceLocation()); |
| 2859 | KeyExprs.push_back(count); |
| 2860 | ValueExprs.push_back(count); |
| 2861 | for (unsigned i = 0; i < NumElements; i++) { |
| 2862 | ObjCDictionaryElement Element = Exp->getKeyValueElement(i); |
| 2863 | KeyExprs.push_back(Element.Key); |
| 2864 | ValueExprs.push_back(Element.Value); |
| 2865 | } |
| 2866 | |
| 2867 | // (const id [])objects |
| 2868 | Expr *NSValueCallExpr = |
| 2869 | new (Context) CallExpr(*Context, NSDictDRE, &ValueExprs[0], ValueExprs.size(), |
| 2870 | NSDictFType, VK_LValue, SourceLocation()); |
| 2871 | |
| 2872 | FieldDecl *ARRFD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 2873 | SourceLocation(), |
| 2874 | &Context->Idents.get("arr"), |
| 2875 | Context->getPointerType(Context->VoidPtrTy), 0, |
| 2876 | /*BitWidth=*/0, /*Mutable=*/true, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2877 | ICIS_NoInit); |
Fariborz Jahanian | e35abe1 | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2878 | MemberExpr *DictLiteralValueME = |
| 2879 | new (Context) MemberExpr(NSValueCallExpr, false, ARRFD, |
| 2880 | SourceLocation(), |
| 2881 | ARRFD->getType(), VK_LValue, |
| 2882 | OK_Ordinary); |
| 2883 | QualType ConstIdT = Context->getObjCIdType().withConst(); |
| 2884 | CStyleCastExpr * DictValueObjects = |
| 2885 | NoTypeInfoCStyleCastExpr(Context, |
| 2886 | Context->getPointerType(ConstIdT), |
| 2887 | CK_BitCast, |
| 2888 | DictLiteralValueME); |
| 2889 | // (const id <NSCopying> [])keys |
| 2890 | Expr *NSKeyCallExpr = |
| 2891 | new (Context) CallExpr(*Context, NSDictDRE, &KeyExprs[0], KeyExprs.size(), |
| 2892 | NSDictFType, VK_LValue, SourceLocation()); |
| 2893 | |
| 2894 | MemberExpr *DictLiteralKeyME = |
| 2895 | new (Context) MemberExpr(NSKeyCallExpr, false, ARRFD, |
| 2896 | SourceLocation(), |
| 2897 | ARRFD->getType(), VK_LValue, |
| 2898 | OK_Ordinary); |
| 2899 | |
| 2900 | CStyleCastExpr * DictKeyObjects = |
| 2901 | NoTypeInfoCStyleCastExpr(Context, |
| 2902 | Context->getPointerType(ConstIdT), |
| 2903 | CK_BitCast, |
| 2904 | DictLiteralKeyME); |
| 2905 | |
| 2906 | |
| 2907 | |
| 2908 | // Synthesize a call to objc_msgSend(). |
| 2909 | SmallVector<Expr*, 32> MsgExprs; |
| 2910 | SmallVector<Expr*, 4> ClsExprs; |
| 2911 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2912 | QualType expType = Exp->getType(); |
| 2913 | |
| 2914 | // Create a call to objc_getClass("NSArray"). It will be th 1st argument. |
| 2915 | ObjCInterfaceDecl *Class = |
| 2916 | expType->getPointeeType()->getAs<ObjCObjectType>()->getInterface(); |
| 2917 | |
| 2918 | IdentifierInfo *clsName = Class->getIdentifier(); |
| 2919 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 2920 | clsName->getName(), |
| 2921 | StringLiteral::Ascii, false, |
| 2922 | argType, SourceLocation())); |
| 2923 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2924 | &ClsExprs[0], |
| 2925 | ClsExprs.size(), |
| 2926 | StartLoc, EndLoc); |
| 2927 | MsgExprs.push_back(Cls); |
| 2928 | |
| 2929 | // Create a call to sel_registerName("arrayWithObjects:count:"). |
| 2930 | // it will be the 2nd argument. |
| 2931 | SmallVector<Expr*, 4> SelExprs; |
| 2932 | ObjCMethodDecl *DictMethod = Exp->getDictWithObjectsMethod(); |
| 2933 | SelExprs.push_back(StringLiteral::Create(*Context, |
| 2934 | DictMethod->getSelector().getAsString(), |
| 2935 | StringLiteral::Ascii, false, |
| 2936 | argType, SourceLocation())); |
| 2937 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2938 | &SelExprs[0], SelExprs.size(), |
| 2939 | StartLoc, EndLoc); |
| 2940 | MsgExprs.push_back(SelExp); |
| 2941 | |
| 2942 | // (const id [])objects |
| 2943 | MsgExprs.push_back(DictValueObjects); |
| 2944 | |
| 2945 | // (const id <NSCopying> [])keys |
| 2946 | MsgExprs.push_back(DictKeyObjects); |
| 2947 | |
| 2948 | // (NSUInteger)cnt |
| 2949 | Expr *cnt = IntegerLiteral::Create(*Context, |
| 2950 | llvm::APInt(UnsignedIntSize, NumElements), |
| 2951 | Context->UnsignedIntTy, SourceLocation()); |
| 2952 | MsgExprs.push_back(cnt); |
| 2953 | |
| 2954 | |
| 2955 | SmallVector<QualType, 8> ArgTypes; |
| 2956 | ArgTypes.push_back(Context->getObjCIdType()); |
| 2957 | ArgTypes.push_back(Context->getObjCSelType()); |
| 2958 | for (ObjCMethodDecl::param_iterator PI = DictMethod->param_begin(), |
| 2959 | E = DictMethod->param_end(); PI != E; ++PI) { |
| 2960 | QualType T = (*PI)->getType(); |
| 2961 | if (const PointerType* PT = T->getAs<PointerType>()) { |
| 2962 | QualType PointeeTy = PT->getPointeeType(); |
| 2963 | convertToUnqualifiedObjCType(PointeeTy); |
| 2964 | T = Context->getPointerType(PointeeTy); |
| 2965 | } |
| 2966 | ArgTypes.push_back(T); |
| 2967 | } |
| 2968 | |
| 2969 | QualType returnType = Exp->getType(); |
| 2970 | // Get the type, we will need to reference it in a couple spots. |
| 2971 | QualType msgSendType = MsgSendFlavor->getType(); |
| 2972 | |
| 2973 | // Create a reference to the objc_msgSend() declaration. |
| 2974 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, false, msgSendType, |
| 2975 | VK_LValue, SourceLocation()); |
| 2976 | |
| 2977 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, |
| 2978 | Context->getPointerType(Context->VoidTy), |
| 2979 | CK_BitCast, DRE); |
| 2980 | |
| 2981 | // Now do the "normal" pointer to function cast. |
| 2982 | QualType castType = |
| 2983 | getSimpleFunctionType(returnType, &ArgTypes[0], ArgTypes.size(), |
| 2984 | DictMethod->isVariadic()); |
| 2985 | castType = Context->getPointerType(castType); |
| 2986 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
| 2987 | cast); |
| 2988 | |
| 2989 | // Don't forget the parens to enforce the proper binding. |
| 2990 | ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast); |
| 2991 | |
| 2992 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
| 2993 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
| 2994 | MsgExprs.size(), |
| 2995 | FT->getResultType(), VK_RValue, |
| 2996 | EndLoc); |
| 2997 | ReplaceStmt(Exp, CE); |
| 2998 | return CE; |
| 2999 | } |
| 3000 | |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3001 | // struct __rw_objc_super { |
| 3002 | // struct objc_object *object; struct objc_object *superClass; |
| 3003 | // }; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3004 | QualType RewriteModernObjC::getSuperStructType() { |
| 3005 | if (!SuperStructDecl) { |
| 3006 | SuperStructDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 3007 | SourceLocation(), SourceLocation(), |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3008 | &Context->Idents.get("__rw_objc_super")); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3009 | QualType FieldTypes[2]; |
| 3010 | |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3011 | // struct objc_object *object; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3012 | FieldTypes[0] = Context->getObjCIdType(); |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3013 | // struct objc_object *superClass; |
| 3014 | FieldTypes[1] = Context->getObjCIdType(); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3015 | |
| 3016 | // Create fields |
| 3017 | for (unsigned i = 0; i < 2; ++i) { |
| 3018 | SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl, |
| 3019 | SourceLocation(), |
| 3020 | SourceLocation(), 0, |
| 3021 | FieldTypes[i], 0, |
| 3022 | /*BitWidth=*/0, |
| 3023 | /*Mutable=*/false, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3024 | ICIS_NoInit)); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3025 | } |
| 3026 | |
| 3027 | SuperStructDecl->completeDefinition(); |
| 3028 | } |
| 3029 | return Context->getTagDeclType(SuperStructDecl); |
| 3030 | } |
| 3031 | |
| 3032 | QualType RewriteModernObjC::getConstantStringStructType() { |
| 3033 | if (!ConstantStringDecl) { |
| 3034 | ConstantStringDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 3035 | SourceLocation(), SourceLocation(), |
| 3036 | &Context->Idents.get("__NSConstantStringImpl")); |
| 3037 | QualType FieldTypes[4]; |
| 3038 | |
| 3039 | // struct objc_object *receiver; |
| 3040 | FieldTypes[0] = Context->getObjCIdType(); |
| 3041 | // int flags; |
| 3042 | FieldTypes[1] = Context->IntTy; |
| 3043 | // char *str; |
| 3044 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
| 3045 | // long length; |
| 3046 | FieldTypes[3] = Context->LongTy; |
| 3047 | |
| 3048 | // Create fields |
| 3049 | for (unsigned i = 0; i < 4; ++i) { |
| 3050 | ConstantStringDecl->addDecl(FieldDecl::Create(*Context, |
| 3051 | ConstantStringDecl, |
| 3052 | SourceLocation(), |
| 3053 | SourceLocation(), 0, |
| 3054 | FieldTypes[i], 0, |
| 3055 | /*BitWidth=*/0, |
| 3056 | /*Mutable=*/true, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3057 | ICIS_NoInit)); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3058 | } |
| 3059 | |
| 3060 | ConstantStringDecl->completeDefinition(); |
| 3061 | } |
| 3062 | return Context->getTagDeclType(ConstantStringDecl); |
| 3063 | } |
| 3064 | |
Fariborz Jahanian | be8d55c | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3065 | /// getFunctionSourceLocation - returns start location of a function |
| 3066 | /// definition. Complication arises when function has declared as |
| 3067 | /// extern "C" or extern "C" {...} |
| 3068 | static SourceLocation getFunctionSourceLocation (RewriteModernObjC &R, |
| 3069 | FunctionDecl *FD) { |
| 3070 | if (FD->isExternC() && !FD->isMain()) { |
| 3071 | const DeclContext *DC = FD->getDeclContext(); |
| 3072 | if (const LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(DC)) |
| 3073 | // if it is extern "C" {...}, return function decl's own location. |
| 3074 | if (!LSD->getRBraceLoc().isValid()) |
| 3075 | return LSD->getExternLoc(); |
| 3076 | } |
| 3077 | if (FD->getStorageClassAsWritten() != SC_None) |
| 3078 | R.RewriteBlockLiteralFunctionDecl(FD); |
| 3079 | return FD->getTypeSpecStartLoc(); |
| 3080 | } |
| 3081 | |
| 3082 | /// SynthMsgSendStretCallExpr - This routine translates message expression |
| 3083 | /// into a call to objc_msgSend_stret() entry point. Tricky part is that |
| 3084 | /// nil check on receiver must be performed before calling objc_msgSend_stret. |
| 3085 | /// MsgSendStretFlavor - function declaration objc_msgSend_stret(...) |
| 3086 | /// msgSendType - function type of objc_msgSend_stret(...) |
| 3087 | /// returnType - Result type of the method being synthesized. |
| 3088 | /// ArgTypes - type of the arguments passed to objc_msgSend_stret, starting with receiver type. |
| 3089 | /// MsgExprs - list of argument expressions being passed to objc_msgSend_stret, |
| 3090 | /// starting with receiver. |
| 3091 | /// Method - Method being rewritten. |
| 3092 | Expr *RewriteModernObjC::SynthMsgSendStretCallExpr(FunctionDecl *MsgSendStretFlavor, |
| 3093 | QualType msgSendType, |
| 3094 | QualType returnType, |
| 3095 | SmallVectorImpl<QualType> &ArgTypes, |
| 3096 | SmallVectorImpl<Expr*> &MsgExprs, |
| 3097 | ObjCMethodDecl *Method) { |
| 3098 | // Now do the "normal" pointer to function cast. |
| 3099 | QualType castType = getSimpleFunctionType(returnType, &ArgTypes[0], ArgTypes.size(), |
| 3100 | Method ? Method->isVariadic() : false); |
| 3101 | castType = Context->getPointerType(castType); |
| 3102 | |
| 3103 | // build type for containing the objc_msgSend_stret object. |
| 3104 | static unsigned stretCount=0; |
| 3105 | std::string name = "__Stret"; name += utostr(stretCount); |
| 3106 | std::string str = "struct "; str += name; |
| 3107 | str += " {\n\t"; |
| 3108 | str += name; |
| 3109 | str += "(id receiver, SEL sel"; |
| 3110 | for (unsigned i = 2; i < ArgTypes.size(); i++) { |
Fariborz Jahanian | 6734ec4 | 2012-06-29 19:55:46 +0000 | [diff] [blame^] | 3111 | std::string ArgName = "arg"; ArgName += utostr(i); |
| 3112 | ArgTypes[i].getAsStringInternal(ArgName, Context->getPrintingPolicy()); |
| 3113 | str += ", "; str += ArgName; |
Fariborz Jahanian | be8d55c | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3114 | } |
| 3115 | // could be vararg. |
| 3116 | for (unsigned i = ArgTypes.size(); i < MsgExprs.size(); i++) { |
Fariborz Jahanian | 6734ec4 | 2012-06-29 19:55:46 +0000 | [diff] [blame^] | 3117 | std::string ArgName = "arg"; ArgName += utostr(i); |
| 3118 | MsgExprs[i]->getType().getAsStringInternal(ArgName, |
| 3119 | Context->getPrintingPolicy()); |
| 3120 | str += ", "; str += ArgName; |
Fariborz Jahanian | be8d55c | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3121 | } |
| 3122 | |
| 3123 | str += ") {\n"; |
| 3124 | str += "\t if (receiver == 0)\n"; |
| 3125 | str += "\t memset((void*)&s, 0, sizeof(s));\n"; |
| 3126 | str += "\t else\n"; |
| 3127 | str += "\t s = (("; str += castType.getAsString(Context->getPrintingPolicy()); |
| 3128 | str += ")(void *)objc_msgSend_stret)(receiver, sel"; |
| 3129 | for (unsigned i = 2; i < ArgTypes.size(); i++) { |
| 3130 | str += ", arg"; str += utostr(i); |
| 3131 | } |
| 3132 | // could be vararg. |
| 3133 | for (unsigned i = ArgTypes.size(); i < MsgExprs.size(); i++) { |
| 3134 | str += ", arg"; str += utostr(i); |
| 3135 | } |
| 3136 | |
| 3137 | str += ");\n"; |
| 3138 | str += "\t}\n"; |
| 3139 | str += "\t"; str += returnType.getAsString(Context->getPrintingPolicy()); |
| 3140 | str += " s;\n"; |
| 3141 | str += "};\n\n"; |
| 3142 | SourceLocation FunLocStart = getFunctionSourceLocation(*this, CurFunctionDef); |
| 3143 | InsertText(FunLocStart, str); |
| 3144 | ++stretCount; |
| 3145 | |
| 3146 | // AST for __Stretn(receiver, args).s; |
| 3147 | IdentifierInfo *ID = &Context->Idents.get(name); |
| 3148 | FunctionDecl *FD = FunctionDecl::Create(*Context, TUDecl, SourceLocation(), |
| 3149 | SourceLocation(), ID, castType, 0, SC_Extern, |
| 3150 | SC_None, false, false); |
| 3151 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, false, castType, VK_RValue, |
| 3152 | SourceLocation()); |
| 3153 | CallExpr *STCE = new (Context) CallExpr(*Context, DRE, &MsgExprs[0], MsgExprs.size(), |
| 3154 | castType, VK_LValue, SourceLocation()); |
| 3155 | |
| 3156 | FieldDecl *FieldD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 3157 | SourceLocation(), |
| 3158 | &Context->Idents.get("s"), |
| 3159 | returnType, 0, |
| 3160 | /*BitWidth=*/0, /*Mutable=*/true, |
| 3161 | ICIS_NoInit); |
| 3162 | MemberExpr *ME = new (Context) MemberExpr(STCE, false, FieldD, SourceLocation(), |
| 3163 | FieldD->getType(), VK_LValue, |
| 3164 | OK_Ordinary); |
| 3165 | |
| 3166 | return ME; |
| 3167 | } |
| 3168 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3169 | Stmt *RewriteModernObjC::SynthMessageExpr(ObjCMessageExpr *Exp, |
| 3170 | SourceLocation StartLoc, |
| 3171 | SourceLocation EndLoc) { |
| 3172 | if (!SelGetUidFunctionDecl) |
| 3173 | SynthSelGetUidFunctionDecl(); |
| 3174 | if (!MsgSendFunctionDecl) |
| 3175 | SynthMsgSendFunctionDecl(); |
| 3176 | if (!MsgSendSuperFunctionDecl) |
| 3177 | SynthMsgSendSuperFunctionDecl(); |
| 3178 | if (!MsgSendStretFunctionDecl) |
| 3179 | SynthMsgSendStretFunctionDecl(); |
| 3180 | if (!MsgSendSuperStretFunctionDecl) |
| 3181 | SynthMsgSendSuperStretFunctionDecl(); |
| 3182 | if (!MsgSendFpretFunctionDecl) |
| 3183 | SynthMsgSendFpretFunctionDecl(); |
| 3184 | if (!GetClassFunctionDecl) |
| 3185 | SynthGetClassFunctionDecl(); |
| 3186 | if (!GetSuperClassFunctionDecl) |
| 3187 | SynthGetSuperClassFunctionDecl(); |
| 3188 | if (!GetMetaClassFunctionDecl) |
| 3189 | SynthGetMetaClassFunctionDecl(); |
| 3190 | |
| 3191 | // default to objc_msgSend(). |
| 3192 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 3193 | // May need to use objc_msgSend_stret() as well. |
| 3194 | FunctionDecl *MsgSendStretFlavor = 0; |
| 3195 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
| 3196 | QualType resultType = mDecl->getResultType(); |
| 3197 | if (resultType->isRecordType()) |
| 3198 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
| 3199 | else if (resultType->isRealFloatingType()) |
| 3200 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
| 3201 | } |
| 3202 | |
| 3203 | // Synthesize a call to objc_msgSend(). |
| 3204 | SmallVector<Expr*, 8> MsgExprs; |
| 3205 | switch (Exp->getReceiverKind()) { |
| 3206 | case ObjCMessageExpr::SuperClass: { |
| 3207 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 3208 | if (MsgSendStretFlavor) |
| 3209 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 3210 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 3211 | |
| 3212 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
| 3213 | |
| 3214 | SmallVector<Expr*, 4> InitExprs; |
| 3215 | |
| 3216 | // set the receiver to self, the first argument to all methods. |
| 3217 | InitExprs.push_back( |
| 3218 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 3219 | CK_BitCast, |
| 3220 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3221 | false, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3222 | Context->getObjCIdType(), |
| 3223 | VK_RValue, |
| 3224 | SourceLocation())) |
| 3225 | ); // set the 'receiver'. |
| 3226 | |
| 3227 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 3228 | SmallVector<Expr*, 8> ClsExprs; |
| 3229 | QualType argType = Context->getPointerType(Context->CharTy); |
| 3230 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 3231 | ClassDecl->getIdentifier()->getName(), |
| 3232 | StringLiteral::Ascii, false, |
| 3233 | argType, SourceLocation())); |
Fariborz Jahanian | 20e181a | 2012-05-08 20:55:55 +0000 | [diff] [blame] | 3234 | // (Class)objc_getClass("CurrentClass") |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3235 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
| 3236 | &ClsExprs[0], |
| 3237 | ClsExprs.size(), |
| 3238 | StartLoc, |
| 3239 | EndLoc); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3240 | ClsExprs.clear(); |
Fariborz Jahanian | 20e181a | 2012-05-08 20:55:55 +0000 | [diff] [blame] | 3241 | ClsExprs.push_back(Cls); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3242 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 3243 | &ClsExprs[0], ClsExprs.size(), |
| 3244 | StartLoc, EndLoc); |
| 3245 | |
| 3246 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 3247 | // To turn off a warning, type-cast to 'id' |
| 3248 | InitExprs.push_back( // set 'super class', using class_getSuperclass(). |
| 3249 | NoTypeInfoCStyleCastExpr(Context, |
| 3250 | Context->getObjCIdType(), |
| 3251 | CK_BitCast, Cls)); |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3252 | // struct __rw_objc_super |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3253 | QualType superType = getSuperStructType(); |
| 3254 | Expr *SuperRep; |
| 3255 | |
| 3256 | if (LangOpts.MicrosoftExt) { |
| 3257 | SynthSuperContructorFunctionDecl(); |
| 3258 | // Simulate a contructor call... |
| 3259 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3260 | false, superType, VK_LValue, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3261 | SourceLocation()); |
| 3262 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
| 3263 | InitExprs.size(), |
| 3264 | superType, VK_LValue, |
| 3265 | SourceLocation()); |
| 3266 | // The code for super is a little tricky to prevent collision with |
| 3267 | // the structure definition in the header. The rewriter has it's own |
| 3268 | // internal definition (__rw_objc_super) that is uses. This is why |
| 3269 | // we need the cast below. For example: |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3270 | // (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] | 3271 | // |
| 3272 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
| 3273 | Context->getPointerType(SuperRep->getType()), |
| 3274 | VK_RValue, OK_Ordinary, |
| 3275 | SourceLocation()); |
| 3276 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 3277 | Context->getPointerType(superType), |
| 3278 | CK_BitCast, SuperRep); |
| 3279 | } else { |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3280 | // (struct __rw_objc_super) { <exprs from above> } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3281 | InitListExpr *ILE = |
| 3282 | new (Context) InitListExpr(*Context, SourceLocation(), |
| 3283 | &InitExprs[0], InitExprs.size(), |
| 3284 | SourceLocation()); |
| 3285 | TypeSourceInfo *superTInfo |
| 3286 | = Context->getTrivialTypeSourceInfo(superType); |
| 3287 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
| 3288 | superType, VK_LValue, |
| 3289 | ILE, false); |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3290 | // struct __rw_objc_super * |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3291 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
| 3292 | Context->getPointerType(SuperRep->getType()), |
| 3293 | VK_RValue, OK_Ordinary, |
| 3294 | SourceLocation()); |
| 3295 | } |
| 3296 | MsgExprs.push_back(SuperRep); |
| 3297 | break; |
| 3298 | } |
| 3299 | |
| 3300 | case ObjCMessageExpr::Class: { |
| 3301 | SmallVector<Expr*, 8> ClsExprs; |
| 3302 | QualType argType = Context->getPointerType(Context->CharTy); |
| 3303 | ObjCInterfaceDecl *Class |
| 3304 | = Exp->getClassReceiver()->getAs<ObjCObjectType>()->getInterface(); |
| 3305 | IdentifierInfo *clsName = Class->getIdentifier(); |
| 3306 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 3307 | clsName->getName(), |
| 3308 | StringLiteral::Ascii, false, |
| 3309 | argType, SourceLocation())); |
| 3310 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 3311 | &ClsExprs[0], |
| 3312 | ClsExprs.size(), |
| 3313 | StartLoc, EndLoc); |
Fariborz Jahanian | 20e181a | 2012-05-08 20:55:55 +0000 | [diff] [blame] | 3314 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 3315 | Context->getObjCIdType(), |
| 3316 | CK_BitCast, Cls); |
| 3317 | MsgExprs.push_back(ArgExpr); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3318 | break; |
| 3319 | } |
| 3320 | |
| 3321 | case ObjCMessageExpr::SuperInstance:{ |
| 3322 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 3323 | if (MsgSendStretFlavor) |
| 3324 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 3325 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 3326 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
| 3327 | SmallVector<Expr*, 4> InitExprs; |
| 3328 | |
| 3329 | InitExprs.push_back( |
| 3330 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 3331 | CK_BitCast, |
| 3332 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3333 | false, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3334 | Context->getObjCIdType(), |
| 3335 | VK_RValue, SourceLocation())) |
| 3336 | ); // set the 'receiver'. |
| 3337 | |
| 3338 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 3339 | SmallVector<Expr*, 8> ClsExprs; |
| 3340 | QualType argType = Context->getPointerType(Context->CharTy); |
| 3341 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 3342 | ClassDecl->getIdentifier()->getName(), |
| 3343 | StringLiteral::Ascii, false, argType, |
| 3344 | SourceLocation())); |
Fariborz Jahanian | 20e181a | 2012-05-08 20:55:55 +0000 | [diff] [blame] | 3345 | // (Class)objc_getClass("CurrentClass") |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3346 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 3347 | &ClsExprs[0], |
| 3348 | ClsExprs.size(), |
| 3349 | StartLoc, EndLoc); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3350 | ClsExprs.clear(); |
Fariborz Jahanian | 20e181a | 2012-05-08 20:55:55 +0000 | [diff] [blame] | 3351 | ClsExprs.push_back(Cls); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3352 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 3353 | &ClsExprs[0], ClsExprs.size(), |
| 3354 | StartLoc, EndLoc); |
| 3355 | |
| 3356 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 3357 | // To turn off a warning, type-cast to 'id' |
| 3358 | InitExprs.push_back( |
| 3359 | // set 'super class', using class_getSuperclass(). |
| 3360 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 3361 | CK_BitCast, Cls)); |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3362 | // struct __rw_objc_super |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3363 | QualType superType = getSuperStructType(); |
| 3364 | Expr *SuperRep; |
| 3365 | |
| 3366 | if (LangOpts.MicrosoftExt) { |
| 3367 | SynthSuperContructorFunctionDecl(); |
| 3368 | // Simulate a contructor call... |
| 3369 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3370 | false, superType, VK_LValue, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3371 | SourceLocation()); |
| 3372 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
| 3373 | InitExprs.size(), |
| 3374 | superType, VK_LValue, SourceLocation()); |
| 3375 | // The code for super is a little tricky to prevent collision with |
| 3376 | // the structure definition in the header. The rewriter has it's own |
| 3377 | // internal definition (__rw_objc_super) that is uses. This is why |
| 3378 | // we need the cast below. For example: |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3379 | // (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] | 3380 | // |
| 3381 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
| 3382 | Context->getPointerType(SuperRep->getType()), |
| 3383 | VK_RValue, OK_Ordinary, |
| 3384 | SourceLocation()); |
| 3385 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 3386 | Context->getPointerType(superType), |
| 3387 | CK_BitCast, SuperRep); |
| 3388 | } else { |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3389 | // (struct __rw_objc_super) { <exprs from above> } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3390 | InitListExpr *ILE = |
| 3391 | new (Context) InitListExpr(*Context, SourceLocation(), |
| 3392 | &InitExprs[0], InitExprs.size(), |
| 3393 | SourceLocation()); |
| 3394 | TypeSourceInfo *superTInfo |
| 3395 | = Context->getTrivialTypeSourceInfo(superType); |
| 3396 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
| 3397 | superType, VK_RValue, ILE, |
| 3398 | false); |
| 3399 | } |
| 3400 | MsgExprs.push_back(SuperRep); |
| 3401 | break; |
| 3402 | } |
| 3403 | |
| 3404 | case ObjCMessageExpr::Instance: { |
| 3405 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 3406 | // Foo<Proto> *. |
| 3407 | Expr *recExpr = Exp->getInstanceReceiver(); |
| 3408 | while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr)) |
| 3409 | recExpr = CE->getSubExpr(); |
| 3410 | CastKind CK = recExpr->getType()->isObjCObjectPointerType() |
| 3411 | ? CK_BitCast : recExpr->getType()->isBlockPointerType() |
| 3412 | ? CK_BlockPointerToObjCPointerCast |
| 3413 | : CK_CPointerToObjCPointerCast; |
| 3414 | |
| 3415 | recExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 3416 | CK, recExpr); |
| 3417 | MsgExprs.push_back(recExpr); |
| 3418 | break; |
| 3419 | } |
| 3420 | } |
| 3421 | |
| 3422 | // Create a call to sel_registerName("selName"), it will be the 2nd argument. |
| 3423 | SmallVector<Expr*, 8> SelExprs; |
| 3424 | QualType argType = Context->getPointerType(Context->CharTy); |
| 3425 | SelExprs.push_back(StringLiteral::Create(*Context, |
| 3426 | Exp->getSelector().getAsString(), |
| 3427 | StringLiteral::Ascii, false, |
| 3428 | argType, SourceLocation())); |
| 3429 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 3430 | &SelExprs[0], SelExprs.size(), |
| 3431 | StartLoc, |
| 3432 | EndLoc); |
| 3433 | MsgExprs.push_back(SelExp); |
| 3434 | |
| 3435 | // Now push any user supplied arguments. |
| 3436 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
| 3437 | Expr *userExpr = Exp->getArg(i); |
| 3438 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 3439 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 3440 | // Reuse the ICE type, it is exactly what the doctor ordered. |
| 3441 | QualType type = ICE->getType(); |
| 3442 | if (needToScanForQualifiers(type)) |
| 3443 | type = Context->getObjCIdType(); |
| 3444 | // Make sure we convert "type (^)(...)" to "type (*)(...)". |
| 3445 | (void)convertBlockPointerToFunctionPointer(type); |
| 3446 | const Expr *SubExpr = ICE->IgnoreParenImpCasts(); |
| 3447 | CastKind CK; |
| 3448 | if (SubExpr->getType()->isIntegralType(*Context) && |
| 3449 | type->isBooleanType()) { |
| 3450 | CK = CK_IntegralToBoolean; |
| 3451 | } else if (type->isObjCObjectPointerType()) { |
| 3452 | if (SubExpr->getType()->isBlockPointerType()) { |
| 3453 | CK = CK_BlockPointerToObjCPointerCast; |
| 3454 | } else if (SubExpr->getType()->isPointerType()) { |
| 3455 | CK = CK_CPointerToObjCPointerCast; |
| 3456 | } else { |
| 3457 | CK = CK_BitCast; |
| 3458 | } |
| 3459 | } else { |
| 3460 | CK = CK_BitCast; |
| 3461 | } |
| 3462 | |
| 3463 | userExpr = NoTypeInfoCStyleCastExpr(Context, type, CK, userExpr); |
| 3464 | } |
| 3465 | // Make id<P...> cast into an 'id' cast. |
| 3466 | else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) { |
| 3467 | if (CE->getType()->isObjCQualifiedIdType()) { |
| 3468 | while ((CE = dyn_cast<CStyleCastExpr>(userExpr))) |
| 3469 | userExpr = CE->getSubExpr(); |
| 3470 | CastKind CK; |
| 3471 | if (userExpr->getType()->isIntegralType(*Context)) { |
| 3472 | CK = CK_IntegralToPointer; |
| 3473 | } else if (userExpr->getType()->isBlockPointerType()) { |
| 3474 | CK = CK_BlockPointerToObjCPointerCast; |
| 3475 | } else if (userExpr->getType()->isPointerType()) { |
| 3476 | CK = CK_CPointerToObjCPointerCast; |
| 3477 | } else { |
| 3478 | CK = CK_BitCast; |
| 3479 | } |
| 3480 | userExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 3481 | CK, userExpr); |
| 3482 | } |
| 3483 | } |
| 3484 | MsgExprs.push_back(userExpr); |
| 3485 | // We've transferred the ownership to MsgExprs. For now, we *don't* null |
| 3486 | // out the argument in the original expression (since we aren't deleting |
| 3487 | // the ObjCMessageExpr). See RewritePropertyOrImplicitSetter() usage for more info. |
| 3488 | //Exp->setArg(i, 0); |
| 3489 | } |
| 3490 | // Generate the funky cast. |
| 3491 | CastExpr *cast; |
| 3492 | SmallVector<QualType, 8> ArgTypes; |
| 3493 | QualType returnType; |
| 3494 | |
| 3495 | // Push 'id' and 'SEL', the 2 implicit arguments. |
| 3496 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 3497 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 3498 | else |
| 3499 | ArgTypes.push_back(Context->getObjCIdType()); |
| 3500 | ArgTypes.push_back(Context->getObjCSelType()); |
| 3501 | if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) { |
| 3502 | // Push any user argument types. |
| 3503 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 3504 | E = OMD->param_end(); PI != E; ++PI) { |
| 3505 | QualType t = (*PI)->getType()->isObjCQualifiedIdType() |
| 3506 | ? Context->getObjCIdType() |
| 3507 | : (*PI)->getType(); |
| 3508 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
| 3509 | (void)convertBlockPointerToFunctionPointer(t); |
| 3510 | ArgTypes.push_back(t); |
| 3511 | } |
| 3512 | returnType = Exp->getType(); |
| 3513 | convertToUnqualifiedObjCType(returnType); |
| 3514 | (void)convertBlockPointerToFunctionPointer(returnType); |
| 3515 | } else { |
| 3516 | returnType = Context->getObjCIdType(); |
| 3517 | } |
| 3518 | // Get the type, we will need to reference it in a couple spots. |
| 3519 | QualType msgSendType = MsgSendFlavor->getType(); |
| 3520 | |
| 3521 | // Create a reference to the objc_msgSend() declaration. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3522 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, false, msgSendType, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3523 | VK_LValue, SourceLocation()); |
| 3524 | |
| 3525 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
| 3526 | // If we don't do this cast, we get the following bizarre warning/note: |
| 3527 | // xx.m:13: warning: function called through a non-compatible type |
| 3528 | // xx.m:13: note: if this code is reached, the program will abort |
| 3529 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 3530 | Context->getPointerType(Context->VoidTy), |
| 3531 | CK_BitCast, DRE); |
| 3532 | |
| 3533 | // Now do the "normal" pointer to function cast. |
| 3534 | QualType castType = |
| 3535 | getSimpleFunctionType(returnType, &ArgTypes[0], ArgTypes.size(), |
| 3536 | // If we don't have a method decl, force a variadic cast. |
| 3537 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true); |
| 3538 | castType = Context->getPointerType(castType); |
| 3539 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
| 3540 | cast); |
| 3541 | |
| 3542 | // Don't forget the parens to enforce the proper binding. |
| 3543 | ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast); |
| 3544 | |
| 3545 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
| 3546 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
| 3547 | MsgExprs.size(), |
| 3548 | FT->getResultType(), VK_RValue, |
| 3549 | EndLoc); |
| 3550 | Stmt *ReplacingStmt = CE; |
| 3551 | if (MsgSendStretFlavor) { |
| 3552 | // We have the method which returns a struct/union. Must also generate |
| 3553 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 3554 | // expression which dictate which one to envoke depending on size of |
| 3555 | // method's return type. |
| 3556 | |
Fariborz Jahanian | be8d55c | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 3557 | Expr *STCE = SynthMsgSendStretCallExpr(MsgSendStretFlavor, |
| 3558 | msgSendType, returnType, |
| 3559 | ArgTypes, MsgExprs, |
| 3560 | Exp->getMethodDecl()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3561 | |
| 3562 | // Build sizeof(returnType) |
| 3563 | UnaryExprOrTypeTraitExpr *sizeofExpr = |
| 3564 | new (Context) UnaryExprOrTypeTraitExpr(UETT_SizeOf, |
| 3565 | Context->getTrivialTypeSourceInfo(returnType), |
| 3566 | Context->getSizeType(), SourceLocation(), |
| 3567 | SourceLocation()); |
| 3568 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 3569 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 3570 | // For X86 it is more complicated and some kind of target specific routine |
| 3571 | // is needed to decide what to do. |
| 3572 | unsigned IntSize = |
| 3573 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 3574 | IntegerLiteral *limit = IntegerLiteral::Create(*Context, |
| 3575 | llvm::APInt(IntSize, 8), |
| 3576 | Context->IntTy, |
| 3577 | SourceLocation()); |
| 3578 | BinaryOperator *lessThanExpr = |
| 3579 | new (Context) BinaryOperator(sizeofExpr, limit, BO_LE, Context->IntTy, |
| 3580 | VK_RValue, OK_Ordinary, SourceLocation()); |
| 3581 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 3582 | ConditionalOperator *CondExpr = |
| 3583 | new (Context) ConditionalOperator(lessThanExpr, |
| 3584 | SourceLocation(), CE, |
| 3585 | SourceLocation(), STCE, |
| 3586 | returnType, VK_RValue, OK_Ordinary); |
| 3587 | ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3588 | CondExpr); |
| 3589 | } |
| 3590 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
| 3591 | return ReplacingStmt; |
| 3592 | } |
| 3593 | |
| 3594 | Stmt *RewriteModernObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
| 3595 | Stmt *ReplacingStmt = SynthMessageExpr(Exp, Exp->getLocStart(), |
| 3596 | Exp->getLocEnd()); |
| 3597 | |
| 3598 | // Now do the actual rewrite. |
| 3599 | ReplaceStmt(Exp, ReplacingStmt); |
| 3600 | |
| 3601 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
| 3602 | return ReplacingStmt; |
| 3603 | } |
| 3604 | |
| 3605 | // typedef struct objc_object Protocol; |
| 3606 | QualType RewriteModernObjC::getProtocolType() { |
| 3607 | if (!ProtocolTypeDecl) { |
| 3608 | TypeSourceInfo *TInfo |
| 3609 | = Context->getTrivialTypeSourceInfo(Context->getObjCIdType()); |
| 3610 | ProtocolTypeDecl = TypedefDecl::Create(*Context, TUDecl, |
| 3611 | SourceLocation(), SourceLocation(), |
| 3612 | &Context->Idents.get("Protocol"), |
| 3613 | TInfo); |
| 3614 | } |
| 3615 | return Context->getTypeDeclType(ProtocolTypeDecl); |
| 3616 | } |
| 3617 | |
| 3618 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
| 3619 | /// a synthesized/forward data reference (to the protocol's metadata). |
| 3620 | /// The forward references (and metadata) are generated in |
| 3621 | /// RewriteModernObjC::HandleTranslationUnit(). |
| 3622 | Stmt *RewriteModernObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
Fariborz Jahanian | 30650eb | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 3623 | std::string Name = "_OBJC_PROTOCOL_REFERENCE_$_" + |
| 3624 | Exp->getProtocol()->getNameAsString(); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3625 | IdentifierInfo *ID = &Context->Idents.get(Name); |
| 3626 | VarDecl *VD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
| 3627 | SourceLocation(), ID, getProtocolType(), 0, |
| 3628 | SC_Extern, SC_None); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3629 | DeclRefExpr *DRE = new (Context) DeclRefExpr(VD, false, getProtocolType(), |
| 3630 | VK_LValue, SourceLocation()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3631 | Expr *DerefExpr = new (Context) UnaryOperator(DRE, UO_AddrOf, |
| 3632 | Context->getPointerType(DRE->getType()), |
| 3633 | VK_RValue, OK_Ordinary, SourceLocation()); |
| 3634 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, DerefExpr->getType(), |
| 3635 | CK_BitCast, |
| 3636 | DerefExpr); |
| 3637 | ReplaceStmt(Exp, castExpr); |
| 3638 | ProtocolExprDecls.insert(Exp->getProtocol()->getCanonicalDecl()); |
| 3639 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
| 3640 | return castExpr; |
| 3641 | |
| 3642 | } |
| 3643 | |
| 3644 | bool RewriteModernObjC::BufferContainsPPDirectives(const char *startBuf, |
| 3645 | const char *endBuf) { |
| 3646 | while (startBuf < endBuf) { |
| 3647 | if (*startBuf == '#') { |
| 3648 | // Skip whitespace. |
| 3649 | for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf) |
| 3650 | ; |
| 3651 | if (!strncmp(startBuf, "if", strlen("if")) || |
| 3652 | !strncmp(startBuf, "ifdef", strlen("ifdef")) || |
| 3653 | !strncmp(startBuf, "ifndef", strlen("ifndef")) || |
| 3654 | !strncmp(startBuf, "define", strlen("define")) || |
| 3655 | !strncmp(startBuf, "undef", strlen("undef")) || |
| 3656 | !strncmp(startBuf, "else", strlen("else")) || |
| 3657 | !strncmp(startBuf, "elif", strlen("elif")) || |
| 3658 | !strncmp(startBuf, "endif", strlen("endif")) || |
| 3659 | !strncmp(startBuf, "pragma", strlen("pragma")) || |
| 3660 | !strncmp(startBuf, "include", strlen("include")) || |
| 3661 | !strncmp(startBuf, "import", strlen("import")) || |
| 3662 | !strncmp(startBuf, "include_next", strlen("include_next"))) |
| 3663 | return true; |
| 3664 | } |
| 3665 | startBuf++; |
| 3666 | } |
| 3667 | return false; |
| 3668 | } |
| 3669 | |
Fariborz Jahanian | 8fba894 | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3670 | /// IsTagDefinedInsideClass - This routine checks that a named tagged type |
| 3671 | /// is defined inside an objective-c class. If so, it returns true. |
Fariborz Jahanian | b68258f | 2012-05-01 17:46:45 +0000 | [diff] [blame] | 3672 | bool RewriteModernObjC::IsTagDefinedInsideClass(ObjCContainerDecl *IDecl, |
Fariborz Jahanian | 8fba894 | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3673 | TagDecl *Tag, |
| 3674 | bool &IsNamedDefinition) { |
Fariborz Jahanian | 89585e80 | 2012-04-30 19:46:53 +0000 | [diff] [blame] | 3675 | if (!IDecl) |
| 3676 | return false; |
| 3677 | SourceLocation TagLocation; |
| 3678 | if (RecordDecl *RD = dyn_cast<RecordDecl>(Tag)) { |
| 3679 | RD = RD->getDefinition(); |
Fariborz Jahanian | 8fba894 | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3680 | if (!RD || !RD->getDeclName().getAsIdentifierInfo()) |
Fariborz Jahanian | 89585e80 | 2012-04-30 19:46:53 +0000 | [diff] [blame] | 3681 | return false; |
Fariborz Jahanian | 8fba894 | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3682 | IsNamedDefinition = true; |
Fariborz Jahanian | 89585e80 | 2012-04-30 19:46:53 +0000 | [diff] [blame] | 3683 | TagLocation = RD->getLocation(); |
| 3684 | return Context->getSourceManager().isBeforeInTranslationUnit( |
Fariborz Jahanian | 8fba894 | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3685 | IDecl->getLocation(), TagLocation); |
| 3686 | } |
| 3687 | if (EnumDecl *ED = dyn_cast<EnumDecl>(Tag)) { |
| 3688 | if (!ED || !ED->getDeclName().getAsIdentifierInfo()) |
| 3689 | return false; |
| 3690 | IsNamedDefinition = true; |
| 3691 | TagLocation = ED->getLocation(); |
| 3692 | return Context->getSourceManager().isBeforeInTranslationUnit( |
| 3693 | IDecl->getLocation(), TagLocation); |
| 3694 | |
Fariborz Jahanian | 89585e80 | 2012-04-30 19:46:53 +0000 | [diff] [blame] | 3695 | } |
| 3696 | return false; |
| 3697 | } |
| 3698 | |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3699 | /// RewriteObjCFieldDeclType - This routine rewrites a type into the buffer. |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3700 | /// It handles elaborated types, as well as enum types in the process. |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3701 | bool RewriteModernObjC::RewriteObjCFieldDeclType(QualType &Type, |
| 3702 | std::string &Result) { |
Fariborz Jahanian | f5eac48 | 2012-05-02 17:34:59 +0000 | [diff] [blame] | 3703 | if (isa<TypedefType>(Type)) { |
| 3704 | Result += "\t"; |
| 3705 | return false; |
| 3706 | } |
| 3707 | |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3708 | if (Type->isArrayType()) { |
| 3709 | QualType ElemTy = Context->getBaseElementType(Type); |
| 3710 | return RewriteObjCFieldDeclType(ElemTy, Result); |
| 3711 | } |
| 3712 | else if (Type->isRecordType()) { |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3713 | RecordDecl *RD = Type->getAs<RecordType>()->getDecl(); |
| 3714 | if (RD->isCompleteDefinition()) { |
| 3715 | if (RD->isStruct()) |
| 3716 | Result += "\n\tstruct "; |
| 3717 | else if (RD->isUnion()) |
| 3718 | Result += "\n\tunion "; |
| 3719 | else |
| 3720 | assert(false && "class not allowed as an ivar type"); |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3721 | |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3722 | Result += RD->getName(); |
Fariborz Jahanian | 8fba894 | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3723 | if (GlobalDefinedTags.count(RD)) { |
| 3724 | // struct/union is defined globally, use it. |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3725 | Result += " "; |
| 3726 | return true; |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3727 | } |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3728 | Result += " {\n"; |
| 3729 | for (RecordDecl::field_iterator i = RD->field_begin(), |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3730 | e = RD->field_end(); i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3731 | FieldDecl *FD = *i; |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3732 | RewriteObjCFieldDecl(FD, Result); |
| 3733 | } |
| 3734 | Result += "\t} "; |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3735 | return true; |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3736 | } |
| 3737 | } |
| 3738 | else if (Type->isEnumeralType()) { |
| 3739 | EnumDecl *ED = Type->getAs<EnumType>()->getDecl(); |
| 3740 | if (ED->isCompleteDefinition()) { |
| 3741 | Result += "\n\tenum "; |
| 3742 | Result += ED->getName(); |
Fariborz Jahanian | 8fba894 | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3743 | if (GlobalDefinedTags.count(ED)) { |
| 3744 | // Enum is globall defined, use it. |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3745 | Result += " "; |
| 3746 | return true; |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3747 | } |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3748 | |
| 3749 | Result += " {\n"; |
| 3750 | for (EnumDecl::enumerator_iterator EC = ED->enumerator_begin(), |
| 3751 | ECEnd = ED->enumerator_end(); EC != ECEnd; ++EC) { |
| 3752 | Result += "\t"; Result += EC->getName(); Result += " = "; |
| 3753 | llvm::APSInt Val = EC->getInitVal(); |
| 3754 | Result += Val.toString(10); |
| 3755 | Result += ",\n"; |
| 3756 | } |
| 3757 | Result += "\t} "; |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3758 | return true; |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3759 | } |
| 3760 | } |
| 3761 | |
| 3762 | Result += "\t"; |
| 3763 | convertObjCTypeToCStyleType(Type); |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3764 | return false; |
| 3765 | } |
| 3766 | |
| 3767 | |
| 3768 | /// RewriteObjCFieldDecl - This routine rewrites a field into the buffer. |
| 3769 | /// It handles elaborated types, as well as enum types in the process. |
| 3770 | void RewriteModernObjC::RewriteObjCFieldDecl(FieldDecl *fieldDecl, |
| 3771 | std::string &Result) { |
| 3772 | QualType Type = fieldDecl->getType(); |
| 3773 | std::string Name = fieldDecl->getNameAsString(); |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3774 | |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3775 | bool EleboratedType = RewriteObjCFieldDeclType(Type, Result); |
| 3776 | if (!EleboratedType) |
| 3777 | Type.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3778 | Result += Name; |
| 3779 | if (fieldDecl->isBitField()) { |
| 3780 | Result += " : "; Result += utostr(fieldDecl->getBitWidthValue(*Context)); |
| 3781 | } |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3782 | else if (EleboratedType && Type->isArrayType()) { |
| 3783 | CanQualType CType = Context->getCanonicalType(Type); |
| 3784 | while (isa<ArrayType>(CType)) { |
| 3785 | if (const ConstantArrayType *CAT = Context->getAsConstantArrayType(CType)) { |
| 3786 | Result += "["; |
| 3787 | llvm::APInt Dim = CAT->getSize(); |
| 3788 | Result += utostr(Dim.getZExtValue()); |
| 3789 | Result += "]"; |
| 3790 | } |
| 3791 | CType = CType->getAs<ArrayType>()->getElementType(); |
| 3792 | } |
| 3793 | } |
| 3794 | |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3795 | Result += ";\n"; |
| 3796 | } |
| 3797 | |
Fariborz Jahanian | 8fba894 | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3798 | /// RewriteLocallyDefinedNamedAggregates - This routine rewrites locally defined |
| 3799 | /// named aggregate types into the input buffer. |
| 3800 | void RewriteModernObjC::RewriteLocallyDefinedNamedAggregates(FieldDecl *fieldDecl, |
| 3801 | std::string &Result) { |
| 3802 | QualType Type = fieldDecl->getType(); |
Fariborz Jahanian | f5eac48 | 2012-05-02 17:34:59 +0000 | [diff] [blame] | 3803 | if (isa<TypedefType>(Type)) |
| 3804 | return; |
Fariborz Jahanian | 8fba894 | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3805 | if (Type->isArrayType()) |
| 3806 | Type = Context->getBaseElementType(Type); |
Fariborz Jahanian | b68258f | 2012-05-01 17:46:45 +0000 | [diff] [blame] | 3807 | ObjCContainerDecl *IDecl = |
| 3808 | dyn_cast<ObjCContainerDecl>(fieldDecl->getDeclContext()); |
Fariborz Jahanian | 8fba894 | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3809 | |
| 3810 | TagDecl *TD = 0; |
| 3811 | if (Type->isRecordType()) { |
| 3812 | TD = Type->getAs<RecordType>()->getDecl(); |
| 3813 | } |
| 3814 | else if (Type->isEnumeralType()) { |
| 3815 | TD = Type->getAs<EnumType>()->getDecl(); |
| 3816 | } |
| 3817 | |
| 3818 | if (TD) { |
| 3819 | if (GlobalDefinedTags.count(TD)) |
| 3820 | return; |
| 3821 | |
| 3822 | bool IsNamedDefinition = false; |
| 3823 | if (IsTagDefinedInsideClass(IDecl, TD, IsNamedDefinition)) { |
| 3824 | RewriteObjCFieldDeclType(Type, Result); |
| 3825 | Result += ";"; |
| 3826 | } |
| 3827 | if (IsNamedDefinition) |
| 3828 | GlobalDefinedTags.insert(TD); |
| 3829 | } |
| 3830 | |
| 3831 | } |
| 3832 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3833 | /// RewriteObjCInternalStruct - Rewrite one internal struct corresponding to |
| 3834 | /// an objective-c class with ivars. |
| 3835 | void RewriteModernObjC::RewriteObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
| 3836 | std::string &Result) { |
| 3837 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
| 3838 | assert(CDecl->getName() != "" && |
| 3839 | "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3840 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 3841 | SmallVector<ObjCIvarDecl *, 8> IVars; |
| 3842 | for (ObjCIvarDecl *IVD = CDecl->all_declared_ivar_begin(); |
Fariborz Jahanian | 9a2105b | 2012-03-06 17:16:27 +0000 | [diff] [blame] | 3843 | IVD; IVD = IVD->getNextIvar()) |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 3844 | IVars.push_back(IVD); |
Fariborz Jahanian | 9a2105b | 2012-03-06 17:16:27 +0000 | [diff] [blame] | 3845 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3846 | SourceLocation LocStart = CDecl->getLocStart(); |
| 3847 | SourceLocation LocEnd = CDecl->getEndOfDefinitionLoc(); |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 3848 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3849 | const char *startBuf = SM->getCharacterData(LocStart); |
| 3850 | const char *endBuf = SM->getCharacterData(LocEnd); |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 3851 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3852 | // If no ivars and no root or if its root, directly or indirectly, |
| 3853 | // 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] | 3854 | if ((!CDecl->isThisDeclarationADefinition() || IVars.size() == 0) && |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3855 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
| 3856 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
| 3857 | ReplaceText(LocStart, endBuf-startBuf, Result); |
| 3858 | return; |
| 3859 | } |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 3860 | |
Fariborz Jahanian | 8fba894 | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3861 | // Insert named struct/union definitions inside class to |
| 3862 | // outer scope. This follows semantics of locally defined |
| 3863 | // struct/unions in objective-c classes. |
| 3864 | for (unsigned i = 0, e = IVars.size(); i < e; i++) |
| 3865 | RewriteLocallyDefinedNamedAggregates(IVars[i], Result); |
| 3866 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3867 | Result += "\nstruct "; |
| 3868 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 3869 | Result += "_IMPL {\n"; |
| 3870 | |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 3871 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 3872 | Result += "\tstruct "; Result += RCDecl->getNameAsString(); |
| 3873 | Result += "_IMPL "; Result += RCDecl->getNameAsString(); |
| 3874 | Result += "_IVARS;\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3875 | } |
Fariborz Jahanian | 8fba894 | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 3876 | |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3877 | for (unsigned i = 0, e = IVars.size(); i < e; i++) |
| 3878 | RewriteObjCFieldDecl(IVars[i], Result); |
Fariborz Jahanian | 0b17b9a | 2012-02-12 21:36:23 +0000 | [diff] [blame] | 3879 | |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 3880 | Result += "};\n"; |
| 3881 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
| 3882 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 3883 | // Mark this struct as having been generated. |
| 3884 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
| 3885 | llvm_unreachable("struct already synthesize- RewriteObjCInternalStruct"); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3886 | } |
| 3887 | |
Fariborz Jahanian | 72c88f1 | 2012-02-22 18:13:25 +0000 | [diff] [blame] | 3888 | /// RewriteIvarOffsetSymbols - Rewrite ivar offset symbols of those ivars which |
| 3889 | /// have been referenced in an ivar access expression. |
| 3890 | void RewriteModernObjC::RewriteIvarOffsetSymbols(ObjCInterfaceDecl *CDecl, |
| 3891 | std::string &Result) { |
| 3892 | // write out ivar offset symbols which have been referenced in an ivar |
| 3893 | // access expression. |
| 3894 | llvm::SmallPtrSet<ObjCIvarDecl *, 8> Ivars = ReferencedIvars[CDecl]; |
| 3895 | if (Ivars.empty()) |
| 3896 | return; |
| 3897 | for (llvm::SmallPtrSet<ObjCIvarDecl *, 8>::iterator i = Ivars.begin(), |
| 3898 | e = Ivars.end(); i != e; i++) { |
| 3899 | ObjCIvarDecl *IvarDecl = (*i); |
Fariborz Jahanian | 40a777a | 2012-03-12 16:46:58 +0000 | [diff] [blame] | 3900 | Result += "\n"; |
| 3901 | if (LangOpts.MicrosoftExt) |
| 3902 | Result += "__declspec(allocate(\".objc_ivar$B\")) "; |
Fariborz Jahanian | 297976d | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 3903 | Result += "extern \"C\" "; |
Fariborz Jahanian | 40a777a | 2012-03-12 16:46:58 +0000 | [diff] [blame] | 3904 | if (LangOpts.MicrosoftExt && |
| 3905 | IvarDecl->getAccessControl() != ObjCIvarDecl::Private && |
Fariborz Jahanian | 297976d | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 3906 | IvarDecl->getAccessControl() != ObjCIvarDecl::Package) |
| 3907 | Result += "__declspec(dllimport) "; |
| 3908 | |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 3909 | Result += "unsigned long "; |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 3910 | WriteInternalIvarName(CDecl, IvarDecl, Result); |
| 3911 | Result += ";"; |
Fariborz Jahanian | 72c88f1 | 2012-02-22 18:13:25 +0000 | [diff] [blame] | 3912 | } |
| 3913 | } |
| 3914 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3915 | //===----------------------------------------------------------------------===// |
| 3916 | // Meta Data Emission |
| 3917 | //===----------------------------------------------------------------------===// |
| 3918 | |
| 3919 | |
| 3920 | /// RewriteImplementations - This routine rewrites all method implementations |
| 3921 | /// and emits meta-data. |
| 3922 | |
| 3923 | void RewriteModernObjC::RewriteImplementations() { |
| 3924 | int ClsDefCount = ClassImplementation.size(); |
| 3925 | int CatDefCount = CategoryImplementation.size(); |
| 3926 | |
| 3927 | // Rewrite implemented methods |
Fariborz Jahanian | 8f1fed0 | 2012-02-11 20:10:52 +0000 | [diff] [blame] | 3928 | for (int i = 0; i < ClsDefCount; i++) { |
| 3929 | ObjCImplementationDecl *OIMP = ClassImplementation[i]; |
| 3930 | ObjCInterfaceDecl *CDecl = OIMP->getClassInterface(); |
| 3931 | if (CDecl->isImplicitInterfaceDecl()) |
Fariborz Jahanian | cf4c60f | 2012-02-17 22:20:12 +0000 | [diff] [blame] | 3932 | assert(false && |
| 3933 | "Legacy implicit interface rewriting not supported in moder abi"); |
Fariborz Jahanian | 8f1fed0 | 2012-02-11 20:10:52 +0000 | [diff] [blame] | 3934 | RewriteImplementationDecl(OIMP); |
| 3935 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3936 | |
Fariborz Jahanian | 8c00a1b | 2012-02-17 20:33:00 +0000 | [diff] [blame] | 3937 | for (int i = 0; i < CatDefCount; i++) { |
| 3938 | ObjCCategoryImplDecl *CIMP = CategoryImplementation[i]; |
| 3939 | ObjCInterfaceDecl *CDecl = CIMP->getClassInterface(); |
| 3940 | if (CDecl->isImplicitInterfaceDecl()) |
| 3941 | assert(false && |
| 3942 | "Legacy implicit interface rewriting not supported in moder abi"); |
Fariborz Jahanian | 8c00a1b | 2012-02-17 20:33:00 +0000 | [diff] [blame] | 3943 | RewriteImplementationDecl(CIMP); |
| 3944 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3945 | } |
| 3946 | |
| 3947 | void RewriteModernObjC::RewriteByRefString(std::string &ResultStr, |
| 3948 | const std::string &Name, |
| 3949 | ValueDecl *VD, bool def) { |
| 3950 | assert(BlockByRefDeclNo.count(VD) && |
| 3951 | "RewriteByRefString: ByRef decl missing"); |
| 3952 | if (def) |
| 3953 | ResultStr += "struct "; |
| 3954 | ResultStr += "__Block_byref_" + Name + |
| 3955 | "_" + utostr(BlockByRefDeclNo[VD]) ; |
| 3956 | } |
| 3957 | |
| 3958 | static bool HasLocalVariableExternalStorage(ValueDecl *VD) { |
| 3959 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 3960 | return (Var->isFunctionOrMethodVarDecl() && !Var->hasLocalStorage()); |
| 3961 | return false; |
| 3962 | } |
| 3963 | |
| 3964 | std::string RewriteModernObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, |
| 3965 | StringRef funcName, |
| 3966 | std::string Tag) { |
| 3967 | const FunctionType *AFT = CE->getFunctionType(); |
| 3968 | QualType RT = AFT->getResultType(); |
| 3969 | std::string StructRef = "struct " + Tag; |
| 3970 | std::string S = "static " + RT.getAsString(Context->getPrintingPolicy()) + " __" + |
Fariborz Jahanian | df474ec | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 3971 | funcName.str() + "_block_func_" + utostr(i); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3972 | |
| 3973 | BlockDecl *BD = CE->getBlockDecl(); |
| 3974 | |
| 3975 | if (isa<FunctionNoProtoType>(AFT)) { |
| 3976 | // No user-supplied arguments. Still need to pass in a pointer to the |
| 3977 | // block (to reference imported block decl refs). |
| 3978 | S += "(" + StructRef + " *__cself)"; |
| 3979 | } else if (BD->param_empty()) { |
| 3980 | S += "(" + StructRef + " *__cself)"; |
| 3981 | } else { |
| 3982 | const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); |
| 3983 | assert(FT && "SynthesizeBlockFunc: No function proto"); |
| 3984 | S += '('; |
| 3985 | // first add the implicit argument. |
| 3986 | S += StructRef + " *__cself, "; |
| 3987 | std::string ParamStr; |
| 3988 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 3989 | E = BD->param_end(); AI != E; ++AI) { |
| 3990 | if (AI != BD->param_begin()) S += ", "; |
| 3991 | ParamStr = (*AI)->getNameAsString(); |
| 3992 | QualType QT = (*AI)->getType(); |
Fariborz Jahanian | 2610f90 | 2012-03-27 16:42:20 +0000 | [diff] [blame] | 3993 | (void)convertBlockPointerToFunctionPointer(QT); |
| 3994 | QT.getAsStringInternal(ParamStr, Context->getPrintingPolicy()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3995 | S += ParamStr; |
| 3996 | } |
| 3997 | if (FT->isVariadic()) { |
| 3998 | if (!BD->param_empty()) S += ", "; |
| 3999 | S += "..."; |
| 4000 | } |
| 4001 | S += ')'; |
| 4002 | } |
| 4003 | S += " {\n"; |
| 4004 | |
| 4005 | // Create local declarations to avoid rewriting all closure decl ref exprs. |
| 4006 | // First, emit a declaration for all "by ref" decls. |
| 4007 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 4008 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 4009 | S += " "; |
| 4010 | std::string Name = (*I)->getNameAsString(); |
| 4011 | std::string TypeString; |
| 4012 | RewriteByRefString(TypeString, Name, (*I)); |
| 4013 | TypeString += " *"; |
| 4014 | Name = TypeString + Name; |
| 4015 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; |
| 4016 | } |
| 4017 | // Next, emit a declaration for all "by copy" declarations. |
| 4018 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 4019 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 4020 | S += " "; |
| 4021 | // Handle nested closure invocation. For example: |
| 4022 | // |
| 4023 | // void (^myImportedClosure)(void); |
| 4024 | // myImportedClosure = ^(void) { setGlobalInt(x + y); }; |
| 4025 | // |
| 4026 | // void (^anotherClosure)(void); |
| 4027 | // anotherClosure = ^(void) { |
| 4028 | // myImportedClosure(); // import and invoke the closure |
| 4029 | // }; |
| 4030 | // |
| 4031 | if (isTopLevelBlockPointerType((*I)->getType())) { |
| 4032 | RewriteBlockPointerTypeVariable(S, (*I)); |
| 4033 | S += " = ("; |
| 4034 | RewriteBlockPointerType(S, (*I)->getType()); |
| 4035 | S += ")"; |
| 4036 | S += "__cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; |
| 4037 | } |
| 4038 | else { |
| 4039 | std::string Name = (*I)->getNameAsString(); |
| 4040 | QualType QT = (*I)->getType(); |
| 4041 | if (HasLocalVariableExternalStorage(*I)) |
| 4042 | QT = Context->getPointerType(QT); |
| 4043 | QT.getAsStringInternal(Name, Context->getPrintingPolicy()); |
| 4044 | S += Name + " = __cself->" + |
| 4045 | (*I)->getNameAsString() + "; // bound by copy\n"; |
| 4046 | } |
| 4047 | } |
| 4048 | std::string RewrittenStr = RewrittenBlockExprs[CE]; |
| 4049 | const char *cstr = RewrittenStr.c_str(); |
| 4050 | while (*cstr++ != '{') ; |
| 4051 | S += cstr; |
| 4052 | S += "\n"; |
| 4053 | return S; |
| 4054 | } |
| 4055 | |
| 4056 | std::string RewriteModernObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
| 4057 | StringRef funcName, |
| 4058 | std::string Tag) { |
| 4059 | std::string StructRef = "struct " + Tag; |
| 4060 | std::string S = "static void __"; |
| 4061 | |
| 4062 | S += funcName; |
| 4063 | S += "_block_copy_" + utostr(i); |
| 4064 | S += "(" + StructRef; |
| 4065 | S += "*dst, " + StructRef; |
| 4066 | S += "*src) {"; |
| 4067 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
| 4068 | E = ImportedBlockDecls.end(); I != E; ++I) { |
| 4069 | ValueDecl *VD = (*I); |
| 4070 | S += "_Block_object_assign((void*)&dst->"; |
| 4071 | S += (*I)->getNameAsString(); |
| 4072 | S += ", (void*)src->"; |
| 4073 | S += (*I)->getNameAsString(); |
| 4074 | if (BlockByRefDeclsPtrSet.count((*I))) |
| 4075 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
| 4076 | else if (VD->getType()->isBlockPointerType()) |
| 4077 | S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);"; |
| 4078 | else |
| 4079 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
| 4080 | } |
| 4081 | S += "}\n"; |
| 4082 | |
| 4083 | S += "\nstatic void __"; |
| 4084 | S += funcName; |
| 4085 | S += "_block_dispose_" + utostr(i); |
| 4086 | S += "(" + StructRef; |
| 4087 | S += "*src) {"; |
| 4088 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
| 4089 | E = ImportedBlockDecls.end(); I != E; ++I) { |
| 4090 | ValueDecl *VD = (*I); |
| 4091 | S += "_Block_object_dispose((void*)src->"; |
| 4092 | S += (*I)->getNameAsString(); |
| 4093 | if (BlockByRefDeclsPtrSet.count((*I))) |
| 4094 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
| 4095 | else if (VD->getType()->isBlockPointerType()) |
| 4096 | S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);"; |
| 4097 | else |
| 4098 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
| 4099 | } |
| 4100 | S += "}\n"; |
| 4101 | return S; |
| 4102 | } |
| 4103 | |
| 4104 | std::string RewriteModernObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 4105 | std::string Desc) { |
| 4106 | std::string S = "\nstruct " + Tag; |
| 4107 | std::string Constructor = " " + Tag; |
| 4108 | |
| 4109 | S += " {\n struct __block_impl impl;\n"; |
| 4110 | S += " struct " + Desc; |
| 4111 | S += "* Desc;\n"; |
| 4112 | |
| 4113 | Constructor += "(void *fp, "; // Invoke function pointer. |
| 4114 | Constructor += "struct " + Desc; // Descriptor pointer. |
| 4115 | Constructor += " *desc"; |
| 4116 | |
| 4117 | if (BlockDeclRefs.size()) { |
| 4118 | // Output all "by copy" declarations. |
| 4119 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 4120 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 4121 | S += " "; |
| 4122 | std::string FieldName = (*I)->getNameAsString(); |
| 4123 | std::string ArgName = "_" + FieldName; |
| 4124 | // Handle nested closure invocation. For example: |
| 4125 | // |
| 4126 | // void (^myImportedBlock)(void); |
| 4127 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
| 4128 | // |
| 4129 | // void (^anotherBlock)(void); |
| 4130 | // anotherBlock = ^(void) { |
| 4131 | // myImportedBlock(); // import and invoke the closure |
| 4132 | // }; |
| 4133 | // |
| 4134 | if (isTopLevelBlockPointerType((*I)->getType())) { |
| 4135 | S += "struct __block_impl *"; |
| 4136 | Constructor += ", void *" + ArgName; |
| 4137 | } else { |
| 4138 | QualType QT = (*I)->getType(); |
| 4139 | if (HasLocalVariableExternalStorage(*I)) |
| 4140 | QT = Context->getPointerType(QT); |
| 4141 | QT.getAsStringInternal(FieldName, Context->getPrintingPolicy()); |
| 4142 | QT.getAsStringInternal(ArgName, Context->getPrintingPolicy()); |
| 4143 | Constructor += ", " + ArgName; |
| 4144 | } |
| 4145 | S += FieldName + ";\n"; |
| 4146 | } |
| 4147 | // Output all "by ref" declarations. |
| 4148 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 4149 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 4150 | S += " "; |
| 4151 | std::string FieldName = (*I)->getNameAsString(); |
| 4152 | std::string ArgName = "_" + FieldName; |
| 4153 | { |
| 4154 | std::string TypeString; |
| 4155 | RewriteByRefString(TypeString, FieldName, (*I)); |
| 4156 | TypeString += " *"; |
| 4157 | FieldName = TypeString + FieldName; |
| 4158 | ArgName = TypeString + ArgName; |
| 4159 | Constructor += ", " + ArgName; |
| 4160 | } |
| 4161 | S += FieldName + "; // by ref\n"; |
| 4162 | } |
| 4163 | // Finish writing the constructor. |
| 4164 | Constructor += ", int flags=0)"; |
| 4165 | // Initialize all "by copy" arguments. |
| 4166 | bool firsTime = true; |
| 4167 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 4168 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 4169 | std::string Name = (*I)->getNameAsString(); |
| 4170 | if (firsTime) { |
| 4171 | Constructor += " : "; |
| 4172 | firsTime = false; |
| 4173 | } |
| 4174 | else |
| 4175 | Constructor += ", "; |
| 4176 | if (isTopLevelBlockPointerType((*I)->getType())) |
| 4177 | Constructor += Name + "((struct __block_impl *)_" + Name + ")"; |
| 4178 | else |
| 4179 | Constructor += Name + "(_" + Name + ")"; |
| 4180 | } |
| 4181 | // Initialize all "by ref" arguments. |
| 4182 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 4183 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 4184 | std::string Name = (*I)->getNameAsString(); |
| 4185 | if (firsTime) { |
| 4186 | Constructor += " : "; |
| 4187 | firsTime = false; |
| 4188 | } |
| 4189 | else |
| 4190 | Constructor += ", "; |
| 4191 | Constructor += Name + "(_" + Name + "->__forwarding)"; |
| 4192 | } |
| 4193 | |
| 4194 | Constructor += " {\n"; |
| 4195 | if (GlobalVarDecl) |
| 4196 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 4197 | else |
| 4198 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
| 4199 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 4200 | |
| 4201 | Constructor += " Desc = desc;\n"; |
| 4202 | } else { |
| 4203 | // Finish writing the constructor. |
| 4204 | Constructor += ", int flags=0) {\n"; |
| 4205 | if (GlobalVarDecl) |
| 4206 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 4207 | else |
| 4208 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
| 4209 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 4210 | Constructor += " Desc = desc;\n"; |
| 4211 | } |
| 4212 | Constructor += " "; |
| 4213 | Constructor += "}\n"; |
| 4214 | S += Constructor; |
| 4215 | S += "};\n"; |
| 4216 | return S; |
| 4217 | } |
| 4218 | |
| 4219 | std::string RewriteModernObjC::SynthesizeBlockDescriptor(std::string DescTag, |
| 4220 | std::string ImplTag, int i, |
| 4221 | StringRef FunName, |
| 4222 | unsigned hasCopy) { |
| 4223 | std::string S = "\nstatic struct " + DescTag; |
| 4224 | |
Fariborz Jahanian | 8b08adb | 2012-05-03 21:44:12 +0000 | [diff] [blame] | 4225 | S += " {\n size_t reserved;\n"; |
| 4226 | S += " size_t Block_size;\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4227 | if (hasCopy) { |
| 4228 | S += " void (*copy)(struct "; |
| 4229 | S += ImplTag; S += "*, struct "; |
| 4230 | S += ImplTag; S += "*);\n"; |
| 4231 | |
| 4232 | S += " void (*dispose)(struct "; |
| 4233 | S += ImplTag; S += "*);\n"; |
| 4234 | } |
| 4235 | S += "} "; |
| 4236 | |
| 4237 | S += DescTag + "_DATA = { 0, sizeof(struct "; |
| 4238 | S += ImplTag + ")"; |
| 4239 | if (hasCopy) { |
| 4240 | S += ", __" + FunName.str() + "_block_copy_" + utostr(i); |
| 4241 | S += ", __" + FunName.str() + "_block_dispose_" + utostr(i); |
| 4242 | } |
| 4243 | S += "};\n"; |
| 4244 | return S; |
| 4245 | } |
| 4246 | |
| 4247 | void RewriteModernObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, |
| 4248 | StringRef FunName) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4249 | bool RewriteSC = (GlobalVarDecl && |
| 4250 | !Blocks.empty() && |
| 4251 | GlobalVarDecl->getStorageClass() == SC_Static && |
| 4252 | GlobalVarDecl->getType().getCVRQualifiers()); |
| 4253 | if (RewriteSC) { |
| 4254 | std::string SC(" void __"); |
| 4255 | SC += GlobalVarDecl->getNameAsString(); |
| 4256 | SC += "() {}"; |
| 4257 | InsertText(FunLocStart, SC); |
| 4258 | } |
| 4259 | |
| 4260 | // Insert closures that were part of the function. |
| 4261 | for (unsigned i = 0, count=0; i < Blocks.size(); i++) { |
| 4262 | CollectBlockDeclRefInfo(Blocks[i]); |
| 4263 | // Need to copy-in the inner copied-in variables not actually used in this |
| 4264 | // block. |
| 4265 | for (int j = 0; j < InnerDeclRefsCount[i]; j++) { |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4266 | DeclRefExpr *Exp = InnerDeclRefs[count++]; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4267 | ValueDecl *VD = Exp->getDecl(); |
| 4268 | BlockDeclRefs.push_back(Exp); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4269 | if (!VD->hasAttr<BlocksAttr>()) { |
| 4270 | if (!BlockByCopyDeclsPtrSet.count(VD)) { |
| 4271 | BlockByCopyDeclsPtrSet.insert(VD); |
| 4272 | BlockByCopyDecls.push_back(VD); |
| 4273 | } |
| 4274 | continue; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4275 | } |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4276 | |
| 4277 | if (!BlockByRefDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4278 | BlockByRefDeclsPtrSet.insert(VD); |
| 4279 | BlockByRefDecls.push_back(VD); |
| 4280 | } |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4281 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4282 | // imported objects in the inner blocks not used in the outer |
| 4283 | // blocks must be copied/disposed in the outer block as well. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4284 | if (VD->getType()->isObjCObjectPointerType() || |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4285 | VD->getType()->isBlockPointerType()) |
| 4286 | ImportedBlockDecls.insert(VD); |
| 4287 | } |
| 4288 | |
| 4289 | std::string ImplTag = "__" + FunName.str() + "_block_impl_" + utostr(i); |
| 4290 | std::string DescTag = "__" + FunName.str() + "_block_desc_" + utostr(i); |
| 4291 | |
| 4292 | std::string CI = SynthesizeBlockImpl(Blocks[i], ImplTag, DescTag); |
| 4293 | |
| 4294 | InsertText(FunLocStart, CI); |
| 4295 | |
| 4296 | std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, ImplTag); |
| 4297 | |
| 4298 | InsertText(FunLocStart, CF); |
| 4299 | |
| 4300 | if (ImportedBlockDecls.size()) { |
| 4301 | std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, ImplTag); |
| 4302 | InsertText(FunLocStart, HF); |
| 4303 | } |
| 4304 | std::string BD = SynthesizeBlockDescriptor(DescTag, ImplTag, i, FunName, |
| 4305 | ImportedBlockDecls.size() > 0); |
| 4306 | InsertText(FunLocStart, BD); |
| 4307 | |
| 4308 | BlockDeclRefs.clear(); |
| 4309 | BlockByRefDecls.clear(); |
| 4310 | BlockByRefDeclsPtrSet.clear(); |
| 4311 | BlockByCopyDecls.clear(); |
| 4312 | BlockByCopyDeclsPtrSet.clear(); |
| 4313 | ImportedBlockDecls.clear(); |
| 4314 | } |
| 4315 | if (RewriteSC) { |
| 4316 | // Must insert any 'const/volatile/static here. Since it has been |
| 4317 | // removed as result of rewriting of block literals. |
| 4318 | std::string SC; |
| 4319 | if (GlobalVarDecl->getStorageClass() == SC_Static) |
| 4320 | SC = "static "; |
| 4321 | if (GlobalVarDecl->getType().isConstQualified()) |
| 4322 | SC += "const "; |
| 4323 | if (GlobalVarDecl->getType().isVolatileQualified()) |
| 4324 | SC += "volatile "; |
| 4325 | if (GlobalVarDecl->getType().isRestrictQualified()) |
| 4326 | SC += "restrict "; |
| 4327 | InsertText(FunLocStart, SC); |
| 4328 | } |
Fariborz Jahanian | df474ec | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 4329 | if (GlobalConstructionExp) { |
| 4330 | // extra fancy dance for global literal expression. |
| 4331 | |
| 4332 | // Always the latest block expression on the block stack. |
| 4333 | std::string Tag = "__"; |
| 4334 | Tag += FunName; |
| 4335 | Tag += "_block_impl_"; |
| 4336 | Tag += utostr(Blocks.size()-1); |
| 4337 | std::string globalBuf = "static "; |
| 4338 | globalBuf += Tag; globalBuf += " "; |
| 4339 | std::string SStr; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4340 | |
Fariborz Jahanian | df474ec | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 4341 | llvm::raw_string_ostream constructorExprBuf(SStr); |
| 4342 | GlobalConstructionExp->printPretty(constructorExprBuf, *Context, 0, |
| 4343 | PrintingPolicy(LangOpts)); |
| 4344 | globalBuf += constructorExprBuf.str(); |
| 4345 | globalBuf += ";\n"; |
| 4346 | InsertText(FunLocStart, globalBuf); |
| 4347 | GlobalConstructionExp = 0; |
| 4348 | } |
| 4349 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4350 | Blocks.clear(); |
| 4351 | InnerDeclRefsCount.clear(); |
| 4352 | InnerDeclRefs.clear(); |
| 4353 | RewrittenBlockExprs.clear(); |
| 4354 | } |
| 4355 | |
| 4356 | void RewriteModernObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { |
Fariborz Jahanian | 0418953 | 2012-04-25 17:56:48 +0000 | [diff] [blame] | 4357 | SourceLocation FunLocStart = |
| 4358 | (!Blocks.empty()) ? getFunctionSourceLocation(*this, FD) |
| 4359 | : FD->getTypeSpecStartLoc(); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4360 | StringRef FuncName = FD->getName(); |
| 4361 | |
| 4362 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 4363 | } |
| 4364 | |
| 4365 | static void BuildUniqueMethodName(std::string &Name, |
| 4366 | ObjCMethodDecl *MD) { |
| 4367 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
| 4368 | Name = IFace->getName(); |
| 4369 | Name += "__" + MD->getSelector().getAsString(); |
| 4370 | // Convert colons to underscores. |
| 4371 | std::string::size_type loc = 0; |
| 4372 | while ((loc = Name.find(":", loc)) != std::string::npos) |
| 4373 | Name.replace(loc, 1, "_"); |
| 4374 | } |
| 4375 | |
| 4376 | void RewriteModernObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { |
| 4377 | //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n"); |
| 4378 | //SourceLocation FunLocStart = MD->getLocStart(); |
| 4379 | SourceLocation FunLocStart = MD->getLocStart(); |
| 4380 | std::string FuncName; |
| 4381 | BuildUniqueMethodName(FuncName, MD); |
| 4382 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 4383 | } |
| 4384 | |
| 4385 | void RewriteModernObjC::GetBlockDeclRefExprs(Stmt *S) { |
| 4386 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
| 4387 | if (*CI) { |
| 4388 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 4389 | GetBlockDeclRefExprs(CBE->getBody()); |
| 4390 | else |
| 4391 | GetBlockDeclRefExprs(*CI); |
| 4392 | } |
| 4393 | // Handle specific things. |
Fariborz Jahanian | 0e97681 | 2012-04-16 23:00:57 +0000 | [diff] [blame] | 4394 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 4395 | if (DRE->refersToEnclosingLocal()) { |
| 4396 | // FIXME: Handle enums. |
| 4397 | if (!isa<FunctionDecl>(DRE->getDecl())) |
| 4398 | BlockDeclRefs.push_back(DRE); |
| 4399 | if (HasLocalVariableExternalStorage(DRE->getDecl())) |
| 4400 | BlockDeclRefs.push_back(DRE); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4401 | } |
Fariborz Jahanian | 0e97681 | 2012-04-16 23:00:57 +0000 | [diff] [blame] | 4402 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4403 | |
| 4404 | return; |
| 4405 | } |
| 4406 | |
| 4407 | void RewriteModernObjC::GetInnerBlockDeclRefExprs(Stmt *S, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4408 | SmallVector<DeclRefExpr *, 8> &InnerBlockDeclRefs, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4409 | llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts) { |
| 4410 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
| 4411 | if (*CI) { |
| 4412 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) { |
| 4413 | InnerContexts.insert(cast<DeclContext>(CBE->getBlockDecl())); |
| 4414 | GetInnerBlockDeclRefExprs(CBE->getBody(), |
| 4415 | InnerBlockDeclRefs, |
| 4416 | InnerContexts); |
| 4417 | } |
| 4418 | else |
| 4419 | GetInnerBlockDeclRefExprs(*CI, |
| 4420 | InnerBlockDeclRefs, |
| 4421 | InnerContexts); |
| 4422 | |
| 4423 | } |
| 4424 | // Handle specific things. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4425 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 4426 | if (DRE->refersToEnclosingLocal()) { |
| 4427 | if (!isa<FunctionDecl>(DRE->getDecl()) && |
| 4428 | !InnerContexts.count(DRE->getDecl()->getDeclContext())) |
| 4429 | InnerBlockDeclRefs.push_back(DRE); |
| 4430 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) |
| 4431 | if (Var->isFunctionOrMethodVarDecl()) |
| 4432 | ImportedLocalExternalDecls.insert(Var); |
| 4433 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4434 | } |
| 4435 | |
| 4436 | return; |
| 4437 | } |
| 4438 | |
Fariborz Jahanian | 164d6f8 | 2012-02-13 18:57:49 +0000 | [diff] [blame] | 4439 | /// convertObjCTypeToCStyleType - This routine converts such objc types |
| 4440 | /// as qualified objects, and blocks to their closest c/c++ types that |
| 4441 | /// it can. It returns true if input type was modified. |
| 4442 | bool RewriteModernObjC::convertObjCTypeToCStyleType(QualType &T) { |
| 4443 | QualType oldT = T; |
| 4444 | convertBlockPointerToFunctionPointer(T); |
| 4445 | if (T->isFunctionPointerType()) { |
| 4446 | QualType PointeeTy; |
| 4447 | if (const PointerType* PT = T->getAs<PointerType>()) { |
| 4448 | PointeeTy = PT->getPointeeType(); |
| 4449 | if (const FunctionType *FT = PointeeTy->getAs<FunctionType>()) { |
| 4450 | T = convertFunctionTypeOfBlocks(FT); |
| 4451 | T = Context->getPointerType(T); |
| 4452 | } |
| 4453 | } |
| 4454 | } |
| 4455 | |
| 4456 | convertToUnqualifiedObjCType(T); |
| 4457 | return T != oldT; |
| 4458 | } |
| 4459 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4460 | /// convertFunctionTypeOfBlocks - This routine converts a function type |
| 4461 | /// whose result type may be a block pointer or whose argument type(s) |
| 4462 | /// might be block pointers to an equivalent function type replacing |
| 4463 | /// all block pointers to function pointers. |
| 4464 | QualType RewriteModernObjC::convertFunctionTypeOfBlocks(const FunctionType *FT) { |
| 4465 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
| 4466 | // FTP will be null for closures that don't take arguments. |
| 4467 | // Generate a funky cast. |
| 4468 | SmallVector<QualType, 8> ArgTypes; |
| 4469 | QualType Res = FT->getResultType(); |
Fariborz Jahanian | 164d6f8 | 2012-02-13 18:57:49 +0000 | [diff] [blame] | 4470 | bool modified = convertObjCTypeToCStyleType(Res); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4471 | |
| 4472 | if (FTP) { |
| 4473 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
| 4474 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 4475 | QualType t = *I; |
| 4476 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 164d6f8 | 2012-02-13 18:57:49 +0000 | [diff] [blame] | 4477 | if (convertObjCTypeToCStyleType(t)) |
| 4478 | modified = true; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4479 | ArgTypes.push_back(t); |
| 4480 | } |
| 4481 | } |
| 4482 | QualType FuncType; |
Fariborz Jahanian | 164d6f8 | 2012-02-13 18:57:49 +0000 | [diff] [blame] | 4483 | if (modified) |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4484 | FuncType = getSimpleFunctionType(Res, &ArgTypes[0], ArgTypes.size()); |
| 4485 | else FuncType = QualType(FT, 0); |
| 4486 | return FuncType; |
| 4487 | } |
| 4488 | |
| 4489 | Stmt *RewriteModernObjC::SynthesizeBlockCall(CallExpr *Exp, const Expr *BlockExp) { |
| 4490 | // Navigate to relevant type information. |
| 4491 | const BlockPointerType *CPT = 0; |
| 4492 | |
| 4493 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BlockExp)) { |
| 4494 | CPT = DRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4495 | } else if (const MemberExpr *MExpr = dyn_cast<MemberExpr>(BlockExp)) { |
| 4496 | CPT = MExpr->getType()->getAs<BlockPointerType>(); |
| 4497 | } |
| 4498 | else if (const ParenExpr *PRE = dyn_cast<ParenExpr>(BlockExp)) { |
| 4499 | return SynthesizeBlockCall(Exp, PRE->getSubExpr()); |
| 4500 | } |
| 4501 | else if (const ImplicitCastExpr *IEXPR = dyn_cast<ImplicitCastExpr>(BlockExp)) |
| 4502 | CPT = IEXPR->getType()->getAs<BlockPointerType>(); |
| 4503 | else if (const ConditionalOperator *CEXPR = |
| 4504 | dyn_cast<ConditionalOperator>(BlockExp)) { |
| 4505 | Expr *LHSExp = CEXPR->getLHS(); |
| 4506 | Stmt *LHSStmt = SynthesizeBlockCall(Exp, LHSExp); |
| 4507 | Expr *RHSExp = CEXPR->getRHS(); |
| 4508 | Stmt *RHSStmt = SynthesizeBlockCall(Exp, RHSExp); |
| 4509 | Expr *CONDExp = CEXPR->getCond(); |
| 4510 | ConditionalOperator *CondExpr = |
| 4511 | new (Context) ConditionalOperator(CONDExp, |
| 4512 | SourceLocation(), cast<Expr>(LHSStmt), |
| 4513 | SourceLocation(), cast<Expr>(RHSStmt), |
| 4514 | Exp->getType(), VK_RValue, OK_Ordinary); |
| 4515 | return CondExpr; |
| 4516 | } else if (const ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(BlockExp)) { |
| 4517 | CPT = IRE->getType()->getAs<BlockPointerType>(); |
| 4518 | } else if (const PseudoObjectExpr *POE |
| 4519 | = dyn_cast<PseudoObjectExpr>(BlockExp)) { |
| 4520 | CPT = POE->getType()->castAs<BlockPointerType>(); |
| 4521 | } else { |
| 4522 | assert(1 && "RewriteBlockClass: Bad type"); |
| 4523 | } |
| 4524 | assert(CPT && "RewriteBlockClass: Bad type"); |
| 4525 | const FunctionType *FT = CPT->getPointeeType()->getAs<FunctionType>(); |
| 4526 | assert(FT && "RewriteBlockClass: Bad type"); |
| 4527 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
| 4528 | // FTP will be null for closures that don't take arguments. |
| 4529 | |
| 4530 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 4531 | SourceLocation(), SourceLocation(), |
| 4532 | &Context->Idents.get("__block_impl")); |
| 4533 | QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD)); |
| 4534 | |
| 4535 | // Generate a funky cast. |
| 4536 | SmallVector<QualType, 8> ArgTypes; |
| 4537 | |
| 4538 | // Push the block argument type. |
| 4539 | ArgTypes.push_back(PtrBlock); |
| 4540 | if (FTP) { |
| 4541 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
| 4542 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 4543 | QualType t = *I; |
| 4544 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
| 4545 | if (!convertBlockPointerToFunctionPointer(t)) |
| 4546 | convertToUnqualifiedObjCType(t); |
| 4547 | ArgTypes.push_back(t); |
| 4548 | } |
| 4549 | } |
| 4550 | // Now do the pointer to function cast. |
| 4551 | QualType PtrToFuncCastType |
| 4552 | = getSimpleFunctionType(Exp->getType(), &ArgTypes[0], ArgTypes.size()); |
| 4553 | |
| 4554 | PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); |
| 4555 | |
| 4556 | CastExpr *BlkCast = NoTypeInfoCStyleCastExpr(Context, PtrBlock, |
| 4557 | CK_BitCast, |
| 4558 | const_cast<Expr*>(BlockExp)); |
| 4559 | // Don't forget the parens to enforce the proper binding. |
| 4560 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 4561 | BlkCast); |
| 4562 | //PE->dump(); |
| 4563 | |
| 4564 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 4565 | SourceLocation(), |
| 4566 | &Context->Idents.get("FuncPtr"), |
| 4567 | Context->VoidPtrTy, 0, |
| 4568 | /*BitWidth=*/0, /*Mutable=*/true, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4569 | ICIS_NoInit); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4570 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
| 4571 | FD->getType(), VK_LValue, |
| 4572 | OK_Ordinary); |
| 4573 | |
| 4574 | |
| 4575 | CastExpr *FunkCast = NoTypeInfoCStyleCastExpr(Context, PtrToFuncCastType, |
| 4576 | CK_BitCast, ME); |
| 4577 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast); |
| 4578 | |
| 4579 | SmallVector<Expr*, 8> BlkExprs; |
| 4580 | // Add the implicit argument. |
| 4581 | BlkExprs.push_back(BlkCast); |
| 4582 | // Add the user arguments. |
| 4583 | for (CallExpr::arg_iterator I = Exp->arg_begin(), |
| 4584 | E = Exp->arg_end(); I != E; ++I) { |
| 4585 | BlkExprs.push_back(*I); |
| 4586 | } |
| 4587 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &BlkExprs[0], |
| 4588 | BlkExprs.size(), |
| 4589 | Exp->getType(), VK_RValue, |
| 4590 | SourceLocation()); |
| 4591 | return CE; |
| 4592 | } |
| 4593 | |
| 4594 | // We need to return the rewritten expression to handle cases where the |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4595 | // DeclRefExpr is embedded in another expression being rewritten. |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4596 | // For example: |
| 4597 | // |
| 4598 | // int main() { |
| 4599 | // __block Foo *f; |
| 4600 | // __block int i; |
| 4601 | // |
| 4602 | // void (^myblock)() = ^() { |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4603 | // [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] | 4604 | // i = 77; |
| 4605 | // }; |
| 4606 | //} |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4607 | Stmt *RewriteModernObjC::RewriteBlockDeclRefExpr(DeclRefExpr *DeclRefExp) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4608 | // Rewrite the byref variable into BYREFVAR->__forwarding->BYREFVAR |
| 4609 | // for each DeclRefExp where BYREFVAR is name of the variable. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4610 | ValueDecl *VD = DeclRefExp->getDecl(); |
| 4611 | bool isArrow = DeclRefExp->refersToEnclosingLocal(); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4612 | |
| 4613 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 4614 | SourceLocation(), |
| 4615 | &Context->Idents.get("__forwarding"), |
| 4616 | Context->VoidPtrTy, 0, |
| 4617 | /*BitWidth=*/0, /*Mutable=*/true, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4618 | ICIS_NoInit); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4619 | MemberExpr *ME = new (Context) MemberExpr(DeclRefExp, isArrow, |
| 4620 | FD, SourceLocation(), |
| 4621 | FD->getType(), VK_LValue, |
| 4622 | OK_Ordinary); |
| 4623 | |
| 4624 | StringRef Name = VD->getName(); |
| 4625 | FD = FieldDecl::Create(*Context, 0, SourceLocation(), SourceLocation(), |
| 4626 | &Context->Idents.get(Name), |
| 4627 | Context->VoidPtrTy, 0, |
| 4628 | /*BitWidth=*/0, /*Mutable=*/true, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4629 | ICIS_NoInit); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4630 | ME = new (Context) MemberExpr(ME, true, FD, SourceLocation(), |
| 4631 | DeclRefExp->getType(), VK_LValue, OK_Ordinary); |
| 4632 | |
| 4633 | |
| 4634 | |
| 4635 | // Need parens to enforce precedence. |
| 4636 | ParenExpr *PE = new (Context) ParenExpr(DeclRefExp->getExprLoc(), |
| 4637 | DeclRefExp->getExprLoc(), |
| 4638 | ME); |
| 4639 | ReplaceStmt(DeclRefExp, PE); |
| 4640 | return PE; |
| 4641 | } |
| 4642 | |
| 4643 | // Rewrites the imported local variable V with external storage |
| 4644 | // (static, extern, etc.) as *V |
| 4645 | // |
| 4646 | Stmt *RewriteModernObjC::RewriteLocalVariableExternalStorage(DeclRefExpr *DRE) { |
| 4647 | ValueDecl *VD = DRE->getDecl(); |
| 4648 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 4649 | if (!ImportedLocalExternalDecls.count(Var)) |
| 4650 | return DRE; |
| 4651 | Expr *Exp = new (Context) UnaryOperator(DRE, UO_Deref, DRE->getType(), |
| 4652 | VK_LValue, OK_Ordinary, |
| 4653 | DRE->getLocation()); |
| 4654 | // Need parens to enforce precedence. |
| 4655 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 4656 | Exp); |
| 4657 | ReplaceStmt(DRE, PE); |
| 4658 | return PE; |
| 4659 | } |
| 4660 | |
| 4661 | void RewriteModernObjC::RewriteCastExpr(CStyleCastExpr *CE) { |
| 4662 | SourceLocation LocStart = CE->getLParenLoc(); |
| 4663 | SourceLocation LocEnd = CE->getRParenLoc(); |
| 4664 | |
| 4665 | // Need to avoid trying to rewrite synthesized casts. |
| 4666 | if (LocStart.isInvalid()) |
| 4667 | return; |
| 4668 | // Need to avoid trying to rewrite casts contained in macros. |
| 4669 | if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) |
| 4670 | return; |
| 4671 | |
| 4672 | const char *startBuf = SM->getCharacterData(LocStart); |
| 4673 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 4674 | QualType QT = CE->getType(); |
| 4675 | const Type* TypePtr = QT->getAs<Type>(); |
| 4676 | if (isa<TypeOfExprType>(TypePtr)) { |
| 4677 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 4678 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 4679 | std::string TypeAsString = "("; |
| 4680 | RewriteBlockPointerType(TypeAsString, QT); |
| 4681 | TypeAsString += ")"; |
| 4682 | ReplaceText(LocStart, endBuf-startBuf+1, TypeAsString); |
| 4683 | return; |
| 4684 | } |
| 4685 | // advance the location to startArgList. |
| 4686 | const char *argPtr = startBuf; |
| 4687 | |
| 4688 | while (*argPtr++ && (argPtr < endBuf)) { |
| 4689 | switch (*argPtr) { |
| 4690 | case '^': |
| 4691 | // Replace the '^' with '*'. |
| 4692 | LocStart = LocStart.getLocWithOffset(argPtr-startBuf); |
| 4693 | ReplaceText(LocStart, 1, "*"); |
| 4694 | break; |
| 4695 | } |
| 4696 | } |
| 4697 | return; |
| 4698 | } |
| 4699 | |
Fariborz Jahanian | f1ee687 | 2012-04-10 00:08:18 +0000 | [diff] [blame] | 4700 | void RewriteModernObjC::RewriteImplicitCastObjCExpr(CastExpr *IC) { |
| 4701 | CastKind CastKind = IC->getCastKind(); |
Fariborz Jahanian | 43aa1c3 | 2012-04-16 22:14:01 +0000 | [diff] [blame] | 4702 | if (CastKind != CK_BlockPointerToObjCPointerCast && |
| 4703 | CastKind != CK_AnyPointerToBlockPointerCast) |
| 4704 | return; |
Fariborz Jahanian | f1ee687 | 2012-04-10 00:08:18 +0000 | [diff] [blame] | 4705 | |
Fariborz Jahanian | 43aa1c3 | 2012-04-16 22:14:01 +0000 | [diff] [blame] | 4706 | QualType QT = IC->getType(); |
| 4707 | (void)convertBlockPointerToFunctionPointer(QT); |
| 4708 | std::string TypeString(QT.getAsString(Context->getPrintingPolicy())); |
| 4709 | std::string Str = "("; |
| 4710 | Str += TypeString; |
| 4711 | Str += ")"; |
| 4712 | InsertText(IC->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 4713 | |
Fariborz Jahanian | f1ee687 | 2012-04-10 00:08:18 +0000 | [diff] [blame] | 4714 | return; |
| 4715 | } |
| 4716 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4717 | void RewriteModernObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { |
| 4718 | SourceLocation DeclLoc = FD->getLocation(); |
| 4719 | unsigned parenCount = 0; |
| 4720 | |
| 4721 | // We have 1 or more arguments that have closure pointers. |
| 4722 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4723 | const char *startArgList = strchr(startBuf, '('); |
| 4724 | |
| 4725 | assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); |
| 4726 | |
| 4727 | parenCount++; |
| 4728 | // advance the location to startArgList. |
| 4729 | DeclLoc = DeclLoc.getLocWithOffset(startArgList-startBuf); |
| 4730 | assert((DeclLoc.isValid()) && "Invalid DeclLoc"); |
| 4731 | |
| 4732 | const char *argPtr = startArgList; |
| 4733 | |
| 4734 | while (*argPtr++ && parenCount) { |
| 4735 | switch (*argPtr) { |
| 4736 | case '^': |
| 4737 | // Replace the '^' with '*'. |
| 4738 | DeclLoc = DeclLoc.getLocWithOffset(argPtr-startArgList); |
| 4739 | ReplaceText(DeclLoc, 1, "*"); |
| 4740 | break; |
| 4741 | case '(': |
| 4742 | parenCount++; |
| 4743 | break; |
| 4744 | case ')': |
| 4745 | parenCount--; |
| 4746 | break; |
| 4747 | } |
| 4748 | } |
| 4749 | return; |
| 4750 | } |
| 4751 | |
| 4752 | bool RewriteModernObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { |
| 4753 | const FunctionProtoType *FTP; |
| 4754 | const PointerType *PT = QT->getAs<PointerType>(); |
| 4755 | if (PT) { |
| 4756 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4757 | } else { |
| 4758 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
| 4759 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
| 4760 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4761 | } |
| 4762 | if (FTP) { |
| 4763 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
| 4764 | E = FTP->arg_type_end(); I != E; ++I) |
| 4765 | if (isTopLevelBlockPointerType(*I)) |
| 4766 | return true; |
| 4767 | } |
| 4768 | return false; |
| 4769 | } |
| 4770 | |
| 4771 | bool RewriteModernObjC::PointerTypeTakesAnyObjCQualifiedType(QualType QT) { |
| 4772 | const FunctionProtoType *FTP; |
| 4773 | const PointerType *PT = QT->getAs<PointerType>(); |
| 4774 | if (PT) { |
| 4775 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4776 | } else { |
| 4777 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
| 4778 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
| 4779 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4780 | } |
| 4781 | if (FTP) { |
| 4782 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
| 4783 | E = FTP->arg_type_end(); I != E; ++I) { |
| 4784 | if ((*I)->isObjCQualifiedIdType()) |
| 4785 | return true; |
| 4786 | if ((*I)->isObjCObjectPointerType() && |
| 4787 | (*I)->getPointeeType()->isObjCQualifiedInterfaceType()) |
| 4788 | return true; |
| 4789 | } |
| 4790 | |
| 4791 | } |
| 4792 | return false; |
| 4793 | } |
| 4794 | |
| 4795 | void RewriteModernObjC::GetExtentOfArgList(const char *Name, const char *&LParen, |
| 4796 | const char *&RParen) { |
| 4797 | const char *argPtr = strchr(Name, '('); |
| 4798 | assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); |
| 4799 | |
| 4800 | LParen = argPtr; // output the start. |
| 4801 | argPtr++; // skip past the left paren. |
| 4802 | unsigned parenCount = 1; |
| 4803 | |
| 4804 | while (*argPtr && parenCount) { |
| 4805 | switch (*argPtr) { |
| 4806 | case '(': parenCount++; break; |
| 4807 | case ')': parenCount--; break; |
| 4808 | default: break; |
| 4809 | } |
| 4810 | if (parenCount) argPtr++; |
| 4811 | } |
| 4812 | assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); |
| 4813 | RParen = argPtr; // output the end |
| 4814 | } |
| 4815 | |
| 4816 | void RewriteModernObjC::RewriteBlockPointerDecl(NamedDecl *ND) { |
| 4817 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 4818 | RewriteBlockPointerFunctionArgs(FD); |
| 4819 | return; |
| 4820 | } |
| 4821 | // Handle Variables and Typedefs. |
| 4822 | SourceLocation DeclLoc = ND->getLocation(); |
| 4823 | QualType DeclT; |
| 4824 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4825 | DeclT = VD->getType(); |
| 4826 | else if (TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(ND)) |
| 4827 | DeclT = TDD->getUnderlyingType(); |
| 4828 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) |
| 4829 | DeclT = FD->getType(); |
| 4830 | else |
| 4831 | llvm_unreachable("RewriteBlockPointerDecl(): Decl type not yet handled"); |
| 4832 | |
| 4833 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4834 | const char *endBuf = startBuf; |
| 4835 | // scan backward (from the decl location) for the end of the previous decl. |
| 4836 | while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) |
| 4837 | startBuf--; |
| 4838 | SourceLocation Start = DeclLoc.getLocWithOffset(startBuf-endBuf); |
| 4839 | std::string buf; |
| 4840 | unsigned OrigLength=0; |
| 4841 | // *startBuf != '^' if we are dealing with a pointer to function that |
| 4842 | // may take block argument types (which will be handled below). |
| 4843 | if (*startBuf == '^') { |
| 4844 | // Replace the '^' with '*', computing a negative offset. |
| 4845 | buf = '*'; |
| 4846 | startBuf++; |
| 4847 | OrigLength++; |
| 4848 | } |
| 4849 | while (*startBuf != ')') { |
| 4850 | buf += *startBuf; |
| 4851 | startBuf++; |
| 4852 | OrigLength++; |
| 4853 | } |
| 4854 | buf += ')'; |
| 4855 | OrigLength++; |
| 4856 | |
| 4857 | if (PointerTypeTakesAnyBlockArguments(DeclT) || |
| 4858 | PointerTypeTakesAnyObjCQualifiedType(DeclT)) { |
| 4859 | // Replace the '^' with '*' for arguments. |
| 4860 | // Replace id<P> with id/*<>*/ |
| 4861 | DeclLoc = ND->getLocation(); |
| 4862 | startBuf = SM->getCharacterData(DeclLoc); |
| 4863 | const char *argListBegin, *argListEnd; |
| 4864 | GetExtentOfArgList(startBuf, argListBegin, argListEnd); |
| 4865 | while (argListBegin < argListEnd) { |
| 4866 | if (*argListBegin == '^') |
| 4867 | buf += '*'; |
| 4868 | else if (*argListBegin == '<') { |
| 4869 | buf += "/*"; |
| 4870 | buf += *argListBegin++; |
| 4871 | OrigLength++;; |
| 4872 | while (*argListBegin != '>') { |
| 4873 | buf += *argListBegin++; |
| 4874 | OrigLength++; |
| 4875 | } |
| 4876 | buf += *argListBegin; |
| 4877 | buf += "*/"; |
| 4878 | } |
| 4879 | else |
| 4880 | buf += *argListBegin; |
| 4881 | argListBegin++; |
| 4882 | OrigLength++; |
| 4883 | } |
| 4884 | buf += ')'; |
| 4885 | OrigLength++; |
| 4886 | } |
| 4887 | ReplaceText(Start, OrigLength, buf); |
| 4888 | |
| 4889 | return; |
| 4890 | } |
| 4891 | |
| 4892 | |
| 4893 | /// SynthesizeByrefCopyDestroyHelper - This routine synthesizes: |
| 4894 | /// void __Block_byref_id_object_copy(struct Block_byref_id_object *dst, |
| 4895 | /// struct Block_byref_id_object *src) { |
| 4896 | /// _Block_object_assign (&_dest->object, _src->object, |
| 4897 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4898 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4899 | /// _Block_object_assign(&_dest->object, _src->object, |
| 4900 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4901 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4902 | /// } |
| 4903 | /// And: |
| 4904 | /// void __Block_byref_id_object_dispose(struct Block_byref_id_object *_src) { |
| 4905 | /// _Block_object_dispose(_src->object, |
| 4906 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4907 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4908 | /// _Block_object_dispose(_src->object, |
| 4909 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4910 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4911 | /// } |
| 4912 | |
| 4913 | std::string RewriteModernObjC::SynthesizeByrefCopyDestroyHelper(VarDecl *VD, |
| 4914 | int flag) { |
| 4915 | std::string S; |
| 4916 | if (CopyDestroyCache.count(flag)) |
| 4917 | return S; |
| 4918 | CopyDestroyCache.insert(flag); |
| 4919 | S = "static void __Block_byref_id_object_copy_"; |
| 4920 | S += utostr(flag); |
| 4921 | S += "(void *dst, void *src) {\n"; |
| 4922 | |
| 4923 | // offset into the object pointer is computed as: |
| 4924 | // void * + void* + int + int + void* + void * |
| 4925 | unsigned IntSize = |
| 4926 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 4927 | unsigned VoidPtrSize = |
| 4928 | static_cast<unsigned>(Context->getTypeSize(Context->VoidPtrTy)); |
| 4929 | |
| 4930 | unsigned offset = (VoidPtrSize*4 + IntSize + IntSize)/Context->getCharWidth(); |
| 4931 | S += " _Block_object_assign((char*)dst + "; |
| 4932 | S += utostr(offset); |
| 4933 | S += ", *(void * *) ((char*)src + "; |
| 4934 | S += utostr(offset); |
| 4935 | S += "), "; |
| 4936 | S += utostr(flag); |
| 4937 | S += ");\n}\n"; |
| 4938 | |
| 4939 | S += "static void __Block_byref_id_object_dispose_"; |
| 4940 | S += utostr(flag); |
| 4941 | S += "(void *src) {\n"; |
| 4942 | S += " _Block_object_dispose(*(void * *) ((char*)src + "; |
| 4943 | S += utostr(offset); |
| 4944 | S += "), "; |
| 4945 | S += utostr(flag); |
| 4946 | S += ");\n}\n"; |
| 4947 | return S; |
| 4948 | } |
| 4949 | |
| 4950 | /// RewriteByRefVar - For each __block typex ND variable this routine transforms |
| 4951 | /// the declaration into: |
| 4952 | /// struct __Block_byref_ND { |
| 4953 | /// void *__isa; // NULL for everything except __weak pointers |
| 4954 | /// struct __Block_byref_ND *__forwarding; |
| 4955 | /// int32_t __flags; |
| 4956 | /// int32_t __size; |
| 4957 | /// void *__Block_byref_id_object_copy; // If variable is __block ObjC object |
| 4958 | /// void *__Block_byref_id_object_dispose; // If variable is __block ObjC object |
| 4959 | /// typex ND; |
| 4960 | /// }; |
| 4961 | /// |
| 4962 | /// It then replaces declaration of ND variable with: |
| 4963 | /// struct __Block_byref_ND ND = {__isa=0B, __forwarding=&ND, __flags=some_flag, |
| 4964 | /// __size=sizeof(struct __Block_byref_ND), |
| 4965 | /// ND=initializer-if-any}; |
| 4966 | /// |
| 4967 | /// |
Fariborz Jahanian | 4fe261c | 2012-04-24 19:38:45 +0000 | [diff] [blame] | 4968 | void RewriteModernObjC::RewriteByRefVar(VarDecl *ND, bool firstDecl, |
| 4969 | bool lastDecl) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4970 | int flag = 0; |
| 4971 | int isa = 0; |
| 4972 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
| 4973 | if (DeclLoc.isInvalid()) |
| 4974 | // If type location is missing, it is because of missing type (a warning). |
| 4975 | // Use variable's location which is good for this case. |
| 4976 | DeclLoc = ND->getLocation(); |
| 4977 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4978 | SourceLocation X = ND->getLocEnd(); |
| 4979 | X = SM->getExpansionLoc(X); |
| 4980 | const char *endBuf = SM->getCharacterData(X); |
| 4981 | std::string Name(ND->getNameAsString()); |
| 4982 | std::string ByrefType; |
| 4983 | RewriteByRefString(ByrefType, Name, ND, true); |
| 4984 | ByrefType += " {\n"; |
| 4985 | ByrefType += " void *__isa;\n"; |
| 4986 | RewriteByRefString(ByrefType, Name, ND); |
| 4987 | ByrefType += " *__forwarding;\n"; |
| 4988 | ByrefType += " int __flags;\n"; |
| 4989 | ByrefType += " int __size;\n"; |
| 4990 | // Add void *__Block_byref_id_object_copy; |
| 4991 | // void *__Block_byref_id_object_dispose; if needed. |
| 4992 | QualType Ty = ND->getType(); |
| 4993 | bool HasCopyAndDispose = Context->BlockRequiresCopying(Ty); |
| 4994 | if (HasCopyAndDispose) { |
| 4995 | ByrefType += " void (*__Block_byref_id_object_copy)(void*, void*);\n"; |
| 4996 | ByrefType += " void (*__Block_byref_id_object_dispose)(void*);\n"; |
| 4997 | } |
| 4998 | |
| 4999 | QualType T = Ty; |
| 5000 | (void)convertBlockPointerToFunctionPointer(T); |
| 5001 | T.getAsStringInternal(Name, Context->getPrintingPolicy()); |
| 5002 | |
| 5003 | ByrefType += " " + Name + ";\n"; |
| 5004 | ByrefType += "};\n"; |
| 5005 | // Insert this type in global scope. It is needed by helper function. |
| 5006 | SourceLocation FunLocStart; |
| 5007 | if (CurFunctionDef) |
Fariborz Jahanian | b75f8de | 2012-04-19 00:50:01 +0000 | [diff] [blame] | 5008 | FunLocStart = getFunctionSourceLocation(*this, CurFunctionDef); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5009 | else { |
| 5010 | assert(CurMethodDef && "RewriteByRefVar - CurMethodDef is null"); |
| 5011 | FunLocStart = CurMethodDef->getLocStart(); |
| 5012 | } |
| 5013 | InsertText(FunLocStart, ByrefType); |
Fariborz Jahanian | 8247c4e | 2012-04-24 16:45:27 +0000 | [diff] [blame] | 5014 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5015 | if (Ty.isObjCGCWeak()) { |
| 5016 | flag |= BLOCK_FIELD_IS_WEAK; |
| 5017 | isa = 1; |
| 5018 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5019 | if (HasCopyAndDispose) { |
| 5020 | flag = BLOCK_BYREF_CALLER; |
| 5021 | QualType Ty = ND->getType(); |
| 5022 | // FIXME. Handle __weak variable (BLOCK_FIELD_IS_WEAK) as well. |
| 5023 | if (Ty->isBlockPointerType()) |
| 5024 | flag |= BLOCK_FIELD_IS_BLOCK; |
| 5025 | else |
| 5026 | flag |= BLOCK_FIELD_IS_OBJECT; |
| 5027 | std::string HF = SynthesizeByrefCopyDestroyHelper(ND, flag); |
| 5028 | if (!HF.empty()) |
| 5029 | InsertText(FunLocStart, HF); |
| 5030 | } |
| 5031 | |
| 5032 | // struct __Block_byref_ND ND = |
| 5033 | // {0, &ND, some_flag, __size=sizeof(struct __Block_byref_ND), |
| 5034 | // initializer-if-any}; |
| 5035 | bool hasInit = (ND->getInit() != 0); |
Fariborz Jahanian | 104dbf9 | 2012-04-11 23:57:12 +0000 | [diff] [blame] | 5036 | // FIXME. rewriter does not support __block c++ objects which |
| 5037 | // require construction. |
Fariborz Jahanian | 65a7c68 | 2012-04-26 23:20:25 +0000 | [diff] [blame] | 5038 | if (hasInit) |
| 5039 | if (CXXConstructExpr *CExp = dyn_cast<CXXConstructExpr>(ND->getInit())) { |
| 5040 | CXXConstructorDecl *CXXDecl = CExp->getConstructor(); |
| 5041 | if (CXXDecl && CXXDecl->isDefaultConstructor()) |
| 5042 | hasInit = false; |
| 5043 | } |
| 5044 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5045 | unsigned flags = 0; |
| 5046 | if (HasCopyAndDispose) |
| 5047 | flags |= BLOCK_HAS_COPY_DISPOSE; |
| 5048 | Name = ND->getNameAsString(); |
| 5049 | ByrefType.clear(); |
| 5050 | RewriteByRefString(ByrefType, Name, ND); |
| 5051 | std::string ForwardingCastType("("); |
| 5052 | ForwardingCastType += ByrefType + " *)"; |
Fariborz Jahanian | 8247c4e | 2012-04-24 16:45:27 +0000 | [diff] [blame] | 5053 | ByrefType += " " + Name + " = {(void*)"; |
| 5054 | ByrefType += utostr(isa); |
| 5055 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
| 5056 | ByrefType += utostr(flags); |
| 5057 | ByrefType += ", "; |
| 5058 | ByrefType += "sizeof("; |
| 5059 | RewriteByRefString(ByrefType, Name, ND); |
| 5060 | ByrefType += ")"; |
| 5061 | if (HasCopyAndDispose) { |
| 5062 | ByrefType += ", __Block_byref_id_object_copy_"; |
| 5063 | ByrefType += utostr(flag); |
| 5064 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 5065 | ByrefType += utostr(flag); |
| 5066 | } |
| 5067 | |
Fariborz Jahanian | 4fe261c | 2012-04-24 19:38:45 +0000 | [diff] [blame] | 5068 | if (!firstDecl) { |
| 5069 | // In multiple __block declarations, and for all but 1st declaration, |
| 5070 | // find location of the separating comma. This would be start location |
| 5071 | // where new text is to be inserted. |
| 5072 | DeclLoc = ND->getLocation(); |
| 5073 | const char *startDeclBuf = SM->getCharacterData(DeclLoc); |
| 5074 | const char *commaBuf = startDeclBuf; |
| 5075 | while (*commaBuf != ',') |
| 5076 | commaBuf--; |
| 5077 | assert((*commaBuf == ',') && "RewriteByRefVar: can't find ','"); |
| 5078 | DeclLoc = DeclLoc.getLocWithOffset(commaBuf - startDeclBuf); |
| 5079 | startBuf = commaBuf; |
| 5080 | } |
| 5081 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5082 | if (!hasInit) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5083 | ByrefType += "};\n"; |
| 5084 | unsigned nameSize = Name.size(); |
| 5085 | // for block or function pointer declaration. Name is aleady |
| 5086 | // part of the declaration. |
| 5087 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) |
| 5088 | nameSize = 1; |
| 5089 | ReplaceText(DeclLoc, endBuf-startBuf+nameSize, ByrefType); |
| 5090 | } |
| 5091 | else { |
Fariborz Jahanian | 8247c4e | 2012-04-24 16:45:27 +0000 | [diff] [blame] | 5092 | ByrefType += ", "; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5093 | SourceLocation startLoc; |
| 5094 | Expr *E = ND->getInit(); |
| 5095 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 5096 | startLoc = ECE->getLParenLoc(); |
| 5097 | else |
| 5098 | startLoc = E->getLocStart(); |
| 5099 | startLoc = SM->getExpansionLoc(startLoc); |
| 5100 | endBuf = SM->getCharacterData(startLoc); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5101 | ReplaceText(DeclLoc, endBuf-startBuf, ByrefType); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5102 | |
Fariborz Jahanian | 4fe261c | 2012-04-24 19:38:45 +0000 | [diff] [blame] | 5103 | const char separator = lastDecl ? ';' : ','; |
| 5104 | const char *startInitializerBuf = SM->getCharacterData(startLoc); |
| 5105 | const char *separatorBuf = strchr(startInitializerBuf, separator); |
| 5106 | assert((*separatorBuf == separator) && |
| 5107 | "RewriteByRefVar: can't find ';' or ','"); |
| 5108 | SourceLocation separatorLoc = |
| 5109 | startLoc.getLocWithOffset(separatorBuf-startInitializerBuf); |
| 5110 | |
| 5111 | InsertText(separatorLoc, lastDecl ? "}" : "};\n"); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5112 | } |
| 5113 | return; |
| 5114 | } |
| 5115 | |
| 5116 | void RewriteModernObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { |
| 5117 | // Add initializers for any closure decl refs. |
| 5118 | GetBlockDeclRefExprs(Exp->getBody()); |
| 5119 | if (BlockDeclRefs.size()) { |
| 5120 | // Unique all "by copy" declarations. |
| 5121 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5122 | if (!BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>()) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5123 | if (!BlockByCopyDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 5124 | BlockByCopyDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 5125 | BlockByCopyDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 5126 | } |
| 5127 | } |
| 5128 | // Unique all "by ref" declarations. |
| 5129 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5130 | if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>()) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5131 | if (!BlockByRefDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 5132 | BlockByRefDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 5133 | BlockByRefDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 5134 | } |
| 5135 | } |
| 5136 | // Find any imported blocks...they will need special attention. |
| 5137 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5138 | if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>() || |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5139 | BlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
| 5140 | BlockDeclRefs[i]->getType()->isBlockPointerType()) |
| 5141 | ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 5142 | } |
| 5143 | } |
| 5144 | |
| 5145 | FunctionDecl *RewriteModernObjC::SynthBlockInitFunctionDecl(StringRef name) { |
| 5146 | IdentifierInfo *ID = &Context->Idents.get(name); |
| 5147 | QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy); |
| 5148 | return FunctionDecl::Create(*Context, TUDecl, SourceLocation(), |
| 5149 | SourceLocation(), ID, FType, 0, SC_Extern, |
| 5150 | SC_None, false, false); |
| 5151 | } |
| 5152 | |
| 5153 | Stmt *RewriteModernObjC::SynthBlockInitExpr(BlockExpr *Exp, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5154 | const SmallVector<DeclRefExpr *, 8> &InnerBlockDeclRefs) { |
Fariborz Jahanian | d13c2c2 | 2012-03-22 19:54:39 +0000 | [diff] [blame] | 5155 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5156 | const BlockDecl *block = Exp->getBlockDecl(); |
Fariborz Jahanian | d13c2c2 | 2012-03-22 19:54:39 +0000 | [diff] [blame] | 5157 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5158 | Blocks.push_back(Exp); |
| 5159 | |
| 5160 | CollectBlockDeclRefInfo(Exp); |
| 5161 | |
| 5162 | // Add inner imported variables now used in current block. |
| 5163 | int countOfInnerDecls = 0; |
| 5164 | if (!InnerBlockDeclRefs.empty()) { |
| 5165 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) { |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5166 | DeclRefExpr *Exp = InnerBlockDeclRefs[i]; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5167 | ValueDecl *VD = Exp->getDecl(); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5168 | if (!VD->hasAttr<BlocksAttr>() && !BlockByCopyDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5169 | // We need to save the copied-in variables in nested |
| 5170 | // blocks because it is needed at the end for some of the API generations. |
| 5171 | // See SynthesizeBlockLiterals routine. |
| 5172 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 5173 | BlockDeclRefs.push_back(Exp); |
| 5174 | BlockByCopyDeclsPtrSet.insert(VD); |
| 5175 | BlockByCopyDecls.push_back(VD); |
| 5176 | } |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5177 | if (VD->hasAttr<BlocksAttr>() && !BlockByRefDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5178 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 5179 | BlockDeclRefs.push_back(Exp); |
| 5180 | BlockByRefDeclsPtrSet.insert(VD); |
| 5181 | BlockByRefDecls.push_back(VD); |
| 5182 | } |
| 5183 | } |
| 5184 | // Find any imported blocks...they will need special attention. |
| 5185 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5186 | if (InnerBlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>() || |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5187 | InnerBlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
| 5188 | InnerBlockDeclRefs[i]->getType()->isBlockPointerType()) |
| 5189 | ImportedBlockDecls.insert(InnerBlockDeclRefs[i]->getDecl()); |
| 5190 | } |
| 5191 | InnerDeclRefsCount.push_back(countOfInnerDecls); |
| 5192 | |
| 5193 | std::string FuncName; |
| 5194 | |
| 5195 | if (CurFunctionDef) |
| 5196 | FuncName = CurFunctionDef->getNameAsString(); |
| 5197 | else if (CurMethodDef) |
| 5198 | BuildUniqueMethodName(FuncName, CurMethodDef); |
| 5199 | else if (GlobalVarDecl) |
| 5200 | FuncName = std::string(GlobalVarDecl->getNameAsString()); |
| 5201 | |
Fariborz Jahanian | df474ec | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 5202 | bool GlobalBlockExpr = |
| 5203 | block->getDeclContext()->getRedeclContext()->isFileContext(); |
| 5204 | |
| 5205 | if (GlobalBlockExpr && !GlobalVarDecl) { |
| 5206 | Diags.Report(block->getLocation(), GlobalBlockRewriteFailedDiag); |
| 5207 | GlobalBlockExpr = false; |
| 5208 | } |
| 5209 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5210 | std::string BlockNumber = utostr(Blocks.size()-1); |
| 5211 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5212 | std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; |
| 5213 | |
| 5214 | // Get a pointer to the function type so we can cast appropriately. |
| 5215 | QualType BFT = convertFunctionTypeOfBlocks(Exp->getFunctionType()); |
| 5216 | QualType FType = Context->getPointerType(BFT); |
| 5217 | |
| 5218 | FunctionDecl *FD; |
| 5219 | Expr *NewRep; |
| 5220 | |
| 5221 | // Simulate a contructor call... |
Fariborz Jahanian | df474ec | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 5222 | std::string Tag; |
| 5223 | |
| 5224 | if (GlobalBlockExpr) |
| 5225 | Tag = "__global_"; |
| 5226 | else |
| 5227 | Tag = "__"; |
| 5228 | Tag += FuncName + "_block_impl_" + BlockNumber; |
| 5229 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5230 | FD = SynthBlockInitFunctionDecl(Tag); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5231 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, false, FType, VK_RValue, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5232 | SourceLocation()); |
| 5233 | |
| 5234 | SmallVector<Expr*, 4> InitExprs; |
| 5235 | |
| 5236 | // Initialize the block function. |
| 5237 | FD = SynthBlockInitFunctionDecl(Func); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5238 | DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, false, FD->getType(), |
| 5239 | VK_LValue, SourceLocation()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5240 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
| 5241 | CK_BitCast, Arg); |
| 5242 | InitExprs.push_back(castExpr); |
| 5243 | |
| 5244 | // Initialize the block descriptor. |
| 5245 | std::string DescData = "__" + FuncName + "_block_desc_" + BlockNumber + "_DATA"; |
| 5246 | |
| 5247 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, |
| 5248 | SourceLocation(), SourceLocation(), |
| 5249 | &Context->Idents.get(DescData.c_str()), |
| 5250 | Context->VoidPtrTy, 0, |
| 5251 | SC_Static, SC_None); |
| 5252 | UnaryOperator *DescRefExpr = |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5253 | new (Context) UnaryOperator(new (Context) DeclRefExpr(NewVD, false, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5254 | Context->VoidPtrTy, |
| 5255 | VK_LValue, |
| 5256 | SourceLocation()), |
| 5257 | UO_AddrOf, |
| 5258 | Context->getPointerType(Context->VoidPtrTy), |
| 5259 | VK_RValue, OK_Ordinary, |
| 5260 | SourceLocation()); |
| 5261 | InitExprs.push_back(DescRefExpr); |
| 5262 | |
| 5263 | // Add initializers for any closure decl refs. |
| 5264 | if (BlockDeclRefs.size()) { |
| 5265 | Expr *Exp; |
| 5266 | // Output all "by copy" declarations. |
| 5267 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 5268 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 5269 | if (isObjCType((*I)->getType())) { |
| 5270 | // FIXME: Conform to ABI ([[obj retain] autorelease]). |
| 5271 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5272 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), |
| 5273 | VK_LValue, SourceLocation()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5274 | if (HasLocalVariableExternalStorage(*I)) { |
| 5275 | QualType QT = (*I)->getType(); |
| 5276 | QT = Context->getPointerType(QT); |
| 5277 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue, |
| 5278 | OK_Ordinary, SourceLocation()); |
| 5279 | } |
| 5280 | } else if (isTopLevelBlockPointerType((*I)->getType())) { |
| 5281 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5282 | Arg = new (Context) DeclRefExpr(FD, false, FD->getType(), |
| 5283 | VK_LValue, SourceLocation()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5284 | Exp = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
| 5285 | CK_BitCast, Arg); |
| 5286 | } else { |
| 5287 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5288 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), |
| 5289 | VK_LValue, SourceLocation()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5290 | if (HasLocalVariableExternalStorage(*I)) { |
| 5291 | QualType QT = (*I)->getType(); |
| 5292 | QT = Context->getPointerType(QT); |
| 5293 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue, |
| 5294 | OK_Ordinary, SourceLocation()); |
| 5295 | } |
| 5296 | |
| 5297 | } |
| 5298 | InitExprs.push_back(Exp); |
| 5299 | } |
| 5300 | // Output all "by ref" declarations. |
| 5301 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 5302 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 5303 | ValueDecl *ND = (*I); |
| 5304 | std::string Name(ND->getNameAsString()); |
| 5305 | std::string RecName; |
| 5306 | RewriteByRefString(RecName, Name, ND, true); |
| 5307 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str() |
| 5308 | + sizeof("struct")); |
| 5309 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 5310 | SourceLocation(), SourceLocation(), |
| 5311 | II); |
| 5312 | assert(RD && "SynthBlockInitExpr(): Can't find RecordDecl"); |
| 5313 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 5314 | |
| 5315 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5316 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5317 | SourceLocation()); |
| 5318 | bool isNestedCapturedVar = false; |
| 5319 | if (block) |
| 5320 | for (BlockDecl::capture_const_iterator ci = block->capture_begin(), |
| 5321 | ce = block->capture_end(); ci != ce; ++ci) { |
| 5322 | const VarDecl *variable = ci->getVariable(); |
| 5323 | if (variable == ND && ci->isNested()) { |
| 5324 | assert (ci->isByRef() && |
| 5325 | "SynthBlockInitExpr - captured block variable is not byref"); |
| 5326 | isNestedCapturedVar = true; |
| 5327 | break; |
| 5328 | } |
| 5329 | } |
| 5330 | // captured nested byref variable has its address passed. Do not take |
| 5331 | // its address again. |
| 5332 | if (!isNestedCapturedVar) |
| 5333 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, |
| 5334 | Context->getPointerType(Exp->getType()), |
| 5335 | VK_RValue, OK_Ordinary, SourceLocation()); |
| 5336 | Exp = NoTypeInfoCStyleCastExpr(Context, castT, CK_BitCast, Exp); |
| 5337 | InitExprs.push_back(Exp); |
| 5338 | } |
| 5339 | } |
| 5340 | if (ImportedBlockDecls.size()) { |
| 5341 | // generate BLOCK_HAS_COPY_DISPOSE(have helper funcs) | BLOCK_HAS_DESCRIPTOR |
| 5342 | int flag = (BLOCK_HAS_COPY_DISPOSE | BLOCK_HAS_DESCRIPTOR); |
| 5343 | unsigned IntSize = |
| 5344 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 5345 | Expr *FlagExp = IntegerLiteral::Create(*Context, llvm::APInt(IntSize, flag), |
| 5346 | Context->IntTy, SourceLocation()); |
| 5347 | InitExprs.push_back(FlagExp); |
| 5348 | } |
| 5349 | NewRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], InitExprs.size(), |
| 5350 | FType, VK_LValue, SourceLocation()); |
Fariborz Jahanian | df474ec | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 5351 | |
| 5352 | if (GlobalBlockExpr) { |
| 5353 | assert (GlobalConstructionExp == 0 && |
| 5354 | "SynthBlockInitExpr - GlobalConstructionExp must be null"); |
| 5355 | GlobalConstructionExp = NewRep; |
| 5356 | NewRep = DRE; |
| 5357 | } |
| 5358 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5359 | NewRep = new (Context) UnaryOperator(NewRep, UO_AddrOf, |
| 5360 | Context->getPointerType(NewRep->getType()), |
| 5361 | VK_RValue, OK_Ordinary, SourceLocation()); |
| 5362 | NewRep = NoTypeInfoCStyleCastExpr(Context, FType, CK_BitCast, |
| 5363 | NewRep); |
| 5364 | BlockDeclRefs.clear(); |
| 5365 | BlockByRefDecls.clear(); |
| 5366 | BlockByRefDeclsPtrSet.clear(); |
| 5367 | BlockByCopyDecls.clear(); |
| 5368 | BlockByCopyDeclsPtrSet.clear(); |
| 5369 | ImportedBlockDecls.clear(); |
| 5370 | return NewRep; |
| 5371 | } |
| 5372 | |
| 5373 | bool RewriteModernObjC::IsDeclStmtInForeachHeader(DeclStmt *DS) { |
| 5374 | if (const ObjCForCollectionStmt * CS = |
| 5375 | dyn_cast<ObjCForCollectionStmt>(Stmts.back())) |
| 5376 | return CS->getElement() == DS; |
| 5377 | return false; |
| 5378 | } |
| 5379 | |
| 5380 | //===----------------------------------------------------------------------===// |
| 5381 | // Function Body / Expression rewriting |
| 5382 | //===----------------------------------------------------------------------===// |
| 5383 | |
| 5384 | Stmt *RewriteModernObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
| 5385 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
| 5386 | isa<DoStmt>(S) || isa<ForStmt>(S)) |
| 5387 | Stmts.push_back(S); |
| 5388 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 5389 | Stmts.push_back(S); |
| 5390 | ObjCBcLabelNo.push_back(++BcLabelCount); |
| 5391 | } |
| 5392 | |
| 5393 | // Pseudo-object operations and ivar references need special |
| 5394 | // treatment because we're going to recursively rewrite them. |
| 5395 | if (PseudoObjectExpr *PseudoOp = dyn_cast<PseudoObjectExpr>(S)) { |
| 5396 | if (isa<BinaryOperator>(PseudoOp->getSyntacticForm())) { |
| 5397 | return RewritePropertyOrImplicitSetter(PseudoOp); |
| 5398 | } else { |
| 5399 | return RewritePropertyOrImplicitGetter(PseudoOp); |
| 5400 | } |
| 5401 | } else if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) { |
| 5402 | return RewriteObjCIvarRefExpr(IvarRefExpr); |
| 5403 | } |
| 5404 | |
| 5405 | SourceRange OrigStmtRange = S->getSourceRange(); |
| 5406 | |
| 5407 | // Perform a bottom up rewrite of all children. |
| 5408 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
| 5409 | if (*CI) { |
| 5410 | Stmt *childStmt = (*CI); |
| 5411 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(childStmt); |
| 5412 | if (newStmt) { |
| 5413 | *CI = newStmt; |
| 5414 | } |
| 5415 | } |
| 5416 | |
| 5417 | if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5418 | SmallVector<DeclRefExpr *, 8> InnerBlockDeclRefs; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5419 | llvm::SmallPtrSet<const DeclContext *, 8> InnerContexts; |
| 5420 | InnerContexts.insert(BE->getBlockDecl()); |
| 5421 | ImportedLocalExternalDecls.clear(); |
| 5422 | GetInnerBlockDeclRefExprs(BE->getBody(), |
| 5423 | InnerBlockDeclRefs, InnerContexts); |
| 5424 | // Rewrite the block body in place. |
| 5425 | Stmt *SaveCurrentBody = CurrentBody; |
| 5426 | CurrentBody = BE->getBody(); |
| 5427 | PropParentMap = 0; |
| 5428 | // block literal on rhs of a property-dot-sytax assignment |
| 5429 | // must be replaced by its synthesize ast so getRewrittenText |
| 5430 | // works as expected. In this case, what actually ends up on RHS |
| 5431 | // is the blockTranscribed which is the helper function for the |
| 5432 | // block literal; as in: self.c = ^() {[ace ARR];}; |
| 5433 | bool saveDisableReplaceStmt = DisableReplaceStmt; |
| 5434 | DisableReplaceStmt = false; |
| 5435 | RewriteFunctionBodyOrGlobalInitializer(BE->getBody()); |
| 5436 | DisableReplaceStmt = saveDisableReplaceStmt; |
| 5437 | CurrentBody = SaveCurrentBody; |
| 5438 | PropParentMap = 0; |
| 5439 | ImportedLocalExternalDecls.clear(); |
| 5440 | // Now we snarf the rewritten text and stash it away for later use. |
| 5441 | std::string Str = Rewrite.getRewrittenText(BE->getSourceRange()); |
| 5442 | RewrittenBlockExprs[BE] = Str; |
| 5443 | |
| 5444 | Stmt *blockTranscribed = SynthBlockInitExpr(BE, InnerBlockDeclRefs); |
| 5445 | |
| 5446 | //blockTranscribed->dump(); |
| 5447 | ReplaceStmt(S, blockTranscribed); |
| 5448 | return blockTranscribed; |
| 5449 | } |
| 5450 | // Handle specific things. |
| 5451 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 5452 | return RewriteAtEncode(AtEncode); |
| 5453 | |
| 5454 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 5455 | return RewriteAtSelector(AtSelector); |
| 5456 | |
| 5457 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 5458 | return RewriteObjCStringLiteral(AtString); |
Fariborz Jahanian | 5594704 | 2012-03-27 20:17:30 +0000 | [diff] [blame] | 5459 | |
| 5460 | if (ObjCBoolLiteralExpr *BoolLitExpr = dyn_cast<ObjCBoolLiteralExpr>(S)) |
| 5461 | return RewriteObjCBoolLiteralExpr(BoolLitExpr); |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 5462 | |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 5463 | if (ObjCBoxedExpr *BoxedExpr = dyn_cast<ObjCBoxedExpr>(S)) |
| 5464 | return RewriteObjCBoxedExpr(BoxedExpr); |
Fariborz Jahanian | 86cff60 | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 5465 | |
| 5466 | if (ObjCArrayLiteral *ArrayLitExpr = dyn_cast<ObjCArrayLiteral>(S)) |
| 5467 | return RewriteObjCArrayLiteralExpr(ArrayLitExpr); |
Fariborz Jahanian | e35abe1 | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 5468 | |
| 5469 | if (ObjCDictionaryLiteral *DictionaryLitExpr = |
| 5470 | dyn_cast<ObjCDictionaryLiteral>(S)) |
| 5471 | return RewriteObjCDictionaryLiteralExpr(DictionaryLitExpr); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5472 | |
| 5473 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
| 5474 | #if 0 |
| 5475 | // Before we rewrite it, put the original message expression in a comment. |
| 5476 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 5477 | SourceLocation endLoc = MessExpr->getLocEnd(); |
| 5478 | |
| 5479 | const char *startBuf = SM->getCharacterData(startLoc); |
| 5480 | const char *endBuf = SM->getCharacterData(endLoc); |
| 5481 | |
| 5482 | std::string messString; |
| 5483 | messString += "// "; |
| 5484 | messString.append(startBuf, endBuf-startBuf+1); |
| 5485 | messString += "\n"; |
| 5486 | |
| 5487 | // FIXME: Missing definition of |
| 5488 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
| 5489 | // InsertText(startLoc, messString.c_str(), messString.size()); |
| 5490 | // Tried this, but it didn't work either... |
| 5491 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
| 5492 | #endif |
| 5493 | return RewriteMessageExpr(MessExpr); |
| 5494 | } |
| 5495 | |
Fariborz Jahanian | 042b91d | 2012-05-23 23:47:20 +0000 | [diff] [blame] | 5496 | if (ObjCAutoreleasePoolStmt *StmtAutoRelease = |
| 5497 | dyn_cast<ObjCAutoreleasePoolStmt>(S)) { |
| 5498 | return RewriteObjCAutoreleasePoolStmt(StmtAutoRelease); |
| 5499 | } |
| 5500 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5501 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 5502 | return RewriteObjCTryStmt(StmtTry); |
| 5503 | |
| 5504 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 5505 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 5506 | |
| 5507 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 5508 | return RewriteObjCThrowStmt(StmtThrow); |
| 5509 | |
| 5510 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 5511 | return RewriteObjCProtocolExpr(ProtocolExp); |
| 5512 | |
| 5513 | if (ObjCForCollectionStmt *StmtForCollection = |
| 5514 | dyn_cast<ObjCForCollectionStmt>(S)) |
| 5515 | return RewriteObjCForCollectionStmt(StmtForCollection, |
| 5516 | OrigStmtRange.getEnd()); |
| 5517 | if (BreakStmt *StmtBreakStmt = |
| 5518 | dyn_cast<BreakStmt>(S)) |
| 5519 | return RewriteBreakStmt(StmtBreakStmt); |
| 5520 | if (ContinueStmt *StmtContinueStmt = |
| 5521 | dyn_cast<ContinueStmt>(S)) |
| 5522 | return RewriteContinueStmt(StmtContinueStmt); |
| 5523 | |
| 5524 | // Need to check for protocol refs (id <P>, Foo <P> *) in variable decls |
| 5525 | // and cast exprs. |
| 5526 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
| 5527 | // FIXME: What we're doing here is modifying the type-specifier that |
| 5528 | // precedes the first Decl. In the future the DeclGroup should have |
| 5529 | // a separate type-specifier that we can rewrite. |
| 5530 | // NOTE: We need to avoid rewriting the DeclStmt if it is within |
| 5531 | // the context of an ObjCForCollectionStmt. For example: |
| 5532 | // NSArray *someArray; |
| 5533 | // for (id <FooProtocol> index in someArray) ; |
| 5534 | // This is because RewriteObjCForCollectionStmt() does textual rewriting |
| 5535 | // and it depends on the original text locations/positions. |
| 5536 | if (Stmts.empty() || !IsDeclStmtInForeachHeader(DS)) |
| 5537 | RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin()); |
| 5538 | |
| 5539 | // Blocks rewrite rules. |
| 5540 | for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); |
| 5541 | DI != DE; ++DI) { |
| 5542 | Decl *SD = *DI; |
| 5543 | if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { |
| 5544 | if (isTopLevelBlockPointerType(ND->getType())) |
| 5545 | RewriteBlockPointerDecl(ND); |
| 5546 | else if (ND->getType()->isFunctionPointerType()) |
| 5547 | CheckFunctionPointerDecl(ND->getType(), ND); |
| 5548 | if (VarDecl *VD = dyn_cast<VarDecl>(SD)) { |
| 5549 | if (VD->hasAttr<BlocksAttr>()) { |
| 5550 | static unsigned uniqueByrefDeclCount = 0; |
| 5551 | assert(!BlockByRefDeclNo.count(ND) && |
| 5552 | "RewriteFunctionBodyOrGlobalInitializer: Duplicate byref decl"); |
| 5553 | BlockByRefDeclNo[ND] = uniqueByrefDeclCount++; |
Fariborz Jahanian | 4fe261c | 2012-04-24 19:38:45 +0000 | [diff] [blame] | 5554 | RewriteByRefVar(VD, (DI == DS->decl_begin()), ((DI+1) == DE)); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5555 | } |
| 5556 | else |
| 5557 | RewriteTypeOfDecl(VD); |
| 5558 | } |
| 5559 | } |
| 5560 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) { |
| 5561 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
| 5562 | RewriteBlockPointerDecl(TD); |
| 5563 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
| 5564 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 5565 | } |
| 5566 | } |
| 5567 | } |
| 5568 | |
| 5569 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) |
| 5570 | RewriteObjCQualifiedInterfaceTypes(CE); |
| 5571 | |
| 5572 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
| 5573 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 5574 | assert(!Stmts.empty() && "Statement stack is empty"); |
| 5575 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 5576 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
| 5577 | && "Statement stack mismatch"); |
| 5578 | Stmts.pop_back(); |
| 5579 | } |
| 5580 | // Handle blocks rewriting. |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5581 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 5582 | ValueDecl *VD = DRE->getDecl(); |
| 5583 | if (VD->hasAttr<BlocksAttr>()) |
| 5584 | return RewriteBlockDeclRefExpr(DRE); |
| 5585 | if (HasLocalVariableExternalStorage(VD)) |
| 5586 | return RewriteLocalVariableExternalStorage(DRE); |
| 5587 | } |
| 5588 | |
| 5589 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
| 5590 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
| 5591 | Stmt *BlockCall = SynthesizeBlockCall(CE, CE->getCallee()); |
| 5592 | ReplaceStmt(S, BlockCall); |
| 5593 | return BlockCall; |
| 5594 | } |
| 5595 | } |
| 5596 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) { |
| 5597 | RewriteCastExpr(CE); |
| 5598 | } |
Fariborz Jahanian | f1ee687 | 2012-04-10 00:08:18 +0000 | [diff] [blame] | 5599 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
| 5600 | RewriteImplicitCastObjCExpr(ICE); |
| 5601 | } |
Fariborz Jahanian | 43aa1c3 | 2012-04-16 22:14:01 +0000 | [diff] [blame] | 5602 | #if 0 |
Fariborz Jahanian | 653b7cf | 2012-04-13 18:00:54 +0000 | [diff] [blame] | 5603 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5604 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
| 5605 | CastExpr *Replacement = new (Context) CastExpr(ICE->getType(), |
| 5606 | ICE->getSubExpr(), |
| 5607 | SourceLocation()); |
| 5608 | // Get the new text. |
| 5609 | std::string SStr; |
| 5610 | llvm::raw_string_ostream Buf(SStr); |
| 5611 | Replacement->printPretty(Buf, *Context); |
| 5612 | const std::string &Str = Buf.str(); |
| 5613 | |
| 5614 | printf("CAST = %s\n", &Str[0]); |
| 5615 | InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 5616 | delete S; |
| 5617 | return Replacement; |
| 5618 | } |
| 5619 | #endif |
| 5620 | // Return this stmt unmodified. |
| 5621 | return S; |
| 5622 | } |
| 5623 | |
| 5624 | void RewriteModernObjC::RewriteRecordBody(RecordDecl *RD) { |
| 5625 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 5626 | e = RD->field_end(); i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5627 | FieldDecl *FD = *i; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5628 | if (isTopLevelBlockPointerType(FD->getType())) |
| 5629 | RewriteBlockPointerDecl(FD); |
| 5630 | if (FD->getType()->isObjCQualifiedIdType() || |
| 5631 | FD->getType()->isObjCQualifiedInterfaceType()) |
| 5632 | RewriteObjCQualifiedInterfaceTypes(FD); |
| 5633 | } |
| 5634 | } |
| 5635 | |
| 5636 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 5637 | /// main file of the input. |
| 5638 | void RewriteModernObjC::HandleDeclInMainFile(Decl *D) { |
| 5639 | switch (D->getKind()) { |
| 5640 | case Decl::Function: { |
| 5641 | FunctionDecl *FD = cast<FunctionDecl>(D); |
| 5642 | if (FD->isOverloadedOperator()) |
| 5643 | return; |
| 5644 | |
| 5645 | // Since function prototypes don't have ParmDecl's, we check the function |
| 5646 | // prototype. This enables us to rewrite function declarations and |
| 5647 | // definitions using the same code. |
| 5648 | RewriteBlocksInFunctionProtoType(FD->getType(), FD); |
| 5649 | |
Argyrios Kyrtzidis | 9335df3 | 2012-02-12 04:48:45 +0000 | [diff] [blame] | 5650 | if (!FD->isThisDeclarationADefinition()) |
| 5651 | break; |
| 5652 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5653 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
| 5654 | if (CompoundStmt *Body = dyn_cast_or_null<CompoundStmt>(FD->getBody())) { |
| 5655 | CurFunctionDef = FD; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5656 | CurrentBody = Body; |
| 5657 | Body = |
| 5658 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 5659 | FD->setBody(Body); |
| 5660 | CurrentBody = 0; |
| 5661 | if (PropParentMap) { |
| 5662 | delete PropParentMap; |
| 5663 | PropParentMap = 0; |
| 5664 | } |
| 5665 | // This synthesizes and inserts the block "impl" struct, invoke function, |
| 5666 | // and any copy/dispose helper functions. |
| 5667 | InsertBlockLiteralsWithinFunction(FD); |
| 5668 | CurFunctionDef = 0; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5669 | } |
| 5670 | break; |
| 5671 | } |
| 5672 | case Decl::ObjCMethod: { |
| 5673 | ObjCMethodDecl *MD = cast<ObjCMethodDecl>(D); |
| 5674 | if (CompoundStmt *Body = MD->getCompoundBody()) { |
| 5675 | CurMethodDef = MD; |
| 5676 | CurrentBody = Body; |
| 5677 | Body = |
| 5678 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 5679 | MD->setBody(Body); |
| 5680 | CurrentBody = 0; |
| 5681 | if (PropParentMap) { |
| 5682 | delete PropParentMap; |
| 5683 | PropParentMap = 0; |
| 5684 | } |
| 5685 | InsertBlockLiteralsWithinMethod(MD); |
| 5686 | CurMethodDef = 0; |
| 5687 | } |
| 5688 | break; |
| 5689 | } |
| 5690 | case Decl::ObjCImplementation: { |
| 5691 | ObjCImplementationDecl *CI = cast<ObjCImplementationDecl>(D); |
| 5692 | ClassImplementation.push_back(CI); |
| 5693 | break; |
| 5694 | } |
| 5695 | case Decl::ObjCCategoryImpl: { |
| 5696 | ObjCCategoryImplDecl *CI = cast<ObjCCategoryImplDecl>(D); |
| 5697 | CategoryImplementation.push_back(CI); |
| 5698 | break; |
| 5699 | } |
| 5700 | case Decl::Var: { |
| 5701 | VarDecl *VD = cast<VarDecl>(D); |
| 5702 | RewriteObjCQualifiedInterfaceTypes(VD); |
| 5703 | if (isTopLevelBlockPointerType(VD->getType())) |
| 5704 | RewriteBlockPointerDecl(VD); |
| 5705 | else if (VD->getType()->isFunctionPointerType()) { |
| 5706 | CheckFunctionPointerDecl(VD->getType(), VD); |
| 5707 | if (VD->getInit()) { |
| 5708 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
| 5709 | RewriteCastExpr(CE); |
| 5710 | } |
| 5711 | } |
| 5712 | } else if (VD->getType()->isRecordType()) { |
| 5713 | RecordDecl *RD = VD->getType()->getAs<RecordType>()->getDecl(); |
| 5714 | if (RD->isCompleteDefinition()) |
| 5715 | RewriteRecordBody(RD); |
| 5716 | } |
| 5717 | if (VD->getInit()) { |
| 5718 | GlobalVarDecl = VD; |
| 5719 | CurrentBody = VD->getInit(); |
| 5720 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
| 5721 | CurrentBody = 0; |
| 5722 | if (PropParentMap) { |
| 5723 | delete PropParentMap; |
| 5724 | PropParentMap = 0; |
| 5725 | } |
| 5726 | SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), VD->getName()); |
| 5727 | GlobalVarDecl = 0; |
| 5728 | |
| 5729 | // This is needed for blocks. |
| 5730 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
| 5731 | RewriteCastExpr(CE); |
| 5732 | } |
| 5733 | } |
| 5734 | break; |
| 5735 | } |
| 5736 | case Decl::TypeAlias: |
| 5737 | case Decl::Typedef: { |
| 5738 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
| 5739 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
| 5740 | RewriteBlockPointerDecl(TD); |
| 5741 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
| 5742 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 5743 | } |
| 5744 | break; |
| 5745 | } |
| 5746 | case Decl::CXXRecord: |
| 5747 | case Decl::Record: { |
| 5748 | RecordDecl *RD = cast<RecordDecl>(D); |
| 5749 | if (RD->isCompleteDefinition()) |
| 5750 | RewriteRecordBody(RD); |
| 5751 | break; |
| 5752 | } |
| 5753 | default: |
| 5754 | break; |
| 5755 | } |
| 5756 | // Nothing yet. |
| 5757 | } |
| 5758 | |
Fariborz Jahanian | 30650eb | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 5759 | /// Write_ProtocolExprReferencedMetadata - This routine writer out the |
| 5760 | /// protocol reference symbols in the for of: |
| 5761 | /// struct _protocol_t *PROTOCOL_REF = &PROTOCOL_METADATA. |
| 5762 | static void Write_ProtocolExprReferencedMetadata(ASTContext *Context, |
| 5763 | ObjCProtocolDecl *PDecl, |
| 5764 | std::string &Result) { |
| 5765 | // Also output .objc_protorefs$B section and its meta-data. |
| 5766 | if (Context->getLangOpts().MicrosoftExt) |
Fariborz Jahanian | bd78cfa | 2012-04-27 21:39:49 +0000 | [diff] [blame] | 5767 | Result += "static "; |
Fariborz Jahanian | 30650eb | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 5768 | Result += "struct _protocol_t *"; |
| 5769 | Result += "_OBJC_PROTOCOL_REFERENCE_$_"; |
| 5770 | Result += PDecl->getNameAsString(); |
| 5771 | Result += " = &"; |
| 5772 | Result += "_OBJC_PROTOCOL_"; Result += PDecl->getNameAsString(); |
| 5773 | Result += ";\n"; |
| 5774 | } |
| 5775 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5776 | void RewriteModernObjC::HandleTranslationUnit(ASTContext &C) { |
| 5777 | if (Diags.hasErrorOccurred()) |
| 5778 | return; |
| 5779 | |
| 5780 | RewriteInclude(); |
| 5781 | |
| 5782 | // Here's a great place to add any extra declarations that may be needed. |
| 5783 | // Write out meta data for each @protocol(<expr>). |
| 5784 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Fariborz Jahanian | 30650eb | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 5785 | E = ProtocolExprDecls.end(); I != E; ++I) { |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 5786 | RewriteObjCProtocolMetaData(*I, Preamble); |
Fariborz Jahanian | 30650eb | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 5787 | Write_ProtocolExprReferencedMetadata(Context, (*I), Preamble); |
| 5788 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5789 | |
| 5790 | InsertText(SM->getLocForStartOfFile(MainFileID), Preamble, false); |
Fariborz Jahanian | 163d3ce | 2012-05-08 23:54:35 +0000 | [diff] [blame] | 5791 | |
| 5792 | if (ClassImplementation.size() || CategoryImplementation.size()) |
| 5793 | RewriteImplementations(); |
| 5794 | |
Fariborz Jahanian | 5731778 | 2012-02-21 23:58:41 +0000 | [diff] [blame] | 5795 | for (unsigned i = 0, e = ObjCInterfacesSeen.size(); i < e; i++) { |
| 5796 | ObjCInterfaceDecl *CDecl = ObjCInterfacesSeen[i]; |
| 5797 | // Write struct declaration for the class matching its ivar declarations. |
| 5798 | // Note that for modern abi, this is postponed until the end of TU |
| 5799 | // because class extensions and the implementation might declare their own |
| 5800 | // private ivars. |
| 5801 | RewriteInterfaceDecl(CDecl); |
| 5802 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5803 | |
| 5804 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 5805 | // we are done. |
| 5806 | if (const RewriteBuffer *RewriteBuf = |
| 5807 | Rewrite.getRewriteBufferFor(MainFileID)) { |
| 5808 | //printf("Changed:\n"); |
| 5809 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 5810 | } else { |
| 5811 | llvm::errs() << "No changes\n"; |
| 5812 | } |
| 5813 | |
| 5814 | if (ClassImplementation.size() || CategoryImplementation.size() || |
| 5815 | ProtocolExprDecls.size()) { |
| 5816 | // Rewrite Objective-c meta data* |
| 5817 | std::string ResultStr; |
| 5818 | RewriteMetaDataIntoBuffer(ResultStr); |
| 5819 | // Emit metadata. |
| 5820 | *OutFile << ResultStr; |
| 5821 | } |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 5822 | // Emit ImageInfo; |
| 5823 | { |
| 5824 | std::string ResultStr; |
| 5825 | WriteImageInfo(ResultStr); |
| 5826 | *OutFile << ResultStr; |
| 5827 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5828 | OutFile->flush(); |
| 5829 | } |
| 5830 | |
| 5831 | void RewriteModernObjC::Initialize(ASTContext &context) { |
| 5832 | InitializeCommon(context); |
| 5833 | |
Fariborz Jahanian | 6991bc5 | 2012-03-10 17:45:38 +0000 | [diff] [blame] | 5834 | Preamble += "#ifndef __OBJC2__\n"; |
| 5835 | Preamble += "#define __OBJC2__\n"; |
| 5836 | Preamble += "#endif\n"; |
| 5837 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5838 | // declaring objc_selector outside the parameter list removes a silly |
| 5839 | // scope related warning... |
| 5840 | if (IsHeader) |
| 5841 | Preamble = "#pragma once\n"; |
| 5842 | Preamble += "struct objc_selector; struct objc_class;\n"; |
Fariborz Jahanian | e2d87bc | 2012-04-12 23:52:52 +0000 | [diff] [blame] | 5843 | Preamble += "struct __rw_objc_super { \n\tstruct objc_object *object; "; |
| 5844 | Preamble += "\n\tstruct objc_object *superClass; "; |
| 5845 | // Add a constructor for creating temporary objects. |
| 5846 | Preamble += "\n\t__rw_objc_super(struct objc_object *o, struct objc_object *s) "; |
| 5847 | Preamble += ": object(o), superClass(s) {} "; |
| 5848 | Preamble += "\n};\n"; |
| 5849 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5850 | if (LangOpts.MicrosoftExt) { |
Fariborz Jahanian | 1ca052c | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 5851 | // Define all sections using syntax that makes sense. |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 5852 | // These are currently generated. |
| 5853 | Preamble += "\n#pragma section(\".objc_classlist$B\", long, read, write)\n"; |
Fariborz Jahanian | 1ca052c | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 5854 | Preamble += "#pragma section(\".objc_catlist$B\", long, read, write)\n"; |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 5855 | Preamble += "#pragma section(\".objc_imageinfo$B\", long, read, write)\n"; |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 5856 | Preamble += "#pragma section(\".objc_nlclslist$B\", long, read, write)\n"; |
| 5857 | Preamble += "#pragma section(\".objc_nlcatlist$B\", long, read, write)\n"; |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 5858 | // These are generated but not necessary for functionality. |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 5859 | Preamble += "#pragma section(\".cat_cls_meth$B\", long, read, write)\n"; |
Fariborz Jahanian | 1ca052c | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 5860 | Preamble += "#pragma section(\".inst_meth$B\", long, read, write)\n"; |
| 5861 | Preamble += "#pragma section(\".cls_meth$B\", long, read, write)\n"; |
Fariborz Jahanian | 40a777a | 2012-03-12 16:46:58 +0000 | [diff] [blame] | 5862 | Preamble += "#pragma section(\".objc_ivar$B\", long, read, write)\n"; |
Fariborz Jahanian | de5d946 | 2012-03-14 18:09:23 +0000 | [diff] [blame] | 5863 | |
Fariborz Jahanian | 30650eb | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 5864 | // These need be generated for performance. Currently they are not, |
| 5865 | // using API calls instead. |
| 5866 | Preamble += "#pragma section(\".objc_selrefs$B\", long, read, write)\n"; |
| 5867 | Preamble += "#pragma section(\".objc_classrefs$B\", long, read, write)\n"; |
| 5868 | Preamble += "#pragma section(\".objc_superrefs$B\", long, read, write)\n"; |
| 5869 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5870 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5871 | Preamble += "#ifndef _REWRITER_typedef_Protocol\n"; |
| 5872 | Preamble += "typedef struct objc_object Protocol;\n"; |
| 5873 | Preamble += "#define _REWRITER_typedef_Protocol\n"; |
| 5874 | Preamble += "#endif\n"; |
| 5875 | if (LangOpts.MicrosoftExt) { |
| 5876 | Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n"; |
| 5877 | Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n"; |
Fariborz Jahanian | 5cf6b6c | 2012-03-21 23:41:04 +0000 | [diff] [blame] | 5878 | } |
| 5879 | else |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5880 | Preamble += "#define __OBJC_RW_DLLIMPORT extern\n"; |
Fariborz Jahanian | 5cf6b6c | 2012-03-21 23:41:04 +0000 | [diff] [blame] | 5881 | |
| 5882 | Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSend(void);\n"; |
| 5883 | Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSendSuper(void);\n"; |
| 5884 | Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSend_stret(void);\n"; |
| 5885 | Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSendSuper_stret(void);\n"; |
| 5886 | Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSend_fpret(void);\n"; |
| 5887 | |
Fariborz Jahanian | 20e181a | 2012-05-08 20:55:55 +0000 | [diff] [blame] | 5888 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_class *objc_getClass"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5889 | Preamble += "(const char *);\n"; |
| 5890 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_class *class_getSuperclass"; |
| 5891 | Preamble += "(struct objc_class *);\n"; |
Fariborz Jahanian | 20e181a | 2012-05-08 20:55:55 +0000 | [diff] [blame] | 5892 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_class *objc_getMetaClass"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5893 | Preamble += "(const char *);\n"; |
Fariborz Jahanian | 55261af | 2012-03-19 18:11:32 +0000 | [diff] [blame] | 5894 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw( struct objc_object *);\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5895 | // @synchronized hooks. |
Fariborz Jahanian | 55261af | 2012-03-19 18:11:32 +0000 | [diff] [blame] | 5896 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_enter( struct objc_object *);\n"; |
| 5897 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_exit( struct objc_object *);\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5898 | Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n"; |
| 5899 | Preamble += "#ifndef __FASTENUMERATIONSTATE\n"; |
| 5900 | Preamble += "struct __objcFastEnumerationState {\n\t"; |
| 5901 | Preamble += "unsigned long state;\n\t"; |
| 5902 | Preamble += "void **itemsPtr;\n\t"; |
| 5903 | Preamble += "unsigned long *mutationsPtr;\n\t"; |
| 5904 | Preamble += "unsigned long extra[5];\n};\n"; |
| 5905 | Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n"; |
| 5906 | Preamble += "#define __FASTENUMERATIONSTATE\n"; |
| 5907 | Preamble += "#endif\n"; |
| 5908 | Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n"; |
| 5909 | Preamble += "struct __NSConstantStringImpl {\n"; |
| 5910 | Preamble += " int *isa;\n"; |
| 5911 | Preamble += " int flags;\n"; |
| 5912 | Preamble += " char *str;\n"; |
| 5913 | Preamble += " long length;\n"; |
| 5914 | Preamble += "};\n"; |
| 5915 | Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n"; |
| 5916 | Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n"; |
| 5917 | Preamble += "#else\n"; |
| 5918 | Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n"; |
| 5919 | Preamble += "#endif\n"; |
| 5920 | Preamble += "#define __NSCONSTANTSTRINGIMPL\n"; |
| 5921 | Preamble += "#endif\n"; |
| 5922 | // Blocks preamble. |
| 5923 | Preamble += "#ifndef BLOCK_IMPL\n"; |
| 5924 | Preamble += "#define BLOCK_IMPL\n"; |
| 5925 | Preamble += "struct __block_impl {\n"; |
| 5926 | Preamble += " void *isa;\n"; |
| 5927 | Preamble += " int Flags;\n"; |
| 5928 | Preamble += " int Reserved;\n"; |
| 5929 | Preamble += " void *FuncPtr;\n"; |
| 5930 | Preamble += "};\n"; |
| 5931 | Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n"; |
| 5932 | Preamble += "#ifdef __OBJC_EXPORT_BLOCKS\n"; |
| 5933 | Preamble += "extern \"C\" __declspec(dllexport) " |
| 5934 | "void _Block_object_assign(void *, const void *, const int);\n"; |
| 5935 | Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_dispose(const void *, const int);\n"; |
| 5936 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteGlobalBlock[32];\n"; |
| 5937 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteStackBlock[32];\n"; |
| 5938 | Preamble += "#else\n"; |
| 5939 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_assign(void *, const void *, const int);\n"; |
| 5940 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_dispose(const void *, const int);\n"; |
| 5941 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteGlobalBlock[32];\n"; |
| 5942 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteStackBlock[32];\n"; |
| 5943 | Preamble += "#endif\n"; |
| 5944 | Preamble += "#endif\n"; |
| 5945 | if (LangOpts.MicrosoftExt) { |
| 5946 | Preamble += "#undef __OBJC_RW_DLLIMPORT\n"; |
| 5947 | Preamble += "#undef __OBJC_RW_STATICIMPORT\n"; |
| 5948 | Preamble += "#ifndef KEEP_ATTRIBUTES\n"; // We use this for clang tests. |
| 5949 | Preamble += "#define __attribute__(X)\n"; |
| 5950 | Preamble += "#endif\n"; |
Fariborz Jahanian | 5ce2827 | 2012-04-12 16:33:31 +0000 | [diff] [blame] | 5951 | Preamble += "#ifndef __weak\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5952 | Preamble += "#define __weak\n"; |
Fariborz Jahanian | 5ce2827 | 2012-04-12 16:33:31 +0000 | [diff] [blame] | 5953 | Preamble += "#endif\n"; |
| 5954 | Preamble += "#ifndef __block\n"; |
| 5955 | Preamble += "#define __block\n"; |
| 5956 | Preamble += "#endif\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5957 | } |
| 5958 | else { |
| 5959 | Preamble += "#define __block\n"; |
| 5960 | Preamble += "#define __weak\n"; |
| 5961 | } |
Fariborz Jahanian | 49f6dac | 2012-06-29 19:33:05 +0000 | [diff] [blame] | 5962 | Preamble += "\nextern \"C\" void * memset(void *b, int c, unsigned long len);\n"; |
Fariborz Jahanian | be8d55c | 2012-06-29 18:27:08 +0000 | [diff] [blame] | 5963 | |
Fariborz Jahanian | b0f245c | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 5964 | // Declarations required for modern objective-c array and dictionary literals. |
| 5965 | Preamble += "\n#include <stdarg.h>\n"; |
Fariborz Jahanian | e35abe1 | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 5966 | Preamble += "struct __NSContainer_literal {\n"; |
Fariborz Jahanian | b0f245c | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 5967 | Preamble += " void * *arr;\n"; |
Fariborz Jahanian | e35abe1 | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 5968 | Preamble += " __NSContainer_literal (unsigned int count, ...) {\n"; |
Fariborz Jahanian | b0f245c | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 5969 | Preamble += "\tva_list marker;\n"; |
| 5970 | Preamble += "\tva_start(marker, count);\n"; |
| 5971 | Preamble += "\tarr = new void *[count];\n"; |
| 5972 | Preamble += "\tfor (unsigned i = 0; i < count; i++)\n"; |
| 5973 | Preamble += "\t arr[i] = va_arg(marker, void *);\n"; |
| 5974 | Preamble += "\tva_end( marker );\n"; |
| 5975 | Preamble += " };\n"; |
Fariborz Jahanian | 13a9c02 | 2012-05-02 23:53:46 +0000 | [diff] [blame] | 5976 | Preamble += " ~__NSContainer_literal() {\n"; |
Fariborz Jahanian | b0f245c | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 5977 | Preamble += "\tdelete[] arr;\n"; |
| 5978 | Preamble += " }\n"; |
| 5979 | Preamble += "};\n"; |
| 5980 | |
Fariborz Jahanian | 042b91d | 2012-05-23 23:47:20 +0000 | [diff] [blame] | 5981 | // Declaration required for implementation of @autoreleasepool statement. |
| 5982 | Preamble += "extern \"C\" __declspec(dllimport) void * objc_autoreleasePoolPush(void);\n"; |
| 5983 | Preamble += "extern \"C\" __declspec(dllimport) void objc_autoreleasePoolPop(void *);\n\n"; |
| 5984 | Preamble += "struct __AtAutoreleasePool {\n"; |
| 5985 | Preamble += " __AtAutoreleasePool() {atautoreleasepoolobj = objc_autoreleasePoolPush();}\n"; |
| 5986 | Preamble += " ~__AtAutoreleasePool() {objc_autoreleasePoolPop(atautoreleasepoolobj);}\n"; |
| 5987 | Preamble += " void * atautoreleasepoolobj;\n"; |
| 5988 | Preamble += "};\n"; |
| 5989 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5990 | // NOTE! Windows uses LLP64 for 64bit mode. So, cast pointer to long long |
| 5991 | // as this avoids warning in any 64bit/32bit compilation model. |
| 5992 | Preamble += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((long long) &((TYPE *)0)->MEMBER)\n"; |
| 5993 | } |
| 5994 | |
| 5995 | /// RewriteIvarOffsetComputation - This rutine synthesizes computation of |
| 5996 | /// ivar offset. |
| 5997 | void RewriteModernObjC::RewriteIvarOffsetComputation(ObjCIvarDecl *ivar, |
| 5998 | std::string &Result) { |
| 5999 | if (ivar->isBitField()) { |
| 6000 | // FIXME: The hack below doesn't work for bitfields. For now, we simply |
| 6001 | // place all bitfields at offset 0. |
| 6002 | Result += "0"; |
| 6003 | } else { |
| 6004 | Result += "__OFFSETOFIVAR__(struct "; |
| 6005 | Result += ivar->getContainingInterface()->getNameAsString(); |
| 6006 | if (LangOpts.MicrosoftExt) |
| 6007 | Result += "_IMPL"; |
| 6008 | Result += ", "; |
| 6009 | Result += ivar->getNameAsString(); |
| 6010 | Result += ")"; |
| 6011 | } |
| 6012 | } |
| 6013 | |
| 6014 | /// WriteModernMetadataDeclarations - Writes out metadata declarations for modern ABI. |
| 6015 | /// struct _prop_t { |
Fariborz Jahanian | da35eac | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 6016 | /// const char *name; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6017 | /// char *attributes; |
| 6018 | /// } |
| 6019 | |
| 6020 | /// struct _prop_list_t { |
| 6021 | /// uint32_t entsize; // sizeof(struct _prop_t) |
| 6022 | /// uint32_t count_of_properties; |
| 6023 | /// struct _prop_t prop_list[count_of_properties]; |
| 6024 | /// } |
| 6025 | |
| 6026 | /// struct _protocol_t; |
| 6027 | |
| 6028 | /// struct _protocol_list_t { |
| 6029 | /// long protocol_count; // Note, this is 32/64 bit |
| 6030 | /// struct _protocol_t * protocol_list[protocol_count]; |
| 6031 | /// } |
| 6032 | |
| 6033 | /// struct _objc_method { |
| 6034 | /// SEL _cmd; |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6035 | /// const char *method_type; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6036 | /// char *_imp; |
| 6037 | /// } |
| 6038 | |
| 6039 | /// struct _method_list_t { |
| 6040 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 6041 | /// uint32_t method_count; |
| 6042 | /// struct _objc_method method_list[method_count]; |
| 6043 | /// } |
| 6044 | |
| 6045 | /// struct _protocol_t { |
| 6046 | /// id isa; // NULL |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 6047 | /// const char *protocol_name; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6048 | /// const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 6049 | /// const struct method_list_t *instance_methods; |
| 6050 | /// const struct method_list_t *class_methods; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6051 | /// const struct method_list_t *optionalInstanceMethods; |
| 6052 | /// const struct method_list_t *optionalClassMethods; |
| 6053 | /// const struct _prop_list_t * properties; |
| 6054 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 6055 | /// const uint32_t flags; // = 0 |
| 6056 | /// const char ** extendedMethodTypes; |
| 6057 | /// } |
| 6058 | |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6059 | /// struct _ivar_t { |
| 6060 | /// unsigned long int *offset; // pointer to ivar offset location |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6061 | /// const char *name; |
| 6062 | /// const char *type; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6063 | /// uint32_t alignment; |
| 6064 | /// uint32_t size; |
| 6065 | /// } |
| 6066 | |
| 6067 | /// struct _ivar_list_t { |
| 6068 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 6069 | /// uint32 count; |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6070 | /// struct _ivar_t list[count]; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6071 | /// } |
| 6072 | |
| 6073 | /// struct _class_ro_t { |
Fariborz Jahanian | 249cd10 | 2012-03-24 16:53:16 +0000 | [diff] [blame] | 6074 | /// uint32_t flags; |
| 6075 | /// uint32_t instanceStart; |
| 6076 | /// uint32_t instanceSize; |
| 6077 | /// uint32_t reserved; // only when building for 64bit targets |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 6078 | /// const uint8_t *ivarLayout; |
| 6079 | /// const char *name; |
| 6080 | /// const struct _method_list_t *baseMethods; |
| 6081 | /// const struct _protocol_list_t *baseProtocols; |
| 6082 | /// const struct _ivar_list_t *ivars; |
| 6083 | /// const uint8_t *weakIvarLayout; |
| 6084 | /// const struct _prop_list_t *properties; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6085 | /// } |
| 6086 | |
| 6087 | /// struct _class_t { |
| 6088 | /// struct _class_t *isa; |
Fariborz Jahanian | fd4ce2c | 2012-03-20 17:34:50 +0000 | [diff] [blame] | 6089 | /// struct _class_t *superclass; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6090 | /// void *cache; |
| 6091 | /// IMP *vtable; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6092 | /// struct _class_ro_t *ro; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6093 | /// } |
| 6094 | |
| 6095 | /// struct _category_t { |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 6096 | /// const char *name; |
Fariborz Jahanian | 4b2fe6e | 2012-03-20 21:41:28 +0000 | [diff] [blame] | 6097 | /// struct _class_t *cls; |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 6098 | /// const struct _method_list_t *instance_methods; |
| 6099 | /// const struct _method_list_t *class_methods; |
| 6100 | /// const struct _protocol_list_t *protocols; |
| 6101 | /// const struct _prop_list_t *properties; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6102 | /// } |
| 6103 | |
| 6104 | /// MessageRefTy - LLVM for: |
| 6105 | /// struct _message_ref_t { |
| 6106 | /// IMP messenger; |
| 6107 | /// SEL name; |
| 6108 | /// }; |
| 6109 | |
| 6110 | /// SuperMessageRefTy - LLVM for: |
| 6111 | /// struct _super_message_ref_t { |
| 6112 | /// SUPER_IMP messenger; |
| 6113 | /// SEL name; |
| 6114 | /// }; |
| 6115 | |
Fariborz Jahanian | de5d946 | 2012-03-14 18:09:23 +0000 | [diff] [blame] | 6116 | static void WriteModernMetadataDeclarations(ASTContext *Context, std::string &Result) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6117 | static bool meta_data_declared = false; |
| 6118 | if (meta_data_declared) |
| 6119 | return; |
| 6120 | |
| 6121 | Result += "\nstruct _prop_t {\n"; |
Fariborz Jahanian | da35eac | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 6122 | Result += "\tconst char *name;\n"; |
| 6123 | Result += "\tconst char *attributes;\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6124 | Result += "};\n"; |
| 6125 | |
| 6126 | Result += "\nstruct _protocol_t;\n"; |
| 6127 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6128 | Result += "\nstruct _objc_method {\n"; |
| 6129 | Result += "\tstruct objc_selector * _cmd;\n"; |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6130 | Result += "\tconst char *method_type;\n"; |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6131 | Result += "\tvoid *_imp;\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6132 | Result += "};\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6133 | |
| 6134 | Result += "\nstruct _protocol_t {\n"; |
| 6135 | Result += "\tvoid * isa; // NULL\n"; |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 6136 | Result += "\tconst char *protocol_name;\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6137 | Result += "\tconst struct _protocol_list_t * protocol_list; // super protocols\n"; |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 6138 | Result += "\tconst struct method_list_t *instance_methods;\n"; |
| 6139 | Result += "\tconst struct method_list_t *class_methods;\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6140 | Result += "\tconst struct method_list_t *optionalInstanceMethods;\n"; |
| 6141 | Result += "\tconst struct method_list_t *optionalClassMethods;\n"; |
| 6142 | Result += "\tconst struct _prop_list_t * properties;\n"; |
| 6143 | Result += "\tconst unsigned int size; // sizeof(struct _protocol_t)\n"; |
| 6144 | Result += "\tconst unsigned int flags; // = 0\n"; |
| 6145 | Result += "\tconst char ** extendedMethodTypes;\n"; |
| 6146 | Result += "};\n"; |
| 6147 | |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6148 | Result += "\nstruct _ivar_t {\n"; |
| 6149 | Result += "\tunsigned long int *offset; // pointer to ivar offset location\n"; |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6150 | Result += "\tconst char *name;\n"; |
| 6151 | Result += "\tconst char *type;\n"; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6152 | Result += "\tunsigned int alignment;\n"; |
| 6153 | Result += "\tunsigned int size;\n"; |
| 6154 | Result += "};\n"; |
| 6155 | |
| 6156 | Result += "\nstruct _class_ro_t {\n"; |
Fariborz Jahanian | 249cd10 | 2012-03-24 16:53:16 +0000 | [diff] [blame] | 6157 | Result += "\tunsigned int flags;\n"; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6158 | Result += "\tunsigned int instanceStart;\n"; |
Fariborz Jahanian | 249cd10 | 2012-03-24 16:53:16 +0000 | [diff] [blame] | 6159 | Result += "\tunsigned int instanceSize;\n"; |
Fariborz Jahanian | de5d946 | 2012-03-14 18:09:23 +0000 | [diff] [blame] | 6160 | const llvm::Triple &Triple(Context->getTargetInfo().getTriple()); |
| 6161 | if (Triple.getArch() == llvm::Triple::x86_64) |
Fariborz Jahanian | 249cd10 | 2012-03-24 16:53:16 +0000 | [diff] [blame] | 6162 | Result += "\tunsigned int reserved;\n"; |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 6163 | Result += "\tconst unsigned char *ivarLayout;\n"; |
| 6164 | Result += "\tconst char *name;\n"; |
| 6165 | Result += "\tconst struct _method_list_t *baseMethods;\n"; |
| 6166 | Result += "\tconst struct _objc_protocol_list *baseProtocols;\n"; |
| 6167 | Result += "\tconst struct _ivar_list_t *ivars;\n"; |
| 6168 | Result += "\tconst unsigned char *weakIvarLayout;\n"; |
| 6169 | Result += "\tconst struct _prop_list_t *properties;\n"; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6170 | Result += "};\n"; |
| 6171 | |
| 6172 | Result += "\nstruct _class_t {\n"; |
| 6173 | Result += "\tstruct _class_t *isa;\n"; |
Fariborz Jahanian | fd4ce2c | 2012-03-20 17:34:50 +0000 | [diff] [blame] | 6174 | Result += "\tstruct _class_t *superclass;\n"; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6175 | Result += "\tvoid *cache;\n"; |
| 6176 | Result += "\tvoid *vtable;\n"; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6177 | Result += "\tstruct _class_ro_t *ro;\n"; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6178 | Result += "};\n"; |
| 6179 | |
| 6180 | Result += "\nstruct _category_t {\n"; |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 6181 | Result += "\tconst char *name;\n"; |
Fariborz Jahanian | 4b2fe6e | 2012-03-20 21:41:28 +0000 | [diff] [blame] | 6182 | Result += "\tstruct _class_t *cls;\n"; |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 6183 | Result += "\tconst struct _method_list_t *instance_methods;\n"; |
| 6184 | Result += "\tconst struct _method_list_t *class_methods;\n"; |
| 6185 | Result += "\tconst struct _protocol_list_t *protocols;\n"; |
| 6186 | Result += "\tconst struct _prop_list_t *properties;\n"; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 6187 | Result += "};\n"; |
| 6188 | |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 6189 | Result += "extern \"C\" __declspec(dllimport) struct objc_cache _objc_empty_cache;\n"; |
Fariborz Jahanian | 297976d | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 6190 | Result += "#pragma warning(disable:4273)\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6191 | meta_data_declared = true; |
| 6192 | } |
| 6193 | |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 6194 | static void Write_protocol_list_t_TypeDecl(std::string &Result, |
| 6195 | long super_protocol_count) { |
| 6196 | Result += "struct /*_protocol_list_t*/"; Result += " {\n"; |
| 6197 | Result += "\tlong protocol_count; // Note, this is 32/64 bit\n"; |
| 6198 | Result += "\tstruct _protocol_t *super_protocols["; |
| 6199 | Result += utostr(super_protocol_count); Result += "];\n"; |
| 6200 | Result += "}"; |
| 6201 | } |
| 6202 | |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6203 | static void Write_method_list_t_TypeDecl(std::string &Result, |
| 6204 | unsigned int method_count) { |
| 6205 | Result += "struct /*_method_list_t*/"; Result += " {\n"; |
| 6206 | Result += "\tunsigned int entsize; // sizeof(struct _objc_method)\n"; |
| 6207 | Result += "\tunsigned int method_count;\n"; |
| 6208 | Result += "\tstruct _objc_method method_list["; |
| 6209 | Result += utostr(method_count); Result += "];\n"; |
| 6210 | Result += "}"; |
| 6211 | } |
| 6212 | |
Fariborz Jahanian | da35eac | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 6213 | static void Write__prop_list_t_TypeDecl(std::string &Result, |
| 6214 | unsigned int property_count) { |
| 6215 | Result += "struct /*_prop_list_t*/"; Result += " {\n"; |
| 6216 | Result += "\tunsigned int entsize; // sizeof(struct _prop_t)\n"; |
| 6217 | Result += "\tunsigned int count_of_properties;\n"; |
| 6218 | Result += "\tstruct _prop_t prop_list["; |
| 6219 | Result += utostr(property_count); Result += "];\n"; |
| 6220 | Result += "}"; |
| 6221 | } |
| 6222 | |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6223 | static void Write__ivar_list_t_TypeDecl(std::string &Result, |
| 6224 | unsigned int ivar_count) { |
| 6225 | Result += "struct /*_ivar_list_t*/"; Result += " {\n"; |
| 6226 | Result += "\tunsigned int entsize; // sizeof(struct _prop_t)\n"; |
| 6227 | Result += "\tunsigned int count;\n"; |
| 6228 | Result += "\tstruct _ivar_t ivar_list["; |
| 6229 | Result += utostr(ivar_count); Result += "];\n"; |
| 6230 | Result += "}"; |
| 6231 | } |
| 6232 | |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 6233 | static void Write_protocol_list_initializer(ASTContext *Context, std::string &Result, |
| 6234 | ArrayRef<ObjCProtocolDecl *> SuperProtocols, |
| 6235 | StringRef VarName, |
| 6236 | StringRef ProtocolName) { |
| 6237 | if (SuperProtocols.size() > 0) { |
| 6238 | Result += "\nstatic "; |
| 6239 | Write_protocol_list_t_TypeDecl(Result, SuperProtocols.size()); |
| 6240 | Result += " "; Result += VarName; |
| 6241 | Result += ProtocolName; |
| 6242 | Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n"; |
| 6243 | Result += "\t"; Result += utostr(SuperProtocols.size()); Result += ",\n"; |
| 6244 | for (unsigned i = 0, e = SuperProtocols.size(); i < e; i++) { |
| 6245 | ObjCProtocolDecl *SuperPD = SuperProtocols[i]; |
| 6246 | Result += "\t&"; Result += "_OBJC_PROTOCOL_"; |
| 6247 | Result += SuperPD->getNameAsString(); |
| 6248 | if (i == e-1) |
| 6249 | Result += "\n};\n"; |
| 6250 | else |
| 6251 | Result += ",\n"; |
| 6252 | } |
| 6253 | } |
| 6254 | } |
| 6255 | |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6256 | static void Write_method_list_t_initializer(RewriteModernObjC &RewriteObj, |
| 6257 | ASTContext *Context, std::string &Result, |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6258 | ArrayRef<ObjCMethodDecl *> Methods, |
| 6259 | StringRef VarName, |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6260 | StringRef TopLevelDeclName, |
| 6261 | bool MethodImpl) { |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6262 | if (Methods.size() > 0) { |
| 6263 | Result += "\nstatic "; |
| 6264 | Write_method_list_t_TypeDecl(Result, Methods.size()); |
| 6265 | Result += " "; Result += VarName; |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6266 | Result += TopLevelDeclName; |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6267 | Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n"; |
| 6268 | Result += "\t"; Result += "sizeof(_objc_method)"; Result += ",\n"; |
| 6269 | Result += "\t"; Result += utostr(Methods.size()); Result += ",\n"; |
| 6270 | for (unsigned i = 0, e = Methods.size(); i < e; i++) { |
| 6271 | ObjCMethodDecl *MD = Methods[i]; |
| 6272 | if (i == 0) |
| 6273 | Result += "\t{{(struct objc_selector *)\""; |
| 6274 | else |
| 6275 | Result += "\t{(struct objc_selector *)\""; |
| 6276 | Result += (MD)->getSelector().getAsString(); Result += "\""; |
| 6277 | Result += ", "; |
| 6278 | std::string MethodTypeString; |
| 6279 | Context->getObjCEncodingForMethodDecl(MD, MethodTypeString); |
| 6280 | Result += "\""; Result += MethodTypeString; Result += "\""; |
| 6281 | Result += ", "; |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6282 | if (!MethodImpl) |
| 6283 | Result += "0"; |
| 6284 | else { |
| 6285 | Result += "(void *)"; |
| 6286 | Result += RewriteObj.MethodInternalNames[MD]; |
| 6287 | } |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6288 | if (i == e-1) |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6289 | Result += "}}\n"; |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6290 | else |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6291 | Result += "},\n"; |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6292 | } |
| 6293 | Result += "};\n"; |
| 6294 | } |
| 6295 | } |
| 6296 | |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6297 | static void Write_prop_list_t_initializer(RewriteModernObjC &RewriteObj, |
Fariborz Jahanian | da35eac | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 6298 | ASTContext *Context, std::string &Result, |
| 6299 | ArrayRef<ObjCPropertyDecl *> Properties, |
| 6300 | const Decl *Container, |
| 6301 | StringRef VarName, |
| 6302 | StringRef ProtocolName) { |
| 6303 | if (Properties.size() > 0) { |
| 6304 | Result += "\nstatic "; |
| 6305 | Write__prop_list_t_TypeDecl(Result, Properties.size()); |
| 6306 | Result += " "; Result += VarName; |
| 6307 | Result += ProtocolName; |
| 6308 | Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n"; |
| 6309 | Result += "\t"; Result += "sizeof(_prop_t)"; Result += ",\n"; |
| 6310 | Result += "\t"; Result += utostr(Properties.size()); Result += ",\n"; |
| 6311 | for (unsigned i = 0, e = Properties.size(); i < e; i++) { |
| 6312 | ObjCPropertyDecl *PropDecl = Properties[i]; |
| 6313 | if (i == 0) |
| 6314 | Result += "\t{{\""; |
| 6315 | else |
| 6316 | Result += "\t{\""; |
| 6317 | Result += PropDecl->getName(); Result += "\","; |
| 6318 | std::string PropertyTypeString, QuotePropertyTypeString; |
| 6319 | Context->getObjCEncodingForPropertyDecl(PropDecl, Container, PropertyTypeString); |
| 6320 | RewriteObj.QuoteDoublequotes(PropertyTypeString, QuotePropertyTypeString); |
| 6321 | Result += "\""; Result += QuotePropertyTypeString; Result += "\""; |
| 6322 | if (i == e-1) |
| 6323 | Result += "}}\n"; |
| 6324 | else |
| 6325 | Result += "},\n"; |
| 6326 | } |
| 6327 | Result += "};\n"; |
| 6328 | } |
| 6329 | } |
| 6330 | |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 6331 | // Metadata flags |
| 6332 | enum MetaDataDlags { |
| 6333 | CLS = 0x0, |
| 6334 | CLS_META = 0x1, |
| 6335 | CLS_ROOT = 0x2, |
| 6336 | OBJC2_CLS_HIDDEN = 0x10, |
| 6337 | CLS_EXCEPTION = 0x20, |
| 6338 | |
| 6339 | /// (Obsolete) ARC-specific: this class has a .release_ivars method |
| 6340 | CLS_HAS_IVAR_RELEASER = 0x40, |
| 6341 | /// class was compiled with -fobjc-arr |
| 6342 | CLS_COMPILED_BY_ARC = 0x80 // (1<<7) |
| 6343 | }; |
| 6344 | |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6345 | static void Write__class_ro_t_initializer(ASTContext *Context, std::string &Result, |
| 6346 | unsigned int flags, |
| 6347 | const std::string &InstanceStart, |
| 6348 | const std::string &InstanceSize, |
| 6349 | ArrayRef<ObjCMethodDecl *>baseMethods, |
| 6350 | ArrayRef<ObjCProtocolDecl *>baseProtocols, |
| 6351 | ArrayRef<ObjCIvarDecl *>ivars, |
| 6352 | ArrayRef<ObjCPropertyDecl *>Properties, |
| 6353 | StringRef VarName, |
| 6354 | StringRef ClassName) { |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6355 | Result += "\nstatic struct _class_ro_t "; |
| 6356 | Result += VarName; Result += ClassName; |
| 6357 | Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n"; |
| 6358 | Result += "\t"; |
| 6359 | Result += llvm::utostr(flags); Result += ", "; |
| 6360 | Result += InstanceStart; Result += ", "; |
| 6361 | Result += InstanceSize; Result += ", \n"; |
| 6362 | Result += "\t"; |
Fariborz Jahanian | de5d946 | 2012-03-14 18:09:23 +0000 | [diff] [blame] | 6363 | const llvm::Triple &Triple(Context->getTargetInfo().getTriple()); |
| 6364 | if (Triple.getArch() == llvm::Triple::x86_64) |
| 6365 | // uint32_t const reserved; // only when building for 64bit targets |
| 6366 | Result += "(unsigned int)0, \n\t"; |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6367 | // const uint8_t * const ivarLayout; |
| 6368 | Result += "0, \n\t"; |
| 6369 | Result += "\""; Result += ClassName; Result += "\",\n\t"; |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 6370 | bool metaclass = ((flags & CLS_META) != 0); |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6371 | if (baseMethods.size() > 0) { |
| 6372 | Result += "(const struct _method_list_t *)&"; |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 6373 | if (metaclass) |
| 6374 | Result += "_OBJC_$_CLASS_METHODS_"; |
| 6375 | else |
| 6376 | Result += "_OBJC_$_INSTANCE_METHODS_"; |
| 6377 | Result += ClassName; |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6378 | Result += ",\n\t"; |
| 6379 | } |
| 6380 | else |
| 6381 | Result += "0, \n\t"; |
| 6382 | |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 6383 | if (!metaclass && baseProtocols.size() > 0) { |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6384 | Result += "(const struct _objc_protocol_list *)&"; |
| 6385 | Result += "_OBJC_CLASS_PROTOCOLS_$_"; Result += ClassName; |
| 6386 | Result += ",\n\t"; |
| 6387 | } |
| 6388 | else |
| 6389 | Result += "0, \n\t"; |
| 6390 | |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 6391 | if (!metaclass && ivars.size() > 0) { |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6392 | Result += "(const struct _ivar_list_t *)&"; |
| 6393 | Result += "_OBJC_$_INSTANCE_VARIABLES_"; Result += ClassName; |
| 6394 | Result += ",\n\t"; |
| 6395 | } |
| 6396 | else |
| 6397 | Result += "0, \n\t"; |
| 6398 | |
| 6399 | // weakIvarLayout |
| 6400 | Result += "0, \n\t"; |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 6401 | if (!metaclass && Properties.size() > 0) { |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6402 | Result += "(const struct _prop_list_t *)&"; |
Fariborz Jahanian | eeabf38 | 2012-02-16 21:57:59 +0000 | [diff] [blame] | 6403 | Result += "_OBJC_$_PROP_LIST_"; Result += ClassName; |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6404 | Result += ",\n"; |
| 6405 | } |
| 6406 | else |
| 6407 | Result += "0, \n"; |
| 6408 | |
| 6409 | Result += "};\n"; |
| 6410 | } |
| 6411 | |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6412 | static void Write_class_t(ASTContext *Context, std::string &Result, |
| 6413 | StringRef VarName, |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6414 | const ObjCInterfaceDecl *CDecl, bool metaclass) { |
| 6415 | bool rootClass = (!CDecl->getSuperClass()); |
| 6416 | const ObjCInterfaceDecl *RootClass = CDecl; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6417 | |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6418 | if (!rootClass) { |
| 6419 | // Find the Root class |
| 6420 | RootClass = CDecl->getSuperClass(); |
| 6421 | while (RootClass->getSuperClass()) { |
| 6422 | RootClass = RootClass->getSuperClass(); |
| 6423 | } |
| 6424 | } |
| 6425 | |
| 6426 | if (metaclass && rootClass) { |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6427 | // Need to handle a case of use of forward declaration. |
Fariborz Jahanian | ce0d897 | 2012-03-10 18:25:06 +0000 | [diff] [blame] | 6428 | Result += "\n"; |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 6429 | Result += "extern \"C\" "; |
Fariborz Jahanian | ce0d897 | 2012-03-10 18:25:06 +0000 | [diff] [blame] | 6430 | if (CDecl->getImplementation()) |
| 6431 | Result += "__declspec(dllexport) "; |
Fariborz Jahanian | 297976d | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 6432 | else |
| 6433 | Result += "__declspec(dllimport) "; |
| 6434 | |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 6435 | Result += "struct _class_t OBJC_CLASS_$_"; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6436 | Result += CDecl->getNameAsString(); |
| 6437 | Result += ";\n"; |
| 6438 | } |
| 6439 | // Also, for possibility of 'super' metadata class not having been defined yet. |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6440 | if (!rootClass) { |
Fariborz Jahanian | 868e985 | 2012-03-29 19:04:10 +0000 | [diff] [blame] | 6441 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
Fariborz Jahanian | ce0d897 | 2012-03-10 18:25:06 +0000 | [diff] [blame] | 6442 | Result += "\n"; |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 6443 | Result += "extern \"C\" "; |
Fariborz Jahanian | 868e985 | 2012-03-29 19:04:10 +0000 | [diff] [blame] | 6444 | if (SuperClass->getImplementation()) |
Fariborz Jahanian | ce0d897 | 2012-03-10 18:25:06 +0000 | [diff] [blame] | 6445 | Result += "__declspec(dllexport) "; |
Fariborz Jahanian | 297976d | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 6446 | else |
| 6447 | Result += "__declspec(dllimport) "; |
| 6448 | |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 6449 | Result += "struct _class_t "; |
Fariborz Jahanian | ce0d897 | 2012-03-10 18:25:06 +0000 | [diff] [blame] | 6450 | Result += VarName; |
Fariborz Jahanian | 868e985 | 2012-03-29 19:04:10 +0000 | [diff] [blame] | 6451 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6452 | Result += ";\n"; |
Fariborz Jahanian | 452eac1 | 2012-03-20 21:09:58 +0000 | [diff] [blame] | 6453 | |
Fariborz Jahanian | 868e985 | 2012-03-29 19:04:10 +0000 | [diff] [blame] | 6454 | if (metaclass && RootClass != SuperClass) { |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 6455 | Result += "extern \"C\" "; |
Fariborz Jahanian | 452eac1 | 2012-03-20 21:09:58 +0000 | [diff] [blame] | 6456 | if (RootClass->getImplementation()) |
| 6457 | Result += "__declspec(dllexport) "; |
Fariborz Jahanian | 297976d | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 6458 | else |
| 6459 | Result += "__declspec(dllimport) "; |
| 6460 | |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 6461 | Result += "struct _class_t "; |
Fariborz Jahanian | 452eac1 | 2012-03-20 21:09:58 +0000 | [diff] [blame] | 6462 | Result += VarName; |
| 6463 | Result += RootClass->getNameAsString(); |
| 6464 | Result += ";\n"; |
| 6465 | } |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6466 | } |
| 6467 | |
Fariborz Jahanian | 297976d | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 6468 | Result += "\nextern \"C\" __declspec(dllexport) struct _class_t "; |
| 6469 | Result += VarName; Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6470 | Result += " __attribute__ ((used, section (\"__DATA,__objc_data\"))) = {\n"; |
| 6471 | Result += "\t"; |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6472 | if (metaclass) { |
| 6473 | if (!rootClass) { |
| 6474 | Result += "0, // &"; Result += VarName; |
| 6475 | Result += RootClass->getNameAsString(); |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6476 | Result += ",\n\t"; |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6477 | Result += "0, // &"; Result += VarName; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6478 | Result += CDecl->getSuperClass()->getNameAsString(); |
| 6479 | Result += ",\n\t"; |
| 6480 | } |
| 6481 | else { |
Fariborz Jahanian | 452eac1 | 2012-03-20 21:09:58 +0000 | [diff] [blame] | 6482 | Result += "0, // &"; Result += VarName; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6483 | Result += CDecl->getNameAsString(); |
| 6484 | Result += ",\n\t"; |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6485 | Result += "0, // &OBJC_CLASS_$_"; Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6486 | Result += ",\n\t"; |
| 6487 | } |
| 6488 | } |
| 6489 | else { |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6490 | Result += "0, // &OBJC_METACLASS_$_"; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6491 | Result += CDecl->getNameAsString(); |
| 6492 | Result += ",\n\t"; |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6493 | if (!rootClass) { |
| 6494 | Result += "0, // &"; Result += VarName; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6495 | Result += CDecl->getSuperClass()->getNameAsString(); |
| 6496 | Result += ",\n\t"; |
| 6497 | } |
| 6498 | else |
| 6499 | Result += "0,\n\t"; |
| 6500 | } |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6501 | Result += "0, // (void *)&_objc_empty_cache,\n\t"; |
| 6502 | Result += "0, // unused, was (void *)&_objc_empty_vtable,\n\t"; |
| 6503 | if (metaclass) |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6504 | Result += "&_OBJC_METACLASS_RO_$_"; |
| 6505 | else |
| 6506 | Result += "&_OBJC_CLASS_RO_$_"; |
| 6507 | Result += CDecl->getNameAsString(); |
| 6508 | Result += ",\n};\n"; |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6509 | |
| 6510 | // Add static function to initialize some of the meta-data fields. |
| 6511 | // avoid doing it twice. |
| 6512 | if (metaclass) |
| 6513 | return; |
| 6514 | |
| 6515 | const ObjCInterfaceDecl *SuperClass = |
| 6516 | rootClass ? CDecl : CDecl->getSuperClass(); |
| 6517 | |
| 6518 | Result += "static void OBJC_CLASS_SETUP_$_"; |
| 6519 | Result += CDecl->getNameAsString(); |
| 6520 | Result += "(void ) {\n"; |
| 6521 | Result += "\tOBJC_METACLASS_$_"; Result += CDecl->getNameAsString(); |
| 6522 | Result += ".isa = "; Result += "&OBJC_METACLASS_$_"; |
Fariborz Jahanian | 452eac1 | 2012-03-20 21:09:58 +0000 | [diff] [blame] | 6523 | Result += RootClass->getNameAsString(); Result += ";\n"; |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6524 | |
| 6525 | Result += "\tOBJC_METACLASS_$_"; Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 452eac1 | 2012-03-20 21:09:58 +0000 | [diff] [blame] | 6526 | Result += ".superclass = "; |
| 6527 | if (rootClass) |
| 6528 | Result += "&OBJC_CLASS_$_"; |
| 6529 | else |
| 6530 | Result += "&OBJC_METACLASS_$_"; |
| 6531 | |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6532 | Result += SuperClass->getNameAsString(); Result += ";\n"; |
| 6533 | |
| 6534 | Result += "\tOBJC_METACLASS_$_"; Result += CDecl->getNameAsString(); |
| 6535 | Result += ".cache = "; Result += "&_objc_empty_cache"; Result += ";\n"; |
| 6536 | |
| 6537 | Result += "\tOBJC_CLASS_$_"; Result += CDecl->getNameAsString(); |
| 6538 | Result += ".isa = "; Result += "&OBJC_METACLASS_$_"; |
| 6539 | Result += CDecl->getNameAsString(); Result += ";\n"; |
| 6540 | |
| 6541 | if (!rootClass) { |
| 6542 | Result += "\tOBJC_CLASS_$_"; Result += CDecl->getNameAsString(); |
| 6543 | Result += ".superclass = "; Result += "&OBJC_CLASS_$_"; |
| 6544 | Result += SuperClass->getNameAsString(); Result += ";\n"; |
| 6545 | } |
| 6546 | |
| 6547 | Result += "\tOBJC_CLASS_$_"; Result += CDecl->getNameAsString(); |
| 6548 | Result += ".cache = "; Result += "&_objc_empty_cache"; Result += ";\n"; |
| 6549 | Result += "}\n"; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6550 | } |
| 6551 | |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6552 | static void Write_category_t(RewriteModernObjC &RewriteObj, ASTContext *Context, |
| 6553 | std::string &Result, |
Fariborz Jahanian | e033578 | 2012-03-27 18:41:05 +0000 | [diff] [blame] | 6554 | ObjCCategoryDecl *CatDecl, |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 6555 | ObjCInterfaceDecl *ClassDecl, |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6556 | ArrayRef<ObjCMethodDecl *> InstanceMethods, |
| 6557 | ArrayRef<ObjCMethodDecl *> ClassMethods, |
| 6558 | ArrayRef<ObjCProtocolDecl *> RefedProtocols, |
| 6559 | ArrayRef<ObjCPropertyDecl *> ClassProperties) { |
Fariborz Jahanian | e033578 | 2012-03-27 18:41:05 +0000 | [diff] [blame] | 6560 | StringRef CatName = CatDecl->getName(); |
NAKAMURA Takumi | 20f8939 | 2012-03-21 03:21:46 +0000 | [diff] [blame] | 6561 | StringRef ClassName = ClassDecl->getName(); |
Fariborz Jahanian | 8c00a1b | 2012-02-17 20:33:00 +0000 | [diff] [blame] | 6562 | // must declare an extern class object in case this class is not implemented |
| 6563 | // in this TU. |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 6564 | Result += "\n"; |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 6565 | Result += "extern \"C\" "; |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 6566 | if (ClassDecl->getImplementation()) |
| 6567 | Result += "__declspec(dllexport) "; |
Fariborz Jahanian | 297976d | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 6568 | else |
| 6569 | Result += "__declspec(dllimport) "; |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 6570 | |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 6571 | Result += "struct _class_t "; |
Fariborz Jahanian | 8c00a1b | 2012-02-17 20:33:00 +0000 | [diff] [blame] | 6572 | Result += "OBJC_CLASS_$_"; Result += ClassName; |
| 6573 | Result += ";\n"; |
| 6574 | |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6575 | Result += "\nstatic struct _category_t "; |
| 6576 | Result += "_OBJC_$_CATEGORY_"; |
| 6577 | Result += ClassName; Result += "_$_"; Result += CatName; |
| 6578 | Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = \n"; |
| 6579 | Result += "{\n"; |
| 6580 | Result += "\t\""; Result += ClassName; Result += "\",\n"; |
Fariborz Jahanian | 4b2fe6e | 2012-03-20 21:41:28 +0000 | [diff] [blame] | 6581 | Result += "\t0, // &"; Result += "OBJC_CLASS_$_"; Result += ClassName; |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6582 | Result += ",\n"; |
| 6583 | if (InstanceMethods.size() > 0) { |
| 6584 | Result += "\t(const struct _method_list_t *)&"; |
| 6585 | Result += "_OBJC_$_CATEGORY_INSTANCE_METHODS_"; |
| 6586 | Result += ClassName; Result += "_$_"; Result += CatName; |
| 6587 | Result += ",\n"; |
| 6588 | } |
| 6589 | else |
| 6590 | Result += "\t0,\n"; |
| 6591 | |
| 6592 | if (ClassMethods.size() > 0) { |
| 6593 | Result += "\t(const struct _method_list_t *)&"; |
| 6594 | Result += "_OBJC_$_CATEGORY_CLASS_METHODS_"; |
| 6595 | Result += ClassName; Result += "_$_"; Result += CatName; |
| 6596 | Result += ",\n"; |
| 6597 | } |
| 6598 | else |
| 6599 | Result += "\t0,\n"; |
| 6600 | |
| 6601 | if (RefedProtocols.size() > 0) { |
| 6602 | Result += "\t(const struct _protocol_list_t *)&"; |
| 6603 | Result += "_OBJC_CATEGORY_PROTOCOLS_$_"; |
| 6604 | Result += ClassName; Result += "_$_"; Result += CatName; |
| 6605 | Result += ",\n"; |
| 6606 | } |
| 6607 | else |
| 6608 | Result += "\t0,\n"; |
| 6609 | |
| 6610 | if (ClassProperties.size() > 0) { |
| 6611 | Result += "\t(const struct _prop_list_t *)&"; Result += "_OBJC_$_PROP_LIST_"; |
| 6612 | Result += ClassName; Result += "_$_"; Result += CatName; |
| 6613 | Result += ",\n"; |
| 6614 | } |
| 6615 | else |
| 6616 | Result += "\t0,\n"; |
| 6617 | |
| 6618 | Result += "};\n"; |
Fariborz Jahanian | 4b2fe6e | 2012-03-20 21:41:28 +0000 | [diff] [blame] | 6619 | |
| 6620 | // Add static function to initialize the class pointer in the category structure. |
| 6621 | Result += "static void OBJC_CATEGORY_SETUP_$_"; |
| 6622 | Result += ClassDecl->getNameAsString(); |
| 6623 | Result += "_$_"; |
| 6624 | Result += CatName; |
| 6625 | Result += "(void ) {\n"; |
| 6626 | Result += "\t_OBJC_$_CATEGORY_"; |
| 6627 | Result += ClassDecl->getNameAsString(); |
| 6628 | Result += "_$_"; |
| 6629 | Result += CatName; |
| 6630 | Result += ".cls = "; Result += "&OBJC_CLASS_$_"; Result += ClassName; |
| 6631 | Result += ";\n}\n"; |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6632 | } |
| 6633 | |
Fariborz Jahanian | e0adbd8 | 2012-02-08 22:23:26 +0000 | [diff] [blame] | 6634 | static void Write__extendedMethodTypes_initializer(RewriteModernObjC &RewriteObj, |
| 6635 | ASTContext *Context, std::string &Result, |
| 6636 | ArrayRef<ObjCMethodDecl *> Methods, |
| 6637 | StringRef VarName, |
| 6638 | StringRef ProtocolName) { |
| 6639 | if (Methods.size() == 0) |
| 6640 | return; |
| 6641 | |
| 6642 | Result += "\nstatic const char *"; |
| 6643 | Result += VarName; Result += ProtocolName; |
| 6644 | Result += " [] __attribute__ ((used, section (\"__DATA,__objc_const\"))) = \n"; |
| 6645 | Result += "{\n"; |
| 6646 | for (unsigned i = 0, e = Methods.size(); i < e; i++) { |
| 6647 | ObjCMethodDecl *MD = Methods[i]; |
| 6648 | std::string MethodTypeString, QuoteMethodTypeString; |
| 6649 | Context->getObjCEncodingForMethodDecl(MD, MethodTypeString, true); |
| 6650 | RewriteObj.QuoteDoublequotes(MethodTypeString, QuoteMethodTypeString); |
| 6651 | Result += "\t\""; Result += QuoteMethodTypeString; Result += "\""; |
| 6652 | if (i == e-1) |
| 6653 | Result += "\n};\n"; |
| 6654 | else { |
| 6655 | Result += ",\n"; |
| 6656 | } |
| 6657 | } |
| 6658 | } |
| 6659 | |
Fariborz Jahanian | acee1c9 | 2012-04-11 21:12:36 +0000 | [diff] [blame] | 6660 | static void Write_IvarOffsetVar(RewriteModernObjC &RewriteObj, |
| 6661 | ASTContext *Context, |
Fariborz Jahanian | 40a777a | 2012-03-12 16:46:58 +0000 | [diff] [blame] | 6662 | std::string &Result, |
Fariborz Jahanian | db64923 | 2012-02-13 20:59:02 +0000 | [diff] [blame] | 6663 | ArrayRef<ObjCIvarDecl *> Ivars, |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 6664 | ObjCInterfaceDecl *CDecl) { |
Fariborz Jahanian | db64923 | 2012-02-13 20:59:02 +0000 | [diff] [blame] | 6665 | // FIXME. visibilty of offset symbols may have to be set; for Darwin |
| 6666 | // this is what happens: |
| 6667 | /** |
| 6668 | if (Ivar->getAccessControl() == ObjCIvarDecl::Private || |
| 6669 | Ivar->getAccessControl() == ObjCIvarDecl::Package || |
| 6670 | Class->getVisibility() == HiddenVisibility) |
| 6671 | Visibility shoud be: HiddenVisibility; |
| 6672 | else |
| 6673 | Visibility shoud be: DefaultVisibility; |
| 6674 | */ |
| 6675 | |
Fariborz Jahanian | 07e5288 | 2012-02-13 21:34:45 +0000 | [diff] [blame] | 6676 | Result += "\n"; |
| 6677 | for (unsigned i =0, e = Ivars.size(); i < e; i++) { |
| 6678 | ObjCIvarDecl *IvarDecl = Ivars[i]; |
Fariborz Jahanian | 40a777a | 2012-03-12 16:46:58 +0000 | [diff] [blame] | 6679 | if (Context->getLangOpts().MicrosoftExt) |
| 6680 | Result += "__declspec(allocate(\".objc_ivar$B\")) "; |
| 6681 | |
| 6682 | if (!Context->getLangOpts().MicrosoftExt || |
| 6683 | IvarDecl->getAccessControl() == ObjCIvarDecl::Private || |
Fariborz Jahanian | 117591f | 2012-03-10 01:34:42 +0000 | [diff] [blame] | 6684 | IvarDecl->getAccessControl() == ObjCIvarDecl::Package) |
Fariborz Jahanian | 297976d | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 6685 | Result += "extern \"C\" unsigned long int "; |
Fariborz Jahanian | d1c84d3 | 2012-03-10 00:53:02 +0000 | [diff] [blame] | 6686 | else |
Fariborz Jahanian | 297976d | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 6687 | Result += "extern \"C\" __declspec(dllexport) unsigned long int "; |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 6688 | WriteInternalIvarName(CDecl, IvarDecl, Result); |
Fariborz Jahanian | 07e5288 | 2012-02-13 21:34:45 +0000 | [diff] [blame] | 6689 | Result += " __attribute__ ((used, section (\"__DATA,__objc_ivar\")))"; |
| 6690 | Result += " = "; |
Fariborz Jahanian | acee1c9 | 2012-04-11 21:12:36 +0000 | [diff] [blame] | 6691 | RewriteObj.RewriteIvarOffsetComputation(IvarDecl, Result); |
| 6692 | Result += ";\n"; |
Fariborz Jahanian | db64923 | 2012-02-13 20:59:02 +0000 | [diff] [blame] | 6693 | } |
| 6694 | } |
| 6695 | |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6696 | static void Write__ivar_list_t_initializer(RewriteModernObjC &RewriteObj, |
| 6697 | ASTContext *Context, std::string &Result, |
| 6698 | ArrayRef<ObjCIvarDecl *> Ivars, |
| 6699 | StringRef VarName, |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 6700 | ObjCInterfaceDecl *CDecl) { |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6701 | if (Ivars.size() > 0) { |
Fariborz Jahanian | acee1c9 | 2012-04-11 21:12:36 +0000 | [diff] [blame] | 6702 | Write_IvarOffsetVar(RewriteObj, Context, Result, Ivars, CDecl); |
Fariborz Jahanian | 07e5288 | 2012-02-13 21:34:45 +0000 | [diff] [blame] | 6703 | |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6704 | Result += "\nstatic "; |
| 6705 | Write__ivar_list_t_TypeDecl(Result, Ivars.size()); |
| 6706 | Result += " "; Result += VarName; |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 6707 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6708 | Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n"; |
| 6709 | Result += "\t"; Result += "sizeof(_ivar_t)"; Result += ",\n"; |
| 6710 | Result += "\t"; Result += utostr(Ivars.size()); Result += ",\n"; |
| 6711 | for (unsigned i =0, e = Ivars.size(); i < e; i++) { |
| 6712 | ObjCIvarDecl *IvarDecl = Ivars[i]; |
| 6713 | if (i == 0) |
| 6714 | Result += "\t{{"; |
| 6715 | else |
| 6716 | Result += "\t {"; |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 6717 | Result += "(unsigned long int *)&"; |
| 6718 | WriteInternalIvarName(CDecl, IvarDecl, Result); |
Fariborz Jahanian | db64923 | 2012-02-13 20:59:02 +0000 | [diff] [blame] | 6719 | Result += ", "; |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6720 | |
| 6721 | Result += "\""; Result += IvarDecl->getName(); Result += "\", "; |
| 6722 | std::string IvarTypeString, QuoteIvarTypeString; |
| 6723 | Context->getObjCEncodingForType(IvarDecl->getType(), IvarTypeString, |
| 6724 | IvarDecl); |
| 6725 | RewriteObj.QuoteDoublequotes(IvarTypeString, QuoteIvarTypeString); |
| 6726 | Result += "\""; Result += QuoteIvarTypeString; Result += "\", "; |
| 6727 | |
Fariborz Jahanian | 8f1fed0 | 2012-02-11 20:10:52 +0000 | [diff] [blame] | 6728 | // FIXME. this alignment represents the host alignment and need be changed to |
| 6729 | // represent the target alignment. |
| 6730 | unsigned Align = Context->getTypeAlign(IvarDecl->getType())/8; |
| 6731 | Align = llvm::Log2_32(Align); |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6732 | Result += llvm::utostr(Align); Result += ", "; |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 6733 | CharUnits Size = Context->getTypeSizeInChars(IvarDecl->getType()); |
| 6734 | Result += llvm::utostr(Size.getQuantity()); |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6735 | if (i == e-1) |
| 6736 | Result += "}}\n"; |
| 6737 | else |
| 6738 | Result += "},\n"; |
| 6739 | } |
| 6740 | Result += "};\n"; |
| 6741 | } |
| 6742 | } |
| 6743 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6744 | /// RewriteObjCProtocolMetaData - Rewrite protocols meta-data. |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 6745 | void RewriteModernObjC::RewriteObjCProtocolMetaData(ObjCProtocolDecl *PDecl, |
| 6746 | std::string &Result) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6747 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6748 | // Do not synthesize the protocol more than once. |
| 6749 | if (ObjCSynthesizedProtocols.count(PDecl->getCanonicalDecl())) |
| 6750 | return; |
Fariborz Jahanian | de5d946 | 2012-03-14 18:09:23 +0000 | [diff] [blame] | 6751 | WriteModernMetadataDeclarations(Context, Result); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6752 | |
| 6753 | if (ObjCProtocolDecl *Def = PDecl->getDefinition()) |
| 6754 | PDecl = Def; |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 6755 | // Must write out all protocol definitions in current qualifier list, |
| 6756 | // and in their nested qualifiers before writing out current definition. |
| 6757 | for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(), |
| 6758 | E = PDecl->protocol_end(); I != E; ++I) |
| 6759 | RewriteObjCProtocolMetaData(*I, Result); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6760 | |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6761 | // Construct method lists. |
| 6762 | std::vector<ObjCMethodDecl *> InstanceMethods, ClassMethods; |
| 6763 | std::vector<ObjCMethodDecl *> OptInstanceMethods, OptClassMethods; |
| 6764 | for (ObjCProtocolDecl::instmeth_iterator |
| 6765 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
| 6766 | I != E; ++I) { |
| 6767 | ObjCMethodDecl *MD = *I; |
| 6768 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 6769 | OptInstanceMethods.push_back(MD); |
| 6770 | } else { |
| 6771 | InstanceMethods.push_back(MD); |
| 6772 | } |
| 6773 | } |
| 6774 | |
| 6775 | for (ObjCProtocolDecl::classmeth_iterator |
| 6776 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
| 6777 | I != E; ++I) { |
| 6778 | ObjCMethodDecl *MD = *I; |
| 6779 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 6780 | OptClassMethods.push_back(MD); |
| 6781 | } else { |
| 6782 | ClassMethods.push_back(MD); |
| 6783 | } |
| 6784 | } |
Fariborz Jahanian | e0adbd8 | 2012-02-08 22:23:26 +0000 | [diff] [blame] | 6785 | std::vector<ObjCMethodDecl *> AllMethods; |
| 6786 | for (unsigned i = 0, e = InstanceMethods.size(); i < e; i++) |
| 6787 | AllMethods.push_back(InstanceMethods[i]); |
| 6788 | for (unsigned i = 0, e = ClassMethods.size(); i < e; i++) |
| 6789 | AllMethods.push_back(ClassMethods[i]); |
| 6790 | for (unsigned i = 0, e = OptInstanceMethods.size(); i < e; i++) |
| 6791 | AllMethods.push_back(OptInstanceMethods[i]); |
| 6792 | for (unsigned i = 0, e = OptClassMethods.size(); i < e; i++) |
| 6793 | AllMethods.push_back(OptClassMethods[i]); |
| 6794 | |
| 6795 | Write__extendedMethodTypes_initializer(*this, Context, Result, |
| 6796 | AllMethods, |
| 6797 | "_OBJC_PROTOCOL_METHOD_TYPES_", |
| 6798 | PDecl->getNameAsString()); |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 6799 | // Protocol's super protocol list |
| 6800 | std::vector<ObjCProtocolDecl *> SuperProtocols; |
| 6801 | for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(), |
| 6802 | E = PDecl->protocol_end(); I != E; ++I) |
| 6803 | SuperProtocols.push_back(*I); |
| 6804 | |
| 6805 | Write_protocol_list_initializer(Context, Result, SuperProtocols, |
| 6806 | "_OBJC_PROTOCOL_REFS_", |
| 6807 | PDecl->getNameAsString()); |
| 6808 | |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6809 | Write_method_list_t_initializer(*this, Context, Result, InstanceMethods, |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6810 | "_OBJC_PROTOCOL_INSTANCE_METHODS_", |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6811 | PDecl->getNameAsString(), false); |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6812 | |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6813 | Write_method_list_t_initializer(*this, Context, Result, ClassMethods, |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6814 | "_OBJC_PROTOCOL_CLASS_METHODS_", |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6815 | PDecl->getNameAsString(), false); |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6816 | |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6817 | Write_method_list_t_initializer(*this, Context, Result, OptInstanceMethods, |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6818 | "_OBJC_PROTOCOL_OPT_INSTANCE_METHODS_", |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6819 | PDecl->getNameAsString(), false); |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6820 | |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6821 | Write_method_list_t_initializer(*this, Context, Result, OptClassMethods, |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6822 | "_OBJC_PROTOCOL_OPT_CLASS_METHODS_", |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6823 | PDecl->getNameAsString(), false); |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6824 | |
Fariborz Jahanian | da35eac | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 6825 | // Protocol's property metadata. |
| 6826 | std::vector<ObjCPropertyDecl *> ProtocolProperties; |
| 6827 | for (ObjCContainerDecl::prop_iterator I = PDecl->prop_begin(), |
| 6828 | E = PDecl->prop_end(); I != E; ++I) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 6829 | ProtocolProperties.push_back(*I); |
Fariborz Jahanian | da35eac | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 6830 | |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6831 | Write_prop_list_t_initializer(*this, Context, Result, ProtocolProperties, |
Fariborz Jahanian | da35eac | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 6832 | /* Container */0, |
| 6833 | "_OBJC_PROTOCOL_PROPERTIES_", |
| 6834 | PDecl->getNameAsString()); |
Fariborz Jahanian | da35eac | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 6835 | |
Fariborz Jahanian | 82848c2 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6836 | // Writer out root metadata for current protocol: struct _protocol_t |
Fariborz Jahanian | 1ca052c | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 6837 | Result += "\n"; |
| 6838 | if (LangOpts.MicrosoftExt) |
Fariborz Jahanian | 8590d86 | 2012-04-14 17:13:08 +0000 | [diff] [blame] | 6839 | Result += "static "; |
Fariborz Jahanian | 30650eb | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 6840 | Result += "struct _protocol_t _OBJC_PROTOCOL_"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6841 | Result += PDecl->getNameAsString(); |
Fariborz Jahanian | 82848c2 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6842 | Result += " __attribute__ ((used, section (\"__DATA,__datacoal_nt,coalesced\"))) = {\n"; |
| 6843 | Result += "\t0,\n"; // id is; is null |
| 6844 | Result += "\t\""; Result += PDecl->getNameAsString(); Result += "\",\n"; |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 6845 | if (SuperProtocols.size() > 0) { |
| 6846 | Result += "\t(const struct _protocol_list_t *)&"; Result += "_OBJC_PROTOCOL_REFS_"; |
| 6847 | Result += PDecl->getNameAsString(); Result += ",\n"; |
| 6848 | } |
| 6849 | else |
| 6850 | Result += "\t0,\n"; |
Fariborz Jahanian | 82848c2 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6851 | if (InstanceMethods.size() > 0) { |
| 6852 | Result += "\t(const struct method_list_t *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 6853 | Result += PDecl->getNameAsString(); Result += ",\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6854 | } |
| 6855 | else |
Fariborz Jahanian | 82848c2 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6856 | Result += "\t0,\n"; |
| 6857 | |
| 6858 | if (ClassMethods.size() > 0) { |
| 6859 | Result += "\t(const struct method_list_t *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
| 6860 | Result += PDecl->getNameAsString(); Result += ",\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6861 | } |
| 6862 | else |
Fariborz Jahanian | 82848c2 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6863 | Result += "\t0,\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6864 | |
Fariborz Jahanian | 82848c2 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6865 | if (OptInstanceMethods.size() > 0) { |
| 6866 | Result += "\t(const struct method_list_t *)&_OBJC_PROTOCOL_OPT_INSTANCE_METHODS_"; |
| 6867 | Result += PDecl->getNameAsString(); Result += ",\n"; |
| 6868 | } |
| 6869 | else |
| 6870 | Result += "\t0,\n"; |
| 6871 | |
| 6872 | if (OptClassMethods.size() > 0) { |
| 6873 | Result += "\t(const struct method_list_t *)&_OBJC_PROTOCOL_OPT_CLASS_METHODS_"; |
| 6874 | Result += PDecl->getNameAsString(); Result += ",\n"; |
| 6875 | } |
| 6876 | else |
| 6877 | Result += "\t0,\n"; |
| 6878 | |
| 6879 | if (ProtocolProperties.size() > 0) { |
| 6880 | Result += "\t(const struct _prop_list_t *)&_OBJC_PROTOCOL_PROPERTIES_"; |
| 6881 | Result += PDecl->getNameAsString(); Result += ",\n"; |
| 6882 | } |
| 6883 | else |
| 6884 | Result += "\t0,\n"; |
| 6885 | |
| 6886 | Result += "\t"; Result += "sizeof(_protocol_t)"; Result += ",\n"; |
| 6887 | Result += "\t0,\n"; |
| 6888 | |
Fariborz Jahanian | e0adbd8 | 2012-02-08 22:23:26 +0000 | [diff] [blame] | 6889 | if (AllMethods.size() > 0) { |
| 6890 | Result += "\t(const char **)&"; Result += "_OBJC_PROTOCOL_METHOD_TYPES_"; |
| 6891 | Result += PDecl->getNameAsString(); |
| 6892 | Result += "\n};\n"; |
| 6893 | } |
| 6894 | else |
| 6895 | Result += "\t0\n};\n"; |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 6896 | |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 6897 | if (LangOpts.MicrosoftExt) |
Fariborz Jahanian | 8590d86 | 2012-04-14 17:13:08 +0000 | [diff] [blame] | 6898 | Result += "static "; |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 6899 | Result += "struct _protocol_t *"; |
| 6900 | Result += "_OBJC_LABEL_PROTOCOL_$_"; Result += PDecl->getNameAsString(); |
| 6901 | Result += " = &_OBJC_PROTOCOL_"; Result += PDecl->getNameAsString(); |
| 6902 | Result += ";\n"; |
Fariborz Jahanian | 82848c2 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6903 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6904 | // Mark this protocol as having been generated. |
| 6905 | if (!ObjCSynthesizedProtocols.insert(PDecl->getCanonicalDecl())) |
| 6906 | llvm_unreachable("protocol already synthesized"); |
| 6907 | |
| 6908 | } |
| 6909 | |
| 6910 | void RewriteModernObjC::RewriteObjCProtocolListMetaData( |
| 6911 | const ObjCList<ObjCProtocolDecl> &Protocols, |
| 6912 | StringRef prefix, StringRef ClassName, |
| 6913 | std::string &Result) { |
| 6914 | if (Protocols.empty()) return; |
| 6915 | |
| 6916 | for (unsigned i = 0; i != Protocols.size(); i++) |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 6917 | RewriteObjCProtocolMetaData(Protocols[i], Result); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6918 | |
| 6919 | // Output the top lovel protocol meta-data for the class. |
| 6920 | /* struct _objc_protocol_list { |
| 6921 | struct _objc_protocol_list *next; |
| 6922 | int protocol_count; |
| 6923 | struct _objc_protocol *class_protocols[]; |
| 6924 | } |
| 6925 | */ |
Fariborz Jahanian | 1ca052c | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 6926 | Result += "\n"; |
| 6927 | if (LangOpts.MicrosoftExt) |
| 6928 | Result += "__declspec(allocate(\".cat_cls_meth$B\")) "; |
| 6929 | Result += "static struct {\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6930 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 6931 | Result += "\tint protocol_count;\n"; |
| 6932 | Result += "\tstruct _objc_protocol *class_protocols["; |
| 6933 | Result += utostr(Protocols.size()); |
| 6934 | Result += "];\n} _OBJC_"; |
| 6935 | Result += prefix; |
| 6936 | Result += "_PROTOCOLS_"; |
| 6937 | Result += ClassName; |
| 6938 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 6939 | "{\n\t0, "; |
| 6940 | Result += utostr(Protocols.size()); |
| 6941 | Result += "\n"; |
| 6942 | |
| 6943 | Result += "\t,{&_OBJC_PROTOCOL_"; |
| 6944 | Result += Protocols[0]->getNameAsString(); |
| 6945 | Result += " \n"; |
| 6946 | |
| 6947 | for (unsigned i = 1; i != Protocols.size(); i++) { |
| 6948 | Result += "\t ,&_OBJC_PROTOCOL_"; |
| 6949 | Result += Protocols[i]->getNameAsString(); |
| 6950 | Result += "\n"; |
| 6951 | } |
| 6952 | Result += "\t }\n};\n"; |
| 6953 | } |
| 6954 | |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 6955 | /// hasObjCExceptionAttribute - Return true if this class or any super |
| 6956 | /// class has the __objc_exception__ attribute. |
| 6957 | /// FIXME. Move this to ASTContext.cpp as it is also used for IRGen. |
| 6958 | static bool hasObjCExceptionAttribute(ASTContext &Context, |
| 6959 | const ObjCInterfaceDecl *OID) { |
| 6960 | if (OID->hasAttr<ObjCExceptionAttr>()) |
| 6961 | return true; |
| 6962 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
| 6963 | return hasObjCExceptionAttribute(Context, Super); |
| 6964 | return false; |
| 6965 | } |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6966 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6967 | void RewriteModernObjC::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
| 6968 | std::string &Result) { |
| 6969 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
| 6970 | |
| 6971 | // Explicitly declared @interface's are already synthesized. |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6972 | if (CDecl->isImplicitInterfaceDecl()) |
| 6973 | assert(false && |
| 6974 | "Legacy implicit interface rewriting not supported in moder abi"); |
Fariborz Jahanian | 8f1fed0 | 2012-02-11 20:10:52 +0000 | [diff] [blame] | 6975 | |
Fariborz Jahanian | de5d946 | 2012-03-14 18:09:23 +0000 | [diff] [blame] | 6976 | WriteModernMetadataDeclarations(Context, Result); |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6977 | SmallVector<ObjCIvarDecl *, 8> IVars; |
| 6978 | |
| 6979 | for (ObjCIvarDecl *IVD = CDecl->all_declared_ivar_begin(); |
| 6980 | IVD; IVD = IVD->getNextIvar()) { |
| 6981 | // Ignore unnamed bit-fields. |
| 6982 | if (!IVD->getDeclName()) |
| 6983 | continue; |
| 6984 | IVars.push_back(IVD); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6985 | } |
| 6986 | |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6987 | Write__ivar_list_t_initializer(*this, Context, Result, IVars, |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6988 | "_OBJC_$_INSTANCE_VARIABLES_", |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 6989 | CDecl); |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6990 | |
| 6991 | // Build _objc_method_list for class's instance methods if needed |
| 6992 | SmallVector<ObjCMethodDecl *, 32> |
| 6993 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
| 6994 | |
| 6995 | // If any of our property implementations have associated getters or |
| 6996 | // setters, produce metadata for them as well. |
| 6997 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 6998 | PropEnd = IDecl->propimpl_end(); |
| 6999 | Prop != PropEnd; ++Prop) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7000 | if (Prop->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 7001 | continue; |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7002 | if (!Prop->getPropertyIvarDecl()) |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 7003 | continue; |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7004 | ObjCPropertyDecl *PD = Prop->getPropertyDecl(); |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 7005 | if (!PD) |
| 7006 | continue; |
| 7007 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
Fariborz Jahanian | 301e2e4 | 2012-05-03 22:52:13 +0000 | [diff] [blame] | 7008 | if (mustSynthesizeSetterGetterMethod(IDecl, PD, true /*getter*/)) |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 7009 | InstanceMethods.push_back(Getter); |
| 7010 | if (PD->isReadOnly()) |
| 7011 | continue; |
| 7012 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
Fariborz Jahanian | 301e2e4 | 2012-05-03 22:52:13 +0000 | [diff] [blame] | 7013 | if (mustSynthesizeSetterGetterMethod(IDecl, PD, false /*setter*/)) |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 7014 | InstanceMethods.push_back(Setter); |
| 7015 | } |
| 7016 | |
| 7017 | Write_method_list_t_initializer(*this, Context, Result, InstanceMethods, |
| 7018 | "_OBJC_$_INSTANCE_METHODS_", |
| 7019 | IDecl->getNameAsString(), true); |
| 7020 | |
| 7021 | SmallVector<ObjCMethodDecl *, 32> |
| 7022 | ClassMethods(IDecl->classmeth_begin(), IDecl->classmeth_end()); |
| 7023 | |
| 7024 | Write_method_list_t_initializer(*this, Context, Result, ClassMethods, |
| 7025 | "_OBJC_$_CLASS_METHODS_", |
| 7026 | IDecl->getNameAsString(), true); |
Fariborz Jahanian | 0a52534 | 2012-02-14 19:31:35 +0000 | [diff] [blame] | 7027 | |
| 7028 | // Protocols referenced in class declaration? |
| 7029 | // Protocol's super protocol list |
| 7030 | std::vector<ObjCProtocolDecl *> RefedProtocols; |
| 7031 | const ObjCList<ObjCProtocolDecl> &Protocols = CDecl->getReferencedProtocols(); |
| 7032 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 7033 | E = Protocols.end(); |
| 7034 | I != E; ++I) { |
| 7035 | RefedProtocols.push_back(*I); |
| 7036 | // Must write out all protocol definitions in current qualifier list, |
| 7037 | // and in their nested qualifiers before writing out current definition. |
| 7038 | RewriteObjCProtocolMetaData(*I, Result); |
| 7039 | } |
| 7040 | |
| 7041 | Write_protocol_list_initializer(Context, Result, |
| 7042 | RefedProtocols, |
| 7043 | "_OBJC_CLASS_PROTOCOLS_$_", |
| 7044 | IDecl->getNameAsString()); |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 7045 | |
| 7046 | // Protocol's property metadata. |
| 7047 | std::vector<ObjCPropertyDecl *> ClassProperties; |
| 7048 | for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(), |
| 7049 | E = CDecl->prop_end(); I != E; ++I) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 7050 | ClassProperties.push_back(*I); |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 7051 | |
| 7052 | Write_prop_list_t_initializer(*this, Context, Result, ClassProperties, |
Fariborz Jahanian | 2df089d | 2012-03-22 17:39:35 +0000 | [diff] [blame] | 7053 | /* Container */IDecl, |
Fariborz Jahanian | eeabf38 | 2012-02-16 21:57:59 +0000 | [diff] [blame] | 7054 | "_OBJC_$_PROP_LIST_", |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 7055 | CDecl->getNameAsString()); |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 7056 | |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 7057 | |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 7058 | // Data for initializing _class_ro_t metaclass meta-data |
| 7059 | uint32_t flags = CLS_META; |
| 7060 | std::string InstanceSize; |
| 7061 | std::string InstanceStart; |
| 7062 | |
| 7063 | |
| 7064 | bool classIsHidden = CDecl->getVisibility() == HiddenVisibility; |
| 7065 | if (classIsHidden) |
| 7066 | flags |= OBJC2_CLS_HIDDEN; |
| 7067 | |
| 7068 | if (!CDecl->getSuperClass()) |
| 7069 | // class is root |
| 7070 | flags |= CLS_ROOT; |
| 7071 | InstanceSize = "sizeof(struct _class_t)"; |
| 7072 | InstanceStart = InstanceSize; |
| 7073 | Write__class_ro_t_initializer(Context, Result, flags, |
| 7074 | InstanceStart, InstanceSize, |
| 7075 | ClassMethods, |
| 7076 | 0, |
| 7077 | 0, |
| 7078 | 0, |
| 7079 | "_OBJC_METACLASS_RO_$_", |
| 7080 | CDecl->getNameAsString()); |
| 7081 | |
| 7082 | |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 7083 | // Data for initializing _class_ro_t meta-data |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 7084 | flags = CLS; |
| 7085 | if (classIsHidden) |
| 7086 | flags |= OBJC2_CLS_HIDDEN; |
| 7087 | |
| 7088 | if (hasObjCExceptionAttribute(*Context, CDecl)) |
| 7089 | flags |= CLS_EXCEPTION; |
| 7090 | |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 7091 | if (!CDecl->getSuperClass()) |
| 7092 | // class is root |
| 7093 | flags |= CLS_ROOT; |
| 7094 | |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 7095 | InstanceSize.clear(); |
| 7096 | InstanceStart.clear(); |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 7097 | if (!ObjCSynthesizedStructs.count(CDecl)) { |
| 7098 | InstanceSize = "0"; |
| 7099 | InstanceStart = "0"; |
| 7100 | } |
| 7101 | else { |
| 7102 | InstanceSize = "sizeof(struct "; |
| 7103 | InstanceSize += CDecl->getNameAsString(); |
| 7104 | InstanceSize += "_IMPL)"; |
| 7105 | |
| 7106 | ObjCIvarDecl *IVD = CDecl->all_declared_ivar_begin(); |
| 7107 | if (IVD) { |
Fariborz Jahanian | acee1c9 | 2012-04-11 21:12:36 +0000 | [diff] [blame] | 7108 | RewriteIvarOffsetComputation(IVD, InstanceStart); |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 7109 | } |
| 7110 | else |
| 7111 | InstanceStart = InstanceSize; |
| 7112 | } |
| 7113 | Write__class_ro_t_initializer(Context, Result, flags, |
| 7114 | InstanceStart, InstanceSize, |
| 7115 | InstanceMethods, |
| 7116 | RefedProtocols, |
| 7117 | IVars, |
| 7118 | ClassProperties, |
| 7119 | "_OBJC_CLASS_RO_$_", |
| 7120 | CDecl->getNameAsString()); |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 7121 | |
| 7122 | Write_class_t(Context, Result, |
| 7123 | "OBJC_METACLASS_$_", |
| 7124 | CDecl, /*metaclass*/true); |
| 7125 | |
| 7126 | Write_class_t(Context, Result, |
| 7127 | "OBJC_CLASS_$_", |
| 7128 | CDecl, /*metaclass*/false); |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 7129 | |
| 7130 | if (ImplementationIsNonLazy(IDecl)) |
| 7131 | DefinedNonLazyClasses.push_back(CDecl); |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 7132 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7133 | } |
| 7134 | |
Fariborz Jahanian | e033578 | 2012-03-27 18:41:05 +0000 | [diff] [blame] | 7135 | void RewriteModernObjC::RewriteClassSetupInitHook(std::string &Result) { |
| 7136 | int ClsDefCount = ClassImplementation.size(); |
| 7137 | if (!ClsDefCount) |
| 7138 | return; |
| 7139 | Result += "#pragma section(\".objc_inithooks$B\", long, read, write)\n"; |
| 7140 | Result += "__declspec(allocate(\".objc_inithooks$B\")) "; |
| 7141 | Result += "static void *OBJC_CLASS_SETUP[] = {\n"; |
| 7142 | for (int i = 0; i < ClsDefCount; i++) { |
| 7143 | ObjCImplementationDecl *IDecl = ClassImplementation[i]; |
| 7144 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
| 7145 | Result += "\t(void *)&OBJC_CLASS_SETUP_$_"; |
| 7146 | Result += CDecl->getName(); Result += ",\n"; |
| 7147 | } |
| 7148 | Result += "};\n"; |
| 7149 | } |
| 7150 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7151 | void RewriteModernObjC::RewriteMetaDataIntoBuffer(std::string &Result) { |
| 7152 | int ClsDefCount = ClassImplementation.size(); |
| 7153 | int CatDefCount = CategoryImplementation.size(); |
| 7154 | |
| 7155 | // For each implemented class, write out all its meta data. |
| 7156 | for (int i = 0; i < ClsDefCount; i++) |
| 7157 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
| 7158 | |
Fariborz Jahanian | e033578 | 2012-03-27 18:41:05 +0000 | [diff] [blame] | 7159 | RewriteClassSetupInitHook(Result); |
| 7160 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7161 | // For each implemented category, write out all its meta data. |
| 7162 | for (int i = 0; i < CatDefCount; i++) |
| 7163 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
| 7164 | |
Fariborz Jahanian | e033578 | 2012-03-27 18:41:05 +0000 | [diff] [blame] | 7165 | RewriteCategorySetupInitHook(Result); |
| 7166 | |
Fariborz Jahanian | df79567 | 2012-02-17 00:06:14 +0000 | [diff] [blame] | 7167 | if (ClsDefCount > 0) { |
Fariborz Jahanian | 1ca052c | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 7168 | if (LangOpts.MicrosoftExt) |
| 7169 | Result += "__declspec(allocate(\".objc_classlist$B\")) "; |
Fariborz Jahanian | df79567 | 2012-02-17 00:06:14 +0000 | [diff] [blame] | 7170 | Result += "static struct _class_t *L_OBJC_LABEL_CLASS_$ ["; |
| 7171 | Result += llvm::utostr(ClsDefCount); Result += "]"; |
| 7172 | Result += |
| 7173 | " __attribute__((used, section (\"__DATA, __objc_classlist," |
| 7174 | "regular,no_dead_strip\")))= {\n"; |
| 7175 | for (int i = 0; i < ClsDefCount; i++) { |
| 7176 | Result += "\t&OBJC_CLASS_$_"; |
| 7177 | Result += ClassImplementation[i]->getNameAsString(); |
| 7178 | Result += ",\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7179 | } |
Fariborz Jahanian | df79567 | 2012-02-17 00:06:14 +0000 | [diff] [blame] | 7180 | Result += "};\n"; |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 7181 | |
| 7182 | if (!DefinedNonLazyClasses.empty()) { |
| 7183 | if (LangOpts.MicrosoftExt) |
| 7184 | Result += "__declspec(allocate(\".objc_nlclslist$B\")) \n"; |
| 7185 | Result += "static struct _class_t *_OBJC_LABEL_NONLAZY_CLASS_$[] = {\n\t"; |
| 7186 | for (unsigned i = 0, e = DefinedNonLazyClasses.size(); i < e; i++) { |
| 7187 | Result += "\t&OBJC_CLASS_$_"; Result += DefinedNonLazyClasses[i]->getNameAsString(); |
| 7188 | Result += ",\n"; |
| 7189 | } |
| 7190 | Result += "};\n"; |
| 7191 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7192 | } |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7193 | |
| 7194 | if (CatDefCount > 0) { |
Fariborz Jahanian | 1ca052c | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 7195 | if (LangOpts.MicrosoftExt) |
| 7196 | Result += "__declspec(allocate(\".objc_catlist$B\")) "; |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7197 | Result += "static struct _category_t *L_OBJC_LABEL_CATEGORY_$ ["; |
| 7198 | Result += llvm::utostr(CatDefCount); Result += "]"; |
| 7199 | Result += |
| 7200 | " __attribute__((used, section (\"__DATA, __objc_catlist," |
| 7201 | "regular,no_dead_strip\")))= {\n"; |
| 7202 | for (int i = 0; i < CatDefCount; i++) { |
| 7203 | Result += "\t&_OBJC_$_CATEGORY_"; |
| 7204 | Result += |
| 7205 | CategoryImplementation[i]->getClassInterface()->getNameAsString(); |
| 7206 | Result += "_$_"; |
| 7207 | Result += CategoryImplementation[i]->getNameAsString(); |
| 7208 | Result += ",\n"; |
| 7209 | } |
| 7210 | Result += "};\n"; |
| 7211 | } |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 7212 | |
| 7213 | if (!DefinedNonLazyCategories.empty()) { |
| 7214 | if (LangOpts.MicrosoftExt) |
| 7215 | Result += "__declspec(allocate(\".objc_nlcatlist$B\")) \n"; |
| 7216 | Result += "static struct _category_t *_OBJC_LABEL_NONLAZY_CATEGORY_$[] = {\n\t"; |
| 7217 | for (unsigned i = 0, e = DefinedNonLazyCategories.size(); i < e; i++) { |
| 7218 | Result += "\t&_OBJC_$_CATEGORY_"; |
| 7219 | Result += |
| 7220 | DefinedNonLazyCategories[i]->getClassInterface()->getNameAsString(); |
| 7221 | Result += "_$_"; |
| 7222 | Result += DefinedNonLazyCategories[i]->getNameAsString(); |
| 7223 | Result += ",\n"; |
| 7224 | } |
| 7225 | Result += "};\n"; |
| 7226 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7227 | } |
| 7228 | |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 7229 | void RewriteModernObjC::WriteImageInfo(std::string &Result) { |
| 7230 | if (LangOpts.MicrosoftExt) |
| 7231 | Result += "__declspec(allocate(\".objc_imageinfo$B\")) \n"; |
| 7232 | |
| 7233 | Result += "static struct IMAGE_INFO { unsigned version; unsigned flag; } "; |
| 7234 | // version 0, ObjCABI is 2 |
Fariborz Jahanian | 30650eb | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 7235 | Result += "_OBJC_IMAGE_INFO = { 0, 2 };\n"; |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 7236 | } |
| 7237 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7238 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
| 7239 | /// implementation. |
| 7240 | void RewriteModernObjC::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
| 7241 | std::string &Result) { |
Fariborz Jahanian | de5d946 | 2012-03-14 18:09:23 +0000 | [diff] [blame] | 7242 | WriteModernMetadataDeclarations(Context, Result); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7243 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
| 7244 | // Find category declaration for this implementation. |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7245 | ObjCCategoryDecl *CDecl=0; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7246 | for (CDecl = ClassDecl->getCategoryList(); CDecl; |
| 7247 | CDecl = CDecl->getNextClassCategory()) |
| 7248 | if (CDecl->getIdentifier() == IDecl->getIdentifier()) |
| 7249 | break; |
| 7250 | |
| 7251 | std::string FullCategoryName = ClassDecl->getNameAsString(); |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7252 | FullCategoryName += "_$_"; |
| 7253 | FullCategoryName += CDecl->getNameAsString(); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7254 | |
| 7255 | // Build _objc_method_list for class's instance methods if needed |
| 7256 | SmallVector<ObjCMethodDecl *, 32> |
| 7257 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
| 7258 | |
| 7259 | // If any of our property implementations have associated getters or |
| 7260 | // setters, produce metadata for them as well. |
| 7261 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 7262 | PropEnd = IDecl->propimpl_end(); |
| 7263 | Prop != PropEnd; ++Prop) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7264 | if (Prop->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7265 | continue; |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7266 | if (!Prop->getPropertyIvarDecl()) |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7267 | continue; |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7268 | ObjCPropertyDecl *PD = Prop->getPropertyDecl(); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7269 | if (!PD) |
| 7270 | continue; |
| 7271 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 7272 | InstanceMethods.push_back(Getter); |
| 7273 | if (PD->isReadOnly()) |
| 7274 | continue; |
| 7275 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 7276 | InstanceMethods.push_back(Setter); |
| 7277 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7278 | |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7279 | Write_method_list_t_initializer(*this, Context, Result, InstanceMethods, |
| 7280 | "_OBJC_$_CATEGORY_INSTANCE_METHODS_", |
| 7281 | FullCategoryName, true); |
| 7282 | |
| 7283 | SmallVector<ObjCMethodDecl *, 32> |
| 7284 | ClassMethods(IDecl->classmeth_begin(), IDecl->classmeth_end()); |
| 7285 | |
| 7286 | Write_method_list_t_initializer(*this, Context, Result, ClassMethods, |
| 7287 | "_OBJC_$_CATEGORY_CLASS_METHODS_", |
| 7288 | FullCategoryName, true); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7289 | |
| 7290 | // Protocols referenced in class declaration? |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7291 | // Protocol's super protocol list |
| 7292 | std::vector<ObjCProtocolDecl *> RefedProtocols; |
Argyrios Kyrtzidis | a5f4441 | 2012-03-13 01:09:41 +0000 | [diff] [blame] | 7293 | for (ObjCInterfaceDecl::protocol_iterator I = CDecl->protocol_begin(), |
| 7294 | E = CDecl->protocol_end(); |
| 7295 | |
| 7296 | I != E; ++I) { |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7297 | RefedProtocols.push_back(*I); |
| 7298 | // Must write out all protocol definitions in current qualifier list, |
| 7299 | // and in their nested qualifiers before writing out current definition. |
| 7300 | RewriteObjCProtocolMetaData(*I, Result); |
| 7301 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7302 | |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7303 | Write_protocol_list_initializer(Context, Result, |
| 7304 | RefedProtocols, |
| 7305 | "_OBJC_CATEGORY_PROTOCOLS_$_", |
| 7306 | FullCategoryName); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7307 | |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7308 | // Protocol's property metadata. |
| 7309 | std::vector<ObjCPropertyDecl *> ClassProperties; |
| 7310 | for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(), |
| 7311 | E = CDecl->prop_end(); I != E; ++I) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 7312 | ClassProperties.push_back(*I); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7313 | |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7314 | Write_prop_list_t_initializer(*this, Context, Result, ClassProperties, |
Fariborz Jahanian | ebfa272 | 2012-05-03 23:19:33 +0000 | [diff] [blame] | 7315 | /* Container */IDecl, |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7316 | "_OBJC_$_PROP_LIST_", |
| 7317 | FullCategoryName); |
| 7318 | |
| 7319 | Write_category_t(*this, Context, Result, |
Fariborz Jahanian | e033578 | 2012-03-27 18:41:05 +0000 | [diff] [blame] | 7320 | CDecl, |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 7321 | ClassDecl, |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7322 | InstanceMethods, |
| 7323 | ClassMethods, |
| 7324 | RefedProtocols, |
| 7325 | ClassProperties); |
| 7326 | |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 7327 | // Determine if this category is also "non-lazy". |
| 7328 | if (ImplementationIsNonLazy(IDecl)) |
| 7329 | DefinedNonLazyCategories.push_back(CDecl); |
Fariborz Jahanian | e033578 | 2012-03-27 18:41:05 +0000 | [diff] [blame] | 7330 | |
| 7331 | } |
| 7332 | |
| 7333 | void RewriteModernObjC::RewriteCategorySetupInitHook(std::string &Result) { |
| 7334 | int CatDefCount = CategoryImplementation.size(); |
| 7335 | if (!CatDefCount) |
| 7336 | return; |
| 7337 | Result += "#pragma section(\".objc_inithooks$B\", long, read, write)\n"; |
| 7338 | Result += "__declspec(allocate(\".objc_inithooks$B\")) "; |
| 7339 | Result += "static void *OBJC_CATEGORY_SETUP[] = {\n"; |
| 7340 | for (int i = 0; i < CatDefCount; i++) { |
| 7341 | ObjCCategoryImplDecl *IDecl = CategoryImplementation[i]; |
| 7342 | ObjCCategoryDecl *CatDecl= IDecl->getCategoryDecl(); |
| 7343 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
| 7344 | Result += "\t(void *)&OBJC_CATEGORY_SETUP_$_"; |
| 7345 | Result += ClassDecl->getName(); |
| 7346 | Result += "_$_"; |
| 7347 | Result += CatDecl->getName(); |
| 7348 | Result += ",\n"; |
| 7349 | } |
| 7350 | Result += "};\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7351 | } |
| 7352 | |
| 7353 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
| 7354 | /// class methods. |
| 7355 | template<typename MethodIterator> |
| 7356 | void RewriteModernObjC::RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 7357 | MethodIterator MethodEnd, |
| 7358 | bool IsInstanceMethod, |
| 7359 | StringRef prefix, |
| 7360 | StringRef ClassName, |
| 7361 | std::string &Result) { |
| 7362 | if (MethodBegin == MethodEnd) return; |
| 7363 | |
| 7364 | if (!objc_impl_method) { |
| 7365 | /* struct _objc_method { |
| 7366 | SEL _cmd; |
| 7367 | char *method_types; |
| 7368 | void *_imp; |
| 7369 | } |
| 7370 | */ |
| 7371 | Result += "\nstruct _objc_method {\n"; |
| 7372 | Result += "\tSEL _cmd;\n"; |
| 7373 | Result += "\tchar *method_types;\n"; |
| 7374 | Result += "\tvoid *_imp;\n"; |
| 7375 | Result += "};\n"; |
| 7376 | |
| 7377 | objc_impl_method = true; |
| 7378 | } |
| 7379 | |
| 7380 | // Build _objc_method_list for class's methods if needed |
| 7381 | |
| 7382 | /* struct { |
| 7383 | struct _objc_method_list *next_method; |
| 7384 | int method_count; |
| 7385 | struct _objc_method method_list[]; |
| 7386 | } |
| 7387 | */ |
| 7388 | unsigned NumMethods = std::distance(MethodBegin, MethodEnd); |
Fariborz Jahanian | 1ca052c | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 7389 | Result += "\n"; |
| 7390 | if (LangOpts.MicrosoftExt) { |
| 7391 | if (IsInstanceMethod) |
| 7392 | Result += "__declspec(allocate(\".inst_meth$B\")) "; |
| 7393 | else |
| 7394 | Result += "__declspec(allocate(\".cls_meth$B\")) "; |
| 7395 | } |
| 7396 | Result += "static struct {\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7397 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 7398 | Result += "\tint method_count;\n"; |
| 7399 | Result += "\tstruct _objc_method method_list["; |
| 7400 | Result += utostr(NumMethods); |
| 7401 | Result += "];\n} _OBJC_"; |
| 7402 | Result += prefix; |
| 7403 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 7404 | Result += "_METHODS_"; |
| 7405 | Result += ClassName; |
| 7406 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
| 7407 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 7408 | Result += "_meth\")))= "; |
| 7409 | Result += "{\n\t0, " + utostr(NumMethods) + "\n"; |
| 7410 | |
| 7411 | Result += "\t,{{(SEL)\""; |
| 7412 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
| 7413 | std::string MethodTypeString; |
| 7414 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
| 7415 | Result += "\", \""; |
| 7416 | Result += MethodTypeString; |
| 7417 | Result += "\", (void *)"; |
| 7418 | Result += MethodInternalNames[*MethodBegin]; |
| 7419 | Result += "}\n"; |
| 7420 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 7421 | Result += "\t ,{(SEL)\""; |
| 7422 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
| 7423 | std::string MethodTypeString; |
| 7424 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
| 7425 | Result += "\", \""; |
| 7426 | Result += MethodTypeString; |
| 7427 | Result += "\", (void *)"; |
| 7428 | Result += MethodInternalNames[*MethodBegin]; |
| 7429 | Result += "}\n"; |
| 7430 | } |
| 7431 | Result += "\t }\n};\n"; |
| 7432 | } |
| 7433 | |
| 7434 | Stmt *RewriteModernObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV) { |
| 7435 | SourceRange OldRange = IV->getSourceRange(); |
| 7436 | Expr *BaseExpr = IV->getBase(); |
| 7437 | |
| 7438 | // Rewrite the base, but without actually doing replaces. |
| 7439 | { |
| 7440 | DisableReplaceStmtScope S(*this); |
| 7441 | BaseExpr = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(BaseExpr)); |
| 7442 | IV->setBase(BaseExpr); |
| 7443 | } |
| 7444 | |
| 7445 | ObjCIvarDecl *D = IV->getDecl(); |
| 7446 | |
| 7447 | Expr *Replacement = IV; |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7448 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7449 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
| 7450 | const ObjCInterfaceType *iFaceDecl = |
Fariborz Jahanian | 163d3ce | 2012-05-08 23:54:35 +0000 | [diff] [blame] | 7451 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7452 | assert(iFaceDecl && "RewriteObjCIvarRefExpr - iFaceDecl is null"); |
| 7453 | // lookup which class implements the instance variable. |
| 7454 | ObjCInterfaceDecl *clsDeclared = 0; |
| 7455 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
| 7456 | clsDeclared); |
| 7457 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
| 7458 | |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7459 | // Build name of symbol holding ivar offset. |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 7460 | std::string IvarOffsetName; |
| 7461 | WriteInternalIvarName(clsDeclared, D, IvarOffsetName); |
| 7462 | |
Fariborz Jahanian | 72c88f1 | 2012-02-22 18:13:25 +0000 | [diff] [blame] | 7463 | ReferencedIvars[clsDeclared].insert(D); |
| 7464 | |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7465 | // cast offset to "char *". |
| 7466 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, |
| 7467 | Context->getPointerType(Context->CharTy), |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7468 | CK_BitCast, |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7469 | BaseExpr); |
| 7470 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
| 7471 | SourceLocation(), &Context->Idents.get(IvarOffsetName), |
| 7472 | Context->UnsignedLongTy, 0, SC_Extern, SC_None); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 7473 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, false, |
| 7474 | Context->UnsignedLongTy, VK_LValue, |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7475 | SourceLocation()); |
| 7476 | BinaryOperator *addExpr = |
| 7477 | new (Context) BinaryOperator(castExpr, DRE, BO_Add, |
| 7478 | Context->getPointerType(Context->CharTy), |
| 7479 | VK_RValue, OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7480 | // Don't forget the parens to enforce the proper binding. |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7481 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), |
| 7482 | SourceLocation(), |
| 7483 | addExpr); |
Fariborz Jahanian | 0d6e22a | 2012-02-24 17:35:35 +0000 | [diff] [blame] | 7484 | QualType IvarT = D->getType(); |
Fariborz Jahanian | 27fc81b | 2012-04-27 22:48:54 +0000 | [diff] [blame] | 7485 | |
Fariborz Jahanian | f5eac48 | 2012-05-02 17:34:59 +0000 | [diff] [blame] | 7486 | if (!isa<TypedefType>(IvarT) && IvarT->isRecordType()) { |
Fariborz Jahanian | 27fc81b | 2012-04-27 22:48:54 +0000 | [diff] [blame] | 7487 | RecordDecl *RD = IvarT->getAs<RecordType>()->getDecl(); |
Fariborz Jahanian | 8fba894 | 2012-04-30 23:20:30 +0000 | [diff] [blame] | 7488 | RD = RD->getDefinition(); |
| 7489 | if (RD && !RD->getDeclName().getAsIdentifierInfo()) { |
Fariborz Jahanian | 27fc81b | 2012-04-27 22:48:54 +0000 | [diff] [blame] | 7490 | // decltype(((Foo_IMPL*)0)->bar) * |
Fariborz Jahanian | f5eac48 | 2012-05-02 17:34:59 +0000 | [diff] [blame] | 7491 | ObjCContainerDecl *CDecl = |
| 7492 | dyn_cast<ObjCContainerDecl>(D->getDeclContext()); |
| 7493 | // ivar in class extensions requires special treatment. |
| 7494 | if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) |
| 7495 | CDecl = CatDecl->getClassInterface(); |
| 7496 | std::string RecName = CDecl->getName(); |
Fariborz Jahanian | 27fc81b | 2012-04-27 22:48:54 +0000 | [diff] [blame] | 7497 | RecName += "_IMPL"; |
| 7498 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 7499 | SourceLocation(), SourceLocation(), |
| 7500 | &Context->Idents.get(RecName.c_str())); |
| 7501 | QualType PtrStructIMPL = Context->getPointerType(Context->getTagDeclType(RD)); |
| 7502 | unsigned UnsignedIntSize = |
| 7503 | static_cast<unsigned>(Context->getTypeSize(Context->UnsignedIntTy)); |
| 7504 | Expr *Zero = IntegerLiteral::Create(*Context, |
| 7505 | llvm::APInt(UnsignedIntSize, 0), |
| 7506 | Context->UnsignedIntTy, SourceLocation()); |
| 7507 | Zero = NoTypeInfoCStyleCastExpr(Context, PtrStructIMPL, CK_BitCast, Zero); |
| 7508 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 7509 | Zero); |
| 7510 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 7511 | SourceLocation(), |
| 7512 | &Context->Idents.get(D->getNameAsString()), |
| 7513 | IvarT, 0, |
| 7514 | /*BitWidth=*/0, /*Mutable=*/true, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 7515 | ICIS_NoInit); |
Fariborz Jahanian | 27fc81b | 2012-04-27 22:48:54 +0000 | [diff] [blame] | 7516 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
| 7517 | FD->getType(), VK_LValue, |
| 7518 | OK_Ordinary); |
| 7519 | IvarT = Context->getDecltypeType(ME, ME->getType()); |
| 7520 | } |
| 7521 | } |
Fariborz Jahanian | 8e0913d | 2012-02-29 00:26:20 +0000 | [diff] [blame] | 7522 | convertObjCTypeToCStyleType(IvarT); |
Fariborz Jahanian | 0d6e22a | 2012-02-24 17:35:35 +0000 | [diff] [blame] | 7523 | QualType castT = Context->getPointerType(IvarT); |
Fariborz Jahanian | 27fc81b | 2012-04-27 22:48:54 +0000 | [diff] [blame] | 7524 | |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7525 | castExpr = NoTypeInfoCStyleCastExpr(Context, |
| 7526 | castT, |
| 7527 | CK_BitCast, |
| 7528 | PE); |
Fariborz Jahanian | 27fc81b | 2012-04-27 22:48:54 +0000 | [diff] [blame] | 7529 | |
| 7530 | |
Fariborz Jahanian | 8e0913d | 2012-02-29 00:26:20 +0000 | [diff] [blame] | 7531 | Expr *Exp = new (Context) UnaryOperator(castExpr, UO_Deref, IvarT, |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7532 | VK_LValue, OK_Ordinary, |
| 7533 | SourceLocation()); |
| 7534 | PE = new (Context) ParenExpr(OldRange.getBegin(), |
| 7535 | OldRange.getEnd(), |
| 7536 | Exp); |
| 7537 | |
| 7538 | Replacement = PE; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7539 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7540 | |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7541 | ReplaceStmtWithRange(IV, Replacement, OldRange); |
| 7542 | return Replacement; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7543 | } |