1 //===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Expression parsing implementation for C++.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Basic/Diagnostic.h"
15 #include "clang/Parse/Parser.h"
16 using namespace clang;
18 /// ParseCXXCasts - This handles the various ways to cast expressions to another
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 ')'
27 Parser::ExprResult Parser::ParseCXXCasts() {
28 tok::TokenKind Kind = Tok.getKind();
29 const char *CastName = 0; // For error messages
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;
39 SourceLocation OpLoc = ConsumeToken();
40 SourceLocation LAngleBracketLoc = Tok.getLocation();
42 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
43 return ExprResult(true);
45 TypeTy *CastTy = ParseTypeName();
46 SourceLocation RAngleBracketLoc = Tok.getLocation();
48 if (ExpectAndConsume(tok::greater, diag::err_expected_greater)) {
49 Diag(LAngleBracketLoc, diag::err_matching, "<");
50 return ExprResult(true);
53 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
55 if (Tok.isNot(tok::l_paren)) {
56 Diag(Tok, diag::err_expected_lparen_after, CastName);
57 return ExprResult(true);
60 ExprResult Result = ParseSimpleParenExpression(RParenLoc);
62 if (!Result.isInvalid)
63 Result = Actions.ActOnCXXCasts(OpLoc, Kind,
64 LAngleBracketLoc, CastTy, RAngleBracketLoc,
65 LParenLoc, Result.Val, RParenLoc);
70 /// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
72 /// boolean-literal: [C++ 2.13.5]
75 Parser::ExprResult Parser::ParseCXXBoolLiteral() {
76 tok::TokenKind Kind = Tok.getKind();
77 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);