[cfe-commits] r42543 - in /cfe/trunk: AST/Decl.cpp AST/StmtPrinter.cpp Sema/Sema.h Sema/SemaDecl.cpp Sema/SemaExpr.cpp clang.xcodeproj/project.pbxproj include/clang/AST/DeclObjC.h include/clang/AST/Expr.h

Steve Naroff snaroff at apple.com
Tue Oct 2 13:01:57 PDT 2007


Author: snaroff
Date: Tue Oct  2 15:01:56 2007
New Revision: 42543

URL: http://llvm.org/viewvc/llvm-project?rev=42543&view=rev
Log:

- Add ObjcInterfaceDecl::lookupInstanceMethod(), lookupClassMethod().
- Add ObjcMessageExpr::getSelector(), getClassName().
- Change Sema::getObjCInterfaceDecl() to simply take an IdentifierInfo (no Scope needed).
- Remove FIXME for printing ObjCMessageExpr's.


Modified:
    cfe/trunk/AST/Decl.cpp
    cfe/trunk/AST/StmtPrinter.cpp
    cfe/trunk/Sema/Sema.h
    cfe/trunk/Sema/SemaDecl.cpp
    cfe/trunk/Sema/SemaExpr.cpp
    cfe/trunk/clang.xcodeproj/project.pbxproj
    cfe/trunk/include/clang/AST/DeclObjC.h
    cfe/trunk/include/clang/AST/Expr.h

Modified: cfe/trunk/AST/Decl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/AST/Decl.cpp?rev=42543&r1=42542&r2=42543&view=diff

==============================================================================
--- cfe/trunk/AST/Decl.cpp (original)
+++ cfe/trunk/AST/Decl.cpp Tue Oct  2 15:01:56 2007
@@ -408,4 +408,36 @@
   }
 }
 
+// FIXME: look through categories...
+ObjcMethodDecl *ObjcInterfaceDecl::lookupInstanceMethod(Selector &Sel) {
+  ObjcInterfaceDecl* ClassDecl = this;
+  while (ClassDecl != NULL) {
+    ObjcMethodDecl **methods = ClassDecl->getInsMethods();
+    int methodCount = ClassDecl->getNumInsMethods();
+    for (int i = 0; i < methodCount; ++i) {
+      if (methods[i]->getSelector() == Sel) {
+        return methods[i];
+      }
+    }
+    ClassDecl = ClassDecl->getSuperClass();
+  }
+  return NULL;
+}
+
+// FIXME: look through categories...
+ObjcMethodDecl *ObjcInterfaceDecl::lookupClassMethod(Selector &Sel) {
+  ObjcInterfaceDecl* ClassDecl = this;
+  while (ClassDecl != NULL) {
+    ObjcMethodDecl **methods = ClassDecl->getClsMethods();
+    int methodCount = ClassDecl->getNumClsMethods();
+    for (int i = 0; i < methodCount; ++i) {
+      if (methods[i]->getSelector() == Sel) {
+        return methods[i];
+      }
+    }
+    ClassDecl = ClassDecl->getSuperClass();
+  }
+  return NULL;
+}
+
 

Modified: cfe/trunk/AST/StmtPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/AST/StmtPrinter.cpp?rev=42543&r1=42542&r2=42543&view=diff

==============================================================================
--- cfe/trunk/AST/StmtPrinter.cpp (original)
+++ cfe/trunk/AST/StmtPrinter.cpp Tue Oct  2 15:01:56 2007
@@ -614,10 +614,17 @@
 
 void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
   OS << "[";
-  PrintExpr(Mess->getReceiver());
-  for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
-    // FIXME: get/print keyword...
-    PrintExpr(Mess->getArg(i));
+  Expr *receiver = Mess->getReceiver();
+  if (receiver) PrintExpr(receiver);
+  else OS << Mess->getClassName()->getName();
+  Selector &selector = Mess->getSelector();
+  if (selector.isUnarySelector()) {
+    OS << " " << selector.getIdentifierInfoForSlot(0)->getName();
+  } else {
+    for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
+      OS << " " << selector.getIdentifierInfoForSlot(i)->getName() << ":";
+      PrintExpr(Mess->getArg(i));
+    }
   }
   OS << "]";
 }

Modified: cfe/trunk/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/Sema.h?rev=42543&r1=42542&r2=42543&view=diff

