blob: 52447f8688cd4d74bda8ecd67d518c7e56ede1a8 [file] [log] [blame]
Chris Lattner29375652006-12-04 18:06:35 +00001//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Bill Wendling and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation for C++.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ExprCXX.h"
15#include "clang/Basic/Diagnostic.h"
16#include "clang/Parse/Parser.h"
17using namespace llvm;
18using namespace clang;
19
20/// ParseCXXCasts - This handles the various ways to cast expressions to another
21/// type.
22///
23/// postfix-expression: [C++ 5.2p1]
24/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
25/// 'static_cast' '<' type-name '>' '(' expression ')'
26/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
27/// 'const_cast' '<' type-name '>' '(' expression ')'
28///
29Parser::ExprResult Parser::ParseCXXCasts() {
30 tok::TokenKind Kind = Tok.getKind();
31 const char *CastName = 0; // For error messages
32
33 switch (Kind) {
34 default: assert(0 && "Unknown C++ cast!"); abort();
35 case tok::kw_const_cast: CastName = "const_cast"; break;
36 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
37 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
38 case tok::kw_static_cast: CastName = "static_cast"; break;
39 }
40
41 SourceLocation OpLoc = ConsumeToken();
42 SourceLocation LAngleBracketLoc = Tok.getLocation();
43
44 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
45 return ExprResult(true);
46
47 TypeTy *CastTy = ParseTypeName();
48 SourceLocation RAngleBracketLoc = Tok.getLocation();
49
50 if (ExpectAndConsume(tok::greater, diag::err_expected_greater)) {
51 Diag(LAngleBracketLoc, diag::err_matching, "<");
52 return ExprResult(true);
53 }
54
55 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
56
57 if (Tok.getKind() != tok::l_paren) {
58 Diag(Tok, diag::err_expected_lparen_after, CastName);
59 return ExprResult(true);
60 }
61
62 ExprResult Result = ParseSimpleParenExpression(RParenLoc);
63
64 if (!Result.isInvalid)
65 Result = Actions.ParseCXXCasts(OpLoc, Kind,
66 LAngleBracketLoc, CastTy, RAngleBracketLoc,
67 LParenLoc, Result.Val, RParenLoc);
68
69 return Result;
70}
Bill Wendling4073ed52007-02-13 01:51:42 +000071
72/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
73///
74/// boolean-literal: [C++ 2.13.5]
75/// 'true'
76/// 'false'
77Parser::ExprResult Parser::ParseCXXBoolLiteral() {
78 tok::TokenKind Kind = Tok.getKind();
79 return Actions.ParseCXXBoolLiteral(ConsumeToken(), Kind);
80}