rmaz created this revision.
rmaz added reviewers: dexonsmith, manmanren, vsapsai.
rmaz requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This refactor changes the GlobalMethodPool to a class that contains
the DenseMap of methods. This is to allow for the addition of a
separate DenseSet in a follow-up diff that will handle method
de-duplication when inserting methods into the global method pool.

Changes:

- the GlobalMethods pair becomes a GlobalMethodLists struct for better clarity
- the GlobalMethodPool becomes a class containing the DenseMap of methods
- pass through methods are added to maintain most of the existing code without 
changing MethodPool -> MethodPool.Methods everywhere


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D109898

Files:
  clang/include/clang/Sema/Sema.h
  clang/lib/Sema/SemaCodeComplete.cpp
  clang/lib/Sema/SemaDeclObjC.cpp
  clang/lib/Sema/SemaExprObjC.cpp
  clang/lib/Serialization/ASTReader.cpp
  clang/lib/Serialization/ASTWriter.cpp

Index: clang/lib/Serialization/ASTWriter.cpp
===================================================================
--- clang/lib/Serialization/ASTWriter.cpp
+++ clang/lib/Serialization/ASTWriter.cpp
@@ -3122,8 +3122,8 @@
         ObjCMethodList()
       };
       if (F != SemaRef.MethodPool.end()) {
-        Data.Instance = F->second.first;
-        Data.Factory = F->second.second;
+        Data.Instance = F->second.InstanceMethods;
+        Data.Factory = F->second.ClassMethods;
       }
       // Only write this selector if it's not in an existing AST or something
       // changed.
Index: clang/lib/Serialization/ASTReader.cpp
===================================================================
--- clang/lib/Serialization/ASTReader.cpp
+++ clang/lib/Serialization/ASTReader.cpp
@@ -4006,8 +4006,9 @@
     return;
 
   // Retrieve the appropriate method list.
-  ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
-                                                    : Known->second.second;
+  ObjCMethodList &Start = Method->isInstanceMethod()
+                              ? Known->second.InstanceMethods
+                              : Known->second.ClassMethods;
   bool Found = false;
   for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
     if (!Found) {
@@ -8208,19 +8209,22 @@
     return;
 
   Sema &S = *getSema();
-  Sema::GlobalMethodPool::iterator Pos
-    = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
+  Sema::GlobalMethodPool::iterator Pos =
+      S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethodLists())).first;
 
-  Pos->second.first.setBits(Visitor.getInstanceBits());
-  Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
-  Pos->second.second.setBits(Visitor.getFactoryBits());
-  Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
+  Pos->second.InstanceMethods.setBits(Visitor.getInstanceBits());
+  Pos->second.InstanceMethods.setHasMoreThanOneDecl(
+      Visitor.instanceHasMoreThanOneDecl());
+  Pos->second.ClassMethods.setBits(Visitor.getFactoryBits());
+  Pos->second.ClassMethods.setHasMoreThanOneDecl(
+      Visitor.factoryHasMoreThanOneDecl());
 
   // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
   // when building a module we keep every method individually and may need to
   // update hasMoreThanOneDecl as we add the methods.
-  addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
-  addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
+  addMethodsToPool(S, Visitor.getInstanceMethods(),
+                   Pos->second.InstanceMethods);
+  addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.ClassMethods);
 }
 
 void ASTReader::updateOutOfDateSelector(Selector Sel) {
Index: clang/lib/Sema/SemaExprObjC.cpp
===================================================================
--- clang/lib/Sema/SemaExprObjC.cpp
+++ clang/lib/Sema/SemaExprObjC.cpp
@@ -1215,13 +1215,13 @@
   for (Sema::GlobalMethodPool::iterator b = S.MethodPool.begin(),
        e = S.MethodPool.end(); b != e; b++) {
     // first, instance methods
-    ObjCMethodList &InstMethList = b->second.first;
+    ObjCMethodList &InstMethList = b->second.InstanceMethods;
     if (HelperToDiagnoseMismatchedMethodsInGlobalPool(S, AtLoc, LParenLoc, RParenLoc,
                                                       Method, InstMethList))
       Warned = true;
 
     // second, class methods
-    ObjCMethodList &ClsMethList = b->second.second;
+    ObjCMethodList &ClsMethList = b->second.ClassMethods;
     if (HelperToDiagnoseMismatchedMethodsInGlobalPool(S, AtLoc, LParenLoc, RParenLoc,
                                                       Method, ClsMethList) || Warned)
       return;
@@ -1262,9 +1262,9 @@
     return nullptr;
 
   ObjCMethodDecl *DirectInstance = LookupDirectMethodInMethodList(
-      S, Sel, Iter->second.first, onlyDirect, anyDirect);
+      S, Sel, Iter->second.InstanceMethods, onlyDirect, anyDirect);
   ObjCMethodDecl *DirectClass = LookupDirectMethodInMethodList(
-      S, Sel, Iter->second.second, onlyDirect, anyDirect);
+      S, Sel, Iter->second.ClassMethods, onlyDirect, anyDirect);
 
   return DirectInstance ? DirectInstance : DirectClass;
 }
