From: Steve Naroff Date: Tue, 2 Dec 2008 15:48:25 +0000 (+0000) Subject: More work to rewrite synthesize properties () X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=eb0646c8df1c5c8de62b622d13d0c9f19aa29277;p=clang More work to rewrite synthesize properties () git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@60414 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/Driver/RewriteObjC.cpp b/Driver/RewriteObjC.cpp index 6979736659..7f55d793d7 100644 --- a/Driver/RewriteObjC.cpp +++ b/Driver/RewriteObjC.cpp @@ -586,9 +586,59 @@ void RewriteObjC::RewriteTabs() { } } +static std::string getIvarAccessString(ObjCInterfaceDecl *ClassDecl, + ObjCIvarDecl *OID) { + std::string S; + S = "((struct "; + S += ClassDecl->getIdentifier()->getName(); + S += "_IMPL *)self)->"; + S += OID->getNameAsCString(); + return S; +} + void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID) { SourceLocation startLoc = PID->getLocStart(); InsertText(startLoc, "// ", 3); + const char *startBuf = SM->getCharacterData(startLoc); + assert((*startBuf == '@') && "bogus @synthesize location"); + const char *semiBuf = strchr(startBuf, ';'); + assert((*semiBuf == ';') && "@synthesize: can't find ';'"); + SourceLocation onePastSemiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf+1); + + if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) + return; // FIXME: is this correct? + + // Generate the 'getter' function. + std::string Getr; + ObjCPropertyDecl *PD = PID->getPropertyDecl(); + + RewriteObjCMethodDecl(PD->getGetterMethodDecl(), Getr); + ObjCInterfaceDecl *ClassDecl = PD->getGetterMethodDecl()->getClassInterface(); + + Getr += "{ "; + ObjCIvarDecl *OID = PID->getPropertyIvarDecl(); + if (OID) { + // Synthesize an explicit cast to gain access to the ivar. + Getr += "return " + getIvarAccessString(ClassDecl, OID); + Getr += "; }"; + } + InsertText(onePastSemiLoc, Getr.c_str(), Getr.size()); + + if (PD->isReadOnly()) + return; + + // Generate the 'setter' function. + std::string Setr; + RewriteObjCMethodDecl(PD->getSetterMethodDecl(), Setr); + + Setr += "{ "; + if (OID) { + // Synthesize an explicit cast to initialize the ivar. + Setr += getIvarAccessString(ClassDecl, OID) + " = "; + Setr += OID->getNameAsCString(); + Setr += "; }"; + } + InsertText(onePastSemiLoc, Setr.c_str(), Setr.size()); } void RewriteObjC::RewriteForwardClassDecl(ObjCClassDecl *ClassDecl) {