[cfe-commits] r42710 - in /cfe/trunk: AST/Decl.cpp Sema/SemaDecl.cpp include/clang/AST/Decl.h include/clang/AST/DeclObjC.h

Chris Lattner sabre at nondot.org
Sat Oct 6 15:53:46 PDT 2007


Author: lattner
Date: Sat Oct  6 17:53:46 2007
New Revision: 42710

URL: http://llvm.org/viewvc/llvm-project?rev=42710&view=rev
Log:
introduce a new NamedDecl class, switch a couple of things over to using it.
NamedDecl is a Decl that has an IdentifierInfo (for example, ScopedDecl), 
but not ObjcMethodDecl.

Simplify some code in ActOnAddMethodsToObjcDecl, by doing the cast from 
DeclTy to Decl at the start of the method.


Modified:
    cfe/trunk/AST/Decl.cpp
    cfe/trunk/Sema/SemaDecl.cpp
    cfe/trunk/include/clang/AST/Decl.h
    cfe/trunk/include/clang/AST/DeclObjC.h

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

==============================================================================
--- cfe/trunk/AST/Decl.cpp (original)
+++ cfe/trunk/AST/Decl.cpp Sat Oct  6 17:53:46 2007
@@ -214,13 +214,7 @@
 Decl::~Decl() {
 }
 
-const char *FieldDecl::getName() const {
-  if (const IdentifierInfo *II = getIdentifier())
-    return II->getName();
-  return "";
-}
-
-const char *ScopedDecl::getName() const {
+const char *NamedDecl::getName() const {
   if (const IdentifierInfo *II = getIdentifier())
     return II->getName();
   return "";

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

==============================================================================
--- cfe/trunk/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/Sema/SemaDecl.cpp Sat Oct  6 17:53:46 2007
@@ -1061,9 +1061,9 @@
                       IdentifierInfo *ClassName, SourceLocation ClassLoc,
                       IdentifierInfo *CategoryName, SourceLocation CategoryLoc,
                       IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs) {
-  ObjcCategoryDecl *CDecl;
-  ObjcInterfaceDecl* IDecl = getObjCInterfaceDecl(ClassName);
-  CDecl = new ObjcCategoryDecl(AtInterfaceLoc, NumProtoRefs);
+  ObjcInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
+  ObjcCategoryDecl *CDecl = new ObjcCategoryDecl(AtInterfaceLoc, NumProtoRefs,
+                                                 CategoryName);
   CDecl->setClassInterface(IDecl);
 
   /// Check that class of this category is already completely declared.
@@ -1074,16 +1074,14 @@
     ObjcCategoryDecl *CDeclChain;
     for (CDeclChain = IDecl->getListCategories(); CDeclChain;
          CDeclChain = CDeclChain->getNextClassCategory()) {
-      if (CDeclChain->getCatName() == CategoryName) {
+      if (CDeclChain->getIdentifier() == CategoryName) {
         Diag(CategoryLoc, diag::err_dup_category_def, ClassName->getName(),
              CategoryName->getName());
         break;
       }
     }
-    if (!CDeclChain) {
-      CDecl->setCatName(CategoryName);
+    if (!CDeclChain)
       CDecl->insertNextClassCategory();
-    }
   }
   
   /// Check then save referenced protocols
@@ -1363,7 +1361,7 @@
   }
   if (IncompleteImpl)
     Diag(CatImplDecl->getLocation(), diag::warn_incomplete_impl_category, 
-         CatClassDecl->getCatName()->getName());
+         CatClassDecl->getName());
 }
 
 /// ActOnForwardClassDeclaration - 
@@ -1723,8 +1721,10 @@
   return true;
 }
 
