]> granicus.if.org Git - clang/commitdiff
introduce a new NamedDecl class, switch a couple of things over to using it.
authorChris Lattner <sabre@nondot.org>
Sat, 6 Oct 2007 22:53:46 +0000 (22:53 +0000)
committerChris Lattner <sabre@nondot.org>
Sat, 6 Oct 2007 22:53:46 +0000 (22:53 +0000)
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.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@42710 91177308-0d34-0410-b5e6-96231b3b80d8

AST/Decl.cpp
Sema/SemaDecl.cpp
include/clang/AST/Decl.h
include/clang/AST/DeclObjC.h

index f046e271c3b37b2a4f3b100e36e59f43034efc11..da7aab9bcca2b83e51797ff6bb8784dc9971bd0d 100644 (file)
@@ -214,13 +214,7 @@ void Decl::addDeclKind(const Kind k) {
 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 "";
index 71ba023dcf081604807b8e12e659db79c57058db..41c04b9ff8ae1838b02385cf6246c5c54bd60583 100644 (file)
@@ -1061,9 +1061,9 @@ Sema::DeclTy *Sema::ActOnStartCategoryInterface(Scope* S,
                       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 @@ Sema::DeclTy *Sema::ActOnStartCategoryInterface(Scope* S,
     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 @@ void Sema::ImplCategoryMethodsVsIntfMethods(ObjcCategoryImplDecl *CatImplDecl,
   }
   if (IncompleteImpl)
     Diag(CatImplDecl->getLocation(), diag::warn_incomplete_impl_category, 
-         CatClassDecl->getCatName()->getName());
+         CatClassDecl->getName());
 }
 
 /// ActOnForwardClassDeclaration - 
@@ -1723,8 +1721,10 @@ bool Sema:: MatchTwoMethodDeclarations(const ObjcMethodDecl *Method,
   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 @@ void Sema::ActOnAddMethodsToObjcDecl(Scope* S, DeclTy *ClassDecl,
   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 @@ void Sema::ActOnAddMethodsToObjcDecl(Scope* S, DeclTy *ClassDecl,
         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);
+  
+  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 {
-    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;
-          }
+  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");
   }
 }
 
index fe2f4c2d898f1a15820987a2769a99bbb912a840..5f45041a75b3774109b98eb7b4d816da1e7d9c68 100644 (file)
@@ -111,12 +111,25 @@ public:
   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 @@ class ScopedDecl : public Decl {
   ///
   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 @@ private:
 
 /// 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 @@ class FieldDecl : public Decl {
   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(); }
index 9c1222dc604499f836834c3067cc334576c9b666..77aadcd8a2a6c733a962478e5eaf91efb97ef6b1 100644 (file)
@@ -33,7 +33,7 @@ class ObjcCategoryDecl;
 ///     // 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 @@ public:
 /// 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 @@ class ObjcCategoryDecl : public Decl {
   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 @@ public:
   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();