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; |
| 105 | FunctionDecl *CurFunctionDeclToDeclareForBlock; |
| 106 | |
| 107 | /* Misc. containers needed for meta-data rewrite. */ |
| 108 | SmallVector<ObjCImplementationDecl *, 8> ClassImplementation; |
| 109 | SmallVector<ObjCCategoryImplDecl *, 8> CategoryImplementation; |
| 110 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCSynthesizedStructs; |
| 111 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> ObjCSynthesizedProtocols; |
Fariborz Jahanian | cf4c60f | 2012-02-17 22:20:12 +0000 | [diff] [blame] | 112 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCWrittenInterfaces; |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 113 | llvm::SmallPtrSet<TagDecl*, 8> TagsDefinedInIvarDecls; |
Fariborz Jahanian | cf4c60f | 2012-02-17 22:20:12 +0000 | [diff] [blame] | 114 | SmallVector<ObjCInterfaceDecl*, 32> ObjCInterfacesSeen; |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 115 | /// DefinedNonLazyClasses - List of defined "non-lazy" classes. |
| 116 | SmallVector<ObjCInterfaceDecl*, 8> DefinedNonLazyClasses; |
| 117 | |
| 118 | /// DefinedNonLazyCategories - List of defined "non-lazy" categories. |
| 119 | llvm::SmallVector<ObjCCategoryDecl*, 8> DefinedNonLazyCategories; |
| 120 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 121 | SmallVector<Stmt *, 32> Stmts; |
| 122 | SmallVector<int, 8> ObjCBcLabelNo; |
| 123 | // Remember all the @protocol(<expr>) expressions. |
| 124 | llvm::SmallPtrSet<ObjCProtocolDecl *, 32> ProtocolExprDecls; |
| 125 | |
| 126 | llvm::DenseSet<uint64_t> CopyDestroyCache; |
| 127 | |
| 128 | // Block expressions. |
| 129 | SmallVector<BlockExpr *, 32> Blocks; |
| 130 | SmallVector<int, 32> InnerDeclRefsCount; |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 131 | SmallVector<DeclRefExpr *, 32> InnerDeclRefs; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 132 | |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 133 | SmallVector<DeclRefExpr *, 32> BlockDeclRefs; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 134 | |
| 135 | // Block related declarations. |
| 136 | SmallVector<ValueDecl *, 8> BlockByCopyDecls; |
| 137 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByCopyDeclsPtrSet; |
| 138 | SmallVector<ValueDecl *, 8> BlockByRefDecls; |
| 139 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByRefDeclsPtrSet; |
| 140 | llvm::DenseMap<ValueDecl *, unsigned> BlockByRefDeclNo; |
| 141 | llvm::SmallPtrSet<ValueDecl *, 8> ImportedBlockDecls; |
| 142 | llvm::SmallPtrSet<VarDecl *, 8> ImportedLocalExternalDecls; |
| 143 | |
| 144 | llvm::DenseMap<BlockExpr *, std::string> RewrittenBlockExprs; |
Fariborz Jahanian | 72c88f1 | 2012-02-22 18:13:25 +0000 | [diff] [blame] | 145 | llvm::DenseMap<ObjCInterfaceDecl *, |
| 146 | llvm::SmallPtrSet<ObjCIvarDecl *, 8> > ReferencedIvars; |
| 147 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 148 | // This maps an original source AST to it's rewritten form. This allows |
| 149 | // us to avoid rewriting the same node twice (which is very uncommon). |
| 150 | // This is needed to support some of the exotic property rewriting. |
| 151 | llvm::DenseMap<Stmt *, Stmt *> ReplacedNodes; |
| 152 | |
| 153 | // Needed for header files being rewritten |
| 154 | bool IsHeader; |
| 155 | bool SilenceRewriteMacroWarning; |
| 156 | bool objc_impl_method; |
| 157 | |
| 158 | bool DisableReplaceStmt; |
| 159 | class DisableReplaceStmtScope { |
| 160 | RewriteModernObjC &R; |
| 161 | bool SavedValue; |
| 162 | |
| 163 | public: |
| 164 | DisableReplaceStmtScope(RewriteModernObjC &R) |
| 165 | : R(R), SavedValue(R.DisableReplaceStmt) { |
| 166 | R.DisableReplaceStmt = true; |
| 167 | } |
| 168 | ~DisableReplaceStmtScope() { |
| 169 | R.DisableReplaceStmt = SavedValue; |
| 170 | } |
| 171 | }; |
| 172 | void InitializeCommon(ASTContext &context); |
| 173 | |
| 174 | public: |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 175 | llvm::DenseMap<ObjCMethodDecl*, std::string> MethodInternalNames; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 176 | // Top Level Driver code. |
| 177 | virtual bool HandleTopLevelDecl(DeclGroupRef D) { |
| 178 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
| 179 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(*I)) { |
| 180 | if (!Class->isThisDeclarationADefinition()) { |
| 181 | RewriteForwardClassDecl(D); |
| 182 | break; |
Fariborz Jahanian | cf4c60f | 2012-02-17 22:20:12 +0000 | [diff] [blame] | 183 | } else { |
| 184 | // Keep track of all interface declarations seen. |
Fariborz Jahanian | f329527 | 2012-02-24 21:42:38 +0000 | [diff] [blame] | 185 | ObjCInterfacesSeen.push_back(Class); |
Fariborz Jahanian | cf4c60f | 2012-02-17 22:20:12 +0000 | [diff] [blame] | 186 | break; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
| 190 | if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(*I)) { |
| 191 | if (!Proto->isThisDeclarationADefinition()) { |
| 192 | RewriteForwardProtocolDecl(D); |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | HandleTopLevelSingleDecl(*I); |
| 198 | } |
| 199 | return true; |
| 200 | } |
| 201 | void HandleTopLevelSingleDecl(Decl *D); |
| 202 | void HandleDeclInMainFile(Decl *D); |
| 203 | RewriteModernObjC(std::string inFile, raw_ostream *OS, |
| 204 | DiagnosticsEngine &D, const LangOptions &LOpts, |
| 205 | bool silenceMacroWarn); |
| 206 | |
| 207 | ~RewriteModernObjC() {} |
| 208 | |
| 209 | virtual void HandleTranslationUnit(ASTContext &C); |
| 210 | |
| 211 | void ReplaceStmt(Stmt *Old, Stmt *New) { |
| 212 | Stmt *ReplacingStmt = ReplacedNodes[Old]; |
| 213 | |
| 214 | if (ReplacingStmt) |
| 215 | return; // We can't rewrite the same node twice. |
| 216 | |
| 217 | if (DisableReplaceStmt) |
| 218 | return; |
| 219 | |
| 220 | // If replacement succeeded or warning disabled return with no warning. |
| 221 | if (!Rewrite.ReplaceStmt(Old, New)) { |
| 222 | ReplacedNodes[Old] = New; |
| 223 | return; |
| 224 | } |
| 225 | if (SilenceRewriteMacroWarning) |
| 226 | return; |
| 227 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 228 | << Old->getSourceRange(); |
| 229 | } |
| 230 | |
| 231 | void ReplaceStmtWithRange(Stmt *Old, Stmt *New, SourceRange SrcRange) { |
| 232 | if (DisableReplaceStmt) |
| 233 | return; |
| 234 | |
| 235 | // Measure the old text. |
| 236 | int Size = Rewrite.getRangeSize(SrcRange); |
| 237 | if (Size == -1) { |
| 238 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 239 | << Old->getSourceRange(); |
| 240 | return; |
| 241 | } |
| 242 | // Get the new text. |
| 243 | std::string SStr; |
| 244 | llvm::raw_string_ostream S(SStr); |
| 245 | New->printPretty(S, *Context, 0, PrintingPolicy(LangOpts)); |
| 246 | const std::string &Str = S.str(); |
| 247 | |
| 248 | // If replacement succeeded or warning disabled return with no warning. |
| 249 | if (!Rewrite.ReplaceText(SrcRange.getBegin(), Size, Str)) { |
| 250 | ReplacedNodes[Old] = New; |
| 251 | return; |
| 252 | } |
| 253 | if (SilenceRewriteMacroWarning) |
| 254 | return; |
| 255 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 256 | << Old->getSourceRange(); |
| 257 | } |
| 258 | |
| 259 | void InsertText(SourceLocation Loc, StringRef Str, |
| 260 | bool InsertAfter = true) { |
| 261 | // If insertion succeeded or warning disabled return with no warning. |
| 262 | if (!Rewrite.InsertText(Loc, Str, InsertAfter) || |
| 263 | SilenceRewriteMacroWarning) |
| 264 | return; |
| 265 | |
| 266 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 267 | } |
| 268 | |
| 269 | void ReplaceText(SourceLocation Start, unsigned OrigLength, |
| 270 | StringRef Str) { |
| 271 | // If removal succeeded or warning disabled return with no warning. |
| 272 | if (!Rewrite.ReplaceText(Start, OrigLength, Str) || |
| 273 | SilenceRewriteMacroWarning) |
| 274 | return; |
| 275 | |
| 276 | Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag); |
| 277 | } |
| 278 | |
| 279 | // Syntactic Rewriting. |
| 280 | void RewriteRecordBody(RecordDecl *RD); |
| 281 | void RewriteInclude(); |
| 282 | void RewriteForwardClassDecl(DeclGroupRef D); |
| 283 | void RewriteForwardClassDecl(const llvm::SmallVector<Decl*, 8> &DG); |
| 284 | void RewriteForwardClassEpilogue(ObjCInterfaceDecl *ClassDecl, |
| 285 | const std::string &typedefString); |
| 286 | void RewriteImplementations(); |
| 287 | void RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 288 | ObjCImplementationDecl *IMD, |
| 289 | ObjCCategoryImplDecl *CID); |
| 290 | void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl); |
| 291 | void RewriteImplementationDecl(Decl *Dcl); |
| 292 | void RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl, |
| 293 | ObjCMethodDecl *MDecl, std::string &ResultStr); |
| 294 | void RewriteTypeIntoString(QualType T, std::string &ResultStr, |
| 295 | const FunctionType *&FPRetType); |
| 296 | void RewriteByRefString(std::string &ResultStr, const std::string &Name, |
| 297 | ValueDecl *VD, bool def=false); |
| 298 | void RewriteCategoryDecl(ObjCCategoryDecl *Dcl); |
| 299 | void RewriteProtocolDecl(ObjCProtocolDecl *Dcl); |
| 300 | void RewriteForwardProtocolDecl(DeclGroupRef D); |
| 301 | void RewriteForwardProtocolDecl(const llvm::SmallVector<Decl*, 8> &DG); |
| 302 | void RewriteMethodDeclaration(ObjCMethodDecl *Method); |
| 303 | void RewriteProperty(ObjCPropertyDecl *prop); |
| 304 | void RewriteFunctionDecl(FunctionDecl *FD); |
| 305 | void RewriteBlockPointerType(std::string& Str, QualType Type); |
| 306 | void RewriteBlockPointerTypeVariable(std::string& Str, ValueDecl *VD); |
| 307 | void RewriteBlockLiteralFunctionDecl(FunctionDecl *FD); |
| 308 | void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl); |
| 309 | void RewriteTypeOfDecl(VarDecl *VD); |
| 310 | void RewriteObjCQualifiedInterfaceTypes(Expr *E); |
| 311 | |
| 312 | // Expression Rewriting. |
| 313 | Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S); |
| 314 | Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp); |
| 315 | Stmt *RewritePropertyOrImplicitGetter(PseudoObjectExpr *Pseudo); |
| 316 | Stmt *RewritePropertyOrImplicitSetter(PseudoObjectExpr *Pseudo); |
| 317 | Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp); |
| 318 | Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp); |
| 319 | Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp); |
| 320 | Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 321 | Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S); |
| 322 | Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
| 323 | Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S); |
| 324 | Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
| 325 | SourceLocation OrigEnd); |
| 326 | Stmt *RewriteBreakStmt(BreakStmt *S); |
| 327 | Stmt *RewriteContinueStmt(ContinueStmt *S); |
| 328 | void RewriteCastExpr(CStyleCastExpr *CE); |
| 329 | |
| 330 | // Block rewriting. |
| 331 | void RewriteBlocksInFunctionProtoType(QualType funcType, NamedDecl *D); |
| 332 | |
| 333 | // Block specific rewrite rules. |
| 334 | void RewriteBlockPointerDecl(NamedDecl *VD); |
| 335 | void RewriteByRefVar(VarDecl *VD); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 336 | Stmt *RewriteBlockDeclRefExpr(DeclRefExpr *VD); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 337 | Stmt *RewriteLocalVariableExternalStorage(DeclRefExpr *DRE); |
| 338 | void RewriteBlockPointerFunctionArgs(FunctionDecl *FD); |
| 339 | |
| 340 | void RewriteObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
| 341 | std::string &Result); |
| 342 | |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 343 | void RewriteObjCFieldDecl(FieldDecl *fieldDecl, std::string &Result); |
| 344 | |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 345 | bool RewriteObjCFieldDeclType(QualType &Type, std::string &Result); |
| 346 | |
Fariborz Jahanian | 72c88f1 | 2012-02-22 18:13:25 +0000 | [diff] [blame] | 347 | void RewriteIvarOffsetSymbols(ObjCInterfaceDecl *CDecl, |
| 348 | std::string &Result); |
| 349 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 350 | virtual void Initialize(ASTContext &context); |
| 351 | |
| 352 | // Misc. AST transformation routines. Somtimes they end up calling |
| 353 | // rewriting routines on the new ASTs. |
| 354 | CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD, |
| 355 | Expr **args, unsigned nargs, |
| 356 | SourceLocation StartLoc=SourceLocation(), |
| 357 | SourceLocation EndLoc=SourceLocation()); |
| 358 | |
| 359 | Stmt *SynthMessageExpr(ObjCMessageExpr *Exp, |
| 360 | SourceLocation StartLoc=SourceLocation(), |
| 361 | SourceLocation EndLoc=SourceLocation()); |
| 362 | |
| 363 | void SynthCountByEnumWithState(std::string &buf); |
| 364 | void SynthMsgSendFunctionDecl(); |
| 365 | void SynthMsgSendSuperFunctionDecl(); |
| 366 | void SynthMsgSendStretFunctionDecl(); |
| 367 | void SynthMsgSendFpretFunctionDecl(); |
| 368 | void SynthMsgSendSuperStretFunctionDecl(); |
| 369 | void SynthGetClassFunctionDecl(); |
| 370 | void SynthGetMetaClassFunctionDecl(); |
| 371 | void SynthGetSuperClassFunctionDecl(); |
| 372 | void SynthSelGetUidFunctionDecl(); |
| 373 | void SynthSuperContructorFunctionDecl(); |
| 374 | |
| 375 | // Rewriting metadata |
| 376 | template<typename MethodIterator> |
| 377 | void RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 378 | MethodIterator MethodEnd, |
| 379 | bool IsInstanceMethod, |
| 380 | StringRef prefix, |
| 381 | StringRef ClassName, |
| 382 | std::string &Result); |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 383 | void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol, |
| 384 | std::string &Result); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 385 | virtual void RewriteObjCProtocolListMetaData( |
| 386 | const ObjCList<ObjCProtocolDecl> &Prots, |
| 387 | StringRef prefix, StringRef ClassName, std::string &Result); |
| 388 | virtual void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
| 389 | std::string &Result); |
Fariborz Jahanian | e033578 | 2012-03-27 18:41:05 +0000 | [diff] [blame^] | 390 | virtual void RewriteClassSetupInitHook(std::string &Result); |
| 391 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 392 | virtual void RewriteMetaDataIntoBuffer(std::string &Result); |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 393 | virtual void WriteImageInfo(std::string &Result); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 394 | virtual void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
| 395 | std::string &Result); |
Fariborz Jahanian | e033578 | 2012-03-27 18:41:05 +0000 | [diff] [blame^] | 396 | virtual void RewriteCategorySetupInitHook(std::string &Result); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 397 | |
| 398 | // Rewriting ivar |
| 399 | virtual void RewriteIvarOffsetComputation(ObjCIvarDecl *ivar, |
| 400 | std::string &Result); |
| 401 | virtual Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV); |
| 402 | |
| 403 | |
| 404 | std::string SynthesizeByrefCopyDestroyHelper(VarDecl *VD, int flag); |
| 405 | std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
| 406 | StringRef funcName, std::string Tag); |
| 407 | std::string SynthesizeBlockFunc(BlockExpr *CE, int i, |
| 408 | StringRef funcName, std::string Tag); |
| 409 | std::string SynthesizeBlockImpl(BlockExpr *CE, |
| 410 | std::string Tag, std::string Desc); |
| 411 | std::string SynthesizeBlockDescriptor(std::string DescTag, |
| 412 | std::string ImplTag, |
| 413 | int i, StringRef funcName, |
| 414 | unsigned hasCopy); |
| 415 | Stmt *SynthesizeBlockCall(CallExpr *Exp, const Expr* BlockExp); |
| 416 | void SynthesizeBlockLiterals(SourceLocation FunLocStart, |
| 417 | StringRef FunName); |
| 418 | FunctionDecl *SynthBlockInitFunctionDecl(StringRef name); |
| 419 | Stmt *SynthBlockInitExpr(BlockExpr *Exp, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 420 | const SmallVector<DeclRefExpr *, 8> &InnerBlockDeclRefs); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 421 | |
| 422 | // Misc. helper routines. |
| 423 | QualType getProtocolType(); |
| 424 | void WarnAboutReturnGotoStmts(Stmt *S); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 425 | void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND); |
| 426 | void InsertBlockLiteralsWithinFunction(FunctionDecl *FD); |
| 427 | void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD); |
| 428 | |
| 429 | bool IsDeclStmtInForeachHeader(DeclStmt *DS); |
| 430 | void CollectBlockDeclRefInfo(BlockExpr *Exp); |
| 431 | void GetBlockDeclRefExprs(Stmt *S); |
| 432 | void GetInnerBlockDeclRefExprs(Stmt *S, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 433 | SmallVector<DeclRefExpr *, 8> &InnerBlockDeclRefs, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 434 | llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts); |
| 435 | |
| 436 | // We avoid calling Type::isBlockPointerType(), since it operates on the |
| 437 | // canonical type. We only care if the top-level type is a closure pointer. |
| 438 | bool isTopLevelBlockPointerType(QualType T) { |
| 439 | return isa<BlockPointerType>(T); |
| 440 | } |
| 441 | |
| 442 | /// convertBlockPointerToFunctionPointer - Converts a block-pointer type |
| 443 | /// to a function pointer type and upon success, returns true; false |
| 444 | /// otherwise. |
| 445 | bool convertBlockPointerToFunctionPointer(QualType &T) { |
| 446 | if (isTopLevelBlockPointerType(T)) { |
| 447 | const BlockPointerType *BPT = T->getAs<BlockPointerType>(); |
| 448 | T = Context->getPointerType(BPT->getPointeeType()); |
| 449 | return true; |
| 450 | } |
| 451 | return false; |
| 452 | } |
| 453 | |
Fariborz Jahanian | 164d6f8 | 2012-02-13 18:57:49 +0000 | [diff] [blame] | 454 | bool convertObjCTypeToCStyleType(QualType &T); |
| 455 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 456 | bool needToScanForQualifiers(QualType T); |
| 457 | QualType getSuperStructType(); |
| 458 | QualType getConstantStringStructType(); |
| 459 | QualType convertFunctionTypeOfBlocks(const FunctionType *FT); |
| 460 | bool BufferContainsPPDirectives(const char *startBuf, const char *endBuf); |
| 461 | |
| 462 | void convertToUnqualifiedObjCType(QualType &T) { |
| 463 | if (T->isObjCQualifiedIdType()) |
| 464 | T = Context->getObjCIdType(); |
| 465 | else if (T->isObjCQualifiedClassType()) |
| 466 | T = Context->getObjCClassType(); |
| 467 | else if (T->isObjCObjectPointerType() && |
| 468 | T->getPointeeType()->isObjCQualifiedInterfaceType()) { |
| 469 | if (const ObjCObjectPointerType * OBJPT = |
| 470 | T->getAsObjCInterfacePointerType()) { |
| 471 | const ObjCInterfaceType *IFaceT = OBJPT->getInterfaceType(); |
| 472 | T = QualType(IFaceT, 0); |
| 473 | T = Context->getPointerType(T); |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | // FIXME: This predicate seems like it would be useful to add to ASTContext. |
| 479 | bool isObjCType(QualType T) { |
| 480 | if (!LangOpts.ObjC1 && !LangOpts.ObjC2) |
| 481 | return false; |
| 482 | |
| 483 | QualType OCT = Context->getCanonicalType(T).getUnqualifiedType(); |
| 484 | |
| 485 | if (OCT == Context->getCanonicalType(Context->getObjCIdType()) || |
| 486 | OCT == Context->getCanonicalType(Context->getObjCClassType())) |
| 487 | return true; |
| 488 | |
| 489 | if (const PointerType *PT = OCT->getAs<PointerType>()) { |
| 490 | if (isa<ObjCInterfaceType>(PT->getPointeeType()) || |
| 491 | PT->getPointeeType()->isObjCQualifiedIdType()) |
| 492 | return true; |
| 493 | } |
| 494 | return false; |
| 495 | } |
| 496 | bool PointerTypeTakesAnyBlockArguments(QualType QT); |
| 497 | bool PointerTypeTakesAnyObjCQualifiedType(QualType QT); |
| 498 | void GetExtentOfArgList(const char *Name, const char *&LParen, |
| 499 | const char *&RParen); |
| 500 | |
| 501 | void QuoteDoublequotes(std::string &From, std::string &To) { |
| 502 | for (unsigned i = 0; i < From.length(); i++) { |
| 503 | if (From[i] == '"') |
| 504 | To += "\\\""; |
| 505 | else |
| 506 | To += From[i]; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | QualType getSimpleFunctionType(QualType result, |
| 511 | const QualType *args, |
| 512 | unsigned numArgs, |
| 513 | bool variadic = false) { |
| 514 | if (result == Context->getObjCInstanceType()) |
| 515 | result = Context->getObjCIdType(); |
| 516 | FunctionProtoType::ExtProtoInfo fpi; |
| 517 | fpi.Variadic = variadic; |
| 518 | return Context->getFunctionType(result, args, numArgs, fpi); |
| 519 | } |
| 520 | |
| 521 | // Helper function: create a CStyleCastExpr with trivial type source info. |
| 522 | CStyleCastExpr* NoTypeInfoCStyleCastExpr(ASTContext *Ctx, QualType Ty, |
| 523 | CastKind Kind, Expr *E) { |
| 524 | TypeSourceInfo *TInfo = Ctx->getTrivialTypeSourceInfo(Ty, SourceLocation()); |
| 525 | return CStyleCastExpr::Create(*Ctx, Ty, VK_RValue, Kind, E, 0, TInfo, |
| 526 | SourceLocation(), SourceLocation()); |
| 527 | } |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 528 | |
| 529 | bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const { |
| 530 | IdentifierInfo* II = &Context->Idents.get("load"); |
| 531 | Selector LoadSel = Context->Selectors.getSelector(0, &II); |
| 532 | return OD->getClassMethod(LoadSel) != 0; |
| 533 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 534 | }; |
| 535 | |
| 536 | } |
| 537 | |
| 538 | void RewriteModernObjC::RewriteBlocksInFunctionProtoType(QualType funcType, |
| 539 | NamedDecl *D) { |
| 540 | if (const FunctionProtoType *fproto |
| 541 | = dyn_cast<FunctionProtoType>(funcType.IgnoreParens())) { |
| 542 | for (FunctionProtoType::arg_type_iterator I = fproto->arg_type_begin(), |
| 543 | E = fproto->arg_type_end(); I && (I != E); ++I) |
| 544 | if (isTopLevelBlockPointerType(*I)) { |
| 545 | // All the args are checked/rewritten. Don't call twice! |
| 546 | RewriteBlockPointerDecl(D); |
| 547 | break; |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | void RewriteModernObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) { |
| 553 | const PointerType *PT = funcType->getAs<PointerType>(); |
| 554 | if (PT && PointerTypeTakesAnyBlockArguments(funcType)) |
| 555 | RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND); |
| 556 | } |
| 557 | |
| 558 | static bool IsHeaderFile(const std::string &Filename) { |
| 559 | std::string::size_type DotPos = Filename.rfind('.'); |
| 560 | |
| 561 | if (DotPos == std::string::npos) { |
| 562 | // no file extension |
| 563 | return false; |
| 564 | } |
| 565 | |
| 566 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 567 | // C header: .h |
| 568 | // C++ header: .hh or .H; |
| 569 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
| 570 | } |
| 571 | |
| 572 | RewriteModernObjC::RewriteModernObjC(std::string inFile, raw_ostream* OS, |
| 573 | DiagnosticsEngine &D, const LangOptions &LOpts, |
| 574 | bool silenceMacroWarn) |
| 575 | : Diags(D), LangOpts(LOpts), InFileName(inFile), OutFile(OS), |
| 576 | SilenceRewriteMacroWarning(silenceMacroWarn) { |
| 577 | IsHeader = IsHeaderFile(inFile); |
| 578 | RewriteFailedDiag = Diags.getCustomDiagID(DiagnosticsEngine::Warning, |
| 579 | "rewriting sub-expression within a macro (may not be correct)"); |
Fariborz Jahanian | d13c2c2 | 2012-03-22 19:54:39 +0000 | [diff] [blame] | 580 | // FIXME. This should be an error. But if block is not called, it is OK. And it |
| 581 | // may break including some headers. |
| 582 | GlobalBlockRewriteFailedDiag = Diags.getCustomDiagID(DiagnosticsEngine::Warning, |
| 583 | "rewriting block literal declared in global scope is not implemented"); |
| 584 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 585 | TryFinallyContainsReturnDiag = Diags.getCustomDiagID( |
| 586 | DiagnosticsEngine::Warning, |
| 587 | "rewriter doesn't support user-specified control flow semantics " |
| 588 | "for @try/@finally (code may not execute properly)"); |
| 589 | } |
| 590 | |
| 591 | ASTConsumer *clang::CreateModernObjCRewriter(const std::string& InFile, |
| 592 | raw_ostream* OS, |
| 593 | DiagnosticsEngine &Diags, |
| 594 | const LangOptions &LOpts, |
| 595 | bool SilenceRewriteMacroWarning) { |
| 596 | return new RewriteModernObjC(InFile, OS, Diags, LOpts, SilenceRewriteMacroWarning); |
| 597 | } |
| 598 | |
| 599 | void RewriteModernObjC::InitializeCommon(ASTContext &context) { |
| 600 | Context = &context; |
| 601 | SM = &Context->getSourceManager(); |
| 602 | TUDecl = Context->getTranslationUnitDecl(); |
| 603 | MsgSendFunctionDecl = 0; |
| 604 | MsgSendSuperFunctionDecl = 0; |
| 605 | MsgSendStretFunctionDecl = 0; |
| 606 | MsgSendSuperStretFunctionDecl = 0; |
| 607 | MsgSendFpretFunctionDecl = 0; |
| 608 | GetClassFunctionDecl = 0; |
| 609 | GetMetaClassFunctionDecl = 0; |
| 610 | GetSuperClassFunctionDecl = 0; |
| 611 | SelGetUidFunctionDecl = 0; |
| 612 | CFStringFunctionDecl = 0; |
| 613 | ConstantStringClassReference = 0; |
| 614 | NSStringRecord = 0; |
| 615 | CurMethodDef = 0; |
| 616 | CurFunctionDef = 0; |
| 617 | CurFunctionDeclToDeclareForBlock = 0; |
| 618 | GlobalVarDecl = 0; |
Fariborz Jahanian | df474ec | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 619 | GlobalConstructionExp = 0; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 620 | SuperStructDecl = 0; |
| 621 | ProtocolTypeDecl = 0; |
| 622 | ConstantStringDecl = 0; |
| 623 | BcLabelCount = 0; |
| 624 | SuperContructorFunctionDecl = 0; |
| 625 | NumObjCStringLiterals = 0; |
| 626 | PropParentMap = 0; |
| 627 | CurrentBody = 0; |
| 628 | DisableReplaceStmt = false; |
| 629 | objc_impl_method = false; |
| 630 | |
| 631 | // Get the ID and start/end of the main file. |
| 632 | MainFileID = SM->getMainFileID(); |
| 633 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 634 | MainFileStart = MainBuf->getBufferStart(); |
| 635 | MainFileEnd = MainBuf->getBufferEnd(); |
| 636 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 637 | Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOpts()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | //===----------------------------------------------------------------------===// |
| 641 | // Top Level Driver Code |
| 642 | //===----------------------------------------------------------------------===// |
| 643 | |
| 644 | void RewriteModernObjC::HandleTopLevelSingleDecl(Decl *D) { |
| 645 | if (Diags.hasErrorOccurred()) |
| 646 | return; |
| 647 | |
| 648 | // Two cases: either the decl could be in the main file, or it could be in a |
| 649 | // #included file. If the former, rewrite it now. If the later, check to see |
| 650 | // if we rewrote the #include/#import. |
| 651 | SourceLocation Loc = D->getLocation(); |
| 652 | Loc = SM->getExpansionLoc(Loc); |
| 653 | |
| 654 | // If this is for a builtin, ignore it. |
| 655 | if (Loc.isInvalid()) return; |
| 656 | |
| 657 | // Look for built-in declarations that we need to refer during the rewrite. |
| 658 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 659 | RewriteFunctionDecl(FD); |
| 660 | } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) { |
| 661 | // declared in <Foundation/NSString.h> |
| 662 | if (FVD->getName() == "_NSConstantStringClassReference") { |
| 663 | ConstantStringClassReference = FVD; |
| 664 | return; |
| 665 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 666 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
| 667 | RewriteCategoryDecl(CD); |
| 668 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
| 669 | if (PD->isThisDeclarationADefinition()) |
| 670 | RewriteProtocolDecl(PD); |
| 671 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
| 672 | // Recurse into linkage specifications |
| 673 | for (DeclContext::decl_iterator DI = LSD->decls_begin(), |
| 674 | DIEnd = LSD->decls_end(); |
| 675 | DI != DIEnd; ) { |
| 676 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>((*DI))) { |
| 677 | if (!IFace->isThisDeclarationADefinition()) { |
| 678 | SmallVector<Decl *, 8> DG; |
| 679 | SourceLocation StartLoc = IFace->getLocStart(); |
| 680 | do { |
| 681 | if (isa<ObjCInterfaceDecl>(*DI) && |
| 682 | !cast<ObjCInterfaceDecl>(*DI)->isThisDeclarationADefinition() && |
| 683 | StartLoc == (*DI)->getLocStart()) |
| 684 | DG.push_back(*DI); |
| 685 | else |
| 686 | break; |
| 687 | |
| 688 | ++DI; |
| 689 | } while (DI != DIEnd); |
| 690 | RewriteForwardClassDecl(DG); |
| 691 | continue; |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>((*DI))) { |
| 696 | if (!Proto->isThisDeclarationADefinition()) { |
| 697 | SmallVector<Decl *, 8> DG; |
| 698 | SourceLocation StartLoc = Proto->getLocStart(); |
| 699 | do { |
| 700 | if (isa<ObjCProtocolDecl>(*DI) && |
| 701 | !cast<ObjCProtocolDecl>(*DI)->isThisDeclarationADefinition() && |
| 702 | StartLoc == (*DI)->getLocStart()) |
| 703 | DG.push_back(*DI); |
| 704 | else |
| 705 | break; |
| 706 | |
| 707 | ++DI; |
| 708 | } while (DI != DIEnd); |
| 709 | RewriteForwardProtocolDecl(DG); |
| 710 | continue; |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | HandleTopLevelSingleDecl(*DI); |
| 715 | ++DI; |
| 716 | } |
| 717 | } |
| 718 | // If we have a decl in the main file, see if we should rewrite it. |
| 719 | if (SM->isFromMainFile(Loc)) |
| 720 | return HandleDeclInMainFile(D); |
| 721 | } |
| 722 | |
| 723 | //===----------------------------------------------------------------------===// |
| 724 | // Syntactic (non-AST) Rewriting Code |
| 725 | //===----------------------------------------------------------------------===// |
| 726 | |
| 727 | void RewriteModernObjC::RewriteInclude() { |
| 728 | SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID); |
| 729 | StringRef MainBuf = SM->getBufferData(MainFileID); |
| 730 | const char *MainBufStart = MainBuf.begin(); |
| 731 | const char *MainBufEnd = MainBuf.end(); |
| 732 | size_t ImportLen = strlen("import"); |
| 733 | |
| 734 | // Loop over the whole file, looking for includes. |
| 735 | for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { |
| 736 | if (*BufPtr == '#') { |
| 737 | if (++BufPtr == MainBufEnd) |
| 738 | return; |
| 739 | while (*BufPtr == ' ' || *BufPtr == '\t') |
| 740 | if (++BufPtr == MainBufEnd) |
| 741 | return; |
| 742 | if (!strncmp(BufPtr, "import", ImportLen)) { |
| 743 | // replace import with include |
| 744 | SourceLocation ImportLoc = |
| 745 | LocStart.getLocWithOffset(BufPtr-MainBufStart); |
| 746 | ReplaceText(ImportLoc, ImportLen, "include"); |
| 747 | BufPtr += ImportLen; |
| 748 | } |
| 749 | } |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | static std::string getIvarAccessString(ObjCIvarDecl *OID) { |
| 754 | const ObjCInterfaceDecl *ClassDecl = OID->getContainingInterface(); |
| 755 | std::string S; |
| 756 | S = "((struct "; |
| 757 | S += ClassDecl->getIdentifier()->getName(); |
| 758 | S += "_IMPL *)self)->"; |
| 759 | S += OID->getName(); |
| 760 | return S; |
| 761 | } |
| 762 | |
| 763 | void RewriteModernObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 764 | ObjCImplementationDecl *IMD, |
| 765 | ObjCCategoryImplDecl *CID) { |
| 766 | static bool objcGetPropertyDefined = false; |
| 767 | static bool objcSetPropertyDefined = false; |
| 768 | SourceLocation startLoc = PID->getLocStart(); |
| 769 | InsertText(startLoc, "// "); |
| 770 | const char *startBuf = SM->getCharacterData(startLoc); |
| 771 | assert((*startBuf == '@') && "bogus @synthesize location"); |
| 772 | const char *semiBuf = strchr(startBuf, ';'); |
| 773 | assert((*semiBuf == ';') && "@synthesize: can't find ';'"); |
| 774 | SourceLocation onePastSemiLoc = |
| 775 | startLoc.getLocWithOffset(semiBuf-startBuf+1); |
| 776 | |
| 777 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 778 | return; // FIXME: is this correct? |
| 779 | |
| 780 | // Generate the 'getter' function. |
| 781 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 782 | ObjCIvarDecl *OID = PID->getPropertyIvarDecl(); |
| 783 | |
| 784 | if (!OID) |
| 785 | return; |
| 786 | unsigned Attributes = PD->getPropertyAttributes(); |
| 787 | if (!PD->getGetterMethodDecl()->isDefined()) { |
| 788 | bool GenGetProperty = !(Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) && |
| 789 | (Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
| 790 | ObjCPropertyDecl::OBJC_PR_copy)); |
| 791 | std::string Getr; |
| 792 | if (GenGetProperty && !objcGetPropertyDefined) { |
| 793 | objcGetPropertyDefined = true; |
| 794 | // FIXME. Is this attribute correct in all cases? |
| 795 | Getr = "\nextern \"C\" __declspec(dllimport) " |
| 796 | "id objc_getProperty(id, SEL, long, bool);\n"; |
| 797 | } |
| 798 | RewriteObjCMethodDecl(OID->getContainingInterface(), |
| 799 | PD->getGetterMethodDecl(), Getr); |
| 800 | Getr += "{ "; |
| 801 | // Synthesize an explicit cast to gain access to the ivar. |
| 802 | // See objc-act.c:objc_synthesize_new_getter() for details. |
| 803 | if (GenGetProperty) { |
| 804 | // return objc_getProperty(self, _cmd, offsetof(ClassDecl, OID), 1) |
| 805 | Getr += "typedef "; |
| 806 | const FunctionType *FPRetType = 0; |
| 807 | RewriteTypeIntoString(PD->getGetterMethodDecl()->getResultType(), Getr, |
| 808 | FPRetType); |
| 809 | Getr += " _TYPE"; |
| 810 | if (FPRetType) { |
| 811 | Getr += ")"; // close the precedence "scope" for "*". |
| 812 | |
| 813 | // Now, emit the argument types (if any). |
| 814 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)){ |
| 815 | Getr += "("; |
| 816 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 817 | if (i) Getr += ", "; |
| 818 | std::string ParamStr = FT->getArgType(i).getAsString( |
| 819 | Context->getPrintingPolicy()); |
| 820 | Getr += ParamStr; |
| 821 | } |
| 822 | if (FT->isVariadic()) { |
| 823 | if (FT->getNumArgs()) Getr += ", "; |
| 824 | Getr += "..."; |
| 825 | } |
| 826 | Getr += ")"; |
| 827 | } else |
| 828 | Getr += "()"; |
| 829 | } |
| 830 | Getr += ";\n"; |
| 831 | Getr += "return (_TYPE)"; |
| 832 | Getr += "objc_getProperty(self, _cmd, "; |
| 833 | RewriteIvarOffsetComputation(OID, Getr); |
| 834 | Getr += ", 1)"; |
| 835 | } |
| 836 | else |
| 837 | Getr += "return " + getIvarAccessString(OID); |
| 838 | Getr += "; }"; |
| 839 | InsertText(onePastSemiLoc, Getr); |
| 840 | } |
| 841 | |
| 842 | if (PD->isReadOnly() || PD->getSetterMethodDecl()->isDefined()) |
| 843 | return; |
| 844 | |
| 845 | // Generate the 'setter' function. |
| 846 | std::string Setr; |
| 847 | bool GenSetProperty = Attributes & (ObjCPropertyDecl::OBJC_PR_retain | |
| 848 | ObjCPropertyDecl::OBJC_PR_copy); |
| 849 | if (GenSetProperty && !objcSetPropertyDefined) { |
| 850 | objcSetPropertyDefined = true; |
| 851 | // FIXME. Is this attribute correct in all cases? |
| 852 | Setr = "\nextern \"C\" __declspec(dllimport) " |
| 853 | "void objc_setProperty (id, SEL, long, id, bool, bool);\n"; |
| 854 | } |
| 855 | |
| 856 | RewriteObjCMethodDecl(OID->getContainingInterface(), |
| 857 | PD->getSetterMethodDecl(), Setr); |
| 858 | Setr += "{ "; |
| 859 | // Synthesize an explicit cast to initialize the ivar. |
| 860 | // See objc-act.c:objc_synthesize_new_setter() for details. |
| 861 | if (GenSetProperty) { |
| 862 | Setr += "objc_setProperty (self, _cmd, "; |
| 863 | RewriteIvarOffsetComputation(OID, Setr); |
| 864 | Setr += ", (id)"; |
| 865 | Setr += PD->getName(); |
| 866 | Setr += ", "; |
| 867 | if (Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 868 | Setr += "0, "; |
| 869 | else |
| 870 | Setr += "1, "; |
| 871 | if (Attributes & ObjCPropertyDecl::OBJC_PR_copy) |
| 872 | Setr += "1)"; |
| 873 | else |
| 874 | Setr += "0)"; |
| 875 | } |
| 876 | else { |
| 877 | Setr += getIvarAccessString(OID) + " = "; |
| 878 | Setr += PD->getName(); |
| 879 | } |
| 880 | Setr += "; }"; |
| 881 | InsertText(onePastSemiLoc, Setr); |
| 882 | } |
| 883 | |
| 884 | static void RewriteOneForwardClassDecl(ObjCInterfaceDecl *ForwardDecl, |
| 885 | std::string &typedefString) { |
| 886 | typedefString += "#ifndef _REWRITER_typedef_"; |
| 887 | typedefString += ForwardDecl->getNameAsString(); |
| 888 | typedefString += "\n"; |
| 889 | typedefString += "#define _REWRITER_typedef_"; |
| 890 | typedefString += ForwardDecl->getNameAsString(); |
| 891 | typedefString += "\n"; |
| 892 | typedefString += "typedef struct objc_object "; |
| 893 | typedefString += ForwardDecl->getNameAsString(); |
Fariborz Jahanian | c38503b | 2012-03-12 23:58:28 +0000 | [diff] [blame] | 894 | // typedef struct { } _objc_exc_Classname; |
| 895 | typedefString += ";\ntypedef struct {} _objc_exc_"; |
| 896 | typedefString += ForwardDecl->getNameAsString(); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 897 | typedefString += ";\n#endif\n"; |
| 898 | } |
| 899 | |
| 900 | void RewriteModernObjC::RewriteForwardClassEpilogue(ObjCInterfaceDecl *ClassDecl, |
| 901 | const std::string &typedefString) { |
| 902 | SourceLocation startLoc = ClassDecl->getLocStart(); |
| 903 | const char *startBuf = SM->getCharacterData(startLoc); |
| 904 | const char *semiPtr = strchr(startBuf, ';'); |
| 905 | // Replace the @class with typedefs corresponding to the classes. |
| 906 | ReplaceText(startLoc, semiPtr-startBuf+1, typedefString); |
| 907 | } |
| 908 | |
| 909 | void RewriteModernObjC::RewriteForwardClassDecl(DeclGroupRef D) { |
| 910 | std::string typedefString; |
| 911 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
| 912 | ObjCInterfaceDecl *ForwardDecl = cast<ObjCInterfaceDecl>(*I); |
| 913 | if (I == D.begin()) { |
| 914 | // Translate to typedef's that forward reference structs with the same name |
| 915 | // as the class. As a convenience, we include the original declaration |
| 916 | // as a comment. |
| 917 | typedefString += "// @class "; |
| 918 | typedefString += ForwardDecl->getNameAsString(); |
| 919 | typedefString += ";\n"; |
| 920 | } |
| 921 | RewriteOneForwardClassDecl(ForwardDecl, typedefString); |
| 922 | } |
| 923 | DeclGroupRef::iterator I = D.begin(); |
| 924 | RewriteForwardClassEpilogue(cast<ObjCInterfaceDecl>(*I), typedefString); |
| 925 | } |
| 926 | |
| 927 | void RewriteModernObjC::RewriteForwardClassDecl( |
| 928 | const llvm::SmallVector<Decl*, 8> &D) { |
| 929 | std::string typedefString; |
| 930 | for (unsigned i = 0; i < D.size(); i++) { |
| 931 | ObjCInterfaceDecl *ForwardDecl = cast<ObjCInterfaceDecl>(D[i]); |
| 932 | if (i == 0) { |
| 933 | typedefString += "// @class "; |
| 934 | typedefString += ForwardDecl->getNameAsString(); |
| 935 | typedefString += ";\n"; |
| 936 | } |
| 937 | RewriteOneForwardClassDecl(ForwardDecl, typedefString); |
| 938 | } |
| 939 | RewriteForwardClassEpilogue(cast<ObjCInterfaceDecl>(D[0]), typedefString); |
| 940 | } |
| 941 | |
| 942 | void RewriteModernObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) { |
| 943 | // When method is a synthesized one, such as a getter/setter there is |
| 944 | // nothing to rewrite. |
| 945 | if (Method->isImplicit()) |
| 946 | return; |
| 947 | SourceLocation LocStart = Method->getLocStart(); |
| 948 | SourceLocation LocEnd = Method->getLocEnd(); |
| 949 | |
| 950 | if (SM->getExpansionLineNumber(LocEnd) > |
| 951 | SM->getExpansionLineNumber(LocStart)) { |
| 952 | InsertText(LocStart, "#if 0\n"); |
| 953 | ReplaceText(LocEnd, 1, ";\n#endif\n"); |
| 954 | } else { |
| 955 | InsertText(LocStart, "// "); |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | void RewriteModernObjC::RewriteProperty(ObjCPropertyDecl *prop) { |
| 960 | SourceLocation Loc = prop->getAtLoc(); |
| 961 | |
| 962 | ReplaceText(Loc, 0, "// "); |
| 963 | // FIXME: handle properties that are declared across multiple lines. |
| 964 | } |
| 965 | |
| 966 | void RewriteModernObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
| 967 | SourceLocation LocStart = CatDecl->getLocStart(); |
| 968 | |
| 969 | // FIXME: handle category headers that are declared across multiple lines. |
| 970 | ReplaceText(LocStart, 0, "// "); |
Fariborz Jahanian | af30029 | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 971 | if (CatDecl->getIvarLBraceLoc().isValid()) |
| 972 | InsertText(CatDecl->getIvarLBraceLoc(), "// "); |
Fariborz Jahanian | d2aea12 | 2012-02-19 19:00:05 +0000 | [diff] [blame] | 973 | for (ObjCCategoryDecl::ivar_iterator |
| 974 | I = CatDecl->ivar_begin(), E = CatDecl->ivar_end(); I != E; ++I) { |
| 975 | ObjCIvarDecl *Ivar = (*I); |
| 976 | SourceLocation LocStart = Ivar->getLocStart(); |
| 977 | ReplaceText(LocStart, 0, "// "); |
| 978 | } |
Fariborz Jahanian | af30029 | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 979 | if (CatDecl->getIvarRBraceLoc().isValid()) |
| 980 | InsertText(CatDecl->getIvarRBraceLoc(), "// "); |
| 981 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 982 | for (ObjCCategoryDecl::prop_iterator I = CatDecl->prop_begin(), |
| 983 | E = CatDecl->prop_end(); I != E; ++I) |
| 984 | RewriteProperty(*I); |
| 985 | |
| 986 | for (ObjCCategoryDecl::instmeth_iterator |
| 987 | I = CatDecl->instmeth_begin(), E = CatDecl->instmeth_end(); |
| 988 | I != E; ++I) |
| 989 | RewriteMethodDeclaration(*I); |
| 990 | for (ObjCCategoryDecl::classmeth_iterator |
| 991 | I = CatDecl->classmeth_begin(), E = CatDecl->classmeth_end(); |
| 992 | I != E; ++I) |
| 993 | RewriteMethodDeclaration(*I); |
| 994 | |
| 995 | // Lastly, comment out the @end. |
| 996 | ReplaceText(CatDecl->getAtEndRange().getBegin(), |
| 997 | strlen("@end"), "/* @end */"); |
| 998 | } |
| 999 | |
| 1000 | void RewriteModernObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
| 1001 | SourceLocation LocStart = PDecl->getLocStart(); |
| 1002 | assert(PDecl->isThisDeclarationADefinition()); |
| 1003 | |
| 1004 | // FIXME: handle protocol headers that are declared across multiple lines. |
| 1005 | ReplaceText(LocStart, 0, "// "); |
| 1006 | |
| 1007 | for (ObjCProtocolDecl::instmeth_iterator |
| 1008 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
| 1009 | I != E; ++I) |
| 1010 | RewriteMethodDeclaration(*I); |
| 1011 | for (ObjCProtocolDecl::classmeth_iterator |
| 1012 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
| 1013 | I != E; ++I) |
| 1014 | RewriteMethodDeclaration(*I); |
| 1015 | |
| 1016 | for (ObjCInterfaceDecl::prop_iterator I = PDecl->prop_begin(), |
| 1017 | E = PDecl->prop_end(); I != E; ++I) |
| 1018 | RewriteProperty(*I); |
| 1019 | |
| 1020 | // Lastly, comment out the @end. |
| 1021 | SourceLocation LocEnd = PDecl->getAtEndRange().getBegin(); |
| 1022 | ReplaceText(LocEnd, strlen("@end"), "/* @end */"); |
| 1023 | |
| 1024 | // Must comment out @optional/@required |
| 1025 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1026 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 1027 | for (const char *p = startBuf; p < endBuf; p++) { |
| 1028 | if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { |
| 1029 | SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf); |
| 1030 | ReplaceText(OptionalLoc, strlen("@optional"), "/* @optional */"); |
| 1031 | |
| 1032 | } |
| 1033 | else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { |
| 1034 | SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf); |
| 1035 | ReplaceText(OptionalLoc, strlen("@required"), "/* @required */"); |
| 1036 | |
| 1037 | } |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | void RewriteModernObjC::RewriteForwardProtocolDecl(DeclGroupRef D) { |
| 1042 | SourceLocation LocStart = (*D.begin())->getLocStart(); |
| 1043 | if (LocStart.isInvalid()) |
| 1044 | llvm_unreachable("Invalid SourceLocation"); |
| 1045 | // FIXME: handle forward protocol that are declared across multiple lines. |
| 1046 | ReplaceText(LocStart, 0, "// "); |
| 1047 | } |
| 1048 | |
| 1049 | void |
| 1050 | RewriteModernObjC::RewriteForwardProtocolDecl(const llvm::SmallVector<Decl*, 8> &DG) { |
| 1051 | SourceLocation LocStart = DG[0]->getLocStart(); |
| 1052 | if (LocStart.isInvalid()) |
| 1053 | llvm_unreachable("Invalid SourceLocation"); |
| 1054 | // FIXME: handle forward protocol that are declared across multiple lines. |
| 1055 | ReplaceText(LocStart, 0, "// "); |
| 1056 | } |
| 1057 | |
| 1058 | void RewriteModernObjC::RewriteTypeIntoString(QualType T, std::string &ResultStr, |
| 1059 | const FunctionType *&FPRetType) { |
| 1060 | if (T->isObjCQualifiedIdType()) |
| 1061 | ResultStr += "id"; |
| 1062 | else if (T->isFunctionPointerType() || |
| 1063 | T->isBlockPointerType()) { |
| 1064 | // needs special handling, since pointer-to-functions have special |
| 1065 | // syntax (where a decaration models use). |
| 1066 | QualType retType = T; |
| 1067 | QualType PointeeTy; |
| 1068 | if (const PointerType* PT = retType->getAs<PointerType>()) |
| 1069 | PointeeTy = PT->getPointeeType(); |
| 1070 | else if (const BlockPointerType *BPT = retType->getAs<BlockPointerType>()) |
| 1071 | PointeeTy = BPT->getPointeeType(); |
| 1072 | if ((FPRetType = PointeeTy->getAs<FunctionType>())) { |
| 1073 | ResultStr += FPRetType->getResultType().getAsString( |
| 1074 | Context->getPrintingPolicy()); |
| 1075 | ResultStr += "(*"; |
| 1076 | } |
| 1077 | } else |
| 1078 | ResultStr += T.getAsString(Context->getPrintingPolicy()); |
| 1079 | } |
| 1080 | |
| 1081 | void RewriteModernObjC::RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl, |
| 1082 | ObjCMethodDecl *OMD, |
| 1083 | std::string &ResultStr) { |
| 1084 | //fprintf(stderr,"In RewriteObjCMethodDecl\n"); |
| 1085 | const FunctionType *FPRetType = 0; |
| 1086 | ResultStr += "\nstatic "; |
| 1087 | RewriteTypeIntoString(OMD->getResultType(), ResultStr, FPRetType); |
| 1088 | ResultStr += " "; |
| 1089 | |
| 1090 | // Unique method name |
| 1091 | std::string NameStr; |
| 1092 | |
| 1093 | if (OMD->isInstanceMethod()) |
| 1094 | NameStr += "_I_"; |
| 1095 | else |
| 1096 | NameStr += "_C_"; |
| 1097 | |
| 1098 | NameStr += IDecl->getNameAsString(); |
| 1099 | NameStr += "_"; |
| 1100 | |
| 1101 | if (ObjCCategoryImplDecl *CID = |
| 1102 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
| 1103 | NameStr += CID->getNameAsString(); |
| 1104 | NameStr += "_"; |
| 1105 | } |
| 1106 | // Append selector names, replacing ':' with '_' |
| 1107 | { |
| 1108 | std::string selString = OMD->getSelector().getAsString(); |
| 1109 | int len = selString.size(); |
| 1110 | for (int i = 0; i < len; i++) |
| 1111 | if (selString[i] == ':') |
| 1112 | selString[i] = '_'; |
| 1113 | NameStr += selString; |
| 1114 | } |
| 1115 | // Remember this name for metadata emission |
| 1116 | MethodInternalNames[OMD] = NameStr; |
| 1117 | ResultStr += NameStr; |
| 1118 | |
| 1119 | // Rewrite arguments |
| 1120 | ResultStr += "("; |
| 1121 | |
| 1122 | // invisible arguments |
| 1123 | if (OMD->isInstanceMethod()) { |
| 1124 | QualType selfTy = Context->getObjCInterfaceType(IDecl); |
| 1125 | selfTy = Context->getPointerType(selfTy); |
| 1126 | if (!LangOpts.MicrosoftExt) { |
| 1127 | if (ObjCSynthesizedStructs.count(const_cast<ObjCInterfaceDecl*>(IDecl))) |
| 1128 | ResultStr += "struct "; |
| 1129 | } |
| 1130 | // When rewriting for Microsoft, explicitly omit the structure name. |
| 1131 | ResultStr += IDecl->getNameAsString(); |
| 1132 | ResultStr += " *"; |
| 1133 | } |
| 1134 | else |
| 1135 | ResultStr += Context->getObjCClassType().getAsString( |
| 1136 | Context->getPrintingPolicy()); |
| 1137 | |
| 1138 | ResultStr += " self, "; |
| 1139 | ResultStr += Context->getObjCSelType().getAsString(Context->getPrintingPolicy()); |
| 1140 | ResultStr += " _cmd"; |
| 1141 | |
| 1142 | // Method arguments. |
| 1143 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 1144 | E = OMD->param_end(); PI != E; ++PI) { |
| 1145 | ParmVarDecl *PDecl = *PI; |
| 1146 | ResultStr += ", "; |
| 1147 | if (PDecl->getType()->isObjCQualifiedIdType()) { |
| 1148 | ResultStr += "id "; |
| 1149 | ResultStr += PDecl->getNameAsString(); |
| 1150 | } else { |
| 1151 | std::string Name = PDecl->getNameAsString(); |
| 1152 | QualType QT = PDecl->getType(); |
| 1153 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 2610f90 | 2012-03-27 16:42:20 +0000 | [diff] [blame] | 1154 | (void)convertBlockPointerToFunctionPointer(QT); |
| 1155 | QT.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1156 | ResultStr += Name; |
| 1157 | } |
| 1158 | } |
| 1159 | if (OMD->isVariadic()) |
| 1160 | ResultStr += ", ..."; |
| 1161 | ResultStr += ") "; |
| 1162 | |
| 1163 | if (FPRetType) { |
| 1164 | ResultStr += ")"; // close the precedence "scope" for "*". |
| 1165 | |
| 1166 | // Now, emit the argument types (if any). |
| 1167 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) { |
| 1168 | ResultStr += "("; |
| 1169 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 1170 | if (i) ResultStr += ", "; |
| 1171 | std::string ParamStr = FT->getArgType(i).getAsString( |
| 1172 | Context->getPrintingPolicy()); |
| 1173 | ResultStr += ParamStr; |
| 1174 | } |
| 1175 | if (FT->isVariadic()) { |
| 1176 | if (FT->getNumArgs()) ResultStr += ", "; |
| 1177 | ResultStr += "..."; |
| 1178 | } |
| 1179 | ResultStr += ")"; |
| 1180 | } else { |
| 1181 | ResultStr += "()"; |
| 1182 | } |
| 1183 | } |
| 1184 | } |
| 1185 | void RewriteModernObjC::RewriteImplementationDecl(Decl *OID) { |
| 1186 | ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID); |
| 1187 | ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID); |
| 1188 | |
Fariborz Jahanian | d2aea12 | 2012-02-19 19:00:05 +0000 | [diff] [blame] | 1189 | if (IMD) { |
| 1190 | InsertText(IMD->getLocStart(), "// "); |
Fariborz Jahanian | af30029 | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 1191 | if (IMD->getIvarLBraceLoc().isValid()) |
| 1192 | InsertText(IMD->getIvarLBraceLoc(), "// "); |
| 1193 | for (ObjCImplementationDecl::ivar_iterator |
| 1194 | I = IMD->ivar_begin(), E = IMD->ivar_end(); I != E; ++I) { |
| 1195 | ObjCIvarDecl *Ivar = (*I); |
| 1196 | SourceLocation LocStart = Ivar->getLocStart(); |
| 1197 | ReplaceText(LocStart, 0, "// "); |
Fariborz Jahanian | d2aea12 | 2012-02-19 19:00:05 +0000 | [diff] [blame] | 1198 | } |
Fariborz Jahanian | af30029 | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 1199 | if (IMD->getIvarRBraceLoc().isValid()) |
| 1200 | InsertText(IMD->getIvarRBraceLoc(), "// "); |
Fariborz Jahanian | d2aea12 | 2012-02-19 19:00:05 +0000 | [diff] [blame] | 1201 | } |
| 1202 | else |
| 1203 | InsertText(CID->getLocStart(), "// "); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1204 | |
| 1205 | for (ObjCCategoryImplDecl::instmeth_iterator |
| 1206 | I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(), |
| 1207 | E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); |
| 1208 | I != E; ++I) { |
| 1209 | std::string ResultStr; |
| 1210 | ObjCMethodDecl *OMD = *I; |
| 1211 | RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr); |
| 1212 | SourceLocation LocStart = OMD->getLocStart(); |
| 1213 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
| 1214 | |
| 1215 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1216 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 1217 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
| 1218 | } |
| 1219 | |
| 1220 | for (ObjCCategoryImplDecl::classmeth_iterator |
| 1221 | I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(), |
| 1222 | E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); |
| 1223 | I != E; ++I) { |
| 1224 | std::string ResultStr; |
| 1225 | ObjCMethodDecl *OMD = *I; |
| 1226 | RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr); |
| 1227 | SourceLocation LocStart = OMD->getLocStart(); |
| 1228 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
| 1229 | |
| 1230 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1231 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 1232 | ReplaceText(LocStart, endBuf-startBuf, ResultStr); |
| 1233 | } |
| 1234 | for (ObjCCategoryImplDecl::propimpl_iterator |
| 1235 | I = IMD ? IMD->propimpl_begin() : CID->propimpl_begin(), |
| 1236 | E = IMD ? IMD->propimpl_end() : CID->propimpl_end(); |
| 1237 | I != E; ++I) { |
| 1238 | RewritePropertyImplDecl(*I, IMD, CID); |
| 1239 | } |
| 1240 | |
| 1241 | InsertText(IMD ? IMD->getLocEnd() : CID->getLocEnd(), "// "); |
| 1242 | } |
| 1243 | |
| 1244 | void RewriteModernObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { |
Fariborz Jahanian | 8f1fed0 | 2012-02-11 20:10:52 +0000 | [diff] [blame] | 1245 | // Do not synthesize more than once. |
| 1246 | if (ObjCSynthesizedStructs.count(ClassDecl)) |
| 1247 | return; |
| 1248 | // Make sure super class's are written before current class is written. |
| 1249 | ObjCInterfaceDecl *SuperClass = ClassDecl->getSuperClass(); |
| 1250 | while (SuperClass) { |
| 1251 | RewriteInterfaceDecl(SuperClass); |
| 1252 | SuperClass = SuperClass->getSuperClass(); |
| 1253 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1254 | std::string ResultStr; |
Fariborz Jahanian | cf4c60f | 2012-02-17 22:20:12 +0000 | [diff] [blame] | 1255 | if (!ObjCWrittenInterfaces.count(ClassDecl->getCanonicalDecl())) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1256 | // we haven't seen a forward decl - generate a typedef. |
Fariborz Jahanian | c38503b | 2012-03-12 23:58:28 +0000 | [diff] [blame] | 1257 | RewriteOneForwardClassDecl(ClassDecl, ResultStr); |
Fariborz Jahanian | 72c88f1 | 2012-02-22 18:13:25 +0000 | [diff] [blame] | 1258 | RewriteIvarOffsetSymbols(ClassDecl, ResultStr); |
| 1259 | |
Fariborz Jahanian | 4339bb3 | 2012-02-15 22:01:47 +0000 | [diff] [blame] | 1260 | RewriteObjCInternalStruct(ClassDecl, ResultStr); |
Fariborz Jahanian | cf4c60f | 2012-02-17 22:20:12 +0000 | [diff] [blame] | 1261 | // Mark this typedef as having been written into its c++ equivalent. |
| 1262 | ObjCWrittenInterfaces.insert(ClassDecl->getCanonicalDecl()); |
Fariborz Jahanian | 4339bb3 | 2012-02-15 22:01:47 +0000 | [diff] [blame] | 1263 | |
| 1264 | for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(), |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1265 | E = ClassDecl->prop_end(); I != E; ++I) |
Fariborz Jahanian | 4339bb3 | 2012-02-15 22:01:47 +0000 | [diff] [blame] | 1266 | RewriteProperty(*I); |
| 1267 | for (ObjCInterfaceDecl::instmeth_iterator |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1268 | I = ClassDecl->instmeth_begin(), E = ClassDecl->instmeth_end(); |
Fariborz Jahanian | 4339bb3 | 2012-02-15 22:01:47 +0000 | [diff] [blame] | 1269 | I != E; ++I) |
| 1270 | RewriteMethodDeclaration(*I); |
| 1271 | for (ObjCInterfaceDecl::classmeth_iterator |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1272 | I = ClassDecl->classmeth_begin(), E = ClassDecl->classmeth_end(); |
Fariborz Jahanian | 4339bb3 | 2012-02-15 22:01:47 +0000 | [diff] [blame] | 1273 | I != E; ++I) |
| 1274 | RewriteMethodDeclaration(*I); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1275 | |
Fariborz Jahanian | 4339bb3 | 2012-02-15 22:01:47 +0000 | [diff] [blame] | 1276 | // Lastly, comment out the @end. |
| 1277 | ReplaceText(ClassDecl->getAtEndRange().getBegin(), strlen("@end"), |
| 1278 | "/* @end */"); |
| 1279 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | Stmt *RewriteModernObjC::RewritePropertyOrImplicitSetter(PseudoObjectExpr *PseudoOp) { |
| 1283 | SourceRange OldRange = PseudoOp->getSourceRange(); |
| 1284 | |
| 1285 | // We just magically know some things about the structure of this |
| 1286 | // expression. |
| 1287 | ObjCMessageExpr *OldMsg = |
| 1288 | cast<ObjCMessageExpr>(PseudoOp->getSemanticExpr( |
| 1289 | PseudoOp->getNumSemanticExprs() - 1)); |
| 1290 | |
| 1291 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 1292 | // we need to suppress rewriting the sub-statements. |
| 1293 | Expr *Base, *RHS; |
| 1294 | { |
| 1295 | DisableReplaceStmtScope S(*this); |
| 1296 | |
| 1297 | // Rebuild the base expression if we have one. |
| 1298 | Base = 0; |
| 1299 | if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) { |
| 1300 | Base = OldMsg->getInstanceReceiver(); |
| 1301 | Base = cast<OpaqueValueExpr>(Base)->getSourceExpr(); |
| 1302 | Base = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Base)); |
| 1303 | } |
| 1304 | |
| 1305 | // Rebuild the RHS. |
| 1306 | RHS = cast<BinaryOperator>(PseudoOp->getSyntacticForm())->getRHS(); |
| 1307 | RHS = cast<OpaqueValueExpr>(RHS)->getSourceExpr(); |
| 1308 | RHS = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(RHS)); |
| 1309 | } |
| 1310 | |
| 1311 | // TODO: avoid this copy. |
| 1312 | SmallVector<SourceLocation, 1> SelLocs; |
| 1313 | OldMsg->getSelectorLocs(SelLocs); |
| 1314 | |
| 1315 | ObjCMessageExpr *NewMsg = 0; |
| 1316 | switch (OldMsg->getReceiverKind()) { |
| 1317 | case ObjCMessageExpr::Class: |
| 1318 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1319 | OldMsg->getValueKind(), |
| 1320 | OldMsg->getLeftLoc(), |
| 1321 | OldMsg->getClassReceiverTypeInfo(), |
| 1322 | OldMsg->getSelector(), |
| 1323 | SelLocs, |
| 1324 | OldMsg->getMethodDecl(), |
| 1325 | RHS, |
| 1326 | OldMsg->getRightLoc(), |
| 1327 | OldMsg->isImplicit()); |
| 1328 | break; |
| 1329 | |
| 1330 | case ObjCMessageExpr::Instance: |
| 1331 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1332 | OldMsg->getValueKind(), |
| 1333 | OldMsg->getLeftLoc(), |
| 1334 | Base, |
| 1335 | OldMsg->getSelector(), |
| 1336 | SelLocs, |
| 1337 | OldMsg->getMethodDecl(), |
| 1338 | RHS, |
| 1339 | OldMsg->getRightLoc(), |
| 1340 | OldMsg->isImplicit()); |
| 1341 | break; |
| 1342 | |
| 1343 | case ObjCMessageExpr::SuperClass: |
| 1344 | case ObjCMessageExpr::SuperInstance: |
| 1345 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1346 | OldMsg->getValueKind(), |
| 1347 | OldMsg->getLeftLoc(), |
| 1348 | OldMsg->getSuperLoc(), |
| 1349 | OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance, |
| 1350 | OldMsg->getSuperType(), |
| 1351 | OldMsg->getSelector(), |
| 1352 | SelLocs, |
| 1353 | OldMsg->getMethodDecl(), |
| 1354 | RHS, |
| 1355 | OldMsg->getRightLoc(), |
| 1356 | OldMsg->isImplicit()); |
| 1357 | break; |
| 1358 | } |
| 1359 | |
| 1360 | Stmt *Replacement = SynthMessageExpr(NewMsg); |
| 1361 | ReplaceStmtWithRange(PseudoOp, Replacement, OldRange); |
| 1362 | return Replacement; |
| 1363 | } |
| 1364 | |
| 1365 | Stmt *RewriteModernObjC::RewritePropertyOrImplicitGetter(PseudoObjectExpr *PseudoOp) { |
| 1366 | SourceRange OldRange = PseudoOp->getSourceRange(); |
| 1367 | |
| 1368 | // We just magically know some things about the structure of this |
| 1369 | // expression. |
| 1370 | ObjCMessageExpr *OldMsg = |
| 1371 | cast<ObjCMessageExpr>(PseudoOp->getResultExpr()->IgnoreImplicit()); |
| 1372 | |
| 1373 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 1374 | // we need to suppress rewriting the sub-statements. |
| 1375 | Expr *Base = 0; |
| 1376 | { |
| 1377 | DisableReplaceStmtScope S(*this); |
| 1378 | |
| 1379 | // Rebuild the base expression if we have one. |
| 1380 | if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) { |
| 1381 | Base = OldMsg->getInstanceReceiver(); |
| 1382 | Base = cast<OpaqueValueExpr>(Base)->getSourceExpr(); |
| 1383 | Base = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Base)); |
| 1384 | } |
| 1385 | } |
| 1386 | |
| 1387 | // Intentionally empty. |
| 1388 | SmallVector<SourceLocation, 1> SelLocs; |
| 1389 | SmallVector<Expr*, 1> Args; |
| 1390 | |
| 1391 | ObjCMessageExpr *NewMsg = 0; |
| 1392 | switch (OldMsg->getReceiverKind()) { |
| 1393 | case ObjCMessageExpr::Class: |
| 1394 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1395 | OldMsg->getValueKind(), |
| 1396 | OldMsg->getLeftLoc(), |
| 1397 | OldMsg->getClassReceiverTypeInfo(), |
| 1398 | OldMsg->getSelector(), |
| 1399 | SelLocs, |
| 1400 | OldMsg->getMethodDecl(), |
| 1401 | Args, |
| 1402 | OldMsg->getRightLoc(), |
| 1403 | OldMsg->isImplicit()); |
| 1404 | break; |
| 1405 | |
| 1406 | case ObjCMessageExpr::Instance: |
| 1407 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1408 | OldMsg->getValueKind(), |
| 1409 | OldMsg->getLeftLoc(), |
| 1410 | Base, |
| 1411 | OldMsg->getSelector(), |
| 1412 | SelLocs, |
| 1413 | OldMsg->getMethodDecl(), |
| 1414 | Args, |
| 1415 | OldMsg->getRightLoc(), |
| 1416 | OldMsg->isImplicit()); |
| 1417 | break; |
| 1418 | |
| 1419 | case ObjCMessageExpr::SuperClass: |
| 1420 | case ObjCMessageExpr::SuperInstance: |
| 1421 | NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(), |
| 1422 | OldMsg->getValueKind(), |
| 1423 | OldMsg->getLeftLoc(), |
| 1424 | OldMsg->getSuperLoc(), |
| 1425 | OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance, |
| 1426 | OldMsg->getSuperType(), |
| 1427 | OldMsg->getSelector(), |
| 1428 | SelLocs, |
| 1429 | OldMsg->getMethodDecl(), |
| 1430 | Args, |
| 1431 | OldMsg->getRightLoc(), |
| 1432 | OldMsg->isImplicit()); |
| 1433 | break; |
| 1434 | } |
| 1435 | |
| 1436 | Stmt *Replacement = SynthMessageExpr(NewMsg); |
| 1437 | ReplaceStmtWithRange(PseudoOp, Replacement, OldRange); |
| 1438 | return Replacement; |
| 1439 | } |
| 1440 | |
| 1441 | /// SynthCountByEnumWithState - To print: |
| 1442 | /// ((unsigned int (*) |
| 1443 | /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
| 1444 | /// (void *)objc_msgSend)((id)l_collection, |
| 1445 | /// sel_registerName( |
| 1446 | /// "countByEnumeratingWithState:objects:count:"), |
| 1447 | /// &enumState, |
| 1448 | /// (id *)__rw_items, (unsigned int)16) |
| 1449 | /// |
| 1450 | void RewriteModernObjC::SynthCountByEnumWithState(std::string &buf) { |
| 1451 | buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, " |
| 1452 | "id *, unsigned int))(void *)objc_msgSend)"; |
| 1453 | buf += "\n\t\t"; |
| 1454 | buf += "((id)l_collection,\n\t\t"; |
| 1455 | buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; |
| 1456 | buf += "\n\t\t"; |
| 1457 | buf += "&enumState, " |
| 1458 | "(id *)__rw_items, (unsigned int)16)"; |
| 1459 | } |
| 1460 | |
| 1461 | /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach |
| 1462 | /// statement to exit to its outer synthesized loop. |
| 1463 | /// |
| 1464 | Stmt *RewriteModernObjC::RewriteBreakStmt(BreakStmt *S) { |
| 1465 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1466 | return S; |
| 1467 | // replace break with goto __break_label |
| 1468 | std::string buf; |
| 1469 | |
| 1470 | SourceLocation startLoc = S->getLocStart(); |
| 1471 | buf = "goto __break_label_"; |
| 1472 | buf += utostr(ObjCBcLabelNo.back()); |
| 1473 | ReplaceText(startLoc, strlen("break"), buf); |
| 1474 | |
| 1475 | return 0; |
| 1476 | } |
| 1477 | |
| 1478 | /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach |
| 1479 | /// statement to continue with its inner synthesized loop. |
| 1480 | /// |
| 1481 | Stmt *RewriteModernObjC::RewriteContinueStmt(ContinueStmt *S) { |
| 1482 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1483 | return S; |
| 1484 | // replace continue with goto __continue_label |
| 1485 | std::string buf; |
| 1486 | |
| 1487 | SourceLocation startLoc = S->getLocStart(); |
| 1488 | buf = "goto __continue_label_"; |
| 1489 | buf += utostr(ObjCBcLabelNo.back()); |
| 1490 | ReplaceText(startLoc, strlen("continue"), buf); |
| 1491 | |
| 1492 | return 0; |
| 1493 | } |
| 1494 | |
| 1495 | /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. |
| 1496 | /// It rewrites: |
| 1497 | /// for ( type elem in collection) { stmts; } |
| 1498 | |
| 1499 | /// Into: |
| 1500 | /// { |
| 1501 | /// type elem; |
| 1502 | /// struct __objcFastEnumerationState enumState = { 0 }; |
| 1503 | /// id __rw_items[16]; |
| 1504 | /// id l_collection = (id)collection; |
| 1505 | /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1506 | /// objects:__rw_items count:16]; |
| 1507 | /// if (limit) { |
| 1508 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1509 | /// do { |
| 1510 | /// unsigned long counter = 0; |
| 1511 | /// do { |
| 1512 | /// if (startMutations != *enumState.mutationsPtr) |
| 1513 | /// objc_enumerationMutation(l_collection); |
| 1514 | /// elem = (type)enumState.itemsPtr[counter++]; |
| 1515 | /// stmts; |
| 1516 | /// __continue_label: ; |
| 1517 | /// } while (counter < limit); |
| 1518 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
| 1519 | /// objects:__rw_items count:16]); |
| 1520 | /// elem = nil; |
| 1521 | /// __break_label: ; |
| 1522 | /// } |
| 1523 | /// else |
| 1524 | /// elem = nil; |
| 1525 | /// } |
| 1526 | /// |
| 1527 | Stmt *RewriteModernObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
| 1528 | SourceLocation OrigEnd) { |
| 1529 | assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); |
| 1530 | assert(isa<ObjCForCollectionStmt>(Stmts.back()) && |
| 1531 | "ObjCForCollectionStmt Statement stack mismatch"); |
| 1532 | assert(!ObjCBcLabelNo.empty() && |
| 1533 | "ObjCForCollectionStmt - Label No stack empty"); |
| 1534 | |
| 1535 | SourceLocation startLoc = S->getLocStart(); |
| 1536 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1537 | StringRef elementName; |
| 1538 | std::string elementTypeAsString; |
| 1539 | std::string buf; |
| 1540 | buf = "\n{\n\t"; |
| 1541 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { |
| 1542 | // type elem; |
| 1543 | NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl()); |
| 1544 | QualType ElementType = cast<ValueDecl>(D)->getType(); |
| 1545 | if (ElementType->isObjCQualifiedIdType() || |
| 1546 | ElementType->isObjCQualifiedInterfaceType()) |
| 1547 | // Simply use 'id' for all qualified types. |
| 1548 | elementTypeAsString = "id"; |
| 1549 | else |
| 1550 | elementTypeAsString = ElementType.getAsString(Context->getPrintingPolicy()); |
| 1551 | buf += elementTypeAsString; |
| 1552 | buf += " "; |
| 1553 | elementName = D->getName(); |
| 1554 | buf += elementName; |
| 1555 | buf += ";\n\t"; |
| 1556 | } |
| 1557 | else { |
| 1558 | DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); |
| 1559 | elementName = DR->getDecl()->getName(); |
| 1560 | ValueDecl *VD = cast<ValueDecl>(DR->getDecl()); |
| 1561 | if (VD->getType()->isObjCQualifiedIdType() || |
| 1562 | VD->getType()->isObjCQualifiedInterfaceType()) |
| 1563 | // Simply use 'id' for all qualified types. |
| 1564 | elementTypeAsString = "id"; |
| 1565 | else |
| 1566 | elementTypeAsString = VD->getType().getAsString(Context->getPrintingPolicy()); |
| 1567 | } |
| 1568 | |
| 1569 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1570 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
| 1571 | // id __rw_items[16]; |
| 1572 | buf += "id __rw_items[16];\n\t"; |
| 1573 | // id l_collection = (id) |
| 1574 | buf += "id l_collection = (id)"; |
| 1575 | // Find start location of 'collection' the hard way! |
| 1576 | const char *startCollectionBuf = startBuf; |
| 1577 | startCollectionBuf += 3; // skip 'for' |
| 1578 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1579 | startCollectionBuf++; // skip '(' |
| 1580 | // find 'in' and skip it. |
| 1581 | while (*startCollectionBuf != ' ' || |
| 1582 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1583 | (*(startCollectionBuf+3) != ' ' && |
| 1584 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1585 | startCollectionBuf++; |
| 1586 | startCollectionBuf += 3; |
| 1587 | |
| 1588 | // Replace: "for (type element in" with string constructed thus far. |
| 1589 | ReplaceText(startLoc, startCollectionBuf - startBuf, buf); |
| 1590 | // Replace ')' in for '(' type elem in collection ')' with ';' |
| 1591 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1592 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
| 1593 | SourceLocation lparenLoc = startLoc.getLocWithOffset(rparenBuf-startBuf); |
| 1594 | buf = ";\n\t"; |
| 1595 | |
| 1596 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1597 | // objects:__rw_items count:16]; |
| 1598 | // which is synthesized into: |
| 1599 | // unsigned int limit = |
| 1600 | // ((unsigned int (*) |
| 1601 | // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
| 1602 | // (void *)objc_msgSend)((id)l_collection, |
| 1603 | // sel_registerName( |
| 1604 | // "countByEnumeratingWithState:objects:count:"), |
| 1605 | // (struct __objcFastEnumerationState *)&state, |
| 1606 | // (id *)__rw_items, (unsigned int)16); |
| 1607 | buf += "unsigned long limit =\n\t\t"; |
| 1608 | SynthCountByEnumWithState(buf); |
| 1609 | buf += ";\n\t"; |
| 1610 | /// if (limit) { |
| 1611 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1612 | /// do { |
| 1613 | /// unsigned long counter = 0; |
| 1614 | /// do { |
| 1615 | /// if (startMutations != *enumState.mutationsPtr) |
| 1616 | /// objc_enumerationMutation(l_collection); |
| 1617 | /// elem = (type)enumState.itemsPtr[counter++]; |
| 1618 | buf += "if (limit) {\n\t"; |
| 1619 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1620 | buf += "do {\n\t\t"; |
| 1621 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1622 | buf += "do {\n\t\t\t"; |
| 1623 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1624 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1625 | buf += elementName; |
| 1626 | buf += " = ("; |
| 1627 | buf += elementTypeAsString; |
| 1628 | buf += ")enumState.itemsPtr[counter++];"; |
| 1629 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
| 1630 | ReplaceText(lparenLoc, 1, buf); |
| 1631 | |
| 1632 | /// __continue_label: ; |
| 1633 | /// } while (counter < limit); |
| 1634 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
| 1635 | /// objects:__rw_items count:16]); |
| 1636 | /// elem = nil; |
| 1637 | /// __break_label: ; |
| 1638 | /// } |
| 1639 | /// else |
| 1640 | /// elem = nil; |
| 1641 | /// } |
| 1642 | /// |
| 1643 | buf = ";\n\t"; |
| 1644 | buf += "__continue_label_"; |
| 1645 | buf += utostr(ObjCBcLabelNo.back()); |
| 1646 | buf += ": ;"; |
| 1647 | buf += "\n\t\t"; |
| 1648 | buf += "} while (counter < limit);\n\t"; |
| 1649 | buf += "} while (limit = "; |
| 1650 | SynthCountByEnumWithState(buf); |
| 1651 | buf += ");\n\t"; |
| 1652 | buf += elementName; |
| 1653 | buf += " = (("; |
| 1654 | buf += elementTypeAsString; |
| 1655 | buf += ")0);\n\t"; |
| 1656 | buf += "__break_label_"; |
| 1657 | buf += utostr(ObjCBcLabelNo.back()); |
| 1658 | buf += ": ;\n\t"; |
| 1659 | buf += "}\n\t"; |
| 1660 | buf += "else\n\t\t"; |
| 1661 | buf += elementName; |
| 1662 | buf += " = (("; |
| 1663 | buf += elementTypeAsString; |
| 1664 | buf += ")0);\n\t"; |
| 1665 | buf += "}\n"; |
| 1666 | |
| 1667 | // Insert all these *after* the statement body. |
| 1668 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
| 1669 | if (isa<CompoundStmt>(S->getBody())) { |
| 1670 | SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(1); |
| 1671 | InsertText(endBodyLoc, buf); |
| 1672 | } else { |
| 1673 | /* Need to treat single statements specially. For example: |
| 1674 | * |
| 1675 | * for (A *a in b) if (stuff()) break; |
| 1676 | * for (A *a in b) xxxyy; |
| 1677 | * |
| 1678 | * The following code simply scans ahead to the semi to find the actual end. |
| 1679 | */ |
| 1680 | const char *stmtBuf = SM->getCharacterData(OrigEnd); |
| 1681 | const char *semiBuf = strchr(stmtBuf, ';'); |
| 1682 | assert(semiBuf && "Can't find ';'"); |
| 1683 | SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(semiBuf-stmtBuf+1); |
| 1684 | InsertText(endBodyLoc, buf); |
| 1685 | } |
| 1686 | Stmts.pop_back(); |
| 1687 | ObjCBcLabelNo.pop_back(); |
| 1688 | return 0; |
| 1689 | } |
| 1690 | |
Fariborz Jahanian | 542125f | 2012-03-16 21:33:16 +0000 | [diff] [blame] | 1691 | static void Write_RethrowObject(std::string &buf) { |
| 1692 | buf += "{ struct _FIN { _FIN(id reth) : rethrow(reth) {}\n"; |
| 1693 | buf += "\t~_FIN() { if (rethrow) objc_exception_throw(rethrow); }\n"; |
| 1694 | buf += "\tid rethrow;\n"; |
| 1695 | buf += "\t} _fin_force_rethow(_rethrow);"; |
| 1696 | } |
| 1697 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1698 | /// RewriteObjCSynchronizedStmt - |
| 1699 | /// This routine rewrites @synchronized(expr) stmt; |
| 1700 | /// into: |
| 1701 | /// objc_sync_enter(expr); |
| 1702 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1703 | /// |
| 1704 | Stmt *RewriteModernObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
| 1705 | // Get the start location and compute the semi location. |
| 1706 | SourceLocation startLoc = S->getLocStart(); |
| 1707 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1708 | |
| 1709 | assert((*startBuf == '@') && "bogus @synchronized location"); |
| 1710 | |
| 1711 | std::string buf; |
Fariborz Jahanian | 22e2f85 | 2012-03-17 17:46:02 +0000 | [diff] [blame] | 1712 | buf = "{ id _rethrow = 0; id _sync_obj = "; |
Fariborz Jahanian | 542125f | 2012-03-16 21:33:16 +0000 | [diff] [blame] | 1713 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1714 | const char *lparenBuf = startBuf; |
| 1715 | while (*lparenBuf != '(') lparenBuf++; |
| 1716 | ReplaceText(startLoc, lparenBuf-startBuf+1, buf); |
Fariborz Jahanian | 22e2f85 | 2012-03-17 17:46:02 +0000 | [diff] [blame] | 1717 | |
| 1718 | buf = "; objc_sync_enter(_sync_obj);\n"; |
| 1719 | buf += "try {\n\tstruct _SYNC_EXIT { _SYNC_EXIT(id arg) : sync_exit(arg) {}"; |
| 1720 | buf += "\n\t~_SYNC_EXIT() {objc_sync_exit(sync_exit);}"; |
| 1721 | buf += "\n\tid sync_exit;"; |
| 1722 | buf += "\n\t} _sync_exit(_sync_obj);\n"; |
| 1723 | |
| 1724 | // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since |
| 1725 | // the sync expression is typically a message expression that's already |
| 1726 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1727 | SourceLocation RParenExprLoc = S->getSynchBody()->getLocStart(); |
| 1728 | const char *RParenExprLocBuf = SM->getCharacterData(RParenExprLoc); |
| 1729 | while (*RParenExprLocBuf != ')') RParenExprLocBuf--; |
| 1730 | RParenExprLoc = startLoc.getLocWithOffset(RParenExprLocBuf-startBuf); |
| 1731 | |
| 1732 | SourceLocation LBranceLoc = S->getSynchBody()->getLocStart(); |
| 1733 | const char *LBraceLocBuf = SM->getCharacterData(LBranceLoc); |
| 1734 | assert (*LBraceLocBuf == '{'); |
| 1735 | ReplaceText(RParenExprLoc, (LBraceLocBuf - SM->getCharacterData(RParenExprLoc) + 1), buf); |
Fariborz Jahanian | 542125f | 2012-03-16 21:33:16 +0000 | [diff] [blame] | 1736 | |
Fariborz Jahanian | b655bf0 | 2012-03-16 21:43:45 +0000 | [diff] [blame] | 1737 | SourceLocation startRBraceLoc = S->getSynchBody()->getLocEnd(); |
Matt Beaumont-Gay | 9ab511c | 2012-03-16 22:20:39 +0000 | [diff] [blame] | 1738 | assert((*SM->getCharacterData(startRBraceLoc) == '}') && |
| 1739 | "bogus @synchronized block"); |
Fariborz Jahanian | 542125f | 2012-03-16 21:33:16 +0000 | [diff] [blame] | 1740 | |
| 1741 | buf = "} catch (id e) {_rethrow = e;}\n"; |
| 1742 | Write_RethrowObject(buf); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1743 | buf += "}\n"; |
Fariborz Jahanian | 542125f | 2012-03-16 21:33:16 +0000 | [diff] [blame] | 1744 | buf += "}\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1745 | |
Fariborz Jahanian | b655bf0 | 2012-03-16 21:43:45 +0000 | [diff] [blame] | 1746 | ReplaceText(startRBraceLoc, 1, buf); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1747 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1748 | return 0; |
| 1749 | } |
| 1750 | |
| 1751 | void RewriteModernObjC::WarnAboutReturnGotoStmts(Stmt *S) |
| 1752 | { |
| 1753 | // Perform a bottom up traversal of all children. |
| 1754 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
| 1755 | if (*CI) |
| 1756 | WarnAboutReturnGotoStmts(*CI); |
| 1757 | |
| 1758 | if (isa<ReturnStmt>(S) || isa<GotoStmt>(S)) { |
| 1759 | Diags.Report(Context->getFullLoc(S->getLocStart()), |
| 1760 | TryFinallyContainsReturnDiag); |
| 1761 | } |
| 1762 | return; |
| 1763 | } |
| 1764 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1765 | Stmt *RewriteModernObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Fariborz Jahanian | b122818 | 2012-03-15 22:42:15 +0000 | [diff] [blame] | 1766 | ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt(); |
Fariborz Jahanian | 220419a | 2012-03-15 23:50:33 +0000 | [diff] [blame] | 1767 | bool noCatch = S->getNumCatchStmts() == 0; |
Fariborz Jahanian | b122818 | 2012-03-15 22:42:15 +0000 | [diff] [blame] | 1768 | std::string buf; |
| 1769 | |
| 1770 | if (finalStmt) { |
Fariborz Jahanian | 220419a | 2012-03-15 23:50:33 +0000 | [diff] [blame] | 1771 | if (noCatch) |
| 1772 | buf = "{ id volatile _rethrow = 0;\n"; |
| 1773 | else { |
| 1774 | buf = "{ id volatile _rethrow = 0;\ntry {\n"; |
| 1775 | } |
Fariborz Jahanian | b122818 | 2012-03-15 22:42:15 +0000 | [diff] [blame] | 1776 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1777 | // Get the start location and compute the semi location. |
| 1778 | SourceLocation startLoc = S->getLocStart(); |
| 1779 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1780 | |
| 1781 | assert((*startBuf == '@') && "bogus @try location"); |
Fariborz Jahanian | b122818 | 2012-03-15 22:42:15 +0000 | [diff] [blame] | 1782 | if (finalStmt) |
| 1783 | ReplaceText(startLoc, 1, buf); |
| 1784 | else |
| 1785 | // @try -> try |
| 1786 | ReplaceText(startLoc, 1, ""); |
| 1787 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1788 | for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I) { |
| 1789 | ObjCAtCatchStmt *Catch = S->getCatchStmt(I); |
Fariborz Jahanian | 4c14881 | 2012-03-15 20:11:10 +0000 | [diff] [blame] | 1790 | VarDecl *catchDecl = Catch->getCatchParamDecl(); |
Fariborz Jahanian | c38503b | 2012-03-12 23:58:28 +0000 | [diff] [blame] | 1791 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1792 | startLoc = Catch->getLocStart(); |
Fariborz Jahanian | 4c14881 | 2012-03-15 20:11:10 +0000 | [diff] [blame] | 1793 | bool AtRemoved = false; |
| 1794 | if (catchDecl) { |
| 1795 | QualType t = catchDecl->getType(); |
| 1796 | if (const ObjCObjectPointerType *Ptr = t->getAs<ObjCObjectPointerType>()) { |
| 1797 | // Should be a pointer to a class. |
| 1798 | ObjCInterfaceDecl *IDecl = Ptr->getObjectType()->getInterface(); |
| 1799 | if (IDecl) { |
| 1800 | std::string Result; |
| 1801 | startBuf = SM->getCharacterData(startLoc); |
| 1802 | assert((*startBuf == '@') && "bogus @catch location"); |
| 1803 | SourceLocation rParenLoc = Catch->getRParenLoc(); |
| 1804 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 1805 | |
| 1806 | // _objc_exc_Foo *_e as argument to catch. |
| 1807 | Result = "catch (_objc_exc_"; Result += IDecl->getNameAsString(); |
| 1808 | Result += " *_"; Result += catchDecl->getNameAsString(); |
| 1809 | Result += ")"; |
| 1810 | ReplaceText(startLoc, rParenBuf-startBuf+1, Result); |
| 1811 | // Foo *e = (Foo *)_e; |
| 1812 | Result.clear(); |
| 1813 | Result = "{ "; |
| 1814 | Result += IDecl->getNameAsString(); |
| 1815 | Result += " *"; Result += catchDecl->getNameAsString(); |
| 1816 | Result += " = ("; Result += IDecl->getNameAsString(); Result += "*)"; |
| 1817 | Result += "_"; Result += catchDecl->getNameAsString(); |
| 1818 | |
| 1819 | Result += "; "; |
| 1820 | SourceLocation lBraceLoc = Catch->getCatchBody()->getLocStart(); |
| 1821 | ReplaceText(lBraceLoc, 1, Result); |
| 1822 | AtRemoved = true; |
| 1823 | } |
| 1824 | } |
| 1825 | } |
| 1826 | if (!AtRemoved) |
| 1827 | // @catch -> catch |
| 1828 | ReplaceText(startLoc, 1, ""); |
Fariborz Jahanian | c38503b | 2012-03-12 23:58:28 +0000 | [diff] [blame] | 1829 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1830 | } |
Fariborz Jahanian | 220419a | 2012-03-15 23:50:33 +0000 | [diff] [blame] | 1831 | if (finalStmt) { |
| 1832 | buf.clear(); |
| 1833 | if (noCatch) |
| 1834 | buf = "catch (id e) {_rethrow = e;}\n"; |
| 1835 | else |
| 1836 | buf = "}\ncatch (id e) {_rethrow = e;}\n"; |
| 1837 | |
| 1838 | SourceLocation startFinalLoc = finalStmt->getLocStart(); |
| 1839 | ReplaceText(startFinalLoc, 8, buf); |
| 1840 | Stmt *body = finalStmt->getFinallyBody(); |
| 1841 | SourceLocation startFinalBodyLoc = body->getLocStart(); |
| 1842 | buf.clear(); |
Fariborz Jahanian | 542125f | 2012-03-16 21:33:16 +0000 | [diff] [blame] | 1843 | Write_RethrowObject(buf); |
Fariborz Jahanian | 220419a | 2012-03-15 23:50:33 +0000 | [diff] [blame] | 1844 | ReplaceText(startFinalBodyLoc, 1, buf); |
| 1845 | |
| 1846 | SourceLocation endFinalBodyLoc = body->getLocEnd(); |
| 1847 | ReplaceText(endFinalBodyLoc, 1, "}\n}"); |
Fariborz Jahanian | 22e2f85 | 2012-03-17 17:46:02 +0000 | [diff] [blame] | 1848 | // Now check for any return/continue/go statements within the @try. |
| 1849 | WarnAboutReturnGotoStmts(S->getTryBody()); |
Fariborz Jahanian | 220419a | 2012-03-15 23:50:33 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1852 | return 0; |
| 1853 | } |
| 1854 | |
| 1855 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
| 1856 | // the throw expression is typically a message expression that's already |
| 1857 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1858 | Stmt *RewriteModernObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
| 1859 | // Get the start location and compute the semi location. |
| 1860 | SourceLocation startLoc = S->getLocStart(); |
| 1861 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1862 | |
| 1863 | assert((*startBuf == '@') && "bogus @throw location"); |
| 1864 | |
| 1865 | std::string buf; |
| 1866 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
| 1867 | if (S->getThrowExpr()) |
| 1868 | buf = "objc_exception_throw("; |
Fariborz Jahanian | 4053946 | 2012-03-16 16:52:06 +0000 | [diff] [blame] | 1869 | else |
| 1870 | buf = "throw"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1871 | |
| 1872 | // handle "@ throw" correctly. |
| 1873 | const char *wBuf = strchr(startBuf, 'w'); |
| 1874 | assert((*wBuf == 'w') && "@throw: can't find 'w'"); |
| 1875 | ReplaceText(startLoc, wBuf-startBuf+1, buf); |
| 1876 | |
| 1877 | const char *semiBuf = strchr(startBuf, ';'); |
| 1878 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
| 1879 | SourceLocation semiLoc = startLoc.getLocWithOffset(semiBuf-startBuf); |
Fariborz Jahanian | 4053946 | 2012-03-16 16:52:06 +0000 | [diff] [blame] | 1880 | if (S->getThrowExpr()) |
| 1881 | ReplaceText(semiLoc, 1, ");"); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1882 | return 0; |
| 1883 | } |
| 1884 | |
| 1885 | Stmt *RewriteModernObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
| 1886 | // Create a new string expression. |
| 1887 | QualType StrType = Context->getPointerType(Context->CharTy); |
| 1888 | std::string StrEncoding; |
| 1889 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); |
| 1890 | Expr *Replacement = StringLiteral::Create(*Context, StrEncoding, |
| 1891 | StringLiteral::Ascii, false, |
| 1892 | StrType, SourceLocation()); |
| 1893 | ReplaceStmt(Exp, Replacement); |
| 1894 | |
| 1895 | // Replace this subexpr in the parent. |
| 1896 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
| 1897 | return Replacement; |
| 1898 | } |
| 1899 | |
| 1900 | Stmt *RewriteModernObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
| 1901 | if (!SelGetUidFunctionDecl) |
| 1902 | SynthSelGetUidFunctionDecl(); |
| 1903 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 1904 | // Create a call to sel_registerName("selName"). |
| 1905 | SmallVector<Expr*, 8> SelExprs; |
| 1906 | QualType argType = Context->getPointerType(Context->CharTy); |
| 1907 | SelExprs.push_back(StringLiteral::Create(*Context, |
| 1908 | Exp->getSelector().getAsString(), |
| 1909 | StringLiteral::Ascii, false, |
| 1910 | argType, SourceLocation())); |
| 1911 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 1912 | &SelExprs[0], SelExprs.size()); |
| 1913 | ReplaceStmt(Exp, SelExp); |
| 1914 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
| 1915 | return SelExp; |
| 1916 | } |
| 1917 | |
| 1918 | CallExpr *RewriteModernObjC::SynthesizeCallToFunctionDecl( |
| 1919 | FunctionDecl *FD, Expr **args, unsigned nargs, SourceLocation StartLoc, |
| 1920 | SourceLocation EndLoc) { |
| 1921 | // Get the type, we will need to reference it in a couple spots. |
| 1922 | QualType msgSendType = FD->getType(); |
| 1923 | |
| 1924 | // Create a reference to the objc_msgSend() declaration. |
| 1925 | DeclRefExpr *DRE = |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1926 | new (Context) DeclRefExpr(FD, false, msgSendType, VK_LValue, SourceLocation()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 1927 | |
| 1928 | // Now, we cast the reference to a pointer to the objc_msgSend type. |
| 1929 | QualType pToFunc = Context->getPointerType(msgSendType); |
| 1930 | ImplicitCastExpr *ICE = |
| 1931 | ImplicitCastExpr::Create(*Context, pToFunc, CK_FunctionToPointerDecay, |
| 1932 | DRE, 0, VK_RValue); |
| 1933 | |
| 1934 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
| 1935 | |
| 1936 | CallExpr *Exp = |
| 1937 | new (Context) CallExpr(*Context, ICE, args, nargs, |
| 1938 | FT->getCallResultType(*Context), |
| 1939 | VK_RValue, EndLoc); |
| 1940 | return Exp; |
| 1941 | } |
| 1942 | |
| 1943 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 1944 | const char *&startRef, const char *&endRef) { |
| 1945 | while (startBuf < endBuf) { |
| 1946 | if (*startBuf == '<') |
| 1947 | startRef = startBuf; // mark the start. |
| 1948 | if (*startBuf == '>') { |
| 1949 | if (startRef && *startRef == '<') { |
| 1950 | endRef = startBuf; // mark the end. |
| 1951 | return true; |
| 1952 | } |
| 1953 | return false; |
| 1954 | } |
| 1955 | startBuf++; |
| 1956 | } |
| 1957 | return false; |
| 1958 | } |
| 1959 | |
| 1960 | static void scanToNextArgument(const char *&argRef) { |
| 1961 | int angle = 0; |
| 1962 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 1963 | if (*argRef == '<') |
| 1964 | angle++; |
| 1965 | else if (*argRef == '>') |
| 1966 | angle--; |
| 1967 | argRef++; |
| 1968 | } |
| 1969 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 1970 | } |
| 1971 | |
| 1972 | bool RewriteModernObjC::needToScanForQualifiers(QualType T) { |
| 1973 | if (T->isObjCQualifiedIdType()) |
| 1974 | return true; |
| 1975 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 1976 | if (PT->getPointeeType()->isObjCQualifiedIdType()) |
| 1977 | return true; |
| 1978 | } |
| 1979 | if (T->isObjCObjectPointerType()) { |
| 1980 | T = T->getPointeeType(); |
| 1981 | return T->isObjCQualifiedInterfaceType(); |
| 1982 | } |
| 1983 | if (T->isArrayType()) { |
| 1984 | QualType ElemTy = Context->getBaseElementType(T); |
| 1985 | return needToScanForQualifiers(ElemTy); |
| 1986 | } |
| 1987 | return false; |
| 1988 | } |
| 1989 | |
| 1990 | void RewriteModernObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) { |
| 1991 | QualType Type = E->getType(); |
| 1992 | if (needToScanForQualifiers(Type)) { |
| 1993 | SourceLocation Loc, EndLoc; |
| 1994 | |
| 1995 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) { |
| 1996 | Loc = ECE->getLParenLoc(); |
| 1997 | EndLoc = ECE->getRParenLoc(); |
| 1998 | } else { |
| 1999 | Loc = E->getLocStart(); |
| 2000 | EndLoc = E->getLocEnd(); |
| 2001 | } |
| 2002 | // This will defend against trying to rewrite synthesized expressions. |
| 2003 | if (Loc.isInvalid() || EndLoc.isInvalid()) |
| 2004 | return; |
| 2005 | |
| 2006 | const char *startBuf = SM->getCharacterData(Loc); |
| 2007 | const char *endBuf = SM->getCharacterData(EndLoc); |
| 2008 | const char *startRef = 0, *endRef = 0; |
| 2009 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2010 | // Get the locations of the startRef, endRef. |
| 2011 | SourceLocation LessLoc = Loc.getLocWithOffset(startRef-startBuf); |
| 2012 | SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-startBuf+1); |
| 2013 | // Comment out the protocol references. |
| 2014 | InsertText(LessLoc, "/*"); |
| 2015 | InsertText(GreaterLoc, "*/"); |
| 2016 | } |
| 2017 | } |
| 2018 | } |
| 2019 | |
| 2020 | void RewriteModernObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
| 2021 | SourceLocation Loc; |
| 2022 | QualType Type; |
| 2023 | const FunctionProtoType *proto = 0; |
| 2024 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 2025 | Loc = VD->getLocation(); |
| 2026 | Type = VD->getType(); |
| 2027 | } |
| 2028 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 2029 | Loc = FD->getLocation(); |
| 2030 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 2031 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
| 2032 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
| 2033 | assert(funcType && "missing function type"); |
| 2034 | proto = dyn_cast<FunctionProtoType>(funcType); |
| 2035 | if (!proto) |
| 2036 | return; |
| 2037 | Type = proto->getResultType(); |
| 2038 | } |
| 2039 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(Dcl)) { |
| 2040 | Loc = FD->getLocation(); |
| 2041 | Type = FD->getType(); |
| 2042 | } |
| 2043 | else |
| 2044 | return; |
| 2045 | |
| 2046 | if (needToScanForQualifiers(Type)) { |
| 2047 | // Since types are unique, we need to scan the buffer. |
| 2048 | |
| 2049 | const char *endBuf = SM->getCharacterData(Loc); |
| 2050 | const char *startBuf = endBuf; |
| 2051 | while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) |
| 2052 | startBuf--; // scan backward (from the decl location) for return type. |
| 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-endBuf); |
| 2057 | SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-endBuf+1); |
| 2058 | // Comment out the protocol references. |
| 2059 | InsertText(LessLoc, "/*"); |
| 2060 | InsertText(GreaterLoc, "*/"); |
| 2061 | } |
| 2062 | } |
| 2063 | if (!proto) |
| 2064 | return; // most likely, was a variable |
| 2065 | // Now check arguments. |
| 2066 | const char *startBuf = SM->getCharacterData(Loc); |
| 2067 | const char *startFuncBuf = startBuf; |
| 2068 | for (unsigned i = 0; i < proto->getNumArgs(); i++) { |
| 2069 | if (needToScanForQualifiers(proto->getArgType(i))) { |
| 2070 | // Since types are unique, we need to scan the buffer. |
| 2071 | |
| 2072 | const char *endBuf = startBuf; |
| 2073 | // scan forward (from the decl location) for argument types. |
| 2074 | scanToNextArgument(endBuf); |
| 2075 | const char *startRef = 0, *endRef = 0; |
| 2076 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 2077 | // Get the locations of the startRef, endRef. |
| 2078 | SourceLocation LessLoc = |
| 2079 | Loc.getLocWithOffset(startRef-startFuncBuf); |
| 2080 | SourceLocation GreaterLoc = |
| 2081 | Loc.getLocWithOffset(endRef-startFuncBuf+1); |
| 2082 | // Comment out the protocol references. |
| 2083 | InsertText(LessLoc, "/*"); |
| 2084 | InsertText(GreaterLoc, "*/"); |
| 2085 | } |
| 2086 | startBuf = ++endBuf; |
| 2087 | } |
| 2088 | else { |
| 2089 | // If the function name is derived from a macro expansion, then the |
| 2090 | // argument buffer will not follow the name. Need to speak with Chris. |
| 2091 | while (*startBuf && *startBuf != ')' && *startBuf != ',') |
| 2092 | startBuf++; // scan forward (from the decl location) for argument types. |
| 2093 | startBuf++; |
| 2094 | } |
| 2095 | } |
| 2096 | } |
| 2097 | |
| 2098 | void RewriteModernObjC::RewriteTypeOfDecl(VarDecl *ND) { |
| 2099 | QualType QT = ND->getType(); |
| 2100 | const Type* TypePtr = QT->getAs<Type>(); |
| 2101 | if (!isa<TypeOfExprType>(TypePtr)) |
| 2102 | return; |
| 2103 | while (isa<TypeOfExprType>(TypePtr)) { |
| 2104 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 2105 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 2106 | TypePtr = QT->getAs<Type>(); |
| 2107 | } |
| 2108 | // FIXME. This will not work for multiple declarators; as in: |
| 2109 | // __typeof__(a) b,c,d; |
| 2110 | std::string TypeAsString(QT.getAsString(Context->getPrintingPolicy())); |
| 2111 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
| 2112 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 2113 | if (ND->getInit()) { |
| 2114 | std::string Name(ND->getNameAsString()); |
| 2115 | TypeAsString += " " + Name + " = "; |
| 2116 | Expr *E = ND->getInit(); |
| 2117 | SourceLocation startLoc; |
| 2118 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 2119 | startLoc = ECE->getLParenLoc(); |
| 2120 | else |
| 2121 | startLoc = E->getLocStart(); |
| 2122 | startLoc = SM->getExpansionLoc(startLoc); |
| 2123 | const char *endBuf = SM->getCharacterData(startLoc); |
| 2124 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
| 2125 | } |
| 2126 | else { |
| 2127 | SourceLocation X = ND->getLocEnd(); |
| 2128 | X = SM->getExpansionLoc(X); |
| 2129 | const char *endBuf = SM->getCharacterData(X); |
| 2130 | ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); |
| 2131 | } |
| 2132 | } |
| 2133 | |
| 2134 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
| 2135 | void RewriteModernObjC::SynthSelGetUidFunctionDecl() { |
| 2136 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
| 2137 | SmallVector<QualType, 16> ArgTys; |
| 2138 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
| 2139 | QualType getFuncType = |
| 2140 | getSimpleFunctionType(Context->getObjCSelType(), &ArgTys[0], ArgTys.size()); |
| 2141 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
| 2142 | SourceLocation(), |
| 2143 | SourceLocation(), |
| 2144 | SelGetUidIdent, getFuncType, 0, |
| 2145 | SC_Extern, |
| 2146 | SC_None, false); |
| 2147 | } |
| 2148 | |
| 2149 | void RewriteModernObjC::RewriteFunctionDecl(FunctionDecl *FD) { |
| 2150 | // declared in <objc/objc.h> |
| 2151 | if (FD->getIdentifier() && |
| 2152 | FD->getName() == "sel_registerName") { |
| 2153 | SelGetUidFunctionDecl = FD; |
| 2154 | return; |
| 2155 | } |
| 2156 | RewriteObjCQualifiedInterfaceTypes(FD); |
| 2157 | } |
| 2158 | |
| 2159 | void RewriteModernObjC::RewriteBlockPointerType(std::string& Str, QualType Type) { |
| 2160 | std::string TypeString(Type.getAsString(Context->getPrintingPolicy())); |
| 2161 | const char *argPtr = TypeString.c_str(); |
| 2162 | if (!strchr(argPtr, '^')) { |
| 2163 | Str += TypeString; |
| 2164 | return; |
| 2165 | } |
| 2166 | while (*argPtr) { |
| 2167 | Str += (*argPtr == '^' ? '*' : *argPtr); |
| 2168 | argPtr++; |
| 2169 | } |
| 2170 | } |
| 2171 | |
| 2172 | // FIXME. Consolidate this routine with RewriteBlockPointerType. |
| 2173 | void RewriteModernObjC::RewriteBlockPointerTypeVariable(std::string& Str, |
| 2174 | ValueDecl *VD) { |
| 2175 | QualType Type = VD->getType(); |
| 2176 | std::string TypeString(Type.getAsString(Context->getPrintingPolicy())); |
| 2177 | const char *argPtr = TypeString.c_str(); |
| 2178 | int paren = 0; |
| 2179 | while (*argPtr) { |
| 2180 | switch (*argPtr) { |
| 2181 | case '(': |
| 2182 | Str += *argPtr; |
| 2183 | paren++; |
| 2184 | break; |
| 2185 | case ')': |
| 2186 | Str += *argPtr; |
| 2187 | paren--; |
| 2188 | break; |
| 2189 | case '^': |
| 2190 | Str += '*'; |
| 2191 | if (paren == 1) |
| 2192 | Str += VD->getNameAsString(); |
| 2193 | break; |
| 2194 | default: |
| 2195 | Str += *argPtr; |
| 2196 | break; |
| 2197 | } |
| 2198 | argPtr++; |
| 2199 | } |
| 2200 | } |
| 2201 | |
| 2202 | |
| 2203 | void RewriteModernObjC::RewriteBlockLiteralFunctionDecl(FunctionDecl *FD) { |
| 2204 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
| 2205 | const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); |
| 2206 | const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(funcType); |
| 2207 | if (!proto) |
| 2208 | return; |
| 2209 | QualType Type = proto->getResultType(); |
| 2210 | std::string FdStr = Type.getAsString(Context->getPrintingPolicy()); |
| 2211 | FdStr += " "; |
| 2212 | FdStr += FD->getName(); |
| 2213 | FdStr += "("; |
| 2214 | unsigned numArgs = proto->getNumArgs(); |
| 2215 | for (unsigned i = 0; i < numArgs; i++) { |
| 2216 | QualType ArgType = proto->getArgType(i); |
| 2217 | RewriteBlockPointerType(FdStr, ArgType); |
| 2218 | if (i+1 < numArgs) |
| 2219 | FdStr += ", "; |
| 2220 | } |
| 2221 | FdStr += ");\n"; |
| 2222 | InsertText(FunLocStart, FdStr); |
| 2223 | CurFunctionDeclToDeclareForBlock = 0; |
| 2224 | } |
| 2225 | |
| 2226 | // SynthSuperContructorFunctionDecl - id objc_super(id obj, id super); |
| 2227 | void RewriteModernObjC::SynthSuperContructorFunctionDecl() { |
| 2228 | if (SuperContructorFunctionDecl) |
| 2229 | return; |
| 2230 | IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super"); |
| 2231 | SmallVector<QualType, 16> ArgTys; |
| 2232 | QualType argT = Context->getObjCIdType(); |
| 2233 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2234 | ArgTys.push_back(argT); |
| 2235 | ArgTys.push_back(argT); |
| 2236 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2237 | &ArgTys[0], ArgTys.size()); |
| 2238 | SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
| 2239 | SourceLocation(), |
| 2240 | SourceLocation(), |
| 2241 | msgSendIdent, msgSendType, 0, |
| 2242 | SC_Extern, |
| 2243 | SC_None, false); |
| 2244 | } |
| 2245 | |
| 2246 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
| 2247 | void RewriteModernObjC::SynthMsgSendFunctionDecl() { |
| 2248 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
| 2249 | SmallVector<QualType, 16> ArgTys; |
| 2250 | QualType argT = Context->getObjCIdType(); |
| 2251 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2252 | ArgTys.push_back(argT); |
| 2253 | argT = Context->getObjCSelType(); |
| 2254 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2255 | ArgTys.push_back(argT); |
| 2256 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2257 | &ArgTys[0], ArgTys.size(), |
| 2258 | true /*isVariadic*/); |
| 2259 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
| 2260 | SourceLocation(), |
| 2261 | SourceLocation(), |
| 2262 | msgSendIdent, msgSendType, 0, |
| 2263 | SC_Extern, |
| 2264 | SC_None, false); |
| 2265 | } |
| 2266 | |
| 2267 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...); |
| 2268 | void RewriteModernObjC::SynthMsgSendSuperFunctionDecl() { |
| 2269 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
| 2270 | SmallVector<QualType, 16> ArgTys; |
| 2271 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 2272 | SourceLocation(), SourceLocation(), |
| 2273 | &Context->Idents.get("objc_super")); |
| 2274 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2275 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2276 | ArgTys.push_back(argT); |
| 2277 | argT = Context->getObjCSelType(); |
| 2278 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2279 | ArgTys.push_back(argT); |
| 2280 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2281 | &ArgTys[0], ArgTys.size(), |
| 2282 | true /*isVariadic*/); |
| 2283 | MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
| 2284 | SourceLocation(), |
| 2285 | SourceLocation(), |
| 2286 | msgSendIdent, msgSendType, 0, |
| 2287 | SC_Extern, |
| 2288 | SC_None, false); |
| 2289 | } |
| 2290 | |
| 2291 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
| 2292 | void RewriteModernObjC::SynthMsgSendStretFunctionDecl() { |
| 2293 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
| 2294 | SmallVector<QualType, 16> ArgTys; |
| 2295 | QualType argT = Context->getObjCIdType(); |
| 2296 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2297 | ArgTys.push_back(argT); |
| 2298 | argT = Context->getObjCSelType(); |
| 2299 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2300 | ArgTys.push_back(argT); |
| 2301 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2302 | &ArgTys[0], ArgTys.size(), |
| 2303 | true /*isVariadic*/); |
| 2304 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
| 2305 | SourceLocation(), |
| 2306 | SourceLocation(), |
| 2307 | msgSendIdent, msgSendType, 0, |
| 2308 | SC_Extern, |
| 2309 | SC_None, false); |
| 2310 | } |
| 2311 | |
| 2312 | // SynthMsgSendSuperStretFunctionDecl - |
| 2313 | // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...); |
| 2314 | void RewriteModernObjC::SynthMsgSendSuperStretFunctionDecl() { |
| 2315 | IdentifierInfo *msgSendIdent = |
| 2316 | &Context->Idents.get("objc_msgSendSuper_stret"); |
| 2317 | SmallVector<QualType, 16> ArgTys; |
| 2318 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 2319 | SourceLocation(), SourceLocation(), |
| 2320 | &Context->Idents.get("objc_super")); |
| 2321 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2322 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2323 | ArgTys.push_back(argT); |
| 2324 | argT = Context->getObjCSelType(); |
| 2325 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2326 | ArgTys.push_back(argT); |
| 2327 | QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2328 | &ArgTys[0], ArgTys.size(), |
| 2329 | true /*isVariadic*/); |
| 2330 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
| 2331 | SourceLocation(), |
| 2332 | SourceLocation(), |
| 2333 | msgSendIdent, msgSendType, 0, |
| 2334 | SC_Extern, |
| 2335 | SC_None, false); |
| 2336 | } |
| 2337 | |
| 2338 | // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); |
| 2339 | void RewriteModernObjC::SynthMsgSendFpretFunctionDecl() { |
| 2340 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
| 2341 | SmallVector<QualType, 16> ArgTys; |
| 2342 | QualType argT = Context->getObjCIdType(); |
| 2343 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2344 | ArgTys.push_back(argT); |
| 2345 | argT = Context->getObjCSelType(); |
| 2346 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2347 | ArgTys.push_back(argT); |
| 2348 | QualType msgSendType = getSimpleFunctionType(Context->DoubleTy, |
| 2349 | &ArgTys[0], ArgTys.size(), |
| 2350 | true /*isVariadic*/); |
| 2351 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
| 2352 | SourceLocation(), |
| 2353 | SourceLocation(), |
| 2354 | msgSendIdent, msgSendType, 0, |
| 2355 | SC_Extern, |
| 2356 | SC_None, false); |
| 2357 | } |
| 2358 | |
| 2359 | // SynthGetClassFunctionDecl - id objc_getClass(const char *name); |
| 2360 | void RewriteModernObjC::SynthGetClassFunctionDecl() { |
| 2361 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
| 2362 | SmallVector<QualType, 16> ArgTys; |
| 2363 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
| 2364 | QualType getClassType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2365 | &ArgTys[0], ArgTys.size()); |
| 2366 | GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
| 2367 | SourceLocation(), |
| 2368 | SourceLocation(), |
| 2369 | getClassIdent, getClassType, 0, |
| 2370 | SC_Extern, |
| 2371 | SC_None, false); |
| 2372 | } |
| 2373 | |
| 2374 | // SynthGetSuperClassFunctionDecl - Class class_getSuperclass(Class cls); |
| 2375 | void RewriteModernObjC::SynthGetSuperClassFunctionDecl() { |
| 2376 | IdentifierInfo *getSuperClassIdent = |
| 2377 | &Context->Idents.get("class_getSuperclass"); |
| 2378 | SmallVector<QualType, 16> ArgTys; |
| 2379 | ArgTys.push_back(Context->getObjCClassType()); |
| 2380 | QualType getClassType = getSimpleFunctionType(Context->getObjCClassType(), |
| 2381 | &ArgTys[0], ArgTys.size()); |
| 2382 | GetSuperClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
| 2383 | SourceLocation(), |
| 2384 | SourceLocation(), |
| 2385 | getSuperClassIdent, |
| 2386 | getClassType, 0, |
| 2387 | SC_Extern, |
| 2388 | SC_None, |
| 2389 | false); |
| 2390 | } |
| 2391 | |
| 2392 | // SynthGetMetaClassFunctionDecl - id objc_getMetaClass(const char *name); |
| 2393 | void RewriteModernObjC::SynthGetMetaClassFunctionDecl() { |
| 2394 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
| 2395 | SmallVector<QualType, 16> ArgTys; |
| 2396 | ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); |
| 2397 | QualType getClassType = getSimpleFunctionType(Context->getObjCIdType(), |
| 2398 | &ArgTys[0], ArgTys.size()); |
| 2399 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
| 2400 | SourceLocation(), |
| 2401 | SourceLocation(), |
| 2402 | getClassIdent, getClassType, 0, |
| 2403 | SC_Extern, |
| 2404 | SC_None, false); |
| 2405 | } |
| 2406 | |
| 2407 | Stmt *RewriteModernObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
| 2408 | QualType strType = getConstantStringStructType(); |
| 2409 | |
| 2410 | std::string S = "__NSConstantStringImpl_"; |
| 2411 | |
| 2412 | std::string tmpName = InFileName; |
| 2413 | unsigned i; |
| 2414 | for (i=0; i < tmpName.length(); i++) { |
| 2415 | char c = tmpName.at(i); |
| 2416 | // replace any non alphanumeric characters with '_'. |
| 2417 | if (!isalpha(c) && (c < '0' || c > '9')) |
| 2418 | tmpName[i] = '_'; |
| 2419 | } |
| 2420 | S += tmpName; |
| 2421 | S += "_"; |
| 2422 | S += utostr(NumObjCStringLiterals++); |
| 2423 | |
| 2424 | Preamble += "static __NSConstantStringImpl " + S; |
| 2425 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 2426 | Preamble += "0x000007c8,"; // utf8_str |
| 2427 | // The pretty printer for StringLiteral handles escape characters properly. |
| 2428 | std::string prettyBufS; |
| 2429 | llvm::raw_string_ostream prettyBuf(prettyBufS); |
| 2430 | Exp->getString()->printPretty(prettyBuf, *Context, 0, |
| 2431 | PrintingPolicy(LangOpts)); |
| 2432 | Preamble += prettyBuf.str(); |
| 2433 | Preamble += ","; |
| 2434 | Preamble += utostr(Exp->getString()->getByteLength()) + "};\n"; |
| 2435 | |
| 2436 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
| 2437 | SourceLocation(), &Context->Idents.get(S), |
| 2438 | strType, 0, SC_Static, SC_None); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2439 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, false, strType, VK_LValue, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2440 | SourceLocation()); |
| 2441 | Expr *Unop = new (Context) UnaryOperator(DRE, UO_AddrOf, |
| 2442 | Context->getPointerType(DRE->getType()), |
| 2443 | VK_RValue, OK_Ordinary, |
| 2444 | SourceLocation()); |
| 2445 | // cast to NSConstantString * |
| 2446 | CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Exp->getType(), |
| 2447 | CK_CPointerToObjCPointerCast, Unop); |
| 2448 | ReplaceStmt(Exp, cast); |
| 2449 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
| 2450 | return cast; |
| 2451 | } |
| 2452 | |
| 2453 | // struct objc_super { struct objc_object *receiver; struct objc_class *super; }; |
| 2454 | QualType RewriteModernObjC::getSuperStructType() { |
| 2455 | if (!SuperStructDecl) { |
| 2456 | SuperStructDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 2457 | SourceLocation(), SourceLocation(), |
| 2458 | &Context->Idents.get("objc_super")); |
| 2459 | QualType FieldTypes[2]; |
| 2460 | |
| 2461 | // struct objc_object *receiver; |
| 2462 | FieldTypes[0] = Context->getObjCIdType(); |
| 2463 | // struct objc_class *super; |
| 2464 | FieldTypes[1] = Context->getObjCClassType(); |
| 2465 | |
| 2466 | // Create fields |
| 2467 | for (unsigned i = 0; i < 2; ++i) { |
| 2468 | SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl, |
| 2469 | SourceLocation(), |
| 2470 | SourceLocation(), 0, |
| 2471 | FieldTypes[i], 0, |
| 2472 | /*BitWidth=*/0, |
| 2473 | /*Mutable=*/false, |
| 2474 | /*HasInit=*/false)); |
| 2475 | } |
| 2476 | |
| 2477 | SuperStructDecl->completeDefinition(); |
| 2478 | } |
| 2479 | return Context->getTagDeclType(SuperStructDecl); |
| 2480 | } |
| 2481 | |
| 2482 | QualType RewriteModernObjC::getConstantStringStructType() { |
| 2483 | if (!ConstantStringDecl) { |
| 2484 | ConstantStringDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 2485 | SourceLocation(), SourceLocation(), |
| 2486 | &Context->Idents.get("__NSConstantStringImpl")); |
| 2487 | QualType FieldTypes[4]; |
| 2488 | |
| 2489 | // struct objc_object *receiver; |
| 2490 | FieldTypes[0] = Context->getObjCIdType(); |
| 2491 | // int flags; |
| 2492 | FieldTypes[1] = Context->IntTy; |
| 2493 | // char *str; |
| 2494 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
| 2495 | // long length; |
| 2496 | FieldTypes[3] = Context->LongTy; |
| 2497 | |
| 2498 | // Create fields |
| 2499 | for (unsigned i = 0; i < 4; ++i) { |
| 2500 | ConstantStringDecl->addDecl(FieldDecl::Create(*Context, |
| 2501 | ConstantStringDecl, |
| 2502 | SourceLocation(), |
| 2503 | SourceLocation(), 0, |
| 2504 | FieldTypes[i], 0, |
| 2505 | /*BitWidth=*/0, |
| 2506 | /*Mutable=*/true, |
| 2507 | /*HasInit=*/false)); |
| 2508 | } |
| 2509 | |
| 2510 | ConstantStringDecl->completeDefinition(); |
| 2511 | } |
| 2512 | return Context->getTagDeclType(ConstantStringDecl); |
| 2513 | } |
| 2514 | |
| 2515 | Stmt *RewriteModernObjC::SynthMessageExpr(ObjCMessageExpr *Exp, |
| 2516 | SourceLocation StartLoc, |
| 2517 | SourceLocation EndLoc) { |
| 2518 | if (!SelGetUidFunctionDecl) |
| 2519 | SynthSelGetUidFunctionDecl(); |
| 2520 | if (!MsgSendFunctionDecl) |
| 2521 | SynthMsgSendFunctionDecl(); |
| 2522 | if (!MsgSendSuperFunctionDecl) |
| 2523 | SynthMsgSendSuperFunctionDecl(); |
| 2524 | if (!MsgSendStretFunctionDecl) |
| 2525 | SynthMsgSendStretFunctionDecl(); |
| 2526 | if (!MsgSendSuperStretFunctionDecl) |
| 2527 | SynthMsgSendSuperStretFunctionDecl(); |
| 2528 | if (!MsgSendFpretFunctionDecl) |
| 2529 | SynthMsgSendFpretFunctionDecl(); |
| 2530 | if (!GetClassFunctionDecl) |
| 2531 | SynthGetClassFunctionDecl(); |
| 2532 | if (!GetSuperClassFunctionDecl) |
| 2533 | SynthGetSuperClassFunctionDecl(); |
| 2534 | if (!GetMetaClassFunctionDecl) |
| 2535 | SynthGetMetaClassFunctionDecl(); |
| 2536 | |
| 2537 | // default to objc_msgSend(). |
| 2538 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2539 | // May need to use objc_msgSend_stret() as well. |
| 2540 | FunctionDecl *MsgSendStretFlavor = 0; |
| 2541 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
| 2542 | QualType resultType = mDecl->getResultType(); |
| 2543 | if (resultType->isRecordType()) |
| 2544 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
| 2545 | else if (resultType->isRealFloatingType()) |
| 2546 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
| 2547 | } |
| 2548 | |
| 2549 | // Synthesize a call to objc_msgSend(). |
| 2550 | SmallVector<Expr*, 8> MsgExprs; |
| 2551 | switch (Exp->getReceiverKind()) { |
| 2552 | case ObjCMessageExpr::SuperClass: { |
| 2553 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2554 | if (MsgSendStretFlavor) |
| 2555 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2556 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 2557 | |
| 2558 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
| 2559 | |
| 2560 | SmallVector<Expr*, 4> InitExprs; |
| 2561 | |
| 2562 | // set the receiver to self, the first argument to all methods. |
| 2563 | InitExprs.push_back( |
| 2564 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 2565 | CK_BitCast, |
| 2566 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2567 | false, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2568 | Context->getObjCIdType(), |
| 2569 | VK_RValue, |
| 2570 | SourceLocation())) |
| 2571 | ); // set the 'receiver'. |
| 2572 | |
| 2573 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2574 | SmallVector<Expr*, 8> ClsExprs; |
| 2575 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2576 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 2577 | ClassDecl->getIdentifier()->getName(), |
| 2578 | StringLiteral::Ascii, false, |
| 2579 | argType, SourceLocation())); |
| 2580 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
| 2581 | &ClsExprs[0], |
| 2582 | ClsExprs.size(), |
| 2583 | StartLoc, |
| 2584 | EndLoc); |
| 2585 | // (Class)objc_getClass("CurrentClass") |
| 2586 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 2587 | Context->getObjCClassType(), |
| 2588 | CK_BitCast, Cls); |
| 2589 | ClsExprs.clear(); |
| 2590 | ClsExprs.push_back(ArgExpr); |
| 2591 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 2592 | &ClsExprs[0], ClsExprs.size(), |
| 2593 | StartLoc, EndLoc); |
| 2594 | |
| 2595 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2596 | // To turn off a warning, type-cast to 'id' |
| 2597 | InitExprs.push_back( // set 'super class', using class_getSuperclass(). |
| 2598 | NoTypeInfoCStyleCastExpr(Context, |
| 2599 | Context->getObjCIdType(), |
| 2600 | CK_BitCast, Cls)); |
| 2601 | // struct objc_super |
| 2602 | QualType superType = getSuperStructType(); |
| 2603 | Expr *SuperRep; |
| 2604 | |
| 2605 | if (LangOpts.MicrosoftExt) { |
| 2606 | SynthSuperContructorFunctionDecl(); |
| 2607 | // Simulate a contructor call... |
| 2608 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2609 | false, superType, VK_LValue, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2610 | SourceLocation()); |
| 2611 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
| 2612 | InitExprs.size(), |
| 2613 | superType, VK_LValue, |
| 2614 | SourceLocation()); |
| 2615 | // The code for super is a little tricky to prevent collision with |
| 2616 | // the structure definition in the header. The rewriter has it's own |
| 2617 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2618 | // we need the cast below. For example: |
| 2619 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2620 | // |
| 2621 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
| 2622 | Context->getPointerType(SuperRep->getType()), |
| 2623 | VK_RValue, OK_Ordinary, |
| 2624 | SourceLocation()); |
| 2625 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2626 | Context->getPointerType(superType), |
| 2627 | CK_BitCast, SuperRep); |
| 2628 | } else { |
| 2629 | // (struct objc_super) { <exprs from above> } |
| 2630 | InitListExpr *ILE = |
| 2631 | new (Context) InitListExpr(*Context, SourceLocation(), |
| 2632 | &InitExprs[0], InitExprs.size(), |
| 2633 | SourceLocation()); |
| 2634 | TypeSourceInfo *superTInfo |
| 2635 | = Context->getTrivialTypeSourceInfo(superType); |
| 2636 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
| 2637 | superType, VK_LValue, |
| 2638 | ILE, false); |
| 2639 | // struct objc_super * |
| 2640 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
| 2641 | Context->getPointerType(SuperRep->getType()), |
| 2642 | VK_RValue, OK_Ordinary, |
| 2643 | SourceLocation()); |
| 2644 | } |
| 2645 | MsgExprs.push_back(SuperRep); |
| 2646 | break; |
| 2647 | } |
| 2648 | |
| 2649 | case ObjCMessageExpr::Class: { |
| 2650 | SmallVector<Expr*, 8> ClsExprs; |
| 2651 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2652 | ObjCInterfaceDecl *Class |
| 2653 | = Exp->getClassReceiver()->getAs<ObjCObjectType>()->getInterface(); |
| 2654 | IdentifierInfo *clsName = Class->getIdentifier(); |
| 2655 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 2656 | clsName->getName(), |
| 2657 | StringLiteral::Ascii, false, |
| 2658 | argType, SourceLocation())); |
| 2659 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2660 | &ClsExprs[0], |
| 2661 | ClsExprs.size(), |
| 2662 | StartLoc, EndLoc); |
| 2663 | MsgExprs.push_back(Cls); |
| 2664 | break; |
| 2665 | } |
| 2666 | |
| 2667 | case ObjCMessageExpr::SuperInstance:{ |
| 2668 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2669 | if (MsgSendStretFlavor) |
| 2670 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2671 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 2672 | ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); |
| 2673 | SmallVector<Expr*, 4> InitExprs; |
| 2674 | |
| 2675 | InitExprs.push_back( |
| 2676 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 2677 | CK_BitCast, |
| 2678 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2679 | false, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2680 | Context->getObjCIdType(), |
| 2681 | VK_RValue, SourceLocation())) |
| 2682 | ); // set the 'receiver'. |
| 2683 | |
| 2684 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2685 | SmallVector<Expr*, 8> ClsExprs; |
| 2686 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2687 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 2688 | ClassDecl->getIdentifier()->getName(), |
| 2689 | StringLiteral::Ascii, false, argType, |
| 2690 | SourceLocation())); |
| 2691 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2692 | &ClsExprs[0], |
| 2693 | ClsExprs.size(), |
| 2694 | StartLoc, EndLoc); |
| 2695 | // (Class)objc_getClass("CurrentClass") |
| 2696 | CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, |
| 2697 | Context->getObjCClassType(), |
| 2698 | CK_BitCast, Cls); |
| 2699 | ClsExprs.clear(); |
| 2700 | ClsExprs.push_back(ArgExpr); |
| 2701 | Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, |
| 2702 | &ClsExprs[0], ClsExprs.size(), |
| 2703 | StartLoc, EndLoc); |
| 2704 | |
| 2705 | // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) |
| 2706 | // To turn off a warning, type-cast to 'id' |
| 2707 | InitExprs.push_back( |
| 2708 | // set 'super class', using class_getSuperclass(). |
| 2709 | NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 2710 | CK_BitCast, Cls)); |
| 2711 | // struct objc_super |
| 2712 | QualType superType = getSuperStructType(); |
| 2713 | Expr *SuperRep; |
| 2714 | |
| 2715 | if (LangOpts.MicrosoftExt) { |
| 2716 | SynthSuperContructorFunctionDecl(); |
| 2717 | // Simulate a contructor call... |
| 2718 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2719 | false, superType, VK_LValue, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2720 | SourceLocation()); |
| 2721 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
| 2722 | InitExprs.size(), |
| 2723 | superType, VK_LValue, SourceLocation()); |
| 2724 | // The code for super is a little tricky to prevent collision with |
| 2725 | // the structure definition in the header. The rewriter has it's own |
| 2726 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2727 | // we need the cast below. For example: |
| 2728 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2729 | // |
| 2730 | SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf, |
| 2731 | Context->getPointerType(SuperRep->getType()), |
| 2732 | VK_RValue, OK_Ordinary, |
| 2733 | SourceLocation()); |
| 2734 | SuperRep = NoTypeInfoCStyleCastExpr(Context, |
| 2735 | Context->getPointerType(superType), |
| 2736 | CK_BitCast, SuperRep); |
| 2737 | } else { |
| 2738 | // (struct objc_super) { <exprs from above> } |
| 2739 | InitListExpr *ILE = |
| 2740 | new (Context) InitListExpr(*Context, SourceLocation(), |
| 2741 | &InitExprs[0], InitExprs.size(), |
| 2742 | SourceLocation()); |
| 2743 | TypeSourceInfo *superTInfo |
| 2744 | = Context->getTrivialTypeSourceInfo(superType); |
| 2745 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, |
| 2746 | superType, VK_RValue, ILE, |
| 2747 | false); |
| 2748 | } |
| 2749 | MsgExprs.push_back(SuperRep); |
| 2750 | break; |
| 2751 | } |
| 2752 | |
| 2753 | case ObjCMessageExpr::Instance: { |
| 2754 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 2755 | // Foo<Proto> *. |
| 2756 | Expr *recExpr = Exp->getInstanceReceiver(); |
| 2757 | while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr)) |
| 2758 | recExpr = CE->getSubExpr(); |
| 2759 | CastKind CK = recExpr->getType()->isObjCObjectPointerType() |
| 2760 | ? CK_BitCast : recExpr->getType()->isBlockPointerType() |
| 2761 | ? CK_BlockPointerToObjCPointerCast |
| 2762 | : CK_CPointerToObjCPointerCast; |
| 2763 | |
| 2764 | recExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 2765 | CK, recExpr); |
| 2766 | MsgExprs.push_back(recExpr); |
| 2767 | break; |
| 2768 | } |
| 2769 | } |
| 2770 | |
| 2771 | // Create a call to sel_registerName("selName"), it will be the 2nd argument. |
| 2772 | SmallVector<Expr*, 8> SelExprs; |
| 2773 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2774 | SelExprs.push_back(StringLiteral::Create(*Context, |
| 2775 | Exp->getSelector().getAsString(), |
| 2776 | StringLiteral::Ascii, false, |
| 2777 | argType, SourceLocation())); |
| 2778 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2779 | &SelExprs[0], SelExprs.size(), |
| 2780 | StartLoc, |
| 2781 | EndLoc); |
| 2782 | MsgExprs.push_back(SelExp); |
| 2783 | |
| 2784 | // Now push any user supplied arguments. |
| 2785 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
| 2786 | Expr *userExpr = Exp->getArg(i); |
| 2787 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 2788 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 2789 | // Reuse the ICE type, it is exactly what the doctor ordered. |
| 2790 | QualType type = ICE->getType(); |
| 2791 | if (needToScanForQualifiers(type)) |
| 2792 | type = Context->getObjCIdType(); |
| 2793 | // Make sure we convert "type (^)(...)" to "type (*)(...)". |
| 2794 | (void)convertBlockPointerToFunctionPointer(type); |
| 2795 | const Expr *SubExpr = ICE->IgnoreParenImpCasts(); |
| 2796 | CastKind CK; |
| 2797 | if (SubExpr->getType()->isIntegralType(*Context) && |
| 2798 | type->isBooleanType()) { |
| 2799 | CK = CK_IntegralToBoolean; |
| 2800 | } else if (type->isObjCObjectPointerType()) { |
| 2801 | if (SubExpr->getType()->isBlockPointerType()) { |
| 2802 | CK = CK_BlockPointerToObjCPointerCast; |
| 2803 | } else if (SubExpr->getType()->isPointerType()) { |
| 2804 | CK = CK_CPointerToObjCPointerCast; |
| 2805 | } else { |
| 2806 | CK = CK_BitCast; |
| 2807 | } |
| 2808 | } else { |
| 2809 | CK = CK_BitCast; |
| 2810 | } |
| 2811 | |
| 2812 | userExpr = NoTypeInfoCStyleCastExpr(Context, type, CK, userExpr); |
| 2813 | } |
| 2814 | // Make id<P...> cast into an 'id' cast. |
| 2815 | else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) { |
| 2816 | if (CE->getType()->isObjCQualifiedIdType()) { |
| 2817 | while ((CE = dyn_cast<CStyleCastExpr>(userExpr))) |
| 2818 | userExpr = CE->getSubExpr(); |
| 2819 | CastKind CK; |
| 2820 | if (userExpr->getType()->isIntegralType(*Context)) { |
| 2821 | CK = CK_IntegralToPointer; |
| 2822 | } else if (userExpr->getType()->isBlockPointerType()) { |
| 2823 | CK = CK_BlockPointerToObjCPointerCast; |
| 2824 | } else if (userExpr->getType()->isPointerType()) { |
| 2825 | CK = CK_CPointerToObjCPointerCast; |
| 2826 | } else { |
| 2827 | CK = CK_BitCast; |
| 2828 | } |
| 2829 | userExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), |
| 2830 | CK, userExpr); |
| 2831 | } |
| 2832 | } |
| 2833 | MsgExprs.push_back(userExpr); |
| 2834 | // We've transferred the ownership to MsgExprs. For now, we *don't* null |
| 2835 | // out the argument in the original expression (since we aren't deleting |
| 2836 | // the ObjCMessageExpr). See RewritePropertyOrImplicitSetter() usage for more info. |
| 2837 | //Exp->setArg(i, 0); |
| 2838 | } |
| 2839 | // Generate the funky cast. |
| 2840 | CastExpr *cast; |
| 2841 | SmallVector<QualType, 8> ArgTypes; |
| 2842 | QualType returnType; |
| 2843 | |
| 2844 | // Push 'id' and 'SEL', the 2 implicit arguments. |
| 2845 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 2846 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 2847 | else |
| 2848 | ArgTypes.push_back(Context->getObjCIdType()); |
| 2849 | ArgTypes.push_back(Context->getObjCSelType()); |
| 2850 | if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) { |
| 2851 | // Push any user argument types. |
| 2852 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 2853 | E = OMD->param_end(); PI != E; ++PI) { |
| 2854 | QualType t = (*PI)->getType()->isObjCQualifiedIdType() |
| 2855 | ? Context->getObjCIdType() |
| 2856 | : (*PI)->getType(); |
| 2857 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
| 2858 | (void)convertBlockPointerToFunctionPointer(t); |
| 2859 | ArgTypes.push_back(t); |
| 2860 | } |
| 2861 | returnType = Exp->getType(); |
| 2862 | convertToUnqualifiedObjCType(returnType); |
| 2863 | (void)convertBlockPointerToFunctionPointer(returnType); |
| 2864 | } else { |
| 2865 | returnType = Context->getObjCIdType(); |
| 2866 | } |
| 2867 | // Get the type, we will need to reference it in a couple spots. |
| 2868 | QualType msgSendType = MsgSendFlavor->getType(); |
| 2869 | |
| 2870 | // Create a reference to the objc_msgSend() declaration. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2871 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, false, msgSendType, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2872 | VK_LValue, SourceLocation()); |
| 2873 | |
| 2874 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
| 2875 | // If we don't do this cast, we get the following bizarre warning/note: |
| 2876 | // xx.m:13: warning: function called through a non-compatible type |
| 2877 | // xx.m:13: note: if this code is reached, the program will abort |
| 2878 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 2879 | Context->getPointerType(Context->VoidTy), |
| 2880 | CK_BitCast, DRE); |
| 2881 | |
| 2882 | // Now do the "normal" pointer to function cast. |
| 2883 | QualType castType = |
| 2884 | getSimpleFunctionType(returnType, &ArgTypes[0], ArgTypes.size(), |
| 2885 | // If we don't have a method decl, force a variadic cast. |
| 2886 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true); |
| 2887 | castType = Context->getPointerType(castType); |
| 2888 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
| 2889 | cast); |
| 2890 | |
| 2891 | // Don't forget the parens to enforce the proper binding. |
| 2892 | ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast); |
| 2893 | |
| 2894 | const FunctionType *FT = msgSendType->getAs<FunctionType>(); |
| 2895 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
| 2896 | MsgExprs.size(), |
| 2897 | FT->getResultType(), VK_RValue, |
| 2898 | EndLoc); |
| 2899 | Stmt *ReplacingStmt = CE; |
| 2900 | if (MsgSendStretFlavor) { |
| 2901 | // We have the method which returns a struct/union. Must also generate |
| 2902 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 2903 | // expression which dictate which one to envoke depending on size of |
| 2904 | // method's return type. |
| 2905 | |
| 2906 | // Create a reference to the objc_msgSend_stret() declaration. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2907 | DeclRefExpr *STDRE = new (Context) DeclRefExpr(MsgSendStretFlavor, |
| 2908 | false, msgSendType, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2909 | VK_LValue, SourceLocation()); |
| 2910 | // Need to cast objc_msgSend_stret to "void *" (see above comment). |
| 2911 | cast = NoTypeInfoCStyleCastExpr(Context, |
| 2912 | Context->getPointerType(Context->VoidTy), |
| 2913 | CK_BitCast, STDRE); |
| 2914 | // Now do the "normal" pointer to function cast. |
| 2915 | castType = getSimpleFunctionType(returnType, &ArgTypes[0], ArgTypes.size(), |
| 2916 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false); |
| 2917 | castType = Context->getPointerType(castType); |
| 2918 | cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast, |
| 2919 | cast); |
| 2920 | |
| 2921 | // Don't forget the parens to enforce the proper binding. |
| 2922 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
| 2923 | |
| 2924 | FT = msgSendType->getAs<FunctionType>(); |
| 2925 | CallExpr *STCE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
| 2926 | MsgExprs.size(), |
| 2927 | FT->getResultType(), VK_RValue, |
| 2928 | SourceLocation()); |
| 2929 | |
| 2930 | // Build sizeof(returnType) |
| 2931 | UnaryExprOrTypeTraitExpr *sizeofExpr = |
| 2932 | new (Context) UnaryExprOrTypeTraitExpr(UETT_SizeOf, |
| 2933 | Context->getTrivialTypeSourceInfo(returnType), |
| 2934 | Context->getSizeType(), SourceLocation(), |
| 2935 | SourceLocation()); |
| 2936 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 2937 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 2938 | // For X86 it is more complicated and some kind of target specific routine |
| 2939 | // is needed to decide what to do. |
| 2940 | unsigned IntSize = |
| 2941 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 2942 | IntegerLiteral *limit = IntegerLiteral::Create(*Context, |
| 2943 | llvm::APInt(IntSize, 8), |
| 2944 | Context->IntTy, |
| 2945 | SourceLocation()); |
| 2946 | BinaryOperator *lessThanExpr = |
| 2947 | new (Context) BinaryOperator(sizeofExpr, limit, BO_LE, Context->IntTy, |
| 2948 | VK_RValue, OK_Ordinary, SourceLocation()); |
| 2949 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 2950 | ConditionalOperator *CondExpr = |
| 2951 | new (Context) ConditionalOperator(lessThanExpr, |
| 2952 | SourceLocation(), CE, |
| 2953 | SourceLocation(), STCE, |
| 2954 | returnType, VK_RValue, OK_Ordinary); |
| 2955 | ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 2956 | CondExpr); |
| 2957 | } |
| 2958 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
| 2959 | return ReplacingStmt; |
| 2960 | } |
| 2961 | |
| 2962 | Stmt *RewriteModernObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
| 2963 | Stmt *ReplacingStmt = SynthMessageExpr(Exp, Exp->getLocStart(), |
| 2964 | Exp->getLocEnd()); |
| 2965 | |
| 2966 | // Now do the actual rewrite. |
| 2967 | ReplaceStmt(Exp, ReplacingStmt); |
| 2968 | |
| 2969 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
| 2970 | return ReplacingStmt; |
| 2971 | } |
| 2972 | |
| 2973 | // typedef struct objc_object Protocol; |
| 2974 | QualType RewriteModernObjC::getProtocolType() { |
| 2975 | if (!ProtocolTypeDecl) { |
| 2976 | TypeSourceInfo *TInfo |
| 2977 | = Context->getTrivialTypeSourceInfo(Context->getObjCIdType()); |
| 2978 | ProtocolTypeDecl = TypedefDecl::Create(*Context, TUDecl, |
| 2979 | SourceLocation(), SourceLocation(), |
| 2980 | &Context->Idents.get("Protocol"), |
| 2981 | TInfo); |
| 2982 | } |
| 2983 | return Context->getTypeDeclType(ProtocolTypeDecl); |
| 2984 | } |
| 2985 | |
| 2986 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
| 2987 | /// a synthesized/forward data reference (to the protocol's metadata). |
| 2988 | /// The forward references (and metadata) are generated in |
| 2989 | /// RewriteModernObjC::HandleTranslationUnit(). |
| 2990 | Stmt *RewriteModernObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
Fariborz Jahanian | 30650eb | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 2991 | std::string Name = "_OBJC_PROTOCOL_REFERENCE_$_" + |
| 2992 | Exp->getProtocol()->getNameAsString(); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2993 | IdentifierInfo *ID = &Context->Idents.get(Name); |
| 2994 | VarDecl *VD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
| 2995 | SourceLocation(), ID, getProtocolType(), 0, |
| 2996 | SC_Extern, SC_None); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2997 | DeclRefExpr *DRE = new (Context) DeclRefExpr(VD, false, getProtocolType(), |
| 2998 | VK_LValue, SourceLocation()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 2999 | Expr *DerefExpr = new (Context) UnaryOperator(DRE, UO_AddrOf, |
| 3000 | Context->getPointerType(DRE->getType()), |
| 3001 | VK_RValue, OK_Ordinary, SourceLocation()); |
| 3002 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, DerefExpr->getType(), |
| 3003 | CK_BitCast, |
| 3004 | DerefExpr); |
| 3005 | ReplaceStmt(Exp, castExpr); |
| 3006 | ProtocolExprDecls.insert(Exp->getProtocol()->getCanonicalDecl()); |
| 3007 | // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info. |
| 3008 | return castExpr; |
| 3009 | |
| 3010 | } |
| 3011 | |
| 3012 | bool RewriteModernObjC::BufferContainsPPDirectives(const char *startBuf, |
| 3013 | const char *endBuf) { |
| 3014 | while (startBuf < endBuf) { |
| 3015 | if (*startBuf == '#') { |
| 3016 | // Skip whitespace. |
| 3017 | for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf) |
| 3018 | ; |
| 3019 | if (!strncmp(startBuf, "if", strlen("if")) || |
| 3020 | !strncmp(startBuf, "ifdef", strlen("ifdef")) || |
| 3021 | !strncmp(startBuf, "ifndef", strlen("ifndef")) || |
| 3022 | !strncmp(startBuf, "define", strlen("define")) || |
| 3023 | !strncmp(startBuf, "undef", strlen("undef")) || |
| 3024 | !strncmp(startBuf, "else", strlen("else")) || |
| 3025 | !strncmp(startBuf, "elif", strlen("elif")) || |
| 3026 | !strncmp(startBuf, "endif", strlen("endif")) || |
| 3027 | !strncmp(startBuf, "pragma", strlen("pragma")) || |
| 3028 | !strncmp(startBuf, "include", strlen("include")) || |
| 3029 | !strncmp(startBuf, "import", strlen("import")) || |
| 3030 | !strncmp(startBuf, "include_next", strlen("include_next"))) |
| 3031 | return true; |
| 3032 | } |
| 3033 | startBuf++; |
| 3034 | } |
| 3035 | return false; |
| 3036 | } |
| 3037 | |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3038 | /// RewriteObjCFieldDeclType - This routine rewrites a type into the buffer. |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3039 | /// It handles elaborated types, as well as enum types in the process. |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3040 | bool RewriteModernObjC::RewriteObjCFieldDeclType(QualType &Type, |
| 3041 | std::string &Result) { |
| 3042 | if (Type->isArrayType()) { |
| 3043 | QualType ElemTy = Context->getBaseElementType(Type); |
| 3044 | return RewriteObjCFieldDeclType(ElemTy, Result); |
| 3045 | } |
| 3046 | else if (Type->isRecordType()) { |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3047 | RecordDecl *RD = Type->getAs<RecordType>()->getDecl(); |
| 3048 | if (RD->isCompleteDefinition()) { |
| 3049 | if (RD->isStruct()) |
| 3050 | Result += "\n\tstruct "; |
| 3051 | else if (RD->isUnion()) |
| 3052 | Result += "\n\tunion "; |
| 3053 | else |
| 3054 | assert(false && "class not allowed as an ivar type"); |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3055 | |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3056 | Result += RD->getName(); |
| 3057 | if (TagsDefinedInIvarDecls.count(RD)) { |
| 3058 | // This struct is already defined. Do not write its definition again. |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3059 | Result += " "; |
| 3060 | return true; |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3061 | } |
| 3062 | TagsDefinedInIvarDecls.insert(RD); |
| 3063 | Result += " {\n"; |
| 3064 | for (RecordDecl::field_iterator i = RD->field_begin(), |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3065 | e = RD->field_end(); i != e; ++i) { |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3066 | FieldDecl *FD = *i; |
| 3067 | RewriteObjCFieldDecl(FD, Result); |
| 3068 | } |
| 3069 | Result += "\t} "; |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3070 | return true; |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3071 | } |
| 3072 | } |
| 3073 | else if (Type->isEnumeralType()) { |
| 3074 | EnumDecl *ED = Type->getAs<EnumType>()->getDecl(); |
| 3075 | if (ED->isCompleteDefinition()) { |
| 3076 | Result += "\n\tenum "; |
| 3077 | Result += ED->getName(); |
| 3078 | if (TagsDefinedInIvarDecls.count(ED)) { |
| 3079 | // This enum is already defined. Do not write its definition again. |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3080 | Result += " "; |
| 3081 | return true; |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3082 | } |
| 3083 | TagsDefinedInIvarDecls.insert(ED); |
| 3084 | |
| 3085 | Result += " {\n"; |
| 3086 | for (EnumDecl::enumerator_iterator EC = ED->enumerator_begin(), |
| 3087 | ECEnd = ED->enumerator_end(); EC != ECEnd; ++EC) { |
| 3088 | Result += "\t"; Result += EC->getName(); Result += " = "; |
| 3089 | llvm::APSInt Val = EC->getInitVal(); |
| 3090 | Result += Val.toString(10); |
| 3091 | Result += ",\n"; |
| 3092 | } |
| 3093 | Result += "\t} "; |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3094 | return true; |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3095 | } |
| 3096 | } |
| 3097 | |
| 3098 | Result += "\t"; |
| 3099 | convertObjCTypeToCStyleType(Type); |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3100 | return false; |
| 3101 | } |
| 3102 | |
| 3103 | |
| 3104 | /// RewriteObjCFieldDecl - This routine rewrites a field into the buffer. |
| 3105 | /// It handles elaborated types, as well as enum types in the process. |
| 3106 | void RewriteModernObjC::RewriteObjCFieldDecl(FieldDecl *fieldDecl, |
| 3107 | std::string &Result) { |
| 3108 | QualType Type = fieldDecl->getType(); |
| 3109 | std::string Name = fieldDecl->getNameAsString(); |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3110 | |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3111 | bool EleboratedType = RewriteObjCFieldDeclType(Type, Result); |
| 3112 | if (!EleboratedType) |
| 3113 | Type.getAsStringInternal(Name, Context->getPrintingPolicy()); |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3114 | Result += Name; |
| 3115 | if (fieldDecl->isBitField()) { |
| 3116 | Result += " : "; Result += utostr(fieldDecl->getBitWidthValue(*Context)); |
| 3117 | } |
Fariborz Jahanian | 97c1fd6 | 2012-03-09 23:46:23 +0000 | [diff] [blame] | 3118 | else if (EleboratedType && Type->isArrayType()) { |
| 3119 | CanQualType CType = Context->getCanonicalType(Type); |
| 3120 | while (isa<ArrayType>(CType)) { |
| 3121 | if (const ConstantArrayType *CAT = Context->getAsConstantArrayType(CType)) { |
| 3122 | Result += "["; |
| 3123 | llvm::APInt Dim = CAT->getSize(); |
| 3124 | Result += utostr(Dim.getZExtValue()); |
| 3125 | Result += "]"; |
| 3126 | } |
| 3127 | CType = CType->getAs<ArrayType>()->getElementType(); |
| 3128 | } |
| 3129 | } |
| 3130 | |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3131 | Result += ";\n"; |
| 3132 | } |
| 3133 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3134 | /// RewriteObjCInternalStruct - Rewrite one internal struct corresponding to |
| 3135 | /// an objective-c class with ivars. |
| 3136 | void RewriteModernObjC::RewriteObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
| 3137 | std::string &Result) { |
| 3138 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
| 3139 | assert(CDecl->getName() != "" && |
| 3140 | "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3141 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 3142 | SmallVector<ObjCIvarDecl *, 8> IVars; |
| 3143 | for (ObjCIvarDecl *IVD = CDecl->all_declared_ivar_begin(); |
Fariborz Jahanian | 9a2105b | 2012-03-06 17:16:27 +0000 | [diff] [blame] | 3144 | IVD; IVD = IVD->getNextIvar()) |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 3145 | IVars.push_back(IVD); |
Fariborz Jahanian | 9a2105b | 2012-03-06 17:16:27 +0000 | [diff] [blame] | 3146 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3147 | SourceLocation LocStart = CDecl->getLocStart(); |
| 3148 | SourceLocation LocEnd = CDecl->getEndOfDefinitionLoc(); |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 3149 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3150 | const char *startBuf = SM->getCharacterData(LocStart); |
| 3151 | const char *endBuf = SM->getCharacterData(LocEnd); |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 3152 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3153 | // If no ivars and no root or if its root, directly or indirectly, |
| 3154 | // 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] | 3155 | if ((!CDecl->isThisDeclarationADefinition() || IVars.size() == 0) && |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3156 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
| 3157 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
| 3158 | ReplaceText(LocStart, endBuf-startBuf, Result); |
| 3159 | return; |
| 3160 | } |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 3161 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3162 | Result += "\nstruct "; |
| 3163 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 3164 | Result += "_IMPL {\n"; |
| 3165 | |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 3166 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 3167 | Result += "\tstruct "; Result += RCDecl->getNameAsString(); |
| 3168 | Result += "_IMPL "; Result += RCDecl->getNameAsString(); |
| 3169 | Result += "_IVARS;\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3170 | } |
Fariborz Jahanian | 15f8777 | 2012-02-28 22:45:07 +0000 | [diff] [blame] | 3171 | TagsDefinedInIvarDecls.clear(); |
| 3172 | for (unsigned i = 0, e = IVars.size(); i < e; i++) |
| 3173 | RewriteObjCFieldDecl(IVars[i], Result); |
Fariborz Jahanian | 0b17b9a | 2012-02-12 21:36:23 +0000 | [diff] [blame] | 3174 | |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 3175 | Result += "};\n"; |
| 3176 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
| 3177 | ReplaceText(LocStart, endBuf-startBuf, Result); |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 3178 | // Mark this struct as having been generated. |
| 3179 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
| 3180 | llvm_unreachable("struct already synthesize- RewriteObjCInternalStruct"); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3181 | } |
| 3182 | |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 3183 | static void WriteInternalIvarName(ObjCInterfaceDecl *IDecl, |
| 3184 | ObjCIvarDecl *IvarDecl, std::string &Result) { |
| 3185 | Result += "OBJC_IVAR_$_"; |
| 3186 | Result += IDecl->getName(); |
| 3187 | Result += "$"; |
| 3188 | Result += IvarDecl->getName(); |
| 3189 | } |
| 3190 | |
Fariborz Jahanian | 72c88f1 | 2012-02-22 18:13:25 +0000 | [diff] [blame] | 3191 | /// RewriteIvarOffsetSymbols - Rewrite ivar offset symbols of those ivars which |
| 3192 | /// have been referenced in an ivar access expression. |
| 3193 | void RewriteModernObjC::RewriteIvarOffsetSymbols(ObjCInterfaceDecl *CDecl, |
| 3194 | std::string &Result) { |
| 3195 | // write out ivar offset symbols which have been referenced in an ivar |
| 3196 | // access expression. |
| 3197 | llvm::SmallPtrSet<ObjCIvarDecl *, 8> Ivars = ReferencedIvars[CDecl]; |
| 3198 | if (Ivars.empty()) |
| 3199 | return; |
| 3200 | for (llvm::SmallPtrSet<ObjCIvarDecl *, 8>::iterator i = Ivars.begin(), |
| 3201 | e = Ivars.end(); i != e; i++) { |
| 3202 | ObjCIvarDecl *IvarDecl = (*i); |
Fariborz Jahanian | 40a777a | 2012-03-12 16:46:58 +0000 | [diff] [blame] | 3203 | Result += "\n"; |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 3204 | Result += "extern \"C\" "; |
Fariborz Jahanian | 40a777a | 2012-03-12 16:46:58 +0000 | [diff] [blame] | 3205 | if (LangOpts.MicrosoftExt) |
| 3206 | Result += "__declspec(allocate(\".objc_ivar$B\")) "; |
| 3207 | if (LangOpts.MicrosoftExt && |
| 3208 | IvarDecl->getAccessControl() != ObjCIvarDecl::Private && |
| 3209 | IvarDecl->getAccessControl() != ObjCIvarDecl::Package) { |
| 3210 | const ObjCInterfaceDecl *CDecl = IvarDecl->getContainingInterface(); |
| 3211 | if (CDecl->getImplementation()) |
| 3212 | Result += "__declspec(dllexport) "; |
| 3213 | } |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 3214 | Result += "unsigned long "; |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 3215 | WriteInternalIvarName(CDecl, IvarDecl, Result); |
| 3216 | Result += ";"; |
Fariborz Jahanian | 72c88f1 | 2012-02-22 18:13:25 +0000 | [diff] [blame] | 3217 | } |
| 3218 | } |
| 3219 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3220 | //===----------------------------------------------------------------------===// |
| 3221 | // Meta Data Emission |
| 3222 | //===----------------------------------------------------------------------===// |
| 3223 | |
| 3224 | |
| 3225 | /// RewriteImplementations - This routine rewrites all method implementations |
| 3226 | /// and emits meta-data. |
| 3227 | |
| 3228 | void RewriteModernObjC::RewriteImplementations() { |
| 3229 | int ClsDefCount = ClassImplementation.size(); |
| 3230 | int CatDefCount = CategoryImplementation.size(); |
| 3231 | |
| 3232 | // Rewrite implemented methods |
Fariborz Jahanian | 8f1fed0 | 2012-02-11 20:10:52 +0000 | [diff] [blame] | 3233 | for (int i = 0; i < ClsDefCount; i++) { |
| 3234 | ObjCImplementationDecl *OIMP = ClassImplementation[i]; |
| 3235 | ObjCInterfaceDecl *CDecl = OIMP->getClassInterface(); |
| 3236 | if (CDecl->isImplicitInterfaceDecl()) |
Fariborz Jahanian | cf4c60f | 2012-02-17 22:20:12 +0000 | [diff] [blame] | 3237 | assert(false && |
| 3238 | "Legacy implicit interface rewriting not supported in moder abi"); |
Fariborz Jahanian | 8f1fed0 | 2012-02-11 20:10:52 +0000 | [diff] [blame] | 3239 | RewriteImplementationDecl(OIMP); |
| 3240 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3241 | |
Fariborz Jahanian | 8c00a1b | 2012-02-17 20:33:00 +0000 | [diff] [blame] | 3242 | for (int i = 0; i < CatDefCount; i++) { |
| 3243 | ObjCCategoryImplDecl *CIMP = CategoryImplementation[i]; |
| 3244 | ObjCInterfaceDecl *CDecl = CIMP->getClassInterface(); |
| 3245 | if (CDecl->isImplicitInterfaceDecl()) |
| 3246 | assert(false && |
| 3247 | "Legacy implicit interface rewriting not supported in moder abi"); |
Fariborz Jahanian | 8c00a1b | 2012-02-17 20:33:00 +0000 | [diff] [blame] | 3248 | RewriteImplementationDecl(CIMP); |
| 3249 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3250 | } |
| 3251 | |
| 3252 | void RewriteModernObjC::RewriteByRefString(std::string &ResultStr, |
| 3253 | const std::string &Name, |
| 3254 | ValueDecl *VD, bool def) { |
| 3255 | assert(BlockByRefDeclNo.count(VD) && |
| 3256 | "RewriteByRefString: ByRef decl missing"); |
| 3257 | if (def) |
| 3258 | ResultStr += "struct "; |
| 3259 | ResultStr += "__Block_byref_" + Name + |
| 3260 | "_" + utostr(BlockByRefDeclNo[VD]) ; |
| 3261 | } |
| 3262 | |
| 3263 | static bool HasLocalVariableExternalStorage(ValueDecl *VD) { |
| 3264 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 3265 | return (Var->isFunctionOrMethodVarDecl() && !Var->hasLocalStorage()); |
| 3266 | return false; |
| 3267 | } |
| 3268 | |
| 3269 | std::string RewriteModernObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, |
| 3270 | StringRef funcName, |
| 3271 | std::string Tag) { |
| 3272 | const FunctionType *AFT = CE->getFunctionType(); |
| 3273 | QualType RT = AFT->getResultType(); |
| 3274 | std::string StructRef = "struct " + Tag; |
| 3275 | std::string S = "static " + RT.getAsString(Context->getPrintingPolicy()) + " __" + |
Fariborz Jahanian | df474ec | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 3276 | funcName.str() + "_block_func_" + utostr(i); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3277 | |
| 3278 | BlockDecl *BD = CE->getBlockDecl(); |
| 3279 | |
| 3280 | if (isa<FunctionNoProtoType>(AFT)) { |
| 3281 | // No user-supplied arguments. Still need to pass in a pointer to the |
| 3282 | // block (to reference imported block decl refs). |
| 3283 | S += "(" + StructRef + " *__cself)"; |
| 3284 | } else if (BD->param_empty()) { |
| 3285 | S += "(" + StructRef + " *__cself)"; |
| 3286 | } else { |
| 3287 | const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); |
| 3288 | assert(FT && "SynthesizeBlockFunc: No function proto"); |
| 3289 | S += '('; |
| 3290 | // first add the implicit argument. |
| 3291 | S += StructRef + " *__cself, "; |
| 3292 | std::string ParamStr; |
| 3293 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 3294 | E = BD->param_end(); AI != E; ++AI) { |
| 3295 | if (AI != BD->param_begin()) S += ", "; |
| 3296 | ParamStr = (*AI)->getNameAsString(); |
| 3297 | QualType QT = (*AI)->getType(); |
Fariborz Jahanian | 2610f90 | 2012-03-27 16:42:20 +0000 | [diff] [blame] | 3298 | (void)convertBlockPointerToFunctionPointer(QT); |
| 3299 | QT.getAsStringInternal(ParamStr, Context->getPrintingPolicy()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3300 | S += ParamStr; |
| 3301 | } |
| 3302 | if (FT->isVariadic()) { |
| 3303 | if (!BD->param_empty()) S += ", "; |
| 3304 | S += "..."; |
| 3305 | } |
| 3306 | S += ')'; |
| 3307 | } |
| 3308 | S += " {\n"; |
| 3309 | |
| 3310 | // Create local declarations to avoid rewriting all closure decl ref exprs. |
| 3311 | // First, emit a declaration for all "by ref" decls. |
| 3312 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 3313 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3314 | S += " "; |
| 3315 | std::string Name = (*I)->getNameAsString(); |
| 3316 | std::string TypeString; |
| 3317 | RewriteByRefString(TypeString, Name, (*I)); |
| 3318 | TypeString += " *"; |
| 3319 | Name = TypeString + Name; |
| 3320 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; |
| 3321 | } |
| 3322 | // Next, emit a declaration for all "by copy" declarations. |
| 3323 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 3324 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3325 | S += " "; |
| 3326 | // Handle nested closure invocation. For example: |
| 3327 | // |
| 3328 | // void (^myImportedClosure)(void); |
| 3329 | // myImportedClosure = ^(void) { setGlobalInt(x + y); }; |
| 3330 | // |
| 3331 | // void (^anotherClosure)(void); |
| 3332 | // anotherClosure = ^(void) { |
| 3333 | // myImportedClosure(); // import and invoke the closure |
| 3334 | // }; |
| 3335 | // |
| 3336 | if (isTopLevelBlockPointerType((*I)->getType())) { |
| 3337 | RewriteBlockPointerTypeVariable(S, (*I)); |
| 3338 | S += " = ("; |
| 3339 | RewriteBlockPointerType(S, (*I)->getType()); |
| 3340 | S += ")"; |
| 3341 | S += "__cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; |
| 3342 | } |
| 3343 | else { |
| 3344 | std::string Name = (*I)->getNameAsString(); |
| 3345 | QualType QT = (*I)->getType(); |
| 3346 | if (HasLocalVariableExternalStorage(*I)) |
| 3347 | QT = Context->getPointerType(QT); |
| 3348 | QT.getAsStringInternal(Name, Context->getPrintingPolicy()); |
| 3349 | S += Name + " = __cself->" + |
| 3350 | (*I)->getNameAsString() + "; // bound by copy\n"; |
| 3351 | } |
| 3352 | } |
| 3353 | std::string RewrittenStr = RewrittenBlockExprs[CE]; |
| 3354 | const char *cstr = RewrittenStr.c_str(); |
| 3355 | while (*cstr++ != '{') ; |
| 3356 | S += cstr; |
| 3357 | S += "\n"; |
| 3358 | return S; |
| 3359 | } |
| 3360 | |
| 3361 | std::string RewriteModernObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
| 3362 | StringRef funcName, |
| 3363 | std::string Tag) { |
| 3364 | std::string StructRef = "struct " + Tag; |
| 3365 | std::string S = "static void __"; |
| 3366 | |
| 3367 | S += funcName; |
| 3368 | S += "_block_copy_" + utostr(i); |
| 3369 | S += "(" + StructRef; |
| 3370 | S += "*dst, " + StructRef; |
| 3371 | S += "*src) {"; |
| 3372 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
| 3373 | E = ImportedBlockDecls.end(); I != E; ++I) { |
| 3374 | ValueDecl *VD = (*I); |
| 3375 | S += "_Block_object_assign((void*)&dst->"; |
| 3376 | S += (*I)->getNameAsString(); |
| 3377 | S += ", (void*)src->"; |
| 3378 | S += (*I)->getNameAsString(); |
| 3379 | if (BlockByRefDeclsPtrSet.count((*I))) |
| 3380 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
| 3381 | else if (VD->getType()->isBlockPointerType()) |
| 3382 | S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);"; |
| 3383 | else |
| 3384 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
| 3385 | } |
| 3386 | S += "}\n"; |
| 3387 | |
| 3388 | S += "\nstatic void __"; |
| 3389 | S += funcName; |
| 3390 | S += "_block_dispose_" + utostr(i); |
| 3391 | S += "(" + StructRef; |
| 3392 | S += "*src) {"; |
| 3393 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
| 3394 | E = ImportedBlockDecls.end(); I != E; ++I) { |
| 3395 | ValueDecl *VD = (*I); |
| 3396 | S += "_Block_object_dispose((void*)src->"; |
| 3397 | S += (*I)->getNameAsString(); |
| 3398 | if (BlockByRefDeclsPtrSet.count((*I))) |
| 3399 | S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; |
| 3400 | else if (VD->getType()->isBlockPointerType()) |
| 3401 | S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);"; |
| 3402 | else |
| 3403 | S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; |
| 3404 | } |
| 3405 | S += "}\n"; |
| 3406 | return S; |
| 3407 | } |
| 3408 | |
| 3409 | std::string RewriteModernObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 3410 | std::string Desc) { |
| 3411 | std::string S = "\nstruct " + Tag; |
| 3412 | std::string Constructor = " " + Tag; |
| 3413 | |
| 3414 | S += " {\n struct __block_impl impl;\n"; |
| 3415 | S += " struct " + Desc; |
| 3416 | S += "* Desc;\n"; |
| 3417 | |
| 3418 | Constructor += "(void *fp, "; // Invoke function pointer. |
| 3419 | Constructor += "struct " + Desc; // Descriptor pointer. |
| 3420 | Constructor += " *desc"; |
| 3421 | |
| 3422 | if (BlockDeclRefs.size()) { |
| 3423 | // Output all "by copy" declarations. |
| 3424 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 3425 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3426 | S += " "; |
| 3427 | std::string FieldName = (*I)->getNameAsString(); |
| 3428 | std::string ArgName = "_" + FieldName; |
| 3429 | // Handle nested closure invocation. For example: |
| 3430 | // |
| 3431 | // void (^myImportedBlock)(void); |
| 3432 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
| 3433 | // |
| 3434 | // void (^anotherBlock)(void); |
| 3435 | // anotherBlock = ^(void) { |
| 3436 | // myImportedBlock(); // import and invoke the closure |
| 3437 | // }; |
| 3438 | // |
| 3439 | if (isTopLevelBlockPointerType((*I)->getType())) { |
| 3440 | S += "struct __block_impl *"; |
| 3441 | Constructor += ", void *" + ArgName; |
| 3442 | } else { |
| 3443 | QualType QT = (*I)->getType(); |
| 3444 | if (HasLocalVariableExternalStorage(*I)) |
| 3445 | QT = Context->getPointerType(QT); |
| 3446 | QT.getAsStringInternal(FieldName, Context->getPrintingPolicy()); |
| 3447 | QT.getAsStringInternal(ArgName, Context->getPrintingPolicy()); |
| 3448 | Constructor += ", " + ArgName; |
| 3449 | } |
| 3450 | S += FieldName + ";\n"; |
| 3451 | } |
| 3452 | // Output all "by ref" declarations. |
| 3453 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 3454 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3455 | S += " "; |
| 3456 | std::string FieldName = (*I)->getNameAsString(); |
| 3457 | std::string ArgName = "_" + FieldName; |
| 3458 | { |
| 3459 | std::string TypeString; |
| 3460 | RewriteByRefString(TypeString, FieldName, (*I)); |
| 3461 | TypeString += " *"; |
| 3462 | FieldName = TypeString + FieldName; |
| 3463 | ArgName = TypeString + ArgName; |
| 3464 | Constructor += ", " + ArgName; |
| 3465 | } |
| 3466 | S += FieldName + "; // by ref\n"; |
| 3467 | } |
| 3468 | // Finish writing the constructor. |
| 3469 | Constructor += ", int flags=0)"; |
| 3470 | // Initialize all "by copy" arguments. |
| 3471 | bool firsTime = true; |
| 3472 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 3473 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3474 | std::string Name = (*I)->getNameAsString(); |
| 3475 | if (firsTime) { |
| 3476 | Constructor += " : "; |
| 3477 | firsTime = false; |
| 3478 | } |
| 3479 | else |
| 3480 | Constructor += ", "; |
| 3481 | if (isTopLevelBlockPointerType((*I)->getType())) |
| 3482 | Constructor += Name + "((struct __block_impl *)_" + Name + ")"; |
| 3483 | else |
| 3484 | Constructor += Name + "(_" + Name + ")"; |
| 3485 | } |
| 3486 | // Initialize all "by ref" arguments. |
| 3487 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 3488 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3489 | std::string Name = (*I)->getNameAsString(); |
| 3490 | if (firsTime) { |
| 3491 | Constructor += " : "; |
| 3492 | firsTime = false; |
| 3493 | } |
| 3494 | else |
| 3495 | Constructor += ", "; |
| 3496 | Constructor += Name + "(_" + Name + "->__forwarding)"; |
| 3497 | } |
| 3498 | |
| 3499 | Constructor += " {\n"; |
| 3500 | if (GlobalVarDecl) |
| 3501 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 3502 | else |
| 3503 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
| 3504 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 3505 | |
| 3506 | Constructor += " Desc = desc;\n"; |
| 3507 | } else { |
| 3508 | // Finish writing the constructor. |
| 3509 | Constructor += ", int flags=0) {\n"; |
| 3510 | if (GlobalVarDecl) |
| 3511 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 3512 | else |
| 3513 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
| 3514 | Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 3515 | Constructor += " Desc = desc;\n"; |
| 3516 | } |
| 3517 | Constructor += " "; |
| 3518 | Constructor += "}\n"; |
| 3519 | S += Constructor; |
| 3520 | S += "};\n"; |
| 3521 | return S; |
| 3522 | } |
| 3523 | |
| 3524 | std::string RewriteModernObjC::SynthesizeBlockDescriptor(std::string DescTag, |
| 3525 | std::string ImplTag, int i, |
| 3526 | StringRef FunName, |
| 3527 | unsigned hasCopy) { |
| 3528 | std::string S = "\nstatic struct " + DescTag; |
| 3529 | |
| 3530 | S += " {\n unsigned long reserved;\n"; |
| 3531 | S += " unsigned long Block_size;\n"; |
| 3532 | if (hasCopy) { |
| 3533 | S += " void (*copy)(struct "; |
| 3534 | S += ImplTag; S += "*, struct "; |
| 3535 | S += ImplTag; S += "*);\n"; |
| 3536 | |
| 3537 | S += " void (*dispose)(struct "; |
| 3538 | S += ImplTag; S += "*);\n"; |
| 3539 | } |
| 3540 | S += "} "; |
| 3541 | |
| 3542 | S += DescTag + "_DATA = { 0, sizeof(struct "; |
| 3543 | S += ImplTag + ")"; |
| 3544 | if (hasCopy) { |
| 3545 | S += ", __" + FunName.str() + "_block_copy_" + utostr(i); |
| 3546 | S += ", __" + FunName.str() + "_block_dispose_" + utostr(i); |
| 3547 | } |
| 3548 | S += "};\n"; |
| 3549 | return S; |
| 3550 | } |
| 3551 | |
| 3552 | void RewriteModernObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, |
| 3553 | StringRef FunName) { |
| 3554 | // Insert declaration for the function in which block literal is used. |
| 3555 | if (CurFunctionDeclToDeclareForBlock && !Blocks.empty()) |
| 3556 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
| 3557 | bool RewriteSC = (GlobalVarDecl && |
| 3558 | !Blocks.empty() && |
| 3559 | GlobalVarDecl->getStorageClass() == SC_Static && |
| 3560 | GlobalVarDecl->getType().getCVRQualifiers()); |
| 3561 | if (RewriteSC) { |
| 3562 | std::string SC(" void __"); |
| 3563 | SC += GlobalVarDecl->getNameAsString(); |
| 3564 | SC += "() {}"; |
| 3565 | InsertText(FunLocStart, SC); |
| 3566 | } |
| 3567 | |
| 3568 | // Insert closures that were part of the function. |
| 3569 | for (unsigned i = 0, count=0; i < Blocks.size(); i++) { |
| 3570 | CollectBlockDeclRefInfo(Blocks[i]); |
| 3571 | // Need to copy-in the inner copied-in variables not actually used in this |
| 3572 | // block. |
| 3573 | for (int j = 0; j < InnerDeclRefsCount[i]; j++) { |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3574 | DeclRefExpr *Exp = InnerDeclRefs[count++]; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3575 | ValueDecl *VD = Exp->getDecl(); |
| 3576 | BlockDeclRefs.push_back(Exp); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3577 | if (!VD->hasAttr<BlocksAttr>()) { |
| 3578 | if (!BlockByCopyDeclsPtrSet.count(VD)) { |
| 3579 | BlockByCopyDeclsPtrSet.insert(VD); |
| 3580 | BlockByCopyDecls.push_back(VD); |
| 3581 | } |
| 3582 | continue; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3583 | } |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3584 | |
| 3585 | if (!BlockByRefDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3586 | BlockByRefDeclsPtrSet.insert(VD); |
| 3587 | BlockByRefDecls.push_back(VD); |
| 3588 | } |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3589 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3590 | // imported objects in the inner blocks not used in the outer |
| 3591 | // blocks must be copied/disposed in the outer block as well. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3592 | if (VD->getType()->isObjCObjectPointerType() || |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3593 | VD->getType()->isBlockPointerType()) |
| 3594 | ImportedBlockDecls.insert(VD); |
| 3595 | } |
| 3596 | |
| 3597 | std::string ImplTag = "__" + FunName.str() + "_block_impl_" + utostr(i); |
| 3598 | std::string DescTag = "__" + FunName.str() + "_block_desc_" + utostr(i); |
| 3599 | |
| 3600 | std::string CI = SynthesizeBlockImpl(Blocks[i], ImplTag, DescTag); |
| 3601 | |
| 3602 | InsertText(FunLocStart, CI); |
| 3603 | |
| 3604 | std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, ImplTag); |
| 3605 | |
| 3606 | InsertText(FunLocStart, CF); |
| 3607 | |
| 3608 | if (ImportedBlockDecls.size()) { |
| 3609 | std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, ImplTag); |
| 3610 | InsertText(FunLocStart, HF); |
| 3611 | } |
| 3612 | std::string BD = SynthesizeBlockDescriptor(DescTag, ImplTag, i, FunName, |
| 3613 | ImportedBlockDecls.size() > 0); |
| 3614 | InsertText(FunLocStart, BD); |
| 3615 | |
| 3616 | BlockDeclRefs.clear(); |
| 3617 | BlockByRefDecls.clear(); |
| 3618 | BlockByRefDeclsPtrSet.clear(); |
| 3619 | BlockByCopyDecls.clear(); |
| 3620 | BlockByCopyDeclsPtrSet.clear(); |
| 3621 | ImportedBlockDecls.clear(); |
| 3622 | } |
| 3623 | if (RewriteSC) { |
| 3624 | // Must insert any 'const/volatile/static here. Since it has been |
| 3625 | // removed as result of rewriting of block literals. |
| 3626 | std::string SC; |
| 3627 | if (GlobalVarDecl->getStorageClass() == SC_Static) |
| 3628 | SC = "static "; |
| 3629 | if (GlobalVarDecl->getType().isConstQualified()) |
| 3630 | SC += "const "; |
| 3631 | if (GlobalVarDecl->getType().isVolatileQualified()) |
| 3632 | SC += "volatile "; |
| 3633 | if (GlobalVarDecl->getType().isRestrictQualified()) |
| 3634 | SC += "restrict "; |
| 3635 | InsertText(FunLocStart, SC); |
| 3636 | } |
Fariborz Jahanian | df474ec | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 3637 | if (GlobalConstructionExp) { |
| 3638 | // extra fancy dance for global literal expression. |
| 3639 | |
| 3640 | // Always the latest block expression on the block stack. |
| 3641 | std::string Tag = "__"; |
| 3642 | Tag += FunName; |
| 3643 | Tag += "_block_impl_"; |
| 3644 | Tag += utostr(Blocks.size()-1); |
| 3645 | std::string globalBuf = "static "; |
| 3646 | globalBuf += Tag; globalBuf += " "; |
| 3647 | std::string SStr; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3648 | |
Fariborz Jahanian | df474ec | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 3649 | llvm::raw_string_ostream constructorExprBuf(SStr); |
| 3650 | GlobalConstructionExp->printPretty(constructorExprBuf, *Context, 0, |
| 3651 | PrintingPolicy(LangOpts)); |
| 3652 | globalBuf += constructorExprBuf.str(); |
| 3653 | globalBuf += ";\n"; |
| 3654 | InsertText(FunLocStart, globalBuf); |
| 3655 | GlobalConstructionExp = 0; |
| 3656 | } |
| 3657 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3658 | Blocks.clear(); |
| 3659 | InnerDeclRefsCount.clear(); |
| 3660 | InnerDeclRefs.clear(); |
| 3661 | RewrittenBlockExprs.clear(); |
| 3662 | } |
| 3663 | |
| 3664 | void RewriteModernObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { |
| 3665 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
| 3666 | StringRef FuncName = FD->getName(); |
| 3667 | |
| 3668 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 3669 | } |
| 3670 | |
| 3671 | static void BuildUniqueMethodName(std::string &Name, |
| 3672 | ObjCMethodDecl *MD) { |
| 3673 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
| 3674 | Name = IFace->getName(); |
| 3675 | Name += "__" + MD->getSelector().getAsString(); |
| 3676 | // Convert colons to underscores. |
| 3677 | std::string::size_type loc = 0; |
| 3678 | while ((loc = Name.find(":", loc)) != std::string::npos) |
| 3679 | Name.replace(loc, 1, "_"); |
| 3680 | } |
| 3681 | |
| 3682 | void RewriteModernObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { |
| 3683 | //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n"); |
| 3684 | //SourceLocation FunLocStart = MD->getLocStart(); |
| 3685 | SourceLocation FunLocStart = MD->getLocStart(); |
| 3686 | std::string FuncName; |
| 3687 | BuildUniqueMethodName(FuncName, MD); |
| 3688 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 3689 | } |
| 3690 | |
| 3691 | void RewriteModernObjC::GetBlockDeclRefExprs(Stmt *S) { |
| 3692 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
| 3693 | if (*CI) { |
| 3694 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 3695 | GetBlockDeclRefExprs(CBE->getBody()); |
| 3696 | else |
| 3697 | GetBlockDeclRefExprs(*CI); |
| 3698 | } |
| 3699 | // Handle specific things. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3700 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) |
| 3701 | if (DRE->refersToEnclosingLocal() && |
| 3702 | HasLocalVariableExternalStorage(DRE->getDecl())) { |
| 3703 | BlockDeclRefs.push_back(DRE); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3704 | } |
| 3705 | |
| 3706 | return; |
| 3707 | } |
| 3708 | |
| 3709 | void RewriteModernObjC::GetInnerBlockDeclRefExprs(Stmt *S, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3710 | SmallVector<DeclRefExpr *, 8> &InnerBlockDeclRefs, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3711 | llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts) { |
| 3712 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
| 3713 | if (*CI) { |
| 3714 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) { |
| 3715 | InnerContexts.insert(cast<DeclContext>(CBE->getBlockDecl())); |
| 3716 | GetInnerBlockDeclRefExprs(CBE->getBody(), |
| 3717 | InnerBlockDeclRefs, |
| 3718 | InnerContexts); |
| 3719 | } |
| 3720 | else |
| 3721 | GetInnerBlockDeclRefExprs(*CI, |
| 3722 | InnerBlockDeclRefs, |
| 3723 | InnerContexts); |
| 3724 | |
| 3725 | } |
| 3726 | // Handle specific things. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3727 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 3728 | if (DRE->refersToEnclosingLocal()) { |
| 3729 | if (!isa<FunctionDecl>(DRE->getDecl()) && |
| 3730 | !InnerContexts.count(DRE->getDecl()->getDeclContext())) |
| 3731 | InnerBlockDeclRefs.push_back(DRE); |
| 3732 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) |
| 3733 | if (Var->isFunctionOrMethodVarDecl()) |
| 3734 | ImportedLocalExternalDecls.insert(Var); |
| 3735 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3736 | } |
| 3737 | |
| 3738 | return; |
| 3739 | } |
| 3740 | |
Fariborz Jahanian | 164d6f8 | 2012-02-13 18:57:49 +0000 | [diff] [blame] | 3741 | /// convertObjCTypeToCStyleType - This routine converts such objc types |
| 3742 | /// as qualified objects, and blocks to their closest c/c++ types that |
| 3743 | /// it can. It returns true if input type was modified. |
| 3744 | bool RewriteModernObjC::convertObjCTypeToCStyleType(QualType &T) { |
| 3745 | QualType oldT = T; |
| 3746 | convertBlockPointerToFunctionPointer(T); |
| 3747 | if (T->isFunctionPointerType()) { |
| 3748 | QualType PointeeTy; |
| 3749 | if (const PointerType* PT = T->getAs<PointerType>()) { |
| 3750 | PointeeTy = PT->getPointeeType(); |
| 3751 | if (const FunctionType *FT = PointeeTy->getAs<FunctionType>()) { |
| 3752 | T = convertFunctionTypeOfBlocks(FT); |
| 3753 | T = Context->getPointerType(T); |
| 3754 | } |
| 3755 | } |
| 3756 | } |
| 3757 | |
| 3758 | convertToUnqualifiedObjCType(T); |
| 3759 | return T != oldT; |
| 3760 | } |
| 3761 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3762 | /// convertFunctionTypeOfBlocks - This routine converts a function type |
| 3763 | /// whose result type may be a block pointer or whose argument type(s) |
| 3764 | /// might be block pointers to an equivalent function type replacing |
| 3765 | /// all block pointers to function pointers. |
| 3766 | QualType RewriteModernObjC::convertFunctionTypeOfBlocks(const FunctionType *FT) { |
| 3767 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
| 3768 | // FTP will be null for closures that don't take arguments. |
| 3769 | // Generate a funky cast. |
| 3770 | SmallVector<QualType, 8> ArgTypes; |
| 3771 | QualType Res = FT->getResultType(); |
Fariborz Jahanian | 164d6f8 | 2012-02-13 18:57:49 +0000 | [diff] [blame] | 3772 | bool modified = convertObjCTypeToCStyleType(Res); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3773 | |
| 3774 | if (FTP) { |
| 3775 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
| 3776 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 3777 | QualType t = *I; |
| 3778 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Fariborz Jahanian | 164d6f8 | 2012-02-13 18:57:49 +0000 | [diff] [blame] | 3779 | if (convertObjCTypeToCStyleType(t)) |
| 3780 | modified = true; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3781 | ArgTypes.push_back(t); |
| 3782 | } |
| 3783 | } |
| 3784 | QualType FuncType; |
Fariborz Jahanian | 164d6f8 | 2012-02-13 18:57:49 +0000 | [diff] [blame] | 3785 | if (modified) |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3786 | FuncType = getSimpleFunctionType(Res, &ArgTypes[0], ArgTypes.size()); |
| 3787 | else FuncType = QualType(FT, 0); |
| 3788 | return FuncType; |
| 3789 | } |
| 3790 | |
| 3791 | Stmt *RewriteModernObjC::SynthesizeBlockCall(CallExpr *Exp, const Expr *BlockExp) { |
| 3792 | // Navigate to relevant type information. |
| 3793 | const BlockPointerType *CPT = 0; |
| 3794 | |
| 3795 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BlockExp)) { |
| 3796 | CPT = DRE->getType()->getAs<BlockPointerType>(); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3797 | } else if (const MemberExpr *MExpr = dyn_cast<MemberExpr>(BlockExp)) { |
| 3798 | CPT = MExpr->getType()->getAs<BlockPointerType>(); |
| 3799 | } |
| 3800 | else if (const ParenExpr *PRE = dyn_cast<ParenExpr>(BlockExp)) { |
| 3801 | return SynthesizeBlockCall(Exp, PRE->getSubExpr()); |
| 3802 | } |
| 3803 | else if (const ImplicitCastExpr *IEXPR = dyn_cast<ImplicitCastExpr>(BlockExp)) |
| 3804 | CPT = IEXPR->getType()->getAs<BlockPointerType>(); |
| 3805 | else if (const ConditionalOperator *CEXPR = |
| 3806 | dyn_cast<ConditionalOperator>(BlockExp)) { |
| 3807 | Expr *LHSExp = CEXPR->getLHS(); |
| 3808 | Stmt *LHSStmt = SynthesizeBlockCall(Exp, LHSExp); |
| 3809 | Expr *RHSExp = CEXPR->getRHS(); |
| 3810 | Stmt *RHSStmt = SynthesizeBlockCall(Exp, RHSExp); |
| 3811 | Expr *CONDExp = CEXPR->getCond(); |
| 3812 | ConditionalOperator *CondExpr = |
| 3813 | new (Context) ConditionalOperator(CONDExp, |
| 3814 | SourceLocation(), cast<Expr>(LHSStmt), |
| 3815 | SourceLocation(), cast<Expr>(RHSStmt), |
| 3816 | Exp->getType(), VK_RValue, OK_Ordinary); |
| 3817 | return CondExpr; |
| 3818 | } else if (const ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(BlockExp)) { |
| 3819 | CPT = IRE->getType()->getAs<BlockPointerType>(); |
| 3820 | } else if (const PseudoObjectExpr *POE |
| 3821 | = dyn_cast<PseudoObjectExpr>(BlockExp)) { |
| 3822 | CPT = POE->getType()->castAs<BlockPointerType>(); |
| 3823 | } else { |
| 3824 | assert(1 && "RewriteBlockClass: Bad type"); |
| 3825 | } |
| 3826 | assert(CPT && "RewriteBlockClass: Bad type"); |
| 3827 | const FunctionType *FT = CPT->getPointeeType()->getAs<FunctionType>(); |
| 3828 | assert(FT && "RewriteBlockClass: Bad type"); |
| 3829 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
| 3830 | // FTP will be null for closures that don't take arguments. |
| 3831 | |
| 3832 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 3833 | SourceLocation(), SourceLocation(), |
| 3834 | &Context->Idents.get("__block_impl")); |
| 3835 | QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD)); |
| 3836 | |
| 3837 | // Generate a funky cast. |
| 3838 | SmallVector<QualType, 8> ArgTypes; |
| 3839 | |
| 3840 | // Push the block argument type. |
| 3841 | ArgTypes.push_back(PtrBlock); |
| 3842 | if (FTP) { |
| 3843 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
| 3844 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 3845 | QualType t = *I; |
| 3846 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
| 3847 | if (!convertBlockPointerToFunctionPointer(t)) |
| 3848 | convertToUnqualifiedObjCType(t); |
| 3849 | ArgTypes.push_back(t); |
| 3850 | } |
| 3851 | } |
| 3852 | // Now do the pointer to function cast. |
| 3853 | QualType PtrToFuncCastType |
| 3854 | = getSimpleFunctionType(Exp->getType(), &ArgTypes[0], ArgTypes.size()); |
| 3855 | |
| 3856 | PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); |
| 3857 | |
| 3858 | CastExpr *BlkCast = NoTypeInfoCStyleCastExpr(Context, PtrBlock, |
| 3859 | CK_BitCast, |
| 3860 | const_cast<Expr*>(BlockExp)); |
| 3861 | // Don't forget the parens to enforce the proper binding. |
| 3862 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3863 | BlkCast); |
| 3864 | //PE->dump(); |
| 3865 | |
| 3866 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 3867 | SourceLocation(), |
| 3868 | &Context->Idents.get("FuncPtr"), |
| 3869 | Context->VoidPtrTy, 0, |
| 3870 | /*BitWidth=*/0, /*Mutable=*/true, |
| 3871 | /*HasInit=*/false); |
| 3872 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
| 3873 | FD->getType(), VK_LValue, |
| 3874 | OK_Ordinary); |
| 3875 | |
| 3876 | |
| 3877 | CastExpr *FunkCast = NoTypeInfoCStyleCastExpr(Context, PtrToFuncCastType, |
| 3878 | CK_BitCast, ME); |
| 3879 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast); |
| 3880 | |
| 3881 | SmallVector<Expr*, 8> BlkExprs; |
| 3882 | // Add the implicit argument. |
| 3883 | BlkExprs.push_back(BlkCast); |
| 3884 | // Add the user arguments. |
| 3885 | for (CallExpr::arg_iterator I = Exp->arg_begin(), |
| 3886 | E = Exp->arg_end(); I != E; ++I) { |
| 3887 | BlkExprs.push_back(*I); |
| 3888 | } |
| 3889 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &BlkExprs[0], |
| 3890 | BlkExprs.size(), |
| 3891 | Exp->getType(), VK_RValue, |
| 3892 | SourceLocation()); |
| 3893 | return CE; |
| 3894 | } |
| 3895 | |
| 3896 | // We need to return the rewritten expression to handle cases where the |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3897 | // DeclRefExpr is embedded in another expression being rewritten. |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3898 | // For example: |
| 3899 | // |
| 3900 | // int main() { |
| 3901 | // __block Foo *f; |
| 3902 | // __block int i; |
| 3903 | // |
| 3904 | // void (^myblock)() = ^() { |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3905 | // [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] | 3906 | // i = 77; |
| 3907 | // }; |
| 3908 | //} |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3909 | Stmt *RewriteModernObjC::RewriteBlockDeclRefExpr(DeclRefExpr *DeclRefExp) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3910 | // Rewrite the byref variable into BYREFVAR->__forwarding->BYREFVAR |
| 3911 | // for each DeclRefExp where BYREFVAR is name of the variable. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3912 | ValueDecl *VD = DeclRefExp->getDecl(); |
| 3913 | bool isArrow = DeclRefExp->refersToEnclosingLocal(); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 3914 | |
| 3915 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 3916 | SourceLocation(), |
| 3917 | &Context->Idents.get("__forwarding"), |
| 3918 | Context->VoidPtrTy, 0, |
| 3919 | /*BitWidth=*/0, /*Mutable=*/true, |
| 3920 | /*HasInit=*/false); |
| 3921 | MemberExpr *ME = new (Context) MemberExpr(DeclRefExp, isArrow, |
| 3922 | FD, SourceLocation(), |
| 3923 | FD->getType(), VK_LValue, |
| 3924 | OK_Ordinary); |
| 3925 | |
| 3926 | StringRef Name = VD->getName(); |
| 3927 | FD = FieldDecl::Create(*Context, 0, SourceLocation(), SourceLocation(), |
| 3928 | &Context->Idents.get(Name), |
| 3929 | Context->VoidPtrTy, 0, |
| 3930 | /*BitWidth=*/0, /*Mutable=*/true, |
| 3931 | /*HasInit=*/false); |
| 3932 | ME = new (Context) MemberExpr(ME, true, FD, SourceLocation(), |
| 3933 | DeclRefExp->getType(), VK_LValue, OK_Ordinary); |
| 3934 | |
| 3935 | |
| 3936 | |
| 3937 | // Need parens to enforce precedence. |
| 3938 | ParenExpr *PE = new (Context) ParenExpr(DeclRefExp->getExprLoc(), |
| 3939 | DeclRefExp->getExprLoc(), |
| 3940 | ME); |
| 3941 | ReplaceStmt(DeclRefExp, PE); |
| 3942 | return PE; |
| 3943 | } |
| 3944 | |
| 3945 | // Rewrites the imported local variable V with external storage |
| 3946 | // (static, extern, etc.) as *V |
| 3947 | // |
| 3948 | Stmt *RewriteModernObjC::RewriteLocalVariableExternalStorage(DeclRefExpr *DRE) { |
| 3949 | ValueDecl *VD = DRE->getDecl(); |
| 3950 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 3951 | if (!ImportedLocalExternalDecls.count(Var)) |
| 3952 | return DRE; |
| 3953 | Expr *Exp = new (Context) UnaryOperator(DRE, UO_Deref, DRE->getType(), |
| 3954 | VK_LValue, OK_Ordinary, |
| 3955 | DRE->getLocation()); |
| 3956 | // Need parens to enforce precedence. |
| 3957 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3958 | Exp); |
| 3959 | ReplaceStmt(DRE, PE); |
| 3960 | return PE; |
| 3961 | } |
| 3962 | |
| 3963 | void RewriteModernObjC::RewriteCastExpr(CStyleCastExpr *CE) { |
| 3964 | SourceLocation LocStart = CE->getLParenLoc(); |
| 3965 | SourceLocation LocEnd = CE->getRParenLoc(); |
| 3966 | |
| 3967 | // Need to avoid trying to rewrite synthesized casts. |
| 3968 | if (LocStart.isInvalid()) |
| 3969 | return; |
| 3970 | // Need to avoid trying to rewrite casts contained in macros. |
| 3971 | if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) |
| 3972 | return; |
| 3973 | |
| 3974 | const char *startBuf = SM->getCharacterData(LocStart); |
| 3975 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 3976 | QualType QT = CE->getType(); |
| 3977 | const Type* TypePtr = QT->getAs<Type>(); |
| 3978 | if (isa<TypeOfExprType>(TypePtr)) { |
| 3979 | const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); |
| 3980 | QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); |
| 3981 | std::string TypeAsString = "("; |
| 3982 | RewriteBlockPointerType(TypeAsString, QT); |
| 3983 | TypeAsString += ")"; |
| 3984 | ReplaceText(LocStart, endBuf-startBuf+1, TypeAsString); |
| 3985 | return; |
| 3986 | } |
| 3987 | // advance the location to startArgList. |
| 3988 | const char *argPtr = startBuf; |
| 3989 | |
| 3990 | while (*argPtr++ && (argPtr < endBuf)) { |
| 3991 | switch (*argPtr) { |
| 3992 | case '^': |
| 3993 | // Replace the '^' with '*'. |
| 3994 | LocStart = LocStart.getLocWithOffset(argPtr-startBuf); |
| 3995 | ReplaceText(LocStart, 1, "*"); |
| 3996 | break; |
| 3997 | } |
| 3998 | } |
| 3999 | return; |
| 4000 | } |
| 4001 | |
| 4002 | void RewriteModernObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { |
| 4003 | SourceLocation DeclLoc = FD->getLocation(); |
| 4004 | unsigned parenCount = 0; |
| 4005 | |
| 4006 | // We have 1 or more arguments that have closure pointers. |
| 4007 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4008 | const char *startArgList = strchr(startBuf, '('); |
| 4009 | |
| 4010 | assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); |
| 4011 | |
| 4012 | parenCount++; |
| 4013 | // advance the location to startArgList. |
| 4014 | DeclLoc = DeclLoc.getLocWithOffset(startArgList-startBuf); |
| 4015 | assert((DeclLoc.isValid()) && "Invalid DeclLoc"); |
| 4016 | |
| 4017 | const char *argPtr = startArgList; |
| 4018 | |
| 4019 | while (*argPtr++ && parenCount) { |
| 4020 | switch (*argPtr) { |
| 4021 | case '^': |
| 4022 | // Replace the '^' with '*'. |
| 4023 | DeclLoc = DeclLoc.getLocWithOffset(argPtr-startArgList); |
| 4024 | ReplaceText(DeclLoc, 1, "*"); |
| 4025 | break; |
| 4026 | case '(': |
| 4027 | parenCount++; |
| 4028 | break; |
| 4029 | case ')': |
| 4030 | parenCount--; |
| 4031 | break; |
| 4032 | } |
| 4033 | } |
| 4034 | return; |
| 4035 | } |
| 4036 | |
| 4037 | bool RewriteModernObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { |
| 4038 | const FunctionProtoType *FTP; |
| 4039 | const PointerType *PT = QT->getAs<PointerType>(); |
| 4040 | if (PT) { |
| 4041 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4042 | } else { |
| 4043 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
| 4044 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
| 4045 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4046 | } |
| 4047 | if (FTP) { |
| 4048 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
| 4049 | E = FTP->arg_type_end(); I != E; ++I) |
| 4050 | if (isTopLevelBlockPointerType(*I)) |
| 4051 | return true; |
| 4052 | } |
| 4053 | return false; |
| 4054 | } |
| 4055 | |
| 4056 | bool RewriteModernObjC::PointerTypeTakesAnyObjCQualifiedType(QualType QT) { |
| 4057 | const FunctionProtoType *FTP; |
| 4058 | const PointerType *PT = QT->getAs<PointerType>(); |
| 4059 | if (PT) { |
| 4060 | FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4061 | } else { |
| 4062 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
| 4063 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
| 4064 | FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 4065 | } |
| 4066 | if (FTP) { |
| 4067 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
| 4068 | E = FTP->arg_type_end(); I != E; ++I) { |
| 4069 | if ((*I)->isObjCQualifiedIdType()) |
| 4070 | return true; |
| 4071 | if ((*I)->isObjCObjectPointerType() && |
| 4072 | (*I)->getPointeeType()->isObjCQualifiedInterfaceType()) |
| 4073 | return true; |
| 4074 | } |
| 4075 | |
| 4076 | } |
| 4077 | return false; |
| 4078 | } |
| 4079 | |
| 4080 | void RewriteModernObjC::GetExtentOfArgList(const char *Name, const char *&LParen, |
| 4081 | const char *&RParen) { |
| 4082 | const char *argPtr = strchr(Name, '('); |
| 4083 | assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); |
| 4084 | |
| 4085 | LParen = argPtr; // output the start. |
| 4086 | argPtr++; // skip past the left paren. |
| 4087 | unsigned parenCount = 1; |
| 4088 | |
| 4089 | while (*argPtr && parenCount) { |
| 4090 | switch (*argPtr) { |
| 4091 | case '(': parenCount++; break; |
| 4092 | case ')': parenCount--; break; |
| 4093 | default: break; |
| 4094 | } |
| 4095 | if (parenCount) argPtr++; |
| 4096 | } |
| 4097 | assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); |
| 4098 | RParen = argPtr; // output the end |
| 4099 | } |
| 4100 | |
| 4101 | void RewriteModernObjC::RewriteBlockPointerDecl(NamedDecl *ND) { |
| 4102 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 4103 | RewriteBlockPointerFunctionArgs(FD); |
| 4104 | return; |
| 4105 | } |
| 4106 | // Handle Variables and Typedefs. |
| 4107 | SourceLocation DeclLoc = ND->getLocation(); |
| 4108 | QualType DeclT; |
| 4109 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4110 | DeclT = VD->getType(); |
| 4111 | else if (TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(ND)) |
| 4112 | DeclT = TDD->getUnderlyingType(); |
| 4113 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) |
| 4114 | DeclT = FD->getType(); |
| 4115 | else |
| 4116 | llvm_unreachable("RewriteBlockPointerDecl(): Decl type not yet handled"); |
| 4117 | |
| 4118 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4119 | const char *endBuf = startBuf; |
| 4120 | // scan backward (from the decl location) for the end of the previous decl. |
| 4121 | while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) |
| 4122 | startBuf--; |
| 4123 | SourceLocation Start = DeclLoc.getLocWithOffset(startBuf-endBuf); |
| 4124 | std::string buf; |
| 4125 | unsigned OrigLength=0; |
| 4126 | // *startBuf != '^' if we are dealing with a pointer to function that |
| 4127 | // may take block argument types (which will be handled below). |
| 4128 | if (*startBuf == '^') { |
| 4129 | // Replace the '^' with '*', computing a negative offset. |
| 4130 | buf = '*'; |
| 4131 | startBuf++; |
| 4132 | OrigLength++; |
| 4133 | } |
| 4134 | while (*startBuf != ')') { |
| 4135 | buf += *startBuf; |
| 4136 | startBuf++; |
| 4137 | OrigLength++; |
| 4138 | } |
| 4139 | buf += ')'; |
| 4140 | OrigLength++; |
| 4141 | |
| 4142 | if (PointerTypeTakesAnyBlockArguments(DeclT) || |
| 4143 | PointerTypeTakesAnyObjCQualifiedType(DeclT)) { |
| 4144 | // Replace the '^' with '*' for arguments. |
| 4145 | // Replace id<P> with id/*<>*/ |
| 4146 | DeclLoc = ND->getLocation(); |
| 4147 | startBuf = SM->getCharacterData(DeclLoc); |
| 4148 | const char *argListBegin, *argListEnd; |
| 4149 | GetExtentOfArgList(startBuf, argListBegin, argListEnd); |
| 4150 | while (argListBegin < argListEnd) { |
| 4151 | if (*argListBegin == '^') |
| 4152 | buf += '*'; |
| 4153 | else if (*argListBegin == '<') { |
| 4154 | buf += "/*"; |
| 4155 | buf += *argListBegin++; |
| 4156 | OrigLength++;; |
| 4157 | while (*argListBegin != '>') { |
| 4158 | buf += *argListBegin++; |
| 4159 | OrigLength++; |
| 4160 | } |
| 4161 | buf += *argListBegin; |
| 4162 | buf += "*/"; |
| 4163 | } |
| 4164 | else |
| 4165 | buf += *argListBegin; |
| 4166 | argListBegin++; |
| 4167 | OrigLength++; |
| 4168 | } |
| 4169 | buf += ')'; |
| 4170 | OrigLength++; |
| 4171 | } |
| 4172 | ReplaceText(Start, OrigLength, buf); |
| 4173 | |
| 4174 | return; |
| 4175 | } |
| 4176 | |
| 4177 | |
| 4178 | /// SynthesizeByrefCopyDestroyHelper - This routine synthesizes: |
| 4179 | /// void __Block_byref_id_object_copy(struct Block_byref_id_object *dst, |
| 4180 | /// struct Block_byref_id_object *src) { |
| 4181 | /// _Block_object_assign (&_dest->object, _src->object, |
| 4182 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4183 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4184 | /// _Block_object_assign(&_dest->object, _src->object, |
| 4185 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4186 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4187 | /// } |
| 4188 | /// And: |
| 4189 | /// void __Block_byref_id_object_dispose(struct Block_byref_id_object *_src) { |
| 4190 | /// _Block_object_dispose(_src->object, |
| 4191 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT |
| 4192 | /// [|BLOCK_FIELD_IS_WEAK]) // object |
| 4193 | /// _Block_object_dispose(_src->object, |
| 4194 | /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK |
| 4195 | /// [|BLOCK_FIELD_IS_WEAK]) // block |
| 4196 | /// } |
| 4197 | |
| 4198 | std::string RewriteModernObjC::SynthesizeByrefCopyDestroyHelper(VarDecl *VD, |
| 4199 | int flag) { |
| 4200 | std::string S; |
| 4201 | if (CopyDestroyCache.count(flag)) |
| 4202 | return S; |
| 4203 | CopyDestroyCache.insert(flag); |
| 4204 | S = "static void __Block_byref_id_object_copy_"; |
| 4205 | S += utostr(flag); |
| 4206 | S += "(void *dst, void *src) {\n"; |
| 4207 | |
| 4208 | // offset into the object pointer is computed as: |
| 4209 | // void * + void* + int + int + void* + void * |
| 4210 | unsigned IntSize = |
| 4211 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 4212 | unsigned VoidPtrSize = |
| 4213 | static_cast<unsigned>(Context->getTypeSize(Context->VoidPtrTy)); |
| 4214 | |
| 4215 | unsigned offset = (VoidPtrSize*4 + IntSize + IntSize)/Context->getCharWidth(); |
| 4216 | S += " _Block_object_assign((char*)dst + "; |
| 4217 | S += utostr(offset); |
| 4218 | S += ", *(void * *) ((char*)src + "; |
| 4219 | S += utostr(offset); |
| 4220 | S += "), "; |
| 4221 | S += utostr(flag); |
| 4222 | S += ");\n}\n"; |
| 4223 | |
| 4224 | S += "static void __Block_byref_id_object_dispose_"; |
| 4225 | S += utostr(flag); |
| 4226 | S += "(void *src) {\n"; |
| 4227 | S += " _Block_object_dispose(*(void * *) ((char*)src + "; |
| 4228 | S += utostr(offset); |
| 4229 | S += "), "; |
| 4230 | S += utostr(flag); |
| 4231 | S += ");\n}\n"; |
| 4232 | return S; |
| 4233 | } |
| 4234 | |
| 4235 | /// RewriteByRefVar - For each __block typex ND variable this routine transforms |
| 4236 | /// the declaration into: |
| 4237 | /// struct __Block_byref_ND { |
| 4238 | /// void *__isa; // NULL for everything except __weak pointers |
| 4239 | /// struct __Block_byref_ND *__forwarding; |
| 4240 | /// int32_t __flags; |
| 4241 | /// int32_t __size; |
| 4242 | /// void *__Block_byref_id_object_copy; // If variable is __block ObjC object |
| 4243 | /// void *__Block_byref_id_object_dispose; // If variable is __block ObjC object |
| 4244 | /// typex ND; |
| 4245 | /// }; |
| 4246 | /// |
| 4247 | /// It then replaces declaration of ND variable with: |
| 4248 | /// struct __Block_byref_ND ND = {__isa=0B, __forwarding=&ND, __flags=some_flag, |
| 4249 | /// __size=sizeof(struct __Block_byref_ND), |
| 4250 | /// ND=initializer-if-any}; |
| 4251 | /// |
| 4252 | /// |
| 4253 | void RewriteModernObjC::RewriteByRefVar(VarDecl *ND) { |
| 4254 | // Insert declaration for the function in which block literal is |
| 4255 | // used. |
| 4256 | if (CurFunctionDeclToDeclareForBlock) |
| 4257 | RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); |
| 4258 | int flag = 0; |
| 4259 | int isa = 0; |
| 4260 | SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); |
| 4261 | if (DeclLoc.isInvalid()) |
| 4262 | // If type location is missing, it is because of missing type (a warning). |
| 4263 | // Use variable's location which is good for this case. |
| 4264 | DeclLoc = ND->getLocation(); |
| 4265 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4266 | SourceLocation X = ND->getLocEnd(); |
| 4267 | X = SM->getExpansionLoc(X); |
| 4268 | const char *endBuf = SM->getCharacterData(X); |
| 4269 | std::string Name(ND->getNameAsString()); |
| 4270 | std::string ByrefType; |
| 4271 | RewriteByRefString(ByrefType, Name, ND, true); |
| 4272 | ByrefType += " {\n"; |
| 4273 | ByrefType += " void *__isa;\n"; |
| 4274 | RewriteByRefString(ByrefType, Name, ND); |
| 4275 | ByrefType += " *__forwarding;\n"; |
| 4276 | ByrefType += " int __flags;\n"; |
| 4277 | ByrefType += " int __size;\n"; |
| 4278 | // Add void *__Block_byref_id_object_copy; |
| 4279 | // void *__Block_byref_id_object_dispose; if needed. |
| 4280 | QualType Ty = ND->getType(); |
| 4281 | bool HasCopyAndDispose = Context->BlockRequiresCopying(Ty); |
| 4282 | if (HasCopyAndDispose) { |
| 4283 | ByrefType += " void (*__Block_byref_id_object_copy)(void*, void*);\n"; |
| 4284 | ByrefType += " void (*__Block_byref_id_object_dispose)(void*);\n"; |
| 4285 | } |
| 4286 | |
| 4287 | QualType T = Ty; |
| 4288 | (void)convertBlockPointerToFunctionPointer(T); |
| 4289 | T.getAsStringInternal(Name, Context->getPrintingPolicy()); |
| 4290 | |
| 4291 | ByrefType += " " + Name + ";\n"; |
| 4292 | ByrefType += "};\n"; |
| 4293 | // Insert this type in global scope. It is needed by helper function. |
| 4294 | SourceLocation FunLocStart; |
| 4295 | if (CurFunctionDef) |
| 4296 | FunLocStart = CurFunctionDef->getTypeSpecStartLoc(); |
| 4297 | else { |
| 4298 | assert(CurMethodDef && "RewriteByRefVar - CurMethodDef is null"); |
| 4299 | FunLocStart = CurMethodDef->getLocStart(); |
| 4300 | } |
| 4301 | InsertText(FunLocStart, ByrefType); |
| 4302 | if (Ty.isObjCGCWeak()) { |
| 4303 | flag |= BLOCK_FIELD_IS_WEAK; |
| 4304 | isa = 1; |
| 4305 | } |
| 4306 | |
| 4307 | if (HasCopyAndDispose) { |
| 4308 | flag = BLOCK_BYREF_CALLER; |
| 4309 | QualType Ty = ND->getType(); |
| 4310 | // FIXME. Handle __weak variable (BLOCK_FIELD_IS_WEAK) as well. |
| 4311 | if (Ty->isBlockPointerType()) |
| 4312 | flag |= BLOCK_FIELD_IS_BLOCK; |
| 4313 | else |
| 4314 | flag |= BLOCK_FIELD_IS_OBJECT; |
| 4315 | std::string HF = SynthesizeByrefCopyDestroyHelper(ND, flag); |
| 4316 | if (!HF.empty()) |
| 4317 | InsertText(FunLocStart, HF); |
| 4318 | } |
| 4319 | |
| 4320 | // struct __Block_byref_ND ND = |
| 4321 | // {0, &ND, some_flag, __size=sizeof(struct __Block_byref_ND), |
| 4322 | // initializer-if-any}; |
| 4323 | bool hasInit = (ND->getInit() != 0); |
| 4324 | unsigned flags = 0; |
| 4325 | if (HasCopyAndDispose) |
| 4326 | flags |= BLOCK_HAS_COPY_DISPOSE; |
| 4327 | Name = ND->getNameAsString(); |
| 4328 | ByrefType.clear(); |
| 4329 | RewriteByRefString(ByrefType, Name, ND); |
| 4330 | std::string ForwardingCastType("("); |
| 4331 | ForwardingCastType += ByrefType + " *)"; |
| 4332 | if (!hasInit) { |
| 4333 | ByrefType += " " + Name + " = {(void*)"; |
| 4334 | ByrefType += utostr(isa); |
| 4335 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
| 4336 | ByrefType += utostr(flags); |
| 4337 | ByrefType += ", "; |
| 4338 | ByrefType += "sizeof("; |
| 4339 | RewriteByRefString(ByrefType, Name, ND); |
| 4340 | ByrefType += ")"; |
| 4341 | if (HasCopyAndDispose) { |
| 4342 | ByrefType += ", __Block_byref_id_object_copy_"; |
| 4343 | ByrefType += utostr(flag); |
| 4344 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 4345 | ByrefType += utostr(flag); |
| 4346 | } |
| 4347 | ByrefType += "};\n"; |
| 4348 | unsigned nameSize = Name.size(); |
| 4349 | // for block or function pointer declaration. Name is aleady |
| 4350 | // part of the declaration. |
| 4351 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) |
| 4352 | nameSize = 1; |
| 4353 | ReplaceText(DeclLoc, endBuf-startBuf+nameSize, ByrefType); |
| 4354 | } |
| 4355 | else { |
| 4356 | SourceLocation startLoc; |
| 4357 | Expr *E = ND->getInit(); |
| 4358 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) |
| 4359 | startLoc = ECE->getLParenLoc(); |
| 4360 | else |
| 4361 | startLoc = E->getLocStart(); |
| 4362 | startLoc = SM->getExpansionLoc(startLoc); |
| 4363 | endBuf = SM->getCharacterData(startLoc); |
| 4364 | ByrefType += " " + Name; |
| 4365 | ByrefType += " = {(void*)"; |
| 4366 | ByrefType += utostr(isa); |
| 4367 | ByrefType += "," + ForwardingCastType + "&" + Name + ", "; |
| 4368 | ByrefType += utostr(flags); |
| 4369 | ByrefType += ", "; |
| 4370 | ByrefType += "sizeof("; |
| 4371 | RewriteByRefString(ByrefType, Name, ND); |
| 4372 | ByrefType += "), "; |
| 4373 | if (HasCopyAndDispose) { |
| 4374 | ByrefType += "__Block_byref_id_object_copy_"; |
| 4375 | ByrefType += utostr(flag); |
| 4376 | ByrefType += ", __Block_byref_id_object_dispose_"; |
| 4377 | ByrefType += utostr(flag); |
| 4378 | ByrefType += ", "; |
| 4379 | } |
| 4380 | ReplaceText(DeclLoc, endBuf-startBuf, ByrefType); |
| 4381 | |
| 4382 | // Complete the newly synthesized compound expression by inserting a right |
| 4383 | // curly brace before the end of the declaration. |
| 4384 | // FIXME: This approach avoids rewriting the initializer expression. It |
| 4385 | // also assumes there is only one declarator. For example, the following |
| 4386 | // isn't currently supported by this routine (in general): |
| 4387 | // |
| 4388 | // double __block BYREFVAR = 1.34, BYREFVAR2 = 1.37; |
| 4389 | // |
| 4390 | const char *startInitializerBuf = SM->getCharacterData(startLoc); |
| 4391 | const char *semiBuf = strchr(startInitializerBuf, ';'); |
| 4392 | assert((*semiBuf == ';') && "RewriteByRefVar: can't find ';'"); |
| 4393 | SourceLocation semiLoc = |
| 4394 | startLoc.getLocWithOffset(semiBuf-startInitializerBuf); |
| 4395 | |
| 4396 | InsertText(semiLoc, "}"); |
| 4397 | } |
| 4398 | return; |
| 4399 | } |
| 4400 | |
| 4401 | void RewriteModernObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { |
| 4402 | // Add initializers for any closure decl refs. |
| 4403 | GetBlockDeclRefExprs(Exp->getBody()); |
| 4404 | if (BlockDeclRefs.size()) { |
| 4405 | // Unique all "by copy" declarations. |
| 4406 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4407 | if (!BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>()) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4408 | if (!BlockByCopyDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 4409 | BlockByCopyDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 4410 | BlockByCopyDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 4411 | } |
| 4412 | } |
| 4413 | // Unique all "by ref" declarations. |
| 4414 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4415 | if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>()) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4416 | if (!BlockByRefDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { |
| 4417 | BlockByRefDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); |
| 4418 | BlockByRefDecls.push_back(BlockDeclRefs[i]->getDecl()); |
| 4419 | } |
| 4420 | } |
| 4421 | // Find any imported blocks...they will need special attention. |
| 4422 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4423 | if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>() || |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4424 | BlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
| 4425 | BlockDeclRefs[i]->getType()->isBlockPointerType()) |
| 4426 | ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 4427 | } |
| 4428 | } |
| 4429 | |
| 4430 | FunctionDecl *RewriteModernObjC::SynthBlockInitFunctionDecl(StringRef name) { |
| 4431 | IdentifierInfo *ID = &Context->Idents.get(name); |
| 4432 | QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy); |
| 4433 | return FunctionDecl::Create(*Context, TUDecl, SourceLocation(), |
| 4434 | SourceLocation(), ID, FType, 0, SC_Extern, |
| 4435 | SC_None, false, false); |
| 4436 | } |
| 4437 | |
| 4438 | Stmt *RewriteModernObjC::SynthBlockInitExpr(BlockExpr *Exp, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4439 | const SmallVector<DeclRefExpr *, 8> &InnerBlockDeclRefs) { |
Fariborz Jahanian | d13c2c2 | 2012-03-22 19:54:39 +0000 | [diff] [blame] | 4440 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4441 | const BlockDecl *block = Exp->getBlockDecl(); |
Fariborz Jahanian | d13c2c2 | 2012-03-22 19:54:39 +0000 | [diff] [blame] | 4442 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4443 | Blocks.push_back(Exp); |
| 4444 | |
| 4445 | CollectBlockDeclRefInfo(Exp); |
| 4446 | |
| 4447 | // Add inner imported variables now used in current block. |
| 4448 | int countOfInnerDecls = 0; |
| 4449 | if (!InnerBlockDeclRefs.empty()) { |
| 4450 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) { |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4451 | DeclRefExpr *Exp = InnerBlockDeclRefs[i]; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4452 | ValueDecl *VD = Exp->getDecl(); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4453 | if (!VD->hasAttr<BlocksAttr>() && !BlockByCopyDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4454 | // We need to save the copied-in variables in nested |
| 4455 | // blocks because it is needed at the end for some of the API generations. |
| 4456 | // See SynthesizeBlockLiterals routine. |
| 4457 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 4458 | BlockDeclRefs.push_back(Exp); |
| 4459 | BlockByCopyDeclsPtrSet.insert(VD); |
| 4460 | BlockByCopyDecls.push_back(VD); |
| 4461 | } |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4462 | if (VD->hasAttr<BlocksAttr>() && !BlockByRefDeclsPtrSet.count(VD)) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4463 | InnerDeclRefs.push_back(Exp); countOfInnerDecls++; |
| 4464 | BlockDeclRefs.push_back(Exp); |
| 4465 | BlockByRefDeclsPtrSet.insert(VD); |
| 4466 | BlockByRefDecls.push_back(VD); |
| 4467 | } |
| 4468 | } |
| 4469 | // Find any imported blocks...they will need special attention. |
| 4470 | for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4471 | if (InnerBlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>() || |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4472 | InnerBlockDeclRefs[i]->getType()->isObjCObjectPointerType() || |
| 4473 | InnerBlockDeclRefs[i]->getType()->isBlockPointerType()) |
| 4474 | ImportedBlockDecls.insert(InnerBlockDeclRefs[i]->getDecl()); |
| 4475 | } |
| 4476 | InnerDeclRefsCount.push_back(countOfInnerDecls); |
| 4477 | |
| 4478 | std::string FuncName; |
| 4479 | |
| 4480 | if (CurFunctionDef) |
| 4481 | FuncName = CurFunctionDef->getNameAsString(); |
| 4482 | else if (CurMethodDef) |
| 4483 | BuildUniqueMethodName(FuncName, CurMethodDef); |
| 4484 | else if (GlobalVarDecl) |
| 4485 | FuncName = std::string(GlobalVarDecl->getNameAsString()); |
| 4486 | |
Fariborz Jahanian | df474ec | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 4487 | bool GlobalBlockExpr = |
| 4488 | block->getDeclContext()->getRedeclContext()->isFileContext(); |
| 4489 | |
| 4490 | if (GlobalBlockExpr && !GlobalVarDecl) { |
| 4491 | Diags.Report(block->getLocation(), GlobalBlockRewriteFailedDiag); |
| 4492 | GlobalBlockExpr = false; |
| 4493 | } |
| 4494 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4495 | std::string BlockNumber = utostr(Blocks.size()-1); |
| 4496 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4497 | std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; |
| 4498 | |
| 4499 | // Get a pointer to the function type so we can cast appropriately. |
| 4500 | QualType BFT = convertFunctionTypeOfBlocks(Exp->getFunctionType()); |
| 4501 | QualType FType = Context->getPointerType(BFT); |
| 4502 | |
| 4503 | FunctionDecl *FD; |
| 4504 | Expr *NewRep; |
| 4505 | |
| 4506 | // Simulate a contructor call... |
Fariborz Jahanian | df474ec | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 4507 | std::string Tag; |
| 4508 | |
| 4509 | if (GlobalBlockExpr) |
| 4510 | Tag = "__global_"; |
| 4511 | else |
| 4512 | Tag = "__"; |
| 4513 | Tag += FuncName + "_block_impl_" + BlockNumber; |
| 4514 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4515 | FD = SynthBlockInitFunctionDecl(Tag); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4516 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, false, FType, VK_RValue, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4517 | SourceLocation()); |
| 4518 | |
| 4519 | SmallVector<Expr*, 4> InitExprs; |
| 4520 | |
| 4521 | // Initialize the block function. |
| 4522 | FD = SynthBlockInitFunctionDecl(Func); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4523 | DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, false, FD->getType(), |
| 4524 | VK_LValue, SourceLocation()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4525 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
| 4526 | CK_BitCast, Arg); |
| 4527 | InitExprs.push_back(castExpr); |
| 4528 | |
| 4529 | // Initialize the block descriptor. |
| 4530 | std::string DescData = "__" + FuncName + "_block_desc_" + BlockNumber + "_DATA"; |
| 4531 | |
| 4532 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, |
| 4533 | SourceLocation(), SourceLocation(), |
| 4534 | &Context->Idents.get(DescData.c_str()), |
| 4535 | Context->VoidPtrTy, 0, |
| 4536 | SC_Static, SC_None); |
| 4537 | UnaryOperator *DescRefExpr = |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4538 | new (Context) UnaryOperator(new (Context) DeclRefExpr(NewVD, false, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4539 | Context->VoidPtrTy, |
| 4540 | VK_LValue, |
| 4541 | SourceLocation()), |
| 4542 | UO_AddrOf, |
| 4543 | Context->getPointerType(Context->VoidPtrTy), |
| 4544 | VK_RValue, OK_Ordinary, |
| 4545 | SourceLocation()); |
| 4546 | InitExprs.push_back(DescRefExpr); |
| 4547 | |
| 4548 | // Add initializers for any closure decl refs. |
| 4549 | if (BlockDeclRefs.size()) { |
| 4550 | Expr *Exp; |
| 4551 | // Output all "by copy" declarations. |
| 4552 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 4553 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 4554 | if (isObjCType((*I)->getType())) { |
| 4555 | // FIXME: Conform to ABI ([[obj retain] autorelease]). |
| 4556 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4557 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), |
| 4558 | VK_LValue, SourceLocation()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4559 | if (HasLocalVariableExternalStorage(*I)) { |
| 4560 | QualType QT = (*I)->getType(); |
| 4561 | QT = Context->getPointerType(QT); |
| 4562 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue, |
| 4563 | OK_Ordinary, SourceLocation()); |
| 4564 | } |
| 4565 | } else if (isTopLevelBlockPointerType((*I)->getType())) { |
| 4566 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4567 | Arg = new (Context) DeclRefExpr(FD, false, FD->getType(), |
| 4568 | VK_LValue, SourceLocation()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4569 | Exp = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, |
| 4570 | CK_BitCast, Arg); |
| 4571 | } else { |
| 4572 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4573 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), |
| 4574 | VK_LValue, SourceLocation()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4575 | if (HasLocalVariableExternalStorage(*I)) { |
| 4576 | QualType QT = (*I)->getType(); |
| 4577 | QT = Context->getPointerType(QT); |
| 4578 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue, |
| 4579 | OK_Ordinary, SourceLocation()); |
| 4580 | } |
| 4581 | |
| 4582 | } |
| 4583 | InitExprs.push_back(Exp); |
| 4584 | } |
| 4585 | // Output all "by ref" declarations. |
| 4586 | for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 4587 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 4588 | ValueDecl *ND = (*I); |
| 4589 | std::string Name(ND->getNameAsString()); |
| 4590 | std::string RecName; |
| 4591 | RewriteByRefString(RecName, Name, ND, true); |
| 4592 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str() |
| 4593 | + sizeof("struct")); |
| 4594 | RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, |
| 4595 | SourceLocation(), SourceLocation(), |
| 4596 | II); |
| 4597 | assert(RD && "SynthBlockInitExpr(): Can't find RecordDecl"); |
| 4598 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 4599 | |
| 4600 | FD = SynthBlockInitFunctionDecl((*I)->getName()); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4601 | Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue, |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4602 | SourceLocation()); |
| 4603 | bool isNestedCapturedVar = false; |
| 4604 | if (block) |
| 4605 | for (BlockDecl::capture_const_iterator ci = block->capture_begin(), |
| 4606 | ce = block->capture_end(); ci != ce; ++ci) { |
| 4607 | const VarDecl *variable = ci->getVariable(); |
| 4608 | if (variable == ND && ci->isNested()) { |
| 4609 | assert (ci->isByRef() && |
| 4610 | "SynthBlockInitExpr - captured block variable is not byref"); |
| 4611 | isNestedCapturedVar = true; |
| 4612 | break; |
| 4613 | } |
| 4614 | } |
| 4615 | // captured nested byref variable has its address passed. Do not take |
| 4616 | // its address again. |
| 4617 | if (!isNestedCapturedVar) |
| 4618 | Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, |
| 4619 | Context->getPointerType(Exp->getType()), |
| 4620 | VK_RValue, OK_Ordinary, SourceLocation()); |
| 4621 | Exp = NoTypeInfoCStyleCastExpr(Context, castT, CK_BitCast, Exp); |
| 4622 | InitExprs.push_back(Exp); |
| 4623 | } |
| 4624 | } |
| 4625 | if (ImportedBlockDecls.size()) { |
| 4626 | // generate BLOCK_HAS_COPY_DISPOSE(have helper funcs) | BLOCK_HAS_DESCRIPTOR |
| 4627 | int flag = (BLOCK_HAS_COPY_DISPOSE | BLOCK_HAS_DESCRIPTOR); |
| 4628 | unsigned IntSize = |
| 4629 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
| 4630 | Expr *FlagExp = IntegerLiteral::Create(*Context, llvm::APInt(IntSize, flag), |
| 4631 | Context->IntTy, SourceLocation()); |
| 4632 | InitExprs.push_back(FlagExp); |
| 4633 | } |
| 4634 | NewRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], InitExprs.size(), |
| 4635 | FType, VK_LValue, SourceLocation()); |
Fariborz Jahanian | df474ec | 2012-03-23 00:00:49 +0000 | [diff] [blame] | 4636 | |
| 4637 | if (GlobalBlockExpr) { |
| 4638 | assert (GlobalConstructionExp == 0 && |
| 4639 | "SynthBlockInitExpr - GlobalConstructionExp must be null"); |
| 4640 | GlobalConstructionExp = NewRep; |
| 4641 | NewRep = DRE; |
| 4642 | } |
| 4643 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4644 | NewRep = new (Context) UnaryOperator(NewRep, UO_AddrOf, |
| 4645 | Context->getPointerType(NewRep->getType()), |
| 4646 | VK_RValue, OK_Ordinary, SourceLocation()); |
| 4647 | NewRep = NoTypeInfoCStyleCastExpr(Context, FType, CK_BitCast, |
| 4648 | NewRep); |
| 4649 | BlockDeclRefs.clear(); |
| 4650 | BlockByRefDecls.clear(); |
| 4651 | BlockByRefDeclsPtrSet.clear(); |
| 4652 | BlockByCopyDecls.clear(); |
| 4653 | BlockByCopyDeclsPtrSet.clear(); |
| 4654 | ImportedBlockDecls.clear(); |
| 4655 | return NewRep; |
| 4656 | } |
| 4657 | |
| 4658 | bool RewriteModernObjC::IsDeclStmtInForeachHeader(DeclStmt *DS) { |
| 4659 | if (const ObjCForCollectionStmt * CS = |
| 4660 | dyn_cast<ObjCForCollectionStmt>(Stmts.back())) |
| 4661 | return CS->getElement() == DS; |
| 4662 | return false; |
| 4663 | } |
| 4664 | |
| 4665 | //===----------------------------------------------------------------------===// |
| 4666 | // Function Body / Expression rewriting |
| 4667 | //===----------------------------------------------------------------------===// |
| 4668 | |
| 4669 | Stmt *RewriteModernObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
| 4670 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
| 4671 | isa<DoStmt>(S) || isa<ForStmt>(S)) |
| 4672 | Stmts.push_back(S); |
| 4673 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 4674 | Stmts.push_back(S); |
| 4675 | ObjCBcLabelNo.push_back(++BcLabelCount); |
| 4676 | } |
| 4677 | |
| 4678 | // Pseudo-object operations and ivar references need special |
| 4679 | // treatment because we're going to recursively rewrite them. |
| 4680 | if (PseudoObjectExpr *PseudoOp = dyn_cast<PseudoObjectExpr>(S)) { |
| 4681 | if (isa<BinaryOperator>(PseudoOp->getSyntacticForm())) { |
| 4682 | return RewritePropertyOrImplicitSetter(PseudoOp); |
| 4683 | } else { |
| 4684 | return RewritePropertyOrImplicitGetter(PseudoOp); |
| 4685 | } |
| 4686 | } else if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) { |
| 4687 | return RewriteObjCIvarRefExpr(IvarRefExpr); |
| 4688 | } |
| 4689 | |
| 4690 | SourceRange OrigStmtRange = S->getSourceRange(); |
| 4691 | |
| 4692 | // Perform a bottom up rewrite of all children. |
| 4693 | for (Stmt::child_range CI = S->children(); CI; ++CI) |
| 4694 | if (*CI) { |
| 4695 | Stmt *childStmt = (*CI); |
| 4696 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(childStmt); |
| 4697 | if (newStmt) { |
| 4698 | *CI = newStmt; |
| 4699 | } |
| 4700 | } |
| 4701 | |
| 4702 | if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4703 | SmallVector<DeclRefExpr *, 8> InnerBlockDeclRefs; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4704 | llvm::SmallPtrSet<const DeclContext *, 8> InnerContexts; |
| 4705 | InnerContexts.insert(BE->getBlockDecl()); |
| 4706 | ImportedLocalExternalDecls.clear(); |
| 4707 | GetInnerBlockDeclRefExprs(BE->getBody(), |
| 4708 | InnerBlockDeclRefs, InnerContexts); |
| 4709 | // Rewrite the block body in place. |
| 4710 | Stmt *SaveCurrentBody = CurrentBody; |
| 4711 | CurrentBody = BE->getBody(); |
| 4712 | PropParentMap = 0; |
| 4713 | // block literal on rhs of a property-dot-sytax assignment |
| 4714 | // must be replaced by its synthesize ast so getRewrittenText |
| 4715 | // works as expected. In this case, what actually ends up on RHS |
| 4716 | // is the blockTranscribed which is the helper function for the |
| 4717 | // block literal; as in: self.c = ^() {[ace ARR];}; |
| 4718 | bool saveDisableReplaceStmt = DisableReplaceStmt; |
| 4719 | DisableReplaceStmt = false; |
| 4720 | RewriteFunctionBodyOrGlobalInitializer(BE->getBody()); |
| 4721 | DisableReplaceStmt = saveDisableReplaceStmt; |
| 4722 | CurrentBody = SaveCurrentBody; |
| 4723 | PropParentMap = 0; |
| 4724 | ImportedLocalExternalDecls.clear(); |
| 4725 | // Now we snarf the rewritten text and stash it away for later use. |
| 4726 | std::string Str = Rewrite.getRewrittenText(BE->getSourceRange()); |
| 4727 | RewrittenBlockExprs[BE] = Str; |
| 4728 | |
| 4729 | Stmt *blockTranscribed = SynthBlockInitExpr(BE, InnerBlockDeclRefs); |
| 4730 | |
| 4731 | //blockTranscribed->dump(); |
| 4732 | ReplaceStmt(S, blockTranscribed); |
| 4733 | return blockTranscribed; |
| 4734 | } |
| 4735 | // Handle specific things. |
| 4736 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 4737 | return RewriteAtEncode(AtEncode); |
| 4738 | |
| 4739 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 4740 | return RewriteAtSelector(AtSelector); |
| 4741 | |
| 4742 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 4743 | return RewriteObjCStringLiteral(AtString); |
| 4744 | |
| 4745 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
| 4746 | #if 0 |
| 4747 | // Before we rewrite it, put the original message expression in a comment. |
| 4748 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 4749 | SourceLocation endLoc = MessExpr->getLocEnd(); |
| 4750 | |
| 4751 | const char *startBuf = SM->getCharacterData(startLoc); |
| 4752 | const char *endBuf = SM->getCharacterData(endLoc); |
| 4753 | |
| 4754 | std::string messString; |
| 4755 | messString += "// "; |
| 4756 | messString.append(startBuf, endBuf-startBuf+1); |
| 4757 | messString += "\n"; |
| 4758 | |
| 4759 | // FIXME: Missing definition of |
| 4760 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
| 4761 | // InsertText(startLoc, messString.c_str(), messString.size()); |
| 4762 | // Tried this, but it didn't work either... |
| 4763 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
| 4764 | #endif |
| 4765 | return RewriteMessageExpr(MessExpr); |
| 4766 | } |
| 4767 | |
| 4768 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 4769 | return RewriteObjCTryStmt(StmtTry); |
| 4770 | |
| 4771 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 4772 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 4773 | |
| 4774 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 4775 | return RewriteObjCThrowStmt(StmtThrow); |
| 4776 | |
| 4777 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 4778 | return RewriteObjCProtocolExpr(ProtocolExp); |
| 4779 | |
| 4780 | if (ObjCForCollectionStmt *StmtForCollection = |
| 4781 | dyn_cast<ObjCForCollectionStmt>(S)) |
| 4782 | return RewriteObjCForCollectionStmt(StmtForCollection, |
| 4783 | OrigStmtRange.getEnd()); |
| 4784 | if (BreakStmt *StmtBreakStmt = |
| 4785 | dyn_cast<BreakStmt>(S)) |
| 4786 | return RewriteBreakStmt(StmtBreakStmt); |
| 4787 | if (ContinueStmt *StmtContinueStmt = |
| 4788 | dyn_cast<ContinueStmt>(S)) |
| 4789 | return RewriteContinueStmt(StmtContinueStmt); |
| 4790 | |
| 4791 | // Need to check for protocol refs (id <P>, Foo <P> *) in variable decls |
| 4792 | // and cast exprs. |
| 4793 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
| 4794 | // FIXME: What we're doing here is modifying the type-specifier that |
| 4795 | // precedes the first Decl. In the future the DeclGroup should have |
| 4796 | // a separate type-specifier that we can rewrite. |
| 4797 | // NOTE: We need to avoid rewriting the DeclStmt if it is within |
| 4798 | // the context of an ObjCForCollectionStmt. For example: |
| 4799 | // NSArray *someArray; |
| 4800 | // for (id <FooProtocol> index in someArray) ; |
| 4801 | // This is because RewriteObjCForCollectionStmt() does textual rewriting |
| 4802 | // and it depends on the original text locations/positions. |
| 4803 | if (Stmts.empty() || !IsDeclStmtInForeachHeader(DS)) |
| 4804 | RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin()); |
| 4805 | |
| 4806 | // Blocks rewrite rules. |
| 4807 | for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); |
| 4808 | DI != DE; ++DI) { |
| 4809 | Decl *SD = *DI; |
| 4810 | if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { |
| 4811 | if (isTopLevelBlockPointerType(ND->getType())) |
| 4812 | RewriteBlockPointerDecl(ND); |
| 4813 | else if (ND->getType()->isFunctionPointerType()) |
| 4814 | CheckFunctionPointerDecl(ND->getType(), ND); |
| 4815 | if (VarDecl *VD = dyn_cast<VarDecl>(SD)) { |
| 4816 | if (VD->hasAttr<BlocksAttr>()) { |
| 4817 | static unsigned uniqueByrefDeclCount = 0; |
| 4818 | assert(!BlockByRefDeclNo.count(ND) && |
| 4819 | "RewriteFunctionBodyOrGlobalInitializer: Duplicate byref decl"); |
| 4820 | BlockByRefDeclNo[ND] = uniqueByrefDeclCount++; |
| 4821 | RewriteByRefVar(VD); |
| 4822 | } |
| 4823 | else |
| 4824 | RewriteTypeOfDecl(VD); |
| 4825 | } |
| 4826 | } |
| 4827 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) { |
| 4828 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
| 4829 | RewriteBlockPointerDecl(TD); |
| 4830 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
| 4831 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 4832 | } |
| 4833 | } |
| 4834 | } |
| 4835 | |
| 4836 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) |
| 4837 | RewriteObjCQualifiedInterfaceTypes(CE); |
| 4838 | |
| 4839 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
| 4840 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 4841 | assert(!Stmts.empty() && "Statement stack is empty"); |
| 4842 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 4843 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
| 4844 | && "Statement stack mismatch"); |
| 4845 | Stmts.pop_back(); |
| 4846 | } |
| 4847 | // Handle blocks rewriting. |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4848 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { |
| 4849 | ValueDecl *VD = DRE->getDecl(); |
| 4850 | if (VD->hasAttr<BlocksAttr>()) |
| 4851 | return RewriteBlockDeclRefExpr(DRE); |
| 4852 | if (HasLocalVariableExternalStorage(VD)) |
| 4853 | return RewriteLocalVariableExternalStorage(DRE); |
| 4854 | } |
| 4855 | |
| 4856 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
| 4857 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
| 4858 | Stmt *BlockCall = SynthesizeBlockCall(CE, CE->getCallee()); |
| 4859 | ReplaceStmt(S, BlockCall); |
| 4860 | return BlockCall; |
| 4861 | } |
| 4862 | } |
| 4863 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) { |
| 4864 | RewriteCastExpr(CE); |
| 4865 | } |
| 4866 | #if 0 |
| 4867 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
| 4868 | CastExpr *Replacement = new (Context) CastExpr(ICE->getType(), |
| 4869 | ICE->getSubExpr(), |
| 4870 | SourceLocation()); |
| 4871 | // Get the new text. |
| 4872 | std::string SStr; |
| 4873 | llvm::raw_string_ostream Buf(SStr); |
| 4874 | Replacement->printPretty(Buf, *Context); |
| 4875 | const std::string &Str = Buf.str(); |
| 4876 | |
| 4877 | printf("CAST = %s\n", &Str[0]); |
| 4878 | InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 4879 | delete S; |
| 4880 | return Replacement; |
| 4881 | } |
| 4882 | #endif |
| 4883 | // Return this stmt unmodified. |
| 4884 | return S; |
| 4885 | } |
| 4886 | |
| 4887 | void RewriteModernObjC::RewriteRecordBody(RecordDecl *RD) { |
| 4888 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 4889 | e = RD->field_end(); i != e; ++i) { |
| 4890 | FieldDecl *FD = *i; |
| 4891 | if (isTopLevelBlockPointerType(FD->getType())) |
| 4892 | RewriteBlockPointerDecl(FD); |
| 4893 | if (FD->getType()->isObjCQualifiedIdType() || |
| 4894 | FD->getType()->isObjCQualifiedInterfaceType()) |
| 4895 | RewriteObjCQualifiedInterfaceTypes(FD); |
| 4896 | } |
| 4897 | } |
| 4898 | |
| 4899 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 4900 | /// main file of the input. |
| 4901 | void RewriteModernObjC::HandleDeclInMainFile(Decl *D) { |
| 4902 | switch (D->getKind()) { |
| 4903 | case Decl::Function: { |
| 4904 | FunctionDecl *FD = cast<FunctionDecl>(D); |
| 4905 | if (FD->isOverloadedOperator()) |
| 4906 | return; |
| 4907 | |
| 4908 | // Since function prototypes don't have ParmDecl's, we check the function |
| 4909 | // prototype. This enables us to rewrite function declarations and |
| 4910 | // definitions using the same code. |
| 4911 | RewriteBlocksInFunctionProtoType(FD->getType(), FD); |
| 4912 | |
Argyrios Kyrtzidis | 9335df3 | 2012-02-12 04:48:45 +0000 | [diff] [blame] | 4913 | if (!FD->isThisDeclarationADefinition()) |
| 4914 | break; |
| 4915 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 4916 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
| 4917 | if (CompoundStmt *Body = dyn_cast_or_null<CompoundStmt>(FD->getBody())) { |
| 4918 | CurFunctionDef = FD; |
| 4919 | CurFunctionDeclToDeclareForBlock = FD; |
| 4920 | CurrentBody = Body; |
| 4921 | Body = |
| 4922 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 4923 | FD->setBody(Body); |
| 4924 | CurrentBody = 0; |
| 4925 | if (PropParentMap) { |
| 4926 | delete PropParentMap; |
| 4927 | PropParentMap = 0; |
| 4928 | } |
| 4929 | // This synthesizes and inserts the block "impl" struct, invoke function, |
| 4930 | // and any copy/dispose helper functions. |
| 4931 | InsertBlockLiteralsWithinFunction(FD); |
| 4932 | CurFunctionDef = 0; |
| 4933 | CurFunctionDeclToDeclareForBlock = 0; |
| 4934 | } |
| 4935 | break; |
| 4936 | } |
| 4937 | case Decl::ObjCMethod: { |
| 4938 | ObjCMethodDecl *MD = cast<ObjCMethodDecl>(D); |
| 4939 | if (CompoundStmt *Body = MD->getCompoundBody()) { |
| 4940 | CurMethodDef = MD; |
| 4941 | CurrentBody = Body; |
| 4942 | Body = |
| 4943 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 4944 | MD->setBody(Body); |
| 4945 | CurrentBody = 0; |
| 4946 | if (PropParentMap) { |
| 4947 | delete PropParentMap; |
| 4948 | PropParentMap = 0; |
| 4949 | } |
| 4950 | InsertBlockLiteralsWithinMethod(MD); |
| 4951 | CurMethodDef = 0; |
| 4952 | } |
| 4953 | break; |
| 4954 | } |
| 4955 | case Decl::ObjCImplementation: { |
| 4956 | ObjCImplementationDecl *CI = cast<ObjCImplementationDecl>(D); |
| 4957 | ClassImplementation.push_back(CI); |
| 4958 | break; |
| 4959 | } |
| 4960 | case Decl::ObjCCategoryImpl: { |
| 4961 | ObjCCategoryImplDecl *CI = cast<ObjCCategoryImplDecl>(D); |
| 4962 | CategoryImplementation.push_back(CI); |
| 4963 | break; |
| 4964 | } |
| 4965 | case Decl::Var: { |
| 4966 | VarDecl *VD = cast<VarDecl>(D); |
| 4967 | RewriteObjCQualifiedInterfaceTypes(VD); |
| 4968 | if (isTopLevelBlockPointerType(VD->getType())) |
| 4969 | RewriteBlockPointerDecl(VD); |
| 4970 | else if (VD->getType()->isFunctionPointerType()) { |
| 4971 | CheckFunctionPointerDecl(VD->getType(), VD); |
| 4972 | if (VD->getInit()) { |
| 4973 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
| 4974 | RewriteCastExpr(CE); |
| 4975 | } |
| 4976 | } |
| 4977 | } else if (VD->getType()->isRecordType()) { |
| 4978 | RecordDecl *RD = VD->getType()->getAs<RecordType>()->getDecl(); |
| 4979 | if (RD->isCompleteDefinition()) |
| 4980 | RewriteRecordBody(RD); |
| 4981 | } |
| 4982 | if (VD->getInit()) { |
| 4983 | GlobalVarDecl = VD; |
| 4984 | CurrentBody = VD->getInit(); |
| 4985 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
| 4986 | CurrentBody = 0; |
| 4987 | if (PropParentMap) { |
| 4988 | delete PropParentMap; |
| 4989 | PropParentMap = 0; |
| 4990 | } |
| 4991 | SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), VD->getName()); |
| 4992 | GlobalVarDecl = 0; |
| 4993 | |
| 4994 | // This is needed for blocks. |
| 4995 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
| 4996 | RewriteCastExpr(CE); |
| 4997 | } |
| 4998 | } |
| 4999 | break; |
| 5000 | } |
| 5001 | case Decl::TypeAlias: |
| 5002 | case Decl::Typedef: { |
| 5003 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
| 5004 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
| 5005 | RewriteBlockPointerDecl(TD); |
| 5006 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
| 5007 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 5008 | } |
| 5009 | break; |
| 5010 | } |
| 5011 | case Decl::CXXRecord: |
| 5012 | case Decl::Record: { |
| 5013 | RecordDecl *RD = cast<RecordDecl>(D); |
| 5014 | if (RD->isCompleteDefinition()) |
| 5015 | RewriteRecordBody(RD); |
| 5016 | break; |
| 5017 | } |
| 5018 | default: |
| 5019 | break; |
| 5020 | } |
| 5021 | // Nothing yet. |
| 5022 | } |
| 5023 | |
Fariborz Jahanian | 30650eb | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 5024 | /// Write_ProtocolExprReferencedMetadata - This routine writer out the |
| 5025 | /// protocol reference symbols in the for of: |
| 5026 | /// struct _protocol_t *PROTOCOL_REF = &PROTOCOL_METADATA. |
| 5027 | static void Write_ProtocolExprReferencedMetadata(ASTContext *Context, |
| 5028 | ObjCProtocolDecl *PDecl, |
| 5029 | std::string &Result) { |
| 5030 | // Also output .objc_protorefs$B section and its meta-data. |
| 5031 | if (Context->getLangOpts().MicrosoftExt) |
| 5032 | Result += "__declspec(allocate(\".objc_protorefs$B\")) "; |
| 5033 | Result += "struct _protocol_t *"; |
| 5034 | Result += "_OBJC_PROTOCOL_REFERENCE_$_"; |
| 5035 | Result += PDecl->getNameAsString(); |
| 5036 | Result += " = &"; |
| 5037 | Result += "_OBJC_PROTOCOL_"; Result += PDecl->getNameAsString(); |
| 5038 | Result += ";\n"; |
| 5039 | } |
| 5040 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5041 | void RewriteModernObjC::HandleTranslationUnit(ASTContext &C) { |
| 5042 | if (Diags.hasErrorOccurred()) |
| 5043 | return; |
| 5044 | |
| 5045 | RewriteInclude(); |
| 5046 | |
| 5047 | // Here's a great place to add any extra declarations that may be needed. |
| 5048 | // Write out meta data for each @protocol(<expr>). |
| 5049 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Fariborz Jahanian | 30650eb | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 5050 | E = ProtocolExprDecls.end(); I != E; ++I) { |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 5051 | RewriteObjCProtocolMetaData(*I, Preamble); |
Fariborz Jahanian | 30650eb | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 5052 | Write_ProtocolExprReferencedMetadata(Context, (*I), Preamble); |
| 5053 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5054 | |
| 5055 | InsertText(SM->getLocForStartOfFile(MainFileID), Preamble, false); |
Fariborz Jahanian | 5731778 | 2012-02-21 23:58:41 +0000 | [diff] [blame] | 5056 | for (unsigned i = 0, e = ObjCInterfacesSeen.size(); i < e; i++) { |
| 5057 | ObjCInterfaceDecl *CDecl = ObjCInterfacesSeen[i]; |
| 5058 | // Write struct declaration for the class matching its ivar declarations. |
| 5059 | // Note that for modern abi, this is postponed until the end of TU |
| 5060 | // because class extensions and the implementation might declare their own |
| 5061 | // private ivars. |
| 5062 | RewriteInterfaceDecl(CDecl); |
| 5063 | } |
Fariborz Jahanian | 72c88f1 | 2012-02-22 18:13:25 +0000 | [diff] [blame] | 5064 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5065 | if (ClassImplementation.size() || CategoryImplementation.size()) |
| 5066 | RewriteImplementations(); |
| 5067 | |
| 5068 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 5069 | // we are done. |
| 5070 | if (const RewriteBuffer *RewriteBuf = |
| 5071 | Rewrite.getRewriteBufferFor(MainFileID)) { |
| 5072 | //printf("Changed:\n"); |
| 5073 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 5074 | } else { |
| 5075 | llvm::errs() << "No changes\n"; |
| 5076 | } |
| 5077 | |
| 5078 | if (ClassImplementation.size() || CategoryImplementation.size() || |
| 5079 | ProtocolExprDecls.size()) { |
| 5080 | // Rewrite Objective-c meta data* |
| 5081 | std::string ResultStr; |
| 5082 | RewriteMetaDataIntoBuffer(ResultStr); |
| 5083 | // Emit metadata. |
| 5084 | *OutFile << ResultStr; |
| 5085 | } |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 5086 | // Emit ImageInfo; |
| 5087 | { |
| 5088 | std::string ResultStr; |
| 5089 | WriteImageInfo(ResultStr); |
| 5090 | *OutFile << ResultStr; |
| 5091 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5092 | OutFile->flush(); |
| 5093 | } |
| 5094 | |
| 5095 | void RewriteModernObjC::Initialize(ASTContext &context) { |
| 5096 | InitializeCommon(context); |
| 5097 | |
Fariborz Jahanian | 6991bc5 | 2012-03-10 17:45:38 +0000 | [diff] [blame] | 5098 | Preamble += "#ifndef __OBJC2__\n"; |
| 5099 | Preamble += "#define __OBJC2__\n"; |
| 5100 | Preamble += "#endif\n"; |
| 5101 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5102 | // declaring objc_selector outside the parameter list removes a silly |
| 5103 | // scope related warning... |
| 5104 | if (IsHeader) |
| 5105 | Preamble = "#pragma once\n"; |
| 5106 | Preamble += "struct objc_selector; struct objc_class;\n"; |
| 5107 | Preamble += "struct __rw_objc_super { struct objc_object *object; "; |
| 5108 | Preamble += "struct objc_object *superClass; "; |
| 5109 | if (LangOpts.MicrosoftExt) { |
Fariborz Jahanian | 1ca052c | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 5110 | // Define all sections using syntax that makes sense. |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 5111 | // These are currently generated. |
| 5112 | Preamble += "\n#pragma section(\".objc_classlist$B\", long, read, write)\n"; |
Fariborz Jahanian | 1ca052c | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 5113 | Preamble += "#pragma section(\".objc_catlist$B\", long, read, write)\n"; |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 5114 | Preamble += "#pragma section(\".objc_protolist$B\", long, read, write)\n"; |
| 5115 | Preamble += "#pragma section(\".objc_imageinfo$B\", long, read, write)\n"; |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 5116 | Preamble += "#pragma section(\".objc_nlclslist$B\", long, read, write)\n"; |
| 5117 | Preamble += "#pragma section(\".objc_nlcatlist$B\", long, read, write)\n"; |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 5118 | Preamble += "#pragma section(\".objc_protorefs$B\", long, read, write)\n"; |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 5119 | // These are generated but not necessary for functionality. |
| 5120 | Preamble += "#pragma section(\".datacoal_nt$B\", long, read, write)\n"; |
| 5121 | Preamble += "#pragma section(\".cat_cls_meth$B\", long, read, write)\n"; |
Fariborz Jahanian | 1ca052c | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 5122 | Preamble += "#pragma section(\".inst_meth$B\", long, read, write)\n"; |
| 5123 | Preamble += "#pragma section(\".cls_meth$B\", long, read, write)\n"; |
Fariborz Jahanian | 40a777a | 2012-03-12 16:46:58 +0000 | [diff] [blame] | 5124 | Preamble += "#pragma section(\".objc_ivar$B\", long, read, write)\n"; |
Fariborz Jahanian | de5d946 | 2012-03-14 18:09:23 +0000 | [diff] [blame] | 5125 | |
Fariborz Jahanian | 30650eb | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 5126 | // These need be generated for performance. Currently they are not, |
| 5127 | // using API calls instead. |
| 5128 | Preamble += "#pragma section(\".objc_selrefs$B\", long, read, write)\n"; |
| 5129 | Preamble += "#pragma section(\".objc_classrefs$B\", long, read, write)\n"; |
| 5130 | Preamble += "#pragma section(\".objc_superrefs$B\", long, read, write)\n"; |
| 5131 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5132 | // Add a constructor for creating temporary objects. |
| 5133 | Preamble += "__rw_objc_super(struct objc_object *o, struct objc_object *s) " |
| 5134 | ": "; |
| 5135 | Preamble += "object(o), superClass(s) {} "; |
| 5136 | } |
| 5137 | Preamble += "};\n"; |
| 5138 | Preamble += "#ifndef _REWRITER_typedef_Protocol\n"; |
| 5139 | Preamble += "typedef struct objc_object Protocol;\n"; |
| 5140 | Preamble += "#define _REWRITER_typedef_Protocol\n"; |
| 5141 | Preamble += "#endif\n"; |
| 5142 | if (LangOpts.MicrosoftExt) { |
| 5143 | Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n"; |
| 5144 | Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n"; |
Fariborz Jahanian | 5cf6b6c | 2012-03-21 23:41:04 +0000 | [diff] [blame] | 5145 | } |
| 5146 | else |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5147 | Preamble += "#define __OBJC_RW_DLLIMPORT extern\n"; |
Fariborz Jahanian | 5cf6b6c | 2012-03-21 23:41:04 +0000 | [diff] [blame] | 5148 | |
| 5149 | Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSend(void);\n"; |
| 5150 | Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSendSuper(void);\n"; |
| 5151 | Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSend_stret(void);\n"; |
| 5152 | Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSendSuper_stret(void);\n"; |
| 5153 | Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSend_fpret(void);\n"; |
| 5154 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5155 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getClass"; |
| 5156 | Preamble += "(const char *);\n"; |
| 5157 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_class *class_getSuperclass"; |
| 5158 | Preamble += "(struct objc_class *);\n"; |
| 5159 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getMetaClass"; |
| 5160 | Preamble += "(const char *);\n"; |
Fariborz Jahanian | 55261af | 2012-03-19 18:11:32 +0000 | [diff] [blame] | 5161 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw( struct objc_object *);\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5162 | // @synchronized hooks. |
Fariborz Jahanian | 55261af | 2012-03-19 18:11:32 +0000 | [diff] [blame] | 5163 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_enter( struct objc_object *);\n"; |
| 5164 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_exit( struct objc_object *);\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5165 | Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n"; |
| 5166 | Preamble += "#ifndef __FASTENUMERATIONSTATE\n"; |
| 5167 | Preamble += "struct __objcFastEnumerationState {\n\t"; |
| 5168 | Preamble += "unsigned long state;\n\t"; |
| 5169 | Preamble += "void **itemsPtr;\n\t"; |
| 5170 | Preamble += "unsigned long *mutationsPtr;\n\t"; |
| 5171 | Preamble += "unsigned long extra[5];\n};\n"; |
| 5172 | Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n"; |
| 5173 | Preamble += "#define __FASTENUMERATIONSTATE\n"; |
| 5174 | Preamble += "#endif\n"; |
| 5175 | Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n"; |
| 5176 | Preamble += "struct __NSConstantStringImpl {\n"; |
| 5177 | Preamble += " int *isa;\n"; |
| 5178 | Preamble += " int flags;\n"; |
| 5179 | Preamble += " char *str;\n"; |
| 5180 | Preamble += " long length;\n"; |
| 5181 | Preamble += "};\n"; |
| 5182 | Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n"; |
| 5183 | Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n"; |
| 5184 | Preamble += "#else\n"; |
| 5185 | Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n"; |
| 5186 | Preamble += "#endif\n"; |
| 5187 | Preamble += "#define __NSCONSTANTSTRINGIMPL\n"; |
| 5188 | Preamble += "#endif\n"; |
| 5189 | // Blocks preamble. |
| 5190 | Preamble += "#ifndef BLOCK_IMPL\n"; |
| 5191 | Preamble += "#define BLOCK_IMPL\n"; |
| 5192 | Preamble += "struct __block_impl {\n"; |
| 5193 | Preamble += " void *isa;\n"; |
| 5194 | Preamble += " int Flags;\n"; |
| 5195 | Preamble += " int Reserved;\n"; |
| 5196 | Preamble += " void *FuncPtr;\n"; |
| 5197 | Preamble += "};\n"; |
| 5198 | Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n"; |
| 5199 | Preamble += "#ifdef __OBJC_EXPORT_BLOCKS\n"; |
| 5200 | Preamble += "extern \"C\" __declspec(dllexport) " |
| 5201 | "void _Block_object_assign(void *, const void *, const int);\n"; |
| 5202 | Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_dispose(const void *, const int);\n"; |
| 5203 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteGlobalBlock[32];\n"; |
| 5204 | Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteStackBlock[32];\n"; |
| 5205 | Preamble += "#else\n"; |
| 5206 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_assign(void *, const void *, const int);\n"; |
| 5207 | Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_dispose(const void *, const int);\n"; |
| 5208 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteGlobalBlock[32];\n"; |
| 5209 | Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteStackBlock[32];\n"; |
| 5210 | Preamble += "#endif\n"; |
| 5211 | Preamble += "#endif\n"; |
| 5212 | if (LangOpts.MicrosoftExt) { |
| 5213 | Preamble += "#undef __OBJC_RW_DLLIMPORT\n"; |
| 5214 | Preamble += "#undef __OBJC_RW_STATICIMPORT\n"; |
| 5215 | Preamble += "#ifndef KEEP_ATTRIBUTES\n"; // We use this for clang tests. |
| 5216 | Preamble += "#define __attribute__(X)\n"; |
| 5217 | Preamble += "#endif\n"; |
| 5218 | Preamble += "#define __weak\n"; |
| 5219 | } |
| 5220 | else { |
| 5221 | Preamble += "#define __block\n"; |
| 5222 | Preamble += "#define __weak\n"; |
| 5223 | } |
| 5224 | // NOTE! Windows uses LLP64 for 64bit mode. So, cast pointer to long long |
| 5225 | // as this avoids warning in any 64bit/32bit compilation model. |
| 5226 | Preamble += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((long long) &((TYPE *)0)->MEMBER)\n"; |
| 5227 | } |
| 5228 | |
| 5229 | /// RewriteIvarOffsetComputation - This rutine synthesizes computation of |
| 5230 | /// ivar offset. |
| 5231 | void RewriteModernObjC::RewriteIvarOffsetComputation(ObjCIvarDecl *ivar, |
| 5232 | std::string &Result) { |
| 5233 | if (ivar->isBitField()) { |
| 5234 | // FIXME: The hack below doesn't work for bitfields. For now, we simply |
| 5235 | // place all bitfields at offset 0. |
| 5236 | Result += "0"; |
| 5237 | } else { |
| 5238 | Result += "__OFFSETOFIVAR__(struct "; |
| 5239 | Result += ivar->getContainingInterface()->getNameAsString(); |
| 5240 | if (LangOpts.MicrosoftExt) |
| 5241 | Result += "_IMPL"; |
| 5242 | Result += ", "; |
| 5243 | Result += ivar->getNameAsString(); |
| 5244 | Result += ")"; |
| 5245 | } |
| 5246 | } |
| 5247 | |
| 5248 | /// WriteModernMetadataDeclarations - Writes out metadata declarations for modern ABI. |
| 5249 | /// struct _prop_t { |
Fariborz Jahanian | da35eac | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 5250 | /// const char *name; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5251 | /// char *attributes; |
| 5252 | /// } |
| 5253 | |
| 5254 | /// struct _prop_list_t { |
| 5255 | /// uint32_t entsize; // sizeof(struct _prop_t) |
| 5256 | /// uint32_t count_of_properties; |
| 5257 | /// struct _prop_t prop_list[count_of_properties]; |
| 5258 | /// } |
| 5259 | |
| 5260 | /// struct _protocol_t; |
| 5261 | |
| 5262 | /// struct _protocol_list_t { |
| 5263 | /// long protocol_count; // Note, this is 32/64 bit |
| 5264 | /// struct _protocol_t * protocol_list[protocol_count]; |
| 5265 | /// } |
| 5266 | |
| 5267 | /// struct _objc_method { |
| 5268 | /// SEL _cmd; |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 5269 | /// const char *method_type; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5270 | /// char *_imp; |
| 5271 | /// } |
| 5272 | |
| 5273 | /// struct _method_list_t { |
| 5274 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 5275 | /// uint32_t method_count; |
| 5276 | /// struct _objc_method method_list[method_count]; |
| 5277 | /// } |
| 5278 | |
| 5279 | /// struct _protocol_t { |
| 5280 | /// id isa; // NULL |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 5281 | /// const char *protocol_name; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5282 | /// const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 5283 | /// const struct method_list_t *instance_methods; |
| 5284 | /// const struct method_list_t *class_methods; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5285 | /// const struct method_list_t *optionalInstanceMethods; |
| 5286 | /// const struct method_list_t *optionalClassMethods; |
| 5287 | /// const struct _prop_list_t * properties; |
| 5288 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 5289 | /// const uint32_t flags; // = 0 |
| 5290 | /// const char ** extendedMethodTypes; |
| 5291 | /// } |
| 5292 | |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 5293 | /// struct _ivar_t { |
| 5294 | /// unsigned long int *offset; // pointer to ivar offset location |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 5295 | /// const char *name; |
| 5296 | /// const char *type; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 5297 | /// uint32_t alignment; |
| 5298 | /// uint32_t size; |
| 5299 | /// } |
| 5300 | |
| 5301 | /// struct _ivar_list_t { |
| 5302 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 5303 | /// uint32 count; |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 5304 | /// struct _ivar_t list[count]; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 5305 | /// } |
| 5306 | |
| 5307 | /// struct _class_ro_t { |
Fariborz Jahanian | 249cd10 | 2012-03-24 16:53:16 +0000 | [diff] [blame] | 5308 | /// uint32_t flags; |
| 5309 | /// uint32_t instanceStart; |
| 5310 | /// uint32_t instanceSize; |
| 5311 | /// uint32_t reserved; // only when building for 64bit targets |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 5312 | /// const uint8_t *ivarLayout; |
| 5313 | /// const char *name; |
| 5314 | /// const struct _method_list_t *baseMethods; |
| 5315 | /// const struct _protocol_list_t *baseProtocols; |
| 5316 | /// const struct _ivar_list_t *ivars; |
| 5317 | /// const uint8_t *weakIvarLayout; |
| 5318 | /// const struct _prop_list_t *properties; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 5319 | /// } |
| 5320 | |
| 5321 | /// struct _class_t { |
| 5322 | /// struct _class_t *isa; |
Fariborz Jahanian | fd4ce2c | 2012-03-20 17:34:50 +0000 | [diff] [blame] | 5323 | /// struct _class_t *superclass; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 5324 | /// void *cache; |
| 5325 | /// IMP *vtable; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 5326 | /// struct _class_ro_t *ro; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 5327 | /// } |
| 5328 | |
| 5329 | /// struct _category_t { |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 5330 | /// const char *name; |
Fariborz Jahanian | 4b2fe6e | 2012-03-20 21:41:28 +0000 | [diff] [blame] | 5331 | /// struct _class_t *cls; |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 5332 | /// const struct _method_list_t *instance_methods; |
| 5333 | /// const struct _method_list_t *class_methods; |
| 5334 | /// const struct _protocol_list_t *protocols; |
| 5335 | /// const struct _prop_list_t *properties; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 5336 | /// } |
| 5337 | |
| 5338 | /// MessageRefTy - LLVM for: |
| 5339 | /// struct _message_ref_t { |
| 5340 | /// IMP messenger; |
| 5341 | /// SEL name; |
| 5342 | /// }; |
| 5343 | |
| 5344 | /// SuperMessageRefTy - LLVM for: |
| 5345 | /// struct _super_message_ref_t { |
| 5346 | /// SUPER_IMP messenger; |
| 5347 | /// SEL name; |
| 5348 | /// }; |
| 5349 | |
Fariborz Jahanian | de5d946 | 2012-03-14 18:09:23 +0000 | [diff] [blame] | 5350 | static void WriteModernMetadataDeclarations(ASTContext *Context, std::string &Result) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5351 | static bool meta_data_declared = false; |
| 5352 | if (meta_data_declared) |
| 5353 | return; |
| 5354 | |
| 5355 | Result += "\nstruct _prop_t {\n"; |
Fariborz Jahanian | da35eac | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 5356 | Result += "\tconst char *name;\n"; |
| 5357 | Result += "\tconst char *attributes;\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5358 | Result += "};\n"; |
| 5359 | |
| 5360 | Result += "\nstruct _protocol_t;\n"; |
| 5361 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5362 | Result += "\nstruct _objc_method {\n"; |
| 5363 | Result += "\tstruct objc_selector * _cmd;\n"; |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 5364 | Result += "\tconst char *method_type;\n"; |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 5365 | Result += "\tvoid *_imp;\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5366 | Result += "};\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5367 | |
| 5368 | Result += "\nstruct _protocol_t {\n"; |
| 5369 | Result += "\tvoid * isa; // NULL\n"; |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 5370 | Result += "\tconst char *protocol_name;\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5371 | Result += "\tconst struct _protocol_list_t * protocol_list; // super protocols\n"; |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 5372 | Result += "\tconst struct method_list_t *instance_methods;\n"; |
| 5373 | Result += "\tconst struct method_list_t *class_methods;\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5374 | Result += "\tconst struct method_list_t *optionalInstanceMethods;\n"; |
| 5375 | Result += "\tconst struct method_list_t *optionalClassMethods;\n"; |
| 5376 | Result += "\tconst struct _prop_list_t * properties;\n"; |
| 5377 | Result += "\tconst unsigned int size; // sizeof(struct _protocol_t)\n"; |
| 5378 | Result += "\tconst unsigned int flags; // = 0\n"; |
| 5379 | Result += "\tconst char ** extendedMethodTypes;\n"; |
| 5380 | Result += "};\n"; |
| 5381 | |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 5382 | Result += "\nstruct _ivar_t {\n"; |
| 5383 | Result += "\tunsigned long int *offset; // pointer to ivar offset location\n"; |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 5384 | Result += "\tconst char *name;\n"; |
| 5385 | Result += "\tconst char *type;\n"; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 5386 | Result += "\tunsigned int alignment;\n"; |
| 5387 | Result += "\tunsigned int size;\n"; |
| 5388 | Result += "};\n"; |
| 5389 | |
| 5390 | Result += "\nstruct _class_ro_t {\n"; |
Fariborz Jahanian | 249cd10 | 2012-03-24 16:53:16 +0000 | [diff] [blame] | 5391 | Result += "\tunsigned int flags;\n"; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 5392 | Result += "\tunsigned int instanceStart;\n"; |
Fariborz Jahanian | 249cd10 | 2012-03-24 16:53:16 +0000 | [diff] [blame] | 5393 | Result += "\tunsigned int instanceSize;\n"; |
Fariborz Jahanian | de5d946 | 2012-03-14 18:09:23 +0000 | [diff] [blame] | 5394 | const llvm::Triple &Triple(Context->getTargetInfo().getTriple()); |
| 5395 | if (Triple.getArch() == llvm::Triple::x86_64) |
Fariborz Jahanian | 249cd10 | 2012-03-24 16:53:16 +0000 | [diff] [blame] | 5396 | Result += "\tunsigned int reserved;\n"; |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 5397 | Result += "\tconst unsigned char *ivarLayout;\n"; |
| 5398 | Result += "\tconst char *name;\n"; |
| 5399 | Result += "\tconst struct _method_list_t *baseMethods;\n"; |
| 5400 | Result += "\tconst struct _objc_protocol_list *baseProtocols;\n"; |
| 5401 | Result += "\tconst struct _ivar_list_t *ivars;\n"; |
| 5402 | Result += "\tconst unsigned char *weakIvarLayout;\n"; |
| 5403 | Result += "\tconst struct _prop_list_t *properties;\n"; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 5404 | Result += "};\n"; |
| 5405 | |
| 5406 | Result += "\nstruct _class_t {\n"; |
| 5407 | Result += "\tstruct _class_t *isa;\n"; |
Fariborz Jahanian | fd4ce2c | 2012-03-20 17:34:50 +0000 | [diff] [blame] | 5408 | Result += "\tstruct _class_t *superclass;\n"; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 5409 | Result += "\tvoid *cache;\n"; |
| 5410 | Result += "\tvoid *vtable;\n"; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 5411 | Result += "\tstruct _class_ro_t *ro;\n"; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 5412 | Result += "};\n"; |
| 5413 | |
| 5414 | Result += "\nstruct _category_t {\n"; |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 5415 | Result += "\tconst char *name;\n"; |
Fariborz Jahanian | 4b2fe6e | 2012-03-20 21:41:28 +0000 | [diff] [blame] | 5416 | Result += "\tstruct _class_t *cls;\n"; |
Fariborz Jahanian | 4e825df | 2012-03-21 16:23:16 +0000 | [diff] [blame] | 5417 | Result += "\tconst struct _method_list_t *instance_methods;\n"; |
| 5418 | Result += "\tconst struct _method_list_t *class_methods;\n"; |
| 5419 | Result += "\tconst struct _protocol_list_t *protocols;\n"; |
| 5420 | Result += "\tconst struct _prop_list_t *properties;\n"; |
Fariborz Jahanian | 42e9a35 | 2012-02-10 00:04:22 +0000 | [diff] [blame] | 5421 | Result += "};\n"; |
| 5422 | |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 5423 | Result += "extern \"C\" __declspec(dllimport) struct objc_cache _objc_empty_cache;\n"; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 5424 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5425 | meta_data_declared = true; |
| 5426 | } |
| 5427 | |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 5428 | static void Write_protocol_list_t_TypeDecl(std::string &Result, |
| 5429 | long super_protocol_count) { |
| 5430 | Result += "struct /*_protocol_list_t*/"; Result += " {\n"; |
| 5431 | Result += "\tlong protocol_count; // Note, this is 32/64 bit\n"; |
| 5432 | Result += "\tstruct _protocol_t *super_protocols["; |
| 5433 | Result += utostr(super_protocol_count); Result += "];\n"; |
| 5434 | Result += "}"; |
| 5435 | } |
| 5436 | |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 5437 | static void Write_method_list_t_TypeDecl(std::string &Result, |
| 5438 | unsigned int method_count) { |
| 5439 | Result += "struct /*_method_list_t*/"; Result += " {\n"; |
| 5440 | Result += "\tunsigned int entsize; // sizeof(struct _objc_method)\n"; |
| 5441 | Result += "\tunsigned int method_count;\n"; |
| 5442 | Result += "\tstruct _objc_method method_list["; |
| 5443 | Result += utostr(method_count); Result += "];\n"; |
| 5444 | Result += "}"; |
| 5445 | } |
| 5446 | |
Fariborz Jahanian | da35eac | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 5447 | static void Write__prop_list_t_TypeDecl(std::string &Result, |
| 5448 | unsigned int property_count) { |
| 5449 | Result += "struct /*_prop_list_t*/"; Result += " {\n"; |
| 5450 | Result += "\tunsigned int entsize; // sizeof(struct _prop_t)\n"; |
| 5451 | Result += "\tunsigned int count_of_properties;\n"; |
| 5452 | Result += "\tstruct _prop_t prop_list["; |
| 5453 | Result += utostr(property_count); Result += "];\n"; |
| 5454 | Result += "}"; |
| 5455 | } |
| 5456 | |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 5457 | static void Write__ivar_list_t_TypeDecl(std::string &Result, |
| 5458 | unsigned int ivar_count) { |
| 5459 | Result += "struct /*_ivar_list_t*/"; Result += " {\n"; |
| 5460 | Result += "\tunsigned int entsize; // sizeof(struct _prop_t)\n"; |
| 5461 | Result += "\tunsigned int count;\n"; |
| 5462 | Result += "\tstruct _ivar_t ivar_list["; |
| 5463 | Result += utostr(ivar_count); Result += "];\n"; |
| 5464 | Result += "}"; |
| 5465 | } |
| 5466 | |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 5467 | static void Write_protocol_list_initializer(ASTContext *Context, std::string &Result, |
| 5468 | ArrayRef<ObjCProtocolDecl *> SuperProtocols, |
| 5469 | StringRef VarName, |
| 5470 | StringRef ProtocolName) { |
| 5471 | if (SuperProtocols.size() > 0) { |
| 5472 | Result += "\nstatic "; |
| 5473 | Write_protocol_list_t_TypeDecl(Result, SuperProtocols.size()); |
| 5474 | Result += " "; Result += VarName; |
| 5475 | Result += ProtocolName; |
| 5476 | Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n"; |
| 5477 | Result += "\t"; Result += utostr(SuperProtocols.size()); Result += ",\n"; |
| 5478 | for (unsigned i = 0, e = SuperProtocols.size(); i < e; i++) { |
| 5479 | ObjCProtocolDecl *SuperPD = SuperProtocols[i]; |
| 5480 | Result += "\t&"; Result += "_OBJC_PROTOCOL_"; |
| 5481 | Result += SuperPD->getNameAsString(); |
| 5482 | if (i == e-1) |
| 5483 | Result += "\n};\n"; |
| 5484 | else |
| 5485 | Result += ",\n"; |
| 5486 | } |
| 5487 | } |
| 5488 | } |
| 5489 | |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 5490 | static void Write_method_list_t_initializer(RewriteModernObjC &RewriteObj, |
| 5491 | ASTContext *Context, std::string &Result, |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 5492 | ArrayRef<ObjCMethodDecl *> Methods, |
| 5493 | StringRef VarName, |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 5494 | StringRef TopLevelDeclName, |
| 5495 | bool MethodImpl) { |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 5496 | if (Methods.size() > 0) { |
| 5497 | Result += "\nstatic "; |
| 5498 | Write_method_list_t_TypeDecl(Result, Methods.size()); |
| 5499 | Result += " "; Result += VarName; |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 5500 | Result += TopLevelDeclName; |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 5501 | Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n"; |
| 5502 | Result += "\t"; Result += "sizeof(_objc_method)"; Result += ",\n"; |
| 5503 | Result += "\t"; Result += utostr(Methods.size()); Result += ",\n"; |
| 5504 | for (unsigned i = 0, e = Methods.size(); i < e; i++) { |
| 5505 | ObjCMethodDecl *MD = Methods[i]; |
| 5506 | if (i == 0) |
| 5507 | Result += "\t{{(struct objc_selector *)\""; |
| 5508 | else |
| 5509 | Result += "\t{(struct objc_selector *)\""; |
| 5510 | Result += (MD)->getSelector().getAsString(); Result += "\""; |
| 5511 | Result += ", "; |
| 5512 | std::string MethodTypeString; |
| 5513 | Context->getObjCEncodingForMethodDecl(MD, MethodTypeString); |
| 5514 | Result += "\""; Result += MethodTypeString; Result += "\""; |
| 5515 | Result += ", "; |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 5516 | if (!MethodImpl) |
| 5517 | Result += "0"; |
| 5518 | else { |
| 5519 | Result += "(void *)"; |
| 5520 | Result += RewriteObj.MethodInternalNames[MD]; |
| 5521 | } |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 5522 | if (i == e-1) |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 5523 | Result += "}}\n"; |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 5524 | else |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 5525 | Result += "},\n"; |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 5526 | } |
| 5527 | Result += "};\n"; |
| 5528 | } |
| 5529 | } |
| 5530 | |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 5531 | static void Write_prop_list_t_initializer(RewriteModernObjC &RewriteObj, |
Fariborz Jahanian | da35eac | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 5532 | ASTContext *Context, std::string &Result, |
| 5533 | ArrayRef<ObjCPropertyDecl *> Properties, |
| 5534 | const Decl *Container, |
| 5535 | StringRef VarName, |
| 5536 | StringRef ProtocolName) { |
| 5537 | if (Properties.size() > 0) { |
| 5538 | Result += "\nstatic "; |
| 5539 | Write__prop_list_t_TypeDecl(Result, Properties.size()); |
| 5540 | Result += " "; Result += VarName; |
| 5541 | Result += ProtocolName; |
| 5542 | Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n"; |
| 5543 | Result += "\t"; Result += "sizeof(_prop_t)"; Result += ",\n"; |
| 5544 | Result += "\t"; Result += utostr(Properties.size()); Result += ",\n"; |
| 5545 | for (unsigned i = 0, e = Properties.size(); i < e; i++) { |
| 5546 | ObjCPropertyDecl *PropDecl = Properties[i]; |
| 5547 | if (i == 0) |
| 5548 | Result += "\t{{\""; |
| 5549 | else |
| 5550 | Result += "\t{\""; |
| 5551 | Result += PropDecl->getName(); Result += "\","; |
| 5552 | std::string PropertyTypeString, QuotePropertyTypeString; |
| 5553 | Context->getObjCEncodingForPropertyDecl(PropDecl, Container, PropertyTypeString); |
| 5554 | RewriteObj.QuoteDoublequotes(PropertyTypeString, QuotePropertyTypeString); |
| 5555 | Result += "\""; Result += QuotePropertyTypeString; Result += "\""; |
| 5556 | if (i == e-1) |
| 5557 | Result += "}}\n"; |
| 5558 | else |
| 5559 | Result += "},\n"; |
| 5560 | } |
| 5561 | Result += "};\n"; |
| 5562 | } |
| 5563 | } |
| 5564 | |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 5565 | // Metadata flags |
| 5566 | enum MetaDataDlags { |
| 5567 | CLS = 0x0, |
| 5568 | CLS_META = 0x1, |
| 5569 | CLS_ROOT = 0x2, |
| 5570 | OBJC2_CLS_HIDDEN = 0x10, |
| 5571 | CLS_EXCEPTION = 0x20, |
| 5572 | |
| 5573 | /// (Obsolete) ARC-specific: this class has a .release_ivars method |
| 5574 | CLS_HAS_IVAR_RELEASER = 0x40, |
| 5575 | /// class was compiled with -fobjc-arr |
| 5576 | CLS_COMPILED_BY_ARC = 0x80 // (1<<7) |
| 5577 | }; |
| 5578 | |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 5579 | static void Write__class_ro_t_initializer(ASTContext *Context, std::string &Result, |
| 5580 | unsigned int flags, |
| 5581 | const std::string &InstanceStart, |
| 5582 | const std::string &InstanceSize, |
| 5583 | ArrayRef<ObjCMethodDecl *>baseMethods, |
| 5584 | ArrayRef<ObjCProtocolDecl *>baseProtocols, |
| 5585 | ArrayRef<ObjCIvarDecl *>ivars, |
| 5586 | ArrayRef<ObjCPropertyDecl *>Properties, |
| 5587 | StringRef VarName, |
| 5588 | StringRef ClassName) { |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 5589 | Result += "\nstatic struct _class_ro_t "; |
| 5590 | Result += VarName; Result += ClassName; |
| 5591 | Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n"; |
| 5592 | Result += "\t"; |
| 5593 | Result += llvm::utostr(flags); Result += ", "; |
| 5594 | Result += InstanceStart; Result += ", "; |
| 5595 | Result += InstanceSize; Result += ", \n"; |
| 5596 | Result += "\t"; |
Fariborz Jahanian | de5d946 | 2012-03-14 18:09:23 +0000 | [diff] [blame] | 5597 | const llvm::Triple &Triple(Context->getTargetInfo().getTriple()); |
| 5598 | if (Triple.getArch() == llvm::Triple::x86_64) |
| 5599 | // uint32_t const reserved; // only when building for 64bit targets |
| 5600 | Result += "(unsigned int)0, \n\t"; |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 5601 | // const uint8_t * const ivarLayout; |
| 5602 | Result += "0, \n\t"; |
| 5603 | Result += "\""; Result += ClassName; Result += "\",\n\t"; |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 5604 | bool metaclass = ((flags & CLS_META) != 0); |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 5605 | if (baseMethods.size() > 0) { |
| 5606 | Result += "(const struct _method_list_t *)&"; |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 5607 | if (metaclass) |
| 5608 | Result += "_OBJC_$_CLASS_METHODS_"; |
| 5609 | else |
| 5610 | Result += "_OBJC_$_INSTANCE_METHODS_"; |
| 5611 | Result += ClassName; |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 5612 | Result += ",\n\t"; |
| 5613 | } |
| 5614 | else |
| 5615 | Result += "0, \n\t"; |
| 5616 | |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 5617 | if (!metaclass && baseProtocols.size() > 0) { |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 5618 | Result += "(const struct _objc_protocol_list *)&"; |
| 5619 | Result += "_OBJC_CLASS_PROTOCOLS_$_"; Result += ClassName; |
| 5620 | Result += ",\n\t"; |
| 5621 | } |
| 5622 | else |
| 5623 | Result += "0, \n\t"; |
| 5624 | |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 5625 | if (!metaclass && ivars.size() > 0) { |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 5626 | Result += "(const struct _ivar_list_t *)&"; |
| 5627 | Result += "_OBJC_$_INSTANCE_VARIABLES_"; Result += ClassName; |
| 5628 | Result += ",\n\t"; |
| 5629 | } |
| 5630 | else |
| 5631 | Result += "0, \n\t"; |
| 5632 | |
| 5633 | // weakIvarLayout |
| 5634 | Result += "0, \n\t"; |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 5635 | if (!metaclass && Properties.size() > 0) { |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 5636 | Result += "(const struct _prop_list_t *)&"; |
Fariborz Jahanian | eeabf38 | 2012-02-16 21:57:59 +0000 | [diff] [blame] | 5637 | Result += "_OBJC_$_PROP_LIST_"; Result += ClassName; |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 5638 | Result += ",\n"; |
| 5639 | } |
| 5640 | else |
| 5641 | Result += "0, \n"; |
| 5642 | |
| 5643 | Result += "};\n"; |
| 5644 | } |
| 5645 | |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 5646 | static void Write_class_t(ASTContext *Context, std::string &Result, |
| 5647 | StringRef VarName, |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 5648 | const ObjCInterfaceDecl *CDecl, bool metaclass) { |
| 5649 | bool rootClass = (!CDecl->getSuperClass()); |
| 5650 | const ObjCInterfaceDecl *RootClass = CDecl; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 5651 | |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 5652 | if (!rootClass) { |
| 5653 | // Find the Root class |
| 5654 | RootClass = CDecl->getSuperClass(); |
| 5655 | while (RootClass->getSuperClass()) { |
| 5656 | RootClass = RootClass->getSuperClass(); |
| 5657 | } |
| 5658 | } |
| 5659 | |
| 5660 | if (metaclass && rootClass) { |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 5661 | // Need to handle a case of use of forward declaration. |
Fariborz Jahanian | ce0d897 | 2012-03-10 18:25:06 +0000 | [diff] [blame] | 5662 | Result += "\n"; |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 5663 | Result += "extern \"C\" "; |
Fariborz Jahanian | ce0d897 | 2012-03-10 18:25:06 +0000 | [diff] [blame] | 5664 | if (CDecl->getImplementation()) |
| 5665 | Result += "__declspec(dllexport) "; |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 5666 | Result += "struct _class_t OBJC_CLASS_$_"; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 5667 | Result += CDecl->getNameAsString(); |
| 5668 | Result += ";\n"; |
| 5669 | } |
| 5670 | // Also, for possibility of 'super' metadata class not having been defined yet. |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 5671 | if (!rootClass) { |
Fariborz Jahanian | ce0d897 | 2012-03-10 18:25:06 +0000 | [diff] [blame] | 5672 | Result += "\n"; |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 5673 | Result += "extern \"C\" "; |
Fariborz Jahanian | ce0d897 | 2012-03-10 18:25:06 +0000 | [diff] [blame] | 5674 | if (CDecl->getSuperClass()->getImplementation()) |
| 5675 | Result += "__declspec(dllexport) "; |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 5676 | Result += "struct _class_t "; |
Fariborz Jahanian | ce0d897 | 2012-03-10 18:25:06 +0000 | [diff] [blame] | 5677 | Result += VarName; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 5678 | Result += CDecl->getSuperClass()->getNameAsString(); |
| 5679 | Result += ";\n"; |
Fariborz Jahanian | 452eac1 | 2012-03-20 21:09:58 +0000 | [diff] [blame] | 5680 | |
| 5681 | if (metaclass) { |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 5682 | Result += "extern \"C\" "; |
Fariborz Jahanian | 452eac1 | 2012-03-20 21:09:58 +0000 | [diff] [blame] | 5683 | if (RootClass->getImplementation()) |
| 5684 | Result += "__declspec(dllexport) "; |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 5685 | Result += "struct _class_t "; |
Fariborz Jahanian | 452eac1 | 2012-03-20 21:09:58 +0000 | [diff] [blame] | 5686 | Result += VarName; |
| 5687 | Result += RootClass->getNameAsString(); |
| 5688 | Result += ";\n"; |
| 5689 | } |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 5690 | } |
| 5691 | |
Fariborz Jahanian | e57303c | 2012-03-10 00:39:34 +0000 | [diff] [blame] | 5692 | Result += "\n__declspec(dllexport) struct _class_t "; Result += VarName; Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 5693 | Result += " __attribute__ ((used, section (\"__DATA,__objc_data\"))) = {\n"; |
| 5694 | Result += "\t"; |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 5695 | if (metaclass) { |
| 5696 | if (!rootClass) { |
| 5697 | Result += "0, // &"; Result += VarName; |
| 5698 | Result += RootClass->getNameAsString(); |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 5699 | Result += ",\n\t"; |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 5700 | Result += "0, // &"; Result += VarName; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 5701 | Result += CDecl->getSuperClass()->getNameAsString(); |
| 5702 | Result += ",\n\t"; |
| 5703 | } |
| 5704 | else { |
Fariborz Jahanian | 452eac1 | 2012-03-20 21:09:58 +0000 | [diff] [blame] | 5705 | Result += "0, // &"; Result += VarName; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 5706 | Result += CDecl->getNameAsString(); |
| 5707 | Result += ",\n\t"; |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 5708 | Result += "0, // &OBJC_CLASS_$_"; Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 5709 | Result += ",\n\t"; |
| 5710 | } |
| 5711 | } |
| 5712 | else { |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 5713 | Result += "0, // &OBJC_METACLASS_$_"; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 5714 | Result += CDecl->getNameAsString(); |
| 5715 | Result += ",\n\t"; |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 5716 | if (!rootClass) { |
| 5717 | Result += "0, // &"; Result += VarName; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 5718 | Result += CDecl->getSuperClass()->getNameAsString(); |
| 5719 | Result += ",\n\t"; |
| 5720 | } |
| 5721 | else |
| 5722 | Result += "0,\n\t"; |
| 5723 | } |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 5724 | Result += "0, // (void *)&_objc_empty_cache,\n\t"; |
| 5725 | Result += "0, // unused, was (void *)&_objc_empty_vtable,\n\t"; |
| 5726 | if (metaclass) |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 5727 | Result += "&_OBJC_METACLASS_RO_$_"; |
| 5728 | else |
| 5729 | Result += "&_OBJC_CLASS_RO_$_"; |
| 5730 | Result += CDecl->getNameAsString(); |
| 5731 | Result += ",\n};\n"; |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 5732 | |
| 5733 | // Add static function to initialize some of the meta-data fields. |
| 5734 | // avoid doing it twice. |
| 5735 | if (metaclass) |
| 5736 | return; |
| 5737 | |
| 5738 | const ObjCInterfaceDecl *SuperClass = |
| 5739 | rootClass ? CDecl : CDecl->getSuperClass(); |
| 5740 | |
| 5741 | Result += "static void OBJC_CLASS_SETUP_$_"; |
| 5742 | Result += CDecl->getNameAsString(); |
| 5743 | Result += "(void ) {\n"; |
| 5744 | Result += "\tOBJC_METACLASS_$_"; Result += CDecl->getNameAsString(); |
| 5745 | Result += ".isa = "; Result += "&OBJC_METACLASS_$_"; |
Fariborz Jahanian | 452eac1 | 2012-03-20 21:09:58 +0000 | [diff] [blame] | 5746 | Result += RootClass->getNameAsString(); Result += ";\n"; |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 5747 | |
| 5748 | Result += "\tOBJC_METACLASS_$_"; Result += CDecl->getNameAsString(); |
Fariborz Jahanian | 452eac1 | 2012-03-20 21:09:58 +0000 | [diff] [blame] | 5749 | Result += ".superclass = "; |
| 5750 | if (rootClass) |
| 5751 | Result += "&OBJC_CLASS_$_"; |
| 5752 | else |
| 5753 | Result += "&OBJC_METACLASS_$_"; |
| 5754 | |
Fariborz Jahanian | a03e40c | 2012-03-20 19:54:33 +0000 | [diff] [blame] | 5755 | Result += SuperClass->getNameAsString(); Result += ";\n"; |
| 5756 | |
| 5757 | Result += "\tOBJC_METACLASS_$_"; Result += CDecl->getNameAsString(); |
| 5758 | Result += ".cache = "; Result += "&_objc_empty_cache"; Result += ";\n"; |
| 5759 | |
| 5760 | Result += "\tOBJC_CLASS_$_"; Result += CDecl->getNameAsString(); |
| 5761 | Result += ".isa = "; Result += "&OBJC_METACLASS_$_"; |
| 5762 | Result += CDecl->getNameAsString(); Result += ";\n"; |
| 5763 | |
| 5764 | if (!rootClass) { |
| 5765 | Result += "\tOBJC_CLASS_$_"; Result += CDecl->getNameAsString(); |
| 5766 | Result += ".superclass = "; Result += "&OBJC_CLASS_$_"; |
| 5767 | Result += SuperClass->getNameAsString(); Result += ";\n"; |
| 5768 | } |
| 5769 | |
| 5770 | Result += "\tOBJC_CLASS_$_"; Result += CDecl->getNameAsString(); |
| 5771 | Result += ".cache = "; Result += "&_objc_empty_cache"; Result += ";\n"; |
| 5772 | Result += "}\n"; |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 5773 | } |
| 5774 | |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 5775 | static void Write_category_t(RewriteModernObjC &RewriteObj, ASTContext *Context, |
| 5776 | std::string &Result, |
Fariborz Jahanian | e033578 | 2012-03-27 18:41:05 +0000 | [diff] [blame^] | 5777 | ObjCCategoryDecl *CatDecl, |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 5778 | ObjCInterfaceDecl *ClassDecl, |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 5779 | ArrayRef<ObjCMethodDecl *> InstanceMethods, |
| 5780 | ArrayRef<ObjCMethodDecl *> ClassMethods, |
| 5781 | ArrayRef<ObjCProtocolDecl *> RefedProtocols, |
| 5782 | ArrayRef<ObjCPropertyDecl *> ClassProperties) { |
Fariborz Jahanian | e033578 | 2012-03-27 18:41:05 +0000 | [diff] [blame^] | 5783 | StringRef CatName = CatDecl->getName(); |
NAKAMURA Takumi | 20f8939 | 2012-03-21 03:21:46 +0000 | [diff] [blame] | 5784 | StringRef ClassName = ClassDecl->getName(); |
Fariborz Jahanian | 8c00a1b | 2012-02-17 20:33:00 +0000 | [diff] [blame] | 5785 | // must declare an extern class object in case this class is not implemented |
| 5786 | // in this TU. |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 5787 | Result += "\n"; |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 5788 | Result += "extern \"C\" "; |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 5789 | if (ClassDecl->getImplementation()) |
| 5790 | Result += "__declspec(dllexport) "; |
| 5791 | |
Fariborz Jahanian | 3f162c3 | 2012-03-27 16:21:30 +0000 | [diff] [blame] | 5792 | Result += "struct _class_t "; |
Fariborz Jahanian | 8c00a1b | 2012-02-17 20:33:00 +0000 | [diff] [blame] | 5793 | Result += "OBJC_CLASS_$_"; Result += ClassName; |
| 5794 | Result += ";\n"; |
| 5795 | |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 5796 | Result += "\nstatic struct _category_t "; |
| 5797 | Result += "_OBJC_$_CATEGORY_"; |
| 5798 | Result += ClassName; Result += "_$_"; Result += CatName; |
| 5799 | Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = \n"; |
| 5800 | Result += "{\n"; |
| 5801 | Result += "\t\""; Result += ClassName; Result += "\",\n"; |
Fariborz Jahanian | 4b2fe6e | 2012-03-20 21:41:28 +0000 | [diff] [blame] | 5802 | Result += "\t0, // &"; Result += "OBJC_CLASS_$_"; Result += ClassName; |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 5803 | Result += ",\n"; |
| 5804 | if (InstanceMethods.size() > 0) { |
| 5805 | Result += "\t(const struct _method_list_t *)&"; |
| 5806 | Result += "_OBJC_$_CATEGORY_INSTANCE_METHODS_"; |
| 5807 | Result += ClassName; Result += "_$_"; Result += CatName; |
| 5808 | Result += ",\n"; |
| 5809 | } |
| 5810 | else |
| 5811 | Result += "\t0,\n"; |
| 5812 | |
| 5813 | if (ClassMethods.size() > 0) { |
| 5814 | Result += "\t(const struct _method_list_t *)&"; |
| 5815 | Result += "_OBJC_$_CATEGORY_CLASS_METHODS_"; |
| 5816 | Result += ClassName; Result += "_$_"; Result += CatName; |
| 5817 | Result += ",\n"; |
| 5818 | } |
| 5819 | else |
| 5820 | Result += "\t0,\n"; |
| 5821 | |
| 5822 | if (RefedProtocols.size() > 0) { |
| 5823 | Result += "\t(const struct _protocol_list_t *)&"; |
| 5824 | Result += "_OBJC_CATEGORY_PROTOCOLS_$_"; |
| 5825 | Result += ClassName; Result += "_$_"; Result += CatName; |
| 5826 | Result += ",\n"; |
| 5827 | } |
| 5828 | else |
| 5829 | Result += "\t0,\n"; |
| 5830 | |
| 5831 | if (ClassProperties.size() > 0) { |
| 5832 | Result += "\t(const struct _prop_list_t *)&"; Result += "_OBJC_$_PROP_LIST_"; |
| 5833 | Result += ClassName; Result += "_$_"; Result += CatName; |
| 5834 | Result += ",\n"; |
| 5835 | } |
| 5836 | else |
| 5837 | Result += "\t0,\n"; |
| 5838 | |
| 5839 | Result += "};\n"; |
Fariborz Jahanian | 4b2fe6e | 2012-03-20 21:41:28 +0000 | [diff] [blame] | 5840 | |
| 5841 | // Add static function to initialize the class pointer in the category structure. |
| 5842 | Result += "static void OBJC_CATEGORY_SETUP_$_"; |
| 5843 | Result += ClassDecl->getNameAsString(); |
| 5844 | Result += "_$_"; |
| 5845 | Result += CatName; |
| 5846 | Result += "(void ) {\n"; |
| 5847 | Result += "\t_OBJC_$_CATEGORY_"; |
| 5848 | Result += ClassDecl->getNameAsString(); |
| 5849 | Result += "_$_"; |
| 5850 | Result += CatName; |
| 5851 | Result += ".cls = "; Result += "&OBJC_CLASS_$_"; Result += ClassName; |
| 5852 | Result += ";\n}\n"; |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 5853 | } |
| 5854 | |
Fariborz Jahanian | e0adbd8 | 2012-02-08 22:23:26 +0000 | [diff] [blame] | 5855 | static void Write__extendedMethodTypes_initializer(RewriteModernObjC &RewriteObj, |
| 5856 | ASTContext *Context, std::string &Result, |
| 5857 | ArrayRef<ObjCMethodDecl *> Methods, |
| 5858 | StringRef VarName, |
| 5859 | StringRef ProtocolName) { |
| 5860 | if (Methods.size() == 0) |
| 5861 | return; |
| 5862 | |
| 5863 | Result += "\nstatic const char *"; |
| 5864 | Result += VarName; Result += ProtocolName; |
| 5865 | Result += " [] __attribute__ ((used, section (\"__DATA,__objc_const\"))) = \n"; |
| 5866 | Result += "{\n"; |
| 5867 | for (unsigned i = 0, e = Methods.size(); i < e; i++) { |
| 5868 | ObjCMethodDecl *MD = Methods[i]; |
| 5869 | std::string MethodTypeString, QuoteMethodTypeString; |
| 5870 | Context->getObjCEncodingForMethodDecl(MD, MethodTypeString, true); |
| 5871 | RewriteObj.QuoteDoublequotes(MethodTypeString, QuoteMethodTypeString); |
| 5872 | Result += "\t\""; Result += QuoteMethodTypeString; Result += "\""; |
| 5873 | if (i == e-1) |
| 5874 | Result += "\n};\n"; |
| 5875 | else { |
| 5876 | Result += ",\n"; |
| 5877 | } |
| 5878 | } |
| 5879 | } |
| 5880 | |
Fariborz Jahanian | 40a777a | 2012-03-12 16:46:58 +0000 | [diff] [blame] | 5881 | static void Write_IvarOffsetVar(ASTContext *Context, |
| 5882 | std::string &Result, |
Fariborz Jahanian | db64923 | 2012-02-13 20:59:02 +0000 | [diff] [blame] | 5883 | ArrayRef<ObjCIvarDecl *> Ivars, |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 5884 | ObjCInterfaceDecl *CDecl) { |
Fariborz Jahanian | db64923 | 2012-02-13 20:59:02 +0000 | [diff] [blame] | 5885 | // FIXME. visibilty of offset symbols may have to be set; for Darwin |
| 5886 | // this is what happens: |
| 5887 | /** |
| 5888 | if (Ivar->getAccessControl() == ObjCIvarDecl::Private || |
| 5889 | Ivar->getAccessControl() == ObjCIvarDecl::Package || |
| 5890 | Class->getVisibility() == HiddenVisibility) |
| 5891 | Visibility shoud be: HiddenVisibility; |
| 5892 | else |
| 5893 | Visibility shoud be: DefaultVisibility; |
| 5894 | */ |
| 5895 | |
Fariborz Jahanian | 07e5288 | 2012-02-13 21:34:45 +0000 | [diff] [blame] | 5896 | Result += "\n"; |
| 5897 | for (unsigned i =0, e = Ivars.size(); i < e; i++) { |
| 5898 | ObjCIvarDecl *IvarDecl = Ivars[i]; |
Fariborz Jahanian | 40a777a | 2012-03-12 16:46:58 +0000 | [diff] [blame] | 5899 | if (Context->getLangOpts().MicrosoftExt) |
| 5900 | Result += "__declspec(allocate(\".objc_ivar$B\")) "; |
| 5901 | |
| 5902 | if (!Context->getLangOpts().MicrosoftExt || |
| 5903 | IvarDecl->getAccessControl() == ObjCIvarDecl::Private || |
Fariborz Jahanian | 117591f | 2012-03-10 01:34:42 +0000 | [diff] [blame] | 5904 | IvarDecl->getAccessControl() == ObjCIvarDecl::Package) |
Fariborz Jahanian | d1c84d3 | 2012-03-10 00:53:02 +0000 | [diff] [blame] | 5905 | Result += "unsigned long int "; |
| 5906 | else |
| 5907 | Result += "__declspec(dllexport) unsigned long int "; |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 5908 | WriteInternalIvarName(CDecl, IvarDecl, Result); |
Fariborz Jahanian | 07e5288 | 2012-02-13 21:34:45 +0000 | [diff] [blame] | 5909 | Result += " __attribute__ ((used, section (\"__DATA,__objc_ivar\")))"; |
| 5910 | Result += " = "; |
| 5911 | if (IvarDecl->isBitField()) { |
| 5912 | // FIXME: The hack below doesn't work for bitfields. For now, we simply |
| 5913 | // place all bitfields at offset 0. |
| 5914 | Result += "0;\n"; |
| 5915 | } |
| 5916 | else { |
Fariborz Jahanian | db64923 | 2012-02-13 20:59:02 +0000 | [diff] [blame] | 5917 | Result += "__OFFSETOFIVAR__(struct "; |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 5918 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | db64923 | 2012-02-13 20:59:02 +0000 | [diff] [blame] | 5919 | Result += "_IMPL, "; |
| 5920 | Result += IvarDecl->getName(); Result += ");\n"; |
| 5921 | } |
| 5922 | } |
| 5923 | } |
| 5924 | |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 5925 | static void Write__ivar_list_t_initializer(RewriteModernObjC &RewriteObj, |
| 5926 | ASTContext *Context, std::string &Result, |
| 5927 | ArrayRef<ObjCIvarDecl *> Ivars, |
| 5928 | StringRef VarName, |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 5929 | ObjCInterfaceDecl *CDecl) { |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 5930 | if (Ivars.size() > 0) { |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 5931 | Write_IvarOffsetVar(Context, Result, Ivars, CDecl); |
Fariborz Jahanian | 07e5288 | 2012-02-13 21:34:45 +0000 | [diff] [blame] | 5932 | |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 5933 | Result += "\nstatic "; |
| 5934 | Write__ivar_list_t_TypeDecl(Result, Ivars.size()); |
| 5935 | Result += " "; Result += VarName; |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 5936 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 5937 | Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n"; |
| 5938 | Result += "\t"; Result += "sizeof(_ivar_t)"; Result += ",\n"; |
| 5939 | Result += "\t"; Result += utostr(Ivars.size()); Result += ",\n"; |
| 5940 | for (unsigned i =0, e = Ivars.size(); i < e; i++) { |
| 5941 | ObjCIvarDecl *IvarDecl = Ivars[i]; |
| 5942 | if (i == 0) |
| 5943 | Result += "\t{{"; |
| 5944 | else |
| 5945 | Result += "\t {"; |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 5946 | Result += "(unsigned long int *)&"; |
| 5947 | WriteInternalIvarName(CDecl, IvarDecl, Result); |
Fariborz Jahanian | db64923 | 2012-02-13 20:59:02 +0000 | [diff] [blame] | 5948 | Result += ", "; |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 5949 | |
| 5950 | Result += "\""; Result += IvarDecl->getName(); Result += "\", "; |
| 5951 | std::string IvarTypeString, QuoteIvarTypeString; |
| 5952 | Context->getObjCEncodingForType(IvarDecl->getType(), IvarTypeString, |
| 5953 | IvarDecl); |
| 5954 | RewriteObj.QuoteDoublequotes(IvarTypeString, QuoteIvarTypeString); |
| 5955 | Result += "\""; Result += QuoteIvarTypeString; Result += "\", "; |
| 5956 | |
Fariborz Jahanian | 8f1fed0 | 2012-02-11 20:10:52 +0000 | [diff] [blame] | 5957 | // FIXME. this alignment represents the host alignment and need be changed to |
| 5958 | // represent the target alignment. |
| 5959 | unsigned Align = Context->getTypeAlign(IvarDecl->getType())/8; |
| 5960 | Align = llvm::Log2_32(Align); |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 5961 | Result += llvm::utostr(Align); Result += ", "; |
Fariborz Jahanian | a63b422 | 2012-02-10 23:18:24 +0000 | [diff] [blame] | 5962 | CharUnits Size = Context->getTypeSizeInChars(IvarDecl->getType()); |
| 5963 | Result += llvm::utostr(Size.getQuantity()); |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 5964 | if (i == e-1) |
| 5965 | Result += "}}\n"; |
| 5966 | else |
| 5967 | Result += "},\n"; |
| 5968 | } |
| 5969 | Result += "};\n"; |
| 5970 | } |
| 5971 | } |
| 5972 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5973 | /// RewriteObjCProtocolMetaData - Rewrite protocols meta-data. |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 5974 | void RewriteModernObjC::RewriteObjCProtocolMetaData(ObjCProtocolDecl *PDecl, |
| 5975 | std::string &Result) { |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5976 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5977 | // Do not synthesize the protocol more than once. |
| 5978 | if (ObjCSynthesizedProtocols.count(PDecl->getCanonicalDecl())) |
| 5979 | return; |
Fariborz Jahanian | de5d946 | 2012-03-14 18:09:23 +0000 | [diff] [blame] | 5980 | WriteModernMetadataDeclarations(Context, Result); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5981 | |
| 5982 | if (ObjCProtocolDecl *Def = PDecl->getDefinition()) |
| 5983 | PDecl = Def; |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 5984 | // Must write out all protocol definitions in current qualifier list, |
| 5985 | // and in their nested qualifiers before writing out current definition. |
| 5986 | for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(), |
| 5987 | E = PDecl->protocol_end(); I != E; ++I) |
| 5988 | RewriteObjCProtocolMetaData(*I, Result); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 5989 | |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 5990 | // Construct method lists. |
| 5991 | std::vector<ObjCMethodDecl *> InstanceMethods, ClassMethods; |
| 5992 | std::vector<ObjCMethodDecl *> OptInstanceMethods, OptClassMethods; |
| 5993 | for (ObjCProtocolDecl::instmeth_iterator |
| 5994 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
| 5995 | I != E; ++I) { |
| 5996 | ObjCMethodDecl *MD = *I; |
| 5997 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 5998 | OptInstanceMethods.push_back(MD); |
| 5999 | } else { |
| 6000 | InstanceMethods.push_back(MD); |
| 6001 | } |
| 6002 | } |
| 6003 | |
| 6004 | for (ObjCProtocolDecl::classmeth_iterator |
| 6005 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
| 6006 | I != E; ++I) { |
| 6007 | ObjCMethodDecl *MD = *I; |
| 6008 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 6009 | OptClassMethods.push_back(MD); |
| 6010 | } else { |
| 6011 | ClassMethods.push_back(MD); |
| 6012 | } |
| 6013 | } |
Fariborz Jahanian | e0adbd8 | 2012-02-08 22:23:26 +0000 | [diff] [blame] | 6014 | std::vector<ObjCMethodDecl *> AllMethods; |
| 6015 | for (unsigned i = 0, e = InstanceMethods.size(); i < e; i++) |
| 6016 | AllMethods.push_back(InstanceMethods[i]); |
| 6017 | for (unsigned i = 0, e = ClassMethods.size(); i < e; i++) |
| 6018 | AllMethods.push_back(ClassMethods[i]); |
| 6019 | for (unsigned i = 0, e = OptInstanceMethods.size(); i < e; i++) |
| 6020 | AllMethods.push_back(OptInstanceMethods[i]); |
| 6021 | for (unsigned i = 0, e = OptClassMethods.size(); i < e; i++) |
| 6022 | AllMethods.push_back(OptClassMethods[i]); |
| 6023 | |
| 6024 | Write__extendedMethodTypes_initializer(*this, Context, Result, |
| 6025 | AllMethods, |
| 6026 | "_OBJC_PROTOCOL_METHOD_TYPES_", |
| 6027 | PDecl->getNameAsString()); |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 6028 | // Protocol's super protocol list |
| 6029 | std::vector<ObjCProtocolDecl *> SuperProtocols; |
| 6030 | for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(), |
| 6031 | E = PDecl->protocol_end(); I != E; ++I) |
| 6032 | SuperProtocols.push_back(*I); |
| 6033 | |
| 6034 | Write_protocol_list_initializer(Context, Result, SuperProtocols, |
| 6035 | "_OBJC_PROTOCOL_REFS_", |
| 6036 | PDecl->getNameAsString()); |
| 6037 | |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6038 | Write_method_list_t_initializer(*this, Context, Result, InstanceMethods, |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6039 | "_OBJC_PROTOCOL_INSTANCE_METHODS_", |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6040 | PDecl->getNameAsString(), false); |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6041 | |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6042 | Write_method_list_t_initializer(*this, Context, Result, ClassMethods, |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6043 | "_OBJC_PROTOCOL_CLASS_METHODS_", |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6044 | PDecl->getNameAsString(), false); |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6045 | |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6046 | Write_method_list_t_initializer(*this, Context, Result, OptInstanceMethods, |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6047 | "_OBJC_PROTOCOL_OPT_INSTANCE_METHODS_", |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6048 | PDecl->getNameAsString(), false); |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6049 | |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6050 | Write_method_list_t_initializer(*this, Context, Result, OptClassMethods, |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6051 | "_OBJC_PROTOCOL_OPT_CLASS_METHODS_", |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6052 | PDecl->getNameAsString(), false); |
Fariborz Jahanian | 77e4bca | 2012-02-07 20:15:08 +0000 | [diff] [blame] | 6053 | |
Fariborz Jahanian | da35eac | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 6054 | // Protocol's property metadata. |
| 6055 | std::vector<ObjCPropertyDecl *> ProtocolProperties; |
| 6056 | for (ObjCContainerDecl::prop_iterator I = PDecl->prop_begin(), |
| 6057 | E = PDecl->prop_end(); I != E; ++I) |
| 6058 | ProtocolProperties.push_back(*I); |
| 6059 | |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6060 | Write_prop_list_t_initializer(*this, Context, Result, ProtocolProperties, |
Fariborz Jahanian | da35eac | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 6061 | /* Container */0, |
| 6062 | "_OBJC_PROTOCOL_PROPERTIES_", |
| 6063 | PDecl->getNameAsString()); |
Fariborz Jahanian | da35eac | 2012-02-07 23:31:52 +0000 | [diff] [blame] | 6064 | |
Fariborz Jahanian | 82848c2 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6065 | // Writer out root metadata for current protocol: struct _protocol_t |
Fariborz Jahanian | 1ca052c | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 6066 | Result += "\n"; |
| 6067 | if (LangOpts.MicrosoftExt) |
| 6068 | Result += "__declspec(allocate(\".datacoal_nt$B\")) "; |
Fariborz Jahanian | 30650eb | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 6069 | Result += "struct _protocol_t _OBJC_PROTOCOL_"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6070 | Result += PDecl->getNameAsString(); |
Fariborz Jahanian | 82848c2 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6071 | Result += " __attribute__ ((used, section (\"__DATA,__datacoal_nt,coalesced\"))) = {\n"; |
| 6072 | Result += "\t0,\n"; // id is; is null |
| 6073 | Result += "\t\""; Result += PDecl->getNameAsString(); Result += "\",\n"; |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 6074 | if (SuperProtocols.size() > 0) { |
| 6075 | Result += "\t(const struct _protocol_list_t *)&"; Result += "_OBJC_PROTOCOL_REFS_"; |
| 6076 | Result += PDecl->getNameAsString(); Result += ",\n"; |
| 6077 | } |
| 6078 | else |
| 6079 | Result += "\t0,\n"; |
Fariborz Jahanian | 82848c2 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6080 | if (InstanceMethods.size() > 0) { |
| 6081 | Result += "\t(const struct method_list_t *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 6082 | Result += PDecl->getNameAsString(); Result += ",\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6083 | } |
| 6084 | else |
Fariborz Jahanian | 82848c2 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6085 | Result += "\t0,\n"; |
| 6086 | |
| 6087 | if (ClassMethods.size() > 0) { |
| 6088 | Result += "\t(const struct method_list_t *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
| 6089 | Result += PDecl->getNameAsString(); Result += ",\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6090 | } |
| 6091 | else |
Fariborz Jahanian | 82848c2 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6092 | Result += "\t0,\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6093 | |
Fariborz Jahanian | 82848c2 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6094 | if (OptInstanceMethods.size() > 0) { |
| 6095 | Result += "\t(const struct method_list_t *)&_OBJC_PROTOCOL_OPT_INSTANCE_METHODS_"; |
| 6096 | Result += PDecl->getNameAsString(); Result += ",\n"; |
| 6097 | } |
| 6098 | else |
| 6099 | Result += "\t0,\n"; |
| 6100 | |
| 6101 | if (OptClassMethods.size() > 0) { |
| 6102 | Result += "\t(const struct method_list_t *)&_OBJC_PROTOCOL_OPT_CLASS_METHODS_"; |
| 6103 | Result += PDecl->getNameAsString(); Result += ",\n"; |
| 6104 | } |
| 6105 | else |
| 6106 | Result += "\t0,\n"; |
| 6107 | |
| 6108 | if (ProtocolProperties.size() > 0) { |
| 6109 | Result += "\t(const struct _prop_list_t *)&_OBJC_PROTOCOL_PROPERTIES_"; |
| 6110 | Result += PDecl->getNameAsString(); Result += ",\n"; |
| 6111 | } |
| 6112 | else |
| 6113 | Result += "\t0,\n"; |
| 6114 | |
| 6115 | Result += "\t"; Result += "sizeof(_protocol_t)"; Result += ",\n"; |
| 6116 | Result += "\t0,\n"; |
| 6117 | |
Fariborz Jahanian | e0adbd8 | 2012-02-08 22:23:26 +0000 | [diff] [blame] | 6118 | if (AllMethods.size() > 0) { |
| 6119 | Result += "\t(const char **)&"; Result += "_OBJC_PROTOCOL_METHOD_TYPES_"; |
| 6120 | Result += PDecl->getNameAsString(); |
| 6121 | Result += "\n};\n"; |
| 6122 | } |
| 6123 | else |
| 6124 | Result += "\t0\n};\n"; |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 6125 | |
| 6126 | // Use this protocol meta-data to build protocol list table in section |
| 6127 | // .objc_protolist$B |
| 6128 | // Unspecified visibility means 'private extern'. |
| 6129 | if (LangOpts.MicrosoftExt) |
| 6130 | Result += "__declspec(allocate(\".objc_protolist$B\")) "; |
| 6131 | Result += "struct _protocol_t *"; |
| 6132 | Result += "_OBJC_LABEL_PROTOCOL_$_"; Result += PDecl->getNameAsString(); |
| 6133 | Result += " = &_OBJC_PROTOCOL_"; Result += PDecl->getNameAsString(); |
| 6134 | Result += ";\n"; |
Fariborz Jahanian | 82848c2 | 2012-02-08 00:50:52 +0000 | [diff] [blame] | 6135 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6136 | // Mark this protocol as having been generated. |
| 6137 | if (!ObjCSynthesizedProtocols.insert(PDecl->getCanonicalDecl())) |
| 6138 | llvm_unreachable("protocol already synthesized"); |
| 6139 | |
| 6140 | } |
| 6141 | |
| 6142 | void RewriteModernObjC::RewriteObjCProtocolListMetaData( |
| 6143 | const ObjCList<ObjCProtocolDecl> &Protocols, |
| 6144 | StringRef prefix, StringRef ClassName, |
| 6145 | std::string &Result) { |
| 6146 | if (Protocols.empty()) return; |
| 6147 | |
| 6148 | for (unsigned i = 0; i != Protocols.size(); i++) |
Fariborz Jahanian | da9624a | 2012-02-08 19:53:58 +0000 | [diff] [blame] | 6149 | RewriteObjCProtocolMetaData(Protocols[i], Result); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6150 | |
| 6151 | // Output the top lovel protocol meta-data for the class. |
| 6152 | /* struct _objc_protocol_list { |
| 6153 | struct _objc_protocol_list *next; |
| 6154 | int protocol_count; |
| 6155 | struct _objc_protocol *class_protocols[]; |
| 6156 | } |
| 6157 | */ |
Fariborz Jahanian | 1ca052c | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 6158 | Result += "\n"; |
| 6159 | if (LangOpts.MicrosoftExt) |
| 6160 | Result += "__declspec(allocate(\".cat_cls_meth$B\")) "; |
| 6161 | Result += "static struct {\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6162 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 6163 | Result += "\tint protocol_count;\n"; |
| 6164 | Result += "\tstruct _objc_protocol *class_protocols["; |
| 6165 | Result += utostr(Protocols.size()); |
| 6166 | Result += "];\n} _OBJC_"; |
| 6167 | Result += prefix; |
| 6168 | Result += "_PROTOCOLS_"; |
| 6169 | Result += ClassName; |
| 6170 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 6171 | "{\n\t0, "; |
| 6172 | Result += utostr(Protocols.size()); |
| 6173 | Result += "\n"; |
| 6174 | |
| 6175 | Result += "\t,{&_OBJC_PROTOCOL_"; |
| 6176 | Result += Protocols[0]->getNameAsString(); |
| 6177 | Result += " \n"; |
| 6178 | |
| 6179 | for (unsigned i = 1; i != Protocols.size(); i++) { |
| 6180 | Result += "\t ,&_OBJC_PROTOCOL_"; |
| 6181 | Result += Protocols[i]->getNameAsString(); |
| 6182 | Result += "\n"; |
| 6183 | } |
| 6184 | Result += "\t }\n};\n"; |
| 6185 | } |
| 6186 | |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 6187 | /// hasObjCExceptionAttribute - Return true if this class or any super |
| 6188 | /// class has the __objc_exception__ attribute. |
| 6189 | /// FIXME. Move this to ASTContext.cpp as it is also used for IRGen. |
| 6190 | static bool hasObjCExceptionAttribute(ASTContext &Context, |
| 6191 | const ObjCInterfaceDecl *OID) { |
| 6192 | if (OID->hasAttr<ObjCExceptionAttr>()) |
| 6193 | return true; |
| 6194 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
| 6195 | return hasObjCExceptionAttribute(Context, Super); |
| 6196 | return false; |
| 6197 | } |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6198 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6199 | void RewriteModernObjC::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
| 6200 | std::string &Result) { |
| 6201 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
| 6202 | |
| 6203 | // Explicitly declared @interface's are already synthesized. |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6204 | if (CDecl->isImplicitInterfaceDecl()) |
| 6205 | assert(false && |
| 6206 | "Legacy implicit interface rewriting not supported in moder abi"); |
Fariborz Jahanian | 8f1fed0 | 2012-02-11 20:10:52 +0000 | [diff] [blame] | 6207 | |
Fariborz Jahanian | de5d946 | 2012-03-14 18:09:23 +0000 | [diff] [blame] | 6208 | WriteModernMetadataDeclarations(Context, Result); |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6209 | SmallVector<ObjCIvarDecl *, 8> IVars; |
| 6210 | |
| 6211 | for (ObjCIvarDecl *IVD = CDecl->all_declared_ivar_begin(); |
| 6212 | IVD; IVD = IVD->getNextIvar()) { |
| 6213 | // Ignore unnamed bit-fields. |
| 6214 | if (!IVD->getDeclName()) |
| 6215 | continue; |
| 6216 | IVars.push_back(IVD); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6217 | } |
| 6218 | |
Fariborz Jahanian | ae93295 | 2012-02-10 20:47:10 +0000 | [diff] [blame] | 6219 | Write__ivar_list_t_initializer(*this, Context, Result, IVars, |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6220 | "_OBJC_$_INSTANCE_VARIABLES_", |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 6221 | CDecl); |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6222 | |
| 6223 | // Build _objc_method_list for class's instance methods if needed |
| 6224 | SmallVector<ObjCMethodDecl *, 32> |
| 6225 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
| 6226 | |
| 6227 | // If any of our property implementations have associated getters or |
| 6228 | // setters, produce metadata for them as well. |
| 6229 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 6230 | PropEnd = IDecl->propimpl_end(); |
| 6231 | Prop != PropEnd; ++Prop) { |
| 6232 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 6233 | continue; |
| 6234 | if (!(*Prop)->getPropertyIvarDecl()) |
| 6235 | continue; |
| 6236 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 6237 | if (!PD) |
| 6238 | continue; |
| 6239 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 6240 | if (!Getter->isDefined()) |
| 6241 | InstanceMethods.push_back(Getter); |
| 6242 | if (PD->isReadOnly()) |
| 6243 | continue; |
| 6244 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 6245 | if (!Setter->isDefined()) |
| 6246 | InstanceMethods.push_back(Setter); |
| 6247 | } |
| 6248 | |
| 6249 | Write_method_list_t_initializer(*this, Context, Result, InstanceMethods, |
| 6250 | "_OBJC_$_INSTANCE_METHODS_", |
| 6251 | IDecl->getNameAsString(), true); |
| 6252 | |
| 6253 | SmallVector<ObjCMethodDecl *, 32> |
| 6254 | ClassMethods(IDecl->classmeth_begin(), IDecl->classmeth_end()); |
| 6255 | |
| 6256 | Write_method_list_t_initializer(*this, Context, Result, ClassMethods, |
| 6257 | "_OBJC_$_CLASS_METHODS_", |
| 6258 | IDecl->getNameAsString(), true); |
Fariborz Jahanian | 0a52534 | 2012-02-14 19:31:35 +0000 | [diff] [blame] | 6259 | |
| 6260 | // Protocols referenced in class declaration? |
| 6261 | // Protocol's super protocol list |
| 6262 | std::vector<ObjCProtocolDecl *> RefedProtocols; |
| 6263 | const ObjCList<ObjCProtocolDecl> &Protocols = CDecl->getReferencedProtocols(); |
| 6264 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 6265 | E = Protocols.end(); |
| 6266 | I != E; ++I) { |
| 6267 | RefedProtocols.push_back(*I); |
| 6268 | // Must write out all protocol definitions in current qualifier list, |
| 6269 | // and in their nested qualifiers before writing out current definition. |
| 6270 | RewriteObjCProtocolMetaData(*I, Result); |
| 6271 | } |
| 6272 | |
| 6273 | Write_protocol_list_initializer(Context, Result, |
| 6274 | RefedProtocols, |
| 6275 | "_OBJC_CLASS_PROTOCOLS_$_", |
| 6276 | IDecl->getNameAsString()); |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6277 | |
| 6278 | // Protocol's property metadata. |
| 6279 | std::vector<ObjCPropertyDecl *> ClassProperties; |
| 6280 | for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(), |
| 6281 | E = CDecl->prop_end(); I != E; ++I) |
| 6282 | ClassProperties.push_back(*I); |
| 6283 | |
| 6284 | Write_prop_list_t_initializer(*this, Context, Result, ClassProperties, |
Fariborz Jahanian | 2df089d | 2012-03-22 17:39:35 +0000 | [diff] [blame] | 6285 | /* Container */IDecl, |
Fariborz Jahanian | eeabf38 | 2012-02-16 21:57:59 +0000 | [diff] [blame] | 6286 | "_OBJC_$_PROP_LIST_", |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6287 | CDecl->getNameAsString()); |
Fariborz Jahanian | 90af4e2 | 2012-02-14 17:19:02 +0000 | [diff] [blame] | 6288 | |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6289 | |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 6290 | // Data for initializing _class_ro_t metaclass meta-data |
| 6291 | uint32_t flags = CLS_META; |
| 6292 | std::string InstanceSize; |
| 6293 | std::string InstanceStart; |
| 6294 | |
| 6295 | |
| 6296 | bool classIsHidden = CDecl->getVisibility() == HiddenVisibility; |
| 6297 | if (classIsHidden) |
| 6298 | flags |= OBJC2_CLS_HIDDEN; |
| 6299 | |
| 6300 | if (!CDecl->getSuperClass()) |
| 6301 | // class is root |
| 6302 | flags |= CLS_ROOT; |
| 6303 | InstanceSize = "sizeof(struct _class_t)"; |
| 6304 | InstanceStart = InstanceSize; |
| 6305 | Write__class_ro_t_initializer(Context, Result, flags, |
| 6306 | InstanceStart, InstanceSize, |
| 6307 | ClassMethods, |
| 6308 | 0, |
| 6309 | 0, |
| 6310 | 0, |
| 6311 | "_OBJC_METACLASS_RO_$_", |
| 6312 | CDecl->getNameAsString()); |
| 6313 | |
| 6314 | |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6315 | // Data for initializing _class_ro_t meta-data |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 6316 | flags = CLS; |
| 6317 | if (classIsHidden) |
| 6318 | flags |= OBJC2_CLS_HIDDEN; |
| 6319 | |
| 6320 | if (hasObjCExceptionAttribute(*Context, CDecl)) |
| 6321 | flags |= CLS_EXCEPTION; |
| 6322 | |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6323 | if (!CDecl->getSuperClass()) |
| 6324 | // class is root |
| 6325 | flags |= CLS_ROOT; |
| 6326 | |
Fariborz Jahanian | 6ade343 | 2012-02-16 18:54:09 +0000 | [diff] [blame] | 6327 | InstanceSize.clear(); |
| 6328 | InstanceStart.clear(); |
Fariborz Jahanian | f1c1d9a | 2012-02-15 00:50:11 +0000 | [diff] [blame] | 6329 | if (!ObjCSynthesizedStructs.count(CDecl)) { |
| 6330 | InstanceSize = "0"; |
| 6331 | InstanceStart = "0"; |
| 6332 | } |
| 6333 | else { |
| 6334 | InstanceSize = "sizeof(struct "; |
| 6335 | InstanceSize += CDecl->getNameAsString(); |
| 6336 | InstanceSize += "_IMPL)"; |
| 6337 | |
| 6338 | ObjCIvarDecl *IVD = CDecl->all_declared_ivar_begin(); |
| 6339 | if (IVD) { |
| 6340 | InstanceStart += "__OFFSETOFIVAR__(struct "; |
| 6341 | InstanceStart += CDecl->getNameAsString(); |
| 6342 | InstanceStart += "_IMPL, "; |
| 6343 | InstanceStart += IVD->getNameAsString(); |
| 6344 | InstanceStart += ")"; |
| 6345 | } |
| 6346 | else |
| 6347 | InstanceStart = InstanceSize; |
| 6348 | } |
| 6349 | Write__class_ro_t_initializer(Context, Result, flags, |
| 6350 | InstanceStart, InstanceSize, |
| 6351 | InstanceMethods, |
| 6352 | RefedProtocols, |
| 6353 | IVars, |
| 6354 | ClassProperties, |
| 6355 | "_OBJC_CLASS_RO_$_", |
| 6356 | CDecl->getNameAsString()); |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6357 | |
| 6358 | Write_class_t(Context, Result, |
| 6359 | "OBJC_METACLASS_$_", |
| 6360 | CDecl, /*metaclass*/true); |
| 6361 | |
| 6362 | Write_class_t(Context, Result, |
| 6363 | "OBJC_CLASS_$_", |
| 6364 | CDecl, /*metaclass*/false); |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 6365 | |
| 6366 | if (ImplementationIsNonLazy(IDecl)) |
| 6367 | DefinedNonLazyClasses.push_back(CDecl); |
Fariborz Jahanian | 3f77c7b | 2012-02-16 21:37:05 +0000 | [diff] [blame] | 6368 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6369 | } |
| 6370 | |
Fariborz Jahanian | e033578 | 2012-03-27 18:41:05 +0000 | [diff] [blame^] | 6371 | void RewriteModernObjC::RewriteClassSetupInitHook(std::string &Result) { |
| 6372 | int ClsDefCount = ClassImplementation.size(); |
| 6373 | if (!ClsDefCount) |
| 6374 | return; |
| 6375 | Result += "#pragma section(\".objc_inithooks$B\", long, read, write)\n"; |
| 6376 | Result += "__declspec(allocate(\".objc_inithooks$B\")) "; |
| 6377 | Result += "static void *OBJC_CLASS_SETUP[] = {\n"; |
| 6378 | for (int i = 0; i < ClsDefCount; i++) { |
| 6379 | ObjCImplementationDecl *IDecl = ClassImplementation[i]; |
| 6380 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
| 6381 | Result += "\t(void *)&OBJC_CLASS_SETUP_$_"; |
| 6382 | Result += CDecl->getName(); Result += ",\n"; |
| 6383 | } |
| 6384 | Result += "};\n"; |
| 6385 | } |
| 6386 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6387 | void RewriteModernObjC::RewriteMetaDataIntoBuffer(std::string &Result) { |
| 6388 | int ClsDefCount = ClassImplementation.size(); |
| 6389 | int CatDefCount = CategoryImplementation.size(); |
| 6390 | |
| 6391 | // For each implemented class, write out all its meta data. |
| 6392 | for (int i = 0; i < ClsDefCount; i++) |
| 6393 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
| 6394 | |
Fariborz Jahanian | e033578 | 2012-03-27 18:41:05 +0000 | [diff] [blame^] | 6395 | RewriteClassSetupInitHook(Result); |
| 6396 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6397 | // For each implemented category, write out all its meta data. |
| 6398 | for (int i = 0; i < CatDefCount; i++) |
| 6399 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
| 6400 | |
Fariborz Jahanian | e033578 | 2012-03-27 18:41:05 +0000 | [diff] [blame^] | 6401 | RewriteCategorySetupInitHook(Result); |
| 6402 | |
Fariborz Jahanian | df79567 | 2012-02-17 00:06:14 +0000 | [diff] [blame] | 6403 | if (ClsDefCount > 0) { |
Fariborz Jahanian | 1ca052c | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 6404 | if (LangOpts.MicrosoftExt) |
| 6405 | Result += "__declspec(allocate(\".objc_classlist$B\")) "; |
Fariborz Jahanian | df79567 | 2012-02-17 00:06:14 +0000 | [diff] [blame] | 6406 | Result += "static struct _class_t *L_OBJC_LABEL_CLASS_$ ["; |
| 6407 | Result += llvm::utostr(ClsDefCount); Result += "]"; |
| 6408 | Result += |
| 6409 | " __attribute__((used, section (\"__DATA, __objc_classlist," |
| 6410 | "regular,no_dead_strip\")))= {\n"; |
| 6411 | for (int i = 0; i < ClsDefCount; i++) { |
| 6412 | Result += "\t&OBJC_CLASS_$_"; |
| 6413 | Result += ClassImplementation[i]->getNameAsString(); |
| 6414 | Result += ",\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6415 | } |
Fariborz Jahanian | df79567 | 2012-02-17 00:06:14 +0000 | [diff] [blame] | 6416 | Result += "};\n"; |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 6417 | |
| 6418 | if (!DefinedNonLazyClasses.empty()) { |
| 6419 | if (LangOpts.MicrosoftExt) |
| 6420 | Result += "__declspec(allocate(\".objc_nlclslist$B\")) \n"; |
| 6421 | Result += "static struct _class_t *_OBJC_LABEL_NONLAZY_CLASS_$[] = {\n\t"; |
| 6422 | for (unsigned i = 0, e = DefinedNonLazyClasses.size(); i < e; i++) { |
| 6423 | Result += "\t&OBJC_CLASS_$_"; Result += DefinedNonLazyClasses[i]->getNameAsString(); |
| 6424 | Result += ",\n"; |
| 6425 | } |
| 6426 | Result += "};\n"; |
| 6427 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6428 | } |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6429 | |
| 6430 | if (CatDefCount > 0) { |
Fariborz Jahanian | 1ca052c | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 6431 | if (LangOpts.MicrosoftExt) |
| 6432 | Result += "__declspec(allocate(\".objc_catlist$B\")) "; |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6433 | Result += "static struct _category_t *L_OBJC_LABEL_CATEGORY_$ ["; |
| 6434 | Result += llvm::utostr(CatDefCount); Result += "]"; |
| 6435 | Result += |
| 6436 | " __attribute__((used, section (\"__DATA, __objc_catlist," |
| 6437 | "regular,no_dead_strip\")))= {\n"; |
| 6438 | for (int i = 0; i < CatDefCount; i++) { |
| 6439 | Result += "\t&_OBJC_$_CATEGORY_"; |
| 6440 | Result += |
| 6441 | CategoryImplementation[i]->getClassInterface()->getNameAsString(); |
| 6442 | Result += "_$_"; |
| 6443 | Result += CategoryImplementation[i]->getNameAsString(); |
| 6444 | Result += ",\n"; |
| 6445 | } |
| 6446 | Result += "};\n"; |
| 6447 | } |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 6448 | |
| 6449 | if (!DefinedNonLazyCategories.empty()) { |
| 6450 | if (LangOpts.MicrosoftExt) |
| 6451 | Result += "__declspec(allocate(\".objc_nlcatlist$B\")) \n"; |
| 6452 | Result += "static struct _category_t *_OBJC_LABEL_NONLAZY_CATEGORY_$[] = {\n\t"; |
| 6453 | for (unsigned i = 0, e = DefinedNonLazyCategories.size(); i < e; i++) { |
| 6454 | Result += "\t&_OBJC_$_CATEGORY_"; |
| 6455 | Result += |
| 6456 | DefinedNonLazyCategories[i]->getClassInterface()->getNameAsString(); |
| 6457 | Result += "_$_"; |
| 6458 | Result += DefinedNonLazyCategories[i]->getNameAsString(); |
| 6459 | Result += ",\n"; |
| 6460 | } |
| 6461 | Result += "};\n"; |
| 6462 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6463 | } |
| 6464 | |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 6465 | void RewriteModernObjC::WriteImageInfo(std::string &Result) { |
| 6466 | if (LangOpts.MicrosoftExt) |
| 6467 | Result += "__declspec(allocate(\".objc_imageinfo$B\")) \n"; |
| 6468 | |
| 6469 | Result += "static struct IMAGE_INFO { unsigned version; unsigned flag; } "; |
| 6470 | // version 0, ObjCABI is 2 |
Fariborz Jahanian | 30650eb | 2012-03-15 17:05:33 +0000 | [diff] [blame] | 6471 | Result += "_OBJC_IMAGE_INFO = { 0, 2 };\n"; |
Fariborz Jahanian | 10cde2f | 2012-03-14 21:44:09 +0000 | [diff] [blame] | 6472 | } |
| 6473 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6474 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
| 6475 | /// implementation. |
| 6476 | void RewriteModernObjC::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
| 6477 | std::string &Result) { |
Fariborz Jahanian | de5d946 | 2012-03-14 18:09:23 +0000 | [diff] [blame] | 6478 | WriteModernMetadataDeclarations(Context, Result); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6479 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
| 6480 | // Find category declaration for this implementation. |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6481 | ObjCCategoryDecl *CDecl=0; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6482 | for (CDecl = ClassDecl->getCategoryList(); CDecl; |
| 6483 | CDecl = CDecl->getNextClassCategory()) |
| 6484 | if (CDecl->getIdentifier() == IDecl->getIdentifier()) |
| 6485 | break; |
| 6486 | |
| 6487 | std::string FullCategoryName = ClassDecl->getNameAsString(); |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6488 | FullCategoryName += "_$_"; |
| 6489 | FullCategoryName += CDecl->getNameAsString(); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6490 | |
| 6491 | // Build _objc_method_list for class's instance methods if needed |
| 6492 | SmallVector<ObjCMethodDecl *, 32> |
| 6493 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
| 6494 | |
| 6495 | // If any of our property implementations have associated getters or |
| 6496 | // setters, produce metadata for them as well. |
| 6497 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 6498 | PropEnd = IDecl->propimpl_end(); |
| 6499 | Prop != PropEnd; ++Prop) { |
| 6500 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 6501 | continue; |
| 6502 | if (!(*Prop)->getPropertyIvarDecl()) |
| 6503 | continue; |
| 6504 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 6505 | if (!PD) |
| 6506 | continue; |
| 6507 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 6508 | InstanceMethods.push_back(Getter); |
| 6509 | if (PD->isReadOnly()) |
| 6510 | continue; |
| 6511 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 6512 | InstanceMethods.push_back(Setter); |
| 6513 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6514 | |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6515 | Write_method_list_t_initializer(*this, Context, Result, InstanceMethods, |
| 6516 | "_OBJC_$_CATEGORY_INSTANCE_METHODS_", |
| 6517 | FullCategoryName, true); |
| 6518 | |
| 6519 | SmallVector<ObjCMethodDecl *, 32> |
| 6520 | ClassMethods(IDecl->classmeth_begin(), IDecl->classmeth_end()); |
| 6521 | |
| 6522 | Write_method_list_t_initializer(*this, Context, Result, ClassMethods, |
| 6523 | "_OBJC_$_CATEGORY_CLASS_METHODS_", |
| 6524 | FullCategoryName, true); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6525 | |
| 6526 | // Protocols referenced in class declaration? |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6527 | // Protocol's super protocol list |
| 6528 | std::vector<ObjCProtocolDecl *> RefedProtocols; |
Argyrios Kyrtzidis | a5f4441 | 2012-03-13 01:09:41 +0000 | [diff] [blame] | 6529 | for (ObjCInterfaceDecl::protocol_iterator I = CDecl->protocol_begin(), |
| 6530 | E = CDecl->protocol_end(); |
| 6531 | |
| 6532 | I != E; ++I) { |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6533 | RefedProtocols.push_back(*I); |
| 6534 | // Must write out all protocol definitions in current qualifier list, |
| 6535 | // and in their nested qualifiers before writing out current definition. |
| 6536 | RewriteObjCProtocolMetaData(*I, Result); |
| 6537 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6538 | |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6539 | Write_protocol_list_initializer(Context, Result, |
| 6540 | RefedProtocols, |
| 6541 | "_OBJC_CATEGORY_PROTOCOLS_$_", |
| 6542 | FullCategoryName); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6543 | |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6544 | // Protocol's property metadata. |
| 6545 | std::vector<ObjCPropertyDecl *> ClassProperties; |
| 6546 | for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(), |
| 6547 | E = CDecl->prop_end(); I != E; ++I) |
| 6548 | ClassProperties.push_back(*I); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6549 | |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6550 | Write_prop_list_t_initializer(*this, Context, Result, ClassProperties, |
| 6551 | /* Container */0, |
| 6552 | "_OBJC_$_PROP_LIST_", |
| 6553 | FullCategoryName); |
| 6554 | |
| 6555 | Write_category_t(*this, Context, Result, |
Fariborz Jahanian | e033578 | 2012-03-27 18:41:05 +0000 | [diff] [blame^] | 6556 | CDecl, |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 6557 | ClassDecl, |
Fariborz Jahanian | 6118612 | 2012-02-17 18:40:41 +0000 | [diff] [blame] | 6558 | InstanceMethods, |
| 6559 | ClassMethods, |
| 6560 | RefedProtocols, |
| 6561 | ClassProperties); |
| 6562 | |
Fariborz Jahanian | 88f7f75 | 2012-03-14 23:18:19 +0000 | [diff] [blame] | 6563 | // Determine if this category is also "non-lazy". |
| 6564 | if (ImplementationIsNonLazy(IDecl)) |
| 6565 | DefinedNonLazyCategories.push_back(CDecl); |
Fariborz Jahanian | e033578 | 2012-03-27 18:41:05 +0000 | [diff] [blame^] | 6566 | |
| 6567 | } |
| 6568 | |
| 6569 | void RewriteModernObjC::RewriteCategorySetupInitHook(std::string &Result) { |
| 6570 | int CatDefCount = CategoryImplementation.size(); |
| 6571 | if (!CatDefCount) |
| 6572 | return; |
| 6573 | Result += "#pragma section(\".objc_inithooks$B\", long, read, write)\n"; |
| 6574 | Result += "__declspec(allocate(\".objc_inithooks$B\")) "; |
| 6575 | Result += "static void *OBJC_CATEGORY_SETUP[] = {\n"; |
| 6576 | for (int i = 0; i < CatDefCount; i++) { |
| 6577 | ObjCCategoryImplDecl *IDecl = CategoryImplementation[i]; |
| 6578 | ObjCCategoryDecl *CatDecl= IDecl->getCategoryDecl(); |
| 6579 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
| 6580 | Result += "\t(void *)&OBJC_CATEGORY_SETUP_$_"; |
| 6581 | Result += ClassDecl->getName(); |
| 6582 | Result += "_$_"; |
| 6583 | Result += CatDecl->getName(); |
| 6584 | Result += ",\n"; |
| 6585 | } |
| 6586 | Result += "};\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6587 | } |
| 6588 | |
| 6589 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
| 6590 | /// class methods. |
| 6591 | template<typename MethodIterator> |
| 6592 | void RewriteModernObjC::RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 6593 | MethodIterator MethodEnd, |
| 6594 | bool IsInstanceMethod, |
| 6595 | StringRef prefix, |
| 6596 | StringRef ClassName, |
| 6597 | std::string &Result) { |
| 6598 | if (MethodBegin == MethodEnd) return; |
| 6599 | |
| 6600 | if (!objc_impl_method) { |
| 6601 | /* struct _objc_method { |
| 6602 | SEL _cmd; |
| 6603 | char *method_types; |
| 6604 | void *_imp; |
| 6605 | } |
| 6606 | */ |
| 6607 | Result += "\nstruct _objc_method {\n"; |
| 6608 | Result += "\tSEL _cmd;\n"; |
| 6609 | Result += "\tchar *method_types;\n"; |
| 6610 | Result += "\tvoid *_imp;\n"; |
| 6611 | Result += "};\n"; |
| 6612 | |
| 6613 | objc_impl_method = true; |
| 6614 | } |
| 6615 | |
| 6616 | // Build _objc_method_list for class's methods if needed |
| 6617 | |
| 6618 | /* struct { |
| 6619 | struct _objc_method_list *next_method; |
| 6620 | int method_count; |
| 6621 | struct _objc_method method_list[]; |
| 6622 | } |
| 6623 | */ |
| 6624 | unsigned NumMethods = std::distance(MethodBegin, MethodEnd); |
Fariborz Jahanian | 1ca052c | 2012-03-11 19:41:56 +0000 | [diff] [blame] | 6625 | Result += "\n"; |
| 6626 | if (LangOpts.MicrosoftExt) { |
| 6627 | if (IsInstanceMethod) |
| 6628 | Result += "__declspec(allocate(\".inst_meth$B\")) "; |
| 6629 | else |
| 6630 | Result += "__declspec(allocate(\".cls_meth$B\")) "; |
| 6631 | } |
| 6632 | Result += "static struct {\n"; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6633 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 6634 | Result += "\tint method_count;\n"; |
| 6635 | Result += "\tstruct _objc_method method_list["; |
| 6636 | Result += utostr(NumMethods); |
| 6637 | Result += "];\n} _OBJC_"; |
| 6638 | Result += prefix; |
| 6639 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 6640 | Result += "_METHODS_"; |
| 6641 | Result += ClassName; |
| 6642 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
| 6643 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 6644 | Result += "_meth\")))= "; |
| 6645 | Result += "{\n\t0, " + utostr(NumMethods) + "\n"; |
| 6646 | |
| 6647 | Result += "\t,{{(SEL)\""; |
| 6648 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
| 6649 | std::string MethodTypeString; |
| 6650 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
| 6651 | Result += "\", \""; |
| 6652 | Result += MethodTypeString; |
| 6653 | Result += "\", (void *)"; |
| 6654 | Result += MethodInternalNames[*MethodBegin]; |
| 6655 | Result += "}\n"; |
| 6656 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 6657 | Result += "\t ,{(SEL)\""; |
| 6658 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
| 6659 | std::string MethodTypeString; |
| 6660 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
| 6661 | Result += "\", \""; |
| 6662 | Result += MethodTypeString; |
| 6663 | Result += "\", (void *)"; |
| 6664 | Result += MethodInternalNames[*MethodBegin]; |
| 6665 | Result += "}\n"; |
| 6666 | } |
| 6667 | Result += "\t }\n};\n"; |
| 6668 | } |
| 6669 | |
| 6670 | Stmt *RewriteModernObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV) { |
| 6671 | SourceRange OldRange = IV->getSourceRange(); |
| 6672 | Expr *BaseExpr = IV->getBase(); |
| 6673 | |
| 6674 | // Rewrite the base, but without actually doing replaces. |
| 6675 | { |
| 6676 | DisableReplaceStmtScope S(*this); |
| 6677 | BaseExpr = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(BaseExpr)); |
| 6678 | IV->setBase(BaseExpr); |
| 6679 | } |
| 6680 | |
| 6681 | ObjCIvarDecl *D = IV->getDecl(); |
| 6682 | |
| 6683 | Expr *Replacement = IV; |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 6684 | |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6685 | if (BaseExpr->getType()->isObjCObjectPointerType()) { |
| 6686 | const ObjCInterfaceType *iFaceDecl = |
| 6687 | dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); |
| 6688 | assert(iFaceDecl && "RewriteObjCIvarRefExpr - iFaceDecl is null"); |
| 6689 | // lookup which class implements the instance variable. |
| 6690 | ObjCInterfaceDecl *clsDeclared = 0; |
| 6691 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
| 6692 | clsDeclared); |
| 6693 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
| 6694 | |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 6695 | // Build name of symbol holding ivar offset. |
Fariborz Jahanian | 7cb2a1b | 2012-03-20 17:13:39 +0000 | [diff] [blame] | 6696 | std::string IvarOffsetName; |
| 6697 | WriteInternalIvarName(clsDeclared, D, IvarOffsetName); |
| 6698 | |
Fariborz Jahanian | 72c88f1 | 2012-02-22 18:13:25 +0000 | [diff] [blame] | 6699 | ReferencedIvars[clsDeclared].insert(D); |
| 6700 | |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 6701 | // cast offset to "char *". |
| 6702 | CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, |
| 6703 | Context->getPointerType(Context->CharTy), |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6704 | CK_BitCast, |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 6705 | BaseExpr); |
| 6706 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
| 6707 | SourceLocation(), &Context->Idents.get(IvarOffsetName), |
| 6708 | Context->UnsignedLongTy, 0, SC_Extern, SC_None); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 6709 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, false, |
| 6710 | Context->UnsignedLongTy, VK_LValue, |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 6711 | SourceLocation()); |
| 6712 | BinaryOperator *addExpr = |
| 6713 | new (Context) BinaryOperator(castExpr, DRE, BO_Add, |
| 6714 | Context->getPointerType(Context->CharTy), |
| 6715 | VK_RValue, OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6716 | // Don't forget the parens to enforce the proper binding. |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 6717 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), |
| 6718 | SourceLocation(), |
| 6719 | addExpr); |
Fariborz Jahanian | 0d6e22a | 2012-02-24 17:35:35 +0000 | [diff] [blame] | 6720 | QualType IvarT = D->getType(); |
Fariborz Jahanian | 8e0913d | 2012-02-29 00:26:20 +0000 | [diff] [blame] | 6721 | convertObjCTypeToCStyleType(IvarT); |
Fariborz Jahanian | 0d6e22a | 2012-02-24 17:35:35 +0000 | [diff] [blame] | 6722 | QualType castT = Context->getPointerType(IvarT); |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6723 | |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 6724 | castExpr = NoTypeInfoCStyleCastExpr(Context, |
| 6725 | castT, |
| 6726 | CK_BitCast, |
| 6727 | PE); |
Fariborz Jahanian | 8e0913d | 2012-02-29 00:26:20 +0000 | [diff] [blame] | 6728 | Expr *Exp = new (Context) UnaryOperator(castExpr, UO_Deref, IvarT, |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 6729 | VK_LValue, OK_Ordinary, |
| 6730 | SourceLocation()); |
| 6731 | PE = new (Context) ParenExpr(OldRange.getBegin(), |
| 6732 | OldRange.getEnd(), |
| 6733 | Exp); |
| 6734 | |
| 6735 | Replacement = PE; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6736 | } |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6737 | |
Fariborz Jahanian | e7b3fa7 | 2012-02-21 23:46:48 +0000 | [diff] [blame] | 6738 | ReplaceStmtWithRange(IV, Replacement, OldRange); |
| 6739 | return Replacement; |
Fariborz Jahanian | 64cb63a | 2012-02-07 17:11:38 +0000 | [diff] [blame] | 6740 | } |