[cfe-commits] r39116 - in /cfe/cfe/trunk: AST/Sema.cpp AST/Sema.h AST/Stmt.cpp AST/StmtPrinter.cpp AST/StmtVisitor.cpp Sema/Sema.cpp Sema/Sema.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:40 PDT 2007


Author: sabre
Date: Wed Jul 11 11:27:40 2007
New Revision: 39116

URL: http://llvm.org/viewvc/llvm-project?rev=39116&view=rev
Log:
implement ast nodes for while and do loops

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

Modified: cfe/cfe/trunk/AST/Sema.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/AST/Sema.cpp?rev=39116&r1=39115&r2=39116&view=diff

==============================================================================
--- cfe/cfe/trunk/AST/Sema.cpp (original)
+++ cfe/cfe/trunk/AST/Sema.cpp Wed Jul 11 11:27:40 2007
@@ -114,6 +114,17 @@
 }
 
 Action::StmtResult
+ASTBuilder::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body){
+  return new WhileStmt((Expr*)Cond, (Stmt*)Body);
+}
+
+Action::StmtResult
+ASTBuilder::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
+                        SourceLocation WhileLoc, ExprTy *Cond) {
+  return new DoStmt((Stmt*)Body, (Expr*)Cond);
+}
+
+Action::StmtResult
 ASTBuilder::ParseReturnStmt(SourceLocation ReturnLoc,
                             ExprTy *RetValExp) {
   return new ReturnStmt((Expr*)RetValExp);

Modified: cfe/cfe/trunk/AST/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/AST/Sema.h?rev=39116&r1=39115&r2=39116&view=diff

==============================================================================
--- cfe/cfe/trunk/AST/Sema.h (original)
+++ cfe/cfe/trunk/AST/Sema.h Wed Jul 11 11:27:40 2007
@@ -61,6 +61,11 @@
                                  StmtTy *ThenVal, SourceLocation ElseLoc,
                                  StmtTy *ElseVal);
   
+  virtual StmtResult ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond,
+                                    StmtTy *Body);
+  virtual StmtResult ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
+                                 SourceLocation WhileLoc, ExprTy *Cond);
+  
   virtual StmtResult ParseForStmt(SourceLocation ForLoc, 
                                   SourceLocation LParenLoc, 
                                   StmtTy *First, ExprTy *Second, ExprTy *Third,

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

==============================================================================
--- cfe/cfe/trunk/AST/Stmt.cpp (original)
+++ cfe/cfe/trunk/AST/Stmt.cpp Wed Jul 11 11:27:40 2007
@@ -23,6 +23,8 @@
 MAKE_VISITOR(Stmt)
 MAKE_VISITOR(CompoundStmt)
 MAKE_VISITOR(IfStmt)
+MAKE_VISITOR(WhileStmt)
+MAKE_VISITOR(DoStmt)
 MAKE_VISITOR(ForStmt)
 MAKE_VISITOR(ReturnStmt)
 

Modified: cfe/cfe/trunk/AST/StmtPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/AST/StmtPrinter.cpp?rev=39116&r1=39115&r2=39116&view=diff

==============================================================================
--- cfe/cfe/trunk/AST/StmtPrinter.cpp (original)
+++ cfe/cfe/trunk/AST/StmtPrinter.cpp Wed Jul 11 11:27:40 2007
@@ -57,7 +57,7 @@
       } else if (S) {
         S->visit(*this);
       } else {
-        Indent() << "<null stmt>\n";
+        Indent() << ";\n";
       }
       --IndentLevel;
     }
@@ -78,6 +78,8 @@
     virtual void VisitStmt(Stmt *Node);
     virtual void VisitCompoundStmt(CompoundStmt *Node);
     virtual void VisitIfStmt(IfStmt *Node);
+    virtual void VisitWhileStmt(WhileStmt *Node);
+    virtual void VisitDoStmt(DoStmt *Node);
     virtual void VisitForStmt(ForStmt *Node);
     virtual void VisitReturnStmt(ReturnStmt *Node);
 