-void Sema::ActOnAddMethodsToObjcDecl(Scope* S, DeclTy *ClassDecl,
+void Sema::ActOnAddMethodsToObjcDecl(Scope* S, DeclTy *classDecl,
                                      DeclTy **allMethods, unsigned allNum) {
+  Decl *ClassDecl = static_cast<Decl *>(classDecl);
+  
   // FIXME: Fix this when we can handle methods declared in protocols.
   // See Parser::ParseObjCAtProtocolDeclaration
   if (!ClassDecl)
@@ -1736,8 +1736,7 @@
   llvm::DenseMap<void *, const ObjcMethodDecl*> ClsMap;
   
   bool isClassDeclaration = 
-        (isa<ObjcInterfaceDecl>(static_cast<Decl *>(ClassDecl))
-         || isa<ObjcCategoryDecl>(static_cast<Decl *>(ClassDecl)));
+        (isa<ObjcInterfaceDecl>(ClassDecl) || isa<ObjcCategoryDecl>(ClassDecl));
   
   for (unsigned i = 0; i < allNum; i++ ) {
     ObjcMethodDecl *Method =
@@ -1782,54 +1781,40 @@
         clsMethods.push_back(Method);
     }
   }
-  if (isa<ObjcInterfaceDecl>(static_cast<Decl *>(ClassDecl))) {
-    ObjcInterfaceDecl *Interface = cast<ObjcInterfaceDecl>(
-                                          static_cast<Decl*>(ClassDecl));
-    Interface->ObjcAddMethods(&insMethods[0], insMethods.size(),
-                              &clsMethods[0], clsMethods.size());
-  }
-  else if (isa<ObjcProtocolDecl>(static_cast<Decl *>(ClassDecl))) {
-    ObjcProtocolDecl *Protocol = cast<ObjcProtocolDecl>(
-                                        static_cast<Decl*>(ClassDecl));
-    Protocol->ObjcAddProtoMethods(&insMethods[0], insMethods.size(),
-                                  &clsMethods[0], clsMethods.size());
-  }
-  else if (isa<ObjcCategoryDecl>(static_cast<Decl *>(ClassDecl))) {
-    ObjcCategoryDecl *Category = cast<ObjcCategoryDecl>(
-                                        static_cast<Decl*>(ClassDecl));
-    Category->ObjcAddCatMethods(&insMethods[0], insMethods.size(),
-                                &clsMethods[0], clsMethods.size());
-  }
-  else if (isa<ObjcImplementationDecl>(static_cast<Decl *>(ClassDecl))) {
-    ObjcImplementationDecl* ImplClass = cast<ObjcImplementationDecl>(
-                                               static_cast<Decl*>(ClassDecl));
-    ImplClass->ObjcAddImplMethods(&insMethods[0], insMethods.size(),
-                                 &clsMethods[0], clsMethods.size());
-    ObjcInterfaceDecl* IDecl = getObjCInterfaceDecl(ImplClass->getIdentifier());
-    if (IDecl)
-      ImplMethodsVsClassMethods(ImplClass, IDecl);
-  }
-  else {
-    ObjcCategoryImplDecl* CatImplClass = dyn_cast<ObjcCategoryImplDecl>(
-                                          static_cast<Decl*>(ClassDecl));
-    if (CatImplClass) {
-      CatImplClass->ObjcAddCatImplMethods(&insMethods[0], insMethods.size(),
-                                          &clsMethods[0], clsMethods.size());
-      ObjcInterfaceDecl* IDecl = CatImplClass->getClassInterface();
-      // Find category interface decl and then check that all methods declared
-      // in this interface is implemented in the category @implementation.
-      if (IDecl) {
-        for (ObjcCategoryDecl *Categories = IDecl->getListCategories();
-             Categories; Categories = Categories->getNextClassCategory()) {
-          if (Categories->getCatName() == CatImplClass->getObjcCatName()) {
-            ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
-            break;
-          }
+  
+  if (ObjcInterfaceDecl *I = dyn_cast<ObjcInterfaceDecl>(ClassDecl)) {
+    I->ObjcAddMethods(&insMethods[0], insMethods.size(),
+                      &clsMethods[0], clsMethods.size());
+  } else if (ObjcProtocolDecl *P = dyn_cast<ObjcProtocolDecl>(ClassDecl)) {
+    P->ObjcAddProtoMethods(&insMethods[0], insMethods.size(),
+                           &clsMethods[0], clsMethods.size());
+  }
+  else if (ObjcCategoryDecl *C = dyn_cast<ObjcCategoryDecl>(ClassDecl)) {
+    C->ObjcAddCatMethods(&insMethods[0], insMethods.size(),
+                         &clsMethods[0], clsMethods.size());
+  }
+  else if (ObjcImplementationDecl *IC = 
+                dyn_cast<ObjcImplementationDecl>(ClassDecl)) {
+    IC->ObjcAddImplMethods(&insMethods[0], insMethods.size(),
+                           &clsMethods[0], clsMethods.size());
+    if (ObjcInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
+      ImplMethodsVsClassMethods(IC, IDecl);
+  } else {
+    ObjcCategoryImplDecl* CatImplClass = cast<ObjcCategoryImplDecl>(ClassDecl);
+    CatImplClass->ObjcAddCatImplMethods(&insMethods[0], insMethods.size(),
+                                        &clsMethods[0], clsMethods.size());
+    ObjcInterfaceDecl* IDecl = CatImplClass->getClassInterface();
+    // Find category interface decl and then check that all methods declared
+    // in this interface is implemented in the category @implementation.
+    if (IDecl) {
+      for (ObjcCategoryDecl *Categories = IDecl->getListCategories();
+           Categories; Categories = Categories->getNextClassCategory()) {
+        if (Categories->getIdentifier() == CatImplClass->getObjcCatName()) {
+          ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
+          break;
         }
       }
     }
-    else
-      assert(0 && "Sema::ActOnAddMethodsToObjcDecl(): Unknown DeclTy");
   }
 }
 

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

==============================================================================
--- cfe/trunk/include/clang/AST/Decl.h (original)
+++ cfe/trunk/include/clang/AST/Decl.h Sat Oct  6 17:53:46 2007
@@ -111,12 +111,25 @@
   static bool classof(const Decl *) { return true; }
 };
 
-/// ScopedDecl - Represent lexically scoped names, used for all ValueDecl's
-/// and TypeDecl's.
-class ScopedDecl : public Decl {
+class NamedDecl : public Decl {
   /// Identifier - The identifier for this declaration (e.g. the name for the
   /// variable, the tag for a struct).
   IdentifierInfo *Identifier;
+public:
+  NamedDecl(Kind DK, SourceLocation L, IdentifierInfo *Id)
+   : Decl(DK, L), Identifier(Id) {}
+  
+  IdentifierInfo *getIdentifier() const { return Identifier; }
+  const char *getName() const;
+  
+  
+  // FIXME: classof.
+  static bool classof(const NamedDecl *D) { return true; }
+};
+
+/// ScopedDecl - Represent lexically scoped names, used for all ValueDecl's
+/// and TypeDecl's.
+class ScopedDecl : public NamedDecl {
   
   /// NextDeclarator - If this decl was part of a multi-declarator declaration,
   /// such as "int X, Y, *Z;" this indicates Decl for the next declarator.
@@ -128,12 +141,9 @@
   ///
   ScopedDecl *Next;
 protected:
-  ScopedDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, ScopedDecl *PrevDecl) 
-    : Decl(DK, L), Identifier(Id), NextDeclarator(PrevDecl), Next(0) {}
+  ScopedDecl(Kind DK, SourceLocation L, IdentifierInfo *Id,ScopedDecl *PrevDecl)
+    : NamedDecl(DK, L, Id), NextDeclarator(PrevDecl), Next(0) {}
 public:
-  IdentifierInfo *getIdentifier() const { return Identifier; }
-  const char *getName() const;
-
   ScopedDecl *getNext() const { return Next; }
   void setNext(ScopedDecl *N) { Next = N; }
   
@@ -325,7 +335,7 @@
 
 /// FieldDecl - An instance of this class is created by Sema::ActOnField to 
 /// represent a member of a struct/union/class.
-class FieldDecl : public Decl {
+class FieldDecl : public NamedDecl {
   /// Identifier - The identifier for this declaration (e.g. the name for the
   /// variable, the tag for a struct).
   IdentifierInfo *Identifier;
@@ -333,12 +343,9 @@
   QualType DeclType;  
 public:
   FieldDecl(SourceLocation L, IdentifierInfo *Id, QualType T)
-    : Decl(Field, L), Identifier(Id), DeclType(T) {}
+    : NamedDecl(Field, L, Id), DeclType(T) {}
   FieldDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, QualType T) 
-    : Decl(DK, L), Identifier(Id), DeclType(T) {}
-
-  IdentifierInfo *getIdentifier() const { return Identifier; }
-  const char *getName() const;
+    : NamedDecl(DK, L, Id), DeclType(T) {}
 
   QualType getType() const { return DeclType; }
   QualType getCanonicalType() const { return DeclType.getCanonicalType(); }

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

==============================================================================
--- cfe/trunk/include/clang/AST/DeclObjC.h (original)
+++ cfe/trunk/include/clang/AST/DeclObjC.h Sat Oct  6 17:53:46 2007
@@ -33,7 +33,7 @@
 ///     // no instance variables or methods.
 ///   @end
 ///
-///   // NSResponder inherits from NSObject and implements NSCoding (a protocol). 
+///   // NSResponder inherits from NSObject & implements NSCoding (a protocol). 
 ///   @interface NSResponder : NSObject <NSCoding>
 ///   { // instance variables are represented by ObjcIvarDecl.
 ///     id nextResponder; // nextResponder instance variable.
@@ -429,13 +429,10 @@
 /// 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 Decl {
+class ObjcCategoryDecl : public NamedDecl {
   /// Interface belonging to this category
   ObjcInterfaceDecl *ClassInterface;
   
-  /// Category name
-  IdentifierInfo *ObjcCatName;
-  
   /// referenced protocols in this category
   ObjcProtocolDecl **ReferencedProtocols;  // Null if none
   int NumReferencedProtocols;  // -1 if none
@@ -452,10 +449,9 @@
   ObjcCategoryDecl *NextClassCategory;
   
 public:
-  ObjcCategoryDecl(SourceLocation L, unsigned numRefProtocol)
-    : Decl(ObjcCategory, L),
-      ClassInterface(0), ObjcCatName(0),
-      ReferencedProtocols(0), NumReferencedProtocols(-1),
+  ObjcCategoryDecl(SourceLocation L, unsigned numRefProtocol,IdentifierInfo *Id)
+    : NamedDecl(ObjcCategory, L, Id),
+      ClassInterface(0), ReferencedProtocols(0), NumReferencedProtocols(-1),
       InstanceMethods(0), NumInstanceMethods(-1),
       ClassMethods(0), NumClassMethods(-1),
       NextClassCategory(0) {
@@ -489,9 +485,6 @@
   void ObjcAddCatMethods(ObjcMethodDecl **insMethods, unsigned numInsMembers,
                          ObjcMethodDecl **clsMethods, unsigned numClsMembers);
   
-  IdentifierInfo *getCatName() const { return ObjcCatName; }
-  void setCatName(IdentifierInfo *catName) { ObjcCatName = catName; }
-  
   ObjcCategoryDecl *getNextClassCategory() const { return NextClassCategory; }
   void insertNextClassCategory() {
     NextClassCategory = ClassInterface->getListCategories();





More information about the cfe-commits mailing list