blob: 19b521b264191231c7a1b062b1fcee1d98f56693 [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
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +000019/// ParseCXXScopeSpecifier - Parse global scope or nested-name-specifier.
20///
21/// '::'[opt] nested-name-specifier
22/// '::'
23///
24/// nested-name-specifier:
25/// type-name '::'
26/// namespace-name '::'
27/// nested-name-specifier identifier '::'
28/// nested-name-specifier 'template'[opt] simple-template-id '::' [TODO]
29///
30void Parser::ParseCXXScopeSpecifier(CXXScopeSpec &SS) {
31 assert(isTokenCXXScopeSpecifier() && "Not scope specifier!");
32
33 if (Tok.is(tok::annot_cxxscope)) {
34 SS.setScopeRep(Tok.getAnnotationValue());
35 SS.setRange(Tok.getAnnotationRange());
36 ConsumeToken();
37 return;
38 }
39
40 SS.setBeginLoc(Tok.getLocation());
41
42 // '::'
43
44 if (Tok.is(tok::coloncolon)) {
45 // Global scope.
46 SourceLocation CCLoc = ConsumeToken();
47 SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, CCLoc));
48 SS.setEndLoc(CCLoc);
49 }
50
51 // nested-name-specifier:
52 // type-name '::'
53 // namespace-name '::'
54 // nested-name-specifier identifier '::'
55 // nested-name-specifier 'template'[opt] simple-template-id '::' [TODO]
56
57 while (Tok.is(tok::identifier) && NextToken().is(tok::coloncolon)) {
58 IdentifierInfo *II = Tok.getIdentifierInfo();
59 SourceLocation IdLoc = ConsumeToken();
60 assert(Tok.is(tok::coloncolon) &&
61 "NextToken() not working properly!");
62 SourceLocation CCLoc = ConsumeToken();
63 if (SS.isInvalid())
64 continue;
65
66 SS.setScopeRep(
67 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, *II) );
68 SS.setEndLoc(CCLoc);
69 }
70}
71
72/// ParseCXXIdExpression - Handle id-expression.
73///
74/// id-expression:
75/// unqualified-id
76/// qualified-id
77///
78/// unqualified-id:
79/// identifier
80/// operator-function-id
81/// conversion-function-id [TODO]
82/// '~' class-name [TODO]
83/// template-id [TODO]
84///
85/// qualified-id:
86/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
87/// '::' identifier
88/// '::' operator-function-id
89/// '::' template-id [TODO]
90///
91/// nested-name-specifier:
92/// type-name '::'
93/// namespace-name '::'
94/// nested-name-specifier identifier '::'
95/// nested-name-specifier 'template'[opt] simple-template-id '::' [TODO]
96///
97/// NOTE: The standard specifies that, for qualified-id, the parser does not
98/// expect:
99///
100/// '::' conversion-function-id
101/// '::' '~' class-name
102///
103/// This may cause a slight inconsistency on diagnostics:
104///
105/// class C {};
106/// namespace A {}
107/// void f() {
108/// :: A :: ~ C(); // Some Sema error about using destructor with a
109/// // namespace.
110/// :: ~ C(); // Some Parser error like 'unexpected ~'.
111/// }
112///
113/// We simplify the parser a bit and make it work like:
114///
115/// qualified-id:
116/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
117/// '::' unqualified-id
118///
119/// That way Sema can handle and report similar errors for namespaces and the
120/// global scope.
121///
122Parser::ExprResult Parser::ParseCXXIdExpression() {
123 // qualified-id:
124 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
125 // '::' unqualified-id
126 //
127 CXXScopeSpec SS;
128 if (isTokenCXXScopeSpecifier())
129 ParseCXXScopeSpecifier(SS);
130
131 // unqualified-id:
132 // identifier
133 // operator-function-id
Douglas Gregorb0212bd2008-11-17 20:34:05 +0000134 // conversion-function-id
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000135 // '~' class-name [TODO]
136 // template-id [TODO]
137 //
138 switch (Tok.getKind()) {
139 default:
140 return Diag(Tok, diag::err_expected_unqualified_id);
141
142 case tok::identifier: {
143 // Consume the identifier so that we can see if it is followed by a '('.
144 IdentifierInfo &II = *Tok.getIdentifierInfo();
145 SourceLocation L = ConsumeToken();
146 return Actions.ActOnIdentifierExpr(CurScope, L, II,
147 Tok.is(tok::l_paren), &SS);
148 }
149
150 case tok::kw_operator: {
151 SourceLocation OperatorLoc = Tok.getLocation();
Douglas Gregor682a8cf2008-11-17 16:14:12 +0000152 if (IdentifierInfo *II = TryParseOperatorFunctionId()) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000153 return Actions.ActOnIdentifierExpr(CurScope, OperatorLoc, *II,
154 Tok.is(tok::l_paren), &SS);
Douglas Gregorb0212bd2008-11-17 20:34:05 +0000155 } else if (TypeTy *Type = ParseConversionFunctionId()) {
156 return Actions.ActOnConversionFunctionExpr(CurScope, OperatorLoc,
157 Type, Tok.is(tok::l_paren),
158 &SS);
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000159 }
Douglas Gregorb0212bd2008-11-17 20:34:05 +0000160
161 // We already complained about a bad conversion-function-id,
162 // above.
163 return true;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000164 }
165
166 } // switch.
167
168 assert(0 && "The switch was supposed to take care everything.");
169}
170
Chris Lattner4b009652007-07-25 00:24:17 +0000171/// ParseCXXCasts - This handles the various ways to cast expressions to another
172/// type.
173///
174/// postfix-expression: [C++ 5.2p1]
175/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
176/// 'static_cast' '<' type-name '>' '(' expression ')'
177/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
178/// 'const_cast' '<' type-name '>' '(' expression ')'
179///
180Parser::ExprResult Parser::ParseCXXCasts() {
181 tok::TokenKind Kind = Tok.getKind();
182 const char *CastName = 0; // For error messages
183
184 switch (Kind) {
185 default: assert(0 && "Unknown C++ cast!"); abort();
186 case tok::kw_const_cast: CastName = "const_cast"; break;
187 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
188 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
189 case tok::kw_static_cast: CastName = "static_cast"; break;
190 }
191
192 SourceLocation OpLoc = ConsumeToken();
193 SourceLocation LAngleBracketLoc = Tok.getLocation();
194
195 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
196 return ExprResult(true);
197
198 TypeTy *CastTy = ParseTypeName();
199 SourceLocation RAngleBracketLoc = Tok.getLocation();
200
201 if (ExpectAndConsume(tok::greater, diag::err_expected_greater)) {
202 Diag(LAngleBracketLoc, diag::err_matching, "<");
203 return ExprResult(true);
204 }
205
206 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
207
Chris Lattner4d7d2342007-10-09 17:41:39 +0000208 if (Tok.isNot(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000209 Diag(Tok, diag::err_expected_lparen_after, CastName);
210 return ExprResult(true);
211 }
212
213 ExprResult Result = ParseSimpleParenExpression(RParenLoc);
214
215 if (!Result.isInvalid)
Douglas Gregor21a04f32008-10-27 19:41:14 +0000216 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
217 LAngleBracketLoc, CastTy, RAngleBracketLoc,
218 LParenLoc, Result.Val, RParenLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000219
220 return Result;
221}
222
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000223/// ParseCXXTypeid - This handles the C++ typeid expression.
224///
225/// postfix-expression: [C++ 5.2p1]
226/// 'typeid' '(' expression ')'
227/// 'typeid' '(' type-id ')'
228///
229Parser::ExprResult Parser::ParseCXXTypeid() {
230 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
231
232 SourceLocation OpLoc = ConsumeToken();
233 SourceLocation LParenLoc = Tok.getLocation();
234 SourceLocation RParenLoc;
235
236 // typeid expressions are always parenthesized.
237 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
238 "typeid"))
239 return ExprResult(true);
240
241 Parser::ExprResult Result;
242
243 if (isTypeIdInParens()) {
244 TypeTy *Ty = ParseTypeName();
245
246 // Match the ')'.
247 MatchRHSPunctuation(tok::r_paren, LParenLoc);
248
249 if (!Ty)
250 return ExprResult(true);
251
252 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
253 Ty, RParenLoc);
254 } else {
255 Result = ParseExpression();
256
257 // Match the ')'.
258 if (Result.isInvalid)
259 SkipUntil(tok::r_paren);
260 else {
261 MatchRHSPunctuation(tok::r_paren, LParenLoc);
262
263 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
264 Result.Val, RParenLoc);
265 }
266 }
267
268 return Result;
269}
270
Chris Lattner4b009652007-07-25 00:24:17 +0000271/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
272///
273/// boolean-literal: [C++ 2.13.5]
274/// 'true'
275/// 'false'
276Parser::ExprResult Parser::ParseCXXBoolLiteral() {
277 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000278 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Chris Lattner4b009652007-07-25 00:24:17 +0000279}
Chris Lattnera7447ba2008-02-26 00:51:44 +0000280
281/// ParseThrowExpression - This handles the C++ throw expression.
282///
283/// throw-expression: [C++ 15]
284/// 'throw' assignment-expression[opt]
285Parser::ExprResult Parser::ParseThrowExpression() {
286 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattnera7447ba2008-02-26 00:51:44 +0000287 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Chris Lattnera00d0392008-04-06 06:03:03 +0000288
Chris Lattner6b8842d2008-04-06 06:02:23 +0000289 // If the current token isn't the start of an assignment-expression,
290 // then the expression is not present. This handles things like:
291 // "C ? throw : (void)42", which is crazy but legal.
292 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
293 case tok::semi:
294 case tok::r_paren:
295 case tok::r_square:
296 case tok::r_brace:
297 case tok::colon:
298 case tok::comma:
Chris Lattnera7447ba2008-02-26 00:51:44 +0000299 return Actions.ActOnCXXThrow(ThrowLoc);
300
Chris Lattner6b8842d2008-04-06 06:02:23 +0000301 default:
Chris Lattnera00d0392008-04-06 06:03:03 +0000302 ExprResult Expr = ParseAssignmentExpression();
Chris Lattner6b8842d2008-04-06 06:02:23 +0000303 if (Expr.isInvalid) return Expr;
304 return Actions.ActOnCXXThrow(ThrowLoc, Expr.Val);
305 }
Chris Lattnera7447ba2008-02-26 00:51:44 +0000306}
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000307
308/// ParseCXXThis - This handles the C++ 'this' pointer.
309///
310/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
311/// a non-lvalue expression whose value is the address of the object for which
312/// the function is called.
313Parser::ExprResult Parser::ParseCXXThis() {
314 assert(Tok.is(tok::kw_this) && "Not 'this'!");
315 SourceLocation ThisLoc = ConsumeToken();
Argiris Kirtzidis8f8a52a2008-08-16 19:34:46 +0000316 return Actions.ActOnCXXThis(ThisLoc);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000317}
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000318
319/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
320/// Can be interpreted either as function-style casting ("int(x)")
321/// or class type construction ("ClassType(x,y,z)")
322/// or creation of a value-initialized type ("int()").
323///
324/// postfix-expression: [C++ 5.2p1]
325/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
326/// typename-specifier '(' expression-list[opt] ')' [TODO]
327///
328Parser::ExprResult Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
329 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
330 TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).Val;
331
332 assert(Tok.is(tok::l_paren) && "Expected '('!");
333 SourceLocation LParenLoc = ConsumeParen();
334
335 ExprListTy Exprs;
336 CommaLocsTy CommaLocs;
337
338 if (Tok.isNot(tok::r_paren)) {
339 if (ParseExpressionList(Exprs, CommaLocs)) {
340 SkipUntil(tok::r_paren);
341 return ExprResult(true);
342 }
343 }
344
345 // Match the ')'.
346 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
347
348 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
349 "Unexpected number of commas!");
350 return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
351 LParenLoc,
352 &Exprs[0], Exprs.size(),
353 &CommaLocs[0], RParenLoc);
354}
355
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000356/// ParseCXXCondition - if/switch/while/for condition expression.
357///
358/// condition:
359/// expression
360/// type-specifier-seq declarator '=' assignment-expression
361/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
362/// '=' assignment-expression
363///
364Parser::ExprResult Parser::ParseCXXCondition() {
Argiris Kirtzidis88527fb2008-10-05 15:03:47 +0000365 if (!isCXXConditionDeclaration())
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000366 return ParseExpression(); // expression
367
368 SourceLocation StartLoc = Tok.getLocation();
369
370 // type-specifier-seq
371 DeclSpec DS;
372 ParseSpecifierQualifierList(DS);
373
374 // declarator
375 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
376 ParseDeclarator(DeclaratorInfo);
377
378 // simple-asm-expr[opt]
379 if (Tok.is(tok::kw_asm)) {
380 ExprResult AsmLabel = ParseSimpleAsm();
381 if (AsmLabel.isInvalid) {
382 SkipUntil(tok::semi);
383 return true;
384 }
385 DeclaratorInfo.setAsmLabel(AsmLabel.Val);
386 }
387
388 // If attributes are present, parse them.
389 if (Tok.is(tok::kw___attribute))
390 DeclaratorInfo.AddAttributes(ParseAttributes());
391
392 // '=' assignment-expression
393 if (Tok.isNot(tok::equal))
394 return Diag(Tok, diag::err_expected_equal_after_declarator);
395 SourceLocation EqualLoc = ConsumeToken();
396 ExprResult AssignExpr = ParseAssignmentExpression();
397 if (AssignExpr.isInvalid)
398 return true;
399
400 return Actions.ActOnCXXConditionDeclarationExpr(CurScope, StartLoc,
401 DeclaratorInfo,
402 EqualLoc, AssignExpr.Val);
403}
404
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000405/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
406/// This should only be called when the current token is known to be part of
407/// simple-type-specifier.
408///
409/// simple-type-specifier:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000410/// '::'[opt] nested-name-specifier[opt] type-name
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000411/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
412/// char
413/// wchar_t
414/// bool
415/// short
416/// int
417/// long
418/// signed
419/// unsigned
420/// float
421/// double
422/// void
423/// [GNU] typeof-specifier
424/// [C++0x] auto [TODO]
425///
426/// type-name:
427/// class-name
428/// enum-name
429/// typedef-name
430///
431void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000432 // Annotate typenames and C++ scope specifiers.
433 TryAnnotateTypeOrScopeToken();
434
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000435 DS.SetRangeStart(Tok.getLocation());
436 const char *PrevSpec;
437 SourceLocation Loc = Tok.getLocation();
438
439 switch (Tok.getKind()) {
440 default:
441 assert(0 && "Not a simple-type-specifier token!");
442 abort();
443
444 // type-name
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000445 case tok::annot_qualtypename: {
446 DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
447 Tok.getAnnotationValue());
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000448 break;
449 }
450
451 // builtin types
452 case tok::kw_short:
453 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
454 break;
455 case tok::kw_long:
456 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
457 break;
458 case tok::kw_signed:
459 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
460 break;
461 case tok::kw_unsigned:
462 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
463 break;
464 case tok::kw_void:
465 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
466 break;
467 case tok::kw_char:
468 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
469 break;
470 case tok::kw_int:
471 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
472 break;
473 case tok::kw_float:
474 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
475 break;
476 case tok::kw_double:
477 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
478 break;
479 case tok::kw_wchar_t:
480 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
481 break;
482 case tok::kw_bool:
483 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
484 break;
485
486 // GNU typeof support.
487 case tok::kw_typeof:
488 ParseTypeofSpecifier(DS);
489 DS.Finish(Diags, PP.getSourceManager(), getLang());
490 return;
491 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000492 if (Tok.is(tok::annot_qualtypename))
493 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
494 else
495 DS.SetRangeEnd(Tok.getLocation());
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000496 ConsumeToken();
497 DS.Finish(Diags, PP.getSourceManager(), getLang());
498}
Douglas Gregore60e5d32008-11-06 22:13:31 +0000499
Douglas Gregor3ef6c972008-11-07 20:08:42 +0000500/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
501/// [dcl.name]), which is a non-empty sequence of type-specifiers,
502/// e.g., "const short int". Note that the DeclSpec is *not* finished
503/// by parsing the type-specifier-seq, because these sequences are
504/// typically followed by some form of declarator. Returns true and
505/// emits diagnostics if this is not a type-specifier-seq, false
506/// otherwise.
507///
508/// type-specifier-seq: [C++ 8.1]
509/// type-specifier type-specifier-seq[opt]
510///
511bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
512 DS.SetRangeStart(Tok.getLocation());
513 const char *PrevSpec = 0;
514 int isInvalid = 0;
515
516 // Parse one or more of the type specifiers.
517 if (!MaybeParseTypeSpecifier(DS, isInvalid, PrevSpec)) {
518 Diag(Tok.getLocation(), diag::err_operator_missing_type_specifier);
519 return true;
520 }
Daniel Dunbar19fa6fb2008-11-08 04:28:37 +0000521 while (MaybeParseTypeSpecifier(DS, isInvalid, PrevSpec)) ;
Douglas Gregor3ef6c972008-11-07 20:08:42 +0000522
523 return false;
524}
525
Douglas Gregor682a8cf2008-11-17 16:14:12 +0000526/// TryParseOperatorFunctionId - Attempts to parse a C++ overloaded
Douglas Gregore60e5d32008-11-06 22:13:31 +0000527/// operator name (C++ [over.oper]). If successful, returns the
528/// predefined identifier that corresponds to that overloaded
529/// operator. Otherwise, returns NULL and does not consume any tokens.
530///
531/// operator-function-id: [C++ 13.5]
532/// 'operator' operator
533///
534/// operator: one of
535/// new delete new[] delete[]
536/// + - * / % ^ & | ~
537/// ! = < > += -= *= /= %=
538/// ^= &= |= << >> >>= <<= == !=
539/// <= >= && || ++ -- , ->* ->
540/// () []
Douglas Gregor682a8cf2008-11-17 16:14:12 +0000541IdentifierInfo *Parser::TryParseOperatorFunctionId() {
Argiris Kirtzidisa9d57b62008-11-07 15:54:02 +0000542 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
Douglas Gregore60e5d32008-11-06 22:13:31 +0000543
544 OverloadedOperatorKind Op = OO_None;
545 switch (NextToken().getKind()) {
546 case tok::kw_new:
547 ConsumeToken(); // 'operator'
548 ConsumeToken(); // 'new'
549 if (Tok.is(tok::l_square)) {
550 ConsumeBracket(); // '['
551 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
552 Op = OO_Array_New;
553 } else {
554 Op = OO_New;
555 }
556 return &PP.getIdentifierTable().getOverloadedOperator(Op);
557
558 case tok::kw_delete:
559 ConsumeToken(); // 'operator'
560 ConsumeToken(); // 'delete'
561 if (Tok.is(tok::l_square)) {
562 ConsumeBracket(); // '['
563 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
564 Op = OO_Array_Delete;
565 } else {
566 Op = OO_Delete;
567 }
568 return &PP.getIdentifierTable().getOverloadedOperator(Op);
569
Douglas Gregor9c6210b2008-11-10 13:38:07 +0000570#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
Douglas Gregore60e5d32008-11-06 22:13:31 +0000571 case tok::Token: Op = OO_##Name; break;
Douglas Gregor9c6210b2008-11-10 13:38:07 +0000572#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
Douglas Gregore60e5d32008-11-06 22:13:31 +0000573#include "clang/Basic/OperatorKinds.def"
574
575 case tok::l_paren:
576 ConsumeToken(); // 'operator'
577 ConsumeParen(); // '('
578 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); // ')'
579 return &PP.getIdentifierTable().getOverloadedOperator(OO_Call);
580
581 case tok::l_square:
582 ConsumeToken(); // 'operator'
583 ConsumeBracket(); // '['
584 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
585 return &PP.getIdentifierTable().getOverloadedOperator(OO_Subscript);
586
587 default:
Douglas Gregor682a8cf2008-11-17 16:14:12 +0000588 return 0;
Douglas Gregore60e5d32008-11-06 22:13:31 +0000589 }
590
Douglas Gregor682a8cf2008-11-17 16:14:12 +0000591 ConsumeToken(); // 'operator'
592 ConsumeAnyToken(); // the operator itself
593 return &PP.getIdentifierTable().getOverloadedOperator(Op);
Douglas Gregore60e5d32008-11-06 22:13:31 +0000594}
Douglas Gregor3ef6c972008-11-07 20:08:42 +0000595
596/// ParseConversionFunctionId - Parse a C++ conversion-function-id,
597/// which expresses the name of a user-defined conversion operator
598/// (C++ [class.conv.fct]p1). Returns the type that this operator is
599/// specifying a conversion for, or NULL if there was an error.
600///
601/// conversion-function-id: [C++ 12.3.2]
602/// operator conversion-type-id
603///
604/// conversion-type-id:
605/// type-specifier-seq conversion-declarator[opt]
606///
607/// conversion-declarator:
608/// ptr-operator conversion-declarator[opt]
609Parser::TypeTy *Parser::ParseConversionFunctionId() {
610 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
611 ConsumeToken(); // 'operator'
612
613 // Parse the type-specifier-seq.
614 DeclSpec DS;
615 if (ParseCXXTypeSpecifierSeq(DS))
616 return 0;
617
618 // Parse the conversion-declarator, which is merely a sequence of
619 // ptr-operators.
620 Declarator D(DS, Declarator::TypeNameContext);
621 ParseDeclaratorInternal(D, /*PtrOperator=*/true);
622
623 // Finish up the type.
624 Action::TypeResult Result = Actions.ActOnTypeName(CurScope, D);
625 if (Result.isInvalid)
626 return 0;
627 else
628 return Result.Val;
629}