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 {
36 BLOCK_FIELD_IS_OBJECT = 3, /* id, NSObject, __attribute__((NSObject)),
38 BLOCK_FIELD_IS_BLOCK = 7, /* a block variable */
39 BLOCK_FIELD_IS_BYREF = 8, /* the on stack structure holding the
41 BLOCK_FIELD_IS_WEAK = 16, /* declared __weak, only used in byref copy
43 BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose
45 BLOCK_BYREF_CURRENT_MAX = 256
49 BLOCK_NEEDS_FREE = (1 << 24),
50 BLOCK_HAS_COPY_DISPOSE = (1 << 25),
51 BLOCK_HAS_CXX_OBJ = (1 << 26),
52 BLOCK_IS_GC = (1 << 27),
53 BLOCK_IS_GLOBAL = (1 << 28),
54 BLOCK_HAS_DESCRIPTOR = (1 << 29)
58 DiagnosticsEngine &Diags;
59 const LangOptions &LangOpts;
60 unsigned RewriteFailedDiag;
61 unsigned TryFinallyContainsReturnDiag;
65 TranslationUnitDecl *TUDecl;
67 const char *MainFileStart, *MainFileEnd;
68 SourceLocation LastIncLoc;
70 SmallVector<ObjCImplementationDecl *, 8> ClassImplementation;
71 SmallVector<ObjCCategoryImplDecl *, 8> CategoryImplementation;
72 llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCSynthesizedStructs;
73 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> ObjCSynthesizedProtocols;
74 llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCForwardDecls;
75 llvm::DenseMap<ObjCMethodDecl*, std::string> MethodInternalNames;
76 SmallVector<Stmt *, 32> Stmts;
77 SmallVector<int, 8> ObjCBcLabelNo;
78 // Remember all the @protocol(<expr>) expressions.
79 llvm::SmallPtrSet<ObjCProtocolDecl *, 32> ProtocolExprDecls;
81 llvm::DenseSet<uint64_t> CopyDestroyCache;
83 unsigned NumObjCStringLiterals;
85 FunctionDecl *MsgSendFunctionDecl;
86 FunctionDecl *MsgSendSuperFunctionDecl;
87 FunctionDecl *MsgSendStretFunctionDecl;
88 FunctionDecl *MsgSendSuperStretFunctionDecl;
89 FunctionDecl *MsgSendFpretFunctionDecl;
90 FunctionDecl *GetClassFunctionDecl;
91 FunctionDecl *GetMetaClassFunctionDecl;
92 FunctionDecl *GetSuperClassFunctionDecl;
93 FunctionDecl *SelGetUidFunctionDecl;
94 FunctionDecl *CFStringFunctionDecl;
95 FunctionDecl *SuperContructorFunctionDecl;
97 // ObjC string constant support.
98 VarDecl *ConstantStringClassReference;
99 RecordDecl *NSStringRecord;
101 // ObjC foreach break/continue generation support.
105 ObjCMethodDecl *CurMethodDef;
106 RecordDecl *SuperStructDecl;
107 RecordDecl *ConstantStringDecl;
109 TypeDecl *ProtocolTypeDecl;
110 QualType getProtocolType();
112 // Needed for header files being rewritten
115 std::string InFileName;
116 raw_ostream* OutFile;
118 bool SilenceRewriteMacroWarning;
119 bool objc_impl_method;
121 std::string Preamble;
123 // Block expressions.
124 SmallVector<BlockExpr *, 32> Blocks;
125 SmallVector<int, 32> InnerDeclRefsCount;
126 SmallVector<BlockDeclRefExpr *, 32> InnerDeclRefs;
128 SmallVector<BlockDeclRefExpr *, 32> BlockDeclRefs;
130 // Block related declarations.
131 SmallVector<ValueDecl *, 8> BlockByCopyDecls;
132 llvm::SmallPtrSet<ValueDecl *, 8> BlockByCopyDeclsPtrSet;
133 SmallVector<ValueDecl *, 8> BlockByRefDecls;
134 llvm::SmallPtrSet<ValueDecl *, 8> BlockByRefDeclsPtrSet;
135 llvm::DenseMap<ValueDecl *, unsigned> BlockByRefDeclNo;
136 llvm::SmallPtrSet<ValueDecl *, 8> ImportedBlockDecls;
137 llvm::SmallPtrSet<VarDecl *, 8> ImportedLocalExternalDecls;
139 llvm::DenseMap<BlockExpr *, std::string> RewrittenBlockExprs;
141 // This maps a property to it's assignment statement.
142 llvm::DenseMap<Expr *, BinaryOperator *> PropSetters;
143 // This maps a property to it's synthesied message expression.
144 // This allows us to rewrite chained getters (e.g. o.a.b.c).
145 llvm::DenseMap<Expr *, Stmt *> PropGetters;
147 // This maps an original source AST to it's rewritten form. This allows
148 // us to avoid rewriting the same node twice (which is very uncommon).
149 // This is needed to support some of the exotic property rewriting.
150 llvm::DenseMap<Stmt *, Stmt *> ReplacedNodes;
152 FunctionDecl *CurFunctionDef;
153 FunctionDecl *CurFunctionDeclToDeclareForBlock;
154 VarDecl *GlobalVarDecl;
156 bool DisableReplaceStmt;
158 static const int OBJC_ABI_VERSION = 7;
160 virtual void Initialize(ASTContext &context);
162 // Top Level Driver code.
163 virtual void HandleTopLevelDecl(DeclGroupRef D) {
164 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) {
165 if (isa<ObjCClassDecl>((*I))) {
166 RewriteForwardClassDecl(D);
169 HandleTopLevelSingleDecl(*I);
172 void HandleTopLevelSingleDecl(Decl *D);
173 void HandleDeclInMainFile(Decl *D);
174 RewriteObjC(std::string inFile, raw_ostream *OS,
175 DiagnosticsEngine &D, const LangOptions &LOpts,
176 bool silenceMacroWarn);
180 virtual void HandleTranslationUnit(ASTContext &C);
182 void ReplaceStmt(Stmt *Old, Stmt *New) {
183 Stmt *ReplacingStmt = ReplacedNodes[Old];
186 return; // We can't rewrite the same node twice.
188 if (DisableReplaceStmt)
189 return; // Used when rewriting the assignment of a property setter.
191 // If replacement succeeded or warning disabled return with no warning.
192 if (!Rewrite.ReplaceStmt(Old, New)) {
193 ReplacedNodes[Old] = New;
196 if (SilenceRewriteMacroWarning)
198 Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag)
199 << Old->getSourceRange();
202 void ReplaceStmtWithRange(Stmt *Old, Stmt *New, SourceRange SrcRange) {
203 // Measure the old text.
204 int Size = Rewrite.getRangeSize(SrcRange);
206 Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag)
207 << Old->getSourceRange();
212 llvm::raw_string_ostream S(SStr);
213 New->printPretty(S, *Context, 0, PrintingPolicy(LangOpts));
214 const std::string &Str = S.str();
216 // If replacement succeeded or warning disabled return with no warning.
217 if (!Rewrite.ReplaceText(SrcRange.getBegin(), Size, Str)) {
218 ReplacedNodes[Old] = New;
221 if (SilenceRewriteMacroWarning)
223 Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag)
224 << Old->getSourceRange();
227 void InsertText(SourceLocation Loc, StringRef Str,
228 bool InsertAfter = true) {
229 // If insertion succeeded or warning disabled return with no warning.
230 if (!Rewrite.InsertText(Loc, Str, InsertAfter) ||
231 SilenceRewriteMacroWarning)
234 Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag);
237 void ReplaceText(SourceLocation Start, unsigned OrigLength,
239 // If removal succeeded or warning disabled return with no warning.
240 if (!Rewrite.ReplaceText(Start, OrigLength, Str) ||
241 SilenceRewriteMacroWarning)
244 Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag);
247 // Syntactic Rewriting.
248 void RewriteInclude();
249 void RewriteForwardClassDecl(DeclGroupRef D);
250 void RewriteForwardClassDecl(const llvm::SmallVector<Decl*, 8> &DG);
251 void RewriteForwardClassEpilogue(ObjCClassDecl *ClassDecl,
252 const std::string &typedefString);
254 void RewritePropertyImplDecl(ObjCPropertyImplDecl *PID,
255 ObjCImplementationDecl *IMD,
256 ObjCCategoryImplDecl *CID);
257 void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl);
258 void RewriteImplementationDecl(Decl *Dcl);
259 void RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl,
260 ObjCMethodDecl *MDecl, std::string &ResultStr);
261 void RewriteTypeIntoString(QualType T, std::string &ResultStr,
262 const FunctionType *&FPRetType);
263 void RewriteByRefString(std::string &ResultStr, const std::string &Name,
264 ValueDecl *VD, bool def=false);
265 void RewriteCategoryDecl(ObjCCategoryDecl *Dcl);
266 void RewriteProtocolDecl(ObjCProtocolDecl *Dcl);
267 void RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *Dcl);
268 void RewriteMethodDeclaration(ObjCMethodDecl *Method);
269 void RewriteProperty(ObjCPropertyDecl *prop);
270 void RewriteFunctionDecl(FunctionDecl *FD);
271 void RewriteBlockPointerType(std::string& Str, QualType Type);
272 void RewriteBlockPointerTypeVariable(std::string& Str, ValueDecl *VD);
273 void RewriteBlockLiteralFunctionDecl(FunctionDecl *FD);
274 void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl);
275 void RewriteTypeOfDecl(VarDecl *VD);
276 void RewriteObjCQualifiedInterfaceTypes(Expr *E);
277 bool needToScanForQualifiers(QualType T);
278 QualType getSuperStructType();
279 QualType getConstantStringStructType();
280 QualType convertFunctionTypeOfBlocks(const FunctionType *FT);
281 bool BufferContainsPPDirectives(const char *startBuf, const char *endBuf);
283 // Expression Rewriting.
284 Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S);
285 void CollectPropertySetters(Stmt *S);
288 ParentMap *PropParentMap; // created lazily.
290 Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp);
291 Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, SourceLocation OrigStart,
293 Stmt *RewriteObjCNestedIvarRefExpr(Stmt *S, bool &replaced);
294 Stmt *RewritePropertyOrImplicitGetter(Expr *PropOrGetterRefExpr);
295 Stmt *RewritePropertyOrImplicitSetter(BinaryOperator *BinOp, Expr *newStmt,
296 SourceRange SrcRange);
297 Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp);
298 Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp);
299 Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp);
300 Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp);
301 void WarnAboutReturnGotoStmts(Stmt *S);
302 void HasReturnStmts(Stmt *S, bool &hasReturns);
303 void RewriteTryReturnStmts(Stmt *S);
304 void RewriteSyncReturnStmts(Stmt *S, std::string buf);
305 Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S);
306 Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S);
307 Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S);
308 Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S,
309 SourceLocation OrigEnd);
310 bool IsDeclStmtInForeachHeader(DeclStmt *DS);
311 CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD,
312 Expr **args, unsigned nargs,
313 SourceLocation StartLoc=SourceLocation(),
314 SourceLocation EndLoc=SourceLocation());
315 Stmt *SynthMessageExpr(ObjCMessageExpr *Exp,
316 SourceLocation StartLoc=SourceLocation(),
317 SourceLocation EndLoc=SourceLocation());
318 Stmt *RewriteBreakStmt(BreakStmt *S);
319 Stmt *RewriteContinueStmt(ContinueStmt *S);
320 void SynthCountByEnumWithState(std::string &buf);
322 void SynthMsgSendFunctionDecl();
323 void SynthMsgSendSuperFunctionDecl();
324 void SynthMsgSendStretFunctionDecl();
325 void SynthMsgSendFpretFunctionDecl();
326 void SynthMsgSendSuperStretFunctionDecl();
327 void SynthGetClassFunctionDecl();
328 void SynthGetMetaClassFunctionDecl();
329 void SynthGetSuperClassFunctionDecl();
330 void SynthSelGetUidFunctionDecl();
331 void SynthSuperContructorFunctionDecl();
333 // Metadata emission.
334 void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl,
335 std::string &Result);
337 void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl,
338 std::string &Result);
340 template<typename MethodIterator>
341 void RewriteObjCMethodsMetaData(MethodIterator MethodBegin,
342 MethodIterator MethodEnd,
343 bool IsInstanceMethod,
346 std::string &Result);
348 void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol,
351 std::string &Result);
352 void RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Prots,
355 std::string &Result);
356 void SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl,
357 std::string &Result);
358 void SynthesizeIvarOffsetComputation(ObjCIvarDecl *ivar,
359 std::string &Result);
360 void RewriteImplementations();
361 void SynthesizeMetaDataIntoBuffer(std::string &Result);
364 void RewriteBlocksInFunctionProtoType(QualType funcType, NamedDecl *D);
365 void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND);
367 void InsertBlockLiteralsWithinFunction(FunctionDecl *FD);
368 void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD);
370 // Block specific rewrite rules.
371 void RewriteBlockPointerDecl(NamedDecl *VD);
372 void RewriteByRefVar(VarDecl *VD);
373 std::string SynthesizeByrefCopyDestroyHelper(VarDecl *VD, int flag);
374 Stmt *RewriteBlockDeclRefExpr(Expr *VD);
375 Stmt *RewriteLocalVariableExternalStorage(DeclRefExpr *DRE);
376 void RewriteBlockPointerFunctionArgs(FunctionDecl *FD);
378 std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i,
379 StringRef funcName, std::string Tag);
380 std::string SynthesizeBlockFunc(BlockExpr *CE, int i,
381 StringRef funcName, std::string Tag);
382 std::string SynthesizeBlockImpl(BlockExpr *CE,
383 std::string Tag, std::string Desc);
384 std::string SynthesizeBlockDescriptor(std::string DescTag,
386 int i, StringRef funcName,
388 Stmt *SynthesizeBlockCall(CallExpr *Exp, const Expr* BlockExp);
389 void SynthesizeBlockLiterals(SourceLocation FunLocStart,
391 void RewriteRecordBody(RecordDecl *RD);
393 void CollectBlockDeclRefInfo(BlockExpr *Exp);
394 void GetBlockDeclRefExprs(Stmt *S);
395 void GetInnerBlockDeclRefExprs(Stmt *S,
396 SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs,
397 llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts);
399 // We avoid calling Type::isBlockPointerType(), since it operates on the
400 // canonical type. We only care if the top-level type is a closure pointer.
401 bool isTopLevelBlockPointerType(QualType T) {
402 return isa<BlockPointerType>(T);
405 /// convertBlockPointerToFunctionPointer - Converts a block-pointer type
406 /// to a function pointer type and upon success, returns true; false
408 bool convertBlockPointerToFunctionPointer(QualType &T) {
409 if (isTopLevelBlockPointerType(T)) {
410 const BlockPointerType *BPT = T->getAs<BlockPointerType>();
411 T = Context->getPointerType(BPT->getPointeeType());
417 void convertToUnqualifiedObjCType(QualType &T) {
418 if (T->isObjCQualifiedIdType())
419 T = Context->getObjCIdType();
420 else if (T->isObjCQualifiedClassType())
421 T = Context->getObjCClassType();
422 else if (T->isObjCObjectPointerType() &&
423 T->getPointeeType()->isObjCQualifiedInterfaceType()) {
424 if (const ObjCObjectPointerType * OBJPT =
425 T->getAsObjCInterfacePointerType()) {
426 const ObjCInterfaceType *IFaceT = OBJPT->getInterfaceType();
427 T = QualType(IFaceT, 0);
428 T = Context->getPointerType(T);
433 // FIXME: This predicate seems like it would be useful to add to ASTContext.
434 bool isObjCType(QualType T) {
435 if (!LangOpts.ObjC1 && !LangOpts.ObjC2)
438 QualType OCT = Context->getCanonicalType(T).getUnqualifiedType();
440 if (OCT == Context->getCanonicalType(Context->getObjCIdType()) ||
441 OCT == Context->getCanonicalType(Context->getObjCClassType()))
444 if (const PointerType *PT = OCT->getAs<PointerType>()) {
445 if (isa<ObjCInterfaceType>(PT->getPointeeType()) ||
446 PT->getPointeeType()->isObjCQualifiedIdType())
451 bool PointerTypeTakesAnyBlockArguments(QualType QT);
452 bool PointerTypeTakesAnyObjCQualifiedType(QualType QT);
453 void GetExtentOfArgList(const char *Name, const char *&LParen,
454 const char *&RParen);
455 void RewriteCastExpr(CStyleCastExpr *CE);
457 FunctionDecl *SynthBlockInitFunctionDecl(StringRef name);
458 Stmt *SynthBlockInitExpr(BlockExpr *Exp,
459 const SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs);
461 void QuoteDoublequotes(std::string &From, std::string &To) {
462 for (unsigned i = 0; i < From.length(); i++) {
470 QualType getSimpleFunctionType(QualType result,
471 const QualType *args,
473 bool variadic = false) {
474 if (result == Context->getObjCInstanceType())
475 result = Context->getObjCIdType();
476 FunctionProtoType::ExtProtoInfo fpi;
477 fpi.Variadic = variadic;
478 return Context->getFunctionType(result, args, numArgs, fpi);
482 // Helper function: create a CStyleCastExpr with trivial type source info.
483 CStyleCastExpr* NoTypeInfoCStyleCastExpr(ASTContext *Ctx, QualType Ty,
484 CastKind Kind, Expr *E) {
485 TypeSourceInfo *TInfo = Ctx->getTrivialTypeSourceInfo(Ty, SourceLocation());
486 return CStyleCastExpr::Create(*Ctx, Ty, VK_RValue, Kind, E, 0, TInfo,
487 SourceLocation(), SourceLocation());
491 void RewriteObjC::RewriteBlocksInFunctionProtoType(QualType funcType,
493 if (const FunctionProtoType *fproto
494 = dyn_cast<FunctionProtoType>(funcType.IgnoreParens())) {
495 for (FunctionProtoType::arg_type_iterator I = fproto->arg_type_begin(),
496 E = fproto->arg_type_end(); I && (I != E); ++I)
497 if (isTopLevelBlockPointerType(*I)) {
498 // All the args are checked/rewritten. Don't call twice!
499 RewriteBlockPointerDecl(D);
505 void RewriteObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) {
506 const PointerType *PT = funcType->getAs<PointerType>();
507 if (PT && PointerTypeTakesAnyBlockArguments(funcType))
508 RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND);
511 static bool IsHeaderFile(const std::string &Filename) {
512 std::string::size_type DotPos = Filename.rfind('.');
514 if (DotPos == std::string::npos) {
519 std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end());
521 // C++ header: .hh or .H;
522 return Ext == "h" || Ext == "hh" || Ext == "H";
525 RewriteObjC::RewriteObjC(std::string inFile, raw_ostream* OS,
526 DiagnosticsEngine &D, const LangOptions &LOpts,
527 bool silenceMacroWarn)
528 : Diags(D), LangOpts(LOpts), InFileName(inFile), OutFile(OS),
529 SilenceRewriteMacroWarning(silenceMacroWarn) {
530 IsHeader = IsHeaderFile(inFile);
531 RewriteFailedDiag = Diags.getCustomDiagID(DiagnosticsEngine::Warning,
532 "rewriting sub-expression within a macro (may not be correct)");
533 TryFinallyContainsReturnDiag = Diags.getCustomDiagID(
534 DiagnosticsEngine::Warning,
535 "rewriter doesn't support user-specified control flow semantics "
536 "for @try/@finally (code may not execute properly)");
539 ASTConsumer *clang::CreateObjCRewriter(const std::string& InFile,
541 DiagnosticsEngine &Diags,
542 const LangOptions &LOpts,
543 bool SilenceRewriteMacroWarning) {
544 return new RewriteObjC(InFile, OS, Diags, LOpts, SilenceRewriteMacroWarning);
547 void RewriteObjC::Initialize(ASTContext &context) {
549 SM = &Context->getSourceManager();
550 TUDecl = Context->getTranslationUnitDecl();
551 MsgSendFunctionDecl = 0;
552 MsgSendSuperFunctionDecl = 0;
553 MsgSendStretFunctionDecl = 0;
554 MsgSendSuperStretFunctionDecl = 0;
555 MsgSendFpretFunctionDecl = 0;
556 GetClassFunctionDecl = 0;
557 GetMetaClassFunctionDecl = 0;
558 GetSuperClassFunctionDecl = 0;
559 SelGetUidFunctionDecl = 0;
560 CFStringFunctionDecl = 0;
561 ConstantStringClassReference = 0;
565 CurFunctionDeclToDeclareForBlock = 0;
568 ProtocolTypeDecl = 0;
569 ConstantStringDecl = 0;
571 SuperContructorFunctionDecl = 0;
572 NumObjCStringLiterals = 0;
575 DisableReplaceStmt = false;
576 objc_impl_method = false;
578 // Get the ID and start/end of the main file.
579 MainFileID = SM->getMainFileID();
580 const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID);
581 MainFileStart = MainBuf->getBufferStart();
582 MainFileEnd = MainBuf->getBufferEnd();
584 Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOptions());
586 // declaring objc_selector outside the parameter list removes a silly
587 // scope related warning...
589 Preamble = "#pragma once\n";
590 Preamble += "struct objc_selector; struct objc_class;\n";
591 Preamble += "struct __rw_objc_super { struct objc_object *object; ";
592 Preamble += "struct objc_object *superClass; ";
593 if (LangOpts.MicrosoftExt) {
594 // Add a constructor for creating temporary objects.
595 Preamble += "__rw_objc_super(struct objc_object *o, struct objc_object *s) "
597 Preamble += "object(o), superClass(s) {} ";
600 Preamble += "#ifndef _REWRITER_typedef_Protocol\n";
601 Preamble += "typedef struct objc_object Protocol;\n";
602 Preamble += "#define _REWRITER_typedef_Protocol\n";
603 Preamble += "#endif\n";
604 if (LangOpts.MicrosoftExt) {
605 Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n";
606 Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n";
608 Preamble += "#define __OBJC_RW_DLLIMPORT extern\n";
609 Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend";
610 Preamble += "(struct objc_object *, struct objc_selector *, ...);\n";
611 Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper";
612 Preamble += "(struct objc_super *, struct objc_selector *, ...);\n";
613 Preamble += "__OBJC_RW_DLLIMPORT struct objc_object* objc_msgSend_stret";
614 Preamble += "(struct objc_object *, struct objc_selector *, ...);\n";
615 Preamble += "__OBJC_RW_DLLIMPORT struct objc_object* objc_msgSendSuper_stret";
616 Preamble += "(struct objc_super *, struct objc_selector *, ...);\n";
617 Preamble += "__OBJC_RW_DLLIMPORT double objc_msgSend_fpret";
618 Preamble += "(struct objc_object *, struct objc_selector *, ...);\n";
619 Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getClass";
620 Preamble += "(const char *);\n";
621 Preamble += "__OBJC_RW_DLLIMPORT struct objc_class *class_getSuperclass";
622 Preamble += "(struct objc_class *);\n";
623 Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getMetaClass";
624 Preamble += "(const char *);\n";
625 Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw(struct objc_object *);\n";
626 Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_enter(void *);\n";
627 Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_exit(void *);\n";
628 Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_exception_extract(void *);\n";
629 Preamble += "__OBJC_RW_DLLIMPORT int objc_exception_match";
630 Preamble += "(struct objc_class *, struct objc_object *);\n";
631 // @synchronized hooks.
632 Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_enter(struct objc_object *);\n";
633 Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_exit(struct objc_object *);\n";
634 Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n";
635 Preamble += "#ifndef __FASTENUMERATIONSTATE\n";
636 Preamble += "struct __objcFastEnumerationState {\n\t";
637 Preamble += "unsigned long state;\n\t";
638 Preamble += "void **itemsPtr;\n\t";
639 Preamble += "unsigned long *mutationsPtr;\n\t";
640 Preamble += "unsigned long extra[5];\n};\n";
641 Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n";
642 Preamble += "#define __FASTENUMERATIONSTATE\n";
643 Preamble += "#endif\n";
644 Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n";
645 Preamble += "struct __NSConstantStringImpl {\n";
646 Preamble += " int *isa;\n";
647 Preamble += " int flags;\n";
648 Preamble += " char *str;\n";
649 Preamble += " long length;\n";
651 Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n";
652 Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n";
653 Preamble += "#else\n";
654 Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n";
655 Preamble += "#endif\n";
656 Preamble += "#define __NSCONSTANTSTRINGIMPL\n";
657 Preamble += "#endif\n";
659 Preamble += "#ifndef BLOCK_IMPL\n";
660 Preamble += "#define BLOCK_IMPL\n";
661 Preamble += "struct __block_impl {\n";
662 Preamble += " void *isa;\n";
663 Preamble += " int Flags;\n";
664 Preamble += " int Reserved;\n";
665 Preamble += " void *FuncPtr;\n";
667 Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n";
668 Preamble += "#ifdef __OBJC_EXPORT_BLOCKS\n";
669 Preamble += "extern \"C\" __declspec(dllexport) "
670 "void _Block_object_assign(void *, const void *, const int);\n";
671 Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_dispose(const void *, const int);\n";
672 Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteGlobalBlock[32];\n";
673 Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteStackBlock[32];\n";
674 Preamble += "#else\n";
675 Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_assign(void *, const void *, const int);\n";
676 Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_dispose(const void *, const int);\n";
677 Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteGlobalBlock[32];\n";
678 Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteStackBlock[32];\n";
679 Preamble += "#endif\n";
680 Preamble += "#endif\n";
681 if (LangOpts.MicrosoftExt) {
682 Preamble += "#undef __OBJC_RW_DLLIMPORT\n";
683 Preamble += "#undef __OBJC_RW_STATICIMPORT\n";
684 Preamble += "#ifndef KEEP_ATTRIBUTES\n"; // We use this for clang tests.
685 Preamble += "#define __attribute__(X)\n";
686 Preamble += "#endif\n";
687 Preamble += "#define __weak\n";
690 Preamble += "#define __block\n";
691 Preamble += "#define __weak\n";
693 // NOTE! Windows uses LLP64 for 64bit mode. So, cast pointer to long long
694 // as this avoids warning in any 64bit/32bit compilation model.
695 Preamble += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((long long) &((TYPE *)0)->MEMBER)\n";
699 //===----------------------------------------------------------------------===//
700 // Top Level Driver Code
701 //===----------------------------------------------------------------------===//
703 void RewriteObjC::HandleTopLevelSingleDecl(Decl *D) {
704 if (Diags.hasErrorOccurred())
707 // Two cases: either the decl could be in the main file, or it could be in a
708 // #included file. If the former, rewrite it now. If the later, check to see
709 // if we rewrote the #include/#import.
710 SourceLocation Loc = D->getLocation();
711 Loc = SM->getExpansionLoc(Loc);
713 // If this is for a builtin, ignore it.
714 if (Loc.isInvalid()) return;
716 // Look for built-in declarations that we need to refer during the rewrite.
717 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
718 RewriteFunctionDecl(FD);
719 } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) {
720 // declared in <Foundation/NSString.h>
721 if (FVD->getName() == "_NSConstantStringClassReference") {
722 ConstantStringClassReference = FVD;
725 } else if (ObjCInterfaceDecl *MD = dyn_cast<ObjCInterfaceDecl>(D)) {
726 RewriteInterfaceDecl(MD);
727 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) {
728 RewriteCategoryDecl(CD);
729 } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
730 RewriteProtocolDecl(PD);
731 } else if (ObjCForwardProtocolDecl *FP =
732 dyn_cast<ObjCForwardProtocolDecl>(D)){
733 RewriteForwardProtocolDecl(FP);
734 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
735 // Recurse into linkage specifications
736 for (DeclContext::decl_iterator DI = LSD->decls_begin(),
737 DIEnd = LSD->decls_end();
739 if (isa<ObjCClassDecl>((*DI))) {
740 SmallVector<Decl *, 8> DG;
742 SourceLocation Loc = D->getLocation();
743 while (DI != DIEnd &&
744 isa<ObjCClassDecl>(D) && D->getLocation() == Loc) {
749 RewriteForwardClassDecl(DG);
752 HandleTopLevelSingleDecl(*DI);
756 // If we have a decl in the main file, see if we should rewrite it.
757 if (SM->isFromMainFile(Loc))
758 return HandleDeclInMainFile(D);
761 //===----------------------------------------------------------------------===//
762 // Syntactic (non-AST) Rewriting Code
763 //===----------------------------------------------------------------------===//
765 void RewriteObjC::RewriteInclude() {
766 SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID);
767 StringRef MainBuf = SM->getBufferData(MainFileID);
768 const char *MainBufStart = MainBuf.begin();
769 const char *MainBufEnd = MainBuf.end();
770 size_t ImportLen = strlen("import");
772 // Loop over the whole file, looking for includes.
773 for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) {
774 if (*BufPtr == '#') {
775 if (++BufPtr == MainBufEnd)
777 while (*BufPtr == ' ' || *BufPtr == '\t')
778 if (++BufPtr == MainBufEnd)
780 if (!strncmp(BufPtr, "import", ImportLen)) {
781 // replace import with include
782 SourceLocation ImportLoc =
783 LocStart.getLocWithOffset(BufPtr-MainBufStart);
784 ReplaceText(ImportLoc, ImportLen, "include");
791 static std::string getIvarAccessString(ObjCIvarDecl *OID) {
792 const ObjCInterfaceDecl *ClassDecl = OID->getContainingInterface();
795 S += ClassDecl->getIdentifier()->getName();
796 S += "_IMPL *)self)->";
801 void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID,
802 ObjCImplementationDecl *IMD,
803 ObjCCategoryImplDecl *CID) {
804 static bool objcGetPropertyDefined = false;
805 static bool objcSetPropertyDefined = false;
806 SourceLocation startLoc = PID->getLocStart();
807 InsertText(startLoc, "// ");
808 const char *startBuf = SM->getCharacterData(startLoc);
809 assert((*startBuf == '@') && "bogus @synthesize location");
810 const char *semiBuf = strchr(startBuf, ';');
811 assert((*semiBuf == ';') && "@synthesize: can't find ';'");
812 SourceLocation onePastSemiLoc =
813 startLoc.getLocWithOffset(semiBuf-startBuf+1);
815 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
816 return; // FIXME: is this correct?
818 // Generate the 'getter' function.
819 ObjCPropertyDecl *PD = PID->getPropertyDecl();
820 ObjCIvarDecl *OID = PID->getPropertyIvarDecl();
824 unsigned Attributes = PD->getPropertyAttributes();
825 if (!PD->getGetterMethodDecl()->isDefined()) {
826 bool GenGetProperty = !(Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) &&
827 (Attributes & (ObjCPropertyDecl::OBJC_PR_retain |
828 ObjCPropertyDecl::OBJC_PR_copy));
830 if (GenGetProperty && !objcGetPropertyDefined) {
831 objcGetPropertyDefined = true;
832 // FIXME. Is this attribute correct in all cases?
833 Getr = "\nextern \"C\" __declspec(dllimport) "
834 "id objc_getProperty(id, SEL, long, bool);\n";
836 RewriteObjCMethodDecl(OID->getContainingInterface(),
837 PD->getGetterMethodDecl(), Getr);
839 // Synthesize an explicit cast to gain access to the ivar.
840 // See objc-act.c:objc_synthesize_new_getter() for details.
841 if (GenGetProperty) {
842 // return objc_getProperty(self, _cmd, offsetof(ClassDecl, OID), 1)
844 const FunctionType *FPRetType = 0;
845 RewriteTypeIntoString(PD->getGetterMethodDecl()->getResultType(), Getr,
849 Getr += ")"; // close the precedence "scope" for "*".
851 // Now, emit the argument types (if any).
852 if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)){
854 for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) {
856 std::string ParamStr = FT->getArgType(i).getAsString(
857 Context->getPrintingPolicy());
860 if (FT->isVariadic()) {
861 if (FT->getNumArgs()) Getr += ", ";
869 Getr += "return (_TYPE)";
870 Getr += "objc_getProperty(self, _cmd, ";
871 SynthesizeIvarOffsetComputation(OID, Getr);
875 Getr += "return " + getIvarAccessString(OID);
877 InsertText(onePastSemiLoc, Getr);
880 if (PD->isReadOnly() || PD->getSetterMethodDecl()->isDefined())
883 // Generate the 'setter' function.
885 bool GenSetProperty = Attributes & (ObjCPropertyDecl::OBJC_PR_retain |
886 ObjCPropertyDecl::OBJC_PR_copy);
887 if (GenSetProperty && !objcSetPropertyDefined) {
888 objcSetPropertyDefined = true;
889 // FIXME. Is this attribute correct in all cases?
890 Setr = "\nextern \"C\" __declspec(dllimport) "
891 "void objc_setProperty (id, SEL, long, id, bool, bool);\n";
894 RewriteObjCMethodDecl(OID->getContainingInterface(),
895 PD->getSetterMethodDecl(), Setr);
897 // Synthesize an explicit cast to initialize the ivar.
898 // See objc-act.c:objc_synthesize_new_setter() for details.
899 if (GenSetProperty) {
900 Setr += "objc_setProperty (self, _cmd, ";
901 SynthesizeIvarOffsetComputation(OID, Setr);
903 Setr += PD->getName();
905 if (Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic)
909 if (Attributes & ObjCPropertyDecl::OBJC_PR_copy)
915 Setr += getIvarAccessString(OID) + " = ";
916 Setr += PD->getName();
919 InsertText(onePastSemiLoc, Setr);
922 static void RewriteOneForwardClassDecl(ObjCInterfaceDecl *ForwardDecl,
923 std::string &typedefString) {
924 typedefString += "#ifndef _REWRITER_typedef_";
925 typedefString += ForwardDecl->getNameAsString();
926 typedefString += "\n";
927 typedefString += "#define _REWRITER_typedef_";
928 typedefString += ForwardDecl->getNameAsString();
929 typedefString += "\n";
930 typedefString += "typedef struct objc_object ";
931 typedefString += ForwardDecl->getNameAsString();
932 typedefString += ";\n#endif\n";
935 void RewriteObjC::RewriteForwardClassEpilogue(ObjCClassDecl *ClassDecl,
936 const std::string &typedefString) {
937 SourceLocation startLoc = ClassDecl->getLocation();
938 const char *startBuf = SM->getCharacterData(startLoc);
939 const char *semiPtr = strchr(startBuf, ';');
940 // Replace the @class with typedefs corresponding to the classes.
941 ReplaceText(startLoc, semiPtr-startBuf+1, typedefString);
944 void RewriteObjC::RewriteForwardClassDecl(DeclGroupRef D) {
945 std::string typedefString;
946 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) {
947 ObjCClassDecl *ClassDecl = cast<ObjCClassDecl>(*I);
948 ObjCInterfaceDecl *ForwardDecl = ClassDecl->getForwardInterfaceDecl();
949 if (I == D.begin()) {
950 // Translate to typedef's that forward reference structs with the same name
951 // as the class. As a convenience, we include the original declaration
953 typedefString += "// @class ";
954 typedefString += ForwardDecl->getNameAsString();
955 typedefString += ";\n";
957 RewriteOneForwardClassDecl(ForwardDecl, typedefString);
959 DeclGroupRef::iterator I = D.begin();
960 RewriteForwardClassEpilogue(cast<ObjCClassDecl>(*I), typedefString);
963 void RewriteObjC::RewriteForwardClassDecl(
964 const llvm::SmallVector<Decl*, 8> &D) {
965 std::string typedefString;
966 for (unsigned i = 0; i < D.size(); i++) {
967 ObjCClassDecl *ClassDecl = cast<ObjCClassDecl>(D[i]);
968 ObjCInterfaceDecl *ForwardDecl = ClassDecl->getForwardInterfaceDecl();
970 typedefString += "// @class ";
971 typedefString += ForwardDecl->getNameAsString();
972 typedefString += ";\n";
974 RewriteOneForwardClassDecl(ForwardDecl, typedefString);
976 RewriteForwardClassEpilogue(cast<ObjCClassDecl>(D[0]), typedefString);
979 void RewriteObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) {
980 // When method is a synthesized one, such as a getter/setter there is
981 // nothing to rewrite.
982 if (Method->isImplicit())
984 SourceLocation LocStart = Method->getLocStart();
985 SourceLocation LocEnd = Method->getLocEnd();
987 if (SM->getExpansionLineNumber(LocEnd) >
988 SM->getExpansionLineNumber(LocStart)) {
989 InsertText(LocStart, "#if 0\n");
990 ReplaceText(LocEnd, 1, ";\n#endif\n");
992 InsertText(LocStart, "// ");
996 void RewriteObjC::RewriteProperty(ObjCPropertyDecl *prop) {
997 SourceLocation Loc = prop->getAtLoc();
999 ReplaceText(Loc, 0, "// ");
1000 // FIXME: handle properties that are declared across multiple lines.
1003 void RewriteObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) {
1004 SourceLocation LocStart = CatDecl->getLocStart();
1006 // FIXME: handle category headers that are declared across multiple lines.
1007 ReplaceText(LocStart, 0, "// ");
1009 for (ObjCCategoryDecl::prop_iterator I = CatDecl->prop_begin(),
1010 E = CatDecl->prop_end(); I != E; ++I)
1011 RewriteProperty(*I);
1013 for (ObjCCategoryDecl::instmeth_iterator
1014 I = CatDecl->instmeth_begin(), E = CatDecl->instmeth_end();
1016 RewriteMethodDeclaration(*I);
1017 for (ObjCCategoryDecl::classmeth_iterator
1018 I = CatDecl->classmeth_begin(), E = CatDecl->classmeth_end();
1020 RewriteMethodDeclaration(*I);
1022 // Lastly, comment out the @end.
1023 ReplaceText(CatDecl->getAtEndRange().getBegin(),
1024 strlen("@end"), "/* @end */");
1027 void RewriteObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) {
1028 SourceLocation LocStart = PDecl->getLocStart();
1030 // FIXME: handle protocol headers that are declared across multiple lines.
1031 ReplaceText(LocStart, 0, "// ");
1033 for (ObjCProtocolDecl::instmeth_iterator
1034 I = PDecl->instmeth_begin(), E = PDecl->instmeth_end();
1036 RewriteMethodDeclaration(*I);
1037 for (ObjCProtocolDecl::classmeth_iterator
1038 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
1040 RewriteMethodDeclaration(*I);
1042 for (ObjCInterfaceDecl::prop_iterator I = PDecl->prop_begin(),
1043 E = PDecl->prop_end(); I != E; ++I)
1044 RewriteProperty(*I);
1046 // Lastly, comment out the @end.
1047 SourceLocation LocEnd = PDecl->getAtEndRange().getBegin();
1048 ReplaceText(LocEnd, strlen("@end"), "/* @end */");
1050 // Must comment out @optional/@required
1051 const char *startBuf = SM->getCharacterData(LocStart);
1052 const char *endBuf = SM->getCharacterData(LocEnd);
1053 for (const char *p = startBuf; p < endBuf; p++) {
1054 if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) {
1055 SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf);
1056 ReplaceText(OptionalLoc, strlen("@optional"), "/* @optional */");
1059 else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) {
1060 SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf);
1061 ReplaceText(OptionalLoc, strlen("@required"), "/* @required */");
1067 void RewriteObjC::RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *PDecl) {
1068 SourceLocation LocStart = PDecl->getLocation();
1069 if (LocStart.isInvalid())
1070 llvm_unreachable("Invalid SourceLocation");
1071 // FIXME: handle forward protocol that are declared across multiple lines.
1072 ReplaceText(LocStart, 0, "// ");
1075 void RewriteObjC::RewriteTypeIntoString(QualType T, std::string &ResultStr,
1076 const FunctionType *&FPRetType) {
1077 if (T->isObjCQualifiedIdType())
1079 else if (T->isFunctionPointerType() ||
1080 T->isBlockPointerType()) {
1081 // needs special handling, since pointer-to-functions have special
1082 // syntax (where a decaration models use).
1083 QualType retType = T;
1085 if (const PointerType* PT = retType->getAs<PointerType>())
1086 PointeeTy = PT->getPointeeType();
1087 else if (const BlockPointerType *BPT = retType->getAs<BlockPointerType>())
1088 PointeeTy = BPT->getPointeeType();
1089 if ((FPRetType = PointeeTy->getAs<FunctionType>())) {
1090 ResultStr += FPRetType->getResultType().getAsString(
1091 Context->getPrintingPolicy());
1095 ResultStr += T.getAsString(Context->getPrintingPolicy());
1098 void RewriteObjC::RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl,
1099 ObjCMethodDecl *OMD,
1100 std::string &ResultStr) {
1101 //fprintf(stderr,"In RewriteObjCMethodDecl\n");
1102 const FunctionType *FPRetType = 0;
1103 ResultStr += "\nstatic ";
1104 RewriteTypeIntoString(OMD->getResultType(), ResultStr, FPRetType);
1107 // Unique method name
1108 std::string NameStr;
1110 if (OMD->isInstanceMethod())
1115 NameStr += IDecl->getNameAsString();
1118 if (ObjCCategoryImplDecl *CID =
1119 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1120 NameStr += CID->getNameAsString();
1123 // Append selector names, replacing ':' with '_'
1125 std::string selString = OMD->getSelector().getAsString();
1126 int len = selString.size();
1127 for (int i = 0; i < len; i++)
1128 if (selString[i] == ':')
1130 NameStr += selString;
1132 // Remember this name for metadata emission
1133 MethodInternalNames[OMD] = NameStr;
1134 ResultStr += NameStr;
1136 // Rewrite arguments
1139 // invisible arguments
1140 if (OMD->isInstanceMethod()) {
1141 QualType selfTy = Context->getObjCInterfaceType(IDecl);
1142 selfTy = Context->getPointerType(selfTy);
1143 if (!LangOpts.MicrosoftExt) {
1144 if (ObjCSynthesizedStructs.count(const_cast<ObjCInterfaceDecl*>(IDecl)))
1145 ResultStr += "struct ";
1147 // When rewriting for Microsoft, explicitly omit the structure name.
1148 ResultStr += IDecl->getNameAsString();
1152 ResultStr += Context->getObjCClassType().getAsString(
1153 Context->getPrintingPolicy());
1155 ResultStr += " self, ";
1156 ResultStr += Context->getObjCSelType().getAsString(Context->getPrintingPolicy());
1157 ResultStr += " _cmd";
1159 // Method arguments.
1160 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
1161 E = OMD->param_end(); PI != E; ++PI) {
1162 ParmVarDecl *PDecl = *PI;
1164 if (PDecl->getType()->isObjCQualifiedIdType()) {
1166 ResultStr += PDecl->getNameAsString();
1168 std::string Name = PDecl->getNameAsString();
1169 QualType QT = PDecl->getType();
1170 // Make sure we convert "t (^)(...)" to "t (*)(...)".
1171 if (convertBlockPointerToFunctionPointer(QT))
1172 QT.getAsStringInternal(Name, Context->getPrintingPolicy());
1174 PDecl->getType().getAsStringInternal(Name, Context->getPrintingPolicy());
1178 if (OMD->isVariadic())
1179 ResultStr += ", ...";
1183 ResultStr += ")"; // close the precedence "scope" for "*".
1185 // Now, emit the argument types (if any).
1186 if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) {
1188 for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) {
1189 if (i) ResultStr += ", ";
1190 std::string ParamStr = FT->getArgType(i).getAsString(
1191 Context->getPrintingPolicy());
1192 ResultStr += ParamStr;
1194 if (FT->isVariadic()) {
1195 if (FT->getNumArgs()) ResultStr += ", ";
1204 void RewriteObjC::RewriteImplementationDecl(Decl *OID) {
1205 ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID);
1206 ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID);
1208 InsertText(IMD ? IMD->getLocStart() : CID->getLocStart(), "// ");
1210 for (ObjCCategoryImplDecl::instmeth_iterator
1211 I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(),
1212 E = IMD ? IMD->instmeth_end() : CID->instmeth_end();
1214 std::string ResultStr;
1215 ObjCMethodDecl *OMD = *I;
1216 RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr);
1217 SourceLocation LocStart = OMD->getLocStart();
1218 SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart();
1220 const char *startBuf = SM->getCharacterData(LocStart);
1221 const char *endBuf = SM->getCharacterData(LocEnd);
1222 ReplaceText(LocStart, endBuf-startBuf, ResultStr);
1225 for (ObjCCategoryImplDecl::classmeth_iterator
1226 I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(),
1227 E = IMD ? IMD->classmeth_end() : CID->classmeth_end();
1229 std::string ResultStr;
1230 ObjCMethodDecl *OMD = *I;
1231 RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr);
1232 SourceLocation LocStart = OMD->getLocStart();
1233 SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart();
1235 const char *startBuf = SM->getCharacterData(LocStart);
1236 const char *endBuf = SM->getCharacterData(LocEnd);
1237 ReplaceText(LocStart, endBuf-startBuf, ResultStr);
1239 for (ObjCCategoryImplDecl::propimpl_iterator
1240 I = IMD ? IMD->propimpl_begin() : CID->propimpl_begin(),
1241 E = IMD ? IMD->propimpl_end() : CID->propimpl_end();
1243 RewritePropertyImplDecl(*I, IMD, CID);
1246 InsertText(IMD ? IMD->getLocEnd() : CID->getLocEnd(), "// ");
1249 void RewriteObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) {
1250 std::string ResultStr;
1251 if (!ObjCForwardDecls.count(ClassDecl)) {
1252 // we haven't seen a forward decl - generate a typedef.
1253 ResultStr = "#ifndef _REWRITER_typedef_";
1254 ResultStr += ClassDecl->getNameAsString();
1256 ResultStr += "#define _REWRITER_typedef_";
1257 ResultStr += ClassDecl->getNameAsString();
1259 ResultStr += "typedef struct objc_object ";
1260 ResultStr += ClassDecl->getNameAsString();
1261 ResultStr += ";\n#endif\n";
1262 // Mark this typedef as having been generated.
1263 ObjCForwardDecls.insert(ClassDecl);
1265 SynthesizeObjCInternalStruct(ClassDecl, ResultStr);
1267 for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(),
1268 E = ClassDecl->prop_end(); I != E; ++I)
1269 RewriteProperty(*I);
1270 for (ObjCInterfaceDecl::instmeth_iterator
1271 I = ClassDecl->instmeth_begin(), E = ClassDecl->instmeth_end();
1273 RewriteMethodDeclaration(*I);
1274 for (ObjCInterfaceDecl::classmeth_iterator
1275 I = ClassDecl->classmeth_begin(), E = ClassDecl->classmeth_end();
1277 RewriteMethodDeclaration(*I);
1279 // Lastly, comment out the @end.
1280 ReplaceText(ClassDecl->getAtEndRange().getBegin(), strlen("@end"),
1284 Stmt *RewriteObjC::RewritePropertyOrImplicitSetter(BinaryOperator *BinOp, Expr *newStmt,
1285 SourceRange SrcRange) {
1286 ObjCMethodDecl *OMD = 0;
1292 SourceLocation SuperLocation;
1293 SourceLocation SelectorLoc;
1294 // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr or ObjCImplicitSetterGetterRefExpr.
1295 // This allows us to reuse all the fun and games in SynthMessageExpr().
1296 if (ObjCPropertyRefExpr *PropRefExpr =
1297 dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS())) {
1298 SelectorLoc = PropRefExpr->getLocation();
1299 if (PropRefExpr->isExplicitProperty()) {
1300 ObjCPropertyDecl *PDecl = PropRefExpr->getExplicitProperty();
1301 OMD = PDecl->getSetterMethodDecl();
1302 Ty = PDecl->getType();
1303 Sel = PDecl->getSetterName();
1305 OMD = PropRefExpr->getImplicitPropertySetter();
1306 Sel = OMD->getSelector();
1307 Ty = PropRefExpr->getType();
1309 Super = PropRefExpr->isSuperReceiver();
1311 Receiver = PropRefExpr->getBase();
1313 SuperTy = PropRefExpr->getSuperReceiverType();
1314 SuperLocation = PropRefExpr->getReceiverLocation();
1318 assert(OMD && "RewritePropertyOrImplicitSetter - null OMD");
1320 ObjCMessageExpr *MsgExpr;
1322 MsgExpr = ObjCMessageExpr::Create(*Context,
1323 Ty.getNonReferenceType(),
1324 Expr::getValueKindForType(Ty),
1325 /*FIXME?*/SourceLocation(),
1327 /*IsInstanceSuper=*/true,
1329 Sel, SelectorLoc, OMD,
1331 /*FIXME:*/SourceLocation());
1333 // FIXME. Refactor this into common code with that in
1334 // RewritePropertyOrImplicitGetter
1335 assert(Receiver && "RewritePropertyOrImplicitSetter - null Receiver");
1336 if (Expr *Exp = dyn_cast<Expr>(Receiver))
1337 if (PropGetters[Exp])
1338 // This allows us to handle chain/nested property/implicit getters.
1339 Receiver = PropGetters[Exp];
1341 MsgExpr = ObjCMessageExpr::Create(*Context,
1342 Ty.getNonReferenceType(),
1343 Expr::getValueKindForType(Ty),
1344 /*FIXME: */SourceLocation(),
1345 cast<Expr>(Receiver),
1346 Sel, SelectorLoc, OMD,
1348 /*FIXME:*/SourceLocation());
1350 Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr);
1352 // Now do the actual rewrite.
1353 ReplaceStmtWithRange(BinOp, ReplacingStmt, SrcRange);
1355 // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references
1356 // to things that stay around.
1357 Context->Deallocate(MsgExpr);
1358 return ReplacingStmt;
1361 Stmt *RewriteObjC::RewritePropertyOrImplicitGetter(Expr *PropOrGetterRefExpr) {
1362 // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr or ImplicitGetter.
1363 // This allows us to reuse all the fun and games in SynthMessageExpr().
1365 ObjCMethodDecl *OMD = 0;
1370 SourceLocation SuperLocation;
1371 SourceLocation SelectorLoc;
1372 if (ObjCPropertyRefExpr *PropRefExpr =
1373 dyn_cast<ObjCPropertyRefExpr>(PropOrGetterRefExpr)) {
1374 SelectorLoc = PropRefExpr->getLocation();
1375 if (PropRefExpr->isExplicitProperty()) {
1376 ObjCPropertyDecl *PDecl = PropRefExpr->getExplicitProperty();
1377 OMD = PDecl->getGetterMethodDecl();
1378 Ty = PDecl->getType();
1379 Sel = PDecl->getGetterName();
1381 OMD = PropRefExpr->getImplicitPropertyGetter();
1382 Sel = OMD->getSelector();
1383 Ty = PropRefExpr->getType();
1385 Super = PropRefExpr->isSuperReceiver();
1387 Receiver = PropRefExpr->getBase();
1389 SuperTy = PropRefExpr->getSuperReceiverType();
1390 SuperLocation = PropRefExpr->getReceiverLocation();
1394 assert (OMD && "RewritePropertyOrImplicitGetter - OMD is null");
1396 ObjCMessageExpr *MsgExpr;
1398 MsgExpr = ObjCMessageExpr::Create(*Context,
1399 Ty.getNonReferenceType(),
1400 Expr::getValueKindForType(Ty),
1401 PropOrGetterRefExpr->getLocStart(),
1403 /*IsInstanceSuper=*/true,
1405 Sel, SelectorLoc, OMD,
1407 PropOrGetterRefExpr->getLocEnd());
1409 assert (Receiver && "RewritePropertyOrImplicitGetter - Receiver is null");
1410 if (Expr *Exp = dyn_cast<Expr>(Receiver))
1411 if (PropGetters[Exp])
1412 // This allows us to handle chain/nested property/implicit getters.
1413 Receiver = PropGetters[Exp];
1414 MsgExpr = ObjCMessageExpr::Create(*Context,
1415 Ty.getNonReferenceType(),
1416 Expr::getValueKindForType(Ty),
1417 PropOrGetterRefExpr->getLocStart(),
1418 cast<Expr>(Receiver),
1419 Sel, SelectorLoc, OMD,
1421 PropOrGetterRefExpr->getLocEnd());
1424 Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr, MsgExpr->getLocStart(),
1425 MsgExpr->getLocEnd());
1428 PropParentMap = new ParentMap(CurrentBody);
1429 bool NestedPropertyRef = false;
1430 Stmt *Parent = PropParentMap->getParent(PropOrGetterRefExpr);
1431 ImplicitCastExpr*ICE=0;
1433 if ((ICE = dyn_cast<ImplicitCastExpr>(Parent))) {
1434 assert((ICE->getCastKind() == CK_GetObjCProperty)
1435 && "RewritePropertyOrImplicitGetter");
1436 Parent = PropParentMap->getParent(Parent);
1437 NestedPropertyRef = (Parent && isa<ObjCPropertyRefExpr>(Parent));
1439 if (NestedPropertyRef) {
1440 // We stash away the ReplacingStmt since actually doing the
1441 // replacement/rewrite won't work for nested getters (e.g. obj.p.i)
1442 PropGetters[ICE] = ReplacingStmt;
1443 // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references
1444 // to things that stay around.
1445 Context->Deallocate(MsgExpr);
1446 return PropOrGetterRefExpr; // return the original...
1448 ReplaceStmt(PropOrGetterRefExpr, ReplacingStmt);
1449 // delete PropRefExpr; elsewhere...
1450 // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references
1451 // to things that stay around.
1452 Context->Deallocate(MsgExpr);
1453 return ReplacingStmt;
1457 Stmt *RewriteObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV,
1458 SourceLocation OrigStart,
1460 ObjCIvarDecl *D = IV->getDecl();
1461 const Expr *BaseExpr = IV->getBase();
1463 if (BaseExpr->getType()->isObjCObjectPointerType()) {
1464 const ObjCInterfaceType *iFaceDecl =
1465 dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType());
1466 assert(iFaceDecl && "RewriteObjCIvarRefExpr - iFaceDecl is null");
1467 // lookup which class implements the instance variable.
1468 ObjCInterfaceDecl *clsDeclared = 0;
1469 iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(),
1471 assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class");
1473 // Synthesize an explicit cast to gain access to the ivar.
1474 std::string RecName = clsDeclared->getIdentifier()->getName();
1476 IdentifierInfo *II = &Context->Idents.get(RecName);
1477 RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl,
1478 SourceLocation(), SourceLocation(),
1480 assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl");
1481 QualType castT = Context->getPointerType(Context->getTagDeclType(RD));
1482 CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT,
1485 // Don't forget the parens to enforce the proper binding.
1486 ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(),
1487 IV->getBase()->getLocEnd(),
1490 if (IV->isFreeIvar() &&
1491 CurMethodDef->getClassInterface() == iFaceDecl->getDecl()) {
1492 MemberExpr *ME = new (Context) MemberExpr(PE, true, D,
1495 VK_LValue, OK_Ordinary);
1496 // delete IV; leak for now, see RewritePropertyOrImplicitSetter() usage for more info.
1500 // Cannot delete IV->getBase(), since PE points to it.
1501 // Replace the old base with the cast. This is important when doing
1502 // embedded rewrites. For example, [newInv->_container addObject:0].
1506 } else { // we are outside a method.
1507 assert(!IV->isFreeIvar() && "Cannot have a free standing ivar outside a method");
1509 // Explicit ivar refs need to have a cast inserted.
1510 // FIXME: consider sharing some of this code with the code above.
1511 if (BaseExpr->getType()->isObjCObjectPointerType()) {
1512 const ObjCInterfaceType *iFaceDecl =
1513 dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType());
1514 // lookup which class implements the instance variable.
1515 ObjCInterfaceDecl *clsDeclared = 0;
1516 iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(),
1518 assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class");
1520 // Synthesize an explicit cast to gain access to the ivar.
1521 std::string RecName = clsDeclared->getIdentifier()->getName();
1523 IdentifierInfo *II = &Context->Idents.get(RecName);
1524 RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl,
1525 SourceLocation(), SourceLocation(),
1527 assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl");
1528 QualType castT = Context->getPointerType(Context->getTagDeclType(RD));
1529 CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT,
1532 // Don't forget the parens to enforce the proper binding.
1533 ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(),
1534 IV->getBase()->getLocEnd(), castExpr);
1536 // Cannot delete IV->getBase(), since PE points to it.
1537 // Replace the old base with the cast. This is important when doing
1538 // embedded rewrites. For example, [newInv->_container addObject:0].
1546 Stmt *RewriteObjC::RewriteObjCNestedIvarRefExpr(Stmt *S, bool &replaced) {
1547 for (Stmt::child_range CI = S->children(); CI; ++CI) {
1549 Stmt *newStmt = RewriteObjCNestedIvarRefExpr(*CI, replaced);
1554 if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) {
1555 SourceRange OrigStmtRange = S->getSourceRange();
1556 Stmt *newStmt = RewriteObjCIvarRefExpr(IvarRefExpr, OrigStmtRange.getBegin(),
1560 if (ObjCMessageExpr *MsgRefExpr = dyn_cast<ObjCMessageExpr>(S)) {
1561 Stmt *newStmt = SynthMessageExpr(MsgRefExpr);
1567 /// SynthCountByEnumWithState - To print:
1568 /// ((unsigned int (*)
1569 /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int))
1570 /// (void *)objc_msgSend)((id)l_collection,
1571 /// sel_registerName(
1572 /// "countByEnumeratingWithState:objects:count:"),
1574 /// (id *)items, (unsigned int)16)
1576 void RewriteObjC::SynthCountByEnumWithState(std::string &buf) {
1577 buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, "
1578 "id *, unsigned int))(void *)objc_msgSend)";
1580 buf += "((id)l_collection,\n\t\t";
1581 buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),";
1583 buf += "&enumState, "
1584 "(id *)items, (unsigned int)16)";
1587 /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach
1588 /// statement to exit to its outer synthesized loop.
1590 Stmt *RewriteObjC::RewriteBreakStmt(BreakStmt *S) {
1591 if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back()))
1593 // replace break with goto __break_label
1596 SourceLocation startLoc = S->getLocStart();
1597 buf = "goto __break_label_";
1598 buf += utostr(ObjCBcLabelNo.back());
1599 ReplaceText(startLoc, strlen("break"), buf);
1604 /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach
1605 /// statement to continue with its inner synthesized loop.
1607 Stmt *RewriteObjC::RewriteContinueStmt(ContinueStmt *S) {
1608 if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back()))
1610 // replace continue with goto __continue_label
1613 SourceLocation startLoc = S->getLocStart();
1614 buf = "goto __continue_label_";
1615 buf += utostr(ObjCBcLabelNo.back());
1616 ReplaceText(startLoc, strlen("continue"), buf);
1621 /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement.
1623 /// for ( type elem in collection) { stmts; }
1628 /// struct __objcFastEnumerationState enumState = { 0 };
1630 /// id l_collection = (id)collection;
1631 /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState
1632 /// objects:items count:16];
1634 /// unsigned long startMutations = *enumState.mutationsPtr;
1636 /// unsigned long counter = 0;
1638 /// if (startMutations != *enumState.mutationsPtr)
1639 /// objc_enumerationMutation(l_collection);
1640 /// elem = (type)enumState.itemsPtr[counter++];
1642 /// __continue_label: ;
1643 /// } while (counter < limit);
1644 /// } while (limit = [l_collection countByEnumeratingWithState:&enumState
1645 /// objects:items count:16]);
1647 /// __break_label: ;
1653 Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S,
1654 SourceLocation OrigEnd) {
1655 assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty");
1656 assert(isa<ObjCForCollectionStmt>(Stmts.back()) &&
1657 "ObjCForCollectionStmt Statement stack mismatch");
1658 assert(!ObjCBcLabelNo.empty() &&
1659 "ObjCForCollectionStmt - Label No stack empty");
1661 SourceLocation startLoc = S->getLocStart();
1662 const char *startBuf = SM->getCharacterData(startLoc);
1663 StringRef elementName;
1664 std::string elementTypeAsString;
1667 if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) {
1669 NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl());
1670 QualType ElementType = cast<ValueDecl>(D)->getType();
1671 if (ElementType->isObjCQualifiedIdType() ||
1672 ElementType->isObjCQualifiedInterfaceType())
1673 // Simply use 'id' for all qualified types.
1674 elementTypeAsString = "id";
1676 elementTypeAsString = ElementType.getAsString(Context->getPrintingPolicy());
1677 buf += elementTypeAsString;
1679 elementName = D->getName();
1684 DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement());
1685 elementName = DR->getDecl()->getName();
1686 ValueDecl *VD = cast<ValueDecl>(DR->getDecl());
1687 if (VD->getType()->isObjCQualifiedIdType() ||
1688 VD->getType()->isObjCQualifiedInterfaceType())
1689 // Simply use 'id' for all qualified types.
1690 elementTypeAsString = "id";
1692 elementTypeAsString = VD->getType().getAsString(Context->getPrintingPolicy());
1695 // struct __objcFastEnumerationState enumState = { 0 };
1696 buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t";
1698 buf += "id items[16];\n\t";
1699 // id l_collection = (id)
1700 buf += "id l_collection = (id)";
1701 // Find start location of 'collection' the hard way!
1702 const char *startCollectionBuf = startBuf;
1703 startCollectionBuf += 3; // skip 'for'
1704 startCollectionBuf = strchr(startCollectionBuf, '(');
1705 startCollectionBuf++; // skip '('
1706 // find 'in' and skip it.
1707 while (*startCollectionBuf != ' ' ||
1708 *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' ||
1709 (*(startCollectionBuf+3) != ' ' &&
1710 *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '('))
1711 startCollectionBuf++;
1712 startCollectionBuf += 3;
1714 // Replace: "for (type element in" with string constructed thus far.
1715 ReplaceText(startLoc, startCollectionBuf - startBuf, buf);
1716 // Replace ')' in for '(' type elem in collection ')' with ';'
1717 SourceLocation rightParenLoc = S->getRParenLoc();
1718 const char *rparenBuf = SM->getCharacterData(rightParenLoc);
1719 SourceLocation lparenLoc = startLoc.getLocWithOffset(rparenBuf-startBuf);
1722 // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState
1723 // objects:items count:16];
1724 // which is synthesized into:
1725 // unsigned int limit =
1726 // ((unsigned int (*)
1727 // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int))
1728 // (void *)objc_msgSend)((id)l_collection,
1729 // sel_registerName(
1730 // "countByEnumeratingWithState:objects:count:"),
1731 // (struct __objcFastEnumerationState *)&state,
1732 // (id *)items, (unsigned int)16);
1733 buf += "unsigned long limit =\n\t\t";
1734 SynthCountByEnumWithState(buf);
1737 /// unsigned long startMutations = *enumState.mutationsPtr;
1739 /// unsigned long counter = 0;
1741 /// if (startMutations != *enumState.mutationsPtr)
1742 /// objc_enumerationMutation(l_collection);
1743 /// elem = (type)enumState.itemsPtr[counter++];
1744 buf += "if (limit) {\n\t";
1745 buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t";
1746 buf += "do {\n\t\t";
1747 buf += "unsigned long counter = 0;\n\t\t";
1748 buf += "do {\n\t\t\t";
1749 buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t";
1750 buf += "objc_enumerationMutation(l_collection);\n\t\t\t";
1753 buf += elementTypeAsString;
1754 buf += ")enumState.itemsPtr[counter++];";
1755 // Replace ')' in for '(' type elem in collection ')' with all of these.
1756 ReplaceText(lparenLoc, 1, buf);
1758 /// __continue_label: ;
1759 /// } while (counter < limit);
1760 /// } while (limit = [l_collection countByEnumeratingWithState:&enumState
1761 /// objects:items count:16]);
1763 /// __break_label: ;
1770 buf += "__continue_label_";
1771 buf += utostr(ObjCBcLabelNo.back());
1774 buf += "} while (counter < limit);\n\t";
1775 buf += "} while (limit = ";
1776 SynthCountByEnumWithState(buf);
1780 buf += elementTypeAsString;
1782 buf += "__break_label_";
1783 buf += utostr(ObjCBcLabelNo.back());
1786 buf += "else\n\t\t";
1789 buf += elementTypeAsString;
1793 // Insert all these *after* the statement body.
1794 // FIXME: If this should support Obj-C++, support CXXTryStmt
1795 if (isa<CompoundStmt>(S->getBody())) {
1796 SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(1);
1797 InsertText(endBodyLoc, buf);
1799 /* Need to treat single statements specially. For example:
1801 * for (A *a in b) if (stuff()) break;
1802 * for (A *a in b) xxxyy;
1804 * The following code simply scans ahead to the semi to find the actual end.
1806 const char *stmtBuf = SM->getCharacterData(OrigEnd);
1807 const char *semiBuf = strchr(stmtBuf, ';');
1808 assert(semiBuf && "Can't find ';'");
1809 SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(semiBuf-stmtBuf+1);
1810 InsertText(endBodyLoc, buf);
1813 ObjCBcLabelNo.pop_back();
1817 /// RewriteObjCSynchronizedStmt -
1818 /// This routine rewrites @synchronized(expr) stmt;
1820 /// objc_sync_enter(expr);
1821 /// @try stmt @finally { objc_sync_exit(expr); }
1823 Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) {
1824 // Get the start location and compute the semi location.
1825 SourceLocation startLoc = S->getLocStart();
1826 const char *startBuf = SM->getCharacterData(startLoc);
1828 assert((*startBuf == '@') && "bogus @synchronized location");
1831 buf = "objc_sync_enter((id)";
1832 const char *lparenBuf = startBuf;
1833 while (*lparenBuf != '(') lparenBuf++;
1834 ReplaceText(startLoc, lparenBuf-startBuf+1, buf);
1835 // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since
1836 // the sync expression is typically a message expression that's already
1837 // been rewritten! (which implies the SourceLocation's are invalid).
1838 SourceLocation endLoc = S->getSynchBody()->getLocStart();
1839 const char *endBuf = SM->getCharacterData(endLoc);
1840 while (*endBuf != ')') endBuf--;
1841 SourceLocation rparenLoc = startLoc.getLocWithOffset(endBuf-startBuf);
1843 // declare a new scope with two variables, _stack and _rethrow.
1844 buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n";
1845 buf += "int buf[18/*32-bit i386*/];\n";
1846 buf += "char *pointers[4];} _stack;\n";
1847 buf += "id volatile _rethrow = 0;\n";
1848 buf += "objc_exception_try_enter(&_stack);\n";
1849 buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n";
1850 ReplaceText(rparenLoc, 1, buf);
1851 startLoc = S->getSynchBody()->getLocEnd();
1852 startBuf = SM->getCharacterData(startLoc);
1854 assert((*startBuf == '}') && "bogus @synchronized block");
1855 SourceLocation lastCurlyLoc = startLoc;
1856 buf = "}\nelse {\n";
1857 buf += " _rethrow = objc_exception_extract(&_stack);\n";
1859 buf += "{ /* implicit finally clause */\n";
1860 buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n";
1862 std::string syncBuf;
1863 syncBuf += " objc_sync_exit(";
1865 Expr *syncExpr = S->getSynchExpr();
1866 CastKind CK = syncExpr->getType()->isObjCObjectPointerType()
1868 syncExpr->getType()->isBlockPointerType()
1869 ? CK_BlockPointerToObjCPointerCast
1870 : CK_CPointerToObjCPointerCast;
1871 syncExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(),
1873 std::string syncExprBufS;
1874 llvm::raw_string_ostream syncExprBuf(syncExprBufS);
1875 syncExpr->printPretty(syncExprBuf, *Context, 0,
1876 PrintingPolicy(LangOpts));
1877 syncBuf += syncExprBuf.str();
1881 buf += "\n if (_rethrow) objc_exception_throw(_rethrow);\n";
1885 ReplaceText(lastCurlyLoc, 1, buf);
1887 bool hasReturns = false;
1888 HasReturnStmts(S->getSynchBody(), hasReturns);
1890 RewriteSyncReturnStmts(S->getSynchBody(), syncBuf);
1895 void RewriteObjC::WarnAboutReturnGotoStmts(Stmt *S)
1897 // Perform a bottom up traversal of all children.
1898 for (Stmt::child_range CI = S->children(); CI; ++CI)
1900 WarnAboutReturnGotoStmts(*CI);
1902 if (isa<ReturnStmt>(S) || isa<GotoStmt>(S)) {
1903 Diags.Report(Context->getFullLoc(S->getLocStart()),
1904 TryFinallyContainsReturnDiag);
1909 void RewriteObjC::HasReturnStmts(Stmt *S, bool &hasReturns)
1911 // Perform a bottom up traversal of all children.
1912 for (Stmt::child_range CI = S->children(); CI; ++CI)
1914 HasReturnStmts(*CI, hasReturns);
1916 if (isa<ReturnStmt>(S))
1921 void RewriteObjC::RewriteTryReturnStmts(Stmt *S) {
1922 // Perform a bottom up traversal of all children.
1923 for (Stmt::child_range CI = S->children(); CI; ++CI)
1925 RewriteTryReturnStmts(*CI);
1927 if (isa<ReturnStmt>(S)) {
1928 SourceLocation startLoc = S->getLocStart();
1929 const char *startBuf = SM->getCharacterData(startLoc);
1931 const char *semiBuf = strchr(startBuf, ';');
1932 assert((*semiBuf == ';') && "RewriteTryReturnStmts: can't find ';'");
1933 SourceLocation onePastSemiLoc = startLoc.getLocWithOffset(semiBuf-startBuf+1);
1936 buf = "{ objc_exception_try_exit(&_stack); return";
1938 ReplaceText(startLoc, 6, buf);
1939 InsertText(onePastSemiLoc, "}");
1944 void RewriteObjC::RewriteSyncReturnStmts(Stmt *S, std::string syncExitBuf) {
1945 // Perform a bottom up traversal of all children.
1946 for (Stmt::child_range CI = S->children(); CI; ++CI)
1948 RewriteSyncReturnStmts(*CI, syncExitBuf);
1950 if (isa<ReturnStmt>(S)) {
1951 SourceLocation startLoc = S->getLocStart();
1952 const char *startBuf = SM->getCharacterData(startLoc);
1954 const char *semiBuf = strchr(startBuf, ';');
1955 assert((*semiBuf == ';') && "RewriteSyncReturnStmts: can't find ';'");
1956 SourceLocation onePastSemiLoc = startLoc.getLocWithOffset(semiBuf-startBuf+1);
1959 buf = "{ objc_exception_try_exit(&_stack);";
1963 ReplaceText(startLoc, 6, buf);
1964 InsertText(onePastSemiLoc, "}");
1969 Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) {
1970 // Get the start location and compute the semi location.
1971 SourceLocation startLoc = S->getLocStart();
1972 const char *startBuf = SM->getCharacterData(startLoc);
1974 assert((*startBuf == '@') && "bogus @try location");
1977 // declare a new scope with two variables, _stack and _rethrow.
1978 buf = "/* @try scope begin */ { struct _objc_exception_data {\n";
1979 buf += "int buf[18/*32-bit i386*/];\n";
1980 buf += "char *pointers[4];} _stack;\n";
1981 buf += "id volatile _rethrow = 0;\n";
1982 buf += "objc_exception_try_enter(&_stack);\n";
1983 buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n";
1985 ReplaceText(startLoc, 4, buf);
1987 startLoc = S->getTryBody()->getLocEnd();
1988 startBuf = SM->getCharacterData(startLoc);
1990 assert((*startBuf == '}') && "bogus @try block");
1992 SourceLocation lastCurlyLoc = startLoc;
1993 if (S->getNumCatchStmts()) {
1994 startLoc = startLoc.getLocWithOffset(1);
1995 buf = " /* @catch begin */ else {\n";
1996 buf += " id _caught = objc_exception_extract(&_stack);\n";
1997 buf += " objc_exception_try_enter (&_stack);\n";
1998 buf += " if (_setjmp(_stack.buf))\n";
1999 buf += " _rethrow = objc_exception_extract(&_stack);\n";
2000 buf += " else { /* @catch continue */";
2002 InsertText(startLoc, buf);
2003 } else { /* no catch list */
2004 buf = "}\nelse {\n";
2005 buf += " _rethrow = objc_exception_extract(&_stack);\n";
2007 ReplaceText(lastCurlyLoc, 1, buf);
2009 Stmt *lastCatchBody = 0;
2010 for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I) {
2011 ObjCAtCatchStmt *Catch = S->getCatchStmt(I);
2012 VarDecl *catchDecl = Catch->getCatchParamDecl();
2015 buf = "if ("; // we are generating code for the first catch clause
2018 startLoc = Catch->getLocStart();
2019 startBuf = SM->getCharacterData(startLoc);
2021 assert((*startBuf == '@') && "bogus @catch location");
2023 const char *lParenLoc = strchr(startBuf, '(');
2025 if (Catch->hasEllipsis()) {
2026 // Now rewrite the body...
2027 lastCatchBody = Catch->getCatchBody();
2028 SourceLocation bodyLoc = lastCatchBody->getLocStart();
2029 const char *bodyBuf = SM->getCharacterData(bodyLoc);
2030 assert(*SM->getCharacterData(Catch->getRParenLoc()) == ')' &&
2031 "bogus @catch paren location");
2032 assert((*bodyBuf == '{') && "bogus @catch body location");
2034 buf += "1) { id _tmp = _caught;";
2035 Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, buf);
2036 } else if (catchDecl) {
2037 QualType t = catchDecl->getType();
2038 if (t == Context->getObjCIdType()) {
2040 ReplaceText(startLoc, lParenLoc-startBuf+1, buf);
2041 } else if (const ObjCObjectPointerType *Ptr =
2042 t->getAs<ObjCObjectPointerType>()) {
2043 // Should be a pointer to a class.
2044 ObjCInterfaceDecl *IDecl = Ptr->getObjectType()->getInterface();
2046 buf += "objc_exception_match((struct objc_class *)objc_getClass(\"";
2047 buf += IDecl->getNameAsString();
2048 buf += "\"), (struct objc_object *)_caught)) { ";
2049 ReplaceText(startLoc, lParenLoc-startBuf+1, buf);
2052 // Now rewrite the body...
2053 lastCatchBody = Catch->getCatchBody();
2054 SourceLocation rParenLoc = Catch->getRParenLoc();
2055 SourceLocation bodyLoc = lastCatchBody->getLocStart();
2056 const char *bodyBuf = SM->getCharacterData(bodyLoc);
2057 const char *rParenBuf = SM->getCharacterData(rParenLoc);
2058 assert((*rParenBuf == ')') && "bogus @catch paren location");
2059 assert((*bodyBuf == '{') && "bogus @catch body location");
2061 // Here we replace ") {" with "= _caught;" (which initializes and
2062 // declares the @catch parameter).
2063 ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, " = _caught;");
2065 llvm_unreachable("@catch rewrite bug");
2068 // Complete the catch list...
2069 if (lastCatchBody) {
2070 SourceLocation bodyLoc = lastCatchBody->getLocEnd();
2071 assert(*SM->getCharacterData(bodyLoc) == '}' &&
2072 "bogus @catch body location");
2074 // Insert the last (implicit) else clause *before* the right curly brace.
2075 bodyLoc = bodyLoc.getLocWithOffset(-1);
2076 buf = "} /* last catch end */\n";
2078 buf += " _rethrow = _caught;\n";
2079 buf += " objc_exception_try_exit(&_stack);\n";
2080 buf += "} } /* @catch end */\n";
2081 if (!S->getFinallyStmt())
2083 InsertText(bodyLoc, buf);
2086 lastCurlyLoc = lastCatchBody->getLocEnd();
2088 if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) {
2089 startLoc = finalStmt->getLocStart();
2090 startBuf = SM->getCharacterData(startLoc);
2091 assert((*startBuf == '@') && "bogus @finally start");
2093 ReplaceText(startLoc, 8, "/* @finally */");
2095 Stmt *body = finalStmt->getFinallyBody();
2096 SourceLocation startLoc = body->getLocStart();
2097 SourceLocation endLoc = body->getLocEnd();
2098 assert(*SM->getCharacterData(startLoc) == '{' &&
2099 "bogus @finally body location");
2100 assert(*SM->getCharacterData(endLoc) == '}' &&
2101 "bogus @finally body location");
2103 startLoc = startLoc.getLocWithOffset(1);
2104 InsertText(startLoc, " if (!_rethrow) objc_exception_try_exit(&_stack);\n");
2105 endLoc = endLoc.getLocWithOffset(-1);
2106 InsertText(endLoc, " if (_rethrow) objc_exception_throw(_rethrow);\n");
2109 lastCurlyLoc = body->getLocEnd();
2111 // Now check for any return/continue/go statements within the @try.
2112 WarnAboutReturnGotoStmts(S->getTryBody());
2113 } else { /* no finally clause - make sure we synthesize an implicit one */
2114 buf = "{ /* implicit finally clause */\n";
2115 buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n";
2116 buf += " if (_rethrow) objc_exception_throw(_rethrow);\n";
2118 ReplaceText(lastCurlyLoc, 1, buf);
2120 // Now check for any return/continue/go statements within the @try.
2121 // The implicit finally clause won't called if the @try contains any
2123 bool hasReturns = false;
2124 HasReturnStmts(S->getTryBody(), hasReturns);
2126 RewriteTryReturnStmts(S->getTryBody());
2128 // Now emit the final closing curly brace...
2129 lastCurlyLoc = lastCurlyLoc.getLocWithOffset(1);
2130 InsertText(lastCurlyLoc, " } /* @try scope end */\n");
2134 // This can't be done with ReplaceStmt(S, ThrowExpr), since
2135 // the throw expression is typically a message expression that's already
2136 // been rewritten! (which implies the SourceLocation's are invalid).
2137 Stmt *RewriteObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) {
2138 // Get the start location and compute the semi location.
2139 SourceLocation startLoc = S->getLocStart();
2140 const char *startBuf = SM->getCharacterData(startLoc);
2142 assert((*startBuf == '@') && "bogus @throw location");
2145 /* void objc_exception_throw(id) __attribute__((noreturn)); */
2146 if (S->getThrowExpr())
2147 buf = "objc_exception_throw(";
2148 else // add an implicit argument
2149 buf = "objc_exception_throw(_caught";
2151 // handle "@ throw" correctly.
2152 const char *wBuf = strchr(startBuf, 'w');
2153 assert((*wBuf == 'w') && "@throw: can't find 'w'");
2154 ReplaceText(startLoc, wBuf-startBuf+1, buf);
2156 const char *semiBuf = strchr(startBuf, ';');
2157 assert((*semiBuf == ';') && "@throw: can't find ';'");
2158 SourceLocation semiLoc = startLoc.getLocWithOffset(semiBuf-startBuf);
2159 ReplaceText(semiLoc, 1, ");");
2163 Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) {
2164 // Create a new string expression.
2165 QualType StrType = Context->getPointerType(Context->CharTy);
2166 std::string StrEncoding;
2167 Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding);
2168 Expr *Replacement = StringLiteral::Create(*Context, StrEncoding,
2169 StringLiteral::Ascii, false,
2170 StrType, SourceLocation());
2171 ReplaceStmt(Exp, Replacement);
2173 // Replace this subexpr in the parent.
2174 // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info.
2178 Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) {
2179 if (!SelGetUidFunctionDecl)
2180 SynthSelGetUidFunctionDecl();
2181 assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl");
2182 // Create a call to sel_registerName("selName").
2183 SmallVector<Expr*, 8> SelExprs;
2184 QualType argType = Context->getPointerType(Context->CharTy);
2185 SelExprs.push_back(StringLiteral::Create(*Context,
2186 Exp->getSelector().getAsString(),
2187 StringLiteral::Ascii, false,
2188 argType, SourceLocation()));
2189 CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl,
2190 &SelExprs[0], SelExprs.size());
2191 ReplaceStmt(Exp, SelExp);
2192 // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info.
2196 CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl(
2197 FunctionDecl *FD, Expr **args, unsigned nargs, SourceLocation StartLoc,
2198 SourceLocation EndLoc) {
2199 // Get the type, we will need to reference it in a couple spots.
2200 QualType msgSendType = FD->getType();
2202 // Create a reference to the objc_msgSend() declaration.
2204 new (Context) DeclRefExpr(FD, msgSendType, VK_LValue, SourceLocation());
2206 // Now, we cast the reference to a pointer to the objc_msgSend type.
2207 QualType pToFunc = Context->getPointerType(msgSendType);
2208 ImplicitCastExpr *ICE =
2209 ImplicitCastExpr::Create(*Context, pToFunc, CK_FunctionToPointerDecay,
2212 const FunctionType *FT = msgSendType->getAs<FunctionType>();
2215 new (Context) CallExpr(*Context, ICE, args, nargs,
2216 FT->getCallResultType(*Context),
2221 static bool scanForProtocolRefs(const char *startBuf, const char *endBuf,
2222 const char *&startRef, const char *&endRef) {
2223 while (startBuf < endBuf) {
2224 if (*startBuf == '<')
2225 startRef = startBuf; // mark the start.
2226 if (*startBuf == '>') {
2227 if (startRef && *startRef == '<') {
2228 endRef = startBuf; // mark the end.
2238 static void scanToNextArgument(const char *&argRef) {
2240 while (*argRef != ')' && (*argRef != ',' || angle > 0)) {
2243 else if (*argRef == '>')
2247 assert(angle == 0 && "scanToNextArgument - bad protocol type syntax");
2250 bool RewriteObjC::needToScanForQualifiers(QualType T) {
2251 if (T->isObjCQualifiedIdType())
2253 if (const PointerType *PT = T->getAs<PointerType>()) {
2254 if (PT->getPointeeType()->isObjCQualifiedIdType())
2257 if (T->isObjCObjectPointerType()) {
2258 T = T->getPointeeType();
2259 return T->isObjCQualifiedInterfaceType();
2261 if (T->isArrayType()) {
2262 QualType ElemTy = Context->getBaseElementType(T);
2263 return needToScanForQualifiers(ElemTy);
2268 void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) {
2269 QualType Type = E->getType();
2270 if (needToScanForQualifiers(Type)) {
2271 SourceLocation Loc, EndLoc;
2273 if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) {
2274 Loc = ECE->getLParenLoc();
2275 EndLoc = ECE->getRParenLoc();
2277 Loc = E->getLocStart();
2278 EndLoc = E->getLocEnd();
2280 // This will defend against trying to rewrite synthesized expressions.
2281 if (Loc.isInvalid() || EndLoc.isInvalid())
2284 const char *startBuf = SM->getCharacterData(Loc);
2285 const char *endBuf = SM->getCharacterData(EndLoc);
2286 const char *startRef = 0, *endRef = 0;
2287 if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) {
2288 // Get the locations of the startRef, endRef.
2289 SourceLocation LessLoc = Loc.getLocWithOffset(startRef-startBuf);
2290 SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-startBuf+1);
2291 // Comment out the protocol references.
2292 InsertText(LessLoc, "/*");
2293 InsertText(GreaterLoc, "*/");
2298 void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) {
2301 const FunctionProtoType *proto = 0;
2302 if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) {
2303 Loc = VD->getLocation();
2304 Type = VD->getType();
2306 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) {
2307 Loc = FD->getLocation();
2308 // Check for ObjC 'id' and class types that have been adorned with protocol
2309 // information (id<p>, C<p>*). The protocol references need to be rewritten!
2310 const FunctionType *funcType = FD->getType()->getAs<FunctionType>();
2311 assert(funcType && "missing function type");
2312 proto = dyn_cast<FunctionProtoType>(funcType);
2315 Type = proto->getResultType();
2317 else if (FieldDecl *FD = dyn_cast<FieldDecl>(Dcl)) {
2318 Loc = FD->getLocation();
2319 Type = FD->getType();
2324 if (needToScanForQualifiers(Type)) {
2325 // Since types are unique, we need to scan the buffer.
2327 const char *endBuf = SM->getCharacterData(Loc);
2328 const char *startBuf = endBuf;
2329 while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart)
2330 startBuf--; // scan backward (from the decl location) for return type.
2331 const char *startRef = 0, *endRef = 0;
2332 if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) {
2333 // Get the locations of the startRef, endRef.
2334 SourceLocation LessLoc = Loc.getLocWithOffset(startRef-endBuf);
2335 SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-endBuf+1);
2336 // Comment out the protocol references.
2337 InsertText(LessLoc, "/*");
2338 InsertText(GreaterLoc, "*/");
2342 return; // most likely, was a variable
2343 // Now check arguments.
2344 const char *startBuf = SM->getCharacterData(Loc);
2345 const char *startFuncBuf = startBuf;
2346 for (unsigned i = 0; i < proto->getNumArgs(); i++) {
2347 if (needToScanForQualifiers(proto->getArgType(i))) {
2348 // Since types are unique, we need to scan the buffer.
2350 const char *endBuf = startBuf;
2351 // scan forward (from the decl location) for argument types.
2352 scanToNextArgument(endBuf);
2353 const char *startRef = 0, *endRef = 0;
2354 if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) {
2355 // Get the locations of the startRef, endRef.
2356 SourceLocation LessLoc =
2357 Loc.getLocWithOffset(startRef-startFuncBuf);
2358 SourceLocation GreaterLoc =
2359 Loc.getLocWithOffset(endRef-startFuncBuf+1);
2360 // Comment out the protocol references.
2361 InsertText(LessLoc, "/*");
2362 InsertText(GreaterLoc, "*/");
2364 startBuf = ++endBuf;
2367 // If the function name is derived from a macro expansion, then the
2368 // argument buffer will not follow the name. Need to speak with Chris.
2369 while (*startBuf && *startBuf != ')' && *startBuf != ',')
2370 startBuf++; // scan forward (from the decl location) for argument types.
2376 void RewriteObjC::RewriteTypeOfDecl(VarDecl *ND) {
2377 QualType QT = ND->getType();
2378 const Type* TypePtr = QT->getAs<Type>();
2379 if (!isa<TypeOfExprType>(TypePtr))
2381 while (isa<TypeOfExprType>(TypePtr)) {
2382 const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr);
2383 QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType();
2384 TypePtr = QT->getAs<Type>();
2386 // FIXME. This will not work for multiple declarators; as in:
2387 // __typeof__(a) b,c,d;
2388 std::string TypeAsString(QT.getAsString(Context->getPrintingPolicy()));
2389 SourceLocation DeclLoc = ND->getTypeSpecStartLoc();
2390 const char *startBuf = SM->getCharacterData(DeclLoc);
2391 if (ND->getInit()) {
2392 std::string Name(ND->getNameAsString());
2393 TypeAsString += " " + Name + " = ";
2394 Expr *E = ND->getInit();
2395 SourceLocation startLoc;
2396 if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E))
2397 startLoc = ECE->getLParenLoc();
2399 startLoc = E->getLocStart();
2400 startLoc = SM->getExpansionLoc(startLoc);
2401 const char *endBuf = SM->getCharacterData(startLoc);
2402 ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString);
2405 SourceLocation X = ND->getLocEnd();
2406 X = SM->getExpansionLoc(X);
2407 const char *endBuf = SM->getCharacterData(X);
2408 ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString);
2412 // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str);
2413 void RewriteObjC::SynthSelGetUidFunctionDecl() {
2414 IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName");
2415 SmallVector<QualType, 16> ArgTys;
2416 ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst()));
2417 QualType getFuncType =
2418 getSimpleFunctionType(Context->getObjCSelType(), &ArgTys[0], ArgTys.size());
2419 SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
2422 SelGetUidIdent, getFuncType, 0,
2427 void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) {
2428 // declared in <objc/objc.h>
2429 if (FD->getIdentifier() &&
2430 FD->getName() == "sel_registerName") {
2431 SelGetUidFunctionDecl = FD;
2434 RewriteObjCQualifiedInterfaceTypes(FD);
2437 void RewriteObjC::RewriteBlockPointerType(std::string& Str, QualType Type) {
2438 std::string TypeString(Type.getAsString(Context->getPrintingPolicy()));
2439 const char *argPtr = TypeString.c_str();
2440 if (!strchr(argPtr, '^')) {
2445 Str += (*argPtr == '^' ? '*' : *argPtr);
2450 // FIXME. Consolidate this routine with RewriteBlockPointerType.
2451 void RewriteObjC::RewriteBlockPointerTypeVariable(std::string& Str,
2453 QualType Type = VD->getType();
2454 std::string TypeString(Type.getAsString(Context->getPrintingPolicy()));
2455 const char *argPtr = TypeString.c_str();
2470 Str += VD->getNameAsString();
2481 void RewriteObjC::RewriteBlockLiteralFunctionDecl(FunctionDecl *FD) {
2482 SourceLocation FunLocStart = FD->getTypeSpecStartLoc();
2483 const FunctionType *funcType = FD->getType()->getAs<FunctionType>();
2484 const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(funcType);
2487 QualType Type = proto->getResultType();
2488 std::string FdStr = Type.getAsString(Context->getPrintingPolicy());
2490 FdStr += FD->getName();
2492 unsigned numArgs = proto->getNumArgs();
2493 for (unsigned i = 0; i < numArgs; i++) {
2494 QualType ArgType = proto->getArgType(i);
2495 RewriteBlockPointerType(FdStr, ArgType);
2500 InsertText(FunLocStart, FdStr);
2501 CurFunctionDeclToDeclareForBlock = 0;
2504 // SynthSuperContructorFunctionDecl - id objc_super(id obj, id super);
2505 void RewriteObjC::SynthSuperContructorFunctionDecl() {
2506 if (SuperContructorFunctionDecl)
2508 IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super");
2509 SmallVector<QualType, 16> ArgTys;
2510 QualType argT = Context->getObjCIdType();
2511 assert(!argT.isNull() && "Can't find 'id' type");
2512 ArgTys.push_back(argT);
2513 ArgTys.push_back(argT);
2514 QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(),
2515 &ArgTys[0], ArgTys.size());
2516 SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
2519 msgSendIdent, msgSendType, 0,
2524 // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...);
2525 void RewriteObjC::SynthMsgSendFunctionDecl() {
2526 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend");
2527 SmallVector<QualType, 16> ArgTys;
2528 QualType argT = Context->getObjCIdType();
2529 assert(!argT.isNull() && "Can't find 'id' type");
2530 ArgTys.push_back(argT);
2531 argT = Context->getObjCSelType();
2532 assert(!argT.isNull() && "Can't find 'SEL' type");
2533 ArgTys.push_back(argT);
2534 QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(),
2535 &ArgTys[0], ArgTys.size(),
2536 true /*isVariadic*/);
2537 MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
2540 msgSendIdent, msgSendType, 0,
2545 // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...);
2546 void RewriteObjC::SynthMsgSendSuperFunctionDecl() {
2547 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper");
2548 SmallVector<QualType, 16> ArgTys;
2549 RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl,
2550 SourceLocation(), SourceLocation(),
2551 &Context->Idents.get("objc_super"));
2552 QualType argT = Context->getPointerType(Context->getTagDeclType(RD));
2553 assert(!argT.isNull() && "Can't build 'struct objc_super *' type");
2554 ArgTys.push_back(argT);
2555 argT = Context->getObjCSelType();
2556 assert(!argT.isNull() && "Can't find 'SEL' type");
2557 ArgTys.push_back(argT);
2558 QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(),
2559 &ArgTys[0], ArgTys.size(),
2560 true /*isVariadic*/);
2561 MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
2564 msgSendIdent, msgSendType, 0,
2569 // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...);
2570 void RewriteObjC::SynthMsgSendStretFunctionDecl() {
2571 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret");
2572 SmallVector<QualType, 16> ArgTys;
2573 QualType argT = Context->getObjCIdType();
2574 assert(!argT.isNull() && "Can't find 'id' type");
2575 ArgTys.push_back(argT);
2576 argT = Context->getObjCSelType();
2577 assert(!argT.isNull() && "Can't find 'SEL' type");
2578 ArgTys.push_back(argT);
2579 QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(),
2580 &ArgTys[0], ArgTys.size(),
2581 true /*isVariadic*/);
2582 MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
2585 msgSendIdent, msgSendType, 0,
2590 // SynthMsgSendSuperStretFunctionDecl -
2591 // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...);
2592 void RewriteObjC::SynthMsgSendSuperStretFunctionDecl() {
2593 IdentifierInfo *msgSendIdent =
2594 &Context->Idents.get("objc_msgSendSuper_stret");
2595 SmallVector<QualType, 16> ArgTys;
2596 RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl,
2597 SourceLocation(), SourceLocation(),
2598 &Context->Idents.get("objc_super"));
2599 QualType argT = Context->getPointerType(Context->getTagDeclType(RD));
2600 assert(!argT.isNull() && "Can't build 'struct objc_super *' type");
2601 ArgTys.push_back(argT);
2602 argT = Context->getObjCSelType();
2603 assert(!argT.isNull() && "Can't find 'SEL' type");
2604 ArgTys.push_back(argT);
2605 QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(),
2606 &ArgTys[0], ArgTys.size(),
2607 true /*isVariadic*/);
2608 MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
2611 msgSendIdent, msgSendType, 0,
2616 // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...);
2617 void RewriteObjC::SynthMsgSendFpretFunctionDecl() {
2618 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret");
2619 SmallVector<QualType, 16> ArgTys;
2620 QualType argT = Context->getObjCIdType();
2621 assert(!argT.isNull() && "Can't find 'id' type");
2622 ArgTys.push_back(argT);
2623 argT = Context->getObjCSelType();
2624 assert(!argT.isNull() && "Can't find 'SEL' type");
2625 ArgTys.push_back(argT);
2626 QualType msgSendType = getSimpleFunctionType(Context->DoubleTy,
2627 &ArgTys[0], ArgTys.size(),
2628 true /*isVariadic*/);
2629 MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
2632 msgSendIdent, msgSendType, 0,
2637 // SynthGetClassFunctionDecl - id objc_getClass(const char *name);
2638 void RewriteObjC::SynthGetClassFunctionDecl() {
2639 IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass");
2640 SmallVector<QualType, 16> ArgTys;
2641 ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst()));
2642 QualType getClassType = getSimpleFunctionType(Context->getObjCIdType(),
2643 &ArgTys[0], ArgTys.size());
2644 GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
2647 getClassIdent, getClassType, 0,
2652 // SynthGetSuperClassFunctionDecl - Class class_getSuperclass(Class cls);
2653 void RewriteObjC::SynthGetSuperClassFunctionDecl() {
2654 IdentifierInfo *getSuperClassIdent =
2655 &Context->Idents.get("class_getSuperclass");
2656 SmallVector<QualType, 16> ArgTys;
2657 ArgTys.push_back(Context->getObjCClassType());
2658 QualType getClassType = getSimpleFunctionType(Context->getObjCClassType(),
2659 &ArgTys[0], ArgTys.size());
2660 GetSuperClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
2670 // SynthGetMetaClassFunctionDecl - id objc_getClass(const char *name);
2671 void RewriteObjC::SynthGetMetaClassFunctionDecl() {
2672 IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass");
2673 SmallVector<QualType, 16> ArgTys;
2674 ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst()));
2675 QualType getClassType = getSimpleFunctionType(Context->getObjCIdType(),
2676 &ArgTys[0], ArgTys.size());
2677 GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
2680 getClassIdent, getClassType, 0,
2685 Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) {
2686 QualType strType = getConstantStringStructType();
2688 std::string S = "__NSConstantStringImpl_";
2690 std::string tmpName = InFileName;
2692 for (i=0; i < tmpName.length(); i++) {
2693 char c = tmpName.at(i);
2694 // replace any non alphanumeric characters with '_'.
2695 if (!isalpha(c) && (c < '0' || c > '9'))
2700 S += utostr(NumObjCStringLiterals++);
2702 Preamble += "static __NSConstantStringImpl " + S;
2703 Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,";
2704 Preamble += "0x000007c8,"; // utf8_str
2705 // The pretty printer for StringLiteral handles escape characters properly.
2706 std::string prettyBufS;
2707 llvm::raw_string_ostream prettyBuf(prettyBufS);
2708 Exp->getString()->printPretty(prettyBuf, *Context, 0,
2709 PrintingPolicy(LangOpts));
2710 Preamble += prettyBuf.str();
2712 Preamble += utostr(Exp->getString()->getByteLength()) + "};\n";
2714 VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(),
2715 SourceLocation(), &Context->Idents.get(S),
2716 strType, 0, SC_Static, SC_None);
2717 DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, strType, VK_LValue,
2719 Expr *Unop = new (Context) UnaryOperator(DRE, UO_AddrOf,
2720 Context->getPointerType(DRE->getType()),
2721 VK_RValue, OK_Ordinary,
2723 // cast to NSConstantString *
2724 CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Exp->getType(),
2725 CK_CPointerToObjCPointerCast, Unop);
2726 ReplaceStmt(Exp, cast);
2727 // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info.
2731 // struct objc_super { struct objc_object *receiver; struct objc_class *super; };
2732 QualType RewriteObjC::getSuperStructType() {
2733 if (!SuperStructDecl) {
2734 SuperStructDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl,
2735 SourceLocation(), SourceLocation(),
2736 &Context->Idents.get("objc_super"));
2737 QualType FieldTypes[2];
2739 // struct objc_object *receiver;
2740 FieldTypes[0] = Context->getObjCIdType();
2741 // struct objc_class *super;
2742 FieldTypes[1] = Context->getObjCClassType();
2745 for (unsigned i = 0; i < 2; ++i) {
2746 SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl,
2748 SourceLocation(), 0,
2752 /*HasInit=*/false));
2755 SuperStructDecl->completeDefinition();
2757 return Context->getTagDeclType(SuperStructDecl);
2760 QualType RewriteObjC::getConstantStringStructType() {
2761 if (!ConstantStringDecl) {
2762 ConstantStringDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl,
2763 SourceLocation(), SourceLocation(),
2764 &Context->Idents.get("__NSConstantStringImpl"));
2765 QualType FieldTypes[4];
2767 // struct objc_object *receiver;
2768 FieldTypes[0] = Context->getObjCIdType();
2770 FieldTypes[1] = Context->IntTy;
2772 FieldTypes[2] = Context->getPointerType(Context->CharTy);
2774 FieldTypes[3] = Context->LongTy;
2777 for (unsigned i = 0; i < 4; ++i) {
2778 ConstantStringDecl->addDecl(FieldDecl::Create(*Context,
2781 SourceLocation(), 0,
2785 /*HasInit=*/false));
2788 ConstantStringDecl->completeDefinition();
2790 return Context->getTagDeclType(ConstantStringDecl);
2793 Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp,
2794 SourceLocation StartLoc,
2795 SourceLocation EndLoc) {
2796 if (!SelGetUidFunctionDecl)
2797 SynthSelGetUidFunctionDecl();
2798 if (!MsgSendFunctionDecl)
2799 SynthMsgSendFunctionDecl();
2800 if (!MsgSendSuperFunctionDecl)
2801 SynthMsgSendSuperFunctionDecl();
2802 if (!MsgSendStretFunctionDecl)
2803 SynthMsgSendStretFunctionDecl();
2804 if (!MsgSendSuperStretFunctionDecl)
2805 SynthMsgSendSuperStretFunctionDecl();
2806 if (!MsgSendFpretFunctionDecl)
2807 SynthMsgSendFpretFunctionDecl();
2808 if (!GetClassFunctionDecl)
2809 SynthGetClassFunctionDecl();
2810 if (!GetSuperClassFunctionDecl)
2811 SynthGetSuperClassFunctionDecl();
2812 if (!GetMetaClassFunctionDecl)
2813 SynthGetMetaClassFunctionDecl();
2815 // default to objc_msgSend().
2816 FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl;
2817 // May need to use objc_msgSend_stret() as well.
2818 FunctionDecl *MsgSendStretFlavor = 0;
2819 if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) {
2820 QualType resultType = mDecl->getResultType();
2821 if (resultType->isRecordType())
2822 MsgSendStretFlavor = MsgSendStretFunctionDecl;
2823 else if (resultType->isRealFloatingType())
2824 MsgSendFlavor = MsgSendFpretFunctionDecl;
2827 // Synthesize a call to objc_msgSend().
2828 SmallVector<Expr*, 8> MsgExprs;
2829 switch (Exp->getReceiverKind()) {
2830 case ObjCMessageExpr::SuperClass: {
2831 MsgSendFlavor = MsgSendSuperFunctionDecl;
2832 if (MsgSendStretFlavor)
2833 MsgSendStretFlavor = MsgSendSuperStretFunctionDecl;
2834 assert(MsgSendFlavor && "MsgSendFlavor is NULL!");
2836 ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface();
2838 SmallVector<Expr*, 4> InitExprs;
2840 // set the receiver to self, the first argument to all methods.
2841 InitExprs.push_back(
2842 NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(),
2844 new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(),
2845 Context->getObjCIdType(),
2848 ); // set the 'receiver'.
2850 // (id)class_getSuperclass((Class)objc_getClass("CurrentClass"))
2851 SmallVector<Expr*, 8> ClsExprs;
2852 QualType argType = Context->getPointerType(Context->CharTy);
2853 ClsExprs.push_back(StringLiteral::Create(*Context,
2854 ClassDecl->getIdentifier()->getName(),
2855 StringLiteral::Ascii, false,
2856 argType, SourceLocation()));
2857 CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl,
2862 // (Class)objc_getClass("CurrentClass")
2863 CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context,
2864 Context->getObjCClassType(),
2865 CK_CPointerToObjCPointerCast, Cls);
2867 ClsExprs.push_back(ArgExpr);
2868 Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl,
2869 &ClsExprs[0], ClsExprs.size(),
2872 // (id)class_getSuperclass((Class)objc_getClass("CurrentClass"))
2873 // To turn off a warning, type-cast to 'id'
2874 InitExprs.push_back( // set 'super class', using class_getSuperclass().
2875 NoTypeInfoCStyleCastExpr(Context,
2876 Context->getObjCIdType(),
2878 // struct objc_super
2879 QualType superType = getSuperStructType();
2882 if (LangOpts.MicrosoftExt) {
2883 SynthSuperContructorFunctionDecl();
2884 // Simulate a contructor call...
2885 DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl,
2886 superType, VK_LValue,
2888 SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0],
2890 superType, VK_LValue,
2892 // The code for super is a little tricky to prevent collision with
2893 // the structure definition in the header. The rewriter has it's own
2894 // internal definition (__rw_objc_super) that is uses. This is why
2895 // we need the cast below. For example:
2896 // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER"))
2898 SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf,
2899 Context->getPointerType(SuperRep->getType()),
2900 VK_RValue, OK_Ordinary,
2902 SuperRep = NoTypeInfoCStyleCastExpr(Context,
2903 Context->getPointerType(superType),
2904 CK_BitCast, SuperRep);
2906 // (struct objc_super) { <exprs from above> }
2908 new (Context) InitListExpr(*Context, SourceLocation(),
2909 &InitExprs[0], InitExprs.size(),
2911 TypeSourceInfo *superTInfo
2912 = Context->getTrivialTypeSourceInfo(superType);
2913 SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo,
2914 superType, VK_LValue,
2916 // struct objc_super *
2917 SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf,
2918 Context->getPointerType(SuperRep->getType()),
2919 VK_RValue, OK_Ordinary,
2922 MsgExprs.push_back(SuperRep);
2926 case ObjCMessageExpr::Class: {
2927 SmallVector<Expr*, 8> ClsExprs;
2928 QualType argType = Context->getPointerType(Context->CharTy);
2929 ObjCInterfaceDecl *Class
2930 = Exp->getClassReceiver()->getAs<ObjCObjectType>()->getInterface();
2931 IdentifierInfo *clsName = Class->getIdentifier();
2932 ClsExprs.push_back(StringLiteral::Create(*Context,
2934 StringLiteral::Ascii, false,
2935 argType, SourceLocation()));
2936 CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl,
2940 MsgExprs.push_back(Cls);
2944 case ObjCMessageExpr::SuperInstance:{
2945 MsgSendFlavor = MsgSendSuperFunctionDecl;
2946 if (MsgSendStretFlavor)
2947 MsgSendStretFlavor = MsgSendSuperStretFunctionDecl;
2948 assert(MsgSendFlavor && "MsgSendFlavor is NULL!");
2949 ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface();
2950 SmallVector<Expr*, 4> InitExprs;
2952 InitExprs.push_back(
2953 NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(),
2955 new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(),
2956 Context->getObjCIdType(),
2957 VK_RValue, SourceLocation()))
2958 ); // set the 'receiver'.
2960 // (id)class_getSuperclass((Class)objc_getClass("CurrentClass"))
2961 SmallVector<Expr*, 8> ClsExprs;
2962 QualType argType = Context->getPointerType(Context->CharTy);
2963 ClsExprs.push_back(StringLiteral::Create(*Context,
2964 ClassDecl->getIdentifier()->getName(),
2965 StringLiteral::Ascii, false, argType,
2967 CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl,
2971 // (Class)objc_getClass("CurrentClass")
2972 CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context,
2973 Context->getObjCClassType(),
2976 ClsExprs.push_back(ArgExpr);
2977 Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl,
2978 &ClsExprs[0], ClsExprs.size(),
2981 // (id)class_getSuperclass((Class)objc_getClass("CurrentClass"))
2982 // To turn off a warning, type-cast to 'id'
2983 InitExprs.push_back(
2984 // set 'super class', using class_getSuperclass().
2985 NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(),
2987 // struct objc_super
2988 QualType superType = getSuperStructType();
2991 if (LangOpts.MicrosoftExt) {
2992 SynthSuperContructorFunctionDecl();
2993 // Simulate a contructor call...
2994 DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl,
2995 superType, VK_LValue,
2997 SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0],
2999 superType, VK_LValue, SourceLocation());
3000 // The code for super is a little tricky to prevent collision with
3001 // the structure definition in the header. The rewriter has it's own
3002 // internal definition (__rw_objc_super) that is uses. This is why
3003 // we need the cast below. For example:
3004 // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER"))
3006 SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf,
3007 Context->getPointerType(SuperRep->getType()),
3008 VK_RValue, OK_Ordinary,
3010 SuperRep = NoTypeInfoCStyleCastExpr(Context,
3011 Context->getPointerType(superType),
3012 CK_BitCast, SuperRep);
3014 // (struct objc_super) { <exprs from above> }
3016 new (Context) InitListExpr(*Context, SourceLocation(),
3017 &InitExprs[0], InitExprs.size(),
3019 TypeSourceInfo *superTInfo
3020 = Context->getTrivialTypeSourceInfo(superType);
3021 SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo,
3022 superType, VK_RValue, ILE,
3025 MsgExprs.push_back(SuperRep);
3029 case ObjCMessageExpr::Instance: {
3030 // Remove all type-casts because it may contain objc-style types; e.g.
3032 Expr *recExpr = Exp->getInstanceReceiver();
3033 while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr))
3034 recExpr = CE->getSubExpr();
3035 CastKind CK = recExpr->getType()->isObjCObjectPointerType()
3036 ? CK_BitCast : recExpr->getType()->isBlockPointerType()
3037 ? CK_BlockPointerToObjCPointerCast
3038 : CK_CPointerToObjCPointerCast;
3040 recExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(),
3042 MsgExprs.push_back(recExpr);
3047 // Create a call to sel_registerName("selName"), it will be the 2nd argument.
3048 SmallVector<Expr*, 8> SelExprs;
3049 QualType argType = Context->getPointerType(Context->CharTy);
3050 SelExprs.push_back(StringLiteral::Create(*Context,
3051 Exp->getSelector().getAsString(),
3052 StringLiteral::Ascii, false,
3053 argType, SourceLocation()));
3054 CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl,
3055 &SelExprs[0], SelExprs.size(),
3058 MsgExprs.push_back(SelExp);
3060 // Now push any user supplied arguments.
3061 for (unsigned i = 0; i < Exp->getNumArgs(); i++) {
3062 Expr *userExpr = Exp->getArg(i);
3063 // Make all implicit casts explicit...ICE comes in handy:-)
3064 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) {
3065 // Reuse the ICE type, it is exactly what the doctor ordered.
3066 QualType type = ICE->getType();
3067 if (needToScanForQualifiers(type))
3068 type = Context->getObjCIdType();
3069 // Make sure we convert "type (^)(...)" to "type (*)(...)".
3070 (void)convertBlockPointerToFunctionPointer(type);
3071 const Expr *SubExpr = ICE->IgnoreParenImpCasts();
3073 if (SubExpr->getType()->isIntegralType(*Context) &&
3074 type->isBooleanType()) {
3075 CK = CK_IntegralToBoolean;
3076 } else if (type->isObjCObjectPointerType()) {
3077 if (SubExpr->getType()->isBlockPointerType()) {
3078 CK = CK_BlockPointerToObjCPointerCast;
3079 } else if (SubExpr->getType()->isPointerType()) {
3080 CK = CK_CPointerToObjCPointerCast;
3088 userExpr = NoTypeInfoCStyleCastExpr(Context, type, CK, userExpr);
3090 // Make id<P...> cast into an 'id' cast.
3091 else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) {
3092 if (CE->getType()->isObjCQualifiedIdType()) {
3093 while ((CE = dyn_cast<CStyleCastExpr>(userExpr)))
3094 userExpr = CE->getSubExpr();
3096 if (userExpr->getType()->isIntegralType(*Context)) {
3097 CK = CK_IntegralToPointer;
3098 } else if (userExpr->getType()->isBlockPointerType()) {
3099 CK = CK_BlockPointerToObjCPointerCast;
3100 } else if (userExpr->getType()->isPointerType()) {
3101 CK = CK_CPointerToObjCPointerCast;
3105 userExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(),
3109 MsgExprs.push_back(userExpr);
3110 // We've transferred the ownership to MsgExprs. For now, we *don't* null
3111 // out the argument in the original expression (since we aren't deleting
3112 // the ObjCMessageExpr). See RewritePropertyOrImplicitSetter() usage for more info.
3113 //Exp->setArg(i, 0);
3115 // Generate the funky cast.
3117 SmallVector<QualType, 8> ArgTypes;
3118 QualType returnType;
3120 // Push 'id' and 'SEL', the 2 implicit arguments.
3121 if (MsgSendFlavor == MsgSendSuperFunctionDecl)
3122 ArgTypes.push_back(Context->getPointerType(getSuperStructType()));
3124 ArgTypes.push_back(Context->getObjCIdType());
3125 ArgTypes.push_back(Context->getObjCSelType());
3126 if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) {
3127 // Push any user argument types.
3128 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
3129 E = OMD->param_end(); PI != E; ++PI) {
3130 QualType t = (*PI)->getType()->isObjCQualifiedIdType()
3131 ? Context->getObjCIdType()
3133 // Make sure we convert "t (^)(...)" to "t (*)(...)".
3134 (void)convertBlockPointerToFunctionPointer(t);
3135 ArgTypes.push_back(t);
3137 returnType = Exp->getType();
3138 convertToUnqualifiedObjCType(returnType);
3139 (void)convertBlockPointerToFunctionPointer(returnType);
3141 returnType = Context->getObjCIdType();
3143 // Get the type, we will need to reference it in a couple spots.
3144 QualType msgSendType = MsgSendFlavor->getType();
3146 // Create a reference to the objc_msgSend() declaration.
3147 DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, msgSendType,
3148 VK_LValue, SourceLocation());
3150 // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid).
3151 // If we don't do this cast, we get the following bizarre warning/note:
3152 // xx.m:13: warning: function called through a non-compatible type
3153 // xx.m:13: note: if this code is reached, the program will abort
3154 cast = NoTypeInfoCStyleCastExpr(Context,
3155 Context->getPointerType(Context->VoidTy),
3158 // Now do the "normal" pointer to function cast.
3160 getSimpleFunctionType(returnType, &ArgTypes[0], ArgTypes.size(),
3161 // If we don't have a method decl, force a variadic cast.
3162 Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true);
3163 castType = Context->getPointerType(castType);
3164 cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast,
3167 // Don't forget the parens to enforce the proper binding.
3168 ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast);
3170 const FunctionType *FT = msgSendType->getAs<FunctionType>();
3171 CallExpr *CE = new (Context) CallExpr(*Context, PE, &MsgExprs[0],
3173 FT->getResultType(), VK_RValue,
3175 Stmt *ReplacingStmt = CE;
3176 if (MsgSendStretFlavor) {
3177 // We have the method which returns a struct/union. Must also generate
3178 // call to objc_msgSend_stret and hang both varieties on a conditional
3179 // expression which dictate which one to envoke depending on size of
3180 // method's return type.
3182 // Create a reference to the objc_msgSend_stret() declaration.
3183 DeclRefExpr *STDRE = new (Context) DeclRefExpr(MsgSendStretFlavor, msgSendType,
3184 VK_LValue, SourceLocation());
3185 // Need to cast objc_msgSend_stret to "void *" (see above comment).
3186 cast = NoTypeInfoCStyleCastExpr(Context,
3187 Context->getPointerType(Context->VoidTy),
3189 // Now do the "normal" pointer to function cast.
3190 castType = getSimpleFunctionType(returnType, &ArgTypes[0], ArgTypes.size(),
3191 Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false);
3192 castType = Context->getPointerType(castType);
3193 cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast,
3196 // Don't forget the parens to enforce the proper binding.
3197 PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast);
3199 FT = msgSendType->getAs<FunctionType>();
3200 CallExpr *STCE = new (Context) CallExpr(*Context, PE, &MsgExprs[0],
3202 FT->getResultType(), VK_RValue,
3205 // Build sizeof(returnType)
3206 UnaryExprOrTypeTraitExpr *sizeofExpr =
3207 new (Context) UnaryExprOrTypeTraitExpr(UETT_SizeOf,
3208 Context->getTrivialTypeSourceInfo(returnType),
3209 Context->getSizeType(), SourceLocation(),
3211 // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...))
3212 // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases.
3213 // For X86 it is more complicated and some kind of target specific routine
3214 // is needed to decide what to do.
3216 static_cast<unsigned>(Context->getTypeSize(Context->IntTy));
3217 IntegerLiteral *limit = IntegerLiteral::Create(*Context,
3218 llvm::APInt(IntSize, 8),
3221 BinaryOperator *lessThanExpr =
3222 new (Context) BinaryOperator(sizeofExpr, limit, BO_LE, Context->IntTy,
3223 VK_RValue, OK_Ordinary, SourceLocation());
3224 // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...))
3225 ConditionalOperator *CondExpr =
3226 new (Context) ConditionalOperator(lessThanExpr,
3227 SourceLocation(), CE,
3228 SourceLocation(), STCE,
3229 returnType, VK_RValue, OK_Ordinary);
3230 ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(),
3233 // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info.
3234 return ReplacingStmt;
3237 Stmt *RewriteObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) {
3238 Stmt *ReplacingStmt = SynthMessageExpr(Exp, Exp->getLocStart(),
3241 // Now do the actual rewrite.
3242 ReplaceStmt(Exp, ReplacingStmt);
3244 // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info.
3245 return ReplacingStmt;
3248 // typedef struct objc_object Protocol;
3249 QualType RewriteObjC::getProtocolType() {
3250 if (!ProtocolTypeDecl) {
3251 TypeSourceInfo *TInfo
3252 = Context->getTrivialTypeSourceInfo(Context->getObjCIdType());
3253 ProtocolTypeDecl = TypedefDecl::Create(*Context, TUDecl,
3254 SourceLocation(), SourceLocation(),
3255 &Context->Idents.get("Protocol"),
3258 return Context->getTypeDeclType(ProtocolTypeDecl);
3261 /// RewriteObjCProtocolExpr - Rewrite a protocol expression into
3262 /// a synthesized/forward data reference (to the protocol's metadata).
3263 /// The forward references (and metadata) are generated in
3264 /// RewriteObjC::HandleTranslationUnit().
3265 Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) {
3266 std::string Name = "_OBJC_PROTOCOL_" + Exp->getProtocol()->getNameAsString();
3267 IdentifierInfo *ID = &Context->Idents.get(Name);
3268 VarDecl *VD = VarDecl::Create(*Context, TUDecl, SourceLocation(),
3269 SourceLocation(), ID, getProtocolType(), 0,
3270 SC_Extern, SC_None);
3271 DeclRefExpr *DRE = new (Context) DeclRefExpr(VD, getProtocolType(), VK_LValue,
3273 Expr *DerefExpr = new (Context) UnaryOperator(DRE, UO_AddrOf,
3274 Context->getPointerType(DRE->getType()),
3275 VK_RValue, OK_Ordinary, SourceLocation());
3276 CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, DerefExpr->getType(),
3279 ReplaceStmt(Exp, castExpr);
3280 ProtocolExprDecls.insert(Exp->getProtocol());
3281 // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info.
3286 bool RewriteObjC::BufferContainsPPDirectives(const char *startBuf,
3287 const char *endBuf) {
3288 while (startBuf < endBuf) {
3289 if (*startBuf == '#') {
3291 for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf)
3293 if (!strncmp(startBuf, "if", strlen("if")) ||
3294 !strncmp(startBuf, "ifdef", strlen("ifdef")) ||
3295 !strncmp(startBuf, "ifndef", strlen("ifndef")) ||
3296 !strncmp(startBuf, "define", strlen("define")) ||
3297 !strncmp(startBuf, "undef", strlen("undef")) ||
3298 !strncmp(startBuf, "else", strlen("else")) ||
3299 !strncmp(startBuf, "elif", strlen("elif")) ||
3300 !strncmp(startBuf, "endif", strlen("endif")) ||
3301 !strncmp(startBuf, "pragma", strlen("pragma")) ||
3302 !strncmp(startBuf, "include", strlen("include")) ||
3303 !strncmp(startBuf, "import", strlen("import")) ||
3304 !strncmp(startBuf, "include_next", strlen("include_next")))
3312 /// SynthesizeObjCInternalStruct - Rewrite one internal struct corresponding to
3313 /// an objective-c class with ivars.
3314 void RewriteObjC::SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl,
3315 std::string &Result) {
3316 assert(CDecl && "Class missing in SynthesizeObjCInternalStruct");
3317 assert(CDecl->getName() != "" &&
3318 "Name missing in SynthesizeObjCInternalStruct");
3319 // Do not synthesize more than once.
3320 if (ObjCSynthesizedStructs.count(CDecl))
3322 ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass();
3323 int NumIvars = CDecl->ivar_size();
3324 SourceLocation LocStart = CDecl->getLocStart();
3325 SourceLocation LocEnd = CDecl->getLocEnd();
3327 const char *startBuf = SM->getCharacterData(LocStart);
3328 const char *endBuf = SM->getCharacterData(LocEnd);
3330 // If no ivars and no root or if its root, directly or indirectly,
3331 // have no ivars (thus not synthesized) then no need to synthesize this class.
3332 if ((CDecl->isForwardDecl() || NumIvars == 0) &&
3333 (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) {
3334 endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts);
3335 ReplaceText(LocStart, endBuf-startBuf, Result);
3339 // FIXME: This has potential of causing problem. If
3340 // SynthesizeObjCInternalStruct is ever called recursively.
3341 Result += "\nstruct ";
3342 Result += CDecl->getNameAsString();
3343 if (LangOpts.MicrosoftExt)
3347 const char *cursor = strchr(startBuf, '{');
3348 assert((cursor && endBuf)
3349 && "SynthesizeObjCInternalStruct - malformed @interface");
3350 // If the buffer contains preprocessor directives, we do more fine-grained
3351 // rewrites. This is intended to fix code that looks like (which occurs in
3352 // NSURL.h, for example):
3355 // @interface Foo : NSObject
3357 // @interface FooBar : NSObject
3364 // This clause is segregated to avoid breaking the common case.
3365 if (BufferContainsPPDirectives(startBuf, cursor)) {
3366 SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() :
3367 CDecl->getAtStartLoc();
3368 const char *endHeader = SM->getCharacterData(L);
3369 endHeader += Lexer::MeasureTokenLength(L, *SM, LangOpts);
3371 if (CDecl->protocol_begin() != CDecl->protocol_end()) {
3372 // advance to the end of the referenced protocols.
3373 while (endHeader < cursor && *endHeader != '>') endHeader++;
3376 // rewrite the original header
3377 ReplaceText(LocStart, endHeader-startBuf, Result);
3379 // rewrite the original header *without* disturbing the '{'
3380 ReplaceText(LocStart, cursor-startBuf, Result);
3382 if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) {
3383 Result = "\n struct ";
3384 Result += RCDecl->getNameAsString();
3386 Result += RCDecl->getNameAsString();
3387 Result += "_IVARS;\n";
3389 // insert the super class structure definition.
3390 SourceLocation OnePastCurly =
3391 LocStart.getLocWithOffset(cursor-startBuf+1);
3392 InsertText(OnePastCurly, Result);
3394 cursor++; // past '{'
3396 // Now comment out any visibility specifiers.
3397 while (cursor < endBuf) {
3398 if (*cursor == '@') {
3399 SourceLocation atLoc = LocStart.getLocWithOffset(cursor-startBuf);
3401 for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor)
3404 // FIXME: presence of @public, etc. inside comment results in
3405 // this transformation as well, which is still correct c-code.
3406 if (!strncmp(cursor, "public", strlen("public")) ||
3407 !strncmp(cursor, "private", strlen("private")) ||
3408 !strncmp(cursor, "package", strlen("package")) ||
3409 !strncmp(cursor, "protected", strlen("protected")))
3410 InsertText(atLoc, "// ");
3412 // FIXME: If there are cases where '<' is used in ivar declaration part
3413 // of user code, then scan the ivar list and use needToScanForQualifiers
3414 // for type checking.
3415 else if (*cursor == '<') {
3416 SourceLocation atLoc = LocStart.getLocWithOffset(cursor-startBuf);
3417 InsertText(atLoc, "/* ");
3418 cursor = strchr(cursor, '>');
3420 atLoc = LocStart.getLocWithOffset(cursor-startBuf);
3421 InsertText(atLoc, " */");
3422 } else if (*cursor == '^') { // rewrite block specifier.
3423 SourceLocation caretLoc = LocStart.getLocWithOffset(cursor-startBuf);
3424 ReplaceText(caretLoc, 1, "*");
3428 // Don't forget to add a ';'!!
3429 InsertText(LocEnd.getLocWithOffset(1), ";");
3430 } else { // we don't have any instance variables - insert super struct.
3431 endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts);
3432 Result += " {\n struct ";
3433 Result += RCDecl->getNameAsString();
3435 Result += RCDecl->getNameAsString();
3436 Result += "_IVARS;\n};\n";
3437 ReplaceText(LocStart, endBuf-startBuf, Result);
3439 // Mark this struct as having been generated.
3440 if (!ObjCSynthesizedStructs.insert(CDecl))
3441 llvm_unreachable("struct already synthesize- SynthesizeObjCInternalStruct");
3444 // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or
3446 template<typename MethodIterator>
3447 void RewriteObjC::RewriteObjCMethodsMetaData(MethodIterator MethodBegin,
3448 MethodIterator MethodEnd,
3449 bool IsInstanceMethod,
3451 StringRef ClassName,
3452 std::string &Result) {
3453 if (MethodBegin == MethodEnd) return;
3455 if (!objc_impl_method) {
3456 /* struct _objc_method {
3462 Result += "\nstruct _objc_method {\n";
3463 Result += "\tSEL _cmd;\n";
3464 Result += "\tchar *method_types;\n";
3465 Result += "\tvoid *_imp;\n";
3468 objc_impl_method = true;
3471 // Build _objc_method_list for class's methods if needed
3474 struct _objc_method_list *next_method;
3476 struct _objc_method method_list[];
3479 unsigned NumMethods = std::distance(MethodBegin, MethodEnd);
3480 Result += "\nstatic struct {\n";
3481 Result += "\tstruct _objc_method_list *next_method;\n";
3482 Result += "\tint method_count;\n";
3483 Result += "\tstruct _objc_method method_list[";
3484 Result += utostr(NumMethods);
3485 Result += "];\n} _OBJC_";
3487 Result += IsInstanceMethod ? "INSTANCE" : "CLASS";
3488 Result += "_METHODS_";
3489 Result += ClassName;
3490 Result += " __attribute__ ((used, section (\"__OBJC, __";
3491 Result += IsInstanceMethod ? "inst" : "cls";
3492 Result += "_meth\")))= ";
3493 Result += "{\n\t0, " + utostr(NumMethods) + "\n";
3495 Result += "\t,{{(SEL)\"";
3496 Result += (*MethodBegin)->getSelector().getAsString().c_str();
3497 std::string MethodTypeString;
3498 Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString);
3500 Result += MethodTypeString;
3501 Result += "\", (void *)";
3502 Result += MethodInternalNames[*MethodBegin];
3504 for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) {
3505 Result += "\t ,{(SEL)\"";
3506 Result += (*MethodBegin)->getSelector().getAsString().c_str();
3507 std::string MethodTypeString;
3508 Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString);
3510 Result += MethodTypeString;
3511 Result += "\", (void *)";
3512 Result += MethodInternalNames[*MethodBegin];
3515 Result += "\t }\n};\n";
3518 /// RewriteObjCProtocolMetaData - Rewrite protocols meta-data.
3520 RewriteObjCProtocolMetaData(ObjCProtocolDecl *PDecl, StringRef prefix,
3521 StringRef ClassName, std::string &Result) {
3522 static bool objc_protocol_methods = false;
3524 // Output struct protocol_methods holder of method selector and type.
3525 if (!objc_protocol_methods && !PDecl->isForwardDecl()) {
3526 /* struct protocol_methods {
3531 Result += "\nstruct _protocol_methods {\n";
3532 Result += "\tstruct objc_selector *_cmd;\n";
3533 Result += "\tchar *method_types;\n";
3536 objc_protocol_methods = true;
3538 // Do not synthesize the protocol more than once.
3539 if (ObjCSynthesizedProtocols.count(PDecl))
3542 if (PDecl->instmeth_begin() != PDecl->instmeth_end()) {
3543 unsigned NumMethods = std::distance(PDecl->instmeth_begin(),
3544 PDecl->instmeth_end());
3545 /* struct _objc_protocol_method_list {
3546 int protocol_method_count;
3547 struct protocol_methods protocols[];
3550 Result += "\nstatic struct {\n";
3551 Result += "\tint protocol_method_count;\n";
3552 Result += "\tstruct _protocol_methods protocol_methods[";
3553 Result += utostr(NumMethods);
3554 Result += "];\n} _OBJC_PROTOCOL_INSTANCE_METHODS_";
3555 Result += PDecl->getNameAsString();
3556 Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= "
3557 "{\n\t" + utostr(NumMethods) + "\n";
3559 // Output instance methods declared in this protocol.
3560 for (ObjCProtocolDecl::instmeth_iterator
3561 I = PDecl->instmeth_begin(), E = PDecl->instmeth_end();
3563 if (I == PDecl->instmeth_begin())
3564 Result += "\t ,{{(struct objc_selector *)\"";
3566 Result += "\t ,{(struct objc_selector *)\"";
3567 Result += (*I)->getSelector().getAsString();
3568 std::string MethodTypeString;
3569 Context->getObjCEncodingForMethodDecl((*I), MethodTypeString);
3571 Result += MethodTypeString;
3574 Result += "\t }\n};\n";
3577 // Output class methods declared in this protocol.
3578 unsigned NumMethods = std::distance(PDecl->classmeth_begin(),
3579 PDecl->classmeth_end());
3580 if (NumMethods > 0) {
3581 /* struct _objc_protocol_method_list {
3582 int protocol_method_count;
3583 struct protocol_methods protocols[];
3586 Result += "\nstatic struct {\n";
3587 Result += "\tint protocol_method_count;\n";
3588 Result += "\tstruct _protocol_methods protocol_methods[";
3589 Result += utostr(NumMethods);
3590 Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_";
3591 Result += PDecl->getNameAsString();
3592 Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= "
3594 Result += utostr(NumMethods);
3597 // Output instance methods declared in this protocol.
3598 for (ObjCProtocolDecl::classmeth_iterator
3599 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
3601 if (I == PDecl->classmeth_begin())
3602 Result += "\t ,{{(struct objc_selector *)\"";
3604 Result += "\t ,{(struct objc_selector *)\"";
3605 Result += (*I)->getSelector().getAsString();
3606 std::string MethodTypeString;
3607 Context->getObjCEncodingForMethodDecl((*I), MethodTypeString);
3609 Result += MethodTypeString;
3612 Result += "\t }\n};\n";
3616 /* struct _objc_protocol {
3617 // Objective-C 1.0 extensions
3618 struct _objc_protocol_extension *isa;
3619 char *protocol_name;
3620 struct _objc_protocol **protocol_list;
3621 struct _objc_protocol_method_list *instance_methods;
3622 struct _objc_protocol_method_list *class_methods;
3625 static bool objc_protocol = false;
3626 if (!objc_protocol) {
3627 Result += "\nstruct _objc_protocol {\n";
3628 Result += "\tstruct _objc_protocol_extension *isa;\n";
3629 Result += "\tchar *protocol_name;\n";
3630 Result += "\tstruct _objc_protocol **protocol_list;\n";
3631 Result += "\tstruct _objc_protocol_method_list *instance_methods;\n";
3632 Result += "\tstruct _objc_protocol_method_list *class_methods;\n";
3635 objc_protocol = true;
3638 Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_";
3639 Result += PDecl->getNameAsString();
3640 Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= "
3642 Result += PDecl->getNameAsString();
3643 Result += "\", 0, ";
3644 if (PDecl->instmeth_begin() != PDecl->instmeth_end()) {
3645 Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_";
3646 Result += PDecl->getNameAsString();
3651 if (PDecl->classmeth_begin() != PDecl->classmeth_end()) {
3652 Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_";
3653 Result += PDecl->getNameAsString();
3660 // Mark this protocol as having been generated.
3661 if (!ObjCSynthesizedProtocols.insert(PDecl))
3662 llvm_unreachable("protocol already synthesized");
3667 RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Protocols,
3668 StringRef prefix, StringRef ClassName,
3669 std::string &Result) {
3670 if (Protocols.empty()) return;
3672 for (unsigned i = 0; i != Protocols.size(); i++)
3673 RewriteObjCProtocolMetaData(Protocols[i], prefix, ClassName, Result);
3675 // Output the top lovel protocol meta-data for the class.
3676 /* struct _objc_protocol_list {
3677 struct _objc_protocol_list *next;
3679 struct _objc_protocol *class_protocols[];
3682 Result += "\nstatic struct {\n";
3683 Result += "\tstruct _objc_protocol_list *next;\n";
3684 Result += "\tint protocol_count;\n";
3685 Result += "\tstruct _objc_protocol *class_protocols[";
3686 Result += utostr(Protocols.size());
3687 Result += "];\n} _OBJC_";
3689 Result += "_PROTOCOLS_";
3690 Result += ClassName;
3691 Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= "
3693 Result += utostr(Protocols.size());
3696 Result += "\t,{&_OBJC_PROTOCOL_";
3697 Result += Protocols[0]->getNameAsString();
3700 for (unsigned i = 1; i != Protocols.size(); i++) {
3701 Result += "\t ,&_OBJC_PROTOCOL_";
3702 Result += Protocols[i]->getNameAsString();
3705 Result += "\t }\n};\n";
3709 /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category
3711 void RewriteObjC::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl,
3712 std::string &Result) {
3713 ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface();
3714 // Find category declaration for this implementation.
3715 ObjCCategoryDecl *CDecl;
3716 for (CDecl = ClassDecl->getCategoryList(); CDecl;
3717 CDecl = CDecl->getNextClassCategory())
3718 if (CDecl->getIdentifier() == IDecl->getIdentifier())
3721 std::string FullCategoryName = ClassDecl->getNameAsString();
3722 FullCategoryName += '_';
3723 FullCategoryName += IDecl->getNameAsString();
3725 // Build _objc_method_list for class's instance methods if needed
3726 SmallVector<ObjCMethodDecl *, 32>
3727 InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end());
3729 // If any of our property implementations have associated getters or
3730 // setters, produce metadata for them as well.
3731 for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(),
3732 PropEnd = IDecl->propimpl_end();
3733 Prop != PropEnd; ++Prop) {
3734 if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
3736 if (!(*Prop)->getPropertyIvarDecl())
3738 ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl();
3741 if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl())
3742 InstanceMethods.push_back(Getter);
3743 if (PD->isReadOnly())
3745 if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl())
3746 InstanceMethods.push_back(Setter);
3748 RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(),
3749 true, "CATEGORY_", FullCategoryName.c_str(),
3752 // Build _objc_method_list for class's class methods if needed
3753 RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(),
3754 false, "CATEGORY_", FullCategoryName.c_str(),
3757 // Protocols referenced in class declaration?
3758 // Null CDecl is case of a category implementation with no category interface
3760 RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), "CATEGORY",
3761 FullCategoryName, Result);
3762 /* struct _objc_category {
3763 char *category_name;
3765 struct _objc_method_list *instance_methods;
3766 struct _objc_method_list *class_methods;
3767 struct _objc_protocol_list *protocols;
3768 // Objective-C 1.0 extensions
3769 uint32_t size; // sizeof (struct _objc_category)
3770 struct _objc_property_list *instance_properties; // category's own
3775 static bool objc_category = false;
3776 if (!objc_category) {
3777 Result += "\nstruct _objc_category {\n";
3778 Result += "\tchar *category_name;\n";
3779 Result += "\tchar *class_name;\n";
3780 Result += "\tstruct _objc_method_list *instance_methods;\n";
3781 Result += "\tstruct _objc_method_list *class_methods;\n";
3782 Result += "\tstruct _objc_protocol_list *protocols;\n";
3783 Result += "\tunsigned int size;\n";
3784 Result += "\tstruct _objc_property_list *instance_properties;\n";
3786 objc_category = true;
3788 Result += "\nstatic struct _objc_category _OBJC_CATEGORY_";
3789 Result += FullCategoryName;
3790 Result += " __attribute__ ((used, section (\"__OBJC, __category\")))= {\n\t\"";
3791 Result += IDecl->getNameAsString();
3792 Result += "\"\n\t, \"";
3793 Result += ClassDecl->getNameAsString();
3796 if (IDecl->instmeth_begin() != IDecl->instmeth_end()) {
3797 Result += "\t, (struct _objc_method_list *)"
3798 "&_OBJC_CATEGORY_INSTANCE_METHODS_";
3799 Result += FullCategoryName;
3803 Result += "\t, 0\n";
3804 if (IDecl->classmeth_begin() != IDecl->classmeth_end()) {
3805 Result += "\t, (struct _objc_method_list *)"
3806 "&_OBJC_CATEGORY_CLASS_METHODS_";
3807 Result += FullCategoryName;
3811 Result += "\t, 0\n";
3813 if (CDecl && CDecl->protocol_begin() != CDecl->protocol_end()) {
3814 Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_";
3815 Result += FullCategoryName;
3819 Result += "\t, 0\n";
3820 Result += "\t, sizeof(struct _objc_category), 0\n};\n";
3823 /// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of
3825 void RewriteObjC::SynthesizeIvarOffsetComputation(ObjCIvarDecl *ivar,
3826 std::string &Result) {
3827 if (ivar->isBitField()) {
3828 // FIXME: The hack below doesn't work for bitfields. For now, we simply
3829 // place all bitfields at offset 0.
3832 Result += "__OFFSETOFIVAR__(struct ";
3833 Result += ivar->getContainingInterface()->getNameAsString();
3834 if (LangOpts.MicrosoftExt)
3837 Result += ivar->getNameAsString();
3842 //===----------------------------------------------------------------------===//
3843 // Meta Data Emission
3844 //===----------------------------------------------------------------------===//
3846 void RewriteObjC::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl,
3847 std::string &Result) {
3848 ObjCInterfaceDecl *CDecl = IDecl->getClassInterface();
3850 // Explicitly declared @interface's are already synthesized.
3851 if (CDecl->isImplicitInterfaceDecl()) {
3852 // FIXME: Implementation of a class with no @interface (legacy) doese not
3853 // produce correct synthesis as yet.
3854 SynthesizeObjCInternalStruct(CDecl, Result);
3857 // Build _objc_ivar_list metadata for classes ivars if needed
3858 unsigned NumIvars = !IDecl->ivar_empty()
3859 ? IDecl->ivar_size()
3860 : (CDecl ? CDecl->ivar_size() : 0);
3862 static bool objc_ivar = false;
3864 /* struct _objc_ivar {
3870 Result += "\nstruct _objc_ivar {\n";
3871 Result += "\tchar *ivar_name;\n";
3872 Result += "\tchar *ivar_type;\n";
3873 Result += "\tint ivar_offset;\n";
3881 struct _objc_ivar ivar_list[nIvars];
3884 Result += "\nstatic struct {\n";
3885 Result += "\tint ivar_count;\n";
3886 Result += "\tstruct _objc_ivar ivar_list[";
3887 Result += utostr(NumIvars);
3888 Result += "];\n} _OBJC_INSTANCE_VARIABLES_";
3889 Result += IDecl->getNameAsString();
3890 Result += " __attribute__ ((used, section (\"__OBJC, __instance_vars\")))= "
3892 Result += utostr(NumIvars);
3895 ObjCInterfaceDecl::ivar_iterator IVI, IVE;
3896 SmallVector<ObjCIvarDecl *, 8> IVars;
3897 if (!IDecl->ivar_empty()) {
3898 for (ObjCInterfaceDecl::ivar_iterator
3899 IV = IDecl->ivar_begin(), IVEnd = IDecl->ivar_end();
3901 IVars.push_back(*IV);
3902 IVI = IDecl->ivar_begin();
3903 IVE = IDecl->ivar_end();
3905 IVI = CDecl->ivar_begin();
3906 IVE = CDecl->ivar_end();
3908 Result += "\t,{{\"";
3909 Result += (*IVI)->getNameAsString();
3911 std::string TmpString, StrEncoding;
3912 Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI);
3913 QuoteDoublequotes(TmpString, StrEncoding);
3914 Result += StrEncoding;
3916 SynthesizeIvarOffsetComputation(*IVI, Result);
3918 for (++IVI; IVI != IVE; ++IVI) {
3919 Result += "\t ,{\"";
3920 Result += (*IVI)->getNameAsString();
3922 std::string TmpString, StrEncoding;
3923 Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI);
3924 QuoteDoublequotes(TmpString, StrEncoding);
3925 Result += StrEncoding;
3927 SynthesizeIvarOffsetComputation((*IVI), Result);
3931 Result += "\t }\n};\n";
3934 // Build _objc_method_list for class's instance methods if needed
3935 SmallVector<ObjCMethodDecl *, 32>
3936 InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end());
3938 // If any of our property implementations have associated getters or
3939 // setters, produce metadata for them as well.
3940 for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(),
3941 PropEnd = IDecl->propimpl_end();
3942 Prop != PropEnd; ++Prop) {
3943 if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
3945 if (!(*Prop)->getPropertyIvarDecl())
3947 ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl();
3950 if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl())
3951 if (!Getter->isDefined())
3952 InstanceMethods.push_back(Getter);
3953 if (PD->isReadOnly())
3955 if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl())
3956 if (!Setter->isDefined())
3957 InstanceMethods.push_back(Setter);
3959 RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(),
3960 true, "", IDecl->getName(), Result);
3962 // Build _objc_method_list for class's class methods if needed
3963 RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(),
3964 false, "", IDecl->getName(), Result);
3966 // Protocols referenced in class declaration?
3967 RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(),
3968 "CLASS", CDecl->getName(), Result);
3970 // Declaration of class/meta-class metadata
3971 /* struct _objc_class {
3972 struct _objc_class *isa; // or const char *root_class_name when metadata
3973 const char *super_class_name;
3978 struct _objc_ivar_list *ivars;
3979 struct _objc_method_list *methods;
3980 struct objc_cache *cache;
3981 struct objc_protocol_list *protocols;
3982 const char *ivar_layout;
3983 struct _objc_class_ext *ext;
3986 static bool objc_class = false;
3988 Result += "\nstruct _objc_class {\n";
3989 Result += "\tstruct _objc_class *isa;\n";
3990 Result += "\tconst char *super_class_name;\n";
3991 Result += "\tchar *name;\n";
3992 Result += "\tlong version;\n";
3993 Result += "\tlong info;\n";
3994 Result += "\tlong instance_size;\n";
3995 Result += "\tstruct _objc_ivar_list *ivars;\n";
3996 Result += "\tstruct _objc_method_list *methods;\n";
3997 Result += "\tstruct objc_cache *cache;\n";
3998 Result += "\tstruct _objc_protocol_list *protocols;\n";
3999 Result += "\tconst char *ivar_layout;\n";
4000 Result += "\tstruct _objc_class_ext *ext;\n";
4005 // Meta-class metadata generation.
4006 ObjCInterfaceDecl *RootClass = 0;
4007 ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass();
4008 while (SuperClass) {
4009 RootClass = SuperClass;
4010 SuperClass = SuperClass->getSuperClass();
4012 SuperClass = CDecl->getSuperClass();
4014 Result += "\nstatic struct _objc_class _OBJC_METACLASS_";
4015 Result += CDecl->getNameAsString();
4016 Result += " __attribute__ ((used, section (\"__OBJC, __meta_class\")))= "
4017 "{\n\t(struct _objc_class *)\"";
4018 Result += (RootClass ? RootClass->getNameAsString() : CDecl->getNameAsString());
4023 Result += SuperClass->getNameAsString();
4025 Result += CDecl->getNameAsString();
4029 Result += ", 0, \"";
4030 Result += CDecl->getNameAsString();
4033 // Set 'ivars' field for root class to 0. ObjC1 runtime does not use it.
4034 // 'info' field is initialized to CLS_META(2) for metaclass
4035 Result += ", 0,2, sizeof(struct _objc_class), 0";
4036 if (IDecl->classmeth_begin() != IDecl->classmeth_end()) {
4037 Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_";
4038 Result += IDecl->getNameAsString();
4043 if (CDecl->protocol_begin() != CDecl->protocol_end()) {
4044 Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_";
4045 Result += CDecl->getNameAsString();
4049 Result += "\t,0,0,0,0\n";
4052 // class metadata generation.
4053 Result += "\nstatic struct _objc_class _OBJC_CLASS_";
4054 Result += CDecl->getNameAsString();
4055 Result += " __attribute__ ((used, section (\"__OBJC, __class\")))= "
4056 "{\n\t&_OBJC_METACLASS_";
4057 Result += CDecl->getNameAsString();
4060 Result += SuperClass->getNameAsString();
4062 Result += CDecl->getNameAsString();
4066 Result += ", 0, \"";
4067 Result += CDecl->getNameAsString();
4070 // 'info' field is initialized to CLS_CLASS(1) for class
4072 if (!ObjCSynthesizedStructs.count(CDecl))
4075 // class has size. Must synthesize its size.
4076 Result += ",sizeof(struct ";
4077 Result += CDecl->getNameAsString();
4078 if (LangOpts.MicrosoftExt)
4083 Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_";
4084 Result += CDecl->getNameAsString();
4089 if (IDecl->instmeth_begin() != IDecl->instmeth_end()) {
4090 Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_";
4091 Result += CDecl->getNameAsString();
4092 Result += ", 0\n\t";
4096 if (CDecl->protocol_begin() != CDecl->protocol_end()) {
4097 Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_";
4098 Result += CDecl->getNameAsString();
4099 Result += ", 0,0\n";
4102 Result += ",0,0,0\n";
4106 /// RewriteImplementations - This routine rewrites all method implementations
4107 /// and emits meta-data.
4109 void RewriteObjC::RewriteImplementations() {
4110 int ClsDefCount = ClassImplementation.size();
4111 int CatDefCount = CategoryImplementation.size();
4113 // Rewrite implemented methods
4114 for (int i = 0; i < ClsDefCount; i++)
4115 RewriteImplementationDecl(ClassImplementation[i]);
4117 for (int i = 0; i < CatDefCount; i++)
4118 RewriteImplementationDecl(CategoryImplementation[i]);
4121 void RewriteObjC::SynthesizeMetaDataIntoBuffer(std::string &Result) {
4122 int ClsDefCount = ClassImplementation.size();
4123 int CatDefCount = CategoryImplementation.size();
4125 // For each implemented class, write out all its meta data.
4126 for (int i = 0; i < ClsDefCount; i++)
4127 RewriteObjCClassMetaData(ClassImplementation[i], Result);
4129 // For each implemented category, write out all its meta data.
4130 for (int i = 0; i < CatDefCount; i++)
4131 RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result);
4133 // Write objc_symtab metadata
4141 void *defs[cls_def_cnt + cat_def_cnt];
4145 Result += "\nstruct _objc_symtab {\n";
4146 Result += "\tlong sel_ref_cnt;\n";
4147 Result += "\tSEL *refs;\n";
4148 Result += "\tshort cls_def_cnt;\n";
4149 Result += "\tshort cat_def_cnt;\n";
4150 Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n";
4153 Result += "static struct _objc_symtab "
4154 "_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n";
4155 Result += "\t0, 0, " + utostr(ClsDefCount)
4156 + ", " + utostr(CatDefCount) + "\n";
4157 for (int i = 0; i < ClsDefCount; i++) {
4158 Result += "\t,&_OBJC_CLASS_";
4159 Result += ClassImplementation[i]->getNameAsString();
4163 for (int i = 0; i < CatDefCount; i++) {
4164 Result += "\t,&_OBJC_CATEGORY_";
4165 Result += CategoryImplementation[i]->getClassInterface()->getNameAsString();
4167 Result += CategoryImplementation[i]->getNameAsString();
4173 // Write objc_module metadata
4176 struct _objc_module {
4180 struct _objc_symtab *symtab;
4184 Result += "\nstruct _objc_module {\n";
4185 Result += "\tlong version;\n";
4186 Result += "\tlong size;\n";
4187 Result += "\tconst char *name;\n";
4188 Result += "\tstruct _objc_symtab *symtab;\n";
4190 Result += "static struct _objc_module "
4191 "_OBJC_MODULES __attribute__ ((used, section (\"__OBJC, __module_info\")))= {\n";
4192 Result += "\t" + utostr(OBJC_ABI_VERSION) +
4193 ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n";
4196 if (LangOpts.MicrosoftExt) {
4197 if (ProtocolExprDecls.size()) {
4198 Result += "#pragma section(\".objc_protocol$B\",long,read,write)\n";
4199 Result += "#pragma data_seg(push, \".objc_protocol$B\")\n";
4200 for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(),
4201 E = ProtocolExprDecls.end(); I != E; ++I) {
4202 Result += "static struct _objc_protocol *_POINTER_OBJC_PROTOCOL_";
4203 Result += (*I)->getNameAsString();
4204 Result += " = &_OBJC_PROTOCOL_";
4205 Result += (*I)->getNameAsString();
4208 Result += "#pragma data_seg(pop)\n\n";
4210 Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n";
4211 Result += "#pragma data_seg(push, \".objc_module_info$B\")\n";
4212 Result += "static struct _objc_module *_POINTER_OBJC_MODULES = ";
4213 Result += "&_OBJC_MODULES;\n";
4214 Result += "#pragma data_seg(pop)\n\n";
4218 void RewriteObjC::RewriteByRefString(std::string &ResultStr,
4219 const std::string &Name,
4220 ValueDecl *VD, bool def) {
4221 assert(BlockByRefDeclNo.count(VD) &&
4222 "RewriteByRefString: ByRef decl missing");
4224 ResultStr += "struct ";
4225 ResultStr += "__Block_byref_" + Name +
4226 "_" + utostr(BlockByRefDeclNo[VD]) ;
4229 static bool HasLocalVariableExternalStorage(ValueDecl *VD) {
4230 if (VarDecl *Var = dyn_cast<VarDecl>(VD))
4231 return (Var->isFunctionOrMethodVarDecl() && !Var->hasLocalStorage());
4235 std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i,
4238 const FunctionType *AFT = CE->getFunctionType();
4239 QualType RT = AFT->getResultType();
4240 std::string StructRef = "struct " + Tag;
4241 std::string S = "static " + RT.getAsString(Context->getPrintingPolicy()) + " __" +
4242 funcName.str() + "_" + "block_func_" + utostr(i);
4244 BlockDecl *BD = CE->getBlockDecl();
4246 if (isa<FunctionNoProtoType>(AFT)) {
4247 // No user-supplied arguments. Still need to pass in a pointer to the
4248 // block (to reference imported block decl refs).
4249 S += "(" + StructRef + " *__cself)";
4250 } else if (BD->param_empty()) {
4251 S += "(" + StructRef + " *__cself)";
4253 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
4254 assert(FT && "SynthesizeBlockFunc: No function proto");
4256 // first add the implicit argument.
4257 S += StructRef + " *__cself, ";
4258 std::string ParamStr;
4259 for (BlockDecl::param_iterator AI = BD->param_begin(),
4260 E = BD->param_end(); AI != E; ++AI) {
4261 if (AI != BD->param_begin()) S += ", ";
4262 ParamStr = (*AI)->getNameAsString();
4263 QualType QT = (*AI)->getType();
4264 if (convertBlockPointerToFunctionPointer(QT))
4265 QT.getAsStringInternal(ParamStr, Context->getPrintingPolicy());
4267 QT.getAsStringInternal(ParamStr, Context->getPrintingPolicy());
4270 if (FT->isVariadic()) {
4271 if (!BD->param_empty()) S += ", ";
4278 // Create local declarations to avoid rewriting all closure decl ref exprs.
4279 // First, emit a declaration for all "by ref" decls.
4280 for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(),
4281 E = BlockByRefDecls.end(); I != E; ++I) {
4283 std::string Name = (*I)->getNameAsString();
4284 std::string TypeString;
4285 RewriteByRefString(TypeString, Name, (*I));
4287 Name = TypeString + Name;
4288 S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n";
4290 // Next, emit a declaration for all "by copy" declarations.
4291 for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(),
4292 E = BlockByCopyDecls.end(); I != E; ++I) {
4294 // Handle nested closure invocation. For example:
4296 // void (^myImportedClosure)(void);
4297 // myImportedClosure = ^(void) { setGlobalInt(x + y); };
4299 // void (^anotherClosure)(void);
4300 // anotherClosure = ^(void) {
4301 // myImportedClosure(); // import and invoke the closure
4304 if (isTopLevelBlockPointerType((*I)->getType())) {
4305 RewriteBlockPointerTypeVariable(S, (*I));
4307 RewriteBlockPointerType(S, (*I)->getType());
4309 S += "__cself->" + (*I)->getNameAsString() + "; // bound by copy\n";
4312 std::string Name = (*I)->getNameAsString();
4313 QualType QT = (*I)->getType();
4314 if (HasLocalVariableExternalStorage(*I))
4315 QT = Context->getPointerType(QT);
4316 QT.getAsStringInternal(Name, Context->getPrintingPolicy());
4317 S += Name + " = __cself->" +
4318 (*I)->getNameAsString() + "; // bound by copy\n";
4321 std::string RewrittenStr = RewrittenBlockExprs[CE];
4322 const char *cstr = RewrittenStr.c_str();
4323 while (*cstr++ != '{') ;
4329 std::string RewriteObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i,
4332 std::string StructRef = "struct " + Tag;
4333 std::string S = "static void __";
4336 S += "_block_copy_" + utostr(i);
4337 S += "(" + StructRef;
4338 S += "*dst, " + StructRef;
4340 for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(),
4341 E = ImportedBlockDecls.end(); I != E; ++I) {
4342 ValueDecl *VD = (*I);
4343 S += "_Block_object_assign((void*)&dst->";
4344 S += (*I)->getNameAsString();
4345 S += ", (void*)src->";
4346 S += (*I)->getNameAsString();
4347 if (BlockByRefDeclsPtrSet.count((*I)))
4348 S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);";
4349 else if (VD->getType()->isBlockPointerType())
4350 S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);";
4352 S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);";
4356 S += "\nstatic void __";
4358 S += "_block_dispose_" + utostr(i);
4359 S += "(" + StructRef;
4361 for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(),
4362 E = ImportedBlockDecls.end(); I != E; ++I) {
4363 ValueDecl *VD = (*I);
4364 S += "_Block_object_dispose((void*)src->";
4365 S += (*I)->getNameAsString();
4366 if (BlockByRefDeclsPtrSet.count((*I)))
4367 S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);";
4368 else if (VD->getType()->isBlockPointerType())
4369 S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);";
4371 S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);";
4377 std::string RewriteObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag,
4379 std::string S = "\nstruct " + Tag;
4380 std::string Constructor = " " + Tag;
4382 S += " {\n struct __block_impl impl;\n";
4383 S += " struct " + Desc;
4386 Constructor += "(void *fp, "; // Invoke function pointer.
4387 Constructor += "struct " + Desc; // Descriptor pointer.
4388 Constructor += " *desc";
4390 if (BlockDeclRefs.size()) {
4391 // Output all "by copy" declarations.
4392 for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(),
4393 E = BlockByCopyDecls.end(); I != E; ++I) {
4395 std::string FieldName = (*I)->getNameAsString();
4396 std::string ArgName = "_" + FieldName;
4397 // Handle nested closure invocation. For example:
4399 // void (^myImportedBlock)(void);
4400 // myImportedBlock = ^(void) { setGlobalInt(x + y); };
4402 // void (^anotherBlock)(void);
4403 // anotherBlock = ^(void) {
4404 // myImportedBlock(); // import and invoke the closure
4407 if (isTopLevelBlockPointerType((*I)->getType())) {
4408 S += "struct __block_impl *";
4409 Constructor += ", void *" + ArgName;
4411 QualType QT = (*I)->getType();
4412 if (HasLocalVariableExternalStorage(*I))
4413 QT = Context->getPointerType(QT);
4414 QT.getAsStringInternal(FieldName, Context->getPrintingPolicy());
4415 QT.getAsStringInternal(ArgName, Context->getPrintingPolicy());
4416 Constructor += ", " + ArgName;
4418 S += FieldName + ";\n";
4420 // Output all "by ref" declarations.
4421 for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(),
4422 E = BlockByRefDecls.end(); I != E; ++I) {
4424 std::string FieldName = (*I)->getNameAsString();
4425 std::string ArgName = "_" + FieldName;
4427 std::string TypeString;
4428 RewriteByRefString(TypeString, FieldName, (*I));
4430 FieldName = TypeString + FieldName;
4431 ArgName = TypeString + ArgName;
4432 Constructor += ", " + ArgName;
4434 S += FieldName + "; // by ref\n";
4436 // Finish writing the constructor.
4437 Constructor += ", int flags=0)";
4438 // Initialize all "by copy" arguments.
4439 bool firsTime = true;
4440 for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(),
4441 E = BlockByCopyDecls.end(); I != E; ++I) {
4442 std::string Name = (*I)->getNameAsString();
4444 Constructor += " : ";
4448 Constructor += ", ";
4449 if (isTopLevelBlockPointerType((*I)->getType()))
4450 Constructor += Name + "((struct __block_impl *)_" + Name + ")";
4452 Constructor += Name + "(_" + Name + ")";
4454 // Initialize all "by ref" arguments.
4455 for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(),
4456 E = BlockByRefDecls.end(); I != E; ++I) {
4457 std::string Name = (*I)->getNameAsString();
4459 Constructor += " : ";
4463 Constructor += ", ";
4464 Constructor += Name + "(_" + Name + "->__forwarding)";
4467 Constructor += " {\n";
4469 Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n";
4471 Constructor += " impl.isa = &_NSConcreteStackBlock;\n";
4472 Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n";
4474 Constructor += " Desc = desc;\n";
4476 // Finish writing the constructor.
4477 Constructor += ", int flags=0) {\n";
4479 Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n";
4481 Constructor += " impl.isa = &_NSConcreteStackBlock;\n";
4482 Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n";
4483 Constructor += " Desc = desc;\n";
4486 Constructor += "}\n";
4492 std::string RewriteObjC::SynthesizeBlockDescriptor(std::string DescTag,
4493 std::string ImplTag, int i,
4496 std::string S = "\nstatic struct " + DescTag;
4498 S += " {\n unsigned long reserved;\n";
4499 S += " unsigned long Block_size;\n";
4501 S += " void (*copy)(struct ";
4502 S += ImplTag; S += "*, struct ";
4503 S += ImplTag; S += "*);\n";
4505 S += " void (*dispose)(struct ";
4506 S += ImplTag; S += "*);\n";
4510 S += DescTag + "_DATA = { 0, sizeof(struct ";
4513 S += ", __" + FunName.str() + "_block_copy_" + utostr(i);
4514 S += ", __" + FunName.str() + "_block_dispose_" + utostr(i);
4520 void RewriteObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart,
4521 StringRef FunName) {
4522 // Insert declaration for the function in which block literal is used.
4523 if (CurFunctionDeclToDeclareForBlock && !Blocks.empty())
4524 RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock);
4525 bool RewriteSC = (GlobalVarDecl &&
4527 GlobalVarDecl->getStorageClass() == SC_Static &&
4528 GlobalVarDecl->getType().getCVRQualifiers());
4530 std::string SC(" void __");
4531 SC += GlobalVarDecl->getNameAsString();
4533 InsertText(FunLocStart, SC);
4536 // Insert closures that were part of the function.
4537 for (unsigned i = 0, count=0; i < Blocks.size(); i++) {
4538 CollectBlockDeclRefInfo(Blocks[i]);
4539 // Need to copy-in the inner copied-in variables not actually used in this
4541 for (int j = 0; j < InnerDeclRefsCount[i]; j++) {
4542 BlockDeclRefExpr *Exp = InnerDeclRefs[count++];
4543 ValueDecl *VD = Exp->getDecl();
4544 BlockDeclRefs.push_back(Exp);
4545 if (!Exp->isByRef() && !BlockByCopyDeclsPtrSet.count(VD)) {
4546 BlockByCopyDeclsPtrSet.insert(VD);
4547 BlockByCopyDecls.push_back(VD);
4549 if (Exp->isByRef() && !BlockByRefDeclsPtrSet.count(VD)) {
4550 BlockByRefDeclsPtrSet.insert(VD);
4551 BlockByRefDecls.push_back(VD);
4553 // imported objects in the inner blocks not used in the outer
4554 // blocks must be copied/disposed in the outer block as well.
4555 if (Exp->isByRef() ||
4556 VD->getType()->isObjCObjectPointerType() ||
4557 VD->getType()->isBlockPointerType())
4558 ImportedBlockDecls.insert(VD);
4561 std::string ImplTag = "__" + FunName.str() + "_block_impl_" + utostr(i);
4562 std::string DescTag = "__" + FunName.str() + "_block_desc_" + utostr(i);
4564 std::string CI = SynthesizeBlockImpl(Blocks[i], ImplTag, DescTag);
4566 InsertText(FunLocStart, CI);
4568 std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, ImplTag);
4570 InsertText(FunLocStart, CF);
4572 if (ImportedBlockDecls.size()) {
4573 std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, ImplTag);
4574 InsertText(FunLocStart, HF);
4576 std::string BD = SynthesizeBlockDescriptor(DescTag, ImplTag, i, FunName,
4577 ImportedBlockDecls.size() > 0);
4578 InsertText(FunLocStart, BD);
4580 BlockDeclRefs.clear();
4581 BlockByRefDecls.clear();
4582 BlockByRefDeclsPtrSet.clear();
4583 BlockByCopyDecls.clear();
4584 BlockByCopyDeclsPtrSet.clear();
4585 ImportedBlockDecls.clear();
4588 // Must insert any 'const/volatile/static here. Since it has been
4589 // removed as result of rewriting of block literals.
4591 if (GlobalVarDecl->getStorageClass() == SC_Static)
4593 if (GlobalVarDecl->getType().isConstQualified())
4595 if (GlobalVarDecl->getType().isVolatileQualified())
4597 if (GlobalVarDecl->getType().isRestrictQualified())
4599 InsertText(FunLocStart, SC);
4603 InnerDeclRefsCount.clear();
4604 InnerDeclRefs.clear();
4605 RewrittenBlockExprs.clear();
4608 void RewriteObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) {
4609 SourceLocation FunLocStart = FD->getTypeSpecStartLoc();
4610 StringRef FuncName = FD->getName();
4612 SynthesizeBlockLiterals(FunLocStart, FuncName);
4615 static void BuildUniqueMethodName(std::string &Name,
4616 ObjCMethodDecl *MD) {
4617 ObjCInterfaceDecl *IFace = MD->getClassInterface();
4618 Name = IFace->getName();
4619 Name += "__" + MD->getSelector().getAsString();
4620 // Convert colons to underscores.
4621 std::string::size_type loc = 0;
4622 while ((loc = Name.find(":", loc)) != std::string::npos)
4623 Name.replace(loc, 1, "_");
4626 void RewriteObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) {
4627 //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n");
4628 //SourceLocation FunLocStart = MD->getLocStart();
4629 SourceLocation FunLocStart = MD->getLocStart();
4630 std::string FuncName;
4631 BuildUniqueMethodName(FuncName, MD);
4632 SynthesizeBlockLiterals(FunLocStart, FuncName);
4635 void RewriteObjC::GetBlockDeclRefExprs(Stmt *S) {
4636 for (Stmt::child_range CI = S->children(); CI; ++CI)
4638 if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI))
4639 GetBlockDeclRefExprs(CBE->getBody());
4641 GetBlockDeclRefExprs(*CI);
4643 // Handle specific things.
4644 if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) {
4645 // FIXME: Handle enums.
4646 if (!isa<FunctionDecl>(CDRE->getDecl()))
4647 BlockDeclRefs.push_back(CDRE);
4649 else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S))
4650 if (HasLocalVariableExternalStorage(DRE->getDecl())) {
4651 BlockDeclRefExpr *BDRE =
4652 new (Context)BlockDeclRefExpr(cast<VarDecl>(DRE->getDecl()),
4654 VK_LValue, DRE->getLocation(), false);
4655 BlockDeclRefs.push_back(BDRE);
4661 void RewriteObjC::GetInnerBlockDeclRefExprs(Stmt *S,
4662 SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs,
4663 llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts) {
4664 for (Stmt::child_range CI = S->children(); CI; ++CI)
4666 if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) {
4667 InnerContexts.insert(cast<DeclContext>(CBE->getBlockDecl()));
4668 GetInnerBlockDeclRefExprs(CBE->getBody(),
4673 GetInnerBlockDeclRefExprs(*CI,
4678 // Handle specific things.
4679 if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) {
4680 if (!isa<FunctionDecl>(CDRE->getDecl()) &&
4681 !InnerContexts.count(CDRE->getDecl()->getDeclContext()))
4682 InnerBlockDeclRefs.push_back(CDRE);
4684 else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) {
4685 if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl()))
4686 if (Var->isFunctionOrMethodVarDecl())
4687 ImportedLocalExternalDecls.insert(Var);
4693 /// convertFunctionTypeOfBlocks - This routine converts a function type
4694 /// whose result type may be a block pointer or whose argument type(s)
4695 /// might be block pointers to an equivalent function type replacing
4696 /// all block pointers to function pointers.
4697 QualType RewriteObjC::convertFunctionTypeOfBlocks(const FunctionType *FT) {
4698 const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT);
4699 // FTP will be null for closures that don't take arguments.
4700 // Generate a funky cast.
4701 SmallVector<QualType, 8> ArgTypes;
4702 QualType Res = FT->getResultType();
4703 bool HasBlockType = convertBlockPointerToFunctionPointer(Res);
4706 for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(),
4707 E = FTP->arg_type_end(); I && (I != E); ++I) {
4709 // Make sure we convert "t (^)(...)" to "t (*)(...)".
4710 if (convertBlockPointerToFunctionPointer(t))
4711 HasBlockType = true;
4712 ArgTypes.push_back(t);
4716 // FIXME. Does this work if block takes no argument but has a return type
4717 // which is of block type?
4719 FuncType = getSimpleFunctionType(Res, &ArgTypes[0], ArgTypes.size());
4720 else FuncType = QualType(FT, 0);
4724 Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp, const Expr *BlockExp) {
4725 // Navigate to relevant type information.
4726 const BlockPointerType *CPT = 0;
4728 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BlockExp)) {
4729 CPT = DRE->getType()->getAs<BlockPointerType>();
4730 } else if (const BlockDeclRefExpr *CDRE =
4731 dyn_cast<BlockDeclRefExpr>(BlockExp)) {
4732 CPT = CDRE->getType()->getAs<BlockPointerType>();
4733 } else if (const MemberExpr *MExpr = dyn_cast<MemberExpr>(BlockExp)) {
4734 CPT = MExpr->getType()->getAs<BlockPointerType>();
4736 else if (const ParenExpr *PRE = dyn_cast<ParenExpr>(BlockExp)) {
4737 return SynthesizeBlockCall(Exp, PRE->getSubExpr());
4739 else if (const ImplicitCastExpr *IEXPR = dyn_cast<ImplicitCastExpr>(BlockExp))
4740 CPT = IEXPR->getType()->getAs<BlockPointerType>();
4741 else if (const ConditionalOperator *CEXPR =
4742 dyn_cast<ConditionalOperator>(BlockExp)) {
4743 Expr *LHSExp = CEXPR->getLHS();
4744 Stmt *LHSStmt = SynthesizeBlockCall(Exp, LHSExp);
4745 Expr *RHSExp = CEXPR->getRHS();
4746 Stmt *RHSStmt = SynthesizeBlockCall(Exp, RHSExp);
4747 Expr *CONDExp = CEXPR->getCond();
4748 ConditionalOperator *CondExpr =
4749 new (Context) ConditionalOperator(CONDExp,
4750 SourceLocation(), cast<Expr>(LHSStmt),
4751 SourceLocation(), cast<Expr>(RHSStmt),
4752 Exp->getType(), VK_RValue, OK_Ordinary);
4754 } else if (const ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(BlockExp)) {
4755 CPT = IRE->getType()->getAs<BlockPointerType>();
4757 assert(1 && "RewriteBlockClass: Bad type");
4759 assert(CPT && "RewriteBlockClass: Bad type");
4760 const FunctionType *FT = CPT->getPointeeType()->getAs<FunctionType>();
4761 assert(FT && "RewriteBlockClass: Bad type");
4762 const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT);
4763 // FTP will be null for closures that don't take arguments.
4765 RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl,
4766 SourceLocation(), SourceLocation(),
4767 &Context->Idents.get("__block_impl"));
4768 QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD));
4770 // Generate a funky cast.
4771 SmallVector<QualType, 8> ArgTypes;
4773 // Push the block argument type.
4774 ArgTypes.push_back(PtrBlock);
4776 for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(),
4777 E = FTP->arg_type_end(); I && (I != E); ++I) {
4779 // Make sure we convert "t (^)(...)" to "t (*)(...)".
4780 if (!convertBlockPointerToFunctionPointer(t))
4781 convertToUnqualifiedObjCType(t);
4782 ArgTypes.push_back(t);
4785 // Now do the pointer to function cast.
4786 QualType PtrToFuncCastType
4787 = getSimpleFunctionType(Exp->getType(), &ArgTypes[0], ArgTypes.size());
4789 PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType);
4791 CastExpr *BlkCast = NoTypeInfoCStyleCastExpr(Context, PtrBlock,
4793 const_cast<Expr*>(BlockExp));
4794 // Don't forget the parens to enforce the proper binding.
4795 ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(),
4799 FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(),
4801 &Context->Idents.get("FuncPtr"),
4802 Context->VoidPtrTy, 0,
4803 /*BitWidth=*/0, /*Mutable=*/true,
4805 MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(),
4806 FD->getType(), VK_LValue,
4810 CastExpr *FunkCast = NoTypeInfoCStyleCastExpr(Context, PtrToFuncCastType,
4812 PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast);
4814 SmallVector<Expr*, 8> BlkExprs;
4815 // Add the implicit argument.
4816 BlkExprs.push_back(BlkCast);
4817 // Add the user arguments.
4818 for (CallExpr::arg_iterator I = Exp->arg_begin(),
4819 E = Exp->arg_end(); I != E; ++I) {
4820 BlkExprs.push_back(*I);
4822 CallExpr *CE = new (Context) CallExpr(*Context, PE, &BlkExprs[0],
4824 Exp->getType(), VK_RValue,
4829 // We need to return the rewritten expression to handle cases where the
4830 // BlockDeclRefExpr is embedded in another expression being rewritten.
4837 // void (^myblock)() = ^() {
4838 // [f test]; // f is a BlockDeclRefExpr embedded in a message (which is being rewritten).
4842 Stmt *RewriteObjC::RewriteBlockDeclRefExpr(Expr *DeclRefExp) {
4843 // Rewrite the byref variable into BYREFVAR->__forwarding->BYREFVAR
4844 // for each DeclRefExp where BYREFVAR is name of the variable.
4846 bool isArrow = true;
4847 if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(DeclRefExp))
4848 VD = BDRE->getDecl();
4850 VD = cast<DeclRefExpr>(DeclRefExp)->getDecl();
4854 FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(),
4856 &Context->Idents.get("__forwarding"),
4857 Context->VoidPtrTy, 0,
4858 /*BitWidth=*/0, /*Mutable=*/true,
4860 MemberExpr *ME = new (Context) MemberExpr(DeclRefExp, isArrow,
4861 FD, SourceLocation(),
4862 FD->getType(), VK_LValue,
4865 StringRef Name = VD->getName();
4866 FD = FieldDecl::Create(*Context, 0, SourceLocation(), SourceLocation(),
4867 &Context->Idents.get(Name),
4868 Context->VoidPtrTy, 0,
4869 /*BitWidth=*/0, /*Mutable=*/true,
4871 ME = new (Context) MemberExpr(ME, true, FD, SourceLocation(),
4872 DeclRefExp->getType(), VK_LValue, OK_Ordinary);
4876 // Need parens to enforce precedence.
4877 ParenExpr *PE = new (Context) ParenExpr(DeclRefExp->getExprLoc(),
4878 DeclRefExp->getExprLoc(),
4880 ReplaceStmt(DeclRefExp, PE);
4884 // Rewrites the imported local variable V with external storage
4885 // (static, extern, etc.) as *V
4887 Stmt *RewriteObjC::RewriteLocalVariableExternalStorage(DeclRefExpr *DRE) {
4888 ValueDecl *VD = DRE->getDecl();
4889 if (VarDecl *Var = dyn_cast<VarDecl>(VD))
4890 if (!ImportedLocalExternalDecls.count(Var))
4892 Expr *Exp = new (Context) UnaryOperator(DRE, UO_Deref, DRE->getType(),
4893 VK_LValue, OK_Ordinary,
4894 DRE->getLocation());
4895 // Need parens to enforce precedence.
4896 ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(),
4898 ReplaceStmt(DRE, PE);
4902 void RewriteObjC::RewriteCastExpr(CStyleCastExpr *CE) {
4903 SourceLocation LocStart = CE->getLParenLoc();
4904 SourceLocation LocEnd = CE->getRParenLoc();
4906 // Need to avoid trying to rewrite synthesized casts.
4907 if (LocStart.isInvalid())
4909 // Need to avoid trying to rewrite casts contained in macros.
4910 if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd))
4913 const char *startBuf = SM->getCharacterData(LocStart);
4914 const char *endBuf = SM->getCharacterData(LocEnd);
4915 QualType QT = CE->getType();
4916 const Type* TypePtr = QT->getAs<Type>();
4917 if (isa<TypeOfExprType>(TypePtr)) {
4918 const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr);
4919 QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType();
4920 std::string TypeAsString = "(";
4921 RewriteBlockPointerType(TypeAsString, QT);
4922 TypeAsString += ")";
4923 ReplaceText(LocStart, endBuf-startBuf+1, TypeAsString);
4926 // advance the location to startArgList.
4927 const char *argPtr = startBuf;
4929 while (*argPtr++ && (argPtr < endBuf)) {
4932 // Replace the '^' with '*'.
4933 LocStart = LocStart.getLocWithOffset(argPtr-startBuf);
4934 ReplaceText(LocStart, 1, "*");
4941 void RewriteObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) {
4942 SourceLocation DeclLoc = FD->getLocation();
4943 unsigned parenCount = 0;
4945 // We have 1 or more arguments that have closure pointers.
4946 const char *startBuf = SM->getCharacterData(DeclLoc);
4947 const char *startArgList = strchr(startBuf, '(');
4949 assert((*startArgList == '(') && "Rewriter fuzzy parser confused");
4952 // advance the location to startArgList.
4953 DeclLoc = DeclLoc.getLocWithOffset(startArgList-startBuf);
4954 assert((DeclLoc.isValid()) && "Invalid DeclLoc");
4956 const char *argPtr = startArgList;
4958 while (*argPtr++ && parenCount) {
4961 // Replace the '^' with '*'.
4962 DeclLoc = DeclLoc.getLocWithOffset(argPtr-startArgList);
4963 ReplaceText(DeclLoc, 1, "*");
4976 bool RewriteObjC::PointerTypeTakesAnyBlockArguments(QualType QT) {
4977 const FunctionProtoType *FTP;
4978 const PointerType *PT = QT->getAs<PointerType>();
4980 FTP = PT->getPointeeType()->getAs<FunctionProtoType>();
4982 const BlockPointerType *BPT = QT->getAs<BlockPointerType>();
4983 assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type");
4984 FTP = BPT->getPointeeType()->getAs<FunctionProtoType>();
4987 for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(),
4988 E = FTP->arg_type_end(); I != E; ++I)
4989 if (isTopLevelBlockPointerType(*I))
4995 bool RewriteObjC::PointerTypeTakesAnyObjCQualifiedType(QualType QT) {
4996 const FunctionProtoType *FTP;
4997 const PointerType *PT = QT->getAs<PointerType>();
4999 FTP = PT->getPointeeType()->getAs<FunctionProtoType>();
5001 const BlockPointerType *BPT = QT->getAs<BlockPointerType>();
5002 assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type");
5003 FTP = BPT->getPointeeType()->getAs<FunctionProtoType>();
5006 for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(),
5007 E = FTP->arg_type_end(); I != E; ++I) {
5008 if ((*I)->isObjCQualifiedIdType())
5010 if ((*I)->isObjCObjectPointerType() &&
5011 (*I)->getPointeeType()->isObjCQualifiedInterfaceType())
5019 void RewriteObjC::GetExtentOfArgList(const char *Name, const char *&LParen,
5020 const char *&RParen) {
5021 const char *argPtr = strchr(Name, '(');
5022 assert((*argPtr == '(') && "Rewriter fuzzy parser confused");
5024 LParen = argPtr; // output the start.
5025 argPtr++; // skip past the left paren.
5026 unsigned parenCount = 1;
5028 while (*argPtr && parenCount) {
5030 case '(': parenCount++; break;
5031 case ')': parenCount--; break;
5034 if (parenCount) argPtr++;
5036 assert((*argPtr == ')') && "Rewriter fuzzy parser confused");
5037 RParen = argPtr; // output the end
5040 void RewriteObjC::RewriteBlockPointerDecl(NamedDecl *ND) {
5041 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
5042 RewriteBlockPointerFunctionArgs(FD);
5045 // Handle Variables and Typedefs.
5046 SourceLocation DeclLoc = ND->getLocation();
5048 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
5049 DeclT = VD->getType();
5050 else if (TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(ND))
5051 DeclT = TDD->getUnderlyingType();
5052 else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND))
5053 DeclT = FD->getType();
5055 llvm_unreachable("RewriteBlockPointerDecl(): Decl type not yet handled");
5057 const char *startBuf = SM->getCharacterData(DeclLoc);
5058 const char *endBuf = startBuf;
5059 // scan backward (from the decl location) for the end of the previous decl.
5060 while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart)
5062 SourceLocation Start = DeclLoc.getLocWithOffset(startBuf-endBuf);
5064 unsigned OrigLength=0;
5065 // *startBuf != '^' if we are dealing with a pointer to function that
5066 // may take block argument types (which will be handled below).
5067 if (*startBuf == '^') {
5068 // Replace the '^' with '*', computing a negative offset.
5073 while (*startBuf != ')') {
5081 if (PointerTypeTakesAnyBlockArguments(DeclT) ||
5082 PointerTypeTakesAnyObjCQualifiedType(DeclT)) {
5083 // Replace the '^' with '*' for arguments.
5084 // Replace id<P> with id/*<>*/
5085 DeclLoc = ND->getLocation();
5086 startBuf = SM->getCharacterData(DeclLoc);
5087 const char *argListBegin, *argListEnd;
5088 GetExtentOfArgList(startBuf, argListBegin, argListEnd);
5089 while (argListBegin < argListEnd) {
5090 if (*argListBegin == '^')
5092 else if (*argListBegin == '<') {
5094 buf += *argListBegin++;
5096 while (*argListBegin != '>') {
5097 buf += *argListBegin++;
5100 buf += *argListBegin;
5104 buf += *argListBegin;
5111 ReplaceText(Start, OrigLength, buf);
5117 /// SynthesizeByrefCopyDestroyHelper - This routine synthesizes:
5118 /// void __Block_byref_id_object_copy(struct Block_byref_id_object *dst,
5119 /// struct Block_byref_id_object *src) {
5120 /// _Block_object_assign (&_dest->object, _src->object,
5121 /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT
5122 /// [|BLOCK_FIELD_IS_WEAK]) // object
5123 /// _Block_object_assign(&_dest->object, _src->object,
5124 /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK
5125 /// [|BLOCK_FIELD_IS_WEAK]) // block
5128 /// void __Block_byref_id_object_dispose(struct Block_byref_id_object *_src) {
5129 /// _Block_object_dispose(_src->object,
5130 /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT
5131 /// [|BLOCK_FIELD_IS_WEAK]) // object
5132 /// _Block_object_dispose(_src->object,
5133 /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK
5134 /// [|BLOCK_FIELD_IS_WEAK]) // block
5137 std::string RewriteObjC::SynthesizeByrefCopyDestroyHelper(VarDecl *VD,
5140 if (CopyDestroyCache.count(flag))
5142 CopyDestroyCache.insert(flag);
5143 S = "static void __Block_byref_id_object_copy_";
5145 S += "(void *dst, void *src) {\n";
5147 // offset into the object pointer is computed as:
5148 // void * + void* + int + int + void* + void *
5150 static_cast<unsigned>(Context->getTypeSize(Context->IntTy));
5151 unsigned VoidPtrSize =
5152 static_cast<unsigned>(Context->getTypeSize(Context->VoidPtrTy));
5154 unsigned offset = (VoidPtrSize*4 + IntSize + IntSize)/Context->getCharWidth();
5155 S += " _Block_object_assign((char*)dst + ";
5156 S += utostr(offset);
5157 S += ", *(void * *) ((char*)src + ";
5158 S += utostr(offset);
5163 S += "static void __Block_byref_id_object_dispose_";
5165 S += "(void *src) {\n";
5166 S += " _Block_object_dispose(*(void * *) ((char*)src + ";
5167 S += utostr(offset);
5174 /// RewriteByRefVar - For each __block typex ND variable this routine transforms
5175 /// the declaration into:
5176 /// struct __Block_byref_ND {
5177 /// void *__isa; // NULL for everything except __weak pointers
5178 /// struct __Block_byref_ND *__forwarding;
5179 /// int32_t __flags;
5181 /// void *__Block_byref_id_object_copy; // If variable is __block ObjC object
5182 /// void *__Block_byref_id_object_dispose; // If variable is __block ObjC object
5186 /// It then replaces declaration of ND variable with:
5187 /// struct __Block_byref_ND ND = {__isa=0B, __forwarding=&ND, __flags=some_flag,
5188 /// __size=sizeof(struct __Block_byref_ND),
5189 /// ND=initializer-if-any};
5192 void RewriteObjC::RewriteByRefVar(VarDecl *ND) {
5193 // Insert declaration for the function in which block literal is
5195 if (CurFunctionDeclToDeclareForBlock)
5196 RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock);
5199 SourceLocation DeclLoc = ND->getTypeSpecStartLoc();
5200 if (DeclLoc.isInvalid())
5201 // If type location is missing, it is because of missing type (a warning).
5202 // Use variable's location which is good for this case.
5203 DeclLoc = ND->getLocation();
5204 const char *startBuf = SM->getCharacterData(DeclLoc);
5205 SourceLocation X = ND->getLocEnd();
5206 X = SM->getExpansionLoc(X);
5207 const char *endBuf = SM->getCharacterData(X);
5208 std::string Name(ND->getNameAsString());
5209 std::string ByrefType;
5210 RewriteByRefString(ByrefType, Name, ND, true);
5211 ByrefType += " {\n";
5212 ByrefType += " void *__isa;\n";
5213 RewriteByRefString(ByrefType, Name, ND);
5214 ByrefType += " *__forwarding;\n";
5215 ByrefType += " int __flags;\n";
5216 ByrefType += " int __size;\n";
5217 // Add void *__Block_byref_id_object_copy;
5218 // void *__Block_byref_id_object_dispose; if needed.
5219 QualType Ty = ND->getType();
5220 bool HasCopyAndDispose = Context->BlockRequiresCopying(Ty);
5221 if (HasCopyAndDispose) {
5222 ByrefType += " void (*__Block_byref_id_object_copy)(void*, void*);\n";
5223 ByrefType += " void (*__Block_byref_id_object_dispose)(void*);\n";
5227 (void)convertBlockPointerToFunctionPointer(T);
5228 T.getAsStringInternal(Name, Context->getPrintingPolicy());
5230 ByrefType += " " + Name + ";\n";
5231 ByrefType += "};\n";
5232 // Insert this type in global scope. It is needed by helper function.
5233 SourceLocation FunLocStart;
5235 FunLocStart = CurFunctionDef->getTypeSpecStartLoc();
5237 assert(CurMethodDef && "RewriteByRefVar - CurMethodDef is null");
5238 FunLocStart = CurMethodDef->getLocStart();
5240 InsertText(FunLocStart, ByrefType);
5241 if (Ty.isObjCGCWeak()) {
5242 flag |= BLOCK_FIELD_IS_WEAK;
5246 if (HasCopyAndDispose) {
5247 flag = BLOCK_BYREF_CALLER;
5248 QualType Ty = ND->getType();
5249 // FIXME. Handle __weak variable (BLOCK_FIELD_IS_WEAK) as well.
5250 if (Ty->isBlockPointerType())
5251 flag |= BLOCK_FIELD_IS_BLOCK;
5253 flag |= BLOCK_FIELD_IS_OBJECT;
5254 std::string HF = SynthesizeByrefCopyDestroyHelper(ND, flag);
5256 InsertText(FunLocStart, HF);
5259 // struct __Block_byref_ND ND =
5260 // {0, &ND, some_flag, __size=sizeof(struct __Block_byref_ND),
5261 // initializer-if-any};
5262 bool hasInit = (ND->getInit() != 0);
5264 if (HasCopyAndDispose)
5265 flags |= BLOCK_HAS_COPY_DISPOSE;
5266 Name = ND->getNameAsString();
5268 RewriteByRefString(ByrefType, Name, ND);
5269 std::string ForwardingCastType("(");
5270 ForwardingCastType += ByrefType + " *)";
5272 ByrefType += " " + Name + " = {(void*)";
5273 ByrefType += utostr(isa);
5274 ByrefType += "," + ForwardingCastType + "&" + Name + ", ";
5275 ByrefType += utostr(flags);
5277 ByrefType += "sizeof(";
5278 RewriteByRefString(ByrefType, Name, ND);
5280 if (HasCopyAndDispose) {
5281 ByrefType += ", __Block_byref_id_object_copy_";
5282 ByrefType += utostr(flag);
5283 ByrefType += ", __Block_byref_id_object_dispose_";
5284 ByrefType += utostr(flag);
5286 ByrefType += "};\n";
5287 unsigned nameSize = Name.size();
5288 // for block or function pointer declaration. Name is aleady
5289 // part of the declaration.
5290 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType())
5292 ReplaceText(DeclLoc, endBuf-startBuf+nameSize, ByrefType);
5295 SourceLocation startLoc;
5296 Expr *E = ND->getInit();
5297 if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E))
5298 startLoc = ECE->getLParenLoc();
5300 startLoc = E->getLocStart();
5301 startLoc = SM->getExpansionLoc(startLoc);
5302 endBuf = SM->getCharacterData(startLoc);
5303 ByrefType += " " + Name;
5304 ByrefType += " = {(void*)";
5305 ByrefType += utostr(isa);
5306 ByrefType += "," + ForwardingCastType + "&" + Name + ", ";
5307 ByrefType += utostr(flags);
5309 ByrefType += "sizeof(";
5310 RewriteByRefString(ByrefType, Name, ND);
5312 if (HasCopyAndDispose) {
5313 ByrefType += "__Block_byref_id_object_copy_";
5314 ByrefType += utostr(flag);
5315 ByrefType += ", __Block_byref_id_object_dispose_";
5316 ByrefType += utostr(flag);
5319 ReplaceText(DeclLoc, endBuf-startBuf, ByrefType);
5321 // Complete the newly synthesized compound expression by inserting a right
5322 // curly brace before the end of the declaration.
5323 // FIXME: This approach avoids rewriting the initializer expression. It
5324 // also assumes there is only one declarator. For example, the following
5325 // isn't currently supported by this routine (in general):
5327 // double __block BYREFVAR = 1.34, BYREFVAR2 = 1.37;
5329 const char *startInitializerBuf = SM->getCharacterData(startLoc);
5330 const char *semiBuf = strchr(startInitializerBuf, ';');
5331 assert((*semiBuf == ';') && "RewriteByRefVar: can't find ';'");
5332 SourceLocation semiLoc =
5333 startLoc.getLocWithOffset(semiBuf-startInitializerBuf);
5335 InsertText(semiLoc, "}");
5340 void RewriteObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) {
5341 // Add initializers for any closure decl refs.
5342 GetBlockDeclRefExprs(Exp->getBody());
5343 if (BlockDeclRefs.size()) {
5344 // Unique all "by copy" declarations.
5345 for (unsigned i = 0; i < BlockDeclRefs.size(); i++)
5346 if (!BlockDeclRefs[i]->isByRef()) {
5347 if (!BlockByCopyDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) {
5348 BlockByCopyDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl());
5349 BlockByCopyDecls.push_back(BlockDeclRefs[i]->getDecl());
5352 // Unique all "by ref" declarations.
5353 for (unsigned i = 0; i < BlockDeclRefs.size(); i++)
5354 if (BlockDeclRefs[i]->isByRef()) {
5355 if (!BlockByRefDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) {
5356 BlockByRefDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl());
5357 BlockByRefDecls.push_back(BlockDeclRefs[i]->getDecl());
5360 // Find any imported blocks...they will need special attention.
5361 for (unsigned i = 0; i < BlockDeclRefs.size(); i++)
5362 if (BlockDeclRefs[i]->isByRef() ||
5363 BlockDeclRefs[i]->getType()->isObjCObjectPointerType() ||
5364 BlockDeclRefs[i]->getType()->isBlockPointerType())
5365 ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl());
5369 FunctionDecl *RewriteObjC::SynthBlockInitFunctionDecl(StringRef name) {
5370 IdentifierInfo *ID = &Context->Idents.get(name);
5371 QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy);
5372 return FunctionDecl::Create(*Context, TUDecl, SourceLocation(),
5373 SourceLocation(), ID, FType, 0, SC_Extern,
5374 SC_None, false, false);
5377 Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp,
5378 const SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs) {
5379 const BlockDecl *block = Exp->getBlockDecl();
5380 Blocks.push_back(Exp);
5382 CollectBlockDeclRefInfo(Exp);
5384 // Add inner imported variables now used in current block.
5385 int countOfInnerDecls = 0;
5386 if (!InnerBlockDeclRefs.empty()) {
5387 for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) {
5388 BlockDeclRefExpr *Exp = InnerBlockDeclRefs[i];
5389 ValueDecl *VD = Exp->getDecl();
5390 if (!Exp->isByRef() && !BlockByCopyDeclsPtrSet.count(VD)) {
5391 // We need to save the copied-in variables in nested
5392 // blocks because it is needed at the end for some of the API generations.
5393 // See SynthesizeBlockLiterals routine.
5394 InnerDeclRefs.push_back(Exp); countOfInnerDecls++;
5395 BlockDeclRefs.push_back(Exp);
5396 BlockByCopyDeclsPtrSet.insert(VD);
5397 BlockByCopyDecls.push_back(VD);
5399 if (Exp->isByRef() && !BlockByRefDeclsPtrSet.count(VD)) {
5400 InnerDeclRefs.push_back(Exp); countOfInnerDecls++;
5401 BlockDeclRefs.push_back(Exp);
5402 BlockByRefDeclsPtrSet.insert(VD);
5403 BlockByRefDecls.push_back(VD);
5406 // Find any imported blocks...they will need special attention.
5407 for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++)
5408 if (InnerBlockDeclRefs[i]->isByRef() ||
5409 InnerBlockDeclRefs[i]->getType()->isObjCObjectPointerType() ||
5410 InnerBlockDeclRefs[i]->getType()->isBlockPointerType())
5411 ImportedBlockDecls.insert(InnerBlockDeclRefs[i]->getDecl());
5413 InnerDeclRefsCount.push_back(countOfInnerDecls);
5415 std::string FuncName;
5418 FuncName = CurFunctionDef->getNameAsString();
5419 else if (CurMethodDef)
5420 BuildUniqueMethodName(FuncName, CurMethodDef);
5421 else if (GlobalVarDecl)
5422 FuncName = std::string(GlobalVarDecl->getNameAsString());
5424 std::string BlockNumber = utostr(Blocks.size()-1);
5426 std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber;
5427 std::string Func = "__" + FuncName + "_block_func_" + BlockNumber;
5429 // Get a pointer to the function type so we can cast appropriately.
5430 QualType BFT = convertFunctionTypeOfBlocks(Exp->getFunctionType());
5431 QualType FType = Context->getPointerType(BFT);
5436 // Simulate a contructor call...
5437 FD = SynthBlockInitFunctionDecl(Tag);
5438 DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, FType, VK_RValue,
5441 SmallVector<Expr*, 4> InitExprs;
5443 // Initialize the block function.
5444 FD = SynthBlockInitFunctionDecl(Func);
5445 DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, FD->getType(), VK_LValue,
5447 CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy,
5449 InitExprs.push_back(castExpr);
5451 // Initialize the block descriptor.
5452 std::string DescData = "__" + FuncName + "_block_desc_" + BlockNumber + "_DATA";
5454 VarDecl *NewVD = VarDecl::Create(*Context, TUDecl,
5455 SourceLocation(), SourceLocation(),
5456 &Context->Idents.get(DescData.c_str()),
5457 Context->VoidPtrTy, 0,
5458 SC_Static, SC_None);
5459 UnaryOperator *DescRefExpr =
5460 new (Context) UnaryOperator(new (Context) DeclRefExpr(NewVD,
5465 Context->getPointerType(Context->VoidPtrTy),
5466 VK_RValue, OK_Ordinary,
5468 InitExprs.push_back(DescRefExpr);
5470 // Add initializers for any closure decl refs.
5471 if (BlockDeclRefs.size()) {
5473 // Output all "by copy" declarations.
5474 for (SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(),
5475 E = BlockByCopyDecls.end(); I != E; ++I) {
5476 if (isObjCType((*I)->getType())) {
5477 // FIXME: Conform to ABI ([[obj retain] autorelease]).
5478 FD = SynthBlockInitFunctionDecl((*I)->getName());
5479 Exp = new (Context) DeclRefExpr(FD, FD->getType(), VK_LValue,
5481 if (HasLocalVariableExternalStorage(*I)) {
5482 QualType QT = (*I)->getType();
5483 QT = Context->getPointerType(QT);
5484 Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue,
5485 OK_Ordinary, SourceLocation());
5487 } else if (isTopLevelBlockPointerType((*I)->getType())) {
5488 FD = SynthBlockInitFunctionDecl((*I)->getName());
5489 Arg = new (Context) DeclRefExpr(FD, FD->getType(), VK_LValue,
5491 Exp = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy,
5494 FD = SynthBlockInitFunctionDecl((*I)->getName());
5495 Exp = new (Context) DeclRefExpr(FD, FD->getType(), VK_LValue,
5497 if (HasLocalVariableExternalStorage(*I)) {
5498 QualType QT = (*I)->getType();
5499 QT = Context->getPointerType(QT);
5500 Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue,
5501 OK_Ordinary, SourceLocation());
5505 InitExprs.push_back(Exp);
5507 // Output all "by ref" declarations.
5508 for (SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(),
5509 E = BlockByRefDecls.end(); I != E; ++I) {
5510 ValueDecl *ND = (*I);
5511 std::string Name(ND->getNameAsString());
5512 std::string RecName;
5513 RewriteByRefString(RecName, Name, ND, true);
5514 IdentifierInfo *II = &Context->Idents.get(RecName.c_str()
5515 + sizeof("struct"));
5516 RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl,
5517 SourceLocation(), SourceLocation(),
5519 assert(RD && "SynthBlockInitExpr(): Can't find RecordDecl");
5520 QualType castT = Context->getPointerType(Context->getTagDeclType(RD));
5522 FD = SynthBlockInitFunctionDecl((*I)->getName());
5523 Exp = new (Context) DeclRefExpr(FD, FD->getType(), VK_LValue,
5525 bool isNestedCapturedVar = false;
5527 for (BlockDecl::capture_const_iterator ci = block->capture_begin(),
5528 ce = block->capture_end(); ci != ce; ++ci) {
5529 const VarDecl *variable = ci->getVariable();
5530 if (variable == ND && ci->isNested()) {
5531 assert (ci->isByRef() &&
5532 "SynthBlockInitExpr - captured block variable is not byref");
5533 isNestedCapturedVar = true;
5537 // captured nested byref variable has its address passed. Do not take
5538 // its address again.
5539 if (!isNestedCapturedVar)
5540 Exp = new (Context) UnaryOperator(Exp, UO_AddrOf,
5541 Context->getPointerType(Exp->getType()),
5542 VK_RValue, OK_Ordinary, SourceLocation());
5543 Exp = NoTypeInfoCStyleCastExpr(Context, castT, CK_BitCast, Exp);
5544 InitExprs.push_back(Exp);
5547 if (ImportedBlockDecls.size()) {
5548 // generate BLOCK_HAS_COPY_DISPOSE(have helper funcs) | BLOCK_HAS_DESCRIPTOR
5549 int flag = (BLOCK_HAS_COPY_DISPOSE | BLOCK_HAS_DESCRIPTOR);
5551 static_cast<unsigned>(Context->getTypeSize(Context->IntTy));
5552 Expr *FlagExp = IntegerLiteral::Create(*Context, llvm::APInt(IntSize, flag),
5553 Context->IntTy, SourceLocation());
5554 InitExprs.push_back(FlagExp);
5556 NewRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], InitExprs.size(),
5557 FType, VK_LValue, SourceLocation());
5558 NewRep = new (Context) UnaryOperator(NewRep, UO_AddrOf,
5559 Context->getPointerType(NewRep->getType()),
5560 VK_RValue, OK_Ordinary, SourceLocation());
5561 NewRep = NoTypeInfoCStyleCastExpr(Context, FType, CK_BitCast,
5563 BlockDeclRefs.clear();
5564 BlockByRefDecls.clear();
5565 BlockByRefDeclsPtrSet.clear();
5566 BlockByCopyDecls.clear();
5567 BlockByCopyDeclsPtrSet.clear();
5568 ImportedBlockDecls.clear();
5572 bool RewriteObjC::IsDeclStmtInForeachHeader(DeclStmt *DS) {
5573 if (const ObjCForCollectionStmt * CS =
5574 dyn_cast<ObjCForCollectionStmt>(Stmts.back()))
5575 return CS->getElement() == DS;
5579 //===----------------------------------------------------------------------===//
5580 // Function Body / Expression rewriting
5581 //===----------------------------------------------------------------------===//
5583 // This is run as a first "pass" prior to RewriteFunctionBodyOrGlobalInitializer().
5584 // The allows the main rewrite loop to associate all ObjCPropertyRefExprs with
5585 // their respective BinaryOperator. Without this knowledge, we'd need to rewrite
5586 // the ObjCPropertyRefExpr twice (once as a getter, and later as a setter).
5587 // Since the rewriter isn't capable of rewriting rewritten code, it's important
5588 // we get this right.
5589 void RewriteObjC::CollectPropertySetters(Stmt *S) {
5590 // Perform a bottom up traversal of all children.
5591 for (Stmt::child_range CI = S->children(); CI; ++CI)
5593 CollectPropertySetters(*CI);
5595 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) {
5596 if (BinOp->isAssignmentOp()) {
5597 if (isa<ObjCPropertyRefExpr>(BinOp->getLHS()))
5598 PropSetters[BinOp->getLHS()] = BinOp;
5603 Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) {
5604 if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) ||
5605 isa<DoStmt>(S) || isa<ForStmt>(S))
5607 else if (isa<ObjCForCollectionStmt>(S)) {
5609 ObjCBcLabelNo.push_back(++BcLabelCount);
5612 SourceRange OrigStmtRange = S->getSourceRange();
5614 // Perform a bottom up rewrite of all children.
5615 for (Stmt::child_range CI = S->children(); CI; ++CI)
5618 Stmt *ChildStmt = (*CI);
5619 if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(ChildStmt)) {
5620 Expr *OldBase = IvarRefExpr->getBase();
5621 bool replaced = false;
5622 newStmt = RewriteObjCNestedIvarRefExpr(ChildStmt, replaced);
5624 if (ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(newStmt))
5625 ReplaceStmt(OldBase, IRE->getBase());
5627 ReplaceStmt(ChildStmt, newStmt);
5631 newStmt = RewriteFunctionBodyOrGlobalInitializer(ChildStmt);
5633 if (Expr *PropOrImplicitRefExpr = dyn_cast<Expr>(ChildStmt))
5634 if (PropSetters[PropOrImplicitRefExpr] == S) {
5641 // If dealing with an assignment with LHS being a property reference
5642 // expression, the entire assignment tree is rewritten into a property
5643 // setter messaging. This involvs the RHS too. Do not attempt to rewrite
5645 if (Expr *Exp = dyn_cast<Expr>(ChildStmt))
5646 if (isa<ObjCPropertyRefExpr>(Exp)) {
5647 if (PropSetters[Exp]) {
5654 if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) {
5655 SmallVector<BlockDeclRefExpr *, 8> InnerBlockDeclRefs;
5656 llvm::SmallPtrSet<const DeclContext *, 8> InnerContexts;
5657 InnerContexts.insert(BE->getBlockDecl());
5658 ImportedLocalExternalDecls.clear();
5659 GetInnerBlockDeclRefExprs(BE->getBody(),
5660 InnerBlockDeclRefs, InnerContexts);
5661 // Rewrite the block body in place.
5662 Stmt *SaveCurrentBody = CurrentBody;
5663 CurrentBody = BE->getBody();
5664 CollectPropertySetters(CurrentBody);
5666 // block literal on rhs of a property-dot-sytax assignment
5667 // must be replaced by its synthesize ast so getRewrittenText
5668 // works as expected. In this case, what actually ends up on RHS
5669 // is the blockTranscribed which is the helper function for the
5670 // block literal; as in: self.c = ^() {[ace ARR];};
5671 bool saveDisableReplaceStmt = DisableReplaceStmt;
5672 DisableReplaceStmt = false;
5673 RewriteFunctionBodyOrGlobalInitializer(BE->getBody());
5674 DisableReplaceStmt = saveDisableReplaceStmt;
5675 CurrentBody = SaveCurrentBody;
5677 ImportedLocalExternalDecls.clear();
5678 // Now we snarf the rewritten text and stash it away for later use.
5679 std::string Str = Rewrite.getRewrittenText(BE->getSourceRange());
5680 RewrittenBlockExprs[BE] = Str;
5682 Stmt *blockTranscribed = SynthBlockInitExpr(BE, InnerBlockDeclRefs);
5684 //blockTranscribed->dump();
5685 ReplaceStmt(S, blockTranscribed);
5686 return blockTranscribed;
5688 // Handle specific things.
5689 if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S))
5690 return RewriteAtEncode(AtEncode);
5692 if (isa<ObjCPropertyRefExpr>(S)) {
5693 Expr *PropOrImplicitRefExpr = dyn_cast<Expr>(S);
5694 assert(PropOrImplicitRefExpr && "Property or implicit setter/getter is null");
5696 BinaryOperator *BinOp = PropSetters[PropOrImplicitRefExpr];
5698 // Because the rewriter doesn't allow us to rewrite rewritten code,
5699 // we need to rewrite the right hand side prior to rewriting the setter.
5700 DisableReplaceStmt = true;
5701 // Save the source range. Even if we disable the replacement, the
5702 // rewritten node will have been inserted into the tree. If the synthesized
5703 // node is at the 'end', the rewriter will fail. Consider this:
5704 // self.errorHandler = handler ? handler :
5705 // ^(NSURL *errorURL, NSError *error) { return (BOOL)1; };
5706 SourceRange SrcRange = BinOp->getSourceRange();
5707 Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(BinOp->getRHS());
5708 // Need to rewrite the ivar access expression if need be.
5709 if (isa<ObjCIvarRefExpr>(newStmt)) {
5710 bool replaced = false;
5711 newStmt = RewriteObjCNestedIvarRefExpr(newStmt, replaced);
5714 DisableReplaceStmt = false;
5716 // Unlike the main iterator, we explicily avoid changing 'BinOp'. If
5717 // we changed the RHS of BinOp, the rewriter would fail (since it needs
5718 // to see the original expression). Consider this example:
5720 // Foo *obj1, *obj2;
5722 // obj1.i = [obj2 rrrr];
5724 // 'BinOp' for the previous expression looks like:
5726 // (BinaryOperator 0x231ccf0 'int' '='
5727 // (ObjCPropertyRefExpr 0x231cc70 'int' Kind=PropertyRef Property="i"
5728 // (DeclRefExpr 0x231cc50 'Foo *' Var='obj1' 0x231cbb0))
5729 // (ObjCMessageExpr 0x231ccb0 'int' selector=rrrr
5730 // (DeclRefExpr 0x231cc90 'Foo *' Var='obj2' 0x231cbe0)))
5732 // 'newStmt' represents the rewritten message expression. For example:
5734 // (CallExpr 0x231d300 'id':'struct objc_object *'
5735 // (ParenExpr 0x231d2e0 'int (*)(id, SEL)'
5736 // (CStyleCastExpr 0x231d2c0 'int (*)(id, SEL)'
5737 // (CStyleCastExpr 0x231d220 'void *'
5738 // (DeclRefExpr 0x231d200 'id (id, SEL, ...)' FunctionDecl='objc_msgSend' 0x231cdc0))))
5740 // Note that 'newStmt' is passed to RewritePropertyOrImplicitSetter so that it
5741 // can be used as the setter argument. ReplaceStmt() will still 'see'
5742 // the original RHS (since we haven't altered BinOp).
5744 // This implies the Rewrite* routines can no longer delete the original
5745 // node. As a result, we now leak the original AST nodes.
5747 return RewritePropertyOrImplicitSetter(BinOp, dyn_cast<Expr>(newStmt), SrcRange);
5749 return RewritePropertyOrImplicitGetter(PropOrImplicitRefExpr);
5753 if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S))
5754 return RewriteAtSelector(AtSelector);
5756 if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S))
5757 return RewriteObjCStringLiteral(AtString);
5759 if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) {
5761 // Before we rewrite it, put the original message expression in a comment.
5762 SourceLocation startLoc = MessExpr->getLocStart();
5763 SourceLocation endLoc = MessExpr->getLocEnd();
5765 const char *startBuf = SM->getCharacterData(startLoc);
5766 const char *endBuf = SM->getCharacterData(endLoc);
5768 std::string messString;
5769 messString += "// ";
5770 messString.append(startBuf, endBuf-startBuf+1);
5773 // FIXME: Missing definition of
5774 // InsertText(clang::SourceLocation, char const*, unsigned int).
5775 // InsertText(startLoc, messString.c_str(), messString.size());
5776 // Tried this, but it didn't work either...
5777 // ReplaceText(startLoc, 0, messString.c_str(), messString.size());
5779 return RewriteMessageExpr(MessExpr);
5782 if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S))
5783 return RewriteObjCTryStmt(StmtTry);
5785 if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S))
5786 return RewriteObjCSynchronizedStmt(StmtTry);
5788 if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S))
5789 return RewriteObjCThrowStmt(StmtThrow);
5791 if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S))
5792 return RewriteObjCProtocolExpr(ProtocolExp);
5794 if (ObjCForCollectionStmt *StmtForCollection =
5795 dyn_cast<ObjCForCollectionStmt>(S))
5796 return RewriteObjCForCollectionStmt(StmtForCollection,
5797 OrigStmtRange.getEnd());
5798 if (BreakStmt *StmtBreakStmt =
5799 dyn_cast<BreakStmt>(S))
5800 return RewriteBreakStmt(StmtBreakStmt);
5801 if (ContinueStmt *StmtContinueStmt =
5802 dyn_cast<ContinueStmt>(S))
5803 return RewriteContinueStmt(StmtContinueStmt);
5805 // Need to check for protocol refs (id <P>, Foo <P> *) in variable decls
5807 if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
5808 // FIXME: What we're doing here is modifying the type-specifier that
5809 // precedes the first Decl. In the future the DeclGroup should have
5810 // a separate type-specifier that we can rewrite.
5811 // NOTE: We need to avoid rewriting the DeclStmt if it is within
5812 // the context of an ObjCForCollectionStmt. For example:
5813 // NSArray *someArray;
5814 // for (id <FooProtocol> index in someArray) ;
5815 // This is because RewriteObjCForCollectionStmt() does textual rewriting
5816 // and it depends on the original text locations/positions.
5817 if (Stmts.empty() || !IsDeclStmtInForeachHeader(DS))
5818 RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin());
5820 // Blocks rewrite rules.
5821 for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end();
5824 if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) {
5825 if (isTopLevelBlockPointerType(ND->getType()))
5826 RewriteBlockPointerDecl(ND);
5827 else if (ND->getType()->isFunctionPointerType())
5828 CheckFunctionPointerDecl(ND->getType(), ND);
5829 if (VarDecl *VD = dyn_cast<VarDecl>(SD)) {
5830 if (VD->hasAttr<BlocksAttr>()) {
5831 static unsigned uniqueByrefDeclCount = 0;
5832 assert(!BlockByRefDeclNo.count(ND) &&
5833 "RewriteFunctionBodyOrGlobalInitializer: Duplicate byref decl");
5834 BlockByRefDeclNo[ND] = uniqueByrefDeclCount++;
5835 RewriteByRefVar(VD);
5838 RewriteTypeOfDecl(VD);
5841 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) {
5842 if (isTopLevelBlockPointerType(TD->getUnderlyingType()))
5843 RewriteBlockPointerDecl(TD);
5844 else if (TD->getUnderlyingType()->isFunctionPointerType())
5845 CheckFunctionPointerDecl(TD->getUnderlyingType(), TD);
5850 if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S))
5851 RewriteObjCQualifiedInterfaceTypes(CE);
5853 if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) ||
5854 isa<DoStmt>(S) || isa<ForStmt>(S)) {
5855 assert(!Stmts.empty() && "Statement stack is empty");
5856 assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) ||
5857 isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back()))
5858 && "Statement stack mismatch");
5861 // Handle blocks rewriting.
5862 if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) {
5863 if (BDRE->isByRef())
5864 return RewriteBlockDeclRefExpr(BDRE);
5866 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) {
5867 ValueDecl *VD = DRE->getDecl();
5868 if (VD->hasAttr<BlocksAttr>())
5869 return RewriteBlockDeclRefExpr(DRE);
5870 if (HasLocalVariableExternalStorage(VD))
5871 return RewriteLocalVariableExternalStorage(DRE);
5874 if (CallExpr *CE = dyn_cast<CallExpr>(S)) {
5875 if (CE->getCallee()->getType()->isBlockPointerType()) {
5876 Stmt *BlockCall = SynthesizeBlockCall(CE, CE->getCallee());
5877 ReplaceStmt(S, BlockCall);
5881 if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) {
5882 RewriteCastExpr(CE);
5885 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) {
5886 CastExpr *Replacement = new (Context) CastExpr(ICE->getType(),
5889 // Get the new text.
5891 llvm::raw_string_ostream Buf(SStr);
5892 Replacement->printPretty(Buf, *Context);
5893 const std::string &Str = Buf.str();
5895 printf("CAST = %s\n", &Str[0]);
5896 InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size());
5901 // Return this stmt unmodified.
5905 void RewriteObjC::RewriteRecordBody(RecordDecl *RD) {
5906 for (RecordDecl::field_iterator i = RD->field_begin(),
5907 e = RD->field_end(); i != e; ++i) {
5909 if (isTopLevelBlockPointerType(FD->getType()))
5910 RewriteBlockPointerDecl(FD);
5911 if (FD->getType()->isObjCQualifiedIdType() ||
5912 FD->getType()->isObjCQualifiedInterfaceType())
5913 RewriteObjCQualifiedInterfaceTypes(FD);
5917 /// HandleDeclInMainFile - This is called for each top-level decl defined in the
5918 /// main file of the input.
5919 void RewriteObjC::HandleDeclInMainFile(Decl *D) {
5920 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
5921 if (FD->isOverloadedOperator())
5924 // Since function prototypes don't have ParmDecl's, we check the function
5925 // prototype. This enables us to rewrite function declarations and
5926 // definitions using the same code.
5927 RewriteBlocksInFunctionProtoType(FD->getType(), FD);
5929 // FIXME: If this should support Obj-C++, support CXXTryStmt
5930 if (CompoundStmt *Body = dyn_cast_or_null<CompoundStmt>(FD->getBody())) {
5931 CurFunctionDef = FD;
5932 CurFunctionDeclToDeclareForBlock = FD;
5933 CollectPropertySetters(Body);
5936 cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body));
5939 if (PropParentMap) {
5940 delete PropParentMap;
5943 // This synthesizes and inserts the block "impl" struct, invoke function,
5944 // and any copy/dispose helper functions.
5945 InsertBlockLiteralsWithinFunction(FD);
5947 CurFunctionDeclToDeclareForBlock = 0;
5951 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
5952 if (CompoundStmt *Body = MD->getCompoundBody()) {
5954 CollectPropertySetters(Body);
5957 cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body));
5960 if (PropParentMap) {
5961 delete PropParentMap;
5964 InsertBlockLiteralsWithinMethod(MD);
5968 if (ObjCImplementationDecl *CI = dyn_cast<ObjCImplementationDecl>(D))
5969 ClassImplementation.push_back(CI);
5970 else if (ObjCCategoryImplDecl *CI = dyn_cast<ObjCCategoryImplDecl>(D))
5971 CategoryImplementation.push_back(CI);
5972 else if (isa<ObjCClassDecl>(D))
5973 llvm_unreachable("RewriteObjC::HandleDeclInMainFile - ObjCClassDecl");
5974 else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
5975 RewriteObjCQualifiedInterfaceTypes(VD);
5976 if (isTopLevelBlockPointerType(VD->getType()))
5977 RewriteBlockPointerDecl(VD);
5978 else if (VD->getType()->isFunctionPointerType()) {
5979 CheckFunctionPointerDecl(VD->getType(), VD);
5980 if (VD->getInit()) {
5981 if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) {
5982 RewriteCastExpr(CE);
5985 } else if (VD->getType()->isRecordType()) {
5986 RecordDecl *RD = VD->getType()->getAs<RecordType>()->getDecl();
5987 if (RD->isCompleteDefinition())
5988 RewriteRecordBody(RD);
5990 if (VD->getInit()) {
5992 CollectPropertySetters(VD->getInit());
5993 CurrentBody = VD->getInit();
5994 RewriteFunctionBodyOrGlobalInitializer(VD->getInit());
5996 if (PropParentMap) {
5997 delete PropParentMap;
6000 SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(),
6004 // This is needed for blocks.
6005 if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) {
6006 RewriteCastExpr(CE);
6011 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
6012 if (isTopLevelBlockPointerType(TD->getUnderlyingType()))
6013 RewriteBlockPointerDecl(TD);
6014 else if (TD->getUnderlyingType()->isFunctionPointerType())
6015 CheckFunctionPointerDecl(TD->getUnderlyingType(), TD);
6018 if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) {
6019 if (RD->isCompleteDefinition())
6020 RewriteRecordBody(RD);
6026 void RewriteObjC::HandleTranslationUnit(ASTContext &C) {
6027 if (Diags.hasErrorOccurred())
6032 // Here's a great place to add any extra declarations that may be needed.
6033 // Write out meta data for each @protocol(<expr>).
6034 for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(),
6035 E = ProtocolExprDecls.end(); I != E; ++I)
6036 RewriteObjCProtocolMetaData(*I, "", "", Preamble);
6038 InsertText(SM->getLocForStartOfFile(MainFileID), Preamble, false);
6039 if (ClassImplementation.size() || CategoryImplementation.size())
6040 RewriteImplementations();
6042 // Get the buffer corresponding to MainFileID. If we haven't changed it, then
6044 if (const RewriteBuffer *RewriteBuf =
6045 Rewrite.getRewriteBufferFor(MainFileID)) {
6046 //printf("Changed:\n");
6047 *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end());
6049 llvm::errs() << "No changes\n";
6052 if (ClassImplementation.size() || CategoryImplementation.size() ||
6053 ProtocolExprDecls.size()) {
6054 // Rewrite Objective-c meta data*
6055 std::string ResultStr;
6056 SynthesizeMetaDataIntoBuffer(ResultStr);
6058 *OutFile << ResultStr;