[cfe-commits] r48427 - in /cfe/trunk: include/clang/AST/DeclObjC.h lib/AST/DeclObjC.cpp lib/Sema/SemaDeclObjC.cpp

Chris Lattner sabre at nondot.org
Sun Mar 16 13:47:46 PDT 2008


Author: lattner
Date: Sun Mar 16 15:47:45 2008
New Revision: 48427

URL: http://llvm.org/viewvc/llvm-project?rev=48427&view=rev
Log:
simplify the way ObjCCategoryDecl's get their referenced protocols list
specified.  Previously, the ctor would allocate memory for the list and then
it would get filled in later.  Move the allocation+filling in to be more 
consistent with other stuff, e.g. the addMethods method.

Modified:
    cfe/trunk/include/clang/AST/DeclObjC.h
    cfe/trunk/lib/AST/DeclObjC.cpp
    cfe/trunk/lib/Sema/SemaDeclObjC.cpp

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

==============================================================================
--- cfe/trunk/include/clang/AST/DeclObjC.h (original)
+++ cfe/trunk/include/clang/AST/DeclObjC.h Sun Mar 16 15:47:45 2008
@@ -610,14 +610,14 @@
 /// several files (a feature more naturally supported in C++).
 ///
 /// Categories were originally inspired by dynamic languages such as Common
-/// Lisp and Smalltalk. More traditional class-based languages (C++, Java) 
+/// Lisp and Smalltalk.  More traditional class-based languages (C++, Java) 
 /// don't support this level of dynamism, which is both powerful and dangerous.
 ///
 class ObjCCategoryDecl : public NamedDecl {
   /// Interface belonging to this category
   ObjCInterfaceDecl *ClassInterface;
   
-  /// referenced protocols in this category
+  /// referenced protocols in this category.
   ObjCProtocolDecl **ReferencedProtocols;  // Null if none
   unsigned NumReferencedProtocols;  // 0 if none
   
@@ -635,28 +635,23 @@
   SourceLocation EndLoc; // marks the '>' or identifier.
   SourceLocation AtEndLoc; // marks the end of the entire interface.
   
-  ObjCCategoryDecl(SourceLocation L, unsigned numRefProtocol,IdentifierInfo *Id)
+  ObjCCategoryDecl(SourceLocation L, IdentifierInfo *Id)
     : NamedDecl(ObjCCategory, L, Id),
       ClassInterface(0), ReferencedProtocols(0), NumReferencedProtocols(0),
       InstanceMethods(0), NumInstanceMethods(0),
       ClassMethods(0), NumClassMethods(0),
       NextClassCategory(0) {
-    if (numRefProtocol) {
-      ReferencedProtocols = new ObjCProtocolDecl*[numRefProtocol];
-      memset(ReferencedProtocols, '\0', 
-             numRefProtocol*sizeof(ObjCProtocolDecl*));
-      NumReferencedProtocols = numRefProtocol;
-    }
   }
 public:
   
   static ObjCCategoryDecl *Create(ASTContext &C, SourceLocation L,
-                                  unsigned numRefProtocol, IdentifierInfo *Id);
-
+                                  IdentifierInfo *Id);
   
   ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
   void setClassInterface(ObjCInterfaceDecl *IDecl) { ClassInterface = IDecl; }
   
+  void setReferencedProtocolList(ObjCProtocolDecl **List, unsigned NumRPs);
+  
   void setCatReferencedProtocols(unsigned idx, ObjCProtocolDecl *OID) {
     assert((idx < NumReferencedProtocols) && "index out of range");
     ReferencedProtocols[idx] = OID;
@@ -682,22 +677,22 @@
   }
 
   // Get the local instance method declared in this interface.
-  ObjCMethodDecl *getInstanceMethod(const Selector &Sel) {
+  ObjCMethodDecl *getInstanceMethod(Selector Sel) {
     for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); 
-             I != E; ++I) {
+         I != E; ++I) {
       if ((*I)->getSelector() == Sel)
         return *I;
     }
-        return 0;
+    return 0;
   }
   // Get the local class method declared in this interface.
-  ObjCMethodDecl *getClassMethod(const Selector &Sel) {
+  ObjCMethodDecl *getClassMethod(Selector Sel) {
     for (classmeth_iterator I = classmeth_begin(), E = classmeth_end(); 
-             I != E; ++I) {
+         I != E; ++I) {
       if ((*I)->getSelector() == Sel)
         return *I;
     }
-        return 0;
+    return 0;
   }
   
   void addMethods(ObjCMethodDecl **insMethods, unsigned numInsMembers,

Modified: cfe/trunk/lib/AST/DeclObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclObjC.cpp?rev=48427&r1=48426&r2=48427&view=diff

==============================================================================
--- cfe/trunk/lib/AST/DeclObjC.cpp (original)
+++ cfe/trunk/lib/AST/DeclObjC.cpp Sun Mar 16 15:47:45 2008
@@ -68,10 +68,9 @@
 }
 
 ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, SourceLocation L,
-                                           unsigned numRefProtocol, 
                                            IdentifierInfo *Id) {
   void *Mem = C.getAllocator().Allocate<ObjCCategoryDecl>();
-  return new (Mem) ObjCCategoryDecl(L, numRefProtocol, Id);
+  return new (Mem) ObjCCategoryDecl(L, Id);
 }
 
 
@@ -164,6 +163,17 @@
   AtEndLoc = endLoc;
 }
 
+void ObjCCategoryDecl::setReferencedProtocolList(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
 /// ObjCCategoryDecl's CatInsMethods and CatClsMethods fields.
 ///

Modified: cfe/trunk/lib/Sema/SemaDeclObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclObjC.cpp?rev=48427&r1=48426&r2=48427&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclObjC.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclObjC.cpp Sun Mar 16 15:47:45 2008
@@ -281,8 +281,7 @@
   ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
   
   ObjCCategoryDecl *CDecl = 
-    ObjCCategoryDecl::Create(Context, AtInterfaceLoc, NumProtoRefs, 
-                             CategoryName);
+    ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName);
   CDecl->setClassInterface(IDecl);
   
   /// Check that class of this category is already completely declared.
@@ -304,7 +303,8 @@
   }
 
   if (NumProtoRefs) {
-    /// Check then save referenced protocols
+    llvm::SmallVector<ObjCProtocolDecl*, 32> RefProtocols;
+    /// Check and then save the referenced protocols.
     for (unsigned int i = 0; i != NumProtoRefs; i++) {
       ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
       if (!RefPDecl || RefPDecl->isForwardDecl()) {
@@ -312,10 +312,13 @@
              ProtoRefNames[i]->getName(),
              CategoryName->getName());
       }
-      CDecl->setCatReferencedProtocols(i, RefPDecl);
+      if (RefPDecl)
+        RefProtocols.push_back(RefPDecl);
     }
-    CDecl->setLocEnd(EndProtoLoc);
+    if (!RefProtocols.empty())
+      CDecl->setReferencedProtocolList(&RefProtocols[0], RefProtocols.size());
   }
+  CDecl->setLocEnd(EndProtoLoc);
   return CDecl;
 }
 





More information about the cfe-commits mailing list