[cfe-commits] r39108 - in /cfe/cfe/trunk: AST/Expr.cpp AST/Stmt.cpp include/clang/AST/Expr.h include/clang/AST/Stmt.h include/clang/AST/StmtVisitor.h

sabre at cs.uiuc.edu sabre at cs.uiuc.edu
Wed Jul 11 09:27:35 PDT 2007


Author: sabre
Date: Wed Jul 11 11:27:35 2007
New Revision: 39108

URL: http://llvm.org/viewvc/llvm-project?rev=39108&view=rev
Log:
make visit methods take a reference to a visitor instead of a pointer.
Remove all dump_impl methods from Stmt subclasses

Modified:
    cfe/cfe/trunk/AST/Expr.cpp
    cfe/cfe/trunk/AST/Stmt.cpp
    cfe/cfe/trunk/include/clang/AST/Expr.h
    cfe/cfe/trunk/include/clang/AST/Stmt.h
    cfe/cfe/trunk/include/clang/AST/StmtVisitor.h

Modified: cfe/cfe/trunk/AST/Expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/AST/Expr.cpp?rev=39108&r1=39107&r2=39108&view=diff

==============================================================================
--- cfe/cfe/trunk/AST/Expr.cpp (original)
+++ cfe/cfe/trunk/AST/Expr.cpp Wed Jul 11 11:27:35 2007
@@ -14,7 +14,6 @@
 #include "clang/AST/Expr.h"
 #include "clang/AST/StmtVisitor.h"
 #include "clang/Lex/IdentifierTable.h"
-#include <iostream>
 using namespace llvm;
 using namespace clang;
 
@@ -23,7 +22,7 @@
 //===----------------------------------------------------------------------===//
 
 #define MAKE_VISITOR(CLASS) \
-  void CLASS::visit(StmtVisitor *V) { return V->Visit ## CLASS(this); }
+  void CLASS::visit(StmtVisitor &V) { return V.Visit##CLASS(this); }
 
 MAKE_VISITOR(Expr)
 MAKE_VISITOR(DeclRefExpr)
@@ -46,19 +45,6 @@
 // Primary Expressions.
 //===----------------------------------------------------------------------===//
 
-void DeclRefExpr::dump_impl() const {
-  std::cerr << "x";
-}
-
-void IntegerConstant::dump_impl() const {
-  std::cerr << "1";
-}
-
-void FloatingConstant::dump_impl() const {
-  std::cerr << "1.0";
-}
-
-
 
 StringExpr::StringExpr(const char *strData, unsigned byteLength, bool Wide) {
   // OPTIMIZE: could allocate this appended to the StringExpr.
@@ -73,19 +59,6 @@
   delete[] StrData;
 }
 
-void StringExpr::dump_impl() const {
-  if (isWide) std::cerr << 'L';
-  std::cerr << '"' << StrData << '"';
-}
-
-
-
-void ParenExpr::dump_impl() const {
-  std::cerr << "'('";
-  Val->dump();
-  std::cerr << "')'";
-}
-
 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
 /// corresponds to, e.g. "sizeof" or "[pre]++".
 const char *UnaryOperator::getOpcodeStr(Opcode Op) {
@@ -109,28 +82,10 @@
   }
 }
 
-void UnaryOperator::dump_impl() const {
-  std::cerr << getOpcodeStr(Opc);
-  Input->dump();
-}
-
-void SizeOfAlignOfTypeExpr::dump_impl() const {
-  std::cerr << (isSizeof ? "sizeof(" : "alignof(");
-  // FIXME: print type.
-  std::cerr << "ty)";
-}
-
 //===----------------------------------------------------------------------===//
 // Postfix Operators.
 //===----------------------------------------------------------------------===//
 
-void ArraySubscriptExpr::dump_impl() const {
-  Base->dump();
-  std::cerr << "[";
-  Idx->dump();
-  std::cerr << "]";
-}
-
 CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs)
   : Fn(fn), NumArgs(numargs) {
   Args = new Expr*[numargs];
@@ -138,35 +93,6 @@
     Args[i] = args[i];
 }
 
