Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Expression parsing implementation for C++. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Basic/Diagnostic.h" |
| 15 | #include "clang/Parse/Parser.h" |
| 16 | using namespace clang; |
| 17 | |
| 18 | /// ParseCXXCasts - This handles the various ways to cast expressions to another |
| 19 | /// type. |
| 20 | /// |
| 21 | /// postfix-expression: [C++ 5.2p1] |
| 22 | /// 'dynamic_cast' '<' type-name '>' '(' expression ')' |
| 23 | /// 'static_cast' '<' type-name '>' '(' expression ')' |
| 24 | /// 'reinterpret_cast' '<' type-name '>' '(' expression ')' |
| 25 | /// 'const_cast' '<' type-name '>' '(' expression ')' |
| 26 | /// |
| 27 | Parser::ExprResult Parser::ParseCXXCasts() { |
| 28 | tok::TokenKind Kind = Tok.getKind(); |
| 29 | const char *CastName = 0; // For error messages |
| 30 | |
| 31 | switch (Kind) { |
| 32 | default: assert(0 && "Unknown C++ cast!"); abort(); |
| 33 | case tok::kw_const_cast: CastName = "const_cast"; break; |
| 34 | case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break; |
| 35 | case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break; |
| 36 | case tok::kw_static_cast: CastName = "static_cast"; break; |
| 37 | } |
| 38 | |
| 39 | SourceLocation OpLoc = ConsumeToken(); |
| 40 | SourceLocation LAngleBracketLoc = Tok.getLocation(); |
| 41 | |
| 42 | if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName)) |
| 43 | return ExprResult(true); |
| 44 | |
| 45 | TypeTy *CastTy = ParseTypeName(); |
| 46 | SourceLocation RAngleBracketLoc = Tok.getLocation(); |
| 47 | |
| 48 | if (ExpectAndConsume(tok::greater, diag::err_expected_greater)) { |
| 49 | Diag(LAngleBracketLoc, diag::err_matching, "<"); |
| 50 | return ExprResult(true); |
| 51 | } |
| 52 | |
| 53 | SourceLocation LParenLoc = Tok.getLocation(), RParenLoc; |
| 54 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 55 | if (Tok.isNot(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 56 | Diag(Tok, diag::err_expected_lparen_after, CastName); |
| 57 | return ExprResult(true); |
| 58 | } |
| 59 | |
| 60 | ExprResult Result = ParseSimpleParenExpression(RParenLoc); |
| 61 | |
| 62 | if (!Result.isInvalid) |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 63 | Result = Actions.ActOnCXXCasts(OpLoc, Kind, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 64 | LAngleBracketLoc, CastTy, RAngleBracketLoc, |
| 65 | LParenLoc, Result.Val, RParenLoc); |
| 66 | |
| 67 | return Result; |
| 68 | } |
| 69 | |
| 70 | /// ParseCXXBoolLiteral - This handles the C++ Boolean literals. |
| 71 | /// |
| 72 | /// boolean-literal: [C++ 2.13.5] |
| 73 | /// 'true' |
| 74 | /// 'false' |
| 75 | Parser::ExprResult Parser::ParseCXXBoolLiteral() { |
| 76 | tok::TokenKind Kind = Tok.getKind(); |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 77 | return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 78 | } |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 79 | |
| 80 | /// ParseThrowExpression - This handles the C++ throw expression. |
| 81 | /// |
| 82 | /// throw-expression: [C++ 15] |
| 83 | /// 'throw' assignment-expression[opt] |
| 84 | Parser::ExprResult Parser::ParseThrowExpression() { |
| 85 | assert(Tok.is(tok::kw_throw) && "Not throw!"); |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 86 | SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token. |
Chris Lattner | 3e3d310 | 2008-04-06 06:03:03 +0000 | [diff] [blame] | 87 | |
Chris Lattner | 2a2819a | 2008-04-06 06:02:23 +0000 | [diff] [blame] | 88 | // If the current token isn't the start of an assignment-expression, |
| 89 | // then the expression is not present. This handles things like: |
| 90 | // "C ? throw : (void)42", which is crazy but legal. |
| 91 | switch (Tok.getKind()) { // FIXME: move this predicate somewhere common. |
| 92 | case tok::semi: |
| 93 | case tok::r_paren: |
| 94 | case tok::r_square: |
| 95 | case tok::r_brace: |
| 96 | case tok::colon: |
| 97 | case tok::comma: |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 98 | return Actions.ActOnCXXThrow(ThrowLoc); |
| 99 | |
Chris Lattner | 2a2819a | 2008-04-06 06:02:23 +0000 | [diff] [blame] | 100 | default: |
Chris Lattner | 3e3d310 | 2008-04-06 06:03:03 +0000 | [diff] [blame] | 101 | ExprResult Expr = ParseAssignmentExpression(); |
Chris Lattner | 2a2819a | 2008-04-06 06:02:23 +0000 | [diff] [blame] | 102 | if (Expr.isInvalid) return Expr; |
| 103 | return Actions.ActOnCXXThrow(ThrowLoc, Expr.Val); |
| 104 | } |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 105 | } |