implement ast nodes for while and do loops

llvm-svn: 39116
This commit is contained in:
Chris Lattner 2006-11-04 20:40:44 +00:00
parent 301cd76b8c
commit 85ed873bdc
9 changed files with 92 additions and 8 deletions

View File

@ -113,6 +113,17 @@ ASTBuilder::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
return new ForStmt((Stmt*)First, (Expr*)Second, (Expr*)Third, (Stmt*)Body);
}
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) {

View File

@ -61,6 +61,11 @@ public:
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,

View File

@ -23,6 +23,8 @@ void CLASS::visit(StmtVisitor &V) { return V.Visit##CLASS(this); }
MAKE_VISITOR(Stmt)
MAKE_VISITOR(CompoundStmt)
MAKE_VISITOR(IfStmt)
MAKE_VISITOR(WhileStmt)
MAKE_VISITOR(DoStmt)
MAKE_VISITOR(ForStmt)
MAKE_VISITOR(ReturnStmt)

View File

@ -57,7 +57,7 @@ namespace {
} else if (S) {
S->visit(*this);
} else {
Indent() << "<null stmt>\n";
Indent() << ";\n";
}
--IndentLevel;
}
@ -78,6 +78,8 @@ namespace {
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::VisitCompoundStmt(CompoundStmt *Node) {
}
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 @@ void StmtPrinter::VisitForStmt(ForStmt *Node) {
if (Node->getThird())
PrintExpr(Node->getThird());
OS << ")\n";
if (Node->getBody())
PrintStmt(Node->getBody());
else
Indent() << " ;";
PrintStmt(Node->getBody());
}
void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {

View File

@ -28,6 +28,8 @@ DELEGATE_VISITOR(Expr, Stmt)
// 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)

View File

@ -113,6 +113,17 @@ ASTBuilder::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
return new ForStmt((Stmt*)First, (Expr*)Second, (Expr*)Third, (Stmt*)Body);
}
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) {

View File

@ -61,6 +61,11 @@ public:
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,

View File

@ -72,6 +72,37 @@ public:
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 {

View File

@ -20,6 +20,8 @@ namespace clang {
class Expr;
class CompoundStmt;
class IfStmt;
class DoStmt;
class WhileStmt;
class ForStmt;
class ReturnStmt;
@ -49,6 +51,8 @@ public:
// 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);