-void CallExpr::dump_impl() const {
-  Fn->dump();
-  std::cerr << "(";
-  for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
-    if (i) std::cerr << ", ";
-    getArg(i)->dump();
-  }
-  std::cerr << ")";
-}
-
-
-void MemberExpr::dump_impl() const {
-  Base->dump();
-  std::cerr << (isArrow ? "->" : ".");
-  
-  if (MemberDecl)
-    /*TODO: Print MemberDecl*/;
-  std::cerr << "member";
-}
-
-
-void CastExpr::dump_impl() const {
-  std::cerr << "'('";
-  // TODO PRINT TYPE
-  std::cerr << "<type>";
-  std::cerr << "')'";
-  Op->dump();
-}
-
 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
 /// corresponds to, e.g. "<<=".
 const char *BinaryOperator::getOpcodeStr(Opcode Op) {
@@ -204,17 +130,3 @@
   case Comma:     return ",";
   }
 }
-
-void BinaryOperator::dump_impl() const {
-  LHS->dump();
-  std::cerr << " " << getOpcodeStr(Opc) << " ";
-  RHS->dump();
-}
-
-void ConditionalOperator::dump_impl() const {
-  Cond->dump();
-  std::cerr << " ? ";
-  LHS->dump();
-  std::cerr << " : ";
-  RHS->dump();
-}

Modified: cfe/cfe/trunk/AST/Stmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/AST/Stmt.cpp?rev=39108&r1=39107&r2=39108&view=diff

==============================================================================
--- cfe/cfe/trunk/AST/Stmt.cpp (original)
+++ cfe/cfe/trunk/AST/Stmt.cpp Wed Jul 11 11:27:35 2007
@@ -14,46 +14,15 @@
 #include "clang/AST/Stmt.h"
 #include "clang/AST/Expr.h"
 #include "clang/AST/StmtVisitor.h"
-#include <iostream>
 using namespace llvm;
 using namespace clang;
 
-void Stmt::dump() const {
-  if (this == 0) {
-    std::cerr << "<null>";
-    return;
-  }
-  if (isExpr()) std::cerr << "(";
-  dump_impl();
-  if (isExpr()) std::cerr << ")";
-}
+#define MAKE_VISITOR(CLASS) \
+void CLASS::visit(StmtVisitor &V) { return V.Visit##CLASS(this); }
 
-void Stmt        ::visit(StmtVisitor *V) { return V->VisitStmt(this); }
-void CompoundStmt::visit(StmtVisitor *V) { return V->VisitCompoundStmt(this); }
-void IfStmt      ::visit(StmtVisitor *V) { return V->VisitIfStmt(this); }
-void ReturnStmt  ::visit(StmtVisitor *V) { return V->VisitReturnStmt(this); }
+MAKE_VISITOR(Stmt)
+MAKE_VISITOR(CompoundStmt)
+MAKE_VISITOR(IfStmt)
+MAKE_VISITOR(ReturnStmt)
 
-
-void CompoundStmt::dump_impl() const {
-  std::cerr << "{\n";
-  for (unsigned i = 0, e = Body.size(); i != e; ++i) {
-    Body[i]->dump();
-    std::cerr << "\n";
-  }
-  std::cerr << "}";
-}
-
-void IfStmt::dump_impl() const {
-  std::cerr << "if ";
-  Cond->dump();
-  std::cerr << " then\n";
-  Then->dump();
-  std::cerr << "\n else ";
-  Else->dump();
-}
-
-void ReturnStmt::dump_impl() const {
-  std::cerr << "return ";
-  if (RetExpr)
-    RetExpr->dump();
-}
+#undef MAKE_VISITOR

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

==============================================================================
--- cfe/cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/cfe/trunk/include/clang/AST/Expr.h Wed Jul 11 11:27:35 2007
@@ -32,10 +32,7 @@
   Expr() {}
   ~Expr() {}
   
-  
-  // FIXME: move to isa/dyncast etc.
-  virtual bool isExpr() const { return true; }
-  virtual void visit(StmtVisitor *Visitor);
+  virtual void visit(StmtVisitor &Visitor);
 };
 
 //===----------------------------------------------------------------------===//
@@ -49,22 +46,19 @@
   Decl &D;
 public:
   DeclRefExpr(Decl &d) : D(d) {}
