[cfe-commits] r42957 - in /cfe/trunk: Parse/ParseObjc.cpp Sema/ASTStreamer.cpp Sema/Sema.h Sema/SemaDecl.cpp include/clang/Parse/Action.h

Steve Naroff snaroff at apple.com
Sat Oct 13 17:58:42 PDT 2007


Author: snaroff
Date: Sat Oct 13 19:58:41 2007
New Revision: 42957

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

- Added Sema::AddFactoryMethodToGlobalPool and Sema::AddInstanceMethodToGlobalPool and DenseMaps. This will allow us to efficiently lookup a method from a selector given no type information (for the "id" data type).

- Fixed some funky "}
                    else {" indentation in Sema::ActOnAddMethodsToObjcDecl(). I'd prefer we stay away from this style...it wastes space and isn't any easier to read (from my perspective, at least:-)

- Changed Parser::ParseObjCInterfaceDeclList() to only call Action::ActOnAddMethodsToObjcDecl() when it actually has methods to add (since most interface have methods, this is a very minor cleanup).


Modified:
    cfe/trunk/Parse/ParseObjc.cpp
    cfe/trunk/Sema/ASTStreamer.cpp
    cfe/trunk/Sema/Sema.h
    cfe/trunk/Sema/SemaDecl.cpp
    cfe/trunk/include/clang/Parse/Action.h

Modified: cfe/trunk/Parse/ParseObjc.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Parse/ParseObjc.cpp?rev=42957&r1=42956&r2=42957&view=diff

==============================================================================
--- cfe/trunk/Parse/ParseObjc.cpp (original)
+++ cfe/trunk/Parse/ParseObjc.cpp Sat Oct 13 19:58:41 2007
@@ -266,9 +266,10 @@
       ParseDeclarationOrFunctionDefinition();
     }
   }
-  /// Insert collected methods declarations into the @interface object.
-  Actions.ActOnAddMethodsToObjcDecl(CurScope, interfaceDecl,
-                                    &allMethods[0], allMethods.size());
+  if (allMethods.size())
+    /// Insert collected methods declarations into the @interface object.
+    Actions.ActOnAddMethodsToObjcDecl(CurScope, interfaceDecl,
+                                      &allMethods[0], allMethods.size());
 }
 
 ///   Parse property attribute declarations.

Modified: cfe/trunk/Sema/ASTStreamer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/ASTStreamer.cpp?rev=42957&r1=42956&r2=42957&view=diff

==============================================================================
--- cfe/trunk/Sema/ASTStreamer.cpp (original)
+++ cfe/trunk/Sema/ASTStreamer.cpp Sat Oct 13 19:58:41 2007
@@ -81,6 +81,7 @@
 }
 
 void ASTStreamer::PrintStats() const {
+  P.getActions().PrintStats();
 }
 
 //===----------------------------------------------------------------------===//

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

==============================================================================
--- cfe/trunk/Sema/Sema.h (original)
+++ cfe/trunk/Sema/Sema.h Sat Oct 13 19:58:41 2007
@@ -120,6 +120,27 @@
   
   /// ObjcIdTypedef - built-in typedef for "id".
   TypedefDecl *ObjcIdTypedef;
+
+  /// ObjCMethodList - a linked list of methods with different signatures.
+  struct ObjcMethodList {
+    ObjcMethodDecl *Method;
+    ObjcMethodList *Next;
+    
+    ObjcMethodList() {
+      Method = 0; 
+      Next = 0;
+    }
+    ObjcMethodList(ObjcMethodDecl *M, ObjcMethodList *C) {
+      Method = M;
+      Next = C;
+    }
+  };
+  /// Instance/Factory Method Pools - allows efficient lookup when typechecking
+  /// messages to "id". We need to maintain a list, since selectors can have
+  /// differing signatures across classes. In Cocoa, this happens to be 
+  /// extremely uncommon (only 1% of selectors are "overloaded").
+  llvm::DenseMap<Selector, ObjcMethodList> InstanceMethodPool;
+  llvm::DenseMap<Selector, ObjcMethodList> FactoryMethodPool;
 public:
   Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup);
   
@@ -259,6 +280,14 @@
   /// GetObjcIdType - Getter for the build-in "id" type.
   QualType GetObjcIdType(SourceLocation Loc = SourceLocation());
   
+  /// AddInstanceMethodToGlobalPool - All instance methods in a translation
+  /// unit are added to a global pool. This allows us to efficiently associate
+  /// a selector with a method declaraation for purposes of typechecking
+  /// messages sent to "id" (where the class of the object is unknown).
+  void AddInstanceMethodToGlobalPool(ObjcMethodDecl *Method);
+  
+  /// AddFactoryMethodToGlobalPool - Same as above, but for factory methods.
+  void AddFactoryMethodToGlobalPool(ObjcMethodDecl *Method);
   //===--------------------------------------------------------------------===//
   // Statement Parsing Callbacks: SemaStmt.cpp.
 public:

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

