blob: 3134ff87b60b4a59536df1d6f88e508c843b443d [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +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"
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +000016#include "clang/Parse/DeclSpec.h"
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner4d7d2342007-10-09 17:41:39 +000056 if (Tok.isNot(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +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)
Douglas Gregor21a04f32008-10-27 19:41:14 +000064 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
65 LAngleBracketLoc, CastTy, RAngleBracketLoc,
66 LParenLoc, Result.Val, RParenLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000067
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 Naroff5cbb02f2007-09-16 14:56:35 +000078 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Chris Lattner4b009652007-07-25 00:24:17 +000079}
Chris Lattnera7447ba2008-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 Lattnera7447ba2008-02-26 00:51:44 +000087 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Chris Lattnera00d0392008-04-06 06:03:03 +000088
Chris Lattner6b8842d2008-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 Lattnera7447ba2008-02-26 00:51:44 +000099 return Actions.ActOnCXXThrow(ThrowLoc);
100
Chris Lattner6b8842d2008-04-06 06:02:23 +0000101 default:
Chris Lattnera00d0392008-04-06 06:03:03 +0000102 ExprResult Expr = ParseAssignmentExpression();
Chris Lattner6b8842d2008-04-06 06:02:23 +0000103 if (Expr.isInvalid) return Expr;
104 return Actions.ActOnCXXThrow(ThrowLoc, Expr.Val);
105 }
Chris Lattnera7447ba2008-02-26 00:51:44 +0000106}
Argiris Kirtzidis9d784332008-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();
Argiris Kirtzidis8f8a52a2008-08-16 19:34:46 +0000116 return Actions.ActOnCXXThis(ThisLoc);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000117}
Argiris Kirtzidis7a1e7412008-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
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000156/// ParseCXXCondition - if/switch/while/for condition expression.
157///
158/// condition:
159/// expression
160/// type-specifier-seq declarator '=' assignment-expression
161/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
162/// '=' assignment-expression
163///
164Parser::ExprResult Parser::ParseCXXCondition() {
Argiris Kirtzidis88527fb2008-10-05 15:03:47 +0000165 if (!isCXXConditionDeclaration())
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000166 return ParseExpression(); // expression
167
168 SourceLocation StartLoc = Tok.getLocation();
169
170 // type-specifier-seq
171 DeclSpec DS;
172 ParseSpecifierQualifierList(DS);
173
174 // declarator
175 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
176 ParseDeclarator(DeclaratorInfo);
177
178 // simple-asm-expr[opt]
179 if (Tok.is(tok::kw_asm)) {
180 ExprResult AsmLabel = ParseSimpleAsm();
181 if (AsmLabel.isInvalid) {
182 SkipUntil(tok::semi);
183 return true;
184 }
185 DeclaratorInfo.setAsmLabel(AsmLabel.Val);
186 }
187
188 // If attributes are present, parse them.
189 if (Tok.is(tok::kw___attribute))
190 DeclaratorInfo.AddAttributes(ParseAttributes());
191
192 // '=' assignment-expression
193 if (Tok.isNot(tok::equal))
194 return Diag(Tok, diag::err_expected_equal_after_declarator);
195 SourceLocation EqualLoc = ConsumeToken();
196 ExprResult AssignExpr = ParseAssignmentExpression();
197 if (AssignExpr.isInvalid)
198 return true;
199
200 return Actions.ActOnCXXConditionDeclarationExpr(CurScope, StartLoc,
201 DeclaratorInfo,
202 EqualLoc, AssignExpr.Val);
203}
204
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000205/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
206/// This should only be called when the current token is known to be part of
207/// simple-type-specifier.
208///
209/// simple-type-specifier:
210/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
211/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
212/// char
213/// wchar_t
214/// bool
215/// short
216/// int
217/// long
218/// signed
219/// unsigned
220/// float
221/// double
222/// void
223/// [GNU] typeof-specifier
224/// [C++0x] auto [TODO]
225///
226/// type-name:
227/// class-name
228/// enum-name
229/// typedef-name
230///
231void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
232 DS.SetRangeStart(Tok.getLocation());
233 const char *PrevSpec;
234 SourceLocation Loc = Tok.getLocation();
235
236 switch (Tok.getKind()) {
237 default:
238 assert(0 && "Not a simple-type-specifier token!");
239 abort();
240
241 // type-name
242 case tok::identifier: {
243 TypeTy *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
244 assert(TypeRep && "Identifier wasn't a type-name!");
245 DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec, TypeRep);
246 break;
247 }
248
249 // builtin types
250 case tok::kw_short:
251 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
252 break;
253 case tok::kw_long:
254 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
255 break;
256 case tok::kw_signed:
257 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
258 break;
259 case tok::kw_unsigned:
260 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
261 break;
262 case tok::kw_void:
263 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
264 break;
265 case tok::kw_char:
266 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
267 break;
268 case tok::kw_int:
269 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
270 break;
271 case tok::kw_float:
272 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
273 break;
274 case tok::kw_double:
275 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
276 break;
277 case tok::kw_wchar_t:
278 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
279 break;
280 case tok::kw_bool:
281 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
282 break;
283
284 // GNU typeof support.
285 case tok::kw_typeof:
286 ParseTypeofSpecifier(DS);
287 DS.Finish(Diags, PP.getSourceManager(), getLang());
288 return;
289 }
290 DS.SetRangeEnd(Tok.getLocation());
291 ConsumeToken();
292 DS.Finish(Diags, PP.getSourceManager(), getLang());
293}
Douglas Gregore60e5d32008-11-06 22:13:31 +0000294
295/// MaybeParseOperatorFunctionId - Attempts to parse a C++ overloaded
296/// operator name (C++ [over.oper]). If successful, returns the
297/// predefined identifier that corresponds to that overloaded
298/// operator. Otherwise, returns NULL and does not consume any tokens.
299///
300/// operator-function-id: [C++ 13.5]
301/// 'operator' operator
302///
303/// operator: one of
304/// new delete new[] delete[]
305/// + - * / % ^ & | ~
306/// ! = < > += -= *= /= %=
307/// ^= &= |= << >> >>= <<= == !=
308/// <= >= && || ++ -- , ->* ->
309/// () []
310IdentifierInfo *Parser::MaybeParseOperatorFunctionId() {
Argiris Kirtzidisa9d57b62008-11-07 15:54:02 +0000311 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
Douglas Gregore60e5d32008-11-06 22:13:31 +0000312
313 OverloadedOperatorKind Op = OO_None;
314 switch (NextToken().getKind()) {
315 case tok::kw_new:
316 ConsumeToken(); // 'operator'
317 ConsumeToken(); // 'new'
318 if (Tok.is(tok::l_square)) {
319 ConsumeBracket(); // '['
320 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
321 Op = OO_Array_New;
322 } else {
323 Op = OO_New;
324 }
325 return &PP.getIdentifierTable().getOverloadedOperator(Op);
326
327 case tok::kw_delete:
328 ConsumeToken(); // 'operator'
329 ConsumeToken(); // 'delete'
330 if (Tok.is(tok::l_square)) {
331 ConsumeBracket(); // '['
332 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
333 Op = OO_Array_Delete;
334 } else {
335 Op = OO_Delete;
336 }
337 return &PP.getIdentifierTable().getOverloadedOperator(Op);
338
339#define OVERLOADED_OPERATOR(Name,Spelling,Token) \
340 case tok::Token: Op = OO_##Name; break;
341#define OVERLOADED_OPERATOR_MULTI(Name,Spelling)
342#include "clang/Basic/OperatorKinds.def"
343
344 case tok::l_paren:
345 ConsumeToken(); // 'operator'
346 ConsumeParen(); // '('
347 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); // ')'
348 return &PP.getIdentifierTable().getOverloadedOperator(OO_Call);
349
350 case tok::l_square:
351 ConsumeToken(); // 'operator'
352 ConsumeBracket(); // '['
353 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
354 return &PP.getIdentifierTable().getOverloadedOperator(OO_Subscript);
355
356 default:
357 break;
358 }
359
360 if (Op == OO_None)
361 return 0;
362 else {
Argiris Kirtzidisa9d57b62008-11-07 15:54:02 +0000363 ConsumeToken(); // 'operator'
Douglas Gregore60e5d32008-11-06 22:13:31 +0000364 ConsumeAnyToken(); // the operator itself
365 return &PP.getIdentifierTable().getOverloadedOperator(Op);
366 }
367}