blob: d97bc2d0288f353afa1eaf2f95390acf72da8536 [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
Chris Lattner500d3292009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000015#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
Chris Lattner7a0ab5f2009-01-06 06:59:53 +000019/// ParseOptionalCXXScopeSpecifier - Parse global scope or
20/// nested-name-specifier if present. Returns true if a nested-name-specifier
21/// was parsed from the token stream. Note that this routine will not parse
22/// ::new or ::delete, it will just leave them in the token stream.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000023///
24/// '::'[opt] nested-name-specifier
25/// '::'
26///
27/// nested-name-specifier:
28/// type-name '::'
29/// namespace-name '::'
30/// nested-name-specifier identifier '::'
31/// nested-name-specifier 'template'[opt] simple-template-id '::' [TODO]
32///
Douglas Gregor495c35d2009-08-25 22:51:20 +000033bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
34 bool EnteringContext) {
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000035 assert(getLang().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +000036 "Call sites of this function should be guarded by checking for C++");
Douglas Gregor495c35d2009-08-25 22:51:20 +000037
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000038 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregor35073692009-03-26 23:56:24 +000039 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000040 SS.setRange(Tok.getAnnotationRange());
41 ConsumeToken();
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000042 return true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000043 }
Chris Lattnere607e802009-01-04 21:14:15 +000044
Douglas Gregor39a8de12009-02-25 19:37:18 +000045 bool HasScopeSpecifier = false;
46
Chris Lattner5b454732009-01-05 03:55:46 +000047 if (Tok.is(tok::coloncolon)) {
48 // ::new and ::delete aren't nested-name-specifiers.
49 tok::TokenKind NextKind = NextToken().getKind();
50 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
51 return false;
Chris Lattner55a7cef2009-01-05 00:13:00 +000052
Chris Lattner55a7cef2009-01-05 00:13:00 +000053 // '::' - Global scope qualifier.
Chris Lattner357089d2009-01-05 02:07:19 +000054 SourceLocation CCLoc = ConsumeToken();
Chris Lattner357089d2009-01-05 02:07:19 +000055 SS.setBeginLoc(CCLoc);
Douglas Gregor35073692009-03-26 23:56:24 +000056 SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, CCLoc));
Chris Lattner357089d2009-01-05 02:07:19 +000057 SS.setEndLoc(CCLoc);
Douglas Gregor39a8de12009-02-25 19:37:18 +000058 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000059 }
60
Douglas Gregor39a8de12009-02-25 19:37:18 +000061 while (true) {
62 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +000063 // nested-name-specifier 'template'[opt] simple-template-id '::'
64
65 // Parse the optional 'template' keyword, then make sure we have
66 // 'identifier <' after it.
67 if (Tok.is(tok::kw_template)) {
68 SourceLocation TemplateKWLoc = ConsumeToken();
69
70 if (Tok.isNot(tok::identifier)) {
71 Diag(Tok.getLocation(),
72 diag::err_id_after_template_in_nested_name_spec)
73 << SourceRange(TemplateKWLoc);
74 break;
75 }
76
77 if (NextToken().isNot(tok::less)) {
78 Diag(NextToken().getLocation(),
79 diag::err_less_after_template_name_in_nested_name_spec)
80 << Tok.getIdentifierInfo()->getName()
81 << SourceRange(TemplateKWLoc, Tok.getLocation());
82 break;
83 }
84
85 TemplateTy Template
86 = Actions.ActOnDependentTemplateName(TemplateKWLoc,
87 *Tok.getIdentifierInfo(),
88 Tok.getLocation(), SS);
Chris Lattnerc8e27cc2009-06-26 04:27:47 +000089 if (AnnotateTemplateIdToken(Template, TNK_Dependent_template_name,
90 &SS, TemplateKWLoc, false))
91 break;
92
Chris Lattner77cf72a2009-06-26 03:47:46 +000093 continue;
94 }
95
Douglas Gregor39a8de12009-02-25 19:37:18 +000096 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
97 // We have
98 //
99 // simple-template-id '::'
100 //
101 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000102 // right kind (it should name a type or be dependent), and then
103 // convert it into a type within the nested-name-specifier.
Douglas Gregor39a8de12009-02-25 19:37:18 +0000104 TemplateIdAnnotation *TemplateId
105 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
106
Douglas Gregorc45c2322009-03-31 00:43:58 +0000107 if (TemplateId->Kind == TNK_Type_template ||
108 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000109 AnnotateTemplateIdTokenAsType(&SS);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000110
111 assert(Tok.is(tok::annot_typename) &&
112 "AnnotateTemplateIdTokenAsType isn't working");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000113 Token TypeToken = Tok;
114 ConsumeToken();
115 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
116 SourceLocation CCLoc = ConsumeToken();
117
118 if (!HasScopeSpecifier) {
119 SS.setBeginLoc(TypeToken.getLocation());
120 HasScopeSpecifier = true;
121 }
Douglas Gregor31a19b62009-04-01 21:51:26 +0000122
123 if (TypeToken.getAnnotationValue())
124 SS.setScopeRep(
125 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS,
126 TypeToken.getAnnotationValue(),
127 TypeToken.getAnnotationRange(),
128 CCLoc));
129 else
130 SS.setScopeRep(0);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000131 SS.setEndLoc(CCLoc);
132 continue;
Chris Lattner67b9e832009-06-26 03:45:46 +0000133 }
134
135 assert(false && "FIXME: Only type template names supported here");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000136 }
137
Chris Lattner5c7f7862009-06-26 03:52:38 +0000138
139 // The rest of the nested-name-specifier possibilities start with
140 // tok::identifier.
141 if (Tok.isNot(tok::identifier))
142 break;
143
144 IdentifierInfo &II = *Tok.getIdentifierInfo();
145
146 // nested-name-specifier:
147 // type-name '::'
148 // namespace-name '::'
149 // nested-name-specifier identifier '::'
150 Token Next = NextToken();
151 if (Next.is(tok::coloncolon)) {
152 // We have an identifier followed by a '::'. Lookup this name
153 // as the name in a nested-name-specifier.
154 SourceLocation IdLoc = ConsumeToken();
155 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
156 SourceLocation CCLoc = ConsumeToken();
157
158 if (!HasScopeSpecifier) {
159 SS.setBeginLoc(IdLoc);
160 HasScopeSpecifier = true;
161 }
162
163 if (SS.isInvalid())
164 continue;
165
166 SS.setScopeRep(
Douglas Gregor495c35d2009-08-25 22:51:20 +0000167 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, II,
168 EnteringContext));
Chris Lattner5c7f7862009-06-26 03:52:38 +0000169 SS.setEndLoc(CCLoc);
170 continue;
171 }
172
173 // nested-name-specifier:
174 // type-name '<'
175 if (Next.is(tok::less)) {
176 TemplateTy Template;
Douglas Gregor495c35d2009-08-25 22:51:20 +0000177 if (TemplateNameKind TNK = Actions.isTemplateName(II, CurScope, &SS,
178 EnteringContext,
179 Template)) {
Chris Lattner5c7f7862009-06-26 03:52:38 +0000180 // We have found a template name, so annotate this this token
181 // with a template-id annotation. We do not permit the
182 // template-id to be translated into a type annotation,
183 // because some clients (e.g., the parsing of class template
184 // specializations) still want to see the original template-id
185 // token.
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000186 if (AnnotateTemplateIdToken(Template, TNK, &SS, SourceLocation(),
187 false))
188 break;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000189 continue;
190 }
191 }
192
Douglas Gregor39a8de12009-02-25 19:37:18 +0000193 // We don't have any tokens that form the beginning of a
194 // nested-name-specifier, so we're done.
195 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000196 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000197
198 return HasScopeSpecifier;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000199}
200
201/// ParseCXXIdExpression - Handle id-expression.
202///
203/// id-expression:
204/// unqualified-id
205/// qualified-id
206///
207/// unqualified-id:
208/// identifier
209/// operator-function-id
210/// conversion-function-id [TODO]
211/// '~' class-name [TODO]
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000212/// template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000213///
214/// qualified-id:
215/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
216/// '::' identifier
217/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000218/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000219///
220/// nested-name-specifier:
221/// type-name '::'
222/// namespace-name '::'
223/// nested-name-specifier identifier '::'
224/// nested-name-specifier 'template'[opt] simple-template-id '::' [TODO]
225///
226/// NOTE: The standard specifies that, for qualified-id, the parser does not
227/// expect:
228///
229/// '::' conversion-function-id
230/// '::' '~' class-name
231///
232/// This may cause a slight inconsistency on diagnostics:
233///
234/// class C {};
235/// namespace A {}
236/// void f() {
237/// :: A :: ~ C(); // Some Sema error about using destructor with a
238/// // namespace.
239/// :: ~ C(); // Some Parser error like 'unexpected ~'.
240/// }
241///
242/// We simplify the parser a bit and make it work like:
243///
244/// qualified-id:
245/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
246/// '::' unqualified-id
247///
248/// That way Sema can handle and report similar errors for namespaces and the
249/// global scope.
250///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000251/// The isAddressOfOperand parameter indicates that this id-expression is a
252/// direct operand of the address-of operator. This is, besides member contexts,
253/// the only place where a qualified-id naming a non-static class member may
254/// appear.
255///
256Parser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000257 // qualified-id:
258 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
259 // '::' unqualified-id
260 //
261 CXXScopeSpec SS;
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000262 ParseOptionalCXXScopeSpecifier(SS);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000263
264 // unqualified-id:
265 // identifier
266 // operator-function-id
Douglas Gregor2def4832008-11-17 20:34:05 +0000267 // conversion-function-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000268 // '~' class-name [TODO]
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000269 // template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000270 //
271 switch (Tok.getKind()) {
272 default:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000273 return ExprError(Diag(Tok, diag::err_expected_unqualified_id));
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000274
275 case tok::identifier: {
276 // Consume the identifier so that we can see if it is followed by a '('.
277 IdentifierInfo &II = *Tok.getIdentifierInfo();
278 SourceLocation L = ConsumeToken();
Sebastian Redlebc07d52009-02-03 20:19:35 +0000279 return Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren),
280 &SS, isAddressOfOperand);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000281 }
282
283 case tok::kw_operator: {
284 SourceLocation OperatorLoc = Tok.getLocation();
Chris Lattner7452c6f2009-01-05 01:24:05 +0000285 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId())
Sebastian Redlcd965b92009-01-18 18:53:16 +0000286 return Actions.ActOnCXXOperatorFunctionIdExpr(
Sebastian Redlebc07d52009-02-03 20:19:35 +0000287 CurScope, OperatorLoc, Op, Tok.is(tok::l_paren), SS,
288 isAddressOfOperand);
Chris Lattner7452c6f2009-01-05 01:24:05 +0000289 if (TypeTy *Type = ParseConversionFunctionId())
Sebastian Redlcd965b92009-01-18 18:53:16 +0000290 return Actions.ActOnCXXConversionFunctionExpr(CurScope, OperatorLoc, Type,
Sebastian Redlebc07d52009-02-03 20:19:35 +0000291 Tok.is(tok::l_paren), SS,
292 isAddressOfOperand);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000293
Douglas Gregor2def4832008-11-17 20:34:05 +0000294 // We already complained about a bad conversion-function-id,
295 // above.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000296 return ExprError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000297 }
298
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000299 case tok::annot_template_id: {
300 TemplateIdAnnotation *TemplateId
301 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
302 assert((TemplateId->Kind == TNK_Function_template ||
303 TemplateId->Kind == TNK_Dependent_template_name) &&
304 "A template type name is not an ID expression");
305
306 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
307 TemplateId->getTemplateArgs(),
308 TemplateId->getTemplateArgIsType(),
309 TemplateId->NumArgs);
310
311 OwningExprResult Result
312 = Actions.ActOnTemplateIdExpr(TemplateTy::make(TemplateId->Template),
313 TemplateId->TemplateNameLoc,
314 TemplateId->LAngleLoc,
315 TemplateArgsPtr,
316 TemplateId->getTemplateArgLocations(),
317 TemplateId->RAngleLoc);
318 ConsumeToken(); // Consume the template-id token
319 return move(Result);
320 }
321
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000322 } // switch.
323
324 assert(0 && "The switch was supposed to take care everything.");
325}
326
Reid Spencer5f016e22007-07-11 17:01:13 +0000327/// ParseCXXCasts - This handles the various ways to cast expressions to another
328/// type.
329///
330/// postfix-expression: [C++ 5.2p1]
331/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
332/// 'static_cast' '<' type-name '>' '(' expression ')'
333/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
334/// 'const_cast' '<' type-name '>' '(' expression ')'
335///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000336Parser::OwningExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000337 tok::TokenKind Kind = Tok.getKind();
338 const char *CastName = 0; // For error messages
339
340 switch (Kind) {
341 default: assert(0 && "Unknown C++ cast!"); abort();
342 case tok::kw_const_cast: CastName = "const_cast"; break;
343 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
344 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
345 case tok::kw_static_cast: CastName = "static_cast"; break;
346 }
347
348 SourceLocation OpLoc = ConsumeToken();
349 SourceLocation LAngleBracketLoc = Tok.getLocation();
350
351 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000352 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000353
Douglas Gregor809070a2009-02-18 17:45:20 +0000354 TypeResult CastTy = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000355 SourceLocation RAngleBracketLoc = Tok.getLocation();
356
Chris Lattner1ab3b962008-11-18 07:48:38 +0000357 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000358 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000359
360 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
361
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000362 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
363 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000364
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000365 OwningExprResult Result = ParseExpression();
366
367 // Match the ')'.
368 if (Result.isInvalid())
369 SkipUntil(tok::r_paren);
370
371 if (Tok.is(tok::r_paren))
372 RParenLoc = ConsumeParen();
373 else
374 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000375
Douglas Gregor809070a2009-02-18 17:45:20 +0000376 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000377 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000378 LAngleBracketLoc, CastTy.get(),
Douglas Gregor809070a2009-02-18 17:45:20 +0000379 RAngleBracketLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000380 LParenLoc, move(Result), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000381
Sebastian Redl20df9b72008-12-11 22:51:44 +0000382 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000383}
384
Sebastian Redlc42e1182008-11-11 11:37:55 +0000385/// ParseCXXTypeid - This handles the C++ typeid expression.
386///
387/// postfix-expression: [C++ 5.2p1]
388/// 'typeid' '(' expression ')'
389/// 'typeid' '(' type-id ')'
390///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000391Parser::OwningExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000392 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
393
394 SourceLocation OpLoc = ConsumeToken();
395 SourceLocation LParenLoc = Tok.getLocation();
396 SourceLocation RParenLoc;
397
398 // typeid expressions are always parenthesized.
399 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
400 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000401 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000402
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000403 OwningExprResult Result(Actions);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000404
405 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000406 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000407
408 // Match the ')'.
409 MatchRHSPunctuation(tok::r_paren, LParenLoc);
410
Douglas Gregor809070a2009-02-18 17:45:20 +0000411 if (Ty.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000412 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000413
414 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
Douglas Gregor809070a2009-02-18 17:45:20 +0000415 Ty.get(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000416 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000417 // C++0x [expr.typeid]p3:
418 // When typeid is applied to an expression other than an lvalue of a
419 // polymorphic class type [...] The expression is an unevaluated
420 // operand (Clause 5).
421 //
422 // Note that we can't tell whether the expression is an lvalue of a
423 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000424 // we the expression is potentially potentially evaluated.
425 EnterExpressionEvaluationContext Unevaluated(Actions,
426 Action::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000427 Result = ParseExpression();
428
429 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000430 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000431 SkipUntil(tok::r_paren);
432 else {
433 MatchRHSPunctuation(tok::r_paren, LParenLoc);
434
435 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000436 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000437 }
438 }
439
Sebastian Redl20df9b72008-12-11 22:51:44 +0000440 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000441}
442
Reid Spencer5f016e22007-07-11 17:01:13 +0000443/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
444///
445/// boolean-literal: [C++ 2.13.5]
446/// 'true'
447/// 'false'
Sebastian Redl20df9b72008-12-11 22:51:44 +0000448Parser::OwningExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000449 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000450 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000451}
Chris Lattner50dd2892008-02-26 00:51:44 +0000452
453/// ParseThrowExpression - This handles the C++ throw expression.
454///
455/// throw-expression: [C++ 15]
456/// 'throw' assignment-expression[opt]
Sebastian Redl20df9b72008-12-11 22:51:44 +0000457Parser::OwningExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000458 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000459 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000460
Chris Lattner2a2819a2008-04-06 06:02:23 +0000461 // If the current token isn't the start of an assignment-expression,
462 // then the expression is not present. This handles things like:
463 // "C ? throw : (void)42", which is crazy but legal.
464 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
465 case tok::semi:
466 case tok::r_paren:
467 case tok::r_square:
468 case tok::r_brace:
469 case tok::colon:
470 case tok::comma:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000471 return Actions.ActOnCXXThrow(ThrowLoc, ExprArg(Actions));
Chris Lattner50dd2892008-02-26 00:51:44 +0000472
Chris Lattner2a2819a2008-04-06 06:02:23 +0000473 default:
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000474 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000475 if (Expr.isInvalid()) return move(Expr);
Sebastian Redlf53597f2009-03-15 17:47:39 +0000476 return Actions.ActOnCXXThrow(ThrowLoc, move(Expr));
Chris Lattner2a2819a2008-04-06 06:02:23 +0000477 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000478}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000479
480/// ParseCXXThis - This handles the C++ 'this' pointer.
481///
482/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
483/// a non-lvalue expression whose value is the address of the object for which
484/// the function is called.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000485Parser::OwningExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000486 assert(Tok.is(tok::kw_this) && "Not 'this'!");
487 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000488 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000489}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000490
491/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
492/// Can be interpreted either as function-style casting ("int(x)")
493/// or class type construction ("ClassType(x,y,z)")
494/// or creation of a value-initialized type ("int()").
495///
496/// postfix-expression: [C++ 5.2p1]
497/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
498/// typename-specifier '(' expression-list[opt] ')' [TODO]
499///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000500Parser::OwningExprResult
501Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000502 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000503 TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000504
505 assert(Tok.is(tok::l_paren) && "Expected '('!");
506 SourceLocation LParenLoc = ConsumeParen();
507
Sebastian Redla55e52c2008-11-25 22:21:31 +0000508 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000509 CommaLocsTy CommaLocs;
510
511 if (Tok.isNot(tok::r_paren)) {
512 if (ParseExpressionList(Exprs, CommaLocs)) {
513 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000514 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000515 }
516 }
517
518 // Match the ')'.
519 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
520
Sebastian Redlef0cb8e2009-07-29 13:50:23 +0000521 // TypeRep could be null, if it references an invalid typedef.
522 if (!TypeRep)
523 return ExprError();
524
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000525 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
526 "Unexpected number of commas!");
Sebastian Redlf53597f2009-03-15 17:47:39 +0000527 return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
528 LParenLoc, move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000529 CommaLocs.data(), RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000530}
531
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000532/// ParseCXXCondition - if/switch/while/for condition expression.
533///
534/// condition:
535/// expression
536/// type-specifier-seq declarator '=' assignment-expression
537/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
538/// '=' assignment-expression
539///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000540Parser::OwningExprResult Parser::ParseCXXCondition() {
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000541 if (!isCXXConditionDeclaration())
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000542 return ParseExpression(); // expression
543
544 SourceLocation StartLoc = Tok.getLocation();
545
546 // type-specifier-seq
547 DeclSpec DS;
548 ParseSpecifierQualifierList(DS);
549
550 // declarator
551 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
552 ParseDeclarator(DeclaratorInfo);
553
554 // simple-asm-expr[opt]
555 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000556 SourceLocation Loc;
557 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000558 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000559 SkipUntil(tok::semi);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000560 return ExprError();
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000561 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000562 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000563 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000564 }
565
566 // If attributes are present, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000567 if (Tok.is(tok::kw___attribute)) {
568 SourceLocation Loc;
569 AttributeList *AttrList = ParseAttributes(&Loc);
570 DeclaratorInfo.AddAttributes(AttrList, Loc);
571 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000572
573 // '=' assignment-expression
574 if (Tok.isNot(tok::equal))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000575 return ExprError(Diag(Tok, diag::err_expected_equal_after_declarator));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000576 SourceLocation EqualLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000577 OwningExprResult AssignExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000578 if (AssignExpr.isInvalid())
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000579 return ExprError();
580
Sebastian Redlf53597f2009-03-15 17:47:39 +0000581 return Actions.ActOnCXXConditionDeclarationExpr(CurScope, StartLoc,
582 DeclaratorInfo,EqualLoc,
583 move(AssignExpr));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000584}
585
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000586/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
587/// This should only be called when the current token is known to be part of
588/// simple-type-specifier.
589///
590/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000591/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000592/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
593/// char
594/// wchar_t
595/// bool
596/// short
597/// int
598/// long
599/// signed
600/// unsigned
601/// float
602/// double
603/// void
604/// [GNU] typeof-specifier
605/// [C++0x] auto [TODO]
606///
607/// type-name:
608/// class-name
609/// enum-name
610/// typedef-name
611///
612void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
613 DS.SetRangeStart(Tok.getLocation());
614 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000615 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000616 SourceLocation Loc = Tok.getLocation();
617
618 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000619 case tok::identifier: // foo::bar
620 case tok::coloncolon: // ::foo::bar
621 assert(0 && "Annotation token should already be formed!");
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000622 default:
623 assert(0 && "Not a simple-type-specifier token!");
624 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000625
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000626 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000627 case tok::annot_typename: {
John McCallfec54012009-08-03 20:12:06 +0000628 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000629 Tok.getAnnotationValue());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000630 break;
631 }
632
633 // builtin types
634 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +0000635 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000636 break;
637 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +0000638 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000639 break;
640 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +0000641 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000642 break;
643 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +0000644 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000645 break;
646 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +0000647 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000648 break;
649 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +0000650 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000651 break;
652 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +0000653 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000654 break;
655 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +0000656 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000657 break;
658 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +0000659 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000660 break;
661 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +0000662 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000663 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000664 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +0000665 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000666 break;
667 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +0000668 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000669 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000670 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +0000671 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000672 break;
673
674 // GNU typeof support.
675 case tok::kw_typeof:
676 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000677 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000678 return;
679 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000680 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000681 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
682 else
683 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000684 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000685 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000686}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000687
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000688/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
689/// [dcl.name]), which is a non-empty sequence of type-specifiers,
690/// e.g., "const short int". Note that the DeclSpec is *not* finished
691/// by parsing the type-specifier-seq, because these sequences are
692/// typically followed by some form of declarator. Returns true and
693/// emits diagnostics if this is not a type-specifier-seq, false
694/// otherwise.
695///
696/// type-specifier-seq: [C++ 8.1]
697/// type-specifier type-specifier-seq[opt]
698///
699bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
700 DS.SetRangeStart(Tok.getLocation());
701 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000702 unsigned DiagID;
703 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000704
705 // Parse one or more of the type specifiers.
John McCallfec54012009-08-03 20:12:06 +0000706 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000707 Diag(Tok, diag::err_operator_missing_type_specifier);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000708 return true;
709 }
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000710
John McCallfec54012009-08-03 20:12:06 +0000711 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) ;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000712
713 return false;
714}
715
Douglas Gregor43c7bad2008-11-17 16:14:12 +0000716/// TryParseOperatorFunctionId - Attempts to parse a C++ overloaded
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000717/// operator name (C++ [over.oper]). If successful, returns the
718/// predefined identifier that corresponds to that overloaded
719/// operator. Otherwise, returns NULL and does not consume any tokens.
720///
721/// operator-function-id: [C++ 13.5]
722/// 'operator' operator
723///
724/// operator: one of
725/// new delete new[] delete[]
726/// + - * / % ^ & | ~
727/// ! = < > += -= *= /= %=
728/// ^= &= |= << >> >>= <<= == !=
729/// <= >= && || ++ -- , ->* ->
730/// () []
Sebastian Redlab197ba2009-02-09 18:23:29 +0000731OverloadedOperatorKind
732Parser::TryParseOperatorFunctionId(SourceLocation *EndLoc) {
Argyrios Kyrtzidis9057a812008-11-07 15:54:02 +0000733 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
Sebastian Redlab197ba2009-02-09 18:23:29 +0000734 SourceLocation Loc;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000735
736 OverloadedOperatorKind Op = OO_None;
737 switch (NextToken().getKind()) {
738 case tok::kw_new:
739 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000740 Loc = ConsumeToken(); // 'new'
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000741 if (Tok.is(tok::l_square)) {
742 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +0000743 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000744 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
745 Op = OO_Array_New;
746 } else {
747 Op = OO_New;
748 }
Sebastian Redlab197ba2009-02-09 18:23:29 +0000749 if (EndLoc)
750 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000751 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000752
753 case tok::kw_delete:
754 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000755 Loc = ConsumeToken(); // 'delete'
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000756 if (Tok.is(tok::l_square)) {
757 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +0000758 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000759 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
760 Op = OO_Array_Delete;
761 } else {
762 Op = OO_Delete;
763 }
Sebastian Redlab197ba2009-02-09 18:23:29 +0000764 if (EndLoc)
765 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000766 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000767
Douglas Gregor02bcd4c2008-11-10 13:38:07 +0000768#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000769 case tok::Token: Op = OO_##Name; break;
Douglas Gregor02bcd4c2008-11-10 13:38:07 +0000770#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000771#include "clang/Basic/OperatorKinds.def"
772
773 case tok::l_paren:
774 ConsumeToken(); // 'operator'
775 ConsumeParen(); // '('
Sebastian Redlab197ba2009-02-09 18:23:29 +0000776 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000777 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); // ')'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000778 if (EndLoc)
779 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000780 return OO_Call;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000781
782 case tok::l_square:
783 ConsumeToken(); // 'operator'
784 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +0000785 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000786 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000787 if (EndLoc)
788 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000789 return OO_Subscript;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000790
791 default:
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000792 return OO_None;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000793 }
794
Douglas Gregor43c7bad2008-11-17 16:14:12 +0000795 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000796 Loc = ConsumeAnyToken(); // the operator itself
797 if (EndLoc)
798 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000799 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000800}
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000801
802/// ParseConversionFunctionId - Parse a C++ conversion-function-id,
803/// which expresses the name of a user-defined conversion operator
804/// (C++ [class.conv.fct]p1). Returns the type that this operator is
805/// specifying a conversion for, or NULL if there was an error.
806///
807/// conversion-function-id: [C++ 12.3.2]
808/// operator conversion-type-id
809///
810/// conversion-type-id:
811/// type-specifier-seq conversion-declarator[opt]
812///
813/// conversion-declarator:
814/// ptr-operator conversion-declarator[opt]
Sebastian Redlab197ba2009-02-09 18:23:29 +0000815Parser::TypeTy *Parser::ParseConversionFunctionId(SourceLocation *EndLoc) {
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000816 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
817 ConsumeToken(); // 'operator'
818
819 // Parse the type-specifier-seq.
820 DeclSpec DS;
821 if (ParseCXXTypeSpecifierSeq(DS))
822 return 0;
823
824 // Parse the conversion-declarator, which is merely a sequence of
825 // ptr-operators.
826 Declarator D(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000827 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000828 if (EndLoc)
829 *EndLoc = D.getSourceRange().getEnd();
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000830
831 // Finish up the type.
832 Action::TypeResult Result = Actions.ActOnTypeName(CurScope, D);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000833 if (Result.isInvalid())
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000834 return 0;
835 else
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000836 return Result.get();
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000837}
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000838
839/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
840/// memory in a typesafe manner and call constructors.
Chris Lattner59232d32009-01-04 21:25:24 +0000841///
842/// This method is called to parse the new expression after the optional :: has
843/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
844/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000845///
846/// new-expression:
847/// '::'[opt] 'new' new-placement[opt] new-type-id
848/// new-initializer[opt]
849/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
850/// new-initializer[opt]
851///
852/// new-placement:
853/// '(' expression-list ')'
854///
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000855/// new-type-id:
856/// type-specifier-seq new-declarator[opt]
857///
858/// new-declarator:
859/// ptr-operator new-declarator[opt]
860/// direct-new-declarator
861///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000862/// new-initializer:
863/// '(' expression-list[opt] ')'
864/// [C++0x] braced-init-list [TODO]
865///
Chris Lattner59232d32009-01-04 21:25:24 +0000866Parser::OwningExprResult
867Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
868 assert(Tok.is(tok::kw_new) && "expected 'new' token");
869 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000870
871 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
872 // second form of new-expression. It can't be a new-type-id.
873
Sebastian Redla55e52c2008-11-25 22:21:31 +0000874 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000875 SourceLocation PlacementLParen, PlacementRParen;
876
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000877 bool ParenTypeId;
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000878 DeclSpec DS;
879 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000880 if (Tok.is(tok::l_paren)) {
881 // If it turns out to be a placement, we change the type location.
882 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000883 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
884 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000885 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000886 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000887
888 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000889 if (PlacementRParen.isInvalid()) {
890 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000891 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000892 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000893
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000894 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000895 // Reset the placement locations. There was no placement.
896 PlacementLParen = PlacementRParen = SourceLocation();
897 ParenTypeId = true;
898 } else {
899 // We still need the type.
900 if (Tok.is(tok::l_paren)) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000901 SourceLocation LParen = ConsumeParen();
902 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000903 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000904 ParseDeclarator(DeclaratorInfo);
905 MatchRHSPunctuation(tok::r_paren, LParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000906 ParenTypeId = true;
907 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000908 if (ParseCXXTypeSpecifierSeq(DS))
909 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000910 else {
911 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000912 ParseDeclaratorInternal(DeclaratorInfo,
913 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000914 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000915 ParenTypeId = false;
916 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000917 }
918 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000919 // A new-type-id is a simplified type-id, where essentially the
920 // direct-declarator is replaced by a direct-new-declarator.
921 if (ParseCXXTypeSpecifierSeq(DS))
922 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000923 else {
924 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000925 ParseDeclaratorInternal(DeclaratorInfo,
926 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000927 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000928 ParenTypeId = false;
929 }
Chris Lattnereaaebc72009-04-25 08:06:05 +0000930 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000931 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000932 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000933 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000934
Sebastian Redla55e52c2008-11-25 22:21:31 +0000935 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000936 SourceLocation ConstructorLParen, ConstructorRParen;
937
938 if (Tok.is(tok::l_paren)) {
939 ConstructorLParen = ConsumeParen();
940 if (Tok.isNot(tok::r_paren)) {
941 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000942 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
943 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000944 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000945 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000946 }
947 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000948 if (ConstructorRParen.isInvalid()) {
949 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000950 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000951 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000952 }
953
Sebastian Redlf53597f2009-03-15 17:47:39 +0000954 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
955 move_arg(PlacementArgs), PlacementRParen,
956 ParenTypeId, DeclaratorInfo, ConstructorLParen,
957 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000958}
959
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000960/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
961/// passed to ParseDeclaratorInternal.
962///
963/// direct-new-declarator:
964/// '[' expression ']'
965/// direct-new-declarator '[' constant-expression ']'
966///
Chris Lattner59232d32009-01-04 21:25:24 +0000967void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000968 // Parse the array dimensions.
969 bool first = true;
970 while (Tok.is(tok::l_square)) {
971 SourceLocation LLoc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000972 OwningExprResult Size(first ? ParseExpression()
973 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000974 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000975 // Recover
976 SkipUntil(tok::r_square);
977 return;
978 }
979 first = false;
980
Sebastian Redlab197ba2009-02-09 18:23:29 +0000981 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000982 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000983 Size.release(), LLoc, RLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +0000984 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000985
Sebastian Redlab197ba2009-02-09 18:23:29 +0000986 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000987 return;
988 }
989}
990
991/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
992/// This ambiguity appears in the syntax of the C++ new operator.
993///
994/// new-expression:
995/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
996/// new-initializer[opt]
997///
998/// new-placement:
999/// '(' expression-list ')'
1000///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001001bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001002 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001003 // The '(' was already consumed.
1004 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001005 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001006 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001007 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001008 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001009 }
1010
1011 // It's not a type, it has to be an expression list.
1012 // Discard the comma locations - ActOnCXXNew has enough parameters.
1013 CommaLocsTy CommaLocs;
1014 return ParseExpressionList(PlacementArgs, CommaLocs);
1015}
1016
1017/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1018/// to free memory allocated by new.
1019///
Chris Lattner59232d32009-01-04 21:25:24 +00001020/// This method is called to parse the 'delete' expression after the optional
1021/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1022/// and "Start" is its location. Otherwise, "Start" is the location of the
1023/// 'delete' token.
1024///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001025/// delete-expression:
1026/// '::'[opt] 'delete' cast-expression
1027/// '::'[opt] 'delete' '[' ']' cast-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001028Parser::OwningExprResult
1029Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1030 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1031 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001032
1033 // Array delete?
1034 bool ArrayDelete = false;
1035 if (Tok.is(tok::l_square)) {
1036 ArrayDelete = true;
1037 SourceLocation LHS = ConsumeBracket();
1038 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1039 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001040 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001041 }
1042
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001043 OwningExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001044 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001045 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001046
Sebastian Redlf53597f2009-03-15 17:47:39 +00001047 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001048}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001049
1050static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind)
1051{
1052 switch(kind) {
1053 default: assert(false && "Not a known unary type trait.");
1054 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1055 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1056 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1057 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1058 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1059 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1060 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1061 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1062 case tok::kw___is_abstract: return UTT_IsAbstract;
1063 case tok::kw___is_class: return UTT_IsClass;
1064 case tok::kw___is_empty: return UTT_IsEmpty;
1065 case tok::kw___is_enum: return UTT_IsEnum;
1066 case tok::kw___is_pod: return UTT_IsPOD;
1067 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1068 case tok::kw___is_union: return UTT_IsUnion;
1069 }
1070}
1071
1072/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1073/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1074/// templates.
1075///
1076/// primary-expression:
1077/// [GNU] unary-type-trait '(' type-id ')'
1078///
1079Parser::OwningExprResult Parser::ParseUnaryTypeTrait()
1080{
1081 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1082 SourceLocation Loc = ConsumeToken();
1083
1084 SourceLocation LParen = Tok.getLocation();
1085 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1086 return ExprError();
1087
1088 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1089 // there will be cryptic errors about mismatched parentheses and missing
1090 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001091 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001092
1093 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1094
Douglas Gregor809070a2009-02-18 17:45:20 +00001095 if (Ty.isInvalid())
1096 return ExprError();
1097
1098 return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001099}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001100
1101/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1102/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1103/// based on the context past the parens.
1104Parser::OwningExprResult
1105Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
1106 TypeTy *&CastTy,
1107 SourceLocation LParenLoc,
1108 SourceLocation &RParenLoc) {
1109 assert(getLang().CPlusPlus && "Should only be called for C++!");
1110 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1111 assert(isTypeIdInParens() && "Not a type-id!");
1112
1113 OwningExprResult Result(Actions, true);
1114 CastTy = 0;
1115
1116 // We need to disambiguate a very ugly part of the C++ syntax:
1117 //
1118 // (T())x; - type-id
1119 // (T())*x; - type-id
1120 // (T())/x; - expression
1121 // (T()); - expression
1122 //
1123 // The bad news is that we cannot use the specialized tentative parser, since
1124 // it can only verify that the thing inside the parens can be parsed as
1125 // type-id, it is not useful for determining the context past the parens.
1126 //
1127 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00001128 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001129 //
1130 // It uses a scheme similar to parsing inline methods. The parenthesized
1131 // tokens are cached, the context that follows is determined (possibly by
1132 // parsing a cast-expression), and then we re-introduce the cached tokens
1133 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001134
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001135 ParenParseOption ParseAs;
1136 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001137
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001138 // Store the tokens of the parentheses. We will parse them after we determine
1139 // the context that follows them.
1140 if (!ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks, tok::semi)) {
1141 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001142 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1143 return ExprError();
1144 }
1145
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001146 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001147 ParseAs = CompoundLiteral;
1148 } else {
1149 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001150 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1151 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1152 NotCastExpr = true;
1153 } else {
1154 // Try parsing the cast-expression that may follow.
1155 // If it is not a cast-expression, NotCastExpr will be true and no token
1156 // will be consumed.
1157 Result = ParseCastExpression(false/*isUnaryExpression*/,
1158 false/*isAddressofOperand*/,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001159 NotCastExpr, false);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001160 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001161
1162 // If we parsed a cast-expression, it's really a type-id, otherwise it's
1163 // an expression.
1164 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001165 }
1166
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001167 // The current token should go after the cached tokens.
1168 Toks.push_back(Tok);
1169 // Re-enter the stored parenthesized tokens into the token stream, so we may
1170 // parse them now.
1171 PP.EnterTokenStream(Toks.data(), Toks.size(),
1172 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1173 // Drop the current token and bring the first cached one. It's the same token
1174 // as when we entered this function.
1175 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001176
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001177 if (ParseAs >= CompoundLiteral) {
1178 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001179
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001180 // Match the ')'.
1181 if (Tok.is(tok::r_paren))
1182 RParenLoc = ConsumeParen();
1183 else
1184 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001185
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001186 if (ParseAs == CompoundLiteral) {
1187 ExprType = CompoundLiteral;
1188 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1189 }
1190
1191 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1192 assert(ParseAs == CastExpr);
1193
1194 if (Ty.isInvalid())
1195 return ExprError();
1196
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001197 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001198
1199 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001200 if (!Result.isInvalid())
Nate Begeman2ef13e52009-08-10 23:49:36 +00001201 Result = Actions.ActOnCastExpr(CurScope, LParenLoc, CastTy, RParenLoc,
1202 move(Result));
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001203 return move(Result);
1204 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001205
1206 // Not a compound literal, and not followed by a cast-expression.
1207 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001208
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001209 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001210 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001211 if (!Result.isInvalid() && Tok.is(tok::r_paren))
1212 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result));
1213
1214 // Match the ')'.
1215 if (Result.isInvalid()) {
1216 SkipUntil(tok::r_paren);
1217 return ExprError();
1218 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001219
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001220 if (Tok.is(tok::r_paren))
1221 RParenLoc = ConsumeParen();
1222 else
1223 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1224
1225 return move(Result);
1226}