==============================================================================
--- cfe/trunk/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/Sema/SemaDecl.cpp Sat Oct 13 19:58:41 2007
@@ -1754,6 +1754,52 @@
   return true;
 }
 
+void Sema::AddInstanceMethodToGlobalPool(ObjcMethodDecl *Method) {
+  ObjcMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
+  if (!FirstMethod.Method) {
+    // Haven't seen a method with this selector name yet - add it.
+    FirstMethod.Method = Method;
+    FirstMethod.Next = 0;
+  } else {
+    // We've seen a method with this name, now check the type signature(s).
+    bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
+    
+    for (ObjcMethodList *Next = FirstMethod.Next; !match && Next; 
+         Next = Next->Next)
+      match = MatchTwoMethodDeclarations(Method, Next->Method);
+      
+    if (!match) {
+      // We have a new signature for an existing method - add it.
+      // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
+      struct ObjcMethodList *OMI = new ObjcMethodList(Method, FirstMethod.Next);
+      FirstMethod.Next = OMI;
+    }
+  }
+}
+
+void Sema::AddFactoryMethodToGlobalPool(ObjcMethodDecl *Method) {
+  ObjcMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
+  if (!FirstMethod.Method) {
+    // Haven't seen a method with this selector name yet - add it.
+    FirstMethod.Method = Method;
+    FirstMethod.Next = 0;
+  } else {
+    // We've seen a method with this name, now check the type signature(s).
+    bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
+    
+    for (ObjcMethodList *Next = FirstMethod.Next; !match && Next; 
+         Next = Next->Next)
+      match = MatchTwoMethodDeclarations(Method, Next->Method);
+      
+    if (!match) {
+      // We have a new signature for an existing method - add it.
+      // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
+      struct ObjcMethodList *OMI = new ObjcMethodList(Method, FirstMethod.Next);
+      FirstMethod.Next = OMI;
+    }
+  }
+}
+
 void Sema::ActOnAddMethodsToObjcDecl(Scope* S, DeclTy *classDecl,
                                      DeclTy **allMethods, unsigned allNum) {
   Decl *ClassDecl = static_cast<Decl *>(classDecl);
@@ -1777,6 +1823,7 @@
   for (unsigned i = 0; i < allNum; i++ ) {
     ObjcMethodDecl *Method =
       cast_or_null<ObjcMethodDecl>(static_cast<Decl*>(allMethods[i]));
+
     if (!Method) continue;  // Already issued a diagnostic.
     if (Method->isInstance()) {
       if (checkDuplicateMethods) {
@@ -1786,16 +1833,17 @@
           Diag(Method->getLocation(), diag::error_duplicate_method_decl,
                Method->getSelector().getName());
           Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
-        }
-        else {
+        } else {
           insMethods.push_back(Method);
           InsMap[Method->getSelector()] = Method;
         }
       }
       else
         insMethods.push_back(Method);
-    }
-    else {
+        
+      /// The following allows us to typecheck messages to "id".
+      AddInstanceMethodToGlobalPool(Method);
+    } else {
       if (checkDuplicateMethods) {
         /// Check for class method of the same name with incompatible types
         const ObjcMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
@@ -1803,14 +1851,16 @@
           Diag(Method->getLocation(), diag::error_duplicate_method_decl,
                Method->getSelector().getName());
           Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
-        }
-        else {        
+        } else {        
           clsMethods.push_back(Method);
           ClsMap[Method->getSelector()] = Method;
         }
       }
       else
         clsMethods.push_back(Method);
+        
+      /// The following allows us to typecheck messages to "id".
+      AddFactoryMethodToGlobalPool(Method);
     }
   }
   

Modified: cfe/trunk/include/clang/Parse/Action.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/Action.h?rev=42957&r1=42956&r2=42957&view=diff

==============================================================================
--- cfe/trunk/include/clang/Parse/Action.h (original)
+++ cfe/trunk/include/clang/Parse/Action.h Sat Oct 13 19:58:41 2007
@@ -85,6 +85,8 @@
   virtual void DeleteExpr(ExprTy *E) {}
   virtual void DeleteStmt(StmtTy *E) {}
   
+  /// Statistics.
+  virtual void PrintStats() const {}
   //===--------------------------------------------------------------------===//
   // Declaration Tracking Callbacks.
   //===--------------------------------------------------------------------===//





More information about the cfe-commits mailing list