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

sabre at cs.uiuc.edu sabre at cs.uiuc.edu
Wed Jul 11 09:40:45 PDT 2007


Author: sabre
Date: Wed Jul 11 11:40:45 2007
New Revision: 39185

URL: http://llvm.org/viewvc/llvm-project?rev=39185&view=rev
Log:
implement RTTI for Decl objects, eliminate some hokey virtual methods.

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

Modified: cfe/cfe/trunk/AST/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/AST/SemaDecl.cpp?rev=39185&r1=39184&r2=39185&view=diff

==============================================================================
--- cfe/cfe/trunk/AST/SemaDecl.cpp (original)
+++ cfe/cfe/trunk/AST/SemaDecl.cpp Wed Jul 11 11:40:45 2007
@@ -22,7 +22,9 @@
 
 bool Sema::isTypeName(const IdentifierInfo &II, Scope *S) const {
   Decl *D = II.getFETokenInfo<Decl>();
-  return D != 0 && D->getDeclSpec().StorageClassSpec == DeclSpec::SCS_typedef;
+  
+  
+  return D != 0 && isa<TypeDecl>(D);
 }
 
 void Sema::PopScope(SourceLocation Loc, Scope *S) {
@@ -74,9 +76,9 @@
   if (D.getDeclSpec().StorageClassSpec == DeclSpec::SCS_typedef) {
     New = ParseTypedefDecl(S, D, PrevDecl);
   } else if (D.isFunctionDeclarator())
-    New = new FunctionDecl(II, D, PrevDecl);
+    New = new FunctionDecl(II, D.getDeclSpec(), PrevDecl);
   else
-    New = new VarDecl(II, D, PrevDecl);
+    New = new VarDecl(II, D.getDeclSpec(), PrevDecl);
   
   if (!New) return 0;
   
@@ -108,8 +110,8 @@
 
 
 Decl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, Decl *PrevDecl) {
-  assert(D.getIdentifier());
+  assert(D.getIdentifier() && "Wrong callback for declspec withotu declarator");
   
-  return new TypedefDecl(D.getIdentifier(), D, PrevDecl);
+  return new TypedefDecl(D.getIdentifier(), D.getDeclSpec(), PrevDecl);
 }
 

Modified: cfe/cfe/trunk/Driver/clang.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Driver/clang.cpp?rev=39185&r1=39184&r2=39185&view=diff

==============================================================================
--- cfe/cfe/trunk/Driver/clang.cpp (original)
+++ cfe/cfe/trunk/Driver/clang.cpp Wed Jul 11 11:40:45 2007
@@ -808,7 +808,7 @@
       std::cerr << II->getName() << "'\n";
     else
       std::cerr << "\n";
