blob: 91b4d4d5a962ffeb43e8016a72482105cb191d2b [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///
Chris Lattner7a0ab5f2009-01-06 06:59:53 +000033bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS) {
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000034 assert(getLang().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +000035 "Call sites of this function should be guarded by checking for C++");
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000036
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000037 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregor35073692009-03-26 23:56:24 +000038 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000039 SS.setRange(Tok.getAnnotationRange());
40 ConsumeToken();
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000041 return true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000042 }
Chris Lattnere607e802009-01-04 21:14:15 +000043
Douglas Gregor39a8de12009-02-25 19:37:18 +000044 bool HasScopeSpecifier = false;
45
Chris Lattner5b454732009-01-05 03:55:46 +000046 if (Tok.is(tok::coloncolon)) {
47 // ::new and ::delete aren't nested-name-specifiers.
48 tok::TokenKind NextKind = NextToken().getKind();
49 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
50 return false;
Chris Lattner55a7cef2009-01-05 00:13:00 +000051
Chris Lattner55a7cef2009-01-05 00:13:00 +000052 // '::' - Global scope qualifier.
Chris Lattner357089d2009-01-05 02:07:19 +000053 SourceLocation CCLoc = ConsumeToken();
Chris Lattner357089d2009-01-05 02:07:19 +000054 SS.setBeginLoc(CCLoc);
Douglas Gregor35073692009-03-26 23:56:24 +000055 SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, CCLoc));
Chris Lattner357089d2009-01-05 02:07:19 +000056 SS.setEndLoc(CCLoc);
Douglas Gregor39a8de12009-02-25 19:37:18 +000057 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000058 }
59
Douglas Gregor39a8de12009-02-25 19:37:18 +000060 while (true) {
61 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +000062 // nested-name-specifier 'template'[opt] simple-template-id '::'
63
64 // Parse the optional 'template' keyword, then make sure we have
65 // 'identifier <' after it.
66 if (Tok.is(tok::kw_template)) {
67 SourceLocation TemplateKWLoc = ConsumeToken();
68
69 if (Tok.isNot(tok::identifier)) {
70 Diag(Tok.getLocation(),
71 diag::err_id_after_template_in_nested_name_spec)
72 << SourceRange(TemplateKWLoc);
73 break;
74 }
75
76 if (NextToken().isNot(tok::less)) {
77 Diag(NextToken().getLocation(),
78 diag::err_less_after_template_name_in_nested_name_spec)
79 << Tok.getIdentifierInfo()->getName()
80 << SourceRange(TemplateKWLoc, Tok.getLocation());
81 break;
82 }
83
84 TemplateTy Template
85 = Actions.ActOnDependentTemplateName(TemplateKWLoc,
86 *Tok.getIdentifierInfo(),
87 Tok.getLocation(), SS);
Chris Lattnerc8e27cc2009-06-26 04:27:47 +000088 if (AnnotateTemplateIdToken(Template, TNK_Dependent_template_name,
89 &SS, TemplateKWLoc, false))
90 break;
91
Chris Lattner77cf72a2009-06-26 03:47:46 +000092 continue;
93 }
94
Douglas Gregor39a8de12009-02-25 19:37:18 +000095 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
96 // We have
97 //
98 // simple-template-id '::'
99 //
100 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000101 // right kind (it should name a type or be dependent), and then
102 // convert it into a type within the nested-name-specifier.
Douglas Gregor39a8de12009-02-25 19:37:18 +0000103 TemplateIdAnnotation *TemplateId
104 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
105
Douglas Gregorc45c2322009-03-31 00:43:58 +0000106 if (TemplateId->Kind == TNK_Type_template ||
107 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000108 AnnotateTemplateIdTokenAsType(&SS);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000109 SS.setScopeRep(0);
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(
167 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, II));
168 SS.setEndLoc(CCLoc);
169 continue;
170 }
171
172 // nested-name-specifier:
173 // type-name '<'
174 if (Next.is(tok::less)) {
175 TemplateTy Template;
176 if (TemplateNameKind TNK = Actions.isTemplateName(II, CurScope,
177 Template, &SS)) {
178 // We have found a template name, so annotate this this token
179 // with a template-id annotation. We do not permit the
180 // template-id to be translated into a type annotation,
181 // because some clients (e.g., the parsing of class template
182 // specializations) still want to see the original template-id
183 // token.
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000184 if (AnnotateTemplateIdToken(Template, TNK, &SS, SourceLocation(),
185 false))
186 break;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000187 continue;
188 }
189 }
190
Douglas Gregor39a8de12009-02-25 19:37:18 +0000191 // We don't have any tokens that form the beginning of a
192 // nested-name-specifier, so we're done.
193 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000194 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000195
196 return HasScopeSpecifier;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000197}
198
199/// ParseCXXIdExpression - Handle id-expression.
200///
201/// id-expression:
202/// unqualified-id
203/// qualified-id
204///
205/// unqualified-id:
206/// identifier
207/// operator-function-id
208/// conversion-function-id [TODO]
209/// '~' class-name [TODO]
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000210/// template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000211///
212/// qualified-id:
213/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
214/// '::' identifier
215/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000216/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000217///
218/// nested-name-specifier:
219/// type-name '::'
220/// namespace-name '::'
221/// nested-name-specifier identifier '::'
222/// nested-name-specifier 'template'[opt] simple-template-id '::' [TODO]
223///
224/// NOTE: The standard specifies that, for qualified-id, the parser does not
225/// expect:
226///
227/// '::' conversion-function-id
228/// '::' '~' class-name
229///
230/// This may cause a slight inconsistency on diagnostics:
231///
232/// class C {};
233/// namespace A {}
234/// void f() {
235/// :: A :: ~ C(); // Some Sema error about using destructor with a
236/// // namespace.
237/// :: ~ C(); // Some Parser error like 'unexpected ~'.
238/// }
239///
240/// We simplify the parser a bit and make it work like:
241///
242/// qualified-id:
243/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
244/// '::' unqualified-id
245///
246/// That way Sema can handle and report similar errors for namespaces and the
247/// global scope.
248///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000249/// The isAddressOfOperand parameter indicates that this id-expression is a
250/// direct operand of the address-of operator. This is, besides member contexts,
251/// the only place where a qualified-id naming a non-static class member may
252/// appear.
253///
254Parser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000255 // qualified-id:
256 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
257 // '::' unqualified-id
258 //
259 CXXScopeSpec SS;
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000260 ParseOptionalCXXScopeSpecifier(SS);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000261
262 // unqualified-id:
263 // identifier
264 // operator-function-id
Douglas Gregor2def4832008-11-17 20:34:05 +0000265 // conversion-function-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000266 // '~' class-name [TODO]
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000267 // template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000268 //
269 switch (Tok.getKind()) {
270 default:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000271 return ExprError(Diag(Tok, diag::err_expected_unqualified_id));
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000272
273 case tok::identifier: {
274 // Consume the identifier so that we can see if it is followed by a '('.
275 IdentifierInfo &II = *Tok.getIdentifierInfo();
276 SourceLocation L = ConsumeToken();
Sebastian Redlebc07d52009-02-03 20:19:35 +0000277 return Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren),
278 &SS, isAddressOfOperand);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000279 }
280
281 case tok::kw_operator: {
282 SourceLocation OperatorLoc = Tok.getLocation();
Chris Lattner7452c6f2009-01-05 01:24:05 +0000283 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId())
Sebastian Redlcd965b92009-01-18 18:53:16 +0000284 return Actions.ActOnCXXOperatorFunctionIdExpr(
Sebastian Redlebc07d52009-02-03 20:19:35 +0000285 CurScope, OperatorLoc, Op, Tok.is(tok::l_paren), SS,
286 isAddressOfOperand);
Chris Lattner7452c6f2009-01-05 01:24:05 +0000287 if (TypeTy *Type = ParseConversionFunctionId())
Sebastian Redlcd965b92009-01-18 18:53:16 +0000288 return Actions.ActOnCXXConversionFunctionExpr(CurScope, OperatorLoc, Type,
Sebastian Redlebc07d52009-02-03 20:19:35 +0000289 Tok.is(tok::l_paren), SS,
290 isAddressOfOperand);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000291
Douglas Gregor2def4832008-11-17 20:34:05 +0000292 // We already complained about a bad conversion-function-id,
293 // above.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000294 return ExprError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000295 }
296
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000297 case tok::annot_template_id: {
298 TemplateIdAnnotation *TemplateId
299 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
300 assert((TemplateId->Kind == TNK_Function_template ||
301 TemplateId->Kind == TNK_Dependent_template_name) &&
302 "A template type name is not an ID expression");
303
304 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
305 TemplateId->getTemplateArgs(),
306 TemplateId->getTemplateArgIsType(),
307 TemplateId->NumArgs);
308
309 OwningExprResult Result
310 = Actions.ActOnTemplateIdExpr(TemplateTy::make(TemplateId->Template),
311 TemplateId->TemplateNameLoc,
312 TemplateId->LAngleLoc,
313 TemplateArgsPtr,
314 TemplateId->getTemplateArgLocations(),
315 TemplateId->RAngleLoc);
316 ConsumeToken(); // Consume the template-id token
317 return move(Result);
318 }
319
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000320 } // switch.
321
322 assert(0 && "The switch was supposed to take care everything.");
323}
324
Reid Spencer5f016e22007-07-11 17:01:13 +0000325/// ParseCXXCasts - This handles the various ways to cast expressions to another
326/// type.
327///
328/// postfix-expression: [C++ 5.2p1]
329/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
330/// 'static_cast' '<' type-name '>' '(' expression ')'
331/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
332/// 'const_cast' '<' type-name '>' '(' expression ')'
333///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000334Parser::OwningExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000335 tok::TokenKind Kind = Tok.getKind();
336 const char *CastName = 0; // For error messages
337
338 switch (Kind) {
339 default: assert(0 && "Unknown C++ cast!"); abort();
340 case tok::kw_const_cast: CastName = "const_cast"; break;
341 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
342 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
343 case tok::kw_static_cast: CastName = "static_cast"; break;
344 }
345
346 SourceLocation OpLoc = ConsumeToken();
347 SourceLocation LAngleBracketLoc = Tok.getLocation();
348
349 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000350 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000351
Douglas Gregor809070a2009-02-18 17:45:20 +0000352 TypeResult CastTy = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000353 SourceLocation RAngleBracketLoc = Tok.getLocation();
354
Chris Lattner1ab3b962008-11-18 07:48:38 +0000355 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000356 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000357
358 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
359
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000360 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
361 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000362
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000363 OwningExprResult Result = ParseExpression();
364
365 // Match the ')'.
366 if (Result.isInvalid())
367 SkipUntil(tok::r_paren);
368
369 if (Tok.is(tok::r_paren))
370 RParenLoc = ConsumeParen();
371 else
372 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000373
Douglas Gregor809070a2009-02-18 17:45:20 +0000374 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000375 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000376 LAngleBracketLoc, CastTy.get(),
Douglas Gregor809070a2009-02-18 17:45:20 +0000377 RAngleBracketLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000378 LParenLoc, move(Result), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000379
Sebastian Redl20df9b72008-12-11 22:51:44 +0000380 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000381}
382
Sebastian Redlc42e1182008-11-11 11:37:55 +0000383/// ParseCXXTypeid - This handles the C++ typeid expression.
384///
385/// postfix-expression: [C++ 5.2p1]
386/// 'typeid' '(' expression ')'
387/// 'typeid' '(' type-id ')'
388///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000389Parser::OwningExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000390 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
391
392 SourceLocation OpLoc = ConsumeToken();
393 SourceLocation LParenLoc = Tok.getLocation();
394 SourceLocation RParenLoc;
395
396 // typeid expressions are always parenthesized.
397 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
398 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000399 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000400
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000401 OwningExprResult Result(Actions);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000402
403 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000404 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000405
406 // Match the ')'.
407 MatchRHSPunctuation(tok::r_paren, LParenLoc);
408
Douglas Gregor809070a2009-02-18 17:45:20 +0000409 if (Ty.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000410 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000411
412 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
Douglas Gregor809070a2009-02-18 17:45:20 +0000413 Ty.get(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000414 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000415 // C++0x [expr.typeid]p3:
416 // When typeid is applied to an expression other than an lvalue of a
417 // polymorphic class type [...] The expression is an unevaluated
418 // operand (Clause 5).
419 //
420 // Note that we can't tell whether the expression is an lvalue of a
421 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000422 // we the expression is potentially potentially evaluated.
423 EnterExpressionEvaluationContext Unevaluated(Actions,
424 Action::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000425 Result = ParseExpression();
426
427 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000428 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000429 SkipUntil(tok::r_paren);
430 else {
431 MatchRHSPunctuation(tok::r_paren, LParenLoc);
432
433 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000434 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000435 }
436 }
437
Sebastian Redl20df9b72008-12-11 22:51:44 +0000438 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000439}
440
Reid Spencer5f016e22007-07-11 17:01:13 +0000441/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
442///
443/// boolean-literal: [C++ 2.13.5]
444/// 'true'
445/// 'false'
Sebastian Redl20df9b72008-12-11 22:51:44 +0000446Parser::OwningExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000447 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000448 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000449}
Chris Lattner50dd2892008-02-26 00:51:44 +0000450
451/// ParseThrowExpression - This handles the C++ throw expression.
452///
453/// throw-expression: [C++ 15]
454/// 'throw' assignment-expression[opt]
Sebastian Redl20df9b72008-12-11 22:51:44 +0000455Parser::OwningExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000456 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000457 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000458
Chris Lattner2a2819a2008-04-06 06:02:23 +0000459 // If the current token isn't the start of an assignment-expression,
460 // then the expression is not present. This handles things like:
461 // "C ? throw : (void)42", which is crazy but legal.
462 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
463 case tok::semi:
464 case tok::r_paren:
465 case tok::r_square:
466 case tok::r_brace:
467 case tok::colon:
468 case tok::comma:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000469 return Actions.ActOnCXXThrow(ThrowLoc, ExprArg(Actions));
Chris Lattner50dd2892008-02-26 00:51:44 +0000470
Chris Lattner2a2819a2008-04-06 06:02:23 +0000471 default:
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000472 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000473 if (Expr.isInvalid()) return move(Expr);
Sebastian Redlf53597f2009-03-15 17:47:39 +0000474 return Actions.ActOnCXXThrow(ThrowLoc, move(Expr));
Chris Lattner2a2819a2008-04-06 06:02:23 +0000475 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000476}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000477
478/// ParseCXXThis - This handles the C++ 'this' pointer.
479///
480/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
481/// a non-lvalue expression whose value is the address of the object for which
482/// the function is called.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000483Parser::OwningExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000484 assert(Tok.is(tok::kw_this) && "Not 'this'!");
485 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000486 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000487}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000488
489/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
490/// Can be interpreted either as function-style casting ("int(x)")
491/// or class type construction ("ClassType(x,y,z)")
492/// or creation of a value-initialized type ("int()").
493///
494/// postfix-expression: [C++ 5.2p1]
495/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
496/// typename-specifier '(' expression-list[opt] ')' [TODO]
497///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000498Parser::OwningExprResult
499Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000500 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000501 TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000502
503 assert(Tok.is(tok::l_paren) && "Expected '('!");
504 SourceLocation LParenLoc = ConsumeParen();
505
Sebastian Redla55e52c2008-11-25 22:21:31 +0000506 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000507 CommaLocsTy CommaLocs;
508
509 if (Tok.isNot(tok::r_paren)) {
510 if (ParseExpressionList(Exprs, CommaLocs)) {
511 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000512 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000513 }
514 }
515
516 // Match the ')'.
517 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
518
Sebastian Redlef0cb8e2009-07-29 13:50:23 +0000519 // TypeRep could be null, if it references an invalid typedef.
520 if (!TypeRep)
521 return ExprError();
522
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000523 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
524 "Unexpected number of commas!");
Sebastian Redlf53597f2009-03-15 17:47:39 +0000525 return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
526 LParenLoc, move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000527 CommaLocs.data(), RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000528}
529
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000530/// ParseCXXCondition - if/switch/while/for condition expression.
531///
532/// condition:
533/// expression
534/// type-specifier-seq declarator '=' assignment-expression
535/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
536/// '=' assignment-expression
537///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000538Parser::OwningExprResult Parser::ParseCXXCondition() {
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000539 if (!isCXXConditionDeclaration())
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000540 return ParseExpression(); // expression
541
542 SourceLocation StartLoc = Tok.getLocation();
543
544 // type-specifier-seq
545 DeclSpec DS;
546 ParseSpecifierQualifierList(DS);
547
548 // declarator
549 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
550 ParseDeclarator(DeclaratorInfo);
551
552 // simple-asm-expr[opt]
553 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000554 SourceLocation Loc;
555 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000556 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000557 SkipUntil(tok::semi);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000558 return ExprError();
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000559 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000560 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000561 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000562 }
563
564 // If attributes are present, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000565 if (Tok.is(tok::kw___attribute)) {
566 SourceLocation Loc;
567 AttributeList *AttrList = ParseAttributes(&Loc);
568 DeclaratorInfo.AddAttributes(AttrList, Loc);
569 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000570
571 // '=' assignment-expression
572 if (Tok.isNot(tok::equal))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000573 return ExprError(Diag(Tok, diag::err_expected_equal_after_declarator));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000574 SourceLocation EqualLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000575 OwningExprResult AssignExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000576 if (AssignExpr.isInvalid())
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000577 return ExprError();
578
Sebastian Redlf53597f2009-03-15 17:47:39 +0000579 return Actions.ActOnCXXConditionDeclarationExpr(CurScope, StartLoc,
580 DeclaratorInfo,EqualLoc,
581 move(AssignExpr));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000582}
583
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000584/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
585/// This should only be called when the current token is known to be part of
586/// simple-type-specifier.
587///
588/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000589/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000590/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
591/// char
592/// wchar_t
593/// bool
594/// short
595/// int
596/// long
597/// signed
598/// unsigned
599/// float
600/// double
601/// void
602/// [GNU] typeof-specifier
603/// [C++0x] auto [TODO]
604///
605/// type-name:
606/// class-name
607/// enum-name
608/// typedef-name
609///
610void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
611 DS.SetRangeStart(Tok.getLocation());
612 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000613 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000614 SourceLocation Loc = Tok.getLocation();
615
616 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000617 case tok::identifier: // foo::bar
618 case tok::coloncolon: // ::foo::bar
619 assert(0 && "Annotation token should already be formed!");
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000620 default:
621 assert(0 && "Not a simple-type-specifier token!");
622 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000623
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000624 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000625 case tok::annot_typename: {
John McCallfec54012009-08-03 20:12:06 +0000626 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000627 Tok.getAnnotationValue());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000628 break;
629 }
630
631 // builtin types
632 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +0000633 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000634 break;
635 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +0000636 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000637 break;
638 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +0000639 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000640 break;
641 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +0000642 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000643 break;
644 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +0000645 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000646 break;
647 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +0000648 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000649 break;
650 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +0000651 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000652 break;
653 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +0000654 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000655 break;
656 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +0000657 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000658 break;
659 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +0000660 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000661 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000662 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +0000663 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000664 break;
665 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +0000666 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000667 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000668 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +0000669 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000670 break;
671
672 // GNU typeof support.
673 case tok::kw_typeof:
674 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000675 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000676 return;
677 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000678 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000679 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
680 else
681 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000682 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000683 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000684}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000685
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000686/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
687/// [dcl.name]), which is a non-empty sequence of type-specifiers,
688/// e.g., "const short int". Note that the DeclSpec is *not* finished
689/// by parsing the type-specifier-seq, because these sequences are
690/// typically followed by some form of declarator. Returns true and
691/// emits diagnostics if this is not a type-specifier-seq, false
692/// otherwise.
693///
694/// type-specifier-seq: [C++ 8.1]
695/// type-specifier type-specifier-seq[opt]
696///
697bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
698 DS.SetRangeStart(Tok.getLocation());
699 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000700 unsigned DiagID;
701 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000702
703 // Parse one or more of the type specifiers.
John McCallfec54012009-08-03 20:12:06 +0000704 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000705 Diag(Tok, diag::err_operator_missing_type_specifier);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000706 return true;
707 }
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000708
John McCallfec54012009-08-03 20:12:06 +0000709 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) ;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000710
711 return false;
712}
713
Douglas Gregor43c7bad2008-11-17 16:14:12 +0000714/// TryParseOperatorFunctionId - Attempts to parse a C++ overloaded
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000715/// operator name (C++ [over.oper]). If successful, returns the
716/// predefined identifier that corresponds to that overloaded
717/// operator. Otherwise, returns NULL and does not consume any tokens.
718///
719/// operator-function-id: [C++ 13.5]
720/// 'operator' operator
721///
722/// operator: one of
723/// new delete new[] delete[]
724/// + - * / % ^ & | ~
725/// ! = < > += -= *= /= %=
726/// ^= &= |= << >> >>= <<= == !=
727/// <= >= && || ++ -- , ->* ->
728/// () []
Sebastian Redlab197ba2009-02-09 18:23:29 +0000729OverloadedOperatorKind
730Parser::TryParseOperatorFunctionId(SourceLocation *EndLoc) {
Argyrios Kyrtzidis9057a812008-11-07 15:54:02 +0000731 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
Sebastian Redlab197ba2009-02-09 18:23:29 +0000732 SourceLocation Loc;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000733
734 OverloadedOperatorKind Op = OO_None;
735 switch (NextToken().getKind()) {
736 case tok::kw_new:
737 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000738 Loc = ConsumeToken(); // 'new'
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000739 if (Tok.is(tok::l_square)) {
740 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +0000741 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000742 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
743 Op = OO_Array_New;
744 } else {
745 Op = OO_New;
746 }
Sebastian Redlab197ba2009-02-09 18:23:29 +0000747 if (EndLoc)
748 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000749 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000750
751 case tok::kw_delete:
752 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000753 Loc = ConsumeToken(); // 'delete'
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000754 if (Tok.is(tok::l_square)) {
755 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +0000756 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000757 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
758 Op = OO_Array_Delete;
759 } else {
760 Op = OO_Delete;
761 }
Sebastian Redlab197ba2009-02-09 18:23:29 +0000762 if (EndLoc)
763 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000764 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000765
Douglas Gregor02bcd4c2008-11-10 13:38:07 +0000766#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000767 case tok::Token: Op = OO_##Name; break;
Douglas Gregor02bcd4c2008-11-10 13:38:07 +0000768#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000769#include "clang/Basic/OperatorKinds.def"
770
771 case tok::l_paren:
772 ConsumeToken(); // 'operator'
773 ConsumeParen(); // '('
Sebastian Redlab197ba2009-02-09 18:23:29 +0000774 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000775 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); // ')'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000776 if (EndLoc)
777 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000778 return OO_Call;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000779
780 case tok::l_square:
781 ConsumeToken(); // 'operator'
782 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +0000783 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000784 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000785 if (EndLoc)
786 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000787 return OO_Subscript;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000788
789 default:
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000790 return OO_None;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000791 }
792
Douglas Gregor43c7bad2008-11-17 16:14:12 +0000793 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000794 Loc = ConsumeAnyToken(); // the operator itself
795 if (EndLoc)
796 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000797 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000798}
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000799
800/// ParseConversionFunctionId - Parse a C++ conversion-function-id,
801/// which expresses the name of a user-defined conversion operator
802/// (C++ [class.conv.fct]p1). Returns the type that this operator is
803/// specifying a conversion for, or NULL if there was an error.
804///
805/// conversion-function-id: [C++ 12.3.2]
806/// operator conversion-type-id
807///
808/// conversion-type-id:
809/// type-specifier-seq conversion-declarator[opt]
810///
811/// conversion-declarator:
812/// ptr-operator conversion-declarator[opt]
Sebastian Redlab197ba2009-02-09 18:23:29 +0000813Parser::TypeTy *Parser::ParseConversionFunctionId(SourceLocation *EndLoc) {
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000814 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
815 ConsumeToken(); // 'operator'
816
817 // Parse the type-specifier-seq.
818 DeclSpec DS;
819 if (ParseCXXTypeSpecifierSeq(DS))
820 return 0;
821
822 // Parse the conversion-declarator, which is merely a sequence of
823 // ptr-operators.
824 Declarator D(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000825 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000826 if (EndLoc)
827 *EndLoc = D.getSourceRange().getEnd();
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000828
829 // Finish up the type.
830 Action::TypeResult Result = Actions.ActOnTypeName(CurScope, D);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000831 if (Result.isInvalid())
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000832 return 0;
833 else
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000834 return Result.get();
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000835}
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000836
837/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
838/// memory in a typesafe manner and call constructors.
Chris Lattner59232d32009-01-04 21:25:24 +0000839///
840/// This method is called to parse the new expression after the optional :: has
841/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
842/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000843///
844/// new-expression:
845/// '::'[opt] 'new' new-placement[opt] new-type-id
846/// new-initializer[opt]
847/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
848/// new-initializer[opt]
849///
850/// new-placement:
851/// '(' expression-list ')'
852///
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000853/// new-type-id:
854/// type-specifier-seq new-declarator[opt]
855///
856/// new-declarator:
857/// ptr-operator new-declarator[opt]
858/// direct-new-declarator
859///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000860/// new-initializer:
861/// '(' expression-list[opt] ')'
862/// [C++0x] braced-init-list [TODO]
863///
Chris Lattner59232d32009-01-04 21:25:24 +0000864Parser::OwningExprResult
865Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
866 assert(Tok.is(tok::kw_new) && "expected 'new' token");
867 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000868
869 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
870 // second form of new-expression. It can't be a new-type-id.
871
Sebastian Redla55e52c2008-11-25 22:21:31 +0000872 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000873 SourceLocation PlacementLParen, PlacementRParen;
874
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000875 bool ParenTypeId;
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000876 DeclSpec DS;
877 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000878 if (Tok.is(tok::l_paren)) {
879 // If it turns out to be a placement, we change the type location.
880 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000881 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
882 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000883 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000884 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000885
886 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000887 if (PlacementRParen.isInvalid()) {
888 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000889 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000890 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000891
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000892 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000893 // Reset the placement locations. There was no placement.
894 PlacementLParen = PlacementRParen = SourceLocation();
895 ParenTypeId = true;
896 } else {
897 // We still need the type.
898 if (Tok.is(tok::l_paren)) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000899 SourceLocation LParen = ConsumeParen();
900 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000901 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000902 ParseDeclarator(DeclaratorInfo);
903 MatchRHSPunctuation(tok::r_paren, LParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000904 ParenTypeId = true;
905 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000906 if (ParseCXXTypeSpecifierSeq(DS))
907 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000908 else {
909 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000910 ParseDeclaratorInternal(DeclaratorInfo,
911 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000912 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000913 ParenTypeId = false;
914 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000915 }
916 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000917 // A new-type-id is a simplified type-id, where essentially the
918 // direct-declarator is replaced by a direct-new-declarator.
919 if (ParseCXXTypeSpecifierSeq(DS))
920 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000921 else {
922 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000923 ParseDeclaratorInternal(DeclaratorInfo,
924 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000925 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000926 ParenTypeId = false;
927 }
Chris Lattnereaaebc72009-04-25 08:06:05 +0000928 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000929 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000930 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000931 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000932
Sebastian Redla55e52c2008-11-25 22:21:31 +0000933 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000934 SourceLocation ConstructorLParen, ConstructorRParen;
935
936 if (Tok.is(tok::l_paren)) {
937 ConstructorLParen = ConsumeParen();
938 if (Tok.isNot(tok::r_paren)) {
939 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000940 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
941 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000942 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000943 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000944 }
945 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000946 if (ConstructorRParen.isInvalid()) {
947 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000948 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000949 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000950 }
951
Sebastian Redlf53597f2009-03-15 17:47:39 +0000952 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
953 move_arg(PlacementArgs), PlacementRParen,
954 ParenTypeId, DeclaratorInfo, ConstructorLParen,
955 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000956}
957
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000958/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
959/// passed to ParseDeclaratorInternal.
960///
961/// direct-new-declarator:
962/// '[' expression ']'
963/// direct-new-declarator '[' constant-expression ']'
964///
Chris Lattner59232d32009-01-04 21:25:24 +0000965void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000966 // Parse the array dimensions.
967 bool first = true;
968 while (Tok.is(tok::l_square)) {
969 SourceLocation LLoc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000970 OwningExprResult Size(first ? ParseExpression()
971 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000972 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000973 // Recover
974 SkipUntil(tok::r_square);
975 return;
976 }
977 first = false;
978
Sebastian Redlab197ba2009-02-09 18:23:29 +0000979 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000980 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000981 Size.release(), LLoc, RLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +0000982 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000983
Sebastian Redlab197ba2009-02-09 18:23:29 +0000984 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000985 return;
986 }
987}
988
989/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
990/// This ambiguity appears in the syntax of the C++ new operator.
991///
992/// new-expression:
993/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
994/// new-initializer[opt]
995///
996/// new-placement:
997/// '(' expression-list ')'
998///
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000999bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001000 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001001 // The '(' was already consumed.
1002 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001003 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001004 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001005 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001006 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001007 }
1008
1009 // It's not a type, it has to be an expression list.
1010 // Discard the comma locations - ActOnCXXNew has enough parameters.
1011 CommaLocsTy CommaLocs;
1012 return ParseExpressionList(PlacementArgs, CommaLocs);
1013}
1014
1015/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1016/// to free memory allocated by new.
1017///
Chris Lattner59232d32009-01-04 21:25:24 +00001018/// This method is called to parse the 'delete' expression after the optional
1019/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1020/// and "Start" is its location. Otherwise, "Start" is the location of the
1021/// 'delete' token.
1022///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001023/// delete-expression:
1024/// '::'[opt] 'delete' cast-expression
1025/// '::'[opt] 'delete' '[' ']' cast-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001026Parser::OwningExprResult
1027Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1028 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1029 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001030
1031 // Array delete?
1032 bool ArrayDelete = false;
1033 if (Tok.is(tok::l_square)) {
1034 ArrayDelete = true;
1035 SourceLocation LHS = ConsumeBracket();
1036 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1037 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001038 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001039 }
1040
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001041 OwningExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001042 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001043 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001044
Sebastian Redlf53597f2009-03-15 17:47:39 +00001045 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001046}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001047
1048static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind)
1049{
1050 switch(kind) {
1051 default: assert(false && "Not a known unary type trait.");
1052 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1053 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1054 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1055 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1056 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1057 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1058 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1059 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1060 case tok::kw___is_abstract: return UTT_IsAbstract;
1061 case tok::kw___is_class: return UTT_IsClass;
1062 case tok::kw___is_empty: return UTT_IsEmpty;
1063 case tok::kw___is_enum: return UTT_IsEnum;
1064 case tok::kw___is_pod: return UTT_IsPOD;
1065 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1066 case tok::kw___is_union: return UTT_IsUnion;
1067 }
1068}
1069
1070/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1071/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1072/// templates.
1073///
1074/// primary-expression:
1075/// [GNU] unary-type-trait '(' type-id ')'
1076///
1077Parser::OwningExprResult Parser::ParseUnaryTypeTrait()
1078{
1079 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1080 SourceLocation Loc = ConsumeToken();
1081
1082 SourceLocation LParen = Tok.getLocation();
1083 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1084 return ExprError();
1085
1086 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1087 // there will be cryptic errors about mismatched parentheses and missing
1088 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001089 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001090
1091 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1092
Douglas Gregor809070a2009-02-18 17:45:20 +00001093 if (Ty.isInvalid())
1094 return ExprError();
1095
1096 return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001097}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001098
1099/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1100/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1101/// based on the context past the parens.
1102Parser::OwningExprResult
1103Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
1104 TypeTy *&CastTy,
1105 SourceLocation LParenLoc,
1106 SourceLocation &RParenLoc) {
1107 assert(getLang().CPlusPlus && "Should only be called for C++!");
1108 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1109 assert(isTypeIdInParens() && "Not a type-id!");
1110
1111 OwningExprResult Result(Actions, true);
1112 CastTy = 0;
1113
1114 // We need to disambiguate a very ugly part of the C++ syntax:
1115 //
1116 // (T())x; - type-id
1117 // (T())*x; - type-id
1118 // (T())/x; - expression
1119 // (T()); - expression
1120 //
1121 // The bad news is that we cannot use the specialized tentative parser, since
1122 // it can only verify that the thing inside the parens can be parsed as
1123 // type-id, it is not useful for determining the context past the parens.
1124 //
1125 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00001126 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001127 //
1128 // It uses a scheme similar to parsing inline methods. The parenthesized
1129 // tokens are cached, the context that follows is determined (possibly by
1130 // parsing a cast-expression), and then we re-introduce the cached tokens
1131 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001132
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001133 ParenParseOption ParseAs;
1134 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001135
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001136 // Store the tokens of the parentheses. We will parse them after we determine
1137 // the context that follows them.
1138 if (!ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks, tok::semi)) {
1139 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001140 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1141 return ExprError();
1142 }
1143
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001144 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001145 ParseAs = CompoundLiteral;
1146 } else {
1147 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001148 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1149 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1150 NotCastExpr = true;
1151 } else {
1152 // Try parsing the cast-expression that may follow.
1153 // If it is not a cast-expression, NotCastExpr will be true and no token
1154 // will be consumed.
1155 Result = ParseCastExpression(false/*isUnaryExpression*/,
1156 false/*isAddressofOperand*/,
1157 NotCastExpr);
1158 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001159
1160 // If we parsed a cast-expression, it's really a type-id, otherwise it's
1161 // an expression.
1162 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001163 }
1164
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001165 // The current token should go after the cached tokens.
1166 Toks.push_back(Tok);
1167 // Re-enter the stored parenthesized tokens into the token stream, so we may
1168 // parse them now.
1169 PP.EnterTokenStream(Toks.data(), Toks.size(),
1170 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1171 // Drop the current token and bring the first cached one. It's the same token
1172 // as when we entered this function.
1173 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001174
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001175 if (ParseAs >= CompoundLiteral) {
1176 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001177
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001178 // Match the ')'.
1179 if (Tok.is(tok::r_paren))
1180 RParenLoc = ConsumeParen();
1181 else
1182 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001183
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001184 if (ParseAs == CompoundLiteral) {
1185 ExprType = CompoundLiteral;
1186 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1187 }
1188
1189 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1190 assert(ParseAs == CastExpr);
1191
1192 if (Ty.isInvalid())
1193 return ExprError();
1194
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001195 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001196
1197 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001198 if (!Result.isInvalid())
1199 Result = Actions.ActOnCastExpr(LParenLoc, CastTy, RParenLoc,move(Result));
1200 return move(Result);
1201 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001202
1203 // Not a compound literal, and not followed by a cast-expression.
1204 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001205
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001206 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001207 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001208 if (!Result.isInvalid() && Tok.is(tok::r_paren))
1209 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result));
1210
1211 // Match the ')'.
1212 if (Result.isInvalid()) {
1213 SkipUntil(tok::r_paren);
1214 return ExprError();
1215 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001216
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001217 if (Tok.is(tok::r_paren))
1218 RParenLoc = ConsumeParen();
1219 else
1220 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1221
1222 return move(Result);
1223}