move two more lists of protocols over to use ObjCList<ObjCProtocolDecl>,
simplifying code along the way and fixing a problem and memory leak or two.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@53876 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/DeclObjC.cpp b/lib/AST/DeclObjC.cpp
index 7b28b10..4b79847 100644
--- a/lib/AST/DeclObjC.cpp
+++ b/lib/AST/DeclObjC.cpp
@@ -94,14 +94,12 @@
 
 ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C,
                                            SourceLocation L, 
-                                           unsigned numRefProtos,
                                            IdentifierInfo *Id) {
   void *Mem = C.getAllocator().Allocate<ObjCProtocolDecl>();
-  return new (Mem) ObjCProtocolDecl(L, numRefProtos, Id);
+  return new (Mem) ObjCProtocolDecl(L, Id);
 }
 
 ObjCProtocolDecl::~ObjCProtocolDecl() {
-  delete [] ReferencedProtocols;
   delete [] InstanceMethods;
   delete [] ClassMethods;
   delete [] PropertyDecl;
@@ -440,15 +438,6 @@
   AtEndLoc = endLoc;
 }
 
-void ObjCCategoryDecl::addReferencedProtocols(ObjCProtocolDecl **List,
-                                              unsigned NumRPs) {
-  assert(NumReferencedProtocols == 0 && "Protocol list already set");
-  if (NumRPs == 0) return;
-  
-  ReferencedProtocols = new ObjCProtocolDecl*[NumRPs];
-  memcpy(ReferencedProtocols, List, NumRPs*sizeof(ObjCProtocolDecl*));
-  NumReferencedProtocols = NumRPs;
-}
 
 
 /// addMethods - Insert instance and methods declarations into
@@ -557,14 +546,11 @@
       return MethodDecl;
 
     // Didn't find one yet - look through protocols.
-    const ObjCList<ObjCProtocolDecl> &Protocols =
-      ClassDecl->getReferencedProtocols();
-
-    for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
-         E = Protocols.end(); I != E; ++I) {
+    for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
+         E = ClassDecl->protocol_end(); I != E; ++I)
       if ((MethodDecl = (*I)->getClassMethod(Sel)))
         return MethodDecl;
-    }
+    
     // Didn't find one yet - now look through categories.
     ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
     while (CatDecl) {
@@ -627,14 +613,9 @@
   if ((MethodDecl = getInstanceMethod(Sel)))
     return MethodDecl;
     
-  if (getNumReferencedProtocols() > 0) {
-    ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
-    
-    for (unsigned i = 0; i < getNumReferencedProtocols(); i++) {
-      if ((MethodDecl = RefPDecl[i]->getInstanceMethod(Sel)))
-        return MethodDecl;
-    }
-  }
+  for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
+    if ((MethodDecl = (*I)->getInstanceMethod(Sel)))
+      return MethodDecl;
   return NULL;
 }
 
@@ -646,14 +627,9 @@
   if ((MethodDecl = getClassMethod(Sel)))
     return MethodDecl;
     
-  if (getNumReferencedProtocols() > 0) {
-    ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
-    
-    for(unsigned i = 0; i < getNumReferencedProtocols(); i++) {
-      if ((MethodDecl = RefPDecl[i]->getClassMethod(Sel)))
-        return MethodDecl;
-    }
-  }
+  for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
+    if ((MethodDecl = (*I)->getClassMethod(Sel)))
+      return MethodDecl;
   return NULL;
 }
 
diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp
index b9c1542..f2112eb 100644
--- a/lib/CodeGen/CodeGenModule.cpp
+++ b/lib/CodeGen/CodeGenModule.cpp
@@ -362,14 +362,15 @@
 }
 void CodeGenModule::EmitObjCProtocolImplementation(const ObjCProtocolDecl *PD){
   llvm::SmallVector<std::string, 16> Protocols;
-  for (unsigned i = 0, e = PD->getNumReferencedProtocols() ; i < e ; i++)
-    Protocols.push_back(PD->getReferencedProtocols()[i]->getName());
+  for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
+       E = PD->protocol_end(); PI != E; ++PI)
+    Protocols.push_back((*PI)->getName());
   llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames;
   llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
   for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(),
-      endIter = PD->instmeth_end() ; iter != endIter ; iter++) {
+       E = PD->instmeth_end(); iter != E; iter++) {
     std::string TypeStr;
-    Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
+    Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
     InstanceMethodNames.push_back(
         GetAddrOfConstantString((*iter)->getSelector().getName()));
     InstanceMethodTypes.push_back(GetAddrOfConstantString(TypeStr));
diff --git a/lib/Sema/SemaDeclObjC.cpp b/lib/Sema/SemaDeclObjC.cpp
index c3c2426..9de9efa 100644
--- a/lib/Sema/SemaDeclObjC.cpp
+++ b/lib/Sema/SemaDeclObjC.cpp
@@ -209,18 +209,17 @@
     }
     
     PDecl->setForwardDecl(false);
