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 | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 112 | llvm::SmallPtrSet<TagDecl*, 8> TagsDefinedInIvarDecls; |
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); |
| 310 | |
| 311 | // Expression Rewriting. |
| 312 | Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S); |
| 313 | Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp); |
| 314 | Stmt *RewritePropertyOrImplicitGetter(PseudoObjectExpr *Pseudo); |
| 315 | Stmt *RewritePropertyOrImplicitSetter(PseudoObjectExpr *Pseudo); |
| 316 | Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp); |
| 317 | Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp); |
| 318 | Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp); |
Fariborz Jahanian | 5594704 | 2012-03-27 20:17:30 +0000 | [diff] [blame] | 319 | Stmt *RewriteObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Exp); |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 320 | Stmt *RewriteObjCBoxedExpr(ObjCBoxedExpr *Exp); |
Fariborz Jahanian | 86cff60 | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 321 | Stmt *RewriteObjCArrayLiteralExpr(ObjCArrayLiteral *Exp); |
Fariborz Jahanian | e35abe1 | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 322 | Stmt *RewriteObjCDictionaryLiteralExpr(ObjCDictionaryLiteral *Exp); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 323 | Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 324 | Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S); |
| 325 | Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
| 326 | Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S); |
| 327 | Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
| 328 | SourceLocation OrigEnd); |
| 329 | Stmt *RewriteBreakStmt(BreakStmt *S); |
| 330 | Stmt *RewriteContinueStmt(ContinueStmt *S); |
| 331 | void RewriteCastExpr(CStyleCastExpr *CE); |
Fariborz Jahanian | f1ee687 | 2012-04-10 00:08:18 +0000 | [diff] [blame] | 332 | void RewriteImplicitCastObjCExpr(CastExpr *IE); |
Fariborz Jahanian | b3f904f | 2012-04-03 17:35:38 +0000 | [diff] [blame] | 333 | void RewriteLinkageSpec(LinkageSpecDecl *LSD); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 334 | |
| 335 | // Block rewriting. |
| 336 | void RewriteBlocksInFunctionProtoType(QualType funcType, NamedDecl *D); |
| 337 | |
| 338 | // Block specific rewrite rules. |
| 339 | void RewriteBlockPointerDecl(NamedDecl *VD); |
Fariborz Jahanian | 4fe261c | 2012-04-24 19:38:45 +0000 | [diff] [blame] | 340 | void RewriteByRefVar(VarDecl *VD, bool firstDecl, bool lastDecl); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 341 | Stmt *RewriteBlockDeclRefExpr(DeclRefExpr *VD); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 342 | Stmt *RewriteLocalVariableExternalStorage(DeclRefExpr *DRE); |
| 343 | void RewriteBlockPointerFunctionArgs(FunctionDecl *FD); |
| 344 | |
| 345 | void RewriteObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
| 346 | std::string &Result); |
| 347 | |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 348 | void RewriteObjCFieldDecl(FieldDecl *fieldDecl, std::string &Result); |
| 349 | |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 350 | bool RewriteObjCFieldDeclType(QualType &Type, std::string &Result); |
| 351 | |
Fariborz Jahanian | 72c88f1 | 2012-02-22 18:13:25 +0000 | [diff] [blame] | 352 | void RewriteIvarOffsetSymbols(ObjCInterfaceDecl *CDecl, |
| 353 | std::string &Result); |
| 354 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 355 | virtual void Initialize(ASTContext &context); |
| 356 | |
| 357 | // Misc. AST transformation routines. Somtimes they end up calling |
| 358 | // rewriting routines on the new ASTs. |
| 359 | CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD, |
| 360 | Expr **args, unsigned nargs, |
| 361 | SourceLocation StartLoc=SourceLocation(), |
| 362 | SourceLocation EndLoc=SourceLocation()); |
| 363 | |
| 364 | Stmt *SynthMessageExpr(ObjCMessageExpr *Exp, |
| 365 | SourceLocation StartLoc=SourceLocation(), |
| 366 | SourceLocation EndLoc=SourceLocation()); |
| 367 | |
| 368 | void SynthCountByEnumWithState(std::string &buf); |
| 369 | void SynthMsgSendFunctionDecl(); |
| 370 | void SynthMsgSendSuperFunctionDecl(); |
| 371 | void SynthMsgSendStretFunctionDecl(); |
| 372 | void SynthMsgSendFpretFunctionDecl(); |
| 373 | void SynthMsgSendSuperStretFunctionDecl(); |
| 374 | void SynthGetClassFunctionDecl(); |
| 375 | void SynthGetMetaClassFunctionDecl(); |
| 376 | void SynthGetSuperClassFunctionDecl(); |
| 377 | void SynthSelGetUidFunctionDecl(); |
| 378 | void SynthSuperContructorFunctionDecl(); |
| 379 | |
| 380 | // Rewriting metadata |
| 381 | template<typename MethodIterator> |
| 382 | void RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 383 | MethodIterator MethodEnd, |
| 384 | bool IsInstanceMethod, |
| 385 | StringRef prefix, |
| 386 | StringRef ClassName, |
| 387 | std::string &Result); |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 388 | void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol, |
| 389 | std::string &Result); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 390 | virtual void RewriteObjCProtocolListMetaData( |
| 391 | const ObjCList<ObjCProtocolDecl> &Prots, |
| 392 | StringRef prefix, StringRef ClassName, std::string &Result); |
| 393 | virtual void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
| 394 | std::string &Result); |
Fariborz Jahanian | e033578 | 2012-03-27 18:41:05 +0000 | [diff] [blame] | 395 | virtual void RewriteClassSetupInitHook(std::string &Result); |
| 396 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 397 | virtual void RewriteMetaDataIntoBuffer(std::string &Result); |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 398 | virtual void WriteImageInfo(std::string &Result); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 399 | virtual void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
| 400 | std::string &Result); |
Fariborz Jahanian | e033578 | 2012-03-27 18:41:05 +0000 | [diff] [blame] | 401 | virtual void RewriteCategorySetupInitHook(std::string &Result); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 402 | |
| 403 | // Rewriting ivar |
| 404 | virtual void RewriteIvarOffsetComputation(ObjCIvarDecl *ivar, |
| 405 | std::string &Result); |
| 406 | virtual Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV); |
| 407 | |
| 408 | |
| 409 | std::string SynthesizeByrefCopyDestroyHelper(VarDecl *VD, int flag); |
| 410 | std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
| 411 | StringRef funcName, std::string Tag); |
| 412 | std::string SynthesizeBlockFunc(BlockExpr *CE, int i, |
| 413 | StringRef funcName, std::string Tag); |
| 414 | std::string SynthesizeBlockImpl(BlockExpr *CE, |
| 415 | std::string Tag, std::string Desc); |
| 416 | std::string SynthesizeBlockDescriptor(std::string DescTag, |
| 417 | std::string ImplTag, |
| 418 | int i, StringRef funcName, |
| 419 | unsigned hasCopy); |
| 420 | Stmt *SynthesizeBlockCall(CallExpr *Exp, const Expr* BlockExp); |
| 421 | void SynthesizeBlockLiterals(SourceLocation FunLocStart, |
| 422 | StringRef FunName); |
| 423 | FunctionDecl *SynthBlockInitFunctionDecl(StringRef name); |
| 424 | Stmt *SynthBlockInitExpr(BlockExpr *Exp, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 425 | const SmallVector<DeclRefExpr *, 8> &InnerBlockDeclRefs); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 426 | |
| 427 | // Misc. helper routines. |
| 428 | QualType getProtocolType(); |
| 429 | void WarnAboutReturnGotoStmts(Stmt *S); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 430 | void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND); |
| 431 | void InsertBlockLiteralsWithinFunction(FunctionDecl *FD); |
| 432 | void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD); |
| 433 | |
| 434 | bool IsDeclStmtInForeachHeader(DeclStmt *DS); |
| 435 | void CollectBlockDeclRefInfo(BlockExpr *Exp); |
| 436 | void GetBlockDeclRefExprs(Stmt *S); |
| 437 | void GetInnerBlockDeclRefExprs(Stmt *S, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 438 | SmallVector<DeclRefExpr *, 8> &InnerBlockDeclRefs, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 439 | llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts); |
| 440 | |
| 441 | // We avoid calling Type::isBlockPointerType(), since it operates on the |
| 442 | // canonical type. We only care if the top-level type is a closure pointer. |
| 443 | bool isTopLevelBlockPointerType(QualType T) { |
| 444 | return isa<BlockPointerType>(T); |
| 445 | } |
| 446 | |
| 447 | /// convertBlockPointerToFunctionPointer - Converts a block-pointer type |
| 448 | /// to a function pointer type and upon success, returns true; false |
| 449 | /// otherwise. |
| 450 | bool convertBlockPointerToFunctionPointer(QualType &T) { |
| 451 | if (isTopLevelBlockPointerType(T)) { |
| 452 | const BlockPointerType *BPT = T->getAs<BlockPointerType>(); |
| 453 | T = Context->getPointerType(BPT->getPointeeType()); |
| 454 | return true; |
| 455 | } |
| 456 | return false; |
| 457 | } |
| 458 | |
Fariborz Jahanian | 164d6f8 | 2012-02-13 18:57:49 +0000 | [diff] [blame] | 459 | bool convertObjCTypeToCStyleType(QualType &T); |
| 460 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 461 | bool needToScanForQualifiers(QualType T); |
| 462 | QualType getSuperStructType(); |
| 463 | QualType getConstantStringStructType(); |
| 464 | QualType convertFunctionTypeOfBlocks(const FunctionType *FT); |
| 465 | bool BufferContainsPPDirectives(const char *startBuf, const char *endBuf); |
| 466 | |
| 467 | void convertToUnqualifiedObjCType(QualType &T) { |
Fariborz Jahanian | e35abe1 | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 468 | if (T->isObjCQualifiedIdType()) { |
| 469 | bool isConst = T.isConstQualified(); |
| 470 | T = isConst ? Context->getObjCIdType().withConst() |
| 471 | : Context->getObjCIdType(); |
| 472 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 473 | else if (T->isObjCQualifiedClassType()) |
| 474 | T = Context->getObjCClassType(); |
| 475 | else if (T->isObjCObjectPointerType() && |
| 476 | T->getPointeeType()->isObjCQualifiedInterfaceType()) { |
| 477 | if (const ObjCObjectPointerType * OBJPT = |
| 478 | T->getAsObjCInterfacePointerType()) { |
| 479 | const ObjCInterfaceType *IFaceT = OBJPT->getInterfaceType(); |
| 480 | T = QualType(IFaceT, 0); |
| 481 | T = Context->getPointerType(T); |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | // FIXME: This predicate seems like it would be useful to add to ASTContext. |
| 487 | bool isObjCType(QualType T) { |
| 488 | if (!LangOpts.ObjC1 && !LangOpts.ObjC2) |
| 489 | return false; |
| 490 | |
| 491 | QualType OCT = Context->getCanonicalType(T).getUnqualifiedType(); |
| 492 | |
| 493 | if (OCT == Context->getCanonicalType(Context->getObjCIdType()) || |
| 494 | OCT == Context->getCanonicalType(Context->getObjCClassType())) |
| 495 | return true; |
| 496 | |
| 497 | if (const PointerType *PT = OCT->getAs<PointerType>()) { |
| 498 | if (isa<ObjCInterfaceType>(PT->getPointeeType()) || |
| 499 | PT->getPointeeType()->isObjCQualifiedIdType()) |
| 500 | return true; |
| 501 | } |
| 502 | return false; |
| 503 | } |
| 504 | bool PointerTypeTakesAnyBlockArguments(QualType QT); |
| 505 | bool PointerTypeTakesAnyObjCQualifiedType(QualType QT); |
| 506 | void GetExtentOfArgList(const char *Name, const char *&LParen, |
| 507 | const char *&RParen); |
| 508 | |
| 509 | void QuoteDoublequotes(std::string &From, std::string &To) { |
| 510 | for (unsigned i = 0; i < From.length(); i++) { |
| 511 | if (From[i] == '"') |
| 512 | To += "\\\""; |
| 513 | else |
| 514 | To += From[i]; |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | QualType getSimpleFunctionType(QualType result, |
| 519 | const QualType *args, |
| 520 | unsigned numArgs, |
| 521 | bool variadic = false) { |
| 522 | if (result == Context->getObjCInstanceType()) |
| 523 | result = Context->getObjCIdType(); |
| 524 | FunctionProtoType::ExtProtoInfo fpi; |
| 525 | fpi.Variadic = variadic; |
| 526 | return Context->getFunctionType(result, args, numArgs, fpi); |
| 527 | } |
| 528 | |
| 529 | // Helper function: create a CStyleCastExpr with trivial type source info. |
| 530 | CStyleCastExpr* NoTypeInfoCStyleCastExpr(ASTContext *Ctx, QualType Ty, |
| 531 | CastKind Kind, Expr *E) { |
| 532 | TypeSourceInfo *TInfo = Ctx->getTrivialTypeSourceInfo(Ty, SourceLocation()); |
| 533 | return CStyleCastExpr::Create(*Ctx, Ty, VK_RValue, Kind, E, 0, TInfo, |
| 534 | SourceLocation(), SourceLocation()); |
| 535 | } |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 536 | |
| 537 | bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const { |
| 538 | IdentifierInfo* II = &Context->Idents.get("load"); |
| 539 | Selector LoadSel = Context->Selectors.getSelector(0, &II); |
| 540 | return OD->getClassMethod(LoadSel) != 0; |
| 541 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 542 | }; |
| 543 | |
| 544 | } |
| 545 | |
| 546 | void RewriteModernObjC::RewriteBlocksInFunctionProtoType(QualType funcType, |
| 547 | NamedDecl *D) { |
| 548 | if (const FunctionProtoType *fproto |
| 549 | = dyn_cast<FunctionProtoType>(funcType.IgnoreParens())) { |
| 550 | for (FunctionProtoType::arg_type_iterator I = fproto->arg_type_begin(), |
| 551 | E = fproto->arg_type_end(); I && (I != E); ++I) |
| 552 | if (isTopLevelBlockPointerType(*I)) { |
| 553 | // All the args are checked/rewritten. Don't call twice! |
| 554 | RewriteBlockPointerDecl(D); |
| 555 | break; |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | void RewriteModernObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) { |
| 561 | const PointerType *PT = funcType->getAs<PointerType>(); |
| 562 | if (PT && PointerTypeTakesAnyBlockArguments(funcType)) |
| 563 | RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND); |
| 564 | } |
| 565 | |
| 566 | static bool IsHeaderFile(const std::string &Filename) { |
| 567 | std::string::size_type DotPos = Filename.rfind('.'); |
| 568 | |
| 569 | if (DotPos == std::string::npos) { |
| 570 | // no file extension |
| 571 | return false; |
| 572 | } |
| 573 | |
| 574 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 575 | // C header: .h |
| 576 | // C++ header: .hh or .H; |
| 577 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
| 578 | } |
| 579 | |
| 580 | RewriteModernObjC::RewriteModernObjC(std::string inFile, raw_ostream* OS, |
| 581 | DiagnosticsEngine &D, const LangOptions &LOpts, |
| 582 | bool silenceMacroWarn) |
| 583 | : Diags(D), LangOpts(LOpts), InFileName(inFile), OutFile(OS), |
| 584 | SilenceRewriteMacroWarning(silenceMacroWarn) { |
| 585 | IsHeader = IsHeaderFile(inFile); |
| 586 | RewriteFailedDiag = Diags.getCustomDiagID(DiagnosticsEngine::Warning, |
| 587 | "rewriting sub-expression within a macro (may not be correct)"); |
Fariborz Jahanian | d13c2c2 | 2012-03-22 19:54:39 +0000 | [diff] [blame] | 588 | // FIXME. This should be an error. But if block is not called, it is OK. And it |
| 589 | // may break including some headers. |
| 590 | GlobalBlockRewriteFailedDiag = Diags.getCustomDiagID(DiagnosticsEngine::Warning, |
| 591 | "rewriting block literal declared in global scope is not implemented"); |
| 592 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 593 | TryFinallyContainsReturnDiag = Diags.getCustomDiagID( |
| 594 | DiagnosticsEngine::Warning, |
| 595 | "rewriter doesn't support user-specified control flow semantics " |
| 596 | "for @try/@finally (code may not execute properly)"); |
| 597 | } |
| 598 | |
| 599 | ASTConsumer *clang::CreateModernObjCRewriter(const std::string& InFile, |
| 600 | raw_ostream* OS, |
| 601 | DiagnosticsEngine &Diags, |
| 602 | const LangOptions &LOpts, |
| 603 | bool SilenceRewriteMacroWarning) { |
| 604 | return new RewriteModernObjC(InFile, OS, Diags, LOpts, SilenceRewriteMacroWarning); |
| 605 | } |
| 606 | |
| 607 | void RewriteModernObjC::InitializeCommon(ASTContext &context) { |
| 608 | Context = &context; |
| 609 | SM = &Context->getSourceManager(); |
| 610 | TUDecl = Context->getTranslationUnitDecl(); |
| 611 | MsgSendFunctionDecl = 0; |
| 612 | MsgSendSuperFunctionDecl = 0; |
| 613 | MsgSendStretFunctionDecl = 0; |
| 614 | MsgSendSuperStretFunctionDecl = 0; |
| 615 | MsgSendFpretFunctionDecl = 0; |
| 616 | GetClassFunctionDecl = 0; |
| 617 | GetMetaClassFunctionDecl = 0; |
| 618 | GetSuperClassFunctionDecl = 0; |
| 619 | SelGetUidFunctionDecl = 0; |
| 620 | CFStringFunctionDecl = 0; |
| 621 | ConstantStringClassReference = 0; |
| 622 | NSStringRecord = 0; |
| 623 | CurMethodDef = 0; |
| 624 | CurFunctionDef = 0; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 625 | GlobalVarDecl = 0; |
Fariborz Jahanian | df474ec | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 626 | GlobalConstructionExp = 0; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 627 | SuperStructDecl = 0; |
| 628 | ProtocolTypeDecl = 0; |
| 629 | ConstantStringDecl = 0; |
| 630 | BcLabelCount = 0; |
| 631 | SuperContructorFunctionDecl = 0; |
| 632 | NumObjCStringLiterals = 0; |
| 633 | PropParentMap = 0; |
| 634 | CurrentBody = 0; |
| 635 | DisableReplaceStmt = false; |
| 636 | objc_impl_method = false; |
| 637 | |
| 638 | // Get the ID and start/end of the main file. |
| 639 | MainFileID = SM->getMainFileID(); |
| 640 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 641 | MainFileStart = MainBuf->getBufferStart(); |
| 642 | MainFileEnd = MainBuf->getBufferEnd(); |
| 643 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 644 | Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOpts()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | //===----------------------------------------------------------------------===// |
| 648 | // Top Level Driver Code |
| 649 | //===----------------------------------------------------------------------===// |
| 650 | |
| 651 | void RewriteModernObjC::HandleTopLevelSingleDecl(Decl *D) { |
| 652 | if (Diags.hasErrorOccurred()) |
| 653 | return; |
| 654 | |
| 655 | // Two cases: either the decl could be in the main file, or it could be in a |
| 656 | // #included file. If the former, rewrite it now. If the later, check to see |
| 657 | // if we rewrote the #include/#import. |
| 658 | SourceLocation Loc = D->getLocation(); |
| 659 | Loc = SM->getExpansionLoc(Loc); |
| 660 | |
| 661 | // If this is for a builtin, ignore it. |
| 662 | if (Loc.isInvalid()) return; |
| 663 | |
| 664 | // Look for built-in declarations that we need to refer during the rewrite. |
| 665 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 666 | RewriteFunctionDecl(FD); |
| 667 | } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) { |
| 668 | // declared in <Foundation/NSString.h> |
| 669 | if (FVD->getName() == "_NSConstantStringClassReference") { |
| 670 | ConstantStringClassReference = FVD; |
| 671 | return; |
| 672 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 673 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
| 674 | RewriteCategoryDecl(CD); |
| 675 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
| 676 | if (PD->isThisDeclarationADefinition()) |
| 677 | RewriteProtocolDecl(PD); |
| 678 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
Fariborz Jahanian | 8e86b2d | 2012-04-04 17:16:15 +0000 | [diff] [blame] | 679 | // FIXME. This will not work in all situations and leaving it out |
| 680 | // is harmless. |
| 681 | // RewriteLinkageSpec(LSD); |
| 682 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 683 | // Recurse into linkage specifications |
| 684 | for (DeclContext::decl_iterator DI = LSD->decls_begin(), |
| 685 | DIEnd = LSD->decls_end(); |
| 686 | DI != DIEnd; ) { |
| 687 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>((*DI))) { |
| 688 | if (!IFace->isThisDeclarationADefinition()) { |
| 689 | SmallVector<Decl *, 8> DG; |
| 690 | SourceLocation StartLoc = IFace->getLocStart(); |
| 691 | do { |
| 692 | if (isa<ObjCInterfaceDecl>(*DI) && |
| 693 | !cast<ObjCInterfaceDecl>(*DI)->isThisDeclarationADefinition() && |
| 694 | StartLoc == (*DI)->getLocStart()) |
| 695 | DG.push_back(*DI); |
| 696 | else |
| 697 | break; |
| 698 | |
| 699 | ++DI; |
| 700 | } while (DI != DIEnd); |
| 701 | RewriteForwardClassDecl(DG); |
| 702 | continue; |
| 703 | } |
Fariborz Jahanian | b3f904f | 2012-04-03 17:35:38 +0000 | [diff] [blame] | 704 | else { |
| 705 | // Keep track of all interface declarations seen. |
| 706 | ObjCInterfacesSeen.push_back(IFace); |
| 707 | ++DI; |
| 708 | continue; |
| 709 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>((*DI))) { |
| 713 | if (!Proto->isThisDeclarationADefinition()) { |
| 714 | SmallVector<Decl *, 8> DG; |
| 715 | SourceLocation StartLoc = Proto->getLocStart(); |
| 716 | do { |
| 717 | if (isa<ObjCProtocolDecl>(*DI) && |
| 718 | !cast<ObjCProtocolDecl>(*DI)->isThisDeclarationADefinition() && |
| 719 | StartLoc == (*DI)->getLocStart()) |
| 720 | DG.push_back(*DI); |
| 721 | else |
| 722 | break; |
| 723 | |
| 724 | ++DI; |
| 725 | } while (DI != DIEnd); |
| 726 | RewriteForwardProtocolDecl(DG); |
| 727 | continue; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | HandleTopLevelSingleDecl(*DI); |
| 732 | ++DI; |
| 733 | } |
| 734 | } |
| 735 | // If we have a decl in the main file, see if we should rewrite it. |
| 736 | if (SM->isFromMainFile(Loc)) |
| 737 | return HandleDeclInMainFile(D); |
| 738 | } |
| 739 | |
| 740 | //===----------------------------------------------------------------------===// |
| 741 | // Syntactic (non-AST) Rewriting Code |
| 742 | //===----------------------------------------------------------------------===// |
| 743 | |
| 744 | void RewriteModernObjC::RewriteInclude() { |
| 745 | SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID); |
| 746 | StringRef MainBuf = SM->getBufferData(MainFileID); |
| 747 | const char *MainBufStart = MainBuf.begin(); |
| 748 | const char *MainBufEnd = MainBuf.end(); |
| 749 | size_t ImportLen = strlen("import"); |
| 750 | |
| 751 | // Loop over the whole file, looking for includes. |
| 752 | for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { |
| 753 | if (*BufPtr == '#') { |
| 754 | if (++BufPtr == MainBufEnd) |
| 755 | return; |
| 756 | while (*BufPtr == ' ' || *BufPtr == '\t') |
| 757 | if (++BufPtr == MainBufEnd) |
| 758 | return; |
| 759 | if (!strncmp(BufPtr, "import", ImportLen)) { |
| 760 | // replace import with include |
| 761 | SourceLocation ImportLoc = |
| 762 | LocStart.getLocWithOffset(BufPtr-MainBufStart); |
| 763 | ReplaceText(ImportLoc, ImportLen, "include"); |
| 764 | BufPtr += ImportLen; |
| 765 | } |
| 766 | } |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | static std::string getIvarAccessString(ObjCIvarDecl *OID) { |
| 771 | const ObjCInterfaceDecl *ClassDecl = OID->getContainingInterface(); |
| 772 | std::string S; |
| 773 | S = "((struct "; |
| 774 | S += ClassDecl->getIdentifier()->getName(); |
| 775 | S += "_IMPL *)self)->"; |
| 776 | S += OID->getName(); |
| 777 | return S; |
| 778 | } |
| 779 | |
| 780 | void RewriteModernObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 781 | ObjCImplementationDecl *IMD, |
| 782 | ObjCCategoryImplDecl *CID) { |
| 783 | static bool objcGetPropertyDefined = false; |
| 784 | static bool objcSetPropertyDefined = false; |
| 785 | SourceLocation startLoc = PID->getLocStart(); |
| 786 | InsertText(startLoc, "// "); |
| 787 | const char *startBuf = SM->getCharacterData(startLoc); |
| 788 | assert((*startBuf == '@') && "bogus @synthesize location"); |
| 789 | const char *semiBuf = strchr(startBuf, ';'); |
| 790 | assert((*semiBuf == ';') && "@synthesize: can't find ';'"); |
| 791 | SourceLocation onePastSemiLoc = |
| 792 | startLoc.getLocWithOffset(semiBuf-startBuf+1); |
| 793 | |
| 794 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 795 | return; // FIXME: is this correct? |
| 796 | |
| 797 | // Generate the 'getter' function. |
| 798 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 799 | ObjCIvarDecl *OID = PID->getPropertyIvarDecl(); |
| 800 | |
| 801 | if (!OID) |
| 802 | return; |
| 803 | unsigned Attributes = PD->getPropertyAttributes(); |
| 804 | if (!PD->getGetterMethodDecl()->isDefined()) { |
| 805 | bool GenGetProperty = !(Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) && |
| 806 | (Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
| 807 | ObjCPropertyDecl::OBJC_PR_copy)); |
| 808 | std::string Getr; |
| 809 | if (GenGetProperty && !objcGetPropertyDefined) { |
| 810 | objcGetPropertyDefined = true; |
| 811 | // FIXME. Is this attribute correct in all cases? |
| 812 | Getr = "\nextern \"C\" __declspec(dllimport) " |
| 813 | "id objc_getProperty(id, SEL, long, bool);\n"; |
| 814 | } |
| 815 | RewriteObjCMethodDecl(OID->getContainingInterface(), |
| 816 | PD->getGetterMethodDecl(), Getr); |
| 817 | Getr += "{ "; |
| 818 | // Synthesize an explicit cast to gain access to the ivar. |
| 819 | // See objc-act.c:objc_synthesize_new_getter() for details. |
| 820 | if (GenGetProperty) { |
| 821 | // return objc_getProperty(self, _cmd, offsetof(ClassDecl, OID), 1) |
| 822 | Getr += "typedef "; |
| 823 | const FunctionType *FPRetType = 0; |
| 824 | RewriteTypeIntoString(PD->getGetterMethodDecl()->getResultType(), Getr, |
| 825 | FPRetType); |
| 826 | Getr += " _TYPE"; |
| 827 | if (FPRetType) { |
| 828 | Getr += ")"; // close the precedence "scope" for "*". |
| 829 | |
| 830 | // Now, emit the argument types (if any). |
| 831 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)){ |
| 832 | Getr += "("; |
| 833 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 834 | if (i) Getr += ", "; |
| 835 | std::string ParamStr = FT->getArgType(i).getAsString( |
| 836 | Context->getPrintingPolicy()); |
| 837 | Getr += ParamStr; |
| 838 | } |
| 839 | if (FT->isVariadic()) { |
| 840 | if (FT->getNumArgs()) Getr += ", "; |
| 841 | Getr += "..."; |
| 842 | } |
| 843 | Getr += ")"; |
| 844 | } else |
| 845 | Getr += "()"; |
| 846 | } |
| 847 | Getr += ";\n"; |
| 848 | Getr += "return (_TYPE)"; |
| 849 | Getr += "objc_getProperty(self, _cmd, "; |
| 850 | RewriteIvarOffsetComputation(OID, Getr); |
| 851 | Getr += ", 1)"; |
| 852 | } |
| 853 | else |
| 854 | Getr += "return " + getIvarAccessString(OID); |
| 855 | Getr += "; }"; |
| 856 | InsertText(onePastSemiLoc, Getr); |
| 857 | } |
| 858 | |
| 859 | if (PD->isReadOnly() || PD->getSetterMethodDecl()->isDefined()) |
| 860 | return; |
| 861 | |
| 862 | // Generate the 'setter' function. |
| 863 | std::string Setr; |
| 864 | bool GenSetProperty = Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
| 865 | ObjCPropertyDecl::OBJC_PR_copy); |
| 866 | if (GenSetProperty && !objcSetPropertyDefined) { |
| 867 | objcSetPropertyDefined = true; |
| 868 | // FIXME. Is this attribute correct in all cases? |
| 869 | Setr = "\nextern \"C\" __declspec(dllimport) " |
| 870 | "void objc_setProperty (id, SEL, long, id, bool, bool);\n"; |
| 871 | } |
| 872 | |
| 873 | RewriteObjCMethodDecl(OID->getContainingInterface(), |
| 874 | PD->getSetterMethodDecl(), Setr); |
| 875 | Setr += "{ "; |
| 876 | // Synthesize an explicit cast to initialize the ivar. |
| 877 | // See objc-act.c:objc_synthesize_new_setter() for details. |
| 878 | if (GenSetProperty) { |
| 879 | Setr += "objc_setProperty (self, _cmd, "; |
| 880 | RewriteIvarOffsetComputation(OID, Setr); |
| 881 | Setr += ", (id)"; |
| 882 | Setr += PD->getName(); |
| 883 | Setr += ", "; |
| 884 | if (Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 885 | Setr += "0, "; |
| 886 | else |
| 887 | Setr += "1, "; |
| 888 | if (Attributes & ObjCPropertyDecl::OBJC_PR_copy) |
| 889 | Setr += "1)"; |
| 890 | else |
| 891 | Setr += "0)"; |
| 892 | } |
| 893 | else { |
| 894 | Setr += getIvarAccessString(OID) + " = "; |
| 895 | Setr += PD->getName(); |
| 896 | } |
| 897 | Setr += "; }"; |
| 898 | InsertText(onePastSemiLoc, Setr); |
| 899 | } |
| 900 | |
| 901 | static void RewriteOneForwardClassDecl(ObjCInterfaceDecl *ForwardDecl, |
| 902 | std::string &typedefString) { |
| 903 | typedefString += "#ifndef _REWRITER_typedef_"; |
| 904 | typedefString += ForwardDecl->getNameAsString(); |
| 905 | typedefString += "\n"; |
| 906 | typedefString += "#define _REWRITER_typedef_"; |
| 907 | typedefString += ForwardDecl->getNameAsString(); |
| 908 | typedefString += "\n"; |
| 909 | typedefString += "typedef struct objc_object "; |
| 910 | typedefString += ForwardDecl->getNameAsString(); |
Fariborz Jahanian | c38503b | 2012-03-12 23:58:28 +0000 | [diff] [blame] | 911 | // typedef struct { } _objc_exc_Classname; |
| 912 | typedefString += ";\ntypedef struct {} _objc_exc_"; |
| 913 | typedefString += ForwardDecl->getNameAsString(); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 914 | typedefString += ";\n#endif\n"; |
| 915 | } |
| 916 | |
| 917 | void RewriteModernObjC::RewriteForwardClassEpilogue(ObjCInterfaceDecl *ClassDecl, |
| 918 | const std::string &typedefString) { |
| 919 | SourceLocation startLoc = ClassDecl->getLocStart(); |
| 920 | const char *startBuf = SM->getCharacterData(startLoc); |
| 921 | const char *semiPtr = strchr(startBuf, ';'); |
| 922 | // Replace the @class with typedefs corresponding to the classes. |
| 923 | ReplaceText(startLoc, semiPtr-startBuf+1, typedefString); |
| 924 | } |
| 925 | |
| 926 | void RewriteModernObjC::RewriteForwardClassDecl(DeclGroupRef D) { |
| 927 | std::string typedefString; |
| 928 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
| 929 | ObjCInterfaceDecl *ForwardDecl = cast<ObjCInterfaceDecl>(*I); |
| 930 | if (I == D.begin()) { |
| 931 | // Translate to typedef's that forward reference structs with the same name |
| 932 | // as the class. As a convenience, we include the original declaration |
| 933 | // as a comment. |
| 934 | typedefString += "// @class "; |
| 935 | typedefString += ForwardDecl->getNameAsString(); |
| 936 | typedefString += ";\n"; |
| 937 | } |
| 938 | RewriteOneForwardClassDecl(ForwardDecl, typedefString); |
| 939 | } |
| 940 | DeclGroupRef::iterator I = D.begin(); |
| 941 | RewriteForwardClassEpilogue(cast<ObjCInterfaceDecl>(*I), typedefString); |
| 942 | } |
| 943 | |
| 944 | void RewriteModernObjC::RewriteForwardClassDecl( |
| 945 | const llvm::SmallVector<Decl*, 8> &D) { |
| 946 | std::string typedefString; |
| 947 | for (unsigned i = 0; i < D.size(); i++) { |
| 948 | ObjCInterfaceDecl *ForwardDecl = cast<ObjCInterfaceDecl>(D[i]); |
| 949 | if (i == 0) { |
| 950 | typedefString += "// @class "; |
| 951 | typedefString += ForwardDecl->getNameAsString(); |
| 952 | typedefString += ";\n"; |
| 953 | } |
| 954 | RewriteOneForwardClassDecl(ForwardDecl, typedefString); |
| 955 | } |
| 956 | RewriteForwardClassEpilogue(cast<ObjCInterfaceDecl>(D[0]), typedefString); |
| 957 | } |
| 958 | |
| 959 | void RewriteModernObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) { |
| 960 | // When method is a synthesized one, such as a getter/setter there is |
| 961 | // nothing to rewrite. |
| 962 | if (Method->isImplicit()) |
| 963 | return; |
| 964 | SourceLocation LocStart = Method->getLocStart(); |
| 965 | SourceLocation LocEnd = Method->getLocEnd(); |
| 966 | |
| 967 | if (SM->getExpansionLineNumber(LocEnd) > |
| 968 | SM->getExpansionLineNumber(LocStart)) { |
| 969 | InsertText(LocStart, "#if 0\n"); |
| 970 | ReplaceText(LocEnd, 1, ";\n#endif\n"); |
| 971 | } else { |
| 972 | InsertText(LocStart, "// "); |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | void RewriteModernObjC::RewriteProperty(ObjCPropertyDecl *prop) { |
| 977 | SourceLocation Loc = prop->getAtLoc(); |
| 978 | |
| 979 | ReplaceText(Loc, 0, "// "); |
| 980 | // FIXME: handle properties that are declared across multiple lines. |
| 981 | } |
| 982 | |
| 983 | void RewriteModernObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
| 984 | SourceLocation LocStart = CatDecl->getLocStart(); |
| 985 | |
| 986 | // FIXME: handle category headers that are declared across multiple lines. |
| 987 | ReplaceText(LocStart, 0, "// "); |
Fariborz Jahanian | af30029 | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 988 | if (CatDecl->getIvarLBraceLoc().isValid()) |
| 989 | InsertText(CatDecl->getIvarLBraceLoc(), "// "); |
Fariborz Jahanian | d2aea12 | 2012-02-19 19:00:05 +0000 | [diff] [blame] | 990 | for (ObjCCategoryDecl::ivar_iterator |
| 991 | I = CatDecl->ivar_begin(), E = CatDecl->ivar_end(); I != E; ++I) { |
| 992 | ObjCIvarDecl *Ivar = (*I); |
| 993 | SourceLocation LocStart = Ivar->getLocStart(); |
| 994 | ReplaceText(LocStart, 0, "// "); |
| 995 | } |
Fariborz Jahanian | af30029 | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 996 | if (CatDecl->getIvarRBraceLoc().isValid()) |
| 997 | InsertText(CatDecl->getIvarRBraceLoc(), "// "); |
| 998 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 999 | for (ObjCCategoryDecl::prop_iterator I = CatDecl->prop_begin(), |
| 1000 | E = CatDecl->prop_end(); I != E; ++I) |
| 1001 | RewriteProperty(*I); |
| 1002 | |
| 1003 | for (ObjCCategoryDecl::instmeth_iterator |
| 1004 | I = CatDecl->instmeth_begin(), E = CatDecl->instmeth_end(); |
| 1005 | I != E; ++I) |
| 1006 | RewriteMethodDeclaration(*I); |
| 1007 | for (ObjCCategoryDecl::classmeth_iterator |
| 1008 | I = CatDecl->classmeth_begin(), E = CatDecl->classmeth_end(); |
| 1009 | I != E; ++I) |
| 1010 | RewriteMethodDeclaration(*I); |
| 1011 | |
| 1012 | // Lastly, comment out the @end. |
| 1013 | ReplaceText(CatDecl->getAtEndRange().getBegin(), |
| 1014 | strlen("@end"), "/* @end */"); |
| 1015 | } |
| 1016 | |
| 1017 | void RewriteModernObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
| 1018 | SourceLocation LocStart = PDecl->getLocStart(); |
| 1019 | assert(PDecl->isThisDeclarationADefinition()); |
| 1020 | |
| 1021 | // FIXME: handle protocol headers that are declared across multiple lines. |
| 1022 | ReplaceText(LocStart, 0, "// "); |
| 1023 | |
| 1024 | for (ObjCProtocolDecl::instmeth_iterator |
| 1025 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
| 1026 | I != E; ++I) |
| 1027 | RewriteMethodDeclaration(*I); |
| 1028 | for (ObjCProtocolDecl::classmeth_iterator |
| 1029 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
| 1030 | I != E; ++I) |
| 1031 | RewriteMethodDeclaration(*I); |
| 1032 | |
| 1033 | for (ObjCInterfaceDecl::prop_iterator I = PDecl->prop_begin(), |
| 1034 | E = PDecl->prop_end(); I != E; ++I) |
| 1035 | RewriteProperty(*I); |
| 1036 | |
| 1037 | // Lastly, comment out the @end. |
| 1038 | SourceLocation LocEnd = PDecl->getAtEndRange().getBegin(); |
| 1039 | ReplaceText(LocEnd, strlen("@end"), "/* @end */"); |
| 1040 | |
| 1041 | // Must comment out @optional/@required |
| 1042 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1043 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 1044 | for (const char *p = startBuf; p < endBuf; p++) { |
| 1045 | if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { |
| 1046 | SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf); |
| 1047 | ReplaceText(OptionalLoc, strlen("@optional"), "/* @optional */"); |
| 1048 | |
| 1049 | } |
| 1050 | else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { |
| 1051 | SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf); |
| 1052 | ReplaceText(OptionalLoc, strlen("@required"), "/* @required */"); |
| 1053 | |
| 1054 | } |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | void RewriteModernObjC::RewriteForwardProtocolDecl(DeclGroupRef D) { |
| 1059 | SourceLocation LocStart = (*D.begin())->getLocStart(); |
| 1060 | if (LocStart.isInvalid()) |
| 1061 | llvm_unreachable("Invalid SourceLocation"); |
| 1062 | // FIXME: handle forward protocol that are declared across multiple lines. |
| 1063 | ReplaceText(LocStart, 0, "// "); |
| 1064 | } |
| 1065 | |
| 1066 | void |
| 1067 | RewriteModernObjC::RewriteForwardProtocolDecl(const llvm::SmallVector<Decl*, 8> &DG) { |
| 1068 | SourceLocation LocStart = DG[0]->getLocStart(); |
| 1069 | if (LocStart.isInvalid()) |
| 1070 | llvm_unreachable("Invalid SourceLocation"); |
| 1071 | // FIXME: handle forward protocol that are declared across multiple lines. |
| 1072 | ReplaceText(LocStart, 0, "// "); |
| 1073 | } |
| 1074 | |
Fariborz Jahanian | b3f904f | 2012-04-03 17:35:38 +0000 | [diff] [blame] | 1075 | void |
| 1076 | RewriteModernObjC::RewriteLinkageSpec(LinkageSpecDecl *LSD) { |
| 1077 | SourceLocation LocStart = LSD->getExternLoc(); |
| 1078 | if (LocStart.isInvalid()) |
| 1079 | llvm_unreachable("Invalid extern SourceLocation"); |
| 1080 | |
| 1081 | ReplaceText(LocStart, 0, "// "); |
| 1082 | if (!LSD->hasBraces()) |
| 1083 | return; |
| 1084 | // FIXME. We don't rewrite well if '{' is not on same line as 'extern'. |
| 1085 | SourceLocation LocRBrace = LSD->getRBraceLoc(); |
| 1086 | if (LocRBrace.isInvalid()) |
| 1087 | llvm_unreachable("Invalid rbrace SourceLocation"); |
| 1088 | ReplaceText(LocRBrace, 0, "// "); |
| 1089 | } |
| 1090 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1091 | void RewriteModernObjC::RewriteTypeIntoString(QualType T, std::string &ResultStr, |
| 1092 | const FunctionType *&FPRetType) { |
| 1093 | if (T->isObjCQualifiedIdType()) |
| 1094 | ResultStr += "id"; |
| 1095 | else if (T->isFunctionPointerType() || |
| 1096 | T->isBlockPointerType()) { |
| 1097 | // needs special handling, since pointer-to-functions have special |
| 1098 | // syntax (where a decaration models use). |
| 1099 | QualType retType = T; |
| 1100 | QualType PointeeTy; |
| 1101 | if (const PointerType* PT = retType->getAs<PointerType>()) |
| 1102 | PointeeTy = PT->getPointeeType(); |
| 1103 | else if (const BlockPointerType *BPT = retType->getAs<BlockPointerType>()) |
| 1104 | PointeeTy = BPT->getPointeeType(); |
| 1105 | if ((FPRetType = PointeeTy->getAs<FunctionType>())) { |
| 1106 | ResultStr += FPRetType->getResultType().getAsString( |
| 1107 | Context->getPrintingPolicy()); |
| 1108 | ResultStr += "(*"; |
| 1109 | } |
| 1110 | } else |
| 1111 | ResultStr += T.getAsString(Context->getPrintingPolicy()); |
| 1112 | } |
| 1113 | |
| 1114 | void RewriteModernObjC::RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl, |
| 1115 | ObjCMethodDecl *OMD, |
| 1116 | std::string &ResultStr) { |
| 1117 | //fprintf(stderr,"In RewriteObjCMethodDecl\n"); |
| 1118 | const FunctionType *FPRetType = 0; |
| 1119 | ResultStr += "\nstatic "; |
| 1120 | RewriteTypeIntoString(OMD->getResultType(), ResultStr, FPRetType); |
| 1121 | ResultStr += " "; |
| 1122 | |
| 1123 | // Unique method name |
| 1124 | std::string NameStr; |
| 1125 | |
| 1126 | if (OMD->isInstanceMethod()) |
| 1127 | NameStr += "_I_"; |
| 1128 | else |
| 1129 | NameStr += "_C_"; |
| 1130 | |
| 1131 | NameStr += IDecl->getNameAsString(); |
| 1132 | NameStr += "_"; |
| 1133 | |
| 1134 | if (ObjCCategoryImplDecl *CID = |
| 1135 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
| 1136 | NameStr += CID->getNameAsString(); |
| 1137 | NameStr += "_"; |
| 1138 | } |
| 1139 | // Append selector names, replacing ':' with '_' |
| 1140 | { |
| 1141 | std::string selString = OMD->getSelector().getAsString(); |
| 1142 | int len = selString.size(); |
| 1143 | for (int i = 0; i < len; i++) |
| 1144 | if (selString[i] == ':') |
| 1145 | selString[i] = '_'; |
| 1146 | NameStr += selString; |
| 1147 | } |
| 1148 | // Remember this name for metadata emission |
| 1149 | MethodInternalNames[OMD] = NameStr; |
| 1150 | ResultStr += NameStr; |
| 1151 | |
| 1152 | // Rewrite arguments |
| 1153 | ResultStr += "("; |
| 1154 | |
| 1155 | // invisible arguments |
| 1156 | if (OMD->isInstanceMethod()) { |
| 1157 | QualType selfTy = Context->getObjCInterfaceType(IDecl); |
| 1158 | selfTy = Context->getPointerType(selfTy); |
| 1159 | if (!LangOpts.MicrosoftExt) { |
| 1160 | if (ObjCSynthesizedStructs.count(const_cast<ObjCInterfaceDecl*>(IDecl))) |
| 1161 | ResultStr += "struct "; |
| 1162 | } |
| 1163 | // When rewriting for Microsoft, explicitly omit the structure name. |
| 1164 | ResultStr += IDecl->getNameAsString(); |
| 1165 | ResultStr += " *"; |
| 1166 | } |
| 1167 | else |
| 1168 | ResultStr += Context->getObjCClassType().getAsString( |
| 1169 | Context->getPrintingPolicy()); |
| 1170 | |
| 1171 | ResultStr += " self, "; |
| 1172 | ResultStr += Context->getObjCSelType().getAsString(Context->getPrintingPolicy()); |
| 1173 | ResultStr += " _cmd"; |
| 1174 | |
| 1175 | // Method arguments. |
| 1176 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 1177 | E = OMD->param_end(); PI != E; ++PI) { |
| 1178 | ParmVarDecl *PDecl = *PI; |
| 1179 | ResultStr += ", "; |
| 1180 | if (PDecl->getType()->isObjCQualifiedIdType()) { |
| 1181 | ResultStr += "id "; |
| 1182 | ResultStr += PDecl->getNameAsString(); |
| 1183 | } else { |
| 1184 | std::string Name = PDecl->getNameAsString(); |
| 1185 | QualType QT = PDecl->getType(); |
| 1186 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 2610f90 | 2012-03-27 16:42:20 +0000 | [diff] [blame] | 1187 | (void)convertBlockPointerToFunctionPointer(QT); |
| 1188 | QT.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1189 | ResultStr += Name; |
| 1190 | } |
| 1191 | } |
| 1192 | if (OMD->isVariadic()) |
| 1193 | ResultStr += ", ..."; |
| 1194 | ResultStr += ") "; |
| 1195 | |
| 1196 | if (FPRetType) { |
| 1197 | ResultStr += ")"; // close the precedence "scope" for "*". |
| 1198 | |
| 1199 | // Now, emit the argument types (if any). |
| 1200 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) { |
| 1201 | ResultStr += "("; |
| 1202 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 1203 | if (i) ResultStr += ", "; |
| 1204 | std::string ParamStr = FT->getArgType(i).getAsString( |
| 1205 | Context->getPrintingPolicy()); |
| 1206 | ResultStr += ParamStr; |
| 1207 | } |
| 1208 | if (FT->isVariadic()) { |
| 1209 | if (FT->getNumArgs()) ResultStr += ", "; |
| 1210 | ResultStr += "..."; |
| 1211 | } |
| 1212 | ResultStr += ")"; |
| 1213 | } else { |
| 1214 | ResultStr += "()"; |
| 1215 | } |
| 1216 | } |
| 1217 | } |
| 1218 | void RewriteModernObjC::RewriteImplementationDecl(Decl *OID) { |
| 1219 | ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID); |
| 1220 | ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID); |
| 1221 | |
Fariborz Jahanian | d2aea12 | 2012-02-19 19:00:05 +0000 | [diff] [blame] | 1222 | if (IMD) { |
| 1223 | InsertText(IMD->getLocStart(), "// "); |
Fariborz Jahanian | af30029 | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 1224 | if (IMD->getIvarLBraceLoc().isValid()) |
| 1225 | InsertText(IMD->getIvarLBraceLoc(), "// "); |
| 1226 | for (ObjCImplementationDecl::ivar_iterator |
| 1227 | I = IMD->ivar_begin(), E = IMD->ivar_end(); I != E; ++I) { |
| 1228 | ObjCIvarDecl *Ivar = (*I); |
| 1229 | SourceLocation LocStart = Ivar->getLocStart(); |
| 1230 | ReplaceText(LocStart, 0, "// "); |
Fariborz Jahanian | d2aea12 | 2012-02-19 19:00:05 +0000 | [diff] [blame] | 1231 | } |
Fariborz Jahanian | af30029 | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 1232 | if (IMD->getIvarRBraceLoc().isValid()) |
| 1233 | InsertText(IMD->getIvarRBraceLoc(), "// "); |
Fariborz Jahanian | d2aea12 | 2012-02-19 19:00:05 +0000 | [diff] [blame] | 1234 | } |
| 1235 | else |
| 1236 | InsertText(CID->getLocStart(), "// "); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1237 | |
| 1238 | for (ObjCCategoryImplDecl::instmeth_iterator |
| 1239 | I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(), |
| 1240 | E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); |
| 1241 | I != E; ++I) { |
| 1242 | std::string ResultStr; |
| 1243 | ObjCMethodDecl *OMD = *I; |
| 1244 | RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr); |
| 1245 | SourceLocation LocStart = OMD->getLocStart(); |
| 1246 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
| 1247 | |
| 1248 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1249 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 1250 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
| 1251 | } |
| 1252 | |
| 1253 | for (ObjCCategoryImplDecl::classmeth_iterator |
| 1254 | I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(), |
| 1255 | E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); |
| 1256 | I != E; ++I) { |
| 1257 | std::string ResultStr; |
| 1258 | ObjCMethodDecl *OMD = *I; |
| 1259 | RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr); |
| 1260 | SourceLocation LocStart = OMD->getLocStart(); |
| 1261 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
| 1262 | |
| 1263 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1264 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 1265 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
| 1266 | } |
| 1267 | for (ObjCCategoryImplDecl::propimpl_iterator |
| 1268 | I = IMD ? IMD->propimpl_begin() : CID->propimpl_begin(), |
| 1269 | E = IMD ? IMD->propimpl_end() : CID->propimpl_end(); |
| 1270 | I != E; ++I) { |
| 1271 | RewritePropertyImplDecl(*I, IMD, CID); |
| 1272 | } |
| 1273 | |
| 1274 | InsertText(IMD ? IMD->getLocEnd() : CID->getLocEnd(), "// "); |
| 1275 | } |
| 1276 | |
| 1277 | void RewriteModernObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { |
Fariborz Jahanian | 8f1fed0 | 2012-02-11 20:10:52 +0000 | [diff] [blame] | 1278 | // Do not synthesize more than once. |
| 1279 | if (ObjCSynthesizedStructs.count(ClassDecl)) |
| 1280 | return; |
| 1281 | // Make sure super class's are written before current class is written. |
| 1282 | ObjCInterfaceDecl *SuperClass = ClassDecl->getSuperClass(); |
| 1283 | while (SuperClass) { |
| 1284 | RewriteInterfaceDecl(SuperClass); |
| 1285 | SuperClass = SuperClass->getSuperClass(); |
| 1286 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1287 | std::string ResultStr; |
Fariborz Jahanian | cf4c60f | 2012-02-17 22:20:12 +0000 | [diff] [blame] | 1288 | if (!ObjCWrittenInterfaces.count(ClassDecl->getCanonicalDecl())) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1289 | // we haven't seen a forward decl - generate a typedef. |
Fariborz Jahanian | c38503b | 2012-03-12 23:58:28 +0000 | [diff] [blame] | 1290 | RewriteOneForwardClassDecl(ClassDecl, ResultStr); |
Fariborz Jahanian | 72c88f1 | 2012-02-22 18:13:25 +0000 | [diff] [blame] | 1291 | RewriteIvarOffsetSymbols(ClassDecl, ResultStr); |
| 1292 | |
Fariborz Jahanian | 4339bb3 | 2012-02-15 22:01:47 +0000 | [diff] [blame] | 1293 | RewriteObjCInternalStruct(ClassDecl, ResultStr); |
Fariborz Jahanian | cf4c60f | 2012-02-17 22:20:12 +0000 | [diff] [blame] | 1294 | // Mark this typedef as having been written into its c++ equivalent. |
| 1295 | ObjCWrittenInterfaces.insert(ClassDecl->getCanonicalDecl()); |
Fariborz Jahanian | 4339bb3 | 2012-02-15 22:01:47 +0000 | [diff] [blame] | 1296 | |
| 1297 | for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(), |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1298 | E = ClassDecl->prop_end(); I != E; ++I) |
Fariborz Jahanian | 4339bb3 | 2012-02-15 22:01:47 +0000 | [diff] [blame] | 1299 | RewriteProperty(*I); |
| 1300 | for (ObjCInterfaceDecl::instmeth_iterator |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1301 | I = ClassDecl->instmeth_begin(), E = ClassDecl->instmeth_end(); |
Fariborz Jahanian | 4339bb3 | 2012-02-15 22:01:47 +0000 | [diff] [blame] | 1302 | I != E; ++I) |
| 1303 | RewriteMethodDeclaration(*I); |
| 1304 | for (ObjCInterfaceDecl::classmeth_iterator |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1305 | I = ClassDecl->classmeth_begin(), E = ClassDecl->classmeth_end(); |
Fariborz Jahanian | 4339bb3 | 2012-02-15 22:01:47 +0000 | [diff] [blame] | 1306 | I != E; ++I) |
| 1307 | RewriteMethodDeclaration(*I); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1308 | |
Fariborz Jahanian | 4339bb3 | 2012-02-15 22:01:47 +0000 | [diff] [blame] | 1309 | // Lastly, comment out the @end. |
| 1310 | ReplaceText(ClassDecl->getAtEndRange().getBegin(), strlen("@end"), |
| 1311 | "/* @end */"); |
| 1312 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | Stmt *RewriteModernObjC::RewritePropertyOrImplicitSetter(PseudoObjectExpr *PseudoOp) { |
| 1316 | SourceRange OldRange = PseudoOp->getSourceRange(); |
| 1317 | |
| 1318 | // We just magically know some things about the structure of this |
| 1319 | // expression. |
| 1320 | ObjCMessageExpr *OldMsg = |
| 1321 | cast<ObjCMessageExpr>(PseudoOp->getSemanticExpr( |
| 1322 | PseudoOp->getNumSemanticExprs() - 1)); |
| 1323 | |
| 1324 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 1325 | // we need to suppress rewriting the sub-statements. |
Fariborz Jahanian | 88ec610 | 2012-04-10 22:06:54 +0000 | [diff] [blame] | 1326 | Expr *Base; |
| 1327 | SmallVector<Expr*, 2> Args; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1328 | { |
| 1329 | DisableReplaceStmtScope S(*this); |
| 1330 | |
| 1331 | // Rebuild the base expression if we have one. |
| 1332 | Base = 0; |
| 1333 | if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) { |
| 1334 | Base = OldMsg->getInstanceReceiver(); |
| 1335 | Base = cast<OpaqueValueExpr>(Base)->getSourceExpr(); |
| 1336 | Base = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Base)); |
| 1337 | } |
Fariborz Jahanian | 88ec610 | 2012-04-10 22:06:54 +0000 | [diff] [blame] | 1338 | |
| 1339 | unsigned numArgs = OldMsg->getNumArgs(); |
| 1340 | for (unsigned i = 0; i < numArgs; i++) { |
| 1341 | Expr *Arg = OldMsg->getArg(i); |
| 1342 | if (isa<OpaqueValueExpr>(Arg)) |
| 1343 | Arg = cast<OpaqueValueExpr>(Arg)->getSourceExpr(); |
| 1344 | Arg = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Arg)); |
| 1345 | Args.push_back(Arg); |
| 1346 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1347 | } |
| 1348 | |
| 1349 | // TODO: avoid this copy. |
| 1350 | SmallVector<SourceLocation, 1> SelLocs; |
| 1351 | OldMsg->getSelectorLocs(SelLocs); |
| 1352 | |
| 1353 | ObjCMessageExpr *NewMsg = 0; |
| 1354 | switch (OldMsg->getReceiverKind()) { |
| 1355 | case ObjCMessageExpr::Class: |
| 1356 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1357 | OldMsg->getValueKind(), |
| 1358 | OldMsg->getLeftLoc(), |
| 1359 | OldMsg->getClassReceiverTypeInfo(), |
| 1360 | OldMsg->getSelector(), |
| 1361 | SelLocs, |
| 1362 | OldMsg->getMethodDecl(), |
Fariborz Jahanian | 88ec610 | 2012-04-10 22:06:54 +0000 | [diff] [blame] | 1363 | Args, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1364 | OldMsg->getRightLoc(), |
| 1365 | OldMsg->isImplicit()); |
| 1366 | break; |
| 1367 | |
| 1368 | case ObjCMessageExpr::Instance: |
| 1369 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1370 | OldMsg->getValueKind(), |
| 1371 | OldMsg->getLeftLoc(), |
| 1372 | Base, |
| 1373 | OldMsg->getSelector(), |
| 1374 | SelLocs, |
| 1375 | OldMsg->getMethodDecl(), |
Fariborz Jahanian | 88ec610 | 2012-04-10 22:06:54 +0000 | [diff] [blame] | 1376 | Args, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1377 | OldMsg->getRightLoc(), |
| 1378 | OldMsg->isImplicit()); |
| 1379 | break; |
| 1380 | |
| 1381 | case ObjCMessageExpr::SuperClass: |
| 1382 | case ObjCMessageExpr::SuperInstance: |
| 1383 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1384 | OldMsg->getValueKind(), |
| 1385 | OldMsg->getLeftLoc(), |
| 1386 | OldMsg->getSuperLoc(), |
| 1387 | OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance, |
| 1388 | OldMsg->getSuperType(), |
| 1389 | OldMsg->getSelector(), |
| 1390 | SelLocs, |
| 1391 | OldMsg->getMethodDecl(), |
Fariborz Jahanian | 88ec610 | 2012-04-10 22:06:54 +0000 | [diff] [blame] | 1392 | Args, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1393 | OldMsg->getRightLoc(), |
| 1394 | OldMsg->isImplicit()); |
| 1395 | break; |
| 1396 | } |
| 1397 | |
| 1398 | Stmt *Replacement = SynthMessageExpr(NewMsg); |
| 1399 | ReplaceStmtWithRange(PseudoOp, Replacement, OldRange); |
| 1400 | return Replacement; |
| 1401 | } |
| 1402 | |
| 1403 | Stmt *RewriteModernObjC::RewritePropertyOrImplicitGetter(PseudoObjectExpr *PseudoOp) { |
| 1404 | SourceRange OldRange = PseudoOp->getSourceRange(); |
| 1405 | |
| 1406 | // We just magically know some things about the structure of this |
| 1407 | // expression. |
| 1408 | ObjCMessageExpr *OldMsg = |
| 1409 | cast<ObjCMessageExpr>(PseudoOp->getResultExpr()->IgnoreImplicit()); |
| 1410 | |
| 1411 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 1412 | // we need to suppress rewriting the sub-statements. |
| 1413 | Expr *Base = 0; |
Fariborz Jahanian | 88ec610 | 2012-04-10 22:06:54 +0000 | [diff] [blame] | 1414 | SmallVector<Expr*, 1> Args; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1415 | { |
| 1416 | DisableReplaceStmtScope S(*this); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1417 | // Rebuild the base expression if we have one. |
| 1418 | if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) { |
| 1419 | Base = OldMsg->getInstanceReceiver(); |
| 1420 | Base = cast<OpaqueValueExpr>(Base)->getSourceExpr(); |
| 1421 | Base = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Base)); |
| 1422 | } |
Fariborz Jahanian | 88ec610 | 2012-04-10 22:06:54 +0000 | [diff] [blame] | 1423 | unsigned numArgs = OldMsg->getNumArgs(); |
| 1424 | for (unsigned i = 0; i < numArgs; i++) { |
| 1425 | Expr *Arg = OldMsg->getArg(i); |
| 1426 | if (isa<OpaqueValueExpr>(Arg)) |
| 1427 | Arg = cast<OpaqueValueExpr>(Arg)->getSourceExpr(); |
| 1428 | Arg = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Arg)); |
| 1429 | Args.push_back(Arg); |
| 1430 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | // Intentionally empty. |
| 1434 | SmallVector<SourceLocation, 1> SelLocs; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1435 | |
| 1436 | ObjCMessageExpr *NewMsg = 0; |
| 1437 | switch (OldMsg->getReceiverKind()) { |
| 1438 | case ObjCMessageExpr::Class: |
| 1439 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1440 | OldMsg->getValueKind(), |
| 1441 | OldMsg->getLeftLoc(), |
| 1442 | OldMsg->getClassReceiverTypeInfo(), |
| 1443 | OldMsg->getSelector(), |
| 1444 | SelLocs, |
| 1445 | OldMsg->getMethodDecl(), |
| 1446 | Args, |
| 1447 | OldMsg->getRightLoc(), |
| 1448 | OldMsg->isImplicit()); |
| 1449 | break; |
| 1450 | |
| 1451 | case ObjCMessageExpr::Instance: |
| 1452 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1453 | OldMsg->getValueKind(), |
| 1454 | OldMsg->getLeftLoc(), |
| 1455 | Base, |
| 1456 | OldMsg->getSelector(), |
| 1457 | SelLocs, |
| 1458 | OldMsg->getMethodDecl(), |
| 1459 | Args, |
| 1460 | OldMsg->getRightLoc(), |
| 1461 | OldMsg->isImplicit()); |
| 1462 | break; |
| 1463 | |
| 1464 | case ObjCMessageExpr::SuperClass: |
| 1465 | case ObjCMessageExpr::SuperInstance: |
| 1466 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1467 | OldMsg->getValueKind(), |
| 1468 | OldMsg->getLeftLoc(), |
| 1469 | OldMsg->getSuperLoc(), |
| 1470 | OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance, |
| 1471 | OldMsg->getSuperType(), |
| 1472 | OldMsg->getSelector(), |
| 1473 | SelLocs, |
| 1474 | OldMsg->getMethodDecl(), |
| 1475 | Args, |
| 1476 | OldMsg->getRightLoc(), |
| 1477 | OldMsg->isImplicit()); |
| 1478 | break; |
| 1479 | } |
| 1480 | |
| 1481 | Stmt *Replacement = SynthMessageExpr(NewMsg); |
| 1482 | ReplaceStmtWithRange(PseudoOp, Replacement, OldRange); |
| 1483 | return Replacement; |
| 1484 | } |
| 1485 | |
| 1486 | /// SynthCountByEnumWithState - To print: |
| 1487 | /// ((unsigned int (*) |
| 1488 | /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
| 1489 | /// (void *)objc_msgSend)((id)l_collection, |
| 1490 | /// sel_registerName( |
| 1491 | /// "countByEnumeratingWithState:objects:count:"), |
| 1492 | /// &enumState, |
| 1493 | /// (id *)__rw_items, (unsigned int)16) |
| 1494 | /// |
| 1495 | void RewriteModernObjC::SynthCountByEnumWithState(std::string &buf) { |
| 1496 | buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, " |
| 1497 | "id *, unsigned int))(void *)objc_msgSend)"; |
| 1498 | buf += "\n\t\t"; |
| 1499 | buf += "((id)l_collection,\n\t\t"; |
| 1500 | buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; |
| 1501 | buf += "\n\t\t"; |
| 1502 | buf += "&enumState, " |
| 1503 | "(id *)__rw_items, (unsigned int)16)"; |
| 1504 | } |
| 1505 | |
| 1506 | /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach |
| 1507 | /// statement to exit to its outer synthesized loop. |
| 1508 | /// |
| 1509 | Stmt *RewriteModernObjC::RewriteBreakStmt(BreakStmt *S) { |
| 1510 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1511 | return S; |
| 1512 | // replace break with goto __break_label |
| 1513 | std::string buf; |
| 1514 | |
| 1515 | SourceLocation startLoc = S->getLocStart(); |
| 1516 | buf = "goto __break_label_"; |
| 1517 | buf += utostr(ObjCBcLabelNo.back()); |
| 1518 | ReplaceText(startLoc, strlen("break"), buf); |
| 1519 | |
| 1520 | return 0; |
| 1521 | } |
| 1522 | |
| 1523 | /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach |
| 1524 | /// statement to continue with its inner synthesized loop. |
| 1525 | /// |
| 1526 | Stmt *RewriteModernObjC::RewriteContinueStmt(ContinueStmt *S) { |
| 1527 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1528 | return S; |
| 1529 | // replace continue with goto __continue_label |
| 1530 | std::string buf; |
| 1531 | |
| 1532 | SourceLocation startLoc = S->getLocStart(); |
| 1533 | buf = "goto __continue_label_"; |
| 1534 | buf += utostr(ObjCBcLabelNo.back()); |
| 1535 | ReplaceText(startLoc, strlen("continue"), buf); |
| 1536 | |
| 1537 | return 0; |
| 1538 | } |
| 1539 | |
| 1540 | /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. |
| 1541 | /// It rewrites: |
| 1542 | /// for ( type elem in collection) { stmts; } |
| 1543 | |
| 1544 | /// Into: |
| 1545 | /// { |
| 1546 | /// type elem; |
| 1547 | /// struct __objcFastEnumerationState enumState = { 0 }; |
| 1548 | /// id __rw_items[16]; |
| 1549 | /// id l_collection = (id)collection; |
| 1550 | /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1551 | /// objects:__rw_items count:16]; |
| 1552 | /// if (limit) { |
| 1553 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1554 | /// do { |
| 1555 | /// unsigned long counter = 0; |
| 1556 | /// do { |
| 1557 | /// if (startMutations != *enumState.mutationsPtr) |
| 1558 | /// objc_enumerationMutation(l_collection); |
| 1559 | /// elem = (type)enumState.itemsPtr[counter++]; |
| 1560 | /// stmts; |
| 1561 | /// __continue_label: ; |
| 1562 | /// } while (counter < limit); |
| 1563 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
| 1564 | /// objects:__rw_items count:16]); |
| 1565 | /// elem = nil; |
| 1566 | /// __break_label: ; |
| 1567 | /// } |
| 1568 | /// else |
| 1569 | /// elem = nil; |
| 1570 | /// } |
| 1571 | /// |
| 1572 | Stmt *RewriteModernObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
| 1573 | SourceLocation OrigEnd) { |
| 1574 | assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); |
| 1575 | assert(isa<ObjCForCollectionStmt>(Stmts.back()) && |
| 1576 | "ObjCForCollectionStmt Statement stack mismatch"); |
| 1577 | assert(!ObjCBcLabelNo.empty() && |
| 1578 | "ObjCForCollectionStmt - Label No stack empty"); |
| 1579 | |
| 1580 | SourceLocation startLoc = S->getLocStart(); |
| 1581 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1582 | StringRef elementName; |
| 1583 | std::string elementTypeAsString; |
| 1584 | std::string buf; |
| 1585 | buf = "\n{\n\t"; |
| 1586 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { |
| 1587 | // type elem; |
| 1588 | NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl()); |
| 1589 | QualType ElementType = cast<ValueDecl>(D)->getType(); |
| 1590 | if (ElementType->isObjCQualifiedIdType() || |
| 1591 | ElementType->isObjCQualifiedInterfaceType()) |
| 1592 | // Simply use 'id' for all qualified types. |
| 1593 | elementTypeAsString = "id"; |
| 1594 | else |
| 1595 | elementTypeAsString = ElementType.getAsString(Context->getPrintingPolicy()); |
| 1596 | buf += elementTypeAsString; |
| 1597 | buf += " "; |
| 1598 | elementName = D->getName(); |
| 1599 | buf += elementName; |
| 1600 | buf += ";\n\t"; |
| 1601 | } |
| 1602 | else { |
| 1603 | DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); |
| 1604 | elementName = DR->getDecl()->getName(); |
| 1605 | ValueDecl *VD = cast<ValueDecl>(DR->getDecl()); |
| 1606 | if (VD->getType()->isObjCQualifiedIdType() || |
| 1607 | VD->getType()->isObjCQualifiedInterfaceType()) |
| 1608 | // Simply use 'id' for all qualified types. |
| 1609 | elementTypeAsString = "id"; |
| 1610 | else |
| 1611 | elementTypeAsString = VD->getType().getAsString(Context->getPrintingPolicy()); |
| 1612 | } |
| 1613 | |
| 1614 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1615 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
| 1616 | // id __rw_items[16]; |
| 1617 | buf += "id __rw_items[16];\n\t"; |
| 1618 | // id l_collection = (id) |
| 1619 | buf += "id l_collection = (id)"; |
| 1620 | // Find start location of 'collection' the hard way! |
| 1621 | const char *startCollectionBuf = startBuf; |
| 1622 | startCollectionBuf += 3; // skip 'for' |
| 1623 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1624 | startCollectionBuf++; // skip '(' |
| 1625 | // find 'in' and skip it. |
| 1626 | while (*startCollectionBuf != ' ' || |
| 1627 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1628 | (*(startCollectionBuf+3) != ' ' && |
| 1629 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1630 | startCollectionBuf++; |
| 1631 | startCollectionBuf += 3; |
| 1632 | |
| 1633 | // Replace: "for (type element in" with string constructed thus far. |
| 1634 | ReplaceText(startLoc, startCollectionBuf - startBuf, buf); |
| 1635 | // Replace ')' in for '(' type elem in collection ')' with ';' |
| 1636 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1637 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
| 1638 | SourceLocation lparenLoc = startLoc.getLocWithOffset(rparenBuf-startBuf); |
| 1639 | buf = ";\n\t"; |
| 1640 | |
| 1641 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1642 | // objects:__rw_items count:16]; |
| 1643 | // which is synthesized into: |
| 1644 | // unsigned int limit = |
| 1645 | // ((unsigned int (*) |
| 1646 | // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
| 1647 | // (void *)objc_msgSend)((id)l_collection, |
| 1648 | // sel_registerName( |
| 1649 | // "countByEnumeratingWithState:objects:count:"), |
| 1650 | // (struct __objcFastEnumerationState *)&state, |
| 1651 | // (id *)__rw_items, (unsigned int)16); |
| 1652 | buf += "unsigned long limit =\n\t\t"; |
| 1653 | SynthCountByEnumWithState(buf); |
| 1654 | buf += ";\n\t"; |
| 1655 | /// if (limit) { |
| 1656 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1657 | /// do { |
| 1658 | /// unsigned long counter = 0; |
| 1659 | /// do { |
| 1660 | /// if (startMutations != *enumState.mutationsPtr) |
| 1661 | /// objc_enumerationMutation(l_collection); |
| 1662 | /// elem = (type)enumState.itemsPtr[counter++]; |
| 1663 | buf += "if (limit) {\n\t"; |
| 1664 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1665 | buf += "do {\n\t\t"; |
| 1666 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1667 | buf += "do {\n\t\t\t"; |
| 1668 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1669 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1670 | buf += elementName; |
| 1671 | buf += " = ("; |
| 1672 | buf += elementTypeAsString; |
| 1673 | buf += ")enumState.itemsPtr[counter++];"; |
| 1674 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
| 1675 | ReplaceText(lparenLoc, 1, buf); |
| 1676 | |
| 1677 | /// __continue_label: ; |
| 1678 | /// } while (counter < limit); |
| 1679 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
| 1680 | /// objects:__rw_items count:16]); |
| 1681 | /// elem = nil; |
| 1682 | /// __break_label: ; |
| 1683 | /// } |
| 1684 | /// else |
| 1685 | /// elem = nil; |
| 1686 | /// } |
| 1687 | /// |
| 1688 | buf = ";\n\t"; |
| 1689 | buf += "__continue_label_"; |
| 1690 | buf += utostr(ObjCBcLabelNo.back()); |
| 1691 | buf += ": ;"; |
| 1692 | buf += "\n\t\t"; |
| 1693 | buf += "} while (counter < limit);\n\t"; |
| 1694 | buf += "} while (limit = "; |
| 1695 | SynthCountByEnumWithState(buf); |
| 1696 | buf += ");\n\t"; |
| 1697 | buf += elementName; |
| 1698 | buf += " = (("; |
| 1699 | buf += elementTypeAsString; |
| 1700 | buf += ")0);\n\t"; |
| 1701 | buf += "__break_label_"; |
| 1702 | buf += utostr(ObjCBcLabelNo.back()); |
| 1703 | buf += ": ;\n\t"; |
| 1704 | buf += "}\n\t"; |
| 1705 | buf += "else\n\t\t"; |
| 1706 | buf += elementName; |
| 1707 | buf += " = (("; |
| 1708 | buf += elementTypeAsString; |
| 1709 | buf += ")0);\n\t"; |
| 1710 | buf += "}\n"; |
| 1711 | |
| 1712 | // Insert all these *after* the statement body. |
| 1713 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
| 1714 | if (isa<CompoundStmt>(S->getBody())) { |
| 1715 | SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(1); |
| 1716 | InsertText(endBodyLoc, buf); |
| 1717 | } else { |
| 1718 | /* Need to treat single statements specially. For example: |
| 1719 | * |
| 1720 | * for (A *a in b) if (stuff()) break; |
| 1721 | * for (A *a in b) xxxyy; |
| 1722 | * |
| 1723 | * The following code simply scans ahead to the semi to find the actual end. |
| 1724 | */ |
| 1725 | const char *stmtBuf = SM->getCharacterData(OrigEnd); |
| 1726 | const char *semiBuf = strchr(stmtBuf, ';'); |
| 1727 | assert(semiBuf && "Can't find ';'"); |
| 1728 | SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(semiBuf-stmtBuf+1); |
| 1729 | InsertText(endBodyLoc, buf); |
| 1730 | } |
| 1731 | Stmts.pop_back(); |
| 1732 | ObjCBcLabelNo.pop_back(); |
| 1733 | return 0; |
| 1734 | } |
| 1735 | |
Fariborz Jahanian | 542125f | 2012-03-16 21:33:16 +0000 | [diff] [blame] | 1736 | static void Write_RethrowObject(std::string &buf) { |
| 1737 | buf += "{ struct _FIN { _FIN(id reth) : rethrow(reth) {}\n"; |
| 1738 | buf += "\t~_FIN() { if (rethrow) objc_exception_throw(rethrow); }\n"; |
| 1739 | buf += "\tid rethrow;\n"; |
| 1740 | buf += "\t} _fin_force_rethow(_rethrow);"; |
| 1741 | } |
| 1742 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1743 | /// RewriteObjCSynchronizedStmt - |
| 1744 | /// This routine rewrites @synchronized(expr) stmt; |
| 1745 | /// into: |
| 1746 | /// objc_sync_enter(expr); |
| 1747 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1748 | /// |
| 1749 | Stmt *RewriteModernObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
| 1750 | // Get the start location and compute the semi location. |
| 1751 | SourceLocation startLoc = S->getLocStart(); |
| 1752 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1753 | |
| 1754 | assert((*startBuf == '@') && "bogus @synchronized location"); |
| 1755 | |
| 1756 | std::string buf; |
Fariborz Jahanian | 22e2f85 | 2012-03-17 17:46:02 +0000 | [diff] [blame] | 1757 | buf = "{ id _rethrow = 0; id _sync_obj = "; |
Fariborz Jahanian | 542125f | 2012-03-16 21:33:16 +0000 | [diff] [blame] | 1758 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1759 | const char *lparenBuf = startBuf; |
| 1760 | while (*lparenBuf != '(') lparenBuf++; |
| 1761 | ReplaceText(startLoc, lparenBuf-startBuf+1, buf); |
Fariborz Jahanian | 22e2f85 | 2012-03-17 17:46:02 +0000 | [diff] [blame] | 1762 | |
| 1763 | buf = "; objc_sync_enter(_sync_obj);\n"; |
| 1764 | buf += "try {\n\tstruct _SYNC_EXIT { _SYNC_EXIT(id arg) : sync_exit(arg) {}"; |
| 1765 | buf += "\n\t~_SYNC_EXIT() {objc_sync_exit(sync_exit);}"; |
| 1766 | buf += "\n\tid sync_exit;"; |
| 1767 | buf += "\n\t} _sync_exit(_sync_obj);\n"; |
| 1768 | |
| 1769 | // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since |
| 1770 | // the sync expression is typically a message expression that's already |
| 1771 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1772 | SourceLocation RParenExprLoc = S->getSynchBody()->getLocStart(); |
| 1773 | const char *RParenExprLocBuf = SM->getCharacterData(RParenExprLoc); |
| 1774 | while (*RParenExprLocBuf != ')') RParenExprLocBuf--; |
| 1775 | RParenExprLoc = startLoc.getLocWithOffset(RParenExprLocBuf-startBuf); |
| 1776 | |
| 1777 | SourceLocation LBranceLoc = S->getSynchBody()->getLocStart(); |
| 1778 | const char *LBraceLocBuf = SM->getCharacterData(LBranceLoc); |
| 1779 | assert (*LBraceLocBuf == '{'); |
| 1780 | ReplaceText(RParenExprLoc, (LBraceLocBuf - SM->getCharacterData(RParenExprLoc) + 1), buf); |
Fariborz Jahanian | 542125f | 2012-03-16 21:33:16 +0000 | [diff] [blame] | 1781 | |
Fariborz Jahanian | b655bf0 | 2012-03-16 21:43:45 +0000 | [diff] [blame] | 1782 | SourceLocation startRBraceLoc = S->getSynchBody()->getLocEnd(); |
Matt Beaumont-Gay | 9ab511c | 2012-03-16 22:20:39 +0000 | [diff] [blame] | 1783 | assert((*SM->getCharacterData(startRBraceLoc) == '}') && |
| 1784 | "bogus @synchronized block"); |
Fariborz Jahanian | 542125f | 2012-03-16 21:33:16 +0000 | [diff] [blame] | 1785 | |
| 1786 | buf = "} catch (id e) {_rethrow = e;}\n"; |
| 1787 | Write_RethrowObject(buf); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1788 | buf += "}\n"; |
Fariborz Jahanian | 542125f | 2012-03-16 21:33:16 +0000 | [diff] [blame] | 1789 | buf += "}\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1790 | |
Fariborz Jahanian | b655bf0 | 2012-03-16 21:43:45 +0000 | [diff] [blame] | 1791 | ReplaceText(startRBraceLoc, 1, buf); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1792 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1793 | return 0; |
| 1794 | } |
| 1795 | |
| 1796 | void RewriteModernObjC::WarnAboutReturnGotoStmts(Stmt *S) |
| 1797 | { |
| 1798 | // Perform a bottom up traversal of all children. |
| 1799 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
| 1800 | if (*CI) |
| 1801 | WarnAboutReturnGotoStmts(*CI); |
| 1802 | |
| 1803 | if (isa<ReturnStmt>(S) || isa<GotoStmt>(S)) { |
| 1804 | Diags.Report(Context->getFullLoc(S->getLocStart()), |
| 1805 | TryFinallyContainsReturnDiag); |
| 1806 | } |
| 1807 | return; |
| 1808 | } |
| 1809 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1810 | Stmt *RewriteModernObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Fariborz Jahanian | b122818 | 2012-03-15 22:42:15 +0000 | [diff] [blame] | 1811 | ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt(); |
Fariborz Jahanian | 220419a | 2012-03-15 23:50:33 +0000 | [diff] [blame] | 1812 | bool noCatch = S->getNumCatchStmts() == 0; |
Fariborz Jahanian | b122818 | 2012-03-15 22:42:15 +0000 | [diff] [blame] | 1813 | std::string buf; |
| 1814 | |
| 1815 | if (finalStmt) { |
Fariborz Jahanian | 220419a | 2012-03-15 23:50:33 +0000 | [diff] [blame] | 1816 | if (noCatch) |
| 1817 | buf = "{ id volatile _rethrow = 0;\n"; |
| 1818 | else { |
| 1819 | buf = "{ id volatile _rethrow = 0;\ntry {\n"; |
| 1820 | } |
Fariborz Jahanian | b122818 | 2012-03-15 22:42:15 +0000 | [diff] [blame] | 1821 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1822 | // Get the start location and compute the semi location. |
| 1823 | SourceLocation startLoc = S->getLocStart(); |
| 1824 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1825 | |
| 1826 | assert((*startBuf == '@') && "bogus @try location"); |
Fariborz Jahanian | b122818 | 2012-03-15 22:42:15 +0000 | [diff] [blame] | 1827 | if (finalStmt) |
| 1828 | ReplaceText(startLoc, 1, buf); |
| 1829 | else |
| 1830 | // @try -> try |
| 1831 | ReplaceText(startLoc, 1, ""); |
| 1832 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1833 | for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I) { |
| 1834 | ObjCAtCatchStmt *Catch = S->getCatchStmt(I); |
Fariborz Jahanian | 4c14881 | 2012-03-15 20:11:10 +0000 | [diff] [blame] | 1835 | VarDecl *catchDecl = Catch->getCatchParamDecl(); |
Fariborz Jahanian | c38503b | 2012-03-12 23:58:28 +0000 | [diff] [blame] | 1836 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1837 | startLoc = Catch->getLocStart(); |
Fariborz Jahanian | 4c14881 | 2012-03-15 20:11:10 +0000 | [diff] [blame] | 1838 | bool AtRemoved = false; |
| 1839 | if (catchDecl) { |
| 1840 | QualType t = catchDecl->getType(); |
| 1841 | if (const ObjCObjectPointerType *Ptr = t->getAs<ObjCObjectPointerType>()) { |
| 1842 | // Should be a pointer to a class. |
| 1843 | ObjCInterfaceDecl *IDecl = Ptr->getObjectType()->getInterface(); |
| 1844 | if (IDecl) { |
| 1845 | std::string Result; |
| 1846 | startBuf = SM->getCharacterData(startLoc); |
| 1847 | assert((*startBuf == '@') && "bogus @catch location"); |
| 1848 | SourceLocation rParenLoc = Catch->getRParenLoc(); |
| 1849 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 1850 | |
| 1851 | // _objc_exc_Foo *_e as argument to catch. |
| 1852 | Result = "catch (_objc_exc_"; Result += IDecl->getNameAsString(); |
| 1853 | Result += " *_"; Result += catchDecl->getNameAsString(); |
| 1854 | Result += ")"; |
| 1855 | ReplaceText(startLoc, rParenBuf-startBuf+1, Result); |
| 1856 | // Foo *e = (Foo *)_e; |
| 1857 | Result.clear(); |
| 1858 | Result = "{ "; |
| 1859 | Result += IDecl->getNameAsString(); |
| 1860 | Result += " *"; Result += catchDecl->getNameAsString(); |
| 1861 | Result += " = ("; Result += IDecl->getNameAsString(); Result += "*)"; |
| 1862 | Result += "_"; Result += catchDecl->getNameAsString(); |
| 1863 | |
| 1864 | Result += "; "; |
| 1865 | SourceLocation lBraceLoc = Catch->getCatchBody()->getLocStart(); |
| 1866 | ReplaceText(lBraceLoc, 1, Result); |
| 1867 | AtRemoved = true; |
| 1868 | } |
| 1869 | } |
| 1870 | } |
| 1871 | if (!AtRemoved) |
| 1872 | // @catch -> catch |
| 1873 | ReplaceText(startLoc, 1, ""); |
Fariborz Jahanian | c38503b | 2012-03-12 23:58:28 +0000 | [diff] [blame] | 1874 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1875 | } |
Fariborz Jahanian | 220419a | 2012-03-15 23:50:33 +0000 | [diff] [blame] | 1876 | if (finalStmt) { |
| 1877 | buf.clear(); |
| 1878 | if (noCatch) |
| 1879 | buf = "catch (id e) {_rethrow = e;}\n"; |
| 1880 | else |
| 1881 | buf = "}\ncatch (id e) {_rethrow = e;}\n"; |
| 1882 | |
| 1883 | SourceLocation startFinalLoc = finalStmt->getLocStart(); |
| 1884 | ReplaceText(startFinalLoc, 8, buf); |
| 1885 | Stmt *body = finalStmt->getFinallyBody(); |
| 1886 | SourceLocation startFinalBodyLoc = body->getLocStart(); |
| 1887 | buf.clear(); |
Fariborz Jahanian | 542125f | 2012-03-16 21:33:16 +0000 | [diff] [blame] | 1888 | Write_RethrowObject(buf); |
Fariborz Jahanian | 220419a | 2012-03-15 23:50:33 +0000 | [diff] [blame] | 1889 | ReplaceText(startFinalBodyLoc, 1, buf); |
| 1890 | |
| 1891 | SourceLocation endFinalBodyLoc = body->getLocEnd(); |
| 1892 | ReplaceText(endFinalBodyLoc, 1, "}\n}"); |
Fariborz Jahanian | 22e2f85 | 2012-03-17 17:46:02 +0000 | [diff] [blame] | 1893 | // Now check for any return/continue/go statements within the @try. |
| 1894 | WarnAboutReturnGotoStmts(S->getTryBody()); |
Fariborz Jahanian | 220419a | 2012-03-15 23:50:33 +0000 | [diff] [blame] | 1895 | } |
| 1896 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1897 | return 0; |
| 1898 | } |
| 1899 | |
| 1900 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
| 1901 | // the throw expression is typically a message expression that's already |
| 1902 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1903 | Stmt *RewriteModernObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
| 1904 | // Get the start location and compute the semi location. |
| 1905 | SourceLocation startLoc = S->getLocStart(); |
| 1906 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1907 | |
| 1908 | assert((*startBuf == '@') && "bogus @throw location"); |
| 1909 | |
| 1910 | std::string buf; |
| 1911 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
| 1912 | if (S->getThrowExpr()) |
| 1913 | buf = "objc_exception_throw("; |
Fariborz Jahanian | 4053946 | 2012-03-16 16:52:06 +0000 | [diff] [blame] | 1914 | else |
| 1915 | buf = "throw"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1916 | |
| 1917 | // handle "@ throw" correctly. |
| 1918 | const char *wBuf = strchr(startBuf, 'w'); |
| 1919 | assert((*wBuf == 'w') && "@throw: can't find 'w'"); |
| 1920 | ReplaceText(startLoc, wBuf-startBuf+1, buf); |
| 1921 | |
| 1922 | const char *semiBuf = strchr(startBuf, ';'); |
| 1923 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
| 1924 | SourceLocation semiLoc = startLoc.getLocWithOffset(semiBuf-startBuf); |
Fariborz Jahanian | 4053946 | 2012-03-16 16:52:06 +0000 | [diff] [blame] | 1925 | if (S->getThrowExpr()) |
| 1926 | ReplaceText(semiLoc, 1, ");"); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1927 | return 0; |
| 1928 | } |
| 1929 | |
| 1930 | Stmt *RewriteModernObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
| 1931 | // Create a new string expression. |
| 1932 | QualType StrType = Context->getPointerType(Context->CharTy); |
| 1933 | std::string StrEncoding; |
| 1934 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); |
| 1935 | Expr *Replacement = StringLiteral::Create(*Context, StrEncoding, |
| 1936 | StringLiteral::Ascii, false, |
| 1937 | StrType, SourceLocation()); |
| 1938 | ReplaceStmt(Exp, Replacement); |
| 1939 | |
| 1940 | // Replace this subexpr in the parent. |
| 1941 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
| 1942 | return Replacement; |
| 1943 | } |
| 1944 | |
| 1945 | Stmt *RewriteModernObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
| 1946 | if (!SelGetUidFunctionDecl) |
| 1947 | SynthSelGetUidFunctionDecl(); |
| 1948 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 1949 | // Create a call to sel_registerName("selName"). |
| 1950 | SmallVector<Expr*, 8> SelExprs; |
| 1951 | QualType argType = Context->getPointerType(Context->CharTy); |
| 1952 | SelExprs.push_back(StringLiteral::Create(*Context, |
| 1953 | Exp->getSelector().getAsString(), |
| 1954 | StringLiteral::Ascii, false, |
| 1955 | argType, SourceLocation())); |
| 1956 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 1957 | &SelExprs[0], SelExprs.size()); |
| 1958 | ReplaceStmt(Exp, SelExp); |
| 1959 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
| 1960 | return SelExp; |
| 1961 | } |
| 1962 | |
| 1963 | CallExpr *RewriteModernObjC::SynthesizeCallToFunctionDecl( |
| 1964 | FunctionDecl *FD, Expr **args, unsigned nargs, SourceLocation StartLoc, |
| 1965 | SourceLocation EndLoc) { |
| 1966 | // Get the type, we will need to reference it in a couple spots. |
| 1967 | QualType msgSendType = FD->getType(); |
| 1968 | |
| 1969 | // Create a reference to the objc_msgSend() declaration. |
| 1970 | DeclRefExpr *DRE = |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1971 | new (Context) DeclRefExpr(FD, false, msgSendType, VK_LValue, SourceLocation()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1972 | |
| 1973 | // Now, we cast the reference to a pointer to the objc_msgSend type. |
| 1974 | QualType pToFunc = Context->getPointerType(msgSendType); |
| 1975 | ImplicitCastExpr *ICE = |
| 1976 | ImplicitCastExpr::Create(*Context, pToFunc, CK_FunctionToPointerDecay, |
| 1977 | DRE, 0, VK_RValue); |
| 1978 | |
| 1979 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
| 1980 | |
| 1981 | CallExpr *Exp = |
| 1982 | new (Context) CallExpr(*Context, ICE, args, nargs, |
| 1983 | FT->getCallResultType(*Context), |
| 1984 | VK_RValue, EndLoc); |
| 1985 | return Exp; |
| 1986 | } |
| 1987 | |
| 1988 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 1989 | const char *&startRef, const char *&endRef) { |
| 1990 | while (startBuf < endBuf) { |
| 1991 | if (*startBuf == '<') |
| 1992 | startRef = startBuf; // mark the start. |
| 1993 | if (*startBuf == '>') { |
| 1994 | if (startRef && *startRef == '<') { |
| 1995 | endRef = startBuf; // mark the end. |
| 1996 | return true; |
| 1997 | } |
| 1998 | return false; |
| 1999 | } |
| 2000 | startBuf++; |
| 2001 | } |
| 2002 | return false; |
| 2003 | } |
| 2004 | |
| 2005 | static void scanToNextArgument(const char *&argRef) { |
| 2006 | int angle = 0; |
| 2007 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 2008 | if (*argRef == '<') |
| 2009 | angle++; |
| 2010 | else if (*argRef == '>') |
| 2011 | angle--; |
| 2012 | argRef++; |
| 2013 | } |
| 2014 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 2015 | } |
| 2016 | |
| 2017 | bool RewriteModernObjC::needToScanForQualifiers(QualType T) { |
| 2018 | if (T->isObjCQualifiedIdType()) |
| 2019 | return true; |
| 2020 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 2021 | if (PT->getPointeeType()->isObjCQualifiedIdType()) |
| 2022 | return true; |
| 2023 | } |
| 2024 | if (T->isObjCObjectPointerType()) { |
| 2025 | T = T->getPointeeType(); |
| 2026 | return T->isObjCQualifiedInterfaceType(); |
| 2027 | } |
| 2028 | if (T->isArrayType()) { |
| 2029 | QualType ElemTy = Context->getBaseElementType(T); |
| 2030 | return needToScanForQualifiers(ElemTy); |
| 2031 | } |
| 2032 | return false; |
| 2033 | } |
| 2034 | |
| 2035 | void RewriteModernObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) { |
| 2036 | QualType Type = E->getType(); |
| 2037 | if (needToScanForQualifiers(Type)) { |
| 2038 | SourceLocation Loc, EndLoc; |
| 2039 | |
| 2040 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) { |
| 2041 | Loc = ECE->getLParenLoc(); |
| 2042 | EndLoc = ECE->getRParenLoc(); |
| 2043 | } else { |
| 2044 | Loc = E->getLocStart(); |
| 2045 | EndLoc = E->getLocEnd(); |
| 2046 | } |
| 2047 | // This will defend against trying to rewrite synthesized expressions. |
| 2048 | if (Loc.isInvalid() || EndLoc.isInvalid()) |
| 2049 | return; |
| 2050 | |
| 2051 | const char *startBuf = SM->getCharacterData(Loc); |
| 2052 | const char *endBuf = SM->getCharacterData(EndLoc); |
| 2053 | const char *startRef = 0, *endRef = 0; |
| 2054 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2055 | // Get the locations of the startRef, endRef. |
| 2056 | SourceLocation LessLoc = Loc.getLocWithOffset(startRef-startBuf); |
| 2057 | SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-startBuf+1); |
| 2058 | // Comment out the protocol references. |
| 2059 | InsertText(LessLoc, "/*"); |
| 2060 | InsertText(GreaterLoc, "*/"); |
| 2061 | } |
| 2062 | } |
| 2063 | } |
| 2064 | |
| 2065 | void RewriteModernObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
| 2066 | SourceLocation Loc; |
| 2067 | QualType Type; |
| 2068 | const FunctionProtoType *proto = 0; |
| 2069 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 2070 | Loc = VD->getLocation(); |
| 2071 | Type = VD->getType(); |
| 2072 | } |
| 2073 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 2074 | Loc = FD->getLocation(); |
| 2075 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 2076 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
| 2077 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
| 2078 | assert(funcType && "missing function type"); |
| 2079 | proto = dyn_cast<FunctionProtoType>(funcType); |
| 2080 | if (!proto) |
| 2081 | return; |
| 2082 | Type = proto->getResultType(); |
| 2083 | } |
| 2084 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(Dcl)) { |
| 2085 | Loc = FD->getLocation(); |
| 2086 | Type = FD->getType(); |
| 2087 | } |
| 2088 | else |
| 2089 | return; |
| 2090 | |
| 2091 | if (needToScanForQualifiers(Type)) { |
| 2092 | // Since types are unique, we need to scan the buffer. |
| 2093 | |
| 2094 | const char *endBuf = SM->getCharacterData(Loc); |
| 2095 | const char *startBuf = endBuf; |
| 2096 | while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) |
| 2097 | startBuf--; // scan backward (from the decl location) for return type. |
| 2098 | const char *startRef = 0, *endRef = 0; |
| 2099 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2100 | // Get the locations of the startRef, endRef. |
| 2101 | SourceLocation LessLoc = Loc.getLocWithOffset(startRef-endBuf); |
| 2102 | SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-endBuf+1); |
| 2103 | // Comment out the protocol references. |
| 2104 | InsertText(LessLoc, "/*"); |
| 2105 | InsertText(GreaterLoc, "*/"); |
| 2106 | } |
| 2107 | } |
| 2108 | if (!proto) |
| 2109 | return; // most likely, was a variable |
| 2110 | // Now check arguments. |
| 2111 | const char *startBuf = SM->getCharacterData(Loc); |
| 2112 | const char *startFuncBuf = startBuf; |
| 2113 | for (unsigned i = 0; i < proto->getNumArgs(); i++) { |
| 2114 | if (needToScanForQualifiers(proto->getArgType(i))) { |
| 2115 | // Since types are unique, we need to scan the buffer. |
| 2116 | |
| 2117 | const char *endBuf = startBuf; |
| 2118 | // scan forward (from the decl location) for argument types. |
| 2119 | scanToNextArgument(endBuf); |
| 2120 | const char *startRef = 0, *endRef = 0; |
| 2121 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2122 | // Get the locations of the startRef, endRef. |
| 2123 | SourceLocation LessLoc = |
| 2124 | Loc.getLocWithOffset(startRef-startFuncBuf); |
| 2125 | SourceLocation GreaterLoc = |
| 2126 | Loc.getLocWithOffset(endRef-startFuncBuf+1); |
| 2127 | // Comment out the protocol references. |
| 2128 | InsertText(LessLoc, "/*"); |
| 2129 | InsertText(GreaterLoc, "*/"); |
| 2130 | } |
| 2131 | startBuf = ++endBuf; |
| 2132 | } |
| 2133 | else { |
| 2134 | // If the function name is derived from a macro expansion, then the |
| 2135 | // argument buffer will not follow the name. Need to speak with Chris. |
| 2136 | while (*startBuf && *startBuf != ')' && *startBuf != ',') |
| 2137 | startBuf++; // scan forward (from the decl location) for argument types. |
| 2138 | startBuf++; |
| 2139 | } |
| 2140 | } |
| 2141 | } |
| 2142 | |
| 2143 | void RewriteModernObjC::RewriteTypeOfDecl(VarDecl *ND) { |
| 2144 | QualType QT = ND->getType(); |
| 2145 | const Type* TypePtr = QT->getAs<Type>(); |
| 2146 | if (!isa<TypeOfExprType>(TypePtr)) |
| 2147 | return; |
| 2148 | while (isa<TypeOfExprType>(TypePtr)) { |
| 2149 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 2150 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 2151 | TypePtr = QT->getAs<Type>(); |
| 2152 | } |
| 2153 | // FIXME. This will not work for multiple declarators; as in: |
| 2154 | // __typeof__(a) b,c,d; |
| 2155 | std::string TypeAsString(QT.getAsString(Context->getPrintingPolicy())); |
| 2156 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
| 2157 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 2158 | if (ND->getInit()) { |
| 2159 | std::string Name(ND->getNameAsString()); |
| 2160 | TypeAsString += " " + Name + " = "; |
| 2161 | Expr *E = ND->getInit(); |
| 2162 | SourceLocation startLoc; |
| 2163 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 2164 | startLoc = ECE->getLParenLoc(); |
| 2165 | else |
| 2166 | startLoc = E->getLocStart(); |
| 2167 | startLoc = SM->getExpansionLoc(startLoc); |
| 2168 | const char *endBuf = SM->getCharacterData(startLoc); |
| 2169 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
| 2170 | } |
| 2171 | else { |
| 2172 | SourceLocation X = ND->getLocEnd(); |
| 2173 | X = SM->getExpansionLoc(X); |
| 2174 | const char *endBuf = SM->getCharacterData(X); |
| 2175 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
| 2176 | } |
| 2177 | } |
| 2178 | |
| 2179 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
| 2180 | void RewriteModernObjC::SynthSelGetUidFunctionDecl() { |
| 2181 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
| 2182 | SmallVector<QualType, 16> ArgTys; |
| 2183 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
| 2184 | QualType getFuncType = |
| 2185 | getSimpleFunctionType(Context->getObjCSelType(), &ArgTys[0], ArgTys.size()); |
| 2186 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
| 2187 | SourceLocation(), |
| 2188 | SourceLocation(), |
| 2189 | SelGetUidIdent, getFuncType, 0, |
| 2190 | SC_Extern, |
| 2191 | SC_None, false); |
| 2192 | } |
| 2193 | |
| 2194 | void RewriteModernObjC::RewriteFunctionDecl(FunctionDecl *FD) { |
| 2195 | // declared in <objc/objc.h> |
| 2196 | if (FD->getIdentifier() && |
| 2197 | FD->getName() == "sel_registerName") { |
| 2198 | SelGetUidFunctionDecl = FD; |
| 2199 | return; |
| 2200 | } |
| 2201 | RewriteObjCQualifiedInterfaceTypes(FD); |
| 2202 | } |
| 2203 | |
| 2204 | void RewriteModernObjC::RewriteBlockPointerType(std::string& Str, QualType Type) { |
| 2205 | std::string TypeString(Type.getAsString(Context->getPrintingPolicy())); |
| 2206 | const char *argPtr = TypeString.c_str(); |
| 2207 | if (!strchr(argPtr, '^')) { |
| 2208 | Str += TypeString; |
| 2209 | return; |
| 2210 | } |
| 2211 | while (*argPtr) { |
| 2212 | Str += (*argPtr == '^' ? '*' : *argPtr); |
| 2213 | argPtr++; |
| 2214 | } |
| 2215 | } |
| 2216 | |
| 2217 | // FIXME. Consolidate this routine with RewriteBlockPointerType. |
| 2218 | void RewriteModernObjC::RewriteBlockPointerTypeVariable(std::string& Str, |
| 2219 | ValueDecl *VD) { |
| 2220 | QualType Type = VD->getType(); |
| 2221 | std::string TypeString(Type.getAsString(Context->getPrintingPolicy())); |
| 2222 | const char *argPtr = TypeString.c_str(); |
| 2223 | int paren = 0; |
| 2224 | while (*argPtr) { |
| 2225 | switch (*argPtr) { |
| 2226 | case '(': |
| 2227 | Str += *argPtr; |
| 2228 | paren++; |
| 2229 | break; |
| 2230 | case ')': |
| 2231 | Str += *argPtr; |
| 2232 | paren--; |
| 2233 | break; |
| 2234 | case '^': |
| 2235 | Str += '*'; |
| 2236 | if (paren == 1) |
| 2237 | Str += VD->getNameAsString(); |
| 2238 | break; |
| 2239 | default: |
| 2240 | Str += *argPtr; |
| 2241 | break; |
| 2242 | } |
| 2243 | argPtr++; |
| 2244 | } |
| 2245 | } |
| 2246 | |
Fariborz Jahanian | b75f8de | 2012-04-19 00:50:01 +0000 | [diff] [blame] | 2247 | void RewriteModernObjC::RewriteBlockLiteralFunctionDecl(FunctionDecl *FD) { |
| 2248 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
| 2249 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
| 2250 | const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(funcType); |
| 2251 | if (!proto) |
| 2252 | return; |
| 2253 | QualType Type = proto->getResultType(); |
| 2254 | std::string FdStr = Type.getAsString(Context->getPrintingPolicy()); |
| 2255 | FdStr += " "; |
| 2256 | FdStr += FD->getName(); |
| 2257 | FdStr += "("; |
| 2258 | unsigned numArgs = proto->getNumArgs(); |
| 2259 | for (unsigned i = 0; i < numArgs; i++) { |
| 2260 | QualType ArgType = proto->getArgType(i); |
| 2261 | RewriteBlockPointerType(FdStr, ArgType); |
| 2262 | if (i+1 < numArgs) |
| 2263 | FdStr += ", "; |
| 2264 | } |
Fariborz Jahanian | b5863da | 2012-04-19 16:30:28 +0000 | [diff] [blame] | 2265 | if (FD->isVariadic()) { |
| 2266 | FdStr += (numArgs > 0) ? ", ...);\n" : "...);\n"; |
| 2267 | } |
| 2268 | else |
| 2269 | FdStr += ");\n"; |
Fariborz Jahanian | b75f8de | 2012-04-19 00:50:01 +0000 | [diff] [blame] | 2270 | InsertText(FunLocStart, FdStr); |
| 2271 | } |
| 2272 | |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 2273 | // SynthSuperContructorFunctionDecl - id __rw_objc_super(id obj, id super); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2274 | void RewriteModernObjC::SynthSuperContructorFunctionDecl() { |
| 2275 | if (SuperContructorFunctionDecl) |
| 2276 | return; |
| 2277 | IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super"); |
| 2278 | SmallVector<QualType, 16> ArgTys; |
| 2279 | QualType argT = Context->getObjCIdType(); |
| 2280 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2281 | ArgTys.push_back(argT); |
| 2282 | ArgTys.push_back(argT); |
| 2283 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2284 | &ArgTys[0], ArgTys.size()); |
| 2285 | SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
| 2286 | SourceLocation(), |
| 2287 | SourceLocation(), |
| 2288 | msgSendIdent, msgSendType, 0, |
| 2289 | SC_Extern, |
| 2290 | SC_None, false); |
| 2291 | } |
| 2292 | |
| 2293 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
| 2294 | void RewriteModernObjC::SynthMsgSendFunctionDecl() { |
| 2295 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
| 2296 | SmallVector<QualType, 16> ArgTys; |
| 2297 | QualType argT = Context->getObjCIdType(); |
| 2298 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2299 | ArgTys.push_back(argT); |
| 2300 | argT = Context->getObjCSelType(); |
| 2301 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2302 | ArgTys.push_back(argT); |
| 2303 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2304 | &ArgTys[0], ArgTys.size(), |
| 2305 | true /*isVariadic*/); |
| 2306 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
| 2307 | SourceLocation(), |
| 2308 | SourceLocation(), |
| 2309 | msgSendIdent, msgSendType, 0, |
| 2310 | SC_Extern, |
| 2311 | SC_None, false); |
| 2312 | } |
| 2313 | |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 2314 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(void); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2315 | void RewriteModernObjC::SynthMsgSendSuperFunctionDecl() { |
| 2316 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 2317 | SmallVector<QualType, 2> ArgTys; |
| 2318 | ArgTys.push_back(Context->VoidTy); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2319 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 2320 | &ArgTys[0], 1, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2321 | true /*isVariadic*/); |
| 2322 | MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
| 2323 | SourceLocation(), |
| 2324 | SourceLocation(), |
| 2325 | msgSendIdent, msgSendType, 0, |
| 2326 | SC_Extern, |
| 2327 | SC_None, false); |
| 2328 | } |
| 2329 | |
| 2330 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
| 2331 | void RewriteModernObjC::SynthMsgSendStretFunctionDecl() { |
| 2332 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
| 2333 | SmallVector<QualType, 16> ArgTys; |
| 2334 | QualType argT = Context->getObjCIdType(); |
| 2335 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2336 | ArgTys.push_back(argT); |
| 2337 | argT = Context->getObjCSelType(); |
| 2338 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2339 | ArgTys.push_back(argT); |
| 2340 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2341 | &ArgTys[0], ArgTys.size(), |
| 2342 | true /*isVariadic*/); |
| 2343 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
| 2344 | SourceLocation(), |
| 2345 | SourceLocation(), |
| 2346 | msgSendIdent, msgSendType, 0, |
| 2347 | SC_Extern, |
| 2348 | SC_None, false); |
| 2349 | } |
| 2350 | |
| 2351 | // SynthMsgSendSuperStretFunctionDecl - |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 2352 | // id objc_msgSendSuper_stret(void); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2353 | void RewriteModernObjC::SynthMsgSendSuperStretFunctionDecl() { |
| 2354 | IdentifierInfo *msgSendIdent = |
| 2355 | &Context->Idents.get("objc_msgSendSuper_stret"); |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 2356 | SmallVector<QualType, 2> ArgTys; |
| 2357 | ArgTys.push_back(Context->VoidTy); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2358 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 2359 | &ArgTys[0], 1, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2360 | true /*isVariadic*/); |
| 2361 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
| 2362 | SourceLocation(), |
| 2363 | SourceLocation(), |
| 2364 | msgSendIdent, msgSendType, 0, |
| 2365 | SC_Extern, |
| 2366 | SC_None, false); |
| 2367 | } |
| 2368 | |
| 2369 | // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); |
| 2370 | void RewriteModernObjC::SynthMsgSendFpretFunctionDecl() { |
| 2371 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
| 2372 | SmallVector<QualType, 16> ArgTys; |
| 2373 | QualType argT = Context->getObjCIdType(); |
| 2374 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2375 | ArgTys.push_back(argT); |
| 2376 | argT = Context->getObjCSelType(); |
| 2377 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2378 | ArgTys.push_back(argT); |
| 2379 | QualType msgSendType = getSimpleFunctionType(Context->DoubleTy, |
| 2380 | &ArgTys[0], ArgTys.size(), |
| 2381 | true /*isVariadic*/); |
| 2382 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
| 2383 | SourceLocation(), |
| 2384 | SourceLocation(), |
| 2385 | msgSendIdent, msgSendType, 0, |
| 2386 | SC_Extern, |
| 2387 | SC_None, false); |
| 2388 | } |
| 2389 | |
| 2390 | // SynthGetClassFunctionDecl - id objc_getClass(const char *name); |
| 2391 | void RewriteModernObjC::SynthGetClassFunctionDecl() { |
| 2392 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
| 2393 | SmallVector<QualType, 16> ArgTys; |
| 2394 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
| 2395 | QualType getClassType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2396 | &ArgTys[0], ArgTys.size()); |
| 2397 | GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
| 2398 | SourceLocation(), |
| 2399 | SourceLocation(), |
| 2400 | getClassIdent, getClassType, 0, |
| 2401 | SC_Extern, |
| 2402 | SC_None, false); |
| 2403 | } |
| 2404 | |
| 2405 | // SynthGetSuperClassFunctionDecl - Class class_getSuperclass(Class cls); |
| 2406 | void RewriteModernObjC::SynthGetSuperClassFunctionDecl() { |
| 2407 | IdentifierInfo *getSuperClassIdent = |
| 2408 | &Context->Idents.get("class_getSuperclass"); |
| 2409 | SmallVector<QualType, 16> ArgTys; |
| 2410 | ArgTys.push_back(Context->getObjCClassType()); |
| 2411 | QualType getClassType = getSimpleFunctionType(Context->getObjCClassType(), |
| 2412 | &ArgTys[0], ArgTys.size()); |
| 2413 | GetSuperClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
| 2414 | SourceLocation(), |
| 2415 | SourceLocation(), |
| 2416 | getSuperClassIdent, |
| 2417 | getClassType, 0, |
| 2418 | SC_Extern, |
| 2419 | SC_None, |
| 2420 | false); |
| 2421 | } |
| 2422 | |
| 2423 | // SynthGetMetaClassFunctionDecl - id objc_getMetaClass(const char *name); |
| 2424 | void RewriteModernObjC::SynthGetMetaClassFunctionDecl() { |
| 2425 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
| 2426 | SmallVector<QualType, 16> ArgTys; |
| 2427 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
| 2428 | QualType getClassType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2429 | &ArgTys[0], ArgTys.size()); |
| 2430 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
| 2431 | SourceLocation(), |
| 2432 | SourceLocation(), |
| 2433 | getClassIdent, getClassType, 0, |
| 2434 | SC_Extern, |
| 2435 | SC_None, false); |
| 2436 | } |
| 2437 | |
| 2438 | Stmt *RewriteModernObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
| 2439 | QualType strType = getConstantStringStructType(); |
| 2440 | |
| 2441 | std::string S = "__NSConstantStringImpl_"; |
| 2442 | |
| 2443 | std::string tmpName = InFileName; |
| 2444 | unsigned i; |
| 2445 | for (i=0; i < tmpName.length(); i++) { |
| 2446 | char c = tmpName.at(i); |
| 2447 | // replace any non alphanumeric characters with '_'. |
| 2448 | if (!isalpha(c) && (c < '0' || c > '9')) |
| 2449 | tmpName[i] = '_'; |
| 2450 | } |
| 2451 | S += tmpName; |
| 2452 | S += "_"; |
| 2453 | S += utostr(NumObjCStringLiterals++); |
| 2454 | |
| 2455 | Preamble += "static __NSConstantStringImpl " + S; |
| 2456 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 2457 | Preamble += "0x000007c8,"; // utf8_str |
| 2458 | // The pretty printer for StringLiteral handles escape characters properly. |
| 2459 | std::string prettyBufS; |
| 2460 | llvm::raw_string_ostream prettyBuf(prettyBufS); |
| 2461 | Exp->getString()->printPretty(prettyBuf, *Context, 0, |
| 2462 | PrintingPolicy(LangOpts)); |
| 2463 | Preamble += prettyBuf.str(); |
| 2464 | Preamble += ","; |
| 2465 | Preamble += utostr(Exp->getString()->getByteLength()) + "};\n"; |
| 2466 | |
| 2467 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
| 2468 | SourceLocation(), &Context->Idents.get(S), |
| 2469 | strType, 0, SC_Static, SC_None); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2470 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, false, strType, VK_LValue, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2471 | SourceLocation()); |
| 2472 | Expr *Unop = new (Context) UnaryOperator(DRE, UO_AddrOf, |
| 2473 | Context->getPointerType(DRE->getType()), |
| 2474 | VK_RValue, OK_Ordinary, |
| 2475 | SourceLocation()); |
| 2476 | // cast to NSConstantString * |
| 2477 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Exp->getType(), |
| 2478 | CK_CPointerToObjCPointerCast, Unop); |
| 2479 | ReplaceStmt(Exp, cast); |
| 2480 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
| 2481 | return cast; |
| 2482 | } |
| 2483 | |
Fariborz Jahanian | 5594704 | 2012-03-27 20:17:30 +0000 | [diff] [blame] | 2484 | Stmt *RewriteModernObjC::RewriteObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Exp) { |
| 2485 | unsigned IntSize = |
| 2486 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 2487 | |
| 2488 | Expr *FlagExp = IntegerLiteral::Create(*Context, |
| 2489 | llvm::APInt(IntSize, Exp->getValue()), |
| 2490 | Context->IntTy, Exp->getLocation()); |
| 2491 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Context->ObjCBuiltinBoolTy, |
| 2492 | CK_BitCast, FlagExp); |
| 2493 | ParenExpr *PE = new (Context) ParenExpr(Exp->getLocation(), Exp->getExprLoc(), |
| 2494 | cast); |
| 2495 | ReplaceStmt(Exp, PE); |
| 2496 | return PE; |
| 2497 | } |
| 2498 | |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2499 | Stmt *RewriteModernObjC::RewriteObjCBoxedExpr(ObjCBoxedExpr *Exp) { |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2500 | // synthesize declaration of helper functions needed in this routine. |
| 2501 | if (!SelGetUidFunctionDecl) |
| 2502 | SynthSelGetUidFunctionDecl(); |
| 2503 | // use objc_msgSend() for all. |
| 2504 | if (!MsgSendFunctionDecl) |
| 2505 | SynthMsgSendFunctionDecl(); |
| 2506 | if (!GetClassFunctionDecl) |
| 2507 | SynthGetClassFunctionDecl(); |
| 2508 | |
| 2509 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2510 | SourceLocation StartLoc = Exp->getLocStart(); |
| 2511 | SourceLocation EndLoc = Exp->getLocEnd(); |
| 2512 | |
| 2513 | // Synthesize a call to objc_msgSend(). |
| 2514 | SmallVector<Expr*, 4> MsgExprs; |
| 2515 | SmallVector<Expr*, 4> ClsExprs; |
| 2516 | QualType argType = Context->getPointerType(Context->CharTy); |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2517 | |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2518 | // Create a call to objc_getClass("<BoxingClass>"). It will be the 1st argument. |
| 2519 | ObjCMethodDecl *BoxingMethod = Exp->getBoxingMethod(); |
| 2520 | ObjCInterfaceDecl *BoxingClass = BoxingMethod->getClassInterface(); |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2521 | |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2522 | IdentifierInfo *clsName = BoxingClass->getIdentifier(); |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2523 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 2524 | clsName->getName(), |
| 2525 | StringLiteral::Ascii, false, |
| 2526 | argType, SourceLocation())); |
| 2527 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2528 | &ClsExprs[0], |
| 2529 | ClsExprs.size(), |
| 2530 | StartLoc, EndLoc); |
| 2531 | MsgExprs.push_back(Cls); |
| 2532 | |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2533 | // Create a call to sel_registerName("<BoxingMethod>:"), etc. |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2534 | // it will be the 2nd argument. |
| 2535 | SmallVector<Expr*, 4> SelExprs; |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2536 | SelExprs.push_back(StringLiteral::Create(*Context, |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2537 | BoxingMethod->getSelector().getAsString(), |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2538 | StringLiteral::Ascii, false, |
| 2539 | argType, SourceLocation())); |
| 2540 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2541 | &SelExprs[0], SelExprs.size(), |
| 2542 | StartLoc, EndLoc); |
| 2543 | MsgExprs.push_back(SelExp); |
| 2544 | |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2545 | // User provided sub-expression is the 3rd, and last, argument. |
| 2546 | Expr *subExpr = Exp->getSubExpr(); |
| 2547 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(subExpr)) { |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2548 | QualType type = ICE->getType(); |
| 2549 | const Expr *SubExpr = ICE->IgnoreParenImpCasts(); |
| 2550 | CastKind CK = CK_BitCast; |
| 2551 | if (SubExpr->getType()->isIntegralType(*Context) && type->isBooleanType()) |
| 2552 | CK = CK_IntegralToBoolean; |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2553 | subExpr = NoTypeInfoCStyleCastExpr(Context, type, CK, subExpr); |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2554 | } |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2555 | MsgExprs.push_back(subExpr); |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2556 | |
| 2557 | SmallVector<QualType, 4> ArgTypes; |
| 2558 | ArgTypes.push_back(Context->getObjCIdType()); |
| 2559 | ArgTypes.push_back(Context->getObjCSelType()); |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2560 | for (ObjCMethodDecl::param_iterator PI = BoxingMethod->param_begin(), |
| 2561 | E = BoxingMethod->param_end(); PI != E; ++PI) |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2562 | ArgTypes.push_back((*PI)->getType()); |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2563 | |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2564 | QualType returnType = Exp->getType(); |
| 2565 | // Get the type, we will need to reference it in a couple spots. |
| 2566 | QualType msgSendType = MsgSendFlavor->getType(); |
| 2567 | |
| 2568 | // Create a reference to the objc_msgSend() declaration. |
| 2569 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, false, msgSendType, |
| 2570 | VK_LValue, SourceLocation()); |
| 2571 | |
| 2572 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2573 | Context->getPointerType(Context->VoidTy), |
| 2574 | CK_BitCast, DRE); |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2575 | |
| 2576 | // Now do the "normal" pointer to function cast. |
| 2577 | QualType castType = |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2578 | getSimpleFunctionType(returnType, &ArgTypes[0], ArgTypes.size(), |
| 2579 | BoxingMethod->isVariadic()); |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 2580 | castType = Context->getPointerType(castType); |
| 2581 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
| 2582 | cast); |
| 2583 | |
| 2584 | // Don't forget the parens to enforce the proper binding. |
| 2585 | ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast); |
| 2586 | |
| 2587 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
| 2588 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
| 2589 | MsgExprs.size(), |
| 2590 | FT->getResultType(), VK_RValue, |
| 2591 | EndLoc); |
| 2592 | ReplaceStmt(Exp, CE); |
| 2593 | return CE; |
| 2594 | } |
| 2595 | |
Fariborz Jahanian | 86cff60 | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2596 | Stmt *RewriteModernObjC::RewriteObjCArrayLiteralExpr(ObjCArrayLiteral *Exp) { |
| 2597 | // synthesize declaration of helper functions needed in this routine. |
| 2598 | if (!SelGetUidFunctionDecl) |
| 2599 | SynthSelGetUidFunctionDecl(); |
| 2600 | // use objc_msgSend() for all. |
| 2601 | if (!MsgSendFunctionDecl) |
| 2602 | SynthMsgSendFunctionDecl(); |
| 2603 | if (!GetClassFunctionDecl) |
| 2604 | SynthGetClassFunctionDecl(); |
| 2605 | |
| 2606 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2607 | SourceLocation StartLoc = Exp->getLocStart(); |
| 2608 | SourceLocation EndLoc = Exp->getLocEnd(); |
| 2609 | |
Fariborz Jahanian | e35abe1 | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2610 | // Build the expression: __NSContainer_literal(int, ...).arr |
Fariborz Jahanian | b0f245c | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 2611 | QualType IntQT = Context->IntTy; |
| 2612 | QualType NSArrayFType = |
| 2613 | getSimpleFunctionType(Context->VoidTy, &IntQT, 1, true); |
Fariborz Jahanian | e35abe1 | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2614 | std::string NSArrayFName("__NSContainer_literal"); |
Fariborz Jahanian | b0f245c | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 2615 | FunctionDecl *NSArrayFD = SynthBlockInitFunctionDecl(NSArrayFName); |
| 2616 | DeclRefExpr *NSArrayDRE = |
| 2617 | new (Context) DeclRefExpr(NSArrayFD, false, NSArrayFType, VK_RValue, |
| 2618 | SourceLocation()); |
| 2619 | |
| 2620 | SmallVector<Expr*, 16> InitExprs; |
| 2621 | unsigned NumElements = Exp->getNumElements(); |
| 2622 | unsigned UnsignedIntSize = |
| 2623 | static_cast<unsigned>(Context->getTypeSize(Context->UnsignedIntTy)); |
| 2624 | Expr *count = IntegerLiteral::Create(*Context, |
| 2625 | llvm::APInt(UnsignedIntSize, NumElements), |
| 2626 | Context->UnsignedIntTy, SourceLocation()); |
| 2627 | InitExprs.push_back(count); |
| 2628 | for (unsigned i = 0; i < NumElements; i++) |
| 2629 | InitExprs.push_back(Exp->getElement(i)); |
| 2630 | Expr *NSArrayCallExpr = |
| 2631 | new (Context) CallExpr(*Context, NSArrayDRE, &InitExprs[0], InitExprs.size(), |
| 2632 | NSArrayFType, VK_LValue, SourceLocation()); |
| 2633 | |
| 2634 | FieldDecl *ARRFD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 2635 | SourceLocation(), |
| 2636 | &Context->Idents.get("arr"), |
| 2637 | Context->getPointerType(Context->VoidPtrTy), 0, |
| 2638 | /*BitWidth=*/0, /*Mutable=*/true, |
| 2639 | /*HasInit=*/false); |
| 2640 | MemberExpr *ArrayLiteralME = |
| 2641 | new (Context) MemberExpr(NSArrayCallExpr, false, ARRFD, |
| 2642 | SourceLocation(), |
| 2643 | ARRFD->getType(), VK_LValue, |
| 2644 | OK_Ordinary); |
| 2645 | QualType ConstIdT = Context->getObjCIdType().withConst(); |
| 2646 | CStyleCastExpr * ArrayLiteralObjects = |
| 2647 | NoTypeInfoCStyleCastExpr(Context, |
| 2648 | Context->getPointerType(ConstIdT), |
| 2649 | CK_BitCast, |
| 2650 | ArrayLiteralME); |
| 2651 | |
Fariborz Jahanian | 86cff60 | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2652 | // Synthesize a call to objc_msgSend(). |
| 2653 | SmallVector<Expr*, 32> MsgExprs; |
| 2654 | SmallVector<Expr*, 4> ClsExprs; |
| 2655 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2656 | QualType expType = Exp->getType(); |
| 2657 | |
| 2658 | // Create a call to objc_getClass("NSArray"). It will be th 1st argument. |
| 2659 | ObjCInterfaceDecl *Class = |
| 2660 | expType->getPointeeType()->getAs<ObjCObjectType>()->getInterface(); |
| 2661 | |
| 2662 | IdentifierInfo *clsName = Class->getIdentifier(); |
| 2663 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 2664 | clsName->getName(), |
| 2665 | StringLiteral::Ascii, false, |
| 2666 | argType, SourceLocation())); |
| 2667 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2668 | &ClsExprs[0], |
| 2669 | ClsExprs.size(), |
| 2670 | StartLoc, EndLoc); |
| 2671 | MsgExprs.push_back(Cls); |
| 2672 | |
| 2673 | // Create a call to sel_registerName("arrayWithObjects:count:"). |
| 2674 | // it will be the 2nd argument. |
| 2675 | SmallVector<Expr*, 4> SelExprs; |
| 2676 | ObjCMethodDecl *ArrayMethod = Exp->getArrayWithObjectsMethod(); |
| 2677 | SelExprs.push_back(StringLiteral::Create(*Context, |
| 2678 | ArrayMethod->getSelector().getAsString(), |
| 2679 | StringLiteral::Ascii, false, |
| 2680 | argType, SourceLocation())); |
| 2681 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2682 | &SelExprs[0], SelExprs.size(), |
| 2683 | StartLoc, EndLoc); |
| 2684 | MsgExprs.push_back(SelExp); |
| 2685 | |
Fariborz Jahanian | b0f245c | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 2686 | // (const id [])objects |
| 2687 | MsgExprs.push_back(ArrayLiteralObjects); |
Fariborz Jahanian | 86cff60 | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2688 | |
Fariborz Jahanian | b0f245c | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 2689 | // (NSUInteger)cnt |
| 2690 | Expr *cnt = IntegerLiteral::Create(*Context, |
| 2691 | llvm::APInt(UnsignedIntSize, NumElements), |
| 2692 | Context->UnsignedIntTy, SourceLocation()); |
| 2693 | MsgExprs.push_back(cnt); |
Fariborz Jahanian | 86cff60 | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 2694 | |
| 2695 | |
| 2696 | SmallVector<QualType, 4> ArgTypes; |
| 2697 | ArgTypes.push_back(Context->getObjCIdType()); |
| 2698 | ArgTypes.push_back(Context->getObjCSelType()); |
| 2699 | for (ObjCMethodDecl::param_iterator PI = ArrayMethod->param_begin(), |
| 2700 | E = ArrayMethod->param_end(); PI != E; ++PI) |
| 2701 | ArgTypes.push_back((*PI)->getType()); |
| 2702 | |
| 2703 | QualType returnType = Exp->getType(); |
| 2704 | // Get the type, we will need to reference it in a couple spots. |
| 2705 | QualType msgSendType = MsgSendFlavor->getType(); |
| 2706 | |
| 2707 | // Create a reference to the objc_msgSend() declaration. |
| 2708 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, false, msgSendType, |
| 2709 | VK_LValue, SourceLocation()); |
| 2710 | |
| 2711 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, |
| 2712 | Context->getPointerType(Context->VoidTy), |
| 2713 | CK_BitCast, DRE); |
| 2714 | |
| 2715 | // Now do the "normal" pointer to function cast. |
| 2716 | QualType castType = |
| 2717 | getSimpleFunctionType(returnType, &ArgTypes[0], ArgTypes.size(), |
| 2718 | ArrayMethod->isVariadic()); |
| 2719 | castType = Context->getPointerType(castType); |
| 2720 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
| 2721 | cast); |
| 2722 | |
| 2723 | // Don't forget the parens to enforce the proper binding. |
| 2724 | ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast); |
| 2725 | |
| 2726 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
| 2727 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
| 2728 | MsgExprs.size(), |
| 2729 | FT->getResultType(), VK_RValue, |
| 2730 | EndLoc); |
| 2731 | ReplaceStmt(Exp, CE); |
| 2732 | return CE; |
| 2733 | } |
| 2734 | |
Fariborz Jahanian | e35abe1 | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 2735 | Stmt *RewriteModernObjC::RewriteObjCDictionaryLiteralExpr(ObjCDictionaryLiteral *Exp) { |
| 2736 | // synthesize declaration of helper functions needed in this routine. |
| 2737 | if (!SelGetUidFunctionDecl) |
| 2738 | SynthSelGetUidFunctionDecl(); |
| 2739 | // use objc_msgSend() for all. |
| 2740 | if (!MsgSendFunctionDecl) |
| 2741 | SynthMsgSendFunctionDecl(); |
| 2742 | if (!GetClassFunctionDecl) |
| 2743 | SynthGetClassFunctionDecl(); |
| 2744 | |
| 2745 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2746 | SourceLocation StartLoc = Exp->getLocStart(); |
| 2747 | SourceLocation EndLoc = Exp->getLocEnd(); |
| 2748 | |
| 2749 | // Build the expression: __NSContainer_literal(int, ...).arr |
| 2750 | QualType IntQT = Context->IntTy; |
| 2751 | QualType NSDictFType = |
| 2752 | getSimpleFunctionType(Context->VoidTy, &IntQT, 1, true); |
| 2753 | std::string NSDictFName("__NSContainer_literal"); |
| 2754 | FunctionDecl *NSDictFD = SynthBlockInitFunctionDecl(NSDictFName); |
| 2755 | DeclRefExpr *NSDictDRE = |
| 2756 | new (Context) DeclRefExpr(NSDictFD, false, NSDictFType, VK_RValue, |
| 2757 | SourceLocation()); |
| 2758 | |
| 2759 | SmallVector<Expr*, 16> KeyExprs; |
| 2760 | SmallVector<Expr*, 16> ValueExprs; |
| 2761 | |
| 2762 | unsigned NumElements = Exp->getNumElements(); |
| 2763 | unsigned UnsignedIntSize = |
| 2764 | static_cast<unsigned>(Context->getTypeSize(Context->UnsignedIntTy)); |
| 2765 | Expr *count = IntegerLiteral::Create(*Context, |
| 2766 | llvm::APInt(UnsignedIntSize, NumElements), |
| 2767 | Context->UnsignedIntTy, SourceLocation()); |
| 2768 | KeyExprs.push_back(count); |
| 2769 | ValueExprs.push_back(count); |
| 2770 | for (unsigned i = 0; i < NumElements; i++) { |
| 2771 | ObjCDictionaryElement Element = Exp->getKeyValueElement(i); |
| 2772 | KeyExprs.push_back(Element.Key); |
| 2773 | ValueExprs.push_back(Element.Value); |
| 2774 | } |
| 2775 | |
| 2776 | // (const id [])objects |
| 2777 | Expr *NSValueCallExpr = |
| 2778 | new (Context) CallExpr(*Context, NSDictDRE, &ValueExprs[0], ValueExprs.size(), |
| 2779 | NSDictFType, VK_LValue, SourceLocation()); |
| 2780 | |
| 2781 | FieldDecl *ARRFD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 2782 | SourceLocation(), |
| 2783 | &Context->Idents.get("arr"), |
| 2784 | Context->getPointerType(Context->VoidPtrTy), 0, |
| 2785 | /*BitWidth=*/0, /*Mutable=*/true, |
| 2786 | /*HasInit=*/false); |
| 2787 | MemberExpr *DictLiteralValueME = |
| 2788 | new (Context) MemberExpr(NSValueCallExpr, false, ARRFD, |
| 2789 | SourceLocation(), |
| 2790 | ARRFD->getType(), VK_LValue, |
| 2791 | OK_Ordinary); |
| 2792 | QualType ConstIdT = Context->getObjCIdType().withConst(); |
| 2793 | CStyleCastExpr * DictValueObjects = |
| 2794 | NoTypeInfoCStyleCastExpr(Context, |
| 2795 | Context->getPointerType(ConstIdT), |
| 2796 | CK_BitCast, |
| 2797 | DictLiteralValueME); |
| 2798 | // (const id <NSCopying> [])keys |
| 2799 | Expr *NSKeyCallExpr = |
| 2800 | new (Context) CallExpr(*Context, NSDictDRE, &KeyExprs[0], KeyExprs.size(), |
| 2801 | NSDictFType, VK_LValue, SourceLocation()); |
| 2802 | |
| 2803 | MemberExpr *DictLiteralKeyME = |
| 2804 | new (Context) MemberExpr(NSKeyCallExpr, false, ARRFD, |
| 2805 | SourceLocation(), |
| 2806 | ARRFD->getType(), VK_LValue, |
| 2807 | OK_Ordinary); |
| 2808 | |
| 2809 | CStyleCastExpr * DictKeyObjects = |
| 2810 | NoTypeInfoCStyleCastExpr(Context, |
| 2811 | Context->getPointerType(ConstIdT), |
| 2812 | CK_BitCast, |
| 2813 | DictLiteralKeyME); |
| 2814 | |
| 2815 | |
| 2816 | |
| 2817 | // Synthesize a call to objc_msgSend(). |
| 2818 | SmallVector<Expr*, 32> MsgExprs; |
| 2819 | SmallVector<Expr*, 4> ClsExprs; |
| 2820 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2821 | QualType expType = Exp->getType(); |
| 2822 | |
| 2823 | // Create a call to objc_getClass("NSArray"). It will be th 1st argument. |
| 2824 | ObjCInterfaceDecl *Class = |
| 2825 | expType->getPointeeType()->getAs<ObjCObjectType>()->getInterface(); |
| 2826 | |
| 2827 | IdentifierInfo *clsName = Class->getIdentifier(); |
| 2828 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 2829 | clsName->getName(), |
| 2830 | StringLiteral::Ascii, false, |
| 2831 | argType, SourceLocation())); |
| 2832 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2833 | &ClsExprs[0], |
| 2834 | ClsExprs.size(), |
| 2835 | StartLoc, EndLoc); |
| 2836 | MsgExprs.push_back(Cls); |
| 2837 | |
| 2838 | // Create a call to sel_registerName("arrayWithObjects:count:"). |
| 2839 | // it will be the 2nd argument. |
| 2840 | SmallVector<Expr*, 4> SelExprs; |
| 2841 | ObjCMethodDecl *DictMethod = Exp->getDictWithObjectsMethod(); |
| 2842 | SelExprs.push_back(StringLiteral::Create(*Context, |
| 2843 | DictMethod->getSelector().getAsString(), |
| 2844 | StringLiteral::Ascii, false, |
| 2845 | argType, SourceLocation())); |
| 2846 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2847 | &SelExprs[0], SelExprs.size(), |
| 2848 | StartLoc, EndLoc); |
| 2849 | MsgExprs.push_back(SelExp); |
| 2850 | |
| 2851 | // (const id [])objects |
| 2852 | MsgExprs.push_back(DictValueObjects); |
| 2853 | |
| 2854 | // (const id <NSCopying> [])keys |
| 2855 | MsgExprs.push_back(DictKeyObjects); |
| 2856 | |
| 2857 | // (NSUInteger)cnt |
| 2858 | Expr *cnt = IntegerLiteral::Create(*Context, |
| 2859 | llvm::APInt(UnsignedIntSize, NumElements), |
| 2860 | Context->UnsignedIntTy, SourceLocation()); |
| 2861 | MsgExprs.push_back(cnt); |
| 2862 | |
| 2863 | |
| 2864 | SmallVector<QualType, 8> ArgTypes; |
| 2865 | ArgTypes.push_back(Context->getObjCIdType()); |
| 2866 | ArgTypes.push_back(Context->getObjCSelType()); |
| 2867 | for (ObjCMethodDecl::param_iterator PI = DictMethod->param_begin(), |
| 2868 | E = DictMethod->param_end(); PI != E; ++PI) { |
| 2869 | QualType T = (*PI)->getType(); |
| 2870 | if (const PointerType* PT = T->getAs<PointerType>()) { |
| 2871 | QualType PointeeTy = PT->getPointeeType(); |
| 2872 | convertToUnqualifiedObjCType(PointeeTy); |
| 2873 | T = Context->getPointerType(PointeeTy); |
| 2874 | } |
| 2875 | ArgTypes.push_back(T); |
| 2876 | } |
| 2877 | |
| 2878 | QualType returnType = Exp->getType(); |
| 2879 | // Get the type, we will need to reference it in a couple spots. |
| 2880 | QualType msgSendType = MsgSendFlavor->getType(); |
| 2881 | |
| 2882 | // Create a reference to the objc_msgSend() declaration. |
| 2883 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, false, msgSendType, |
| 2884 | VK_LValue, SourceLocation()); |
| 2885 | |
| 2886 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, |
| 2887 | Context->getPointerType(Context->VoidTy), |
| 2888 | CK_BitCast, DRE); |
| 2889 | |
| 2890 | // Now do the "normal" pointer to function cast. |
| 2891 | QualType castType = |
| 2892 | getSimpleFunctionType(returnType, &ArgTypes[0], ArgTypes.size(), |
| 2893 | DictMethod->isVariadic()); |
| 2894 | castType = Context->getPointerType(castType); |
| 2895 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
| 2896 | cast); |
| 2897 | |
| 2898 | // Don't forget the parens to enforce the proper binding. |
| 2899 | ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast); |
| 2900 | |
| 2901 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
| 2902 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
| 2903 | MsgExprs.size(), |
| 2904 | FT->getResultType(), VK_RValue, |
| 2905 | EndLoc); |
| 2906 | ReplaceStmt(Exp, CE); |
| 2907 | return CE; |
| 2908 | } |
| 2909 | |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 2910 | // struct __rw_objc_super { |
| 2911 | // struct objc_object *object; struct objc_object *superClass; |
| 2912 | // }; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2913 | QualType RewriteModernObjC::getSuperStructType() { |
| 2914 | if (!SuperStructDecl) { |
| 2915 | SuperStructDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 2916 | SourceLocation(), SourceLocation(), |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 2917 | &Context->Idents.get("__rw_objc_super")); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2918 | QualType FieldTypes[2]; |
| 2919 | |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 2920 | // struct objc_object *object; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2921 | FieldTypes[0] = Context->getObjCIdType(); |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 2922 | // struct objc_object *superClass; |
| 2923 | FieldTypes[1] = Context->getObjCIdType(); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2924 | |
| 2925 | // Create fields |
| 2926 | for (unsigned i = 0; i < 2; ++i) { |
| 2927 | SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl, |
| 2928 | SourceLocation(), |
| 2929 | SourceLocation(), 0, |
| 2930 | FieldTypes[i], 0, |
| 2931 | /*BitWidth=*/0, |
| 2932 | /*Mutable=*/false, |
| 2933 | /*HasInit=*/false)); |
| 2934 | } |
| 2935 | |
| 2936 | SuperStructDecl->completeDefinition(); |
| 2937 | } |
| 2938 | return Context->getTagDeclType(SuperStructDecl); |
| 2939 | } |
| 2940 | |
| 2941 | QualType RewriteModernObjC::getConstantStringStructType() { |
| 2942 | if (!ConstantStringDecl) { |
| 2943 | ConstantStringDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 2944 | SourceLocation(), SourceLocation(), |
| 2945 | &Context->Idents.get("__NSConstantStringImpl")); |
| 2946 | QualType FieldTypes[4]; |
| 2947 | |
| 2948 | // struct objc_object *receiver; |
| 2949 | FieldTypes[0] = Context->getObjCIdType(); |
| 2950 | // int flags; |
| 2951 | FieldTypes[1] = Context->IntTy; |
| 2952 | // char *str; |
| 2953 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
| 2954 | // long length; |
| 2955 | FieldTypes[3] = Context->LongTy; |
| 2956 | |
| 2957 | // Create fields |
| 2958 | for (unsigned i = 0; i < 4; ++i) { |
| 2959 | ConstantStringDecl->addDecl(FieldDecl::Create(*Context, |
| 2960 | ConstantStringDecl, |
| 2961 | SourceLocation(), |
| 2962 | SourceLocation(), 0, |
| 2963 | FieldTypes[i], 0, |
| 2964 | /*BitWidth=*/0, |
| 2965 | /*Mutable=*/true, |
| 2966 | /*HasInit=*/false)); |
| 2967 | } |
| 2968 | |
| 2969 | ConstantStringDecl->completeDefinition(); |
| 2970 | } |
| 2971 | return Context->getTagDeclType(ConstantStringDecl); |
| 2972 | } |
| 2973 | |
| 2974 | Stmt *RewriteModernObjC::SynthMessageExpr(ObjCMessageExpr *Exp, |
| 2975 | SourceLocation StartLoc, |
| 2976 | SourceLocation EndLoc) { |
| 2977 | if (!SelGetUidFunctionDecl) |
| 2978 | SynthSelGetUidFunctionDecl(); |
| 2979 | if (!MsgSendFunctionDecl) |
| 2980 | SynthMsgSendFunctionDecl(); |
| 2981 | if (!MsgSendSuperFunctionDecl) |
| 2982 | SynthMsgSendSuperFunctionDecl(); |
| 2983 | if (!MsgSendStretFunctionDecl) |
| 2984 | SynthMsgSendStretFunctionDecl(); |
| 2985 | if (!MsgSendSuperStretFunctionDecl) |
| 2986 | SynthMsgSendSuperStretFunctionDecl(); |
| 2987 | if (!MsgSendFpretFunctionDecl) |
| 2988 | SynthMsgSendFpretFunctionDecl(); |
| 2989 | if (!GetClassFunctionDecl) |
| 2990 | SynthGetClassFunctionDecl(); |
| 2991 | if (!GetSuperClassFunctionDecl) |
| 2992 | SynthGetSuperClassFunctionDecl(); |
| 2993 | if (!GetMetaClassFunctionDecl) |
| 2994 | SynthGetMetaClassFunctionDecl(); |
| 2995 | |
| 2996 | // default to objc_msgSend(). |
| 2997 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2998 | // May need to use objc_msgSend_stret() as well. |
| 2999 | FunctionDecl *MsgSendStretFlavor = 0; |
| 3000 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
| 3001 | QualType resultType = mDecl->getResultType(); |
| 3002 | if (resultType->isRecordType()) |
| 3003 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
| 3004 | else if (resultType->isRealFloatingType()) |
| 3005 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
| 3006 | } |
| 3007 | |
| 3008 | // Synthesize a call to objc_msgSend(). |
| 3009 | SmallVector<Expr*, 8> MsgExprs; |
| 3010 | switch (Exp->getReceiverKind()) { |
| 3011 | case ObjCMessageExpr::SuperClass: { |
| 3012 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 3013 | if (MsgSendStretFlavor) |
| 3014 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 3015 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 3016 | |
| 3017 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
| 3018 | |
| 3019 | SmallVector<Expr*, 4> InitExprs; |
| 3020 | |
| 3021 | // set the receiver to self, the first argument to all methods. |
| 3022 | InitExprs.push_back( |
| 3023 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 3024 | CK_BitCast, |
| 3025 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3026 | false, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3027 | Context->getObjCIdType(), |
| 3028 | VK_RValue, |
| 3029 | SourceLocation())) |
| 3030 | ); // set the 'receiver'. |
| 3031 | |
| 3032 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 3033 | SmallVector<Expr*, 8> ClsExprs; |
| 3034 | QualType argType = Context->getPointerType(Context->CharTy); |
| 3035 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 3036 | ClassDecl->getIdentifier()->getName(), |
| 3037 | StringLiteral::Ascii, false, |
| 3038 | argType, SourceLocation())); |
| 3039 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
| 3040 | &ClsExprs[0], |
| 3041 | ClsExprs.size(), |
| 3042 | StartLoc, |
| 3043 | EndLoc); |
| 3044 | // (Class)objc_getClass("CurrentClass") |
| 3045 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 3046 | Context->getObjCClassType(), |
| 3047 | CK_BitCast, Cls); |
| 3048 | ClsExprs.clear(); |
| 3049 | ClsExprs.push_back(ArgExpr); |
| 3050 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 3051 | &ClsExprs[0], ClsExprs.size(), |
| 3052 | StartLoc, EndLoc); |
| 3053 | |
| 3054 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 3055 | // To turn off a warning, type-cast to 'id' |
| 3056 | InitExprs.push_back( // set 'super class', using class_getSuperclass(). |
| 3057 | NoTypeInfoCStyleCastExpr(Context, |
| 3058 | Context->getObjCIdType(), |
| 3059 | CK_BitCast, Cls)); |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3060 | // struct __rw_objc_super |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3061 | QualType superType = getSuperStructType(); |
| 3062 | Expr *SuperRep; |
| 3063 | |
| 3064 | if (LangOpts.MicrosoftExt) { |
| 3065 | SynthSuperContructorFunctionDecl(); |
| 3066 | // Simulate a contructor call... |
| 3067 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3068 | false, superType, VK_LValue, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3069 | SourceLocation()); |
| 3070 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
| 3071 | InitExprs.size(), |
| 3072 | superType, VK_LValue, |
| 3073 | SourceLocation()); |
| 3074 | // The code for super is a little tricky to prevent collision with |
| 3075 | // the structure definition in the header. The rewriter has it's own |
| 3076 | // internal definition (__rw_objc_super) that is uses. This is why |
| 3077 | // we need the cast below. For example: |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3078 | // (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] | 3079 | // |
| 3080 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
| 3081 | Context->getPointerType(SuperRep->getType()), |
| 3082 | VK_RValue, OK_Ordinary, |
| 3083 | SourceLocation()); |
| 3084 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 3085 | Context->getPointerType(superType), |
| 3086 | CK_BitCast, SuperRep); |
| 3087 | } else { |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3088 | // (struct __rw_objc_super) { <exprs from above> } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3089 | InitListExpr *ILE = |
| 3090 | new (Context) InitListExpr(*Context, SourceLocation(), |
| 3091 | &InitExprs[0], InitExprs.size(), |
| 3092 | SourceLocation()); |
| 3093 | TypeSourceInfo *superTInfo |
| 3094 | = Context->getTrivialTypeSourceInfo(superType); |
| 3095 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
| 3096 | superType, VK_LValue, |
| 3097 | ILE, false); |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3098 | // struct __rw_objc_super * |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3099 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
| 3100 | Context->getPointerType(SuperRep->getType()), |
| 3101 | VK_RValue, OK_Ordinary, |
| 3102 | SourceLocation()); |
| 3103 | } |
| 3104 | MsgExprs.push_back(SuperRep); |
| 3105 | break; |
| 3106 | } |
| 3107 | |
| 3108 | case ObjCMessageExpr::Class: { |
| 3109 | SmallVector<Expr*, 8> ClsExprs; |
| 3110 | QualType argType = Context->getPointerType(Context->CharTy); |
| 3111 | ObjCInterfaceDecl *Class |
| 3112 | = Exp->getClassReceiver()->getAs<ObjCObjectType>()->getInterface(); |
| 3113 | IdentifierInfo *clsName = Class->getIdentifier(); |
| 3114 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 3115 | clsName->getName(), |
| 3116 | StringLiteral::Ascii, false, |
| 3117 | argType, SourceLocation())); |
| 3118 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 3119 | &ClsExprs[0], |
| 3120 | ClsExprs.size(), |
| 3121 | StartLoc, EndLoc); |
| 3122 | MsgExprs.push_back(Cls); |
| 3123 | break; |
| 3124 | } |
| 3125 | |
| 3126 | case ObjCMessageExpr::SuperInstance:{ |
| 3127 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 3128 | if (MsgSendStretFlavor) |
| 3129 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 3130 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 3131 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
| 3132 | SmallVector<Expr*, 4> InitExprs; |
| 3133 | |
| 3134 | InitExprs.push_back( |
| 3135 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 3136 | CK_BitCast, |
| 3137 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3138 | false, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3139 | Context->getObjCIdType(), |
| 3140 | VK_RValue, SourceLocation())) |
| 3141 | ); // set the 'receiver'. |
| 3142 | |
| 3143 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 3144 | SmallVector<Expr*, 8> ClsExprs; |
| 3145 | QualType argType = Context->getPointerType(Context->CharTy); |
| 3146 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 3147 | ClassDecl->getIdentifier()->getName(), |
| 3148 | StringLiteral::Ascii, false, argType, |
| 3149 | SourceLocation())); |
| 3150 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 3151 | &ClsExprs[0], |
| 3152 | ClsExprs.size(), |
| 3153 | StartLoc, EndLoc); |
| 3154 | // (Class)objc_getClass("CurrentClass") |
| 3155 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 3156 | Context->getObjCClassType(), |
| 3157 | CK_BitCast, Cls); |
| 3158 | ClsExprs.clear(); |
| 3159 | ClsExprs.push_back(ArgExpr); |
| 3160 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 3161 | &ClsExprs[0], ClsExprs.size(), |
| 3162 | StartLoc, EndLoc); |
| 3163 | |
| 3164 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 3165 | // To turn off a warning, type-cast to 'id' |
| 3166 | InitExprs.push_back( |
| 3167 | // set 'super class', using class_getSuperclass(). |
| 3168 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 3169 | CK_BitCast, Cls)); |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3170 | // struct __rw_objc_super |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3171 | QualType superType = getSuperStructType(); |
| 3172 | Expr *SuperRep; |
| 3173 | |
| 3174 | if (LangOpts.MicrosoftExt) { |
| 3175 | SynthSuperContructorFunctionDecl(); |
| 3176 | // Simulate a contructor call... |
| 3177 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3178 | false, superType, VK_LValue, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3179 | SourceLocation()); |
| 3180 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
| 3181 | InitExprs.size(), |
| 3182 | superType, VK_LValue, SourceLocation()); |
| 3183 | // The code for super is a little tricky to prevent collision with |
| 3184 | // the structure definition in the header. The rewriter has it's own |
| 3185 | // internal definition (__rw_objc_super) that is uses. This is why |
| 3186 | // we need the cast below. For example: |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3187 | // (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] | 3188 | // |
| 3189 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
| 3190 | Context->getPointerType(SuperRep->getType()), |
| 3191 | VK_RValue, OK_Ordinary, |
| 3192 | SourceLocation()); |
| 3193 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 3194 | Context->getPointerType(superType), |
| 3195 | CK_BitCast, SuperRep); |
| 3196 | } else { |
Fariborz Jahanian | b20c46e | 2012-04-13 16:20:05 +0000 | [diff] [blame] | 3197 | // (struct __rw_objc_super) { <exprs from above> } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3198 | InitListExpr *ILE = |
| 3199 | new (Context) InitListExpr(*Context, SourceLocation(), |
| 3200 | &InitExprs[0], InitExprs.size(), |
| 3201 | SourceLocation()); |
| 3202 | TypeSourceInfo *superTInfo |
| 3203 | = Context->getTrivialTypeSourceInfo(superType); |
| 3204 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
| 3205 | superType, VK_RValue, ILE, |
| 3206 | false); |
| 3207 | } |
| 3208 | MsgExprs.push_back(SuperRep); |
| 3209 | break; |
| 3210 | } |
| 3211 | |
| 3212 | case ObjCMessageExpr::Instance: { |
| 3213 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 3214 | // Foo<Proto> *. |
| 3215 | Expr *recExpr = Exp->getInstanceReceiver(); |
| 3216 | while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr)) |
| 3217 | recExpr = CE->getSubExpr(); |
| 3218 | CastKind CK = recExpr->getType()->isObjCObjectPointerType() |
| 3219 | ? CK_BitCast : recExpr->getType()->isBlockPointerType() |
| 3220 | ? CK_BlockPointerToObjCPointerCast |
| 3221 | : CK_CPointerToObjCPointerCast; |
| 3222 | |
| 3223 | recExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 3224 | CK, recExpr); |
| 3225 | MsgExprs.push_back(recExpr); |
| 3226 | break; |
| 3227 | } |
| 3228 | } |
| 3229 | |
| 3230 | // Create a call to sel_registerName("selName"), it will be the 2nd argument. |
| 3231 | SmallVector<Expr*, 8> SelExprs; |
| 3232 | QualType argType = Context->getPointerType(Context->CharTy); |
| 3233 | SelExprs.push_back(StringLiteral::Create(*Context, |
| 3234 | Exp->getSelector().getAsString(), |
| 3235 | StringLiteral::Ascii, false, |
| 3236 | argType, SourceLocation())); |
| 3237 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 3238 | &SelExprs[0], SelExprs.size(), |
| 3239 | StartLoc, |
| 3240 | EndLoc); |
| 3241 | MsgExprs.push_back(SelExp); |
| 3242 | |
| 3243 | // Now push any user supplied arguments. |
| 3244 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
| 3245 | Expr *userExpr = Exp->getArg(i); |
| 3246 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 3247 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 3248 | // Reuse the ICE type, it is exactly what the doctor ordered. |
| 3249 | QualType type = ICE->getType(); |
| 3250 | if (needToScanForQualifiers(type)) |
| 3251 | type = Context->getObjCIdType(); |
| 3252 | // Make sure we convert "type (^)(...)" to "type (*)(...)". |
| 3253 | (void)convertBlockPointerToFunctionPointer(type); |
| 3254 | const Expr *SubExpr = ICE->IgnoreParenImpCasts(); |
| 3255 | CastKind CK; |
| 3256 | if (SubExpr->getType()->isIntegralType(*Context) && |
| 3257 | type->isBooleanType()) { |
| 3258 | CK = CK_IntegralToBoolean; |
| 3259 | } else if (type->isObjCObjectPointerType()) { |
| 3260 | if (SubExpr->getType()->isBlockPointerType()) { |
| 3261 | CK = CK_BlockPointerToObjCPointerCast; |
| 3262 | } else if (SubExpr->getType()->isPointerType()) { |
| 3263 | CK = CK_CPointerToObjCPointerCast; |
| 3264 | } else { |
| 3265 | CK = CK_BitCast; |
| 3266 | } |
| 3267 | } else { |
| 3268 | CK = CK_BitCast; |
| 3269 | } |
| 3270 | |
| 3271 | userExpr = NoTypeInfoCStyleCastExpr(Context, type, CK, userExpr); |
| 3272 | } |
| 3273 | // Make id<P...> cast into an 'id' cast. |
| 3274 | else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) { |
| 3275 | if (CE->getType()->isObjCQualifiedIdType()) { |
| 3276 | while ((CE = dyn_cast<CStyleCastExpr>(userExpr))) |
| 3277 | userExpr = CE->getSubExpr(); |
| 3278 | CastKind CK; |
| 3279 | if (userExpr->getType()->isIntegralType(*Context)) { |
| 3280 | CK = CK_IntegralToPointer; |
| 3281 | } else if (userExpr->getType()->isBlockPointerType()) { |
| 3282 | CK = CK_BlockPointerToObjCPointerCast; |
| 3283 | } else if (userExpr->getType()->isPointerType()) { |
| 3284 | CK = CK_CPointerToObjCPointerCast; |
| 3285 | } else { |
| 3286 | CK = CK_BitCast; |
| 3287 | } |
| 3288 | userExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 3289 | CK, userExpr); |
| 3290 | } |
| 3291 | } |
| 3292 | MsgExprs.push_back(userExpr); |
| 3293 | // We've transferred the ownership to MsgExprs. For now, we *don't* null |
| 3294 | // out the argument in the original expression (since we aren't deleting |
| 3295 | // the ObjCMessageExpr). See RewritePropertyOrImplicitSetter() usage for more info. |
| 3296 | //Exp->setArg(i, 0); |
| 3297 | } |
| 3298 | // Generate the funky cast. |
| 3299 | CastExpr *cast; |
| 3300 | SmallVector<QualType, 8> ArgTypes; |
| 3301 | QualType returnType; |
| 3302 | |
| 3303 | // Push 'id' and 'SEL', the 2 implicit arguments. |
| 3304 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 3305 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 3306 | else |
| 3307 | ArgTypes.push_back(Context->getObjCIdType()); |
| 3308 | ArgTypes.push_back(Context->getObjCSelType()); |
| 3309 | if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) { |
| 3310 | // Push any user argument types. |
| 3311 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 3312 | E = OMD->param_end(); PI != E; ++PI) { |
| 3313 | QualType t = (*PI)->getType()->isObjCQualifiedIdType() |
| 3314 | ? Context->getObjCIdType() |
| 3315 | : (*PI)->getType(); |
| 3316 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
| 3317 | (void)convertBlockPointerToFunctionPointer(t); |
| 3318 | ArgTypes.push_back(t); |
| 3319 | } |
| 3320 | returnType = Exp->getType(); |
| 3321 | convertToUnqualifiedObjCType(returnType); |
| 3322 | (void)convertBlockPointerToFunctionPointer(returnType); |
| 3323 | } else { |
| 3324 | returnType = Context->getObjCIdType(); |
| 3325 | } |
| 3326 | // Get the type, we will need to reference it in a couple spots. |
| 3327 | QualType msgSendType = MsgSendFlavor->getType(); |
| 3328 | |
| 3329 | // Create a reference to the objc_msgSend() declaration. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3330 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, false, msgSendType, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3331 | VK_LValue, SourceLocation()); |
| 3332 | |
| 3333 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
| 3334 | // If we don't do this cast, we get the following bizarre warning/note: |
| 3335 | // xx.m:13: warning: function called through a non-compatible type |
| 3336 | // xx.m:13: note: if this code is reached, the program will abort |
| 3337 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 3338 | Context->getPointerType(Context->VoidTy), |
| 3339 | CK_BitCast, DRE); |
| 3340 | |
| 3341 | // Now do the "normal" pointer to function cast. |
| 3342 | QualType castType = |
| 3343 | getSimpleFunctionType(returnType, &ArgTypes[0], ArgTypes.size(), |
| 3344 | // If we don't have a method decl, force a variadic cast. |
| 3345 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true); |
| 3346 | castType = Context->getPointerType(castType); |
| 3347 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
| 3348 | cast); |
| 3349 | |
| 3350 | // Don't forget the parens to enforce the proper binding. |
| 3351 | ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast); |
| 3352 | |
| 3353 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
| 3354 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
| 3355 | MsgExprs.size(), |
| 3356 | FT->getResultType(), VK_RValue, |
| 3357 | EndLoc); |
| 3358 | Stmt *ReplacingStmt = CE; |
| 3359 | if (MsgSendStretFlavor) { |
| 3360 | // We have the method which returns a struct/union. Must also generate |
| 3361 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 3362 | // expression which dictate which one to envoke depending on size of |
| 3363 | // method's return type. |
| 3364 | |
| 3365 | // Create a reference to the objc_msgSend_stret() declaration. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3366 | DeclRefExpr *STDRE = new (Context) DeclRefExpr(MsgSendStretFlavor, |
| 3367 | false, msgSendType, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3368 | VK_LValue, SourceLocation()); |
| 3369 | // Need to cast objc_msgSend_stret to "void *" (see above comment). |
| 3370 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 3371 | Context->getPointerType(Context->VoidTy), |
| 3372 | CK_BitCast, STDRE); |
| 3373 | // Now do the "normal" pointer to function cast. |
| 3374 | castType = getSimpleFunctionType(returnType, &ArgTypes[0], ArgTypes.size(), |
| 3375 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false); |
| 3376 | castType = Context->getPointerType(castType); |
| 3377 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
| 3378 | cast); |
| 3379 | |
| 3380 | // Don't forget the parens to enforce the proper binding. |
| 3381 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
| 3382 | |
| 3383 | FT = msgSendType->getAs<FunctionType>(); |
| 3384 | CallExpr *STCE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
| 3385 | MsgExprs.size(), |
| 3386 | FT->getResultType(), VK_RValue, |
| 3387 | SourceLocation()); |
| 3388 | |
| 3389 | // Build sizeof(returnType) |
| 3390 | UnaryExprOrTypeTraitExpr *sizeofExpr = |
| 3391 | new (Context) UnaryExprOrTypeTraitExpr(UETT_SizeOf, |
| 3392 | Context->getTrivialTypeSourceInfo(returnType), |
| 3393 | Context->getSizeType(), SourceLocation(), |
| 3394 | SourceLocation()); |
| 3395 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 3396 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 3397 | // For X86 it is more complicated and some kind of target specific routine |
| 3398 | // is needed to decide what to do. |
| 3399 | unsigned IntSize = |
| 3400 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 3401 | IntegerLiteral *limit = IntegerLiteral::Create(*Context, |
| 3402 | llvm::APInt(IntSize, 8), |
| 3403 | Context->IntTy, |
| 3404 | SourceLocation()); |
| 3405 | BinaryOperator *lessThanExpr = |
| 3406 | new (Context) BinaryOperator(sizeofExpr, limit, BO_LE, Context->IntTy, |
| 3407 | VK_RValue, OK_Ordinary, SourceLocation()); |
| 3408 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 3409 | ConditionalOperator *CondExpr = |
| 3410 | new (Context) ConditionalOperator(lessThanExpr, |
| 3411 | SourceLocation(), CE, |
| 3412 | SourceLocation(), STCE, |
| 3413 | returnType, VK_RValue, OK_Ordinary); |
| 3414 | ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3415 | CondExpr); |
| 3416 | } |
| 3417 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
| 3418 | return ReplacingStmt; |
| 3419 | } |
| 3420 | |
| 3421 | Stmt *RewriteModernObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
| 3422 | Stmt *ReplacingStmt = SynthMessageExpr(Exp, Exp->getLocStart(), |
| 3423 | Exp->getLocEnd()); |
| 3424 | |
| 3425 | // Now do the actual rewrite. |
| 3426 | ReplaceStmt(Exp, ReplacingStmt); |
| 3427 | |
| 3428 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
| 3429 | return ReplacingStmt; |
| 3430 | } |
| 3431 | |
| 3432 | // typedef struct objc_object Protocol; |
| 3433 | QualType RewriteModernObjC::getProtocolType() { |
| 3434 | if (!ProtocolTypeDecl) { |
| 3435 | TypeSourceInfo *TInfo |
| 3436 | = Context->getTrivialTypeSourceInfo(Context->getObjCIdType()); |
| 3437 | ProtocolTypeDecl = TypedefDecl::Create(*Context, TUDecl, |
| 3438 | SourceLocation(), SourceLocation(), |
| 3439 | &Context->Idents.get("Protocol"), |
| 3440 | TInfo); |
| 3441 | } |
| 3442 | return Context->getTypeDeclType(ProtocolTypeDecl); |
| 3443 | } |
| 3444 | |
| 3445 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
| 3446 | /// a synthesized/forward data reference (to the protocol's metadata). |
| 3447 | /// The forward references (and metadata) are generated in |
| 3448 | /// RewriteModernObjC::HandleTranslationUnit(). |
| 3449 | Stmt *RewriteModernObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
Fariborz Jahanian | 30650eb | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 3450 | std::string Name = "_OBJC_PROTOCOL_REFERENCE_$_" + |
| 3451 | Exp->getProtocol()->getNameAsString(); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3452 | IdentifierInfo *ID = &Context->Idents.get(Name); |
| 3453 | VarDecl *VD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
| 3454 | SourceLocation(), ID, getProtocolType(), 0, |
| 3455 | SC_Extern, SC_None); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3456 | DeclRefExpr *DRE = new (Context) DeclRefExpr(VD, false, getProtocolType(), |
| 3457 | VK_LValue, SourceLocation()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3458 | Expr *DerefExpr = new (Context) UnaryOperator(DRE, UO_AddrOf, |
| 3459 | Context->getPointerType(DRE->getType()), |
| 3460 | VK_RValue, OK_Ordinary, SourceLocation()); |
| 3461 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, DerefExpr->getType(), |
| 3462 | CK_BitCast, |
| 3463 | DerefExpr); |
| 3464 | ReplaceStmt(Exp, castExpr); |
| 3465 | ProtocolExprDecls.insert(Exp->getProtocol()->getCanonicalDecl()); |
| 3466 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
| 3467 | return castExpr; |
| 3468 | |
| 3469 | } |
| 3470 | |
| 3471 | bool RewriteModernObjC::BufferContainsPPDirectives(const char *startBuf, |
| 3472 | const char *endBuf) { |
| 3473 | while (startBuf < endBuf) { |
| 3474 | if (*startBuf == '#') { |
| 3475 | // Skip whitespace. |
| 3476 | for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf) |
| 3477 | ; |
| 3478 | if (!strncmp(startBuf, "if", strlen("if")) || |
| 3479 | !strncmp(startBuf, "ifdef", strlen("ifdef")) || |
| 3480 | !strncmp(startBuf, "ifndef", strlen("ifndef")) || |
| 3481 | !strncmp(startBuf, "define", strlen("define")) || |
| 3482 | !strncmp(startBuf, "undef", strlen("undef")) || |
| 3483 | !strncmp(startBuf, "else", strlen("else")) || |
| 3484 | !strncmp(startBuf, "elif", strlen("elif")) || |
| 3485 | !strncmp(startBuf, "endif", strlen("endif")) || |
| 3486 | !strncmp(startBuf, "pragma", strlen("pragma")) || |
| 3487 | !strncmp(startBuf, "include", strlen("include")) || |
| 3488 | !strncmp(startBuf, "import", strlen("import")) || |
| 3489 | !strncmp(startBuf, "include_next", strlen("include_next"))) |
| 3490 | return true; |
| 3491 | } |
| 3492 | startBuf++; |
| 3493 | } |
| 3494 | return false; |
| 3495 | } |
| 3496 | |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3497 | /// RewriteObjCFieldDeclType - This routine rewrites a type into the buffer. |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3498 | /// It handles elaborated types, as well as enum types in the process. |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3499 | bool RewriteModernObjC::RewriteObjCFieldDeclType(QualType &Type, |
| 3500 | std::string &Result) { |
| 3501 | if (Type->isArrayType()) { |
| 3502 | QualType ElemTy = Context->getBaseElementType(Type); |
| 3503 | return RewriteObjCFieldDeclType(ElemTy, Result); |
| 3504 | } |
| 3505 | else if (Type->isRecordType()) { |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3506 | RecordDecl *RD = Type->getAs<RecordType>()->getDecl(); |
| 3507 | if (RD->isCompleteDefinition()) { |
| 3508 | if (RD->isStruct()) |
| 3509 | Result += "\n\tstruct "; |
| 3510 | else if (RD->isUnion()) |
| 3511 | Result += "\n\tunion "; |
| 3512 | else |
| 3513 | assert(false && "class not allowed as an ivar type"); |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3514 | |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3515 | Result += RD->getName(); |
| 3516 | if (TagsDefinedInIvarDecls.count(RD)) { |
| 3517 | // This struct is already defined. Do not write its definition again. |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3518 | Result += " "; |
| 3519 | return true; |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3520 | } |
| 3521 | TagsDefinedInIvarDecls.insert(RD); |
| 3522 | Result += " {\n"; |
| 3523 | for (RecordDecl::field_iterator i = RD->field_begin(), |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3524 | e = RD->field_end(); i != e; ++i) { |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3525 | FieldDecl *FD = *i; |
| 3526 | RewriteObjCFieldDecl(FD, Result); |
| 3527 | } |
| 3528 | Result += "\t} "; |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3529 | return true; |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3530 | } |
| 3531 | } |
| 3532 | else if (Type->isEnumeralType()) { |
| 3533 | EnumDecl *ED = Type->getAs<EnumType>()->getDecl(); |
| 3534 | if (ED->isCompleteDefinition()) { |
| 3535 | Result += "\n\tenum "; |
| 3536 | Result += ED->getName(); |
| 3537 | if (TagsDefinedInIvarDecls.count(ED)) { |
| 3538 | // This enum is already defined. Do not write its definition again. |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3539 | Result += " "; |
| 3540 | return true; |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3541 | } |
| 3542 | TagsDefinedInIvarDecls.insert(ED); |
| 3543 | |
| 3544 | Result += " {\n"; |
| 3545 | for (EnumDecl::enumerator_iterator EC = ED->enumerator_begin(), |
| 3546 | ECEnd = ED->enumerator_end(); EC != ECEnd; ++EC) { |
| 3547 | Result += "\t"; Result += EC->getName(); Result += " = "; |
| 3548 | llvm::APSInt Val = EC->getInitVal(); |
| 3549 | Result += Val.toString(10); |
| 3550 | Result += ",\n"; |
| 3551 | } |
| 3552 | Result += "\t} "; |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3553 | return true; |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3554 | } |
| 3555 | } |
| 3556 | |
| 3557 | Result += "\t"; |
| 3558 | convertObjCTypeToCStyleType(Type); |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3559 | return false; |
| 3560 | } |
| 3561 | |
| 3562 | |
| 3563 | /// RewriteObjCFieldDecl - This routine rewrites a field into the buffer. |
| 3564 | /// It handles elaborated types, as well as enum types in the process. |
| 3565 | void RewriteModernObjC::RewriteObjCFieldDecl(FieldDecl *fieldDecl, |
| 3566 | std::string &Result) { |
| 3567 | QualType Type = fieldDecl->getType(); |
| 3568 | std::string Name = fieldDecl->getNameAsString(); |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3569 | |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3570 | bool EleboratedType = RewriteObjCFieldDeclType(Type, Result); |
| 3571 | if (!EleboratedType) |
| 3572 | Type.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3573 | Result += Name; |
| 3574 | if (fieldDecl->isBitField()) { |
| 3575 | Result += " : "; Result += utostr(fieldDecl->getBitWidthValue(*Context)); |
| 3576 | } |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3577 | else if (EleboratedType && Type->isArrayType()) { |
| 3578 | CanQualType CType = Context->getCanonicalType(Type); |
| 3579 | while (isa<ArrayType>(CType)) { |
| 3580 | if (const ConstantArrayType *CAT = Context->getAsConstantArrayType(CType)) { |
| 3581 | Result += "["; |
| 3582 | llvm::APInt Dim = CAT->getSize(); |
| 3583 | Result += utostr(Dim.getZExtValue()); |
| 3584 | Result += "]"; |
| 3585 | } |
| 3586 | CType = CType->getAs<ArrayType>()->getElementType(); |
| 3587 | } |
| 3588 | } |
| 3589 | |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3590 | Result += ";\n"; |
| 3591 | } |
| 3592 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3593 | /// RewriteObjCInternalStruct - Rewrite one internal struct corresponding to |
| 3594 | /// an objective-c class with ivars. |
| 3595 | void RewriteModernObjC::RewriteObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
| 3596 | std::string &Result) { |
| 3597 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
| 3598 | assert(CDecl->getName() != "" && |
| 3599 | "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3600 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 3601 | SmallVector<ObjCIvarDecl *, 8> IVars; |
| 3602 | for (ObjCIvarDecl *IVD = CDecl->all_declared_ivar_begin(); |
Fariborz Jahanian | 9a2105b | 2012-03-06 17:16:27 +0000 | [diff] [blame] | 3603 | IVD; IVD = IVD->getNextIvar()) |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 3604 | IVars.push_back(IVD); |
Fariborz Jahanian | 9a2105b | 2012-03-06 17:16:27 +0000 | [diff] [blame] | 3605 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3606 | SourceLocation LocStart = CDecl->getLocStart(); |
| 3607 | SourceLocation LocEnd = CDecl->getEndOfDefinitionLoc(); |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 3608 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3609 | const char *startBuf = SM->getCharacterData(LocStart); |
| 3610 | const char *endBuf = SM->getCharacterData(LocEnd); |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 3611 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3612 | // If no ivars and no root or if its root, directly or indirectly, |
| 3613 | // 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] | 3614 | if ((!CDecl->isThisDeclarationADefinition() || IVars.size() == 0) && |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3615 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
| 3616 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
| 3617 | ReplaceText(LocStart, endBuf-startBuf, Result); |
| 3618 | return; |
| 3619 | } |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 3620 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3621 | Result += "\nstruct "; |
| 3622 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 3623 | Result += "_IMPL {\n"; |
| 3624 | |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 3625 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 3626 | Result += "\tstruct "; Result += RCDecl->getNameAsString(); |
| 3627 | Result += "_IMPL "; Result += RCDecl->getNameAsString(); |
| 3628 | Result += "_IVARS;\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3629 | } |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3630 | TagsDefinedInIvarDecls.clear(); |
| 3631 | for (unsigned i = 0, e = IVars.size(); i < e; i++) |
| 3632 | RewriteObjCFieldDecl(IVars[i], Result); |
Fariborz Jahanian | 0b17b9a | 2012-02-12 21:36:23 +0000 | [diff] [blame] | 3633 | |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 3634 | Result += "};\n"; |
| 3635 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
| 3636 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 3637 | // Mark this struct as having been generated. |
| 3638 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
| 3639 | llvm_unreachable("struct already synthesize- RewriteObjCInternalStruct"); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3640 | } |
| 3641 | |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 3642 | static void WriteInternalIvarName(ObjCInterfaceDecl *IDecl, |
| 3643 | ObjCIvarDecl *IvarDecl, std::string &Result) { |
| 3644 | Result += "OBJC_IVAR_$_"; |
| 3645 | Result += IDecl->getName(); |
| 3646 | Result += "$"; |
| 3647 | Result += IvarDecl->getName(); |
| 3648 | } |
| 3649 | |
Fariborz Jahanian | 72c88f1 | 2012-02-22 18:13:25 +0000 | [diff] [blame] | 3650 | /// RewriteIvarOffsetSymbols - Rewrite ivar offset symbols of those ivars which |
| 3651 | /// have been referenced in an ivar access expression. |
| 3652 | void RewriteModernObjC::RewriteIvarOffsetSymbols(ObjCInterfaceDecl *CDecl, |
| 3653 | std::string &Result) { |
| 3654 | // write out ivar offset symbols which have been referenced in an ivar |
| 3655 | // access expression. |
| 3656 | llvm::SmallPtrSet<ObjCIvarDecl *, 8> Ivars = ReferencedIvars[CDecl]; |
| 3657 | if (Ivars.empty()) |
| 3658 | return; |
| 3659 | for (llvm::SmallPtrSet<ObjCIvarDecl *, 8>::iterator i = Ivars.begin(), |
| 3660 | e = Ivars.end(); i != e; i++) { |
| 3661 | ObjCIvarDecl *IvarDecl = (*i); |
Fariborz Jahanian | 40a777a | 2012-03-12 16:46:58 +0000 | [diff] [blame] | 3662 | Result += "\n"; |
| 3663 | if (LangOpts.MicrosoftExt) |
| 3664 | Result += "__declspec(allocate(\".objc_ivar$B\")) "; |
Fariborz Jahanian | 297976d | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 3665 | Result += "extern \"C\" "; |
Fariborz Jahanian | 40a777a | 2012-03-12 16:46:58 +0000 | [diff] [blame] | 3666 | if (LangOpts.MicrosoftExt && |
| 3667 | IvarDecl->getAccessControl() != ObjCIvarDecl::Private && |
Fariborz Jahanian | 297976d | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 3668 | IvarDecl->getAccessControl() != ObjCIvarDecl::Package) |
| 3669 | Result += "__declspec(dllimport) "; |
| 3670 | |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 3671 | Result += "unsigned long "; |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 3672 | WriteInternalIvarName(CDecl, IvarDecl, Result); |
| 3673 | Result += ";"; |
Fariborz Jahanian | 72c88f1 | 2012-02-22 18:13:25 +0000 | [diff] [blame] | 3674 | } |
| 3675 | } |
| 3676 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3677 | //===----------------------------------------------------------------------===// |
| 3678 | // Meta Data Emission |
| 3679 | //===----------------------------------------------------------------------===// |
| 3680 | |
| 3681 | |
| 3682 | /// RewriteImplementations - This routine rewrites all method implementations |
| 3683 | /// and emits meta-data. |
| 3684 | |
| 3685 | void RewriteModernObjC::RewriteImplementations() { |
| 3686 | int ClsDefCount = ClassImplementation.size(); |
| 3687 | int CatDefCount = CategoryImplementation.size(); |
| 3688 | |
| 3689 | // Rewrite implemented methods |
Fariborz Jahanian | 8f1fed0 | 2012-02-11 20:10:52 +0000 | [diff] [blame] | 3690 | for (int i = 0; i < ClsDefCount; i++) { |
| 3691 | ObjCImplementationDecl *OIMP = ClassImplementation[i]; |
| 3692 | ObjCInterfaceDecl *CDecl = OIMP->getClassInterface(); |
| 3693 | if (CDecl->isImplicitInterfaceDecl()) |
Fariborz Jahanian | cf4c60f | 2012-02-17 22:20:12 +0000 | [diff] [blame] | 3694 | assert(false && |
| 3695 | "Legacy implicit interface rewriting not supported in moder abi"); |
Fariborz Jahanian | 8f1fed0 | 2012-02-11 20:10:52 +0000 | [diff] [blame] | 3696 | RewriteImplementationDecl(OIMP); |
| 3697 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3698 | |
Fariborz Jahanian | 8c00a1b | 2012-02-17 20:33:00 +0000 | [diff] [blame] | 3699 | for (int i = 0; i < CatDefCount; i++) { |
| 3700 | ObjCCategoryImplDecl *CIMP = CategoryImplementation[i]; |
| 3701 | ObjCInterfaceDecl *CDecl = CIMP->getClassInterface(); |
| 3702 | if (CDecl->isImplicitInterfaceDecl()) |
| 3703 | assert(false && |
| 3704 | "Legacy implicit interface rewriting not supported in moder abi"); |
Fariborz Jahanian | 8c00a1b | 2012-02-17 20:33:00 +0000 | [diff] [blame] | 3705 | RewriteImplementationDecl(CIMP); |
| 3706 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3707 | } |
| 3708 | |
| 3709 | void RewriteModernObjC::RewriteByRefString(std::string &ResultStr, |
| 3710 | const std::string &Name, |
| 3711 | ValueDecl *VD, bool def) { |
| 3712 | assert(BlockByRefDeclNo.count(VD) && |
| 3713 | "RewriteByRefString: ByRef decl missing"); |
| 3714 | if (def) |
| 3715 | ResultStr += "struct "; |
| 3716 | ResultStr += "__Block_byref_" + Name + |
| 3717 | "_" + utostr(BlockByRefDeclNo[VD]) ; |
| 3718 | } |
| 3719 | |
| 3720 | static bool HasLocalVariableExternalStorage(ValueDecl *VD) { |
| 3721 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 3722 | return (Var->isFunctionOrMethodVarDecl() && !Var->hasLocalStorage()); |
| 3723 | return false; |
| 3724 | } |
| 3725 | |
| 3726 | std::string RewriteModernObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, |
| 3727 | StringRef funcName, |
| 3728 | std::string Tag) { |
| 3729 | const FunctionType *AFT = CE->getFunctionType(); |
| 3730 | QualType RT = AFT->getResultType(); |
| 3731 | std::string StructRef = "struct " + Tag; |
| 3732 | std::string S = "static " + RT.getAsString(Context->getPrintingPolicy()) + " __" + |
Fariborz Jahanian | df474ec | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 3733 | funcName.str() + "_block_func_" + utostr(i); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3734 | |
| 3735 | BlockDecl *BD = CE->getBlockDecl(); |
| 3736 | |
| 3737 | if (isa<FunctionNoProtoType>(AFT)) { |
| 3738 | // No user-supplied arguments. Still need to pass in a pointer to the |
| 3739 | // block (to reference imported block decl refs). |
| 3740 | S += "(" + StructRef + " *__cself)"; |
| 3741 | } else if (BD->param_empty()) { |
| 3742 | S += "(" + StructRef + " *__cself)"; |
| 3743 | } else { |
| 3744 | const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); |
| 3745 | assert(FT && "SynthesizeBlockFunc: No function proto"); |
| 3746 | S += '('; |
| 3747 | // first add the implicit argument. |
| 3748 | S += StructRef + " *__cself, "; |
| 3749 | std::string ParamStr; |
| 3750 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 3751 | E = BD->param_end(); AI != E; ++AI) { |
| 3752 | if (AI != BD->param_begin()) S += ", "; |
| 3753 | ParamStr = (*AI)->getNameAsString(); |
| 3754 | QualType QT = (*AI)->getType(); |
Fariborz Jahanian | 2610f90 | 2012-03-27 16:42:20 +0000 | [diff] [blame] | 3755 | (void)convertBlockPointerToFunctionPointer(QT); |
| 3756 | QT.getAsStringInternal(ParamStr, Context->getPrintingPolicy()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3757 | S += ParamStr; |
| 3758 | } |
| 3759 | if (FT->isVariadic()) { |
| 3760 | if (!BD->param_empty()) S += ", "; |
| 3761 | S += "..."; |
| 3762 | } |
| 3763 | S += ')'; |
| 3764 | } |
| 3765 | S += " {\n"; |
| 3766 | |
| 3767 | // Create local declarations to avoid rewriting all closure decl ref exprs. |
| 3768 | // First, emit a declaration for all "by ref" decls. |
| 3769 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 3770 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3771 | S += " "; |
| 3772 | std::string Name = (*I)->getNameAsString(); |
| 3773 | std::string TypeString; |
| 3774 | RewriteByRefString(TypeString, Name, (*I)); |
| 3775 | TypeString += " *"; |
| 3776 | Name = TypeString + Name; |
| 3777 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; |
| 3778 | } |
| 3779 | // Next, emit a declaration for all "by copy" declarations. |
| 3780 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 3781 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3782 | S += " "; |
| 3783 | // Handle nested closure invocation. For example: |
| 3784 | // |
| 3785 | // void (^myImportedClosure)(void); |
| 3786 | // myImportedClosure = ^(void) { setGlobalInt(x + y); }; |
| 3787 | // |
| 3788 | // void (^anotherClosure)(void); |
| 3789 | // anotherClosure = ^(void) { |
| 3790 | // myImportedClosure(); // import and invoke the closure |
| 3791 | // }; |
| 3792 | // |
| 3793 | if (isTopLevelBlockPointerType((*I)->getType())) { |
| 3794 | RewriteBlockPointerTypeVariable(S, (*I)); |
| 3795 | S += " = ("; |
| 3796 | RewriteBlockPointerType(S, (*I)->getType()); |
| 3797 | S += ")"; |
| 3798 | S += "__cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; |
| 3799 | } |
| 3800 | else { |
| 3801 | std::string Name = (*I)->getNameAsString(); |
| 3802 | QualType QT = (*I)->getType(); |
| 3803 | if (HasLocalVariableExternalStorage(*I)) |
| 3804 | QT = Context->getPointerType(QT); |
| 3805 | QT.getAsStringInternal(Name, Context->getPrintingPolicy()); |
| 3806 | S += Name + " = __cself->" + |
| 3807 | (*I)->getNameAsString() + "; // bound by copy\n"; |
| 3808 | } |
| 3809 | } |
| 3810 | std::string RewrittenStr = RewrittenBlockExprs[CE]; |
| 3811 | const char *cstr = RewrittenStr.c_str(); |
| 3812 | while (*cstr++ != '{') ; |
| 3813 | S += cstr; |
| 3814 | S += "\n"; |
| 3815 | return S; |
| 3816 | } |
| 3817 | |
| 3818 | std::string RewriteModernObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
| 3819 | StringRef funcName, |
| 3820 | std::string Tag) { |
| 3821 | std::string StructRef = "struct " + Tag; |
| 3822 | std::string S = "static void __"; |
| 3823 | |
| 3824 | S += funcName; |
| 3825 | S += "_block_copy_" + utostr(i); |
| 3826 | S += "(" + StructRef; |
| 3827 | S += "*dst, " + StructRef; |
| 3828 | S += "*src) {"; |
| 3829 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
| 3830 | E = ImportedBlockDecls.end(); I != E; ++I) { |
| 3831 | ValueDecl *VD = (*I); |
| 3832 | S += "_Block_object_assign((void*)&dst->"; |
| 3833 | S += (*I)->getNameAsString(); |
| 3834 | S += ", (void*)src->"; |
| 3835 | S += (*I)->getNameAsString(); |
| 3836 | if (BlockByRefDeclsPtrSet.count((*I))) |
| 3837 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
| 3838 | else if (VD->getType()->isBlockPointerType()) |
| 3839 | S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);"; |
| 3840 | else |
| 3841 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
| 3842 | } |
| 3843 | S += "}\n"; |
| 3844 | |
| 3845 | S += "\nstatic void __"; |
| 3846 | S += funcName; |
| 3847 | S += "_block_dispose_" + utostr(i); |
| 3848 | S += "(" + StructRef; |
| 3849 | S += "*src) {"; |
| 3850 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
| 3851 | E = ImportedBlockDecls.end(); I != E; ++I) { |
| 3852 | ValueDecl *VD = (*I); |
| 3853 | S += "_Block_object_dispose((void*)src->"; |
| 3854 | S += (*I)->getNameAsString(); |
| 3855 | if (BlockByRefDeclsPtrSet.count((*I))) |
| 3856 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
| 3857 | else if (VD->getType()->isBlockPointerType()) |
| 3858 | S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);"; |
| 3859 | else |
| 3860 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
| 3861 | } |
| 3862 | S += "}\n"; |
| 3863 | return S; |
| 3864 | } |
| 3865 | |
| 3866 | std::string RewriteModernObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 3867 | std::string Desc) { |
| 3868 | std::string S = "\nstruct " + Tag; |
| 3869 | std::string Constructor = " " + Tag; |
| 3870 | |
| 3871 | S += " {\n struct __block_impl impl;\n"; |
| 3872 | S += " struct " + Desc; |
| 3873 | S += "* Desc;\n"; |
| 3874 | |
| 3875 | Constructor += "(void *fp, "; // Invoke function pointer. |
| 3876 | Constructor += "struct " + Desc; // Descriptor pointer. |
| 3877 | Constructor += " *desc"; |
| 3878 | |
| 3879 | if (BlockDeclRefs.size()) { |
| 3880 | // Output all "by copy" declarations. |
| 3881 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 3882 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3883 | S += " "; |
| 3884 | std::string FieldName = (*I)->getNameAsString(); |
| 3885 | std::string ArgName = "_" + FieldName; |
| 3886 | // Handle nested closure invocation. For example: |
| 3887 | // |
| 3888 | // void (^myImportedBlock)(void); |
| 3889 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
| 3890 | // |
| 3891 | // void (^anotherBlock)(void); |
| 3892 | // anotherBlock = ^(void) { |
| 3893 | // myImportedBlock(); // import and invoke the closure |
| 3894 | // }; |
| 3895 | // |
| 3896 | if (isTopLevelBlockPointerType((*I)->getType())) { |
| 3897 | S += "struct __block_impl *"; |
| 3898 | Constructor += ", void *" + ArgName; |
| 3899 | } else { |
| 3900 | QualType QT = (*I)->getType(); |
| 3901 | if (HasLocalVariableExternalStorage(*I)) |
| 3902 | QT = Context->getPointerType(QT); |
| 3903 | QT.getAsStringInternal(FieldName, Context->getPrintingPolicy()); |
| 3904 | QT.getAsStringInternal(ArgName, Context->getPrintingPolicy()); |
| 3905 | Constructor += ", " + ArgName; |
| 3906 | } |
| 3907 | S += FieldName + ";\n"; |
| 3908 | } |
| 3909 | // Output all "by ref" declarations. |
| 3910 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 3911 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3912 | S += " "; |
| 3913 | std::string FieldName = (*I)->getNameAsString(); |
| 3914 | std::string ArgName = "_" + FieldName; |
| 3915 | { |
| 3916 | std::string TypeString; |
| 3917 | RewriteByRefString(TypeString, FieldName, (*I)); |
| 3918 | TypeString += " *"; |
| 3919 | FieldName = TypeString + FieldName; |
| 3920 | ArgName = TypeString + ArgName; |
| 3921 | Constructor += ", " + ArgName; |
| 3922 | } |
| 3923 | S += FieldName + "; // by ref\n"; |
| 3924 | } |
| 3925 | // Finish writing the constructor. |
| 3926 | Constructor += ", int flags=0)"; |
| 3927 | // Initialize all "by copy" arguments. |
| 3928 | bool firsTime = true; |
| 3929 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 3930 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3931 | std::string Name = (*I)->getNameAsString(); |
| 3932 | if (firsTime) { |
| 3933 | Constructor += " : "; |
| 3934 | firsTime = false; |
| 3935 | } |
| 3936 | else |
| 3937 | Constructor += ", "; |
| 3938 | if (isTopLevelBlockPointerType((*I)->getType())) |
| 3939 | Constructor += Name + "((struct __block_impl *)_" + Name + ")"; |
| 3940 | else |
| 3941 | Constructor += Name + "(_" + Name + ")"; |
| 3942 | } |
| 3943 | // Initialize all "by ref" arguments. |
| 3944 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 3945 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3946 | std::string Name = (*I)->getNameAsString(); |
| 3947 | if (firsTime) { |
| 3948 | Constructor += " : "; |
| 3949 | firsTime = false; |
| 3950 | } |
| 3951 | else |
| 3952 | Constructor += ", "; |
| 3953 | Constructor += Name + "(_" + Name + "->__forwarding)"; |
| 3954 | } |
| 3955 | |
| 3956 | Constructor += " {\n"; |
| 3957 | if (GlobalVarDecl) |
| 3958 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 3959 | else |
| 3960 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
| 3961 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 3962 | |
| 3963 | Constructor += " Desc = desc;\n"; |
| 3964 | } else { |
| 3965 | // Finish writing the constructor. |
| 3966 | Constructor += ", int flags=0) {\n"; |
| 3967 | if (GlobalVarDecl) |
| 3968 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 3969 | else |
| 3970 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
| 3971 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 3972 | Constructor += " Desc = desc;\n"; |
| 3973 | } |
| 3974 | Constructor += " "; |
| 3975 | Constructor += "}\n"; |
| 3976 | S += Constructor; |
| 3977 | S += "};\n"; |
| 3978 | return S; |
| 3979 | } |
| 3980 | |
| 3981 | std::string RewriteModernObjC::SynthesizeBlockDescriptor(std::string DescTag, |
| 3982 | std::string ImplTag, int i, |
| 3983 | StringRef FunName, |
| 3984 | unsigned hasCopy) { |
| 3985 | std::string S = "\nstatic struct " + DescTag; |
| 3986 | |
| 3987 | S += " {\n unsigned long reserved;\n"; |
| 3988 | S += " unsigned long Block_size;\n"; |
| 3989 | if (hasCopy) { |
| 3990 | S += " void (*copy)(struct "; |
| 3991 | S += ImplTag; S += "*, struct "; |
| 3992 | S += ImplTag; S += "*);\n"; |
| 3993 | |
| 3994 | S += " void (*dispose)(struct "; |
| 3995 | S += ImplTag; S += "*);\n"; |
| 3996 | } |
| 3997 | S += "} "; |
| 3998 | |
| 3999 | S += DescTag + "_DATA = { 0, sizeof(struct "; |
| 4000 | S += ImplTag + ")"; |
| 4001 | if (hasCopy) { |
| 4002 | S += ", __" + FunName.str() + "_block_copy_" + utostr(i); |
| 4003 | S += ", __" + FunName.str() + "_block_dispose_" + utostr(i); |
| 4004 | } |
| 4005 | S += "};\n"; |
| 4006 | return S; |
| 4007 | } |
| 4008 | |
Fariborz Jahanian | 76a98be | 2012-04-17 18:40:53 +0000 | [diff] [blame] | 4009 | /// getFunctionSourceLocation - returns start location of a function |
| 4010 | /// definition. Complication arises when function has declared as |
| 4011 | /// extern "C" or extern "C" {...} |
Fariborz Jahanian | b75f8de | 2012-04-19 00:50:01 +0000 | [diff] [blame] | 4012 | static SourceLocation getFunctionSourceLocation (RewriteModernObjC &R, |
| 4013 | FunctionDecl *FD) { |
Fariborz Jahanian | b5863da | 2012-04-19 16:30:28 +0000 | [diff] [blame] | 4014 | if (FD->isExternC() && !FD->isMain()) { |
| 4015 | const DeclContext *DC = FD->getDeclContext(); |
| 4016 | if (const LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(DC)) |
| 4017 | // if it is extern "C" {...}, return function decl's own location. |
| 4018 | if (!LSD->getRBraceLoc().isValid()) |
| 4019 | return LSD->getExternLoc(); |
Fariborz Jahanian | b75f8de | 2012-04-19 00:50:01 +0000 | [diff] [blame] | 4020 | } |
Fariborz Jahanian | b5863da | 2012-04-19 16:30:28 +0000 | [diff] [blame] | 4021 | if (FD->getStorageClassAsWritten() != SC_None) |
| 4022 | R.RewriteBlockLiteralFunctionDecl(FD); |
Fariborz Jahanian | 76a98be | 2012-04-17 18:40:53 +0000 | [diff] [blame] | 4023 | return FD->getTypeSpecStartLoc(); |
| 4024 | } |
| 4025 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4026 | void RewriteModernObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, |
| 4027 | StringRef FunName) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4028 | bool RewriteSC = (GlobalVarDecl && |
| 4029 | !Blocks.empty() && |
| 4030 | GlobalVarDecl->getStorageClass() == SC_Static && |
| 4031 | GlobalVarDecl->getType().getCVRQualifiers()); |
| 4032 | if (RewriteSC) { |
| 4033 | std::string SC(" void __"); |
| 4034 | SC += GlobalVarDecl->getNameAsString(); |
| 4035 | SC += "() {}"; |
| 4036 | InsertText(FunLocStart, SC); |
| 4037 | } |
| 4038 | |
| 4039 | // Insert closures that were part of the function. |
| 4040 | for (unsigned i = 0, count=0; i < Blocks.size(); i++) { |
| 4041 | CollectBlockDeclRefInfo(Blocks[i]); |
| 4042 | // Need to copy-in the inner copied-in variables not actually used in this |
| 4043 | // block. |
| 4044 | for (int j = 0; j < InnerDeclRefsCount[i]; j++) { |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4045 | DeclRefExpr *Exp = InnerDeclRefs[count++]; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4046 | ValueDecl *VD = Exp->getDecl(); |
| 4047 | BlockDeclRefs.push_back(Exp); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4048 | if (!VD->hasAttr<BlocksAttr>()) { |
| 4049 | if (!BlockByCopyDeclsPtrSet.count(VD)) { |
| 4050 | BlockByCopyDeclsPtrSet.insert(VD); |
| 4051 | BlockByCopyDecls.push_back(VD); |
| 4052 | } |
| 4053 | continue; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4054 | } |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4055 | |
| 4056 | if (!BlockByRefDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4057 | BlockByRefDeclsPtrSet.insert(VD); |
| 4058 | BlockByRefDecls.push_back(VD); |
| 4059 | } |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4060 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4061 | // imported objects in the inner blocks not used in the outer |
| 4062 | // blocks must be copied/disposed in the outer block as well. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4063 | if (VD->getType()->isObjCObjectPointerType() || |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4064 | VD->getType()->isBlockPointerType()) |
| 4065 | ImportedBlockDecls.insert(VD); |
| 4066 | } |
| 4067 | |
| 4068 | std::string ImplTag = "__" + FunName.str() + "_block_impl_" + utostr(i); |
| 4069 | std::string DescTag = "__" + FunName.str() + "_block_desc_" + utostr(i); |
| 4070 | |
| 4071 | std::string CI = SynthesizeBlockImpl(Blocks[i], ImplTag, DescTag); |
| 4072 | |
| 4073 | InsertText(FunLocStart, CI); |
| 4074 | |
| 4075 | std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, ImplTag); |
| 4076 | |
| 4077 | InsertText(FunLocStart, CF); |
| 4078 | |
| 4079 | if (ImportedBlockDecls.size()) { |
| 4080 | std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, ImplTag); |
| 4081 | InsertText(FunLocStart, HF); |
| 4082 | } |
| 4083 | std::string BD = SynthesizeBlockDescriptor(DescTag, ImplTag, i, FunName, |
| 4084 | ImportedBlockDecls.size() > 0); |
| 4085 | InsertText(FunLocStart, BD); |
| 4086 | |
| 4087 | BlockDeclRefs.clear(); |
| 4088 | BlockByRefDecls.clear(); |
| 4089 | BlockByRefDeclsPtrSet.clear(); |
| 4090 | BlockByCopyDecls.clear(); |
| 4091 | BlockByCopyDeclsPtrSet.clear(); |
| 4092 | ImportedBlockDecls.clear(); |
| 4093 | } |
| 4094 | if (RewriteSC) { |
| 4095 | // Must insert any 'const/volatile/static here. Since it has been |
| 4096 | // removed as result of rewriting of block literals. |
| 4097 | std::string SC; |
| 4098 | if (GlobalVarDecl->getStorageClass() == SC_Static) |
| 4099 | SC = "static "; |
| 4100 | if (GlobalVarDecl->getType().isConstQualified()) |
| 4101 | SC += "const "; |
| 4102 | if (GlobalVarDecl->getType().isVolatileQualified()) |
| 4103 | SC += "volatile "; |
| 4104 | if (GlobalVarDecl->getType().isRestrictQualified()) |
| 4105 | SC += "restrict "; |
| 4106 | InsertText(FunLocStart, SC); |
| 4107 | } |
Fariborz Jahanian | df474ec | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 4108 | if (GlobalConstructionExp) { |
| 4109 | // extra fancy dance for global literal expression. |
| 4110 | |
| 4111 | // Always the latest block expression on the block stack. |
| 4112 | std::string Tag = "__"; |
| 4113 | Tag += FunName; |
| 4114 | Tag += "_block_impl_"; |
| 4115 | Tag += utostr(Blocks.size()-1); |
| 4116 | std::string globalBuf = "static "; |
| 4117 | globalBuf += Tag; globalBuf += " "; |
| 4118 | std::string SStr; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4119 | |
Fariborz Jahanian | df474ec | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 4120 | llvm::raw_string_ostream constructorExprBuf(SStr); |
| 4121 | GlobalConstructionExp->printPretty(constructorExprBuf, *Context, 0, |
| 4122 | PrintingPolicy(LangOpts)); |
| 4123 | globalBuf += constructorExprBuf.str(); |
| 4124 | globalBuf += ";\n"; |
| 4125 | InsertText(FunLocStart, globalBuf); |
| 4126 | GlobalConstructionExp = 0; |
| 4127 | } |
| 4128 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4129 | Blocks.clear(); |
| 4130 | InnerDeclRefsCount.clear(); |
| 4131 | InnerDeclRefs.clear(); |
| 4132 | RewrittenBlockExprs.clear(); |
| 4133 | } |
| 4134 | |
| 4135 | void RewriteModernObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { |
Fariborz Jahanian | 0418953 | 2012-04-25 17:56:48 +0000 | [diff] [blame] | 4136 | SourceLocation FunLocStart = |
| 4137 | (!Blocks.empty()) ? getFunctionSourceLocation(*this, FD) |
| 4138 | : FD->getTypeSpecStartLoc(); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4139 | StringRef FuncName = FD->getName(); |
| 4140 | |
| 4141 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 4142 | } |
| 4143 | |
| 4144 | static void BuildUniqueMethodName(std::string &Name, |
| 4145 | ObjCMethodDecl *MD) { |
| 4146 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
| 4147 | Name = IFace->getName(); |
| 4148 | Name += "__" + MD->getSelector().getAsString(); |
| 4149 | // Convert colons to underscores. |
| 4150 | std::string::size_type loc = 0; |
| 4151 | while ((loc = Name.find(":", loc)) != std::string::npos) |
| 4152 | Name.replace(loc, 1, "_"); |
| 4153 | } |
| 4154 | |
| 4155 | void RewriteModernObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { |
| 4156 | //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n"); |
| 4157 | //SourceLocation FunLocStart = MD->getLocStart(); |
| 4158 | SourceLocation FunLocStart = MD->getLocStart(); |
| 4159 | std::string FuncName; |
| 4160 | BuildUniqueMethodName(FuncName, MD); |
| 4161 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 4162 | } |
| 4163 | |
| 4164 | void RewriteModernObjC::GetBlockDeclRefExprs(Stmt *S) { |
| 4165 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
| 4166 | if (*CI) { |
| 4167 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 4168 | GetBlockDeclRefExprs(CBE->getBody()); |
| 4169 | else |
| 4170 | GetBlockDeclRefExprs(*CI); |
| 4171 | } |
| 4172 | // Handle specific things. |
Fariborz Jahanian | 0e97681 | 2012-04-16 23:00:57 +0000 | [diff] [blame] | 4173 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 4174 | if (DRE->refersToEnclosingLocal()) { |
| 4175 | // FIXME: Handle enums. |
| 4176 | if (!isa<FunctionDecl>(DRE->getDecl())) |
| 4177 | BlockDeclRefs.push_back(DRE); |
| 4178 | if (HasLocalVariableExternalStorage(DRE->getDecl())) |
| 4179 | BlockDeclRefs.push_back(DRE); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4180 | } |
Fariborz Jahanian | 0e97681 | 2012-04-16 23:00:57 +0000 | [diff] [blame] | 4181 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4182 | |
| 4183 | return; |
| 4184 | } |
| 4185 | |
| 4186 | void RewriteModernObjC::GetInnerBlockDeclRefExprs(Stmt *S, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4187 | SmallVector<DeclRefExpr *, 8> &InnerBlockDeclRefs, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4188 | llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts) { |
| 4189 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
| 4190 | if (*CI) { |
| 4191 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) { |
| 4192 | InnerContexts.insert(cast<DeclContext>(CBE->getBlockDecl())); |
| 4193 | GetInnerBlockDeclRefExprs(CBE->getBody(), |
| 4194 | InnerBlockDeclRefs, |
| 4195 | InnerContexts); |
| 4196 | } |
| 4197 | else |
| 4198 | GetInnerBlockDeclRefExprs(*CI, |
| 4199 | InnerBlockDeclRefs, |
| 4200 | InnerContexts); |
| 4201 | |
| 4202 | } |
| 4203 | // Handle specific things. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4204 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 4205 | if (DRE->refersToEnclosingLocal()) { |
| 4206 | if (!isa<FunctionDecl>(DRE->getDecl()) && |
| 4207 | !InnerContexts.count(DRE->getDecl()->getDeclContext())) |
| 4208 | InnerBlockDeclRefs.push_back(DRE); |
| 4209 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) |
| 4210 | if (Var->isFunctionOrMethodVarDecl()) |
| 4211 | ImportedLocalExternalDecls.insert(Var); |
| 4212 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4213 | } |
| 4214 | |
| 4215 | return; |
| 4216 | } |
| 4217 | |
Fariborz Jahanian | 164d6f8 | 2012-02-13 18:57:49 +0000 | [diff] [blame] | 4218 | /// convertObjCTypeToCStyleType - This routine converts such objc types |
| 4219 | /// as qualified objects, and blocks to their closest c/c++ types that |
| 4220 | /// it can. It returns true if input type was modified. |
| 4221 | bool RewriteModernObjC::convertObjCTypeToCStyleType(QualType &T) { |
| 4222 | QualType oldT = T; |
| 4223 | convertBlockPointerToFunctionPointer(T); |
| 4224 | if (T->isFunctionPointerType()) { |
| 4225 | QualType PointeeTy; |
| 4226 | if (const PointerType* PT = T->getAs<PointerType>()) { |
| 4227 | PointeeTy = PT->getPointeeType(); |
| 4228 | if (const FunctionType *FT = PointeeTy->getAs<FunctionType>()) { |
| 4229 | T = convertFunctionTypeOfBlocks(FT); |
| 4230 | T = Context->getPointerType(T); |
| 4231 | } |
| 4232 | } |
| 4233 | } |
| 4234 | |
| 4235 | convertToUnqualifiedObjCType(T); |
| 4236 | return T != oldT; |
| 4237 | } |
| 4238 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4239 | /// convertFunctionTypeOfBlocks - This routine converts a function type |
| 4240 | /// whose result type may be a block pointer or whose argument type(s) |
| 4241 | /// might be block pointers to an equivalent function type replacing |
| 4242 | /// all block pointers to function pointers. |
| 4243 | QualType RewriteModernObjC::convertFunctionTypeOfBlocks(const FunctionType *FT) { |
| 4244 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
| 4245 | // FTP will be null for closures that don't take arguments. |
| 4246 | // Generate a funky cast. |
| 4247 | SmallVector<QualType, 8> ArgTypes; |
| 4248 | QualType Res = FT->getResultType(); |
Fariborz Jahanian | 164d6f8 | 2012-02-13 18:57:49 +0000 | [diff] [blame] | 4249 | bool modified = convertObjCTypeToCStyleType(Res); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4250 | |
| 4251 | if (FTP) { |
| 4252 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
| 4253 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 4254 | QualType t = *I; |
| 4255 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 164d6f8 | 2012-02-13 18:57:49 +0000 | [diff] [blame] | 4256 | if (convertObjCTypeToCStyleType(t)) |
| 4257 | modified = true; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4258 | ArgTypes.push_back(t); |
| 4259 | } |
| 4260 | } |
| 4261 | QualType FuncType; |
Fariborz Jahanian | 164d6f8 | 2012-02-13 18:57:49 +0000 | [diff] [blame] | 4262 | if (modified) |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4263 | FuncType = getSimpleFunctionType(Res, &ArgTypes[0], ArgTypes.size()); |
| 4264 | else FuncType = QualType(FT, 0); |
| 4265 | return FuncType; |
| 4266 | } |
| 4267 | |
| 4268 | Stmt *RewriteModernObjC::SynthesizeBlockCall(CallExpr *Exp, const Expr *BlockExp) { |
| 4269 | // Navigate to relevant type information. |
| 4270 | const BlockPointerType *CPT = 0; |
| 4271 | |
| 4272 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BlockExp)) { |
| 4273 | CPT = DRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4274 | } else if (const MemberExpr *MExpr = dyn_cast<MemberExpr>(BlockExp)) { |
| 4275 | CPT = MExpr->getType()->getAs<BlockPointerType>(); |
| 4276 | } |
| 4277 | else if (const ParenExpr *PRE = dyn_cast<ParenExpr>(BlockExp)) { |
| 4278 | return SynthesizeBlockCall(Exp, PRE->getSubExpr()); |
| 4279 | } |
| 4280 | else if (const ImplicitCastExpr *IEXPR = dyn_cast<ImplicitCastExpr>(BlockExp)) |
| 4281 | CPT = IEXPR->getType()->getAs<BlockPointerType>(); |
| 4282 | else if (const ConditionalOperator *CEXPR = |
| 4283 | dyn_cast<ConditionalOperator>(BlockExp)) { |
| 4284 | Expr *LHSExp = CEXPR->getLHS(); |
| 4285 | Stmt *LHSStmt = SynthesizeBlockCall(Exp, LHSExp); |
| 4286 | Expr *RHSExp = CEXPR->getRHS(); |
| 4287 | Stmt *RHSStmt = SynthesizeBlockCall(Exp, RHSExp); |
| 4288 | Expr *CONDExp = CEXPR->getCond(); |
| 4289 | ConditionalOperator *CondExpr = |
| 4290 | new (Context) ConditionalOperator(CONDExp, |
| 4291 | SourceLocation(), cast<Expr>(LHSStmt), |
| 4292 | SourceLocation(), cast<Expr>(RHSStmt), |
| 4293 | Exp->getType(), VK_RValue, OK_Ordinary); |
| 4294 | return CondExpr; |
| 4295 | } else if (const ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(BlockExp)) { |
| 4296 | CPT = IRE->getType()->getAs<BlockPointerType>(); |
| 4297 | } else if (const PseudoObjectExpr *POE |
| 4298 | = dyn_cast<PseudoObjectExpr>(BlockExp)) { |
| 4299 | CPT = POE->getType()->castAs<BlockPointerType>(); |
| 4300 | } else { |
| 4301 | assert(1 && "RewriteBlockClass: Bad type"); |
| 4302 | } |
| 4303 | assert(CPT && "RewriteBlockClass: Bad type"); |
| 4304 | const FunctionType *FT = CPT->getPointeeType()->getAs<FunctionType>(); |
| 4305 | assert(FT && "RewriteBlockClass: Bad type"); |
| 4306 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
| 4307 | // FTP will be null for closures that don't take arguments. |
| 4308 | |
| 4309 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 4310 | SourceLocation(), SourceLocation(), |
| 4311 | &Context->Idents.get("__block_impl")); |
| 4312 | QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD)); |
| 4313 | |
| 4314 | // Generate a funky cast. |
| 4315 | SmallVector<QualType, 8> ArgTypes; |
| 4316 | |
| 4317 | // Push the block argument type. |
| 4318 | ArgTypes.push_back(PtrBlock); |
| 4319 | if (FTP) { |
| 4320 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
| 4321 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 4322 | QualType t = *I; |
| 4323 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
| 4324 | if (!convertBlockPointerToFunctionPointer(t)) |
| 4325 | convertToUnqualifiedObjCType(t); |
| 4326 | ArgTypes.push_back(t); |
| 4327 | } |
| 4328 | } |
| 4329 | // Now do the pointer to function cast. |
| 4330 | QualType PtrToFuncCastType |
| 4331 | = getSimpleFunctionType(Exp->getType(), &ArgTypes[0], ArgTypes.size()); |
| 4332 | |
| 4333 | PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); |
| 4334 | |
| 4335 | CastExpr *BlkCast = NoTypeInfoCStyleCastExpr(Context, PtrBlock, |
| 4336 | CK_BitCast, |
| 4337 | const_cast<Expr*>(BlockExp)); |
| 4338 | // Don't forget the parens to enforce the proper binding. |
| 4339 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 4340 | BlkCast); |
| 4341 | //PE->dump(); |
| 4342 | |
| 4343 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 4344 | SourceLocation(), |
| 4345 | &Context->Idents.get("FuncPtr"), |
| 4346 | Context->VoidPtrTy, 0, |
| 4347 | /*BitWidth=*/0, /*Mutable=*/true, |
| 4348 | /*HasInit=*/false); |
| 4349 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
| 4350 | FD->getType(), VK_LValue, |
| 4351 | OK_Ordinary); |
| 4352 | |
| 4353 | |
| 4354 | CastExpr *FunkCast = NoTypeInfoCStyleCastExpr(Context, PtrToFuncCastType, |
| 4355 | CK_BitCast, ME); |
| 4356 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast); |
| 4357 | |
| 4358 | SmallVector<Expr*, 8> BlkExprs; |
| 4359 | // Add the implicit argument. |
| 4360 | BlkExprs.push_back(BlkCast); |
| 4361 | // Add the user arguments. |
| 4362 | for (CallExpr::arg_iterator I = Exp->arg_begin(), |
| 4363 | E = Exp->arg_end(); I != E; ++I) { |
| 4364 | BlkExprs.push_back(*I); |
| 4365 | } |
| 4366 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &BlkExprs[0], |
| 4367 | BlkExprs.size(), |
| 4368 | Exp->getType(), VK_RValue, |
| 4369 | SourceLocation()); |
| 4370 | return CE; |
| 4371 | } |
| 4372 | |
| 4373 | // We need to return the rewritten expression to handle cases where the |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4374 | // DeclRefExpr is embedded in another expression being rewritten. |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4375 | // For example: |
| 4376 | // |
| 4377 | // int main() { |
| 4378 | // __block Foo *f; |
| 4379 | // __block int i; |
| 4380 | // |
| 4381 | // void (^myblock)() = ^() { |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4382 | // [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] | 4383 | // i = 77; |
| 4384 | // }; |
| 4385 | //} |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4386 | Stmt *RewriteModernObjC::RewriteBlockDeclRefExpr(DeclRefExpr *DeclRefExp) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4387 | // Rewrite the byref variable into BYREFVAR->__forwarding->BYREFVAR |
| 4388 | // for each DeclRefExp where BYREFVAR is name of the variable. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4389 | ValueDecl *VD = DeclRefExp->getDecl(); |
| 4390 | bool isArrow = DeclRefExp->refersToEnclosingLocal(); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4391 | |
| 4392 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 4393 | SourceLocation(), |
| 4394 | &Context->Idents.get("__forwarding"), |
| 4395 | Context->VoidPtrTy, 0, |
| 4396 | /*BitWidth=*/0, /*Mutable=*/true, |
| 4397 | /*HasInit=*/false); |
| 4398 | MemberExpr *ME = new (Context) MemberExpr(DeclRefExp, isArrow, |
| 4399 | FD, SourceLocation(), |
| 4400 | FD->getType(), VK_LValue, |
| 4401 | OK_Ordinary); |
| 4402 | |
| 4403 | StringRef Name = VD->getName(); |
| 4404 | FD = FieldDecl::Create(*Context, 0, SourceLocation(), SourceLocation(), |
| 4405 | &Context->Idents.get(Name), |
| 4406 | Context->VoidPtrTy, 0, |
| 4407 | /*BitWidth=*/0, /*Mutable=*/true, |
| 4408 | /*HasInit=*/false); |
| 4409 | ME = new (Context) MemberExpr(ME, true, FD, SourceLocation(), |
| 4410 | DeclRefExp->getType(), VK_LValue, OK_Ordinary); |
| 4411 | |
| 4412 | |
| 4413 | |
| 4414 | // Need parens to enforce precedence. |
| 4415 | ParenExpr *PE = new (Context) ParenExpr(DeclRefExp->getExprLoc(), |
| 4416 | DeclRefExp->getExprLoc(), |
| 4417 | ME); |
| 4418 | ReplaceStmt(DeclRefExp, PE); |
| 4419 | return PE; |
| 4420 | } |
| 4421 | |
| 4422 | // Rewrites the imported local variable V with external storage |
| 4423 | // (static, extern, etc.) as *V |
| 4424 | // |
| 4425 | Stmt *RewriteModernObjC::RewriteLocalVariableExternalStorage(DeclRefExpr *DRE) { |
| 4426 | ValueDecl *VD = DRE->getDecl(); |
| 4427 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 4428 | if (!ImportedLocalExternalDecls.count(Var)) |
| 4429 | return DRE; |
| 4430 | Expr *Exp = new (Context) UnaryOperator(DRE, UO_Deref, DRE->getType(), |
| 4431 | VK_LValue, OK_Ordinary, |
| 4432 | DRE->getLocation()); |
| 4433 | // Need parens to enforce precedence. |
| 4434 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 4435 | Exp); |
| 4436 | ReplaceStmt(DRE, PE); |
| 4437 | return PE; |
| 4438 | } |
| 4439 | |
| 4440 | void RewriteModernObjC::RewriteCastExpr(CStyleCastExpr *CE) { |
| 4441 | SourceLocation LocStart = CE->getLParenLoc(); |
| 4442 | SourceLocation LocEnd = CE->getRParenLoc(); |
| 4443 | |
| 4444 | // Need to avoid trying to rewrite synthesized casts. |
| 4445 | if (LocStart.isInvalid()) |
| 4446 | return; |
| 4447 | // Need to avoid trying to rewrite casts contained in macros. |
| 4448 | if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) |
| 4449 | return; |
| 4450 | |
| 4451 | const char *startBuf = SM->getCharacterData(LocStart); |
| 4452 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 4453 | QualType QT = CE->getType(); |
| 4454 | const Type* TypePtr = QT->getAs<Type>(); |
| 4455 | if (isa<TypeOfExprType>(TypePtr)) { |
| 4456 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 4457 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 4458 | std::string TypeAsString = "("; |
| 4459 | RewriteBlockPointerType(TypeAsString, QT); |
| 4460 | TypeAsString += ")"; |
| 4461 | ReplaceText(LocStart, endBuf-startBuf+1, TypeAsString); |
| 4462 | return; |
| 4463 | } |
| 4464 | // advance the location to startArgList. |
| 4465 | const char *argPtr = startBuf; |
| 4466 | |
| 4467 | while (*argPtr++ && (argPtr < endBuf)) { |
| 4468 | switch (*argPtr) { |
| 4469 | case '^': |
| 4470 | // Replace the '^' with '*'. |
| 4471 | LocStart = LocStart.getLocWithOffset(argPtr-startBuf); |
| 4472 | ReplaceText(LocStart, 1, "*"); |
| 4473 | break; |
| 4474 | } |
| 4475 | } |
| 4476 | return; |
| 4477 | } |
| 4478 | |
Fariborz Jahanian | f1ee687 | 2012-04-10 00:08:18 +0000 | [diff] [blame] | 4479 | void RewriteModernObjC::RewriteImplicitCastObjCExpr(CastExpr *IC) { |
| 4480 | CastKind CastKind = IC->getCastKind(); |
Fariborz Jahanian | 43aa1c3 | 2012-04-16 22:14:01 +0000 | [diff] [blame] | 4481 | if (CastKind != CK_BlockPointerToObjCPointerCast && |
| 4482 | CastKind != CK_AnyPointerToBlockPointerCast) |
| 4483 | return; |
Fariborz Jahanian | f1ee687 | 2012-04-10 00:08:18 +0000 | [diff] [blame] | 4484 | |
Fariborz Jahanian | 43aa1c3 | 2012-04-16 22:14:01 +0000 | [diff] [blame] | 4485 | QualType QT = IC->getType(); |
| 4486 | (void)convertBlockPointerToFunctionPointer(QT); |
| 4487 | std::string TypeString(QT.getAsString(Context->getPrintingPolicy())); |
| 4488 | std::string Str = "("; |
| 4489 | Str += TypeString; |
| 4490 | Str += ")"; |
| 4491 | InsertText(IC->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 4492 | |
Fariborz Jahanian | f1ee687 | 2012-04-10 00:08:18 +0000 | [diff] [blame] | 4493 | return; |
| 4494 | } |
| 4495 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4496 | void RewriteModernObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { |
| 4497 | SourceLocation DeclLoc = FD->getLocation(); |
| 4498 | unsigned parenCount = 0; |
| 4499 | |
| 4500 | // We have 1 or more arguments that have closure pointers. |
| 4501 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4502 | const char *startArgList = strchr(startBuf, '('); |
| 4503 | |
| 4504 | assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); |
| 4505 | |
| 4506 | parenCount++; |
| 4507 | // advance the location to startArgList. |
| 4508 | DeclLoc = DeclLoc.getLocWithOffset(startArgList-startBuf); |
| 4509 | assert((DeclLoc.isValid()) && "Invalid DeclLoc"); |
| 4510 | |
| 4511 | const char *argPtr = startArgList; |
| 4512 | |
| 4513 | while (*argPtr++ && parenCount) { |
| 4514 | switch (*argPtr) { |
| 4515 | case '^': |
| 4516 | // Replace the '^' with '*'. |
| 4517 | DeclLoc = DeclLoc.getLocWithOffset(argPtr-startArgList); |
| 4518 | ReplaceText(DeclLoc, 1, "*"); |
| 4519 | break; |
| 4520 | case '(': |
| 4521 | parenCount++; |
| 4522 | break; |
| 4523 | case ')': |
| 4524 | parenCount--; |
| 4525 | break; |
| 4526 | } |
| 4527 | } |
| 4528 | return; |
| 4529 | } |
| 4530 | |
| 4531 | bool RewriteModernObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { |
| 4532 | const FunctionProtoType *FTP; |
| 4533 | const PointerType *PT = QT->getAs<PointerType>(); |
| 4534 | if (PT) { |
| 4535 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4536 | } else { |
| 4537 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
| 4538 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
| 4539 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4540 | } |
| 4541 | if (FTP) { |
| 4542 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
| 4543 | E = FTP->arg_type_end(); I != E; ++I) |
| 4544 | if (isTopLevelBlockPointerType(*I)) |
| 4545 | return true; |
| 4546 | } |
| 4547 | return false; |
| 4548 | } |
| 4549 | |
| 4550 | bool RewriteModernObjC::PointerTypeTakesAnyObjCQualifiedType(QualType QT) { |
| 4551 | const FunctionProtoType *FTP; |
| 4552 | const PointerType *PT = QT->getAs<PointerType>(); |
| 4553 | if (PT) { |
| 4554 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4555 | } else { |
| 4556 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
| 4557 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
| 4558 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4559 | } |
| 4560 | if (FTP) { |
| 4561 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
| 4562 | E = FTP->arg_type_end(); I != E; ++I) { |
| 4563 | if ((*I)->isObjCQualifiedIdType()) |
| 4564 | return true; |
| 4565 | if ((*I)->isObjCObjectPointerType() && |
| 4566 | (*I)->getPointeeType()->isObjCQualifiedInterfaceType()) |
| 4567 | return true; |
| 4568 | } |
| 4569 | |
| 4570 | } |
| 4571 | return false; |
| 4572 | } |
| 4573 | |
| 4574 | void RewriteModernObjC::GetExtentOfArgList(const char *Name, const char *&LParen, |
| 4575 | const char *&RParen) { |
| 4576 | const char *argPtr = strchr(Name, '('); |
| 4577 | assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); |
| 4578 | |
| 4579 | LParen = argPtr; // output the start. |
| 4580 | argPtr++; // skip past the left paren. |
| 4581 | unsigned parenCount = 1; |
| 4582 | |
| 4583 | while (*argPtr && parenCount) { |
| 4584 | switch (*argPtr) { |
| 4585 | case '(': parenCount++; break; |
| 4586 | case ')': parenCount--; break; |
| 4587 | default: break; |
| 4588 | } |
| 4589 | if (parenCount) argPtr++; |
| 4590 | } |
| 4591 | assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); |
| 4592 | RParen = argPtr; // output the end |
| 4593 | } |
| 4594 | |
| 4595 | void RewriteModernObjC::RewriteBlockPointerDecl(NamedDecl *ND) { |
| 4596 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 4597 | RewriteBlockPointerFunctionArgs(FD); |
| 4598 | return; |
| 4599 | } |
| 4600 | // Handle Variables and Typedefs. |
| 4601 | SourceLocation DeclLoc = ND->getLocation(); |
| 4602 | QualType DeclT; |
| 4603 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4604 | DeclT = VD->getType(); |
| 4605 | else if (TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(ND)) |
| 4606 | DeclT = TDD->getUnderlyingType(); |
| 4607 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) |
| 4608 | DeclT = FD->getType(); |
| 4609 | else |
| 4610 | llvm_unreachable("RewriteBlockPointerDecl(): Decl type not yet handled"); |
| 4611 | |
| 4612 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4613 | const char *endBuf = startBuf; |
| 4614 | // scan backward (from the decl location) for the end of the previous decl. |
| 4615 | while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) |
| 4616 | startBuf--; |
| 4617 | SourceLocation Start = DeclLoc.getLocWithOffset(startBuf-endBuf); |
| 4618 | std::string buf; |
| 4619 | unsigned OrigLength=0; |
| 4620 | // *startBuf != '^' if we are dealing with a pointer to function that |
| 4621 | // may take block argument types (which will be handled below). |
| 4622 | if (*startBuf == '^') { |
| 4623 | // Replace the '^' with '*', computing a negative offset. |
| 4624 | buf = '*'; |
| 4625 | startBuf++; |
| 4626 | OrigLength++; |
| 4627 | } |
| 4628 | while (*startBuf != ')') { |
| 4629 | buf += *startBuf; |
| 4630 | startBuf++; |
| 4631 | OrigLength++; |
| 4632 | } |
| 4633 | buf += ')'; |
| 4634 | OrigLength++; |
| 4635 | |
| 4636 | if (PointerTypeTakesAnyBlockArguments(DeclT) || |
| 4637 | PointerTypeTakesAnyObjCQualifiedType(DeclT)) { |
| 4638 | // Replace the '^' with '*' for arguments. |
| 4639 | // Replace id<P> with id/*<>*/ |
| 4640 | DeclLoc = ND->getLocation(); |
| 4641 | startBuf = SM->getCharacterData(DeclLoc); |
| 4642 | const char *argListBegin, *argListEnd; |
| 4643 | GetExtentOfArgList(startBuf, argListBegin, argListEnd); |
| 4644 | while (argListBegin < argListEnd) { |
| 4645 | if (*argListBegin == '^') |
| 4646 | buf += '*'; |
| 4647 | else if (*argListBegin == '<') { |
| 4648 | buf += "/*"; |
| 4649 | buf += *argListBegin++; |
| 4650 | OrigLength++;; |
| 4651 | while (*argListBegin != '>') { |
| 4652 | buf += *argListBegin++; |
| 4653 | OrigLength++; |
| 4654 | } |
| 4655 | buf += *argListBegin; |
| 4656 | buf += "*/"; |
| 4657 | } |
| 4658 | else |
| 4659 | buf += *argListBegin; |
| 4660 | argListBegin++; |
| 4661 | OrigLength++; |
| 4662 | } |
| 4663 | buf += ')'; |
| 4664 | OrigLength++; |
| 4665 | } |
| 4666 | ReplaceText(Start, OrigLength, buf); |
| 4667 | |
| 4668 | return; |
| 4669 | } |
| 4670 | |
| 4671 | |
| 4672 | /// SynthesizeByrefCopyDestroyHelper - This routine synthesizes: |
| 4673 | /// void __Block_byref_id_object_copy(struct Block_byref_id_object *dst, |
| 4674 | /// struct Block_byref_id_object *src) { |
| 4675 | /// _Block_object_assign (&_dest->object, _src->object, |
| 4676 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4677 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4678 | /// _Block_object_assign(&_dest->object, _src->object, |
| 4679 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4680 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4681 | /// } |
| 4682 | /// And: |
| 4683 | /// void __Block_byref_id_object_dispose(struct Block_byref_id_object *_src) { |
| 4684 | /// _Block_object_dispose(_src->object, |
| 4685 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4686 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4687 | /// _Block_object_dispose(_src->object, |
| 4688 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4689 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4690 | /// } |
| 4691 | |
| 4692 | std::string RewriteModernObjC::SynthesizeByrefCopyDestroyHelper(VarDecl *VD, |
| 4693 | int flag) { |
| 4694 | std::string S; |
| 4695 | if (CopyDestroyCache.count(flag)) |
| 4696 | return S; |
| 4697 | CopyDestroyCache.insert(flag); |
| 4698 | S = "static void __Block_byref_id_object_copy_"; |
| 4699 | S += utostr(flag); |
| 4700 | S += "(void *dst, void *src) {\n"; |
| 4701 | |
| 4702 | // offset into the object pointer is computed as: |
| 4703 | // void * + void* + int + int + void* + void * |
| 4704 | unsigned IntSize = |
| 4705 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 4706 | unsigned VoidPtrSize = |
| 4707 | static_cast<unsigned>(Context->getTypeSize(Context->VoidPtrTy)); |
| 4708 | |
| 4709 | unsigned offset = (VoidPtrSize*4 + IntSize + IntSize)/Context->getCharWidth(); |
| 4710 | S += " _Block_object_assign((char*)dst + "; |
| 4711 | S += utostr(offset); |
| 4712 | S += ", *(void * *) ((char*)src + "; |
| 4713 | S += utostr(offset); |
| 4714 | S += "), "; |
| 4715 | S += utostr(flag); |
| 4716 | S += ");\n}\n"; |
| 4717 | |
| 4718 | S += "static void __Block_byref_id_object_dispose_"; |
| 4719 | S += utostr(flag); |
| 4720 | S += "(void *src) {\n"; |
| 4721 | S += " _Block_object_dispose(*(void * *) ((char*)src + "; |
| 4722 | S += utostr(offset); |
| 4723 | S += "), "; |
| 4724 | S += utostr(flag); |
| 4725 | S += ");\n}\n"; |
| 4726 | return S; |
| 4727 | } |
| 4728 | |
| 4729 | /// RewriteByRefVar - For each __block typex ND variable this routine transforms |
| 4730 | /// the declaration into: |
| 4731 | /// struct __Block_byref_ND { |
| 4732 | /// void *__isa; // NULL for everything except __weak pointers |
| 4733 | /// struct __Block_byref_ND *__forwarding; |
| 4734 | /// int32_t __flags; |
| 4735 | /// int32_t __size; |
| 4736 | /// void *__Block_byref_id_object_copy; // If variable is __block ObjC object |
| 4737 | /// void *__Block_byref_id_object_dispose; // If variable is __block ObjC object |
| 4738 | /// typex ND; |
| 4739 | /// }; |
| 4740 | /// |
| 4741 | /// It then replaces declaration of ND variable with: |
| 4742 | /// struct __Block_byref_ND ND = {__isa=0B, __forwarding=&ND, __flags=some_flag, |
| 4743 | /// __size=sizeof(struct __Block_byref_ND), |
| 4744 | /// ND=initializer-if-any}; |
| 4745 | /// |
| 4746 | /// |
Fariborz Jahanian | 4fe261c | 2012-04-24 19:38:45 +0000 | [diff] [blame] | 4747 | void RewriteModernObjC::RewriteByRefVar(VarDecl *ND, bool firstDecl, |
| 4748 | bool lastDecl) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4749 | int flag = 0; |
| 4750 | int isa = 0; |
| 4751 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
| 4752 | if (DeclLoc.isInvalid()) |
| 4753 | // If type location is missing, it is because of missing type (a warning). |
| 4754 | // Use variable's location which is good for this case. |
| 4755 | DeclLoc = ND->getLocation(); |
| 4756 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4757 | SourceLocation X = ND->getLocEnd(); |
| 4758 | X = SM->getExpansionLoc(X); |
| 4759 | const char *endBuf = SM->getCharacterData(X); |
| 4760 | std::string Name(ND->getNameAsString()); |
| 4761 | std::string ByrefType; |
| 4762 | RewriteByRefString(ByrefType, Name, ND, true); |
| 4763 | ByrefType += " {\n"; |
| 4764 | ByrefType += " void *__isa;\n"; |
| 4765 | RewriteByRefString(ByrefType, Name, ND); |
| 4766 | ByrefType += " *__forwarding;\n"; |
| 4767 | ByrefType += " int __flags;\n"; |
| 4768 | ByrefType += " int __size;\n"; |
| 4769 | // Add void *__Block_byref_id_object_copy; |
| 4770 | // void *__Block_byref_id_object_dispose; if needed. |
| 4771 | QualType Ty = ND->getType(); |
| 4772 | bool HasCopyAndDispose = Context->BlockRequiresCopying(Ty); |
| 4773 | if (HasCopyAndDispose) { |
| 4774 | ByrefType += " void (*__Block_byref_id_object_copy)(void*, void*);\n"; |
| 4775 | ByrefType += " void (*__Block_byref_id_object_dispose)(void*);\n"; |
| 4776 | } |
| 4777 | |
| 4778 | QualType T = Ty; |
| 4779 | (void)convertBlockPointerToFunctionPointer(T); |
| 4780 | T.getAsStringInternal(Name, Context->getPrintingPolicy()); |
| 4781 | |
| 4782 | ByrefType += " " + Name + ";\n"; |
| 4783 | ByrefType += "};\n"; |
| 4784 | // Insert this type in global scope. It is needed by helper function. |
| 4785 | SourceLocation FunLocStart; |
| 4786 | if (CurFunctionDef) |
Fariborz Jahanian | b75f8de | 2012-04-19 00:50:01 +0000 | [diff] [blame] | 4787 | FunLocStart = getFunctionSourceLocation(*this, CurFunctionDef); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4788 | else { |
| 4789 | assert(CurMethodDef && "RewriteByRefVar - CurMethodDef is null"); |
| 4790 | FunLocStart = CurMethodDef->getLocStart(); |
| 4791 | } |
| 4792 | InsertText(FunLocStart, ByrefType); |
Fariborz Jahanian | 8247c4e | 2012-04-24 16:45:27 +0000 | [diff] [blame] | 4793 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4794 | if (Ty.isObjCGCWeak()) { |
| 4795 | flag |= BLOCK_FIELD_IS_WEAK; |
| 4796 | isa = 1; |
| 4797 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4798 | if (HasCopyAndDispose) { |
| 4799 | flag = BLOCK_BYREF_CALLER; |
| 4800 | QualType Ty = ND->getType(); |
| 4801 | // FIXME. Handle __weak variable (BLOCK_FIELD_IS_WEAK) as well. |
| 4802 | if (Ty->isBlockPointerType()) |
| 4803 | flag |= BLOCK_FIELD_IS_BLOCK; |
| 4804 | else |
| 4805 | flag |= BLOCK_FIELD_IS_OBJECT; |
| 4806 | std::string HF = SynthesizeByrefCopyDestroyHelper(ND, flag); |
| 4807 | if (!HF.empty()) |
| 4808 | InsertText(FunLocStart, HF); |
| 4809 | } |
| 4810 | |
| 4811 | // struct __Block_byref_ND ND = |
| 4812 | // {0, &ND, some_flag, __size=sizeof(struct __Block_byref_ND), |
| 4813 | // initializer-if-any}; |
| 4814 | bool hasInit = (ND->getInit() != 0); |
Fariborz Jahanian | 104dbf9 | 2012-04-11 23:57:12 +0000 | [diff] [blame] | 4815 | // FIXME. rewriter does not support __block c++ objects which |
| 4816 | // require construction. |
Fariborz Jahanian | 65a7c68 | 2012-04-26 23:20:25 +0000 | [diff] [blame] | 4817 | if (hasInit) |
| 4818 | if (CXXConstructExpr *CExp = dyn_cast<CXXConstructExpr>(ND->getInit())) { |
| 4819 | CXXConstructorDecl *CXXDecl = CExp->getConstructor(); |
| 4820 | if (CXXDecl && CXXDecl->isDefaultConstructor()) |
| 4821 | hasInit = false; |
| 4822 | } |
| 4823 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4824 | unsigned flags = 0; |
| 4825 | if (HasCopyAndDispose) |
| 4826 | flags |= BLOCK_HAS_COPY_DISPOSE; |
| 4827 | Name = ND->getNameAsString(); |
| 4828 | ByrefType.clear(); |
| 4829 | RewriteByRefString(ByrefType, Name, ND); |
| 4830 | std::string ForwardingCastType("("); |
| 4831 | ForwardingCastType += ByrefType + " *)"; |
Fariborz Jahanian | 8247c4e | 2012-04-24 16:45:27 +0000 | [diff] [blame] | 4832 | ByrefType += " " + Name + " = {(void*)"; |
| 4833 | ByrefType += utostr(isa); |
| 4834 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
| 4835 | ByrefType += utostr(flags); |
| 4836 | ByrefType += ", "; |
| 4837 | ByrefType += "sizeof("; |
| 4838 | RewriteByRefString(ByrefType, Name, ND); |
| 4839 | ByrefType += ")"; |
| 4840 | if (HasCopyAndDispose) { |
| 4841 | ByrefType += ", __Block_byref_id_object_copy_"; |
| 4842 | ByrefType += utostr(flag); |
| 4843 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 4844 | ByrefType += utostr(flag); |
| 4845 | } |
| 4846 | |
Fariborz Jahanian | 4fe261c | 2012-04-24 19:38:45 +0000 | [diff] [blame] | 4847 | if (!firstDecl) { |
| 4848 | // In multiple __block declarations, and for all but 1st declaration, |
| 4849 | // find location of the separating comma. This would be start location |
| 4850 | // where new text is to be inserted. |
| 4851 | DeclLoc = ND->getLocation(); |
| 4852 | const char *startDeclBuf = SM->getCharacterData(DeclLoc); |
| 4853 | const char *commaBuf = startDeclBuf; |
| 4854 | while (*commaBuf != ',') |
| 4855 | commaBuf--; |
| 4856 | assert((*commaBuf == ',') && "RewriteByRefVar: can't find ','"); |
| 4857 | DeclLoc = DeclLoc.getLocWithOffset(commaBuf - startDeclBuf); |
| 4858 | startBuf = commaBuf; |
| 4859 | } |
| 4860 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4861 | if (!hasInit) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4862 | ByrefType += "};\n"; |
| 4863 | unsigned nameSize = Name.size(); |
| 4864 | // for block or function pointer declaration. Name is aleady |
| 4865 | // part of the declaration. |
| 4866 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) |
| 4867 | nameSize = 1; |
| 4868 | ReplaceText(DeclLoc, endBuf-startBuf+nameSize, ByrefType); |
| 4869 | } |
| 4870 | else { |
Fariborz Jahanian | 8247c4e | 2012-04-24 16:45:27 +0000 | [diff] [blame] | 4871 | ByrefType += ", "; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4872 | SourceLocation startLoc; |
| 4873 | Expr *E = ND->getInit(); |
| 4874 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 4875 | startLoc = ECE->getLParenLoc(); |
| 4876 | else |
| 4877 | startLoc = E->getLocStart(); |
| 4878 | startLoc = SM->getExpansionLoc(startLoc); |
| 4879 | endBuf = SM->getCharacterData(startLoc); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4880 | ReplaceText(DeclLoc, endBuf-startBuf, ByrefType); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4881 | |
Fariborz Jahanian | 4fe261c | 2012-04-24 19:38:45 +0000 | [diff] [blame] | 4882 | const char separator = lastDecl ? ';' : ','; |
| 4883 | const char *startInitializerBuf = SM->getCharacterData(startLoc); |
| 4884 | const char *separatorBuf = strchr(startInitializerBuf, separator); |
| 4885 | assert((*separatorBuf == separator) && |
| 4886 | "RewriteByRefVar: can't find ';' or ','"); |
| 4887 | SourceLocation separatorLoc = |
| 4888 | startLoc.getLocWithOffset(separatorBuf-startInitializerBuf); |
| 4889 | |
| 4890 | InsertText(separatorLoc, lastDecl ? "}" : "};\n"); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4891 | } |
| 4892 | return; |
| 4893 | } |
| 4894 | |
| 4895 | void RewriteModernObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { |
| 4896 | // Add initializers for any closure decl refs. |
| 4897 | GetBlockDeclRefExprs(Exp->getBody()); |
| 4898 | if (BlockDeclRefs.size()) { |
| 4899 | // Unique all "by copy" declarations. |
| 4900 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4901 | if (!BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>()) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4902 | if (!BlockByCopyDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 4903 | BlockByCopyDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 4904 | BlockByCopyDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 4905 | } |
| 4906 | } |
| 4907 | // Unique all "by ref" declarations. |
| 4908 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4909 | if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>()) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4910 | if (!BlockByRefDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 4911 | BlockByRefDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 4912 | BlockByRefDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 4913 | } |
| 4914 | } |
| 4915 | // Find any imported blocks...they will need special attention. |
| 4916 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4917 | if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>() || |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4918 | BlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
| 4919 | BlockDeclRefs[i]->getType()->isBlockPointerType()) |
| 4920 | ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 4921 | } |
| 4922 | } |
| 4923 | |
| 4924 | FunctionDecl *RewriteModernObjC::SynthBlockInitFunctionDecl(StringRef name) { |
| 4925 | IdentifierInfo *ID = &Context->Idents.get(name); |
| 4926 | QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy); |
| 4927 | return FunctionDecl::Create(*Context, TUDecl, SourceLocation(), |
| 4928 | SourceLocation(), ID, FType, 0, SC_Extern, |
| 4929 | SC_None, false, false); |
| 4930 | } |
| 4931 | |
| 4932 | Stmt *RewriteModernObjC::SynthBlockInitExpr(BlockExpr *Exp, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4933 | const SmallVector<DeclRefExpr *, 8> &InnerBlockDeclRefs) { |
Fariborz Jahanian | d13c2c2 | 2012-03-22 19:54:39 +0000 | [diff] [blame] | 4934 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4935 | const BlockDecl *block = Exp->getBlockDecl(); |
Fariborz Jahanian | d13c2c2 | 2012-03-22 19:54:39 +0000 | [diff] [blame] | 4936 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4937 | Blocks.push_back(Exp); |
| 4938 | |
| 4939 | CollectBlockDeclRefInfo(Exp); |
| 4940 | |
| 4941 | // Add inner imported variables now used in current block. |
| 4942 | int countOfInnerDecls = 0; |
| 4943 | if (!InnerBlockDeclRefs.empty()) { |
| 4944 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) { |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4945 | DeclRefExpr *Exp = InnerBlockDeclRefs[i]; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4946 | ValueDecl *VD = Exp->getDecl(); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4947 | if (!VD->hasAttr<BlocksAttr>() && !BlockByCopyDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4948 | // We need to save the copied-in variables in nested |
| 4949 | // blocks because it is needed at the end for some of the API generations. |
| 4950 | // See SynthesizeBlockLiterals routine. |
| 4951 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 4952 | BlockDeclRefs.push_back(Exp); |
| 4953 | BlockByCopyDeclsPtrSet.insert(VD); |
| 4954 | BlockByCopyDecls.push_back(VD); |
| 4955 | } |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4956 | if (VD->hasAttr<BlocksAttr>() && !BlockByRefDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4957 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 4958 | BlockDeclRefs.push_back(Exp); |
| 4959 | BlockByRefDeclsPtrSet.insert(VD); |
| 4960 | BlockByRefDecls.push_back(VD); |
| 4961 | } |
| 4962 | } |
| 4963 | // Find any imported blocks...they will need special attention. |
| 4964 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4965 | if (InnerBlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>() || |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4966 | InnerBlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
| 4967 | InnerBlockDeclRefs[i]->getType()->isBlockPointerType()) |
| 4968 | ImportedBlockDecls.insert(InnerBlockDeclRefs[i]->getDecl()); |
| 4969 | } |
| 4970 | InnerDeclRefsCount.push_back(countOfInnerDecls); |
| 4971 | |
| 4972 | std::string FuncName; |
| 4973 | |
| 4974 | if (CurFunctionDef) |
| 4975 | FuncName = CurFunctionDef->getNameAsString(); |
| 4976 | else if (CurMethodDef) |
| 4977 | BuildUniqueMethodName(FuncName, CurMethodDef); |
| 4978 | else if (GlobalVarDecl) |
| 4979 | FuncName = std::string(GlobalVarDecl->getNameAsString()); |
| 4980 | |
Fariborz Jahanian | df474ec | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 4981 | bool GlobalBlockExpr = |
| 4982 | block->getDeclContext()->getRedeclContext()->isFileContext(); |
| 4983 | |
| 4984 | if (GlobalBlockExpr && !GlobalVarDecl) { |
| 4985 | Diags.Report(block->getLocation(), GlobalBlockRewriteFailedDiag); |
| 4986 | GlobalBlockExpr = false; |
| 4987 | } |
| 4988 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4989 | std::string BlockNumber = utostr(Blocks.size()-1); |
| 4990 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4991 | std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; |
| 4992 | |
| 4993 | // Get a pointer to the function type so we can cast appropriately. |
| 4994 | QualType BFT = convertFunctionTypeOfBlocks(Exp->getFunctionType()); |
| 4995 | QualType FType = Context->getPointerType(BFT); |
| 4996 | |
| 4997 | FunctionDecl *FD; |
| 4998 | Expr *NewRep; |
| 4999 | |
| 5000 | // Simulate a contructor call... |
Fariborz Jahanian | df474ec | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 5001 | std::string Tag; |
| 5002 | |
| 5003 | if (GlobalBlockExpr) |
| 5004 | Tag = "__global_"; |
| 5005 | else |
| 5006 | Tag = "__"; |
| 5007 | Tag += FuncName + "_block_impl_" + BlockNumber; |
| 5008 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5009 | FD = SynthBlockInitFunctionDecl(Tag); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5010 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, false, FType, VK_RValue, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5011 | SourceLocation()); |
| 5012 | |
| 5013 | SmallVector<Expr*, 4> InitExprs; |
| 5014 | |
| 5015 | // Initialize the block function. |
| 5016 | FD = SynthBlockInitFunctionDecl(Func); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5017 | DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, false, FD->getType(), |
| 5018 | VK_LValue, SourceLocation()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5019 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
| 5020 | CK_BitCast, Arg); |
| 5021 | InitExprs.push_back(castExpr); |
| 5022 | |
| 5023 | // Initialize the block descriptor. |
| 5024 | std::string DescData = "__" + FuncName + "_block_desc_" + BlockNumber + "_DATA"; |
| 5025 | |
| 5026 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, |
| 5027 | SourceLocation(), SourceLocation(), |
| 5028 | &Context->Idents.get(DescData.c_str()), |
| 5029 | Context->VoidPtrTy, 0, |
| 5030 | SC_Static, SC_None); |
| 5031 | UnaryOperator *DescRefExpr = |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5032 | new (Context) UnaryOperator(new (Context) DeclRefExpr(NewVD, false, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5033 | Context->VoidPtrTy, |
| 5034 | VK_LValue, |
| 5035 | SourceLocation()), |
| 5036 | UO_AddrOf, |
| 5037 | Context->getPointerType(Context->VoidPtrTy), |
| 5038 | VK_RValue, OK_Ordinary, |
| 5039 | SourceLocation()); |
| 5040 | InitExprs.push_back(DescRefExpr); |
| 5041 | |
| 5042 | // Add initializers for any closure decl refs. |
| 5043 | if (BlockDeclRefs.size()) { |
| 5044 | Expr *Exp; |
| 5045 | // Output all "by copy" declarations. |
| 5046 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 5047 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 5048 | if (isObjCType((*I)->getType())) { |
| 5049 | // FIXME: Conform to ABI ([[obj retain] autorelease]). |
| 5050 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5051 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), |
| 5052 | VK_LValue, SourceLocation()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5053 | if (HasLocalVariableExternalStorage(*I)) { |
| 5054 | QualType QT = (*I)->getType(); |
| 5055 | QT = Context->getPointerType(QT); |
| 5056 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue, |
| 5057 | OK_Ordinary, SourceLocation()); |
| 5058 | } |
| 5059 | } else if (isTopLevelBlockPointerType((*I)->getType())) { |
| 5060 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5061 | Arg = new (Context) DeclRefExpr(FD, false, FD->getType(), |
| 5062 | VK_LValue, SourceLocation()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5063 | Exp = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
| 5064 | CK_BitCast, Arg); |
| 5065 | } else { |
| 5066 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5067 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), |
| 5068 | VK_LValue, SourceLocation()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5069 | if (HasLocalVariableExternalStorage(*I)) { |
| 5070 | QualType QT = (*I)->getType(); |
| 5071 | QT = Context->getPointerType(QT); |
| 5072 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue, |
| 5073 | OK_Ordinary, SourceLocation()); |
| 5074 | } |
| 5075 | |
| 5076 | } |
| 5077 | InitExprs.push_back(Exp); |
| 5078 | } |
| 5079 | // Output all "by ref" declarations. |
| 5080 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 5081 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 5082 | ValueDecl *ND = (*I); |
| 5083 | std::string Name(ND->getNameAsString()); |
| 5084 | std::string RecName; |
| 5085 | RewriteByRefString(RecName, Name, ND, true); |
| 5086 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str() |
| 5087 | + sizeof("struct")); |
| 5088 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 5089 | SourceLocation(), SourceLocation(), |
| 5090 | II); |
| 5091 | assert(RD && "SynthBlockInitExpr(): Can't find RecordDecl"); |
| 5092 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 5093 | |
| 5094 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5095 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5096 | SourceLocation()); |
| 5097 | bool isNestedCapturedVar = false; |
| 5098 | if (block) |
| 5099 | for (BlockDecl::capture_const_iterator ci = block->capture_begin(), |
| 5100 | ce = block->capture_end(); ci != ce; ++ci) { |
| 5101 | const VarDecl *variable = ci->getVariable(); |
| 5102 | if (variable == ND && ci->isNested()) { |
| 5103 | assert (ci->isByRef() && |
| 5104 | "SynthBlockInitExpr - captured block variable is not byref"); |
| 5105 | isNestedCapturedVar = true; |
| 5106 | break; |
| 5107 | } |
| 5108 | } |
| 5109 | // captured nested byref variable has its address passed. Do not take |
| 5110 | // its address again. |
| 5111 | if (!isNestedCapturedVar) |
| 5112 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, |
| 5113 | Context->getPointerType(Exp->getType()), |
| 5114 | VK_RValue, OK_Ordinary, SourceLocation()); |
| 5115 | Exp = NoTypeInfoCStyleCastExpr(Context, castT, CK_BitCast, Exp); |
| 5116 | InitExprs.push_back(Exp); |
| 5117 | } |
| 5118 | } |
| 5119 | if (ImportedBlockDecls.size()) { |
| 5120 | // generate BLOCK_HAS_COPY_DISPOSE(have helper funcs) | BLOCK_HAS_DESCRIPTOR |
| 5121 | int flag = (BLOCK_HAS_COPY_DISPOSE | BLOCK_HAS_DESCRIPTOR); |
| 5122 | unsigned IntSize = |
| 5123 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 5124 | Expr *FlagExp = IntegerLiteral::Create(*Context, llvm::APInt(IntSize, flag), |
| 5125 | Context->IntTy, SourceLocation()); |
| 5126 | InitExprs.push_back(FlagExp); |
| 5127 | } |
| 5128 | NewRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], InitExprs.size(), |
| 5129 | FType, VK_LValue, SourceLocation()); |
Fariborz Jahanian | df474ec | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 5130 | |
| 5131 | if (GlobalBlockExpr) { |
| 5132 | assert (GlobalConstructionExp == 0 && |
| 5133 | "SynthBlockInitExpr - GlobalConstructionExp must be null"); |
| 5134 | GlobalConstructionExp = NewRep; |
| 5135 | NewRep = DRE; |
| 5136 | } |
| 5137 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5138 | NewRep = new (Context) UnaryOperator(NewRep, UO_AddrOf, |
| 5139 | Context->getPointerType(NewRep->getType()), |
| 5140 | VK_RValue, OK_Ordinary, SourceLocation()); |
| 5141 | NewRep = NoTypeInfoCStyleCastExpr(Context, FType, CK_BitCast, |
| 5142 | NewRep); |
| 5143 | BlockDeclRefs.clear(); |
| 5144 | BlockByRefDecls.clear(); |
| 5145 | BlockByRefDeclsPtrSet.clear(); |
| 5146 | BlockByCopyDecls.clear(); |
| 5147 | BlockByCopyDeclsPtrSet.clear(); |
| 5148 | ImportedBlockDecls.clear(); |
| 5149 | return NewRep; |
| 5150 | } |
| 5151 | |
| 5152 | bool RewriteModernObjC::IsDeclStmtInForeachHeader(DeclStmt *DS) { |
| 5153 | if (const ObjCForCollectionStmt * CS = |
| 5154 | dyn_cast<ObjCForCollectionStmt>(Stmts.back())) |
| 5155 | return CS->getElement() == DS; |
| 5156 | return false; |
| 5157 | } |
| 5158 | |
| 5159 | //===----------------------------------------------------------------------===// |
| 5160 | // Function Body / Expression rewriting |
| 5161 | //===----------------------------------------------------------------------===// |
| 5162 | |
| 5163 | Stmt *RewriteModernObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
| 5164 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
| 5165 | isa<DoStmt>(S) || isa<ForStmt>(S)) |
| 5166 | Stmts.push_back(S); |
| 5167 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 5168 | Stmts.push_back(S); |
| 5169 | ObjCBcLabelNo.push_back(++BcLabelCount); |
| 5170 | } |
| 5171 | |
| 5172 | // Pseudo-object operations and ivar references need special |
| 5173 | // treatment because we're going to recursively rewrite them. |
| 5174 | if (PseudoObjectExpr *PseudoOp = dyn_cast<PseudoObjectExpr>(S)) { |
| 5175 | if (isa<BinaryOperator>(PseudoOp->getSyntacticForm())) { |
| 5176 | return RewritePropertyOrImplicitSetter(PseudoOp); |
| 5177 | } else { |
| 5178 | return RewritePropertyOrImplicitGetter(PseudoOp); |
| 5179 | } |
| 5180 | } else if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) { |
| 5181 | return RewriteObjCIvarRefExpr(IvarRefExpr); |
| 5182 | } |
| 5183 | |
| 5184 | SourceRange OrigStmtRange = S->getSourceRange(); |
| 5185 | |
| 5186 | // Perform a bottom up rewrite of all children. |
| 5187 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
| 5188 | if (*CI) { |
| 5189 | Stmt *childStmt = (*CI); |
| 5190 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(childStmt); |
| 5191 | if (newStmt) { |
| 5192 | *CI = newStmt; |
| 5193 | } |
| 5194 | } |
| 5195 | |
| 5196 | if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5197 | SmallVector<DeclRefExpr *, 8> InnerBlockDeclRefs; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5198 | llvm::SmallPtrSet<const DeclContext *, 8> InnerContexts; |
| 5199 | InnerContexts.insert(BE->getBlockDecl()); |
| 5200 | ImportedLocalExternalDecls.clear(); |
| 5201 | GetInnerBlockDeclRefExprs(BE->getBody(), |
| 5202 | InnerBlockDeclRefs, InnerContexts); |
| 5203 | // Rewrite the block body in place. |
| 5204 | Stmt *SaveCurrentBody = CurrentBody; |
| 5205 | CurrentBody = BE->getBody(); |
| 5206 | PropParentMap = 0; |
| 5207 | // block literal on rhs of a property-dot-sytax assignment |
| 5208 | // must be replaced by its synthesize ast so getRewrittenText |
| 5209 | // works as expected. In this case, what actually ends up on RHS |
| 5210 | // is the blockTranscribed which is the helper function for the |
| 5211 | // block literal; as in: self.c = ^() {[ace ARR];}; |
| 5212 | bool saveDisableReplaceStmt = DisableReplaceStmt; |
| 5213 | DisableReplaceStmt = false; |
| 5214 | RewriteFunctionBodyOrGlobalInitializer(BE->getBody()); |
| 5215 | DisableReplaceStmt = saveDisableReplaceStmt; |
| 5216 | CurrentBody = SaveCurrentBody; |
| 5217 | PropParentMap = 0; |
| 5218 | ImportedLocalExternalDecls.clear(); |
| 5219 | // Now we snarf the rewritten text and stash it away for later use. |
| 5220 | std::string Str = Rewrite.getRewrittenText(BE->getSourceRange()); |
| 5221 | RewrittenBlockExprs[BE] = Str; |
| 5222 | |
| 5223 | Stmt *blockTranscribed = SynthBlockInitExpr(BE, InnerBlockDeclRefs); |
| 5224 | |
| 5225 | //blockTranscribed->dump(); |
| 5226 | ReplaceStmt(S, blockTranscribed); |
| 5227 | return blockTranscribed; |
| 5228 | } |
| 5229 | // Handle specific things. |
| 5230 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 5231 | return RewriteAtEncode(AtEncode); |
| 5232 | |
| 5233 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 5234 | return RewriteAtSelector(AtSelector); |
| 5235 | |
| 5236 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 5237 | return RewriteObjCStringLiteral(AtString); |
Fariborz Jahanian | 5594704 | 2012-03-27 20:17:30 +0000 | [diff] [blame] | 5238 | |
| 5239 | if (ObjCBoolLiteralExpr *BoolLitExpr = dyn_cast<ObjCBoolLiteralExpr>(S)) |
| 5240 | return RewriteObjCBoolLiteralExpr(BoolLitExpr); |
Fariborz Jahanian | 0f9b18e | 2012-03-30 16:49:36 +0000 | [diff] [blame] | 5241 | |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 5242 | if (ObjCBoxedExpr *BoxedExpr = dyn_cast<ObjCBoxedExpr>(S)) |
| 5243 | return RewriteObjCBoxedExpr(BoxedExpr); |
Fariborz Jahanian | 86cff60 | 2012-03-30 23:35:47 +0000 | [diff] [blame] | 5244 | |
| 5245 | if (ObjCArrayLiteral *ArrayLitExpr = dyn_cast<ObjCArrayLiteral>(S)) |
| 5246 | return RewriteObjCArrayLiteralExpr(ArrayLitExpr); |
Fariborz Jahanian | e35abe1 | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 5247 | |
| 5248 | if (ObjCDictionaryLiteral *DictionaryLitExpr = |
| 5249 | dyn_cast<ObjCDictionaryLiteral>(S)) |
| 5250 | return RewriteObjCDictionaryLiteralExpr(DictionaryLitExpr); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5251 | |
| 5252 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
| 5253 | #if 0 |
| 5254 | // Before we rewrite it, put the original message expression in a comment. |
| 5255 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 5256 | SourceLocation endLoc = MessExpr->getLocEnd(); |
| 5257 | |
| 5258 | const char *startBuf = SM->getCharacterData(startLoc); |
| 5259 | const char *endBuf = SM->getCharacterData(endLoc); |
| 5260 | |
| 5261 | std::string messString; |
| 5262 | messString += "// "; |
| 5263 | messString.append(startBuf, endBuf-startBuf+1); |
| 5264 | messString += "\n"; |
| 5265 | |
| 5266 | // FIXME: Missing definition of |
| 5267 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
| 5268 | // InsertText(startLoc, messString.c_str(), messString.size()); |
| 5269 | // Tried this, but it didn't work either... |
| 5270 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
| 5271 | #endif |
| 5272 | return RewriteMessageExpr(MessExpr); |
| 5273 | } |
| 5274 | |
| 5275 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 5276 | return RewriteObjCTryStmt(StmtTry); |
| 5277 | |
| 5278 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 5279 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 5280 | |
| 5281 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 5282 | return RewriteObjCThrowStmt(StmtThrow); |
| 5283 | |
| 5284 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 5285 | return RewriteObjCProtocolExpr(ProtocolExp); |
| 5286 | |
| 5287 | if (ObjCForCollectionStmt *StmtForCollection = |
| 5288 | dyn_cast<ObjCForCollectionStmt>(S)) |
| 5289 | return RewriteObjCForCollectionStmt(StmtForCollection, |
| 5290 | OrigStmtRange.getEnd()); |
| 5291 | if (BreakStmt *StmtBreakStmt = |
| 5292 | dyn_cast<BreakStmt>(S)) |
| 5293 | return RewriteBreakStmt(StmtBreakStmt); |
| 5294 | if (ContinueStmt *StmtContinueStmt = |
| 5295 | dyn_cast<ContinueStmt>(S)) |
| 5296 | return RewriteContinueStmt(StmtContinueStmt); |
| 5297 | |
| 5298 | // Need to check for protocol refs (id <P>, Foo <P> *) in variable decls |
| 5299 | // and cast exprs. |
| 5300 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
| 5301 | // FIXME: What we're doing here is modifying the type-specifier that |
| 5302 | // precedes the first Decl. In the future the DeclGroup should have |
| 5303 | // a separate type-specifier that we can rewrite. |
| 5304 | // NOTE: We need to avoid rewriting the DeclStmt if it is within |
| 5305 | // the context of an ObjCForCollectionStmt. For example: |
| 5306 | // NSArray *someArray; |
| 5307 | // for (id <FooProtocol> index in someArray) ; |
| 5308 | // This is because RewriteObjCForCollectionStmt() does textual rewriting |
| 5309 | // and it depends on the original text locations/positions. |
| 5310 | if (Stmts.empty() || !IsDeclStmtInForeachHeader(DS)) |
| 5311 | RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin()); |
| 5312 | |
| 5313 | // Blocks rewrite rules. |
| 5314 | for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); |
| 5315 | DI != DE; ++DI) { |
| 5316 | Decl *SD = *DI; |
| 5317 | if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { |
| 5318 | if (isTopLevelBlockPointerType(ND->getType())) |
| 5319 | RewriteBlockPointerDecl(ND); |
| 5320 | else if (ND->getType()->isFunctionPointerType()) |
| 5321 | CheckFunctionPointerDecl(ND->getType(), ND); |
| 5322 | if (VarDecl *VD = dyn_cast<VarDecl>(SD)) { |
| 5323 | if (VD->hasAttr<BlocksAttr>()) { |
| 5324 | static unsigned uniqueByrefDeclCount = 0; |
| 5325 | assert(!BlockByRefDeclNo.count(ND) && |
| 5326 | "RewriteFunctionBodyOrGlobalInitializer: Duplicate byref decl"); |
| 5327 | BlockByRefDeclNo[ND] = uniqueByrefDeclCount++; |
Fariborz Jahanian | 4fe261c | 2012-04-24 19:38:45 +0000 | [diff] [blame] | 5328 | RewriteByRefVar(VD, (DI == DS->decl_begin()), ((DI+1) == DE)); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5329 | } |
| 5330 | else |
| 5331 | RewriteTypeOfDecl(VD); |
| 5332 | } |
| 5333 | } |
| 5334 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) { |
| 5335 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
| 5336 | RewriteBlockPointerDecl(TD); |
| 5337 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
| 5338 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 5339 | } |
| 5340 | } |
| 5341 | } |
| 5342 | |
| 5343 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) |
| 5344 | RewriteObjCQualifiedInterfaceTypes(CE); |
| 5345 | |
| 5346 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
| 5347 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 5348 | assert(!Stmts.empty() && "Statement stack is empty"); |
| 5349 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 5350 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
| 5351 | && "Statement stack mismatch"); |
| 5352 | Stmts.pop_back(); |
| 5353 | } |
| 5354 | // Handle blocks rewriting. |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5355 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 5356 | ValueDecl *VD = DRE->getDecl(); |
| 5357 | if (VD->hasAttr<BlocksAttr>()) |
| 5358 | return RewriteBlockDeclRefExpr(DRE); |
| 5359 | if (HasLocalVariableExternalStorage(VD)) |
| 5360 | return RewriteLocalVariableExternalStorage(DRE); |
| 5361 | } |
| 5362 | |
| 5363 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
| 5364 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
| 5365 | Stmt *BlockCall = SynthesizeBlockCall(CE, CE->getCallee()); |
| 5366 | ReplaceStmt(S, BlockCall); |
| 5367 | return BlockCall; |
| 5368 | } |
| 5369 | } |
| 5370 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) { |
| 5371 | RewriteCastExpr(CE); |
| 5372 | } |
Fariborz Jahanian | f1ee687 | 2012-04-10 00:08:18 +0000 | [diff] [blame] | 5373 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
| 5374 | RewriteImplicitCastObjCExpr(ICE); |
| 5375 | } |
Fariborz Jahanian | 43aa1c3 | 2012-04-16 22:14:01 +0000 | [diff] [blame] | 5376 | #if 0 |
Fariborz Jahanian | 653b7cf | 2012-04-13 18:00:54 +0000 | [diff] [blame] | 5377 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5378 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
| 5379 | CastExpr *Replacement = new (Context) CastExpr(ICE->getType(), |
| 5380 | ICE->getSubExpr(), |
| 5381 | SourceLocation()); |
| 5382 | // Get the new text. |
| 5383 | std::string SStr; |
| 5384 | llvm::raw_string_ostream Buf(SStr); |
| 5385 | Replacement->printPretty(Buf, *Context); |
| 5386 | const std::string &Str = Buf.str(); |
| 5387 | |
| 5388 | printf("CAST = %s\n", &Str[0]); |
| 5389 | InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 5390 | delete S; |
| 5391 | return Replacement; |
| 5392 | } |
| 5393 | #endif |
| 5394 | // Return this stmt unmodified. |
| 5395 | return S; |
| 5396 | } |
| 5397 | |
| 5398 | void RewriteModernObjC::RewriteRecordBody(RecordDecl *RD) { |
| 5399 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 5400 | e = RD->field_end(); i != e; ++i) { |
| 5401 | FieldDecl *FD = *i; |
| 5402 | if (isTopLevelBlockPointerType(FD->getType())) |
| 5403 | RewriteBlockPointerDecl(FD); |
| 5404 | if (FD->getType()->isObjCQualifiedIdType() || |
| 5405 | FD->getType()->isObjCQualifiedInterfaceType()) |
| 5406 | RewriteObjCQualifiedInterfaceTypes(FD); |
| 5407 | } |
| 5408 | } |
| 5409 | |
| 5410 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 5411 | /// main file of the input. |
| 5412 | void RewriteModernObjC::HandleDeclInMainFile(Decl *D) { |
| 5413 | switch (D->getKind()) { |
| 5414 | case Decl::Function: { |
| 5415 | FunctionDecl *FD = cast<FunctionDecl>(D); |
| 5416 | if (FD->isOverloadedOperator()) |
| 5417 | return; |
| 5418 | |
| 5419 | // Since function prototypes don't have ParmDecl's, we check the function |
| 5420 | // prototype. This enables us to rewrite function declarations and |
| 5421 | // definitions using the same code. |
| 5422 | RewriteBlocksInFunctionProtoType(FD->getType(), FD); |
| 5423 | |
Argyrios Kyrtzidis | 9335df3 | 2012-02-12 04:48:45 +0000 | [diff] [blame] | 5424 | if (!FD->isThisDeclarationADefinition()) |
| 5425 | break; |
| 5426 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5427 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
| 5428 | if (CompoundStmt *Body = dyn_cast_or_null<CompoundStmt>(FD->getBody())) { |
| 5429 | CurFunctionDef = FD; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5430 | CurrentBody = Body; |
| 5431 | Body = |
| 5432 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 5433 | FD->setBody(Body); |
| 5434 | CurrentBody = 0; |
| 5435 | if (PropParentMap) { |
| 5436 | delete PropParentMap; |
| 5437 | PropParentMap = 0; |
| 5438 | } |
| 5439 | // This synthesizes and inserts the block "impl" struct, invoke function, |
| 5440 | // and any copy/dispose helper functions. |
| 5441 | InsertBlockLiteralsWithinFunction(FD); |
| 5442 | CurFunctionDef = 0; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5443 | } |
| 5444 | break; |
| 5445 | } |
| 5446 | case Decl::ObjCMethod: { |
| 5447 | ObjCMethodDecl *MD = cast<ObjCMethodDecl>(D); |
| 5448 | if (CompoundStmt *Body = MD->getCompoundBody()) { |
| 5449 | CurMethodDef = MD; |
| 5450 | CurrentBody = Body; |
| 5451 | Body = |
| 5452 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 5453 | MD->setBody(Body); |
| 5454 | CurrentBody = 0; |
| 5455 | if (PropParentMap) { |
| 5456 | delete PropParentMap; |
| 5457 | PropParentMap = 0; |
| 5458 | } |
| 5459 | InsertBlockLiteralsWithinMethod(MD); |
| 5460 | CurMethodDef = 0; |
| 5461 | } |
| 5462 | break; |
| 5463 | } |
| 5464 | case Decl::ObjCImplementation: { |
| 5465 | ObjCImplementationDecl *CI = cast<ObjCImplementationDecl>(D); |
| 5466 | ClassImplementation.push_back(CI); |
| 5467 | break; |
| 5468 | } |
| 5469 | case Decl::ObjCCategoryImpl: { |
| 5470 | ObjCCategoryImplDecl *CI = cast<ObjCCategoryImplDecl>(D); |
| 5471 | CategoryImplementation.push_back(CI); |
| 5472 | break; |
| 5473 | } |
| 5474 | case Decl::Var: { |
| 5475 | VarDecl *VD = cast<VarDecl>(D); |
| 5476 | RewriteObjCQualifiedInterfaceTypes(VD); |
| 5477 | if (isTopLevelBlockPointerType(VD->getType())) |
| 5478 | RewriteBlockPointerDecl(VD); |
| 5479 | else if (VD->getType()->isFunctionPointerType()) { |
| 5480 | CheckFunctionPointerDecl(VD->getType(), VD); |
| 5481 | if (VD->getInit()) { |
| 5482 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
| 5483 | RewriteCastExpr(CE); |
| 5484 | } |
| 5485 | } |
| 5486 | } else if (VD->getType()->isRecordType()) { |
| 5487 | RecordDecl *RD = VD->getType()->getAs<RecordType>()->getDecl(); |
| 5488 | if (RD->isCompleteDefinition()) |
| 5489 | RewriteRecordBody(RD); |
| 5490 | } |
| 5491 | if (VD->getInit()) { |
| 5492 | GlobalVarDecl = VD; |
| 5493 | CurrentBody = VD->getInit(); |
| 5494 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
| 5495 | CurrentBody = 0; |
| 5496 | if (PropParentMap) { |
| 5497 | delete PropParentMap; |
| 5498 | PropParentMap = 0; |
| 5499 | } |
| 5500 | SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), VD->getName()); |
| 5501 | GlobalVarDecl = 0; |
| 5502 | |
| 5503 | // This is needed for blocks. |
| 5504 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
| 5505 | RewriteCastExpr(CE); |
| 5506 | } |
| 5507 | } |
| 5508 | break; |
| 5509 | } |
| 5510 | case Decl::TypeAlias: |
| 5511 | case Decl::Typedef: { |
| 5512 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
| 5513 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
| 5514 | RewriteBlockPointerDecl(TD); |
| 5515 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
| 5516 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 5517 | } |
| 5518 | break; |
| 5519 | } |
| 5520 | case Decl::CXXRecord: |
| 5521 | case Decl::Record: { |
| 5522 | RecordDecl *RD = cast<RecordDecl>(D); |
| 5523 | if (RD->isCompleteDefinition()) |
| 5524 | RewriteRecordBody(RD); |
| 5525 | break; |
| 5526 | } |
| 5527 | default: |
| 5528 | break; |
| 5529 | } |
| 5530 | // Nothing yet. |
| 5531 | } |
| 5532 | |
Fariborz Jahanian | 30650eb | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 5533 | /// Write_ProtocolExprReferencedMetadata - This routine writer out the |
| 5534 | /// protocol reference symbols in the for of: |
| 5535 | /// struct _protocol_t *PROTOCOL_REF = &PROTOCOL_METADATA. |
| 5536 | static void Write_ProtocolExprReferencedMetadata(ASTContext *Context, |
| 5537 | ObjCProtocolDecl *PDecl, |
| 5538 | std::string &Result) { |
| 5539 | // Also output .objc_protorefs$B section and its meta-data. |
| 5540 | if (Context->getLangOpts().MicrosoftExt) |
Fariborz Jahanian | bd78cfa | 2012-04-27 21:39:49 +0000 | [diff] [blame] | 5541 | Result += "static "; |
Fariborz Jahanian | 30650eb | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 5542 | Result += "struct _protocol_t *"; |
| 5543 | Result += "_OBJC_PROTOCOL_REFERENCE_$_"; |
| 5544 | Result += PDecl->getNameAsString(); |
| 5545 | Result += " = &"; |
| 5546 | Result += "_OBJC_PROTOCOL_"; Result += PDecl->getNameAsString(); |
| 5547 | Result += ";\n"; |
| 5548 | } |
| 5549 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5550 | void RewriteModernObjC::HandleTranslationUnit(ASTContext &C) { |
| 5551 | if (Diags.hasErrorOccurred()) |
| 5552 | return; |
| 5553 | |
| 5554 | RewriteInclude(); |
| 5555 | |
| 5556 | // Here's a great place to add any extra declarations that may be needed. |
| 5557 | // Write out meta data for each @protocol(<expr>). |
| 5558 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Fariborz Jahanian | 30650eb | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 5559 | E = ProtocolExprDecls.end(); I != E; ++I) { |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 5560 | RewriteObjCProtocolMetaData(*I, Preamble); |
Fariborz Jahanian | 30650eb | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 5561 | Write_ProtocolExprReferencedMetadata(Context, (*I), Preamble); |
| 5562 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5563 | |
| 5564 | InsertText(SM->getLocForStartOfFile(MainFileID), Preamble, false); |
Fariborz Jahanian | 5731778 | 2012-02-21 23:58:41 +0000 | [diff] [blame] | 5565 | for (unsigned i = 0, e = ObjCInterfacesSeen.size(); i < e; i++) { |
| 5566 | ObjCInterfaceDecl *CDecl = ObjCInterfacesSeen[i]; |
| 5567 | // Write struct declaration for the class matching its ivar declarations. |
| 5568 | // Note that for modern abi, this is postponed until the end of TU |
| 5569 | // because class extensions and the implementation might declare their own |
| 5570 | // private ivars. |
| 5571 | RewriteInterfaceDecl(CDecl); |
| 5572 | } |
Fariborz Jahanian | 72c88f1 | 2012-02-22 18:13:25 +0000 | [diff] [blame] | 5573 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5574 | if (ClassImplementation.size() || CategoryImplementation.size()) |
| 5575 | RewriteImplementations(); |
| 5576 | |
| 5577 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 5578 | // we are done. |
| 5579 | if (const RewriteBuffer *RewriteBuf = |
| 5580 | Rewrite.getRewriteBufferFor(MainFileID)) { |
| 5581 | //printf("Changed:\n"); |
| 5582 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 5583 | } else { |
| 5584 | llvm::errs() << "No changes\n"; |
| 5585 | } |
| 5586 | |
| 5587 | if (ClassImplementation.size() || CategoryImplementation.size() || |
| 5588 | ProtocolExprDecls.size()) { |
| 5589 | // Rewrite Objective-c meta data* |
| 5590 | std::string ResultStr; |
| 5591 | RewriteMetaDataIntoBuffer(ResultStr); |
| 5592 | // Emit metadata. |
| 5593 | *OutFile << ResultStr; |
| 5594 | } |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 5595 | // Emit ImageInfo; |
| 5596 | { |
| 5597 | std::string ResultStr; |
| 5598 | WriteImageInfo(ResultStr); |
| 5599 | *OutFile << ResultStr; |
| 5600 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5601 | OutFile->flush(); |
| 5602 | } |
| 5603 | |
| 5604 | void RewriteModernObjC::Initialize(ASTContext &context) { |
| 5605 | InitializeCommon(context); |
| 5606 | |
Fariborz Jahanian | 6991bc5 | 2012-03-10 17:45:38 +0000 | [diff] [blame] | 5607 | Preamble += "#ifndef __OBJC2__\n"; |
| 5608 | Preamble += "#define __OBJC2__\n"; |
| 5609 | Preamble += "#endif\n"; |
| 5610 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5611 | // declaring objc_selector outside the parameter list removes a silly |
| 5612 | // scope related warning... |
| 5613 | if (IsHeader) |
| 5614 | Preamble = "#pragma once\n"; |
| 5615 | Preamble += "struct objc_selector; struct objc_class;\n"; |
Fariborz Jahanian | e2d87bc | 2012-04-12 23:52:52 +0000 | [diff] [blame] | 5616 | Preamble += "struct __rw_objc_super { \n\tstruct objc_object *object; "; |
| 5617 | Preamble += "\n\tstruct objc_object *superClass; "; |
| 5618 | // Add a constructor for creating temporary objects. |
| 5619 | Preamble += "\n\t__rw_objc_super(struct objc_object *o, struct objc_object *s) "; |
| 5620 | Preamble += ": object(o), superClass(s) {} "; |
| 5621 | Preamble += "\n};\n"; |
| 5622 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5623 | if (LangOpts.MicrosoftExt) { |
Fariborz Jahanian | 1ca052c | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 5624 | // Define all sections using syntax that makes sense. |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 5625 | // These are currently generated. |
| 5626 | Preamble += "\n#pragma section(\".objc_classlist$B\", long, read, write)\n"; |
Fariborz Jahanian | 1ca052c | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 5627 | Preamble += "#pragma section(\".objc_catlist$B\", long, read, write)\n"; |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 5628 | Preamble += "#pragma section(\".objc_imageinfo$B\", long, read, write)\n"; |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 5629 | Preamble += "#pragma section(\".objc_nlclslist$B\", long, read, write)\n"; |
| 5630 | Preamble += "#pragma section(\".objc_nlcatlist$B\", long, read, write)\n"; |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 5631 | // These are generated but not necessary for functionality. |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 5632 | Preamble += "#pragma section(\".cat_cls_meth$B\", long, read, write)\n"; |
Fariborz Jahanian | 1ca052c | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 5633 | Preamble += "#pragma section(\".inst_meth$B\", long, read, write)\n"; |
| 5634 | Preamble += "#pragma section(\".cls_meth$B\", long, read, write)\n"; |
Fariborz Jahanian | 40a777a | 2012-03-12 16:46:58 +0000 | [diff] [blame] | 5635 | Preamble += "#pragma section(\".objc_ivar$B\", long, read, write)\n"; |
Fariborz Jahanian | de5d946 | 2012-03-14 18:09:23 +0000 | [diff] [blame] | 5636 | |
Fariborz Jahanian | 30650eb | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 5637 | // These need be generated for performance. Currently they are not, |
| 5638 | // using API calls instead. |
| 5639 | Preamble += "#pragma section(\".objc_selrefs$B\", long, read, write)\n"; |
| 5640 | Preamble += "#pragma section(\".objc_classrefs$B\", long, read, write)\n"; |
| 5641 | Preamble += "#pragma section(\".objc_superrefs$B\", long, read, write)\n"; |
| 5642 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5643 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5644 | Preamble += "#ifndef _REWRITER_typedef_Protocol\n"; |
| 5645 | Preamble += "typedef struct objc_object Protocol;\n"; |
| 5646 | Preamble += "#define _REWRITER_typedef_Protocol\n"; |
| 5647 | Preamble += "#endif\n"; |
| 5648 | if (LangOpts.MicrosoftExt) { |
| 5649 | Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n"; |
| 5650 | Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n"; |
Fariborz Jahanian | 5cf6b6c | 2012-03-21 23:41:04 +0000 | [diff] [blame] | 5651 | } |
| 5652 | else |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5653 | Preamble += "#define __OBJC_RW_DLLIMPORT extern\n"; |
Fariborz Jahanian | 5cf6b6c | 2012-03-21 23:41:04 +0000 | [diff] [blame] | 5654 | |
| 5655 | Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSend(void);\n"; |
| 5656 | Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSendSuper(void);\n"; |
| 5657 | Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSend_stret(void);\n"; |
| 5658 | Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSendSuper_stret(void);\n"; |
| 5659 | Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSend_fpret(void);\n"; |
| 5660 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5661 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getClass"; |
| 5662 | Preamble += "(const char *);\n"; |
| 5663 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_class *class_getSuperclass"; |
| 5664 | Preamble += "(struct objc_class *);\n"; |
| 5665 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getMetaClass"; |
| 5666 | Preamble += "(const char *);\n"; |
Fariborz Jahanian | 55261af | 2012-03-19 18:11:32 +0000 | [diff] [blame] | 5667 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw( struct objc_object *);\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5668 | // @synchronized hooks. |
Fariborz Jahanian | 55261af | 2012-03-19 18:11:32 +0000 | [diff] [blame] | 5669 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_enter( struct objc_object *);\n"; |
| 5670 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_exit( struct objc_object *);\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5671 | Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n"; |
| 5672 | Preamble += "#ifndef __FASTENUMERATIONSTATE\n"; |
| 5673 | Preamble += "struct __objcFastEnumerationState {\n\t"; |
| 5674 | Preamble += "unsigned long state;\n\t"; |
| 5675 | Preamble += "void **itemsPtr;\n\t"; |
| 5676 | Preamble += "unsigned long *mutationsPtr;\n\t"; |
| 5677 | Preamble += "unsigned long extra[5];\n};\n"; |
| 5678 | Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n"; |
| 5679 | Preamble += "#define __FASTENUMERATIONSTATE\n"; |
| 5680 | Preamble += "#endif\n"; |
| 5681 | Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n"; |
| 5682 | Preamble += "struct __NSConstantStringImpl {\n"; |
| 5683 | Preamble += " int *isa;\n"; |
| 5684 | Preamble += " int flags;\n"; |
| 5685 | Preamble += " char *str;\n"; |
| 5686 | Preamble += " long length;\n"; |
| 5687 | Preamble += "};\n"; |
| 5688 | Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n"; |
| 5689 | Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n"; |
| 5690 | Preamble += "#else\n"; |
| 5691 | Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n"; |
| 5692 | Preamble += "#endif\n"; |
| 5693 | Preamble += "#define __NSCONSTANTSTRINGIMPL\n"; |
| 5694 | Preamble += "#endif\n"; |
| 5695 | // Blocks preamble. |
| 5696 | Preamble += "#ifndef BLOCK_IMPL\n"; |
| 5697 | Preamble += "#define BLOCK_IMPL\n"; |
| 5698 | Preamble += "struct __block_impl {\n"; |
| 5699 | Preamble += " void *isa;\n"; |
| 5700 | Preamble += " int Flags;\n"; |
| 5701 | Preamble += " int Reserved;\n"; |
| 5702 | Preamble += " void *FuncPtr;\n"; |
| 5703 | Preamble += "};\n"; |
| 5704 | Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n"; |
| 5705 | Preamble += "#ifdef __OBJC_EXPORT_BLOCKS\n"; |
| 5706 | Preamble += "extern \"C\" __declspec(dllexport) " |
| 5707 | "void _Block_object_assign(void *, const void *, const int);\n"; |
| 5708 | Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_dispose(const void *, const int);\n"; |
| 5709 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteGlobalBlock[32];\n"; |
| 5710 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteStackBlock[32];\n"; |
| 5711 | Preamble += "#else\n"; |
| 5712 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_assign(void *, const void *, const int);\n"; |
| 5713 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_dispose(const void *, const int);\n"; |
| 5714 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteGlobalBlock[32];\n"; |
| 5715 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteStackBlock[32];\n"; |
| 5716 | Preamble += "#endif\n"; |
| 5717 | Preamble += "#endif\n"; |
| 5718 | if (LangOpts.MicrosoftExt) { |
| 5719 | Preamble += "#undef __OBJC_RW_DLLIMPORT\n"; |
| 5720 | Preamble += "#undef __OBJC_RW_STATICIMPORT\n"; |
| 5721 | Preamble += "#ifndef KEEP_ATTRIBUTES\n"; // We use this for clang tests. |
| 5722 | Preamble += "#define __attribute__(X)\n"; |
| 5723 | Preamble += "#endif\n"; |
Fariborz Jahanian | 5ce2827 | 2012-04-12 16:33:31 +0000 | [diff] [blame] | 5724 | Preamble += "#ifndef __weak\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5725 | Preamble += "#define __weak\n"; |
Fariborz Jahanian | 5ce2827 | 2012-04-12 16:33:31 +0000 | [diff] [blame] | 5726 | Preamble += "#endif\n"; |
| 5727 | Preamble += "#ifndef __block\n"; |
| 5728 | Preamble += "#define __block\n"; |
| 5729 | Preamble += "#endif\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5730 | } |
| 5731 | else { |
| 5732 | Preamble += "#define __block\n"; |
| 5733 | Preamble += "#define __weak\n"; |
| 5734 | } |
Fariborz Jahanian | b0f245c | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 5735 | |
| 5736 | // Declarations required for modern objective-c array and dictionary literals. |
| 5737 | Preamble += "\n#include <stdarg.h>\n"; |
Fariborz Jahanian | e35abe1 | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 5738 | Preamble += "struct __NSContainer_literal {\n"; |
Fariborz Jahanian | b0f245c | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 5739 | Preamble += " void * *arr;\n"; |
Fariborz Jahanian | e35abe1 | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 5740 | Preamble += " __NSContainer_literal (unsigned int count, ...) {\n"; |
Fariborz Jahanian | b0f245c | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 5741 | Preamble += "\tva_list marker;\n"; |
| 5742 | Preamble += "\tva_start(marker, count);\n"; |
| 5743 | Preamble += "\tarr = new void *[count];\n"; |
| 5744 | Preamble += "\tfor (unsigned i = 0; i < count; i++)\n"; |
| 5745 | Preamble += "\t arr[i] = va_arg(marker, void *);\n"; |
| 5746 | Preamble += "\tva_end( marker );\n"; |
| 5747 | Preamble += " };\n"; |
Fariborz Jahanian | e35abe1 | 2012-04-06 22:29:36 +0000 | [diff] [blame] | 5748 | Preamble += " __NSContainer_literal() {\n"; |
Fariborz Jahanian | b0f245c | 2012-04-06 19:47:36 +0000 | [diff] [blame] | 5749 | Preamble += "\tdelete[] arr;\n"; |
| 5750 | Preamble += " }\n"; |
| 5751 | Preamble += "};\n"; |
| 5752 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5753 | // NOTE! Windows uses LLP64 for 64bit mode. So, cast pointer to long long |
| 5754 | // as this avoids warning in any 64bit/32bit compilation model. |
| 5755 | Preamble += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((long long) &((TYPE *)0)->MEMBER)\n"; |
| 5756 | } |
| 5757 | |
| 5758 | /// RewriteIvarOffsetComputation - This rutine synthesizes computation of |
| 5759 | /// ivar offset. |
| 5760 | void RewriteModernObjC::RewriteIvarOffsetComputation(ObjCIvarDecl *ivar, |
| 5761 | std::string &Result) { |
| 5762 | if (ivar->isBitField()) { |
| 5763 | // FIXME: The hack below doesn't work for bitfields. For now, we simply |
| 5764 | // place all bitfields at offset 0. |
| 5765 | Result += "0"; |
| 5766 | } else { |
| 5767 | Result += "__OFFSETOFIVAR__(struct "; |
| 5768 | Result += ivar->getContainingInterface()->getNameAsString(); |
| 5769 | if (LangOpts.MicrosoftExt) |
| 5770 | Result += "_IMPL"; |
| 5771 | Result += ", "; |
| 5772 | Result += ivar->getNameAsString(); |
| 5773 | Result += ")"; |
| 5774 | } |
| 5775 | } |
| 5776 | |
| 5777 | /// WriteModernMetadataDeclarations - Writes out metadata declarations for modern ABI. |
| 5778 | /// struct _prop_t { |
Fariborz Jahanian | da35eac | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 5779 | /// const char *name; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5780 | /// char *attributes; |
| 5781 | /// } |
| 5782 | |
| 5783 | /// struct _prop_list_t { |
| 5784 | /// uint32_t entsize; // sizeof(struct _prop_t) |
| 5785 | /// uint32_t count_of_properties; |
| 5786 | /// struct _prop_t prop_list[count_of_properties]; |
| 5787 | /// } |
| 5788 | |
| 5789 | /// struct _protocol_t; |
| 5790 | |
| 5791 | /// struct _protocol_list_t { |
| 5792 | /// long protocol_count; // Note, this is 32/64 bit |
| 5793 | /// struct _protocol_t * protocol_list[protocol_count]; |
| 5794 | /// } |
| 5795 | |
| 5796 | /// struct _objc_method { |
| 5797 | /// SEL _cmd; |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 5798 | /// const char *method_type; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5799 | /// char *_imp; |
| 5800 | /// } |
| 5801 | |
| 5802 | /// struct _method_list_t { |
| 5803 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 5804 | /// uint32_t method_count; |
| 5805 | /// struct _objc_method method_list[method_count]; |
| 5806 | /// } |
| 5807 | |
| 5808 | /// struct _protocol_t { |
| 5809 | /// id isa; // NULL |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 5810 | /// const char *protocol_name; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5811 | /// const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 5812 | /// const struct method_list_t *instance_methods; |
| 5813 | /// const struct method_list_t *class_methods; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5814 | /// const struct method_list_t *optionalInstanceMethods; |
| 5815 | /// const struct method_list_t *optionalClassMethods; |
| 5816 | /// const struct _prop_list_t * properties; |
| 5817 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 5818 | /// const uint32_t flags; // = 0 |
| 5819 | /// const char ** extendedMethodTypes; |
| 5820 | /// } |
| 5821 | |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 5822 | /// struct _ivar_t { |
| 5823 | /// unsigned long int *offset; // pointer to ivar offset location |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 5824 | /// const char *name; |
| 5825 | /// const char *type; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 5826 | /// uint32_t alignment; |
| 5827 | /// uint32_t size; |
| 5828 | /// } |
| 5829 | |
| 5830 | /// struct _ivar_list_t { |
| 5831 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 5832 | /// uint32 count; |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 5833 | /// struct _ivar_t list[count]; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 5834 | /// } |
| 5835 | |
| 5836 | /// struct _class_ro_t { |
Fariborz Jahanian | 249cd10 | 2012-03-24 16:53:16 +0000 | [diff] [blame] | 5837 | /// uint32_t flags; |
| 5838 | /// uint32_t instanceStart; |
| 5839 | /// uint32_t instanceSize; |
| 5840 | /// uint32_t reserved; // only when building for 64bit targets |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 5841 | /// const uint8_t *ivarLayout; |
| 5842 | /// const char *name; |
| 5843 | /// const struct _method_list_t *baseMethods; |
| 5844 | /// const struct _protocol_list_t *baseProtocols; |
| 5845 | /// const struct _ivar_list_t *ivars; |
| 5846 | /// const uint8_t *weakIvarLayout; |
| 5847 | /// const struct _prop_list_t *properties; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 5848 | /// } |
| 5849 | |
| 5850 | /// struct _class_t { |
| 5851 | /// struct _class_t *isa; |
Fariborz Jahanian | fd4ce2c | 2012-03-20 17:34:50 +0000 | [diff] [blame] | 5852 | /// struct _class_t *superclass; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 5853 | /// void *cache; |
| 5854 | /// IMP *vtable; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 5855 | /// struct _class_ro_t *ro; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 5856 | /// } |
| 5857 | |
| 5858 | /// struct _category_t { |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 5859 | /// const char *name; |
Fariborz Jahanian | 4b2fe6e | 2012-03-20 21:41:28 +0000 | [diff] [blame] | 5860 | /// struct _class_t *cls; |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 5861 | /// const struct _method_list_t *instance_methods; |
| 5862 | /// const struct _method_list_t *class_methods; |
| 5863 | /// const struct _protocol_list_t *protocols; |
| 5864 | /// const struct _prop_list_t *properties; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 5865 | /// } |
| 5866 | |
| 5867 | /// MessageRefTy - LLVM for: |
| 5868 | /// struct _message_ref_t { |
| 5869 | /// IMP messenger; |
| 5870 | /// SEL name; |
| 5871 | /// }; |
| 5872 | |
| 5873 | /// SuperMessageRefTy - LLVM for: |
| 5874 | /// struct _super_message_ref_t { |
| 5875 | /// SUPER_IMP messenger; |
| 5876 | /// SEL name; |
| 5877 | /// }; |
| 5878 | |
Fariborz Jahanian | de5d946 | 2012-03-14 18:09:23 +0000 | [diff] [blame] | 5879 | static void WriteModernMetadataDeclarations(ASTContext *Context, std::string &Result) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5880 | static bool meta_data_declared = false; |
| 5881 | if (meta_data_declared) |
| 5882 | return; |
| 5883 | |
| 5884 | Result += "\nstruct _prop_t {\n"; |
Fariborz Jahanian | da35eac | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 5885 | Result += "\tconst char *name;\n"; |
| 5886 | Result += "\tconst char *attributes;\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5887 | Result += "};\n"; |
| 5888 | |
| 5889 | Result += "\nstruct _protocol_t;\n"; |
| 5890 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5891 | Result += "\nstruct _objc_method {\n"; |
| 5892 | Result += "\tstruct objc_selector * _cmd;\n"; |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 5893 | Result += "\tconst char *method_type;\n"; |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 5894 | Result += "\tvoid *_imp;\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5895 | Result += "};\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5896 | |
| 5897 | Result += "\nstruct _protocol_t {\n"; |
| 5898 | Result += "\tvoid * isa; // NULL\n"; |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 5899 | Result += "\tconst char *protocol_name;\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5900 | Result += "\tconst struct _protocol_list_t * protocol_list; // super protocols\n"; |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 5901 | Result += "\tconst struct method_list_t *instance_methods;\n"; |
| 5902 | Result += "\tconst struct method_list_t *class_methods;\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5903 | Result += "\tconst struct method_list_t *optionalInstanceMethods;\n"; |
| 5904 | Result += "\tconst struct method_list_t *optionalClassMethods;\n"; |
| 5905 | Result += "\tconst struct _prop_list_t * properties;\n"; |
| 5906 | Result += "\tconst unsigned int size; // sizeof(struct _protocol_t)\n"; |
| 5907 | Result += "\tconst unsigned int flags; // = 0\n"; |
| 5908 | Result += "\tconst char ** extendedMethodTypes;\n"; |
| 5909 | Result += "};\n"; |
| 5910 | |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 5911 | Result += "\nstruct _ivar_t {\n"; |
| 5912 | Result += "\tunsigned long int *offset; // pointer to ivar offset location\n"; |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 5913 | Result += "\tconst char *name;\n"; |
| 5914 | Result += "\tconst char *type;\n"; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 5915 | Result += "\tunsigned int alignment;\n"; |
| 5916 | Result += "\tunsigned int size;\n"; |
| 5917 | Result += "};\n"; |
| 5918 | |
| 5919 | Result += "\nstruct _class_ro_t {\n"; |
Fariborz Jahanian | 249cd10 | 2012-03-24 16:53:16 +0000 | [diff] [blame] | 5920 | Result += "\tunsigned int flags;\n"; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 5921 | Result += "\tunsigned int instanceStart;\n"; |
Fariborz Jahanian | 249cd10 | 2012-03-24 16:53:16 +0000 | [diff] [blame] | 5922 | Result += "\tunsigned int instanceSize;\n"; |
Fariborz Jahanian | de5d946 | 2012-03-14 18:09:23 +0000 | [diff] [blame] | 5923 | const llvm::Triple &Triple(Context->getTargetInfo().getTriple()); |
| 5924 | if (Triple.getArch() == llvm::Triple::x86_64) |
Fariborz Jahanian | 249cd10 | 2012-03-24 16:53:16 +0000 | [diff] [blame] | 5925 | Result += "\tunsigned int reserved;\n"; |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 5926 | Result += "\tconst unsigned char *ivarLayout;\n"; |
| 5927 | Result += "\tconst char *name;\n"; |
| 5928 | Result += "\tconst struct _method_list_t *baseMethods;\n"; |
| 5929 | Result += "\tconst struct _objc_protocol_list *baseProtocols;\n"; |
| 5930 | Result += "\tconst struct _ivar_list_t *ivars;\n"; |
| 5931 | Result += "\tconst unsigned char *weakIvarLayout;\n"; |
| 5932 | Result += "\tconst struct _prop_list_t *properties;\n"; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 5933 | Result += "};\n"; |
| 5934 | |
| 5935 | Result += "\nstruct _class_t {\n"; |
| 5936 | Result += "\tstruct _class_t *isa;\n"; |
Fariborz Jahanian | fd4ce2c | 2012-03-20 17:34:50 +0000 | [diff] [blame] | 5937 | Result += "\tstruct _class_t *superclass;\n"; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 5938 | Result += "\tvoid *cache;\n"; |
| 5939 | Result += "\tvoid *vtable;\n"; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 5940 | Result += "\tstruct _class_ro_t *ro;\n"; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 5941 | Result += "};\n"; |
| 5942 | |
| 5943 | Result += "\nstruct _category_t {\n"; |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 5944 | Result += "\tconst char *name;\n"; |
Fariborz Jahanian | 4b2fe6e | 2012-03-20 21:41:28 +0000 | [diff] [blame] | 5945 | Result += "\tstruct _class_t *cls;\n"; |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 5946 | Result += "\tconst struct _method_list_t *instance_methods;\n"; |
| 5947 | Result += "\tconst struct _method_list_t *class_methods;\n"; |
| 5948 | Result += "\tconst struct _protocol_list_t *protocols;\n"; |
| 5949 | Result += "\tconst struct _prop_list_t *properties;\n"; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 5950 | Result += "};\n"; |
| 5951 | |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 5952 | Result += "extern \"C\" __declspec(dllimport) struct objc_cache _objc_empty_cache;\n"; |
Fariborz Jahanian | 297976d | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 5953 | Result += "#pragma warning(disable:4273)\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5954 | meta_data_declared = true; |
| 5955 | } |
| 5956 | |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 5957 | static void Write_protocol_list_t_TypeDecl(std::string &Result, |
| 5958 | long super_protocol_count) { |
| 5959 | Result += "struct /*_protocol_list_t*/"; Result += " {\n"; |
| 5960 | Result += "\tlong protocol_count; // Note, this is 32/64 bit\n"; |
| 5961 | Result += "\tstruct _protocol_t *super_protocols["; |
| 5962 | Result += utostr(super_protocol_count); Result += "];\n"; |
| 5963 | Result += "}"; |
| 5964 | } |
| 5965 | |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 5966 | static void Write_method_list_t_TypeDecl(std::string &Result, |
| 5967 | unsigned int method_count) { |
| 5968 | Result += "struct /*_method_list_t*/"; Result += " {\n"; |
| 5969 | Result += "\tunsigned int entsize; // sizeof(struct _objc_method)\n"; |
| 5970 | Result += "\tunsigned int method_count;\n"; |
| 5971 | Result += "\tstruct _objc_method method_list["; |
| 5972 | Result += utostr(method_count); Result += "];\n"; |
| 5973 | Result += "}"; |
| 5974 | } |
| 5975 | |
Fariborz Jahanian | da35eac | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 5976 | static void Write__prop_list_t_TypeDecl(std::string &Result, |
| 5977 | unsigned int property_count) { |
| 5978 | Result += "struct /*_prop_list_t*/"; Result += " {\n"; |
| 5979 | Result += "\tunsigned int entsize; // sizeof(struct _prop_t)\n"; |
| 5980 | Result += "\tunsigned int count_of_properties;\n"; |
| 5981 | Result += "\tstruct _prop_t prop_list["; |
| 5982 | Result += utostr(property_count); Result += "];\n"; |
| 5983 | Result += "}"; |
| 5984 | } |
| 5985 | |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 5986 | static void Write__ivar_list_t_TypeDecl(std::string &Result, |
| 5987 | unsigned int ivar_count) { |
| 5988 | Result += "struct /*_ivar_list_t*/"; Result += " {\n"; |
| 5989 | Result += "\tunsigned int entsize; // sizeof(struct _prop_t)\n"; |
| 5990 | Result += "\tunsigned int count;\n"; |
| 5991 | Result += "\tstruct _ivar_t ivar_list["; |
| 5992 | Result += utostr(ivar_count); Result += "];\n"; |
| 5993 | Result += "}"; |
| 5994 | } |
| 5995 | |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 5996 | static void Write_protocol_list_initializer(ASTContext *Context, std::string &Result, |
| 5997 | ArrayRef<ObjCProtocolDecl *> SuperProtocols, |
| 5998 | StringRef VarName, |
| 5999 | StringRef ProtocolName) { |
| 6000 | if (SuperProtocols.size() > 0) { |
| 6001 | Result += "\nstatic "; |
| 6002 | Write_protocol_list_t_TypeDecl(Result, SuperProtocols.size()); |
| 6003 | Result += " "; Result += VarName; |
| 6004 | Result += ProtocolName; |
| 6005 | Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n"; |
| 6006 | Result += "\t"; Result += utostr(SuperProtocols.size()); Result += ",\n"; |
| 6007 | for (unsigned i = 0, e = SuperProtocols.size(); i < e; i++) { |
| 6008 | ObjCProtocolDecl *SuperPD = SuperProtocols[i]; |
| 6009 | Result += "\t&"; Result += "_OBJC_PROTOCOL_"; |
| 6010 | Result += SuperPD->getNameAsString(); |
| 6011 | if (i == e-1) |
| 6012 | Result += "\n};\n"; |
| 6013 | else |
| 6014 | Result += ",\n"; |
| 6015 | } |
| 6016 | } |
| 6017 | } |
| 6018 | |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6019 | static void Write_method_list_t_initializer(RewriteModernObjC &RewriteObj, |
| 6020 | ASTContext *Context, std::string &Result, |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6021 | ArrayRef<ObjCMethodDecl *> Methods, |
| 6022 | StringRef VarName, |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6023 | StringRef TopLevelDeclName, |
| 6024 | bool MethodImpl) { |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6025 | if (Methods.size() > 0) { |
| 6026 | Result += "\nstatic "; |
| 6027 | Write_method_list_t_TypeDecl(Result, Methods.size()); |
| 6028 | Result += " "; Result += VarName; |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6029 | Result += TopLevelDeclName; |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6030 | Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n"; |
| 6031 | Result += "\t"; Result += "sizeof(_objc_method)"; Result += ",\n"; |
| 6032 | Result += "\t"; Result += utostr(Methods.size()); Result += ",\n"; |
| 6033 | for (unsigned i = 0, e = Methods.size(); i < e; i++) { |
| 6034 | ObjCMethodDecl *MD = Methods[i]; |
| 6035 | if (i == 0) |
| 6036 | Result += "\t{{(struct objc_selector *)\""; |
| 6037 | else |
| 6038 | Result += "\t{(struct objc_selector *)\""; |
| 6039 | Result += (MD)->getSelector().getAsString(); Result += "\""; |
| 6040 | Result += ", "; |
| 6041 | std::string MethodTypeString; |
| 6042 | Context->getObjCEncodingForMethodDecl(MD, MethodTypeString); |
| 6043 | Result += "\""; Result += MethodTypeString; Result += "\""; |
| 6044 | Result += ", "; |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6045 | if (!MethodImpl) |
| 6046 | Result += "0"; |
| 6047 | else { |
| 6048 | Result += "(void *)"; |
| 6049 | Result += RewriteObj.MethodInternalNames[MD]; |
| 6050 | } |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6051 | if (i == e-1) |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6052 | Result += "}}\n"; |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6053 | else |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6054 | Result += "},\n"; |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6055 | } |
| 6056 | Result += "};\n"; |
| 6057 | } |
| 6058 | } |
| 6059 | |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6060 | static void Write_prop_list_t_initializer(RewriteModernObjC &RewriteObj, |
Fariborz Jahanian | da35eac | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 6061 | ASTContext *Context, std::string &Result, |
| 6062 | ArrayRef<ObjCPropertyDecl *> Properties, |
| 6063 | const Decl *Container, |
| 6064 | StringRef VarName, |
| 6065 | StringRef ProtocolName) { |
| 6066 | if (Properties.size() > 0) { |
| 6067 | Result += "\nstatic "; |
| 6068 | Write__prop_list_t_TypeDecl(Result, Properties.size()); |
| 6069 | Result += " "; Result += VarName; |
| 6070 | Result += ProtocolName; |
| 6071 | Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n"; |
| 6072 | Result += "\t"; Result += "sizeof(_prop_t)"; Result += ",\n"; |
| 6073 | Result += "\t"; Result += utostr(Properties.size()); Result += ",\n"; |
| 6074 | for (unsigned i = 0, e = Properties.size(); i < e; i++) { |
| 6075 | ObjCPropertyDecl *PropDecl = Properties[i]; |
| 6076 | if (i == 0) |
| 6077 | Result += "\t{{\""; |
| 6078 | else |
| 6079 | Result += "\t{\""; |
| 6080 | Result += PropDecl->getName(); Result += "\","; |
| 6081 | std::string PropertyTypeString, QuotePropertyTypeString; |
| 6082 | Context->getObjCEncodingForPropertyDecl(PropDecl, Container, PropertyTypeString); |
| 6083 | RewriteObj.QuoteDoublequotes(PropertyTypeString, QuotePropertyTypeString); |
| 6084 | Result += "\""; Result += QuotePropertyTypeString; Result += "\""; |
| 6085 | if (i == e-1) |
| 6086 | Result += "}}\n"; |
| 6087 | else |
| 6088 | Result += "},\n"; |
| 6089 | } |
| 6090 | Result += "};\n"; |
| 6091 | } |
| 6092 | } |
| 6093 | |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 6094 | // Metadata flags |
| 6095 | enum MetaDataDlags { |
| 6096 | CLS = 0x0, |
| 6097 | CLS_META = 0x1, |
| 6098 | CLS_ROOT = 0x2, |
| 6099 | OBJC2_CLS_HIDDEN = 0x10, |
| 6100 | CLS_EXCEPTION = 0x20, |
| 6101 | |
| 6102 | /// (Obsolete) ARC-specific: this class has a .release_ivars method |
| 6103 | CLS_HAS_IVAR_RELEASER = 0x40, |
| 6104 | /// class was compiled with -fobjc-arr |
| 6105 | CLS_COMPILED_BY_ARC = 0x80 // (1<<7) |
| 6106 | }; |
| 6107 | |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6108 | static void Write__class_ro_t_initializer(ASTContext *Context, std::string &Result, |
| 6109 | unsigned int flags, |
| 6110 | const std::string &InstanceStart, |
| 6111 | const std::string &InstanceSize, |
| 6112 | ArrayRef<ObjCMethodDecl *>baseMethods, |
| 6113 | ArrayRef<ObjCProtocolDecl *>baseProtocols, |
| 6114 | ArrayRef<ObjCIvarDecl *>ivars, |
| 6115 | ArrayRef<ObjCPropertyDecl *>Properties, |
| 6116 | StringRef VarName, |
| 6117 | StringRef ClassName) { |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6118 | Result += "\nstatic struct _class_ro_t "; |
| 6119 | Result += VarName; Result += ClassName; |
| 6120 | Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n"; |
| 6121 | Result += "\t"; |
| 6122 | Result += llvm::utostr(flags); Result += ", "; |
| 6123 | Result += InstanceStart; Result += ", "; |
| 6124 | Result += InstanceSize; Result += ", \n"; |
| 6125 | Result += "\t"; |
Fariborz Jahanian | de5d946 | 2012-03-14 18:09:23 +0000 | [diff] [blame] | 6126 | const llvm::Triple &Triple(Context->getTargetInfo().getTriple()); |
| 6127 | if (Triple.getArch() == llvm::Triple::x86_64) |
| 6128 | // uint32_t const reserved; // only when building for 64bit targets |
| 6129 | Result += "(unsigned int)0, \n\t"; |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6130 | // const uint8_t * const ivarLayout; |
| 6131 | Result += "0, \n\t"; |
| 6132 | Result += "\""; Result += ClassName; Result += "\",\n\t"; |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 6133 | bool metaclass = ((flags & CLS_META) != 0); |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6134 | if (baseMethods.size() > 0) { |
| 6135 | Result += "(const struct _method_list_t *)&"; |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 6136 | if (metaclass) |
| 6137 | Result += "_OBJC_$_CLASS_METHODS_"; |
| 6138 | else |
| 6139 | Result += "_OBJC_$_INSTANCE_METHODS_"; |
| 6140 | Result += ClassName; |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6141 | Result += ",\n\t"; |
| 6142 | } |
| 6143 | else |
| 6144 | Result += "0, \n\t"; |
| 6145 | |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 6146 | if (!metaclass && baseProtocols.size() > 0) { |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6147 | Result += "(const struct _objc_protocol_list *)&"; |
| 6148 | Result += "_OBJC_CLASS_PROTOCOLS_$_"; Result += ClassName; |
| 6149 | Result += ",\n\t"; |
| 6150 | } |
| 6151 | else |
| 6152 | Result += "0, \n\t"; |
| 6153 | |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 6154 | if (!metaclass && ivars.size() > 0) { |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6155 | Result += "(const struct _ivar_list_t *)&"; |
| 6156 | Result += "_OBJC_$_INSTANCE_VARIABLES_"; Result += ClassName; |
| 6157 | Result += ",\n\t"; |
| 6158 | } |
| 6159 | else |
| 6160 | Result += "0, \n\t"; |
| 6161 | |
| 6162 | // weakIvarLayout |
| 6163 | Result += "0, \n\t"; |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 6164 | if (!metaclass && Properties.size() > 0) { |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6165 | Result += "(const struct _prop_list_t *)&"; |
Fariborz Jahanian | eeabf38 | 2012-02-16 21:57:59 +0000 | [diff] [blame] | 6166 | Result += "_OBJC_$_PROP_LIST_"; Result += ClassName; |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6167 | Result += ",\n"; |
| 6168 | } |
| 6169 | else |
| 6170 | Result += "0, \n"; |
| 6171 | |
| 6172 | Result += "};\n"; |
| 6173 | } |
| 6174 | |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6175 | static void Write_class_t(ASTContext *Context, std::string &Result, |
| 6176 | StringRef VarName, |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6177 | const ObjCInterfaceDecl *CDecl, bool metaclass) { |
| 6178 | bool rootClass = (!CDecl->getSuperClass()); |
| 6179 | const ObjCInterfaceDecl *RootClass = CDecl; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6180 | |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6181 | if (!rootClass) { |
| 6182 | // Find the Root class |
| 6183 | RootClass = CDecl->getSuperClass(); |
| 6184 | while (RootClass->getSuperClass()) { |
| 6185 | RootClass = RootClass->getSuperClass(); |
| 6186 | } |
| 6187 | } |
| 6188 | |
| 6189 | if (metaclass && rootClass) { |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6190 | // Need to handle a case of use of forward declaration. |
Fariborz Jahanian | ce0d897 | 2012-03-10 18:25:06 +0000 | [diff] [blame] | 6191 | Result += "\n"; |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 6192 | Result += "extern \"C\" "; |
Fariborz Jahanian | ce0d897 | 2012-03-10 18:25:06 +0000 | [diff] [blame] | 6193 | if (CDecl->getImplementation()) |
| 6194 | Result += "__declspec(dllexport) "; |
Fariborz Jahanian | 297976d | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 6195 | else |
| 6196 | Result += "__declspec(dllimport) "; |
| 6197 | |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 6198 | Result += "struct _class_t OBJC_CLASS_$_"; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6199 | Result += CDecl->getNameAsString(); |
| 6200 | Result += ";\n"; |
| 6201 | } |
| 6202 | // Also, for possibility of 'super' metadata class not having been defined yet. |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6203 | if (!rootClass) { |
Fariborz Jahanian | 868e985 | 2012-03-29 19:04:10 +0000 | [diff] [blame] | 6204 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
Fariborz Jahanian | ce0d897 | 2012-03-10 18:25:06 +0000 | [diff] [blame] | 6205 | Result += "\n"; |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 6206 | Result += "extern \"C\" "; |
Fariborz Jahanian | 868e985 | 2012-03-29 19:04:10 +0000 | [diff] [blame] | 6207 | if (SuperClass->getImplementation()) |
Fariborz Jahanian | ce0d897 | 2012-03-10 18:25:06 +0000 | [diff] [blame] | 6208 | Result += "__declspec(dllexport) "; |
Fariborz Jahanian | 297976d | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 6209 | else |
| 6210 | Result += "__declspec(dllimport) "; |
| 6211 | |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 6212 | Result += "struct _class_t "; |
Fariborz Jahanian | ce0d897 | 2012-03-10 18:25:06 +0000 | [diff] [blame] | 6213 | Result += VarName; |
Fariborz Jahanian | 868e985 | 2012-03-29 19:04:10 +0000 | [diff] [blame] | 6214 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6215 | Result += ";\n"; |
Fariborz Jahanian | 452eac1 | 2012-03-20 21:09:58 +0000 | [diff] [blame] | 6216 | |
Fariborz Jahanian | 868e985 | 2012-03-29 19:04:10 +0000 | [diff] [blame] | 6217 | if (metaclass && RootClass != SuperClass) { |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 6218 | Result += "extern \"C\" "; |
Fariborz Jahanian | 452eac1 | 2012-03-20 21:09:58 +0000 | [diff] [blame] | 6219 | if (RootClass->getImplementation()) |
| 6220 | Result += "__declspec(dllexport) "; |
Fariborz Jahanian | 297976d | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 6221 | else |
| 6222 | Result += "__declspec(dllimport) "; |
| 6223 | |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 6224 | Result += "struct _class_t "; |
Fariborz Jahanian | 452eac1 | 2012-03-20 21:09:58 +0000 | [diff] [blame] | 6225 | Result += VarName; |
| 6226 | Result += RootClass->getNameAsString(); |
| 6227 | Result += ";\n"; |
| 6228 | } |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6229 | } |
| 6230 | |
Fariborz Jahanian | 297976d | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 6231 | Result += "\nextern \"C\" __declspec(dllexport) struct _class_t "; |
| 6232 | Result += VarName; Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6233 | Result += " __attribute__ ((used, section (\"__DATA,__objc_data\"))) = {\n"; |
| 6234 | Result += "\t"; |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6235 | if (metaclass) { |
| 6236 | if (!rootClass) { |
| 6237 | Result += "0, // &"; Result += VarName; |
| 6238 | Result += RootClass->getNameAsString(); |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6239 | Result += ",\n\t"; |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6240 | Result += "0, // &"; Result += VarName; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6241 | Result += CDecl->getSuperClass()->getNameAsString(); |
| 6242 | Result += ",\n\t"; |
| 6243 | } |
| 6244 | else { |
Fariborz Jahanian | 452eac1 | 2012-03-20 21:09:58 +0000 | [diff] [blame] | 6245 | Result += "0, // &"; Result += VarName; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6246 | Result += CDecl->getNameAsString(); |
| 6247 | Result += ",\n\t"; |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6248 | Result += "0, // &OBJC_CLASS_$_"; Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6249 | Result += ",\n\t"; |
| 6250 | } |
| 6251 | } |
| 6252 | else { |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6253 | Result += "0, // &OBJC_METACLASS_$_"; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6254 | Result += CDecl->getNameAsString(); |
| 6255 | Result += ",\n\t"; |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6256 | if (!rootClass) { |
| 6257 | Result += "0, // &"; Result += VarName; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6258 | Result += CDecl->getSuperClass()->getNameAsString(); |
| 6259 | Result += ",\n\t"; |
| 6260 | } |
| 6261 | else |
| 6262 | Result += "0,\n\t"; |
| 6263 | } |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6264 | Result += "0, // (void *)&_objc_empty_cache,\n\t"; |
| 6265 | Result += "0, // unused, was (void *)&_objc_empty_vtable,\n\t"; |
| 6266 | if (metaclass) |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6267 | Result += "&_OBJC_METACLASS_RO_$_"; |
| 6268 | else |
| 6269 | Result += "&_OBJC_CLASS_RO_$_"; |
| 6270 | Result += CDecl->getNameAsString(); |
| 6271 | Result += ",\n};\n"; |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6272 | |
| 6273 | // Add static function to initialize some of the meta-data fields. |
| 6274 | // avoid doing it twice. |
| 6275 | if (metaclass) |
| 6276 | return; |
| 6277 | |
| 6278 | const ObjCInterfaceDecl *SuperClass = |
| 6279 | rootClass ? CDecl : CDecl->getSuperClass(); |
| 6280 | |
| 6281 | Result += "static void OBJC_CLASS_SETUP_$_"; |
| 6282 | Result += CDecl->getNameAsString(); |
| 6283 | Result += "(void ) {\n"; |
| 6284 | Result += "\tOBJC_METACLASS_$_"; Result += CDecl->getNameAsString(); |
| 6285 | Result += ".isa = "; Result += "&OBJC_METACLASS_$_"; |
Fariborz Jahanian | 452eac1 | 2012-03-20 21:09:58 +0000 | [diff] [blame] | 6286 | Result += RootClass->getNameAsString(); Result += ";\n"; |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6287 | |
| 6288 | Result += "\tOBJC_METACLASS_$_"; Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 452eac1 | 2012-03-20 21:09:58 +0000 | [diff] [blame] | 6289 | Result += ".superclass = "; |
| 6290 | if (rootClass) |
| 6291 | Result += "&OBJC_CLASS_$_"; |
| 6292 | else |
| 6293 | Result += "&OBJC_METACLASS_$_"; |
| 6294 | |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 6295 | Result += SuperClass->getNameAsString(); Result += ";\n"; |
| 6296 | |
| 6297 | Result += "\tOBJC_METACLASS_$_"; Result += CDecl->getNameAsString(); |
| 6298 | Result += ".cache = "; Result += "&_objc_empty_cache"; Result += ";\n"; |
| 6299 | |
| 6300 | Result += "\tOBJC_CLASS_$_"; Result += CDecl->getNameAsString(); |
| 6301 | Result += ".isa = "; Result += "&OBJC_METACLASS_$_"; |
| 6302 | Result += CDecl->getNameAsString(); Result += ";\n"; |
| 6303 | |
| 6304 | if (!rootClass) { |
| 6305 | Result += "\tOBJC_CLASS_$_"; Result += CDecl->getNameAsString(); |
| 6306 | Result += ".superclass = "; Result += "&OBJC_CLASS_$_"; |
| 6307 | Result += SuperClass->getNameAsString(); Result += ";\n"; |
| 6308 | } |
| 6309 | |
| 6310 | Result += "\tOBJC_CLASS_$_"; Result += CDecl->getNameAsString(); |
| 6311 | Result += ".cache = "; Result += "&_objc_empty_cache"; Result += ";\n"; |
| 6312 | Result += "}\n"; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6313 | } |
| 6314 | |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6315 | static void Write_category_t(RewriteModernObjC &RewriteObj, ASTContext *Context, |
| 6316 | std::string &Result, |
Fariborz Jahanian | e033578 | 2012-03-27 18:41:05 +0000 | [diff] [blame] | 6317 | ObjCCategoryDecl *CatDecl, |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 6318 | ObjCInterfaceDecl *ClassDecl, |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6319 | ArrayRef<ObjCMethodDecl *> InstanceMethods, |
| 6320 | ArrayRef<ObjCMethodDecl *> ClassMethods, |
| 6321 | ArrayRef<ObjCProtocolDecl *> RefedProtocols, |
| 6322 | ArrayRef<ObjCPropertyDecl *> ClassProperties) { |
Fariborz Jahanian | e033578 | 2012-03-27 18:41:05 +0000 | [diff] [blame] | 6323 | StringRef CatName = CatDecl->getName(); |
NAKAMURA Takumi | 20f8939 | 2012-03-21 03:21:46 +0000 | [diff] [blame] | 6324 | StringRef ClassName = ClassDecl->getName(); |
Fariborz Jahanian | 8c00a1b | 2012-02-17 20:33:00 +0000 | [diff] [blame] | 6325 | // must declare an extern class object in case this class is not implemented |
| 6326 | // in this TU. |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 6327 | Result += "\n"; |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 6328 | Result += "extern \"C\" "; |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 6329 | if (ClassDecl->getImplementation()) |
| 6330 | Result += "__declspec(dllexport) "; |
Fariborz Jahanian | 297976d | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 6331 | else |
| 6332 | Result += "__declspec(dllimport) "; |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 6333 | |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 6334 | Result += "struct _class_t "; |
Fariborz Jahanian | 8c00a1b | 2012-02-17 20:33:00 +0000 | [diff] [blame] | 6335 | Result += "OBJC_CLASS_$_"; Result += ClassName; |
| 6336 | Result += ";\n"; |
| 6337 | |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6338 | Result += "\nstatic struct _category_t "; |
| 6339 | Result += "_OBJC_$_CATEGORY_"; |
| 6340 | Result += ClassName; Result += "_$_"; Result += CatName; |
| 6341 | Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = \n"; |
| 6342 | Result += "{\n"; |
| 6343 | Result += "\t\""; Result += ClassName; Result += "\",\n"; |
Fariborz Jahanian | 4b2fe6e | 2012-03-20 21:41:28 +0000 | [diff] [blame] | 6344 | Result += "\t0, // &"; Result += "OBJC_CLASS_$_"; Result += ClassName; |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6345 | Result += ",\n"; |
| 6346 | if (InstanceMethods.size() > 0) { |
| 6347 | Result += "\t(const struct _method_list_t *)&"; |
| 6348 | Result += "_OBJC_$_CATEGORY_INSTANCE_METHODS_"; |
| 6349 | Result += ClassName; Result += "_$_"; Result += CatName; |
| 6350 | Result += ",\n"; |
| 6351 | } |
| 6352 | else |
| 6353 | Result += "\t0,\n"; |
| 6354 | |
| 6355 | if (ClassMethods.size() > 0) { |
| 6356 | Result += "\t(const struct _method_list_t *)&"; |
| 6357 | Result += "_OBJC_$_CATEGORY_CLASS_METHODS_"; |
| 6358 | Result += ClassName; Result += "_$_"; Result += CatName; |
| 6359 | Result += ",\n"; |
| 6360 | } |
| 6361 | else |
| 6362 | Result += "\t0,\n"; |
| 6363 | |
| 6364 | if (RefedProtocols.size() > 0) { |
| 6365 | Result += "\t(const struct _protocol_list_t *)&"; |
| 6366 | Result += "_OBJC_CATEGORY_PROTOCOLS_$_"; |
| 6367 | Result += ClassName; Result += "_$_"; Result += CatName; |
| 6368 | Result += ",\n"; |
| 6369 | } |
| 6370 | else |
| 6371 | Result += "\t0,\n"; |
| 6372 | |
| 6373 | if (ClassProperties.size() > 0) { |
| 6374 | Result += "\t(const struct _prop_list_t *)&"; Result += "_OBJC_$_PROP_LIST_"; |
| 6375 | Result += ClassName; Result += "_$_"; Result += CatName; |
| 6376 | Result += ",\n"; |
| 6377 | } |
| 6378 | else |
| 6379 | Result += "\t0,\n"; |
| 6380 | |
| 6381 | Result += "};\n"; |
Fariborz Jahanian | 4b2fe6e | 2012-03-20 21:41:28 +0000 | [diff] [blame] | 6382 | |
| 6383 | // Add static function to initialize the class pointer in the category structure. |
| 6384 | Result += "static void OBJC_CATEGORY_SETUP_$_"; |
| 6385 | Result += ClassDecl->getNameAsString(); |
| 6386 | Result += "_$_"; |
| 6387 | Result += CatName; |
| 6388 | Result += "(void ) {\n"; |
| 6389 | Result += "\t_OBJC_$_CATEGORY_"; |
| 6390 | Result += ClassDecl->getNameAsString(); |
| 6391 | Result += "_$_"; |
| 6392 | Result += CatName; |
| 6393 | Result += ".cls = "; Result += "&OBJC_CLASS_$_"; Result += ClassName; |
| 6394 | Result += ";\n}\n"; |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6395 | } |
| 6396 | |
Fariborz Jahanian | e0adbd8 | 2012-02-08 22:23:26 +0000 | [diff] [blame] | 6397 | static void Write__extendedMethodTypes_initializer(RewriteModernObjC &RewriteObj, |
| 6398 | ASTContext *Context, std::string &Result, |
| 6399 | ArrayRef<ObjCMethodDecl *> Methods, |
| 6400 | StringRef VarName, |
| 6401 | StringRef ProtocolName) { |
| 6402 | if (Methods.size() == 0) |
| 6403 | return; |
| 6404 | |
| 6405 | Result += "\nstatic const char *"; |
| 6406 | Result += VarName; Result += ProtocolName; |
| 6407 | Result += " [] __attribute__ ((used, section (\"__DATA,__objc_const\"))) = \n"; |
| 6408 | Result += "{\n"; |
| 6409 | for (unsigned i = 0, e = Methods.size(); i < e; i++) { |
| 6410 | ObjCMethodDecl *MD = Methods[i]; |
| 6411 | std::string MethodTypeString, QuoteMethodTypeString; |
| 6412 | Context->getObjCEncodingForMethodDecl(MD, MethodTypeString, true); |
| 6413 | RewriteObj.QuoteDoublequotes(MethodTypeString, QuoteMethodTypeString); |
| 6414 | Result += "\t\""; Result += QuoteMethodTypeString; Result += "\""; |
| 6415 | if (i == e-1) |
| 6416 | Result += "\n};\n"; |
| 6417 | else { |
| 6418 | Result += ",\n"; |
| 6419 | } |
| 6420 | } |
| 6421 | } |
| 6422 | |
Fariborz Jahanian | acee1c9 | 2012-04-11 21:12:36 +0000 | [diff] [blame] | 6423 | static void Write_IvarOffsetVar(RewriteModernObjC &RewriteObj, |
| 6424 | ASTContext *Context, |
Fariborz Jahanian | 40a777a | 2012-03-12 16:46:58 +0000 | [diff] [blame] | 6425 | std::string &Result, |
Fariborz Jahanian | db64923 | 2012-02-13 20:59:02 +0000 | [diff] [blame] | 6426 | ArrayRef<ObjCIvarDecl *> Ivars, |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 6427 | ObjCInterfaceDecl *CDecl) { |
Fariborz Jahanian | db64923 | 2012-02-13 20:59:02 +0000 | [diff] [blame] | 6428 | // FIXME. visibilty of offset symbols may have to be set; for Darwin |
| 6429 | // this is what happens: |
| 6430 | /** |
| 6431 | if (Ivar->getAccessControl() == ObjCIvarDecl::Private || |
| 6432 | Ivar->getAccessControl() == ObjCIvarDecl::Package || |
| 6433 | Class->getVisibility() == HiddenVisibility) |
| 6434 | Visibility shoud be: HiddenVisibility; |
| 6435 | else |
| 6436 | Visibility shoud be: DefaultVisibility; |
| 6437 | */ |
| 6438 | |
Fariborz Jahanian | 07e5288 | 2012-02-13 21:34:45 +0000 | [diff] [blame] | 6439 | Result += "\n"; |
| 6440 | for (unsigned i =0, e = Ivars.size(); i < e; i++) { |
| 6441 | ObjCIvarDecl *IvarDecl = Ivars[i]; |
Fariborz Jahanian | 40a777a | 2012-03-12 16:46:58 +0000 | [diff] [blame] | 6442 | if (Context->getLangOpts().MicrosoftExt) |
| 6443 | Result += "__declspec(allocate(\".objc_ivar$B\")) "; |
| 6444 | |
| 6445 | if (!Context->getLangOpts().MicrosoftExt || |
| 6446 | IvarDecl->getAccessControl() == ObjCIvarDecl::Private || |
Fariborz Jahanian | 117591f | 2012-03-10 01:34:42 +0000 | [diff] [blame] | 6447 | IvarDecl->getAccessControl() == ObjCIvarDecl::Package) |
Fariborz Jahanian | 297976d | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 6448 | Result += "extern \"C\" unsigned long int "; |
Fariborz Jahanian | d1c84d3 | 2012-03-10 00:53:02 +0000 | [diff] [blame] | 6449 | else |
Fariborz Jahanian | 297976d | 2012-03-29 17:51:09 +0000 | [diff] [blame] | 6450 | Result += "extern \"C\" __declspec(dllexport) unsigned long int "; |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 6451 | WriteInternalIvarName(CDecl, IvarDecl, Result); |
Fariborz Jahanian | 07e5288 | 2012-02-13 21:34:45 +0000 | [diff] [blame] | 6452 | Result += " __attribute__ ((used, section (\"__DATA,__objc_ivar\")))"; |
| 6453 | Result += " = "; |
Fariborz Jahanian | acee1c9 | 2012-04-11 21:12:36 +0000 | [diff] [blame] | 6454 | RewriteObj.RewriteIvarOffsetComputation(IvarDecl, Result); |
| 6455 | Result += ";\n"; |
Fariborz Jahanian | db64923 | 2012-02-13 20:59:02 +0000 | [diff] [blame] | 6456 | } |
| 6457 | } |
| 6458 | |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6459 | static void Write__ivar_list_t_initializer(RewriteModernObjC &RewriteObj, |
| 6460 | ASTContext *Context, std::string &Result, |
| 6461 | ArrayRef<ObjCIvarDecl *> Ivars, |
| 6462 | StringRef VarName, |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 6463 | ObjCInterfaceDecl *CDecl) { |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6464 | if (Ivars.size() > 0) { |
Fariborz Jahanian | acee1c9 | 2012-04-11 21:12:36 +0000 | [diff] [blame] | 6465 | Write_IvarOffsetVar(RewriteObj, Context, Result, Ivars, CDecl); |
Fariborz Jahanian | 07e5288 | 2012-02-13 21:34:45 +0000 | [diff] [blame] | 6466 | |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6467 | Result += "\nstatic "; |
| 6468 | Write__ivar_list_t_TypeDecl(Result, Ivars.size()); |
| 6469 | Result += " "; Result += VarName; |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 6470 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6471 | Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n"; |
| 6472 | Result += "\t"; Result += "sizeof(_ivar_t)"; Result += ",\n"; |
| 6473 | Result += "\t"; Result += utostr(Ivars.size()); Result += ",\n"; |
| 6474 | for (unsigned i =0, e = Ivars.size(); i < e; i++) { |
| 6475 | ObjCIvarDecl *IvarDecl = Ivars[i]; |
| 6476 | if (i == 0) |
| 6477 | Result += "\t{{"; |
| 6478 | else |
| 6479 | Result += "\t {"; |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 6480 | Result += "(unsigned long int *)&"; |
| 6481 | WriteInternalIvarName(CDecl, IvarDecl, Result); |
Fariborz Jahanian | db64923 | 2012-02-13 20:59:02 +0000 | [diff] [blame] | 6482 | Result += ", "; |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6483 | |
| 6484 | Result += "\""; Result += IvarDecl->getName(); Result += "\", "; |
| 6485 | std::string IvarTypeString, QuoteIvarTypeString; |
| 6486 | Context->getObjCEncodingForType(IvarDecl->getType(), IvarTypeString, |
| 6487 | IvarDecl); |
| 6488 | RewriteObj.QuoteDoublequotes(IvarTypeString, QuoteIvarTypeString); |
| 6489 | Result += "\""; Result += QuoteIvarTypeString; Result += "\", "; |
| 6490 | |
Fariborz Jahanian | 8f1fed0 | 2012-02-11 20:10:52 +0000 | [diff] [blame] | 6491 | // FIXME. this alignment represents the host alignment and need be changed to |
| 6492 | // represent the target alignment. |
| 6493 | unsigned Align = Context->getTypeAlign(IvarDecl->getType())/8; |
| 6494 | Align = llvm::Log2_32(Align); |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6495 | Result += llvm::utostr(Align); Result += ", "; |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 6496 | CharUnits Size = Context->getTypeSizeInChars(IvarDecl->getType()); |
| 6497 | Result += llvm::utostr(Size.getQuantity()); |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6498 | if (i == e-1) |
| 6499 | Result += "}}\n"; |
| 6500 | else |
| 6501 | Result += "},\n"; |
| 6502 | } |
| 6503 | Result += "};\n"; |
| 6504 | } |
| 6505 | } |
| 6506 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6507 | /// RewriteObjCProtocolMetaData - Rewrite protocols meta-data. |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 6508 | void RewriteModernObjC::RewriteObjCProtocolMetaData(ObjCProtocolDecl *PDecl, |
| 6509 | std::string &Result) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6510 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6511 | // Do not synthesize the protocol more than once. |
| 6512 | if (ObjCSynthesizedProtocols.count(PDecl->getCanonicalDecl())) |
| 6513 | return; |
Fariborz Jahanian | de5d946 | 2012-03-14 18:09:23 +0000 | [diff] [blame] | 6514 | WriteModernMetadataDeclarations(Context, Result); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6515 | |
| 6516 | if (ObjCProtocolDecl *Def = PDecl->getDefinition()) |
| 6517 | PDecl = Def; |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 6518 | // Must write out all protocol definitions in current qualifier list, |
| 6519 | // and in their nested qualifiers before writing out current definition. |
| 6520 | for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(), |
| 6521 | E = PDecl->protocol_end(); I != E; ++I) |
| 6522 | RewriteObjCProtocolMetaData(*I, Result); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6523 | |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6524 | // Construct method lists. |
| 6525 | std::vector<ObjCMethodDecl *> InstanceMethods, ClassMethods; |
| 6526 | std::vector<ObjCMethodDecl *> OptInstanceMethods, OptClassMethods; |
| 6527 | for (ObjCProtocolDecl::instmeth_iterator |
| 6528 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
| 6529 | I != E; ++I) { |
| 6530 | ObjCMethodDecl *MD = *I; |
| 6531 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 6532 | OptInstanceMethods.push_back(MD); |
| 6533 | } else { |
| 6534 | InstanceMethods.push_back(MD); |
| 6535 | } |
| 6536 | } |
| 6537 | |
| 6538 | for (ObjCProtocolDecl::classmeth_iterator |
| 6539 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
| 6540 | I != E; ++I) { |
| 6541 | ObjCMethodDecl *MD = *I; |
| 6542 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 6543 | OptClassMethods.push_back(MD); |
| 6544 | } else { |
| 6545 | ClassMethods.push_back(MD); |
| 6546 | } |
| 6547 | } |
Fariborz Jahanian | e0adbd8 | 2012-02-08 22:23:26 +0000 | [diff] [blame] | 6548 | std::vector<ObjCMethodDecl *> AllMethods; |
| 6549 | for (unsigned i = 0, e = InstanceMethods.size(); i < e; i++) |
| 6550 | AllMethods.push_back(InstanceMethods[i]); |
| 6551 | for (unsigned i = 0, e = ClassMethods.size(); i < e; i++) |
| 6552 | AllMethods.push_back(ClassMethods[i]); |
| 6553 | for (unsigned i = 0, e = OptInstanceMethods.size(); i < e; i++) |
| 6554 | AllMethods.push_back(OptInstanceMethods[i]); |
| 6555 | for (unsigned i = 0, e = OptClassMethods.size(); i < e; i++) |
| 6556 | AllMethods.push_back(OptClassMethods[i]); |
| 6557 | |
| 6558 | Write__extendedMethodTypes_initializer(*this, Context, Result, |
| 6559 | AllMethods, |
| 6560 | "_OBJC_PROTOCOL_METHOD_TYPES_", |
| 6561 | PDecl->getNameAsString()); |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 6562 | // Protocol's super protocol list |
| 6563 | std::vector<ObjCProtocolDecl *> SuperProtocols; |
| 6564 | for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(), |
| 6565 | E = PDecl->protocol_end(); I != E; ++I) |
| 6566 | SuperProtocols.push_back(*I); |
| 6567 | |
| 6568 | Write_protocol_list_initializer(Context, Result, SuperProtocols, |
| 6569 | "_OBJC_PROTOCOL_REFS_", |
| 6570 | PDecl->getNameAsString()); |
| 6571 | |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6572 | Write_method_list_t_initializer(*this, Context, Result, InstanceMethods, |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6573 | "_OBJC_PROTOCOL_INSTANCE_METHODS_", |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6574 | PDecl->getNameAsString(), false); |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6575 | |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6576 | Write_method_list_t_initializer(*this, Context, Result, ClassMethods, |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6577 | "_OBJC_PROTOCOL_CLASS_METHODS_", |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6578 | PDecl->getNameAsString(), false); |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6579 | |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6580 | Write_method_list_t_initializer(*this, Context, Result, OptInstanceMethods, |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6581 | "_OBJC_PROTOCOL_OPT_INSTANCE_METHODS_", |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6582 | PDecl->getNameAsString(), false); |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6583 | |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6584 | Write_method_list_t_initializer(*this, Context, Result, OptClassMethods, |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6585 | "_OBJC_PROTOCOL_OPT_CLASS_METHODS_", |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6586 | PDecl->getNameAsString(), false); |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6587 | |
Fariborz Jahanian | da35eac | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 6588 | // Protocol's property metadata. |
| 6589 | std::vector<ObjCPropertyDecl *> ProtocolProperties; |
| 6590 | for (ObjCContainerDecl::prop_iterator I = PDecl->prop_begin(), |
| 6591 | E = PDecl->prop_end(); I != E; ++I) |
| 6592 | ProtocolProperties.push_back(*I); |
| 6593 | |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6594 | Write_prop_list_t_initializer(*this, Context, Result, ProtocolProperties, |
Fariborz Jahanian | da35eac | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 6595 | /* Container */0, |
| 6596 | "_OBJC_PROTOCOL_PROPERTIES_", |
| 6597 | PDecl->getNameAsString()); |
Fariborz Jahanian | da35eac | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 6598 | |
Fariborz Jahanian | 82848c2 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6599 | // Writer out root metadata for current protocol: struct _protocol_t |
Fariborz Jahanian | 1ca052c | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 6600 | Result += "\n"; |
| 6601 | if (LangOpts.MicrosoftExt) |
Fariborz Jahanian | 8590d86 | 2012-04-14 17:13:08 +0000 | [diff] [blame] | 6602 | Result += "static "; |
Fariborz Jahanian | 30650eb | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 6603 | Result += "struct _protocol_t _OBJC_PROTOCOL_"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6604 | Result += PDecl->getNameAsString(); |
Fariborz Jahanian | 82848c2 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6605 | Result += " __attribute__ ((used, section (\"__DATA,__datacoal_nt,coalesced\"))) = {\n"; |
| 6606 | Result += "\t0,\n"; // id is; is null |
| 6607 | Result += "\t\""; Result += PDecl->getNameAsString(); Result += "\",\n"; |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 6608 | if (SuperProtocols.size() > 0) { |
| 6609 | Result += "\t(const struct _protocol_list_t *)&"; Result += "_OBJC_PROTOCOL_REFS_"; |
| 6610 | Result += PDecl->getNameAsString(); Result += ",\n"; |
| 6611 | } |
| 6612 | else |
| 6613 | Result += "\t0,\n"; |
Fariborz Jahanian | 82848c2 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6614 | if (InstanceMethods.size() > 0) { |
| 6615 | Result += "\t(const struct method_list_t *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 6616 | Result += PDecl->getNameAsString(); Result += ",\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6617 | } |
| 6618 | else |
Fariborz Jahanian | 82848c2 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6619 | Result += "\t0,\n"; |
| 6620 | |
| 6621 | if (ClassMethods.size() > 0) { |
| 6622 | Result += "\t(const struct method_list_t *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
| 6623 | Result += PDecl->getNameAsString(); Result += ",\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6624 | } |
| 6625 | else |
Fariborz Jahanian | 82848c2 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6626 | Result += "\t0,\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6627 | |
Fariborz Jahanian | 82848c2 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6628 | if (OptInstanceMethods.size() > 0) { |
| 6629 | Result += "\t(const struct method_list_t *)&_OBJC_PROTOCOL_OPT_INSTANCE_METHODS_"; |
| 6630 | Result += PDecl->getNameAsString(); Result += ",\n"; |
| 6631 | } |
| 6632 | else |
| 6633 | Result += "\t0,\n"; |
| 6634 | |
| 6635 | if (OptClassMethods.size() > 0) { |
| 6636 | Result += "\t(const struct method_list_t *)&_OBJC_PROTOCOL_OPT_CLASS_METHODS_"; |
| 6637 | Result += PDecl->getNameAsString(); Result += ",\n"; |
| 6638 | } |
| 6639 | else |
| 6640 | Result += "\t0,\n"; |
| 6641 | |
| 6642 | if (ProtocolProperties.size() > 0) { |
| 6643 | Result += "\t(const struct _prop_list_t *)&_OBJC_PROTOCOL_PROPERTIES_"; |
| 6644 | Result += PDecl->getNameAsString(); Result += ",\n"; |
| 6645 | } |
| 6646 | else |
| 6647 | Result += "\t0,\n"; |
| 6648 | |
| 6649 | Result += "\t"; Result += "sizeof(_protocol_t)"; Result += ",\n"; |
| 6650 | Result += "\t0,\n"; |
| 6651 | |
Fariborz Jahanian | e0adbd8 | 2012-02-08 22:23:26 +0000 | [diff] [blame] | 6652 | if (AllMethods.size() > 0) { |
| 6653 | Result += "\t(const char **)&"; Result += "_OBJC_PROTOCOL_METHOD_TYPES_"; |
| 6654 | Result += PDecl->getNameAsString(); |
| 6655 | Result += "\n};\n"; |
| 6656 | } |
| 6657 | else |
| 6658 | Result += "\t0\n};\n"; |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 6659 | |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 6660 | if (LangOpts.MicrosoftExt) |
Fariborz Jahanian | 8590d86 | 2012-04-14 17:13:08 +0000 | [diff] [blame] | 6661 | Result += "static "; |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 6662 | Result += "struct _protocol_t *"; |
| 6663 | Result += "_OBJC_LABEL_PROTOCOL_$_"; Result += PDecl->getNameAsString(); |
| 6664 | Result += " = &_OBJC_PROTOCOL_"; Result += PDecl->getNameAsString(); |
| 6665 | Result += ";\n"; |
Fariborz Jahanian | 82848c2 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6666 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6667 | // Mark this protocol as having been generated. |
| 6668 | if (!ObjCSynthesizedProtocols.insert(PDecl->getCanonicalDecl())) |
| 6669 | llvm_unreachable("protocol already synthesized"); |
| 6670 | |
| 6671 | } |
| 6672 | |
| 6673 | void RewriteModernObjC::RewriteObjCProtocolListMetaData( |
| 6674 | const ObjCList<ObjCProtocolDecl> &Protocols, |
| 6675 | StringRef prefix, StringRef ClassName, |
| 6676 | std::string &Result) { |
| 6677 | if (Protocols.empty()) return; |
| 6678 | |
| 6679 | for (unsigned i = 0; i != Protocols.size(); i++) |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 6680 | RewriteObjCProtocolMetaData(Protocols[i], Result); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6681 | |
| 6682 | // Output the top lovel protocol meta-data for the class. |
| 6683 | /* struct _objc_protocol_list { |
| 6684 | struct _objc_protocol_list *next; |
| 6685 | int protocol_count; |
| 6686 | struct _objc_protocol *class_protocols[]; |
| 6687 | } |
| 6688 | */ |
Fariborz Jahanian | 1ca052c | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 6689 | Result += "\n"; |
| 6690 | if (LangOpts.MicrosoftExt) |
| 6691 | Result += "__declspec(allocate(\".cat_cls_meth$B\")) "; |
| 6692 | Result += "static struct {\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6693 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 6694 | Result += "\tint protocol_count;\n"; |
| 6695 | Result += "\tstruct _objc_protocol *class_protocols["; |
| 6696 | Result += utostr(Protocols.size()); |
| 6697 | Result += "];\n} _OBJC_"; |
| 6698 | Result += prefix; |
| 6699 | Result += "_PROTOCOLS_"; |
| 6700 | Result += ClassName; |
| 6701 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 6702 | "{\n\t0, "; |
| 6703 | Result += utostr(Protocols.size()); |
| 6704 | Result += "\n"; |
| 6705 | |
| 6706 | Result += "\t,{&_OBJC_PROTOCOL_"; |
| 6707 | Result += Protocols[0]->getNameAsString(); |
| 6708 | Result += " \n"; |
| 6709 | |
| 6710 | for (unsigned i = 1; i != Protocols.size(); i++) { |
| 6711 | Result += "\t ,&_OBJC_PROTOCOL_"; |
| 6712 | Result += Protocols[i]->getNameAsString(); |
| 6713 | Result += "\n"; |
| 6714 | } |
| 6715 | Result += "\t }\n};\n"; |
| 6716 | } |
| 6717 | |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 6718 | /// hasObjCExceptionAttribute - Return true if this class or any super |
| 6719 | /// class has the __objc_exception__ attribute. |
| 6720 | /// FIXME. Move this to ASTContext.cpp as it is also used for IRGen. |
| 6721 | static bool hasObjCExceptionAttribute(ASTContext &Context, |
| 6722 | const ObjCInterfaceDecl *OID) { |
| 6723 | if (OID->hasAttr<ObjCExceptionAttr>()) |
| 6724 | return true; |
| 6725 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
| 6726 | return hasObjCExceptionAttribute(Context, Super); |
| 6727 | return false; |
| 6728 | } |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6729 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6730 | void RewriteModernObjC::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
| 6731 | std::string &Result) { |
| 6732 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
| 6733 | |
| 6734 | // Explicitly declared @interface's are already synthesized. |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6735 | if (CDecl->isImplicitInterfaceDecl()) |
| 6736 | assert(false && |
| 6737 | "Legacy implicit interface rewriting not supported in moder abi"); |
Fariborz Jahanian | 8f1fed0 | 2012-02-11 20:10:52 +0000 | [diff] [blame] | 6738 | |
Fariborz Jahanian | de5d946 | 2012-03-14 18:09:23 +0000 | [diff] [blame] | 6739 | WriteModernMetadataDeclarations(Context, Result); |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6740 | SmallVector<ObjCIvarDecl *, 8> IVars; |
| 6741 | |
| 6742 | for (ObjCIvarDecl *IVD = CDecl->all_declared_ivar_begin(); |
| 6743 | IVD; IVD = IVD->getNextIvar()) { |
| 6744 | // Ignore unnamed bit-fields. |
| 6745 | if (!IVD->getDeclName()) |
| 6746 | continue; |
| 6747 | IVars.push_back(IVD); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6748 | } |
| 6749 | |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6750 | Write__ivar_list_t_initializer(*this, Context, Result, IVars, |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6751 | "_OBJC_$_INSTANCE_VARIABLES_", |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 6752 | CDecl); |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6753 | |
| 6754 | // Build _objc_method_list for class's instance methods if needed |
| 6755 | SmallVector<ObjCMethodDecl *, 32> |
| 6756 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
| 6757 | |
| 6758 | // If any of our property implementations have associated getters or |
| 6759 | // setters, produce metadata for them as well. |
| 6760 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 6761 | PropEnd = IDecl->propimpl_end(); |
| 6762 | Prop != PropEnd; ++Prop) { |
| 6763 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 6764 | continue; |
| 6765 | if (!(*Prop)->getPropertyIvarDecl()) |
| 6766 | continue; |
| 6767 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 6768 | if (!PD) |
| 6769 | continue; |
| 6770 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 6771 | if (!Getter->isDefined()) |
| 6772 | InstanceMethods.push_back(Getter); |
| 6773 | if (PD->isReadOnly()) |
| 6774 | continue; |
| 6775 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 6776 | if (!Setter->isDefined()) |
| 6777 | InstanceMethods.push_back(Setter); |
| 6778 | } |
| 6779 | |
| 6780 | Write_method_list_t_initializer(*this, Context, Result, InstanceMethods, |
| 6781 | "_OBJC_$_INSTANCE_METHODS_", |
| 6782 | IDecl->getNameAsString(), true); |
| 6783 | |
| 6784 | SmallVector<ObjCMethodDecl *, 32> |
| 6785 | ClassMethods(IDecl->classmeth_begin(), IDecl->classmeth_end()); |
| 6786 | |
| 6787 | Write_method_list_t_initializer(*this, Context, Result, ClassMethods, |
| 6788 | "_OBJC_$_CLASS_METHODS_", |
| 6789 | IDecl->getNameAsString(), true); |
Fariborz Jahanian | 0a52534 | 2012-02-14 19:31:35 +0000 | [diff] [blame] | 6790 | |
| 6791 | // Protocols referenced in class declaration? |
| 6792 | // Protocol's super protocol list |
| 6793 | std::vector<ObjCProtocolDecl *> RefedProtocols; |
| 6794 | const ObjCList<ObjCProtocolDecl> &Protocols = CDecl->getReferencedProtocols(); |
| 6795 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 6796 | E = Protocols.end(); |
| 6797 | I != E; ++I) { |
| 6798 | RefedProtocols.push_back(*I); |
| 6799 | // Must write out all protocol definitions in current qualifier list, |
| 6800 | // and in their nested qualifiers before writing out current definition. |
| 6801 | RewriteObjCProtocolMetaData(*I, Result); |
| 6802 | } |
| 6803 | |
| 6804 | Write_protocol_list_initializer(Context, Result, |
| 6805 | RefedProtocols, |
| 6806 | "_OBJC_CLASS_PROTOCOLS_$_", |
| 6807 | IDecl->getNameAsString()); |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6808 | |
| 6809 | // Protocol's property metadata. |
| 6810 | std::vector<ObjCPropertyDecl *> ClassProperties; |
| 6811 | for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(), |
| 6812 | E = CDecl->prop_end(); I != E; ++I) |
| 6813 | ClassProperties.push_back(*I); |
| 6814 | |
| 6815 | Write_prop_list_t_initializer(*this, Context, Result, ClassProperties, |
Fariborz Jahanian | 2df089d | 2012-03-22 17:39:35 +0000 | [diff] [blame] | 6816 | /* Container */IDecl, |
Fariborz Jahanian | eeabf38 | 2012-02-16 21:57:59 +0000 | [diff] [blame] | 6817 | "_OBJC_$_PROP_LIST_", |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6818 | CDecl->getNameAsString()); |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6819 | |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6820 | |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 6821 | // Data for initializing _class_ro_t metaclass meta-data |
| 6822 | uint32_t flags = CLS_META; |
| 6823 | std::string InstanceSize; |
| 6824 | std::string InstanceStart; |
| 6825 | |
| 6826 | |
| 6827 | bool classIsHidden = CDecl->getVisibility() == HiddenVisibility; |
| 6828 | if (classIsHidden) |
| 6829 | flags |= OBJC2_CLS_HIDDEN; |
| 6830 | |
| 6831 | if (!CDecl->getSuperClass()) |
| 6832 | // class is root |
| 6833 | flags |= CLS_ROOT; |
| 6834 | InstanceSize = "sizeof(struct _class_t)"; |
| 6835 | InstanceStart = InstanceSize; |
| 6836 | Write__class_ro_t_initializer(Context, Result, flags, |
| 6837 | InstanceStart, InstanceSize, |
| 6838 | ClassMethods, |
| 6839 | 0, |
| 6840 | 0, |
| 6841 | 0, |
| 6842 | "_OBJC_METACLASS_RO_$_", |
| 6843 | CDecl->getNameAsString()); |
| 6844 | |
| 6845 | |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6846 | // Data for initializing _class_ro_t meta-data |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 6847 | flags = CLS; |
| 6848 | if (classIsHidden) |
| 6849 | flags |= OBJC2_CLS_HIDDEN; |
| 6850 | |
| 6851 | if (hasObjCExceptionAttribute(*Context, CDecl)) |
| 6852 | flags |= CLS_EXCEPTION; |
| 6853 | |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6854 | if (!CDecl->getSuperClass()) |
| 6855 | // class is root |
| 6856 | flags |= CLS_ROOT; |
| 6857 | |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 6858 | InstanceSize.clear(); |
| 6859 | InstanceStart.clear(); |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6860 | if (!ObjCSynthesizedStructs.count(CDecl)) { |
| 6861 | InstanceSize = "0"; |
| 6862 | InstanceStart = "0"; |
| 6863 | } |
| 6864 | else { |
| 6865 | InstanceSize = "sizeof(struct "; |
| 6866 | InstanceSize += CDecl->getNameAsString(); |
| 6867 | InstanceSize += "_IMPL)"; |
| 6868 | |
| 6869 | ObjCIvarDecl *IVD = CDecl->all_declared_ivar_begin(); |
| 6870 | if (IVD) { |
Fariborz Jahanian | acee1c9 | 2012-04-11 21:12:36 +0000 | [diff] [blame] | 6871 | RewriteIvarOffsetComputation(IVD, InstanceStart); |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6872 | } |
| 6873 | else |
| 6874 | InstanceStart = InstanceSize; |
| 6875 | } |
| 6876 | Write__class_ro_t_initializer(Context, Result, flags, |
| 6877 | InstanceStart, InstanceSize, |
| 6878 | InstanceMethods, |
| 6879 | RefedProtocols, |
| 6880 | IVars, |
| 6881 | ClassProperties, |
| 6882 | "_OBJC_CLASS_RO_$_", |
| 6883 | CDecl->getNameAsString()); |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6884 | |
| 6885 | Write_class_t(Context, Result, |
| 6886 | "OBJC_METACLASS_$_", |
| 6887 | CDecl, /*metaclass*/true); |
| 6888 | |
| 6889 | Write_class_t(Context, Result, |
| 6890 | "OBJC_CLASS_$_", |
| 6891 | CDecl, /*metaclass*/false); |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 6892 | |
| 6893 | if (ImplementationIsNonLazy(IDecl)) |
| 6894 | DefinedNonLazyClasses.push_back(CDecl); |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6895 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6896 | } |
| 6897 | |
Fariborz Jahanian | e033578 | 2012-03-27 18:41:05 +0000 | [diff] [blame] | 6898 | void RewriteModernObjC::RewriteClassSetupInitHook(std::string &Result) { |
| 6899 | int ClsDefCount = ClassImplementation.size(); |
| 6900 | if (!ClsDefCount) |
| 6901 | return; |
| 6902 | Result += "#pragma section(\".objc_inithooks$B\", long, read, write)\n"; |
| 6903 | Result += "__declspec(allocate(\".objc_inithooks$B\")) "; |
| 6904 | Result += "static void *OBJC_CLASS_SETUP[] = {\n"; |
| 6905 | for (int i = 0; i < ClsDefCount; i++) { |
| 6906 | ObjCImplementationDecl *IDecl = ClassImplementation[i]; |
| 6907 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
| 6908 | Result += "\t(void *)&OBJC_CLASS_SETUP_$_"; |
| 6909 | Result += CDecl->getName(); Result += ",\n"; |
| 6910 | } |
| 6911 | Result += "};\n"; |
| 6912 | } |
| 6913 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6914 | void RewriteModernObjC::RewriteMetaDataIntoBuffer(std::string &Result) { |
| 6915 | int ClsDefCount = ClassImplementation.size(); |
| 6916 | int CatDefCount = CategoryImplementation.size(); |
| 6917 | |
| 6918 | // For each implemented class, write out all its meta data. |
| 6919 | for (int i = 0; i < ClsDefCount; i++) |
| 6920 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
| 6921 | |
Fariborz Jahanian | e033578 | 2012-03-27 18:41:05 +0000 | [diff] [blame] | 6922 | RewriteClassSetupInitHook(Result); |
| 6923 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6924 | // For each implemented category, write out all its meta data. |
| 6925 | for (int i = 0; i < CatDefCount; i++) |
| 6926 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
| 6927 | |
Fariborz Jahanian | e033578 | 2012-03-27 18:41:05 +0000 | [diff] [blame] | 6928 | RewriteCategorySetupInitHook(Result); |
| 6929 | |
Fariborz Jahanian | df79567 | 2012-02-17 00:06:14 +0000 | [diff] [blame] | 6930 | if (ClsDefCount > 0) { |
Fariborz Jahanian | 1ca052c | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 6931 | if (LangOpts.MicrosoftExt) |
| 6932 | Result += "__declspec(allocate(\".objc_classlist$B\")) "; |
Fariborz Jahanian | df79567 | 2012-02-17 00:06:14 +0000 | [diff] [blame] | 6933 | Result += "static struct _class_t *L_OBJC_LABEL_CLASS_$ ["; |
| 6934 | Result += llvm::utostr(ClsDefCount); Result += "]"; |
| 6935 | Result += |
| 6936 | " __attribute__((used, section (\"__DATA, __objc_classlist," |
| 6937 | "regular,no_dead_strip\")))= {\n"; |
| 6938 | for (int i = 0; i < ClsDefCount; i++) { |
| 6939 | Result += "\t&OBJC_CLASS_$_"; |
| 6940 | Result += ClassImplementation[i]->getNameAsString(); |
| 6941 | Result += ",\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6942 | } |
Fariborz Jahanian | df79567 | 2012-02-17 00:06:14 +0000 | [diff] [blame] | 6943 | Result += "};\n"; |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 6944 | |
| 6945 | if (!DefinedNonLazyClasses.empty()) { |
| 6946 | if (LangOpts.MicrosoftExt) |
| 6947 | Result += "__declspec(allocate(\".objc_nlclslist$B\")) \n"; |
| 6948 | Result += "static struct _class_t *_OBJC_LABEL_NONLAZY_CLASS_$[] = {\n\t"; |
| 6949 | for (unsigned i = 0, e = DefinedNonLazyClasses.size(); i < e; i++) { |
| 6950 | Result += "\t&OBJC_CLASS_$_"; Result += DefinedNonLazyClasses[i]->getNameAsString(); |
| 6951 | Result += ",\n"; |
| 6952 | } |
| 6953 | Result += "};\n"; |
| 6954 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6955 | } |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6956 | |
| 6957 | if (CatDefCount > 0) { |
Fariborz Jahanian | 1ca052c | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 6958 | if (LangOpts.MicrosoftExt) |
| 6959 | Result += "__declspec(allocate(\".objc_catlist$B\")) "; |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6960 | Result += "static struct _category_t *L_OBJC_LABEL_CATEGORY_$ ["; |
| 6961 | Result += llvm::utostr(CatDefCount); Result += "]"; |
| 6962 | Result += |
| 6963 | " __attribute__((used, section (\"__DATA, __objc_catlist," |
| 6964 | "regular,no_dead_strip\")))= {\n"; |
| 6965 | for (int i = 0; i < CatDefCount; i++) { |
| 6966 | Result += "\t&_OBJC_$_CATEGORY_"; |
| 6967 | Result += |
| 6968 | CategoryImplementation[i]->getClassInterface()->getNameAsString(); |
| 6969 | Result += "_$_"; |
| 6970 | Result += CategoryImplementation[i]->getNameAsString(); |
| 6971 | Result += ",\n"; |
| 6972 | } |
| 6973 | Result += "};\n"; |
| 6974 | } |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 6975 | |
| 6976 | if (!DefinedNonLazyCategories.empty()) { |
| 6977 | if (LangOpts.MicrosoftExt) |
| 6978 | Result += "__declspec(allocate(\".objc_nlcatlist$B\")) \n"; |
| 6979 | Result += "static struct _category_t *_OBJC_LABEL_NONLAZY_CATEGORY_$[] = {\n\t"; |
| 6980 | for (unsigned i = 0, e = DefinedNonLazyCategories.size(); i < e; i++) { |
| 6981 | Result += "\t&_OBJC_$_CATEGORY_"; |
| 6982 | Result += |
| 6983 | DefinedNonLazyCategories[i]->getClassInterface()->getNameAsString(); |
| 6984 | Result += "_$_"; |
| 6985 | Result += DefinedNonLazyCategories[i]->getNameAsString(); |
| 6986 | Result += ",\n"; |
| 6987 | } |
| 6988 | Result += "};\n"; |
| 6989 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6990 | } |
| 6991 | |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 6992 | void RewriteModernObjC::WriteImageInfo(std::string &Result) { |
| 6993 | if (LangOpts.MicrosoftExt) |
| 6994 | Result += "__declspec(allocate(\".objc_imageinfo$B\")) \n"; |
| 6995 | |
| 6996 | Result += "static struct IMAGE_INFO { unsigned version; unsigned flag; } "; |
| 6997 | // version 0, ObjCABI is 2 |
Fariborz Jahanian | 30650eb | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 6998 | Result += "_OBJC_IMAGE_INFO = { 0, 2 };\n"; |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 6999 | } |
| 7000 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7001 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
| 7002 | /// implementation. |
| 7003 | void RewriteModernObjC::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
| 7004 | std::string &Result) { |
Fariborz Jahanian | de5d946 | 2012-03-14 18:09:23 +0000 | [diff] [blame] | 7005 | WriteModernMetadataDeclarations(Context, Result); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7006 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
| 7007 | // Find category declaration for this implementation. |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7008 | ObjCCategoryDecl *CDecl=0; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7009 | for (CDecl = ClassDecl->getCategoryList(); CDecl; |
| 7010 | CDecl = CDecl->getNextClassCategory()) |
| 7011 | if (CDecl->getIdentifier() == IDecl->getIdentifier()) |
| 7012 | break; |
| 7013 | |
| 7014 | std::string FullCategoryName = ClassDecl->getNameAsString(); |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7015 | FullCategoryName += "_$_"; |
| 7016 | FullCategoryName += CDecl->getNameAsString(); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7017 | |
| 7018 | // Build _objc_method_list for class's instance methods if needed |
| 7019 | SmallVector<ObjCMethodDecl *, 32> |
| 7020 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
| 7021 | |
| 7022 | // If any of our property implementations have associated getters or |
| 7023 | // setters, produce metadata for them as well. |
| 7024 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 7025 | PropEnd = IDecl->propimpl_end(); |
| 7026 | Prop != PropEnd; ++Prop) { |
| 7027 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 7028 | continue; |
| 7029 | if (!(*Prop)->getPropertyIvarDecl()) |
| 7030 | continue; |
| 7031 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 7032 | if (!PD) |
| 7033 | continue; |
| 7034 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 7035 | InstanceMethods.push_back(Getter); |
| 7036 | if (PD->isReadOnly()) |
| 7037 | continue; |
| 7038 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 7039 | InstanceMethods.push_back(Setter); |
| 7040 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7041 | |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7042 | Write_method_list_t_initializer(*this, Context, Result, InstanceMethods, |
| 7043 | "_OBJC_$_CATEGORY_INSTANCE_METHODS_", |
| 7044 | FullCategoryName, true); |
| 7045 | |
| 7046 | SmallVector<ObjCMethodDecl *, 32> |
| 7047 | ClassMethods(IDecl->classmeth_begin(), IDecl->classmeth_end()); |
| 7048 | |
| 7049 | Write_method_list_t_initializer(*this, Context, Result, ClassMethods, |
| 7050 | "_OBJC_$_CATEGORY_CLASS_METHODS_", |
| 7051 | FullCategoryName, true); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7052 | |
| 7053 | // Protocols referenced in class declaration? |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7054 | // Protocol's super protocol list |
| 7055 | std::vector<ObjCProtocolDecl *> RefedProtocols; |
Argyrios Kyrtzidis | a5f4441 | 2012-03-13 01:09:41 +0000 | [diff] [blame] | 7056 | for (ObjCInterfaceDecl::protocol_iterator I = CDecl->protocol_begin(), |
| 7057 | E = CDecl->protocol_end(); |
| 7058 | |
| 7059 | I != E; ++I) { |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7060 | RefedProtocols.push_back(*I); |
| 7061 | // Must write out all protocol definitions in current qualifier list, |
| 7062 | // and in their nested qualifiers before writing out current definition. |
| 7063 | RewriteObjCProtocolMetaData(*I, Result); |
| 7064 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7065 | |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7066 | Write_protocol_list_initializer(Context, Result, |
| 7067 | RefedProtocols, |
| 7068 | "_OBJC_CATEGORY_PROTOCOLS_$_", |
| 7069 | FullCategoryName); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7070 | |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7071 | // Protocol's property metadata. |
| 7072 | std::vector<ObjCPropertyDecl *> ClassProperties; |
| 7073 | for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(), |
| 7074 | E = CDecl->prop_end(); I != E; ++I) |
| 7075 | ClassProperties.push_back(*I); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7076 | |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7077 | Write_prop_list_t_initializer(*this, Context, Result, ClassProperties, |
| 7078 | /* Container */0, |
| 7079 | "_OBJC_$_PROP_LIST_", |
| 7080 | FullCategoryName); |
| 7081 | |
| 7082 | Write_category_t(*this, Context, Result, |
Fariborz Jahanian | e033578 | 2012-03-27 18:41:05 +0000 | [diff] [blame] | 7083 | CDecl, |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 7084 | ClassDecl, |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 7085 | InstanceMethods, |
| 7086 | ClassMethods, |
| 7087 | RefedProtocols, |
| 7088 | ClassProperties); |
| 7089 | |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 7090 | // Determine if this category is also "non-lazy". |
| 7091 | if (ImplementationIsNonLazy(IDecl)) |
| 7092 | DefinedNonLazyCategories.push_back(CDecl); |
Fariborz Jahanian | e033578 | 2012-03-27 18:41:05 +0000 | [diff] [blame] | 7093 | |
| 7094 | } |
| 7095 | |
| 7096 | void RewriteModernObjC::RewriteCategorySetupInitHook(std::string &Result) { |
| 7097 | int CatDefCount = CategoryImplementation.size(); |
| 7098 | if (!CatDefCount) |
| 7099 | return; |
| 7100 | Result += "#pragma section(\".objc_inithooks$B\", long, read, write)\n"; |
| 7101 | Result += "__declspec(allocate(\".objc_inithooks$B\")) "; |
| 7102 | Result += "static void *OBJC_CATEGORY_SETUP[] = {\n"; |
| 7103 | for (int i = 0; i < CatDefCount; i++) { |
| 7104 | ObjCCategoryImplDecl *IDecl = CategoryImplementation[i]; |
| 7105 | ObjCCategoryDecl *CatDecl= IDecl->getCategoryDecl(); |
| 7106 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
| 7107 | Result += "\t(void *)&OBJC_CATEGORY_SETUP_$_"; |
| 7108 | Result += ClassDecl->getName(); |
| 7109 | Result += "_$_"; |
| 7110 | Result += CatDecl->getName(); |
| 7111 | Result += ",\n"; |
| 7112 | } |
| 7113 | Result += "};\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7114 | } |
| 7115 | |
| 7116 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
| 7117 | /// class methods. |
| 7118 | template<typename MethodIterator> |
| 7119 | void RewriteModernObjC::RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 7120 | MethodIterator MethodEnd, |
| 7121 | bool IsInstanceMethod, |
| 7122 | StringRef prefix, |
| 7123 | StringRef ClassName, |
| 7124 | std::string &Result) { |
| 7125 | if (MethodBegin == MethodEnd) return; |
| 7126 | |
| 7127 | if (!objc_impl_method) { |
| 7128 | /* struct _objc_method { |
| 7129 | SEL _cmd; |
| 7130 | char *method_types; |
| 7131 | void *_imp; |
| 7132 | } |
| 7133 | */ |
| 7134 | Result += "\nstruct _objc_method {\n"; |
| 7135 | Result += "\tSEL _cmd;\n"; |
| 7136 | Result += "\tchar *method_types;\n"; |
| 7137 | Result += "\tvoid *_imp;\n"; |
| 7138 | Result += "};\n"; |
| 7139 | |
| 7140 | objc_impl_method = true; |
| 7141 | } |
| 7142 | |
| 7143 | // Build _objc_method_list for class's methods if needed |
| 7144 | |
| 7145 | /* struct { |
| 7146 | struct _objc_method_list *next_method; |
| 7147 | int method_count; |
| 7148 | struct _objc_method method_list[]; |
| 7149 | } |
| 7150 | */ |
| 7151 | unsigned NumMethods = std::distance(MethodBegin, MethodEnd); |
Fariborz Jahanian | 1ca052c | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 7152 | Result += "\n"; |
| 7153 | if (LangOpts.MicrosoftExt) { |
| 7154 | if (IsInstanceMethod) |
| 7155 | Result += "__declspec(allocate(\".inst_meth$B\")) "; |
| 7156 | else |
| 7157 | Result += "__declspec(allocate(\".cls_meth$B\")) "; |
| 7158 | } |
| 7159 | Result += "static struct {\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7160 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 7161 | Result += "\tint method_count;\n"; |
| 7162 | Result += "\tstruct _objc_method method_list["; |
| 7163 | Result += utostr(NumMethods); |
| 7164 | Result += "];\n} _OBJC_"; |
| 7165 | Result += prefix; |
| 7166 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 7167 | Result += "_METHODS_"; |
| 7168 | Result += ClassName; |
| 7169 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
| 7170 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 7171 | Result += "_meth\")))= "; |
| 7172 | Result += "{\n\t0, " + utostr(NumMethods) + "\n"; |
| 7173 | |
| 7174 | Result += "\t,{{(SEL)\""; |
| 7175 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
| 7176 | std::string MethodTypeString; |
| 7177 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
| 7178 | Result += "\", \""; |
| 7179 | Result += MethodTypeString; |
| 7180 | Result += "\", (void *)"; |
| 7181 | Result += MethodInternalNames[*MethodBegin]; |
| 7182 | Result += "}\n"; |
| 7183 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 7184 | Result += "\t ,{(SEL)\""; |
| 7185 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
| 7186 | std::string MethodTypeString; |
| 7187 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
| 7188 | Result += "\", \""; |
| 7189 | Result += MethodTypeString; |
| 7190 | Result += "\", (void *)"; |
| 7191 | Result += MethodInternalNames[*MethodBegin]; |
| 7192 | Result += "}\n"; |
| 7193 | } |
| 7194 | Result += "\t }\n};\n"; |
| 7195 | } |
| 7196 | |
| 7197 | Stmt *RewriteModernObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV) { |
| 7198 | SourceRange OldRange = IV->getSourceRange(); |
| 7199 | Expr *BaseExpr = IV->getBase(); |
| 7200 | |
| 7201 | // Rewrite the base, but without actually doing replaces. |
| 7202 | { |
| 7203 | DisableReplaceStmtScope S(*this); |
| 7204 | BaseExpr = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(BaseExpr)); |
| 7205 | IV->setBase(BaseExpr); |
| 7206 | } |
| 7207 | |
| 7208 | ObjCIvarDecl *D = IV->getDecl(); |
| 7209 | |
| 7210 | Expr *Replacement = IV; |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7211 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7212 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
| 7213 | const ObjCInterfaceType *iFaceDecl = |
| 7214 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
| 7215 | assert(iFaceDecl && "RewriteObjCIvarRefExpr - iFaceDecl is null"); |
| 7216 | // lookup which class implements the instance variable. |
| 7217 | ObjCInterfaceDecl *clsDeclared = 0; |
| 7218 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
| 7219 | clsDeclared); |
| 7220 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
| 7221 | |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7222 | // Build name of symbol holding ivar offset. |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 7223 | std::string IvarOffsetName; |
| 7224 | WriteInternalIvarName(clsDeclared, D, IvarOffsetName); |
| 7225 | |
Fariborz Jahanian | 72c88f1 | 2012-02-22 18:13:25 +0000 | [diff] [blame] | 7226 | ReferencedIvars[clsDeclared].insert(D); |
| 7227 | |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7228 | // cast offset to "char *". |
| 7229 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, |
| 7230 | Context->getPointerType(Context->CharTy), |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7231 | CK_BitCast, |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7232 | BaseExpr); |
| 7233 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
| 7234 | SourceLocation(), &Context->Idents.get(IvarOffsetName), |
| 7235 | Context->UnsignedLongTy, 0, SC_Extern, SC_None); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 7236 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, false, |
| 7237 | Context->UnsignedLongTy, VK_LValue, |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7238 | SourceLocation()); |
| 7239 | BinaryOperator *addExpr = |
| 7240 | new (Context) BinaryOperator(castExpr, DRE, BO_Add, |
| 7241 | Context->getPointerType(Context->CharTy), |
| 7242 | VK_RValue, OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7243 | // Don't forget the parens to enforce the proper binding. |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7244 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), |
| 7245 | SourceLocation(), |
| 7246 | addExpr); |
Fariborz Jahanian | 0d6e22a | 2012-02-24 17:35:35 +0000 | [diff] [blame] | 7247 | QualType IvarT = D->getType(); |
Fariborz Jahanian | 27fc81b | 2012-04-27 22:48:54 +0000 | [diff] [blame] | 7248 | |
| 7249 | if (IvarT->isRecordType()) { |
| 7250 | RecordDecl *RD = IvarT->getAs<RecordType>()->getDecl(); |
| 7251 | RD = RD->getDefinition(); |
| 7252 | bool structIsInside = RD && |
| 7253 | Context->getSourceManager().isBeforeInTranslationUnit( |
| 7254 | iFaceDecl->getDecl()->getLocation(), RD->getLocation()); |
| 7255 | if (structIsInside) { |
| 7256 | // decltype(((Foo_IMPL*)0)->bar) * |
| 7257 | std::string RecName = iFaceDecl->getDecl()->getName(); |
| 7258 | RecName += "_IMPL"; |
| 7259 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 7260 | SourceLocation(), SourceLocation(), |
| 7261 | &Context->Idents.get(RecName.c_str())); |
| 7262 | QualType PtrStructIMPL = Context->getPointerType(Context->getTagDeclType(RD)); |
| 7263 | unsigned UnsignedIntSize = |
| 7264 | static_cast<unsigned>(Context->getTypeSize(Context->UnsignedIntTy)); |
| 7265 | Expr *Zero = IntegerLiteral::Create(*Context, |
| 7266 | llvm::APInt(UnsignedIntSize, 0), |
| 7267 | Context->UnsignedIntTy, SourceLocation()); |
| 7268 | Zero = NoTypeInfoCStyleCastExpr(Context, PtrStructIMPL, CK_BitCast, Zero); |
| 7269 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 7270 | Zero); |
| 7271 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 7272 | SourceLocation(), |
| 7273 | &Context->Idents.get(D->getNameAsString()), |
| 7274 | IvarT, 0, |
| 7275 | /*BitWidth=*/0, /*Mutable=*/true, |
| 7276 | /*HasInit=*/false); |
| 7277 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
| 7278 | FD->getType(), VK_LValue, |
| 7279 | OK_Ordinary); |
| 7280 | IvarT = Context->getDecltypeType(ME, ME->getType()); |
| 7281 | } |
| 7282 | } |
Fariborz Jahanian | 8e0913d | 2012-02-29 00:26:20 +0000 | [diff] [blame] | 7283 | convertObjCTypeToCStyleType(IvarT); |
Fariborz Jahanian | 0d6e22a | 2012-02-24 17:35:35 +0000 | [diff] [blame] | 7284 | QualType castT = Context->getPointerType(IvarT); |
Fariborz Jahanian | 27fc81b | 2012-04-27 22:48:54 +0000 | [diff] [blame] | 7285 | |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7286 | castExpr = NoTypeInfoCStyleCastExpr(Context, |
| 7287 | castT, |
| 7288 | CK_BitCast, |
| 7289 | PE); |
Fariborz Jahanian | 27fc81b | 2012-04-27 22:48:54 +0000 | [diff] [blame] | 7290 | |
| 7291 | |
Fariborz Jahanian | 8e0913d | 2012-02-29 00:26:20 +0000 | [diff] [blame] | 7292 | Expr *Exp = new (Context) UnaryOperator(castExpr, UO_Deref, IvarT, |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7293 | VK_LValue, OK_Ordinary, |
| 7294 | SourceLocation()); |
| 7295 | PE = new (Context) ParenExpr(OldRange.getBegin(), |
| 7296 | OldRange.getEnd(), |
| 7297 | Exp); |
| 7298 | |
| 7299 | Replacement = PE; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7300 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7301 | |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 7302 | ReplaceStmtWithRange(IV, Replacement, OldRange); |
| 7303 | return Replacement; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 7304 | } |