]> granicus.if.org Git - clang/commitdiff
- Add rewrite rule for @class.
authorSteve Naroff <snaroff@apple.com>
Tue, 23 Oct 2007 20:20:08 +0000 (20:20 +0000)
committerSteve Naroff <snaroff@apple.com>
Tue, 23 Oct 2007 20:20:08 +0000 (20:20 +0000)
- Add setter/getter to ObjcClassDecl.
- Predefined key runtime functions.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@43257 91177308-0d34-0410-b5e6-96231b3b80d8

Driver/RewriteTest.cpp
Lex/Preprocessor.cpp
clang.xcodeproj/project.pbxproj
include/clang/AST/DeclObjC.h

index cf812a284f891cb7aea418e3c3015e588ca2d8a8..7cf1342591a6fb22abe3ccf0f10bffa2021b1b97 100644 (file)
@@ -44,7 +44,8 @@ namespace {
     
     void RewriteFunctionBody(Stmt *S);
     void RewriteAtEncode(ObjCEncodeExpr *Exp);
-
+    void RewriteForwardClassDecl(ObjcClassDecl *Dcl);
+    
     void WriteObjcClassMetaData(ObjcImplementationDecl *IDecl);
     void WriteObjcMetaData();
     
@@ -105,6 +106,8 @@ void RewriteTest::HandleDeclInMainFile(Decl *D) {
     ClassImplementation.push_back(CI);
   else if (ObjcCategoryImplDecl *CI = dyn_cast<ObjcCategoryImplDecl>(D))
     CategoryImplementation.push_back(CI);
+  else if (ObjcClassDecl *CD = dyn_cast<ObjcClassDecl>(D))
+    RewriteForwardClassDecl(CD);
   // Nothing yet.
 }
 
@@ -130,6 +133,37 @@ void RewriteTest::RewriteAtEncode(ObjCEncodeExpr *Exp) {
   delete Replacement;
 }
 
+void RewriteTest::RewriteForwardClassDecl(ObjcClassDecl *ClassDecl) {
+  int numDecls = ClassDecl->getNumForwardDecls();
+  ObjcInterfaceDecl **ForwardDecls = ClassDecl->getForwardDecls();
+  
+  // Get the start location and compute the semi location.
+  SourceLocation startLoc = ClassDecl->getLocation();
+  const char *startBuf = SM->getCharacterData(startLoc);
+  const char *semiPtr = startBuf;
+  while (semiPtr && (*semiPtr != ';')) semiPtr++;
+
+  // Translate to typedef's that forward reference structs with the same name
+  // as the class. As a convenience, we include the original declaration
+  // as a comment.
+  std::string typedefString;
+  typedefString += "// ";
+  typedefString.append(startBuf, semiPtr-startBuf+1);
+  typedefString += "\n";
+  for (int i = 0; i < numDecls; i++) {
+    ObjcInterfaceDecl *ForwardDecl = ForwardDecls[i];
+    typedefString += "typedef struct ";
+    typedefString += ForwardDecl->getName();
+    typedefString += " ";
+    typedefString += ForwardDecl->getName();
+    typedefString += ";\n";
+  }
+  
+  // Replace the @class with typedefs corresponding to the classes.
+  Rewrite.ReplaceText(startLoc, semiPtr-startBuf+1, 
+                      typedefString.c_str(), typedefString.size());
+}
+
 void RewriteTest::WriteObjcClassMetaData(ObjcImplementationDecl *IDecl) {
   ObjcInterfaceDecl *CDecl = IDecl->getClassInterface();
   
index c1f14a9b9bc12b37b185c5d918fbf162d4cad8a6..63805443765f38d11b106e6ecf9bf37d967d56d0 100644 (file)
@@ -392,6 +392,23 @@ static void InitializePredefinedMacros(Preprocessor &PP,
     DefineBuiltinMacro(Buf, "NO=(BOOL)0");
     DefineBuiltinMacro(Buf, "Nil=0");
     DefineBuiltinMacro(Buf, "nil=0");
+    ObjcType = "OBJC_EXPORT const char *sel_getName(SEL sel);\n";
+    Buf.insert(Buf.end(), ObjcType, ObjcType+strlen(ObjcType));
+    ObjcType = "OBJC_EXPORT SEL sel_getUid(const char *str);\n";
+    Buf.insert(Buf.end(), ObjcType, ObjcType+strlen(ObjcType));
+    
+    // Predefine ObjC primitive functions, traditionally declared in
+    // <objc/objc-runtime.h>. Unlike the declarations above, we don't protect
+    // these with a header guard (since multiple identical function declarations
+    // don't result in an error.
+    ObjcType = "OBJC_EXPORT id objc_getClass(const char *name);\n";
+    Buf.insert(Buf.end(), ObjcType, ObjcType+strlen(ObjcType));
+    ObjcType = "OBJC_EXPORT id objc_getMetaClass(const char *name);\n";
+    Buf.insert(Buf.end(), ObjcType, ObjcType+strlen(ObjcType));
+    ObjcType = "OBJC_EXPORT id objc_msgSend(id self, SEL op, ...);\n";
+    Buf.insert(Buf.end(), ObjcType, ObjcType+strlen(ObjcType));
+    ObjcType = "OBJC_EXPORT id objc_msgSendSuper(struct objc_super *super, SEL op, ...);\n";
+    Buf.insert(Buf.end(), ObjcType, ObjcType+strlen(ObjcType));
   }
 
   // Add __builtin_va_list typedef.
index b086e54ce30adcddc328c186a208d99e0dba4bd0..267202c41d83d5aef1f391344dfc144c6be055aa 100644 (file)
                84AF36A00CB17A3B00C820A5 /* DeclObjC.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = DeclObjC.h; path = clang/AST/DeclObjC.h; sourceTree = "<group>"; };
                84D9A8870C1A57E100AC7ABC /* AttributeList.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = AttributeList.cpp; path = Parse/AttributeList.cpp; sourceTree = "<group>"; };
                84D9A88B0C1A581300AC7ABC /* AttributeList.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = AttributeList.h; path = clang/Parse/AttributeList.h; sourceTree = "<group>"; };
-               8DD76F6C0486A84900D96B5E /* clang */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = clang; sourceTree = BUILT_PRODUCTS_DIR; };
+               8DD76F6C0486A84900D96B5E /* clang */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = "compiled.mach-o.executable"; path = clang; sourceTree = BUILT_PRODUCTS_DIR; };
                DE01DA480B12ADA300AC22CE /* PPCallbacks.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = PPCallbacks.h; sourceTree = "<group>"; };
                DE06756B0C051CFE00EBBFD8 /* ParseExprCXX.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = ParseExprCXX.cpp; path = Parse/ParseExprCXX.cpp; sourceTree = "<group>"; };
                DE06B73D0A8307640050E87E /* LangOptions.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = LangOptions.h; sourceTree = "<group>"; };
index b5d211f30847cfec10d8a2e0fdb556d8bbf6351d..40a2d9643ce9313ab42bdd2e7142dad1782e2b53 100644 (file)
@@ -355,6 +355,9 @@ public:
     assert(idx < NumForwardDecls && "index out of range");
     ForwardDecls[idx] = OID;
   }
+  ObjcInterfaceDecl** getForwardDecls() const { return ForwardDecls; }
+  int getNumForwardDecls() const { return NumForwardDecls; }
+  
   static bool classof(const Decl *D) { return D->getKind() == ObjcClass; }
   static bool classof(const ObjcClassDecl *D) { return true; }
 };