implement ast nodes for while and do loops
llvm-svn: 39116
This commit is contained in:
parent
301cd76b8c
commit
85ed873bdc
|
@ -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) {
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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() << " ;";
|
||||
}
|
||||
|
||||
void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
Loading…
Reference in New Issue