-    if (FunctionDecl *FD = D->isFunctionDecl()) {
+    if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
       if (FD->getBody()) {
         FD->getBody()->dump();
         std::cerr << "\n";

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

==============================================================================
--- cfe/cfe/trunk/Sema/SemaDecl.cpp (original)
+++ cfe/cfe/trunk/Sema/SemaDecl.cpp Wed Jul 11 11:40:45 2007
@@ -22,7 +22,9 @@
 
 bool Sema::isTypeName(const IdentifierInfo &II, Scope *S) const {
   Decl *D = II.getFETokenInfo<Decl>();
-  return D != 0 && D->getDeclSpec().StorageClassSpec == DeclSpec::SCS_typedef;
+  
+  
+  return D != 0 && isa<TypeDecl>(D);
 }
 
 void Sema::PopScope(SourceLocation Loc, Scope *S) {
@@ -74,9 +76,9 @@
   if (D.getDeclSpec().StorageClassSpec == DeclSpec::SCS_typedef) {
     New = ParseTypedefDecl(S, D, PrevDecl);
   } else if (D.isFunctionDeclarator())
-    New = new FunctionDecl(II, D, PrevDecl);
+    New = new FunctionDecl(II, D.getDeclSpec(), PrevDecl);
   else
-    New = new VarDecl(II, D, PrevDecl);
+    New = new VarDecl(II, D.getDeclSpec(), PrevDecl);
   
   if (!New) return 0;
   
@@ -108,8 +110,8 @@
 
 
 Decl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, Decl *PrevDecl) {
-  assert(D.getIdentifier());
+  assert(D.getIdentifier() && "Wrong callback for declspec withotu declarator");
   
-  return new TypedefDecl(D.getIdentifier(), D, PrevDecl);
+  return new TypedefDecl(D.getIdentifier(), D.getDeclSpec(), PrevDecl);
 }
 

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

==============================================================================
--- cfe/cfe/trunk/include/clang/AST/Decl.h (original)
+++ cfe/cfe/trunk/include/clang/AST/Decl.h Wed Jul 11 11:40:45 2007
@@ -27,6 +27,11 @@
 /// typedef, function, struct, etc.  
 ///
 class Decl {
+public:
+  enum Kind {
+    Typedef, Function, Variable
+  };
+private:
   /// Identifier - The identifier for this declaration (e.g. the name for the
   /// variable, the tag for a struct).
   IdentifierInfo *Identifier;
@@ -37,33 +42,50 @@
   DeclSpec DeclarationSpecifier;
   
   /// Type.
-  /// Kind.
+  
+  /// DeclKind - This indicates which class this is.
+  Kind DeclKind;
   
   /// Scope stack info when parsing, otherwise decl list when scope is popped.
   ///
   Decl *Next;
+  
 public:
-  Decl(IdentifierInfo *Id, const Declarator &D, Decl *next)
-    : Identifier(Id), DeclarationSpecifier(D.getDeclSpec()), Next(next) {}
+  Decl(IdentifierInfo *Id, const DeclSpec &DS, Kind DK, Decl *next)
+    : Identifier(Id), DeclarationSpecifier(DS), DeclKind(DK), Next(next) {}
   virtual ~Decl();
   
   const IdentifierInfo *getIdentifier() const { return Identifier; }
-  
-  const DeclSpec &getDeclSpec() const { return DeclarationSpecifier; }
+
+  Kind getKind() const { return DeclKind; }
   
   Decl *getNext() const { return Next; }
   
-  // FIXME: Implement cast/dyn_cast/etc
-  virtual FunctionDecl *isFunctionDecl() { return 0; }
-  virtual const FunctionDecl *isFunctionDecl() const { return 0; }
+  // Implement isa/cast/dyncast/etc.
+  static bool classof(const Decl *) { return true; }
 };
 
-class TypedefDecl : public Decl {
+/// TypeDecl - Common base-class for all type name decls, which as Typedefs and
+/// Objective-C classes.
+class TypeDecl : public Decl {
+public:
+  TypeDecl(IdentifierInfo *Id, const DeclSpec &DS, Kind DK, Decl *Next)
+    : Decl(Id, DS, DK, Next) {}
+
+  // Implement isa/cast/dyncast/etc.
+  static bool classof(const Decl *D) { return D->getKind() == Typedef; }
+  static bool classof(const TypeDecl *D) { return true; }
+};
+
+class TypedefDecl : public TypeDecl {
 public:
   // FIXME: Remove Declarator argument.
-  TypedefDecl(IdentifierInfo *Id, const Declarator &D, Decl *Next)
-    : Decl(Id, D, Next) {}
+  TypedefDecl(IdentifierInfo *Id, const DeclSpec &DS, Decl *Next)
+    : TypeDecl(Id, DS, Typedef, Next) {}
 
+  // Implement isa/cast/dyncast/etc.
+  static bool classof(const Decl *D) { return D->getKind() == Typedef; }
+  static bool classof(const TypedefDecl *D) { return true; }
 };
 
 /// FunctionDecl - An instance of this class is created to represent a function
@@ -72,16 +94,16 @@
   // Args etc.
   Stmt *Body;  // Null if a prototype.
 public:
-  FunctionDecl(IdentifierInfo *Id, const Declarator &D, Decl *Next)
-    : Decl(Id, D, Next), Body(0) {}
+  FunctionDecl(IdentifierInfo *Id, const DeclSpec &DS, Decl *Next)
+    : Decl(Id, DS, Function, Next), Body(0) {}
   
   Stmt *getBody() const { return Body; }
   void setBody(Stmt *B) { Body = B; }
   
   
-  // FIXME: Implement cast/dyn_cast/etc
-  virtual FunctionDecl *isFunctionDecl() { return this; }
-  virtual const FunctionDecl *isFunctionDecl() const { return this; }
+  // Implement isa/cast/dyncast/etc.
+  static bool classof(const Decl *D) { return D->getKind() == Function; }
+  static bool classof(const FunctionDecl *D) { return true; }
 };
 
 /// VarDecl - An instance of this class is created to represent a variable
@@ -89,9 +111,13 @@
 class VarDecl : public Decl {
   // Initializer.
 public:
-  VarDecl(IdentifierInfo *Id, const Declarator &D, Decl *Next)
-    : Decl(Id, D, Next) {}
+  VarDecl(IdentifierInfo *Id, const DeclSpec &DS, Decl *Next)
+    : Decl(Id, DS, Variable, Next) {}
+  
   
+  // Implement isa/cast/dyncast/etc.
+  static bool classof(const Decl *D) { return D->getKind() == Variable; }
+  static bool classof(const VarDecl *D) { return true; }
 };
   
 }  // end namespace clang





More information about the cfe-commits mailing list