[cfe-commits] r47582 - in /cfe/trunk: AST/ExprCXX.cpp AST/StmtPrinter.cpp Parse/ParseExpr.cpp Parse/ParseExprCXX.cpp Sema/Sema.h Sema/SemaExprCXX.cpp include/clang/AST/ExprCXX.h include/clang/AST/StmtNodes.def include/clang/Parse/Action.h include/clang/Parse/Parser.h
Chris Lattner
sabre at nondot.org
Mon Feb 25 16:51:45 PST 2008
Author: lattner
Date: Mon Feb 25 18:51:44 2008
New Revision: 47582
URL: http://llvm.org/viewvc/llvm-project?rev=47582&view=rev
Log:
add parsing, ast building and pretty printing support for C++ throw expressions.
Patch by Mike Stump!
Modified:
cfe/trunk/AST/ExprCXX.cpp
cfe/trunk/AST/StmtPrinter.cpp
cfe/trunk/Parse/ParseExpr.cpp
cfe/trunk/Parse/ParseExprCXX.cpp
cfe/trunk/Sema/Sema.h
cfe/trunk/Sema/SemaExprCXX.cpp
cfe/trunk/include/clang/AST/ExprCXX.h
cfe/trunk/include/clang/AST/StmtNodes.def
cfe/trunk/include/clang/Parse/Action.h
cfe/trunk/include/clang/Parse/Parser.h
Modified: cfe/trunk/AST/ExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/AST/ExprCXX.cpp?rev=47582&r1=47581&r2=47582&view=diff
==============================================================================
--- cfe/trunk/AST/ExprCXX.cpp (original)
+++ cfe/trunk/AST/ExprCXX.cpp Mon Feb 25 18:51:44 2008
@@ -23,7 +23,6 @@
Stmt::child_iterator CXXCastExpr::child_begin() {
return reinterpret_cast<Stmt**>(&Op);
}
-
Stmt::child_iterator CXXCastExpr::child_end() {
return reinterpret_cast<Stmt**>(&Op)+1;
}
@@ -35,3 +34,14 @@
Stmt::child_iterator CXXBoolLiteralExpr::child_end() {
return child_iterator();
}
+
+// CXXThrowExpr
+Stmt::child_iterator CXXThrowExpr::child_begin() {
+ return reinterpret_cast<Stmt**>(&Op);
+}
+Stmt::child_iterator CXXThrowExpr::child_end() {
+ // If Op is 0, we are processing throw; which has no children.
+ if (Op == 0)
+ return reinterpret_cast<Stmt**>(&Op)+0;
+ return reinterpret_cast<Stmt**>(&Op)+1;
+}
Modified: cfe/trunk/AST/StmtPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/AST/StmtPrinter.cpp?rev=47582&r1=47581&r2=47582&view=diff
==============================================================================
--- cfe/trunk/AST/StmtPrinter.cpp (original)
+++ cfe/trunk/AST/StmtPrinter.cpp Mon Feb 25 18:51:44 2008
@@ -780,6 +780,15 @@
OS << (Node->getValue() ? "true" : "false");
}
+void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
+ if (Node->getSubExpr() == 0)
+ OS << "throw";
+ else {
+ OS << "throw ";
+ PrintExpr(Node->getSubExpr());
+ }
+}
+
// Obj-C
void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
Modified: cfe/trunk/Parse/ParseExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Parse/ParseExpr.cpp?rev=47582&r1=47581&r2=47582&view=diff
==============================================================================
--- cfe/trunk/Parse/ParseExpr.cpp (original)
+++ cfe/trunk/Parse/ParseExpr.cpp Mon Feb 25 18:51:44 2008
@@ -157,6 +157,7 @@
/// assignment-expression: [C99 6.5.16]
/// conditional-expression
/// unary-expression assignment-operator assignment-expression
+/// [C++] throw-expression [C++ 15]
///
/// assignment-operator: one of
/// = *= /= %= += -= <<= >>= &= ^= |=
@@ -166,6 +167,9 @@
/// expression ',' assignment-expression
///
Parser::ExprResult Parser::ParseExpression() {
+ if (Tok.is(tok::kw_throw))
+ return ParseThrowExpression();
+
ExprResult LHS = ParseCastExpression(false);
if (LHS.isInvalid) return LHS;
@@ -187,6 +191,9 @@
/// ParseAssignmentExpression - Parse an expr that doesn't include commas.
///
Parser::ExprResult Parser::ParseAssignmentExpression() {
+ if (Tok.is(tok::kw_throw))
+ return ParseThrowExpression();
+
ExprResult LHS = ParseCastExpression(false);
if (LHS.isInvalid) return LHS;
Modified: cfe/trunk/Parse/ParseExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Parse/ParseExprCXX.cpp?rev=47582&r1=47581&r2=47582&view=diff
==============================================================================
--- cfe/trunk/Parse/ParseExprCXX.cpp (original)
+++ cfe/trunk/Parse/ParseExprCXX.cpp Mon Feb 25 18:51:44 2008
@@ -76,3 +76,24 @@
tok::TokenKind Kind = Tok.getKind();
return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
}
+
+/// ParseThrowExpression - This handles the C++ throw expression.
+///
+/// throw-expression: [C++ 15]
+/// 'throw' assignment-expression[opt]
+Parser::ExprResult Parser::ParseThrowExpression() {
+ assert(Tok.is(tok::kw_throw) && "Not throw!");
+
+ ExprResult Expr;
+
+ SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
+ // FIXME: Anything that isn't an assignment-expression should bail out now.
+ if (Tok.is(tok::semi) || Tok.is(tok::r_paren) || Tok.is(tok::colon) ||
+ Tok.is(tok::comma))
+ return Actions.ActOnCXXThrow(ThrowLoc);
+
+ Expr = ParseAssignmentExpression();
+ if (!Expr.isInvalid)
+ Expr = Actions.ActOnCXXThrow(ThrowLoc, Expr.Val);
+ return Expr;
+}
Modified: cfe/trunk/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/Sema.h?rev=47582&r1=47581&r2=47582&view=diff
==============================================================================
--- cfe/trunk/Sema/Sema.h (original)
+++ cfe/trunk/Sema/Sema.h Mon Feb 25 18:51:44 2008
@@ -510,6 +510,10 @@
virtual ExprResult ActOnCXXBoolLiteral(SourceLocation OpLoc,
tok::TokenKind Kind);
+ //// ActOnCXXThrow - Parse throw expressions.
+ virtual ExprResult ActOnCXXThrow(SourceLocation OpLoc,
+ ExprTy *expr);
+
// ParseObjCStringLiteral - Parse Objective-C string literals.
virtual ExprResult ParseObjCStringLiteral(SourceLocation *AtLocs,
ExprTy **Strings,
Modified: cfe/trunk/Sema/SemaExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/SemaExprCXX.cpp?rev=47582&r1=47581&r2=47582&view=diff
==============================================================================
--- cfe/trunk/Sema/SemaExprCXX.cpp (original)
+++ cfe/trunk/Sema/SemaExprCXX.cpp Mon Feb 25 18:51:44 2008
@@ -43,3 +43,9 @@
"Unknown C++ Boolean value!");
return new CXXBoolLiteralExpr(Kind == tok::kw_true, Context.BoolTy, OpLoc);
}
+
+/// ActOnCXXThrow - Parse throw expressions.
+Action::ExprResult
+Sema::ActOnCXXThrow(SourceLocation OpLoc, ExprTy *E) {
+ return new CXXThrowExpr((Expr*)E, Context.VoidTy, OpLoc);
+}
Modified: cfe/trunk/include/clang/AST/ExprCXX.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ExprCXX.h?rev=47582&r1=47581&r2=47582&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/ExprCXX.h (original)
+++ cfe/trunk/include/clang/AST/ExprCXX.h Mon Feb 25 18:51:44 2008
@@ -95,6 +95,38 @@
virtual child_iterator child_end();
};
+ /// CXXThrowExpr - [C++ 15] C++ Throw Expression. This handles
+ /// 'throw' and 'throw' assignment-expression. When
+ /// assignment-expression isn't present, Op will be null.
+ ///
+ class CXXThrowExpr : public Expr {
+ Expr *Op;
+ SourceLocation ThrowLoc;
+ public:
+ // Ty is the void type which is used as the result type of the
+ // exepression. The l is the location of the throw keyword. expr
+ // can by null, if the optional expression to throw isn't present.
+ CXXThrowExpr(Expr *expr, QualType Ty, SourceLocation l) :
+ Expr(CXXThrowExprClass, Ty), Op(expr), ThrowLoc(l) {}
+ const Expr *getSubExpr() const { return Op; }
+ Expr *getSubExpr() { return Op; }
+
+ virtual SourceRange getSourceRange() const {
+ if (getSubExpr() == 0)
+ return SourceRange(ThrowLoc, ThrowLoc);
+ return SourceRange(ThrowLoc, getSubExpr()->getSourceRange().getEnd());
+ }
+
+ static bool classof(const Stmt *T) {
+ return T->getStmtClass() == CXXThrowExprClass;
+ }
+ static bool classof(const CXXThrowExpr *) { return true; }
+
+ // Iterators
+ virtual child_iterator child_begin();
+ virtual child_iterator child_end();
+ };
+
} // end namespace clang
#endif
Modified: cfe/trunk/include/clang/AST/StmtNodes.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/StmtNodes.def?rev=47582&r1=47581&r2=47582&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/StmtNodes.def (original)
+++ cfe/trunk/include/clang/AST/StmtNodes.def Mon Feb 25 18:51:44 2008
@@ -90,6 +90,7 @@
// C++ Expressions.
STMT(60, CXXCastExpr , Expr)
STMT(61, CXXBoolLiteralExpr , Expr)
+STMT(62, CXXThrowExpr , Expr)
// Obj-C Expressions.
STMT(70, ObjCStringLiteral , Expr)
Modified: cfe/trunk/include/clang/Parse/Action.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/Action.h?rev=47582&r1=47581&r2=47582&view=diff
==============================================================================
--- cfe/trunk/include/clang/Parse/Action.h (original)
+++ cfe/trunk/include/clang/Parse/Action.h Mon Feb 25 18:51:44 2008
@@ -519,6 +519,12 @@
tok::TokenKind Kind) {
return 0;
}
+
+ /// ActOnCXXThrow - Parse throw expressions.
+ virtual ExprResult ActOnCXXThrow(SourceLocation OpLoc,
+ ExprTy *Op = 0) {
+ return 0;
+ }
//===----------------------- Obj-C Declarations -------------------------===//
// ActOnStartClassInterface - this action is called immdiately after parsing
Modified: cfe/trunk/include/clang/Parse/Parser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/Parser.h?rev=47582&r1=47581&r2=47582&view=diff
==============================================================================
--- cfe/trunk/include/clang/Parse/Parser.h (original)
+++ cfe/trunk/include/clang/Parse/Parser.h Mon Feb 25 18:51:44 2008
@@ -354,6 +354,10 @@
ExprResult ParseCXXCasts();
//===--------------------------------------------------------------------===//
+ // C++ 15: C++ Throw Expression
+ ExprResult ParseThrowExpression();
+
+ //===--------------------------------------------------------------------===//
// C++ 2.13.5: C++ Boolean Literals
ExprResult ParseCXXBoolLiteral();
More information about the cfe-commits
mailing list