-  virtual void dump_impl() const;
-  virtual void visit(StmtVisitor *Visitor);
+  virtual void visit(StmtVisitor &Visitor);
 };
 
 class IntegerConstant : public Expr {
 public:
   IntegerConstant() {}
-  virtual void dump_impl() const;
-  virtual void visit(StmtVisitor *Visitor);
+  virtual void visit(StmtVisitor &Visitor);
 };
 
 class FloatingConstant : public Expr {
 public:
   FloatingConstant() {}
-  virtual void dump_impl() const;
-  virtual void visit(StmtVisitor *Visitor);
+  virtual void visit(StmtVisitor &Visitor);
 };
 
 class StringExpr : public Expr {
@@ -74,8 +68,7 @@
 public:
   StringExpr(const char *strData, unsigned byteLength, bool Wide);
   virtual ~StringExpr();
-  virtual void dump_impl() const;
-  virtual void visit(StmtVisitor *Visitor);
+  virtual void visit(StmtVisitor &Visitor);
 };
 
 /// ParenExpr - This represents a parethesized expression, e.g. "(1)".  This
@@ -86,8 +79,7 @@
 public:
   ParenExpr(SourceLocation l, SourceLocation r, Expr *val)
     : L(l), R(r), Val(val) {}
-  virtual void dump_impl() const;
-  virtual void visit(StmtVisitor *Visitor);
+  virtual void visit(StmtVisitor &Visitor);
 };
 
 
@@ -115,8 +107,7 @@
   /// corresponds to, e.g. "sizeof" or "[pre]++"
   static const char *getOpcodeStr(Opcode Op);
   
-  virtual void dump_impl() const;
-  virtual void visit(StmtVisitor *Visitor);
+  virtual void visit(StmtVisitor &Visitor);
 
 private:
   Expr *Input;
@@ -132,8 +123,7 @@
   SizeOfAlignOfTypeExpr(bool issizeof, Type *ty) : isSizeof(issizeof), Ty(ty) {
   }
 
-  virtual void dump_impl() const;
-  virtual void visit(StmtVisitor *Visitor);
+  virtual void visit(StmtVisitor &Visitor);
 };
 
 //===----------------------------------------------------------------------===//
@@ -146,8 +136,7 @@
 public:
   ArraySubscriptExpr(Expr *base, Expr *idx) : Base(base), Idx(idx) {}
   
-  virtual void dump_impl() const;
-  virtual void visit(StmtVisitor *Visitor);
+  virtual void visit(StmtVisitor &Visitor);
 };
 
 
@@ -177,8 +166,7 @@
   /// this function call.
   unsigned getNumCommas() const { return NumArgs ? NumArgs - 1 : 0; }
   
-  virtual void dump_impl() const;
-  virtual void visit(StmtVisitor *Visitor);
+  virtual void visit(StmtVisitor &Visitor);
 };
 
 /// MemberExpr - [C99 6.5.2.3] Structure and Union Members.
@@ -191,8 +179,7 @@
   MemberExpr(Expr *base, bool isarrow, Decl *memberdecl) 
     : Base(base), MemberDecl(memberdecl), isArrow(isarrow) {
   }
-  virtual void dump_impl() const;
-  virtual void visit(StmtVisitor *Visitor);
+  virtual void visit(StmtVisitor &Visitor);
 };
 
 /// CastExpr - [C99 6.5.4] Cast Operators.
@@ -203,8 +190,7 @@
 public:
   CastExpr(Type *ty, Expr *op) : Ty(ty), Op(op) {}
   
-  virtual void dump_impl() const;
-  virtual void visit(StmtVisitor *Visitor);
+  virtual void visit(StmtVisitor &Visitor);
 };
 
 
@@ -238,8 +224,7 @@
   /// corresponds to, e.g. "<<=".
   static const char *getOpcodeStr(Opcode Op);
   
-  virtual void dump_impl() const;
-  virtual void visit(StmtVisitor *Visitor);
+  virtual void visit(StmtVisitor &Visitor);
 
 private:
   Expr *LHS, *RHS;
