blob: bd8e7d510ea7995446f08394b59c5041ad2e12e3 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
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"
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +000016#include "clang/Parse/DeclSpec.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017using namespace clang;
18
19/// ParseCXXCasts - This handles the various ways to cast expressions to another
20/// type.
21///
22/// postfix-expression: [C++ 5.2p1]
23/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
24/// 'static_cast' '<' type-name '>' '(' expression ')'
25/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
26/// 'const_cast' '<' type-name '>' '(' expression ')'
27///
28Parser::ExprResult Parser::ParseCXXCasts() {
29 tok::TokenKind Kind = Tok.getKind();
30 const char *CastName = 0; // For error messages
31
32 switch (Kind) {
33 default: assert(0 && "Unknown C++ cast!"); abort();
34 case tok::kw_const_cast: CastName = "const_cast"; break;
35 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
36 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
37 case tok::kw_static_cast: CastName = "static_cast"; break;
38 }
39
40 SourceLocation OpLoc = ConsumeToken();
41 SourceLocation LAngleBracketLoc = Tok.getLocation();
42
43 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
44 return ExprResult(true);
45
46 TypeTy *CastTy = ParseTypeName();
47 SourceLocation RAngleBracketLoc = Tok.getLocation();
48
49 if (ExpectAndConsume(tok::greater, diag::err_expected_greater)) {
50 Diag(LAngleBracketLoc, diag::err_matching, "<");
51 return ExprResult(true);
52 }
53
54 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
55
Chris Lattner4e1d99a2007-10-09 17:41:39 +000056 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000057 Diag(Tok, diag::err_expected_lparen_after, CastName);
58 return ExprResult(true);
59 }
60
61 ExprResult Result = ParseSimpleParenExpression(RParenLoc);
62
63 if (!Result.isInvalid)
Steve Naroff1b273c42007-09-16 14:56:35 +000064 Result = Actions.ActOnCXXCasts(OpLoc, Kind,
Reid Spencer5f016e22007-07-11 17:01:13 +000065 LAngleBracketLoc, CastTy, RAngleBracketLoc,
66 LParenLoc, Result.Val, RParenLoc);
67
68 return Result;
69}
70
71/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
72///
73/// boolean-literal: [C++ 2.13.5]
74/// 'true'
75/// 'false'
76Parser::ExprResult Parser::ParseCXXBoolLiteral() {
77 tok::TokenKind Kind = Tok.getKind();
Steve Naroff1b273c42007-09-16 14:56:35 +000078 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +000079}
Chris Lattner50dd2892008-02-26 00:51:44 +000080
81/// ParseThrowExpression - This handles the C++ throw expression.
82///
83/// throw-expression: [C++ 15]
84/// 'throw' assignment-expression[opt]
85Parser::ExprResult Parser::ParseThrowExpression() {
86 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +000087 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Chris Lattner3e3d3102008-04-06 06:03:03 +000088
Chris Lattner2a2819a2008-04-06 06:02:23 +000089 // If the current token isn't the start of an assignment-expression,
90 // then the expression is not present. This handles things like:
91 // "C ? throw : (void)42", which is crazy but legal.
92 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
93 case tok::semi:
94 case tok::r_paren:
95 case tok::r_square:
96 case tok::r_brace:
97 case tok::colon:
98 case tok::comma:
Chris Lattner50dd2892008-02-26 00:51:44 +000099 return Actions.ActOnCXXThrow(ThrowLoc);
100
Chris Lattner2a2819a2008-04-06 06:02:23 +0000101 default:
Chris Lattner3e3d3102008-04-06 06:03:03 +0000102 ExprResult Expr = ParseAssignmentExpression();
Chris Lattner2a2819a2008-04-06 06:02:23 +0000103 if (Expr.isInvalid) return Expr;
104 return Actions.ActOnCXXThrow(ThrowLoc, Expr.Val);
105 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000106}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000107
108/// ParseCXXThis - This handles the C++ 'this' pointer.
109///
110/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
111/// a non-lvalue expression whose value is the address of the object for which
112/// the function is called.
113Parser::ExprResult Parser::ParseCXXThis() {
114 assert(Tok.is(tok::kw_this) && "Not 'this'!");
115 SourceLocation ThisLoc = ConsumeToken();
Argyrios Kyrtzidis289d7732008-08-16 19:34:46 +0000116 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000117}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000118
119/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
120/// Can be interpreted either as function-style casting ("int(x)")
121/// or class type construction ("ClassType(x,y,z)")
122/// or creation of a value-initialized type ("int()").
123///
124/// postfix-expression: [C++ 5.2p1]
125/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
126/// typename-specifier '(' expression-list[opt] ')' [TODO]
127///
128Parser::ExprResult Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
129 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
130 TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).Val;
131
132 assert(Tok.is(tok::l_paren) && "Expected '('!");
133 SourceLocation LParenLoc = ConsumeParen();
134
135 ExprListTy Exprs;
136 CommaLocsTy CommaLocs;
137
138 if (Tok.isNot(tok::r_paren)) {
139 if (ParseExpressionList(Exprs, CommaLocs)) {
140 SkipUntil(tok::r_paren);
141 return ExprResult(true);
142 }
143 }
144
145 // Match the ')'.
146 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
147
148 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
149 "Unexpected number of commas!");
150 return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
151 LParenLoc,
152 &Exprs[0], Exprs.size(),
153 &CommaLocs[0], RParenLoc);
154}
155
156/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
157/// This should only be called when the current token is known to be part of
158/// simple-type-specifier.
159///
160/// simple-type-specifier:
161/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
162/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
163/// char
164/// wchar_t
165/// bool
166/// short
167/// int
168/// long
169/// signed
170/// unsigned
171/// float
172/// double
173/// void
174/// [GNU] typeof-specifier
175/// [C++0x] auto [TODO]
176///
177/// type-name:
178/// class-name
179/// enum-name
180/// typedef-name
181///
182void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
183 DS.SetRangeStart(Tok.getLocation());
184 const char *PrevSpec;
185 SourceLocation Loc = Tok.getLocation();
186
187 switch (Tok.getKind()) {
188 default:
189 assert(0 && "Not a simple-type-specifier token!");
190 abort();
191
192 // type-name
193 case tok::identifier: {
194 TypeTy *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
195 assert(TypeRep && "Identifier wasn't a type-name!");
196 DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec, TypeRep);
197 break;
198 }
199
200 // builtin types
201 case tok::kw_short:
202 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
203 break;
204 case tok::kw_long:
205 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
206 break;
207 case tok::kw_signed:
208 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
209 break;
210 case tok::kw_unsigned:
211 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
212 break;
213 case tok::kw_void:
214 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
215 break;
216 case tok::kw_char:
217 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
218 break;
219 case tok::kw_int:
220 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
221 break;
222 case tok::kw_float:
223 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
224 break;
225 case tok::kw_double:
226 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
227 break;
228 case tok::kw_wchar_t:
229 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
230 break;
231 case tok::kw_bool:
232 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
233 break;
234
235 // GNU typeof support.
236 case tok::kw_typeof:
237 ParseTypeofSpecifier(DS);
238 DS.Finish(Diags, PP.getSourceManager(), getLang());
239 return;
240 }
241 DS.SetRangeEnd(Tok.getLocation());
242 ConsumeToken();
243 DS.Finish(Diags, PP.getSourceManager(), getLang());
244}