Index: clang/lib/Sema/SemaDeclObjC.cpp
===================================================================
--- clang/lib/Sema/SemaDeclObjC.cpp
+++ clang/lib/Sema/SemaDeclObjC.cpp
@@ -3427,12 +3427,15 @@
 
   GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
   if (Pos == MethodPool.end())
-    Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
-                                           GlobalMethods())).first;
+    Pos =
+        MethodPool
+            .insert(std::make_pair(Method->getSelector(), GlobalMethodLists()))
+            .first;
 
   Method->setDefined(impl);
 
-  ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
+  ObjCMethodList &Entry =
+      instance ? Pos->second.InstanceMethods : Pos->second.ClassMethods;
   addMethodToGlobalList(&Entry, Method);
 }
 
@@ -3505,8 +3508,8 @@
     return false;
 
   // Gather the non-hidden methods.
-  ObjCMethodList &MethList = InstanceFirst ? Pos->second.first :
-                             Pos->second.second;
+  ObjCMethodList &MethList =
+      InstanceFirst ? Pos->second.InstanceMethods : Pos->second.ClassMethods;
   for (ObjCMethodList *M = &MethList; M; M = M->getNext())
     if (M->getMethod() && M->getMethod()->isUnconditionallyVisible()) {
       if (FilterMethodsByTypeBound(M->getMethod(), TypeBound))
@@ -3521,8 +3524,8 @@
     return false;
 
   // Gather the other kind.
-  ObjCMethodList &MethList2 = InstanceFirst ? Pos->second.second :
-                              Pos->second.first;
+  ObjCMethodList &MethList2 =
+      InstanceFirst ? Pos->second.ClassMethods : Pos->second.InstanceMethods;
   for (ObjCMethodList *M = &MethList2; M; M = M->getNext())
     if (M->getMethod() && M->getMethod()->isUnconditionallyVisible()) {
       if (FilterMethodsByTypeBound(M->getMethod(), TypeBound))
@@ -3552,8 +3555,9 @@
   // caller.
   if (Pos == MethodPool.end())
     return true;
-  ObjCMethodList &MethList =
-    BestMethod->isInstanceMethod() ? Pos->second.first : Pos->second.second;
+  ObjCMethodList &MethList = BestMethod->isInstanceMethod()
+                                 ? Pos->second.InstanceMethods
+                                 : Pos->second.ClassMethods;
   return MethList.hasMoreThanOneDecl();
 }
 
@@ -3568,7 +3572,8 @@
     return nullptr;
 
   // Gather the non-hidden methods.
-  ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
+  ObjCMethodList &MethList =
+      instance ? Pos->second.InstanceMethods : Pos->second.ClassMethods;
   SmallVector<ObjCMethodDecl *, 4> Methods;
   for (ObjCMethodList *M = &MethList; M; M = M->getNext()) {
     if (M->getMethod() && M->getMethod()->isUnconditionallyVisible())
@@ -3636,15 +3641,15 @@
   if (Pos == MethodPool.end())
     return nullptr;
 
-  GlobalMethods &Methods = Pos->second;
-  for (const ObjCMethodList *Method = &Methods.first; Method;
+  GlobalMethodLists &Methods = Pos->second;
+  for (const ObjCMethodList *Method = &Methods.InstanceMethods; Method;
        Method = Method->getNext())
     if (Method->getMethod() &&
         (Method->getMethod()->isDefined() ||
          Method->getMethod()->isPropertyAccessor()))
       return Method->getMethod();
 
-  for (const ObjCMethodList *Method = &Methods.second; Method;
+  for (const ObjCMethodList *Method = &Methods.ClassMethods; Method;
        Method = Method->getNext())
     if (Method->getMethod() &&
         (Method->getMethod()->isDefined() ||
@@ -3711,7 +3716,7 @@
   for (GlobalMethodPool::iterator b = MethodPool.begin(),
        e = MethodPool.end(); b != e; b++) {
     // instance methods
-    for (ObjCMethodList *M = &b->second.first; M; M=M->getNext())
+    for (ObjCMethodList *M = &b->second.InstanceMethods; M; M = M->getNext())
       if (M->getMethod() &&
           (M->getMethod()->getSelector().getNumArgs() == NumArgs) &&
           (M->getMethod()->getSelector() != Sel)) {
@@ -3723,7 +3728,7 @@
           Methods.push_back(M->getMethod());
       }
     // class methods
-    for (ObjCMethodList *M = &b->second.second; M; M=M->getNext())
+    for (ObjCMethodList *M = &b->second.ClassMethods; M; M = M->getNext())
       if (M->getMethod() &&
           (M->getMethod()->getSelector().getNumArgs() == NumArgs) &&
           (M->getMethod()->getSelector() != Sel)) {
@@ -4281,8 +4286,9 @@
       if (it == S.MethodPool.end())
         return;
     }
-    const ObjCMethodList &list =
-      method->isInstanceMethod() ? it->second.first : it->second.second;
+    const ObjCMethodList &list = method->isInstanceMethod()
+                                     ? it->second.InstanceMethods
+                                     : it->second.ClassMethods;
     if (!list.getMethod()) return;
 
     const ObjCContainerDecl *container
@@ -4450,8 +4456,9 @@
         GlobalMethodPool::iterator It =
             MethodPool.find(ObjCMethod->getSelector());
         if (It != MethodPool.end()) {
-          ObjCMethodList &List =
-            ObjCMethod->isInstanceMethod()? It->second.first: It->second.second;
+          ObjCMethodList &List = ObjCMethod->isInstanceMethod()
+                                     ? It->second.InstanceMethods
+                                     : It->second.ClassMethods;
           unsigned CategCount = List.getBits();
           if (CategCount > 0) {
             // If the method is in a category we'll do lookup if there were at
Index: clang/lib/Sema/SemaCodeComplete.cpp
===================================================================
--- clang/lib/Sema/SemaCodeComplete.cpp
+++ clang/lib/Sema/SemaCodeComplete.cpp
@@ -7693,7 +7693,7 @@
     for (Sema::GlobalMethodPool::iterator M = SemaRef.MethodPool.begin(),
                                           MEnd = SemaRef.MethodPool.end();
          M != MEnd; ++M) {
-      for (ObjCMethodList *MethList = &M->second.second;
+      for (ObjCMethodList *MethList = &M->second.ClassMethods;
            MethList && MethList->getMethod(); MethList = MethList->getNext()) {
         if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents))
           continue;
@@ -7865,7 +7865,7 @@
     for (GlobalMethodPool::iterator M = MethodPool.begin(),
                                     MEnd = MethodPool.end();
          M != MEnd; ++M) {
-      for (ObjCMethodList *MethList = &M->second.first;
+      for (ObjCMethodList *MethList = &M->second.InstanceMethods;
            MethList && MethList->getMethod(); MethList = MethList->getNext()) {
         if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents))
           continue;
@@ -9266,8 +9266,9 @@
   for (GlobalMethodPool::iterator M = MethodPool.begin(),
                                   MEnd = MethodPool.end();
        M != MEnd; ++M) {
-    for (ObjCMethodList *MethList = IsInstanceMethod ? &M->second.first
-                                                     : &M->second.second;
+    for (ObjCMethodList *MethList = IsInstanceMethod
+                                        ? &M->second.InstanceMethods
+                                        : &M->second.ClassMethods;
          MethList && MethList->getMethod(); MethList = MethList->getNext()) {
       if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents))
         continue;
Index: clang/include/clang/Sema/Sema.h
===================================================================
--- clang/include/clang/Sema/Sema.h
+++ clang/include/clang/Sema/Sema.h
@@ -1419,15 +1419,31 @@
   const llvm::MapVector<FieldDecl *, DeleteLocs> &
   getMismatchingDeleteExpressions() const;
 
-  typedef std::pair<ObjCMethodList, ObjCMethodList> GlobalMethods;
-  typedef llvm::DenseMap<Selector, GlobalMethods> GlobalMethodPool;
-
   /// Method Pool - 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").
   /// At the head of the list it is recorded whether there were 0, 1, or >= 2
   /// methods inside categories with a particular selector.
+  struct GlobalMethodLists {
+    ObjCMethodList InstanceMethods;
+    ObjCMethodList ClassMethods;
+  };
+  class GlobalMethodPool {
+    llvm::DenseMap<Selector, GlobalMethodLists> Methods;
+
+  public:
+    typedef llvm::DenseMap<Selector, GlobalMethodLists>::iterator iterator;
+    iterator begin() { return Methods.begin(); }
+    iterator end() { return Methods.end(); }
+    iterator find(Selector Sel) { return Methods.find(Sel); }
+    std::pair<iterator, bool>
+    insert(std::pair<Selector, GlobalMethodLists> val) {
+      return Methods.insert(val);
+    }
+    int count(Selector Sel) { return Methods.count(Sel); }
+    bool empty() { return Methods.empty(); }
+  };
   GlobalMethodPool MethodPool;
 
   /// Method selectors used in a \@selector expression. Used for implementation
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D10989... Richard Howell via Phabricator via cfe-commits
    • [PATCH] D... Duncan P. N. Exon Smith via Phabricator via cfe-commits
    • [PATCH] D... Richard Howell via Phabricator via cfe-commits
    • [PATCH] D... Daniel Rodríguez Troitiño via Phabricator via cfe-commits

Reply via email to