1 //===--- RewriteObjC.cpp - Playground for the code rewriter ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Hacks and fun related to the code rewriter.
12 //===----------------------------------------------------------------------===//
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"
30 using namespace clang;
34 class RewriteObjC : public ASTConsumer {
38 BLOCK_FIELD_IS_OBJECT = 3, /* id, NSObject, __attribute__((NSObject)),
40 BLOCK_FIELD_IS_BLOCK = 7, /* a block variable */
41 BLOCK_FIELD_IS_BYREF = 8, /* the on stack structure holding the
43 BLOCK_FIELD_IS_WEAK = 16, /* declared __weak, only used in byref copy
45 BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose
47 BLOCK_BYREF_CURRENT_MAX = 256
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)
58 static const int OBJC_ABI_VERSION = 7;
61 DiagnosticsEngine &Diags;
62 const LangOptions &LangOpts;
65 TranslationUnitDecl *TUDecl;
67 const char *MainFileStart, *MainFileEnd;
69 ParentMap *PropParentMap; // created lazily.
70 std::string InFileName;
74 TypeDecl *ProtocolTypeDecl;
75 VarDecl *GlobalVarDecl;
76 unsigned RewriteFailedDiag;
77 // ObjC string constant support.
78 unsigned NumObjCStringLiterals;
79 VarDecl *ConstantStringClassReference;
80 RecordDecl *NSStringRecord;
82 // ObjC foreach break/continue generation support.
85 unsigned TryFinallyContainsReturnDiag;
87 ObjCMethodDecl *CurMethodDef;
88 RecordDecl *SuperStructDecl;
89 RecordDecl *ConstantStringDecl;
91 FunctionDecl *MsgSendFunctionDecl;
92 FunctionDecl *MsgSendSuperFunctionDecl;
93 FunctionDecl *MsgSendStretFunctionDecl;
94 FunctionDecl *MsgSendSuperStretFunctionDecl;
95 FunctionDecl *MsgSendFpretFunctionDecl;
96 FunctionDecl *GetClassFunctionDecl;
97 FunctionDecl *GetMetaClassFunctionDecl;
98 FunctionDecl *GetSuperClassFunctionDecl;
99 FunctionDecl *SelGetUidFunctionDecl;
100 FunctionDecl *CFStringFunctionDecl;
101 FunctionDecl *SuperContructorFunctionDecl;
102 FunctionDecl *CurFunctionDef;
103 FunctionDecl *CurFunctionDeclToDeclareForBlock;
105 /* Misc. containers needed for meta-data rewrite. */
106 SmallVector<ObjCImplementationDecl *, 8> ClassImplementation;
107 SmallVector<ObjCCategoryImplDecl *, 8> CategoryImplementation;
108 llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCSynthesizedStructs;
109 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> ObjCSynthesizedProtocols;
110 llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCForwardDecls;
111 llvm::DenseMap<ObjCMethodDecl*, std::string> MethodInternalNames;
112 SmallVector<Stmt *, 32> Stmts;
113 SmallVector<int, 8> ObjCBcLabelNo;
114 // Remember all the @protocol(<expr>) expressions.
115 llvm::SmallPtrSet<ObjCProtocolDecl *, 32> ProtocolExprDecls;
117 llvm::DenseSet<uint64_t> CopyDestroyCache;
119 // Block expressions.
120 SmallVector<BlockExpr *, 32> Blocks;
121 SmallVector<int, 32> InnerDeclRefsCount;
122 SmallVector<DeclRefExpr *, 32> InnerDeclRefs;
124 SmallVector<DeclRefExpr *, 32> BlockDeclRefs;
126 // Block related declarations.
127 SmallVector<ValueDecl *, 8> BlockByCopyDecls;
128 llvm::SmallPtrSet<ValueDecl *, 8> BlockByCopyDeclsPtrSet;
129 SmallVector<ValueDecl *, 8> BlockByRefDecls;
130 llvm::SmallPtrSet<ValueDecl *, 8> BlockByRefDeclsPtrSet;
131 llvm::DenseMap<ValueDecl *, unsigned> BlockByRefDeclNo;
132 llvm::SmallPtrSet<ValueDecl *, 8> ImportedBlockDecls;
133 llvm::SmallPtrSet<VarDecl *, 8> ImportedLocalExternalDecls;
135 llvm::DenseMap<BlockExpr *, std::string> RewrittenBlockExprs;
137 // This maps an original source AST to it's rewritten form. This allows
138 // us to avoid rewriting the same node twice (which is very uncommon).
139 // This is needed to support some of the exotic property rewriting.
140 llvm::DenseMap<Stmt *, Stmt *> ReplacedNodes;
142 // Needed for header files being rewritten
144 bool SilenceRewriteMacroWarning;
145 bool objc_impl_method;
147 bool DisableReplaceStmt;
148 class DisableReplaceStmtScope {
153 DisableReplaceStmtScope(RewriteObjC &R)
154 : R(R), SavedValue(R.DisableReplaceStmt) {
155 R.DisableReplaceStmt = true;
157 ~DisableReplaceStmtScope() {
158 R.DisableReplaceStmt = SavedValue;
161 void InitializeCommon(ASTContext &context);
165 // Top Level Driver code.
166 virtual bool HandleTopLevelDecl(DeclGroupRef D) {
167 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) {
168 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(*I)) {
169 if (!Class->isThisDeclarationADefinition()) {
170 RewriteForwardClassDecl(D);
175 if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(*I)) {
176 if (!Proto->isThisDeclarationADefinition()) {
177 RewriteForwardProtocolDecl(D);
182 HandleTopLevelSingleDecl(*I);
186 void HandleTopLevelSingleDecl(Decl *D);
187 void HandleDeclInMainFile(Decl *D);
188 RewriteObjC(std::string inFile, raw_ostream *OS,
189 DiagnosticsEngine &D, const LangOptions &LOpts,
190 bool silenceMacroWarn);
194 virtual void HandleTranslationUnit(ASTContext &C);
196 void ReplaceStmt(Stmt *Old, Stmt *New) {
197 Stmt *ReplacingStmt = ReplacedNodes[Old];
200 return; // We can't rewrite the same node twice.
202 if (DisableReplaceStmt)
205 // If replacement succeeded or warning disabled return with no warning.
206 if (!Rewrite.ReplaceStmt(Old, New)) {
207 ReplacedNodes[Old] = New;
210 if (SilenceRewriteMacroWarning)
212 Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag)
213 << Old->getSourceRange();
216 void ReplaceStmtWithRange(Stmt *Old, Stmt *New, SourceRange SrcRange) {
217 if (DisableReplaceStmt)
220 // Measure the old text.
221 int Size = Rewrite.getRangeSize(SrcRange);
223 Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag)
224 << Old->getSourceRange();
229 llvm::raw_string_ostream S(SStr);
230 New->printPretty(S, *Context, 0, PrintingPolicy(LangOpts));
231 const std::string &Str = S.str();
233 // If replacement succeeded or warning disabled return with no warning.
234 if (!Rewrite.ReplaceText(SrcRange.getBegin(), Size, Str)) {
235 ReplacedNodes[Old] = New;
238 if (SilenceRewriteMacroWarning)
240 Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag)
241 << Old->getSourceRange();
244 void InsertText(SourceLocation Loc, StringRef Str,
245 bool InsertAfter = true) {
246 // If insertion succeeded or warning disabled return with no warning.
247 if (!Rewrite.InsertText(Loc, Str, InsertAfter) ||
248 SilenceRewriteMacroWarning)
251 Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag);
254 void ReplaceText(SourceLocation Start, unsigned OrigLength,
256 // If removal succeeded or warning disabled return with no warning.
257 if (!Rewrite.ReplaceText(Start, OrigLength, Str) ||
258 SilenceRewriteMacroWarning)
261 Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag);
264 // Syntactic Rewriting.
265 void RewriteRecordBody(RecordDecl *RD);
266 void RewriteInclude();
267 void RewriteForwardClassDecl(DeclGroupRef D);
268 void RewriteForwardClassDecl(const llvm::SmallVector<Decl*, 8> &DG);
269 void RewriteForwardClassEpilogue(ObjCInterfaceDecl *ClassDecl,
270 const std::string &typedefString);
271 void RewriteImplementations();
272 void RewritePropertyImplDecl(ObjCPropertyImplDecl *PID,
273 ObjCImplementationDecl *IMD,
274 ObjCCategoryImplDecl *CID);
275 void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl);
276 void RewriteImplementationDecl(Decl *Dcl);
277 void RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl,
278 ObjCMethodDecl *MDecl, std::string &ResultStr);
279 void RewriteTypeIntoString(QualType T, std::string &ResultStr,
280 const FunctionType *&FPRetType);
281 void RewriteByRefString(std::string &ResultStr, const std::string &Name,
282 ValueDecl *VD, bool def=false);
283 void RewriteCategoryDecl(ObjCCategoryDecl *Dcl);
284 void RewriteProtocolDecl(ObjCProtocolDecl *Dcl);
285 void RewriteForwardProtocolDecl(DeclGroupRef D);
286 void RewriteForwardProtocolDecl(const llvm::SmallVector<Decl*, 8> &DG);
287 void RewriteMethodDeclaration(ObjCMethodDecl *Method);
288 void RewriteProperty(ObjCPropertyDecl *prop);
289 void RewriteFunctionDecl(FunctionDecl *FD);
290 void RewriteBlockPointerType(std::string& Str, QualType Type);
291 void RewriteBlockPointerTypeVariable(std::string& Str, ValueDecl *VD);
292 void RewriteBlockLiteralFunctionDecl(FunctionDecl *FD);
293 void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl);
294 void RewriteTypeOfDecl(VarDecl *VD);
295 void RewriteObjCQualifiedInterfaceTypes(Expr *E);
297 // Expression Rewriting.
298 Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S);
299 Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp);
300 Stmt *RewritePropertyOrImplicitGetter(PseudoObjectExpr *Pseudo);
301 Stmt *RewritePropertyOrImplicitSetter(PseudoObjectExpr *Pseudo);
302 Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp);
303 Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp);
304 Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp);
305 Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp);
306 void RewriteTryReturnStmts(Stmt *S);
307 void RewriteSyncReturnStmts(Stmt *S, std::string buf);
308 Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S);
309 Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S);
310 Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S);
311 Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S,
312 SourceLocation OrigEnd);
313 Stmt *RewriteBreakStmt(BreakStmt *S);
314 Stmt *RewriteContinueStmt(ContinueStmt *S);
315 void RewriteCastExpr(CStyleCastExpr *CE);
318 void RewriteBlocksInFunctionProtoType(QualType funcType, NamedDecl *D);
320 // Block specific rewrite rules.
321 void RewriteBlockPointerDecl(NamedDecl *VD);
322 void RewriteByRefVar(VarDecl *VD);
323 Stmt *RewriteBlockDeclRefExpr(DeclRefExpr *VD);
324 Stmt *RewriteLocalVariableExternalStorage(DeclRefExpr *DRE);
325 void RewriteBlockPointerFunctionArgs(FunctionDecl *FD);
327 void RewriteObjCInternalStruct(ObjCInterfaceDecl *CDecl,
328 std::string &Result);
330 virtual void Initialize(ASTContext &context) = 0;
332 // Metadata Rewriting.
333 virtual void RewriteMetaDataIntoBuffer(std::string &Result) = 0;
334 virtual void RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Prots,
337 std::string &Result) = 0;
338 virtual void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl,
339 std::string &Result) = 0;
340 virtual void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol,
343 std::string &Result) = 0;
344 virtual void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl,
345 std::string &Result) = 0;
347 // Rewriting ivar access
348 virtual Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV) = 0;
349 virtual void RewriteIvarOffsetComputation(ObjCIvarDecl *ivar,
350 std::string &Result) = 0;
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());
359 Stmt *SynthMessageExpr(ObjCMessageExpr *Exp,
360 SourceLocation StartLoc=SourceLocation(),
361 SourceLocation EndLoc=SourceLocation());
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();
375 std::string SynthesizeByrefCopyDestroyHelper(VarDecl *VD, int flag);
376 std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i,
377 StringRef funcName, std::string Tag);
378 std::string SynthesizeBlockFunc(BlockExpr *CE, int i,
379 StringRef funcName, std::string Tag);
380 std::string SynthesizeBlockImpl(BlockExpr *CE,
381 std::string Tag, std::string Desc);
382 std::string SynthesizeBlockDescriptor(std::string DescTag,
384 int i, StringRef funcName,
386 Stmt *SynthesizeBlockCall(CallExpr *Exp, const Expr* BlockExp);
387 void SynthesizeBlockLiterals(SourceLocation FunLocStart,
389 FunctionDecl *SynthBlockInitFunctionDecl(StringRef name);
390 Stmt *SynthBlockInitExpr(BlockExpr *Exp,
391 const SmallVector<DeclRefExpr *, 8> &InnerBlockDeclRefs);
393 // Misc. helper routines.
394 QualType getProtocolType();
395 void WarnAboutReturnGotoStmts(Stmt *S);
396 void HasReturnStmts(Stmt *S, bool &hasReturns);
397 void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND);
398 void InsertBlockLiteralsWithinFunction(FunctionDecl *FD);
399 void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD);
401 bool IsDeclStmtInForeachHeader(DeclStmt *DS);
402 void CollectBlockDeclRefInfo(BlockExpr *Exp);
403 void GetBlockDeclRefExprs(Stmt *S);
404 void GetInnerBlockDeclRefExprs(Stmt *S,
405 SmallVector<DeclRefExpr *, 8> &InnerBlockDeclRefs,
406 llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts);
408 // We avoid calling Type::isBlockPointerType(), since it operates on the
409 // canonical type. We only care if the top-level type is a closure pointer.
410 bool isTopLevelBlockPointerType(QualType T) {
411 return isa<BlockPointerType>(T);
414 /// convertBlockPointerToFunctionPointer - Converts a block-pointer type
415 /// to a function pointer type and upon success, returns true; false
417 bool convertBlockPointerToFunctionPointer(QualType &T) {
418 if (isTopLevelBlockPointerType(T)) {
419 const BlockPointerType *BPT = T->getAs<BlockPointerType>();
420 T = Context->getPointerType(BPT->getPointeeType());
426 bool needToScanForQualifiers(QualType T);
427 QualType getSuperStructType();
428 QualType getConstantStringStructType();
429 QualType convertFunctionTypeOfBlocks(const FunctionType *FT);
430 bool BufferContainsPPDirectives(const char *startBuf, const char *endBuf);
432 void convertToUnqualifiedObjCType(QualType &T) {
433 if (T->isObjCQualifiedIdType())
434 T = Context->getObjCIdType();
435 else if (T->isObjCQualifiedClassType())
436 T = Context->getObjCClassType();
437 else if (T->isObjCObjectPointerType() &&
438 T->getPointeeType()->isObjCQualifiedInterfaceType()) {
439 if (const ObjCObjectPointerType * OBJPT =
440 T->getAsObjCInterfacePointerType()) {
441 const ObjCInterfaceType *IFaceT = OBJPT->getInterfaceType();
442 T = QualType(IFaceT, 0);
443 T = Context->getPointerType(T);
448 // FIXME: This predicate seems like it would be useful to add to ASTContext.
449 bool isObjCType(QualType T) {
450 if (!LangOpts.ObjC1 && !LangOpts.ObjC2)
453 QualType OCT = Context->getCanonicalType(T).getUnqualifiedType();
455 if (OCT == Context->getCanonicalType(Context->getObjCIdType()) ||
456 OCT == Context->getCanonicalType(Context->getObjCClassType()))
459 if (const PointerType *PT = OCT->getAs<PointerType>()) {
460 if (isa<ObjCInterfaceType>(PT->getPointeeType()) ||
461 PT->getPointeeType()->isObjCQualifiedIdType())
466 bool PointerTypeTakesAnyBlockArguments(QualType QT);
467 bool PointerTypeTakesAnyObjCQualifiedType(QualType QT);
468 void GetExtentOfArgList(const char *Name, const char *&LParen,
469 const char *&RParen);
471 void QuoteDoublequotes(std::string &From, std::string &To) {
472 for (unsigned i = 0; i < From.length(); i++) {
480 QualType getSimpleFunctionType(QualType result,
481 const QualType *args,
483 bool variadic = false) {
484 if (result == Context->getObjCInstanceType())
485 result = Context->getObjCIdType();
486 FunctionProtoType::ExtProtoInfo fpi;
487 fpi.Variadic = variadic;
488 return Context->getFunctionType(result, args, numArgs, fpi);
491 // Helper function: create a CStyleCastExpr with trivial type source info.
492 CStyleCastExpr* NoTypeInfoCStyleCastExpr(ASTContext *Ctx, QualType Ty,
493 CastKind Kind, Expr *E) {
494 TypeSourceInfo *TInfo = Ctx->getTrivialTypeSourceInfo(Ty, SourceLocation());
495 return CStyleCastExpr::Create(*Ctx, Ty, VK_RValue, Kind, E, 0, TInfo,
496 SourceLocation(), SourceLocation());
500 class RewriteObjCFragileABI : public RewriteObjC {
503 RewriteObjCFragileABI(std::string inFile, raw_ostream *OS,
504 DiagnosticsEngine &D, const LangOptions &LOpts,
505 bool silenceMacroWarn) : RewriteObjC(inFile, OS,
509 ~RewriteObjCFragileABI() {}
510 virtual void Initialize(ASTContext &context);
512 // Rewriting metadata
513 template<typename MethodIterator>
514 void RewriteObjCMethodsMetaData(MethodIterator MethodBegin,
515 MethodIterator MethodEnd,
516 bool IsInstanceMethod,
519 std::string &Result);
520 virtual void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol,
523 std::string &Result);
524 virtual void RewriteObjCProtocolListMetaData(
525 const ObjCList<ObjCProtocolDecl> &Prots,
526 StringRef prefix, StringRef ClassName, std::string &Result);
527 virtual void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl,
528 std::string &Result);
529 virtual void RewriteMetaDataIntoBuffer(std::string &Result);
530 virtual void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl,
531 std::string &Result);
534 virtual void RewriteIvarOffsetComputation(ObjCIvarDecl *ivar,
535 std::string &Result);
536 virtual Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV);
540 void RewriteObjC::RewriteBlocksInFunctionProtoType(QualType funcType,
542 if (const FunctionProtoType *fproto
543 = dyn_cast<FunctionProtoType>(funcType.IgnoreParens())) {
544 for (FunctionProtoType::arg_type_iterator I = fproto->arg_type_begin(),
545 E = fproto->arg_type_end(); I && (I != E); ++I)
546 if (isTopLevelBlockPointerType(*I)) {
547 // All the args are checked/rewritten. Don't call twice!
548 RewriteBlockPointerDecl(D);
554 void RewriteObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) {
555 const PointerType *PT = funcType->getAs<PointerType>();
556 if (PT && PointerTypeTakesAnyBlockArguments(funcType))
557 RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND);
560 static bool IsHeaderFile(const std::string &Filename) {
561 std::string::size_type DotPos = Filename.rfind('.');
563 if (DotPos == std::string::npos) {
568 std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end());
570 // C++ header: .hh or .H;
571 return Ext == "h" || Ext == "hh" || Ext == "H";
574 RewriteObjC::RewriteObjC(std::string inFile, raw_ostream* OS,
575 DiagnosticsEngine &D, const LangOptions &LOpts,
576 bool silenceMacroWarn)
577 : Diags(D), LangOpts(LOpts), InFileName(inFile), OutFile(OS),
578 SilenceRewriteMacroWarning(silenceMacroWarn) {
579 IsHeader = IsHeaderFile(inFile);
580 RewriteFailedDiag = Diags.getCustomDiagID(DiagnosticsEngine::Warning,
581 "rewriting sub-expression within a macro (may not be correct)");
582 TryFinallyContainsReturnDiag = Diags.getCustomDiagID(
583 DiagnosticsEngine::Warning,
584 "rewriter doesn't support user-specified control flow semantics "
585 "for @try/@finally (code may not execute properly)");
588 ASTConsumer *clang::CreateObjCRewriter(const std::string& InFile,
590 DiagnosticsEngine &Diags,
591 const LangOptions &LOpts,
592 bool SilenceRewriteMacroWarning) {
593 return new RewriteObjCFragileABI(InFile, OS, Diags, LOpts, SilenceRewriteMacroWarning);
596 void RewriteObjC::InitializeCommon(ASTContext &context) {
598 SM = &Context->getSourceManager();
599 TUDecl = Context->getTranslationUnitDecl();
600 MsgSendFunctionDecl = 0;
601 MsgSendSuperFunctionDecl = 0;
602 MsgSendStretFunctionDecl = 0;
603 MsgSendSuperStretFunctionDecl = 0;
604 MsgSendFpretFunctionDecl = 0;
605 GetClassFunctionDecl = 0;
606 GetMetaClassFunctionDecl = 0;
607 GetSuperClassFunctionDecl = 0;
608 SelGetUidFunctionDecl = 0;
609 CFStringFunctionDecl = 0;
610 ConstantStringClassReference = 0;
614 CurFunctionDeclToDeclareForBlock = 0;
617 ProtocolTypeDecl = 0;
618 ConstantStringDecl = 0;
620 SuperContructorFunctionDecl = 0;
621 NumObjCStringLiterals = 0;
624 DisableReplaceStmt = false;
625 objc_impl_method = false;
627 // Get the ID and start/end of the main file.
628 MainFileID = SM->getMainFileID();
629 const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID);
630 MainFileStart = MainBuf->getBufferStart();
631 MainFileEnd = MainBuf->getBufferEnd();
633 Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOpts());
636 //===----------------------------------------------------------------------===//
637 // Top Level Driver Code
638 //===----------------------------------------------------------------------===//
640 void RewriteObjC::HandleTopLevelSingleDecl(Decl *D) {
641 if (Diags.hasErrorOccurred())
644 // Two cases: either the decl could be in the main file, or it could be in a
645 // #included file. If the former, rewrite it now. If the later, check to see
646 // if we rewrote the #include/#import.
647 SourceLocation Loc = D->getLocation();
648 Loc = SM->getExpansionLoc(Loc);
650 // If this is for a builtin, ignore it.
651 if (Loc.isInvalid()) return;
653 // Look for built-in declarations that we need to refer during the rewrite.
654 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
655 RewriteFunctionDecl(FD);
656 } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) {
657 // declared in <Foundation/NSString.h>
658 if (FVD->getName() == "_NSConstantStringClassReference") {
659 ConstantStringClassReference = FVD;
662 } else if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
663 if (ID->isThisDeclarationADefinition())
664 RewriteInterfaceDecl(ID);
665 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) {
666 RewriteCategoryDecl(CD);
667 } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
668 if (PD->isThisDeclarationADefinition())
669 RewriteProtocolDecl(PD);
670 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
671 // Recurse into linkage specifications
672 for (DeclContext::decl_iterator DI = LSD->decls_begin(),
673 DIEnd = LSD->decls_end();
675 if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>((*DI))) {
676 if (!IFace->isThisDeclarationADefinition()) {
677 SmallVector<Decl *, 8> DG;
678 SourceLocation StartLoc = IFace->getLocStart();
680 if (isa<ObjCInterfaceDecl>(*DI) &&
681 !cast<ObjCInterfaceDecl>(*DI)->isThisDeclarationADefinition() &&
682 StartLoc == (*DI)->getLocStart())
688 } while (DI != DIEnd);
689 RewriteForwardClassDecl(DG);
694 if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>((*DI))) {
695 if (!Proto->isThisDeclarationADefinition()) {
696 SmallVector<Decl *, 8> DG;
697 SourceLocation StartLoc = Proto->getLocStart();
699 if (isa<ObjCProtocolDecl>(*DI) &&
700 !cast<ObjCProtocolDecl>(*DI)->isThisDeclarationADefinition() &&
701 StartLoc == (*DI)->getLocStart())
707 } while (DI != DIEnd);
708 RewriteForwardProtocolDecl(DG);
713 HandleTopLevelSingleDecl(*DI);
717 // If we have a decl in the main file, see if we should rewrite it.
718 if (SM->isFromMainFile(Loc))
719 return HandleDeclInMainFile(D);
722 //===----------------------------------------------------------------------===//
723 // Syntactic (non-AST) Rewriting Code
724 //===----------------------------------------------------------------------===//
726 void RewriteObjC::RewriteInclude() {
727 SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID);
728 StringRef MainBuf = SM->getBufferData(MainFileID);
729 const char *MainBufStart = MainBuf.begin();
730 const char *MainBufEnd = MainBuf.end();
731 size_t ImportLen = strlen("import");
733 // Loop over the whole file, looking for includes.
734 for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) {
735 if (*BufPtr == '#') {
736 if (++BufPtr == MainBufEnd)
738 while (*BufPtr == ' ' || *BufPtr == '\t')
739 if (++BufPtr == MainBufEnd)
741 if (!strncmp(BufPtr, "import", ImportLen)) {
742 // replace import with include
743 SourceLocation ImportLoc =
744 LocStart.getLocWithOffset(BufPtr-MainBufStart);
745 ReplaceText(ImportLoc, ImportLen, "include");
752 static std::string getIvarAccessString(ObjCIvarDecl *OID) {
753 const ObjCInterfaceDecl *ClassDecl = OID->getContainingInterface();
756 S += ClassDecl->getIdentifier()->getName();
757 S += "_IMPL *)self)->";
762 void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID,
763 ObjCImplementationDecl *IMD,
764 ObjCCategoryImplDecl *CID) {
765 static bool objcGetPropertyDefined = false;
766 static bool objcSetPropertyDefined = false;
767 SourceLocation startLoc = PID->getLocStart();
768 InsertText(startLoc, "// ");
769 const char *startBuf = SM->getCharacterData(startLoc);
770 assert((*startBuf == '@') && "bogus @synthesize location");
771 const char *semiBuf = strchr(startBuf, ';');
772 assert((*semiBuf == ';') && "@synthesize: can't find ';'");
773 SourceLocation onePastSemiLoc =
774 startLoc.getLocWithOffset(semiBuf-startBuf+1);
776 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
777 return; // FIXME: is this correct?
779 // Generate the 'getter' function.
780 ObjCPropertyDecl *PD = PID->getPropertyDecl();
781 ObjCIvarDecl *OID = PID->getPropertyIvarDecl();
785 unsigned Attributes = PD->getPropertyAttributes();
786 if (!PD->getGetterMethodDecl()->isDefined()) {
787 bool GenGetProperty = !(Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) &&
788 (Attributes & (ObjCPropertyDecl::OBJC_PR_retain |
789 ObjCPropertyDecl::OBJC_PR_copy));
791 if (GenGetProperty && !objcGetPropertyDefined) {
792 objcGetPropertyDefined = true;
793 // FIXME. Is this attribute correct in all cases?
794 Getr = "\nextern \"C\" __declspec(dllimport) "
795 "id objc_getProperty(id, SEL, long, bool);\n";
797 RewriteObjCMethodDecl(OID->getContainingInterface(),
798 PD->getGetterMethodDecl(), Getr);
800 // Synthesize an explicit cast to gain access to the ivar.
801 // See objc-act.c:objc_synthesize_new_getter() for details.
802 if (GenGetProperty) {
803 // return objc_getProperty(self, _cmd, offsetof(ClassDecl, OID), 1)
805 const FunctionType *FPRetType = 0;
806 RewriteTypeIntoString(PD->getGetterMethodDecl()->getResultType(), Getr,
810 Getr += ")"; // close the precedence "scope" for "*".
812 // Now, emit the argument types (if any).
813 if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)){
815 for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) {
817 std::string ParamStr = FT->getArgType(i).getAsString(
818 Context->getPrintingPolicy());
821 if (FT->isVariadic()) {
822 if (FT->getNumArgs()) Getr += ", ";
830 Getr += "return (_TYPE)";
831 Getr += "objc_getProperty(self, _cmd, ";
832 RewriteIvarOffsetComputation(OID, Getr);
836 Getr += "return " + getIvarAccessString(OID);
838 InsertText(onePastSemiLoc, Getr);
841 if (PD->isReadOnly() || PD->getSetterMethodDecl()->isDefined())
844 // Generate the 'setter' function.
846 bool GenSetProperty = Attributes & (ObjCPropertyDecl::OBJC_PR_retain |
847 ObjCPropertyDecl::OBJC_PR_copy);
848 if (GenSetProperty && !objcSetPropertyDefined) {
849 objcSetPropertyDefined = true;
850 // FIXME. Is this attribute correct in all cases?
851 Setr = "\nextern \"C\" __declspec(dllimport) "
852 "void objc_setProperty (id, SEL, long, id, bool, bool);\n";
855 RewriteObjCMethodDecl(OID->getContainingInterface(),
856 PD->getSetterMethodDecl(), Setr);
858 // Synthesize an explicit cast to initialize the ivar.
859 // See objc-act.c:objc_synthesize_new_setter() for details.
860 if (GenSetProperty) {
861 Setr += "objc_setProperty (self, _cmd, ";
862 RewriteIvarOffsetComputation(OID, Setr);
864 Setr += PD->getName();
866 if (Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic)
870 if (Attributes & ObjCPropertyDecl::OBJC_PR_copy)
876 Setr += getIvarAccessString(OID) + " = ";
877 Setr += PD->getName();
880 InsertText(onePastSemiLoc, Setr);
883 static void RewriteOneForwardClassDecl(ObjCInterfaceDecl *ForwardDecl,
884 std::string &typedefString) {
885 typedefString += "#ifndef _REWRITER_typedef_";
886 typedefString += ForwardDecl->getNameAsString();
887 typedefString += "\n";
888 typedefString += "#define _REWRITER_typedef_";
889 typedefString += ForwardDecl->getNameAsString();
890 typedefString += "\n";
891 typedefString += "typedef struct objc_object ";
892 typedefString += ForwardDecl->getNameAsString();
893 typedefString += ";\n#endif\n";
896 void RewriteObjC::RewriteForwardClassEpilogue(ObjCInterfaceDecl *ClassDecl,
897 const std::string &typedefString) {
898 SourceLocation startLoc = ClassDecl->getLocStart();
899 const char *startBuf = SM->getCharacterData(startLoc);
900 const char *semiPtr = strchr(startBuf, ';');
901 // Replace the @class with typedefs corresponding to the classes.
902 ReplaceText(startLoc, semiPtr-startBuf+1, typedefString);
905 void RewriteObjC::RewriteForwardClassDecl(DeclGroupRef D) {
906 std::string typedefString;
907 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) {
908 ObjCInterfaceDecl *ForwardDecl = cast<ObjCInterfaceDecl>(*I);
909 if (I == D.begin()) {
910 // Translate to typedef's that forward reference structs with the same name
911 // as the class. As a convenience, we include the original declaration
913 typedefString += "// @class ";
914 typedefString += ForwardDecl->getNameAsString();
915 typedefString += ";\n";
917 RewriteOneForwardClassDecl(ForwardDecl, typedefString);
919 DeclGroupRef::iterator I = D.begin();
920 RewriteForwardClassEpilogue(cast<ObjCInterfaceDecl>(*I), typedefString);
923 void RewriteObjC::RewriteForwardClassDecl(
924 const llvm::SmallVector<Decl*, 8> &D) {
925 std::string typedefString;
926 for (unsigned i = 0; i < D.size(); i++) {
927 ObjCInterfaceDecl *ForwardDecl = cast<ObjCInterfaceDecl>(D[i]);
929 typedefString += "// @class ";
930 typedefString += ForwardDecl->getNameAsString();
931 typedefString += ";\n";
933 RewriteOneForwardClassDecl(ForwardDecl, typedefString);
935 RewriteForwardClassEpilogue(cast<ObjCInterfaceDecl>(D[0]), typedefString);
938 void RewriteObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) {
939 // When method is a synthesized one, such as a getter/setter there is
940 // nothing to rewrite.
941 if (Method->isImplicit())
943 SourceLocation LocStart = Method->getLocStart();
944 SourceLocation LocEnd = Method->getLocEnd();
946 if (SM->getExpansionLineNumber(LocEnd) >
947 SM->getExpansionLineNumber(LocStart)) {
948 InsertText(LocStart, "#if 0\n");
949 ReplaceText(LocEnd, 1, ";\n#endif\n");
951 InsertText(LocStart, "// ");
955 void RewriteObjC::RewriteProperty(ObjCPropertyDecl *prop) {
956 SourceLocation Loc = prop->getAtLoc();
958 ReplaceText(Loc, 0, "// ");
959 // FIXME: handle properties that are declared across multiple lines.
962 void RewriteObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) {
963 SourceLocation LocStart = CatDecl->getLocStart();
965 // FIXME: handle category headers that are declared across multiple lines.
966 ReplaceText(LocStart, 0, "// ");
968 for (ObjCCategoryDecl::prop_iterator I = CatDecl->prop_begin(),
969 E = CatDecl->prop_end(); I != E; ++I)
972 for (ObjCCategoryDecl::instmeth_iterator
973 I = CatDecl->instmeth_begin(), E = CatDecl->instmeth_end();
975 RewriteMethodDeclaration(*I);
976 for (ObjCCategoryDecl::classmeth_iterator
977 I = CatDecl->classmeth_begin(), E = CatDecl->classmeth_end();
979 RewriteMethodDeclaration(*I);
981 // Lastly, comment out the @end.
982 ReplaceText(CatDecl->getAtEndRange().getBegin(),
983 strlen("@end"), "/* @end */");
986 void RewriteObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) {
987 SourceLocation LocStart = PDecl->getLocStart();
988 assert(PDecl->isThisDeclarationADefinition());
990 // FIXME: handle protocol headers that are declared across multiple lines.
991 ReplaceText(LocStart, 0, "// ");
993 for (ObjCProtocolDecl::instmeth_iterator
994 I = PDecl->instmeth_begin(), E = PDecl->instmeth_end();
996 RewriteMethodDeclaration(*I);
997 for (ObjCProtocolDecl::classmeth_iterator
998 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
1000 RewriteMethodDeclaration(*I);
1002 for (ObjCInterfaceDecl::prop_iterator I = PDecl->prop_begin(),
1003 E = PDecl->prop_end(); I != E; ++I)
1004 RewriteProperty(*I);
1006 // Lastly, comment out the @end.
1007 SourceLocation LocEnd = PDecl->getAtEndRange().getBegin();
1008 ReplaceText(LocEnd, strlen("@end"), "/* @end */");
1010 // Must comment out @optional/@required
1011 const char *startBuf = SM->getCharacterData(LocStart);
1012 const char *endBuf = SM->getCharacterData(LocEnd);
1013 for (const char *p = startBuf; p < endBuf; p++) {
1014 if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) {
1015 SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf);
1016 ReplaceText(OptionalLoc, strlen("@optional"), "/* @optional */");
1019 else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) {
1020 SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf);
1021 ReplaceText(OptionalLoc, strlen("@required"), "/* @required */");
1027 void RewriteObjC::RewriteForwardProtocolDecl(DeclGroupRef D) {
1028 SourceLocation LocStart = (*D.begin())->getLocStart();
1029 if (LocStart.isInvalid())
1030 llvm_unreachable("Invalid SourceLocation");
1031 // FIXME: handle forward protocol that are declared across multiple lines.
1032 ReplaceText(LocStart, 0, "// ");
1036 RewriteObjC::RewriteForwardProtocolDecl(const llvm::SmallVector<Decl*, 8> &DG) {
1037 SourceLocation LocStart = DG[0]->getLocStart();
1038 if (LocStart.isInvalid())
1039 llvm_unreachable("Invalid SourceLocation");
1040 // FIXME: handle forward protocol that are declared across multiple lines.
1041 ReplaceText(LocStart, 0, "// ");
1044 void RewriteObjC::RewriteTypeIntoString(QualType T, std::string &ResultStr,
1045 const FunctionType *&FPRetType) {
1046 if (T->isObjCQualifiedIdType())
1048 else if (T->isFunctionPointerType() ||
1049 T->isBlockPointerType()) {
1050 // needs special handling, since pointer-to-functions have special
1051 // syntax (where a decaration models use).
1052 QualType retType = T;
1054 if (const PointerType* PT = retType->getAs<PointerType>())
1055 PointeeTy = PT->getPointeeType();
1056 else if (const BlockPointerType *BPT = retType->getAs<BlockPointerType>())
1057 PointeeTy = BPT->getPointeeType();
1058 if ((FPRetType = PointeeTy->getAs<FunctionType>())) {
1059 ResultStr += FPRetType->getResultType().getAsString(
1060 Context->getPrintingPolicy());
1064 ResultStr += T.getAsString(Context->getPrintingPolicy());
1067 void RewriteObjC::RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl,
1068 ObjCMethodDecl *OMD,
1069 std::string &ResultStr) {
1070 //fprintf(stderr,"In RewriteObjCMethodDecl\n");
1071 const FunctionType *FPRetType = 0;
1072 ResultStr += "\nstatic ";
1073 RewriteTypeIntoString(OMD->getResultType(), ResultStr, FPRetType);
1076 // Unique method name
1077 std::string NameStr;
1079 if (OMD->isInstanceMethod())
1084 NameStr += IDecl->getNameAsString();
1087 if (ObjCCategoryImplDecl *CID =
1088 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1089 NameStr += CID->getNameAsString();
1092 // Append selector names, replacing ':' with '_'
1094 std::string selString = OMD->getSelector().getAsString();
1095 int len = selString.size();
1096 for (int i = 0; i < len; i++)
1097 if (selString[i] == ':')
1099 NameStr += selString;
1101 // Remember this name for metadata emission
1102 MethodInternalNames[OMD] = NameStr;
1103 ResultStr += NameStr;
1105 // Rewrite arguments
1108 // invisible arguments
1109 if (OMD->isInstanceMethod()) {
1110 QualType selfTy = Context->getObjCInterfaceType(IDecl);
1111 selfTy = Context->getPointerType(selfTy);
1112 if (!LangOpts.MicrosoftExt) {
1113 if (ObjCSynthesizedStructs.count(const_cast<ObjCInterfaceDecl*>(IDecl)))
1114 ResultStr += "struct ";
1116 // When rewriting for Microsoft, explicitly omit the structure name.
1117 ResultStr += IDecl->getNameAsString();
1121 ResultStr += Context->getObjCClassType().getAsString(
1122 Context->getPrintingPolicy());
1124 ResultStr += " self, ";
1125 ResultStr += Context->getObjCSelType().getAsString(Context->getPrintingPolicy());
1126 ResultStr += " _cmd";
1128 // Method arguments.
1129 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
1130 E = OMD->param_end(); PI != E; ++PI) {
1131 ParmVarDecl *PDecl = *PI;
1133 if (PDecl->getType()->isObjCQualifiedIdType()) {
1135 ResultStr += PDecl->getNameAsString();
1137 std::string Name = PDecl->getNameAsString();
1138 QualType QT = PDecl->getType();
1139 // Make sure we convert "t (^)(...)" to "t (*)(...)".
1140 (void)convertBlockPointerToFunctionPointer(QT);
1141 QT.getAsStringInternal(Name, Context->getPrintingPolicy());
1145 if (OMD->isVariadic())
1146 ResultStr += ", ...";
1150 ResultStr += ")"; // close the precedence "scope" for "*".
1152 // Now, emit the argument types (if any).
1153 if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) {
1155 for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) {
1156 if (i) ResultStr += ", ";
1157 std::string ParamStr = FT->getArgType(i).getAsString(
1158 Context->getPrintingPolicy());
1159 ResultStr += ParamStr;
1161 if (FT->isVariadic()) {
1162 if (FT->getNumArgs()) ResultStr += ", ";
1171 void RewriteObjC::RewriteImplementationDecl(Decl *OID) {
1172 ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID);
1173 ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID);
1175 InsertText(IMD ? IMD->getLocStart() : CID->getLocStart(), "// ");
1177 for (ObjCCategoryImplDecl::instmeth_iterator
1178 I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(),
1179 E = IMD ? IMD->instmeth_end() : CID->instmeth_end();
1181 std::string ResultStr;
1182 ObjCMethodDecl *OMD = *I;
1183 RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr);
1184 SourceLocation LocStart = OMD->getLocStart();
1185 SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart();
1187 const char *startBuf = SM->getCharacterData(LocStart);
1188 const char *endBuf = SM->getCharacterData(LocEnd);
1189 ReplaceText(LocStart, endBuf-startBuf, ResultStr);
1192 for (ObjCCategoryImplDecl::classmeth_iterator
1193 I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(),
1194 E = IMD ? IMD->classmeth_end() : CID->classmeth_end();
1196 std::string ResultStr;
1197 ObjCMethodDecl *OMD = *I;
1198 RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr);
1199 SourceLocation LocStart = OMD->getLocStart();
1200 SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart();
1202 const char *startBuf = SM->getCharacterData(LocStart);
1203 const char *endBuf = SM->getCharacterData(LocEnd);
1204 ReplaceText(LocStart, endBuf-startBuf, ResultStr);
1206 for (ObjCCategoryImplDecl::propimpl_iterator
1207 I = IMD ? IMD->propimpl_begin() : CID->propimpl_begin(),
1208 E = IMD ? IMD->propimpl_end() : CID->propimpl_end();
1210 RewritePropertyImplDecl(*I, IMD, CID);
1213 InsertText(IMD ? IMD->getLocEnd() : CID->getLocEnd(), "// ");
1216 void RewriteObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) {
1217 std::string ResultStr;
1218 if (!ObjCForwardDecls.count(ClassDecl->getCanonicalDecl())) {
1219 // we haven't seen a forward decl - generate a typedef.
1220 ResultStr = "#ifndef _REWRITER_typedef_";
1221 ResultStr += ClassDecl->getNameAsString();
1223 ResultStr += "#define _REWRITER_typedef_";
1224 ResultStr += ClassDecl->getNameAsString();
1226 ResultStr += "typedef struct objc_object ";
1227 ResultStr += ClassDecl->getNameAsString();
1228 ResultStr += ";\n#endif\n";
1229 // Mark this typedef as having been generated.
1230 ObjCForwardDecls.insert(ClassDecl->getCanonicalDecl());
1232 RewriteObjCInternalStruct(ClassDecl, ResultStr);
1234 for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(),
1235 E = ClassDecl->prop_end(); I != E; ++I)
1236 RewriteProperty(*I);
1237 for (ObjCInterfaceDecl::instmeth_iterator
1238 I = ClassDecl->instmeth_begin(), E = ClassDecl->instmeth_end();
1240 RewriteMethodDeclaration(*I);
1241 for (ObjCInterfaceDecl::classmeth_iterator
1242 I = ClassDecl->classmeth_begin(), E = ClassDecl->classmeth_end();
1244 RewriteMethodDeclaration(*I);
1246 // Lastly, comment out the @end.
1247 ReplaceText(ClassDecl->getAtEndRange().getBegin(), strlen("@end"),
1251 Stmt *RewriteObjC::RewritePropertyOrImplicitSetter(PseudoObjectExpr *PseudoOp) {
1252 SourceRange OldRange = PseudoOp->getSourceRange();
1254 // We just magically know some things about the structure of this
1256 ObjCMessageExpr *OldMsg =
1257 cast<ObjCMessageExpr>(PseudoOp->getSemanticExpr(
1258 PseudoOp->getNumSemanticExprs() - 1));
1260 // Because the rewriter doesn't allow us to rewrite rewritten code,
1261 // we need to suppress rewriting the sub-statements.
1264 DisableReplaceStmtScope S(*this);
1266 // Rebuild the base expression if we have one.
1268 if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) {
1269 Base = OldMsg->getInstanceReceiver();
1270 Base = cast<OpaqueValueExpr>(Base)->getSourceExpr();
1271 Base = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Base));
1275 RHS = cast<BinaryOperator>(PseudoOp->getSyntacticForm())->getRHS();
1276 RHS = cast<OpaqueValueExpr>(RHS)->getSourceExpr();
1277 RHS = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(RHS));
1280 // TODO: avoid this copy.
1281 SmallVector<SourceLocation, 1> SelLocs;
1282 OldMsg->getSelectorLocs(SelLocs);
1284 ObjCMessageExpr *NewMsg = 0;
1285 switch (OldMsg->getReceiverKind()) {
1286 case ObjCMessageExpr::Class:
1287 NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(),
1288 OldMsg->getValueKind(),
1289 OldMsg->getLeftLoc(),
1290 OldMsg->getClassReceiverTypeInfo(),
1291 OldMsg->getSelector(),
1293 OldMsg->getMethodDecl(),
1295 OldMsg->getRightLoc(),
1296 OldMsg->isImplicit());
1299 case ObjCMessageExpr::Instance:
1300 NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(),
1301 OldMsg->getValueKind(),
1302 OldMsg->getLeftLoc(),
1304 OldMsg->getSelector(),
1306 OldMsg->getMethodDecl(),
1308 OldMsg->getRightLoc(),
1309 OldMsg->isImplicit());
1312 case ObjCMessageExpr::SuperClass:
1313 case ObjCMessageExpr::SuperInstance:
1314 NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(),
1315 OldMsg->getValueKind(),
1316 OldMsg->getLeftLoc(),
1317 OldMsg->getSuperLoc(),
1318 OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance,
1319 OldMsg->getSuperType(),
1320 OldMsg->getSelector(),
1322 OldMsg->getMethodDecl(),
1324 OldMsg->getRightLoc(),
1325 OldMsg->isImplicit());
1329 Stmt *Replacement = SynthMessageExpr(NewMsg);
1330 ReplaceStmtWithRange(PseudoOp, Replacement, OldRange);
1334 Stmt *RewriteObjC::RewritePropertyOrImplicitGetter(PseudoObjectExpr *PseudoOp) {
1335 SourceRange OldRange = PseudoOp->getSourceRange();
1337 // We just magically know some things about the structure of this
1339 ObjCMessageExpr *OldMsg =
1340 cast<ObjCMessageExpr>(PseudoOp->getResultExpr()->IgnoreImplicit());
1342 // Because the rewriter doesn't allow us to rewrite rewritten code,
1343 // we need to suppress rewriting the sub-statements.
1346 DisableReplaceStmtScope S(*this);
1348 // Rebuild the base expression if we have one.
1349 if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) {
1350 Base = OldMsg->getInstanceReceiver();
1351 Base = cast<OpaqueValueExpr>(Base)->getSourceExpr();
1352 Base = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Base));
1356 // Intentionally empty.
1357 SmallVector<SourceLocation, 1> SelLocs;
1358 SmallVector<Expr*, 1> Args;
1360 ObjCMessageExpr *NewMsg = 0;
1361 switch (OldMsg->getReceiverKind()) {
1362 case ObjCMessageExpr::Class:
1363 NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(),
1364 OldMsg->getValueKind(),
1365 OldMsg->getLeftLoc(),
1366 OldMsg->getClassReceiverTypeInfo(),
1367 OldMsg->getSelector(),
1369 OldMsg->getMethodDecl(),
1371 OldMsg->getRightLoc(),
1372 OldMsg->isImplicit());
1375 case ObjCMessageExpr::Instance:
1376 NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(),
1377 OldMsg->getValueKind(),
1378 OldMsg->getLeftLoc(),
1380 OldMsg->getSelector(),
1382 OldMsg->getMethodDecl(),
1384 OldMsg->getRightLoc(),
1385 OldMsg->isImplicit());
1388 case ObjCMessageExpr::SuperClass:
1389 case ObjCMessageExpr::SuperInstance:
1390 NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(),
1391 OldMsg->getValueKind(),
1392 OldMsg->getLeftLoc(),
1393 OldMsg->getSuperLoc(),
1394 OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance,
1395 OldMsg->getSuperType(),
1396 OldMsg->getSelector(),
1398 OldMsg->getMethodDecl(),
1400 OldMsg->getRightLoc(),
1401 OldMsg->isImplicit());
1405 Stmt *Replacement = SynthMessageExpr(NewMsg);
1406 ReplaceStmtWithRange(PseudoOp, Replacement, OldRange);
1410 /// SynthCountByEnumWithState - To print:
1411 /// ((unsigned int (*)
1412 /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int))
1413 /// (void *)objc_msgSend)((id)l_collection,
1414 /// sel_registerName(
1415 /// "countByEnumeratingWithState:objects:count:"),
1417 /// (id *)__rw_items, (unsigned int)16)
1419 void RewriteObjC::SynthCountByEnumWithState(std::string &buf) {
1420 buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, "
1421 "id *, unsigned int))(void *)objc_msgSend)";
1423 buf += "((id)l_collection,\n\t\t";
1424 buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),";
1426 buf += "&enumState, "
1427 "(id *)__rw_items, (unsigned int)16)";
1430 /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach
1431 /// statement to exit to its outer synthesized loop.
1433 Stmt *RewriteObjC::RewriteBreakStmt(BreakStmt *S) {
1434 if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back()))
1436 // replace break with goto __break_label
1439 SourceLocation startLoc = S->getLocStart();
1440 buf = "goto __break_label_";
1441 buf += utostr(ObjCBcLabelNo.back());
1442 ReplaceText(startLoc, strlen("break"), buf);
1447 /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach
1448 /// statement to continue with its inner synthesized loop.
1450 Stmt *RewriteObjC::RewriteContinueStmt(ContinueStmt *S) {
1451 if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back()))
1453 // replace continue with goto __continue_label
1456 SourceLocation startLoc = S->getLocStart();
1457 buf = "goto __continue_label_";
1458 buf += utostr(ObjCBcLabelNo.back());
1459 ReplaceText(startLoc, strlen("continue"), buf);
1464 /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement.
1466 /// for ( type elem in collection) { stmts; }
1471 /// struct __objcFastEnumerationState enumState = { 0 };
1472 /// id __rw_items[16];
1473 /// id l_collection = (id)collection;
1474 /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState
1475 /// objects:__rw_items count:16];
1477 /// unsigned long startMutations = *enumState.mutationsPtr;
1479 /// unsigned long counter = 0;
1481 /// if (startMutations != *enumState.mutationsPtr)
1482 /// objc_enumerationMutation(l_collection);
1483 /// elem = (type)enumState.itemsPtr[counter++];
1485 /// __continue_label: ;
1486 /// } while (counter < limit);
1487 /// } while (limit = [l_collection countByEnumeratingWithState:&enumState
1488 /// objects:__rw_items count:16]);
1490 /// __break_label: ;
1496 Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S,
1497 SourceLocation OrigEnd) {
1498 assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty");
1499 assert(isa<ObjCForCollectionStmt>(Stmts.back()) &&
1500 "ObjCForCollectionStmt Statement stack mismatch");
1501 assert(!ObjCBcLabelNo.empty() &&
1502 "ObjCForCollectionStmt - Label No stack empty");
1504 SourceLocation startLoc = S->getLocStart();
1505 const char *startBuf = SM->getCharacterData(startLoc);
1506 StringRef elementName;
1507 std::string elementTypeAsString;
1510 if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) {
1512 NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl());
1513 QualType ElementType = cast<ValueDecl>(D)->getType();
1514 if (ElementType->isObjCQualifiedIdType() ||
1515 ElementType->isObjCQualifiedInterfaceType())
1516 // Simply use 'id' for all qualified types.
1517 elementTypeAsString = "id";
1519 elementTypeAsString = ElementType.getAsString(Context->getPrintingPolicy());
1520 buf += elementTypeAsString;
1522 elementName = D->getName();
1527 DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement());
1528 elementName = DR->getDecl()->getName();
1529 ValueDecl *VD = cast<ValueDecl>(DR->getDecl());
1530 if (VD->getType()->isObjCQualifiedIdType() ||
1531 VD->getType()->isObjCQualifiedInterfaceType())
1532 // Simply use 'id' for all qualified types.
1533 elementTypeAsString = "id";
1535 elementTypeAsString = VD->getType().getAsString(Context->getPrintingPolicy());
1538 // struct __objcFastEnumerationState enumState = { 0 };
1539 buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t";
1540 // id __rw_items[16];
1541 buf += "id __rw_items[16];\n\t";
1542 // id l_collection = (id)
1543 buf += "id l_collection = (id)";
1544 // Find start location of 'collection' the hard way!
1545 const char *startCollectionBuf = startBuf;
1546 startCollectionBuf += 3; // skip 'for'
1547 startCollectionBuf = strchr(startCollectionBuf, '(');
1548 startCollectionBuf++; // skip '('
1549 // find 'in' and skip it.
1550 while (*startCollectionBuf != ' ' ||
1551 *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' ||
1552 (*(startCollectionBuf+3) != ' ' &&
1553 *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '('))
1554 startCollectionBuf++;
1555 startCollectionBuf += 3;
1557 // Replace: "for (type element in" with string constructed thus far.
1558 ReplaceText(startLoc, startCollectionBuf - startBuf, buf);
1559 // Replace ')' in for '(' type elem in collection ')' with ';'
1560 SourceLocation rightParenLoc = S->getRParenLoc();
1561 const char *rparenBuf = SM->getCharacterData(rightParenLoc);
1562 SourceLocation lparenLoc = startLoc.getLocWithOffset(rparenBuf-startBuf);
1565 // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState
1566 // objects:__rw_items count:16];
1567 // which is synthesized into:
1568 // unsigned int limit =
1569 // ((unsigned int (*)
1570 // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int))
1571 // (void *)objc_msgSend)((id)l_collection,
1572 // sel_registerName(
1573 // "countByEnumeratingWithState:objects:count:"),
1574 // (struct __objcFastEnumerationState *)&state,
1575 // (id *)__rw_items, (unsigned int)16);
1576 buf += "unsigned long limit =\n\t\t";
1577 SynthCountByEnumWithState(buf);
1580 /// unsigned long startMutations = *enumState.mutationsPtr;
1582 /// unsigned long counter = 0;
1584 /// if (startMutations != *enumState.mutationsPtr)
1585 /// objc_enumerationMutation(l_collection);
1586 /// elem = (type)enumState.itemsPtr[counter++];
1587 buf += "if (limit) {\n\t";
1588 buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t";
1589 buf += "do {\n\t\t";
1590 buf += "unsigned long counter = 0;\n\t\t";
1591 buf += "do {\n\t\t\t";
1592 buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t";
1593 buf += "objc_enumerationMutation(l_collection);\n\t\t\t";
1596 buf += elementTypeAsString;
1597 buf += ")enumState.itemsPtr[counter++];";
1598 // Replace ')' in for '(' type elem in collection ')' with all of these.
1599 ReplaceText(lparenLoc, 1, buf);
1601 /// __continue_label: ;
1602 /// } while (counter < limit);
1603 /// } while (limit = [l_collection countByEnumeratingWithState:&enumState
1604 /// objects:__rw_items count:16]);
1606 /// __break_label: ;
1613 buf += "__continue_label_";
1614 buf += utostr(ObjCBcLabelNo.back());
1617 buf += "} while (counter < limit);\n\t";
1618 buf += "} while (limit = ";
1619 SynthCountByEnumWithState(buf);
1623 buf += elementTypeAsString;
1625 buf += "__break_label_";
1626 buf += utostr(ObjCBcLabelNo.back());
1629 buf += "else\n\t\t";
1632 buf += elementTypeAsString;
1636 // Insert all these *after* the statement body.
1637 // FIXME: If this should support Obj-C++, support CXXTryStmt
1638 if (isa<CompoundStmt>(S->getBody())) {
1639 SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(1);
1640 InsertText(endBodyLoc, buf);
1642 /* Need to treat single statements specially. For example:
1644 * for (A *a in b) if (stuff()) break;
1645 * for (A *a in b) xxxyy;
1647 * The following code simply scans ahead to the semi to find the actual end.
1649 const char *stmtBuf = SM->getCharacterData(OrigEnd);
1650 const char *semiBuf = strchr(stmtBuf, ';');
1651 assert(semiBuf && "Can't find ';'");
1652 SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(semiBuf-stmtBuf+1);
1653 InsertText(endBodyLoc, buf);
1656 ObjCBcLabelNo.pop_back();
1660 /// RewriteObjCSynchronizedStmt -
1661 /// This routine rewrites @synchronized(expr) stmt;
1663 /// objc_sync_enter(expr);
1664 /// @try stmt @finally { objc_sync_exit(expr); }
1666 Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) {
1667 // Get the start location and compute the semi location.
1668 SourceLocation startLoc = S->getLocStart();
1669 const char *startBuf = SM->getCharacterData(startLoc);
1671 assert((*startBuf == '@') && "bogus @synchronized location");
1674 buf = "objc_sync_enter((id)";
1675 const char *lparenBuf = startBuf;
1676 while (*lparenBuf != '(') lparenBuf++;
1677 ReplaceText(startLoc, lparenBuf-startBuf+1, buf);
1678 // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since
1679 // the sync expression is typically a message expression that's already
1680 // been rewritten! (which implies the SourceLocation's are invalid).
1681 SourceLocation endLoc = S->getSynchBody()->getLocStart();
1682 const char *endBuf = SM->getCharacterData(endLoc);
1683 while (*endBuf != ')') endBuf--;
1684 SourceLocation rparenLoc = startLoc.getLocWithOffset(endBuf-startBuf);
1686 // declare a new scope with two variables, _stack and _rethrow.
1687 buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n";
1688 buf += "int buf[18/*32-bit i386*/];\n";
1689 buf += "char *pointers[4];} _stack;\n";
1690 buf += "id volatile _rethrow = 0;\n";
1691 buf += "objc_exception_try_enter(&_stack);\n";
1692 buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n";
1693 ReplaceText(rparenLoc, 1, buf);
1694 startLoc = S->getSynchBody()->getLocEnd();
1695 startBuf = SM->getCharacterData(startLoc);
1697 assert((*startBuf == '}') && "bogus @synchronized block");
1698 SourceLocation lastCurlyLoc = startLoc;
1699 buf = "}\nelse {\n";
1700 buf += " _rethrow = objc_exception_extract(&_stack);\n";
1702 buf += "{ /* implicit finally clause */\n";
1703 buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n";
1705 std::string syncBuf;
1706 syncBuf += " objc_sync_exit(";
1708 Expr *syncExpr = S->getSynchExpr();
1709 CastKind CK = syncExpr->getType()->isObjCObjectPointerType()
1711 syncExpr->getType()->isBlockPointerType()
1712 ? CK_BlockPointerToObjCPointerCast
1713 : CK_CPointerToObjCPointerCast;
1714 syncExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(),
1716 std::string syncExprBufS;
1717 llvm::raw_string_ostream syncExprBuf(syncExprBufS);
1718 syncExpr->printPretty(syncExprBuf, *Context, 0,
1719 PrintingPolicy(LangOpts));
1720 syncBuf += syncExprBuf.str();
1724 buf += "\n if (_rethrow) objc_exception_throw(_rethrow);\n";
1728 ReplaceText(lastCurlyLoc, 1, buf);
1730 bool hasReturns = false;
1731 HasReturnStmts(S->getSynchBody(), hasReturns);
1733 RewriteSyncReturnStmts(S->getSynchBody(), syncBuf);
1738 void RewriteObjC::WarnAboutReturnGotoStmts(Stmt *S)
1740 // Perform a bottom up traversal of all children.
1741 for (Stmt::child_range CI = S->children(); CI; ++CI)
1743 WarnAboutReturnGotoStmts(*CI);
1745 if (isa<ReturnStmt>(S) || isa<GotoStmt>(S)) {
1746 Diags.Report(Context->getFullLoc(S->getLocStart()),
1747 TryFinallyContainsReturnDiag);
1752 void RewriteObjC::HasReturnStmts(Stmt *S, bool &hasReturns)
1754 // Perform a bottom up traversal of all children.
1755 for (Stmt::child_range CI = S->children(); CI; ++CI)
1757 HasReturnStmts(*CI, hasReturns);
1759 if (isa<ReturnStmt>(S))
1764 void RewriteObjC::RewriteTryReturnStmts(Stmt *S) {
1765 // Perform a bottom up traversal of all children.
1766 for (Stmt::child_range CI = S->children(); CI; ++CI)
1768 RewriteTryReturnStmts(*CI);
1770 if (isa<ReturnStmt>(S)) {
1771 SourceLocation startLoc = S->getLocStart();
1772 const char *startBuf = SM->getCharacterData(startLoc);
1774 const char *semiBuf = strchr(startBuf, ';');
1775 assert((*semiBuf == ';') && "RewriteTryReturnStmts: can't find ';'");
1776 SourceLocation onePastSemiLoc = startLoc.getLocWithOffset(semiBuf-startBuf+1);
1779 buf = "{ objc_exception_try_exit(&_stack); return";
1781 ReplaceText(startLoc, 6, buf);
1782 InsertText(onePastSemiLoc, "}");
1787 void RewriteObjC::RewriteSyncReturnStmts(Stmt *S, std::string syncExitBuf) {
1788 // Perform a bottom up traversal of all children.
1789 for (Stmt::child_range CI = S->children(); CI; ++CI)
1791 RewriteSyncReturnStmts(*CI, syncExitBuf);
1793 if (isa<ReturnStmt>(S)) {
1794 SourceLocation startLoc = S->getLocStart();
1795 const char *startBuf = SM->getCharacterData(startLoc);
1797 const char *semiBuf = strchr(startBuf, ';');
1798 assert((*semiBuf == ';') && "RewriteSyncReturnStmts: can't find ';'");
1799 SourceLocation onePastSemiLoc = startLoc.getLocWithOffset(semiBuf-startBuf+1);
1802 buf = "{ objc_exception_try_exit(&_stack);";
1806 ReplaceText(startLoc, 6, buf);
1807 InsertText(onePastSemiLoc, "}");
1812 Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) {
1813 // Get the start location and compute the semi location.
1814 SourceLocation startLoc = S->getLocStart();
1815 const char *startBuf = SM->getCharacterData(startLoc);
1817 assert((*startBuf == '@') && "bogus @try location");
1820 // declare a new scope with two variables, _stack and _rethrow.
1821 buf = "/* @try scope begin */ { struct _objc_exception_data {\n";
1822 buf += "int buf[18/*32-bit i386*/];\n";
1823 buf += "char *pointers[4];} _stack;\n";
1824 buf += "id volatile _rethrow = 0;\n";
1825 buf += "objc_exception_try_enter(&_stack);\n";
1826 buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n";
1828 ReplaceText(startLoc, 4, buf);
1830 startLoc = S->getTryBody()->getLocEnd();
1831 startBuf = SM->getCharacterData(startLoc);
1833 assert((*startBuf == '}') && "bogus @try block");
1835 SourceLocation lastCurlyLoc = startLoc;
1836 if (S->getNumCatchStmts()) {
1837 startLoc = startLoc.getLocWithOffset(1);
1838 buf = " /* @catch begin */ else {\n";
1839 buf += " id _caught = objc_exception_extract(&_stack);\n";
1840 buf += " objc_exception_try_enter (&_stack);\n";
1841 buf += " if (_setjmp(_stack.buf))\n";
1842 buf += " _rethrow = objc_exception_extract(&_stack);\n";
1843 buf += " else { /* @catch continue */";
1845 InsertText(startLoc, buf);
1846 } else { /* no catch list */
1847 buf = "}\nelse {\n";
1848 buf += " _rethrow = objc_exception_extract(&_stack);\n";
1850 ReplaceText(lastCurlyLoc, 1, buf);
1852 Stmt *lastCatchBody = 0;
1853 for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I) {
1854 ObjCAtCatchStmt *Catch = S->getCatchStmt(I);
1855 VarDecl *catchDecl = Catch->getCatchParamDecl();
1858 buf = "if ("; // we are generating code for the first catch clause
1861 startLoc = Catch->getLocStart();
1862 startBuf = SM->getCharacterData(startLoc);
1864 assert((*startBuf == '@') && "bogus @catch location");
1866 const char *lParenLoc = strchr(startBuf, '(');
1868 if (Catch->hasEllipsis()) {
1869 // Now rewrite the body...
1870 lastCatchBody = Catch->getCatchBody();
1871 SourceLocation bodyLoc = lastCatchBody->getLocStart();
1872 const char *bodyBuf = SM->getCharacterData(bodyLoc);
1873 assert(*SM->getCharacterData(Catch->getRParenLoc()) == ')' &&
1874 "bogus @catch paren location");
1875 assert((*bodyBuf == '{') && "bogus @catch body location");
1877 buf += "1) { id _tmp = _caught;";
1878 Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, buf);
1879 } else if (catchDecl) {
1880 QualType t = catchDecl->getType();
1881 if (t == Context->getObjCIdType()) {
1883 ReplaceText(startLoc, lParenLoc-startBuf+1, buf);
1884 } else if (const ObjCObjectPointerType *Ptr =
1885 t->getAs<ObjCObjectPointerType>()) {
1886 // Should be a pointer to a class.
1887 ObjCInterfaceDecl *IDecl = Ptr->getObjectType()->getInterface();
1889 buf += "objc_exception_match((struct objc_class *)objc_getClass(\"";
1890 buf += IDecl->getNameAsString();
1891 buf += "\"), (struct objc_object *)_caught)) { ";
1892 ReplaceText(startLoc, lParenLoc-startBuf+1, buf);
1895 // Now rewrite the body...
1896 lastCatchBody = Catch->getCatchBody();
1897 SourceLocation rParenLoc = Catch->getRParenLoc();
1898 SourceLocation bodyLoc = lastCatchBody->getLocStart();
1899 const char *bodyBuf = SM->getCharacterData(bodyLoc);
1900 const char *rParenBuf = SM->getCharacterData(rParenLoc);
1901 assert((*rParenBuf == ')') && "bogus @catch paren location");
1902 assert((*bodyBuf == '{') && "bogus @catch body location");
1904 // Here we replace ") {" with "= _caught;" (which initializes and
1905 // declares the @catch parameter).
1906 ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, " = _caught;");
1908 llvm_unreachable("@catch rewrite bug");
1911 // Complete the catch list...
1912 if (lastCatchBody) {
1913 SourceLocation bodyLoc = lastCatchBody->getLocEnd();
1914 assert(*SM->getCharacterData(bodyLoc) == '}' &&
1915 "bogus @catch body location");
1917 // Insert the last (implicit) else clause *before* the right curly brace.
1918 bodyLoc = bodyLoc.getLocWithOffset(-1);
1919 buf = "} /* last catch end */\n";
1921 buf += " _rethrow = _caught;\n";
1922 buf += " objc_exception_try_exit(&_stack);\n";
1923 buf += "} } /* @catch end */\n";
1924 if (!S->getFinallyStmt())
1926 InsertText(bodyLoc, buf);
1929 lastCurlyLoc = lastCatchBody->getLocEnd();
1931 if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) {
1932 startLoc = finalStmt->getLocStart();
1933 startBuf = SM->getCharacterData(startLoc);
1934 assert((*startBuf == '@') && "bogus @finally start");
1936 ReplaceText(startLoc, 8, "/* @finally */");
1938 Stmt *body = finalStmt->getFinallyBody();
1939 SourceLocation startLoc = body->getLocStart();
1940 SourceLocation endLoc = body->getLocEnd();
1941 assert(*SM->getCharacterData(startLoc) == '{' &&
1942 "bogus @finally body location");
1943 assert(*SM->getCharacterData(endLoc) == '}' &&
1944 "bogus @finally body location");
1946 startLoc = startLoc.getLocWithOffset(1);
1947 InsertText(startLoc, " if (!_rethrow) objc_exception_try_exit(&_stack);\n");
1948 endLoc = endLoc.getLocWithOffset(-1);
1949 InsertText(endLoc, " if (_rethrow) objc_exception_throw(_rethrow);\n");
1952 lastCurlyLoc = body->getLocEnd();
1954 // Now check for any return/continue/go statements within the @try.
1955 WarnAboutReturnGotoStmts(S->getTryBody());
1956 } else { /* no finally clause - make sure we synthesize an implicit one */
1957 buf = "{ /* implicit finally clause */\n";
1958 buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n";
1959 buf += " if (_rethrow) objc_exception_throw(_rethrow);\n";
1961 ReplaceText(lastCurlyLoc, 1, buf);
1963 // Now check for any return/continue/go statements within the @try.
1964 // The implicit finally clause won't called if the @try contains any
1966 bool hasReturns = false;
1967 HasReturnStmts(S->getTryBody(), hasReturns);
1969 RewriteTryReturnStmts(S->getTryBody());
1971 // Now emit the final closing curly brace...
1972 lastCurlyLoc = lastCurlyLoc.getLocWithOffset(1);
1973 InsertText(lastCurlyLoc, " } /* @try scope end */\n");
1977 // This can't be done with ReplaceStmt(S, ThrowExpr), since
1978 // the throw expression is typically a message expression that's already
1979 // been rewritten! (which implies the SourceLocation's are invalid).
1980 Stmt *RewriteObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) {
1981 // Get the start location and compute the semi location.
1982 SourceLocation startLoc = S->getLocStart();
1983 const char *startBuf = SM->getCharacterData(startLoc);
1985 assert((*startBuf == '@') && "bogus @throw location");
1988 /* void objc_exception_throw(id) __attribute__((noreturn)); */
1989 if (S->getThrowExpr())
1990 buf = "objc_exception_throw(";
1991 else // add an implicit argument
1992 buf = "objc_exception_throw(_caught";
1994 // handle "@ throw" correctly.
1995 const char *wBuf = strchr(startBuf, 'w');
1996 assert((*wBuf == 'w') && "@throw: can't find 'w'");
1997 ReplaceText(startLoc, wBuf-startBuf+1, buf);
1999 const char *semiBuf = strchr(startBuf, ';');
2000 assert((*semiBuf == ';') && "@throw: can't find ';'");
2001 SourceLocation semiLoc = startLoc.getLocWithOffset(semiBuf-startBuf);
2002 ReplaceText(semiLoc, 1, ");");
2006 Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) {
2007 // Create a new string expression.
2008 QualType StrType = Context->getPointerType(Context->CharTy);
2009 std::string StrEncoding;
2010 Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding);
2011 Expr *Replacement = StringLiteral::Create(*Context, StrEncoding,
2012 StringLiteral::Ascii, false,
2013 StrType, SourceLocation());
2014 ReplaceStmt(Exp, Replacement);
2016 // Replace this subexpr in the parent.
2017 // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info.
2021 Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) {
2022 if (!SelGetUidFunctionDecl)
2023 SynthSelGetUidFunctionDecl();
2024 assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl");
2025 // Create a call to sel_registerName("selName").
2026 SmallVector<Expr*, 8> SelExprs;
2027 QualType argType = Context->getPointerType(Context->CharTy);
2028 SelExprs.push_back(StringLiteral::Create(*Context,
2029 Exp->getSelector().getAsString(),
2030 StringLiteral::Ascii, false,
2031 argType, SourceLocation()));
2032 CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl,
2033 &SelExprs[0], SelExprs.size());
2034 ReplaceStmt(Exp, SelExp);
2035 // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info.
2039 CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl(
2040 FunctionDecl *FD, Expr **args, unsigned nargs, SourceLocation StartLoc,
2041 SourceLocation EndLoc) {
2042 // Get the type, we will need to reference it in a couple spots.
2043 QualType msgSendType = FD->getType();
2045 // Create a reference to the objc_msgSend() declaration.
2046 DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, false, msgSendType,
2047 VK_LValue, SourceLocation());
2049 // Now, we cast the reference to a pointer to the objc_msgSend type.
2050 QualType pToFunc = Context->getPointerType(msgSendType);
2051 ImplicitCastExpr *ICE =
2052 ImplicitCastExpr::Create(*Context, pToFunc, CK_FunctionToPointerDecay,
2055 const FunctionType *FT = msgSendType->getAs<FunctionType>();
2058 new (Context) CallExpr(*Context, ICE, args, nargs,
2059 FT->getCallResultType(*Context),
2064 static bool scanForProtocolRefs(const char *startBuf, const char *endBuf,
2065 const char *&startRef, const char *&endRef) {
2066 while (startBuf < endBuf) {
2067 if (*startBuf == '<')
2068 startRef = startBuf; // mark the start.
2069 if (*startBuf == '>') {
2070 if (startRef && *startRef == '<') {
2071 endRef = startBuf; // mark the end.
2081 static void scanToNextArgument(const char *&argRef) {
2083 while (*argRef != ')' && (*argRef != ',' || angle > 0)) {
2086 else if (*argRef == '>')
2090 assert(angle == 0 && "scanToNextArgument - bad protocol type syntax");
2093 bool RewriteObjC::needToScanForQualifiers(QualType T) {
2094 if (T->isObjCQualifiedIdType())
2096 if (const PointerType *PT = T->getAs<PointerType>()) {
2097 if (PT->getPointeeType()->isObjCQualifiedIdType())
2100 if (T->isObjCObjectPointerType()) {
2101 T = T->getPointeeType();
2102 return T->isObjCQualifiedInterfaceType();
2104 if (T->isArrayType()) {
2105 QualType ElemTy = Context->getBaseElementType(T);
2106 return needToScanForQualifiers(ElemTy);
2111 void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) {
2112 QualType Type = E->getType();
2113 if (needToScanForQualifiers(Type)) {
2114 SourceLocation Loc, EndLoc;
2116 if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) {
2117 Loc = ECE->getLParenLoc();
2118 EndLoc = ECE->getRParenLoc();
2120 Loc = E->getLocStart();
2121 EndLoc = E->getLocEnd();
2123 // This will defend against trying to rewrite synthesized expressions.
2124 if (Loc.isInvalid() || EndLoc.isInvalid())
2127 const char *startBuf = SM->getCharacterData(Loc);
2128 const char *endBuf = SM->getCharacterData(EndLoc);
2129 const char *startRef = 0, *endRef = 0;
2130 if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) {
2131 // Get the locations of the startRef, endRef.
2132 SourceLocation LessLoc = Loc.getLocWithOffset(startRef-startBuf);
2133 SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-startBuf+1);
2134 // Comment out the protocol references.
2135 InsertText(LessLoc, "/*");
2136 InsertText(GreaterLoc, "*/");
2141 void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) {
2144 const FunctionProtoType *proto = 0;
2145 if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) {
2146 Loc = VD->getLocation();
2147 Type = VD->getType();
2149 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) {
2150 Loc = FD->getLocation();
2151 // Check for ObjC 'id' and class types that have been adorned with protocol
2152 // information (id<p>, C<p>*). The protocol references need to be rewritten!
2153 const FunctionType *funcType = FD->getType()->getAs<FunctionType>();
2154 assert(funcType && "missing function type");
2155 proto = dyn_cast<FunctionProtoType>(funcType);
2158 Type = proto->getResultType();
2160 else if (FieldDecl *FD = dyn_cast<FieldDecl>(Dcl)) {
2161 Loc = FD->getLocation();
2162 Type = FD->getType();
2167 if (needToScanForQualifiers(Type)) {
2168 // Since types are unique, we need to scan the buffer.
2170 const char *endBuf = SM->getCharacterData(Loc);
2171 const char *startBuf = endBuf;
2172 while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart)
2173 startBuf--; // scan backward (from the decl location) for return type.
2174 const char *startRef = 0, *endRef = 0;
2175 if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) {
2176 // Get the locations of the startRef, endRef.
2177 SourceLocation LessLoc = Loc.getLocWithOffset(startRef-endBuf);
2178 SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-endBuf+1);
2179 // Comment out the protocol references.
2180 InsertText(LessLoc, "/*");
2181 InsertText(GreaterLoc, "*/");
2185 return; // most likely, was a variable
2186 // Now check arguments.
2187 const char *startBuf = SM->getCharacterData(Loc);
2188 const char *startFuncBuf = startBuf;
2189 for (unsigned i = 0; i < proto->getNumArgs(); i++) {
2190 if (needToScanForQualifiers(proto->getArgType(i))) {
2191 // Since types are unique, we need to scan the buffer.
2193 const char *endBuf = startBuf;
2194 // scan forward (from the decl location) for argument types.
2195 scanToNextArgument(endBuf);
2196 const char *startRef = 0, *endRef = 0;
2197 if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) {
2198 // Get the locations of the startRef, endRef.
2199 SourceLocation LessLoc =
2200 Loc.getLocWithOffset(startRef-startFuncBuf);
2201 SourceLocation GreaterLoc =
2202 Loc.getLocWithOffset(endRef-startFuncBuf+1);
2203 // Comment out the protocol references.
2204 InsertText(LessLoc, "/*");
2205 InsertText(GreaterLoc, "*/");
2207 startBuf = ++endBuf;
2210 // If the function name is derived from a macro expansion, then the
2211 // argument buffer will not follow the name. Need to speak with Chris.
2212 while (*startBuf && *startBuf != ')' && *startBuf != ',')
2213 startBuf++; // scan forward (from the decl location) for argument types.
2219 void RewriteObjC::RewriteTypeOfDecl(VarDecl *ND) {
2220 QualType QT = ND->getType();
2221 const Type* TypePtr = QT->getAs<Type>();
2222 if (!isa<TypeOfExprType>(TypePtr))
2224 while (isa<TypeOfExprType>(TypePtr)) {
2225 const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr);
2226 QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType();
2227 TypePtr = QT->getAs<Type>();
2229 // FIXME. This will not work for multiple declarators; as in:
2230 // __typeof__(a) b,c,d;
2231 std::string TypeAsString(QT.getAsString(Context->getPrintingPolicy()));
2232 SourceLocation DeclLoc = ND->getTypeSpecStartLoc();
2233 const char *startBuf = SM->getCharacterData(DeclLoc);
2234 if (ND->getInit()) {
2235 std::string Name(ND->getNameAsString());
2236 TypeAsString += " " + Name + " = ";
2237 Expr *E = ND->getInit();
2238 SourceLocation startLoc;
2239 if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E))
2240 startLoc = ECE->getLParenLoc();
2242 startLoc = E->getLocStart();
2243 startLoc = SM->getExpansionLoc(startLoc);
2244 const char *endBuf = SM->getCharacterData(startLoc);
2245 ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString);
2248 SourceLocation X = ND->getLocEnd();
2249 X = SM->getExpansionLoc(X);
2250 const char *endBuf = SM->getCharacterData(X);
2251 ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString);
2255 // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str);
2256 void RewriteObjC::SynthSelGetUidFunctionDecl() {
2257 IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName");
2258 SmallVector<QualType, 16> ArgTys;
2259 ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst()));
2260 QualType getFuncType =
2261 getSimpleFunctionType(Context->getObjCSelType(), &ArgTys[0], ArgTys.size());
2262 SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
2265 SelGetUidIdent, getFuncType, 0,
2270 void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) {
2271 // declared in <objc/objc.h>
2272 if (FD->getIdentifier() &&
2273 FD->getName() == "sel_registerName") {
2274 SelGetUidFunctionDecl = FD;
2277 RewriteObjCQualifiedInterfaceTypes(FD);
2280 void RewriteObjC::RewriteBlockPointerType(std::string& Str, QualType Type) {
2281 std::string TypeString(Type.getAsString(Context->getPrintingPolicy()));
2282 const char *argPtr = TypeString.c_str();
2283 if (!strchr(argPtr, '^')) {
2288 Str += (*argPtr == '^' ? '*' : *argPtr);
2293 // FIXME. Consolidate this routine with RewriteBlockPointerType.
2294 void RewriteObjC::RewriteBlockPointerTypeVariable(std::string& Str,
2296 QualType Type = VD->getType();
2297 std::string TypeString(Type.getAsString(Context->getPrintingPolicy()));
2298 const char *argPtr = TypeString.c_str();
2313 Str += VD->getNameAsString();
2324 void RewriteObjC::RewriteBlockLiteralFunctionDecl(FunctionDecl *FD) {
2325 SourceLocation FunLocStart = FD->getTypeSpecStartLoc();
2326 const FunctionType *funcType = FD->getType()->getAs<FunctionType>();
2327 const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(funcType);
2330 QualType Type = proto->getResultType();
2331 std::string FdStr = Type.getAsString(Context->getPrintingPolicy());
2333 FdStr += FD->getName();
2335 unsigned numArgs = proto->getNumArgs();
2336 for (unsigned i = 0; i < numArgs; i++) {
2337 QualType ArgType = proto->getArgType(i);
2338 RewriteBlockPointerType(FdStr, ArgType);
2343 InsertText(FunLocStart, FdStr);
2344 CurFunctionDeclToDeclareForBlock = 0;
2347 // SynthSuperContructorFunctionDecl - id objc_super(id obj, id super);
2348 void RewriteObjC::SynthSuperContructorFunctionDecl() {
2349 if (SuperContructorFunctionDecl)
2351 IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super");
2352 SmallVector<QualType, 16> ArgTys;
2353 QualType argT = Context->getObjCIdType();
2354 assert(!argT.isNull() && "Can't find 'id' type");
2355 ArgTys.push_back(argT);
2356 ArgTys.push_back(argT);
2357 QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(),
2358 &ArgTys[0], ArgTys.size());
2359 SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
2362 msgSendIdent, msgSendType, 0,
2367 // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...);
2368 void RewriteObjC::SynthMsgSendFunctionDecl() {
2369 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend");
2370 SmallVector<QualType, 16> ArgTys;
2371 QualType argT = Context->getObjCIdType();
2372 assert(!argT.isNull() && "Can't find 'id' type");
2373 ArgTys.push_back(argT);
2374 argT = Context->getObjCSelType();
2375 assert(!argT.isNull() && "Can't find 'SEL' type");
2376 ArgTys.push_back(argT);
2377 QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(),
2378 &ArgTys[0], ArgTys.size(),
2379 true /*isVariadic*/);
2380 MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
2383 msgSendIdent, msgSendType, 0,
2388 // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...);
2389 void RewriteObjC::SynthMsgSendSuperFunctionDecl() {
2390 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper");
2391 SmallVector<QualType, 16> ArgTys;
2392 RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl,
2393 SourceLocation(), SourceLocation(),
2394 &Context->Idents.get("objc_super"));
2395 QualType argT = Context->getPointerType(Context->getTagDeclType(RD));
2396 assert(!argT.isNull() && "Can't build 'struct objc_super *' type");
2397 ArgTys.push_back(argT);
2398 argT = Context->getObjCSelType();
2399 assert(!argT.isNull() && "Can't find 'SEL' type");
2400 ArgTys.push_back(argT);
2401 QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(),
2402 &ArgTys[0], ArgTys.size(),
2403 true /*isVariadic*/);
2404 MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
2407 msgSendIdent, msgSendType, 0,
2412 // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...);
2413 void RewriteObjC::SynthMsgSendStretFunctionDecl() {
2414 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret");
2415 SmallVector<QualType, 16> ArgTys;
2416 QualType argT = Context->getObjCIdType();
2417 assert(!argT.isNull() && "Can't find 'id' type");
2418 ArgTys.push_back(argT);
2419 argT = Context->getObjCSelType();
2420 assert(!argT.isNull() && "Can't find 'SEL' type");
2421 ArgTys.push_back(argT);
2422 QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(),
2423 &ArgTys[0], ArgTys.size(),
2424 true /*isVariadic*/);
2425 MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
2428 msgSendIdent, msgSendType, 0,
2433 // SynthMsgSendSuperStretFunctionDecl -
2434 // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...);
2435 void RewriteObjC::SynthMsgSendSuperStretFunctionDecl() {
2436 IdentifierInfo *msgSendIdent =
2437 &Context->Idents.get("objc_msgSendSuper_stret");
2438 SmallVector<QualType, 16> ArgTys;
2439 RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl,
2440 SourceLocation(), SourceLocation(),
2441 &Context->Idents.get("objc_super"));
2442 QualType argT = Context->getPointerType(Context->getTagDeclType(RD));
2443 assert(!argT.isNull() && "Can't build 'struct objc_super *' type");
2444 ArgTys.push_back(argT);
2445 argT = Context->getObjCSelType();
2446 assert(!argT.isNull() && "Can't find 'SEL' type");
2447 ArgTys.push_back(argT);
2448 QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(),
2449 &ArgTys[0], ArgTys.size(),
2450 true /*isVariadic*/);
2451 MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
2454 msgSendIdent, msgSendType, 0,
2459 // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...);
2460 void RewriteObjC::SynthMsgSendFpretFunctionDecl() {
2461 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret");
2462 SmallVector<QualType, 16> ArgTys;
2463 QualType argT = Context->getObjCIdType();
2464 assert(!argT.isNull() && "Can't find 'id' type");
2465 ArgTys.push_back(argT);
2466 argT = Context->getObjCSelType();
2467 assert(!argT.isNull() && "Can't find 'SEL' type");
2468 ArgTys.push_back(argT);
2469 QualType msgSendType = getSimpleFunctionType(Context->DoubleTy,
2470 &ArgTys[0], ArgTys.size(),
2471 true /*isVariadic*/);
2472 MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
2475 msgSendIdent, msgSendType, 0,
2480 // SynthGetClassFunctionDecl - id objc_getClass(const char *name);
2481 void RewriteObjC::SynthGetClassFunctionDecl() {
2482 IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass");
2483 SmallVector<QualType, 16> ArgTys;
2484 ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst()));
2485 QualType getClassType = getSimpleFunctionType(Context->getObjCIdType(),
2486 &ArgTys[0], ArgTys.size());
2487 GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
2490 getClassIdent, getClassType, 0,
2495 // SynthGetSuperClassFunctionDecl - Class class_getSuperclass(Class cls);
2496 void RewriteObjC::SynthGetSuperClassFunctionDecl() {
2497 IdentifierInfo *getSuperClassIdent =
2498 &Context->Idents.get("class_getSuperclass");
2499 SmallVector<QualType, 16> ArgTys;
2500 ArgTys.push_back(Context->getObjCClassType());
2501 QualType getClassType = getSimpleFunctionType(Context->getObjCClassType(),
2502 &ArgTys[0], ArgTys.size());
2503 GetSuperClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
2513 // SynthGetMetaClassFunctionDecl - id objc_getMetaClass(const char *name);
2514 void RewriteObjC::SynthGetMetaClassFunctionDecl() {
2515 IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass");
2516 SmallVector<QualType, 16> ArgTys;
2517 ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst()));
2518 QualType getClassType = getSimpleFunctionType(Context->getObjCIdType(),
2519 &ArgTys[0], ArgTys.size());
2520 GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
2523 getClassIdent, getClassType, 0,
2528 Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) {
2529 QualType strType = getConstantStringStructType();
2531 std::string S = "__NSConstantStringImpl_";
2533 std::string tmpName = InFileName;
2535 for (i=0; i < tmpName.length(); i++) {
2536 char c = tmpName.at(i);
2537 // replace any non alphanumeric characters with '_'.
2538 if (!isalpha(c) && (c < '0' || c > '9'))
2543 S += utostr(NumObjCStringLiterals++);
2545 Preamble += "static __NSConstantStringImpl " + S;
2546 Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,";
2547 Preamble += "0x000007c8,"; // utf8_str
2548 // The pretty printer for StringLiteral handles escape characters properly.
2549 std::string prettyBufS;
2550 llvm::raw_string_ostream prettyBuf(prettyBufS);
2551 Exp->getString()->printPretty(prettyBuf, *Context, 0,
2552 PrintingPolicy(LangOpts));
2553 Preamble += prettyBuf.str();
2555 Preamble += utostr(Exp->getString()->getByteLength()) + "};\n";
2557 VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(),
2558 SourceLocation(), &Context->Idents.get(S),
2559 strType, 0, SC_Static, SC_None);
2560 DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, false, strType, VK_LValue,
2562 Expr *Unop = new (Context) UnaryOperator(DRE, UO_AddrOf,
2563 Context->getPointerType(DRE->getType()),
2564 VK_RValue, OK_Ordinary,
2566 // cast to NSConstantString *
2567 CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Exp->getType(),
2568 CK_CPointerToObjCPointerCast, Unop);
2569 ReplaceStmt(Exp, cast);
2570 // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info.
2574 // struct objc_super { struct objc_object *receiver; struct objc_class *super; };
2575 QualType RewriteObjC::getSuperStructType() {
2576 if (!SuperStructDecl) {
2577 SuperStructDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl,
2578 SourceLocation(), SourceLocation(),
2579 &Context->Idents.get("objc_super"));
2580 QualType FieldTypes[2];
2582 // struct objc_object *receiver;
2583 FieldTypes[0] = Context->getObjCIdType();
2584 // struct objc_class *super;
2585 FieldTypes[1] = Context->getObjCClassType();
2588 for (unsigned i = 0; i < 2; ++i) {
2589 SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl,
2591 SourceLocation(), 0,
2595 /*HasInit=*/false));
2598 SuperStructDecl->completeDefinition();
2600 return Context->getTagDeclType(SuperStructDecl);
2603 QualType RewriteObjC::getConstantStringStructType() {
2604 if (!ConstantStringDecl) {
2605 ConstantStringDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl,
2606 SourceLocation(), SourceLocation(),
2607 &Context->Idents.get("__NSConstantStringImpl"));
2608 QualType FieldTypes[4];
2610 // struct objc_object *receiver;
2611 FieldTypes[0] = Context->getObjCIdType();
2613 FieldTypes[1] = Context->IntTy;
2615 FieldTypes[2] = Context->getPointerType(Context->CharTy);
2617 FieldTypes[3] = Context->LongTy;
2620 for (unsigned i = 0; i < 4; ++i) {
2621 ConstantStringDecl->addDecl(FieldDecl::Create(*Context,
2624 SourceLocation(), 0,
2628 /*HasInit=*/false));
2631 ConstantStringDecl->completeDefinition();
2633 return Context->getTagDeclType(ConstantStringDecl);
2636 Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp,
2637 SourceLocation StartLoc,
2638 SourceLocation EndLoc) {
2639 if (!SelGetUidFunctionDecl)
2640 SynthSelGetUidFunctionDecl();
2641 if (!MsgSendFunctionDecl)
2642 SynthMsgSendFunctionDecl();
2643 if (!MsgSendSuperFunctionDecl)
2644 SynthMsgSendSuperFunctionDecl();
2645 if (!MsgSendStretFunctionDecl)
2646 SynthMsgSendStretFunctionDecl();
2647 if (!MsgSendSuperStretFunctionDecl)
2648 SynthMsgSendSuperStretFunctionDecl();
2649 if (!MsgSendFpretFunctionDecl)
2650 SynthMsgSendFpretFunctionDecl();
2651 if (!GetClassFunctionDecl)
2652 SynthGetClassFunctionDecl();
2653 if (!GetSuperClassFunctionDecl)
2654 SynthGetSuperClassFunctionDecl();
2655 if (!GetMetaClassFunctionDecl)
2656 SynthGetMetaClassFunctionDecl();
2658 // default to objc_msgSend().
2659 FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl;
2660 // May need to use objc_msgSend_stret() as well.
2661 FunctionDecl *MsgSendStretFlavor = 0;
2662 if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) {
2663 QualType resultType = mDecl->getResultType();
2664 if (resultType->isRecordType())
2665 MsgSendStretFlavor = MsgSendStretFunctionDecl;
2666 else if (resultType->isRealFloatingType())
2667 MsgSendFlavor = MsgSendFpretFunctionDecl;
2670 // Synthesize a call to objc_msgSend().
2671 SmallVector<Expr*, 8> MsgExprs;
2672 switch (Exp->getReceiverKind()) {
2673 case ObjCMessageExpr::SuperClass: {
2674 MsgSendFlavor = MsgSendSuperFunctionDecl;
2675 if (MsgSendStretFlavor)
2676 MsgSendStretFlavor = MsgSendSuperStretFunctionDecl;
2677 assert(MsgSendFlavor && "MsgSendFlavor is NULL!");
2679 ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface();
2681 SmallVector<Expr*, 4> InitExprs;
2683 // set the receiver to self, the first argument to all methods.
2684 InitExprs.push_back(
2685 NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(),
2687 new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(),
2689 Context->getObjCIdType(),
2692 ); // set the 'receiver'.
2694 // (id)class_getSuperclass((Class)objc_getClass("CurrentClass"))
2695 SmallVector<Expr*, 8> ClsExprs;
2696 QualType argType = Context->getPointerType(Context->CharTy);
2697 ClsExprs.push_back(StringLiteral::Create(*Context,
2698 ClassDecl->getIdentifier()->getName(),
2699 StringLiteral::Ascii, false,
2700 argType, SourceLocation()));
2701 CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl,
2706 // (Class)objc_getClass("CurrentClass")
2707 CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context,
2708 Context->getObjCClassType(),
2711 ClsExprs.push_back(ArgExpr);
2712 Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl,
2713 &ClsExprs[0], ClsExprs.size(),
2716 // (id)class_getSuperclass((Class)objc_getClass("CurrentClass"))
2717 // To turn off a warning, type-cast to 'id'
2718 InitExprs.push_back( // set 'super class', using class_getSuperclass().
2719 NoTypeInfoCStyleCastExpr(Context,
2720 Context->getObjCIdType(),
2722 // struct objc_super
2723 QualType superType = getSuperStructType();
2726 if (LangOpts.MicrosoftExt) {
2727 SynthSuperContructorFunctionDecl();
2728 // Simulate a contructor call...
2729 DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl,
2730 false, superType, VK_LValue,
2732 SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0],
2734 superType, VK_LValue,
2736 // The code for super is a little tricky to prevent collision with
2737 // the structure definition in the header. The rewriter has it's own
2738 // internal definition (__rw_objc_super) that is uses. This is why
2739 // we need the cast below. For example:
2740 // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER"))
2742 SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf,
2743 Context->getPointerType(SuperRep->getType()),
2744 VK_RValue, OK_Ordinary,
2746 SuperRep = NoTypeInfoCStyleCastExpr(Context,
2747 Context->getPointerType(superType),
2748 CK_BitCast, SuperRep);
2750 // (struct objc_super) { <exprs from above> }
2752 new (Context) InitListExpr(*Context, SourceLocation(),
2753 &InitExprs[0], InitExprs.size(),
2755 TypeSourceInfo *superTInfo
2756 = Context->getTrivialTypeSourceInfo(superType);
2757 SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo,
2758 superType, VK_LValue,
2760 // struct objc_super *
2761 SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf,
2762 Context->getPointerType(SuperRep->getType()),
2763 VK_RValue, OK_Ordinary,
2766 MsgExprs.push_back(SuperRep);
2770 case ObjCMessageExpr::Class: {
2771 SmallVector<Expr*, 8> ClsExprs;
2772 QualType argType = Context->getPointerType(Context->CharTy);
2773 ObjCInterfaceDecl *Class
2774 = Exp->getClassReceiver()->getAs<ObjCObjectType>()->getInterface();
2775 IdentifierInfo *clsName = Class->getIdentifier();
2776 ClsExprs.push_back(StringLiteral::Create(*Context,
2778 StringLiteral::Ascii, false,
2779 argType, SourceLocation()));
2780 CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl,
2784 MsgExprs.push_back(Cls);
2788 case ObjCMessageExpr::SuperInstance:{
2789 MsgSendFlavor = MsgSendSuperFunctionDecl;
2790 if (MsgSendStretFlavor)
2791 MsgSendStretFlavor = MsgSendSuperStretFunctionDecl;
2792 assert(MsgSendFlavor && "MsgSendFlavor is NULL!");
2793 ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface();
2794 SmallVector<Expr*, 4> InitExprs;
2796 InitExprs.push_back(
2797 NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(),
2799 new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(),
2801 Context->getObjCIdType(),
2802 VK_RValue, SourceLocation()))
2803 ); // set the 'receiver'.
2805 // (id)class_getSuperclass((Class)objc_getClass("CurrentClass"))
2806 SmallVector<Expr*, 8> ClsExprs;
2807 QualType argType = Context->getPointerType(Context->CharTy);
2808 ClsExprs.push_back(StringLiteral::Create(*Context,
2809 ClassDecl->getIdentifier()->getName(),
2810 StringLiteral::Ascii, false, argType,
2812 CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl,
2816 // (Class)objc_getClass("CurrentClass")
2817 CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context,
2818 Context->getObjCClassType(),
2821 ClsExprs.push_back(ArgExpr);
2822 Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl,
2823 &ClsExprs[0], ClsExprs.size(),
2826 // (id)class_getSuperclass((Class)objc_getClass("CurrentClass"))
2827 // To turn off a warning, type-cast to 'id'
2828 InitExprs.push_back(
2829 // set 'super class', using class_getSuperclass().
2830 NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(),
2832 // struct objc_super
2833 QualType superType = getSuperStructType();
2836 if (LangOpts.MicrosoftExt) {
2837 SynthSuperContructorFunctionDecl();
2838 // Simulate a contructor call...
2839 DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl,
2840 false, superType, VK_LValue,
2842 SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0],
2844 superType, VK_LValue, SourceLocation());
2845 // The code for super is a little tricky to prevent collision with
2846 // the structure definition in the header. The rewriter has it's own
2847 // internal definition (__rw_objc_super) that is uses. This is why
2848 // we need the cast below. For example:
2849 // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER"))
2851 SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf,
2852 Context->getPointerType(SuperRep->getType()),
2853 VK_RValue, OK_Ordinary,
2855 SuperRep = NoTypeInfoCStyleCastExpr(Context,
2856 Context->getPointerType(superType),
2857 CK_BitCast, SuperRep);
2859 // (struct objc_super) { <exprs from above> }
2861 new (Context) InitListExpr(*Context, SourceLocation(),
2862 &InitExprs[0], InitExprs.size(),
2864 TypeSourceInfo *superTInfo
2865 = Context->getTrivialTypeSourceInfo(superType);
2866 SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo,
2867 superType, VK_RValue, ILE,
2870 MsgExprs.push_back(SuperRep);
2874 case ObjCMessageExpr::Instance: {
2875 // Remove all type-casts because it may contain objc-style types; e.g.
2877 Expr *recExpr = Exp->getInstanceReceiver();
2878 while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr))
2879 recExpr = CE->getSubExpr();
2880 CastKind CK = recExpr->getType()->isObjCObjectPointerType()
2881 ? CK_BitCast : recExpr->getType()->isBlockPointerType()
2882 ? CK_BlockPointerToObjCPointerCast
2883 : CK_CPointerToObjCPointerCast;
2885 recExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(),
2887 MsgExprs.push_back(recExpr);
2892 // Create a call to sel_registerName("selName"), it will be the 2nd argument.
2893 SmallVector<Expr*, 8> SelExprs;
2894 QualType argType = Context->getPointerType(Context->CharTy);
2895 SelExprs.push_back(StringLiteral::Create(*Context,
2896 Exp->getSelector().getAsString(),
2897 StringLiteral::Ascii, false,
2898 argType, SourceLocation()));
2899 CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl,
2900 &SelExprs[0], SelExprs.size(),
2903 MsgExprs.push_back(SelExp);
2905 // Now push any user supplied arguments.
2906 for (unsigned i = 0; i < Exp->getNumArgs(); i++) {
2907 Expr *userExpr = Exp->getArg(i);
2908 // Make all implicit casts explicit...ICE comes in handy:-)
2909 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) {
2910 // Reuse the ICE type, it is exactly what the doctor ordered.
2911 QualType type = ICE->getType();
2912 if (needToScanForQualifiers(type))
2913 type = Context->getObjCIdType();
2914 // Make sure we convert "type (^)(...)" to "type (*)(...)".
2915 (void)convertBlockPointerToFunctionPointer(type);
2916 const Expr *SubExpr = ICE->IgnoreParenImpCasts();
2918 if (SubExpr->getType()->isIntegralType(*Context) &&
2919 type->isBooleanType()) {
2920 CK = CK_IntegralToBoolean;
2921 } else if (type->isObjCObjectPointerType()) {
2922 if (SubExpr->getType()->isBlockPointerType()) {
2923 CK = CK_BlockPointerToObjCPointerCast;
2924 } else if (SubExpr->getType()->isPointerType()) {
2925 CK = CK_CPointerToObjCPointerCast;
2933 userExpr = NoTypeInfoCStyleCastExpr(Context, type, CK, userExpr);
2935 // Make id<P...> cast into an 'id' cast.
2936 else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) {
2937 if (CE->getType()->isObjCQualifiedIdType()) {
2938 while ((CE = dyn_cast<CStyleCastExpr>(userExpr)))
2939 userExpr = CE->getSubExpr();
2941 if (userExpr->getType()->isIntegralType(*Context)) {
2942 CK = CK_IntegralToPointer;
2943 } else if (userExpr->getType()->isBlockPointerType()) {
2944 CK = CK_BlockPointerToObjCPointerCast;
2945 } else if (userExpr->getType()->isPointerType()) {
2946 CK = CK_CPointerToObjCPointerCast;
2950 userExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(),
2954 MsgExprs.push_back(userExpr);
2955 // We've transferred the ownership to MsgExprs. For now, we *don't* null
2956 // out the argument in the original expression (since we aren't deleting
2957 // the ObjCMessageExpr). See RewritePropertyOrImplicitSetter() usage for more info.
2958 //Exp->setArg(i, 0);
2960 // Generate the funky cast.
2962 SmallVector<QualType, 8> ArgTypes;
2963 QualType returnType;
2965 // Push 'id' and 'SEL', the 2 implicit arguments.
2966 if (MsgSendFlavor == MsgSendSuperFunctionDecl)
2967 ArgTypes.push_back(Context->getPointerType(getSuperStructType()));
2969 ArgTypes.push_back(Context->getObjCIdType());
2970 ArgTypes.push_back(Context->getObjCSelType());
2971 if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) {
2972 // Push any user argument types.
2973 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
2974 E = OMD->param_end(); PI != E; ++PI) {
2975 QualType t = (*PI)->getType()->isObjCQualifiedIdType()
2976 ? Context->getObjCIdType()
2978 // Make sure we convert "t (^)(...)" to "t (*)(...)".
2979 (void)convertBlockPointerToFunctionPointer(t);
2980 ArgTypes.push_back(t);
2982 returnType = Exp->getType();
2983 convertToUnqualifiedObjCType(returnType);
2984 (void)convertBlockPointerToFunctionPointer(returnType);
2986 returnType = Context->getObjCIdType();
2988 // Get the type, we will need to reference it in a couple spots.
2989 QualType msgSendType = MsgSendFlavor->getType();
2991 // Create a reference to the objc_msgSend() declaration.
2992 DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, false, msgSendType,
2993 VK_LValue, SourceLocation());
2995 // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid).
2996 // If we don't do this cast, we get the following bizarre warning/note:
2997 // xx.m:13: warning: function called through a non-compatible type
2998 // xx.m:13: note: if this code is reached, the program will abort
2999 cast = NoTypeInfoCStyleCastExpr(Context,
3000 Context->getPointerType(Context->VoidTy),
3003 // Now do the "normal" pointer to function cast.
3005 getSimpleFunctionType(returnType, &ArgTypes[0], ArgTypes.size(),
3006 // If we don't have a method decl, force a variadic cast.
3007 Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true);
3008 castType = Context->getPointerType(castType);
3009 cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast,
3012 // Don't forget the parens to enforce the proper binding.
3013 ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast);
3015 const FunctionType *FT = msgSendType->getAs<FunctionType>();
3016 CallExpr *CE = new (Context) CallExpr(*Context, PE, &MsgExprs[0],
3018 FT->getResultType(), VK_RValue,
3020 Stmt *ReplacingStmt = CE;
3021 if (MsgSendStretFlavor) {
3022 // We have the method which returns a struct/union. Must also generate
3023 // call to objc_msgSend_stret and hang both varieties on a conditional
3024 // expression which dictate which one to envoke depending on size of
3025 // method's return type.
3027 // Create a reference to the objc_msgSend_stret() declaration.
3028 DeclRefExpr *STDRE = new (Context) DeclRefExpr(MsgSendStretFlavor,
3030 VK_LValue, SourceLocation());
3031 // Need to cast objc_msgSend_stret to "void *" (see above comment).
3032 cast = NoTypeInfoCStyleCastExpr(Context,
3033 Context->getPointerType(Context->VoidTy),
3035 // Now do the "normal" pointer to function cast.
3036 castType = getSimpleFunctionType(returnType, &ArgTypes[0], ArgTypes.size(),
3037 Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false);
3038 castType = Context->getPointerType(castType);
3039 cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast,
3042 // Don't forget the parens to enforce the proper binding.
3043 PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast);
3045 FT = msgSendType->getAs<FunctionType>();
3046 CallExpr *STCE = new (Context) CallExpr(*Context, PE, &MsgExprs[0],
3048 FT->getResultType(), VK_RValue,
3051 // Build sizeof(returnType)
3052 UnaryExprOrTypeTraitExpr *sizeofExpr =
3053 new (Context) UnaryExprOrTypeTraitExpr(UETT_SizeOf,
3054 Context->getTrivialTypeSourceInfo(returnType),
3055 Context->getSizeType(), SourceLocation(),
3057 // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...))
3058 // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases.
3059 // For X86 it is more complicated and some kind of target specific routine
3060 // is needed to decide what to do.
3062 static_cast<unsigned>(Context->getTypeSize(Context->IntTy));
3063 IntegerLiteral *limit = IntegerLiteral::Create(*Context,
3064 llvm::APInt(IntSize, 8),
3067 BinaryOperator *lessThanExpr =
3068 new (Context) BinaryOperator(sizeofExpr, limit, BO_LE, Context->IntTy,
3069 VK_RValue, OK_Ordinary, SourceLocation());
3070 // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...))
3071 ConditionalOperator *CondExpr =
3072 new (Context) ConditionalOperator(lessThanExpr,
3073 SourceLocation(), CE,
3074 SourceLocation(), STCE,
3075 returnType, VK_RValue, OK_Ordinary);
3076 ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(),
3079 // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info.
3080 return ReplacingStmt;
3083 Stmt *RewriteObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) {
3084 Stmt *ReplacingStmt = SynthMessageExpr(Exp, Exp->getLocStart(),
3087 // Now do the actual rewrite.
3088 ReplaceStmt(Exp, ReplacingStmt);
3090 // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info.
3091 return ReplacingStmt;
3094 // typedef struct objc_object Protocol;
3095 QualType RewriteObjC::getProtocolType() {
3096 if (!ProtocolTypeDecl) {
3097 TypeSourceInfo *TInfo
3098 = Context->getTrivialTypeSourceInfo(Context->getObjCIdType());
3099 ProtocolTypeDecl = TypedefDecl::Create(*Context, TUDecl,
3100 SourceLocation(), SourceLocation(),
3101 &Context->Idents.get("Protocol"),
3104 return Context->getTypeDeclType(ProtocolTypeDecl);
3107 /// RewriteObjCProtocolExpr - Rewrite a protocol expression into
3108 /// a synthesized/forward data reference (to the protocol's metadata).
3109 /// The forward references (and metadata) are generated in
3110 /// RewriteObjC::HandleTranslationUnit().
3111 Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) {
3112 std::string Name = "_OBJC_PROTOCOL_" + Exp->getProtocol()->getNameAsString();
3113 IdentifierInfo *ID = &Context->Idents.get(Name);
3114 VarDecl *VD = VarDecl::Create(*Context, TUDecl, SourceLocation(),
3115 SourceLocation(), ID, getProtocolType(), 0,
3116 SC_Extern, SC_None);
3117 DeclRefExpr *DRE = new (Context) DeclRefExpr(VD, false, getProtocolType(),
3118 VK_LValue, SourceLocation());
3119 Expr *DerefExpr = new (Context) UnaryOperator(DRE, UO_AddrOf,
3120 Context->getPointerType(DRE->getType()),
3121 VK_RValue, OK_Ordinary, SourceLocation());
3122 CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, DerefExpr->getType(),
3125 ReplaceStmt(Exp, castExpr);
3126 ProtocolExprDecls.insert(Exp->getProtocol()->getCanonicalDecl());
3127 // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info.
3132 bool RewriteObjC::BufferContainsPPDirectives(const char *startBuf,
3133 const char *endBuf) {
3134 while (startBuf < endBuf) {
3135 if (*startBuf == '#') {
3137 for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf)
3139 if (!strncmp(startBuf, "if", strlen("if")) ||
3140 !strncmp(startBuf, "ifdef", strlen("ifdef")) ||
3141 !strncmp(startBuf, "ifndef", strlen("ifndef")) ||
3142 !strncmp(startBuf, "define", strlen("define")) ||
3143 !strncmp(startBuf, "undef", strlen("undef")) ||
3144 !strncmp(startBuf, "else", strlen("else")) ||
3145 !strncmp(startBuf, "elif", strlen("elif")) ||
3146 !strncmp(startBuf, "endif", strlen("endif")) ||
3147 !strncmp(startBuf, "pragma", strlen("pragma")) ||
3148 !strncmp(startBuf, "include", strlen("include")) ||
3149 !strncmp(startBuf, "import", strlen("import")) ||
3150 !strncmp(startBuf, "include_next", strlen("include_next")))
3158 /// RewriteObjCInternalStruct - Rewrite one internal struct corresponding to
3159 /// an objective-c class with ivars.
3160 void RewriteObjC::RewriteObjCInternalStruct(ObjCInterfaceDecl *CDecl,
3161 std::string &Result) {
3162 assert(CDecl && "Class missing in SynthesizeObjCInternalStruct");
3163 assert(CDecl->getName() != "" &&
3164 "Name missing in SynthesizeObjCInternalStruct");
3165 // Do not synthesize more than once.
3166 if (ObjCSynthesizedStructs.count(CDecl))
3168 ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass();
3169 int NumIvars = CDecl->ivar_size();
3170 SourceLocation LocStart = CDecl->getLocStart();
3171 SourceLocation LocEnd = CDecl->getEndOfDefinitionLoc();
3173 const char *startBuf = SM->getCharacterData(LocStart);
3174 const char *endBuf = SM->getCharacterData(LocEnd);
3176 // If no ivars and no root or if its root, directly or indirectly,
3177 // have no ivars (thus not synthesized) then no need to synthesize this class.
3178 if ((!CDecl->isThisDeclarationADefinition() || NumIvars == 0) &&
3179 (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) {
3180 endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts);
3181 ReplaceText(LocStart, endBuf-startBuf, Result);
3185 // FIXME: This has potential of causing problem. If
3186 // SynthesizeObjCInternalStruct is ever called recursively.
3187 Result += "\nstruct ";
3188 Result += CDecl->getNameAsString();
3189 if (LangOpts.MicrosoftExt)
3193 const char *cursor = strchr(startBuf, '{');
3194 assert((cursor && endBuf)
3195 && "SynthesizeObjCInternalStruct - malformed @interface");
3196 // If the buffer contains preprocessor directives, we do more fine-grained
3197 // rewrites. This is intended to fix code that looks like (which occurs in
3198 // NSURL.h, for example):
3201 // @interface Foo : NSObject
3203 // @interface FooBar : NSObject
3210 // This clause is segregated to avoid breaking the common case.
3211 if (BufferContainsPPDirectives(startBuf, cursor)) {
3212 SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() :
3213 CDecl->getAtStartLoc();
3214 const char *endHeader = SM->getCharacterData(L);
3215 endHeader += Lexer::MeasureTokenLength(L, *SM, LangOpts);
3217 if (CDecl->protocol_begin() != CDecl->protocol_end()) {
3218 // advance to the end of the referenced protocols.
3219 while (endHeader < cursor && *endHeader != '>') endHeader++;
3222 // rewrite the original header
3223 ReplaceText(LocStart, endHeader-startBuf, Result);
3225 // rewrite the original header *without* disturbing the '{'
3226 ReplaceText(LocStart, cursor-startBuf, Result);
3228 if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) {
3229 Result = "\n struct ";
3230 Result += RCDecl->getNameAsString();
3232 Result += RCDecl->getNameAsString();
3233 Result += "_IVARS;\n";
3235 // insert the super class structure definition.
3236 SourceLocation OnePastCurly =
3237 LocStart.getLocWithOffset(cursor-startBuf+1);
3238 InsertText(OnePastCurly, Result);
3240 cursor++; // past '{'
3242 // Now comment out any visibility specifiers.
3243 while (cursor < endBuf) {
3244 if (*cursor == '@') {
3245 SourceLocation atLoc = LocStart.getLocWithOffset(cursor-startBuf);
3247 for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor)
3250 // FIXME: presence of @public, etc. inside comment results in
3251 // this transformation as well, which is still correct c-code.
3252 if (!strncmp(cursor, "public", strlen("public")) ||
3253 !strncmp(cursor, "private", strlen("private")) ||
3254 !strncmp(cursor, "package", strlen("package")) ||
3255 !strncmp(cursor, "protected", strlen("protected")))
3256 InsertText(atLoc, "// ");
3258 // FIXME: If there are cases where '<' is used in ivar declaration part
3259 // of user code, then scan the ivar list and use needToScanForQualifiers
3260 // for type checking.
3261 else if (*cursor == '<') {
3262 SourceLocation atLoc = LocStart.getLocWithOffset(cursor-startBuf);
3263 InsertText(atLoc, "/* ");
3264 cursor = strchr(cursor, '>');
3266 atLoc = LocStart.getLocWithOffset(cursor-startBuf);
3267 InsertText(atLoc, " */");
3268 } else if (*cursor == '^') { // rewrite block specifier.
3269 SourceLocation caretLoc = LocStart.getLocWithOffset(cursor-startBuf);
3270 ReplaceText(caretLoc, 1, "*");
3274 // Don't forget to add a ';'!!
3275 InsertText(LocEnd.getLocWithOffset(1), ";");
3276 } else { // we don't have any instance variables - insert super struct.
3277 endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts);
3278 Result += " {\n struct ";
3279 Result += RCDecl->getNameAsString();
3281 Result += RCDecl->getNameAsString();
3282 Result += "_IVARS;\n};\n";
3283 ReplaceText(LocStart, endBuf-startBuf, Result);
3285 // Mark this struct as having been generated.
3286 if (!ObjCSynthesizedStructs.insert(CDecl))
3287 llvm_unreachable("struct already synthesize- SynthesizeObjCInternalStruct");
3290 //===----------------------------------------------------------------------===//
3291 // Meta Data Emission
3292 //===----------------------------------------------------------------------===//
3295 /// RewriteImplementations - This routine rewrites all method implementations
3296 /// and emits meta-data.
3298 void RewriteObjC::RewriteImplementations() {
3299 int ClsDefCount = ClassImplementation.size();
3300 int CatDefCount = CategoryImplementation.size();
3302 // Rewrite implemented methods
3303 for (int i = 0; i < ClsDefCount; i++)
3304 RewriteImplementationDecl(ClassImplementation[i]);
3306 for (int i = 0; i < CatDefCount; i++)
3307 RewriteImplementationDecl(CategoryImplementation[i]);
3310 void RewriteObjC::RewriteByRefString(std::string &ResultStr,
3311 const std::string &Name,
3312 ValueDecl *VD, bool def) {
3313 assert(BlockByRefDeclNo.count(VD) &&
3314 "RewriteByRefString: ByRef decl missing");
3316 ResultStr += "struct ";
3317 ResultStr += "__Block_byref_" + Name +
3318 "_" + utostr(BlockByRefDeclNo[VD]) ;
3321 static bool HasLocalVariableExternalStorage(ValueDecl *VD) {
3322 if (VarDecl *Var = dyn_cast<VarDecl>(VD))
3323 return (Var->isFunctionOrMethodVarDecl() && !Var->hasLocalStorage());
3327 std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i,
3330 const FunctionType *AFT = CE->getFunctionType();
3331 QualType RT = AFT->getResultType();
3332 std::string StructRef = "struct " + Tag;
3333 std::string S = "static " + RT.getAsString(Context->getPrintingPolicy()) + " __" +
3334 funcName.str() + "_" + "block_func_" + utostr(i);
3336 BlockDecl *BD = CE->getBlockDecl();
3338 if (isa<FunctionNoProtoType>(AFT)) {
3339 // No user-supplied arguments. Still need to pass in a pointer to the
3340 // block (to reference imported block decl refs).
3341 S += "(" + StructRef + " *__cself)";
3342 } else if (BD->param_empty()) {
3343 S += "(" + StructRef + " *__cself)";
3345 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
3346 assert(FT && "SynthesizeBlockFunc: No function proto");
3348 // first add the implicit argument.
3349 S += StructRef + " *__cself, ";
3350 std::string ParamStr;
3351 for (BlockDecl::param_iterator AI = BD->param_begin(),
3352 E = BD->param_end(); AI != E; ++AI) {
3353 if (AI != BD->param_begin()) S += ", ";
3354 ParamStr = (*AI)->getNameAsString();
3355 QualType QT = (*AI)->getType();
3356 (void)convertBlockPointerToFunctionPointer(QT);
3357 QT.getAsStringInternal(ParamStr, Context->getPrintingPolicy());
3360 if (FT->isVariadic()) {
3361 if (!BD->param_empty()) S += ", ";
3368 // Create local declarations to avoid rewriting all closure decl ref exprs.
3369 // First, emit a declaration for all "by ref" decls.
3370 for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(),
3371 E = BlockByRefDecls.end(); I != E; ++I) {
3373 std::string Name = (*I)->getNameAsString();
3374 std::string TypeString;
3375 RewriteByRefString(TypeString, Name, (*I));
3377 Name = TypeString + Name;
3378 S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n";
3380 // Next, emit a declaration for all "by copy" declarations.
3381 for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(),
3382 E = BlockByCopyDecls.end(); I != E; ++I) {
3384 // Handle nested closure invocation. For example:
3386 // void (^myImportedClosure)(void);
3387 // myImportedClosure = ^(void) { setGlobalInt(x + y); };
3389 // void (^anotherClosure)(void);
3390 // anotherClosure = ^(void) {
3391 // myImportedClosure(); // import and invoke the closure
3394 if (isTopLevelBlockPointerType((*I)->getType())) {
3395 RewriteBlockPointerTypeVariable(S, (*I));
3397 RewriteBlockPointerType(S, (*I)->getType());
3399 S += "__cself->" + (*I)->getNameAsString() + "; // bound by copy\n";
3402 std::string Name = (*I)->getNameAsString();
3403 QualType QT = (*I)->getType();
3404 if (HasLocalVariableExternalStorage(*I))
3405 QT = Context->getPointerType(QT);
3406 QT.getAsStringInternal(Name, Context->getPrintingPolicy());
3407 S += Name + " = __cself->" +
3408 (*I)->getNameAsString() + "; // bound by copy\n";
3411 std::string RewrittenStr = RewrittenBlockExprs[CE];
3412 const char *cstr = RewrittenStr.c_str();
3413 while (*cstr++ != '{') ;
3419 std::string RewriteObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i,
3422 std::string StructRef = "struct " + Tag;
3423 std::string S = "static void __";
3426 S += "_block_copy_" + utostr(i);
3427 S += "(" + StructRef;
3428 S += "*dst, " + StructRef;
3430 for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(),
3431 E = ImportedBlockDecls.end(); I != E; ++I) {
3432 ValueDecl *VD = (*I);
3433 S += "_Block_object_assign((void*)&dst->";
3434 S += (*I)->getNameAsString();
3435 S += ", (void*)src->";
3436 S += (*I)->getNameAsString();
3437 if (BlockByRefDeclsPtrSet.count((*I)))
3438 S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);";
3439 else if (VD->getType()->isBlockPointerType())
3440 S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);";
3442 S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);";
3446 S += "\nstatic void __";
3448 S += "_block_dispose_" + utostr(i);
3449 S += "(" + StructRef;
3451 for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(),
3452 E = ImportedBlockDecls.end(); I != E; ++I) {
3453 ValueDecl *VD = (*I);
3454 S += "_Block_object_dispose((void*)src->";
3455 S += (*I)->getNameAsString();
3456 if (BlockByRefDeclsPtrSet.count((*I)))
3457 S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);";
3458 else if (VD->getType()->isBlockPointerType())
3459 S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);";
3461 S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);";
3467 std::string RewriteObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag,
3469 std::string S = "\nstruct " + Tag;
3470 std::string Constructor = " " + Tag;
3472 S += " {\n struct __block_impl impl;\n";
3473 S += " struct " + Desc;
3476 Constructor += "(void *fp, "; // Invoke function pointer.
3477 Constructor += "struct " + Desc; // Descriptor pointer.
3478 Constructor += " *desc";
3480 if (BlockDeclRefs.size()) {
3481 // Output all "by copy" declarations.
3482 for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(),
3483 E = BlockByCopyDecls.end(); I != E; ++I) {
3485 std::string FieldName = (*I)->getNameAsString();
3486 std::string ArgName = "_" + FieldName;
3487 // Handle nested closure invocation. For example:
3489 // void (^myImportedBlock)(void);
3490 // myImportedBlock = ^(void) { setGlobalInt(x + y); };
3492 // void (^anotherBlock)(void);
3493 // anotherBlock = ^(void) {
3494 // myImportedBlock(); // import and invoke the closure
3497 if (isTopLevelBlockPointerType((*I)->getType())) {
3498 S += "struct __block_impl *";
3499 Constructor += ", void *" + ArgName;
3501 QualType QT = (*I)->getType();
3502 if (HasLocalVariableExternalStorage(*I))
3503 QT = Context->getPointerType(QT);
3504 QT.getAsStringInternal(FieldName, Context->getPrintingPolicy());
3505 QT.getAsStringInternal(ArgName, Context->getPrintingPolicy());
3506 Constructor += ", " + ArgName;
3508 S += FieldName + ";\n";
3510 // Output all "by ref" declarations.
3511 for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(),
3512 E = BlockByRefDecls.end(); I != E; ++I) {
3514 std::string FieldName = (*I)->getNameAsString();
3515 std::string ArgName = "_" + FieldName;
3517 std::string TypeString;
3518 RewriteByRefString(TypeString, FieldName, (*I));
3520 FieldName = TypeString + FieldName;
3521 ArgName = TypeString + ArgName;
3522 Constructor += ", " + ArgName;
3524 S += FieldName + "; // by ref\n";
3526 // Finish writing the constructor.
3527 Constructor += ", int flags=0)";
3528 // Initialize all "by copy" arguments.
3529 bool firsTime = true;
3530 for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(),
3531 E = BlockByCopyDecls.end(); I != E; ++I) {
3532 std::string Name = (*I)->getNameAsString();
3534 Constructor += " : ";
3538 Constructor += ", ";
3539 if (isTopLevelBlockPointerType((*I)->getType()))
3540 Constructor += Name + "((struct __block_impl *)_" + Name + ")";
3542 Constructor += Name + "(_" + Name + ")";
3544 // Initialize all "by ref" arguments.
3545 for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(),
3546 E = BlockByRefDecls.end(); I != E; ++I) {
3547 std::string Name = (*I)->getNameAsString();
3549 Constructor += " : ";
3553 Constructor += ", ";
3554 Constructor += Name + "(_" + Name + "->__forwarding)";
3557 Constructor += " {\n";
3559 Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n";
3561 Constructor += " impl.isa = &_NSConcreteStackBlock;\n";
3562 Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n";
3564 Constructor += " Desc = desc;\n";
3566 // Finish writing the constructor.
3567 Constructor += ", int flags=0) {\n";
3569 Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n";
3571 Constructor += " impl.isa = &_NSConcreteStackBlock;\n";
3572 Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n";
3573 Constructor += " Desc = desc;\n";
3576 Constructor += "}\n";
3582 std::string RewriteObjC::SynthesizeBlockDescriptor(std::string DescTag,
3583 std::string ImplTag, int i,
3586 std::string S = "\nstatic struct " + DescTag;
3588 S += " {\n unsigned long reserved;\n";
3589 S += " unsigned long Block_size;\n";
3591 S += " void (*copy)(struct ";
3592 S += ImplTag; S += "*, struct ";
3593 S += ImplTag; S += "*);\n";
3595 S += " void (*dispose)(struct ";
3596 S += ImplTag; S += "*);\n";
3600 S += DescTag + "_DATA = { 0, sizeof(struct ";
3603 S += ", __" + FunName.str() + "_block_copy_" + utostr(i);
3604 S += ", __" + FunName.str() + "_block_dispose_" + utostr(i);
3610 void RewriteObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart,
3611 StringRef FunName) {
3612 // Insert declaration for the function in which block literal is used.
3613 if (CurFunctionDeclToDeclareForBlock && !Blocks.empty())
3614 RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock);
3615 bool RewriteSC = (GlobalVarDecl &&
3617 GlobalVarDecl->getStorageClass() == SC_Static &&
3618 GlobalVarDecl->getType().getCVRQualifiers());
3620 std::string SC(" void __");
3621 SC += GlobalVarDecl->getNameAsString();
3623 InsertText(FunLocStart, SC);
3626 // Insert closures that were part of the function.
3627 for (unsigned i = 0, count=0; i < Blocks.size(); i++) {
3628 CollectBlockDeclRefInfo(Blocks[i]);
3629 // Need to copy-in the inner copied-in variables not actually used in this
3631 for (int j = 0; j < InnerDeclRefsCount[i]; j++) {
3632 DeclRefExpr *Exp = InnerDeclRefs[count++];
3633 ValueDecl *VD = Exp->getDecl();
3634 BlockDeclRefs.push_back(Exp);
3635 if (!VD->hasAttr<BlocksAttr>() && !BlockByCopyDeclsPtrSet.count(VD)) {
3636 BlockByCopyDeclsPtrSet.insert(VD);
3637 BlockByCopyDecls.push_back(VD);
3639 if (VD->hasAttr<BlocksAttr>() && !BlockByRefDeclsPtrSet.count(VD)) {
3640 BlockByRefDeclsPtrSet.insert(VD);
3641 BlockByRefDecls.push_back(VD);
3643 // imported objects in the inner blocks not used in the outer
3644 // blocks must be copied/disposed in the outer block as well.
3645 if (VD->hasAttr<BlocksAttr>() ||
3646 VD->getType()->isObjCObjectPointerType() ||
3647 VD->getType()->isBlockPointerType())
3648 ImportedBlockDecls.insert(VD);
3651 std::string ImplTag = "__" + FunName.str() + "_block_impl_" + utostr(i);
3652 std::string DescTag = "__" + FunName.str() + "_block_desc_" + utostr(i);
3654 std::string CI = SynthesizeBlockImpl(Blocks[i], ImplTag, DescTag);
3656 InsertText(FunLocStart, CI);
3658 std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, ImplTag);
3660 InsertText(FunLocStart, CF);
3662 if (ImportedBlockDecls.size()) {
3663 std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, ImplTag);
3664 InsertText(FunLocStart, HF);
3666 std::string BD = SynthesizeBlockDescriptor(DescTag, ImplTag, i, FunName,
3667 ImportedBlockDecls.size() > 0);
3668 InsertText(FunLocStart, BD);
3670 BlockDeclRefs.clear();
3671 BlockByRefDecls.clear();
3672 BlockByRefDeclsPtrSet.clear();
3673 BlockByCopyDecls.clear();
3674 BlockByCopyDeclsPtrSet.clear();
3675 ImportedBlockDecls.clear();
3678 // Must insert any 'const/volatile/static here. Since it has been
3679 // removed as result of rewriting of block literals.
3681 if (GlobalVarDecl->getStorageClass() == SC_Static)
3683 if (GlobalVarDecl->getType().isConstQualified())
3685 if (GlobalVarDecl->getType().isVolatileQualified())
3687 if (GlobalVarDecl->getType().isRestrictQualified())
3689 InsertText(FunLocStart, SC);
3693 InnerDeclRefsCount.clear();
3694 InnerDeclRefs.clear();
3695 RewrittenBlockExprs.clear();
3698 void RewriteObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) {
3699 SourceLocation FunLocStart = FD->getTypeSpecStartLoc();
3700 StringRef FuncName = FD->getName();
3702 SynthesizeBlockLiterals(FunLocStart, FuncName);
3705 static void BuildUniqueMethodName(std::string &Name,
3706 ObjCMethodDecl *MD) {
3707 ObjCInterfaceDecl *IFace = MD->getClassInterface();
3708 Name = IFace->getName();
3709 Name += "__" + MD->getSelector().getAsString();
3710 // Convert colons to underscores.
3711 std::string::size_type loc = 0;
3712 while ((loc = Name.find(":", loc)) != std::string::npos)
3713 Name.replace(loc, 1, "_");
3716 void RewriteObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) {
3717 //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n");
3718 //SourceLocation FunLocStart = MD->getLocStart();
3719 SourceLocation FunLocStart = MD->getLocStart();
3720 std::string FuncName;
3721 BuildUniqueMethodName(FuncName, MD);
3722 SynthesizeBlockLiterals(FunLocStart, FuncName);
3725 void RewriteObjC::GetBlockDeclRefExprs(Stmt *S) {
3726 for (Stmt::child_range CI = S->children(); CI; ++CI)
3728 if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI))
3729 GetBlockDeclRefExprs(CBE->getBody());
3731 GetBlockDeclRefExprs(*CI);
3733 // Handle specific things.
3734 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) {
3735 if (DRE->refersToEnclosingLocal()) {
3736 // FIXME: Handle enums.
3737 if (!isa<FunctionDecl>(DRE->getDecl()))
3738 BlockDeclRefs.push_back(DRE);
3739 if (HasLocalVariableExternalStorage(DRE->getDecl()))
3740 BlockDeclRefs.push_back(DRE);
3747 void RewriteObjC::GetInnerBlockDeclRefExprs(Stmt *S,
3748 SmallVector<DeclRefExpr *, 8> &InnerBlockDeclRefs,
3749 llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts) {
3750 for (Stmt::child_range CI = S->children(); CI; ++CI)
3752 if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) {
3753 InnerContexts.insert(cast<DeclContext>(CBE->getBlockDecl()));
3754 GetInnerBlockDeclRefExprs(CBE->getBody(),
3759 GetInnerBlockDeclRefExprs(*CI,
3764 // Handle specific things.
3765 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) {
3766 if (DRE->refersToEnclosingLocal()) {
3767 if (!isa<FunctionDecl>(DRE->getDecl()) &&
3768 !InnerContexts.count(DRE->getDecl()->getDeclContext()))
3769 InnerBlockDeclRefs.push_back(DRE);
3770 if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl()))
3771 if (Var->isFunctionOrMethodVarDecl())
3772 ImportedLocalExternalDecls.insert(Var);
3779 /// convertFunctionTypeOfBlocks - This routine converts a function type
3780 /// whose result type may be a block pointer or whose argument type(s)
3781 /// might be block pointers to an equivalent function type replacing
3782 /// all block pointers to function pointers.
3783 QualType RewriteObjC::convertFunctionTypeOfBlocks(const FunctionType *FT) {
3784 const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT);
3785 // FTP will be null for closures that don't take arguments.
3786 // Generate a funky cast.
3787 SmallVector<QualType, 8> ArgTypes;
3788 QualType Res = FT->getResultType();
3789 bool HasBlockType = convertBlockPointerToFunctionPointer(Res);
3792 for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(),
3793 E = FTP->arg_type_end(); I && (I != E); ++I) {
3795 // Make sure we convert "t (^)(...)" to "t (*)(...)".
3796 if (convertBlockPointerToFunctionPointer(t))
3797 HasBlockType = true;
3798 ArgTypes.push_back(t);
3802 // FIXME. Does this work if block takes no argument but has a return type
3803 // which is of block type?
3805 FuncType = getSimpleFunctionType(Res, &ArgTypes[0], ArgTypes.size());
3806 else FuncType = QualType(FT, 0);
3810 Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp, const Expr *BlockExp) {
3811 // Navigate to relevant type information.
3812 const BlockPointerType *CPT = 0;
3814 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BlockExp)) {
3815 CPT = DRE->getType()->getAs<BlockPointerType>();
3816 } else if (const MemberExpr *MExpr = dyn_cast<MemberExpr>(BlockExp)) {
3817 CPT = MExpr->getType()->getAs<BlockPointerType>();
3819 else if (const ParenExpr *PRE = dyn_cast<ParenExpr>(BlockExp)) {
3820 return SynthesizeBlockCall(Exp, PRE->getSubExpr());
3822 else if (const ImplicitCastExpr *IEXPR = dyn_cast<ImplicitCastExpr>(BlockExp))
3823 CPT = IEXPR->getType()->getAs<BlockPointerType>();
3824 else if (const ConditionalOperator *CEXPR =
3825 dyn_cast<ConditionalOperator>(BlockExp)) {
3826 Expr *LHSExp = CEXPR->getLHS();
3827 Stmt *LHSStmt = SynthesizeBlockCall(Exp, LHSExp);
3828 Expr *RHSExp = CEXPR->getRHS();
3829 Stmt *RHSStmt = SynthesizeBlockCall(Exp, RHSExp);
3830 Expr *CONDExp = CEXPR->getCond();
3831 ConditionalOperator *CondExpr =
3832 new (Context) ConditionalOperator(CONDExp,
3833 SourceLocation(), cast<Expr>(LHSStmt),
3834 SourceLocation(), cast<Expr>(RHSStmt),
3835 Exp->getType(), VK_RValue, OK_Ordinary);
3837 } else if (const ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(BlockExp)) {
3838 CPT = IRE->getType()->getAs<BlockPointerType>();
3839 } else if (const PseudoObjectExpr *POE
3840 = dyn_cast<PseudoObjectExpr>(BlockExp)) {
3841 CPT = POE->getType()->castAs<BlockPointerType>();
3843 assert(1 && "RewriteBlockClass: Bad type");
3845 assert(CPT && "RewriteBlockClass: Bad type");
3846 const FunctionType *FT = CPT->getPointeeType()->getAs<FunctionType>();
3847 assert(FT && "RewriteBlockClass: Bad type");
3848 const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT);
3849 // FTP will be null for closures that don't take arguments.
3851 RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl,
3852 SourceLocation(), SourceLocation(),
3853 &Context->Idents.get("__block_impl"));
3854 QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD));
3856 // Generate a funky cast.
3857 SmallVector<QualType, 8> ArgTypes;
3859 // Push the block argument type.
3860 ArgTypes.push_back(PtrBlock);
3862 for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(),
3863 E = FTP->arg_type_end(); I && (I != E); ++I) {
3865 // Make sure we convert "t (^)(...)" to "t (*)(...)".
3866 if (!convertBlockPointerToFunctionPointer(t))
3867 convertToUnqualifiedObjCType(t);
3868 ArgTypes.push_back(t);
3871 // Now do the pointer to function cast.
3872 QualType PtrToFuncCastType
3873 = getSimpleFunctionType(Exp->getType(), &ArgTypes[0], ArgTypes.size());
3875 PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType);
3877 CastExpr *BlkCast = NoTypeInfoCStyleCastExpr(Context, PtrBlock,
3879 const_cast<Expr*>(BlockExp));
3880 // Don't forget the parens to enforce the proper binding.
3881 ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(),
3885 FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(),
3887 &Context->Idents.get("FuncPtr"),
3888 Context->VoidPtrTy, 0,
3889 /*BitWidth=*/0, /*Mutable=*/true,
3891 MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(),
3892 FD->getType(), VK_LValue,
3896 CastExpr *FunkCast = NoTypeInfoCStyleCastExpr(Context, PtrToFuncCastType,
3898 PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast);
3900 SmallVector<Expr*, 8> BlkExprs;
3901 // Add the implicit argument.
3902 BlkExprs.push_back(BlkCast);
3903 // Add the user arguments.
3904 for (CallExpr::arg_iterator I = Exp->arg_begin(),
3905 E = Exp->arg_end(); I != E; ++I) {
3906 BlkExprs.push_back(*I);
3908 CallExpr *CE = new (Context) CallExpr(*Context, PE, &BlkExprs[0],
3910 Exp->getType(), VK_RValue,
3915 // We need to return the rewritten expression to handle cases where the
3916 // BlockDeclRefExpr is embedded in another expression being rewritten.
3923 // void (^myblock)() = ^() {
3924 // [f test]; // f is a BlockDeclRefExpr embedded in a message (which is being rewritten).
3928 Stmt *RewriteObjC::RewriteBlockDeclRefExpr(DeclRefExpr *DeclRefExp) {
3929 // Rewrite the byref variable into BYREFVAR->__forwarding->BYREFVAR
3930 // for each DeclRefExp where BYREFVAR is name of the variable.
3931 ValueDecl *VD = DeclRefExp->getDecl();
3932 bool isArrow = DeclRefExp->refersToEnclosingLocal();
3934 FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(),
3936 &Context->Idents.get("__forwarding"),
3937 Context->VoidPtrTy, 0,
3938 /*BitWidth=*/0, /*Mutable=*/true,
3940 MemberExpr *ME = new (Context) MemberExpr(DeclRefExp, isArrow,
3941 FD, SourceLocation(),
3942 FD->getType(), VK_LValue,
3945 StringRef Name = VD->getName();
3946 FD = FieldDecl::Create(*Context, 0, SourceLocation(), SourceLocation(),
3947 &Context->Idents.get(Name),
3948 Context->VoidPtrTy, 0,
3949 /*BitWidth=*/0, /*Mutable=*/true,
3951 ME = new (Context) MemberExpr(ME, true, FD, SourceLocation(),
3952 DeclRefExp->getType(), VK_LValue, OK_Ordinary);
3956 // Need parens to enforce precedence.
3957 ParenExpr *PE = new (Context) ParenExpr(DeclRefExp->getExprLoc(),
3958 DeclRefExp->getExprLoc(),
3960 ReplaceStmt(DeclRefExp, PE);
3964 // Rewrites the imported local variable V with external storage
3965 // (static, extern, etc.) as *V
3967 Stmt *RewriteObjC::RewriteLocalVariableExternalStorage(DeclRefExpr *DRE) {
3968 ValueDecl *VD = DRE->getDecl();
3969 if (VarDecl *Var = dyn_cast<VarDecl>(VD))
3970 if (!ImportedLocalExternalDecls.count(Var))
3972 Expr *Exp = new (Context) UnaryOperator(DRE, UO_Deref, DRE->getType(),
3973 VK_LValue, OK_Ordinary,
3974 DRE->getLocation());
3975 // Need parens to enforce precedence.
3976 ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(),
3978 ReplaceStmt(DRE, PE);
3982 void RewriteObjC::RewriteCastExpr(CStyleCastExpr *CE) {
3983 SourceLocation LocStart = CE->getLParenLoc();
3984 SourceLocation LocEnd = CE->getRParenLoc();
3986 // Need to avoid trying to rewrite synthesized casts.
3987 if (LocStart.isInvalid())
3989 // Need to avoid trying to rewrite casts contained in macros.
3990 if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd))
3993 const char *startBuf = SM->getCharacterData(LocStart);
3994 const char *endBuf = SM->getCharacterData(LocEnd);
3995 QualType QT = CE->getType();
3996 const Type* TypePtr = QT->getAs<Type>();
3997 if (isa<TypeOfExprType>(TypePtr)) {
3998 const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr);
3999 QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType();
4000 std::string TypeAsString = "(";
4001 RewriteBlockPointerType(TypeAsString, QT);
4002 TypeAsString += ")";
4003 ReplaceText(LocStart, endBuf-startBuf+1, TypeAsString);
4006 // advance the location to startArgList.
4007 const char *argPtr = startBuf;
4009 while (*argPtr++ && (argPtr < endBuf)) {
4012 // Replace the '^' with '*'.
4013 LocStart = LocStart.getLocWithOffset(argPtr-startBuf);
4014 ReplaceText(LocStart, 1, "*");
4021 void RewriteObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) {
4022 SourceLocation DeclLoc = FD->getLocation();
4023 unsigned parenCount = 0;
4025 // We have 1 or more arguments that have closure pointers.
4026 const char *startBuf = SM->getCharacterData(DeclLoc);
4027 const char *startArgList = strchr(startBuf, '(');
4029 assert((*startArgList == '(') && "Rewriter fuzzy parser confused");
4032 // advance the location to startArgList.
4033 DeclLoc = DeclLoc.getLocWithOffset(startArgList-startBuf);
4034 assert((DeclLoc.isValid()) && "Invalid DeclLoc");
4036 const char *argPtr = startArgList;
4038 while (*argPtr++ && parenCount) {
4041 // Replace the '^' with '*'.
4042 DeclLoc = DeclLoc.getLocWithOffset(argPtr-startArgList);
4043 ReplaceText(DeclLoc, 1, "*");
4056 bool RewriteObjC::PointerTypeTakesAnyBlockArguments(QualType QT) {
4057 const FunctionProtoType *FTP;
4058 const PointerType *PT = QT->getAs<PointerType>();
4060 FTP = PT->getPointeeType()->getAs<FunctionProtoType>();
4062 const BlockPointerType *BPT = QT->getAs<BlockPointerType>();
4063 assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type");
4064 FTP = BPT->getPointeeType()->getAs<FunctionProtoType>();
4067 for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(),
4068 E = FTP->arg_type_end(); I != E; ++I)
4069 if (isTopLevelBlockPointerType(*I))
4075 bool RewriteObjC::PointerTypeTakesAnyObjCQualifiedType(QualType QT) {
4076 const FunctionProtoType *FTP;
4077 const PointerType *PT = QT->getAs<PointerType>();
4079 FTP = PT->getPointeeType()->getAs<FunctionProtoType>();
4081 const BlockPointerType *BPT = QT->getAs<BlockPointerType>();
4082 assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type");
4083 FTP = BPT->getPointeeType()->getAs<FunctionProtoType>();
4086 for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(),
4087 E = FTP->arg_type_end(); I != E; ++I) {
4088 if ((*I)->isObjCQualifiedIdType())
4090 if ((*I)->isObjCObjectPointerType() &&
4091 (*I)->getPointeeType()->isObjCQualifiedInterfaceType())
4099 void RewriteObjC::GetExtentOfArgList(const char *Name, const char *&LParen,
4100 const char *&RParen) {
4101 const char *argPtr = strchr(Name, '(');
4102 assert((*argPtr == '(') && "Rewriter fuzzy parser confused");
4104 LParen = argPtr; // output the start.
4105 argPtr++; // skip past the left paren.
4106 unsigned parenCount = 1;
4108 while (*argPtr && parenCount) {
4110 case '(': parenCount++; break;
4111 case ')': parenCount--; break;
4114 if (parenCount) argPtr++;
4116 assert((*argPtr == ')') && "Rewriter fuzzy parser confused");
4117 RParen = argPtr; // output the end
4120 void RewriteObjC::RewriteBlockPointerDecl(NamedDecl *ND) {
4121 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
4122 RewriteBlockPointerFunctionArgs(FD);
4125 // Handle Variables and Typedefs.
4126 SourceLocation DeclLoc = ND->getLocation();
4128 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
4129 DeclT = VD->getType();
4130 else if (TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(ND))
4131 DeclT = TDD->getUnderlyingType();
4132 else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND))
4133 DeclT = FD->getType();
4135 llvm_unreachable("RewriteBlockPointerDecl(): Decl type not yet handled");
4137 const char *startBuf = SM->getCharacterData(DeclLoc);
4138 const char *endBuf = startBuf;
4139 // scan backward (from the decl location) for the end of the previous decl.
4140 while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart)
4142 SourceLocation Start = DeclLoc.getLocWithOffset(startBuf-endBuf);
4144 unsigned OrigLength=0;
4145 // *startBuf != '^' if we are dealing with a pointer to function that
4146 // may take block argument types (which will be handled below).
4147 if (*startBuf == '^') {
4148 // Replace the '^' with '*', computing a negative offset.
4153 while (*startBuf != ')') {
4161 if (PointerTypeTakesAnyBlockArguments(DeclT) ||
4162 PointerTypeTakesAnyObjCQualifiedType(DeclT)) {
4163 // Replace the '^' with '*' for arguments.
4164 // Replace id<P> with id/*<>*/
4165 DeclLoc = ND->getLocation();
4166 startBuf = SM->getCharacterData(DeclLoc);
4167 const char *argListBegin, *argListEnd;
4168 GetExtentOfArgList(startBuf, argListBegin, argListEnd);
4169 while (argListBegin < argListEnd) {
4170 if (*argListBegin == '^')
4172 else if (*argListBegin == '<') {
4174 buf += *argListBegin++;
4176 while (*argListBegin != '>') {
4177 buf += *argListBegin++;
4180 buf += *argListBegin;
4184 buf += *argListBegin;
4191 ReplaceText(Start, OrigLength, buf);
4197 /// SynthesizeByrefCopyDestroyHelper - This routine synthesizes:
4198 /// void __Block_byref_id_object_copy(struct Block_byref_id_object *dst,
4199 /// struct Block_byref_id_object *src) {
4200 /// _Block_object_assign (&_dest->object, _src->object,
4201 /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT
4202 /// [|BLOCK_FIELD_IS_WEAK]) // object
4203 /// _Block_object_assign(&_dest->object, _src->object,
4204 /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK
4205 /// [|BLOCK_FIELD_IS_WEAK]) // block
4208 /// void __Block_byref_id_object_dispose(struct Block_byref_id_object *_src) {
4209 /// _Block_object_dispose(_src->object,
4210 /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT
4211 /// [|BLOCK_FIELD_IS_WEAK]) // object
4212 /// _Block_object_dispose(_src->object,
4213 /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK
4214 /// [|BLOCK_FIELD_IS_WEAK]) // block
4217 std::string RewriteObjC::SynthesizeByrefCopyDestroyHelper(VarDecl *VD,
4220 if (CopyDestroyCache.count(flag))
4222 CopyDestroyCache.insert(flag);
4223 S = "static void __Block_byref_id_object_copy_";
4225 S += "(void *dst, void *src) {\n";
4227 // offset into the object pointer is computed as:
4228 // void * + void* + int + int + void* + void *
4230 static_cast<unsigned>(Context->getTypeSize(Context->IntTy));
4231 unsigned VoidPtrSize =
4232 static_cast<unsigned>(Context->getTypeSize(Context->VoidPtrTy));
4234 unsigned offset = (VoidPtrSize*4 + IntSize + IntSize)/Context->getCharWidth();
4235 S += " _Block_object_assign((char*)dst + ";
4236 S += utostr(offset);
4237 S += ", *(void * *) ((char*)src + ";
4238 S += utostr(offset);
4243 S += "static void __Block_byref_id_object_dispose_";
4245 S += "(void *src) {\n";
4246 S += " _Block_object_dispose(*(void * *) ((char*)src + ";
4247 S += utostr(offset);
4254 /// RewriteByRefVar - For each __block typex ND variable this routine transforms
4255 /// the declaration into:
4256 /// struct __Block_byref_ND {
4257 /// void *__isa; // NULL for everything except __weak pointers
4258 /// struct __Block_byref_ND *__forwarding;
4259 /// int32_t __flags;
4261 /// void *__Block_byref_id_object_copy; // If variable is __block ObjC object
4262 /// void *__Block_byref_id_object_dispose; // If variable is __block ObjC object
4266 /// It then replaces declaration of ND variable with:
4267 /// struct __Block_byref_ND ND = {__isa=0B, __forwarding=&ND, __flags=some_flag,
4268 /// __size=sizeof(struct __Block_byref_ND),
4269 /// ND=initializer-if-any};
4272 void RewriteObjC::RewriteByRefVar(VarDecl *ND) {
4273 // Insert declaration for the function in which block literal is
4275 if (CurFunctionDeclToDeclareForBlock)
4276 RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock);
4279 SourceLocation DeclLoc = ND->getTypeSpecStartLoc();
4280 if (DeclLoc.isInvalid())
4281 // If type location is missing, it is because of missing type (a warning).
4282 // Use variable's location which is good for this case.
4283 DeclLoc = ND->getLocation();
4284 const char *startBuf = SM->getCharacterData(DeclLoc);
4285 SourceLocation X = ND->getLocEnd();
4286 X = SM->getExpansionLoc(X);
4287 const char *endBuf = SM->getCharacterData(X);
4288 std::string Name(ND->getNameAsString());
4289 std::string ByrefType;
4290 RewriteByRefString(ByrefType, Name, ND, true);
4291 ByrefType += " {\n";
4292 ByrefType += " void *__isa;\n";
4293 RewriteByRefString(ByrefType, Name, ND);
4294 ByrefType += " *__forwarding;\n";
4295 ByrefType += " int __flags;\n";
4296 ByrefType += " int __size;\n";
4297 // Add void *__Block_byref_id_object_copy;
4298 // void *__Block_byref_id_object_dispose; if needed.
4299 QualType Ty = ND->getType();
4300 bool HasCopyAndDispose = Context->BlockRequiresCopying(Ty);
4301 if (HasCopyAndDispose) {
4302 ByrefType += " void (*__Block_byref_id_object_copy)(void*, void*);\n";
4303 ByrefType += " void (*__Block_byref_id_object_dispose)(void*);\n";
4307 (void)convertBlockPointerToFunctionPointer(T);
4308 T.getAsStringInternal(Name, Context->getPrintingPolicy());
4310 ByrefType += " " + Name + ";\n";
4311 ByrefType += "};\n";
4312 // Insert this type in global scope. It is needed by helper function.
4313 SourceLocation FunLocStart;
4315 FunLocStart = CurFunctionDef->getTypeSpecStartLoc();
4317 assert(CurMethodDef && "RewriteByRefVar - CurMethodDef is null");
4318 FunLocStart = CurMethodDef->getLocStart();
4320 InsertText(FunLocStart, ByrefType);
4321 if (Ty.isObjCGCWeak()) {
4322 flag |= BLOCK_FIELD_IS_WEAK;
4326 if (HasCopyAndDispose) {
4327 flag = BLOCK_BYREF_CALLER;
4328 QualType Ty = ND->getType();
4329 // FIXME. Handle __weak variable (BLOCK_FIELD_IS_WEAK) as well.
4330 if (Ty->isBlockPointerType())
4331 flag |= BLOCK_FIELD_IS_BLOCK;
4333 flag |= BLOCK_FIELD_IS_OBJECT;
4334 std::string HF = SynthesizeByrefCopyDestroyHelper(ND, flag);
4336 InsertText(FunLocStart, HF);
4339 // struct __Block_byref_ND ND =
4340 // {0, &ND, some_flag, __size=sizeof(struct __Block_byref_ND),
4341 // initializer-if-any};
4342 bool hasInit = (ND->getInit() != 0);
4344 if (HasCopyAndDispose)
4345 flags |= BLOCK_HAS_COPY_DISPOSE;
4346 Name = ND->getNameAsString();
4348 RewriteByRefString(ByrefType, Name, ND);
4349 std::string ForwardingCastType("(");
4350 ForwardingCastType += ByrefType + " *)";
4352 ByrefType += " " + Name + " = {(void*)";
4353 ByrefType += utostr(isa);
4354 ByrefType += "," + ForwardingCastType + "&" + Name + ", ";
4355 ByrefType += utostr(flags);
4357 ByrefType += "sizeof(";
4358 RewriteByRefString(ByrefType, Name, ND);
4360 if (HasCopyAndDispose) {
4361 ByrefType += ", __Block_byref_id_object_copy_";
4362 ByrefType += utostr(flag);
4363 ByrefType += ", __Block_byref_id_object_dispose_";
4364 ByrefType += utostr(flag);
4366 ByrefType += "};\n";
4367 unsigned nameSize = Name.size();
4368 // for block or function pointer declaration. Name is aleady
4369 // part of the declaration.
4370 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType())
4372 ReplaceText(DeclLoc, endBuf-startBuf+nameSize, ByrefType);
4375 SourceLocation startLoc;
4376 Expr *E = ND->getInit();
4377 if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E))
4378 startLoc = ECE->getLParenLoc();
4380 startLoc = E->getLocStart();
4381 startLoc = SM->getExpansionLoc(startLoc);
4382 endBuf = SM->getCharacterData(startLoc);
4383 ByrefType += " " + Name;
4384 ByrefType += " = {(void*)";
4385 ByrefType += utostr(isa);
4386 ByrefType += "," + ForwardingCastType + "&" + Name + ", ";
4387 ByrefType += utostr(flags);
4389 ByrefType += "sizeof(";
4390 RewriteByRefString(ByrefType, Name, ND);
4392 if (HasCopyAndDispose) {
4393 ByrefType += "__Block_byref_id_object_copy_";
4394 ByrefType += utostr(flag);
4395 ByrefType += ", __Block_byref_id_object_dispose_";
4396 ByrefType += utostr(flag);
4399 ReplaceText(DeclLoc, endBuf-startBuf, ByrefType);
4401 // Complete the newly synthesized compound expression by inserting a right
4402 // curly brace before the end of the declaration.
4403 // FIXME: This approach avoids rewriting the initializer expression. It
4404 // also assumes there is only one declarator. For example, the following
4405 // isn't currently supported by this routine (in general):
4407 // double __block BYREFVAR = 1.34, BYREFVAR2 = 1.37;
4409 const char *startInitializerBuf = SM->getCharacterData(startLoc);
4410 const char *semiBuf = strchr(startInitializerBuf, ';');
4411 assert((*semiBuf == ';') && "RewriteByRefVar: can't find ';'");
4412 SourceLocation semiLoc =
4413 startLoc.getLocWithOffset(semiBuf-startInitializerBuf);
4415 InsertText(semiLoc, "}");
4420 void RewriteObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) {
4421 // Add initializers for any closure decl refs.
4422 GetBlockDeclRefExprs(Exp->getBody());
4423 if (BlockDeclRefs.size()) {
4424 // Unique all "by copy" declarations.
4425 for (unsigned i = 0; i < BlockDeclRefs.size(); i++)
4426 if (!BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>()) {
4427 if (!BlockByCopyDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) {
4428 BlockByCopyDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl());
4429 BlockByCopyDecls.push_back(BlockDeclRefs[i]->getDecl());
4432 // Unique all "by ref" declarations.
4433 for (unsigned i = 0; i < BlockDeclRefs.size(); i++)
4434 if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>()) {
4435 if (!BlockByRefDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) {
4436 BlockByRefDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl());
4437 BlockByRefDecls.push_back(BlockDeclRefs[i]->getDecl());
4440 // Find any imported blocks...they will need special attention.
4441 for (unsigned i = 0; i < BlockDeclRefs.size(); i++)
4442 if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>() ||
4443 BlockDeclRefs[i]->getType()->isObjCObjectPointerType() ||
4444 BlockDeclRefs[i]->getType()->isBlockPointerType())
4445 ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl());
4449 FunctionDecl *RewriteObjC::SynthBlockInitFunctionDecl(StringRef name) {
4450 IdentifierInfo *ID = &Context->Idents.get(name);
4451 QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy);
4452 return FunctionDecl::Create(*Context, TUDecl, SourceLocation(),
4453 SourceLocation(), ID, FType, 0, SC_Extern,
4454 SC_None, false, false);
4457 Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp,
4458 const SmallVector<DeclRefExpr *, 8> &InnerBlockDeclRefs) {
4459 const BlockDecl *block = Exp->getBlockDecl();
4460 Blocks.push_back(Exp);
4462 CollectBlockDeclRefInfo(Exp);
4464 // Add inner imported variables now used in current block.
4465 int countOfInnerDecls = 0;
4466 if (!InnerBlockDeclRefs.empty()) {
4467 for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) {
4468 DeclRefExpr *Exp = InnerBlockDeclRefs[i];
4469 ValueDecl *VD = Exp->getDecl();
4470 if (!VD->hasAttr<BlocksAttr>() && !BlockByCopyDeclsPtrSet.count(VD)) {
4471 // We need to save the copied-in variables in nested
4472 // blocks because it is needed at the end for some of the API generations.
4473 // See SynthesizeBlockLiterals routine.
4474 InnerDeclRefs.push_back(Exp); countOfInnerDecls++;
4475 BlockDeclRefs.push_back(Exp);
4476 BlockByCopyDeclsPtrSet.insert(VD);
4477 BlockByCopyDecls.push_back(VD);
4479 if (VD->hasAttr<BlocksAttr>() && !BlockByRefDeclsPtrSet.count(VD)) {
4480 InnerDeclRefs.push_back(Exp); countOfInnerDecls++;
4481 BlockDeclRefs.push_back(Exp);
4482 BlockByRefDeclsPtrSet.insert(VD);
4483 BlockByRefDecls.push_back(VD);
4486 // Find any imported blocks...they will need special attention.
4487 for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++)
4488 if (InnerBlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>() ||
4489 InnerBlockDeclRefs[i]->getType()->isObjCObjectPointerType() ||
4490 InnerBlockDeclRefs[i]->getType()->isBlockPointerType())
4491 ImportedBlockDecls.insert(InnerBlockDeclRefs[i]->getDecl());
4493 InnerDeclRefsCount.push_back(countOfInnerDecls);
4495 std::string FuncName;
4498 FuncName = CurFunctionDef->getNameAsString();
4499 else if (CurMethodDef)
4500 BuildUniqueMethodName(FuncName, CurMethodDef);
4501 else if (GlobalVarDecl)
4502 FuncName = std::string(GlobalVarDecl->getNameAsString());
4504 std::string BlockNumber = utostr(Blocks.size()-1);
4506 std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber;
4507 std::string Func = "__" + FuncName + "_block_func_" + BlockNumber;
4509 // Get a pointer to the function type so we can cast appropriately.
4510 QualType BFT = convertFunctionTypeOfBlocks(Exp->getFunctionType());
4511 QualType FType = Context->getPointerType(BFT);
4516 // Simulate a contructor call...
4517 FD = SynthBlockInitFunctionDecl(Tag);
4518 DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, false, FType, VK_RValue,
4521 SmallVector<Expr*, 4> InitExprs;
4523 // Initialize the block function.
4524 FD = SynthBlockInitFunctionDecl(Func);
4525 DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, false, FD->getType(),
4526 VK_LValue, SourceLocation());
4527 CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy,
4529 InitExprs.push_back(castExpr);
4531 // Initialize the block descriptor.
4532 std::string DescData = "__" + FuncName + "_block_desc_" + BlockNumber + "_DATA";
4534 VarDecl *NewVD = VarDecl::Create(*Context, TUDecl,
4535 SourceLocation(), SourceLocation(),
4536 &Context->Idents.get(DescData.c_str()),
4537 Context->VoidPtrTy, 0,
4538 SC_Static, SC_None);
4539 UnaryOperator *DescRefExpr =
4540 new (Context) UnaryOperator(new (Context) DeclRefExpr(NewVD, false,
4545 Context->getPointerType(Context->VoidPtrTy),
4546 VK_RValue, OK_Ordinary,
4548 InitExprs.push_back(DescRefExpr);
4550 // Add initializers for any closure decl refs.
4551 if (BlockDeclRefs.size()) {
4553 // Output all "by copy" declarations.
4554 for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(),
4555 E = BlockByCopyDecls.end(); I != E; ++I) {
4556 if (isObjCType((*I)->getType())) {
4557 // FIXME: Conform to ABI ([[obj retain] autorelease]).
4558 FD = SynthBlockInitFunctionDecl((*I)->getName());
4559 Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue,
4561 if (HasLocalVariableExternalStorage(*I)) {
4562 QualType QT = (*I)->getType();
4563 QT = Context->getPointerType(QT);
4564 Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue,
4565 OK_Ordinary, SourceLocation());
4567 } else if (isTopLevelBlockPointerType((*I)->getType())) {
4568 FD = SynthBlockInitFunctionDecl((*I)->getName());
4569 Arg = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue,
4571 Exp = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy,
4574 FD = SynthBlockInitFunctionDecl((*I)->getName());
4575 Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue,
4577 if (HasLocalVariableExternalStorage(*I)) {
4578 QualType QT = (*I)->getType();
4579 QT = Context->getPointerType(QT);
4580 Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue,
4581 OK_Ordinary, SourceLocation());
4585 InitExprs.push_back(Exp);
4587 // Output all "by ref" declarations.
4588 for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(),
4589 E = BlockByRefDecls.end(); I != E; ++I) {
4590 ValueDecl *ND = (*I);
4591 std::string Name(ND->getNameAsString());
4592 std::string RecName;
4593 RewriteByRefString(RecName, Name, ND, true);
4594 IdentifierInfo *II = &Context->Idents.get(RecName.c_str()
4595 + sizeof("struct"));
4596 RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl,
4597 SourceLocation(), SourceLocation(),
4599 assert(RD && "SynthBlockInitExpr(): Can't find RecordDecl");
4600 QualType castT = Context->getPointerType(Context->getTagDeclType(RD));
4602 FD = SynthBlockInitFunctionDecl((*I)->getName());
4603 Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue,
4605 bool isNestedCapturedVar = false;
4607 for (BlockDecl::capture_const_iterator ci = block->capture_begin(),
4608 ce = block->capture_end(); ci != ce; ++ci) {
4609 const VarDecl *variable = ci->getVariable();
4610 if (variable == ND && ci->isNested()) {
4611 assert (ci->isByRef() &&
4612 "SynthBlockInitExpr - captured block variable is not byref");
4613 isNestedCapturedVar = true;
4617 // captured nested byref variable has its address passed. Do not take
4618 // its address again.
4619 if (!isNestedCapturedVar)
4620 Exp = new (Context) UnaryOperator(Exp, UO_AddrOf,
4621 Context->getPointerType(Exp->getType()),
4622 VK_RValue, OK_Ordinary, SourceLocation());
4623 Exp = NoTypeInfoCStyleCastExpr(Context, castT, CK_BitCast, Exp);
4624 InitExprs.push_back(Exp);
4627 if (ImportedBlockDecls.size()) {
4628 // generate BLOCK_HAS_COPY_DISPOSE(have helper funcs) | BLOCK_HAS_DESCRIPTOR
4629 int flag = (BLOCK_HAS_COPY_DISPOSE | BLOCK_HAS_DESCRIPTOR);
4631 static_cast<unsigned>(Context->getTypeSize(Context->IntTy));
4632 Expr *FlagExp = IntegerLiteral::Create(*Context, llvm::APInt(IntSize, flag),
4633 Context->IntTy, SourceLocation());
4634 InitExprs.push_back(FlagExp);
4636 NewRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], InitExprs.size(),
4637 FType, VK_LValue, SourceLocation());
4638 NewRep = new (Context) UnaryOperator(NewRep, UO_AddrOf,
4639 Context->getPointerType(NewRep->getType()),
4640 VK_RValue, OK_Ordinary, SourceLocation());
4641 NewRep = NoTypeInfoCStyleCastExpr(Context, FType, CK_BitCast,
4643 BlockDeclRefs.clear();
4644 BlockByRefDecls.clear();
4645 BlockByRefDeclsPtrSet.clear();
4646 BlockByCopyDecls.clear();
4647 BlockByCopyDeclsPtrSet.clear();
4648 ImportedBlockDecls.clear();
4652 bool RewriteObjC::IsDeclStmtInForeachHeader(DeclStmt *DS) {
4653 if (const ObjCForCollectionStmt * CS =
4654 dyn_cast<ObjCForCollectionStmt>(Stmts.back()))
4655 return CS->getElement() == DS;
4659 //===----------------------------------------------------------------------===//
4660 // Function Body / Expression rewriting
4661 //===----------------------------------------------------------------------===//
4663 Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) {
4664 if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) ||
4665 isa<DoStmt>(S) || isa<ForStmt>(S))
4667 else if (isa<ObjCForCollectionStmt>(S)) {
4669 ObjCBcLabelNo.push_back(++BcLabelCount);
4672 // Pseudo-object operations and ivar references need special
4673 // treatment because we're going to recursively rewrite them.
4674 if (PseudoObjectExpr *PseudoOp = dyn_cast<PseudoObjectExpr>(S)) {
4675 if (isa<BinaryOperator>(PseudoOp->getSyntacticForm())) {
4676 return RewritePropertyOrImplicitSetter(PseudoOp);
4678 return RewritePropertyOrImplicitGetter(PseudoOp);
4680 } else if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) {
4681 return RewriteObjCIvarRefExpr(IvarRefExpr);
4684 SourceRange OrigStmtRange = S->getSourceRange();
4686 // Perform a bottom up rewrite of all children.
4687 for (Stmt::child_range CI = S->children(); CI; ++CI)
4689 Stmt *childStmt = (*CI);
4690 Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(childStmt);
4696 if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) {
4697 SmallVector<DeclRefExpr *, 8> InnerBlockDeclRefs;
4698 llvm::SmallPtrSet<const DeclContext *, 8> InnerContexts;
4699 InnerContexts.insert(BE->getBlockDecl());
4700 ImportedLocalExternalDecls.clear();
4701 GetInnerBlockDeclRefExprs(BE->getBody(),
4702 InnerBlockDeclRefs, InnerContexts);
4703 // Rewrite the block body in place.
4704 Stmt *SaveCurrentBody = CurrentBody;
4705 CurrentBody = BE->getBody();
4707 // block literal on rhs of a property-dot-sytax assignment
4708 // must be replaced by its synthesize ast so getRewrittenText
4709 // works as expected. In this case, what actually ends up on RHS
4710 // is the blockTranscribed which is the helper function for the
4711 // block literal; as in: self.c = ^() {[ace ARR];};
4712 bool saveDisableReplaceStmt = DisableReplaceStmt;
4713 DisableReplaceStmt = false;
4714 RewriteFunctionBodyOrGlobalInitializer(BE->getBody());
4715 DisableReplaceStmt = saveDisableReplaceStmt;
4716 CurrentBody = SaveCurrentBody;
4718 ImportedLocalExternalDecls.clear();
4719 // Now we snarf the rewritten text and stash it away for later use.
4720 std::string Str = Rewrite.getRewrittenText(BE->getSourceRange());
4721 RewrittenBlockExprs[BE] = Str;
4723 Stmt *blockTranscribed = SynthBlockInitExpr(BE, InnerBlockDeclRefs);
4725 //blockTranscribed->dump();
4726 ReplaceStmt(S, blockTranscribed);
4727 return blockTranscribed;
4729 // Handle specific things.
4730 if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S))
4731 return RewriteAtEncode(AtEncode);
4733 if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S))
4734 return RewriteAtSelector(AtSelector);
4736 if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S))
4737 return RewriteObjCStringLiteral(AtString);
4739 if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) {
4741 // Before we rewrite it, put the original message expression in a comment.
4742 SourceLocation startLoc = MessExpr->getLocStart();
4743 SourceLocation endLoc = MessExpr->getLocEnd();
4745 const char *startBuf = SM->getCharacterData(startLoc);
4746 const char *endBuf = SM->getCharacterData(endLoc);
4748 std::string messString;
4749 messString += "// ";
4750 messString.append(startBuf, endBuf-startBuf+1);
4753 // FIXME: Missing definition of
4754 // InsertText(clang::SourceLocation, char const*, unsigned int).
4755 // InsertText(startLoc, messString.c_str(), messString.size());
4756 // Tried this, but it didn't work either...
4757 // ReplaceText(startLoc, 0, messString.c_str(), messString.size());
4759 return RewriteMessageExpr(MessExpr);
4762 if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S))
4763 return RewriteObjCTryStmt(StmtTry);
4765 if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S))
4766 return RewriteObjCSynchronizedStmt(StmtTry);
4768 if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S))
4769 return RewriteObjCThrowStmt(StmtThrow);
4771 if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S))
4772 return RewriteObjCProtocolExpr(ProtocolExp);
4774 if (ObjCForCollectionStmt *StmtForCollection =
4775 dyn_cast<ObjCForCollectionStmt>(S))
4776 return RewriteObjCForCollectionStmt(StmtForCollection,
4777 OrigStmtRange.getEnd());
4778 if (BreakStmt *StmtBreakStmt =
4779 dyn_cast<BreakStmt>(S))
4780 return RewriteBreakStmt(StmtBreakStmt);
4781 if (ContinueStmt *StmtContinueStmt =
4782 dyn_cast<ContinueStmt>(S))
4783 return RewriteContinueStmt(StmtContinueStmt);
4785 // Need to check for protocol refs (id <P>, Foo <P> *) in variable decls
4787 if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
4788 // FIXME: What we're doing here is modifying the type-specifier that
4789 // precedes the first Decl. In the future the DeclGroup should have
4790 // a separate type-specifier that we can rewrite.
4791 // NOTE: We need to avoid rewriting the DeclStmt if it is within
4792 // the context of an ObjCForCollectionStmt. For example:
4793 // NSArray *someArray;
4794 // for (id <FooProtocol> index in someArray) ;
4795 // This is because RewriteObjCForCollectionStmt() does textual rewriting
4796 // and it depends on the original text locations/positions.
4797 if (Stmts.empty() || !IsDeclStmtInForeachHeader(DS))
4798 RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin());
4800 // Blocks rewrite rules.
4801 for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end();
4804 if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) {
4805 if (isTopLevelBlockPointerType(ND->getType()))
4806 RewriteBlockPointerDecl(ND);
4807 else if (ND->getType()->isFunctionPointerType())
4808 CheckFunctionPointerDecl(ND->getType(), ND);
4809 if (VarDecl *VD = dyn_cast<VarDecl>(SD)) {
4810 if (VD->hasAttr<BlocksAttr>()) {
4811 static unsigned uniqueByrefDeclCount = 0;
4812 assert(!BlockByRefDeclNo.count(ND) &&
4813 "RewriteFunctionBodyOrGlobalInitializer: Duplicate byref decl");
4814 BlockByRefDeclNo[ND] = uniqueByrefDeclCount++;
4815 RewriteByRefVar(VD);
4818 RewriteTypeOfDecl(VD);
4821 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) {
4822 if (isTopLevelBlockPointerType(TD->getUnderlyingType()))
4823 RewriteBlockPointerDecl(TD);
4824 else if (TD->getUnderlyingType()->isFunctionPointerType())
4825 CheckFunctionPointerDecl(TD->getUnderlyingType(), TD);
4830 if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S))
4831 RewriteObjCQualifiedInterfaceTypes(CE);
4833 if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) ||
4834 isa<DoStmt>(S) || isa<ForStmt>(S)) {
4835 assert(!Stmts.empty() && "Statement stack is empty");
4836 assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) ||
4837 isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back()))
4838 && "Statement stack mismatch");
4841 // Handle blocks rewriting.
4842 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) {
4843 ValueDecl *VD = DRE->getDecl();
4844 if (VD->hasAttr<BlocksAttr>())
4845 return RewriteBlockDeclRefExpr(DRE);
4846 if (HasLocalVariableExternalStorage(VD))
4847 return RewriteLocalVariableExternalStorage(DRE);
4850 if (CallExpr *CE = dyn_cast<CallExpr>(S)) {
4851 if (CE->getCallee()->getType()->isBlockPointerType()) {
4852 Stmt *BlockCall = SynthesizeBlockCall(CE, CE->getCallee());
4853 ReplaceStmt(S, BlockCall);
4857 if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) {
4858 RewriteCastExpr(CE);
4861 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) {
4862 CastExpr *Replacement = new (Context) CastExpr(ICE->getType(),
4865 // Get the new text.
4867 llvm::raw_string_ostream Buf(SStr);
4868 Replacement->printPretty(Buf, *Context);
4869 const std::string &Str = Buf.str();
4871 printf("CAST = %s\n", &Str[0]);
4872 InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size());
4877 // Return this stmt unmodified.
4881 void RewriteObjC::RewriteRecordBody(RecordDecl *RD) {
4882 for (RecordDecl::field_iterator i = RD->field_begin(),
4883 e = RD->field_end(); i != e; ++i) {
4885 if (isTopLevelBlockPointerType(FD->getType()))
4886 RewriteBlockPointerDecl(FD);
4887 if (FD->getType()->isObjCQualifiedIdType() ||
4888 FD->getType()->isObjCQualifiedInterfaceType())
4889 RewriteObjCQualifiedInterfaceTypes(FD);
4893 /// HandleDeclInMainFile - This is called for each top-level decl defined in the
4894 /// main file of the input.
4895 void RewriteObjC::HandleDeclInMainFile(Decl *D) {
4896 switch (D->getKind()) {
4897 case Decl::Function: {
4898 FunctionDecl *FD = cast<FunctionDecl>(D);
4899 if (FD->isOverloadedOperator())
4902 // Since function prototypes don't have ParmDecl's, we check the function
4903 // prototype. This enables us to rewrite function declarations and
4904 // definitions using the same code.
4905 RewriteBlocksInFunctionProtoType(FD->getType(), FD);
4907 if (!FD->isThisDeclarationADefinition())
4910 // FIXME: If this should support Obj-C++, support CXXTryStmt
4911 if (CompoundStmt *Body = dyn_cast_or_null<CompoundStmt>(FD->getBody())) {
4912 CurFunctionDef = FD;
4913 CurFunctionDeclToDeclareForBlock = FD;
4916 cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body));
4919 if (PropParentMap) {
4920 delete PropParentMap;
4923 // This synthesizes and inserts the block "impl" struct, invoke function,
4924 // and any copy/dispose helper functions.
4925 InsertBlockLiteralsWithinFunction(FD);
4927 CurFunctionDeclToDeclareForBlock = 0;
4931 case Decl::ObjCMethod: {
4932 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(D);
4933 if (CompoundStmt *Body = MD->getCompoundBody()) {
4937 cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body));
4940 if (PropParentMap) {
4941 delete PropParentMap;
4944 InsertBlockLiteralsWithinMethod(MD);
4949 case Decl::ObjCImplementation: {
4950 ObjCImplementationDecl *CI = cast<ObjCImplementationDecl>(D);
4951 ClassImplementation.push_back(CI);
4954 case Decl::ObjCCategoryImpl: {
4955 ObjCCategoryImplDecl *CI = cast<ObjCCategoryImplDecl>(D);
4956 CategoryImplementation.push_back(CI);
4960 VarDecl *VD = cast<VarDecl>(D);
4961 RewriteObjCQualifiedInterfaceTypes(VD);
4962 if (isTopLevelBlockPointerType(VD->getType()))
4963 RewriteBlockPointerDecl(VD);
4964 else if (VD->getType()->isFunctionPointerType()) {
4965 CheckFunctionPointerDecl(VD->getType(), VD);
4966 if (VD->getInit()) {
4967 if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) {
4968 RewriteCastExpr(CE);
4971 } else if (VD->getType()->isRecordType()) {
4972 RecordDecl *RD = VD->getType()->getAs<RecordType>()->getDecl();
4973 if (RD->isCompleteDefinition())
4974 RewriteRecordBody(RD);
4976 if (VD->getInit()) {
4978 CurrentBody = VD->getInit();
4979 RewriteFunctionBodyOrGlobalInitializer(VD->getInit());
4981 if (PropParentMap) {
4982 delete PropParentMap;
4985 SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), VD->getName());
4988 // This is needed for blocks.
4989 if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) {
4990 RewriteCastExpr(CE);
4995 case Decl::TypeAlias:
4996 case Decl::Typedef: {
4997 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
4998 if (isTopLevelBlockPointerType(TD->getUnderlyingType()))
4999 RewriteBlockPointerDecl(TD);
5000 else if (TD->getUnderlyingType()->isFunctionPointerType())
5001 CheckFunctionPointerDecl(TD->getUnderlyingType(), TD);
5005 case Decl::CXXRecord:
5006 case Decl::Record: {
5007 RecordDecl *RD = cast<RecordDecl>(D);
5008 if (RD->isCompleteDefinition())
5009 RewriteRecordBody(RD);
5018 void RewriteObjC::HandleTranslationUnit(ASTContext &C) {
5019 if (Diags.hasErrorOccurred())
5024 // Here's a great place to add any extra declarations that may be needed.
5025 // Write out meta data for each @protocol(<expr>).
5026 for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(),
5027 E = ProtocolExprDecls.end(); I != E; ++I)
5028 RewriteObjCProtocolMetaData(*I, "", "", Preamble);
5030 InsertText(SM->getLocForStartOfFile(MainFileID), Preamble, false);
5031 if (ClassImplementation.size() || CategoryImplementation.size())
5032 RewriteImplementations();
5034 // Get the buffer corresponding to MainFileID. If we haven't changed it, then
5036 if (const RewriteBuffer *RewriteBuf =
5037 Rewrite.getRewriteBufferFor(MainFileID)) {
5038 //printf("Changed:\n");
5039 *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end());
5041 llvm::errs() << "No changes\n";
5044 if (ClassImplementation.size() || CategoryImplementation.size() ||
5045 ProtocolExprDecls.size()) {
5046 // Rewrite Objective-c meta data*
5047 std::string ResultStr;
5048 RewriteMetaDataIntoBuffer(ResultStr);
5050 *OutFile << ResultStr;
5055 void RewriteObjCFragileABI::Initialize(ASTContext &context) {
5056 InitializeCommon(context);
5058 // declaring objc_selector outside the parameter list removes a silly
5059 // scope related warning...
5061 Preamble = "#pragma once\n";
5062 Preamble += "struct objc_selector; struct objc_class;\n";
5063 Preamble += "struct __rw_objc_super { struct objc_object *object; ";
5064 Preamble += "struct objc_object *superClass; ";
5065 if (LangOpts.MicrosoftExt) {
5066 // Add a constructor for creating temporary objects.
5067 Preamble += "__rw_objc_super(struct objc_object *o, struct objc_object *s) "
5069 Preamble += "object(o), superClass(s) {} ";
5072 Preamble += "#ifndef _REWRITER_typedef_Protocol\n";
5073 Preamble += "typedef struct objc_object Protocol;\n";
5074 Preamble += "#define _REWRITER_typedef_Protocol\n";
5075 Preamble += "#endif\n";
5076 if (LangOpts.MicrosoftExt) {
5077 Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n";
5078 Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n";
5080 Preamble += "#define __OBJC_RW_DLLIMPORT extern\n";
5081 Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend";
5082 Preamble += "(struct objc_object *, struct objc_selector *, ...);\n";
5083 Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper";
5084 Preamble += "(struct objc_super *, struct objc_selector *, ...);\n";
5085 Preamble += "__OBJC_RW_DLLIMPORT struct objc_object* objc_msgSend_stret";
5086 Preamble += "(struct objc_object *, struct objc_selector *, ...);\n";
5087 Preamble += "__OBJC_RW_DLLIMPORT struct objc_object* objc_msgSendSuper_stret";
5088 Preamble += "(struct objc_super *, struct objc_selector *, ...);\n";
5089 Preamble += "__OBJC_RW_DLLIMPORT double objc_msgSend_fpret";
5090 Preamble += "(struct objc_object *, struct objc_selector *, ...);\n";
5091 Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getClass";
5092 Preamble += "(const char *);\n";
5093 Preamble += "__OBJC_RW_DLLIMPORT struct objc_class *class_getSuperclass";
5094 Preamble += "(struct objc_class *);\n";
5095 Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getMetaClass";
5096 Preamble += "(const char *);\n";
5097 Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw(struct objc_object *);\n";
5098 Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_enter(void *);\n";
5099 Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_exit(void *);\n";
5100 Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_exception_extract(void *);\n";
5101 Preamble += "__OBJC_RW_DLLIMPORT int objc_exception_match";
5102 Preamble += "(struct objc_class *, struct objc_object *);\n";
5103 // @synchronized hooks.
5104 Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_enter(struct objc_object *);\n";
5105 Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_exit(struct objc_object *);\n";
5106 Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n";
5107 Preamble += "#ifndef __FASTENUMERATIONSTATE\n";
5108 Preamble += "struct __objcFastEnumerationState {\n\t";
5109 Preamble += "unsigned long state;\n\t";
5110 Preamble += "void **itemsPtr;\n\t";
5111 Preamble += "unsigned long *mutationsPtr;\n\t";
5112 Preamble += "unsigned long extra[5];\n};\n";
5113 Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n";
5114 Preamble += "#define __FASTENUMERATIONSTATE\n";
5115 Preamble += "#endif\n";
5116 Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n";
5117 Preamble += "struct __NSConstantStringImpl {\n";
5118 Preamble += " int *isa;\n";
5119 Preamble += " int flags;\n";
5120 Preamble += " char *str;\n";
5121 Preamble += " long length;\n";
5123 Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n";
5124 Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n";
5125 Preamble += "#else\n";
5126 Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n";
5127 Preamble += "#endif\n";
5128 Preamble += "#define __NSCONSTANTSTRINGIMPL\n";
5129 Preamble += "#endif\n";
5131 Preamble += "#ifndef BLOCK_IMPL\n";
5132 Preamble += "#define BLOCK_IMPL\n";
5133 Preamble += "struct __block_impl {\n";
5134 Preamble += " void *isa;\n";
5135 Preamble += " int Flags;\n";
5136 Preamble += " int Reserved;\n";
5137 Preamble += " void *FuncPtr;\n";
5139 Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n";
5140 Preamble += "#ifdef __OBJC_EXPORT_BLOCKS\n";
5141 Preamble += "extern \"C\" __declspec(dllexport) "
5142 "void _Block_object_assign(void *, const void *, const int);\n";
5143 Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_dispose(const void *, const int);\n";
5144 Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteGlobalBlock[32];\n";
5145 Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteStackBlock[32];\n";
5146 Preamble += "#else\n";
5147 Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_assign(void *, const void *, const int);\n";
5148 Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_dispose(const void *, const int);\n";
5149 Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteGlobalBlock[32];\n";
5150 Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteStackBlock[32];\n";
5151 Preamble += "#endif\n";
5152 Preamble += "#endif\n";
5153 if (LangOpts.MicrosoftExt) {
5154 Preamble += "#undef __OBJC_RW_DLLIMPORT\n";
5155 Preamble += "#undef __OBJC_RW_STATICIMPORT\n";
5156 Preamble += "#ifndef KEEP_ATTRIBUTES\n"; // We use this for clang tests.
5157 Preamble += "#define __attribute__(X)\n";
5158 Preamble += "#endif\n";
5159 Preamble += "#define __weak\n";
5162 Preamble += "#define __block\n";
5163 Preamble += "#define __weak\n";
5165 // NOTE! Windows uses LLP64 for 64bit mode. So, cast pointer to long long
5166 // as this avoids warning in any 64bit/32bit compilation model.
5167 Preamble += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((long long) &((TYPE *)0)->MEMBER)\n";
5170 /// RewriteIvarOffsetComputation - This rutine synthesizes computation of
5172 void RewriteObjCFragileABI::RewriteIvarOffsetComputation(ObjCIvarDecl *ivar,
5173 std::string &Result) {
5174 if (ivar->isBitField()) {
5175 // FIXME: The hack below doesn't work for bitfields. For now, we simply
5176 // place all bitfields at offset 0.
5179 Result += "__OFFSETOFIVAR__(struct ";
5180 Result += ivar->getContainingInterface()->getNameAsString();
5181 if (LangOpts.MicrosoftExt)
5184 Result += ivar->getNameAsString();
5189 /// RewriteObjCProtocolMetaData - Rewrite protocols meta-data.
5190 void RewriteObjCFragileABI::RewriteObjCProtocolMetaData(
5191 ObjCProtocolDecl *PDecl, StringRef prefix,
5192 StringRef ClassName, std::string &Result) {
5193 static bool objc_protocol_methods = false;
5195 // Output struct protocol_methods holder of method selector and type.
5196 if (!objc_protocol_methods && PDecl->hasDefinition()) {
5197 /* struct protocol_methods {
5202 Result += "\nstruct _protocol_methods {\n";
5203 Result += "\tstruct objc_selector *_cmd;\n";
5204 Result += "\tchar *method_types;\n";
5207 objc_protocol_methods = true;
5209 // Do not synthesize the protocol more than once.
5210 if (ObjCSynthesizedProtocols.count(PDecl->getCanonicalDecl()))
5213 if (ObjCProtocolDecl *Def = PDecl->getDefinition())
5216 if (PDecl->instmeth_begin() != PDecl->instmeth_end()) {
5217 unsigned NumMethods = std::distance(PDecl->instmeth_begin(),
5218 PDecl->instmeth_end());
5219 /* struct _objc_protocol_method_list {
5220 int protocol_method_count;
5221 struct protocol_methods protocols[];
5224 Result += "\nstatic struct {\n";
5225 Result += "\tint protocol_method_count;\n";
5226 Result += "\tstruct _protocol_methods protocol_methods[";
5227 Result += utostr(NumMethods);
5228 Result += "];\n} _OBJC_PROTOCOL_INSTANCE_METHODS_";
5229 Result += PDecl->getNameAsString();
5230 Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= "
5231 "{\n\t" + utostr(NumMethods) + "\n";
5233 // Output instance methods declared in this protocol.
5234 for (ObjCProtocolDecl::instmeth_iterator
5235 I = PDecl->instmeth_begin(), E = PDecl->instmeth_end();
5237 if (I == PDecl->instmeth_begin())
5238 Result += "\t ,{{(struct objc_selector *)\"";
5240 Result += "\t ,{(struct objc_selector *)\"";
5241 Result += (*I)->getSelector().getAsString();
5242 std::string MethodTypeString;
5243 Context->getObjCEncodingForMethodDecl((*I), MethodTypeString);
5245 Result += MethodTypeString;
5248 Result += "\t }\n};\n";
5251 // Output class methods declared in this protocol.
5252 unsigned NumMethods = std::distance(PDecl->classmeth_begin(),
5253 PDecl->classmeth_end());
5254 if (NumMethods > 0) {
5255 /* struct _objc_protocol_method_list {
5256 int protocol_method_count;
5257 struct protocol_methods protocols[];
5260 Result += "\nstatic struct {\n";
5261 Result += "\tint protocol_method_count;\n";
5262 Result += "\tstruct _protocol_methods protocol_methods[";
5263 Result += utostr(NumMethods);
5264 Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_";
5265 Result += PDecl->getNameAsString();
5266 Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= "
5268 Result += utostr(NumMethods);
5271 // Output instance methods declared in this protocol.
5272 for (ObjCProtocolDecl::classmeth_iterator
5273 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
5275 if (I == PDecl->classmeth_begin())
5276 Result += "\t ,{{(struct objc_selector *)\"";
5278 Result += "\t ,{(struct objc_selector *)\"";
5279 Result += (*I)->getSelector().getAsString();
5280 std::string MethodTypeString;
5281 Context->getObjCEncodingForMethodDecl((*I), MethodTypeString);
5283 Result += MethodTypeString;
5286 Result += "\t }\n};\n";
5290 /* struct _objc_protocol {
5291 // Objective-C 1.0 extensions
5292 struct _objc_protocol_extension *isa;
5293 char *protocol_name;
5294 struct _objc_protocol **protocol_list;
5295 struct _objc_protocol_method_list *instance_methods;
5296 struct _objc_protocol_method_list *class_methods;
5299 static bool objc_protocol = false;
5300 if (!objc_protocol) {
5301 Result += "\nstruct _objc_protocol {\n";
5302 Result += "\tstruct _objc_protocol_extension *isa;\n";
5303 Result += "\tchar *protocol_name;\n";
5304 Result += "\tstruct _objc_protocol **protocol_list;\n";
5305 Result += "\tstruct _objc_protocol_method_list *instance_methods;\n";
5306 Result += "\tstruct _objc_protocol_method_list *class_methods;\n";
5309 objc_protocol = true;
5312 Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_";
5313 Result += PDecl->getNameAsString();
5314 Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= "
5316 Result += PDecl->getNameAsString();
5317 Result += "\", 0, ";
5318 if (PDecl->instmeth_begin() != PDecl->instmeth_end()) {
5319 Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_";
5320 Result += PDecl->getNameAsString();
5325 if (PDecl->classmeth_begin() != PDecl->classmeth_end()) {
5326 Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_";
5327 Result += PDecl->getNameAsString();
5334 // Mark this protocol as having been generated.
5335 if (!ObjCSynthesizedProtocols.insert(PDecl->getCanonicalDecl()))
5336 llvm_unreachable("protocol already synthesized");
5340 void RewriteObjCFragileABI::RewriteObjCProtocolListMetaData(
5341 const ObjCList<ObjCProtocolDecl> &Protocols,
5342 StringRef prefix, StringRef ClassName,
5343 std::string &Result) {
5344 if (Protocols.empty()) return;
5346 for (unsigned i = 0; i != Protocols.size(); i++)
5347 RewriteObjCProtocolMetaData(Protocols[i], prefix, ClassName, Result);
5349 // Output the top lovel protocol meta-data for the class.
5350 /* struct _objc_protocol_list {
5351 struct _objc_protocol_list *next;
5353 struct _objc_protocol *class_protocols[];
5356 Result += "\nstatic struct {\n";
5357 Result += "\tstruct _objc_protocol_list *next;\n";
5358 Result += "\tint protocol_count;\n";
5359 Result += "\tstruct _objc_protocol *class_protocols[";
5360 Result += utostr(Protocols.size());
5361 Result += "];\n} _OBJC_";
5363 Result += "_PROTOCOLS_";
5364 Result += ClassName;
5365 Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= "
5367 Result += utostr(Protocols.size());
5370 Result += "\t,{&_OBJC_PROTOCOL_";
5371 Result += Protocols[0]->getNameAsString();
5374 for (unsigned i = 1; i != Protocols.size(); i++) {
5375 Result += "\t ,&_OBJC_PROTOCOL_";
5376 Result += Protocols[i]->getNameAsString();
5379 Result += "\t }\n};\n";
5382 void RewriteObjCFragileABI::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl,
5383 std::string &Result) {
5384 ObjCInterfaceDecl *CDecl = IDecl->getClassInterface();
5386 // Explicitly declared @interface's are already synthesized.
5387 if (CDecl->isImplicitInterfaceDecl()) {
5388 // FIXME: Implementation of a class with no @interface (legacy) does not
5389 // produce correct synthesis as yet.
5390 RewriteObjCInternalStruct(CDecl, Result);
5393 // Build _objc_ivar_list metadata for classes ivars if needed
5394 unsigned NumIvars = !IDecl->ivar_empty()
5395 ? IDecl->ivar_size()
5396 : (CDecl ? CDecl->ivar_size() : 0);
5398 static bool objc_ivar = false;
5400 /* struct _objc_ivar {
5406 Result += "\nstruct _objc_ivar {\n";
5407 Result += "\tchar *ivar_name;\n";
5408 Result += "\tchar *ivar_type;\n";
5409 Result += "\tint ivar_offset;\n";
5417 struct _objc_ivar ivar_list[nIvars];
5420 Result += "\nstatic struct {\n";
5421 Result += "\tint ivar_count;\n";
5422 Result += "\tstruct _objc_ivar ivar_list[";
5423 Result += utostr(NumIvars);
5424 Result += "];\n} _OBJC_INSTANCE_VARIABLES_";
5425 Result += IDecl->getNameAsString();
5426 Result += " __attribute__ ((used, section (\"__OBJC, __instance_vars\")))= "
5428 Result += utostr(NumIvars);
5431 ObjCInterfaceDecl::ivar_iterator IVI, IVE;
5432 SmallVector<ObjCIvarDecl *, 8> IVars;
5433 if (!IDecl->ivar_empty()) {
5434 for (ObjCInterfaceDecl::ivar_iterator
5435 IV = IDecl->ivar_begin(), IVEnd = IDecl->ivar_end();
5437 IVars.push_back(*IV);
5438 IVI = IDecl->ivar_begin();
5439 IVE = IDecl->ivar_end();
5441 IVI = CDecl->ivar_begin();
5442 IVE = CDecl->ivar_end();
5444 Result += "\t,{{\"";
5445 Result += (*IVI)->getNameAsString();
5447 std::string TmpString, StrEncoding;
5448 Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI);
5449 QuoteDoublequotes(TmpString, StrEncoding);
5450 Result += StrEncoding;
5452 RewriteIvarOffsetComputation(*IVI, Result);
5454 for (++IVI; IVI != IVE; ++IVI) {
5455 Result += "\t ,{\"";
5456 Result += (*IVI)->getNameAsString();
5458 std::string TmpString, StrEncoding;
5459 Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI);
5460 QuoteDoublequotes(TmpString, StrEncoding);
5461 Result += StrEncoding;
5463 RewriteIvarOffsetComputation((*IVI), Result);
5467 Result += "\t }\n};\n";
5470 // Build _objc_method_list for class's instance methods if needed
5471 SmallVector<ObjCMethodDecl *, 32>
5472 InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end());
5474 // If any of our property implementations have associated getters or
5475 // setters, produce metadata for them as well.
5476 for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(),
5477 PropEnd = IDecl->propimpl_end();
5478 Prop != PropEnd; ++Prop) {
5479 if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
5481 if (!(*Prop)->getPropertyIvarDecl())
5483 ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl();
5486 if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl())
5487 if (!Getter->isDefined())
5488 InstanceMethods.push_back(Getter);
5489 if (PD->isReadOnly())
5491 if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl())
5492 if (!Setter->isDefined())
5493 InstanceMethods.push_back(Setter);
5495 RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(),
5496 true, "", IDecl->getName(), Result);
5498 // Build _objc_method_list for class's class methods if needed
5499 RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(),
5500 false, "", IDecl->getName(), Result);
5502 // Protocols referenced in class declaration?
5503 RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(),
5504 "CLASS", CDecl->getName(), Result);
5506 // Declaration of class/meta-class metadata
5507 /* struct _objc_class {
5508 struct _objc_class *isa; // or const char *root_class_name when metadata
5509 const char *super_class_name;
5514 struct _objc_ivar_list *ivars;
5515 struct _objc_method_list *methods;
5516 struct objc_cache *cache;
5517 struct objc_protocol_list *protocols;
5518 const char *ivar_layout;
5519 struct _objc_class_ext *ext;
5522 static bool objc_class = false;
5524 Result += "\nstruct _objc_class {\n";
5525 Result += "\tstruct _objc_class *isa;\n";
5526 Result += "\tconst char *super_class_name;\n";
5527 Result += "\tchar *name;\n";
5528 Result += "\tlong version;\n";
5529 Result += "\tlong info;\n";
5530 Result += "\tlong instance_size;\n";
5531 Result += "\tstruct _objc_ivar_list *ivars;\n";
5532 Result += "\tstruct _objc_method_list *methods;\n";
5533 Result += "\tstruct objc_cache *cache;\n";
5534 Result += "\tstruct _objc_protocol_list *protocols;\n";
5535 Result += "\tconst char *ivar_layout;\n";
5536 Result += "\tstruct _objc_class_ext *ext;\n";
5541 // Meta-class metadata generation.
5542 ObjCInterfaceDecl *RootClass = 0;
5543 ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass();
5544 while (SuperClass) {
5545 RootClass = SuperClass;
5546 SuperClass = SuperClass->getSuperClass();
5548 SuperClass = CDecl->getSuperClass();
5550 Result += "\nstatic struct _objc_class _OBJC_METACLASS_";
5551 Result += CDecl->getNameAsString();
5552 Result += " __attribute__ ((used, section (\"__OBJC, __meta_class\")))= "
5553 "{\n\t(struct _objc_class *)\"";
5554 Result += (RootClass ? RootClass->getNameAsString() : CDecl->getNameAsString());
5559 Result += SuperClass->getNameAsString();
5561 Result += CDecl->getNameAsString();
5565 Result += ", 0, \"";
5566 Result += CDecl->getNameAsString();
5569 // Set 'ivars' field for root class to 0. ObjC1 runtime does not use it.
5570 // 'info' field is initialized to CLS_META(2) for metaclass
5571 Result += ", 0,2, sizeof(struct _objc_class), 0";
5572 if (IDecl->classmeth_begin() != IDecl->classmeth_end()) {
5573 Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_";
5574 Result += IDecl->getNameAsString();
5579 if (CDecl->protocol_begin() != CDecl->protocol_end()) {
5580 Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_";
5581 Result += CDecl->getNameAsString();
5585 Result += "\t,0,0,0,0\n";
5588 // class metadata generation.
5589 Result += "\nstatic struct _objc_class _OBJC_CLASS_";
5590 Result += CDecl->getNameAsString();
5591 Result += " __attribute__ ((used, section (\"__OBJC, __class\")))= "
5592 "{\n\t&_OBJC_METACLASS_";
5593 Result += CDecl->getNameAsString();
5596 Result += SuperClass->getNameAsString();
5598 Result += CDecl->getNameAsString();
5602 Result += ", 0, \"";
5603 Result += CDecl->getNameAsString();
5606 // 'info' field is initialized to CLS_CLASS(1) for class
5608 if (!ObjCSynthesizedStructs.count(CDecl))
5611 // class has size. Must synthesize its size.
5612 Result += ",sizeof(struct ";
5613 Result += CDecl->getNameAsString();
5614 if (LangOpts.MicrosoftExt)
5619 Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_";
5620 Result += CDecl->getNameAsString();
5625 if (IDecl->instmeth_begin() != IDecl->instmeth_end()) {
5626 Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_";
5627 Result += CDecl->getNameAsString();
5628 Result += ", 0\n\t";
5632 if (CDecl->protocol_begin() != CDecl->protocol_end()) {
5633 Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_";
5634 Result += CDecl->getNameAsString();
5635 Result += ", 0,0\n";
5638 Result += ",0,0,0\n";
5642 void RewriteObjCFragileABI::RewriteMetaDataIntoBuffer(std::string &Result) {
5643 int ClsDefCount = ClassImplementation.size();
5644 int CatDefCount = CategoryImplementation.size();
5646 // For each implemented class, write out all its meta data.
5647 for (int i = 0; i < ClsDefCount; i++)
5648 RewriteObjCClassMetaData(ClassImplementation[i], Result);
5650 // For each implemented category, write out all its meta data.
5651 for (int i = 0; i < CatDefCount; i++)
5652 RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result);
5654 // Write objc_symtab metadata
5662 void *defs[cls_def_cnt + cat_def_cnt];
5666 Result += "\nstruct _objc_symtab {\n";
5667 Result += "\tlong sel_ref_cnt;\n";
5668 Result += "\tSEL *refs;\n";
5669 Result += "\tshort cls_def_cnt;\n";
5670 Result += "\tshort cat_def_cnt;\n";
5671 Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n";
5674 Result += "static struct _objc_symtab "
5675 "_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n";
5676 Result += "\t0, 0, " + utostr(ClsDefCount)
5677 + ", " + utostr(CatDefCount) + "\n";
5678 for (int i = 0; i < ClsDefCount; i++) {
5679 Result += "\t,&_OBJC_CLASS_";
5680 Result += ClassImplementation[i]->getNameAsString();
5684 for (int i = 0; i < CatDefCount; i++) {
5685 Result += "\t,&_OBJC_CATEGORY_";
5686 Result += CategoryImplementation[i]->getClassInterface()->getNameAsString();
5688 Result += CategoryImplementation[i]->getNameAsString();
5694 // Write objc_module metadata
5697 struct _objc_module {
5701 struct _objc_symtab *symtab;
5705 Result += "\nstruct _objc_module {\n";
5706 Result += "\tlong version;\n";
5707 Result += "\tlong size;\n";
5708 Result += "\tconst char *name;\n";
5709 Result += "\tstruct _objc_symtab *symtab;\n";
5711 Result += "static struct _objc_module "
5712 "_OBJC_MODULES __attribute__ ((used, section (\"__OBJC, __module_info\")))= {\n";
5713 Result += "\t" + utostr(OBJC_ABI_VERSION) +
5714 ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n";
5717 if (LangOpts.MicrosoftExt) {
5718 if (ProtocolExprDecls.size()) {
5719 Result += "#pragma section(\".objc_protocol$B\",long,read,write)\n";
5720 Result += "#pragma data_seg(push, \".objc_protocol$B\")\n";
5721 for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(),
5722 E = ProtocolExprDecls.end(); I != E; ++I) {
5723 Result += "static struct _objc_protocol *_POINTER_OBJC_PROTOCOL_";
5724 Result += (*I)->getNameAsString();
5725 Result += " = &_OBJC_PROTOCOL_";
5726 Result += (*I)->getNameAsString();
5729 Result += "#pragma data_seg(pop)\n\n";
5731 Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n";
5732 Result += "#pragma data_seg(push, \".objc_module_info$B\")\n";
5733 Result += "static struct _objc_module *_POINTER_OBJC_MODULES = ";
5734 Result += "&_OBJC_MODULES;\n";
5735 Result += "#pragma data_seg(pop)\n\n";
5739 /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category
5741 void RewriteObjCFragileABI::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl,
5742 std::string &Result) {
5743 ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface();
5744 // Find category declaration for this implementation.
5745 ObjCCategoryDecl *CDecl;
5746 for (CDecl = ClassDecl->getCategoryList(); CDecl;
5747 CDecl = CDecl->getNextClassCategory())
5748 if (CDecl->getIdentifier() == IDecl->getIdentifier())
5751 std::string FullCategoryName = ClassDecl->getNameAsString();
5752 FullCategoryName += '_';
5753 FullCategoryName += IDecl->getNameAsString();
5755 // Build _objc_method_list for class's instance methods if needed
5756 SmallVector<ObjCMethodDecl *, 32>
5757 InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end());
5759 // If any of our property implementations have associated getters or
5760 // setters, produce metadata for them as well.
5761 for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(),
5762 PropEnd = IDecl->propimpl_end();
5763 Prop != PropEnd; ++Prop) {
5764 if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
5766 if (!(*Prop)->getPropertyIvarDecl())
5768 ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl();
5771 if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl())
5772 InstanceMethods.push_back(Getter);
5773 if (PD->isReadOnly())
5775 if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl())
5776 InstanceMethods.push_back(Setter);
5778 RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(),
5779 true, "CATEGORY_", FullCategoryName.c_str(),
5782 // Build _objc_method_list for class's class methods if needed
5783 RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(),
5784 false, "CATEGORY_", FullCategoryName.c_str(),
5787 // Protocols referenced in class declaration?
5788 // Null CDecl is case of a category implementation with no category interface
5790 RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), "CATEGORY",
5791 FullCategoryName, Result);
5792 /* struct _objc_category {
5793 char *category_name;
5795 struct _objc_method_list *instance_methods;
5796 struct _objc_method_list *class_methods;
5797 struct _objc_protocol_list *protocols;
5798 // Objective-C 1.0 extensions
5799 uint32_t size; // sizeof (struct _objc_category)
5800 struct _objc_property_list *instance_properties; // category's own
5805 static bool objc_category = false;
5806 if (!objc_category) {
5807 Result += "\nstruct _objc_category {\n";
5808 Result += "\tchar *category_name;\n";
5809 Result += "\tchar *class_name;\n";
5810 Result += "\tstruct _objc_method_list *instance_methods;\n";
5811 Result += "\tstruct _objc_method_list *class_methods;\n";
5812 Result += "\tstruct _objc_protocol_list *protocols;\n";
5813 Result += "\tunsigned int size;\n";
5814 Result += "\tstruct _objc_property_list *instance_properties;\n";
5816 objc_category = true;
5818 Result += "\nstatic struct _objc_category _OBJC_CATEGORY_";
5819 Result += FullCategoryName;
5820 Result += " __attribute__ ((used, section (\"__OBJC, __category\")))= {\n\t\"";
5821 Result += IDecl->getNameAsString();
5822 Result += "\"\n\t, \"";
5823 Result += ClassDecl->getNameAsString();
5826 if (IDecl->instmeth_begin() != IDecl->instmeth_end()) {
5827 Result += "\t, (struct _objc_method_list *)"
5828 "&_OBJC_CATEGORY_INSTANCE_METHODS_";
5829 Result += FullCategoryName;
5833 Result += "\t, 0\n";
5834 if (IDecl->classmeth_begin() != IDecl->classmeth_end()) {
5835 Result += "\t, (struct _objc_method_list *)"
5836 "&_OBJC_CATEGORY_CLASS_METHODS_";
5837 Result += FullCategoryName;
5841 Result += "\t, 0\n";
5843 if (CDecl && CDecl->protocol_begin() != CDecl->protocol_end()) {
5844 Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_";
5845 Result += FullCategoryName;
5849 Result += "\t, 0\n";
5850 Result += "\t, sizeof(struct _objc_category), 0\n};\n";
5853 // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or
5855 template<typename MethodIterator>
5856 void RewriteObjCFragileABI::RewriteObjCMethodsMetaData(MethodIterator MethodBegin,
5857 MethodIterator MethodEnd,
5858 bool IsInstanceMethod,
5860 StringRef ClassName,
5861 std::string &Result) {
5862 if (MethodBegin == MethodEnd) return;
5864 if (!objc_impl_method) {
5865 /* struct _objc_method {
5871 Result += "\nstruct _objc_method {\n";
5872 Result += "\tSEL _cmd;\n";
5873 Result += "\tchar *method_types;\n";
5874 Result += "\tvoid *_imp;\n";
5877 objc_impl_method = true;
5880 // Build _objc_method_list for class's methods if needed
5883 struct _objc_method_list *next_method;
5885 struct _objc_method method_list[];
5888 unsigned NumMethods = std::distance(MethodBegin, MethodEnd);
5889 Result += "\nstatic struct {\n";
5890 Result += "\tstruct _objc_method_list *next_method;\n";
5891 Result += "\tint method_count;\n";
5892 Result += "\tstruct _objc_method method_list[";
5893 Result += utostr(NumMethods);
5894 Result += "];\n} _OBJC_";
5896 Result += IsInstanceMethod ? "INSTANCE" : "CLASS";
5897 Result += "_METHODS_";
5898 Result += ClassName;
5899 Result += " __attribute__ ((used, section (\"__OBJC, __";
5900 Result += IsInstanceMethod ? "inst" : "cls";
5901 Result += "_meth\")))= ";
5902 Result += "{\n\t0, " + utostr(NumMethods) + "\n";
5904 Result += "\t,{{(SEL)\"";
5905 Result += (*MethodBegin)->getSelector().getAsString().c_str();
5906 std::string MethodTypeString;
5907 Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString);
5909 Result += MethodTypeString;
5910 Result += "\", (void *)";
5911 Result += MethodInternalNames[*MethodBegin];
5913 for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) {
5914 Result += "\t ,{(SEL)\"";
5915 Result += (*MethodBegin)->getSelector().getAsString().c_str();
5916 std::string MethodTypeString;
5917 Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString);
5919 Result += MethodTypeString;
5920 Result += "\", (void *)";
5921 Result += MethodInternalNames[*MethodBegin];
5924 Result += "\t }\n};\n";
5927 Stmt *RewriteObjCFragileABI::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV) {
5928 SourceRange OldRange = IV->getSourceRange();
5929 Expr *BaseExpr = IV->getBase();
5931 // Rewrite the base, but without actually doing replaces.
5933 DisableReplaceStmtScope S(*this);
5934 BaseExpr = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(BaseExpr));
5935 IV->setBase(BaseExpr);
5938 ObjCIvarDecl *D = IV->getDecl();
5940 Expr *Replacement = IV;
5942 if (BaseExpr->getType()->isObjCObjectPointerType()) {
5943 const ObjCInterfaceType *iFaceDecl =
5944 dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType());
5945 assert(iFaceDecl && "RewriteObjCIvarRefExpr - iFaceDecl is null");
5946 // lookup which class implements the instance variable.
5947 ObjCInterfaceDecl *clsDeclared = 0;
5948 iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(),
5950 assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class");
5952 // Synthesize an explicit cast to gain access to the ivar.
5953 std::string RecName = clsDeclared->getIdentifier()->getName();
5955 IdentifierInfo *II = &Context->Idents.get(RecName);
5956 RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl,
5957 SourceLocation(), SourceLocation(),
5959 assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl");
5960 QualType castT = Context->getPointerType(Context->getTagDeclType(RD));
5961 CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT,
5964 // Don't forget the parens to enforce the proper binding.
5965 ParenExpr *PE = new (Context) ParenExpr(OldRange.getBegin(),
5968 if (IV->isFreeIvar() &&
5969 declaresSameEntity(CurMethodDef->getClassInterface(), iFaceDecl->getDecl())) {
5970 MemberExpr *ME = new (Context) MemberExpr(PE, true, D,
5973 VK_LValue, OK_Ordinary);
5979 } else { // we are outside a method.
5980 assert(!IV->isFreeIvar() && "Cannot have a free standing ivar outside a method");
5982 // Explicit ivar refs need to have a cast inserted.
5983 // FIXME: consider sharing some of this code with the code above.
5984 if (BaseExpr->getType()->isObjCObjectPointerType()) {
5985 const ObjCInterfaceType *iFaceDecl =
5986 dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType());
5987 // lookup which class implements the instance variable.
5988 ObjCInterfaceDecl *clsDeclared = 0;
5989 iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(),
5991 assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class");
5993 // Synthesize an explicit cast to gain access to the ivar.
5994 std::string RecName = clsDeclared->getIdentifier()->getName();
5996 IdentifierInfo *II = &Context->Idents.get(RecName);
5997 RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl,
5998 SourceLocation(), SourceLocation(),
6000 assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl");
6001 QualType castT = Context->getPointerType(Context->getTagDeclType(RD));
6002 CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT,
6005 // Don't forget the parens to enforce the proper binding.
6006 ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(),
6007 IV->getBase()->getLocEnd(), castExpr);
6008 // Cannot delete IV->getBase(), since PE points to it.
6009 // Replace the old base with the cast. This is important when doing
6010 // embedded rewrites. For example, [newInv->_container addObject:0].
6015 ReplaceStmtWithRange(IV, Replacement, OldRange);