-    PDecl->AllocReferencedProtocols(NumProtoRefs);
   } else {
-    PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc, NumProtoRefs, 
-                                     ProtocolName);
+    PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc,ProtocolName);
     PDecl->setForwardDecl(false);
     ObjCProtocols[ProtocolName] = PDecl;
   }
   
   if (NumProtoRefs) {
     /// Check then save referenced protocols.
+    llvm::SmallVector<ObjCProtocolDecl*, 8> Protocols;
     for (unsigned int i = 0; i != NumProtoRefs; i++) {
-      ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
+      ObjCProtocolDecl *RefPDecl = ObjCProtocols[ProtoRefNames[i]];
       if (!RefPDecl)
         Diag(ProtocolLoc, diag::err_undef_protocolref,
              ProtoRefNames[i]->getName(), ProtocolName->getName());
@@ -228,9 +227,11 @@
         if (RefPDecl->isForwardDecl())
           Diag(ProtocolLoc, diag::warn_undef_protocolref,
                ProtoRefNames[i]->getName(), ProtocolName->getName());
-        PDecl->setReferencedProtocols(i, RefPDecl);
+        Protocols.push_back(RefPDecl);
       }
     }
+    if (!Protocols.empty())
+      PDecl->addReferencedProtocols(&Protocols[0], Protocols.size());
     PDecl->setLocEnd(EndProtoLoc);
   }
   return PDecl;
@@ -389,7 +390,7 @@
     ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
     if (PDecl == 0)  { // Not already seen?
       // FIXME: Pass in the location of the identifier!
-      PDecl = ObjCProtocolDecl::Create(Context, AtProtocolLoc, 0, Ident);
+      PDecl = ObjCProtocolDecl::Create(Context, AtProtocolLoc, Ident);
     }
     
     Protocols.push_back(PDecl);
@@ -627,10 +628,10 @@
         method->getImplementationControl() != ObjCMethodDecl::Optional)
       WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
   }
-  // Check on this protocols's referenced protocols, recursively
-  ObjCProtocolDecl** RefPDecl = PDecl->getReferencedProtocols();
-  for (unsigned i = 0; i < PDecl->getNumReferencedProtocols(); i++)
-    CheckProtocolMethodDefs(ImpLoc, RefPDecl[i], IncompleteImpl, InsMap, ClsMap);
+  // Check on this protocols's referenced protocols, recursively.
+  for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
+       E = PDecl->protocol_end(); PI != E; ++PI)
+    CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap);
 }
 
 void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl, 
@@ -702,12 +703,10 @@
   
   // Check the protocol list for unimplemented methods in the @implementation
   // class.
-  ObjCProtocolDecl** protocols = CatClassDecl->getReferencedProtocols();
-  for (unsigned i = 0; i < CatClassDecl->getNumReferencedProtocols(); i++) {
-    ObjCProtocolDecl* PDecl = protocols[i];
-    CheckProtocolMethodDefs(CatImplDecl->getLocation(), PDecl, IncompleteImpl, 
+  for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
+       E = CatClassDecl->protocol_end(); PI != E; ++PI)
+    CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl, 
                             InsMap, ClsMap);
-  }
 }
 
 /// ActOnForwardClassDeclaration - 
diff --git a/lib/Sema/SemaExprObjC.cpp b/lib/Sema/SemaExprObjC.cpp
index 6d19b87..333268e 100644
--- a/lib/Sema/SemaExprObjC.cpp
+++ b/lib/Sema/SemaExprObjC.cpp
@@ -359,9 +359,9 @@
                                            ObjCProtocolDecl *rProto) {
   if (lProto == rProto)
     return true;
-  ObjCProtocolDecl** RefPDecl = rProto->getReferencedProtocols();
-  for (unsigned i = 0; i < rProto->getNumReferencedProtocols(); i++)
-    if (ProtocolCompatibleWithProtocol(lProto, RefPDecl[i]))
+  for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
+       E = rProto->protocol_end(); PI != E; ++PI)
+    if (ProtocolCompatibleWithProtocol(lProto, *PI))
       return true;
   return false;
 }
@@ -396,11 +396,10 @@
   if (lookupCategory)
     for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
          CDecl = CDecl->getNextClassCategory()) {
-      ObjCProtocolDecl **protoList = CDecl->getReferencedProtocols();
-      for (unsigned i = 0; i < CDecl->getNumReferencedProtocols(); i++) {
-        if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
+      for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
+           E = CDecl->protocol_end(); PI != E; ++PI)
+        if (ProtocolCompatibleWithProtocol(lProto, *PI))
           return true;
-      }
     }
   
   // 3rd, look up the super class(s)