@@ -117,16 +119,30 @@
 }
 
 void StmtPrinter::VisitIfStmt(IfStmt *If) {
-  Indent() << "if ";
+  Indent() << "if (";
   PrintExpr(If->getCond());
 
-  OS << " then\n";
+  OS << ")\n";
   PrintStmt(If->getThen());
   if (If->getElse()) {
     Indent() << "else\n";
     PrintStmt(If->getElse());
   }
-  Indent() << "endif\n";
+}
+
+void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
+  Indent() << "while (";
+  PrintExpr(Node->getCond());
+  OS << ")\n";
+  PrintStmt(Node->getBody());
+}
+
+void StmtPrinter::VisitDoStmt(DoStmt *Node) {
+  Indent() << "do\n";
+  PrintStmt(Node->getBody());
+  Indent() << "while (";
+  PrintExpr(Node->getCond());
+  OS << ")\n";
 }
 
 void StmtPrinter::VisitForStmt(ForStmt *Node) {
@@ -140,10 +156,7 @@
   if (Node->getThird())
     PrintExpr(Node->getThird());
   OS << ")\n";
-  if (Node->getBody())
-    PrintStmt(Node->getBody());
-  else
-    Indent() << "  ;";
+  PrintStmt(Node->getBody());
 }
 
 void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {

Modified: cfe/cfe/trunk/AST/StmtVisitor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/AST/StmtVisitor.cpp?rev=39116&r1=39115&r2=39116&view=diff

==============================================================================
--- cfe/cfe/trunk/AST/StmtVisitor.cpp (original)
+++ cfe/cfe/trunk/AST/StmtVisitor.cpp Wed Jul 11 11:27:40 2007
@@ -28,6 +28,8 @@
 // Stmt subclasses to Stmt.
 DELEGATE_VISITOR(CompoundStmt, Stmt)
 DELEGATE_VISITOR(IfStmt      , Stmt)
+DELEGATE_VISITOR(WhileStmt   , Stmt)
+DELEGATE_VISITOR(DoStmt      , Stmt)
 DELEGATE_VISITOR(ForStmt     , Stmt)
 DELEGATE_VISITOR(ReturnStmt  , Stmt)
 

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

==============================================================================
--- cfe/cfe/trunk/Sema/Sema.cpp (original)
+++ cfe/cfe/trunk/Sema/Sema.cpp Wed Jul 11 11:27:40 2007
@@ -114,6 +114,17 @@
 }
 
 Action::StmtResult
+ASTBuilder::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body){
+  return new WhileStmt((Expr*)Cond, (Stmt*)Body);
+}
+
+Action::StmtResult
+ASTBuilder::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
+                        SourceLocation WhileLoc, ExprTy *Cond) {
+  return new DoStmt((Stmt*)Body, (Expr*)Cond);
+}
+
+Action::StmtResult
 ASTBuilder::ParseReturnStmt(SourceLocation ReturnLoc,
                             ExprTy *RetValExp) {
   return new ReturnStmt((Expr*)RetValExp);

Modified: cfe/cfe/trunk/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Sema/Sema.h?rev=39116&r1=39115&r2=39116&view=diff

==============================================================================
--- cfe/cfe/trunk/Sema/Sema.h (original)
+++ cfe/cfe/trunk/Sema/Sema.h Wed Jul 11 11:27:40 2007
@@ -61,6 +61,11 @@
                                  StmtTy *ThenVal, SourceLocation ElseLoc,
                                  StmtTy *ElseVal);
   
+  virtual StmtResult ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond,
+                                    StmtTy *Body);
+  virtual StmtResult ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
+                                 SourceLocation WhileLoc, ExprTy *Cond);
+  
   virtual StmtResult ParseForStmt(SourceLocation ForLoc, 
                                   SourceLocation LParenLoc, 
                                   StmtTy *First, ExprTy *Second, ExprTy *Third,

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=39116&r1=39115&r2=39116&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/AST/Stmt.h (original)
+++ cfe/cfe/trunk/include/clang/AST/Stmt.h Wed Jul 11 11:27:40 2007
@@ -72,6 +72,37 @@
   virtual void visit(StmtVisitor &Visitor);
 };
 
+/// WhileStmt - This represents a 'while' stmt.
+///
+class WhileStmt : public Stmt {
+  Expr *Cond;
+  Stmt *Body;
+public:
+  WhileStmt(Expr *cond, Stmt *body)
+    : Cond(cond), Body(body) {}
+  
+  Expr *getCond() { return Cond; }
+  Stmt *getBody() { return Body; }
+  
+  virtual void visit(StmtVisitor &Visitor);
+};
+
+/// DoStmt - This represents a 'do/while' stmt.
+///
+class DoStmt : public Stmt {
+  Stmt *Body;
+  Expr *Cond;
+public:
+  DoStmt(Stmt *body, Expr *cond)
+    : Body(body), Cond(cond) {}
+  
+  Stmt *getBody() { return Body; }
+  Expr *getCond() { return Cond; }
+  
+  virtual void visit(StmtVisitor &Visitor);
+};
+
+
 /// ForStmt - This represents a 'for' stmt.
 ///
 class ForStmt : public Stmt {

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=39116&r1=39115&r2=39116&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/AST/StmtVisitor.h (original)
+++ cfe/cfe/trunk/include/clang/AST/StmtVisitor.h Wed Jul 11 11:27:40 2007
@@ -20,6 +20,8 @@
   class Expr;
   class CompoundStmt;
   class IfStmt;
+  class DoStmt;
+  class WhileStmt;
   class ForStmt;
   class ReturnStmt;
 
@@ -49,6 +51,8 @@
   // Visitation methods for various Stmt subclasses.
   virtual void VisitCompoundStmt(CompoundStmt *Node);
   virtual void VisitIfStmt(IfStmt *Node);
+  virtual void VisitWhileStmt(WhileStmt *Node);
+  virtual void VisitDoStmt(DoStmt *Node);
   virtual void VisitForStmt(ForStmt *Node);
   virtual void VisitReturnStmt(ReturnStmt *Node);
   





More information about the cfe-commits mailing list