==============================================================================
--- cfe/trunk/Sema/Sema.h (original)
+++ cfe/trunk/Sema/Sema.h Tue Oct  2 15:01:56 2007
@@ -187,8 +187,7 @@
                                     Scope *FnBodyScope);
   ScopedDecl *LookupScopedDecl(IdentifierInfo *II, unsigned NSI, 
                                SourceLocation IdLoc, Scope *S);  
-  ObjcInterfaceDecl *getObjCInterfaceDecl(Scope *S, 
-		       IdentifierInfo *Id, SourceLocation IdLoc);
+  ObjcInterfaceDecl *getObjCInterfaceDecl(IdentifierInfo *Id);
   ObjcProtocolDecl *getObjCProtocolDecl(Scope *S, 
 		       IdentifierInfo *Id, SourceLocation IdLoc);
   ScopedDecl *LazilyCreateBuiltin(IdentifierInfo *II, unsigned ID, Scope *S);
@@ -436,7 +435,7 @@
   // is obtained from Sel.getNumArgs().
   virtual ExprResult ActOnClassMessage(
     IdentifierInfo *receivingClassName, Selector Sel,
-    SourceLocation lbrac, SourceLocation rbrac, ExprTy **ArgExprs);
+     SourceLocation lbrac, SourceLocation rbrac, ExprTy **ArgExprs);
 
   // ActOnInstanceMessage - used for both unary and keyword messages.
   // ArgExprs is optional - if it is present, the number of expressions

Modified: cfe/trunk/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/SemaDecl.cpp?rev=42543&r1=42542&r2=42543&view=diff

==============================================================================
--- cfe/trunk/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/Sema/SemaDecl.cpp Tue Oct  2 15:01:56 2007
@@ -80,11 +80,18 @@
 
 /// getObjcInterfaceDecl - Look up a for a class declaration in the scope.
 /// return 0 if one not found.
