[cfe-commits] r60414 - /cfe/trunk/Driver/RewriteObjC.cpp

Steve Naroff snaroff at apple.com
Tue Dec 2 07:48:38 PST 2008


Author: snaroff
Date: Tue Dec  2 09:48:25 2008
New Revision: 60414

URL: http://llvm.org/viewvc/llvm-project?rev=60414&view=rev
Log:
More work to rewrite synthesize properties (<rdar://problem/6213955>)

Modified:
    cfe/trunk/Driver/RewriteObjC.cpp

Modified: cfe/trunk/Driver/RewriteObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/RewriteObjC.cpp?rev=60414&r1=60413&r2=60414&view=diff

==============================================================================
--- cfe/trunk/Driver/RewriteObjC.cpp (original)
+++ cfe/trunk/Driver/RewriteObjC.cpp Tue Dec  2 09:48:25 2008
@@ -586,9 +586,59 @@
   }
 }
 
+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) {





More information about the cfe-commits mailing list