[cfe-commits] r39113 - in /cfe/cfe/trunk: AST/Sema.cpp AST/Sema.h AST/Stmt.cpp AST/StmtPrinter.cpp AST/StmtVisitor.cpp Driver/clang.cpp Parse/ParseStmt.cpp Parse/Parser.cpp Sema/Sema.cpp Sema/Sema.h include/clang/AST/Stmt.h include/clang/AST/StmtVisitor.h include/clang/Parse/Action.h include/clang/Parse/Parser.h
sabre at cs.uiuc.edu
sabre at cs.uiuc.edu
Wed Jul 11 09:27:38 PDT 2007
Author: sabre
Date: Wed Jul 11 11:27:38 2007
New Revision: 39113
URL: http://llvm.org/viewvc/llvm-project?rev=39113&view=rev
Log:
Add AST node, AST building, actions, and printing for 'for' stmts.
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/Driver/clang.cpp
cfe/cfe/trunk/Parse/ParseStmt.cpp
cfe/cfe/trunk/Parse/Parser.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
cfe/cfe/trunk/include/clang/Parse/Action.h
cfe/cfe/trunk/include/clang/Parse/Parser.h
Modified: cfe/cfe/trunk/AST/Sema.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/AST/Sema.cpp?rev=39113&r1=39112&r2=39113&view=diff
==============================================================================
--- cfe/cfe/trunk/AST/Sema.cpp (original)
+++ cfe/cfe/trunk/AST/Sema.cpp Wed Jul 11 11:27:38 2007
@@ -106,6 +106,13 @@
return new IfStmt((Expr*)CondVal, (Stmt*)ThenVal, (Stmt*)ElseVal);
}
+Action::StmtResult
+ASTBuilder::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
+ StmtTy *First, ExprTy *Second, ExprTy *Third,
+ SourceLocation RParenLoc, StmtTy *Body) {
+ return new ForStmt((Stmt*)First, (Expr*)Second, (Expr*)Third, (Stmt*)Body);
+}
+
Action::StmtResult
ASTBuilder::ParseReturnStmt(SourceLocation ReturnLoc,
ExprTy *RetValExp) {
Modified: cfe/cfe/trunk/AST/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/AST/Sema.h?rev=39113&r1=39112&r2=39113&view=diff
==============================================================================
--- cfe/cfe/trunk/AST/Sema.h (original)
+++ cfe/cfe/trunk/AST/Sema.h Wed Jul 11 11:27:38 2007
@@ -61,6 +61,11 @@
StmtTy *ThenVal, SourceLocation ElseLoc,
StmtTy *ElseVal);
+ virtual StmtResult ParseForStmt(SourceLocation ForLoc,
+ SourceLocation LParenLoc,
+ StmtTy *First, ExprTy *Second, ExprTy *Third,
+ SourceLocation RParenLoc, StmtTy *Body);
+
virtual StmtResult ParseReturnStmt(SourceLocation ReturnLoc,
ExprTy *RetValExp);
Modified: cfe/cfe/trunk/AST/Stmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/AST/Stmt.cpp?rev=39113&r1=39112&r2=39113&view=diff
==============================================================================
--- cfe/cfe/trunk/AST/Stmt.cpp (original)
+++ cfe/cfe/trunk/AST/Stmt.cpp Wed Jul 11 11:27:38 2007
@@ -23,6 +23,7 @@
MAKE_VISITOR(Stmt)
MAKE_VISITOR(CompoundStmt)
MAKE_VISITOR(IfStmt)
+MAKE_VISITOR(ForStmt)
MAKE_VISITOR(ReturnStmt)
#undef MAKE_VISITOR
Modified: cfe/cfe/trunk/AST/StmtPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/AST/StmtPrinter.cpp?rev=39113&r1=39112&r2=39113&view=diff
==============================================================================
--- cfe/cfe/trunk/AST/StmtPrinter.cpp (original)
+++ cfe/cfe/trunk/AST/StmtPrinter.cpp Wed Jul 11 11:27:38 2007
@@ -78,6 +78,7 @@
virtual void VisitStmt(Stmt *Node);
virtual void VisitCompoundStmt(CompoundStmt *Node);
virtual void VisitIfStmt(IfStmt *Node);
+ virtual void VisitForStmt(ForStmt *Node);
virtual void VisitReturnStmt(ReturnStmt *Node);
virtual void VisitExpr(Expr *Node);
@@ -94,9 +95,13 @@
virtual void VisitCastExpr(CastExpr *Node);
virtual void VisitBinaryOperator(BinaryOperator *Node);
virtual void VisitConditionalOperator(ConditionalOperator *Node);
-};
+ };
}
+//===----------------------------------------------------------------------===//
+// Stmt printing methods.
+//===----------------------------------------------------------------------===//
+
void StmtPrinter::VisitStmt(Stmt *Node) {
Indent() << "<<unknown stmt type>>\n";
}
@@ -124,6 +129,23 @@
Indent() << "endif\n";
}
+void StmtPrinter::VisitForStmt(ForStmt *Node) {
+ Indent() << "for (";
+ if (Node->getFirst())
+ PrintExpr((Expr*)Node->getFirst());
+ OS << "; ";
+ if (Node->getSecond())
+ PrintExpr(Node->getSecond());
+ OS << "; ";
+ if (Node->getThird())
+ PrintExpr(Node->getThird());
+ OS << ")\n";
+ if (Node->getBody())
+ PrintStmt(Node->getBody());
+ else
+ Indent() << " ;";
+}
+
void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
Indent() << "return";
if (Node->getRetValue()) {
@@ -133,6 +155,9 @@
OS << "\n";
}
+//===----------------------------------------------------------------------===//
+// Expr printing methods.
+//===----------------------------------------------------------------------===//
void StmtPrinter::VisitExpr(Expr *Node) {
OS << "<<unknown expr type>>";
Modified: cfe/cfe/trunk/AST/StmtVisitor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/AST/StmtVisitor.cpp?rev=39113&r1=39112&r2=39113&view=diff
==============================================================================
--- cfe/cfe/trunk/AST/StmtVisitor.cpp (original)
+++ cfe/cfe/trunk/AST/StmtVisitor.cpp Wed Jul 11 11:27:38 2007
@@ -28,6 +28,7 @@
// Stmt subclasses to Stmt.
DELEGATE_VISITOR(CompoundStmt, Stmt)
DELEGATE_VISITOR(IfStmt , Stmt)
+DELEGATE_VISITOR(ForStmt , Stmt)
DELEGATE_VISITOR(ReturnStmt , Stmt)
// Expr subclasses to Expr.
Modified: cfe/cfe/trunk/Driver/clang.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Driver/clang.cpp?rev=39113&r1=39112&r2=39113&view=diff
==============================================================================
--- cfe/cfe/trunk/Driver/clang.cpp (original)
+++ cfe/cfe/trunk/Driver/clang.cpp Wed Jul 11 11:27:38 2007
@@ -794,8 +794,10 @@
else
std::cerr << "\n";
if (FunctionDecl *FD = D->isFunctionDecl()) {
- FD->getBody()->dump();
- std::cerr << "\n";
+ if (FD->getBody()) {
+ FD->getBody()->dump();
+ std::cerr << "\n";
+ }
}
}
Modified: cfe/cfe/trunk/Parse/ParseStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Parse/ParseStmt.cpp?rev=39113&r1=39112&r2=39113&view=diff
==============================================================================
--- cfe/cfe/trunk/Parse/ParseStmt.cpp (original)
+++ cfe/cfe/trunk/Parse/ParseStmt.cpp Wed Jul 11 11:27:38 2007
@@ -525,9 +525,11 @@
}
SourceLocation LParenLoc = ConsumeParen();
-
ExprResult Value;
+ StmtTy *FirstPart = 0;
+ ExprTy *SecondPart = 0, *ThirdPart = 0;
+
// Parse the first part of the for specifier.
if (Tok.getKind() == tok::semi) { // for (;
// no first part, eat the ';'.
@@ -537,9 +539,18 @@
if (!getLang().C99) // Use of C99-style for loops in C90 mode?
Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
ParseDeclaration(Declarator::ForContext);
+ // FIXME: Turn declaration into stmt.
+ FirstPart = 0;
} else {
Value = ParseExpression();
-
+
+ // Turn the expression into a stmt.
+ if (!Value.isInvalid) {
+ StmtResult R = Actions.ParseExprStmt(Value.Val);
+ if (!R.isInvalid)
+ FirstPart = R.Val;
+ }
+
if (Tok.getKind() == tok::semi) {
ConsumeToken();
} else {
@@ -554,6 +565,8 @@
Value = ExprResult();
} else {
Value = ParseExpression();
+ if (!Value.isInvalid)
+ SecondPart = Value.Val;
}
if (Tok.getKind() == tok::semi) {
@@ -569,16 +582,20 @@
Value = ExprResult();
} else {
Value = ParseExpression();
+ if (!Value.isInvalid)
+ ThirdPart = Value.Val;
}
// Match the ')'.
- MatchRHSPunctuation(tok::r_paren, LParenLoc);
+ SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
// Read the body statement.
- ParseStatement();
+ StmtResult Body = ParseStatement();
+ if (Body.isInvalid)
+ return Body;
- // FIXME: ACTION FOR FOR STMT.
- return false;
+ return Actions.ParseForStmt(ForLoc, LParenLoc, FirstPart, SecondPart,
+ ThirdPart, RParenLoc, Body.Val);
}
/// ParseGotoStatement
Modified: cfe/cfe/trunk/Parse/Parser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Parse/Parser.cpp?rev=39113&r1=39112&r2=39113&view=diff
==============================================================================
--- cfe/cfe/trunk/Parse/Parser.cpp (original)
+++ cfe/cfe/trunk/Parse/Parser.cpp Wed Jul 11 11:27:38 2007
@@ -44,23 +44,25 @@
/// present. If not present, it emits the specified diagnostic indicating
/// that the parser failed to match the RHS of the token at LHSLoc. LHSName
/// should be the name of the unmatched LHS token.
-void Parser::MatchRHSPunctuation(tok::TokenKind RHSTok, SourceLocation LHSLoc) {
+SourceLocation Parser::MatchRHSPunctuation(tok::TokenKind RHSTok,
+ SourceLocation LHSLoc) {
- if (Tok.getKind() == RHSTok) {
- ConsumeAnyToken();
- } else {
- const char *LHSName = "unknown";
- diag::kind DID = diag::err_parse_error;
- switch (RHSTok) {
- default: break;
- case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break;
- case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break;
- case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break;
- }
- Diag(Tok, DID);
- Diag(LHSLoc, diag::err_matching, LHSName);
- SkipUntil(RHSTok);
- }
+ if (Tok.getKind() == RHSTok)
+ return ConsumeAnyToken();
+
+ SourceLocation R = Tok.getLocation();
+ const char *LHSName = "unknown";
+ diag::kind DID = diag::err_parse_error;
+ switch (RHSTok) {
+ default: break;
+ case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break;
+ case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break;
+ case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break;
+ }
+ Diag(Tok, DID);
+ Diag(LHSLoc, diag::err_matching, LHSName);
+ SkipUntil(RHSTok);
+ return R;
}
/// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the
Modified: cfe/cfe/trunk/Sema/Sema.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Sema/Sema.cpp?rev=39113&r1=39112&r2=39113&view=diff
==============================================================================
--- cfe/cfe/trunk/Sema/Sema.cpp (original)
+++ cfe/cfe/trunk/Sema/Sema.cpp Wed Jul 11 11:27:38 2007
@@ -106,6 +106,13 @@
return new IfStmt((Expr*)CondVal, (Stmt*)ThenVal, (Stmt*)ElseVal);
}
+Action::StmtResult
+ASTBuilder::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
+ StmtTy *First, ExprTy *Second, ExprTy *Third,
+ SourceLocation RParenLoc, StmtTy *Body) {
+ return new ForStmt((Stmt*)First, (Expr*)Second, (Expr*)Third, (Stmt*)Body);
+}
+
Action::StmtResult
ASTBuilder::ParseReturnStmt(SourceLocation ReturnLoc,
ExprTy *RetValExp) {
Modified: cfe/cfe/trunk/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Sema/Sema.h?rev=39113&r1=39112&r2=39113&view=diff
==============================================================================
--- cfe/cfe/trunk/Sema/Sema.h (original)
+++ cfe/cfe/trunk/Sema/Sema.h Wed Jul 11 11:27:38 2007
@@ -61,6 +61,11 @@
StmtTy *ThenVal, SourceLocation ElseLoc,
StmtTy *ElseVal);
+ virtual StmtResult ParseForStmt(SourceLocation ForLoc,
+ SourceLocation LParenLoc,
+ StmtTy *First, ExprTy *Second, ExprTy *Third,
+ SourceLocation RParenLoc, StmtTy *Body);
+
virtual StmtResult ParseReturnStmt(SourceLocation ReturnLoc,
ExprTy *RetValExp);
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=39113&r1=39112&r2=39113&view=diff
==============================================================================
--- cfe/cfe/trunk/include/clang/AST/Stmt.h (original)
+++ cfe/cfe/trunk/include/clang/AST/Stmt.h Wed Jul 11 11:27:38 2007
@@ -72,6 +72,24 @@
virtual void visit(StmtVisitor &Visitor);
};
+/// ForStmt - This represents a 'for' stmt.
+///
+class ForStmt : public Stmt {
+ Stmt *First; // Expression or decl.
+ Expr *Second, *Third;
+ Stmt *Body;
+public:
+ ForStmt(Stmt *first, Expr *second, Expr *third, Stmt *body)
+ : First(first), Second(second), Third(third), Body(body) {}
+
+ Stmt *getFirst() { return First; }
+ Expr *getSecond() { return Second; }
+ Expr *getThird() { return Third; }
+ Stmt *getBody() { return Body; }
+
+ virtual void visit(StmtVisitor &Visitor);
+};
+
/// ReturnStmt - This represents a return, optionally of an expression.
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=39113&r1=39112&r2=39113&view=diff
==============================================================================
--- cfe/cfe/trunk/include/clang/AST/StmtVisitor.h (original)
+++ cfe/cfe/trunk/include/clang/AST/StmtVisitor.h Wed Jul 11 11:27:38 2007
@@ -20,6 +20,7 @@
class Expr;
class CompoundStmt;
class IfStmt;
+ class ForStmt;
class ReturnStmt;
class DeclRefExpr;
@@ -48,6 +49,7 @@
// Visitation methods for various Stmt subclasses.
virtual void VisitCompoundStmt(CompoundStmt *Node);
virtual void VisitIfStmt(IfStmt *Node);
+ virtual void VisitForStmt(ForStmt *Node);
virtual void VisitReturnStmt(ReturnStmt *Node);
// Visitation methods for various Expr subclasses.
Modified: cfe/cfe/trunk/include/clang/Parse/Action.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Parse/Action.h?rev=39113&r1=39112&r2=39113&view=diff
==============================================================================
--- cfe/cfe/trunk/include/clang/Parse/Action.h (original)
+++ cfe/cfe/trunk/include/clang/Parse/Action.h Wed Jul 11 11:27:38 2007
@@ -157,14 +157,19 @@
return 0;
}
virtual StmtResult ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond,
- StmtTy *Body) {
+ StmtTy *Body) {
return 0;
}
virtual StmtResult ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
SourceLocation WhileLoc, ExprTy *Cond) {
return 0;
}
- // PARSE FOR STMT.
+ virtual StmtResult ParseForStmt(SourceLocation ForLoc,
+ SourceLocation LParenLoc,
+ StmtTy *First, ExprTy *Second, ExprTy *Third,
+ SourceLocation RParenLoc, StmtTy *Body) {
+ return 0;
+ }
virtual StmtResult ParseGotoStmt(SourceLocation GotoLoc,
SourceLocation LabelLoc,
IdentifierInfo *LabelII) {
Modified: cfe/cfe/trunk/include/clang/Parse/Parser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Parse/Parser.h?rev=39113&r1=39112&r2=39113&view=diff
==============================================================================
--- cfe/cfe/trunk/include/clang/Parse/Parser.h (original)
+++ cfe/cfe/trunk/include/clang/Parse/Parser.h Wed Jul 11 11:27:38 2007
@@ -111,15 +111,15 @@
/// ConsumeAnyToken - Dispatch to the right Consume* method based on the
/// current token type. This should only be used in cases where the type of
/// the token really isn't known, e.g. in error recovery.
- void ConsumeAnyToken() {
+ SourceLocation ConsumeAnyToken() {
if (isTokenParen())
- ConsumeParen();
+ return ConsumeParen();
else if (isTokenBracket())
- ConsumeBracket();
+ return ConsumeBracket();
else if (isTokenBrace())
- ConsumeBrace();
+ return ConsumeBrace();
else
- ConsumeToken();
+ return ConsumeToken();
}
/// ConsumeParen - This consume method keeps the paren count up-to-date.
@@ -180,8 +180,10 @@
/// this helper function matches and consumes the specified RHS token if
/// present. If not present, it emits the specified diagnostic indicating
/// that the parser failed to match the RHS of the token at LHSLoc. LHSName
- /// should be the name of the unmatched LHS token.
- void MatchRHSPunctuation(tok::TokenKind RHSTok, SourceLocation LHSLoc);
+ /// should be the name of the unmatched LHS token. This returns the location
+ /// of the consumed token.
+ SourceLocation MatchRHSPunctuation(tok::TokenKind RHSTok,
+ SourceLocation LHSLoc);
/// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the
/// input. If so, it is consumed and false is returned.
More information about the cfe-commits
mailing list