blob: 4cd952e393f11d203836c1b7bbcf8047ea5af21d [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
Douglas Gregor2dd078a2009-09-02 22:59:36 +000019/// \brief Parse global scope or nested-name-specifier if present.
20///
21/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
22/// may be preceded by '::'). Note that this routine will not parse ::new or
23/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000024///
25/// '::'[opt] nested-name-specifier
26/// '::'
27///
28/// nested-name-specifier:
29/// type-name '::'
30/// namespace-name '::'
31/// nested-name-specifier identifier '::'
Douglas Gregor2dd078a2009-09-02 22:59:36 +000032/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000033///
Douglas Gregor2dd078a2009-09-02 22:59:36 +000034///
35/// \param SS the scope specifier that will be set to the parsed
36/// nested-name-specifier (or empty)
37///
38/// \param ObjectType if this nested-name-specifier is being parsed following
39/// the "." or "->" of a member access expression, this parameter provides the
40/// type of the object whose members are being accessed.
41///
42/// \param EnteringContext whether we will be entering into the context of
43/// the nested-name-specifier after parsing it.
44///
45/// \returns true if a scope specifier was parsed.
Douglas Gregor495c35d2009-08-25 22:51:20 +000046bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
Douglas Gregor2dd078a2009-09-02 22:59:36 +000047 Action::TypeTy *ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +000048 bool EnteringContext) {
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000049 assert(getLang().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +000050 "Call sites of this function should be guarded by checking for C++");
Douglas Gregor495c35d2009-08-25 22:51:20 +000051
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000052 if (Tok.is(tok::annot_cxxscope)) {
Douglas Gregor35073692009-03-26 23:56:24 +000053 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000054 SS.setRange(Tok.getAnnotationRange());
55 ConsumeToken();
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +000056 return true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000057 }
Chris Lattnere607e802009-01-04 21:14:15 +000058
Douglas Gregor39a8de12009-02-25 19:37:18 +000059 bool HasScopeSpecifier = false;
60
Chris Lattner5b454732009-01-05 03:55:46 +000061 if (Tok.is(tok::coloncolon)) {
62 // ::new and ::delete aren't nested-name-specifiers.
63 tok::TokenKind NextKind = NextToken().getKind();
64 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
65 return false;
Chris Lattner55a7cef2009-01-05 00:13:00 +000066
Chris Lattner55a7cef2009-01-05 00:13:00 +000067 // '::' - Global scope qualifier.
Chris Lattner357089d2009-01-05 02:07:19 +000068 SourceLocation CCLoc = ConsumeToken();
Chris Lattner357089d2009-01-05 02:07:19 +000069 SS.setBeginLoc(CCLoc);
Douglas Gregor35073692009-03-26 23:56:24 +000070 SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, CCLoc));
Chris Lattner357089d2009-01-05 02:07:19 +000071 SS.setEndLoc(CCLoc);
Douglas Gregor39a8de12009-02-25 19:37:18 +000072 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +000073 }
74
Douglas Gregor39a8de12009-02-25 19:37:18 +000075 while (true) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +000076 if (HasScopeSpecifier) {
77 // C++ [basic.lookup.classref]p5:
78 // If the qualified-id has the form
79 // ::class-name-or-namespace-name::...
80 // the class-name-or-namespace-name is looked up in global scope as a
81 // class-name or namespace-name.
82 //
83 // To implement this, we clear out the object type as soon as we've
84 // seen a leading '::' or part of a nested-name-specifier.
85 ObjectType = 0;
86 }
87
Douglas Gregor39a8de12009-02-25 19:37:18 +000088 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +000089 // nested-name-specifier 'template'[opt] simple-template-id '::'
90
91 // Parse the optional 'template' keyword, then make sure we have
92 // 'identifier <' after it.
93 if (Tok.is(tok::kw_template)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +000094 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +000095 // nested-name-specifier, since they aren't allowed to start with
96 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +000097 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +000098 break;
99
Chris Lattner77cf72a2009-06-26 03:47:46 +0000100 SourceLocation TemplateKWLoc = ConsumeToken();
101
102 if (Tok.isNot(tok::identifier)) {
103 Diag(Tok.getLocation(),
104 diag::err_id_after_template_in_nested_name_spec)
105 << SourceRange(TemplateKWLoc);
106 break;
107 }
108
109 if (NextToken().isNot(tok::less)) {
110 Diag(NextToken().getLocation(),
111 diag::err_less_after_template_name_in_nested_name_spec)
112 << Tok.getIdentifierInfo()->getName()
113 << SourceRange(TemplateKWLoc, Tok.getLocation());
114 break;
115 }
116
117 TemplateTy Template
118 = Actions.ActOnDependentTemplateName(TemplateKWLoc,
119 *Tok.getIdentifierInfo(),
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000120 Tok.getLocation(), SS,
121 ObjectType);
Eli Friedmaneab975d2009-08-29 04:08:08 +0000122 if (!Template)
123 break;
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000124 if (AnnotateTemplateIdToken(Template, TNK_Dependent_template_name,
125 &SS, TemplateKWLoc, false))
126 break;
127
Chris Lattner77cf72a2009-06-26 03:47:46 +0000128 continue;
129 }
130
Douglas Gregor39a8de12009-02-25 19:37:18 +0000131 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
132 // We have
133 //
134 // simple-template-id '::'
135 //
136 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000137 // right kind (it should name a type or be dependent), and then
138 // convert it into a type within the nested-name-specifier.
Douglas Gregor39a8de12009-02-25 19:37:18 +0000139 TemplateIdAnnotation *TemplateId
140 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
141
Douglas Gregorc45c2322009-03-31 00:43:58 +0000142 if (TemplateId->Kind == TNK_Type_template ||
143 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000144 AnnotateTemplateIdTokenAsType(&SS);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000145
146 assert(Tok.is(tok::annot_typename) &&
147 "AnnotateTemplateIdTokenAsType isn't working");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000148 Token TypeToken = Tok;
149 ConsumeToken();
150 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
151 SourceLocation CCLoc = ConsumeToken();
152
153 if (!HasScopeSpecifier) {
154 SS.setBeginLoc(TypeToken.getLocation());
155 HasScopeSpecifier = true;
156 }
Douglas Gregor31a19b62009-04-01 21:51:26 +0000157
158 if (TypeToken.getAnnotationValue())
159 SS.setScopeRep(
160 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS,
161 TypeToken.getAnnotationValue(),
162 TypeToken.getAnnotationRange(),
163 CCLoc));
164 else
165 SS.setScopeRep(0);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000166 SS.setEndLoc(CCLoc);
167 continue;
Chris Lattner67b9e832009-06-26 03:45:46 +0000168 }
169
170 assert(false && "FIXME: Only type template names supported here");
Douglas Gregor39a8de12009-02-25 19:37:18 +0000171 }
172
Chris Lattner5c7f7862009-06-26 03:52:38 +0000173
174 // The rest of the nested-name-specifier possibilities start with
175 // tok::identifier.
176 if (Tok.isNot(tok::identifier))
177 break;
178
179 IdentifierInfo &II = *Tok.getIdentifierInfo();
180
181 // nested-name-specifier:
182 // type-name '::'
183 // namespace-name '::'
184 // nested-name-specifier identifier '::'
185 Token Next = NextToken();
186 if (Next.is(tok::coloncolon)) {
187 // We have an identifier followed by a '::'. Lookup this name
188 // as the name in a nested-name-specifier.
189 SourceLocation IdLoc = ConsumeToken();
190 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
191 SourceLocation CCLoc = ConsumeToken();
192
193 if (!HasScopeSpecifier) {
194 SS.setBeginLoc(IdLoc);
195 HasScopeSpecifier = true;
196 }
197
198 if (SS.isInvalid())
199 continue;
200
201 SS.setScopeRep(
Douglas Gregor495c35d2009-08-25 22:51:20 +0000202 Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, II,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000203 ObjectType, EnteringContext));
Chris Lattner5c7f7862009-06-26 03:52:38 +0000204 SS.setEndLoc(CCLoc);
205 continue;
206 }
207
208 // nested-name-specifier:
209 // type-name '<'
210 if (Next.is(tok::less)) {
211 TemplateTy Template;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000212 if (TemplateNameKind TNK = Actions.isTemplateName(CurScope, II,
213 Tok.getLocation(),
214 &SS,
215 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000216 EnteringContext,
217 Template)) {
Chris Lattner5c7f7862009-06-26 03:52:38 +0000218 // We have found a template name, so annotate this this token
219 // with a template-id annotation. We do not permit the
220 // template-id to be translated into a type annotation,
221 // because some clients (e.g., the parsing of class template
222 // specializations) still want to see the original template-id
223 // token.
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000224 if (AnnotateTemplateIdToken(Template, TNK, &SS, SourceLocation(),
225 false))
226 break;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000227 continue;
228 }
229 }
230
Douglas Gregor39a8de12009-02-25 19:37:18 +0000231 // We don't have any tokens that form the beginning of a
232 // nested-name-specifier, so we're done.
233 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000234 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000235
236 return HasScopeSpecifier;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000237}
238
239/// ParseCXXIdExpression - Handle id-expression.
240///
241/// id-expression:
242/// unqualified-id
243/// qualified-id
244///
245/// unqualified-id:
246/// identifier
247/// operator-function-id
248/// conversion-function-id [TODO]
249/// '~' class-name [TODO]
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000250/// template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000251///
252/// qualified-id:
253/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
254/// '::' identifier
255/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000256/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000257///
258/// nested-name-specifier:
259/// type-name '::'
260/// namespace-name '::'
261/// nested-name-specifier identifier '::'
262/// nested-name-specifier 'template'[opt] simple-template-id '::' [TODO]
263///
264/// NOTE: The standard specifies that, for qualified-id, the parser does not
265/// expect:
266///
267/// '::' conversion-function-id
268/// '::' '~' class-name
269///
270/// This may cause a slight inconsistency on diagnostics:
271///
272/// class C {};
273/// namespace A {}
274/// void f() {
275/// :: A :: ~ C(); // Some Sema error about using destructor with a
276/// // namespace.
277/// :: ~ C(); // Some Parser error like 'unexpected ~'.
278/// }
279///
280/// We simplify the parser a bit and make it work like:
281///
282/// qualified-id:
283/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
284/// '::' unqualified-id
285///
286/// That way Sema can handle and report similar errors for namespaces and the
287/// global scope.
288///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000289/// The isAddressOfOperand parameter indicates that this id-expression is a
290/// direct operand of the address-of operator. This is, besides member contexts,
291/// the only place where a qualified-id naming a non-static class member may
292/// appear.
293///
294Parser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000295 // qualified-id:
296 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
297 // '::' unqualified-id
298 //
299 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000300 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000301
302 // unqualified-id:
303 // identifier
304 // operator-function-id
Douglas Gregor2def4832008-11-17 20:34:05 +0000305 // conversion-function-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000306 // '~' class-name [TODO]
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000307 // template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000308 //
309 switch (Tok.getKind()) {
310 default:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000311 return ExprError(Diag(Tok, diag::err_expected_unqualified_id));
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000312
313 case tok::identifier: {
314 // Consume the identifier so that we can see if it is followed by a '('.
315 IdentifierInfo &II = *Tok.getIdentifierInfo();
316 SourceLocation L = ConsumeToken();
Sebastian Redlebc07d52009-02-03 20:19:35 +0000317 return Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren),
318 &SS, isAddressOfOperand);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000319 }
320
321 case tok::kw_operator: {
322 SourceLocation OperatorLoc = Tok.getLocation();
Chris Lattner7452c6f2009-01-05 01:24:05 +0000323 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId())
Sebastian Redlcd965b92009-01-18 18:53:16 +0000324 return Actions.ActOnCXXOperatorFunctionIdExpr(
Sebastian Redlebc07d52009-02-03 20:19:35 +0000325 CurScope, OperatorLoc, Op, Tok.is(tok::l_paren), SS,
326 isAddressOfOperand);
Chris Lattner7452c6f2009-01-05 01:24:05 +0000327 if (TypeTy *Type = ParseConversionFunctionId())
Sebastian Redlcd965b92009-01-18 18:53:16 +0000328 return Actions.ActOnCXXConversionFunctionExpr(CurScope, OperatorLoc, Type,
Sebastian Redlebc07d52009-02-03 20:19:35 +0000329 Tok.is(tok::l_paren), SS,
330 isAddressOfOperand);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000331
Douglas Gregor2def4832008-11-17 20:34:05 +0000332 // We already complained about a bad conversion-function-id,
333 // above.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000334 return ExprError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000335 }
336
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000337 case tok::annot_template_id: {
338 TemplateIdAnnotation *TemplateId
339 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
340 assert((TemplateId->Kind == TNK_Function_template ||
341 TemplateId->Kind == TNK_Dependent_template_name) &&
342 "A template type name is not an ID expression");
343
344 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
345 TemplateId->getTemplateArgs(),
346 TemplateId->getTemplateArgIsType(),
347 TemplateId->NumArgs);
348
349 OwningExprResult Result
350 = Actions.ActOnTemplateIdExpr(TemplateTy::make(TemplateId->Template),
351 TemplateId->TemplateNameLoc,
352 TemplateId->LAngleLoc,
353 TemplateArgsPtr,
354 TemplateId->getTemplateArgLocations(),
355 TemplateId->RAngleLoc);
356 ConsumeToken(); // Consume the template-id token
357 return move(Result);
358 }
359
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000360 } // switch.
361
362 assert(0 && "The switch was supposed to take care everything.");
363}
364
Reid Spencer5f016e22007-07-11 17:01:13 +0000365/// ParseCXXCasts - This handles the various ways to cast expressions to another
366/// type.
367///
368/// postfix-expression: [C++ 5.2p1]
369/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
370/// 'static_cast' '<' type-name '>' '(' expression ')'
371/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
372/// 'const_cast' '<' type-name '>' '(' expression ')'
373///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000374Parser::OwningExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000375 tok::TokenKind Kind = Tok.getKind();
376 const char *CastName = 0; // For error messages
377
378 switch (Kind) {
379 default: assert(0 && "Unknown C++ cast!"); abort();
380 case tok::kw_const_cast: CastName = "const_cast"; break;
381 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
382 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
383 case tok::kw_static_cast: CastName = "static_cast"; break;
384 }
385
386 SourceLocation OpLoc = ConsumeToken();
387 SourceLocation LAngleBracketLoc = Tok.getLocation();
388
389 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000390 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000391
Douglas Gregor809070a2009-02-18 17:45:20 +0000392 TypeResult CastTy = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000393 SourceLocation RAngleBracketLoc = Tok.getLocation();
394
Chris Lattner1ab3b962008-11-18 07:48:38 +0000395 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000396 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +0000397
398 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
399
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000400 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName))
401 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000402
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +0000403 OwningExprResult Result = ParseExpression();
404
405 // Match the ')'.
406 if (Result.isInvalid())
407 SkipUntil(tok::r_paren);
408
409 if (Tok.is(tok::r_paren))
410 RParenLoc = ConsumeParen();
411 else
412 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000413
Douglas Gregor809070a2009-02-18 17:45:20 +0000414 if (!Result.isInvalid() && !CastTy.isInvalid())
Douglas Gregor49badde2008-10-27 19:41:14 +0000415 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000416 LAngleBracketLoc, CastTy.get(),
Douglas Gregor809070a2009-02-18 17:45:20 +0000417 RAngleBracketLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000418 LParenLoc, move(Result), RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000419
Sebastian Redl20df9b72008-12-11 22:51:44 +0000420 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000421}
422
Sebastian Redlc42e1182008-11-11 11:37:55 +0000423/// ParseCXXTypeid - This handles the C++ typeid expression.
424///
425/// postfix-expression: [C++ 5.2p1]
426/// 'typeid' '(' expression ')'
427/// 'typeid' '(' type-id ')'
428///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000429Parser::OwningExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +0000430 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
431
432 SourceLocation OpLoc = ConsumeToken();
433 SourceLocation LParenLoc = Tok.getLocation();
434 SourceLocation RParenLoc;
435
436 // typeid expressions are always parenthesized.
437 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
438 "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000439 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000440
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000441 OwningExprResult Result(Actions);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000442
443 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +0000444 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000445
446 // Match the ')'.
447 MatchRHSPunctuation(tok::r_paren, LParenLoc);
448
Douglas Gregor809070a2009-02-18 17:45:20 +0000449 if (Ty.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +0000450 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +0000451
452 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
Douglas Gregor809070a2009-02-18 17:45:20 +0000453 Ty.get(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000454 } else {
Douglas Gregore0762c92009-06-19 23:52:42 +0000455 // C++0x [expr.typeid]p3:
456 // When typeid is applied to an expression other than an lvalue of a
457 // polymorphic class type [...] The expression is an unevaluated
458 // operand (Clause 5).
459 //
460 // Note that we can't tell whether the expression is an lvalue of a
461 // polymorphic class type until after we've parsed the expression, so
Douglas Gregorac7610d2009-06-22 20:57:11 +0000462 // we the expression is potentially potentially evaluated.
463 EnterExpressionEvaluationContext Unevaluated(Actions,
464 Action::PotentiallyPotentiallyEvaluated);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000465 Result = ParseExpression();
466
467 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000468 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +0000469 SkipUntil(tok::r_paren);
470 else {
471 MatchRHSPunctuation(tok::r_paren, LParenLoc);
472
473 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000474 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000475 }
476 }
477
Sebastian Redl20df9b72008-12-11 22:51:44 +0000478 return move(Result);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000479}
480
Reid Spencer5f016e22007-07-11 17:01:13 +0000481/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
482///
483/// boolean-literal: [C++ 2.13.5]
484/// 'true'
485/// 'false'
Sebastian Redl20df9b72008-12-11 22:51:44 +0000486Parser::OwningExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000487 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000488 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000489}
Chris Lattner50dd2892008-02-26 00:51:44 +0000490
491/// ParseThrowExpression - This handles the C++ throw expression.
492///
493/// throw-expression: [C++ 15]
494/// 'throw' assignment-expression[opt]
Sebastian Redl20df9b72008-12-11 22:51:44 +0000495Parser::OwningExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000496 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +0000497 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000498
Chris Lattner2a2819a2008-04-06 06:02:23 +0000499 // If the current token isn't the start of an assignment-expression,
500 // then the expression is not present. This handles things like:
501 // "C ? throw : (void)42", which is crazy but legal.
502 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
503 case tok::semi:
504 case tok::r_paren:
505 case tok::r_square:
506 case tok::r_brace:
507 case tok::colon:
508 case tok::comma:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000509 return Actions.ActOnCXXThrow(ThrowLoc, ExprArg(Actions));
Chris Lattner50dd2892008-02-26 00:51:44 +0000510
Chris Lattner2a2819a2008-04-06 06:02:23 +0000511 default:
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000512 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl20df9b72008-12-11 22:51:44 +0000513 if (Expr.isInvalid()) return move(Expr);
Sebastian Redlf53597f2009-03-15 17:47:39 +0000514 return Actions.ActOnCXXThrow(ThrowLoc, move(Expr));
Chris Lattner2a2819a2008-04-06 06:02:23 +0000515 }
Chris Lattner50dd2892008-02-26 00:51:44 +0000516}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000517
518/// ParseCXXThis - This handles the C++ 'this' pointer.
519///
520/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
521/// a non-lvalue expression whose value is the address of the object for which
522/// the function is called.
Sebastian Redl20df9b72008-12-11 22:51:44 +0000523Parser::OwningExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000524 assert(Tok.is(tok::kw_this) && "Not 'this'!");
525 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000526 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000527}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000528
529/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
530/// Can be interpreted either as function-style casting ("int(x)")
531/// or class type construction ("ClassType(x,y,z)")
532/// or creation of a value-initialized type ("int()").
533///
534/// postfix-expression: [C++ 5.2p1]
535/// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
536/// typename-specifier '(' expression-list[opt] ')' [TODO]
537///
Sebastian Redl20df9b72008-12-11 22:51:44 +0000538Parser::OwningExprResult
539Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000540 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000541 TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000542
543 assert(Tok.is(tok::l_paren) && "Expected '('!");
544 SourceLocation LParenLoc = ConsumeParen();
545
Sebastian Redla55e52c2008-11-25 22:21:31 +0000546 ExprVector Exprs(Actions);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000547 CommaLocsTy CommaLocs;
548
549 if (Tok.isNot(tok::r_paren)) {
550 if (ParseExpressionList(Exprs, CommaLocs)) {
551 SkipUntil(tok::r_paren);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000552 return ExprError();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000553 }
554 }
555
556 // Match the ')'.
557 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
558
Sebastian Redlef0cb8e2009-07-29 13:50:23 +0000559 // TypeRep could be null, if it references an invalid typedef.
560 if (!TypeRep)
561 return ExprError();
562
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000563 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
564 "Unexpected number of commas!");
Sebastian Redlf53597f2009-03-15 17:47:39 +0000565 return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
566 LParenLoc, move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000567 CommaLocs.data(), RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000568}
569
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000570/// ParseCXXCondition - if/switch/while/for condition expression.
571///
572/// condition:
573/// expression
574/// type-specifier-seq declarator '=' assignment-expression
575/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
576/// '=' assignment-expression
577///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000578Parser::OwningExprResult Parser::ParseCXXCondition() {
Argyrios Kyrtzidisa8a45982008-10-05 15:03:47 +0000579 if (!isCXXConditionDeclaration())
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000580 return ParseExpression(); // expression
581
582 SourceLocation StartLoc = Tok.getLocation();
583
584 // type-specifier-seq
585 DeclSpec DS;
586 ParseSpecifierQualifierList(DS);
587
588 // declarator
589 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
590 ParseDeclarator(DeclaratorInfo);
591
592 // simple-asm-expr[opt]
593 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000594 SourceLocation Loc;
595 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000596 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000597 SkipUntil(tok::semi);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000598 return ExprError();
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000599 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000600 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000601 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000602 }
603
604 // If attributes are present, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000605 if (Tok.is(tok::kw___attribute)) {
606 SourceLocation Loc;
607 AttributeList *AttrList = ParseAttributes(&Loc);
608 DeclaratorInfo.AddAttributes(AttrList, Loc);
609 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000610
611 // '=' assignment-expression
612 if (Tok.isNot(tok::equal))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000613 return ExprError(Diag(Tok, diag::err_expected_equal_after_declarator));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000614 SourceLocation EqualLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000615 OwningExprResult AssignExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000616 if (AssignExpr.isInvalid())
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000617 return ExprError();
618
Sebastian Redlf53597f2009-03-15 17:47:39 +0000619 return Actions.ActOnCXXConditionDeclarationExpr(CurScope, StartLoc,
620 DeclaratorInfo,EqualLoc,
621 move(AssignExpr));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000622}
623
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000624/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
625/// This should only be called when the current token is known to be part of
626/// simple-type-specifier.
627///
628/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000629/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000630/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
631/// char
632/// wchar_t
633/// bool
634/// short
635/// int
636/// long
637/// signed
638/// unsigned
639/// float
640/// double
641/// void
642/// [GNU] typeof-specifier
643/// [C++0x] auto [TODO]
644///
645/// type-name:
646/// class-name
647/// enum-name
648/// typedef-name
649///
650void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
651 DS.SetRangeStart(Tok.getLocation());
652 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000653 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000654 SourceLocation Loc = Tok.getLocation();
655
656 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +0000657 case tok::identifier: // foo::bar
658 case tok::coloncolon: // ::foo::bar
659 assert(0 && "Annotation token should already be formed!");
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000660 default:
661 assert(0 && "Not a simple-type-specifier token!");
662 abort();
Chris Lattner55a7cef2009-01-05 00:13:00 +0000663
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000664 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +0000665 case tok::annot_typename: {
John McCallfec54012009-08-03 20:12:06 +0000666 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000667 Tok.getAnnotationValue());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000668 break;
669 }
670
671 // builtin types
672 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +0000673 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000674 break;
675 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +0000676 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000677 break;
678 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +0000679 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000680 break;
681 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +0000682 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000683 break;
684 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +0000685 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000686 break;
687 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +0000688 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000689 break;
690 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +0000691 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000692 break;
693 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +0000694 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000695 break;
696 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +0000697 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000698 break;
699 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +0000700 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000701 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000702 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +0000703 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000704 break;
705 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +0000706 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000707 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000708 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +0000709 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000710 break;
711
712 // GNU typeof support.
713 case tok::kw_typeof:
714 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000715 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000716 return;
717 }
Chris Lattnerb31757b2009-01-06 05:06:21 +0000718 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000719 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
720 else
721 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000722 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000723 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000724}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000725
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000726/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
727/// [dcl.name]), which is a non-empty sequence of type-specifiers,
728/// e.g., "const short int". Note that the DeclSpec is *not* finished
729/// by parsing the type-specifier-seq, because these sequences are
730/// typically followed by some form of declarator. Returns true and
731/// emits diagnostics if this is not a type-specifier-seq, false
732/// otherwise.
733///
734/// type-specifier-seq: [C++ 8.1]
735/// type-specifier type-specifier-seq[opt]
736///
737bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
738 DS.SetRangeStart(Tok.getLocation());
739 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000740 unsigned DiagID;
741 bool isInvalid = 0;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000742
743 // Parse one or more of the type specifiers.
John McCallfec54012009-08-03 20:12:06 +0000744 if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000745 Diag(Tok, diag::err_operator_missing_type_specifier);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000746 return true;
747 }
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000748
John McCallfec54012009-08-03 20:12:06 +0000749 while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) ;
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000750
751 return false;
752}
753
Douglas Gregor43c7bad2008-11-17 16:14:12 +0000754/// TryParseOperatorFunctionId - Attempts to parse a C++ overloaded
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000755/// operator name (C++ [over.oper]). If successful, returns the
756/// predefined identifier that corresponds to that overloaded
757/// operator. Otherwise, returns NULL and does not consume any tokens.
758///
759/// operator-function-id: [C++ 13.5]
760/// 'operator' operator
761///
762/// operator: one of
763/// new delete new[] delete[]
764/// + - * / % ^ & | ~
765/// ! = < > += -= *= /= %=
766/// ^= &= |= << >> >>= <<= == !=
767/// <= >= && || ++ -- , ->* ->
768/// () []
Sebastian Redlab197ba2009-02-09 18:23:29 +0000769OverloadedOperatorKind
770Parser::TryParseOperatorFunctionId(SourceLocation *EndLoc) {
Argyrios Kyrtzidis9057a812008-11-07 15:54:02 +0000771 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
Sebastian Redlab197ba2009-02-09 18:23:29 +0000772 SourceLocation Loc;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000773
774 OverloadedOperatorKind Op = OO_None;
775 switch (NextToken().getKind()) {
776 case tok::kw_new:
777 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000778 Loc = ConsumeToken(); // 'new'
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000779 if (Tok.is(tok::l_square)) {
780 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +0000781 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000782 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
783 Op = OO_Array_New;
784 } else {
785 Op = OO_New;
786 }
Sebastian Redlab197ba2009-02-09 18:23:29 +0000787 if (EndLoc)
788 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000789 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000790
791 case tok::kw_delete:
792 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000793 Loc = ConsumeToken(); // 'delete'
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000794 if (Tok.is(tok::l_square)) {
795 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +0000796 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000797 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
798 Op = OO_Array_Delete;
799 } else {
800 Op = OO_Delete;
801 }
Sebastian Redlab197ba2009-02-09 18:23:29 +0000802 if (EndLoc)
803 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000804 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000805
Douglas Gregor02bcd4c2008-11-10 13:38:07 +0000806#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000807 case tok::Token: Op = OO_##Name; break;
Douglas Gregor02bcd4c2008-11-10 13:38:07 +0000808#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000809#include "clang/Basic/OperatorKinds.def"
810
811 case tok::l_paren:
812 ConsumeToken(); // 'operator'
813 ConsumeParen(); // '('
Sebastian Redlab197ba2009-02-09 18:23:29 +0000814 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000815 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); // ')'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000816 if (EndLoc)
817 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000818 return OO_Call;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000819
820 case tok::l_square:
821 ConsumeToken(); // 'operator'
822 ConsumeBracket(); // '['
Sebastian Redlab197ba2009-02-09 18:23:29 +0000823 Loc = Tok.getLocation();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000824 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000825 if (EndLoc)
826 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000827 return OO_Subscript;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000828
829 default:
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000830 return OO_None;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000831 }
832
Douglas Gregor43c7bad2008-11-17 16:14:12 +0000833 ConsumeToken(); // 'operator'
Sebastian Redlab197ba2009-02-09 18:23:29 +0000834 Loc = ConsumeAnyToken(); // the operator itself
835 if (EndLoc)
836 *EndLoc = Loc;
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000837 return Op;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000838}
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000839
840/// ParseConversionFunctionId - Parse a C++ conversion-function-id,
841/// which expresses the name of a user-defined conversion operator
842/// (C++ [class.conv.fct]p1). Returns the type that this operator is
843/// specifying a conversion for, or NULL if there was an error.
844///
845/// conversion-function-id: [C++ 12.3.2]
846/// operator conversion-type-id
847///
848/// conversion-type-id:
849/// type-specifier-seq conversion-declarator[opt]
850///
851/// conversion-declarator:
852/// ptr-operator conversion-declarator[opt]
Sebastian Redlab197ba2009-02-09 18:23:29 +0000853Parser::TypeTy *Parser::ParseConversionFunctionId(SourceLocation *EndLoc) {
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000854 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
855 ConsumeToken(); // 'operator'
856
857 // Parse the type-specifier-seq.
858 DeclSpec DS;
859 if (ParseCXXTypeSpecifierSeq(DS))
860 return 0;
861
862 // Parse the conversion-declarator, which is merely a sequence of
863 // ptr-operators.
864 Declarator D(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000865 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000866 if (EndLoc)
867 *EndLoc = D.getSourceRange().getEnd();
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000868
869 // Finish up the type.
870 Action::TypeResult Result = Actions.ActOnTypeName(CurScope, D);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000871 if (Result.isInvalid())
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000872 return 0;
873 else
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000874 return Result.get();
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000875}
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000876
877/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
878/// memory in a typesafe manner and call constructors.
Chris Lattner59232d32009-01-04 21:25:24 +0000879///
880/// This method is called to parse the new expression after the optional :: has
881/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
882/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000883///
884/// new-expression:
885/// '::'[opt] 'new' new-placement[opt] new-type-id
886/// new-initializer[opt]
887/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
888/// new-initializer[opt]
889///
890/// new-placement:
891/// '(' expression-list ')'
892///
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000893/// new-type-id:
894/// type-specifier-seq new-declarator[opt]
895///
896/// new-declarator:
897/// ptr-operator new-declarator[opt]
898/// direct-new-declarator
899///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000900/// new-initializer:
901/// '(' expression-list[opt] ')'
902/// [C++0x] braced-init-list [TODO]
903///
Chris Lattner59232d32009-01-04 21:25:24 +0000904Parser::OwningExprResult
905Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
906 assert(Tok.is(tok::kw_new) && "expected 'new' token");
907 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000908
909 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
910 // second form of new-expression. It can't be a new-type-id.
911
Sebastian Redla55e52c2008-11-25 22:21:31 +0000912 ExprVector PlacementArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000913 SourceLocation PlacementLParen, PlacementRParen;
914
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000915 bool ParenTypeId;
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000916 DeclSpec DS;
917 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000918 if (Tok.is(tok::l_paren)) {
919 // If it turns out to be a placement, we change the type location.
920 PlacementLParen = ConsumeParen();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000921 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
922 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000923 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000924 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000925
926 PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000927 if (PlacementRParen.isInvalid()) {
928 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000929 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000930 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000931
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000932 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000933 // Reset the placement locations. There was no placement.
934 PlacementLParen = PlacementRParen = SourceLocation();
935 ParenTypeId = true;
936 } else {
937 // We still need the type.
938 if (Tok.is(tok::l_paren)) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000939 SourceLocation LParen = ConsumeParen();
940 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000941 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000942 ParseDeclarator(DeclaratorInfo);
943 MatchRHSPunctuation(tok::r_paren, LParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000944 ParenTypeId = true;
945 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000946 if (ParseCXXTypeSpecifierSeq(DS))
947 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000948 else {
949 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000950 ParseDeclaratorInternal(DeclaratorInfo,
951 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000952 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000953 ParenTypeId = false;
954 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000955 }
956 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000957 // A new-type-id is a simplified type-id, where essentially the
958 // direct-declarator is replaced by a direct-new-declarator.
959 if (ParseCXXTypeSpecifierSeq(DS))
960 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000961 else {
962 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000963 ParseDeclaratorInternal(DeclaratorInfo,
964 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000965 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000966 ParenTypeId = false;
967 }
Chris Lattnereaaebc72009-04-25 08:06:05 +0000968 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000969 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000970 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000971 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000972
Sebastian Redla55e52c2008-11-25 22:21:31 +0000973 ExprVector ConstructorArgs(Actions);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000974 SourceLocation ConstructorLParen, ConstructorRParen;
975
976 if (Tok.is(tok::l_paren)) {
977 ConstructorLParen = ConsumeParen();
978 if (Tok.isNot(tok::r_paren)) {
979 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000980 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
981 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000982 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000983 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000984 }
985 ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen);
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000986 if (ConstructorRParen.isInvalid()) {
987 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +0000988 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000989 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000990 }
991
Sebastian Redlf53597f2009-03-15 17:47:39 +0000992 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
993 move_arg(PlacementArgs), PlacementRParen,
994 ParenTypeId, DeclaratorInfo, ConstructorLParen,
995 move_arg(ConstructorArgs), ConstructorRParen);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000996}
997
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000998/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
999/// passed to ParseDeclaratorInternal.
1000///
1001/// direct-new-declarator:
1002/// '[' expression ']'
1003/// direct-new-declarator '[' constant-expression ']'
1004///
Chris Lattner59232d32009-01-04 21:25:24 +00001005void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001006 // Parse the array dimensions.
1007 bool first = true;
1008 while (Tok.is(tok::l_square)) {
1009 SourceLocation LLoc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001010 OwningExprResult Size(first ? ParseExpression()
1011 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001012 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001013 // Recover
1014 SkipUntil(tok::r_square);
1015 return;
1016 }
1017 first = false;
1018
Sebastian Redlab197ba2009-02-09 18:23:29 +00001019 SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001020 D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001021 Size.release(), LLoc, RLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001022 RLoc);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001023
Sebastian Redlab197ba2009-02-09 18:23:29 +00001024 if (RLoc.isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001025 return;
1026 }
1027}
1028
1029/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
1030/// This ambiguity appears in the syntax of the C++ new operator.
1031///
1032/// new-expression:
1033/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
1034/// new-initializer[opt]
1035///
1036/// new-placement:
1037/// '(' expression-list ')'
1038///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001039bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00001040 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001041 // The '(' was already consumed.
1042 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001043 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001044 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001045 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001046 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001047 }
1048
1049 // It's not a type, it has to be an expression list.
1050 // Discard the comma locations - ActOnCXXNew has enough parameters.
1051 CommaLocsTy CommaLocs;
1052 return ParseExpressionList(PlacementArgs, CommaLocs);
1053}
1054
1055/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
1056/// to free memory allocated by new.
1057///
Chris Lattner59232d32009-01-04 21:25:24 +00001058/// This method is called to parse the 'delete' expression after the optional
1059/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
1060/// and "Start" is its location. Otherwise, "Start" is the location of the
1061/// 'delete' token.
1062///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001063/// delete-expression:
1064/// '::'[opt] 'delete' cast-expression
1065/// '::'[opt] 'delete' '[' ']' cast-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001066Parser::OwningExprResult
1067Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
1068 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
1069 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001070
1071 // Array delete?
1072 bool ArrayDelete = false;
1073 if (Tok.is(tok::l_square)) {
1074 ArrayDelete = true;
1075 SourceLocation LHS = ConsumeBracket();
1076 SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS);
1077 if (RHS.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001078 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001079 }
1080
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001081 OwningExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001082 if (Operand.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001083 return move(Operand);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001084
Sebastian Redlf53597f2009-03-15 17:47:39 +00001085 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001086}
Sebastian Redl64b45f72009-01-05 20:52:13 +00001087
1088static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind)
1089{
1090 switch(kind) {
1091 default: assert(false && "Not a known unary type trait.");
1092 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
1093 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
1094 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
1095 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
1096 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
1097 case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor;
1098 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
1099 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
1100 case tok::kw___is_abstract: return UTT_IsAbstract;
1101 case tok::kw___is_class: return UTT_IsClass;
1102 case tok::kw___is_empty: return UTT_IsEmpty;
1103 case tok::kw___is_enum: return UTT_IsEnum;
1104 case tok::kw___is_pod: return UTT_IsPOD;
1105 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
1106 case tok::kw___is_union: return UTT_IsUnion;
1107 }
1108}
1109
1110/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
1111/// pseudo-functions that allow implementation of the TR1/C++0x type traits
1112/// templates.
1113///
1114/// primary-expression:
1115/// [GNU] unary-type-trait '(' type-id ')'
1116///
1117Parser::OwningExprResult Parser::ParseUnaryTypeTrait()
1118{
1119 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
1120 SourceLocation Loc = ConsumeToken();
1121
1122 SourceLocation LParen = Tok.getLocation();
1123 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1124 return ExprError();
1125
1126 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
1127 // there will be cryptic errors about mismatched parentheses and missing
1128 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00001129 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00001130
1131 SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen);
1132
Douglas Gregor809070a2009-02-18 17:45:20 +00001133 if (Ty.isInvalid())
1134 return ExprError();
1135
1136 return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001137}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001138
1139/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
1140/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
1141/// based on the context past the parens.
1142Parser::OwningExprResult
1143Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
1144 TypeTy *&CastTy,
1145 SourceLocation LParenLoc,
1146 SourceLocation &RParenLoc) {
1147 assert(getLang().CPlusPlus && "Should only be called for C++!");
1148 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
1149 assert(isTypeIdInParens() && "Not a type-id!");
1150
1151 OwningExprResult Result(Actions, true);
1152 CastTy = 0;
1153
1154 // We need to disambiguate a very ugly part of the C++ syntax:
1155 //
1156 // (T())x; - type-id
1157 // (T())*x; - type-id
1158 // (T())/x; - expression
1159 // (T()); - expression
1160 //
1161 // The bad news is that we cannot use the specialized tentative parser, since
1162 // it can only verify that the thing inside the parens can be parsed as
1163 // type-id, it is not useful for determining the context past the parens.
1164 //
1165 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00001166 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001167 //
1168 // It uses a scheme similar to parsing inline methods. The parenthesized
1169 // tokens are cached, the context that follows is determined (possibly by
1170 // parsing a cast-expression), and then we re-introduce the cached tokens
1171 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001172
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001173 ParenParseOption ParseAs;
1174 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001175
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001176 // Store the tokens of the parentheses. We will parse them after we determine
1177 // the context that follows them.
1178 if (!ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks, tok::semi)) {
1179 // We didn't find the ')' we expected.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001180 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1181 return ExprError();
1182 }
1183
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001184 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001185 ParseAs = CompoundLiteral;
1186 } else {
1187 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001188 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
1189 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
1190 NotCastExpr = true;
1191 } else {
1192 // Try parsing the cast-expression that may follow.
1193 // If it is not a cast-expression, NotCastExpr will be true and no token
1194 // will be consumed.
1195 Result = ParseCastExpression(false/*isUnaryExpression*/,
1196 false/*isAddressofOperand*/,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001197 NotCastExpr, false);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00001198 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001199
1200 // If we parsed a cast-expression, it's really a type-id, otherwise it's
1201 // an expression.
1202 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001203 }
1204
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001205 // The current token should go after the cached tokens.
1206 Toks.push_back(Tok);
1207 // Re-enter the stored parenthesized tokens into the token stream, so we may
1208 // parse them now.
1209 PP.EnterTokenStream(Toks.data(), Toks.size(),
1210 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
1211 // Drop the current token and bring the first cached one. It's the same token
1212 // as when we entered this function.
1213 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001214
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001215 if (ParseAs >= CompoundLiteral) {
1216 TypeResult Ty = ParseTypeName();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001217
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001218 // Match the ')'.
1219 if (Tok.is(tok::r_paren))
1220 RParenLoc = ConsumeParen();
1221 else
1222 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001223
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001224 if (ParseAs == CompoundLiteral) {
1225 ExprType = CompoundLiteral;
1226 return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc);
1227 }
1228
1229 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
1230 assert(ParseAs == CastExpr);
1231
1232 if (Ty.isInvalid())
1233 return ExprError();
1234
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001235 CastTy = Ty.get();
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001236
1237 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001238 if (!Result.isInvalid())
Nate Begeman2ef13e52009-08-10 23:49:36 +00001239 Result = Actions.ActOnCastExpr(CurScope, LParenLoc, CastTy, RParenLoc,
1240 move(Result));
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001241 return move(Result);
1242 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001243
1244 // Not a compound literal, and not followed by a cast-expression.
1245 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001246
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001247 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001248 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001249 if (!Result.isInvalid() && Tok.is(tok::r_paren))
1250 Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result));
1251
1252 // Match the ')'.
1253 if (Result.isInvalid()) {
1254 SkipUntil(tok::r_paren);
1255 return ExprError();
1256 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00001257
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001258 if (Tok.is(tok::r_paren))
1259 RParenLoc = ConsumeParen();
1260 else
1261 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1262
1263 return move(Result);
1264}