blob: 439fb077da2a7ab2a044f61ceeff91828ad3e5a0 [file] [log] [blame]
Chris Lattner29375652006-12-04 18:06:35 +00001//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner29375652006-12-04 18:06:35 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation for C++.
11//
12//===----------------------------------------------------------------------===//
Erik Verbruggen888d52a2014-01-15 09:15:43 +000013#include "clang/AST/ASTContext.h"
Douglas Gregor94a32472011-01-11 00:33:19 +000014#include "RAIIObjectsForParser.h"
Chandler Carruth757fcd62014-03-04 10:05:20 +000015#include "clang/AST/DeclTemplate.h"
Eli Friedmanc7c97142012-01-04 02:40:39 +000016#include "clang/Basic/PrettyStackTrace.h"
Richard Smith7d182a72012-03-08 23:06:02 +000017#include "clang/Lex/LiteralSupport.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/Parse/ParseDiagnostic.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000019#include "clang/Parse/Parser.h"
John McCall8b0666c2010-08-20 18:27:03 +000020#include "clang/Sema/DeclSpec.h"
21#include "clang/Sema/ParsedTemplate.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/Sema/Scope.h"
Douglas Gregor7861a802009-11-03 01:35:08 +000023#include "llvm/Support/ErrorHandling.h"
24
Faisal Vali2b391ab2013-09-26 19:54:12 +000025
Chris Lattner29375652006-12-04 18:06:35 +000026using namespace clang;
27
Alp Tokerf990cef2014-01-07 02:35:33 +000028static int SelectDigraphErrorMessage(tok::TokenKind Kind) {
29 switch (Kind) {
30 // template name
31 case tok::unknown: return 0;
32 // casts
33 case tok::kw_const_cast: return 1;
34 case tok::kw_dynamic_cast: return 2;
35 case tok::kw_reinterpret_cast: return 3;
36 case tok::kw_static_cast: return 4;
37 default:
38 llvm_unreachable("Unknown type for digraph error message.");
39 }
40}
41
Richard Smith55858492011-04-14 21:45:45 +000042// Are the two tokens adjacent in the same source file?
Richard Smith7b3f3222012-06-18 06:11:04 +000043bool Parser::areTokensAdjacent(const Token &First, const Token &Second) {
Richard Smith55858492011-04-14 21:45:45 +000044 SourceManager &SM = PP.getSourceManager();
45 SourceLocation FirstLoc = SM.getSpellingLoc(First.getLocation());
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +000046 SourceLocation FirstEnd = FirstLoc.getLocWithOffset(First.getLength());
Richard Smith55858492011-04-14 21:45:45 +000047 return FirstEnd == SM.getSpellingLoc(Second.getLocation());
48}
49
50// Suggest fixit for "<::" after a cast.
51static void FixDigraph(Parser &P, Preprocessor &PP, Token &DigraphToken,
52 Token &ColonToken, tok::TokenKind Kind, bool AtDigraph) {
53 // Pull '<:' and ':' off token stream.
54 if (!AtDigraph)
55 PP.Lex(DigraphToken);
56 PP.Lex(ColonToken);
57
58 SourceRange Range;
59 Range.setBegin(DigraphToken.getLocation());
60 Range.setEnd(ColonToken.getLocation());
61 P.Diag(DigraphToken.getLocation(), diag::err_missing_whitespace_digraph)
Alp Tokerf990cef2014-01-07 02:35:33 +000062 << SelectDigraphErrorMessage(Kind)
63 << FixItHint::CreateReplacement(Range, "< ::");
Richard Smith55858492011-04-14 21:45:45 +000064
65 // Update token information to reflect their change in token type.
66 ColonToken.setKind(tok::coloncolon);
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +000067 ColonToken.setLocation(ColonToken.getLocation().getLocWithOffset(-1));
Richard Smith55858492011-04-14 21:45:45 +000068 ColonToken.setLength(2);
69 DigraphToken.setKind(tok::less);
70 DigraphToken.setLength(1);
71
72 // Push new tokens back to token stream.
73 PP.EnterToken(ColonToken);
74 if (!AtDigraph)
75 PP.EnterToken(DigraphToken);
76}
77
Richard Trieu01fc0012011-09-19 19:01:00 +000078// Check for '<::' which should be '< ::' instead of '[:' when following
79// a template name.
80void Parser::CheckForTemplateAndDigraph(Token &Next, ParsedType ObjectType,
81 bool EnteringContext,
82 IdentifierInfo &II, CXXScopeSpec &SS) {
Richard Trieu02e25db2011-09-20 20:03:50 +000083 if (!Next.is(tok::l_square) || Next.getLength() != 2)
Richard Trieu01fc0012011-09-19 19:01:00 +000084 return;
85
86 Token SecondToken = GetLookAheadToken(2);
Richard Smith7b3f3222012-06-18 06:11:04 +000087 if (!SecondToken.is(tok::colon) || !areTokensAdjacent(Next, SecondToken))
Richard Trieu01fc0012011-09-19 19:01:00 +000088 return;
89
90 TemplateTy Template;
91 UnqualifiedId TemplateName;
92 TemplateName.setIdentifier(&II, Tok.getLocation());
93 bool MemberOfUnknownSpecialization;
94 if (!Actions.isTemplateName(getCurScope(), SS, /*hasTemplateKeyword=*/false,
95 TemplateName, ObjectType, EnteringContext,
96 Template, MemberOfUnknownSpecialization))
97 return;
98
Alp Tokerf990cef2014-01-07 02:35:33 +000099 FixDigraph(*this, PP, Next, SecondToken, tok::unknown,
100 /*AtDigraph*/false);
Richard Trieu01fc0012011-09-19 19:01:00 +0000101}
102
Richard Trieu1f3ea7b2012-11-02 01:08:58 +0000103/// \brief Emits an error for a left parentheses after a double colon.
104///
105/// When a '(' is found after a '::', emit an error. Attempt to fix the token
Nico Weber6be9b252012-11-29 05:29:23 +0000106/// stream by removing the '(', and the matching ')' if found.
Richard Trieu1f3ea7b2012-11-02 01:08:58 +0000107void Parser::CheckForLParenAfterColonColon() {
108 if (!Tok.is(tok::l_paren))
109 return;
110
111 SourceLocation l_parenLoc = ConsumeParen(), r_parenLoc;
112 Token Tok1 = getCurToken();
113 if (!Tok1.is(tok::identifier) && !Tok1.is(tok::star))
114 return;
115
116 if (Tok1.is(tok::identifier)) {
117 Token Tok2 = GetLookAheadToken(1);
118 if (Tok2.is(tok::r_paren)) {
119 ConsumeToken();
120 PP.EnterToken(Tok1);
121 r_parenLoc = ConsumeParen();
122 }
123 } else if (Tok1.is(tok::star)) {
124 Token Tok2 = GetLookAheadToken(1);
125 if (Tok2.is(tok::identifier)) {
126 Token Tok3 = GetLookAheadToken(2);
127 if (Tok3.is(tok::r_paren)) {
128 ConsumeToken();
129 ConsumeToken();
130 PP.EnterToken(Tok2);
131 PP.EnterToken(Tok1);
132 r_parenLoc = ConsumeParen();
133 }
134 }
135 }
136
137 Diag(l_parenLoc, diag::err_paren_after_colon_colon)
138 << FixItHint::CreateRemoval(l_parenLoc)
139 << FixItHint::CreateRemoval(r_parenLoc);
140}
141
Mike Stump11289f42009-09-09 15:08:12 +0000142/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000143///
144/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump11289f42009-09-09 15:08:12 +0000145/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000146/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000147///
148/// '::'[opt] nested-name-specifier
149/// '::'
150///
151/// nested-name-specifier:
152/// type-name '::'
153/// namespace-name '::'
154/// nested-name-specifier identifier '::'
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000155/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000156///
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000157///
Mike Stump11289f42009-09-09 15:08:12 +0000158/// \param SS the scope specifier that will be set to the parsed
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000159/// nested-name-specifier (or empty)
160///
Mike Stump11289f42009-09-09 15:08:12 +0000161/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000162/// the "." or "->" of a member access expression, this parameter provides the
163/// type of the object whose members are being accessed.
164///
165/// \param EnteringContext whether we will be entering into the context of
166/// the nested-name-specifier after parsing it.
167///
Douglas Gregore610ada2010-02-24 18:44:31 +0000168/// \param MayBePseudoDestructor When non-NULL, points to a flag that
169/// indicates whether this nested-name-specifier may be part of a
170/// pseudo-destructor name. In this case, the flag will be set false
171/// if we don't actually end up parsing a destructor name. Moreorover,
172/// if we do end up determining that we are parsing a destructor name,
173/// the last component of the nested-name-specifier is not parsed as
174/// part of the scope specifier.
Richard Smith7447af42013-03-26 01:15:19 +0000175///
176/// \param IsTypename If \c true, this nested-name-specifier is known to be
177/// part of a type name. This is used to improve error recovery.
178///
179/// \param LastII When non-NULL, points to an IdentifierInfo* that will be
180/// filled in with the leading identifier in the last component of the
181/// nested-name-specifier, if any.
Douglas Gregor90d554e2010-02-21 18:36:56 +0000182///
John McCall1f476a12010-02-26 08:45:28 +0000183/// \returns true if there was an error parsing a scope specifier
Douglas Gregore861bac2009-08-25 22:51:20 +0000184bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
John McCallba7bf592010-08-24 05:47:05 +0000185 ParsedType ObjectType,
Douglas Gregor90d554e2010-02-21 18:36:56 +0000186 bool EnteringContext,
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000187 bool *MayBePseudoDestructor,
Richard Smith7447af42013-03-26 01:15:19 +0000188 bool IsTypename,
189 IdentifierInfo **LastII) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000190 assert(getLangOpts().CPlusPlus &&
Chris Lattnerb5134c02009-01-05 01:24:05 +0000191 "Call sites of this function should be guarded by checking for C++");
Mike Stump11289f42009-09-09 15:08:12 +0000192
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000193 if (Tok.is(tok::annot_cxxscope)) {
Richard Smith7447af42013-03-26 01:15:19 +0000194 assert(!LastII && "want last identifier but have already annotated scope");
Douglas Gregor869ad452011-02-24 17:54:50 +0000195 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
196 Tok.getAnnotationRange(),
197 SS);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000198 ConsumeToken();
John McCall1f476a12010-02-26 08:45:28 +0000199 return false;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000200 }
Chris Lattnerf9b2cd42009-01-04 21:14:15 +0000201
Larisse Voufob959c3c2013-08-06 05:49:26 +0000202 if (Tok.is(tok::annot_template_id)) {
203 // If the current token is an annotated template id, it may already have
204 // a scope specifier. Restore it.
205 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
206 SS = TemplateId->SS;
207 }
208
Richard Smith7447af42013-03-26 01:15:19 +0000209 if (LastII)
210 *LastII = 0;
211
Douglas Gregor7f741122009-02-25 19:37:18 +0000212 bool HasScopeSpecifier = false;
213
Chris Lattner8a7d10d2009-01-05 03:55:46 +0000214 if (Tok.is(tok::coloncolon)) {
215 // ::new and ::delete aren't nested-name-specifiers.
216 tok::TokenKind NextKind = NextToken().getKind();
217 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
218 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000219
Chris Lattner45ddec32009-01-05 00:13:00 +0000220 // '::' - Global scope qualifier.
Douglas Gregor90c99722011-02-24 00:17:56 +0000221 if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
222 return true;
Richard Trieu1f3ea7b2012-11-02 01:08:58 +0000223
224 CheckForLParenAfterColonColon();
225
Douglas Gregor7f741122009-02-25 19:37:18 +0000226 HasScopeSpecifier = true;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000227 }
228
Douglas Gregore610ada2010-02-24 18:44:31 +0000229 bool CheckForDestructor = false;
230 if (MayBePseudoDestructor && *MayBePseudoDestructor) {
231 CheckForDestructor = true;
232 *MayBePseudoDestructor = false;
233 }
234
David Blaikie15a430a2011-12-04 05:04:18 +0000235 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
236 DeclSpec DS(AttrFactory);
237 SourceLocation DeclLoc = Tok.getLocation();
238 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
Alp Tokera3ebe6e2013-12-17 14:12:37 +0000239
240 SourceLocation CCLoc;
241 if (!TryConsumeToken(tok::coloncolon, CCLoc)) {
David Blaikie15a430a2011-12-04 05:04:18 +0000242 AnnotateExistingDecltypeSpecifier(DS, DeclLoc, EndLoc);
243 return false;
244 }
Alp Tokera3ebe6e2013-12-17 14:12:37 +0000245
David Blaikie15a430a2011-12-04 05:04:18 +0000246 if (Actions.ActOnCXXNestedNameSpecifierDecltype(SS, DS, CCLoc))
247 SS.SetInvalid(SourceRange(DeclLoc, CCLoc));
248
249 HasScopeSpecifier = true;
250 }
251
Douglas Gregor7f741122009-02-25 19:37:18 +0000252 while (true) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000253 if (HasScopeSpecifier) {
254 // C++ [basic.lookup.classref]p5:
255 // If the qualified-id has the form
Douglas Gregor308047d2009-09-09 00:23:06 +0000256 //
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000257 // ::class-name-or-namespace-name::...
Douglas Gregor308047d2009-09-09 00:23:06 +0000258 //
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000259 // the class-name-or-namespace-name is looked up in global scope as a
260 // class-name or namespace-name.
261 //
262 // To implement this, we clear out the object type as soon as we've
263 // seen a leading '::' or part of a nested-name-specifier.
John McCallba7bf592010-08-24 05:47:05 +0000264 ObjectType = ParsedType();
Douglas Gregor2436e712009-09-17 21:32:03 +0000265
266 if (Tok.is(tok::code_completion)) {
267 // Code completion for a nested-name-specifier, where the code
268 // code completion token follows the '::'.
Douglas Gregor0be31a22010-07-02 17:43:08 +0000269 Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
Argyrios Kyrtzidis7d94c922011-04-23 01:04:12 +0000270 // Include code completion token into the range of the scope otherwise
271 // when we try to annotate the scope tokens the dangling code completion
272 // token will cause assertion in
273 // Preprocessor::AnnotatePreviousCachedTokens.
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000274 SS.setEndLoc(Tok.getLocation());
275 cutOffParsing();
276 return true;
Douglas Gregor2436e712009-09-17 21:32:03 +0000277 }
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000278 }
Mike Stump11289f42009-09-09 15:08:12 +0000279
Douglas Gregor7f741122009-02-25 19:37:18 +0000280 // nested-name-specifier:
Chris Lattner0eed3a62009-06-26 03:47:46 +0000281 // nested-name-specifier 'template'[opt] simple-template-id '::'
282
283 // Parse the optional 'template' keyword, then make sure we have
284 // 'identifier <' after it.
285 if (Tok.is(tok::kw_template)) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000286 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedman2624be42009-08-29 04:08:08 +0000287 // nested-name-specifier, since they aren't allowed to start with
288 // 'template'.
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000289 if (!HasScopeSpecifier && !ObjectType)
Eli Friedman2624be42009-08-29 04:08:08 +0000290 break;
291
Douglas Gregor120635b2009-11-11 16:39:34 +0000292 TentativeParsingAction TPA(*this);
Chris Lattner0eed3a62009-06-26 03:47:46 +0000293 SourceLocation TemplateKWLoc = ConsumeToken();
Richard Smithd091dc12013-12-05 00:58:33 +0000294
Douglas Gregor71395fa2009-11-04 00:56:37 +0000295 UnqualifiedId TemplateName;
296 if (Tok.is(tok::identifier)) {
Douglas Gregor71395fa2009-11-04 00:56:37 +0000297 // Consume the identifier.
Douglas Gregor120635b2009-11-11 16:39:34 +0000298 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregor71395fa2009-11-04 00:56:37 +0000299 ConsumeToken();
300 } else if (Tok.is(tok::kw_operator)) {
Richard Smithd091dc12013-12-05 00:58:33 +0000301 // We don't need to actually parse the unqualified-id in this case,
302 // because a simple-template-id cannot start with 'operator', but
303 // go ahead and parse it anyway for consistency with the case where
304 // we already annotated the template-id.
305 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor120635b2009-11-11 16:39:34 +0000306 TemplateName)) {
307 TPA.Commit();
Douglas Gregor71395fa2009-11-04 00:56:37 +0000308 break;
Douglas Gregor120635b2009-11-11 16:39:34 +0000309 }
Richard Smithd091dc12013-12-05 00:58:33 +0000310
Alexis Hunted0530f2009-11-28 08:58:14 +0000311 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
312 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor71395fa2009-11-04 00:56:37 +0000313 Diag(TemplateName.getSourceRange().getBegin(),
314 diag::err_id_after_template_in_nested_name_spec)
315 << TemplateName.getSourceRange();
Douglas Gregor120635b2009-11-11 16:39:34 +0000316 TPA.Commit();
Douglas Gregor71395fa2009-11-04 00:56:37 +0000317 break;
318 }
319 } else {
Douglas Gregor120635b2009-11-11 16:39:34 +0000320 TPA.Revert();
Chris Lattner0eed3a62009-06-26 03:47:46 +0000321 break;
322 }
Mike Stump11289f42009-09-09 15:08:12 +0000323
Douglas Gregor120635b2009-11-11 16:39:34 +0000324 // If the next token is not '<', we have a qualified-id that refers
325 // to a template name, such as T::template apply, but is not a
326 // template-id.
327 if (Tok.isNot(tok::less)) {
328 TPA.Revert();
329 break;
330 }
331
332 // Commit to parsing the template-id.
333 TPA.Commit();
Douglas Gregorbb119652010-06-16 23:00:59 +0000334 TemplateTy Template;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000335 if (TemplateNameKind TNK
336 = Actions.ActOnDependentTemplateName(getCurScope(),
337 SS, TemplateKWLoc, TemplateName,
338 ObjectType, EnteringContext,
339 Template)) {
340 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateKWLoc,
341 TemplateName, false))
Douglas Gregorbb119652010-06-16 23:00:59 +0000342 return true;
343 } else
John McCall1f476a12010-02-26 08:45:28 +0000344 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000345
Chris Lattner0eed3a62009-06-26 03:47:46 +0000346 continue;
347 }
Mike Stump11289f42009-09-09 15:08:12 +0000348
Douglas Gregor7f741122009-02-25 19:37:18 +0000349 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump11289f42009-09-09 15:08:12 +0000350 // We have
Douglas Gregor7f741122009-02-25 19:37:18 +0000351 //
Richard Smith72bfbd82013-12-04 00:28:23 +0000352 // template-id '::'
Douglas Gregor7f741122009-02-25 19:37:18 +0000353 //
Richard Smith72bfbd82013-12-04 00:28:23 +0000354 // So we need to check whether the template-id is a simple-template-id of
355 // the right kind (it should name a type or be dependent), and then
Douglas Gregorb67535d2009-03-31 00:43:58 +0000356 // convert it into a type within the nested-name-specifier.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +0000357 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregore610ada2010-02-24 18:44:31 +0000358 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
359 *MayBePseudoDestructor = true;
John McCall1f476a12010-02-26 08:45:28 +0000360 return false;
Douglas Gregore610ada2010-02-24 18:44:31 +0000361 }
362
Richard Smith7447af42013-03-26 01:15:19 +0000363 if (LastII)
364 *LastII = TemplateId->Name;
365
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000366 // Consume the template-id token.
367 ConsumeToken();
368
369 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
370 SourceLocation CCLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000371
David Blaikie8c045bc2011-11-07 03:30:03 +0000372 HasScopeSpecifier = true;
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000373
Benjamin Kramercc4c49d2012-08-23 23:38:35 +0000374 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000375 TemplateId->NumArgs);
376
377 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000378 SS,
379 TemplateId->TemplateKWLoc,
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000380 TemplateId->Template,
381 TemplateId->TemplateNameLoc,
382 TemplateId->LAngleLoc,
383 TemplateArgsPtr,
384 TemplateId->RAngleLoc,
385 CCLoc,
386 EnteringContext)) {
387 SourceLocation StartLoc
388 = SS.getBeginLoc().isValid()? SS.getBeginLoc()
389 : TemplateId->TemplateNameLoc;
390 SS.SetInvalid(SourceRange(StartLoc, CCLoc));
Chris Lattner704edfb2009-06-26 03:45:46 +0000391 }
Argyrios Kyrtzidis13935672011-05-03 18:45:38 +0000392
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000393 continue;
Douglas Gregor7f741122009-02-25 19:37:18 +0000394 }
395
Chris Lattnere2355f72009-06-26 03:52:38 +0000396
397 // The rest of the nested-name-specifier possibilities start with
398 // tok::identifier.
399 if (Tok.isNot(tok::identifier))
400 break;
401
402 IdentifierInfo &II = *Tok.getIdentifierInfo();
403
404 // nested-name-specifier:
405 // type-name '::'
406 // namespace-name '::'
407 // nested-name-specifier identifier '::'
408 Token Next = NextToken();
Chris Lattner1c428032009-12-07 01:36:53 +0000409
410 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
411 // and emit a fixit hint for it.
Douglas Gregor90d554e2010-02-21 18:36:56 +0000412 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000413 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
414 Tok.getLocation(),
415 Next.getLocation(), ObjectType,
Douglas Gregor90d554e2010-02-21 18:36:56 +0000416 EnteringContext) &&
417 // If the token after the colon isn't an identifier, it's still an
418 // error, but they probably meant something else strange so don't
419 // recover like this.
420 PP.LookAhead(1).is(tok::identifier)) {
421 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
Douglas Gregora771f462010-03-31 17:46:05 +0000422 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregor90d554e2010-02-21 18:36:56 +0000423
424 // Recover as if the user wrote '::'.
425 Next.setKind(tok::coloncolon);
426 }
Chris Lattner1c428032009-12-07 01:36:53 +0000427 }
428
Chris Lattnere2355f72009-06-26 03:52:38 +0000429 if (Next.is(tok::coloncolon)) {
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000430 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +0000431 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000432 II, ObjectType)) {
Douglas Gregore610ada2010-02-24 18:44:31 +0000433 *MayBePseudoDestructor = true;
John McCall1f476a12010-02-26 08:45:28 +0000434 return false;
Douglas Gregore610ada2010-02-24 18:44:31 +0000435 }
436
Richard Smith7447af42013-03-26 01:15:19 +0000437 if (LastII)
438 *LastII = &II;
439
Chris Lattnere2355f72009-06-26 03:52:38 +0000440 // We have an identifier followed by a '::'. Lookup this name
441 // as the name in a nested-name-specifier.
442 SourceLocation IdLoc = ConsumeToken();
Chris Lattner1c428032009-12-07 01:36:53 +0000443 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
444 "NextToken() not working properly!");
Chris Lattnere2355f72009-06-26 03:52:38 +0000445 SourceLocation CCLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000446
Richard Trieu1f3ea7b2012-11-02 01:08:58 +0000447 CheckForLParenAfterColonColon();
448
Douglas Gregor90c99722011-02-24 00:17:56 +0000449 HasScopeSpecifier = true;
450 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
451 ObjectType, EnteringContext, SS))
452 SS.SetInvalid(SourceRange(IdLoc, CCLoc));
453
Chris Lattnere2355f72009-06-26 03:52:38 +0000454 continue;
455 }
Mike Stump11289f42009-09-09 15:08:12 +0000456
Richard Trieu01fc0012011-09-19 19:01:00 +0000457 CheckForTemplateAndDigraph(Next, ObjectType, EnteringContext, II, SS);
Richard Smith55858492011-04-14 21:45:45 +0000458
Chris Lattnere2355f72009-06-26 03:52:38 +0000459 // nested-name-specifier:
460 // type-name '<'
461 if (Next.is(tok::less)) {
462 TemplateTy Template;
Douglas Gregor3cf81312009-11-03 23:16:33 +0000463 UnqualifiedId TemplateName;
464 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor786123d2010-05-21 23:18:07 +0000465 bool MemberOfUnknownSpecialization;
Douglas Gregor0be31a22010-07-02 17:43:08 +0000466 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c5dee42010-08-06 12:11:11 +0000467 /*hasTemplateKeyword=*/false,
Douglas Gregor3cf81312009-11-03 23:16:33 +0000468 TemplateName,
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000469 ObjectType,
Douglas Gregore861bac2009-08-25 22:51:20 +0000470 EnteringContext,
Douglas Gregor786123d2010-05-21 23:18:07 +0000471 Template,
472 MemberOfUnknownSpecialization)) {
David Blaikie8c045bc2011-11-07 03:30:03 +0000473 // We have found a template name, so annotate this token
Chris Lattnere2355f72009-06-26 03:52:38 +0000474 // with a template-id annotation. We do not permit the
475 // template-id to be translated into a type annotation,
476 // because some clients (e.g., the parsing of class template
477 // specializations) still want to see the original template-id
478 // token.
Douglas Gregor71395fa2009-11-04 00:56:37 +0000479 ConsumeToken();
Abramo Bagnara7945c982012-01-27 09:46:47 +0000480 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
481 TemplateName, false))
John McCall1f476a12010-02-26 08:45:28 +0000482 return true;
Chris Lattnere2355f72009-06-26 03:52:38 +0000483 continue;
Larisse Voufo39a1e502013-08-06 01:03:05 +0000484 }
485
Douglas Gregor20c38a72010-05-21 23:43:39 +0000486 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000487 (IsTypename || IsTemplateArgumentList(1))) {
Douglas Gregor20c38a72010-05-21 23:43:39 +0000488 // We have something like t::getAs<T>, where getAs is a
489 // member of an unknown specialization. However, this will only
490 // parse correctly as a template, so suggest the keyword 'template'
491 // before 'getAs' and treat this as a dependent template name.
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000492 unsigned DiagID = diag::err_missing_dependent_template_keyword;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000493 if (getLangOpts().MicrosoftExt)
Francois Pichet93921652011-04-22 08:25:24 +0000494 DiagID = diag::warn_missing_dependent_template_keyword;
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000495
496 Diag(Tok.getLocation(), DiagID)
Douglas Gregor20c38a72010-05-21 23:43:39 +0000497 << II.getName()
498 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
499
Douglas Gregorbb119652010-06-16 23:00:59 +0000500 if (TemplateNameKind TNK
Douglas Gregor0be31a22010-07-02 17:43:08 +0000501 = Actions.ActOnDependentTemplateName(getCurScope(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000502 SS, SourceLocation(),
Douglas Gregorbb119652010-06-16 23:00:59 +0000503 TemplateName, ObjectType,
504 EnteringContext, Template)) {
505 // Consume the identifier.
506 ConsumeToken();
Abramo Bagnara7945c982012-01-27 09:46:47 +0000507 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
508 TemplateName, false))
509 return true;
Douglas Gregorbb119652010-06-16 23:00:59 +0000510 }
511 else
Douglas Gregor20c38a72010-05-21 23:43:39 +0000512 return true;
Douglas Gregorbb119652010-06-16 23:00:59 +0000513
Douglas Gregor20c38a72010-05-21 23:43:39 +0000514 continue;
Chris Lattnere2355f72009-06-26 03:52:38 +0000515 }
516 }
517
Douglas Gregor7f741122009-02-25 19:37:18 +0000518 // We don't have any tokens that form the beginning of a
519 // nested-name-specifier, so we're done.
520 break;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000521 }
Mike Stump11289f42009-09-09 15:08:12 +0000522
Douglas Gregore610ada2010-02-24 18:44:31 +0000523 // Even if we didn't see any pieces of a nested-name-specifier, we
524 // still check whether there is a tilde in this position, which
525 // indicates a potential pseudo-destructor.
526 if (CheckForDestructor && Tok.is(tok::tilde))
527 *MayBePseudoDestructor = true;
528
John McCall1f476a12010-02-26 08:45:28 +0000529 return false;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000530}
531
532/// ParseCXXIdExpression - Handle id-expression.
533///
534/// id-expression:
535/// unqualified-id
536/// qualified-id
537///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000538/// qualified-id:
539/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
540/// '::' identifier
541/// '::' operator-function-id
Douglas Gregora727cb92009-06-30 22:34:41 +0000542/// '::' template-id
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000543///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000544/// NOTE: The standard specifies that, for qualified-id, the parser does not
545/// expect:
546///
547/// '::' conversion-function-id
548/// '::' '~' class-name
549///
550/// This may cause a slight inconsistency on diagnostics:
551///
552/// class C {};
553/// namespace A {}
554/// void f() {
555/// :: A :: ~ C(); // Some Sema error about using destructor with a
556/// // namespace.
557/// :: ~ C(); // Some Parser error like 'unexpected ~'.
558/// }
559///
560/// We simplify the parser a bit and make it work like:
561///
562/// qualified-id:
563/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
564/// '::' unqualified-id
565///
566/// That way Sema can handle and report similar errors for namespaces and the
567/// global scope.
568///
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000569/// The isAddressOfOperand parameter indicates that this id-expression is a
570/// direct operand of the address-of operator. This is, besides member contexts,
571/// the only place where a qualified-id naming a non-static class member may
572/// appear.
573///
John McCalldadc5752010-08-24 06:29:42 +0000574ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000575 // qualified-id:
576 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
577 // '::' unqualified-id
578 //
579 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +0000580 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000581
582 SourceLocation TemplateKWLoc;
Douglas Gregora121b752009-11-03 16:56:39 +0000583 UnqualifiedId Name;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000584 if (ParseUnqualifiedId(SS,
585 /*EnteringContext=*/false,
586 /*AllowDestructorName=*/false,
587 /*AllowConstructorName=*/false,
John McCallba7bf592010-08-24 05:47:05 +0000588 /*ObjectType=*/ ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000589 TemplateKWLoc,
Douglas Gregora121b752009-11-03 16:56:39 +0000590 Name))
591 return ExprError();
John McCalla9ee3252009-11-22 02:49:43 +0000592
593 // This is only the direct operand of an & operator if it is not
594 // followed by a postfix-expression suffix.
John McCall8d08b9b2010-08-27 09:08:28 +0000595 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
596 isAddressOfOperand = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000597
598 return Actions.ActOnIdExpression(getCurScope(), SS, TemplateKWLoc, Name,
599 Tok.is(tok::l_paren), isAddressOfOperand);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000600}
601
Richard Smith21b3ab42013-05-09 21:36:41 +0000602/// ParseLambdaExpression - Parse a C++11 lambda expression.
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000603///
604/// lambda-expression:
605/// lambda-introducer lambda-declarator[opt] compound-statement
606///
607/// lambda-introducer:
608/// '[' lambda-capture[opt] ']'
609///
610/// lambda-capture:
611/// capture-default
612/// capture-list
613/// capture-default ',' capture-list
614///
615/// capture-default:
616/// '&'
617/// '='
618///
619/// capture-list:
620/// capture
621/// capture-list ',' capture
622///
623/// capture:
Richard Smith21b3ab42013-05-09 21:36:41 +0000624/// simple-capture
625/// init-capture [C++1y]
626///
627/// simple-capture:
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000628/// identifier
629/// '&' identifier
630/// 'this'
631///
Richard Smith21b3ab42013-05-09 21:36:41 +0000632/// init-capture: [C++1y]
633/// identifier initializer
634/// '&' identifier initializer
635///
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000636/// lambda-declarator:
637/// '(' parameter-declaration-clause ')' attribute-specifier[opt]
638/// 'mutable'[opt] exception-specification[opt]
639/// trailing-return-type[opt]
640///
641ExprResult Parser::ParseLambdaExpression() {
642 // Parse lambda-introducer.
643 LambdaIntroducer Intro;
Faisal Vali5fb7c3c2013-12-05 01:40:41 +0000644 Optional<unsigned> DiagID = ParseLambdaIntroducer(Intro);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000645 if (DiagID) {
646 Diag(Tok, DiagID.getValue());
Alexey Bataevee6507d2013-11-18 08:17:37 +0000647 SkipUntil(tok::r_square, StopAtSemi);
648 SkipUntil(tok::l_brace, StopAtSemi);
649 SkipUntil(tok::r_brace, StopAtSemi);
Eli Friedmanc7c97142012-01-04 02:40:39 +0000650 return ExprError();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000651 }
652
653 return ParseLambdaExpressionAfterIntroducer(Intro);
654}
655
656/// TryParseLambdaExpression - Use lookahead and potentially tentative
657/// parsing to determine if we are looking at a C++0x lambda expression, and parse
658/// it if we are.
659///
660/// If we are not looking at a lambda expression, returns ExprError().
661ExprResult Parser::TryParseLambdaExpression() {
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000662 assert(getLangOpts().CPlusPlus11
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000663 && Tok.is(tok::l_square)
664 && "Not at the start of a possible lambda expression.");
665
666 const Token Next = NextToken(), After = GetLookAheadToken(2);
667
668 // If lookahead indicates this is a lambda...
669 if (Next.is(tok::r_square) || // []
670 Next.is(tok::equal) || // [=
671 (Next.is(tok::amp) && // [&] or [&,
672 (After.is(tok::r_square) ||
673 After.is(tok::comma))) ||
674 (Next.is(tok::identifier) && // [identifier]
675 After.is(tok::r_square))) {
676 return ParseLambdaExpression();
677 }
678
Eli Friedmanc7c97142012-01-04 02:40:39 +0000679 // If lookahead indicates an ObjC message send...
680 // [identifier identifier
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000681 if (Next.is(tok::identifier) && After.is(tok::identifier)) {
Eli Friedmanc7c97142012-01-04 02:40:39 +0000682 return ExprEmpty();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000683 }
Faisal Vali5fb7c3c2013-12-05 01:40:41 +0000684
Eli Friedmanc7c97142012-01-04 02:40:39 +0000685 // Here, we're stuck: lambda introducers and Objective-C message sends are
686 // unambiguous, but it requires arbitrary lookhead. [a,b,c,d,e,f,g] is a
687 // lambda, and [a,b,c,d,e,f,g h] is a Objective-C message send. Instead of
688 // writing two routines to parse a lambda introducer, just try to parse
689 // a lambda introducer first, and fall back if that fails.
690 // (TryParseLambdaIntroducer never produces any diagnostic output.)
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000691 LambdaIntroducer Intro;
692 if (TryParseLambdaIntroducer(Intro))
Eli Friedmanc7c97142012-01-04 02:40:39 +0000693 return ExprEmpty();
Faisal Vali5fb7c3c2013-12-05 01:40:41 +0000694
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000695 return ParseLambdaExpressionAfterIntroducer(Intro);
696}
697
Richard Smithf44d2a82013-05-21 22:21:19 +0000698/// \brief Parse a lambda introducer.
699/// \param Intro A LambdaIntroducer filled in with information about the
700/// contents of the lambda-introducer.
701/// \param SkippedInits If non-null, we are disambiguating between an Obj-C
702/// message send and a lambda expression. In this mode, we will
703/// sometimes skip the initializers for init-captures and not fully
704/// populate \p Intro. This flag will be set to \c true if we do so.
705/// \return A DiagnosticID if it hit something unexpected. The location for
706/// for the diagnostic is that of the current token.
707Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro,
708 bool *SkippedInits) {
David Blaikie05785d12013-02-20 22:23:23 +0000709 typedef Optional<unsigned> DiagResult;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000710
711 assert(Tok.is(tok::l_square) && "Lambda expressions begin with '['.");
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000712 BalancedDelimiterTracker T(*this, tok::l_square);
713 T.consumeOpen();
714
715 Intro.Range.setBegin(T.getOpenLocation());
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000716
717 bool first = true;
718
719 // Parse capture-default.
720 if (Tok.is(tok::amp) &&
721 (NextToken().is(tok::comma) || NextToken().is(tok::r_square))) {
722 Intro.Default = LCD_ByRef;
Douglas Gregora1bffa22012-02-10 17:46:20 +0000723 Intro.DefaultLoc = ConsumeToken();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000724 first = false;
725 } else if (Tok.is(tok::equal)) {
726 Intro.Default = LCD_ByCopy;
Douglas Gregora1bffa22012-02-10 17:46:20 +0000727 Intro.DefaultLoc = ConsumeToken();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000728 first = false;
729 }
730
731 while (Tok.isNot(tok::r_square)) {
732 if (!first) {
Douglas Gregord8c61782012-02-15 15:34:24 +0000733 if (Tok.isNot(tok::comma)) {
Douglas Gregor721b14d2012-07-31 00:50:07 +0000734 // Provide a completion for a lambda introducer here. Except
735 // in Objective-C, where this is Almost Surely meant to be a message
736 // send. In that case, fail here and let the ObjC message
737 // expression parser perform the completion.
Douglas Gregor2d8db8f2012-07-31 15:27:48 +0000738 if (Tok.is(tok::code_completion) &&
739 !(getLangOpts().ObjC1 && Intro.Default == LCD_None &&
740 !Intro.Captures.empty())) {
Douglas Gregord8c61782012-02-15 15:34:24 +0000741 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
742 /*AfterAmpersand=*/false);
743 ConsumeCodeCompletionToken();
744 break;
745 }
746
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000747 return DiagResult(diag::err_expected_comma_or_rsquare);
Douglas Gregord8c61782012-02-15 15:34:24 +0000748 }
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000749 ConsumeToken();
750 }
751
Douglas Gregord8c61782012-02-15 15:34:24 +0000752 if (Tok.is(tok::code_completion)) {
753 // If we're in Objective-C++ and we have a bare '[', then this is more
754 // likely to be a message receiver.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000755 if (getLangOpts().ObjC1 && first)
Douglas Gregord8c61782012-02-15 15:34:24 +0000756 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
757 else
758 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
759 /*AfterAmpersand=*/false);
760 ConsumeCodeCompletionToken();
761 break;
762 }
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000763
Douglas Gregord8c61782012-02-15 15:34:24 +0000764 first = false;
765
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000766 // Parse capture.
767 LambdaCaptureKind Kind = LCK_ByCopy;
768 SourceLocation Loc;
769 IdentifierInfo* Id = 0;
Douglas Gregor3e308b12012-02-14 19:27:52 +0000770 SourceLocation EllipsisLoc;
Richard Smith21b3ab42013-05-09 21:36:41 +0000771 ExprResult Init;
Douglas Gregor3e308b12012-02-14 19:27:52 +0000772
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000773 if (Tok.is(tok::kw_this)) {
774 Kind = LCK_This;
775 Loc = ConsumeToken();
776 } else {
777 if (Tok.is(tok::amp)) {
778 Kind = LCK_ByRef;
779 ConsumeToken();
Douglas Gregord8c61782012-02-15 15:34:24 +0000780
781 if (Tok.is(tok::code_completion)) {
782 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
783 /*AfterAmpersand=*/true);
784 ConsumeCodeCompletionToken();
785 break;
786 }
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000787 }
788
789 if (Tok.is(tok::identifier)) {
790 Id = Tok.getIdentifierInfo();
791 Loc = ConsumeToken();
792 } else if (Tok.is(tok::kw_this)) {
793 // FIXME: If we want to suggest a fixit here, will need to return more
794 // than just DiagnosticID. Perhaps full DiagnosticBuilder that can be
795 // Clear()ed to prevent emission in case of tentative parsing?
796 return DiagResult(diag::err_this_captured_by_reference);
797 } else {
798 return DiagResult(diag::err_expected_capture);
799 }
Richard Smith21b3ab42013-05-09 21:36:41 +0000800
801 if (Tok.is(tok::l_paren)) {
802 BalancedDelimiterTracker Parens(*this, tok::l_paren);
803 Parens.consumeOpen();
804
805 ExprVector Exprs;
806 CommaLocsTy Commas;
Richard Smithf44d2a82013-05-21 22:21:19 +0000807 if (SkippedInits) {
808 Parens.skipToEnd();
809 *SkippedInits = true;
810 } else if (ParseExpressionList(Exprs, Commas)) {
Richard Smith21b3ab42013-05-09 21:36:41 +0000811 Parens.skipToEnd();
812 Init = ExprError();
813 } else {
814 Parens.consumeClose();
815 Init = Actions.ActOnParenListExpr(Parens.getOpenLocation(),
816 Parens.getCloseLocation(),
817 Exprs);
818 }
819 } else if (Tok.is(tok::l_brace) || Tok.is(tok::equal)) {
Faisal Vali5fb7c3c2013-12-05 01:40:41 +0000820 // Each lambda init-capture forms its own full expression, which clears
821 // Actions.MaybeODRUseExprs. So create an expression evaluation context
822 // to save the necessary state, and restore it later.
823 EnterExpressionEvaluationContext EC(Actions,
824 Sema::PotentiallyEvaluated);
Alp Tokera3ebe6e2013-12-17 14:12:37 +0000825 TryConsumeToken(tok::equal);
Richard Smith21b3ab42013-05-09 21:36:41 +0000826
Richard Smithf44d2a82013-05-21 22:21:19 +0000827 if (!SkippedInits)
828 Init = ParseInitializer();
829 else if (Tok.is(tok::l_brace)) {
830 BalancedDelimiterTracker Braces(*this, tok::l_brace);
831 Braces.consumeOpen();
832 Braces.skipToEnd();
833 *SkippedInits = true;
834 } else {
835 // We're disambiguating this:
836 //
837 // [..., x = expr
838 //
839 // We need to find the end of the following expression in order to
840 // determine whether this is an Obj-C message send's receiver, or a
841 // lambda init-capture.
842 //
843 // Parse the expression to find where it ends, and annotate it back
844 // onto the tokens. We would have parsed this expression the same way
845 // in either case: both the RHS of an init-capture and the RHS of an
846 // assignment expression are parsed as an initializer-clause, and in
847 // neither case can anything be added to the scope between the '[' and
848 // here.
849 //
850 // FIXME: This is horrible. Adding a mechanism to skip an expression
851 // would be much cleaner.
852 // FIXME: If there is a ',' before the next ']' or ':', we can skip to
853 // that instead. (And if we see a ':' with no matching '?', we can
854 // classify this as an Obj-C message send.)
855 SourceLocation StartLoc = Tok.getLocation();
856 InMessageExpressionRAIIObject MaybeInMessageExpression(*this, true);
857 Init = ParseInitializer();
858
859 if (Tok.getLocation() != StartLoc) {
860 // Back out the lexing of the token after the initializer.
861 PP.RevertCachedTokens(1);
862
863 // Replace the consumed tokens with an appropriate annotation.
864 Tok.setLocation(StartLoc);
865 Tok.setKind(tok::annot_primary_expr);
866 setExprAnnotation(Tok, Init);
867 Tok.setAnnotationEndLoc(PP.getLastCachedTokenLocation());
868 PP.AnnotateCachedTokens(Tok);
869
870 // Consume the annotated initializer.
871 ConsumeToken();
872 }
873 }
Alp Tokera3ebe6e2013-12-17 14:12:37 +0000874 } else
875 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000876 }
Faisal Vali5fb7c3c2013-12-05 01:40:41 +0000877 // If this is an init capture, process the initialization expression
878 // right away. For lambda init-captures such as the following:
879 // const int x = 10;
880 // auto L = [i = x+1](int a) {
881 // return [j = x+2,
882 // &k = x](char b) { };
883 // };
884 // keep in mind that each lambda init-capture has to have:
885 // - its initialization expression executed in the context
886 // of the enclosing/parent decl-context.
887 // - but the variable itself has to be 'injected' into the
888 // decl-context of its lambda's call-operator (which has
889 // not yet been created).
890 // Each init-expression is a full-expression that has to get
891 // Sema-analyzed (for capturing etc.) before its lambda's
892 // call-operator's decl-context, scope & scopeinfo are pushed on their
893 // respective stacks. Thus if any variable is odr-used in the init-capture
894 // it will correctly get captured in the enclosing lambda, if one exists.
895 // The init-variables above are created later once the lambdascope and
896 // call-operators decl-context is pushed onto its respective stack.
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000897
Faisal Vali5fb7c3c2013-12-05 01:40:41 +0000898 // Since the lambda init-capture's initializer expression occurs in the
899 // context of the enclosing function or lambda, therefore we can not wait
900 // till a lambda scope has been pushed on before deciding whether the
901 // variable needs to be captured. We also need to process all
902 // lvalue-to-rvalue conversions and discarded-value conversions,
903 // so that we can avoid capturing certain constant variables.
904 // For e.g.,
905 // void test() {
906 // const int x = 10;
907 // auto L = [&z = x](char a) { <-- don't capture by the current lambda
908 // return [y = x](int i) { <-- don't capture by enclosing lambda
909 // return y;
910 // }
911 // };
912 // If x was not const, the second use would require 'L' to capture, and
913 // that would be an error.
914
915 ParsedType InitCaptureParsedType;
916 if (Init.isUsable()) {
917 // Get the pointer and store it in an lvalue, so we can use it as an
918 // out argument.
919 Expr *InitExpr = Init.get();
920 // This performs any lvalue-to-rvalue conversions if necessary, which
921 // can affect what gets captured in the containing decl-context.
922 QualType InitCaptureType = Actions.performLambdaInitCaptureInitialization(
923 Loc, Kind == LCK_ByRef, Id, InitExpr);
924 Init = InitExpr;
925 InitCaptureParsedType.set(InitCaptureType);
926 }
927 Intro.addCapture(Kind, Loc, Id, EllipsisLoc, Init, InitCaptureParsedType);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000928 }
929
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000930 T.consumeClose();
931 Intro.Range.setEnd(T.getCloseLocation());
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000932 return DiagResult();
933}
934
Douglas Gregord8c61782012-02-15 15:34:24 +0000935/// TryParseLambdaIntroducer - Tentatively parse a lambda introducer.
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000936///
937/// Returns true if it hit something unexpected.
938bool Parser::TryParseLambdaIntroducer(LambdaIntroducer &Intro) {
939 TentativeParsingAction PA(*this);
940
Richard Smithf44d2a82013-05-21 22:21:19 +0000941 bool SkippedInits = false;
942 Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro, &SkippedInits));
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000943
944 if (DiagID) {
945 PA.Revert();
946 return true;
947 }
948
Richard Smithf44d2a82013-05-21 22:21:19 +0000949 if (SkippedInits) {
950 // Parse it again, but this time parse the init-captures too.
951 PA.Revert();
952 Intro = LambdaIntroducer();
953 DiagID = ParseLambdaIntroducer(Intro);
954 assert(!DiagID && "parsing lambda-introducer failed on reparse");
955 return false;
956 }
957
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000958 PA.Commit();
959 return false;
960}
961
962/// ParseLambdaExpressionAfterIntroducer - Parse the rest of a lambda
963/// expression.
964ExprResult Parser::ParseLambdaExpressionAfterIntroducer(
965 LambdaIntroducer &Intro) {
Eli Friedmanc7c97142012-01-04 02:40:39 +0000966 SourceLocation LambdaBeginLoc = Intro.Range.getBegin();
967 Diag(LambdaBeginLoc, diag::warn_cxx98_compat_lambda);
968
969 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), LambdaBeginLoc,
970 "lambda expression parsing");
971
Faisal Vali2b391ab2013-09-26 19:54:12 +0000972
973
Richard Smith21b3ab42013-05-09 21:36:41 +0000974 // FIXME: Call into Actions to add any init-capture declarations to the
975 // scope while parsing the lambda-declarator and compound-statement.
976
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000977 // Parse lambda-declarator[opt].
978 DeclSpec DS(AttrFactory);
Eli Friedman36d12942012-01-04 04:41:38 +0000979 Declarator D(DS, Declarator::LambdaExprContext);
Faisal Vali2b391ab2013-09-26 19:54:12 +0000980 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
981 Actions.PushLambdaScope();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000982
983 if (Tok.is(tok::l_paren)) {
984 ParseScope PrototypeScope(this,
985 Scope::FunctionPrototypeScope |
Richard Smithe233fbf2013-01-28 22:42:45 +0000986 Scope::FunctionDeclarationScope |
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000987 Scope::DeclScope);
988
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000989 SourceLocation DeclEndLoc;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000990 BalancedDelimiterTracker T(*this, tok::l_paren);
991 T.consumeOpen();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000992 SourceLocation LParenLoc = T.getOpenLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000993
994 // Parse parameter-declaration-clause.
995 ParsedAttributes Attr(AttrFactory);
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000996 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000997 SourceLocation EllipsisLoc;
998
Faisal Vali2b391ab2013-09-26 19:54:12 +0000999
1000 if (Tok.isNot(tok::r_paren)) {
Faisal Vali2b391ab2013-09-26 19:54:12 +00001001 Actions.RecordParsingTemplateParameterDepth(TemplateParameterDepth);
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001002 ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc);
Faisal Vali2b391ab2013-09-26 19:54:12 +00001003 // For a generic lambda, each 'auto' within the parameter declaration
1004 // clause creates a template type parameter, so increment the depth.
1005 if (Actions.getCurGenericLambda())
1006 ++CurTemplateDepthTracker;
1007 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001008 T.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001009 SourceLocation RParenLoc = T.getCloseLocation();
1010 DeclEndLoc = RParenLoc;
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001011
1012 // Parse 'mutable'[opt].
1013 SourceLocation MutableLoc;
Alp Toker094e5212014-01-05 03:27:11 +00001014 if (TryConsumeToken(tok::kw_mutable, MutableLoc))
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001015 DeclEndLoc = MutableLoc;
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001016
1017 // Parse exception-specification[opt].
1018 ExceptionSpecificationType ESpecType = EST_None;
1019 SourceRange ESpecRange;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001020 SmallVector<ParsedType, 2> DynamicExceptions;
1021 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001022 ExprResult NoexceptExpr;
Richard Smith2331bbf2012-05-02 22:22:32 +00001023 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00001024 DynamicExceptions,
1025 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00001026 NoexceptExpr);
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001027
1028 if (ESpecType != EST_None)
1029 DeclEndLoc = ESpecRange.getEnd();
1030
1031 // Parse attribute-specifier[opt].
Richard Smith89645bc2013-01-02 12:01:23 +00001032 MaybeParseCXX11Attributes(Attr, &DeclEndLoc);
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001033
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001034 SourceLocation FunLocalRangeEnd = DeclEndLoc;
1035
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001036 // Parse trailing-return-type[opt].
Richard Smith700537c2012-06-12 01:51:59 +00001037 TypeResult TrailingReturnType;
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001038 if (Tok.is(tok::arrow)) {
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001039 FunLocalRangeEnd = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001040 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00001041 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001042 if (Range.getEnd().isValid())
1043 DeclEndLoc = Range.getEnd();
1044 }
1045
1046 PrototypeScope.Exit();
1047
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001048 SourceLocation NoLoc;
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001049 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001050 /*isAmbiguous=*/false,
1051 LParenLoc,
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001052 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001053 EllipsisLoc, RParenLoc,
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001054 DS.getTypeQualifiers(),
1055 /*RefQualifierIsLValueRef=*/true,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001056 /*RefQualifierLoc=*/NoLoc,
1057 /*ConstQualifierLoc=*/NoLoc,
1058 /*VolatileQualifierLoc=*/NoLoc,
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001059 MutableLoc,
1060 ESpecType, ESpecRange.getBegin(),
1061 DynamicExceptions.data(),
1062 DynamicExceptionRanges.data(),
1063 DynamicExceptions.size(),
1064 NoexceptExpr.isUsable() ?
1065 NoexceptExpr.get() : 0,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001066 LParenLoc, FunLocalRangeEnd, D,
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001067 TrailingReturnType),
1068 Attr, DeclEndLoc);
Aaron Ballmanb5c59f52014-03-11 13:03:15 +00001069 } else if (Tok.is(tok::kw_mutable) || Tok.is(tok::arrow) ||
1070 (Tok.is(tok::l_square) && NextToken().is(tok::l_square))) {
1071 // It's common to forget that one needs '()' before 'mutable', an attribute
1072 // specifier, or the result type. Deal with this.
1073 unsigned TokKind = 0;
1074 switch (Tok.getKind()) {
1075 case tok::kw_mutable: TokKind = 0; break;
1076 case tok::arrow: TokKind = 1; break;
1077 case tok::l_square: TokKind = 2; break;
1078 default: llvm_unreachable("Unknown token kind");
1079 }
1080
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001081 Diag(Tok, diag::err_lambda_missing_parens)
Aaron Ballmanb5c59f52014-03-11 13:03:15 +00001082 << TokKind
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001083 << FixItHint::CreateInsertion(Tok.getLocation(), "() ");
1084 SourceLocation DeclLoc = Tok.getLocation();
1085 SourceLocation DeclEndLoc = DeclLoc;
1086
1087 // Parse 'mutable', if it's there.
1088 SourceLocation MutableLoc;
1089 if (Tok.is(tok::kw_mutable)) {
1090 MutableLoc = ConsumeToken();
1091 DeclEndLoc = MutableLoc;
1092 }
Aaron Ballmanb5c59f52014-03-11 13:03:15 +00001093
1094 // Parse attribute-specifier[opt].
1095 ParsedAttributes Attr(AttrFactory);
1096 MaybeParseCXX11Attributes(Attr, &DeclEndLoc);
1097
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001098 // Parse the return type, if there is one.
Richard Smith700537c2012-06-12 01:51:59 +00001099 TypeResult TrailingReturnType;
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001100 if (Tok.is(tok::arrow)) {
1101 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00001102 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001103 if (Range.getEnd().isValid())
1104 DeclEndLoc = Range.getEnd();
1105 }
1106
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001107 SourceLocation NoLoc;
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001108 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001109 /*isAmbiguous=*/false,
1110 /*LParenLoc=*/NoLoc,
1111 /*Params=*/0,
1112 /*NumParams=*/0,
1113 /*EllipsisLoc=*/NoLoc,
1114 /*RParenLoc=*/NoLoc,
1115 /*TypeQuals=*/0,
1116 /*RefQualifierIsLValueRef=*/true,
1117 /*RefQualifierLoc=*/NoLoc,
1118 /*ConstQualifierLoc=*/NoLoc,
1119 /*VolatileQualifierLoc=*/NoLoc,
1120 MutableLoc,
1121 EST_None,
1122 /*ESpecLoc=*/NoLoc,
1123 /*Exceptions=*/0,
1124 /*ExceptionRanges=*/0,
1125 /*NumExceptions=*/0,
1126 /*NoexceptExpr=*/0,
1127 DeclLoc, DeclEndLoc, D,
1128 TrailingReturnType),
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001129 Attr, DeclEndLoc);
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001130 }
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001131
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001132
Eli Friedman4817cf72012-01-06 03:05:34 +00001133 // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
1134 // it.
Douglas Gregorb8389972012-02-21 22:51:27 +00001135 unsigned ScopeFlags = Scope::BlockScope | Scope::FnScope | Scope::DeclScope;
Douglas Gregorb8389972012-02-21 22:51:27 +00001136 ParseScope BodyScope(this, ScopeFlags);
Eli Friedman4817cf72012-01-06 03:05:34 +00001137
Eli Friedman71c80552012-01-05 03:35:19 +00001138 Actions.ActOnStartOfLambdaDefinition(Intro, D, getCurScope());
1139
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001140 // Parse compound-statement.
Eli Friedmanc7c97142012-01-04 02:40:39 +00001141 if (!Tok.is(tok::l_brace)) {
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001142 Diag(Tok, diag::err_expected_lambda_body);
Eli Friedmanc7c97142012-01-04 02:40:39 +00001143 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
1144 return ExprError();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001145 }
1146
Eli Friedmanc7c97142012-01-04 02:40:39 +00001147 StmtResult Stmt(ParseCompoundStatementBody());
1148 BodyScope.Exit();
1149
Eli Friedman898caf82012-01-04 02:46:53 +00001150 if (!Stmt.isInvalid())
Douglas Gregor63798542012-02-20 19:44:39 +00001151 return Actions.ActOnLambdaExpr(LambdaBeginLoc, Stmt.take(), getCurScope());
Eli Friedmanc7c97142012-01-04 02:40:39 +00001152
Eli Friedman898caf82012-01-04 02:46:53 +00001153 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
1154 return ExprError();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001155}
1156
Chris Lattner29375652006-12-04 18:06:35 +00001157/// ParseCXXCasts - This handles the various ways to cast expressions to another
1158/// type.
1159///
1160/// postfix-expression: [C++ 5.2p1]
1161/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
1162/// 'static_cast' '<' type-name '>' '(' expression ')'
1163/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
1164/// 'const_cast' '<' type-name '>' '(' expression ')'
1165///
John McCalldadc5752010-08-24 06:29:42 +00001166ExprResult Parser::ParseCXXCasts() {
Chris Lattner29375652006-12-04 18:06:35 +00001167 tok::TokenKind Kind = Tok.getKind();
1168 const char *CastName = 0; // For error messages
1169
1170 switch (Kind) {
David Blaikieaa347f92011-09-23 20:26:49 +00001171 default: llvm_unreachable("Unknown C++ cast!");
Chris Lattner29375652006-12-04 18:06:35 +00001172 case tok::kw_const_cast: CastName = "const_cast"; break;
1173 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
1174 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
1175 case tok::kw_static_cast: CastName = "static_cast"; break;
1176 }
1177
1178 SourceLocation OpLoc = ConsumeToken();
1179 SourceLocation LAngleBracketLoc = Tok.getLocation();
1180
Richard Smith55858492011-04-14 21:45:45 +00001181 // Check for "<::" which is parsed as "[:". If found, fix token stream,
1182 // diagnose error, suggest fix, and recover parsing.
Richard Smith62e66302012-08-20 17:37:52 +00001183 if (Tok.is(tok::l_square) && Tok.getLength() == 2) {
1184 Token Next = NextToken();
1185 if (Next.is(tok::colon) && areTokensAdjacent(Tok, Next))
1186 FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
1187 }
Richard Smith55858492011-04-14 21:45:45 +00001188
Chris Lattner29375652006-12-04 18:06:35 +00001189 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redld65cea82008-12-11 22:51:44 +00001190 return ExprError();
Chris Lattner29375652006-12-04 18:06:35 +00001191
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +00001192 // Parse the common declaration-specifiers piece.
1193 DeclSpec DS(AttrFactory);
1194 ParseSpecifierQualifierList(DS);
1195
1196 // Parse the abstract-declarator, if present.
1197 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1198 ParseDeclarator(DeclaratorInfo);
1199
Chris Lattner29375652006-12-04 18:06:35 +00001200 SourceLocation RAngleBracketLoc = Tok.getLocation();
1201
Alp Toker383d2c42014-01-01 03:08:43 +00001202 if (ExpectAndConsume(tok::greater))
Alp Tokerec543272013-12-24 09:48:30 +00001203 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << tok::less);
Chris Lattner29375652006-12-04 18:06:35 +00001204
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001205 SourceLocation LParenLoc, RParenLoc;
1206 BalancedDelimiterTracker T(*this, tok::l_paren);
Chris Lattner29375652006-12-04 18:06:35 +00001207
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001208 if (T.expectAndConsume(diag::err_expected_lparen_after, CastName))
Argyrios Kyrtzidis387a3342009-05-22 10:23:16 +00001209 return ExprError();
Chris Lattner29375652006-12-04 18:06:35 +00001210
John McCalldadc5752010-08-24 06:29:42 +00001211 ExprResult Result = ParseExpression();
Mike Stump11289f42009-09-09 15:08:12 +00001212
Argyrios Kyrtzidis387a3342009-05-22 10:23:16 +00001213 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001214 T.consumeClose();
Chris Lattner29375652006-12-04 18:06:35 +00001215
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +00001216 if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
Douglas Gregore200adc2008-10-27 19:41:14 +00001217 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +00001218 LAngleBracketLoc, DeclaratorInfo,
Douglas Gregor220cac52009-02-18 17:45:20 +00001219 RAngleBracketLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001220 T.getOpenLocation(), Result.take(),
1221 T.getCloseLocation());
Chris Lattner29375652006-12-04 18:06:35 +00001222
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001223 return Result;
Chris Lattner29375652006-12-04 18:06:35 +00001224}
Bill Wendling4073ed52007-02-13 01:51:42 +00001225
Sebastian Redlc4704762008-11-11 11:37:55 +00001226/// ParseCXXTypeid - This handles the C++ typeid expression.
1227///
1228/// postfix-expression: [C++ 5.2p1]
1229/// 'typeid' '(' expression ')'
1230/// 'typeid' '(' type-id ')'
1231///
John McCalldadc5752010-08-24 06:29:42 +00001232ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc4704762008-11-11 11:37:55 +00001233 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
1234
1235 SourceLocation OpLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001236 SourceLocation LParenLoc, RParenLoc;
1237 BalancedDelimiterTracker T(*this, tok::l_paren);
Sebastian Redlc4704762008-11-11 11:37:55 +00001238
1239 // typeid expressions are always parenthesized.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001240 if (T.expectAndConsume(diag::err_expected_lparen_after, "typeid"))
Sebastian Redld65cea82008-12-11 22:51:44 +00001241 return ExprError();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001242 LParenLoc = T.getOpenLocation();
Sebastian Redlc4704762008-11-11 11:37:55 +00001243
John McCalldadc5752010-08-24 06:29:42 +00001244 ExprResult Result;
Sebastian Redlc4704762008-11-11 11:37:55 +00001245
Richard Smith4f605af2012-08-18 00:55:03 +00001246 // C++0x [expr.typeid]p3:
1247 // When typeid is applied to an expression other than an lvalue of a
1248 // polymorphic class type [...] The expression is an unevaluated
1249 // operand (Clause 5).
1250 //
1251 // Note that we can't tell whether the expression is an lvalue of a
1252 // polymorphic class type until after we've parsed the expression; we
1253 // speculatively assume the subexpression is unevaluated, and fix it up
1254 // later.
1255 //
1256 // We enter the unevaluated context before trying to determine whether we
1257 // have a type-id, because the tentative parse logic will try to resolve
1258 // names, and must treat them as unevaluated.
Eli Friedman15681d62012-09-26 04:34:21 +00001259 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
1260 Sema::ReuseLambdaContextDecl);
Richard Smith4f605af2012-08-18 00:55:03 +00001261
Sebastian Redlc4704762008-11-11 11:37:55 +00001262 if (isTypeIdInParens()) {
Douglas Gregor220cac52009-02-18 17:45:20 +00001263 TypeResult Ty = ParseTypeName();
Sebastian Redlc4704762008-11-11 11:37:55 +00001264
1265 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001266 T.consumeClose();
1267 RParenLoc = T.getCloseLocation();
Douglas Gregor4c7c1092010-09-08 23:14:30 +00001268 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +00001269 return ExprError();
Sebastian Redlc4704762008-11-11 11:37:55 +00001270
1271 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallba7bf592010-08-24 05:47:05 +00001272 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc4704762008-11-11 11:37:55 +00001273 } else {
1274 Result = ParseExpression();
1275
1276 // Match the ')'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001277 if (Result.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00001278 SkipUntil(tok::r_paren, StopAtSemi);
Sebastian Redlc4704762008-11-11 11:37:55 +00001279 else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001280 T.consumeClose();
1281 RParenLoc = T.getCloseLocation();
Douglas Gregor4c7c1092010-09-08 23:14:30 +00001282 if (RParenLoc.isInvalid())
1283 return ExprError();
Douglas Gregor1beec452011-03-12 01:48:56 +00001284
Sebastian Redlc4704762008-11-11 11:37:55 +00001285 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001286 Result.release(), RParenLoc);
Sebastian Redlc4704762008-11-11 11:37:55 +00001287 }
1288 }
1289
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001290 return Result;
Sebastian Redlc4704762008-11-11 11:37:55 +00001291}
1292
Francois Pichet9f4f2072010-09-08 12:20:18 +00001293/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
1294///
1295/// '__uuidof' '(' expression ')'
1296/// '__uuidof' '(' type-id ')'
1297///
1298ExprResult Parser::ParseCXXUuidof() {
1299 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
1300
1301 SourceLocation OpLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001302 BalancedDelimiterTracker T(*this, tok::l_paren);
Francois Pichet9f4f2072010-09-08 12:20:18 +00001303
1304 // __uuidof expressions are always parenthesized.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001305 if (T.expectAndConsume(diag::err_expected_lparen_after, "__uuidof"))
Francois Pichet9f4f2072010-09-08 12:20:18 +00001306 return ExprError();
1307
1308 ExprResult Result;
1309
1310 if (isTypeIdInParens()) {
1311 TypeResult Ty = ParseTypeName();
1312
1313 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001314 T.consumeClose();
Francois Pichet9f4f2072010-09-08 12:20:18 +00001315
1316 if (Ty.isInvalid())
1317 return ExprError();
1318
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001319 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), /*isType=*/true,
1320 Ty.get().getAsOpaquePtr(),
1321 T.getCloseLocation());
Francois Pichet9f4f2072010-09-08 12:20:18 +00001322 } else {
1323 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1324 Result = ParseExpression();
1325
1326 // Match the ')'.
1327 if (Result.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00001328 SkipUntil(tok::r_paren, StopAtSemi);
Francois Pichet9f4f2072010-09-08 12:20:18 +00001329 else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001330 T.consumeClose();
Francois Pichet9f4f2072010-09-08 12:20:18 +00001331
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001332 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(),
1333 /*isType=*/false,
1334 Result.release(), T.getCloseLocation());
Francois Pichet9f4f2072010-09-08 12:20:18 +00001335 }
1336 }
1337
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001338 return Result;
Francois Pichet9f4f2072010-09-08 12:20:18 +00001339}
1340
Douglas Gregore610ada2010-02-24 18:44:31 +00001341/// \brief Parse a C++ pseudo-destructor expression after the base,
1342/// . or -> operator, and nested-name-specifier have already been
1343/// parsed.
1344///
1345/// postfix-expression: [C++ 5.2]
1346/// postfix-expression . pseudo-destructor-name
1347/// postfix-expression -> pseudo-destructor-name
1348///
1349/// pseudo-destructor-name:
1350/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
1351/// ::[opt] nested-name-specifier template simple-template-id ::
1352/// ~type-name
1353/// ::[opt] nested-name-specifier[opt] ~type-name
1354///
John McCalldadc5752010-08-24 06:29:42 +00001355ExprResult
Douglas Gregore610ada2010-02-24 18:44:31 +00001356Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
1357 tok::TokenKind OpKind,
1358 CXXScopeSpec &SS,
John McCallba7bf592010-08-24 05:47:05 +00001359 ParsedType ObjectType) {
Douglas Gregore610ada2010-02-24 18:44:31 +00001360 // We're parsing either a pseudo-destructor-name or a dependent
1361 // member access that has the same form as a
1362 // pseudo-destructor-name. We parse both in the same way and let
1363 // the action model sort them out.
1364 //
1365 // Note that the ::[opt] nested-name-specifier[opt] has already
1366 // been parsed, and if there was a simple-template-id, it has
1367 // been coalesced into a template-id annotation token.
1368 UnqualifiedId FirstTypeName;
1369 SourceLocation CCLoc;
1370 if (Tok.is(tok::identifier)) {
1371 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1372 ConsumeToken();
1373 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1374 CCLoc = ConsumeToken();
1375 } else if (Tok.is(tok::annot_template_id)) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001376 // FIXME: retrieve TemplateKWLoc from template-id annotation and
1377 // store it in the pseudo-dtor node (to be used when instantiating it).
Douglas Gregore610ada2010-02-24 18:44:31 +00001378 FirstTypeName.setTemplateId(
1379 (TemplateIdAnnotation *)Tok.getAnnotationValue());
1380 ConsumeToken();
1381 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1382 CCLoc = ConsumeToken();
1383 } else {
1384 FirstTypeName.setIdentifier(0, SourceLocation());
1385 }
1386
1387 // Parse the tilde.
1388 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
1389 SourceLocation TildeLoc = ConsumeToken();
David Blaikie1d578782011-12-16 16:03:09 +00001390
1391 if (Tok.is(tok::kw_decltype) && !FirstTypeName.isValid() && SS.isEmpty()) {
1392 DeclSpec DS(AttrFactory);
Benjamin Kramer198e0832011-12-18 12:18:02 +00001393 ParseDecltypeSpecifier(DS);
David Blaikie1d578782011-12-16 16:03:09 +00001394 if (DS.getTypeSpecType() == TST_error)
1395 return ExprError();
1396 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, OpLoc,
1397 OpKind, TildeLoc, DS,
1398 Tok.is(tok::l_paren));
1399 }
1400
Douglas Gregore610ada2010-02-24 18:44:31 +00001401 if (!Tok.is(tok::identifier)) {
1402 Diag(Tok, diag::err_destructor_tilde_identifier);
1403 return ExprError();
1404 }
1405
1406 // Parse the second type.
1407 UnqualifiedId SecondTypeName;
1408 IdentifierInfo *Name = Tok.getIdentifierInfo();
1409 SourceLocation NameLoc = ConsumeToken();
1410 SecondTypeName.setIdentifier(Name, NameLoc);
1411
1412 // If there is a '<', the second type name is a template-id. Parse
1413 // it as such.
1414 if (Tok.is(tok::less) &&
Abramo Bagnara7945c982012-01-27 09:46:47 +00001415 ParseUnqualifiedIdTemplateId(SS, SourceLocation(),
1416 Name, NameLoc,
1417 false, ObjectType, SecondTypeName,
1418 /*AssumeTemplateName=*/true))
Douglas Gregore610ada2010-02-24 18:44:31 +00001419 return ExprError();
1420
John McCallb268a282010-08-23 23:25:46 +00001421 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
1422 OpLoc, OpKind,
Douglas Gregore610ada2010-02-24 18:44:31 +00001423 SS, FirstTypeName, CCLoc,
1424 TildeLoc, SecondTypeName,
1425 Tok.is(tok::l_paren));
1426}
1427
Bill Wendling4073ed52007-02-13 01:51:42 +00001428/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
1429///
1430/// boolean-literal: [C++ 2.13.5]
1431/// 'true'
1432/// 'false'
John McCalldadc5752010-08-24 06:29:42 +00001433ExprResult Parser::ParseCXXBoolLiteral() {
Bill Wendling4073ed52007-02-13 01:51:42 +00001434 tok::TokenKind Kind = Tok.getKind();
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001435 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Bill Wendling4073ed52007-02-13 01:51:42 +00001436}
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001437
1438/// ParseThrowExpression - This handles the C++ throw expression.
1439///
1440/// throw-expression: [C++ 15]
1441/// 'throw' assignment-expression[opt]
John McCalldadc5752010-08-24 06:29:42 +00001442ExprResult Parser::ParseThrowExpression() {
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001443 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001444 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redld65cea82008-12-11 22:51:44 +00001445
Chris Lattner65dd8432008-04-06 06:02:23 +00001446 // If the current token isn't the start of an assignment-expression,
1447 // then the expression is not present. This handles things like:
1448 // "C ? throw : (void)42", which is crazy but legal.
1449 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
1450 case tok::semi:
1451 case tok::r_paren:
1452 case tok::r_square:
1453 case tok::r_brace:
1454 case tok::colon:
1455 case tok::comma:
Douglas Gregor53e191ed2011-07-06 22:04:06 +00001456 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0);
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001457
Chris Lattner65dd8432008-04-06 06:02:23 +00001458 default:
John McCalldadc5752010-08-24 06:29:42 +00001459 ExprResult Expr(ParseAssignmentExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001460 if (Expr.isInvalid()) return Expr;
Douglas Gregor53e191ed2011-07-06 22:04:06 +00001461 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take());
Chris Lattner65dd8432008-04-06 06:02:23 +00001462 }
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001463}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001464
1465/// ParseCXXThis - This handles the C++ 'this' pointer.
1466///
1467/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
1468/// a non-lvalue expression whose value is the address of the object for which
1469/// the function is called.
John McCalldadc5752010-08-24 06:29:42 +00001470ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001471 assert(Tok.is(tok::kw_this) && "Not 'this'!");
1472 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001473 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001474}
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001475
1476/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1477/// Can be interpreted either as function-style casting ("int(x)")
1478/// or class type construction ("ClassType(x,y,z)")
1479/// or creation of a value-initialized type ("int()").
Sebastian Redl3da34892011-06-05 12:23:16 +00001480/// See [C++ 5.2.3].
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001481///
1482/// postfix-expression: [C++ 5.2p1]
Sebastian Redl3da34892011-06-05 12:23:16 +00001483/// simple-type-specifier '(' expression-list[opt] ')'
1484/// [C++0x] simple-type-specifier braced-init-list
1485/// typename-specifier '(' expression-list[opt] ')'
1486/// [C++0x] typename-specifier braced-init-list
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001487///
John McCalldadc5752010-08-24 06:29:42 +00001488ExprResult
Sebastian Redld65cea82008-12-11 22:51:44 +00001489Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001490 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallba7bf592010-08-24 05:47:05 +00001491 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001492
Sebastian Redl3da34892011-06-05 12:23:16 +00001493 assert((Tok.is(tok::l_paren) ||
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001494 (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)))
Sebastian Redl3da34892011-06-05 12:23:16 +00001495 && "Expected '(' or '{'!");
Douglas Gregor94a32472011-01-11 00:33:19 +00001496
Sebastian Redl3da34892011-06-05 12:23:16 +00001497 if (Tok.is(tok::l_brace)) {
Sebastian Redld74dd492012-02-12 18:41:05 +00001498 ExprResult Init = ParseBraceInitializer();
1499 if (Init.isInvalid())
1500 return Init;
1501 Expr *InitList = Init.take();
1502 return Actions.ActOnCXXTypeConstructExpr(TypeRep, SourceLocation(),
1503 MultiExprArg(&InitList, 1),
1504 SourceLocation());
Sebastian Redl3da34892011-06-05 12:23:16 +00001505 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001506 BalancedDelimiterTracker T(*this, tok::l_paren);
1507 T.consumeOpen();
Sebastian Redl3da34892011-06-05 12:23:16 +00001508
Benjamin Kramerf0623432012-08-23 22:51:59 +00001509 ExprVector Exprs;
Sebastian Redl3da34892011-06-05 12:23:16 +00001510 CommaLocsTy CommaLocs;
1511
1512 if (Tok.isNot(tok::r_paren)) {
1513 if (ParseExpressionList(Exprs, CommaLocs)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001514 SkipUntil(tok::r_paren, StopAtSemi);
Sebastian Redl3da34892011-06-05 12:23:16 +00001515 return ExprError();
1516 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001517 }
Sebastian Redl3da34892011-06-05 12:23:16 +00001518
1519 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001520 T.consumeClose();
Sebastian Redl3da34892011-06-05 12:23:16 +00001521
1522 // TypeRep could be null, if it references an invalid typedef.
1523 if (!TypeRep)
1524 return ExprError();
1525
1526 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1527 "Unexpected number of commas!");
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001528 return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001529 Exprs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001530 T.getCloseLocation());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001531 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001532}
1533
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001534/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001535///
1536/// condition:
1537/// expression
1538/// type-specifier-seq declarator '=' assignment-expression
Richard Smith2a15b742012-02-22 06:49:09 +00001539/// [C++11] type-specifier-seq declarator '=' initializer-clause
1540/// [C++11] type-specifier-seq declarator braced-init-list
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001541/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
1542/// '=' assignment-expression
1543///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00001544/// \param ExprOut if the condition was parsed as an expression, the parsed
1545/// expression.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001546///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00001547/// \param DeclOut if the condition was parsed as a declaration, the parsed
1548/// declaration.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001549///
Douglas Gregore60e41a2010-05-06 17:25:47 +00001550/// \param Loc The location of the start of the statement that requires this
1551/// condition, e.g., the "for" in a for loop.
1552///
1553/// \param ConvertToBoolean Whether the condition expression should be
1554/// converted to a boolean value.
1555///
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001556/// \returns true if there was a parsing, false otherwise.
John McCalldadc5752010-08-24 06:29:42 +00001557bool Parser::ParseCXXCondition(ExprResult &ExprOut,
1558 Decl *&DeclOut,
Douglas Gregore60e41a2010-05-06 17:25:47 +00001559 SourceLocation Loc,
1560 bool ConvertToBoolean) {
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001561 if (Tok.is(tok::code_completion)) {
John McCallfaf5fb42010-08-26 23:41:50 +00001562 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001563 cutOffParsing();
1564 return true;
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001565 }
1566
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001567 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00001568 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001569
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001570 if (!isCXXConditionDeclaration()) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001571 ProhibitAttributes(attrs);
1572
Douglas Gregore60e41a2010-05-06 17:25:47 +00001573 // Parse the expression.
John McCalldadc5752010-08-24 06:29:42 +00001574 ExprOut = ParseExpression(); // expression
1575 DeclOut = 0;
1576 if (ExprOut.isInvalid())
Douglas Gregore60e41a2010-05-06 17:25:47 +00001577 return true;
1578
1579 // If required, convert to a boolean value.
1580 if (ConvertToBoolean)
John McCalldadc5752010-08-24 06:29:42 +00001581 ExprOut
1582 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
1583 return ExprOut.isInvalid();
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001584 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001585
1586 // type-specifier-seq
John McCall084e83d2011-03-24 11:26:52 +00001587 DeclSpec DS(AttrFactory);
Richard Smith54ecd982013-02-20 19:22:51 +00001588 DS.takeAttributesFrom(attrs);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001589 ParseSpecifierQualifierList(DS);
1590
1591 // declarator
1592 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
1593 ParseDeclarator(DeclaratorInfo);
1594
1595 // simple-asm-expr[opt]
1596 if (Tok.is(tok::kw_asm)) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001597 SourceLocation Loc;
John McCalldadc5752010-08-24 06:29:42 +00001598 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001599 if (AsmLabel.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001600 SkipUntil(tok::semi, StopAtSemi);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001601 return true;
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001602 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001603 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001604 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001605 }
1606
1607 // If attributes are present, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001608 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001609
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001610 // Type-check the declaration itself.
John McCalldadc5752010-08-24 06:29:42 +00001611 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall53fa7142010-12-24 02:08:15 +00001612 DeclaratorInfo);
John McCalldadc5752010-08-24 06:29:42 +00001613 DeclOut = Dcl.get();
1614 ExprOut = ExprError();
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +00001615
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001616 // '=' assignment-expression
Richard Trieuc64d3232012-01-18 22:54:52 +00001617 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Smith2a15b742012-02-22 06:49:09 +00001618 bool CopyInitialization = isTokenEqualOrEqualTypo();
1619 if (CopyInitialization)
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001620 ConsumeToken();
Richard Smith2a15b742012-02-22 06:49:09 +00001621
1622 ExprResult InitExpr = ExprError();
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001623 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith2a15b742012-02-22 06:49:09 +00001624 Diag(Tok.getLocation(),
1625 diag::warn_cxx98_compat_generalized_initializer_lists);
1626 InitExpr = ParseBraceInitializer();
1627 } else if (CopyInitialization) {
1628 InitExpr = ParseAssignmentExpression();
1629 } else if (Tok.is(tok::l_paren)) {
1630 // This was probably an attempt to initialize the variable.
1631 SourceLocation LParen = ConsumeParen(), RParen = LParen;
Alexey Bataevee6507d2013-11-18 08:17:37 +00001632 if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch))
Richard Smith2a15b742012-02-22 06:49:09 +00001633 RParen = ConsumeParen();
1634 Diag(DeclOut ? DeclOut->getLocation() : LParen,
1635 diag::err_expected_init_in_condition_lparen)
1636 << SourceRange(LParen, RParen);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001637 } else {
Richard Smith2a15b742012-02-22 06:49:09 +00001638 Diag(DeclOut ? DeclOut->getLocation() : Tok.getLocation(),
1639 diag::err_expected_init_in_condition);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001640 }
Richard Smith2a15b742012-02-22 06:49:09 +00001641
1642 if (!InitExpr.isInvalid())
1643 Actions.AddInitializerToDecl(DeclOut, InitExpr.take(), !CopyInitialization,
Richard Smith74aeef52013-04-26 16:15:35 +00001644 DS.containsPlaceholderType());
Richard Smith27d807c2013-04-30 13:56:41 +00001645 else
1646 Actions.ActOnInitializerError(DeclOut);
Richard Smith2a15b742012-02-22 06:49:09 +00001647
Douglas Gregore60e41a2010-05-06 17:25:47 +00001648 // FIXME: Build a reference to this declaration? Convert it to bool?
1649 // (This is currently handled by Sema).
Richard Smithb2bc2e62011-02-21 20:05:19 +00001650
1651 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregore60e41a2010-05-06 17:25:47 +00001652
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001653 return false;
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001654}
1655
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001656/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1657/// This should only be called when the current token is known to be part of
1658/// simple-type-specifier.
1659///
1660/// simple-type-specifier:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001661/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001662/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1663/// char
1664/// wchar_t
1665/// bool
1666/// short
1667/// int
1668/// long
1669/// signed
1670/// unsigned
1671/// float
1672/// double
1673/// void
1674/// [GNU] typeof-specifier
1675/// [C++0x] auto [TODO]
1676///
1677/// type-name:
1678/// class-name
1679/// enum-name
1680/// typedef-name
1681///
1682void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1683 DS.SetRangeStart(Tok.getLocation());
1684 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +00001685 unsigned DiagID;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001686 SourceLocation Loc = Tok.getLocation();
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001687 const clang::PrintingPolicy &Policy =
1688 Actions.getASTContext().getPrintingPolicy();
Mike Stump11289f42009-09-09 15:08:12 +00001689
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001690 switch (Tok.getKind()) {
Chris Lattner45ddec32009-01-05 00:13:00 +00001691 case tok::identifier: // foo::bar
1692 case tok::coloncolon: // ::foo::bar
David Blaikie83d382b2011-09-23 05:06:16 +00001693 llvm_unreachable("Annotation token should already be formed!");
Mike Stump11289f42009-09-09 15:08:12 +00001694 default:
David Blaikie83d382b2011-09-23 05:06:16 +00001695 llvm_unreachable("Not a simple-type-specifier token!");
Chris Lattner45ddec32009-01-05 00:13:00 +00001696
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001697 // type-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00001698 case tok::annot_typename: {
Douglas Gregor0231d8d2011-01-19 20:10:05 +00001699 if (getTypeAnnotation(Tok))
1700 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001701 getTypeAnnotation(Tok), Policy);
Douglas Gregor0231d8d2011-01-19 20:10:05 +00001702 else
1703 DS.SetTypeSpecError();
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001704
1705 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1706 ConsumeToken();
1707
1708 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1709 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1710 // Objective-C interface. If we don't have Objective-C or a '<', this is
1711 // just a normal reference to a typedef name.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001712 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001713 ParseObjCProtocolQualifiers(DS);
1714
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001715 DS.Finish(Diags, PP, Policy);
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001716 return;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001717 }
Mike Stump11289f42009-09-09 15:08:12 +00001718
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001719 // builtin types
1720 case tok::kw_short:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001721 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001722 break;
1723 case tok::kw_long:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001724 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001725 break;
Francois Pichet84133e42011-04-28 01:59:37 +00001726 case tok::kw___int64:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001727 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID, Policy);
Francois Pichet84133e42011-04-28 01:59:37 +00001728 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001729 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001730 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001731 break;
1732 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001733 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001734 break;
1735 case tok::kw_void:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001736 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001737 break;
1738 case tok::kw_char:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001739 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001740 break;
1741 case tok::kw_int:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001742 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001743 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00001744 case tok::kw___int128:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001745 DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, DiagID, Policy);
Richard Smithf016bbc2012-04-04 06:24:32 +00001746 break;
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00001747 case tok::kw_half:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001748 DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID, Policy);
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00001749 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001750 case tok::kw_float:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001751 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001752 break;
1753 case tok::kw_double:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001754 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001755 break;
1756 case tok::kw_wchar_t:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001757 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001758 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001759 case tok::kw_char16_t:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001760 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID, Policy);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001761 break;
1762 case tok::kw_char32_t:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001763 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID, Policy);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001764 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001765 case tok::kw_bool:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001766 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001767 break;
David Blaikie25896afb2012-01-24 05:47:35 +00001768 case tok::annot_decltype:
1769 case tok::kw_decltype:
1770 DS.SetRangeEnd(ParseDecltypeSpecifier(DS));
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001771 return DS.Finish(Diags, PP, Policy);
Mike Stump11289f42009-09-09 15:08:12 +00001772
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001773 // GNU typeof support.
1774 case tok::kw_typeof:
1775 ParseTypeofSpecifier(DS);
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001776 DS.Finish(Diags, PP, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001777 return;
1778 }
Chris Lattnera8a3f732009-01-06 05:06:21 +00001779 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001780 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1781 else
1782 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001783 ConsumeToken();
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001784 DS.Finish(Diags, PP, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001785}
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001786
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001787/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1788/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1789/// e.g., "const short int". Note that the DeclSpec is *not* finished
1790/// by parsing the type-specifier-seq, because these sequences are
1791/// typically followed by some form of declarator. Returns true and
1792/// emits diagnostics if this is not a type-specifier-seq, false
1793/// otherwise.
1794///
1795/// type-specifier-seq: [C++ 8.1]
1796/// type-specifier type-specifier-seq[opt]
1797///
1798bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
Richard Smithc5b05522012-03-12 07:56:15 +00001799 ParseSpecifierQualifierList(DS, AS_none, DSC_type_specifier);
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001800 DS.Finish(Diags, PP, Actions.getASTContext().getPrintingPolicy());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001801 return false;
1802}
1803
Douglas Gregor7861a802009-11-03 01:35:08 +00001804/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1805/// some form.
1806///
1807/// This routine is invoked when a '<' is encountered after an identifier or
1808/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1809/// whether the unqualified-id is actually a template-id. This routine will
1810/// then parse the template arguments and form the appropriate template-id to
1811/// return to the caller.
1812///
1813/// \param SS the nested-name-specifier that precedes this template-id, if
1814/// we're actually parsing a qualified-id.
1815///
1816/// \param Name for constructor and destructor names, this is the actual
1817/// identifier that may be a template-name.
1818///
1819/// \param NameLoc the location of the class-name in a constructor or
1820/// destructor.
1821///
1822/// \param EnteringContext whether we're entering the scope of the
1823/// nested-name-specifier.
1824///
Douglas Gregor127ea592009-11-03 21:24:04 +00001825/// \param ObjectType if this unqualified-id occurs within a member access
1826/// expression, the type of the base object whose member is being accessed.
1827///
Douglas Gregor7861a802009-11-03 01:35:08 +00001828/// \param Id as input, describes the template-name or operator-function-id
1829/// that precedes the '<'. If template arguments were parsed successfully,
1830/// will be updated with the template-id.
1831///
Douglas Gregore610ada2010-02-24 18:44:31 +00001832/// \param AssumeTemplateId When true, this routine will assume that the name
1833/// refers to a template without performing name lookup to verify.
1834///
Douglas Gregor7861a802009-11-03 01:35:08 +00001835/// \returns true if a parse error occurred, false otherwise.
1836bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001837 SourceLocation TemplateKWLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00001838 IdentifierInfo *Name,
1839 SourceLocation NameLoc,
1840 bool EnteringContext,
John McCallba7bf592010-08-24 05:47:05 +00001841 ParsedType ObjectType,
Douglas Gregore610ada2010-02-24 18:44:31 +00001842 UnqualifiedId &Id,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001843 bool AssumeTemplateId) {
Douglas Gregorb22ee882010-05-05 05:58:24 +00001844 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1845 "Expected '<' to finish parsing a template-id");
Douglas Gregor7861a802009-11-03 01:35:08 +00001846
1847 TemplateTy Template;
1848 TemplateNameKind TNK = TNK_Non_template;
1849 switch (Id.getKind()) {
1850 case UnqualifiedId::IK_Identifier:
Douglas Gregor3cf81312009-11-03 23:16:33 +00001851 case UnqualifiedId::IK_OperatorFunctionId:
Alexis Hunted0530f2009-11-28 08:58:14 +00001852 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregore610ada2010-02-24 18:44:31 +00001853 if (AssumeTemplateId) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001854 TNK = Actions.ActOnDependentTemplateName(getCurScope(), SS, TemplateKWLoc,
Douglas Gregorbb119652010-06-16 23:00:59 +00001855 Id, ObjectType, EnteringContext,
1856 Template);
1857 if (TNK == TNK_Non_template)
1858 return true;
Douglas Gregor786123d2010-05-21 23:18:07 +00001859 } else {
1860 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001861 TNK = Actions.isTemplateName(getCurScope(), SS,
1862 TemplateKWLoc.isValid(), Id,
1863 ObjectType, EnteringContext, Template,
Douglas Gregor786123d2010-05-21 23:18:07 +00001864 MemberOfUnknownSpecialization);
1865
1866 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1867 ObjectType && IsTemplateArgumentList()) {
1868 // We have something like t->getAs<T>(), where getAs is a
1869 // member of an unknown specialization. However, this will only
1870 // parse correctly as a template, so suggest the keyword 'template'
1871 // before 'getAs' and treat this as a dependent template name.
1872 std::string Name;
1873 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1874 Name = Id.Identifier->getName();
1875 else {
1876 Name = "operator ";
1877 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1878 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1879 else
1880 Name += Id.Identifier->getName();
1881 }
1882 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1883 << Name
1884 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Abramo Bagnara7945c982012-01-27 09:46:47 +00001885 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1886 SS, TemplateKWLoc, Id,
1887 ObjectType, EnteringContext,
1888 Template);
Douglas Gregorbb119652010-06-16 23:00:59 +00001889 if (TNK == TNK_Non_template)
Douglas Gregor786123d2010-05-21 23:18:07 +00001890 return true;
1891 }
1892 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001893 break;
1894
Douglas Gregor3cf81312009-11-03 23:16:33 +00001895 case UnqualifiedId::IK_ConstructorName: {
1896 UnqualifiedId TemplateName;
Douglas Gregor786123d2010-05-21 23:18:07 +00001897 bool MemberOfUnknownSpecialization;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001898 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001899 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1900 TemplateName, ObjectType,
Douglas Gregor786123d2010-05-21 23:18:07 +00001901 EnteringContext, Template,
1902 MemberOfUnknownSpecialization);
Douglas Gregor7861a802009-11-03 01:35:08 +00001903 break;
1904 }
1905
Douglas Gregor3cf81312009-11-03 23:16:33 +00001906 case UnqualifiedId::IK_DestructorName: {
1907 UnqualifiedId TemplateName;
Douglas Gregor786123d2010-05-21 23:18:07 +00001908 bool MemberOfUnknownSpecialization;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001909 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001910 if (ObjectType) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001911 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1912 SS, TemplateKWLoc, TemplateName,
1913 ObjectType, EnteringContext,
1914 Template);
Douglas Gregorbb119652010-06-16 23:00:59 +00001915 if (TNK == TNK_Non_template)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001916 return true;
1917 } else {
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001918 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1919 TemplateName, ObjectType,
Douglas Gregor786123d2010-05-21 23:18:07 +00001920 EnteringContext, Template,
1921 MemberOfUnknownSpecialization);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001922
John McCallba7bf592010-08-24 05:47:05 +00001923 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00001924 Diag(NameLoc, diag::err_destructor_template_id)
1925 << Name << SS.getRange();
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001926 return true;
1927 }
1928 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001929 break;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001930 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001931
1932 default:
1933 return false;
1934 }
1935
1936 if (TNK == TNK_Non_template)
1937 return false;
1938
1939 // Parse the enclosed template argument list.
1940 SourceLocation LAngleLoc, RAngleLoc;
1941 TemplateArgList TemplateArgs;
Douglas Gregorb22ee882010-05-05 05:58:24 +00001942 if (Tok.is(tok::less) &&
1943 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregore7c20652011-03-02 00:47:37 +00001944 SS, true, LAngleLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00001945 TemplateArgs,
Douglas Gregor7861a802009-11-03 01:35:08 +00001946 RAngleLoc))
1947 return true;
1948
1949 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Alexis Hunted0530f2009-11-28 08:58:14 +00001950 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1951 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor7861a802009-11-03 01:35:08 +00001952 // Form a parsed representation of the template-id to be stored in the
1953 // UnqualifiedId.
1954 TemplateIdAnnotation *TemplateId
Benjamin Kramer1e6b6062012-04-14 12:14:03 +00001955 = TemplateIdAnnotation::Allocate(TemplateArgs.size(), TemplateIds);
Douglas Gregor7861a802009-11-03 01:35:08 +00001956
Richard Smith72bfbd82013-12-04 00:28:23 +00001957 // FIXME: Store name for literal operator too.
Douglas Gregor7861a802009-11-03 01:35:08 +00001958 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1959 TemplateId->Name = Id.Identifier;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001960 TemplateId->Operator = OO_None;
Douglas Gregor7861a802009-11-03 01:35:08 +00001961 TemplateId->TemplateNameLoc = Id.StartLocation;
1962 } else {
Douglas Gregor3cf81312009-11-03 23:16:33 +00001963 TemplateId->Name = 0;
1964 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1965 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor7861a802009-11-03 01:35:08 +00001966 }
1967
Douglas Gregore7c20652011-03-02 00:47:37 +00001968 TemplateId->SS = SS;
Benjamin Kramer807c2db2012-02-19 23:37:39 +00001969 TemplateId->TemplateKWLoc = TemplateKWLoc;
John McCall3e56fd42010-08-23 07:28:44 +00001970 TemplateId->Template = Template;
Douglas Gregor7861a802009-11-03 01:35:08 +00001971 TemplateId->Kind = TNK;
1972 TemplateId->LAngleLoc = LAngleLoc;
1973 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001974 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor7861a802009-11-03 01:35:08 +00001975 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001976 Arg != ArgEnd; ++Arg)
Douglas Gregor7861a802009-11-03 01:35:08 +00001977 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor7861a802009-11-03 01:35:08 +00001978
1979 Id.setTemplateId(TemplateId);
1980 return false;
1981 }
1982
1983 // Bundle the template arguments together.
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001984 ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs);
Abramo Bagnara4244b432012-01-27 08:46:19 +00001985
Douglas Gregor7861a802009-11-03 01:35:08 +00001986 // Constructor and destructor names.
John McCallfaf5fb42010-08-26 23:41:50 +00001987 TypeResult Type
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001988 = Actions.ActOnTemplateIdType(SS, TemplateKWLoc,
1989 Template, NameLoc,
Abramo Bagnara4244b432012-01-27 08:46:19 +00001990 LAngleLoc, TemplateArgsPtr, RAngleLoc,
1991 /*IsCtorOrDtorName=*/true);
Douglas Gregor7861a802009-11-03 01:35:08 +00001992 if (Type.isInvalid())
1993 return true;
1994
1995 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1996 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1997 else
1998 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1999
2000 return false;
2001}
2002
Douglas Gregor71395fa2009-11-04 00:56:37 +00002003/// \brief Parse an operator-function-id or conversion-function-id as part
2004/// of a C++ unqualified-id.
2005///
2006/// This routine is responsible only for parsing the operator-function-id or
2007/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor7861a802009-11-03 01:35:08 +00002008///
2009/// \code
Douglas Gregor7861a802009-11-03 01:35:08 +00002010/// operator-function-id: [C++ 13.5]
2011/// 'operator' operator
2012///
Douglas Gregor71395fa2009-11-04 00:56:37 +00002013/// operator: one of
Douglas Gregor7861a802009-11-03 01:35:08 +00002014/// new delete new[] delete[]
2015/// + - * / % ^ & | ~
2016/// ! = < > += -= *= /= %=
2017/// ^= &= |= << >> >>= <<= == !=
2018/// <= >= && || ++ -- , ->* ->
2019/// () []
2020///
2021/// conversion-function-id: [C++ 12.3.2]
2022/// operator conversion-type-id
2023///
2024/// conversion-type-id:
2025/// type-specifier-seq conversion-declarator[opt]
2026///
2027/// conversion-declarator:
2028/// ptr-operator conversion-declarator[opt]
2029/// \endcode
2030///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00002031/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregor7861a802009-11-03 01:35:08 +00002032/// non-empty, then we are parsing the unqualified-id of a qualified-id.
2033///
2034/// \param EnteringContext whether we are entering the scope of the
2035/// nested-name-specifier.
2036///
Douglas Gregor71395fa2009-11-04 00:56:37 +00002037/// \param ObjectType if this unqualified-id occurs within a member access
2038/// expression, the type of the base object whose member is being accessed.
2039///
2040/// \param Result on a successful parse, contains the parsed unqualified-id.
2041///
2042/// \returns true if parsing fails, false otherwise.
2043bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallba7bf592010-08-24 05:47:05 +00002044 ParsedType ObjectType,
Douglas Gregor71395fa2009-11-04 00:56:37 +00002045 UnqualifiedId &Result) {
2046 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
2047
2048 // Consume the 'operator' keyword.
2049 SourceLocation KeywordLoc = ConsumeToken();
2050
2051 // Determine what kind of operator name we have.
2052 unsigned SymbolIdx = 0;
2053 SourceLocation SymbolLocations[3];
2054 OverloadedOperatorKind Op = OO_None;
2055 switch (Tok.getKind()) {
2056 case tok::kw_new:
2057 case tok::kw_delete: {
2058 bool isNew = Tok.getKind() == tok::kw_new;
2059 // Consume the 'new' or 'delete'.
2060 SymbolLocations[SymbolIdx++] = ConsumeToken();
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002061 // Check for array new/delete.
2062 if (Tok.is(tok::l_square) &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002063 (!getLangOpts().CPlusPlus11 || NextToken().isNot(tok::l_square))) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002064 // Consume the '[' and ']'.
2065 BalancedDelimiterTracker T(*this, tok::l_square);
2066 T.consumeOpen();
2067 T.consumeClose();
2068 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00002069 return true;
2070
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002071 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
2072 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00002073 Op = isNew? OO_Array_New : OO_Array_Delete;
2074 } else {
2075 Op = isNew? OO_New : OO_Delete;
2076 }
2077 break;
2078 }
2079
2080#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
2081 case tok::Token: \
2082 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
2083 Op = OO_##Name; \
2084 break;
2085#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
2086#include "clang/Basic/OperatorKinds.def"
2087
2088 case tok::l_paren: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002089 // Consume the '(' and ')'.
2090 BalancedDelimiterTracker T(*this, tok::l_paren);
2091 T.consumeOpen();
2092 T.consumeClose();
2093 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00002094 return true;
2095
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002096 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
2097 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00002098 Op = OO_Call;
2099 break;
2100 }
2101
2102 case tok::l_square: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002103 // Consume the '[' and ']'.
2104 BalancedDelimiterTracker T(*this, tok::l_square);
2105 T.consumeOpen();
2106 T.consumeClose();
2107 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00002108 return true;
2109
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002110 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
2111 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00002112 Op = OO_Subscript;
2113 break;
2114 }
2115
2116 case tok::code_completion: {
2117 // Code completion for the operator name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002118 Actions.CodeCompleteOperatorName(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002119 cutOffParsing();
Douglas Gregor71395fa2009-11-04 00:56:37 +00002120 // Don't try to parse any further.
2121 return true;
2122 }
2123
2124 default:
2125 break;
2126 }
2127
2128 if (Op != OO_None) {
2129 // We have parsed an operator-function-id.
2130 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
2131 return false;
2132 }
Alexis Hunt34458502009-11-28 04:44:28 +00002133
2134 // Parse a literal-operator-id.
2135 //
Richard Smith6f212062012-10-20 08:41:10 +00002136 // literal-operator-id: C++11 [over.literal]
2137 // operator string-literal identifier
2138 // operator user-defined-string-literal
Alexis Hunt34458502009-11-28 04:44:28 +00002139
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002140 if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002141 Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator);
Alexis Hunt34458502009-11-28 04:44:28 +00002142
Richard Smith7d182a72012-03-08 23:06:02 +00002143 SourceLocation DiagLoc;
2144 unsigned DiagId = 0;
2145
2146 // We're past translation phase 6, so perform string literal concatenation
2147 // before checking for "".
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002148 SmallVector<Token, 4> Toks;
2149 SmallVector<SourceLocation, 4> TokLocs;
Richard Smith7d182a72012-03-08 23:06:02 +00002150 while (isTokenStringLiteral()) {
2151 if (!Tok.is(tok::string_literal) && !DiagId) {
Richard Smith6f212062012-10-20 08:41:10 +00002152 // C++11 [over.literal]p1:
2153 // The string-literal or user-defined-string-literal in a
2154 // literal-operator-id shall have no encoding-prefix [...].
Richard Smith7d182a72012-03-08 23:06:02 +00002155 DiagLoc = Tok.getLocation();
2156 DiagId = diag::err_literal_operator_string_prefix;
2157 }
2158 Toks.push_back(Tok);
2159 TokLocs.push_back(ConsumeStringToken());
2160 }
2161
2162 StringLiteralParser Literal(Toks.data(), Toks.size(), PP);
2163 if (Literal.hadError)
2164 return true;
2165
2166 // Grab the literal operator's suffix, which will be either the next token
2167 // or a ud-suffix from the string literal.
2168 IdentifierInfo *II = 0;
2169 SourceLocation SuffixLoc;
2170 if (!Literal.getUDSuffix().empty()) {
2171 II = &PP.getIdentifierTable().get(Literal.getUDSuffix());
2172 SuffixLoc =
2173 Lexer::AdvanceToTokenCharacter(TokLocs[Literal.getUDSuffixToken()],
2174 Literal.getUDSuffixOffset(),
David Blaikiebbafb8a2012-03-11 07:00:24 +00002175 PP.getSourceManager(), getLangOpts());
Richard Smith7d182a72012-03-08 23:06:02 +00002176 } else if (Tok.is(tok::identifier)) {
2177 II = Tok.getIdentifierInfo();
2178 SuffixLoc = ConsumeToken();
2179 TokLocs.push_back(SuffixLoc);
2180 } else {
Alp Tokerec543272013-12-24 09:48:30 +00002181 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
Alexis Hunt34458502009-11-28 04:44:28 +00002182 return true;
2183 }
2184
Richard Smith7d182a72012-03-08 23:06:02 +00002185 // The string literal must be empty.
2186 if (!Literal.GetString().empty() || Literal.Pascal) {
Richard Smith6f212062012-10-20 08:41:10 +00002187 // C++11 [over.literal]p1:
2188 // The string-literal or user-defined-string-literal in a
2189 // literal-operator-id shall [...] contain no characters
2190 // other than the implicit terminating '\0'.
Richard Smith7d182a72012-03-08 23:06:02 +00002191 DiagLoc = TokLocs.front();
2192 DiagId = diag::err_literal_operator_string_not_empty;
2193 }
2194
2195 if (DiagId) {
2196 // This isn't a valid literal-operator-id, but we think we know
2197 // what the user meant. Tell them what they should have written.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002198 SmallString<32> Str;
Richard Smith7d182a72012-03-08 23:06:02 +00002199 Str += "\"\" ";
2200 Str += II->getName();
2201 Diag(DiagLoc, DiagId) << FixItHint::CreateReplacement(
2202 SourceRange(TokLocs.front(), TokLocs.back()), Str);
2203 }
2204
2205 Result.setLiteralOperatorId(II, KeywordLoc, SuffixLoc);
Richard Smithd091dc12013-12-05 00:58:33 +00002206
2207 return Actions.checkLiteralOperatorId(SS, Result);
Alexis Hunt34458502009-11-28 04:44:28 +00002208 }
Richard Smithd091dc12013-12-05 00:58:33 +00002209
Douglas Gregor71395fa2009-11-04 00:56:37 +00002210 // Parse a conversion-function-id.
2211 //
2212 // conversion-function-id: [C++ 12.3.2]
2213 // operator conversion-type-id
2214 //
2215 // conversion-type-id:
2216 // type-specifier-seq conversion-declarator[opt]
2217 //
2218 // conversion-declarator:
2219 // ptr-operator conversion-declarator[opt]
2220
2221 // Parse the type-specifier-seq.
John McCall084e83d2011-03-24 11:26:52 +00002222 DeclSpec DS(AttrFactory);
Douglas Gregora25d65d2009-11-20 22:03:38 +00002223 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregor71395fa2009-11-04 00:56:37 +00002224 return true;
2225
2226 // Parse the conversion-declarator, which is merely a sequence of
2227 // ptr-operators.
Richard Smith01518fa2013-05-04 01:26:46 +00002228 Declarator D(DS, Declarator::ConversionIdContext);
Douglas Gregor71395fa2009-11-04 00:56:37 +00002229 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
2230
2231 // Finish up the type.
John McCallfaf5fb42010-08-26 23:41:50 +00002232 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregor71395fa2009-11-04 00:56:37 +00002233 if (Ty.isInvalid())
2234 return true;
2235
2236 // Note that this is a conversion-function-id.
2237 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
2238 D.getSourceRange().getEnd());
2239 return false;
2240}
2241
2242/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
2243/// name of an entity.
2244///
2245/// \code
2246/// unqualified-id: [C++ expr.prim.general]
2247/// identifier
2248/// operator-function-id
2249/// conversion-function-id
2250/// [C++0x] literal-operator-id [TODO]
2251/// ~ class-name
2252/// template-id
2253///
2254/// \endcode
2255///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00002256/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregor71395fa2009-11-04 00:56:37 +00002257/// non-empty, then we are parsing the unqualified-id of a qualified-id.
2258///
2259/// \param EnteringContext whether we are entering the scope of the
2260/// nested-name-specifier.
2261///
Douglas Gregor7861a802009-11-03 01:35:08 +00002262/// \param AllowDestructorName whether we allow parsing of a destructor name.
2263///
2264/// \param AllowConstructorName whether we allow parsing a constructor name.
2265///
Douglas Gregor127ea592009-11-03 21:24:04 +00002266/// \param ObjectType if this unqualified-id occurs within a member access
2267/// expression, the type of the base object whose member is being accessed.
2268///
Douglas Gregor7861a802009-11-03 01:35:08 +00002269/// \param Result on a successful parse, contains the parsed unqualified-id.
2270///
2271/// \returns true if parsing fails, false otherwise.
2272bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
2273 bool AllowDestructorName,
2274 bool AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00002275 ParsedType ObjectType,
Abramo Bagnara7945c982012-01-27 09:46:47 +00002276 SourceLocation& TemplateKWLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00002277 UnqualifiedId &Result) {
Douglas Gregorb22ee882010-05-05 05:58:24 +00002278
2279 // Handle 'A::template B'. This is for template-ids which have not
2280 // already been annotated by ParseOptionalCXXScopeSpecifier().
2281 bool TemplateSpecified = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002282 if (getLangOpts().CPlusPlus && Tok.is(tok::kw_template) &&
Douglas Gregorb22ee882010-05-05 05:58:24 +00002283 (ObjectType || SS.isSet())) {
2284 TemplateSpecified = true;
2285 TemplateKWLoc = ConsumeToken();
2286 }
2287
Douglas Gregor7861a802009-11-03 01:35:08 +00002288 // unqualified-id:
2289 // identifier
2290 // template-id (when it hasn't already been annotated)
2291 if (Tok.is(tok::identifier)) {
2292 // Consume the identifier.
2293 IdentifierInfo *Id = Tok.getIdentifierInfo();
2294 SourceLocation IdLoc = ConsumeToken();
2295
David Blaikiebbafb8a2012-03-11 07:00:24 +00002296 if (!getLangOpts().CPlusPlus) {
Douglas Gregor411e5ac2010-01-11 23:29:10 +00002297 // If we're not in C++, only identifiers matter. Record the
2298 // identifier and return.
2299 Result.setIdentifier(Id, IdLoc);
2300 return false;
2301 }
2302
Douglas Gregor7861a802009-11-03 01:35:08 +00002303 if (AllowConstructorName &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002304 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor7861a802009-11-03 01:35:08 +00002305 // We have parsed a constructor name.
Abramo Bagnara4244b432012-01-27 08:46:19 +00002306 ParsedType Ty = Actions.getTypeName(*Id, IdLoc, getCurScope(),
2307 &SS, false, false,
2308 ParsedType(),
2309 /*IsCtorOrDtorName=*/true,
2310 /*NonTrivialTypeSourceInfo=*/true);
2311 Result.setConstructorName(Ty, IdLoc, IdLoc);
Douglas Gregor7861a802009-11-03 01:35:08 +00002312 } else {
2313 // We have parsed an identifier.
2314 Result.setIdentifier(Id, IdLoc);
2315 }
2316
2317 // If the next token is a '<', we may have a template.
Douglas Gregorb22ee882010-05-05 05:58:24 +00002318 if (TemplateSpecified || Tok.is(tok::less))
Abramo Bagnara7945c982012-01-27 09:46:47 +00002319 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc, Id, IdLoc,
2320 EnteringContext, ObjectType,
2321 Result, TemplateSpecified);
Douglas Gregor7861a802009-11-03 01:35:08 +00002322
2323 return false;
2324 }
2325
2326 // unqualified-id:
2327 // template-id (already parsed and annotated)
2328 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002329 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002330
2331 // If the template-name names the current class, then this is a constructor
2332 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002333 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002334 if (SS.isSet()) {
2335 // C++ [class.qual]p2 specifies that a qualified template-name
2336 // is taken as the constructor name where a constructor can be
2337 // declared. Thus, the template arguments are extraneous, so
2338 // complain about them and remove them entirely.
2339 Diag(TemplateId->TemplateNameLoc,
2340 diag::err_out_of_line_constructor_template_id)
2341 << TemplateId->Name
Douglas Gregora771f462010-03-31 17:46:05 +00002342 << FixItHint::CreateRemoval(
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002343 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
Abramo Bagnara4244b432012-01-27 08:46:19 +00002344 ParsedType Ty = Actions.getTypeName(*TemplateId->Name,
2345 TemplateId->TemplateNameLoc,
2346 getCurScope(),
2347 &SS, false, false,
2348 ParsedType(),
2349 /*IsCtorOrDtorName=*/true,
2350 /*NontrivialTypeSourceInfo=*/true);
2351 Result.setConstructorName(Ty, TemplateId->TemplateNameLoc,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002352 TemplateId->RAngleLoc);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002353 ConsumeToken();
2354 return false;
2355 }
2356
2357 Result.setConstructorTemplateId(TemplateId);
2358 ConsumeToken();
2359 return false;
2360 }
2361
Douglas Gregor7861a802009-11-03 01:35:08 +00002362 // We have already parsed a template-id; consume the annotation token as
2363 // our unqualified-id.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002364 Result.setTemplateId(TemplateId);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002365 TemplateKWLoc = TemplateId->TemplateKWLoc;
Douglas Gregor7861a802009-11-03 01:35:08 +00002366 ConsumeToken();
2367 return false;
2368 }
2369
2370 // unqualified-id:
2371 // operator-function-id
2372 // conversion-function-id
2373 if (Tok.is(tok::kw_operator)) {
Douglas Gregor71395fa2009-11-04 00:56:37 +00002374 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor7861a802009-11-03 01:35:08 +00002375 return true;
2376
Alexis Hunted0530f2009-11-28 08:58:14 +00002377 // If we have an operator-function-id or a literal-operator-id and the next
2378 // token is a '<', we may have a
Douglas Gregor71395fa2009-11-04 00:56:37 +00002379 //
2380 // template-id:
2381 // operator-function-id < template-argument-list[opt] >
Alexis Hunted0530f2009-11-28 08:58:14 +00002382 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
2383 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregorb22ee882010-05-05 05:58:24 +00002384 (TemplateSpecified || Tok.is(tok::less)))
Abramo Bagnara7945c982012-01-27 09:46:47 +00002385 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2386 0, SourceLocation(),
2387 EnteringContext, ObjectType,
2388 Result, TemplateSpecified);
Douglas Gregor7861a802009-11-03 01:35:08 +00002389
Douglas Gregor7861a802009-11-03 01:35:08 +00002390 return false;
2391 }
2392
David Blaikiebbafb8a2012-03-11 07:00:24 +00002393 if (getLangOpts().CPlusPlus &&
Douglas Gregor411e5ac2010-01-11 23:29:10 +00002394 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor7861a802009-11-03 01:35:08 +00002395 // C++ [expr.unary.op]p10:
2396 // There is an ambiguity in the unary-expression ~X(), where X is a
2397 // class-name. The ambiguity is resolved in favor of treating ~ as a
2398 // unary complement rather than treating ~X as referring to a destructor.
2399
2400 // Parse the '~'.
2401 SourceLocation TildeLoc = ConsumeToken();
David Blaikieecd8a942011-12-08 16:13:53 +00002402
2403 if (SS.isEmpty() && Tok.is(tok::kw_decltype)) {
2404 DeclSpec DS(AttrFactory);
2405 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
2406 if (ParsedType Type = Actions.getDestructorType(DS, ObjectType)) {
2407 Result.setDestructorName(TildeLoc, Type, EndLoc);
2408 return false;
2409 }
2410 return true;
2411 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002412
2413 // Parse the class-name.
2414 if (Tok.isNot(tok::identifier)) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00002415 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor7861a802009-11-03 01:35:08 +00002416 return true;
2417 }
2418
2419 // Parse the class-name (or template-name in a simple-template-id).
2420 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
2421 SourceLocation ClassNameLoc = ConsumeToken();
2422
Douglas Gregorb22ee882010-05-05 05:58:24 +00002423 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallba7bf592010-08-24 05:47:05 +00002424 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002425 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2426 ClassName, ClassNameLoc,
2427 EnteringContext, ObjectType,
2428 Result, TemplateSpecified);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002429 }
2430
Douglas Gregor7861a802009-11-03 01:35:08 +00002431 // Note that this is a destructor name.
John McCallba7bf592010-08-24 05:47:05 +00002432 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
2433 ClassNameLoc, getCurScope(),
2434 SS, ObjectType,
2435 EnteringContext);
Douglas Gregorfe17d252010-02-16 19:09:40 +00002436 if (!Ty)
Douglas Gregor7861a802009-11-03 01:35:08 +00002437 return true;
Douglas Gregorfe17d252010-02-16 19:09:40 +00002438
Douglas Gregor7861a802009-11-03 01:35:08 +00002439 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor7861a802009-11-03 01:35:08 +00002440 return false;
2441 }
2442
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002443 Diag(Tok, diag::err_expected_unqualified_id)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002444 << getLangOpts().CPlusPlus;
Douglas Gregor7861a802009-11-03 01:35:08 +00002445 return true;
2446}
2447
Sebastian Redlbd150f42008-11-21 19:14:01 +00002448/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
2449/// memory in a typesafe manner and call constructors.
Mike Stump11289f42009-09-09 15:08:12 +00002450///
Chris Lattner109faf22009-01-04 21:25:24 +00002451/// This method is called to parse the new expression after the optional :: has
2452/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
2453/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002454///
2455/// new-expression:
2456/// '::'[opt] 'new' new-placement[opt] new-type-id
2457/// new-initializer[opt]
2458/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2459/// new-initializer[opt]
2460///
2461/// new-placement:
2462/// '(' expression-list ')'
2463///
Sebastian Redl351bb782008-12-02 14:43:59 +00002464/// new-type-id:
2465/// type-specifier-seq new-declarator[opt]
Douglas Gregora3a020a2011-04-15 19:40:02 +00002466/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redl351bb782008-12-02 14:43:59 +00002467///
2468/// new-declarator:
2469/// ptr-operator new-declarator[opt]
2470/// direct-new-declarator
2471///
Sebastian Redlbd150f42008-11-21 19:14:01 +00002472/// new-initializer:
2473/// '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00002474/// [C++0x] braced-init-list
Sebastian Redlbd150f42008-11-21 19:14:01 +00002475///
John McCalldadc5752010-08-24 06:29:42 +00002476ExprResult
Chris Lattner109faf22009-01-04 21:25:24 +00002477Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
2478 assert(Tok.is(tok::kw_new) && "expected 'new' token");
2479 ConsumeToken(); // Consume 'new'
Sebastian Redlbd150f42008-11-21 19:14:01 +00002480
2481 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
2482 // second form of new-expression. It can't be a new-type-id.
2483
Benjamin Kramerf0623432012-08-23 22:51:59 +00002484 ExprVector PlacementArgs;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002485 SourceLocation PlacementLParen, PlacementRParen;
2486
Douglas Gregorf2753b32010-07-13 15:54:32 +00002487 SourceRange TypeIdParens;
John McCall084e83d2011-03-24 11:26:52 +00002488 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis3ff13572011-06-28 03:01:23 +00002489 Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002490 if (Tok.is(tok::l_paren)) {
2491 // If it turns out to be a placement, we change the type location.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002492 BalancedDelimiterTracker T(*this, tok::l_paren);
2493 T.consumeOpen();
2494 PlacementLParen = T.getOpenLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002495 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002496 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redld65cea82008-12-11 22:51:44 +00002497 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002498 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002499
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002500 T.consumeClose();
2501 PlacementRParen = T.getCloseLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002502 if (PlacementRParen.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002503 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redld65cea82008-12-11 22:51:44 +00002504 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002505 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002506
Sebastian Redl351bb782008-12-02 14:43:59 +00002507 if (PlacementArgs.empty()) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002508 // Reset the placement locations. There was no placement.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002509 TypeIdParens = T.getRange();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002510 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002511 } else {
2512 // We still need the type.
2513 if (Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002514 BalancedDelimiterTracker T(*this, tok::l_paren);
2515 T.consumeOpen();
Douglas Gregora3a020a2011-04-15 19:40:02 +00002516 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002517 ParseSpecifierQualifierList(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002518 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002519 ParseDeclarator(DeclaratorInfo);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002520 T.consumeClose();
2521 TypeIdParens = T.getRange();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002522 } else {
Douglas Gregora3a020a2011-04-15 19:40:02 +00002523 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002524 if (ParseCXXTypeSpecifierSeq(DS))
2525 DeclaratorInfo.setInvalidType(true);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002526 else {
2527 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002528 ParseDeclaratorInternal(DeclaratorInfo,
2529 &Parser::ParseDirectNewDeclarator);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002530 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002531 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002532 }
2533 } else {
Sebastian Redl351bb782008-12-02 14:43:59 +00002534 // A new-type-id is a simplified type-id, where essentially the
2535 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregora3a020a2011-04-15 19:40:02 +00002536 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002537 if (ParseCXXTypeSpecifierSeq(DS))
2538 DeclaratorInfo.setInvalidType(true);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002539 else {
2540 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002541 ParseDeclaratorInternal(DeclaratorInfo,
2542 &Parser::ParseDirectNewDeclarator);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002543 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002544 }
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00002545 if (DeclaratorInfo.isInvalidType()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002546 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redld65cea82008-12-11 22:51:44 +00002547 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002548 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002549
Sebastian Redl6047f072012-02-16 12:22:20 +00002550 ExprResult Initializer;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002551
2552 if (Tok.is(tok::l_paren)) {
Sebastian Redl6047f072012-02-16 12:22:20 +00002553 SourceLocation ConstructorLParen, ConstructorRParen;
Benjamin Kramerf0623432012-08-23 22:51:59 +00002554 ExprVector ConstructorArgs;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002555 BalancedDelimiterTracker T(*this, tok::l_paren);
2556 T.consumeOpen();
2557 ConstructorLParen = T.getOpenLocation();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002558 if (Tok.isNot(tok::r_paren)) {
2559 CommaLocsTy CommaLocs;
Sebastian Redl351bb782008-12-02 14:43:59 +00002560 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002561 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redld65cea82008-12-11 22:51:44 +00002562 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002563 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002564 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002565 T.consumeClose();
2566 ConstructorRParen = T.getCloseLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002567 if (ConstructorRParen.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002568 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redld65cea82008-12-11 22:51:44 +00002569 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002570 }
Sebastian Redl6047f072012-02-16 12:22:20 +00002571 Initializer = Actions.ActOnParenListExpr(ConstructorLParen,
2572 ConstructorRParen,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002573 ConstructorArgs);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002574 } else if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus11) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002575 Diag(Tok.getLocation(),
2576 diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl6047f072012-02-16 12:22:20 +00002577 Initializer = ParseBraceInitializer();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002578 }
Sebastian Redl6047f072012-02-16 12:22:20 +00002579 if (Initializer.isInvalid())
2580 return Initializer;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002581
Sebastian Redl6d4256c2009-03-15 17:47:39 +00002582 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002583 PlacementArgs, PlacementRParen,
Sebastian Redl6047f072012-02-16 12:22:20 +00002584 TypeIdParens, DeclaratorInfo, Initializer.take());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002585}
2586
Sebastian Redlbd150f42008-11-21 19:14:01 +00002587/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
2588/// passed to ParseDeclaratorInternal.
2589///
2590/// direct-new-declarator:
2591/// '[' expression ']'
2592/// direct-new-declarator '[' constant-expression ']'
2593///
Chris Lattner109faf22009-01-04 21:25:24 +00002594void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002595 // Parse the array dimensions.
2596 bool first = true;
2597 while (Tok.is(tok::l_square)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002598 // An array-size expression can't start with a lambda.
2599 if (CheckProhibitedCXX11Attribute())
2600 continue;
2601
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002602 BalancedDelimiterTracker T(*this, tok::l_square);
2603 T.consumeOpen();
2604
John McCalldadc5752010-08-24 06:29:42 +00002605 ExprResult Size(first ? ParseExpression()
Sebastian Redl59b5e512008-12-11 21:36:32 +00002606 : ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002607 if (Size.isInvalid()) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002608 // Recover
Alexey Bataevee6507d2013-11-18 08:17:37 +00002609 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002610 return;
2611 }
2612 first = false;
2613
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002614 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00002615
Bill Wendling44426052012-12-20 19:22:21 +00002616 // Attributes here appertain to the array type. C++11 [expr.new]p5.
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002617 ParsedAttributes Attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002618 MaybeParseCXX11Attributes(Attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002619
John McCall084e83d2011-03-24 11:26:52 +00002620 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall53fa7142010-12-24 02:08:15 +00002621 /*static=*/false, /*star=*/false,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002622 Size.release(),
2623 T.getOpenLocation(),
2624 T.getCloseLocation()),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002625 Attrs, T.getCloseLocation());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002626
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002627 if (T.getCloseLocation().isInvalid())
Sebastian Redlbd150f42008-11-21 19:14:01 +00002628 return;
2629 }
2630}
2631
2632/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
2633/// This ambiguity appears in the syntax of the C++ new operator.
2634///
2635/// new-expression:
2636/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2637/// new-initializer[opt]
2638///
2639/// new-placement:
2640/// '(' expression-list ')'
2641///
John McCall37ad5512010-08-23 06:44:23 +00002642bool Parser::ParseExpressionListOrTypeId(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002643 SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner109faf22009-01-04 21:25:24 +00002644 Declarator &D) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002645 // The '(' was already consumed.
2646 if (isTypeIdInParens()) {
Sebastian Redl351bb782008-12-02 14:43:59 +00002647 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002648 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002649 ParseDeclarator(D);
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00002650 return D.isInvalidType();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002651 }
2652
2653 // It's not a type, it has to be an expression list.
2654 // Discard the comma locations - ActOnCXXNew has enough parameters.
2655 CommaLocsTy CommaLocs;
2656 return ParseExpressionList(PlacementArgs, CommaLocs);
2657}
2658
2659/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
2660/// to free memory allocated by new.
2661///
Chris Lattner109faf22009-01-04 21:25:24 +00002662/// This method is called to parse the 'delete' expression after the optional
2663/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
2664/// and "Start" is its location. Otherwise, "Start" is the location of the
2665/// 'delete' token.
2666///
Sebastian Redlbd150f42008-11-21 19:14:01 +00002667/// delete-expression:
2668/// '::'[opt] 'delete' cast-expression
2669/// '::'[opt] 'delete' '[' ']' cast-expression
John McCalldadc5752010-08-24 06:29:42 +00002670ExprResult
Chris Lattner109faf22009-01-04 21:25:24 +00002671Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
2672 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
2673 ConsumeToken(); // Consume 'delete'
Sebastian Redlbd150f42008-11-21 19:14:01 +00002674
2675 // Array delete?
2676 bool ArrayDelete = false;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002677 if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
Richard Smith10c60722012-08-09 19:01:51 +00002678 // C++11 [expr.delete]p1:
2679 // Whenever the delete keyword is followed by empty square brackets, it
2680 // shall be interpreted as [array delete].
2681 // [Footnote: A lambda expression with a lambda-introducer that consists
2682 // of empty square brackets can follow the delete keyword if
2683 // the lambda expression is enclosed in parentheses.]
2684 // FIXME: Produce a better diagnostic if the '[]' is unambiguously a
2685 // lambda-introducer.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002686 ArrayDelete = true;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002687 BalancedDelimiterTracker T(*this, tok::l_square);
2688
2689 T.consumeOpen();
2690 T.consumeClose();
2691 if (T.getCloseLocation().isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +00002692 return ExprError();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002693 }
2694
John McCalldadc5752010-08-24 06:29:42 +00002695 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002696 if (Operand.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002697 return Operand;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002698
John McCallb268a282010-08-23 23:25:46 +00002699 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002700}
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002701
Douglas Gregor29c42f22012-02-24 07:38:34 +00002702static TypeTrait TypeTraitFromTokKind(tok::TokenKind kind) {
2703 switch (kind) {
2704 default: llvm_unreachable("Not a known type trait");
Alp Toker95e7ff22014-01-01 05:57:51 +00002705#define TYPE_TRAIT_1(Spelling, Name, Key) \
2706case tok::kw_ ## Spelling: return UTT_ ## Name;
Alp Tokercbb90342013-12-13 20:49:58 +00002707#define TYPE_TRAIT_2(Spelling, Name, Key) \
2708case tok::kw_ ## Spelling: return BTT_ ## Name;
2709#include "clang/Basic/TokenKinds.def"
Alp Toker40f9b1c2013-12-12 21:23:03 +00002710#define TYPE_TRAIT_N(Spelling, Name, Key) \
2711 case tok::kw_ ## Spelling: return TT_ ## Name;
2712#include "clang/Basic/TokenKinds.def"
Douglas Gregor29c42f22012-02-24 07:38:34 +00002713 }
2714}
2715
John Wiegley6242b6a2011-04-28 00:16:57 +00002716static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
2717 switch(kind) {
2718 default: llvm_unreachable("Not a known binary type trait");
2719 case tok::kw___array_rank: return ATT_ArrayRank;
2720 case tok::kw___array_extent: return ATT_ArrayExtent;
2721 }
2722}
2723
John Wiegleyf9f65842011-04-25 06:54:41 +00002724static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2725 switch(kind) {
David Blaikie83d382b2011-09-23 05:06:16 +00002726 default: llvm_unreachable("Not a known unary expression trait.");
John Wiegleyf9f65842011-04-25 06:54:41 +00002727 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
2728 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
2729 }
2730}
2731
Alp Toker40f9b1c2013-12-12 21:23:03 +00002732static unsigned TypeTraitArity(tok::TokenKind kind) {
2733 switch (kind) {
2734 default: llvm_unreachable("Not a known type trait");
2735#define TYPE_TRAIT(N,Spelling,K) case tok::kw_##Spelling: return N;
2736#include "clang/Basic/TokenKinds.def"
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002737 }
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002738}
2739
Douglas Gregor29c42f22012-02-24 07:38:34 +00002740/// \brief Parse the built-in type-trait pseudo-functions that allow
2741/// implementation of the TR1/C++11 type traits templates.
2742///
2743/// primary-expression:
Alp Toker40f9b1c2013-12-12 21:23:03 +00002744/// unary-type-trait '(' type-id ')'
2745/// binary-type-trait '(' type-id ',' type-id ')'
Douglas Gregor29c42f22012-02-24 07:38:34 +00002746/// type-trait '(' type-id-seq ')'
2747///
2748/// type-id-seq:
2749/// type-id ...[opt] type-id-seq[opt]
2750///
2751ExprResult Parser::ParseTypeTrait() {
Alp Toker40f9b1c2013-12-12 21:23:03 +00002752 tok::TokenKind Kind = Tok.getKind();
2753 unsigned Arity = TypeTraitArity(Kind);
2754
Douglas Gregor29c42f22012-02-24 07:38:34 +00002755 SourceLocation Loc = ConsumeToken();
2756
2757 BalancedDelimiterTracker Parens(*this, tok::l_paren);
Alp Toker383d2c42014-01-01 03:08:43 +00002758 if (Parens.expectAndConsume())
Douglas Gregor29c42f22012-02-24 07:38:34 +00002759 return ExprError();
2760
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002761 SmallVector<ParsedType, 2> Args;
Douglas Gregor29c42f22012-02-24 07:38:34 +00002762 do {
2763 // Parse the next type.
2764 TypeResult Ty = ParseTypeName();
2765 if (Ty.isInvalid()) {
2766 Parens.skipToEnd();
2767 return ExprError();
2768 }
2769
2770 // Parse the ellipsis, if present.
2771 if (Tok.is(tok::ellipsis)) {
2772 Ty = Actions.ActOnPackExpansion(Ty.get(), ConsumeToken());
2773 if (Ty.isInvalid()) {
2774 Parens.skipToEnd();
2775 return ExprError();
2776 }
2777 }
2778
2779 // Add this type to the list of arguments.
2780 Args.push_back(Ty.get());
Alp Tokera3ebe6e2013-12-17 14:12:37 +00002781 } while (TryConsumeToken(tok::comma));
2782
Douglas Gregor29c42f22012-02-24 07:38:34 +00002783 if (Parens.consumeClose())
2784 return ExprError();
Alp Toker40f9b1c2013-12-12 21:23:03 +00002785
2786 SourceLocation EndLoc = Parens.getCloseLocation();
2787
2788 if (Arity && Args.size() != Arity) {
2789 Diag(EndLoc, diag::err_type_trait_arity)
2790 << Arity << 0 << (Arity > 1) << (int)Args.size() << SourceRange(Loc);
2791 return ExprError();
2792 }
2793
2794 if (!Arity && Args.empty()) {
2795 Diag(EndLoc, diag::err_type_trait_arity)
2796 << 1 << 1 << 1 << (int)Args.size() << SourceRange(Loc);
2797 return ExprError();
2798 }
2799
Alp Toker88f64e62013-12-13 21:19:30 +00002800 return Actions.ActOnTypeTrait(TypeTraitFromTokKind(Kind), Loc, Args, EndLoc);
Douglas Gregor29c42f22012-02-24 07:38:34 +00002801}
2802
John Wiegley6242b6a2011-04-28 00:16:57 +00002803/// ParseArrayTypeTrait - Parse the built-in array type-trait
2804/// pseudo-functions.
2805///
2806/// primary-expression:
2807/// [Embarcadero] '__array_rank' '(' type-id ')'
2808/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
2809///
2810ExprResult Parser::ParseArrayTypeTrait() {
2811 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
2812 SourceLocation Loc = ConsumeToken();
2813
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002814 BalancedDelimiterTracker T(*this, tok::l_paren);
Alp Toker383d2c42014-01-01 03:08:43 +00002815 if (T.expectAndConsume())
John Wiegley6242b6a2011-04-28 00:16:57 +00002816 return ExprError();
2817
2818 TypeResult Ty = ParseTypeName();
2819 if (Ty.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002820 SkipUntil(tok::comma, StopAtSemi);
2821 SkipUntil(tok::r_paren, StopAtSemi);
John Wiegley6242b6a2011-04-28 00:16:57 +00002822 return ExprError();
2823 }
2824
2825 switch (ATT) {
2826 case ATT_ArrayRank: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002827 T.consumeClose();
2828 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL,
2829 T.getCloseLocation());
John Wiegley6242b6a2011-04-28 00:16:57 +00002830 }
2831 case ATT_ArrayExtent: {
Alp Toker383d2c42014-01-01 03:08:43 +00002832 if (ExpectAndConsume(tok::comma)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002833 SkipUntil(tok::r_paren, StopAtSemi);
John Wiegley6242b6a2011-04-28 00:16:57 +00002834 return ExprError();
2835 }
2836
2837 ExprResult DimExpr = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002838 T.consumeClose();
John Wiegley6242b6a2011-04-28 00:16:57 +00002839
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002840 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(),
2841 T.getCloseLocation());
John Wiegley6242b6a2011-04-28 00:16:57 +00002842 }
John Wiegley6242b6a2011-04-28 00:16:57 +00002843 }
David Blaikiee4d798f2012-01-20 21:50:17 +00002844 llvm_unreachable("Invalid ArrayTypeTrait!");
John Wiegley6242b6a2011-04-28 00:16:57 +00002845}
2846
John Wiegleyf9f65842011-04-25 06:54:41 +00002847/// ParseExpressionTrait - Parse built-in expression-trait
2848/// pseudo-functions like __is_lvalue_expr( xxx ).
2849///
2850/// primary-expression:
2851/// [Embarcadero] expression-trait '(' expression ')'
2852///
2853ExprResult Parser::ParseExpressionTrait() {
2854 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2855 SourceLocation Loc = ConsumeToken();
2856
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002857 BalancedDelimiterTracker T(*this, tok::l_paren);
Alp Toker383d2c42014-01-01 03:08:43 +00002858 if (T.expectAndConsume())
John Wiegleyf9f65842011-04-25 06:54:41 +00002859 return ExprError();
2860
2861 ExprResult Expr = ParseExpression();
2862
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002863 T.consumeClose();
John Wiegleyf9f65842011-04-25 06:54:41 +00002864
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002865 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(),
2866 T.getCloseLocation());
John Wiegleyf9f65842011-04-25 06:54:41 +00002867}
2868
2869
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002870/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2871/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2872/// based on the context past the parens.
John McCalldadc5752010-08-24 06:29:42 +00002873ExprResult
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002874Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallba7bf592010-08-24 05:47:05 +00002875 ParsedType &CastTy,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002876 BalancedDelimiterTracker &Tracker) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002877 assert(getLangOpts().CPlusPlus && "Should only be called for C++!");
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002878 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2879 assert(isTypeIdInParens() && "Not a type-id!");
2880
John McCalldadc5752010-08-24 06:29:42 +00002881 ExprResult Result(true);
John McCallba7bf592010-08-24 05:47:05 +00002882 CastTy = ParsedType();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002883
2884 // We need to disambiguate a very ugly part of the C++ syntax:
2885 //
2886 // (T())x; - type-id
2887 // (T())*x; - type-id
2888 // (T())/x; - expression
2889 // (T()); - expression
2890 //
2891 // The bad news is that we cannot use the specialized tentative parser, since
2892 // it can only verify that the thing inside the parens can be parsed as
2893 // type-id, it is not useful for determining the context past the parens.
2894 //
2895 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidis24ad6922009-05-22 15:12:46 +00002896 // making any unnecessary Action calls.
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002897 //
2898 // It uses a scheme similar to parsing inline methods. The parenthesized
2899 // tokens are cached, the context that follows is determined (possibly by
2900 // parsing a cast-expression), and then we re-introduce the cached tokens
2901 // into the token stream and parse them appropriately.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002902
Mike Stump11289f42009-09-09 15:08:12 +00002903 ParenParseOption ParseAs;
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002904 CachedTokens Toks;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002905
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002906 // Store the tokens of the parentheses. We will parse them after we determine
2907 // the context that follows them.
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00002908 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002909 // We didn't find the ')' we expected.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002910 Tracker.consumeClose();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002911 return ExprError();
2912 }
2913
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002914 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002915 ParseAs = CompoundLiteral;
2916 } else {
2917 bool NotCastExpr;
Eli Friedmancf7530f2009-05-25 19:41:42 +00002918 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2919 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2920 NotCastExpr = true;
2921 } else {
2922 // Try parsing the cast-expression that may follow.
2923 // If it is not a cast-expression, NotCastExpr will be true and no token
2924 // will be consumed.
2925 Result = ParseCastExpression(false/*isUnaryExpression*/,
2926 false/*isAddressofOperand*/,
John McCallba7bf592010-08-24 05:47:05 +00002927 NotCastExpr,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002928 // type-id has priority.
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +00002929 IsTypeCast);
Eli Friedmancf7530f2009-05-25 19:41:42 +00002930 }
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002931
2932 // If we parsed a cast-expression, it's really a type-id, otherwise it's
2933 // an expression.
2934 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002935 }
2936
Mike Stump11289f42009-09-09 15:08:12 +00002937 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002938 Toks.push_back(Tok);
2939 // Re-enter the stored parenthesized tokens into the token stream, so we may
2940 // parse them now.
2941 PP.EnterTokenStream(Toks.data(), Toks.size(),
2942 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2943 // Drop the current token and bring the first cached one. It's the same token
2944 // as when we entered this function.
2945 ConsumeAnyToken();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002946
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002947 if (ParseAs >= CompoundLiteral) {
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002948 // Parse the type declarator.
2949 DeclSpec DS(AttrFactory);
2950 ParseSpecifierQualifierList(DS);
2951 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2952 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002953
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002954 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002955 Tracker.consumeClose();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002956
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002957 if (ParseAs == CompoundLiteral) {
2958 ExprType = CompoundLiteral;
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002959 TypeResult Ty = ParseTypeName();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002960 return ParseCompoundLiteralExpression(Ty.get(),
2961 Tracker.getOpenLocation(),
2962 Tracker.getCloseLocation());
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002963 }
Mike Stump11289f42009-09-09 15:08:12 +00002964
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002965 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2966 assert(ParseAs == CastExpr);
2967
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002968 if (DeclaratorInfo.isInvalidType())
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002969 return ExprError();
2970
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002971 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002972 if (!Result.isInvalid())
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002973 Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(),
2974 DeclaratorInfo, CastTy,
2975 Tracker.getCloseLocation(), Result.take());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002976 return Result;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002977 }
Mike Stump11289f42009-09-09 15:08:12 +00002978
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002979 // Not a compound literal, and not followed by a cast-expression.
2980 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002981
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002982 ExprType = SimpleExpr;
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002983 Result = ParseExpression();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002984 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002985 Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(),
2986 Tok.getLocation(), Result.take());
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002987
2988 // Match the ')'.
2989 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002990 SkipUntil(tok::r_paren, StopAtSemi);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002991 return ExprError();
2992 }
Mike Stump11289f42009-09-09 15:08:12 +00002993
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002994 Tracker.consumeClose();
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002995 return Result;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002996}