-ObjcInterfaceDecl *Sema::getObjCInterfaceDecl(Scope *S,
-                       			      IdentifierInfo *Id, 
-				              SourceLocation IdLoc) {
-  ScopedDecl *IdDecl = LookupScopedDecl(Id, Decl::IDNS_Ordinary, 
-				        IdLoc, S);
+ObjcInterfaceDecl *Sema::getObjCInterfaceDecl(IdentifierInfo *Id) {
+
+  // Scan up the scope chain looking for a decl that matches this identifier
+  // that is in the appropriate namespace.  This search should not take long, as
+  // shadowing of names is uncommon, and deep shadowing is extremely uncommon.
+  ScopedDecl *IdDecl = NULL;
+  for (ScopedDecl *D = Id->getFETokenInfo<ScopedDecl>(); D; D = D->getNext()) {
+    if (D->getIdentifierNamespace() == Decl::IDNS_Ordinary) {
+      IdDecl = D;
+      break;
+    }
+  }
   if (IdDecl && !isa<ObjcInterfaceDecl>(IdDecl))
     IdDecl = 0;
   return cast_or_null<ObjcInterfaceDecl>(static_cast<Decl*>(IdDecl));
@@ -905,7 +912,7 @@
     Diag(PrevDecl->getLocation(), diag::err_previous_definition);
   }
   
-  ObjcInterfaceDecl* IDecl = getObjCInterfaceDecl(S, ClassName, ClassLoc);
+  ObjcInterfaceDecl* IDecl = getObjCInterfaceDecl(ClassName);
   if (IDecl) {
     // Class already seen. Is it a forward declaration?
     if (!IDecl->getIsForwardDecl())
@@ -936,7 +943,7 @@
     }
     else {
       // Check that super class is previously defined
-      SuperClassEntry = getObjCInterfaceDecl(S, SuperName, SuperLoc); 
+      SuperClassEntry = getObjCInterfaceDecl(SuperName); 
                               
       if (!SuperClassEntry || SuperClassEntry->getIsForwardDecl()) {
         Diag(AtInterfaceLoc, diag::err_undef_superclass, SuperName->getName(),
@@ -1030,7 +1037,7 @@
                       IdentifierInfo *CategoryName, SourceLocation CategoryLoc,
                       IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs) {
   ObjcCategoryDecl *CDecl;
-  ObjcInterfaceDecl* IDecl = getObjCInterfaceDecl(S, ClassName, ClassLoc);
+  ObjcInterfaceDecl* IDecl = getObjCInterfaceDecl(ClassName);
   CDecl = new ObjcCategoryDecl(AtInterfaceLoc, NumProtoRefs);
   CDecl->setClassInterface(IDecl);
 
@@ -1075,7 +1082,7 @@
                       SourceLocation AtCatImplLoc,
                       IdentifierInfo *ClassName, SourceLocation ClassLoc,
                       IdentifierInfo *CatName, SourceLocation CatLoc) {
-  ObjcInterfaceDecl *IDecl = getObjCInterfaceDecl(S, ClassName, ClassLoc);
+  ObjcInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
   ObjcCategoryImplDecl *CDecl = new ObjcCategoryImplDecl(AtCatImplLoc, 
                                                          ClassName, IDecl,
                                                          CatName);
@@ -1103,7 +1110,7 @@
   }
   else {
     // Is there an interface declaration of this class; if not, warn!
-    IDecl = getObjCInterfaceDecl(S, ClassName, ClassLoc); 
+    IDecl = getObjCInterfaceDecl(ClassName); 
     if (!IDecl)
       Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName());
   }
@@ -1121,7 +1128,7 @@
       Diag(PrevDecl->getLocation(), diag::err_previous_definition);
     }
     else {
-      SDecl = getObjCInterfaceDecl(S, SuperClassname, SuperClassLoc); 
+      SDecl = getObjCInterfaceDecl(SuperClassname); 
       if (!SDecl)
         Diag(SuperClassLoc, diag::err_undef_superclass, 
              SuperClassname->getName(), ClassName->getName());
@@ -1330,7 +1337,7 @@
 
   for (unsigned i = 0; i != NumElts; ++i) {
     ObjcInterfaceDecl *IDecl;
-    IDecl = getObjCInterfaceDecl(S, IdentList[i], AtClassLoc); 
+    IDecl = getObjCInterfaceDecl(IdentList[i]); 
     if (!IDecl)  {// Already seen?
       IDecl = new ObjcInterfaceDecl(SourceLocation(), 0, IdentList[i], true);
       // Chain & install the interface decl into the identifier.
@@ -1655,8 +1662,7 @@
 	cast<ObjcImplementationDecl>(static_cast<Decl*>(RecDecl));
       assert(IMPDecl && "ActOnFields - missing ObjcImplementationDecl");
       IMPDecl->ObjcAddInstanceVariablesToClassImpl(ClsFields, RecFields.size());
-      ObjcInterfaceDecl* IDecl = getObjCInterfaceDecl(S, 
-				   IMPDecl->getIdentifier(), RecLoc);
+      ObjcInterfaceDecl* IDecl = getObjCInterfaceDecl(IMPDecl->getIdentifier());
       if (IDecl)
         ActOnImpleIvarVsClassIvars(static_cast<DeclTy*>(IDecl), 
           reinterpret_cast<DeclTy**>(&RecFields[0]), RecFields.size());
@@ -1705,8 +1711,7 @@
                                                static_cast<Decl*>(ClassDecl));
     ImplClass->ObjcAddImplMethods(&insMethods[0], insMethods.size(),
                                  &clsMethods[0], clsMethods.size());
-    ObjcInterfaceDecl* IDecl = getObjCInterfaceDecl(S, 
-				 ImplClass->getIdentifier(), SourceLocation());
+    ObjcInterfaceDecl* IDecl = getObjCInterfaceDecl(ImplClass->getIdentifier());
     if (IDecl)
       ImplMethodsVsClassMethods(ImplClass, IDecl);
   }

Modified: cfe/trunk/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/SemaExpr.cpp?rev=42543&r1=42542&r2=42543&view=diff

==============================================================================
--- cfe/trunk/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/Sema/SemaExpr.cpp Tue Oct  2 15:01:56 2007
@@ -14,6 +14,7 @@
 #include "Sema.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/Decl.h"
+#include "clang/AST/DeclObjC.h"
 #include "clang/AST/Expr.h"
 #include "clang/Parse/DeclSpec.h" 
 #include "clang/Lex/Preprocessor.h"
@@ -1875,14 +1876,18 @@
 // ArgExprs is optional - if it is present, the number of expressions
 // is obtained from Sel.getNumArgs().
 Sema::ExprResult Sema::ActOnClassMessage(
-  IdentifierInfo *receivingClassName, Selector Sel,
+  IdentifierInfo *receiverName, Selector Sel,
   SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args)
 {
-  assert(receivingClassName && "missing receiver class name");
+  assert(receiverName && "missing receiver class name");
 
+  ObjcInterfaceDecl* ClassDecl = getObjCInterfaceDecl(receiverName);
+  ObjcMethodDecl *Method = ClassDecl->lookupClassMethod(Sel);
+  assert(Method && "missing method declaration");
+  QualType retType = Method->getMethodType();
+  // Expr *RExpr = global reference to the class symbol...
   Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
-  return new ObjCMessageExpr(receivingClassName, Sel, 
-                             Context.IntTy/*FIXME*/, lbrac, rbrac, ArgExprs);
+  return new ObjCMessageExpr(receiverName, Sel, retType, lbrac, rbrac, ArgExprs);
 }
 
 // ActOnInstanceMessage - used for both unary and keyword messages.
@@ -1895,7 +1900,19 @@
   assert(receiver && "missing receiver expression");
   
   Expr *RExpr = static_cast<Expr *>(receiver);
+  // FIXME (snaroff): checking in this code from Patrick. Needs to be revisited.
+  // how do we get the ClassDecl from the receiver expression?
+  QualType receiverType = RExpr->getType();
+  while (receiverType->isPointerType()) {
+    PointerType *pointerType = static_cast<PointerType*>(receiverType.getTypePtr());
+    receiverType = pointerType->getPointeeType();
+  }
+  assert(ObjcInterfaceType::classof(receiverType.getTypePtr()) && "bad receiver type");
+  ObjcInterfaceDecl* ClassDecl = static_cast<ObjcInterfaceType*>(
+                                   receiverType.getTypePtr())->getDecl();
+  ObjcMethodDecl *Method = ClassDecl->lookupInstanceMethod(Sel);
+  assert(Method && "missing method declaration");
+  QualType returnType = Method->getMethodType();
   Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
-  return new ObjCMessageExpr(RExpr, Sel,
-                             Context.IntTy/*FIXME*/, lbrac, rbrac, ArgExprs);
+  return new ObjCMessageExpr(RExpr, Sel, returnType, lbrac, rbrac, ArgExprs);
 }

Modified: cfe/trunk/clang.xcodeproj/project.pbxproj
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/clang.xcodeproj/project.pbxproj?rev=42543&r1=42542&r2=42543&view=diff

==============================================================================
--- cfe/trunk/clang.xcodeproj/project.pbxproj (original)
+++ cfe/trunk/clang.xcodeproj/project.pbxproj Tue Oct  2 15:01:56 2007
@@ -737,7 +737,6 @@
 		08FB7793FE84155DC02AAC07 /* Project object */ = {
 			isa = PBXProject;
 			buildConfigurationList = 1DEB923508733DC60010E9CD /* Build configuration list for PBXProject "clang" */;
-			compatibilityVersion = "Xcode 2.4";
 			hasScannedForEncodings = 1;
 			mainGroup = 08FB7794FE84155DC02AAC07 /* clang */;
 			projectDirPath = "";

Modified: cfe/trunk/include/clang/AST/DeclObjC.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclObjC.h?rev=42543&r1=42542&r2=42543&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/DeclObjC.h (original)
+++ cfe/trunk/include/clang/AST/DeclObjC.h Tue Oct  2 15:01:56 2007
@@ -127,6 +127,8 @@
   void setListCategories(ObjcCategoryDecl *category) { 
          ListCategories = category; 
   }
+  ObjcMethodDecl *lookupInstanceMethod(Selector &Sel);
+  ObjcMethodDecl *lookupClassMethod(Selector &Sel);
   
   static bool classof(const Decl *D) {
     return D->getKind() == ObjcInterface;

Modified: cfe/trunk/include/clang/AST/Expr.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Expr.h?rev=42543&r1=42542&r2=42543&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/trunk/include/clang/AST/Expr.h Tue Oct  2 15:01:56 2007
@@ -1100,6 +1100,12 @@
   const Expr *getReceiver() const { return SubExprs[RECEIVER]; }
   Expr *getReceiver() { return SubExprs[RECEIVER]; }
   
+  const Selector &getSelector() const { return SelName; }
+  Selector &getSelector() { return SelName; }
+  
+  const IdentifierInfo *getClassName() const { return ClassName; }
+  IdentifierInfo *getClassName() { return ClassName; }
+  
   /// getNumArgs - Return the number of actual arguments to this call.
   unsigned getNumArgs() const { return SelName.getNumArgs(); }
 





More information about the cfe-commits mailing list