@@ -254,8 +239,7 @@
 public:
   ConditionalOperator(Expr *cond, Expr *lhs, Expr *rhs)
     : Cond(cond), LHS(lhs), RHS(rhs) {}
-  virtual void dump_impl() const;
-  virtual void visit(StmtVisitor *Visitor);
+  virtual void visit(StmtVisitor &Visitor);
 };
 
   

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

==============================================================================
--- cfe/cfe/trunk/include/clang/AST/Stmt.h (original)
+++ cfe/cfe/trunk/include/clang/AST/Stmt.h Wed Jul 11 11:27:35 2007
@@ -16,6 +16,7 @@
 
 #include "clang/Basic/SourceLocation.h"
 #include "llvm/ADT/SmallVector.h"
+#include <iosfwd>
 
 namespace llvm {
 namespace clang {
@@ -29,16 +30,11 @@
   Stmt() {}
   virtual ~Stmt() {}
   
-  // FIXME: Change to non-virtual method that uses visitor pattern to do this.
   void dump() const;
-  
-  // FIXME: move to isa/dyncast etc.
-  virtual bool isExpr() const { return false; }
+  void print(std::ostream &OS) const;
   
   // Implement visitor support.
-  virtual void visit(StmtVisitor *Visitor);
-private:
-  virtual void dump_impl() const = 0;
+  virtual void visit(StmtVisitor &Visitor);
 };
 
 /// CompoundStmt - This represents a group of statements like { stmt stmt }.
@@ -49,9 +45,11 @@
   CompoundStmt(Stmt **StmtStart, unsigned NumStmts)
     : Body(StmtStart, StmtStart+NumStmts) {}
   
-  virtual void dump_impl() const;
+  typedef SmallVector<Stmt*, 16>::iterator body_iterator;
+  body_iterator body_begin() { return Body.begin(); }
+  body_iterator body_end() { return Body.end(); }
   
-  virtual void visit(StmtVisitor *Visitor);
+  virtual void visit(StmtVisitor &Visitor);
 };
 
 /// IfStmt - This represents an if/then/else.
@@ -63,8 +61,15 @@
   IfStmt(Expr *cond, Stmt *then, Stmt *elsev = 0)
     : Cond(cond), Then(then), Else(elsev) {}
   
-  virtual void dump_impl() const;
-  virtual void visit(StmtVisitor *Visitor);
+  const Expr *getCond() const { return Cond; }
+  const Stmt *getThen() const { return Then; }
+  const Stmt *getElse() const { return Else; }
+
+  Expr *getCond() { return Cond; }
+  Stmt *getThen() { return Then; }
+  Stmt *getElse() { return Else; }
+  
+  virtual void visit(StmtVisitor &Visitor);
 };
 
 
@@ -76,8 +81,7 @@
 public:
   ReturnStmt(Expr *E = 0) : RetExpr(E) {}
   
-  virtual void dump_impl() const;
-  virtual void visit(StmtVisitor *Visitor);
+  virtual void visit(StmtVisitor &Visitor);
 };
 
 

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

==============================================================================
--- cfe/cfe/trunk/include/clang/AST/StmtVisitor.h (original)
+++ cfe/cfe/trunk/include/clang/AST/StmtVisitor.h Wed Jul 11 11:27:35 2007
@@ -38,7 +38,8 @@
   
 /// StmtVisitor - This class implements a simple visitor for Stmt subclasses.
 /// Since Expr derives from Stmt, this also includes support for visiting Exprs.
-struct StmtVisitor {
+class StmtVisitor {
+public:
   virtual ~StmtVisitor();
   
   /// VisitNull - Visit a null pointer.
@@ -50,8 +51,8 @@
   
   // Visitation methods for various Stmt subclasses.
   virtual void VisitCompoundStmt(CompoundStmt *Node);
-  virtual void VisitIfStmt(IfStmt       *Node);
-  virtual void VisitReturnStmt(ReturnStmt   *Node);
+  virtual void VisitIfStmt(IfStmt *Node);
+  virtual void VisitReturnStmt(ReturnStmt *Node);
   
   // Visitation methods for various Expr subclasses.
   virtual void VisitDeclRefExpr(DeclRefExpr *Node);





More information about the cfe-commits mailing list