blob: 6173a6cd63ed5032244edef367cd524d4987ab5e [file] [log] [blame]
Chris Lattner29375652006-12-04 18:06:35 +00001//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner29375652006-12-04 18:06:35 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Expression parsing implementation for C++.
10//
11//===----------------------------------------------------------------------===//
Vassil Vassilev11ad3392017-03-23 15:11:07 +000012#include "clang/Parse/Parser.h"
Erik Verbruggen888d52a2014-01-15 09:15:43 +000013#include "clang/AST/ASTContext.h"
Chandler Carruth757fcd62014-03-04 10:05:20 +000014#include "clang/AST/DeclTemplate.h"
Eli Friedmanc7c97142012-01-04 02:40:39 +000015#include "clang/Basic/PrettyStackTrace.h"
Richard Smith7d182a72012-03-08 23:06:02 +000016#include "clang/Lex/LiteralSupport.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/Parse/ParseDiagnostic.h"
Vassil Vassilev11ad3392017-03-23 15:11:07 +000018#include "clang/Parse/RAIIObjectsForParser.h"
John McCall8b0666c2010-08-20 18:27:03 +000019#include "clang/Sema/DeclSpec.h"
20#include "clang/Sema/ParsedTemplate.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/Sema/Scope.h"
Douglas Gregor7861a802009-11-03 01:35:08 +000022#include "llvm/Support/ErrorHandling.h"
Richard Smithb2997f52019-05-21 20:10:50 +000023#include <numeric>
Faisal Vali2b391ab2013-09-26 19:54:12 +000024
Chris Lattner29375652006-12-04 18:06:35 +000025using namespace clang;
26
Alp Tokerf990cef2014-01-07 02:35:33 +000027static int SelectDigraphErrorMessage(tok::TokenKind Kind) {
28 switch (Kind) {
29 // template name
30 case tok::unknown: return 0;
31 // casts
32 case tok::kw_const_cast: return 1;
33 case tok::kw_dynamic_cast: return 2;
34 case tok::kw_reinterpret_cast: return 3;
35 case tok::kw_static_cast: return 4;
36 default:
37 llvm_unreachable("Unknown type for digraph error message.");
38 }
39}
40
Richard Smith55858492011-04-14 21:45:45 +000041// Are the two tokens adjacent in the same source file?
Richard Smith7b3f3222012-06-18 06:11:04 +000042bool Parser::areTokensAdjacent(const Token &First, const Token &Second) {
Richard Smith55858492011-04-14 21:45:45 +000043 SourceManager &SM = PP.getSourceManager();
44 SourceLocation FirstLoc = SM.getSpellingLoc(First.getLocation());
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +000045 SourceLocation FirstEnd = FirstLoc.getLocWithOffset(First.getLength());
Richard Smith55858492011-04-14 21:45:45 +000046 return FirstEnd == SM.getSpellingLoc(Second.getLocation());
47}
48
49// Suggest fixit for "<::" after a cast.
50static void FixDigraph(Parser &P, Preprocessor &PP, Token &DigraphToken,
51 Token &ColonToken, tok::TokenKind Kind, bool AtDigraph) {
52 // Pull '<:' and ':' off token stream.
53 if (!AtDigraph)
54 PP.Lex(DigraphToken);
55 PP.Lex(ColonToken);
56
57 SourceRange Range;
58 Range.setBegin(DigraphToken.getLocation());
59 Range.setEnd(ColonToken.getLocation());
60 P.Diag(DigraphToken.getLocation(), diag::err_missing_whitespace_digraph)
Alp Tokerf990cef2014-01-07 02:35:33 +000061 << SelectDigraphErrorMessage(Kind)
62 << FixItHint::CreateReplacement(Range, "< ::");
Richard Smith55858492011-04-14 21:45:45 +000063
64 // Update token information to reflect their change in token type.
65 ColonToken.setKind(tok::coloncolon);
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +000066 ColonToken.setLocation(ColonToken.getLocation().getLocWithOffset(-1));
Richard Smith55858492011-04-14 21:45:45 +000067 ColonToken.setLength(2);
68 DigraphToken.setKind(tok::less);
69 DigraphToken.setLength(1);
70
71 // Push new tokens back to token stream.
Ilya Biryukov929af672019-05-17 09:32:05 +000072 PP.EnterToken(ColonToken, /*IsReinject*/ true);
Richard Smith55858492011-04-14 21:45:45 +000073 if (!AtDigraph)
Ilya Biryukov929af672019-05-17 09:32:05 +000074 PP.EnterToken(DigraphToken, /*IsReinject*/ true);
Richard Smith55858492011-04-14 21:45:45 +000075}
76
Richard Trieu01fc0012011-09-19 19:01:00 +000077// Check for '<::' which should be '< ::' instead of '[:' when following
78// a template name.
79void Parser::CheckForTemplateAndDigraph(Token &Next, ParsedType ObjectType,
80 bool EnteringContext,
81 IdentifierInfo &II, CXXScopeSpec &SS) {
Richard Trieu02e25db2011-09-20 20:03:50 +000082 if (!Next.is(tok::l_square) || Next.getLength() != 2)
Richard Trieu01fc0012011-09-19 19:01:00 +000083 return;
84
85 Token SecondToken = GetLookAheadToken(2);
Richard Smith7b3f3222012-06-18 06:11:04 +000086 if (!SecondToken.is(tok::colon) || !areTokensAdjacent(Next, SecondToken))
Richard Trieu01fc0012011-09-19 19:01:00 +000087 return;
88
89 TemplateTy Template;
90 UnqualifiedId TemplateName;
91 TemplateName.setIdentifier(&II, Tok.getLocation());
92 bool MemberOfUnknownSpecialization;
93 if (!Actions.isTemplateName(getCurScope(), SS, /*hasTemplateKeyword=*/false,
94 TemplateName, ObjectType, EnteringContext,
95 Template, MemberOfUnknownSpecialization))
96 return;
97
Alp Tokerf990cef2014-01-07 02:35:33 +000098 FixDigraph(*this, PP, Next, SecondToken, tok::unknown,
99 /*AtDigraph*/false);
Richard Trieu01fc0012011-09-19 19:01:00 +0000100}
101
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000102/// Parse global scope or nested-name-specifier if present.
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000103///
104/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump11289f42009-09-09 15:08:12 +0000105/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000106/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000107///
108/// '::'[opt] nested-name-specifier
109/// '::'
110///
111/// nested-name-specifier:
112/// type-name '::'
113/// namespace-name '::'
114/// nested-name-specifier identifier '::'
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000115/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000116///
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000117///
Mike Stump11289f42009-09-09 15:08:12 +0000118/// \param SS the scope specifier that will be set to the parsed
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000119/// nested-name-specifier (or empty)
120///
Mike Stump11289f42009-09-09 15:08:12 +0000121/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000122/// the "." or "->" of a member access expression, this parameter provides the
123/// type of the object whose members are being accessed.
124///
125/// \param EnteringContext whether we will be entering into the context of
126/// the nested-name-specifier after parsing it.
127///
Douglas Gregore610ada2010-02-24 18:44:31 +0000128/// \param MayBePseudoDestructor When non-NULL, points to a flag that
129/// indicates whether this nested-name-specifier may be part of a
130/// pseudo-destructor name. In this case, the flag will be set false
131/// if we don't actually end up parsing a destructor name. Moreorover,
132/// if we do end up determining that we are parsing a destructor name,
133/// the last component of the nested-name-specifier is not parsed as
134/// part of the scope specifier.
Richard Smith7447af42013-03-26 01:15:19 +0000135///
136/// \param IsTypename If \c true, this nested-name-specifier is known to be
137/// part of a type name. This is used to improve error recovery.
138///
139/// \param LastII When non-NULL, points to an IdentifierInfo* that will be
140/// filled in with the leading identifier in the last component of the
141/// nested-name-specifier, if any.
Douglas Gregor90d554e2010-02-21 18:36:56 +0000142///
Matthias Gehredc01bb42017-03-17 21:41:20 +0000143/// \param OnlyNamespace If true, only considers namespaces in lookup.
144///
John McCall1f476a12010-02-26 08:45:28 +0000145/// \returns true if there was an error parsing a scope specifier
Douglas Gregore861bac2009-08-25 22:51:20 +0000146bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
John McCallba7bf592010-08-24 05:47:05 +0000147 ParsedType ObjectType,
Douglas Gregor90d554e2010-02-21 18:36:56 +0000148 bool EnteringContext,
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000149 bool *MayBePseudoDestructor,
Richard Smith7447af42013-03-26 01:15:19 +0000150 bool IsTypename,
Matthias Gehredc01bb42017-03-17 21:41:20 +0000151 IdentifierInfo **LastII,
152 bool OnlyNamespace) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000153 assert(getLangOpts().CPlusPlus &&
Chris Lattnerb5134c02009-01-05 01:24:05 +0000154 "Call sites of this function should be guarded by checking for C++");
Mike Stump11289f42009-09-09 15:08:12 +0000155
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000156 if (Tok.is(tok::annot_cxxscope)) {
Richard Smith7447af42013-03-26 01:15:19 +0000157 assert(!LastII && "want last identifier but have already annotated scope");
Nico Weberc60aa712015-02-16 22:32:46 +0000158 assert(!MayBePseudoDestructor && "unexpected annot_cxxscope");
Douglas Gregor869ad452011-02-24 17:54:50 +0000159 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
160 Tok.getAnnotationRange(),
161 SS);
Richard Smithaf3b3252017-05-18 19:21:48 +0000162 ConsumeAnnotationToken();
John McCall1f476a12010-02-26 08:45:28 +0000163 return false;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000164 }
Chris Lattnerf9b2cd42009-01-04 21:14:15 +0000165
Larisse Voufob959c3c2013-08-06 05:49:26 +0000166 if (Tok.is(tok::annot_template_id)) {
167 // If the current token is an annotated template id, it may already have
168 // a scope specifier. Restore it.
169 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
170 SS = TemplateId->SS;
171 }
172
Nico Weberc60aa712015-02-16 22:32:46 +0000173 // Has to happen before any "return false"s in this function.
174 bool CheckForDestructor = false;
175 if (MayBePseudoDestructor && *MayBePseudoDestructor) {
176 CheckForDestructor = true;
177 *MayBePseudoDestructor = false;
178 }
179
Richard Smith7447af42013-03-26 01:15:19 +0000180 if (LastII)
Craig Topper161e4db2014-05-21 06:02:52 +0000181 *LastII = nullptr;
Richard Smith7447af42013-03-26 01:15:19 +0000182
Douglas Gregor7f741122009-02-25 19:37:18 +0000183 bool HasScopeSpecifier = false;
184
Chris Lattner8a7d10d2009-01-05 03:55:46 +0000185 if (Tok.is(tok::coloncolon)) {
186 // ::new and ::delete aren't nested-name-specifiers.
187 tok::TokenKind NextKind = NextToken().getKind();
188 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
189 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000190
David Majnemere8fb28f2014-12-29 19:19:18 +0000191 if (NextKind == tok::l_brace) {
192 // It is invalid to have :: {, consume the scope qualifier and pretend
193 // like we never saw it.
194 Diag(ConsumeToken(), diag::err_expected) << tok::identifier;
195 } else {
196 // '::' - Global scope qualifier.
197 if (Actions.ActOnCXXGlobalScopeSpecifier(ConsumeToken(), SS))
198 return true;
Richard Trieu1f3ea7b2012-11-02 01:08:58 +0000199
David Majnemere8fb28f2014-12-29 19:19:18 +0000200 HasScopeSpecifier = true;
201 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000202 }
203
Nikola Smiljanic67860242014-09-26 00:28:20 +0000204 if (Tok.is(tok::kw___super)) {
205 SourceLocation SuperLoc = ConsumeToken();
206 if (!Tok.is(tok::coloncolon)) {
207 Diag(Tok.getLocation(), diag::err_expected_coloncolon_after_super);
208 return true;
209 }
210
211 return Actions.ActOnSuperScopeSpecifier(SuperLoc, ConsumeToken(), SS);
212 }
213
Richard Smitha9d10012014-10-04 01:57:39 +0000214 if (!HasScopeSpecifier &&
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000215 Tok.isOneOf(tok::kw_decltype, tok::annot_decltype)) {
David Blaikie15a430a2011-12-04 05:04:18 +0000216 DeclSpec DS(AttrFactory);
217 SourceLocation DeclLoc = Tok.getLocation();
218 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
Alp Tokera3ebe6e2013-12-17 14:12:37 +0000219
220 SourceLocation CCLoc;
Richard Smith3f846bd2017-02-08 19:58:48 +0000221 // Work around a standard defect: 'decltype(auto)::' is not a
222 // nested-name-specifier.
223 if (DS.getTypeSpecType() == DeclSpec::TST_decltype_auto ||
224 !TryConsumeToken(tok::coloncolon, CCLoc)) {
David Blaikie15a430a2011-12-04 05:04:18 +0000225 AnnotateExistingDecltypeSpecifier(DS, DeclLoc, EndLoc);
226 return false;
227 }
Alp Tokera3ebe6e2013-12-17 14:12:37 +0000228
David Blaikie15a430a2011-12-04 05:04:18 +0000229 if (Actions.ActOnCXXNestedNameSpecifierDecltype(SS, DS, CCLoc))
230 SS.SetInvalid(SourceRange(DeclLoc, CCLoc));
231
232 HasScopeSpecifier = true;
233 }
234
Douglas Gregor7f741122009-02-25 19:37:18 +0000235 while (true) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000236 if (HasScopeSpecifier) {
Ilya Biryukovf1822ec2018-12-03 13:29:17 +0000237 if (Tok.is(tok::code_completion)) {
238 // Code completion for a nested-name-specifier, where the code
239 // completion token follows the '::'.
240 Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext,
241 ObjectType.get());
242 // Include code completion token into the range of the scope otherwise
243 // when we try to annotate the scope tokens the dangling code completion
244 // token will cause assertion in
245 // Preprocessor::AnnotatePreviousCachedTokens.
246 SS.setEndLoc(Tok.getLocation());
247 cutOffParsing();
248 return true;
249 }
250
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000251 // C++ [basic.lookup.classref]p5:
252 // If the qualified-id has the form
Douglas Gregor308047d2009-09-09 00:23:06 +0000253 //
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000254 // ::class-name-or-namespace-name::...
Douglas Gregor308047d2009-09-09 00:23:06 +0000255 //
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000256 // the class-name-or-namespace-name is looked up in global scope as a
257 // class-name or namespace-name.
258 //
259 // To implement this, we clear out the object type as soon as we've
260 // seen a leading '::' or part of a nested-name-specifier.
David Blaikieefdccaa2016-01-15 23:43:34 +0000261 ObjectType = nullptr;
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000262 }
Mike Stump11289f42009-09-09 15:08:12 +0000263
Douglas Gregor7f741122009-02-25 19:37:18 +0000264 // nested-name-specifier:
Chris Lattner0eed3a62009-06-26 03:47:46 +0000265 // nested-name-specifier 'template'[opt] simple-template-id '::'
266
267 // Parse the optional 'template' keyword, then make sure we have
268 // 'identifier <' after it.
269 if (Tok.is(tok::kw_template)) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000270 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedman2624be42009-08-29 04:08:08 +0000271 // nested-name-specifier, since they aren't allowed to start with
272 // 'template'.
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000273 if (!HasScopeSpecifier && !ObjectType)
Eli Friedman2624be42009-08-29 04:08:08 +0000274 break;
275
Douglas Gregor120635b2009-11-11 16:39:34 +0000276 TentativeParsingAction TPA(*this);
Chris Lattner0eed3a62009-06-26 03:47:46 +0000277 SourceLocation TemplateKWLoc = ConsumeToken();
Richard Smithd091dc12013-12-05 00:58:33 +0000278
Douglas Gregor71395fa2009-11-04 00:56:37 +0000279 UnqualifiedId TemplateName;
280 if (Tok.is(tok::identifier)) {
Douglas Gregor71395fa2009-11-04 00:56:37 +0000281 // Consume the identifier.
Douglas Gregor120635b2009-11-11 16:39:34 +0000282 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregor71395fa2009-11-04 00:56:37 +0000283 ConsumeToken();
284 } else if (Tok.is(tok::kw_operator)) {
Richard Smithd091dc12013-12-05 00:58:33 +0000285 // We don't need to actually parse the unqualified-id in this case,
286 // because a simple-template-id cannot start with 'operator', but
287 // go ahead and parse it anyway for consistency with the case where
288 // we already annotated the template-id.
289 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor120635b2009-11-11 16:39:34 +0000290 TemplateName)) {
291 TPA.Commit();
Douglas Gregor71395fa2009-11-04 00:56:37 +0000292 break;
Douglas Gregor120635b2009-11-11 16:39:34 +0000293 }
Richard Smithd091dc12013-12-05 00:58:33 +0000294
Faisal Vali2ab8c152017-12-30 04:15:27 +0000295 if (TemplateName.getKind() != UnqualifiedIdKind::IK_OperatorFunctionId &&
296 TemplateName.getKind() != UnqualifiedIdKind::IK_LiteralOperatorId) {
Douglas Gregor71395fa2009-11-04 00:56:37 +0000297 Diag(TemplateName.getSourceRange().getBegin(),
298 diag::err_id_after_template_in_nested_name_spec)
299 << TemplateName.getSourceRange();
Douglas Gregor120635b2009-11-11 16:39:34 +0000300 TPA.Commit();
Douglas Gregor71395fa2009-11-04 00:56:37 +0000301 break;
302 }
303 } else {
Douglas Gregor120635b2009-11-11 16:39:34 +0000304 TPA.Revert();
Chris Lattner0eed3a62009-06-26 03:47:46 +0000305 break;
306 }
Mike Stump11289f42009-09-09 15:08:12 +0000307
Douglas Gregor120635b2009-11-11 16:39:34 +0000308 // If the next token is not '<', we have a qualified-id that refers
Fangrui Song6907ce22018-07-30 19:24:48 +0000309 // to a template name, such as T::template apply, but is not a
Douglas Gregor120635b2009-11-11 16:39:34 +0000310 // template-id.
311 if (Tok.isNot(tok::less)) {
312 TPA.Revert();
313 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000314 }
315
Douglas Gregor120635b2009-11-11 16:39:34 +0000316 // Commit to parsing the template-id.
317 TPA.Commit();
Douglas Gregorbb119652010-06-16 23:00:59 +0000318 TemplateTy Template;
Richard Smithfd3dae02017-01-20 00:20:39 +0000319 if (TemplateNameKind TNK = Actions.ActOnDependentTemplateName(
320 getCurScope(), SS, TemplateKWLoc, TemplateName, ObjectType,
321 EnteringContext, Template, /*AllowInjectedClassName*/ true)) {
Abramo Bagnara7945c982012-01-27 09:46:47 +0000322 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateKWLoc,
323 TemplateName, false))
Douglas Gregorbb119652010-06-16 23:00:59 +0000324 return true;
325 } else
John McCall1f476a12010-02-26 08:45:28 +0000326 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000327
Chris Lattner0eed3a62009-06-26 03:47:46 +0000328 continue;
329 }
Mike Stump11289f42009-09-09 15:08:12 +0000330
Douglas Gregor7f741122009-02-25 19:37:18 +0000331 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump11289f42009-09-09 15:08:12 +0000332 // We have
Douglas Gregor7f741122009-02-25 19:37:18 +0000333 //
Richard Smith72bfbd82013-12-04 00:28:23 +0000334 // template-id '::'
Douglas Gregor7f741122009-02-25 19:37:18 +0000335 //
Richard Smith72bfbd82013-12-04 00:28:23 +0000336 // So we need to check whether the template-id is a simple-template-id of
337 // the right kind (it should name a type or be dependent), and then
Douglas Gregorb67535d2009-03-31 00:43:58 +0000338 // convert it into a type within the nested-name-specifier.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +0000339 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregore610ada2010-02-24 18:44:31 +0000340 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
341 *MayBePseudoDestructor = true;
John McCall1f476a12010-02-26 08:45:28 +0000342 return false;
Douglas Gregore610ada2010-02-24 18:44:31 +0000343 }
344
Richard Smith7447af42013-03-26 01:15:19 +0000345 if (LastII)
346 *LastII = TemplateId->Name;
347
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000348 // Consume the template-id token.
Richard Smithaf3b3252017-05-18 19:21:48 +0000349 ConsumeAnnotationToken();
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000350
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000351 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
352 SourceLocation CCLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000353
David Blaikie8c045bc2011-11-07 03:30:03 +0000354 HasScopeSpecifier = true;
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000355
Benjamin Kramercc4c49d2012-08-23 23:38:35 +0000356 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000357 TemplateId->NumArgs);
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000358
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000359 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000360 SS,
361 TemplateId->TemplateKWLoc,
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000362 TemplateId->Template,
363 TemplateId->TemplateNameLoc,
364 TemplateId->LAngleLoc,
365 TemplateArgsPtr,
366 TemplateId->RAngleLoc,
367 CCLoc,
368 EnteringContext)) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000369 SourceLocation StartLoc
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000370 = SS.getBeginLoc().isValid()? SS.getBeginLoc()
371 : TemplateId->TemplateNameLoc;
372 SS.SetInvalid(SourceRange(StartLoc, CCLoc));
Chris Lattner704edfb2009-06-26 03:45:46 +0000373 }
Argyrios Kyrtzidis13935672011-05-03 18:45:38 +0000374
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000375 continue;
Douglas Gregor7f741122009-02-25 19:37:18 +0000376 }
377
Chris Lattnere2355f72009-06-26 03:52:38 +0000378 // The rest of the nested-name-specifier possibilities start with
379 // tok::identifier.
380 if (Tok.isNot(tok::identifier))
381 break;
382
383 IdentifierInfo &II = *Tok.getIdentifierInfo();
384
385 // nested-name-specifier:
386 // type-name '::'
387 // namespace-name '::'
388 // nested-name-specifier identifier '::'
389 Token Next = NextToken();
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000390 Sema::NestedNameSpecInfo IdInfo(&II, Tok.getLocation(), Next.getLocation(),
391 ObjectType);
392
Chris Lattner1c428032009-12-07 01:36:53 +0000393 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
394 // and emit a fixit hint for it.
Douglas Gregor90d554e2010-02-21 18:36:56 +0000395 if (Next.is(tok::colon) && !ColonIsSacred) {
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000396 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, IdInfo,
Douglas Gregor90d554e2010-02-21 18:36:56 +0000397 EnteringContext) &&
398 // If the token after the colon isn't an identifier, it's still an
399 // error, but they probably meant something else strange so don't
400 // recover like this.
401 PP.LookAhead(1).is(tok::identifier)) {
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000402 Diag(Next, diag::err_unexpected_colon_in_nested_name_spec)
Douglas Gregora771f462010-03-31 17:46:05 +0000403 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregor90d554e2010-02-21 18:36:56 +0000404 // Recover as if the user wrote '::'.
405 Next.setKind(tok::coloncolon);
406 }
Chris Lattner1c428032009-12-07 01:36:53 +0000407 }
David Majnemerf58efd92014-12-29 23:12:23 +0000408
409 if (Next.is(tok::coloncolon) && GetLookAheadToken(2).is(tok::l_brace)) {
410 // It is invalid to have :: {, consume the scope qualifier and pretend
411 // like we never saw it.
412 Token Identifier = Tok; // Stash away the identifier.
413 ConsumeToken(); // Eat the identifier, current token is now '::'.
David Majnemerec3f49d2014-12-29 23:24:27 +0000414 Diag(PP.getLocForEndOfToken(ConsumeToken()), diag::err_expected)
415 << tok::identifier;
David Majnemerf58efd92014-12-29 23:12:23 +0000416 UnconsumeToken(Identifier); // Stick the identifier back.
417 Next = NextToken(); // Point Next at the '{' token.
418 }
419
Chris Lattnere2355f72009-06-26 03:52:38 +0000420 if (Next.is(tok::coloncolon)) {
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000421 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000422 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, IdInfo)) {
Douglas Gregore610ada2010-02-24 18:44:31 +0000423 *MayBePseudoDestructor = true;
John McCall1f476a12010-02-26 08:45:28 +0000424 return false;
Douglas Gregore610ada2010-02-24 18:44:31 +0000425 }
426
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000427 if (ColonIsSacred) {
428 const Token &Next2 = GetLookAheadToken(2);
429 if (Next2.is(tok::kw_private) || Next2.is(tok::kw_protected) ||
430 Next2.is(tok::kw_public) || Next2.is(tok::kw_virtual)) {
431 Diag(Next2, diag::err_unexpected_token_in_nested_name_spec)
432 << Next2.getName()
433 << FixItHint::CreateReplacement(Next.getLocation(), ":");
434 Token ColonColon;
435 PP.Lex(ColonColon);
436 ColonColon.setKind(tok::colon);
Ilya Biryukov929af672019-05-17 09:32:05 +0000437 PP.EnterToken(ColonColon, /*IsReinject*/ true);
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000438 break;
439 }
440 }
441
Richard Smith7447af42013-03-26 01:15:19 +0000442 if (LastII)
443 *LastII = &II;
444
Chris Lattnere2355f72009-06-26 03:52:38 +0000445 // We have an identifier followed by a '::'. Lookup this name
446 // as the name in a nested-name-specifier.
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000447 Token Identifier = Tok;
Chris Lattnere2355f72009-06-26 03:52:38 +0000448 SourceLocation IdLoc = ConsumeToken();
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000449 assert(Tok.isOneOf(tok::coloncolon, tok::colon) &&
Chris Lattner1c428032009-12-07 01:36:53 +0000450 "NextToken() not working properly!");
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000451 Token ColonColon = Tok;
Chris Lattnere2355f72009-06-26 03:52:38 +0000452 SourceLocation CCLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000453
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000454 bool IsCorrectedToColon = false;
Craig Topper161e4db2014-05-21 06:02:52 +0000455 bool *CorrectionFlagPtr = ColonIsSacred ? &IsCorrectedToColon : nullptr;
Matthias Gehredc01bb42017-03-17 21:41:20 +0000456 if (Actions.ActOnCXXNestedNameSpecifier(
457 getCurScope(), IdInfo, EnteringContext, SS, false,
458 CorrectionFlagPtr, OnlyNamespace)) {
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000459 // Identifier is not recognized as a nested name, but we can have
460 // mistyped '::' instead of ':'.
461 if (CorrectionFlagPtr && IsCorrectedToColon) {
462 ColonColon.setKind(tok::colon);
Ilya Biryukov929af672019-05-17 09:32:05 +0000463 PP.EnterToken(Tok, /*IsReinject*/ true);
464 PP.EnterToken(ColonColon, /*IsReinject*/ true);
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000465 Tok = Identifier;
466 break;
467 }
Douglas Gregor90c99722011-02-24 00:17:56 +0000468 SS.SetInvalid(SourceRange(IdLoc, CCLoc));
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000469 }
470 HasScopeSpecifier = true;
Chris Lattnere2355f72009-06-26 03:52:38 +0000471 continue;
472 }
Mike Stump11289f42009-09-09 15:08:12 +0000473
Richard Trieu01fc0012011-09-19 19:01:00 +0000474 CheckForTemplateAndDigraph(Next, ObjectType, EnteringContext, II, SS);
Richard Smith55858492011-04-14 21:45:45 +0000475
Chris Lattnere2355f72009-06-26 03:52:38 +0000476 // nested-name-specifier:
477 // type-name '<'
478 if (Next.is(tok::less)) {
479 TemplateTy Template;
Douglas Gregor3cf81312009-11-03 23:16:33 +0000480 UnqualifiedId TemplateName;
481 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor786123d2010-05-21 23:18:07 +0000482 bool MemberOfUnknownSpecialization;
Fangrui Song6907ce22018-07-30 19:24:48 +0000483 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c5dee42010-08-06 12:11:11 +0000484 /*hasTemplateKeyword=*/false,
Douglas Gregor3cf81312009-11-03 23:16:33 +0000485 TemplateName,
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000486 ObjectType,
Douglas Gregore861bac2009-08-25 22:51:20 +0000487 EnteringContext,
Douglas Gregor786123d2010-05-21 23:18:07 +0000488 Template,
489 MemberOfUnknownSpecialization)) {
Richard Smithb23c5e82019-05-09 03:31:27 +0000490 // If lookup didn't find anything, we treat the name as a template-name
491 // anyway. C++20 requires this, and in prior language modes it improves
492 // error recovery. But before we commit to this, check that we actually
493 // have something that looks like a template-argument-list next.
494 if (!IsTypename && TNK == TNK_Undeclared_template &&
495 isTemplateArgumentList(1) == TPResult::False)
496 break;
497
David Blaikie8c045bc2011-11-07 03:30:03 +0000498 // We have found a template name, so annotate this token
Chris Lattnere2355f72009-06-26 03:52:38 +0000499 // with a template-id annotation. We do not permit the
500 // template-id to be translated into a type annotation,
501 // because some clients (e.g., the parsing of class template
502 // specializations) still want to see the original template-id
503 // token.
Douglas Gregor71395fa2009-11-04 00:56:37 +0000504 ConsumeToken();
Abramo Bagnara7945c982012-01-27 09:46:47 +0000505 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
506 TemplateName, false))
John McCall1f476a12010-02-26 08:45:28 +0000507 return true;
Chris Lattnere2355f72009-06-26 03:52:38 +0000508 continue;
Larisse Voufo39a1e502013-08-06 01:03:05 +0000509 }
510
Fangrui Song6907ce22018-07-30 19:24:48 +0000511 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
Richard Smithb23c5e82019-05-09 03:31:27 +0000512 (IsTypename || isTemplateArgumentList(1) == TPResult::True)) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000513 // We have something like t::getAs<T>, where getAs is a
Douglas Gregor20c38a72010-05-21 23:43:39 +0000514 // member of an unknown specialization. However, this will only
515 // parse correctly as a template, so suggest the keyword 'template'
516 // before 'getAs' and treat this as a dependent template name.
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000517 unsigned DiagID = diag::err_missing_dependent_template_keyword;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000518 if (getLangOpts().MicrosoftExt)
Francois Pichet93921652011-04-22 08:25:24 +0000519 DiagID = diag::warn_missing_dependent_template_keyword;
Fangrui Song6907ce22018-07-30 19:24:48 +0000520
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000521 Diag(Tok.getLocation(), DiagID)
Douglas Gregor20c38a72010-05-21 23:43:39 +0000522 << II.getName()
523 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
Richard Smithfd3dae02017-01-20 00:20:39 +0000524
525 if (TemplateNameKind TNK = Actions.ActOnDependentTemplateName(
Richard Smith79810042018-05-11 02:43:08 +0000526 getCurScope(), SS, Tok.getLocation(), TemplateName, ObjectType,
Richard Smithfd3dae02017-01-20 00:20:39 +0000527 EnteringContext, Template, /*AllowInjectedClassName*/ true)) {
Douglas Gregorbb119652010-06-16 23:00:59 +0000528 // Consume the identifier.
529 ConsumeToken();
Abramo Bagnara7945c982012-01-27 09:46:47 +0000530 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
531 TemplateName, false))
532 return true;
Douglas Gregorbb119652010-06-16 23:00:59 +0000533 }
534 else
Fangrui Song6907ce22018-07-30 19:24:48 +0000535 return true;
536
537 continue;
Chris Lattnere2355f72009-06-26 03:52:38 +0000538 }
539 }
540
Douglas Gregor7f741122009-02-25 19:37:18 +0000541 // We don't have any tokens that form the beginning of a
542 // nested-name-specifier, so we're done.
543 break;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000544 }
Mike Stump11289f42009-09-09 15:08:12 +0000545
Douglas Gregore610ada2010-02-24 18:44:31 +0000546 // Even if we didn't see any pieces of a nested-name-specifier, we
547 // still check whether there is a tilde in this position, which
548 // indicates a potential pseudo-destructor.
549 if (CheckForDestructor && Tok.is(tok::tilde))
550 *MayBePseudoDestructor = true;
551
John McCall1f476a12010-02-26 08:45:28 +0000552 return false;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000553}
554
Kaelyn Takatab16e6322014-11-20 22:06:40 +0000555ExprResult Parser::tryParseCXXIdExpression(CXXScopeSpec &SS, bool isAddressOfOperand,
556 Token &Replacement) {
557 SourceLocation TemplateKWLoc;
558 UnqualifiedId Name;
559 if (ParseUnqualifiedId(SS,
560 /*EnteringContext=*/false,
561 /*AllowDestructorName=*/false,
562 /*AllowConstructorName=*/false,
Richard Smith35845152017-02-07 01:37:30 +0000563 /*AllowDeductionGuide=*/false,
Richard Smithc08b6932018-04-27 02:00:13 +0000564 /*ObjectType=*/nullptr, &TemplateKWLoc, Name))
Kaelyn Takatab16e6322014-11-20 22:06:40 +0000565 return ExprError();
566
567 // This is only the direct operand of an & operator if it is not
568 // followed by a postfix-expression suffix.
569 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
570 isAddressOfOperand = false;
571
Richard Smithc2dead42018-06-27 01:32:04 +0000572 ExprResult E = Actions.ActOnIdExpression(
573 getCurScope(), SS, TemplateKWLoc, Name, Tok.is(tok::l_paren),
Bruno Ricci70ad3962019-03-25 17:08:51 +0000574 isAddressOfOperand, /*CCC=*/nullptr, /*IsInlineAsmIdentifier=*/false,
Richard Smithc2dead42018-06-27 01:32:04 +0000575 &Replacement);
576 if (!E.isInvalid() && !E.isUnset() && Tok.is(tok::less))
577 checkPotentialAngleBracket(E);
578 return E;
Kaelyn Takatab16e6322014-11-20 22:06:40 +0000579}
580
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000581/// ParseCXXIdExpression - Handle id-expression.
582///
583/// id-expression:
584/// unqualified-id
585/// qualified-id
586///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000587/// qualified-id:
588/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
589/// '::' identifier
590/// '::' operator-function-id
Douglas Gregora727cb92009-06-30 22:34:41 +0000591/// '::' template-id
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000592///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000593/// NOTE: The standard specifies that, for qualified-id, the parser does not
594/// expect:
595///
596/// '::' conversion-function-id
597/// '::' '~' class-name
598///
599/// This may cause a slight inconsistency on diagnostics:
600///
601/// class C {};
602/// namespace A {}
603/// void f() {
604/// :: A :: ~ C(); // Some Sema error about using destructor with a
605/// // namespace.
606/// :: ~ C(); // Some Parser error like 'unexpected ~'.
607/// }
608///
609/// We simplify the parser a bit and make it work like:
610///
611/// qualified-id:
612/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
613/// '::' unqualified-id
614///
615/// That way Sema can handle and report similar errors for namespaces and the
616/// global scope.
617///
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000618/// The isAddressOfOperand parameter indicates that this id-expression is a
619/// direct operand of the address-of operator. This is, besides member contexts,
620/// the only place where a qualified-id naming a non-static class member may
621/// appear.
622///
John McCalldadc5752010-08-24 06:29:42 +0000623ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000624 // qualified-id:
625 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
626 // '::' unqualified-id
627 //
628 CXXScopeSpec SS;
David Blaikieefdccaa2016-01-15 23:43:34 +0000629 ParseOptionalCXXScopeSpecifier(SS, nullptr, /*EnteringContext=*/false);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000630
Kaelyn Takatab16e6322014-11-20 22:06:40 +0000631 Token Replacement;
Nico Weber01a46ad2015-02-15 06:15:40 +0000632 ExprResult Result =
633 tryParseCXXIdExpression(SS, isAddressOfOperand, Replacement);
Kaelyn Takatab16e6322014-11-20 22:06:40 +0000634 if (Result.isUnset()) {
635 // If the ExprResult is valid but null, then typo correction suggested a
636 // keyword replacement that needs to be reparsed.
637 UnconsumeToken(Replacement);
638 Result = tryParseCXXIdExpression(SS, isAddressOfOperand, Replacement);
639 }
640 assert(!Result.isUnset() && "Typo correction suggested a keyword replacement "
641 "for a previous keyword suggestion");
642 return Result;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000643}
644
Richard Smith21b3ab42013-05-09 21:36:41 +0000645/// ParseLambdaExpression - Parse a C++11 lambda expression.
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000646///
647/// lambda-expression:
648/// lambda-introducer lambda-declarator[opt] compound-statement
Hamza Sood8205a812019-05-04 10:49:46 +0000649/// lambda-introducer '<' template-parameter-list '>'
650/// lambda-declarator[opt] compound-statement
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000651///
652/// lambda-introducer:
653/// '[' lambda-capture[opt] ']'
654///
655/// lambda-capture:
656/// capture-default
657/// capture-list
658/// capture-default ',' capture-list
659///
660/// capture-default:
661/// '&'
662/// '='
663///
664/// capture-list:
665/// capture
666/// capture-list ',' capture
667///
668/// capture:
Richard Smith21b3ab42013-05-09 21:36:41 +0000669/// simple-capture
670/// init-capture [C++1y]
671///
672/// simple-capture:
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000673/// identifier
674/// '&' identifier
675/// 'this'
676///
Richard Smith21b3ab42013-05-09 21:36:41 +0000677/// init-capture: [C++1y]
678/// identifier initializer
679/// '&' identifier initializer
680///
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000681/// lambda-declarator:
682/// '(' parameter-declaration-clause ')' attribute-specifier[opt]
683/// 'mutable'[opt] exception-specification[opt]
684/// trailing-return-type[opt]
685///
686ExprResult Parser::ParseLambdaExpression() {
687 // Parse lambda-introducer.
688 LambdaIntroducer Intro;
Richard Smithe9585062019-05-20 18:01:54 +0000689 if (ParseLambdaIntroducer(Intro)) {
David Majnemer234b8182015-01-12 03:36:37 +0000690 SkipUntil(tok::r_square, StopAtSemi);
691 SkipUntil(tok::l_brace, StopAtSemi);
692 SkipUntil(tok::r_brace, StopAtSemi);
Eli Friedmanc7c97142012-01-04 02:40:39 +0000693 return ExprError();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000694 }
695
696 return ParseLambdaExpressionAfterIntroducer(Intro);
697}
698
Richard Smithe9585062019-05-20 18:01:54 +0000699/// Use lookahead and potentially tentative parsing to determine if we are
700/// looking at a C++11 lambda expression, and parse it if we are.
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000701///
702/// If we are not looking at a lambda expression, returns ExprError().
703ExprResult Parser::TryParseLambdaExpression() {
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000704 assert(getLangOpts().CPlusPlus11
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000705 && Tok.is(tok::l_square)
706 && "Not at the start of a possible lambda expression.");
707
Bruno Cardoso Lopes29b34232016-05-31 18:46:31 +0000708 const Token Next = NextToken();
709 if (Next.is(tok::eof)) // Nothing else to lookup here...
710 return ExprEmpty();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000711
Bruno Cardoso Lopes29b34232016-05-31 18:46:31 +0000712 const Token After = GetLookAheadToken(2);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000713 // If lookahead indicates this is a lambda...
714 if (Next.is(tok::r_square) || // []
715 Next.is(tok::equal) || // [=
716 (Next.is(tok::amp) && // [&] or [&,
Richard Smithb2997f52019-05-21 20:10:50 +0000717 After.isOneOf(tok::r_square, tok::comma)) ||
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000718 (Next.is(tok::identifier) && // [identifier]
Richard Smithb2997f52019-05-21 20:10:50 +0000719 After.is(tok::r_square)) ||
720 Next.is(tok::ellipsis)) { // [...
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000721 return ParseLambdaExpression();
722 }
723
Eli Friedmanc7c97142012-01-04 02:40:39 +0000724 // If lookahead indicates an ObjC message send...
725 // [identifier identifier
Richard Smithe9585062019-05-20 18:01:54 +0000726 if (Next.is(tok::identifier) && After.is(tok::identifier))
Eli Friedmanc7c97142012-01-04 02:40:39 +0000727 return ExprEmpty();
Fangrui Song6907ce22018-07-30 19:24:48 +0000728
Eli Friedmanc7c97142012-01-04 02:40:39 +0000729 // Here, we're stuck: lambda introducers and Objective-C message sends are
730 // unambiguous, but it requires arbitrary lookhead. [a,b,c,d,e,f,g] is a
731 // lambda, and [a,b,c,d,e,f,g h] is a Objective-C message send. Instead of
732 // writing two routines to parse a lambda introducer, just try to parse
733 // a lambda introducer first, and fall back if that fails.
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000734 LambdaIntroducer Intro;
Richard Smithe9585062019-05-20 18:01:54 +0000735 {
736 TentativeParsingAction TPA(*this);
737 LambdaIntroducerTentativeParse Tentative;
738 if (ParseLambdaIntroducer(Intro, &Tentative)) {
739 TPA.Commit();
740 return ExprError();
741 }
742
743 switch (Tentative) {
744 case LambdaIntroducerTentativeParse::Success:
745 TPA.Commit();
746 break;
747
748 case LambdaIntroducerTentativeParse::Incomplete:
749 // Didn't fully parse the lambda-introducer, try again with a
750 // non-tentative parse.
751 TPA.Revert();
752 Intro = LambdaIntroducer();
753 if (ParseLambdaIntroducer(Intro))
754 return ExprError();
755 break;
756
757 case LambdaIntroducerTentativeParse::MessageSend:
758 case LambdaIntroducerTentativeParse::Invalid:
759 // Not a lambda-introducer, might be a message send.
760 TPA.Revert();
761 return ExprEmpty();
762 }
763 }
Faisal Vali5fb7c3c2013-12-05 01:40:41 +0000764
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000765 return ParseLambdaExpressionAfterIntroducer(Intro);
766}
767
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000768/// Parse a lambda introducer.
Richard Smithf44d2a82013-05-21 22:21:19 +0000769/// \param Intro A LambdaIntroducer filled in with information about the
770/// contents of the lambda-introducer.
Richard Smithe9585062019-05-20 18:01:54 +0000771/// \param Tentative If non-null, we are disambiguating between a
772/// lambda-introducer and some other construct. In this mode, we do not
773/// produce any diagnostics or take any other irreversible action unless
774/// we're sure that this is a lambda-expression.
775/// \return \c true if parsing (or disambiguation) failed with a diagnostic and
776/// the caller should bail out / recover.
777bool Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro,
778 LambdaIntroducerTentativeParse *Tentative) {
779 if (Tentative)
780 *Tentative = LambdaIntroducerTentativeParse::Success;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000781
782 assert(Tok.is(tok::l_square) && "Lambda expressions begin with '['.");
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000783 BalancedDelimiterTracker T(*this, tok::l_square);
784 T.consumeOpen();
785
786 Intro.Range.setBegin(T.getOpenLocation());
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000787
Richard Smithe9585062019-05-20 18:01:54 +0000788 bool First = true;
789
790 // Produce a diagnostic if we're not tentatively parsing; otherwise track
791 // that our parse has failed.
792 auto Invalid = [&](llvm::function_ref<void()> Action) {
793 if (Tentative) {
794 *Tentative = LambdaIntroducerTentativeParse::Invalid;
795 return false;
796 }
797 Action();
798 return true;
799 };
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000800
Richard Smithb2997f52019-05-21 20:10:50 +0000801 // Perform some irreversible action if this is a non-tentative parse;
802 // otherwise note that our actions were incomplete.
803 auto NonTentativeAction = [&](llvm::function_ref<void()> Action) {
804 if (Tentative)
805 *Tentative = LambdaIntroducerTentativeParse::Incomplete;
806 else
807 Action();
808 };
809
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000810 // Parse capture-default.
811 if (Tok.is(tok::amp) &&
812 (NextToken().is(tok::comma) || NextToken().is(tok::r_square))) {
813 Intro.Default = LCD_ByRef;
Douglas Gregora1bffa22012-02-10 17:46:20 +0000814 Intro.DefaultLoc = ConsumeToken();
Richard Smithe9585062019-05-20 18:01:54 +0000815 First = false;
816 if (!Tok.getIdentifierInfo()) {
817 // This can only be a lambda; no need for tentative parsing any more.
818 // '[[and]]' can still be an attribute, though.
819 Tentative = nullptr;
820 }
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000821 } else if (Tok.is(tok::equal)) {
822 Intro.Default = LCD_ByCopy;
Douglas Gregora1bffa22012-02-10 17:46:20 +0000823 Intro.DefaultLoc = ConsumeToken();
Richard Smithe9585062019-05-20 18:01:54 +0000824 First = false;
825 Tentative = nullptr;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000826 }
827
828 while (Tok.isNot(tok::r_square)) {
Richard Smithe9585062019-05-20 18:01:54 +0000829 if (!First) {
Douglas Gregord8c61782012-02-15 15:34:24 +0000830 if (Tok.isNot(tok::comma)) {
Douglas Gregor721b14d2012-07-31 00:50:07 +0000831 // Provide a completion for a lambda introducer here. Except
832 // in Objective-C, where this is Almost Surely meant to be a message
833 // send. In that case, fail here and let the ObjC message
834 // expression parser perform the completion.
Douglas Gregor2d8db8f2012-07-31 15:27:48 +0000835 if (Tok.is(tok::code_completion) &&
Richard Smithe9585062019-05-20 18:01:54 +0000836 !(getLangOpts().ObjC && Tentative)) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000837 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
Douglas Gregord8c61782012-02-15 15:34:24 +0000838 /*AfterAmpersand=*/false);
Alp Toker1c583cc2014-05-02 03:43:14 +0000839 cutOffParsing();
Douglas Gregord8c61782012-02-15 15:34:24 +0000840 break;
841 }
842
Richard Smithe9585062019-05-20 18:01:54 +0000843 return Invalid([&] {
844 Diag(Tok.getLocation(), diag::err_expected_comma_or_rsquare);
845 });
Douglas Gregord8c61782012-02-15 15:34:24 +0000846 }
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000847 ConsumeToken();
848 }
849
Douglas Gregord8c61782012-02-15 15:34:24 +0000850 if (Tok.is(tok::code_completion)) {
851 // If we're in Objective-C++ and we have a bare '[', then this is more
852 // likely to be a message receiver.
Richard Smithe9585062019-05-20 18:01:54 +0000853 if (getLangOpts().ObjC && Tentative && First)
Douglas Gregord8c61782012-02-15 15:34:24 +0000854 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
855 else
Fangrui Song6907ce22018-07-30 19:24:48 +0000856 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
Douglas Gregord8c61782012-02-15 15:34:24 +0000857 /*AfterAmpersand=*/false);
Alp Toker1c583cc2014-05-02 03:43:14 +0000858 cutOffParsing();
Douglas Gregord8c61782012-02-15 15:34:24 +0000859 break;
860 }
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000861
Richard Smithe9585062019-05-20 18:01:54 +0000862 First = false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000863
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000864 // Parse capture.
865 LambdaCaptureKind Kind = LCK_ByCopy;
Richard Smith42b10572015-11-11 01:36:17 +0000866 LambdaCaptureInitKind InitKind = LambdaCaptureInitKind::NoInit;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000867 SourceLocation Loc;
Craig Topper161e4db2014-05-21 06:02:52 +0000868 IdentifierInfo *Id = nullptr;
Richard Smithb2997f52019-05-21 20:10:50 +0000869 SourceLocation EllipsisLocs[4];
Richard Smith21b3ab42013-05-09 21:36:41 +0000870 ExprResult Init;
Alexander Shaposhnikov832f49b2018-07-16 07:23:47 +0000871 SourceLocation LocStart = Tok.getLocation();
Faisal Validc6b5962016-03-21 09:25:37 +0000872
873 if (Tok.is(tok::star)) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000874 Loc = ConsumeToken();
Faisal Validc6b5962016-03-21 09:25:37 +0000875 if (Tok.is(tok::kw_this)) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000876 ConsumeToken();
877 Kind = LCK_StarThis;
Faisal Validc6b5962016-03-21 09:25:37 +0000878 } else {
Richard Smithe9585062019-05-20 18:01:54 +0000879 return Invalid([&] {
880 Diag(Tok.getLocation(), diag::err_expected_star_this_capture);
881 });
Faisal Validc6b5962016-03-21 09:25:37 +0000882 }
883 } else if (Tok.is(tok::kw_this)) {
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000884 Kind = LCK_This;
885 Loc = ConsumeToken();
886 } else {
Richard Smithb2997f52019-05-21 20:10:50 +0000887 TryConsumeToken(tok::ellipsis, EllipsisLocs[0]);
888
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000889 if (Tok.is(tok::amp)) {
890 Kind = LCK_ByRef;
891 ConsumeToken();
Douglas Gregord8c61782012-02-15 15:34:24 +0000892
893 if (Tok.is(tok::code_completion)) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000894 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
Douglas Gregord8c61782012-02-15 15:34:24 +0000895 /*AfterAmpersand=*/true);
Alp Toker1c583cc2014-05-02 03:43:14 +0000896 cutOffParsing();
Douglas Gregord8c61782012-02-15 15:34:24 +0000897 break;
898 }
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000899 }
900
Richard Smithb2997f52019-05-21 20:10:50 +0000901 TryConsumeToken(tok::ellipsis, EllipsisLocs[1]);
902
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000903 if (Tok.is(tok::identifier)) {
904 Id = Tok.getIdentifierInfo();
905 Loc = ConsumeToken();
906 } else if (Tok.is(tok::kw_this)) {
Richard Smithe9585062019-05-20 18:01:54 +0000907 return Invalid([&] {
908 // FIXME: Suggest a fixit here.
909 Diag(Tok.getLocation(), diag::err_this_captured_by_reference);
910 });
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000911 } else {
Richard Smithe9585062019-05-20 18:01:54 +0000912 return Invalid([&] {
913 Diag(Tok.getLocation(), diag::err_expected_capture);
914 });
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000915 }
Richard Smith21b3ab42013-05-09 21:36:41 +0000916
Richard Smithb2997f52019-05-21 20:10:50 +0000917 TryConsumeToken(tok::ellipsis, EllipsisLocs[2]);
918
Richard Smith21b3ab42013-05-09 21:36:41 +0000919 if (Tok.is(tok::l_paren)) {
920 BalancedDelimiterTracker Parens(*this, tok::l_paren);
921 Parens.consumeOpen();
922
Richard Smith42b10572015-11-11 01:36:17 +0000923 InitKind = LambdaCaptureInitKind::DirectInit;
924
Richard Smith21b3ab42013-05-09 21:36:41 +0000925 ExprVector Exprs;
926 CommaLocsTy Commas;
Richard Smithe9585062019-05-20 18:01:54 +0000927 if (Tentative) {
Richard Smithf44d2a82013-05-21 22:21:19 +0000928 Parens.skipToEnd();
Richard Smithe9585062019-05-20 18:01:54 +0000929 *Tentative = LambdaIntroducerTentativeParse::Incomplete;
Richard Smithf44d2a82013-05-21 22:21:19 +0000930 } else if (ParseExpressionList(Exprs, Commas)) {
Richard Smith21b3ab42013-05-09 21:36:41 +0000931 Parens.skipToEnd();
932 Init = ExprError();
933 } else {
934 Parens.consumeClose();
935 Init = Actions.ActOnParenListExpr(Parens.getOpenLocation(),
936 Parens.getCloseLocation(),
937 Exprs);
938 }
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000939 } else if (Tok.isOneOf(tok::l_brace, tok::equal)) {
Faisal Vali5fb7c3c2013-12-05 01:40:41 +0000940 // Each lambda init-capture forms its own full expression, which clears
941 // Actions.MaybeODRUseExprs. So create an expression evaluation context
942 // to save the necessary state, and restore it later.
Faisal Valid143a0c2017-04-01 21:30:49 +0000943 EnterExpressionEvaluationContext EC(
944 Actions, Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
Richard Smith42b10572015-11-11 01:36:17 +0000945
946 if (TryConsumeToken(tok::equal))
947 InitKind = LambdaCaptureInitKind::CopyInit;
948 else
949 InitKind = LambdaCaptureInitKind::ListInit;
Richard Smith21b3ab42013-05-09 21:36:41 +0000950
Richard Smithe9585062019-05-20 18:01:54 +0000951 if (!Tentative) {
Richard Smithf44d2a82013-05-21 22:21:19 +0000952 Init = ParseInitializer();
Richard Smith215f4232015-02-11 02:41:33 +0000953 } else if (Tok.is(tok::l_brace)) {
Richard Smithf44d2a82013-05-21 22:21:19 +0000954 BalancedDelimiterTracker Braces(*this, tok::l_brace);
955 Braces.consumeOpen();
956 Braces.skipToEnd();
Richard Smithe9585062019-05-20 18:01:54 +0000957 *Tentative = LambdaIntroducerTentativeParse::Incomplete;
Richard Smithf44d2a82013-05-21 22:21:19 +0000958 } else {
959 // We're disambiguating this:
960 //
961 // [..., x = expr
962 //
963 // We need to find the end of the following expression in order to
Richard Smith9e2f0a42014-04-13 04:31:48 +0000964 // determine whether this is an Obj-C message send's receiver, a
965 // C99 designator, or a lambda init-capture.
Richard Smithf44d2a82013-05-21 22:21:19 +0000966 //
967 // Parse the expression to find where it ends, and annotate it back
968 // onto the tokens. We would have parsed this expression the same way
969 // in either case: both the RHS of an init-capture and the RHS of an
970 // assignment expression are parsed as an initializer-clause, and in
971 // neither case can anything be added to the scope between the '[' and
972 // here.
973 //
974 // FIXME: This is horrible. Adding a mechanism to skip an expression
975 // would be much cleaner.
976 // FIXME: If there is a ',' before the next ']' or ':', we can skip to
977 // that instead. (And if we see a ':' with no matching '?', we can
978 // classify this as an Obj-C message send.)
979 SourceLocation StartLoc = Tok.getLocation();
980 InMessageExpressionRAIIObject MaybeInMessageExpression(*this, true);
981 Init = ParseInitializer();
Akira Hatanaka51e60f92016-12-20 02:11:29 +0000982 if (!Init.isInvalid())
983 Init = Actions.CorrectDelayedTyposInExpr(Init.get());
Richard Smithf44d2a82013-05-21 22:21:19 +0000984
985 if (Tok.getLocation() != StartLoc) {
986 // Back out the lexing of the token after the initializer.
987 PP.RevertCachedTokens(1);
988
989 // Replace the consumed tokens with an appropriate annotation.
990 Tok.setLocation(StartLoc);
991 Tok.setKind(tok::annot_primary_expr);
992 setExprAnnotation(Tok, Init);
993 Tok.setAnnotationEndLoc(PP.getLastCachedTokenLocation());
994 PP.AnnotateCachedTokens(Tok);
995
996 // Consume the annotated initializer.
Richard Smithaf3b3252017-05-18 19:21:48 +0000997 ConsumeAnnotationToken();
Richard Smithf44d2a82013-05-21 22:21:19 +0000998 }
999 }
Richard Smithe9585062019-05-20 18:01:54 +00001000 }
Richard Smithb2997f52019-05-21 20:10:50 +00001001
1002 TryConsumeToken(tok::ellipsis, EllipsisLocs[3]);
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001003 }
Richard Smithe9585062019-05-20 18:01:54 +00001004
1005 // Check if this is a message send before we act on a possible init-capture.
1006 if (Tentative && Tok.is(tok::identifier) &&
1007 NextToken().isOneOf(tok::colon, tok::r_square)) {
1008 // This can only be a message send. We're done with disambiguation.
1009 *Tentative = LambdaIntroducerTentativeParse::MessageSend;
1010 return false;
1011 }
1012
Richard Smithb2997f52019-05-21 20:10:50 +00001013 // Ensure that any ellipsis was in the right place.
1014 SourceLocation EllipsisLoc;
1015 if (std::any_of(std::begin(EllipsisLocs), std::end(EllipsisLocs),
1016 [](SourceLocation Loc) { return Loc.isValid(); })) {
1017 // The '...' should appear before the identifier in an init-capture, and
1018 // after the identifier otherwise.
1019 bool InitCapture = InitKind != LambdaCaptureInitKind::NoInit;
1020 SourceLocation *ExpectedEllipsisLoc =
1021 !InitCapture ? &EllipsisLocs[2] :
1022 Kind == LCK_ByRef ? &EllipsisLocs[1] :
1023 &EllipsisLocs[0];
1024 EllipsisLoc = *ExpectedEllipsisLoc;
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001025
Richard Smithb2997f52019-05-21 20:10:50 +00001026 unsigned DiagID = 0;
1027 if (EllipsisLoc.isInvalid()) {
1028 DiagID = diag::err_lambda_capture_misplaced_ellipsis;
1029 for (SourceLocation Loc : EllipsisLocs) {
1030 if (Loc.isValid())
1031 EllipsisLoc = Loc;
1032 }
1033 } else {
1034 unsigned NumEllipses = std::accumulate(
1035 std::begin(EllipsisLocs), std::end(EllipsisLocs), 0,
1036 [](int N, SourceLocation Loc) { return N + Loc.isValid(); });
1037 if (NumEllipses > 1)
1038 DiagID = diag::err_lambda_capture_multiple_ellipses;
1039 }
1040 if (DiagID) {
1041 NonTentativeAction([&] {
1042 // Point the diagnostic at the first misplaced ellipsis.
1043 SourceLocation DiagLoc;
1044 for (SourceLocation &Loc : EllipsisLocs) {
1045 if (&Loc != ExpectedEllipsisLoc && Loc.isValid()) {
1046 DiagLoc = Loc;
1047 break;
1048 }
1049 }
1050 assert(DiagLoc.isValid() && "no location for diagnostic");
Faisal Vali5fb7c3c2013-12-05 01:40:41 +00001051
Richard Smithb2997f52019-05-21 20:10:50 +00001052 // Issue the diagnostic and produce fixits showing where the ellipsis
1053 // should have been written.
1054 auto &&D = Diag(DiagLoc, DiagID);
1055 if (DiagID == diag::err_lambda_capture_misplaced_ellipsis) {
1056 SourceLocation ExpectedLoc =
1057 InitCapture ? Loc
1058 : Lexer::getLocForEndOfToken(
1059 Loc, 0, PP.getSourceManager(), getLangOpts());
1060 D << InitCapture << FixItHint::CreateInsertion(ExpectedLoc, "...");
1061 }
1062 for (SourceLocation &Loc : EllipsisLocs) {
1063 if (&Loc != ExpectedEllipsisLoc && Loc.isValid())
1064 D << FixItHint::CreateRemoval(Loc);
1065 }
1066 });
1067 }
1068 }
1069
1070 // Process the init-capture initializers now rather than delaying until we
1071 // form the lambda-expression so that they can be handled in the context
1072 // enclosing the lambda-expression, rather than in the context of the
1073 // lambda-expression itself.
Richard Smith42b10572015-11-11 01:36:17 +00001074 ParsedType InitCaptureType;
Richard Smithb2997f52019-05-21 20:10:50 +00001075 if (Init.isUsable())
Volodymyr Sapsaib0f1aae2017-08-22 17:55:19 +00001076 Init = Actions.CorrectDelayedTyposInExpr(Init.get());
Richard Smithb2997f52019-05-21 20:10:50 +00001077 if (Init.isUsable()) {
1078 NonTentativeAction([&] {
Richard Smithe9585062019-05-20 18:01:54 +00001079 // Get the pointer and store it in an lvalue, so we can use it as an
1080 // out argument.
1081 Expr *InitExpr = Init.get();
1082 // This performs any lvalue-to-rvalue conversions if necessary, which
1083 // can affect what gets captured in the containing decl-context.
1084 InitCaptureType = Actions.actOnLambdaInitCaptureInitialization(
Richard Smithb2997f52019-05-21 20:10:50 +00001085 Loc, Kind == LCK_ByRef, EllipsisLoc, Id, InitKind, InitExpr);
Richard Smithe9585062019-05-20 18:01:54 +00001086 Init = InitExpr;
Richard Smithb2997f52019-05-21 20:10:50 +00001087 });
Faisal Vali5fb7c3c2013-12-05 01:40:41 +00001088 }
Alexander Shaposhnikov832f49b2018-07-16 07:23:47 +00001089
1090 SourceLocation LocEnd = PrevTokLocation;
1091
Richard Smith42b10572015-11-11 01:36:17 +00001092 Intro.addCapture(Kind, Loc, Id, EllipsisLoc, InitKind, Init,
Alexander Shaposhnikov832f49b2018-07-16 07:23:47 +00001093 InitCaptureType, SourceRange(LocStart, LocEnd));
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001094 }
1095
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001096 T.consumeClose();
1097 Intro.Range.setEnd(T.getCloseLocation());
Richard Smithe9585062019-05-20 18:01:54 +00001098 return false;
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001099}
1100
Faisal Valia734ab92016-03-26 16:11:37 +00001101static void
1102tryConsumeMutableOrConstexprToken(Parser &P, SourceLocation &MutableLoc,
1103 SourceLocation &ConstexprLoc,
1104 SourceLocation &DeclEndLoc) {
1105 assert(MutableLoc.isInvalid());
1106 assert(ConstexprLoc.isInvalid());
1107 // Consume constexpr-opt mutable-opt in any sequence, and set the DeclEndLoc
1108 // to the final of those locations. Emit an error if we have multiple
1109 // copies of those keywords and recover.
1110
1111 while (true) {
1112 switch (P.getCurToken().getKind()) {
1113 case tok::kw_mutable: {
1114 if (MutableLoc.isValid()) {
1115 P.Diag(P.getCurToken().getLocation(),
1116 diag::err_lambda_decl_specifier_repeated)
1117 << 0 << FixItHint::CreateRemoval(P.getCurToken().getLocation());
1118 }
1119 MutableLoc = P.ConsumeToken();
1120 DeclEndLoc = MutableLoc;
1121 break /*switch*/;
1122 }
1123 case tok::kw_constexpr:
1124 if (ConstexprLoc.isValid()) {
1125 P.Diag(P.getCurToken().getLocation(),
1126 diag::err_lambda_decl_specifier_repeated)
1127 << 1 << FixItHint::CreateRemoval(P.getCurToken().getLocation());
1128 }
1129 ConstexprLoc = P.ConsumeToken();
1130 DeclEndLoc = ConstexprLoc;
1131 break /*switch*/;
1132 default:
1133 return;
1134 }
1135 }
1136}
1137
1138static void
1139addConstexprToLambdaDeclSpecifier(Parser &P, SourceLocation ConstexprLoc,
1140 DeclSpec &DS) {
1141 if (ConstexprLoc.isValid()) {
Aaron Ballmanc351fba2017-12-04 20:27:34 +00001142 P.Diag(ConstexprLoc, !P.getLangOpts().CPlusPlus17
Richard Smithb115e5d2017-08-13 23:37:29 +00001143 ? diag::ext_constexpr_on_lambda_cxx17
Faisal Valia734ab92016-03-26 16:11:37 +00001144 : diag::warn_cxx14_compat_constexpr_on_lambda);
1145 const char *PrevSpec = nullptr;
1146 unsigned DiagID = 0;
1147 DS.SetConstexprSpec(ConstexprLoc, PrevSpec, DiagID);
1148 assert(PrevSpec == nullptr && DiagID == 0 &&
1149 "Constexpr cannot have been set previously!");
1150 }
1151}
1152
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001153/// ParseLambdaExpressionAfterIntroducer - Parse the rest of a lambda
1154/// expression.
1155ExprResult Parser::ParseLambdaExpressionAfterIntroducer(
1156 LambdaIntroducer &Intro) {
Eli Friedmanc7c97142012-01-04 02:40:39 +00001157 SourceLocation LambdaBeginLoc = Intro.Range.getBegin();
1158 Diag(LambdaBeginLoc, diag::warn_cxx98_compat_lambda);
1159
1160 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), LambdaBeginLoc,
1161 "lambda expression parsing");
1162
Fangrui Song6907ce22018-07-30 19:24:48 +00001163
Faisal Vali2b391ab2013-09-26 19:54:12 +00001164
Richard Smith21b3ab42013-05-09 21:36:41 +00001165 // FIXME: Call into Actions to add any init-capture declarations to the
1166 // scope while parsing the lambda-declarator and compound-statement.
1167
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001168 // Parse lambda-declarator[opt].
1169 DeclSpec DS(AttrFactory);
Faisal Vali421b2d12017-12-29 05:41:00 +00001170 Declarator D(DS, DeclaratorContext::LambdaExprContext);
Faisal Vali2b391ab2013-09-26 19:54:12 +00001171 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
Justin Lebar0fad0ba2016-09-30 17:14:48 +00001172 Actions.PushLambdaScope();
1173
1174 ParsedAttributes Attr(AttrFactory);
1175 SourceLocation DeclLoc = Tok.getLocation();
Justin Lebar0fad0ba2016-09-30 17:14:48 +00001176 if (getLangOpts().CUDA) {
1177 // In CUDA code, GNU attributes are allowed to appear immediately after the
1178 // "[...]", even if there is no "(...)" before the lambda body.
Justin Lebar0139a5d2016-09-30 19:55:48 +00001179 MaybeParseGNUAttributes(D);
Justin Lebar0fad0ba2016-09-30 17:14:48 +00001180 }
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001181
Justin Lebare46ea722016-09-30 19:55:55 +00001182 // Helper to emit a warning if we see a CUDA host/device/global attribute
1183 // after '(...)'. nvcc doesn't accept this.
1184 auto WarnIfHasCUDATargetAttr = [&] {
1185 if (getLangOpts().CUDA)
Erich Keanee891aa92018-07-13 15:07:47 +00001186 for (const ParsedAttr &A : Attr)
1187 if (A.getKind() == ParsedAttr::AT_CUDADevice ||
1188 A.getKind() == ParsedAttr::AT_CUDAHost ||
1189 A.getKind() == ParsedAttr::AT_CUDAGlobal)
Erich Keanec480f302018-07-12 21:09:05 +00001190 Diag(A.getLoc(), diag::warn_cuda_attr_lambda_position)
1191 << A.getName()->getName();
Justin Lebare46ea722016-09-30 19:55:55 +00001192 };
1193
Hamza Sood8205a812019-05-04 10:49:46 +00001194 // FIXME: Consider allowing this as an extension for GCC compatibiblity.
1195 const bool HasExplicitTemplateParams = Tok.is(tok::less);
1196 ParseScope TemplateParamScope(this, Scope::TemplateParamScope,
1197 /*EnteredScope=*/HasExplicitTemplateParams);
1198 if (HasExplicitTemplateParams) {
1199 Diag(Tok, getLangOpts().CPlusPlus2a
1200 ? diag::warn_cxx17_compat_lambda_template_parameter_list
1201 : diag::ext_lambda_template_parameter_list);
1202
1203 SmallVector<NamedDecl*, 4> TemplateParams;
1204 SourceLocation LAngleLoc, RAngleLoc;
1205 if (ParseTemplateParameters(CurTemplateDepthTracker.getDepth(),
1206 TemplateParams, LAngleLoc, RAngleLoc)) {
1207 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
1208 return ExprError();
1209 }
1210
1211 if (TemplateParams.empty()) {
1212 Diag(RAngleLoc,
1213 diag::err_lambda_template_parameter_list_empty);
1214 } else {
1215 Actions.ActOnLambdaExplicitTemplateParameterList(
1216 LAngleLoc, TemplateParams, RAngleLoc);
1217 ++CurTemplateDepthTracker;
1218 }
1219 }
1220
David Majnemere01c4662015-01-09 05:10:55 +00001221 TypeResult TrailingReturnType;
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001222 if (Tok.is(tok::l_paren)) {
1223 ParseScope PrototypeScope(this,
1224 Scope::FunctionPrototypeScope |
Richard Smithe233fbf2013-01-28 22:42:45 +00001225 Scope::FunctionDeclarationScope |
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001226 Scope::DeclScope);
1227
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001228 BalancedDelimiterTracker T(*this, tok::l_paren);
1229 T.consumeOpen();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001230 SourceLocation LParenLoc = T.getOpenLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001231
1232 // Parse parameter-declaration-clause.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001233 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001234 SourceLocation EllipsisLoc;
Fangrui Song6907ce22018-07-30 19:24:48 +00001235
Faisal Vali2b391ab2013-09-26 19:54:12 +00001236 if (Tok.isNot(tok::r_paren)) {
Hamza Sood8205a812019-05-04 10:49:46 +00001237 Actions.RecordParsingTemplateParameterDepth(
1238 CurTemplateDepthTracker.getOriginalDepth());
1239
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001240 ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc);
Hamza Sood8205a812019-05-04 10:49:46 +00001241
Fangrui Song6907ce22018-07-30 19:24:48 +00001242 // For a generic lambda, each 'auto' within the parameter declaration
Faisal Vali2b391ab2013-09-26 19:54:12 +00001243 // clause creates a template type parameter, so increment the depth.
Hamza Sood8205a812019-05-04 10:49:46 +00001244 // If we've parsed any explicit template parameters, then the depth will
1245 // have already been incremented. So we make sure that at most a single
1246 // depth level is added.
Fangrui Song6907ce22018-07-30 19:24:48 +00001247 if (Actions.getCurGenericLambda())
Hamza Sood8205a812019-05-04 10:49:46 +00001248 CurTemplateDepthTracker.setAddedDepth(1);
Faisal Vali2b391ab2013-09-26 19:54:12 +00001249 }
Hamza Sood8205a812019-05-04 10:49:46 +00001250
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001251 T.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001252 SourceLocation RParenLoc = T.getCloseLocation();
Justin Lebar0139a5d2016-09-30 19:55:48 +00001253 SourceLocation DeclEndLoc = RParenLoc;
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001254
Aaron Ballmane8d69b72014-03-12 00:01:07 +00001255 // GNU-style attributes must be parsed before the mutable specifier to be
1256 // compatible with GCC.
1257 MaybeParseGNUAttributes(Attr, &DeclEndLoc);
1258
David Majnemerbda86322015-02-04 08:22:46 +00001259 // MSVC-style attributes must be parsed before the mutable specifier to be
1260 // compatible with MSVC.
Aaron Ballman068aa512015-05-20 20:58:33 +00001261 MaybeParseMicrosoftDeclSpecs(Attr, &DeclEndLoc);
David Majnemerbda86322015-02-04 08:22:46 +00001262
Faisal Valia734ab92016-03-26 16:11:37 +00001263 // Parse mutable-opt and/or constexpr-opt, and update the DeclEndLoc.
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001264 SourceLocation MutableLoc;
Faisal Valia734ab92016-03-26 16:11:37 +00001265 SourceLocation ConstexprLoc;
1266 tryConsumeMutableOrConstexprToken(*this, MutableLoc, ConstexprLoc,
1267 DeclEndLoc);
Fangrui Song6907ce22018-07-30 19:24:48 +00001268
Faisal Valia734ab92016-03-26 16:11:37 +00001269 addConstexprToLambdaDeclSpecifier(*this, ConstexprLoc, DS);
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001270
1271 // Parse exception-specification[opt].
1272 ExceptionSpecificationType ESpecType = EST_None;
1273 SourceRange ESpecRange;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001274 SmallVector<ParsedType, 2> DynamicExceptions;
1275 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001276 ExprResult NoexceptExpr;
Richard Smith0b3a4622014-11-13 20:01:57 +00001277 CachedTokens *ExceptionSpecTokens;
1278 ESpecType = tryParseExceptionSpecification(/*Delayed=*/false,
1279 ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00001280 DynamicExceptions,
1281 DynamicExceptionRanges,
Richard Smith0b3a4622014-11-13 20:01:57 +00001282 NoexceptExpr,
1283 ExceptionSpecTokens);
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001284
1285 if (ESpecType != EST_None)
1286 DeclEndLoc = ESpecRange.getEnd();
1287
1288 // Parse attribute-specifier[opt].
Richard Smith89645bc2013-01-02 12:01:23 +00001289 MaybeParseCXX11Attributes(Attr, &DeclEndLoc);
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001290
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001291 SourceLocation FunLocalRangeEnd = DeclEndLoc;
1292
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001293 // Parse trailing-return-type[opt].
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001294 if (Tok.is(tok::arrow)) {
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001295 FunLocalRangeEnd = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001296 SourceRange Range;
Richard Smithe303e352018-02-02 22:24:54 +00001297 TrailingReturnType =
1298 ParseTrailingReturnType(Range, /*MayBeFollowedByDirectInit*/ false);
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001299 if (Range.getEnd().isValid())
1300 DeclEndLoc = Range.getEnd();
1301 }
1302
1303 PrototypeScope.Exit();
1304
Justin Lebare46ea722016-09-30 19:55:55 +00001305 WarnIfHasCUDATargetAttr();
1306
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001307 SourceLocation NoLoc;
Erich Keanec480f302018-07-12 21:09:05 +00001308 D.AddTypeInfo(DeclaratorChunk::getFunction(
1309 /*hasProto=*/true,
1310 /*isAmbiguous=*/false, LParenLoc, ParamInfo.data(),
1311 ParamInfo.size(), EllipsisLoc, RParenLoc,
Erich Keanec480f302018-07-12 21:09:05 +00001312 /*RefQualifierIsLValueRef=*/true,
Anastasia Stulovaa9bc4bd2019-01-09 11:25:09 +00001313 /*RefQualifierLoc=*/NoLoc, MutableLoc, ESpecType,
Erich Keanec480f302018-07-12 21:09:05 +00001314 ESpecRange, DynamicExceptions.data(),
1315 DynamicExceptionRanges.data(), DynamicExceptions.size(),
1316 NoexceptExpr.isUsable() ? NoexceptExpr.get() : nullptr,
1317 /*ExceptionSpecTokens*/ nullptr,
1318 /*DeclsInPrototype=*/None, LParenLoc, FunLocalRangeEnd, D,
1319 TrailingReturnType),
1320 std::move(Attr), DeclEndLoc);
Faisal Valia734ab92016-03-26 16:11:37 +00001321 } else if (Tok.isOneOf(tok::kw_mutable, tok::arrow, tok::kw___attribute,
1322 tok::kw_constexpr) ||
Aaron Ballmanb5c59f52014-03-11 13:03:15 +00001323 (Tok.is(tok::l_square) && NextToken().is(tok::l_square))) {
1324 // It's common to forget that one needs '()' before 'mutable', an attribute
1325 // specifier, or the result type. Deal with this.
1326 unsigned TokKind = 0;
1327 switch (Tok.getKind()) {
1328 case tok::kw_mutable: TokKind = 0; break;
1329 case tok::arrow: TokKind = 1; break;
Aaron Ballmane8d69b72014-03-12 00:01:07 +00001330 case tok::kw___attribute:
Aaron Ballmanb5c59f52014-03-11 13:03:15 +00001331 case tok::l_square: TokKind = 2; break;
Faisal Valia734ab92016-03-26 16:11:37 +00001332 case tok::kw_constexpr: TokKind = 3; break;
Aaron Ballmanb5c59f52014-03-11 13:03:15 +00001333 default: llvm_unreachable("Unknown token kind");
1334 }
1335
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001336 Diag(Tok, diag::err_lambda_missing_parens)
Aaron Ballmanb5c59f52014-03-11 13:03:15 +00001337 << TokKind
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001338 << FixItHint::CreateInsertion(Tok.getLocation(), "() ");
Justin Lebar0139a5d2016-09-30 19:55:48 +00001339 SourceLocation DeclEndLoc = DeclLoc;
Aaron Ballmane8d69b72014-03-12 00:01:07 +00001340
1341 // GNU-style attributes must be parsed before the mutable specifier to be
1342 // compatible with GCC.
Aaron Ballmane8d69b72014-03-12 00:01:07 +00001343 MaybeParseGNUAttributes(Attr, &DeclEndLoc);
1344
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001345 // Parse 'mutable', if it's there.
1346 SourceLocation MutableLoc;
1347 if (Tok.is(tok::kw_mutable)) {
1348 MutableLoc = ConsumeToken();
1349 DeclEndLoc = MutableLoc;
1350 }
Aaron Ballmanb5c59f52014-03-11 13:03:15 +00001351
1352 // Parse attribute-specifier[opt].
Aaron Ballmanb5c59f52014-03-11 13:03:15 +00001353 MaybeParseCXX11Attributes(Attr, &DeclEndLoc);
1354
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001355 // Parse the return type, if there is one.
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001356 if (Tok.is(tok::arrow)) {
1357 SourceRange Range;
Richard Smithe303e352018-02-02 22:24:54 +00001358 TrailingReturnType =
1359 ParseTrailingReturnType(Range, /*MayBeFollowedByDirectInit*/ false);
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001360 if (Range.getEnd().isValid())
David Majnemere01c4662015-01-09 05:10:55 +00001361 DeclEndLoc = Range.getEnd();
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001362 }
1363
Justin Lebare46ea722016-09-30 19:55:55 +00001364 WarnIfHasCUDATargetAttr();
1365
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001366 SourceLocation NoLoc;
Erich Keanec480f302018-07-12 21:09:05 +00001367 D.AddTypeInfo(DeclaratorChunk::getFunction(
1368 /*hasProto=*/true,
1369 /*isAmbiguous=*/false,
1370 /*LParenLoc=*/NoLoc,
1371 /*Params=*/nullptr,
1372 /*NumParams=*/0,
1373 /*EllipsisLoc=*/NoLoc,
1374 /*RParenLoc=*/NoLoc,
Erich Keanec480f302018-07-12 21:09:05 +00001375 /*RefQualifierIsLValueRef=*/true,
Anastasia Stulovaa9bc4bd2019-01-09 11:25:09 +00001376 /*RefQualifierLoc=*/NoLoc, MutableLoc, EST_None,
Erich Keanec480f302018-07-12 21:09:05 +00001377 /*ESpecRange=*/SourceRange(),
1378 /*Exceptions=*/nullptr,
1379 /*ExceptionRanges=*/nullptr,
1380 /*NumExceptions=*/0,
1381 /*NoexceptExpr=*/nullptr,
1382 /*ExceptionSpecTokens=*/nullptr,
1383 /*DeclsInPrototype=*/None, DeclLoc, DeclEndLoc, D,
1384 TrailingReturnType),
1385 std::move(Attr), DeclEndLoc);
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001386 }
1387
Eli Friedman4817cf72012-01-06 03:05:34 +00001388 // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
1389 // it.
Momchil Velikov57c681f2017-08-10 15:43:06 +00001390 unsigned ScopeFlags = Scope::BlockScope | Scope::FnScope | Scope::DeclScope |
1391 Scope::CompoundStmtScope;
Douglas Gregorb8389972012-02-21 22:51:27 +00001392 ParseScope BodyScope(this, ScopeFlags);
Eli Friedman4817cf72012-01-06 03:05:34 +00001393
Eli Friedman71c80552012-01-05 03:35:19 +00001394 Actions.ActOnStartOfLambdaDefinition(Intro, D, getCurScope());
1395
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001396 // Parse compound-statement.
Eli Friedmanc7c97142012-01-04 02:40:39 +00001397 if (!Tok.is(tok::l_brace)) {
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001398 Diag(Tok, diag::err_expected_lambda_body);
Eli Friedmanc7c97142012-01-04 02:40:39 +00001399 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
1400 return ExprError();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001401 }
1402
Eli Friedmanc7c97142012-01-04 02:40:39 +00001403 StmtResult Stmt(ParseCompoundStatementBody());
1404 BodyScope.Exit();
Hamza Sood8205a812019-05-04 10:49:46 +00001405 TemplateParamScope.Exit();
Eli Friedmanc7c97142012-01-04 02:40:39 +00001406
David Majnemere01c4662015-01-09 05:10:55 +00001407 if (!Stmt.isInvalid() && !TrailingReturnType.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001408 return Actions.ActOnLambdaExpr(LambdaBeginLoc, Stmt.get(), getCurScope());
David Majnemere01c4662015-01-09 05:10:55 +00001409
Eli Friedman898caf82012-01-04 02:46:53 +00001410 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
1411 return ExprError();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001412}
1413
Chris Lattner29375652006-12-04 18:06:35 +00001414/// ParseCXXCasts - This handles the various ways to cast expressions to another
1415/// type.
1416///
1417/// postfix-expression: [C++ 5.2p1]
1418/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
1419/// 'static_cast' '<' type-name '>' '(' expression ')'
1420/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
1421/// 'const_cast' '<' type-name '>' '(' expression ')'
1422///
John McCalldadc5752010-08-24 06:29:42 +00001423ExprResult Parser::ParseCXXCasts() {
Chris Lattner29375652006-12-04 18:06:35 +00001424 tok::TokenKind Kind = Tok.getKind();
Craig Topper161e4db2014-05-21 06:02:52 +00001425 const char *CastName = nullptr; // For error messages
Chris Lattner29375652006-12-04 18:06:35 +00001426
1427 switch (Kind) {
David Blaikieaa347f92011-09-23 20:26:49 +00001428 default: llvm_unreachable("Unknown C++ cast!");
Chris Lattner29375652006-12-04 18:06:35 +00001429 case tok::kw_const_cast: CastName = "const_cast"; break;
1430 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
1431 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
1432 case tok::kw_static_cast: CastName = "static_cast"; break;
1433 }
1434
1435 SourceLocation OpLoc = ConsumeToken();
1436 SourceLocation LAngleBracketLoc = Tok.getLocation();
1437
Richard Smith55858492011-04-14 21:45:45 +00001438 // Check for "<::" which is parsed as "[:". If found, fix token stream,
1439 // diagnose error, suggest fix, and recover parsing.
Richard Smith62e66302012-08-20 17:37:52 +00001440 if (Tok.is(tok::l_square) && Tok.getLength() == 2) {
1441 Token Next = NextToken();
1442 if (Next.is(tok::colon) && areTokensAdjacent(Tok, Next))
1443 FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
1444 }
Richard Smith55858492011-04-14 21:45:45 +00001445
Chris Lattner29375652006-12-04 18:06:35 +00001446 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redld65cea82008-12-11 22:51:44 +00001447 return ExprError();
Chris Lattner29375652006-12-04 18:06:35 +00001448
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +00001449 // Parse the common declaration-specifiers piece.
1450 DeclSpec DS(AttrFactory);
1451 ParseSpecifierQualifierList(DS);
1452
1453 // Parse the abstract-declarator, if present.
Faisal Vali421b2d12017-12-29 05:41:00 +00001454 Declarator DeclaratorInfo(DS, DeclaratorContext::TypeNameContext);
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +00001455 ParseDeclarator(DeclaratorInfo);
1456
Chris Lattner29375652006-12-04 18:06:35 +00001457 SourceLocation RAngleBracketLoc = Tok.getLocation();
1458
Alp Toker383d2c42014-01-01 03:08:43 +00001459 if (ExpectAndConsume(tok::greater))
Alp Tokerec543272013-12-24 09:48:30 +00001460 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << tok::less);
Chris Lattner29375652006-12-04 18:06:35 +00001461
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001462 BalancedDelimiterTracker T(*this, tok::l_paren);
Chris Lattner29375652006-12-04 18:06:35 +00001463
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001464 if (T.expectAndConsume(diag::err_expected_lparen_after, CastName))
Argyrios Kyrtzidis387a3342009-05-22 10:23:16 +00001465 return ExprError();
Chris Lattner29375652006-12-04 18:06:35 +00001466
John McCalldadc5752010-08-24 06:29:42 +00001467 ExprResult Result = ParseExpression();
Mike Stump11289f42009-09-09 15:08:12 +00001468
Argyrios Kyrtzidis387a3342009-05-22 10:23:16 +00001469 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001470 T.consumeClose();
Chris Lattner29375652006-12-04 18:06:35 +00001471
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +00001472 if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
Douglas Gregore200adc2008-10-27 19:41:14 +00001473 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +00001474 LAngleBracketLoc, DeclaratorInfo,
Douglas Gregor220cac52009-02-18 17:45:20 +00001475 RAngleBracketLoc,
Fangrui Song6907ce22018-07-30 19:24:48 +00001476 T.getOpenLocation(), Result.get(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001477 T.getCloseLocation());
Chris Lattner29375652006-12-04 18:06:35 +00001478
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001479 return Result;
Chris Lattner29375652006-12-04 18:06:35 +00001480}
Bill Wendling4073ed52007-02-13 01:51:42 +00001481
Sebastian Redlc4704762008-11-11 11:37:55 +00001482/// ParseCXXTypeid - This handles the C++ typeid expression.
1483///
1484/// postfix-expression: [C++ 5.2p1]
1485/// 'typeid' '(' expression ')'
1486/// 'typeid' '(' type-id ')'
1487///
John McCalldadc5752010-08-24 06:29:42 +00001488ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc4704762008-11-11 11:37:55 +00001489 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
1490
1491 SourceLocation OpLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001492 SourceLocation LParenLoc, RParenLoc;
1493 BalancedDelimiterTracker T(*this, tok::l_paren);
Sebastian Redlc4704762008-11-11 11:37:55 +00001494
1495 // typeid expressions are always parenthesized.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001496 if (T.expectAndConsume(diag::err_expected_lparen_after, "typeid"))
Sebastian Redld65cea82008-12-11 22:51:44 +00001497 return ExprError();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001498 LParenLoc = T.getOpenLocation();
Sebastian Redlc4704762008-11-11 11:37:55 +00001499
John McCalldadc5752010-08-24 06:29:42 +00001500 ExprResult Result;
Sebastian Redlc4704762008-11-11 11:37:55 +00001501
Richard Smith4f605af2012-08-18 00:55:03 +00001502 // C++0x [expr.typeid]p3:
1503 // When typeid is applied to an expression other than an lvalue of a
1504 // polymorphic class type [...] The expression is an unevaluated
1505 // operand (Clause 5).
1506 //
1507 // Note that we can't tell whether the expression is an lvalue of a
1508 // polymorphic class type until after we've parsed the expression; we
1509 // speculatively assume the subexpression is unevaluated, and fix it up
1510 // later.
1511 //
1512 // We enter the unevaluated context before trying to determine whether we
1513 // have a type-id, because the tentative parse logic will try to resolve
1514 // names, and must treat them as unevaluated.
Faisal Valid143a0c2017-04-01 21:30:49 +00001515 EnterExpressionEvaluationContext Unevaluated(
1516 Actions, Sema::ExpressionEvaluationContext::Unevaluated,
1517 Sema::ReuseLambdaContextDecl);
Richard Smith4f605af2012-08-18 00:55:03 +00001518
Sebastian Redlc4704762008-11-11 11:37:55 +00001519 if (isTypeIdInParens()) {
Douglas Gregor220cac52009-02-18 17:45:20 +00001520 TypeResult Ty = ParseTypeName();
Sebastian Redlc4704762008-11-11 11:37:55 +00001521
1522 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001523 T.consumeClose();
1524 RParenLoc = T.getCloseLocation();
Douglas Gregor4c7c1092010-09-08 23:14:30 +00001525 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +00001526 return ExprError();
Sebastian Redlc4704762008-11-11 11:37:55 +00001527
1528 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallba7bf592010-08-24 05:47:05 +00001529 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc4704762008-11-11 11:37:55 +00001530 } else {
1531 Result = ParseExpression();
1532
1533 // Match the ')'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001534 if (Result.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00001535 SkipUntil(tok::r_paren, StopAtSemi);
Sebastian Redlc4704762008-11-11 11:37:55 +00001536 else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001537 T.consumeClose();
1538 RParenLoc = T.getCloseLocation();
Douglas Gregor4c7c1092010-09-08 23:14:30 +00001539 if (RParenLoc.isInvalid())
1540 return ExprError();
Douglas Gregor1beec452011-03-12 01:48:56 +00001541
Sebastian Redlc4704762008-11-11 11:37:55 +00001542 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001543 Result.get(), RParenLoc);
Sebastian Redlc4704762008-11-11 11:37:55 +00001544 }
1545 }
1546
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001547 return Result;
Sebastian Redlc4704762008-11-11 11:37:55 +00001548}
1549
Francois Pichet9f4f2072010-09-08 12:20:18 +00001550/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
1551///
1552/// '__uuidof' '(' expression ')'
1553/// '__uuidof' '(' type-id ')'
1554///
1555ExprResult Parser::ParseCXXUuidof() {
1556 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
1557
1558 SourceLocation OpLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001559 BalancedDelimiterTracker T(*this, tok::l_paren);
Francois Pichet9f4f2072010-09-08 12:20:18 +00001560
1561 // __uuidof expressions are always parenthesized.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001562 if (T.expectAndConsume(diag::err_expected_lparen_after, "__uuidof"))
Francois Pichet9f4f2072010-09-08 12:20:18 +00001563 return ExprError();
1564
1565 ExprResult Result;
1566
1567 if (isTypeIdInParens()) {
1568 TypeResult Ty = ParseTypeName();
1569
1570 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001571 T.consumeClose();
Francois Pichet9f4f2072010-09-08 12:20:18 +00001572
1573 if (Ty.isInvalid())
1574 return ExprError();
1575
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001576 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), /*isType=*/true,
Fangrui Song6907ce22018-07-30 19:24:48 +00001577 Ty.get().getAsOpaquePtr(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001578 T.getCloseLocation());
Francois Pichet9f4f2072010-09-08 12:20:18 +00001579 } else {
Faisal Valid143a0c2017-04-01 21:30:49 +00001580 EnterExpressionEvaluationContext Unevaluated(
1581 Actions, Sema::ExpressionEvaluationContext::Unevaluated);
Francois Pichet9f4f2072010-09-08 12:20:18 +00001582 Result = ParseExpression();
1583
1584 // Match the ')'.
1585 if (Result.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00001586 SkipUntil(tok::r_paren, StopAtSemi);
Francois Pichet9f4f2072010-09-08 12:20:18 +00001587 else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001588 T.consumeClose();
Francois Pichet9f4f2072010-09-08 12:20:18 +00001589
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001590 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(),
1591 /*isType=*/false,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001592 Result.get(), T.getCloseLocation());
Francois Pichet9f4f2072010-09-08 12:20:18 +00001593 }
1594 }
1595
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001596 return Result;
Francois Pichet9f4f2072010-09-08 12:20:18 +00001597}
1598
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001599/// Parse a C++ pseudo-destructor expression after the base,
Douglas Gregore610ada2010-02-24 18:44:31 +00001600/// . or -> operator, and nested-name-specifier have already been
1601/// parsed.
1602///
1603/// postfix-expression: [C++ 5.2]
1604/// postfix-expression . pseudo-destructor-name
1605/// postfix-expression -> pseudo-destructor-name
1606///
Fangrui Song6907ce22018-07-30 19:24:48 +00001607/// pseudo-destructor-name:
1608/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
1609/// ::[opt] nested-name-specifier template simple-template-id ::
1610/// ~type-name
Douglas Gregore610ada2010-02-24 18:44:31 +00001611/// ::[opt] nested-name-specifier[opt] ~type-name
Fangrui Song6907ce22018-07-30 19:24:48 +00001612///
1613ExprResult
Craig Toppera2c51532014-10-30 05:30:05 +00001614Parser::ParseCXXPseudoDestructor(Expr *Base, SourceLocation OpLoc,
Douglas Gregore610ada2010-02-24 18:44:31 +00001615 tok::TokenKind OpKind,
1616 CXXScopeSpec &SS,
John McCallba7bf592010-08-24 05:47:05 +00001617 ParsedType ObjectType) {
Douglas Gregore610ada2010-02-24 18:44:31 +00001618 // We're parsing either a pseudo-destructor-name or a dependent
1619 // member access that has the same form as a
1620 // pseudo-destructor-name. We parse both in the same way and let
1621 // the action model sort them out.
1622 //
1623 // Note that the ::[opt] nested-name-specifier[opt] has already
1624 // been parsed, and if there was a simple-template-id, it has
1625 // been coalesced into a template-id annotation token.
1626 UnqualifiedId FirstTypeName;
1627 SourceLocation CCLoc;
1628 if (Tok.is(tok::identifier)) {
1629 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1630 ConsumeToken();
1631 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1632 CCLoc = ConsumeToken();
1633 } else if (Tok.is(tok::annot_template_id)) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001634 // FIXME: retrieve TemplateKWLoc from template-id annotation and
1635 // store it in the pseudo-dtor node (to be used when instantiating it).
Douglas Gregore610ada2010-02-24 18:44:31 +00001636 FirstTypeName.setTemplateId(
1637 (TemplateIdAnnotation *)Tok.getAnnotationValue());
Richard Smithaf3b3252017-05-18 19:21:48 +00001638 ConsumeAnnotationToken();
Douglas Gregore610ada2010-02-24 18:44:31 +00001639 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1640 CCLoc = ConsumeToken();
1641 } else {
Craig Topper161e4db2014-05-21 06:02:52 +00001642 FirstTypeName.setIdentifier(nullptr, SourceLocation());
Douglas Gregore610ada2010-02-24 18:44:31 +00001643 }
1644
1645 // Parse the tilde.
1646 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
1647 SourceLocation TildeLoc = ConsumeToken();
David Blaikie1d578782011-12-16 16:03:09 +00001648
1649 if (Tok.is(tok::kw_decltype) && !FirstTypeName.isValid() && SS.isEmpty()) {
1650 DeclSpec DS(AttrFactory);
Benjamin Kramer198e0832011-12-18 12:18:02 +00001651 ParseDecltypeSpecifier(DS);
Faisal Vali090da2d2018-01-01 18:23:28 +00001652 if (DS.getTypeSpecType() == TST_error)
David Blaikie1d578782011-12-16 16:03:09 +00001653 return ExprError();
David Majnemerced8bdf2015-02-25 17:36:15 +00001654 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, OpLoc, OpKind,
1655 TildeLoc, DS);
David Blaikie1d578782011-12-16 16:03:09 +00001656 }
1657
Douglas Gregore610ada2010-02-24 18:44:31 +00001658 if (!Tok.is(tok::identifier)) {
1659 Diag(Tok, diag::err_destructor_tilde_identifier);
1660 return ExprError();
1661 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001662
Douglas Gregore610ada2010-02-24 18:44:31 +00001663 // Parse the second type.
1664 UnqualifiedId SecondTypeName;
1665 IdentifierInfo *Name = Tok.getIdentifierInfo();
1666 SourceLocation NameLoc = ConsumeToken();
1667 SecondTypeName.setIdentifier(Name, NameLoc);
Fangrui Song6907ce22018-07-30 19:24:48 +00001668
Douglas Gregore610ada2010-02-24 18:44:31 +00001669 // If there is a '<', the second type name is a template-id. Parse
1670 // it as such.
1671 if (Tok.is(tok::less) &&
Abramo Bagnara7945c982012-01-27 09:46:47 +00001672 ParseUnqualifiedIdTemplateId(SS, SourceLocation(),
1673 Name, NameLoc,
1674 false, ObjectType, SecondTypeName,
1675 /*AssumeTemplateName=*/true))
Douglas Gregore610ada2010-02-24 18:44:31 +00001676 return ExprError();
1677
David Majnemerced8bdf2015-02-25 17:36:15 +00001678 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, OpLoc, OpKind,
1679 SS, FirstTypeName, CCLoc, TildeLoc,
1680 SecondTypeName);
Douglas Gregore610ada2010-02-24 18:44:31 +00001681}
1682
Bill Wendling4073ed52007-02-13 01:51:42 +00001683/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
1684///
1685/// boolean-literal: [C++ 2.13.5]
1686/// 'true'
1687/// 'false'
John McCalldadc5752010-08-24 06:29:42 +00001688ExprResult Parser::ParseCXXBoolLiteral() {
Bill Wendling4073ed52007-02-13 01:51:42 +00001689 tok::TokenKind Kind = Tok.getKind();
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001690 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Bill Wendling4073ed52007-02-13 01:51:42 +00001691}
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001692
1693/// ParseThrowExpression - This handles the C++ throw expression.
1694///
1695/// throw-expression: [C++ 15]
1696/// 'throw' assignment-expression[opt]
John McCalldadc5752010-08-24 06:29:42 +00001697ExprResult Parser::ParseThrowExpression() {
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001698 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001699 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redld65cea82008-12-11 22:51:44 +00001700
Chris Lattner65dd8432008-04-06 06:02:23 +00001701 // If the current token isn't the start of an assignment-expression,
1702 // then the expression is not present. This handles things like:
1703 // "C ? throw : (void)42", which is crazy but legal.
1704 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
1705 case tok::semi:
1706 case tok::r_paren:
1707 case tok::r_square:
1708 case tok::r_brace:
1709 case tok::colon:
1710 case tok::comma:
Craig Topper161e4db2014-05-21 06:02:52 +00001711 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, nullptr);
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001712
Chris Lattner65dd8432008-04-06 06:02:23 +00001713 default:
John McCalldadc5752010-08-24 06:29:42 +00001714 ExprResult Expr(ParseAssignmentExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001715 if (Expr.isInvalid()) return Expr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001716 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.get());
Chris Lattner65dd8432008-04-06 06:02:23 +00001717 }
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001718}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001719
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001720/// Parse the C++ Coroutines co_yield expression.
Richard Smith0e304ea2015-10-22 04:46:14 +00001721///
1722/// co_yield-expression:
1723/// 'co_yield' assignment-expression[opt]
1724ExprResult Parser::ParseCoyieldExpression() {
1725 assert(Tok.is(tok::kw_co_yield) && "Not co_yield!");
1726
1727 SourceLocation Loc = ConsumeToken();
Richard Smithae3d1472015-11-20 22:47:10 +00001728 ExprResult Expr = Tok.is(tok::l_brace) ? ParseBraceInitializer()
1729 : ParseAssignmentExpression();
Richard Smithcfd53b42015-10-22 06:13:50 +00001730 if (!Expr.isInvalid())
Richard Smith9f690bd2015-10-27 06:02:45 +00001731 Expr = Actions.ActOnCoyieldExpr(getCurScope(), Loc, Expr.get());
Richard Smith0e304ea2015-10-22 04:46:14 +00001732 return Expr;
1733}
1734
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001735/// ParseCXXThis - This handles the C++ 'this' pointer.
1736///
1737/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
1738/// a non-lvalue expression whose value is the address of the object for which
1739/// the function is called.
John McCalldadc5752010-08-24 06:29:42 +00001740ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001741 assert(Tok.is(tok::kw_this) && "Not 'this'!");
1742 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001743 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001744}
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001745
1746/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1747/// Can be interpreted either as function-style casting ("int(x)")
1748/// or class type construction ("ClassType(x,y,z)")
1749/// or creation of a value-initialized type ("int()").
Sebastian Redl3da34892011-06-05 12:23:16 +00001750/// See [C++ 5.2.3].
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001751///
1752/// postfix-expression: [C++ 5.2p1]
Sebastian Redl3da34892011-06-05 12:23:16 +00001753/// simple-type-specifier '(' expression-list[opt] ')'
1754/// [C++0x] simple-type-specifier braced-init-list
1755/// typename-specifier '(' expression-list[opt] ')'
1756/// [C++0x] typename-specifier braced-init-list
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001757///
Richard Smith600b5262017-01-26 20:40:47 +00001758/// In C++1z onwards, the type specifier can also be a template-name.
John McCalldadc5752010-08-24 06:29:42 +00001759ExprResult
Sebastian Redld65cea82008-12-11 22:51:44 +00001760Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Faisal Vali421b2d12017-12-29 05:41:00 +00001761 Declarator DeclaratorInfo(DS, DeclaratorContext::FunctionalCastContext);
John McCallba7bf592010-08-24 05:47:05 +00001762 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001763
Sebastian Redl3da34892011-06-05 12:23:16 +00001764 assert((Tok.is(tok::l_paren) ||
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001765 (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)))
Sebastian Redl3da34892011-06-05 12:23:16 +00001766 && "Expected '(' or '{'!");
Douglas Gregor94a32472011-01-11 00:33:19 +00001767
Sebastian Redl3da34892011-06-05 12:23:16 +00001768 if (Tok.is(tok::l_brace)) {
Sebastian Redld74dd492012-02-12 18:41:05 +00001769 ExprResult Init = ParseBraceInitializer();
1770 if (Init.isInvalid())
1771 return Init;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001772 Expr *InitList = Init.get();
Vedant Kumara14a1f92018-01-17 18:53:51 +00001773 return Actions.ActOnCXXTypeConstructExpr(
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001774 TypeRep, InitList->getBeginLoc(), MultiExprArg(&InitList, 1),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00001775 InitList->getEndLoc(), /*ListInitialization=*/true);
Sebastian Redl3da34892011-06-05 12:23:16 +00001776 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001777 BalancedDelimiterTracker T(*this, tok::l_paren);
1778 T.consumeOpen();
Sebastian Redl3da34892011-06-05 12:23:16 +00001779
Ilya Biryukov4f9543b2019-01-31 20:20:32 +00001780 PreferredType.enterTypeCast(Tok.getLocation(), TypeRep.get());
1781
Benjamin Kramerf0623432012-08-23 22:51:59 +00001782 ExprVector Exprs;
Sebastian Redl3da34892011-06-05 12:23:16 +00001783 CommaLocsTy CommaLocs;
1784
Ilya Biryukovff2a9972019-02-26 11:01:50 +00001785 auto RunSignatureHelp = [&]() {
1786 QualType PreferredType = Actions.ProduceConstructorSignatureHelp(
1787 getCurScope(), TypeRep.get()->getCanonicalTypeInternal(),
1788 DS.getEndLoc(), Exprs, T.getOpenLocation());
1789 CalledSignatureHelp = true;
1790 return PreferredType;
1791 };
1792
Sebastian Redl3da34892011-06-05 12:23:16 +00001793 if (Tok.isNot(tok::r_paren)) {
Francisco Lopes da Silva975a9f62015-01-21 16:24:11 +00001794 if (ParseExpressionList(Exprs, CommaLocs, [&] {
Ilya Biryukovff2a9972019-02-26 11:01:50 +00001795 PreferredType.enterFunctionArgument(Tok.getLocation(),
1796 RunSignatureHelp);
Stephen Kelly1c301dc2018-08-09 21:09:38 +00001797 })) {
Ilya Biryukovff2a9972019-02-26 11:01:50 +00001798 if (PP.isCodeCompletionReached() && !CalledSignatureHelp)
1799 RunSignatureHelp();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001800 SkipUntil(tok::r_paren, StopAtSemi);
Sebastian Redl3da34892011-06-05 12:23:16 +00001801 return ExprError();
1802 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001803 }
Sebastian Redl3da34892011-06-05 12:23:16 +00001804
1805 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001806 T.consumeClose();
Sebastian Redl3da34892011-06-05 12:23:16 +00001807
1808 // TypeRep could be null, if it references an invalid typedef.
1809 if (!TypeRep)
1810 return ExprError();
1811
1812 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1813 "Unexpected number of commas!");
Vedant Kumara14a1f92018-01-17 18:53:51 +00001814 return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(),
1815 Exprs, T.getCloseLocation(),
1816 /*ListInitialization=*/false);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001817 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001818}
1819
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001820/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001821///
1822/// condition:
1823/// expression
1824/// type-specifier-seq declarator '=' assignment-expression
Richard Smith2a15b742012-02-22 06:49:09 +00001825/// [C++11] type-specifier-seq declarator '=' initializer-clause
1826/// [C++11] type-specifier-seq declarator braced-init-list
Zhihao Yuanc81f4532017-12-07 07:03:15 +00001827/// [Clang] type-specifier-seq ref-qualifier[opt] '[' identifier-list ']'
1828/// brace-or-equal-initializer
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001829/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
1830/// '=' assignment-expression
1831///
Richard Smithc7a05a92016-06-29 21:17:59 +00001832/// In C++1z, a condition may in some contexts be preceded by an
1833/// optional init-statement. This function will parse that too.
1834///
1835/// \param InitStmt If non-null, an init-statement is permitted, and if present
1836/// will be parsed and stored here.
1837///
Douglas Gregore60e41a2010-05-06 17:25:47 +00001838/// \param Loc The location of the start of the statement that requires this
1839/// condition, e.g., the "for" in a for loop.
1840///
Richard Smith8baa5002018-09-28 18:44:09 +00001841/// \param FRI If non-null, a for range declaration is permitted, and if
1842/// present will be parsed and stored here, and a null result will be returned.
1843///
Richard Smith03a4aa32016-06-23 19:02:52 +00001844/// \returns The parsed condition.
Richard Smithc7a05a92016-06-29 21:17:59 +00001845Sema::ConditionResult Parser::ParseCXXCondition(StmtResult *InitStmt,
1846 SourceLocation Loc,
Richard Smith8baa5002018-09-28 18:44:09 +00001847 Sema::ConditionKind CK,
1848 ForRangeInfo *FRI) {
Richard Smithbf5bcf22018-06-26 23:20:26 +00001849 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Ilya Biryukov4f9543b2019-01-31 20:20:32 +00001850 PreferredType.enterCondition(Actions, Tok.getLocation());
Richard Smithbf5bcf22018-06-26 23:20:26 +00001851
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001852 if (Tok.is(tok::code_completion)) {
John McCallfaf5fb42010-08-26 23:41:50 +00001853 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001854 cutOffParsing();
Richard Smith03a4aa32016-06-23 19:02:52 +00001855 return Sema::ConditionError();
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001856 }
1857
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001858 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00001859 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001860
Zhihao Yuan52b5bf82018-03-17 21:42:10 +00001861 const auto WarnOnInit = [this, &CK] {
1862 Diag(Tok.getLocation(), getLangOpts().CPlusPlus17
1863 ? diag::warn_cxx14_compat_init_statement
1864 : diag::ext_init_statement)
1865 << (CK == Sema::ConditionKind::Switch);
1866 };
1867
Richard Smithc7a05a92016-06-29 21:17:59 +00001868 // Determine what kind of thing we have.
Richard Smith8baa5002018-09-28 18:44:09 +00001869 switch (isCXXConditionDeclarationOrInitStatement(InitStmt, FRI)) {
Richard Smithc7a05a92016-06-29 21:17:59 +00001870 case ConditionOrInitStatement::Expression: {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001871 ProhibitAttributes(attrs);
1872
Zhihao Yuan52b5bf82018-03-17 21:42:10 +00001873 // We can have an empty expression here.
1874 // if (; true);
1875 if (InitStmt && Tok.is(tok::semi)) {
1876 WarnOnInit();
Roman Lebedev377748f2018-11-20 18:59:05 +00001877 SourceLocation SemiLoc = Tok.getLocation();
1878 if (!Tok.hasLeadingEmptyMacro() && !SemiLoc.isMacroID()) {
1879 Diag(SemiLoc, diag::warn_empty_init_statement)
1880 << (CK == Sema::ConditionKind::Switch)
1881 << FixItHint::CreateRemoval(SemiLoc);
1882 }
1883 ConsumeToken();
Zhihao Yuan52b5bf82018-03-17 21:42:10 +00001884 *InitStmt = Actions.ActOnNullStmt(SemiLoc);
1885 return ParseCXXCondition(nullptr, Loc, CK);
1886 }
1887
Douglas Gregore60e41a2010-05-06 17:25:47 +00001888 // Parse the expression.
Richard Smith03a4aa32016-06-23 19:02:52 +00001889 ExprResult Expr = ParseExpression(); // expression
1890 if (Expr.isInvalid())
1891 return Sema::ConditionError();
Douglas Gregore60e41a2010-05-06 17:25:47 +00001892
Richard Smithc7a05a92016-06-29 21:17:59 +00001893 if (InitStmt && Tok.is(tok::semi)) {
Zhihao Yuan52b5bf82018-03-17 21:42:10 +00001894 WarnOnInit();
Richard Smithc7a05a92016-06-29 21:17:59 +00001895 *InitStmt = Actions.ActOnExprStmt(Expr.get());
1896 ConsumeToken();
1897 return ParseCXXCondition(nullptr, Loc, CK);
1898 }
1899
Richard Smith03a4aa32016-06-23 19:02:52 +00001900 return Actions.ActOnCondition(getCurScope(), Loc, Expr.get(), CK);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001901 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001902
Richard Smithc7a05a92016-06-29 21:17:59 +00001903 case ConditionOrInitStatement::InitStmtDecl: {
Zhihao Yuan52b5bf82018-03-17 21:42:10 +00001904 WarnOnInit();
Richard Smithc7a05a92016-06-29 21:17:59 +00001905 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Faisal Vali421b2d12017-12-29 05:41:00 +00001906 DeclGroupPtrTy DG =
1907 ParseSimpleDeclaration(DeclaratorContext::InitStmtContext, DeclEnd,
1908 attrs, /*RequireSemi=*/true);
Richard Smithc7a05a92016-06-29 21:17:59 +00001909 *InitStmt = Actions.ActOnDeclStmt(DG, DeclStart, DeclEnd);
1910 return ParseCXXCondition(nullptr, Loc, CK);
1911 }
1912
Richard Smith8baa5002018-09-28 18:44:09 +00001913 case ConditionOrInitStatement::ForRangeDecl: {
1914 assert(FRI && "should not parse a for range declaration here");
1915 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
1916 DeclGroupPtrTy DG = ParseSimpleDeclaration(
1917 DeclaratorContext::ForContext, DeclEnd, attrs, false, FRI);
1918 FRI->LoopVar = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
1919 return Sema::ConditionResult();
1920 }
1921
Richard Smithc7a05a92016-06-29 21:17:59 +00001922 case ConditionOrInitStatement::ConditionDecl:
1923 case ConditionOrInitStatement::Error:
1924 break;
1925 }
1926
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001927 // type-specifier-seq
John McCall084e83d2011-03-24 11:26:52 +00001928 DeclSpec DS(AttrFactory);
Richard Smith54ecd982013-02-20 19:22:51 +00001929 DS.takeAttributesFrom(attrs);
Faisal Vali7db85c52017-12-31 00:06:40 +00001930 ParseSpecifierQualifierList(DS, AS_none, DeclSpecContext::DSC_condition);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001931
1932 // declarator
Faisal Vali421b2d12017-12-29 05:41:00 +00001933 Declarator DeclaratorInfo(DS, DeclaratorContext::ConditionContext);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001934 ParseDeclarator(DeclaratorInfo);
1935
1936 // simple-asm-expr[opt]
1937 if (Tok.is(tok::kw_asm)) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001938 SourceLocation Loc;
John McCalldadc5752010-08-24 06:29:42 +00001939 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001940 if (AsmLabel.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001941 SkipUntil(tok::semi, StopAtSemi);
Richard Smith03a4aa32016-06-23 19:02:52 +00001942 return Sema::ConditionError();
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001943 }
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001944 DeclaratorInfo.setAsmLabel(AsmLabel.get());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001945 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001946 }
1947
1948 // If attributes are present, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001949 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001950
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001951 // Type-check the declaration itself.
Fangrui Song6907ce22018-07-30 19:24:48 +00001952 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall53fa7142010-12-24 02:08:15 +00001953 DeclaratorInfo);
Richard Smith03a4aa32016-06-23 19:02:52 +00001954 if (Dcl.isInvalid())
1955 return Sema::ConditionError();
1956 Decl *DeclOut = Dcl.get();
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +00001957
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001958 // '=' assignment-expression
Richard Trieuc64d3232012-01-18 22:54:52 +00001959 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Smith2a15b742012-02-22 06:49:09 +00001960 bool CopyInitialization = isTokenEqualOrEqualTypo();
1961 if (CopyInitialization)
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001962 ConsumeToken();
Richard Smith2a15b742012-02-22 06:49:09 +00001963
1964 ExprResult InitExpr = ExprError();
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001965 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith2a15b742012-02-22 06:49:09 +00001966 Diag(Tok.getLocation(),
1967 diag::warn_cxx98_compat_generalized_initializer_lists);
1968 InitExpr = ParseBraceInitializer();
1969 } else if (CopyInitialization) {
Ilya Biryukov4f9543b2019-01-31 20:20:32 +00001970 PreferredType.enterVariableInit(Tok.getLocation(), DeclOut);
Richard Smith2a15b742012-02-22 06:49:09 +00001971 InitExpr = ParseAssignmentExpression();
1972 } else if (Tok.is(tok::l_paren)) {
1973 // This was probably an attempt to initialize the variable.
1974 SourceLocation LParen = ConsumeParen(), RParen = LParen;
Alexey Bataevee6507d2013-11-18 08:17:37 +00001975 if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch))
Richard Smith2a15b742012-02-22 06:49:09 +00001976 RParen = ConsumeParen();
Richard Smith03a4aa32016-06-23 19:02:52 +00001977 Diag(DeclOut->getLocation(),
Richard Smith2a15b742012-02-22 06:49:09 +00001978 diag::err_expected_init_in_condition_lparen)
1979 << SourceRange(LParen, RParen);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001980 } else {
Richard Smith03a4aa32016-06-23 19:02:52 +00001981 Diag(DeclOut->getLocation(), diag::err_expected_init_in_condition);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001982 }
Richard Smith2a15b742012-02-22 06:49:09 +00001983
1984 if (!InitExpr.isInvalid())
Richard Smith3beb7c62017-01-12 02:27:38 +00001985 Actions.AddInitializerToDecl(DeclOut, InitExpr.get(), !CopyInitialization);
Richard Smith27d807c2013-04-30 13:56:41 +00001986 else
1987 Actions.ActOnInitializerError(DeclOut);
Richard Smith2a15b742012-02-22 06:49:09 +00001988
Richard Smithb2bc2e62011-02-21 20:05:19 +00001989 Actions.FinalizeDeclaration(DeclOut);
Richard Smith03a4aa32016-06-23 19:02:52 +00001990 return Actions.ActOnConditionVariable(DeclOut, Loc, CK);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001991}
1992
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001993/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1994/// This should only be called when the current token is known to be part of
1995/// simple-type-specifier.
1996///
1997/// simple-type-specifier:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001998/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001999/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
2000/// char
2001/// wchar_t
2002/// bool
2003/// short
2004/// int
2005/// long
2006/// signed
2007/// unsigned
2008/// float
2009/// double
2010/// void
2011/// [GNU] typeof-specifier
2012/// [C++0x] auto [TODO]
2013///
2014/// type-name:
2015/// class-name
2016/// enum-name
2017/// typedef-name
2018///
2019void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
2020 DS.SetRangeStart(Tok.getLocation());
2021 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +00002022 unsigned DiagID;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00002023 SourceLocation Loc = Tok.getLocation();
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002024 const clang::PrintingPolicy &Policy =
2025 Actions.getASTContext().getPrintingPolicy();
Mike Stump11289f42009-09-09 15:08:12 +00002026
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00002027 switch (Tok.getKind()) {
Chris Lattner45ddec32009-01-05 00:13:00 +00002028 case tok::identifier: // foo::bar
2029 case tok::coloncolon: // ::foo::bar
David Blaikie83d382b2011-09-23 05:06:16 +00002030 llvm_unreachable("Annotation token should already be formed!");
Mike Stump11289f42009-09-09 15:08:12 +00002031 default:
David Blaikie83d382b2011-09-23 05:06:16 +00002032 llvm_unreachable("Not a simple-type-specifier token!");
Chris Lattner45ddec32009-01-05 00:13:00 +00002033
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00002034 // type-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002035 case tok::annot_typename: {
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002036 if (getTypeAnnotation(Tok))
2037 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002038 getTypeAnnotation(Tok), Policy);
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002039 else
2040 DS.SetTypeSpecError();
Fangrui Song6907ce22018-07-30 19:24:48 +00002041
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002042 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
Richard Smithaf3b3252017-05-18 19:21:48 +00002043 ConsumeAnnotationToken();
Fangrui Song6907ce22018-07-30 19:24:48 +00002044
Craig Topper25122412015-11-15 03:32:11 +00002045 DS.Finish(Actions, Policy);
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002046 return;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00002047 }
Mike Stump11289f42009-09-09 15:08:12 +00002048
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00002049 // builtin types
2050 case tok::kw_short:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002051 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00002052 break;
2053 case tok::kw_long:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002054 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00002055 break;
Francois Pichet84133e42011-04-28 01:59:37 +00002056 case tok::kw___int64:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002057 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID, Policy);
Francois Pichet84133e42011-04-28 01:59:37 +00002058 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00002059 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00002060 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00002061 break;
2062 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00002063 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00002064 break;
2065 case tok::kw_void:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002066 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00002067 break;
2068 case tok::kw_char:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002069 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00002070 break;
2071 case tok::kw_int:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002072 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00002073 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00002074 case tok::kw___int128:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002075 DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, DiagID, Policy);
Richard Smithf016bbc2012-04-04 06:24:32 +00002076 break;
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00002077 case tok::kw_half:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002078 DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID, Policy);
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00002079 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00002080 case tok::kw_float:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002081 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00002082 break;
2083 case tok::kw_double:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002084 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00002085 break;
Sjoerd Meijercc623ad2017-09-08 15:15:00 +00002086 case tok::kw__Float16:
2087 DS.SetTypeSpecType(DeclSpec::TST_float16, Loc, PrevSpec, DiagID, Policy);
2088 break;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00002089 case tok::kw___float128:
2090 DS.SetTypeSpecType(DeclSpec::TST_float128, Loc, PrevSpec, DiagID, Policy);
2091 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00002092 case tok::kw_wchar_t:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002093 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00002094 break;
Richard Smith3a8244d2018-05-01 05:02:45 +00002095 case tok::kw_char8_t:
2096 DS.SetTypeSpecType(DeclSpec::TST_char8, Loc, PrevSpec, DiagID, Policy);
2097 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002098 case tok::kw_char16_t:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002099 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID, Policy);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002100 break;
2101 case tok::kw_char32_t:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002102 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID, Policy);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002103 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00002104 case tok::kw_bool:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002105 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00002106 break;
Anastasia Stulova2c4730d2019-02-15 12:07:57 +00002107#define GENERIC_IMAGE_TYPE(ImgType, Id) \
2108 case tok::kw_##ImgType##_t: \
2109 DS.SetTypeSpecType(DeclSpec::TST_##ImgType##_t, Loc, PrevSpec, DiagID, \
2110 Policy); \
2111 break;
2112#include "clang/Basic/OpenCLImageTypes.def"
2113
David Blaikie25896afb2012-01-24 05:47:35 +00002114 case tok::annot_decltype:
2115 case tok::kw_decltype:
2116 DS.SetRangeEnd(ParseDecltypeSpecifier(DS));
Craig Topper25122412015-11-15 03:32:11 +00002117 return DS.Finish(Actions, Policy);
Mike Stump11289f42009-09-09 15:08:12 +00002118
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00002119 // GNU typeof support.
2120 case tok::kw_typeof:
2121 ParseTypeofSpecifier(DS);
Craig Topper25122412015-11-15 03:32:11 +00002122 DS.Finish(Actions, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00002123 return;
2124 }
Richard Smithaf3b3252017-05-18 19:21:48 +00002125 ConsumeAnyToken();
2126 DS.SetRangeEnd(PrevTokLocation);
Craig Topper25122412015-11-15 03:32:11 +00002127 DS.Finish(Actions, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00002128}
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002129
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002130/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
2131/// [dcl.name]), which is a non-empty sequence of type-specifiers,
2132/// e.g., "const short int". Note that the DeclSpec is *not* finished
2133/// by parsing the type-specifier-seq, because these sequences are
2134/// typically followed by some form of declarator. Returns true and
2135/// emits diagnostics if this is not a type-specifier-seq, false
2136/// otherwise.
2137///
2138/// type-specifier-seq: [C++ 8.1]
2139/// type-specifier type-specifier-seq[opt]
2140///
2141bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
Faisal Vali7db85c52017-12-31 00:06:40 +00002142 ParseSpecifierQualifierList(DS, AS_none, DeclSpecContext::DSC_type_specifier);
Craig Topper25122412015-11-15 03:32:11 +00002143 DS.Finish(Actions, Actions.getASTContext().getPrintingPolicy());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002144 return false;
2145}
2146
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002147/// Finish parsing a C++ unqualified-id that is a template-id of
Fangrui Song6907ce22018-07-30 19:24:48 +00002148/// some form.
Douglas Gregor7861a802009-11-03 01:35:08 +00002149///
2150/// This routine is invoked when a '<' is encountered after an identifier or
2151/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
2152/// whether the unqualified-id is actually a template-id. This routine will
2153/// then parse the template arguments and form the appropriate template-id to
2154/// return to the caller.
2155///
2156/// \param SS the nested-name-specifier that precedes this template-id, if
2157/// we're actually parsing a qualified-id.
2158///
2159/// \param Name for constructor and destructor names, this is the actual
2160/// identifier that may be a template-name.
2161///
Fangrui Song6907ce22018-07-30 19:24:48 +00002162/// \param NameLoc the location of the class-name in a constructor or
Douglas Gregor7861a802009-11-03 01:35:08 +00002163/// destructor.
2164///
Fangrui Song6907ce22018-07-30 19:24:48 +00002165/// \param EnteringContext whether we're entering the scope of the
Douglas Gregor7861a802009-11-03 01:35:08 +00002166/// nested-name-specifier.
2167///
Douglas Gregor127ea592009-11-03 21:24:04 +00002168/// \param ObjectType if this unqualified-id occurs within a member access
2169/// expression, the type of the base object whose member is being accessed.
2170///
Douglas Gregor7861a802009-11-03 01:35:08 +00002171/// \param Id as input, describes the template-name or operator-function-id
2172/// that precedes the '<'. If template arguments were parsed successfully,
2173/// will be updated with the template-id.
Fangrui Song6907ce22018-07-30 19:24:48 +00002174///
Douglas Gregore610ada2010-02-24 18:44:31 +00002175/// \param AssumeTemplateId When true, this routine will assume that the name
Fangrui Song6907ce22018-07-30 19:24:48 +00002176/// refers to a template without performing name lookup to verify.
Douglas Gregore610ada2010-02-24 18:44:31 +00002177///
Douglas Gregor7861a802009-11-03 01:35:08 +00002178/// \returns true if a parse error occurred, false otherwise.
2179bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
Abramo Bagnara7945c982012-01-27 09:46:47 +00002180 SourceLocation TemplateKWLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00002181 IdentifierInfo *Name,
2182 SourceLocation NameLoc,
2183 bool EnteringContext,
John McCallba7bf592010-08-24 05:47:05 +00002184 ParsedType ObjectType,
Douglas Gregore610ada2010-02-24 18:44:31 +00002185 UnqualifiedId &Id,
Abramo Bagnara7945c982012-01-27 09:46:47 +00002186 bool AssumeTemplateId) {
Richard Smithc08b6932018-04-27 02:00:13 +00002187 assert(Tok.is(tok::less) && "Expected '<' to finish parsing a template-id");
2188
Douglas Gregor7861a802009-11-03 01:35:08 +00002189 TemplateTy Template;
2190 TemplateNameKind TNK = TNK_Non_template;
2191 switch (Id.getKind()) {
Faisal Vali2ab8c152017-12-30 04:15:27 +00002192 case UnqualifiedIdKind::IK_Identifier:
2193 case UnqualifiedIdKind::IK_OperatorFunctionId:
2194 case UnqualifiedIdKind::IK_LiteralOperatorId:
Douglas Gregore610ada2010-02-24 18:44:31 +00002195 if (AssumeTemplateId) {
Richard Smithfd3dae02017-01-20 00:20:39 +00002196 // We defer the injected-class-name checks until we've found whether
2197 // this template-id is used to form a nested-name-specifier or not.
2198 TNK = Actions.ActOnDependentTemplateName(
2199 getCurScope(), SS, TemplateKWLoc, Id, ObjectType, EnteringContext,
2200 Template, /*AllowInjectedClassName*/ true);
Douglas Gregorbb119652010-06-16 23:00:59 +00002201 if (TNK == TNK_Non_template)
2202 return true;
Douglas Gregor786123d2010-05-21 23:18:07 +00002203 } else {
2204 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00002205 TNK = Actions.isTemplateName(getCurScope(), SS,
2206 TemplateKWLoc.isValid(), Id,
2207 ObjectType, EnteringContext, Template,
Douglas Gregor786123d2010-05-21 23:18:07 +00002208 MemberOfUnknownSpecialization);
Richard Smithb23c5e82019-05-09 03:31:27 +00002209 // If lookup found nothing but we're assuming that this is a template
2210 // name, double-check that makes sense syntactically before committing
2211 // to it.
2212 if (TNK == TNK_Undeclared_template &&
2213 isTemplateArgumentList(0) == TPResult::False)
2214 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002215
Douglas Gregor786123d2010-05-21 23:18:07 +00002216 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
Richard Smithb23c5e82019-05-09 03:31:27 +00002217 ObjectType && isTemplateArgumentList(0) == TPResult::True) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002218 // We have something like t->getAs<T>(), where getAs is a
Douglas Gregor786123d2010-05-21 23:18:07 +00002219 // member of an unknown specialization. However, this will only
2220 // parse correctly as a template, so suggest the keyword 'template'
2221 // before 'getAs' and treat this as a dependent template name.
2222 std::string Name;
Faisal Vali2ab8c152017-12-30 04:15:27 +00002223 if (Id.getKind() == UnqualifiedIdKind::IK_Identifier)
Douglas Gregor786123d2010-05-21 23:18:07 +00002224 Name = Id.Identifier->getName();
2225 else {
2226 Name = "operator ";
Faisal Vali2ab8c152017-12-30 04:15:27 +00002227 if (Id.getKind() == UnqualifiedIdKind::IK_OperatorFunctionId)
Douglas Gregor786123d2010-05-21 23:18:07 +00002228 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
2229 else
2230 Name += Id.Identifier->getName();
2231 }
2232 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
2233 << Name
2234 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Richard Smithfd3dae02017-01-20 00:20:39 +00002235 TNK = Actions.ActOnDependentTemplateName(
2236 getCurScope(), SS, TemplateKWLoc, Id, ObjectType, EnteringContext,
2237 Template, /*AllowInjectedClassName*/ true);
Douglas Gregorbb119652010-06-16 23:00:59 +00002238 if (TNK == TNK_Non_template)
Fangrui Song6907ce22018-07-30 19:24:48 +00002239 return true;
Douglas Gregor786123d2010-05-21 23:18:07 +00002240 }
2241 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002242 break;
Fangrui Song6907ce22018-07-30 19:24:48 +00002243
Faisal Vali2ab8c152017-12-30 04:15:27 +00002244 case UnqualifiedIdKind::IK_ConstructorName: {
Douglas Gregor3cf81312009-11-03 23:16:33 +00002245 UnqualifiedId TemplateName;
Douglas Gregor786123d2010-05-21 23:18:07 +00002246 bool MemberOfUnknownSpecialization;
Douglas Gregor3cf81312009-11-03 23:16:33 +00002247 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00002248 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
Fangrui Song6907ce22018-07-30 19:24:48 +00002249 TemplateName, ObjectType,
Douglas Gregor786123d2010-05-21 23:18:07 +00002250 EnteringContext, Template,
2251 MemberOfUnknownSpecialization);
Douglas Gregor7861a802009-11-03 01:35:08 +00002252 break;
2253 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002254
Faisal Vali2ab8c152017-12-30 04:15:27 +00002255 case UnqualifiedIdKind::IK_DestructorName: {
Douglas Gregor3cf81312009-11-03 23:16:33 +00002256 UnqualifiedId TemplateName;
Douglas Gregor786123d2010-05-21 23:18:07 +00002257 bool MemberOfUnknownSpecialization;
Douglas Gregor3cf81312009-11-03 23:16:33 +00002258 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002259 if (ObjectType) {
Richard Smithfd3dae02017-01-20 00:20:39 +00002260 TNK = Actions.ActOnDependentTemplateName(
2261 getCurScope(), SS, TemplateKWLoc, TemplateName, ObjectType,
2262 EnteringContext, Template, /*AllowInjectedClassName*/ true);
Douglas Gregorbb119652010-06-16 23:00:59 +00002263 if (TNK == TNK_Non_template)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002264 return true;
2265 } else {
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00002266 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
Fangrui Song6907ce22018-07-30 19:24:48 +00002267 TemplateName, ObjectType,
Douglas Gregor786123d2010-05-21 23:18:07 +00002268 EnteringContext, Template,
2269 MemberOfUnknownSpecialization);
Fangrui Song6907ce22018-07-30 19:24:48 +00002270
John McCallba7bf592010-08-24 05:47:05 +00002271 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00002272 Diag(NameLoc, diag::err_destructor_template_id)
2273 << Name << SS.getRange();
Fangrui Song6907ce22018-07-30 19:24:48 +00002274 return true;
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002275 }
2276 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002277 break;
Douglas Gregor3cf81312009-11-03 23:16:33 +00002278 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002279
Douglas Gregor7861a802009-11-03 01:35:08 +00002280 default:
2281 return false;
2282 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002283
Douglas Gregor7861a802009-11-03 01:35:08 +00002284 if (TNK == TNK_Non_template)
2285 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002286
Douglas Gregor7861a802009-11-03 01:35:08 +00002287 // Parse the enclosed template argument list.
2288 SourceLocation LAngleLoc, RAngleLoc;
2289 TemplateArgList TemplateArgs;
Richard Smithc08b6932018-04-27 02:00:13 +00002290 if (ParseTemplateIdAfterTemplateName(true, LAngleLoc, TemplateArgs,
2291 RAngleLoc))
Douglas Gregor7861a802009-11-03 01:35:08 +00002292 return true;
Richard Smithc08b6932018-04-27 02:00:13 +00002293
Faisal Vali2ab8c152017-12-30 04:15:27 +00002294 if (Id.getKind() == UnqualifiedIdKind::IK_Identifier ||
2295 Id.getKind() == UnqualifiedIdKind::IK_OperatorFunctionId ||
2296 Id.getKind() == UnqualifiedIdKind::IK_LiteralOperatorId) {
Douglas Gregor7861a802009-11-03 01:35:08 +00002297 // Form a parsed representation of the template-id to be stored in the
2298 // UnqualifiedId.
Douglas Gregor7861a802009-11-03 01:35:08 +00002299
Richard Smith72bfbd82013-12-04 00:28:23 +00002300 // FIXME: Store name for literal operator too.
Faisal Vali43caf672017-05-23 01:07:12 +00002301 IdentifierInfo *TemplateII =
Faisal Vali2ab8c152017-12-30 04:15:27 +00002302 Id.getKind() == UnqualifiedIdKind::IK_Identifier ? Id.Identifier
2303 : nullptr;
2304 OverloadedOperatorKind OpKind =
2305 Id.getKind() == UnqualifiedIdKind::IK_Identifier
2306 ? OO_None
2307 : Id.OperatorFunctionId.Operator;
Douglas Gregor7861a802009-11-03 01:35:08 +00002308
Faisal Vali43caf672017-05-23 01:07:12 +00002309 TemplateIdAnnotation *TemplateId = TemplateIdAnnotation::Create(
2310 SS, TemplateKWLoc, Id.StartLocation, TemplateII, OpKind, Template, TNK,
2311 LAngleLoc, RAngleLoc, TemplateArgs, TemplateIds);
2312
Douglas Gregor7861a802009-11-03 01:35:08 +00002313 Id.setTemplateId(TemplateId);
2314 return false;
2315 }
2316
2317 // Bundle the template arguments together.
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00002318 ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs);
Abramo Bagnara4244b432012-01-27 08:46:19 +00002319
Douglas Gregor7861a802009-11-03 01:35:08 +00002320 // Constructor and destructor names.
Richard Smithb23c5e82019-05-09 03:31:27 +00002321 TypeResult Type = Actions.ActOnTemplateIdType(
2322 getCurScope(), SS, TemplateKWLoc, Template, Name, NameLoc, LAngleLoc,
2323 TemplateArgsPtr, RAngleLoc, /*IsCtorOrDtorName=*/true);
Douglas Gregor7861a802009-11-03 01:35:08 +00002324 if (Type.isInvalid())
2325 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00002326
Faisal Vali2ab8c152017-12-30 04:15:27 +00002327 if (Id.getKind() == UnqualifiedIdKind::IK_ConstructorName)
Douglas Gregor7861a802009-11-03 01:35:08 +00002328 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
2329 else
2330 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
Fangrui Song6907ce22018-07-30 19:24:48 +00002331
Douglas Gregor7861a802009-11-03 01:35:08 +00002332 return false;
2333}
2334
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002335/// Parse an operator-function-id or conversion-function-id as part
Douglas Gregor71395fa2009-11-04 00:56:37 +00002336/// of a C++ unqualified-id.
2337///
2338/// This routine is responsible only for parsing the operator-function-id or
2339/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor7861a802009-11-03 01:35:08 +00002340///
2341/// \code
Douglas Gregor7861a802009-11-03 01:35:08 +00002342/// operator-function-id: [C++ 13.5]
2343/// 'operator' operator
2344///
Douglas Gregor71395fa2009-11-04 00:56:37 +00002345/// operator: one of
Douglas Gregor7861a802009-11-03 01:35:08 +00002346/// new delete new[] delete[]
2347/// + - * / % ^ & | ~
2348/// ! = < > += -= *= /= %=
2349/// ^= &= |= << >> >>= <<= == !=
2350/// <= >= && || ++ -- , ->* ->
Richard Smithd30b23d2017-12-01 02:13:10 +00002351/// () [] <=>
Douglas Gregor7861a802009-11-03 01:35:08 +00002352///
2353/// conversion-function-id: [C++ 12.3.2]
2354/// operator conversion-type-id
2355///
2356/// conversion-type-id:
2357/// type-specifier-seq conversion-declarator[opt]
2358///
2359/// conversion-declarator:
2360/// ptr-operator conversion-declarator[opt]
2361/// \endcode
2362///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00002363/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregor7861a802009-11-03 01:35:08 +00002364/// non-empty, then we are parsing the unqualified-id of a qualified-id.
2365///
Fangrui Song6907ce22018-07-30 19:24:48 +00002366/// \param EnteringContext whether we are entering the scope of the
Douglas Gregor7861a802009-11-03 01:35:08 +00002367/// nested-name-specifier.
2368///
Douglas Gregor71395fa2009-11-04 00:56:37 +00002369/// \param ObjectType if this unqualified-id occurs within a member access
2370/// expression, the type of the base object whose member is being accessed.
2371///
2372/// \param Result on a successful parse, contains the parsed unqualified-id.
2373///
2374/// \returns true if parsing fails, false otherwise.
2375bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallba7bf592010-08-24 05:47:05 +00002376 ParsedType ObjectType,
Douglas Gregor71395fa2009-11-04 00:56:37 +00002377 UnqualifiedId &Result) {
2378 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
Fangrui Song6907ce22018-07-30 19:24:48 +00002379
Douglas Gregor71395fa2009-11-04 00:56:37 +00002380 // Consume the 'operator' keyword.
2381 SourceLocation KeywordLoc = ConsumeToken();
Fangrui Song6907ce22018-07-30 19:24:48 +00002382
Douglas Gregor71395fa2009-11-04 00:56:37 +00002383 // Determine what kind of operator name we have.
2384 unsigned SymbolIdx = 0;
2385 SourceLocation SymbolLocations[3];
2386 OverloadedOperatorKind Op = OO_None;
2387 switch (Tok.getKind()) {
2388 case tok::kw_new:
2389 case tok::kw_delete: {
2390 bool isNew = Tok.getKind() == tok::kw_new;
2391 // Consume the 'new' or 'delete'.
2392 SymbolLocations[SymbolIdx++] = ConsumeToken();
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002393 // Check for array new/delete.
2394 if (Tok.is(tok::l_square) &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002395 (!getLangOpts().CPlusPlus11 || NextToken().isNot(tok::l_square))) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002396 // Consume the '[' and ']'.
2397 BalancedDelimiterTracker T(*this, tok::l_square);
2398 T.consumeOpen();
2399 T.consumeClose();
2400 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00002401 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00002402
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002403 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
2404 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00002405 Op = isNew? OO_Array_New : OO_Array_Delete;
2406 } else {
2407 Op = isNew? OO_New : OO_Delete;
2408 }
2409 break;
2410 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002411
Douglas Gregor71395fa2009-11-04 00:56:37 +00002412#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
2413 case tok::Token: \
2414 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
2415 Op = OO_##Name; \
2416 break;
2417#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
2418#include "clang/Basic/OperatorKinds.def"
Fangrui Song6907ce22018-07-30 19:24:48 +00002419
Douglas Gregor71395fa2009-11-04 00:56:37 +00002420 case tok::l_paren: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002421 // Consume the '(' and ')'.
2422 BalancedDelimiterTracker T(*this, tok::l_paren);
2423 T.consumeOpen();
2424 T.consumeClose();
2425 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00002426 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00002427
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002428 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
2429 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00002430 Op = OO_Call;
2431 break;
2432 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002433
Douglas Gregor71395fa2009-11-04 00:56:37 +00002434 case tok::l_square: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002435 // Consume the '[' and ']'.
2436 BalancedDelimiterTracker T(*this, tok::l_square);
2437 T.consumeOpen();
2438 T.consumeClose();
2439 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00002440 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00002441
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002442 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
2443 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00002444 Op = OO_Subscript;
2445 break;
2446 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002447
Douglas Gregor71395fa2009-11-04 00:56:37 +00002448 case tok::code_completion: {
2449 // Code completion for the operator name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002450 Actions.CodeCompleteOperatorName(getCurScope());
Fangrui Song6907ce22018-07-30 19:24:48 +00002451 cutOffParsing();
Douglas Gregor71395fa2009-11-04 00:56:37 +00002452 // Don't try to parse any further.
2453 return true;
2454 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002455
Douglas Gregor71395fa2009-11-04 00:56:37 +00002456 default:
2457 break;
2458 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002459
Douglas Gregor71395fa2009-11-04 00:56:37 +00002460 if (Op != OO_None) {
2461 // We have parsed an operator-function-id.
2462 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
2463 return false;
2464 }
Alexis Hunt34458502009-11-28 04:44:28 +00002465
2466 // Parse a literal-operator-id.
2467 //
Richard Smith6f212062012-10-20 08:41:10 +00002468 // literal-operator-id: C++11 [over.literal]
2469 // operator string-literal identifier
2470 // operator user-defined-string-literal
Alexis Hunt34458502009-11-28 04:44:28 +00002471
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002472 if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002473 Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator);
Alexis Hunt34458502009-11-28 04:44:28 +00002474
Richard Smith7d182a72012-03-08 23:06:02 +00002475 SourceLocation DiagLoc;
2476 unsigned DiagId = 0;
2477
2478 // We're past translation phase 6, so perform string literal concatenation
2479 // before checking for "".
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002480 SmallVector<Token, 4> Toks;
2481 SmallVector<SourceLocation, 4> TokLocs;
Richard Smith7d182a72012-03-08 23:06:02 +00002482 while (isTokenStringLiteral()) {
2483 if (!Tok.is(tok::string_literal) && !DiagId) {
Richard Smith6f212062012-10-20 08:41:10 +00002484 // C++11 [over.literal]p1:
2485 // The string-literal or user-defined-string-literal in a
2486 // literal-operator-id shall have no encoding-prefix [...].
Richard Smith7d182a72012-03-08 23:06:02 +00002487 DiagLoc = Tok.getLocation();
2488 DiagId = diag::err_literal_operator_string_prefix;
2489 }
2490 Toks.push_back(Tok);
2491 TokLocs.push_back(ConsumeStringToken());
2492 }
2493
Craig Topper9d5583e2014-06-26 04:58:39 +00002494 StringLiteralParser Literal(Toks, PP);
Richard Smith7d182a72012-03-08 23:06:02 +00002495 if (Literal.hadError)
2496 return true;
2497
2498 // Grab the literal operator's suffix, which will be either the next token
2499 // or a ud-suffix from the string literal.
Craig Topper161e4db2014-05-21 06:02:52 +00002500 IdentifierInfo *II = nullptr;
Richard Smith7d182a72012-03-08 23:06:02 +00002501 SourceLocation SuffixLoc;
2502 if (!Literal.getUDSuffix().empty()) {
2503 II = &PP.getIdentifierTable().get(Literal.getUDSuffix());
2504 SuffixLoc =
2505 Lexer::AdvanceToTokenCharacter(TokLocs[Literal.getUDSuffixToken()],
2506 Literal.getUDSuffixOffset(),
David Blaikiebbafb8a2012-03-11 07:00:24 +00002507 PP.getSourceManager(), getLangOpts());
Richard Smith7d182a72012-03-08 23:06:02 +00002508 } else if (Tok.is(tok::identifier)) {
2509 II = Tok.getIdentifierInfo();
2510 SuffixLoc = ConsumeToken();
2511 TokLocs.push_back(SuffixLoc);
2512 } else {
Alp Tokerec543272013-12-24 09:48:30 +00002513 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
Alexis Hunt34458502009-11-28 04:44:28 +00002514 return true;
2515 }
2516
Richard Smith7d182a72012-03-08 23:06:02 +00002517 // The string literal must be empty.
2518 if (!Literal.GetString().empty() || Literal.Pascal) {
Richard Smith6f212062012-10-20 08:41:10 +00002519 // C++11 [over.literal]p1:
2520 // The string-literal or user-defined-string-literal in a
2521 // literal-operator-id shall [...] contain no characters
2522 // other than the implicit terminating '\0'.
Richard Smith7d182a72012-03-08 23:06:02 +00002523 DiagLoc = TokLocs.front();
2524 DiagId = diag::err_literal_operator_string_not_empty;
2525 }
2526
2527 if (DiagId) {
2528 // This isn't a valid literal-operator-id, but we think we know
2529 // what the user meant. Tell them what they should have written.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002530 SmallString<32> Str;
Richard Smithe87aeb32015-10-08 00:17:59 +00002531 Str += "\"\"";
Richard Smith7d182a72012-03-08 23:06:02 +00002532 Str += II->getName();
2533 Diag(DiagLoc, DiagId) << FixItHint::CreateReplacement(
2534 SourceRange(TokLocs.front(), TokLocs.back()), Str);
2535 }
2536
2537 Result.setLiteralOperatorId(II, KeywordLoc, SuffixLoc);
Richard Smithd091dc12013-12-05 00:58:33 +00002538
2539 return Actions.checkLiteralOperatorId(SS, Result);
Alexis Hunt34458502009-11-28 04:44:28 +00002540 }
Richard Smithd091dc12013-12-05 00:58:33 +00002541
Douglas Gregor71395fa2009-11-04 00:56:37 +00002542 // Parse a conversion-function-id.
2543 //
2544 // conversion-function-id: [C++ 12.3.2]
2545 // operator conversion-type-id
2546 //
2547 // conversion-type-id:
2548 // type-specifier-seq conversion-declarator[opt]
2549 //
2550 // conversion-declarator:
2551 // ptr-operator conversion-declarator[opt]
Fangrui Song6907ce22018-07-30 19:24:48 +00002552
Douglas Gregor71395fa2009-11-04 00:56:37 +00002553 // Parse the type-specifier-seq.
John McCall084e83d2011-03-24 11:26:52 +00002554 DeclSpec DS(AttrFactory);
Douglas Gregora25d65d2009-11-20 22:03:38 +00002555 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregor71395fa2009-11-04 00:56:37 +00002556 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00002557
Douglas Gregor71395fa2009-11-04 00:56:37 +00002558 // Parse the conversion-declarator, which is merely a sequence of
2559 // ptr-operators.
Faisal Vali421b2d12017-12-29 05:41:00 +00002560 Declarator D(DS, DeclaratorContext::ConversionIdContext);
Craig Topper161e4db2014-05-21 06:02:52 +00002561 ParseDeclaratorInternal(D, /*DirectDeclParser=*/nullptr);
2562
Douglas Gregor71395fa2009-11-04 00:56:37 +00002563 // Finish up the type.
John McCallfaf5fb42010-08-26 23:41:50 +00002564 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregor71395fa2009-11-04 00:56:37 +00002565 if (Ty.isInvalid())
2566 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00002567
Douglas Gregor71395fa2009-11-04 00:56:37 +00002568 // Note that this is a conversion-function-id.
Fangrui Song6907ce22018-07-30 19:24:48 +00002569 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
Douglas Gregor71395fa2009-11-04 00:56:37 +00002570 D.getSourceRange().getEnd());
Fangrui Song6907ce22018-07-30 19:24:48 +00002571 return false;
Douglas Gregor71395fa2009-11-04 00:56:37 +00002572}
2573
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002574/// Parse a C++ unqualified-id (or a C identifier), which describes the
Douglas Gregor71395fa2009-11-04 00:56:37 +00002575/// name of an entity.
2576///
2577/// \code
2578/// unqualified-id: [C++ expr.prim.general]
2579/// identifier
2580/// operator-function-id
2581/// conversion-function-id
2582/// [C++0x] literal-operator-id [TODO]
2583/// ~ class-name
2584/// template-id
2585///
2586/// \endcode
2587///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00002588/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregor71395fa2009-11-04 00:56:37 +00002589/// non-empty, then we are parsing the unqualified-id of a qualified-id.
2590///
Fangrui Song6907ce22018-07-30 19:24:48 +00002591/// \param EnteringContext whether we are entering the scope of the
Douglas Gregor71395fa2009-11-04 00:56:37 +00002592/// nested-name-specifier.
2593///
Douglas Gregor7861a802009-11-03 01:35:08 +00002594/// \param AllowDestructorName whether we allow parsing of a destructor name.
2595///
2596/// \param AllowConstructorName whether we allow parsing a constructor name.
2597///
Richard Smith35845152017-02-07 01:37:30 +00002598/// \param AllowDeductionGuide whether we allow parsing a deduction guide name.
2599///
Douglas Gregor127ea592009-11-03 21:24:04 +00002600/// \param ObjectType if this unqualified-id occurs within a member access
2601/// expression, the type of the base object whose member is being accessed.
2602///
Douglas Gregor7861a802009-11-03 01:35:08 +00002603/// \param Result on a successful parse, contains the parsed unqualified-id.
2604///
2605/// \returns true if parsing fails, false otherwise.
2606bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
2607 bool AllowDestructorName,
2608 bool AllowConstructorName,
Richard Smith35845152017-02-07 01:37:30 +00002609 bool AllowDeductionGuide,
John McCallba7bf592010-08-24 05:47:05 +00002610 ParsedType ObjectType,
Richard Smithc08b6932018-04-27 02:00:13 +00002611 SourceLocation *TemplateKWLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00002612 UnqualifiedId &Result) {
Richard Smithc08b6932018-04-27 02:00:13 +00002613 if (TemplateKWLoc)
2614 *TemplateKWLoc = SourceLocation();
Douglas Gregorb22ee882010-05-05 05:58:24 +00002615
2616 // Handle 'A::template B'. This is for template-ids which have not
2617 // already been annotated by ParseOptionalCXXScopeSpecifier().
2618 bool TemplateSpecified = false;
Richard Smithc08b6932018-04-27 02:00:13 +00002619 if (Tok.is(tok::kw_template)) {
2620 if (TemplateKWLoc && (ObjectType || SS.isSet())) {
2621 TemplateSpecified = true;
2622 *TemplateKWLoc = ConsumeToken();
2623 } else {
2624 SourceLocation TemplateLoc = ConsumeToken();
2625 Diag(TemplateLoc, diag::err_unexpected_template_in_unqualified_id)
2626 << FixItHint::CreateRemoval(TemplateLoc);
2627 }
Douglas Gregorb22ee882010-05-05 05:58:24 +00002628 }
2629
Douglas Gregor7861a802009-11-03 01:35:08 +00002630 // unqualified-id:
2631 // identifier
2632 // template-id (when it hasn't already been annotated)
2633 if (Tok.is(tok::identifier)) {
2634 // Consume the identifier.
2635 IdentifierInfo *Id = Tok.getIdentifierInfo();
2636 SourceLocation IdLoc = ConsumeToken();
2637
David Blaikiebbafb8a2012-03-11 07:00:24 +00002638 if (!getLangOpts().CPlusPlus) {
Douglas Gregor411e5ac2010-01-11 23:29:10 +00002639 // If we're not in C++, only identifiers matter. Record the
2640 // identifier and return.
2641 Result.setIdentifier(Id, IdLoc);
2642 return false;
2643 }
2644
Richard Smith35845152017-02-07 01:37:30 +00002645 ParsedTemplateTy TemplateName;
Fangrui Song6907ce22018-07-30 19:24:48 +00002646 if (AllowConstructorName &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002647 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor7861a802009-11-03 01:35:08 +00002648 // We have parsed a constructor name.
Richard Smith69bc9aa2018-06-22 19:50:19 +00002649 ParsedType Ty = Actions.getConstructorName(*Id, IdLoc, getCurScope(), SS,
2650 EnteringContext);
Richard Smith715ee072018-06-20 21:58:20 +00002651 if (!Ty)
2652 return true;
Abramo Bagnara4244b432012-01-27 08:46:19 +00002653 Result.setConstructorName(Ty, IdLoc, IdLoc);
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002654 } else if (getLangOpts().CPlusPlus17 &&
Richard Smith35845152017-02-07 01:37:30 +00002655 AllowDeductionGuide && SS.isEmpty() &&
2656 Actions.isDeductionGuideName(getCurScope(), *Id, IdLoc,
2657 &TemplateName)) {
2658 // We have parsed a template-name naming a deduction guide.
2659 Result.setDeductionGuideName(TemplateName, IdLoc);
Douglas Gregor7861a802009-11-03 01:35:08 +00002660 } else {
2661 // We have parsed an identifier.
Fangrui Song6907ce22018-07-30 19:24:48 +00002662 Result.setIdentifier(Id, IdLoc);
Douglas Gregor7861a802009-11-03 01:35:08 +00002663 }
2664
2665 // If the next token is a '<', we may have a template.
Richard Smithc08b6932018-04-27 02:00:13 +00002666 TemplateTy Template;
2667 if (Tok.is(tok::less))
2668 return ParseUnqualifiedIdTemplateId(
2669 SS, TemplateKWLoc ? *TemplateKWLoc : SourceLocation(), Id, IdLoc,
2670 EnteringContext, ObjectType, Result, TemplateSpecified);
2671 else if (TemplateSpecified &&
2672 Actions.ActOnDependentTemplateName(
2673 getCurScope(), SS, *TemplateKWLoc, Result, ObjectType,
2674 EnteringContext, Template,
2675 /*AllowInjectedClassName*/ true) == TNK_Non_template)
2676 return true;
2677
Douglas Gregor7861a802009-11-03 01:35:08 +00002678 return false;
2679 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002680
Douglas Gregor7861a802009-11-03 01:35:08 +00002681 // unqualified-id:
2682 // template-id (already parsed and annotated)
2683 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002684 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002685
Fangrui Song6907ce22018-07-30 19:24:48 +00002686 // If the template-name names the current class, then this is a constructor
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002687 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002688 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002689 if (SS.isSet()) {
2690 // C++ [class.qual]p2 specifies that a qualified template-name
2691 // is taken as the constructor name where a constructor can be
2692 // declared. Thus, the template arguments are extraneous, so
2693 // complain about them and remove them entirely.
Fangrui Song6907ce22018-07-30 19:24:48 +00002694 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002695 diag::err_out_of_line_constructor_template_id)
2696 << TemplateId->Name
Douglas Gregora771f462010-03-31 17:46:05 +00002697 << FixItHint::CreateRemoval(
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002698 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
Richard Smith715ee072018-06-20 21:58:20 +00002699 ParsedType Ty = Actions.getConstructorName(
Richard Smith69bc9aa2018-06-22 19:50:19 +00002700 *TemplateId->Name, TemplateId->TemplateNameLoc, getCurScope(), SS,
2701 EnteringContext);
Richard Smith715ee072018-06-20 21:58:20 +00002702 if (!Ty)
2703 return true;
Abramo Bagnara4244b432012-01-27 08:46:19 +00002704 Result.setConstructorName(Ty, TemplateId->TemplateNameLoc,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002705 TemplateId->RAngleLoc);
Richard Smithaf3b3252017-05-18 19:21:48 +00002706 ConsumeAnnotationToken();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002707 return false;
2708 }
2709
2710 Result.setConstructorTemplateId(TemplateId);
Richard Smithaf3b3252017-05-18 19:21:48 +00002711 ConsumeAnnotationToken();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002712 return false;
2713 }
2714
Douglas Gregor7861a802009-11-03 01:35:08 +00002715 // We have already parsed a template-id; consume the annotation token as
2716 // our unqualified-id.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002717 Result.setTemplateId(TemplateId);
Richard Smithc08b6932018-04-27 02:00:13 +00002718 SourceLocation TemplateLoc = TemplateId->TemplateKWLoc;
2719 if (TemplateLoc.isValid()) {
2720 if (TemplateKWLoc && (ObjectType || SS.isSet()))
2721 *TemplateKWLoc = TemplateLoc;
2722 else
2723 Diag(TemplateLoc, diag::err_unexpected_template_in_unqualified_id)
2724 << FixItHint::CreateRemoval(TemplateLoc);
2725 }
Richard Smithaf3b3252017-05-18 19:21:48 +00002726 ConsumeAnnotationToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00002727 return false;
2728 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002729
Douglas Gregor7861a802009-11-03 01:35:08 +00002730 // unqualified-id:
2731 // operator-function-id
2732 // conversion-function-id
2733 if (Tok.is(tok::kw_operator)) {
Douglas Gregor71395fa2009-11-04 00:56:37 +00002734 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor7861a802009-11-03 01:35:08 +00002735 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00002736
Alexis Hunted0530f2009-11-28 08:58:14 +00002737 // If we have an operator-function-id or a literal-operator-id and the next
2738 // token is a '<', we may have a
Fangrui Song6907ce22018-07-30 19:24:48 +00002739 //
Douglas Gregor71395fa2009-11-04 00:56:37 +00002740 // template-id:
2741 // operator-function-id < template-argument-list[opt] >
Richard Smithc08b6932018-04-27 02:00:13 +00002742 TemplateTy Template;
Faisal Vali2ab8c152017-12-30 04:15:27 +00002743 if ((Result.getKind() == UnqualifiedIdKind::IK_OperatorFunctionId ||
2744 Result.getKind() == UnqualifiedIdKind::IK_LiteralOperatorId) &&
Richard Smithc08b6932018-04-27 02:00:13 +00002745 Tok.is(tok::less))
2746 return ParseUnqualifiedIdTemplateId(
2747 SS, TemplateKWLoc ? *TemplateKWLoc : SourceLocation(), nullptr,
2748 SourceLocation(), EnteringContext, ObjectType, Result,
2749 TemplateSpecified);
2750 else if (TemplateSpecified &&
2751 Actions.ActOnDependentTemplateName(
2752 getCurScope(), SS, *TemplateKWLoc, Result, ObjectType,
2753 EnteringContext, Template,
2754 /*AllowInjectedClassName*/ true) == TNK_Non_template)
2755 return true;
Craig Topper161e4db2014-05-21 06:02:52 +00002756
Douglas Gregor7861a802009-11-03 01:35:08 +00002757 return false;
2758 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002759
2760 if (getLangOpts().CPlusPlus &&
Douglas Gregor411e5ac2010-01-11 23:29:10 +00002761 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor7861a802009-11-03 01:35:08 +00002762 // C++ [expr.unary.op]p10:
Fangrui Song6907ce22018-07-30 19:24:48 +00002763 // There is an ambiguity in the unary-expression ~X(), where X is a
2764 // class-name. The ambiguity is resolved in favor of treating ~ as a
Douglas Gregor7861a802009-11-03 01:35:08 +00002765 // unary complement rather than treating ~X as referring to a destructor.
Fangrui Song6907ce22018-07-30 19:24:48 +00002766
Douglas Gregor7861a802009-11-03 01:35:08 +00002767 // Parse the '~'.
2768 SourceLocation TildeLoc = ConsumeToken();
David Blaikieecd8a942011-12-08 16:13:53 +00002769
2770 if (SS.isEmpty() && Tok.is(tok::kw_decltype)) {
2771 DeclSpec DS(AttrFactory);
2772 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
Richard Smithef2cd8f2017-02-08 20:39:08 +00002773 if (ParsedType Type =
2774 Actions.getDestructorTypeForDecltype(DS, ObjectType)) {
David Blaikieecd8a942011-12-08 16:13:53 +00002775 Result.setDestructorName(TildeLoc, Type, EndLoc);
2776 return false;
2777 }
2778 return true;
2779 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002780
Douglas Gregor7861a802009-11-03 01:35:08 +00002781 // Parse the class-name.
2782 if (Tok.isNot(tok::identifier)) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00002783 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor7861a802009-11-03 01:35:08 +00002784 return true;
2785 }
2786
Richard Smithefa6f732014-09-06 02:06:12 +00002787 // If the user wrote ~T::T, correct it to T::~T.
Richard Smith64e033f2015-01-15 00:48:52 +00002788 DeclaratorScopeObj DeclScopeObj(*this, SS);
Nico Weber10d02b52015-02-02 04:18:38 +00002789 if (!TemplateSpecified && NextToken().is(tok::coloncolon)) {
Nico Weberf9e37be2015-01-30 16:53:11 +00002790 // Don't let ParseOptionalCXXScopeSpecifier() "correct"
2791 // `int A; struct { ~A::A(); };` to `int A; struct { ~A:A(); };`,
2792 // it will confuse this recovery logic.
2793 ColonProtectionRAIIObject ColonRAII(*this, false);
2794
Richard Smithefa6f732014-09-06 02:06:12 +00002795 if (SS.isSet()) {
2796 AnnotateScopeToken(SS, /*NewAnnotation*/true);
2797 SS.clear();
2798 }
2799 if (ParseOptionalCXXScopeSpecifier(SS, ObjectType, EnteringContext))
2800 return true;
Nico Weber7f8ec522015-02-02 05:33:50 +00002801 if (SS.isNotEmpty())
David Blaikieefdccaa2016-01-15 23:43:34 +00002802 ObjectType = nullptr;
Nico Weberd0045862015-01-30 04:05:15 +00002803 if (Tok.isNot(tok::identifier) || NextToken().is(tok::coloncolon) ||
Benjamin Kramer3012e592015-03-29 14:35:39 +00002804 !SS.isSet()) {
Richard Smithefa6f732014-09-06 02:06:12 +00002805 Diag(TildeLoc, diag::err_destructor_tilde_scope);
2806 return true;
2807 }
2808
2809 // Recover as if the tilde had been written before the identifier.
2810 Diag(TildeLoc, diag::err_destructor_tilde_scope)
2811 << FixItHint::CreateRemoval(TildeLoc)
2812 << FixItHint::CreateInsertion(Tok.getLocation(), "~");
Richard Smith64e033f2015-01-15 00:48:52 +00002813
2814 // Temporarily enter the scope for the rest of this function.
2815 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
2816 DeclScopeObj.EnterDeclaratorScope();
Richard Smithefa6f732014-09-06 02:06:12 +00002817 }
2818
Douglas Gregor7861a802009-11-03 01:35:08 +00002819 // Parse the class-name (or template-name in a simple-template-id).
2820 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
2821 SourceLocation ClassNameLoc = ConsumeToken();
Richard Smithefa6f732014-09-06 02:06:12 +00002822
Richard Smithc08b6932018-04-27 02:00:13 +00002823 if (Tok.is(tok::less)) {
David Blaikieefdccaa2016-01-15 23:43:34 +00002824 Result.setDestructorName(TildeLoc, nullptr, ClassNameLoc);
Richard Smithc08b6932018-04-27 02:00:13 +00002825 return ParseUnqualifiedIdTemplateId(
2826 SS, TemplateKWLoc ? *TemplateKWLoc : SourceLocation(), ClassName,
2827 ClassNameLoc, EnteringContext, ObjectType, Result, TemplateSpecified);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002828 }
Richard Smithefa6f732014-09-06 02:06:12 +00002829
Douglas Gregor7861a802009-11-03 01:35:08 +00002830 // Note that this is a destructor name.
Fangrui Song6907ce22018-07-30 19:24:48 +00002831 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
John McCallba7bf592010-08-24 05:47:05 +00002832 ClassNameLoc, getCurScope(),
2833 SS, ObjectType,
2834 EnteringContext);
Douglas Gregorfe17d252010-02-16 19:09:40 +00002835 if (!Ty)
Douglas Gregor7861a802009-11-03 01:35:08 +00002836 return true;
Douglas Gregorfe17d252010-02-16 19:09:40 +00002837
Douglas Gregor7861a802009-11-03 01:35:08 +00002838 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor7861a802009-11-03 01:35:08 +00002839 return false;
2840 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002841
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002842 Diag(Tok, diag::err_expected_unqualified_id)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002843 << getLangOpts().CPlusPlus;
Douglas Gregor7861a802009-11-03 01:35:08 +00002844 return true;
2845}
2846
Sebastian Redlbd150f42008-11-21 19:14:01 +00002847/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
2848/// memory in a typesafe manner and call constructors.
Mike Stump11289f42009-09-09 15:08:12 +00002849///
Chris Lattner109faf22009-01-04 21:25:24 +00002850/// This method is called to parse the new expression after the optional :: has
2851/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
2852/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002853///
2854/// new-expression:
2855/// '::'[opt] 'new' new-placement[opt] new-type-id
2856/// new-initializer[opt]
2857/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2858/// new-initializer[opt]
2859///
2860/// new-placement:
2861/// '(' expression-list ')'
2862///
Sebastian Redl351bb782008-12-02 14:43:59 +00002863/// new-type-id:
2864/// type-specifier-seq new-declarator[opt]
Douglas Gregora3a020a2011-04-15 19:40:02 +00002865/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redl351bb782008-12-02 14:43:59 +00002866///
2867/// new-declarator:
2868/// ptr-operator new-declarator[opt]
2869/// direct-new-declarator
2870///
Sebastian Redlbd150f42008-11-21 19:14:01 +00002871/// new-initializer:
2872/// '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00002873/// [C++0x] braced-init-list
Sebastian Redlbd150f42008-11-21 19:14:01 +00002874///
John McCalldadc5752010-08-24 06:29:42 +00002875ExprResult
Chris Lattner109faf22009-01-04 21:25:24 +00002876Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
2877 assert(Tok.is(tok::kw_new) && "expected 'new' token");
2878 ConsumeToken(); // Consume 'new'
Sebastian Redlbd150f42008-11-21 19:14:01 +00002879
2880 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
2881 // second form of new-expression. It can't be a new-type-id.
2882
Benjamin Kramerf0623432012-08-23 22:51:59 +00002883 ExprVector PlacementArgs;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002884 SourceLocation PlacementLParen, PlacementRParen;
2885
Douglas Gregorf2753b32010-07-13 15:54:32 +00002886 SourceRange TypeIdParens;
John McCall084e83d2011-03-24 11:26:52 +00002887 DeclSpec DS(AttrFactory);
Faisal Vali421b2d12017-12-29 05:41:00 +00002888 Declarator DeclaratorInfo(DS, DeclaratorContext::CXXNewContext);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002889 if (Tok.is(tok::l_paren)) {
2890 // If it turns out to be a placement, we change the type location.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002891 BalancedDelimiterTracker T(*this, tok::l_paren);
2892 T.consumeOpen();
2893 PlacementLParen = T.getOpenLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002894 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002895 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redld65cea82008-12-11 22:51:44 +00002896 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002897 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002898
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002899 T.consumeClose();
2900 PlacementRParen = T.getCloseLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002901 if (PlacementRParen.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002902 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redld65cea82008-12-11 22:51:44 +00002903 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002904 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002905
Sebastian Redl351bb782008-12-02 14:43:59 +00002906 if (PlacementArgs.empty()) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002907 // Reset the placement locations. There was no placement.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002908 TypeIdParens = T.getRange();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002909 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002910 } else {
2911 // We still need the type.
2912 if (Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002913 BalancedDelimiterTracker T(*this, tok::l_paren);
2914 T.consumeOpen();
Douglas Gregora3a020a2011-04-15 19:40:02 +00002915 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002916 ParseSpecifierQualifierList(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002917 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002918 ParseDeclarator(DeclaratorInfo);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002919 T.consumeClose();
2920 TypeIdParens = T.getRange();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002921 } else {
Douglas Gregora3a020a2011-04-15 19:40:02 +00002922 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002923 if (ParseCXXTypeSpecifierSeq(DS))
2924 DeclaratorInfo.setInvalidType(true);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002925 else {
2926 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002927 ParseDeclaratorInternal(DeclaratorInfo,
2928 &Parser::ParseDirectNewDeclarator);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002929 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002930 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002931 }
2932 } else {
Sebastian Redl351bb782008-12-02 14:43:59 +00002933 // A new-type-id is a simplified type-id, where essentially the
2934 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregora3a020a2011-04-15 19:40:02 +00002935 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002936 if (ParseCXXTypeSpecifierSeq(DS))
2937 DeclaratorInfo.setInvalidType(true);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002938 else {
2939 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002940 ParseDeclaratorInternal(DeclaratorInfo,
2941 &Parser::ParseDirectNewDeclarator);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002942 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002943 }
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00002944 if (DeclaratorInfo.isInvalidType()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002945 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redld65cea82008-12-11 22:51:44 +00002946 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002947 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002948
Sebastian Redl6047f072012-02-16 12:22:20 +00002949 ExprResult Initializer;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002950
2951 if (Tok.is(tok::l_paren)) {
Sebastian Redl6047f072012-02-16 12:22:20 +00002952 SourceLocation ConstructorLParen, ConstructorRParen;
Benjamin Kramerf0623432012-08-23 22:51:59 +00002953 ExprVector ConstructorArgs;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002954 BalancedDelimiterTracker T(*this, tok::l_paren);
2955 T.consumeOpen();
2956 ConstructorLParen = T.getOpenLocation();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002957 if (Tok.isNot(tok::r_paren)) {
2958 CommaLocsTy CommaLocs;
Ilya Biryukovff2a9972019-02-26 11:01:50 +00002959 auto RunSignatureHelp = [&]() {
2960 ParsedType TypeRep =
2961 Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
2962 QualType PreferredType = Actions.ProduceConstructorSignatureHelp(
2963 getCurScope(), TypeRep.get()->getCanonicalTypeInternal(),
2964 DeclaratorInfo.getEndLoc(), ConstructorArgs, ConstructorLParen);
2965 CalledSignatureHelp = true;
2966 return PreferredType;
2967 };
Francisco Lopes da Silva975a9f62015-01-21 16:24:11 +00002968 if (ParseExpressionList(ConstructorArgs, CommaLocs, [&] {
Ilya Biryukovff2a9972019-02-26 11:01:50 +00002969 PreferredType.enterFunctionArgument(Tok.getLocation(),
2970 RunSignatureHelp);
Kadir Cetinkayaa32d2532018-09-10 13:46:28 +00002971 })) {
Ilya Biryukovff2a9972019-02-26 11:01:50 +00002972 if (PP.isCodeCompletionReached() && !CalledSignatureHelp)
2973 RunSignatureHelp();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002974 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redld65cea82008-12-11 22:51:44 +00002975 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002976 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002977 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002978 T.consumeClose();
2979 ConstructorRParen = T.getCloseLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002980 if (ConstructorRParen.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002981 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redld65cea82008-12-11 22:51:44 +00002982 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002983 }
Sebastian Redl6047f072012-02-16 12:22:20 +00002984 Initializer = Actions.ActOnParenListExpr(ConstructorLParen,
2985 ConstructorRParen,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002986 ConstructorArgs);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002987 } else if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus11) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002988 Diag(Tok.getLocation(),
2989 diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl6047f072012-02-16 12:22:20 +00002990 Initializer = ParseBraceInitializer();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002991 }
Sebastian Redl6047f072012-02-16 12:22:20 +00002992 if (Initializer.isInvalid())
2993 return Initializer;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002994
Sebastian Redl6d4256c2009-03-15 17:47:39 +00002995 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002996 PlacementArgs, PlacementRParen,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002997 TypeIdParens, DeclaratorInfo, Initializer.get());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002998}
2999
Sebastian Redlbd150f42008-11-21 19:14:01 +00003000/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
3001/// passed to ParseDeclaratorInternal.
3002///
3003/// direct-new-declarator:
Richard Smithb9fb1212019-05-06 03:47:15 +00003004/// '[' expression[opt] ']'
Sebastian Redlbd150f42008-11-21 19:14:01 +00003005/// direct-new-declarator '[' constant-expression ']'
3006///
Chris Lattner109faf22009-01-04 21:25:24 +00003007void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00003008 // Parse the array dimensions.
Richard Smithb9fb1212019-05-06 03:47:15 +00003009 bool First = true;
Sebastian Redlbd150f42008-11-21 19:14:01 +00003010 while (Tok.is(tok::l_square)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003011 // An array-size expression can't start with a lambda.
3012 if (CheckProhibitedCXX11Attribute())
3013 continue;
3014
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003015 BalancedDelimiterTracker T(*this, tok::l_square);
3016 T.consumeOpen();
3017
Richard Smithb9fb1212019-05-06 03:47:15 +00003018 ExprResult Size =
3019 First ? (Tok.is(tok::r_square) ? ExprResult() : ParseExpression())
3020 : ParseConstantExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003021 if (Size.isInvalid()) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00003022 // Recover
Alexey Bataevee6507d2013-11-18 08:17:37 +00003023 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlbd150f42008-11-21 19:14:01 +00003024 return;
3025 }
Richard Smithb9fb1212019-05-06 03:47:15 +00003026 First = false;
Sebastian Redlbd150f42008-11-21 19:14:01 +00003027
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003028 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00003029
Bill Wendling44426052012-12-20 19:22:21 +00003030 // Attributes here appertain to the array type. C++11 [expr.new]p5.
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003031 ParsedAttributes Attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00003032 MaybeParseCXX11Attributes(Attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003033
John McCall084e83d2011-03-24 11:26:52 +00003034 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall53fa7142010-12-24 02:08:15 +00003035 /*static=*/false, /*star=*/false,
Erich Keanec480f302018-07-12 21:09:05 +00003036 Size.get(), T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003037 T.getCloseLocation()),
Erich Keanec480f302018-07-12 21:09:05 +00003038 std::move(Attrs), T.getCloseLocation());
Sebastian Redlbd150f42008-11-21 19:14:01 +00003039
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003040 if (T.getCloseLocation().isInvalid())
Sebastian Redlbd150f42008-11-21 19:14:01 +00003041 return;
3042 }
3043}
3044
3045/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
3046/// This ambiguity appears in the syntax of the C++ new operator.
3047///
3048/// new-expression:
3049/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
3050/// new-initializer[opt]
3051///
3052/// new-placement:
3053/// '(' expression-list ')'
3054///
John McCall37ad5512010-08-23 06:44:23 +00003055bool Parser::ParseExpressionListOrTypeId(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003056 SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner109faf22009-01-04 21:25:24 +00003057 Declarator &D) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00003058 // The '(' was already consumed.
3059 if (isTypeIdInParens()) {
Sebastian Redl351bb782008-12-02 14:43:59 +00003060 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003061 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00003062 ParseDeclarator(D);
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00003063 return D.isInvalidType();
Sebastian Redlbd150f42008-11-21 19:14:01 +00003064 }
3065
3066 // It's not a type, it has to be an expression list.
3067 // Discard the comma locations - ActOnCXXNew has enough parameters.
3068 CommaLocsTy CommaLocs;
3069 return ParseExpressionList(PlacementArgs, CommaLocs);
3070}
3071
3072/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
3073/// to free memory allocated by new.
3074///
Chris Lattner109faf22009-01-04 21:25:24 +00003075/// This method is called to parse the 'delete' expression after the optional
3076/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
3077/// and "Start" is its location. Otherwise, "Start" is the location of the
3078/// 'delete' token.
3079///
Sebastian Redlbd150f42008-11-21 19:14:01 +00003080/// delete-expression:
3081/// '::'[opt] 'delete' cast-expression
3082/// '::'[opt] 'delete' '[' ']' cast-expression
John McCalldadc5752010-08-24 06:29:42 +00003083ExprResult
Chris Lattner109faf22009-01-04 21:25:24 +00003084Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
3085 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
3086 ConsumeToken(); // Consume 'delete'
Sebastian Redlbd150f42008-11-21 19:14:01 +00003087
3088 // Array delete?
3089 bool ArrayDelete = false;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003090 if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
Richard Smith10c60722012-08-09 19:01:51 +00003091 // C++11 [expr.delete]p1:
3092 // Whenever the delete keyword is followed by empty square brackets, it
3093 // shall be interpreted as [array delete].
3094 // [Footnote: A lambda expression with a lambda-introducer that consists
3095 // of empty square brackets can follow the delete keyword if
3096 // the lambda expression is enclosed in parentheses.]
Nicolas Lesserf53d1722019-05-19 15:07:58 +00003097
3098 const Token Next = GetLookAheadToken(2);
3099
3100 // Basic lookahead to check if we have a lambda expression.
3101 if (Next.isOneOf(tok::l_brace, tok::less) ||
3102 (Next.is(tok::l_paren) &&
3103 (GetLookAheadToken(3).is(tok::r_paren) ||
3104 (GetLookAheadToken(3).is(tok::identifier) &&
3105 GetLookAheadToken(4).is(tok::identifier))))) {
3106 TentativeParsingAction TPA(*this);
3107 SourceLocation LSquareLoc = Tok.getLocation();
3108 SourceLocation RSquareLoc = NextToken().getLocation();
3109
3110 // SkipUntil can't skip pairs of </*...*/>; don't emit a FixIt in this
3111 // case.
3112 SkipUntil({tok::l_brace, tok::less}, StopBeforeMatch);
3113 SourceLocation RBraceLoc;
3114 bool EmitFixIt = false;
Nicolas Lessere47ae692019-05-19 15:30:00 +00003115 if (Tok.is(tok::l_brace)) {
3116 ConsumeBrace();
Nicolas Lesserf53d1722019-05-19 15:07:58 +00003117 SkipUntil(tok::r_brace, StopBeforeMatch);
3118 RBraceLoc = Tok.getLocation();
3119 EmitFixIt = true;
3120 }
3121
3122 TPA.Revert();
3123
3124 if (EmitFixIt)
3125 Diag(Start, diag::err_lambda_after_delete)
3126 << SourceRange(Start, RSquareLoc)
3127 << FixItHint::CreateInsertion(LSquareLoc, "(")
3128 << FixItHint::CreateInsertion(
3129 Lexer::getLocForEndOfToken(
3130 RBraceLoc, 0, Actions.getSourceManager(), getLangOpts()),
3131 ")");
3132 else
3133 Diag(Start, diag::err_lambda_after_delete)
3134 << SourceRange(Start, RSquareLoc);
3135
3136 // Warn that the non-capturing lambda isn't surrounded by parentheses
3137 // to disambiguate it from 'delete[]'.
3138 ExprResult Lambda = ParseLambdaExpression();
3139 if (Lambda.isInvalid())
3140 return ExprError();
3141
3142 // Evaluate any postfix expressions used on the lambda.
3143 Lambda = ParsePostfixExpressionSuffix(Lambda);
3144 if (Lambda.isInvalid())
3145 return ExprError();
3146 return Actions.ActOnCXXDelete(Start, UseGlobal, /*ArrayForm=*/false,
3147 Lambda.get());
3148 }
3149
Sebastian Redlbd150f42008-11-21 19:14:01 +00003150 ArrayDelete = true;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003151 BalancedDelimiterTracker T(*this, tok::l_square);
3152
3153 T.consumeOpen();
3154 T.consumeClose();
3155 if (T.getCloseLocation().isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +00003156 return ExprError();
Sebastian Redlbd150f42008-11-21 19:14:01 +00003157 }
3158
John McCalldadc5752010-08-24 06:29:42 +00003159 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003160 if (Operand.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003161 return Operand;
Sebastian Redlbd150f42008-11-21 19:14:01 +00003162
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003163 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.get());
Sebastian Redlbd150f42008-11-21 19:14:01 +00003164}
Sebastian Redlbaad4e72009-01-05 20:52:13 +00003165
Douglas Gregor29c42f22012-02-24 07:38:34 +00003166static TypeTrait TypeTraitFromTokKind(tok::TokenKind kind) {
3167 switch (kind) {
3168 default: llvm_unreachable("Not a known type trait");
Alp Toker95e7ff22014-01-01 05:57:51 +00003169#define TYPE_TRAIT_1(Spelling, Name, Key) \
3170case tok::kw_ ## Spelling: return UTT_ ## Name;
Alp Tokercbb90342013-12-13 20:49:58 +00003171#define TYPE_TRAIT_2(Spelling, Name, Key) \
3172case tok::kw_ ## Spelling: return BTT_ ## Name;
3173#include "clang/Basic/TokenKinds.def"
Alp Toker40f9b1c2013-12-12 21:23:03 +00003174#define TYPE_TRAIT_N(Spelling, Name, Key) \
3175 case tok::kw_ ## Spelling: return TT_ ## Name;
3176#include "clang/Basic/TokenKinds.def"
Douglas Gregor29c42f22012-02-24 07:38:34 +00003177 }
3178}
3179
John Wiegley6242b6a2011-04-28 00:16:57 +00003180static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
3181 switch(kind) {
3182 default: llvm_unreachable("Not a known binary type trait");
3183 case tok::kw___array_rank: return ATT_ArrayRank;
3184 case tok::kw___array_extent: return ATT_ArrayExtent;
3185 }
3186}
3187
John Wiegleyf9f65842011-04-25 06:54:41 +00003188static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
3189 switch(kind) {
David Blaikie83d382b2011-09-23 05:06:16 +00003190 default: llvm_unreachable("Not a known unary expression trait.");
John Wiegleyf9f65842011-04-25 06:54:41 +00003191 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
3192 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
3193 }
3194}
3195
Alp Toker40f9b1c2013-12-12 21:23:03 +00003196static unsigned TypeTraitArity(tok::TokenKind kind) {
3197 switch (kind) {
3198 default: llvm_unreachable("Not a known type trait");
3199#define TYPE_TRAIT(N,Spelling,K) case tok::kw_##Spelling: return N;
3200#include "clang/Basic/TokenKinds.def"
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00003201 }
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00003202}
3203
Fangrui Song6907ce22018-07-30 19:24:48 +00003204/// Parse the built-in type-trait pseudo-functions that allow
Douglas Gregor29c42f22012-02-24 07:38:34 +00003205/// implementation of the TR1/C++11 type traits templates.
3206///
3207/// primary-expression:
Alp Toker40f9b1c2013-12-12 21:23:03 +00003208/// unary-type-trait '(' type-id ')'
3209/// binary-type-trait '(' type-id ',' type-id ')'
Douglas Gregor29c42f22012-02-24 07:38:34 +00003210/// type-trait '(' type-id-seq ')'
3211///
3212/// type-id-seq:
3213/// type-id ...[opt] type-id-seq[opt]
3214///
3215ExprResult Parser::ParseTypeTrait() {
Alp Toker40f9b1c2013-12-12 21:23:03 +00003216 tok::TokenKind Kind = Tok.getKind();
3217 unsigned Arity = TypeTraitArity(Kind);
3218
Douglas Gregor29c42f22012-02-24 07:38:34 +00003219 SourceLocation Loc = ConsumeToken();
Fangrui Song6907ce22018-07-30 19:24:48 +00003220
Douglas Gregor29c42f22012-02-24 07:38:34 +00003221 BalancedDelimiterTracker Parens(*this, tok::l_paren);
Alp Toker383d2c42014-01-01 03:08:43 +00003222 if (Parens.expectAndConsume())
Douglas Gregor29c42f22012-02-24 07:38:34 +00003223 return ExprError();
3224
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003225 SmallVector<ParsedType, 2> Args;
Douglas Gregor29c42f22012-02-24 07:38:34 +00003226 do {
3227 // Parse the next type.
3228 TypeResult Ty = ParseTypeName();
3229 if (Ty.isInvalid()) {
3230 Parens.skipToEnd();
3231 return ExprError();
3232 }
3233
3234 // Parse the ellipsis, if present.
3235 if (Tok.is(tok::ellipsis)) {
3236 Ty = Actions.ActOnPackExpansion(Ty.get(), ConsumeToken());
3237 if (Ty.isInvalid()) {
3238 Parens.skipToEnd();
3239 return ExprError();
3240 }
3241 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003242
Douglas Gregor29c42f22012-02-24 07:38:34 +00003243 // Add this type to the list of arguments.
3244 Args.push_back(Ty.get());
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003245 } while (TryConsumeToken(tok::comma));
3246
Douglas Gregor29c42f22012-02-24 07:38:34 +00003247 if (Parens.consumeClose())
3248 return ExprError();
Alp Toker40f9b1c2013-12-12 21:23:03 +00003249
3250 SourceLocation EndLoc = Parens.getCloseLocation();
3251
3252 if (Arity && Args.size() != Arity) {
3253 Diag(EndLoc, diag::err_type_trait_arity)
3254 << Arity << 0 << (Arity > 1) << (int)Args.size() << SourceRange(Loc);
3255 return ExprError();
3256 }
3257
3258 if (!Arity && Args.empty()) {
3259 Diag(EndLoc, diag::err_type_trait_arity)
3260 << 1 << 1 << 1 << (int)Args.size() << SourceRange(Loc);
3261 return ExprError();
3262 }
3263
Alp Toker88f64e62013-12-13 21:19:30 +00003264 return Actions.ActOnTypeTrait(TypeTraitFromTokKind(Kind), Loc, Args, EndLoc);
Douglas Gregor29c42f22012-02-24 07:38:34 +00003265}
3266
John Wiegley6242b6a2011-04-28 00:16:57 +00003267/// ParseArrayTypeTrait - Parse the built-in array type-trait
3268/// pseudo-functions.
3269///
3270/// primary-expression:
3271/// [Embarcadero] '__array_rank' '(' type-id ')'
3272/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
3273///
3274ExprResult Parser::ParseArrayTypeTrait() {
3275 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
3276 SourceLocation Loc = ConsumeToken();
3277
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003278 BalancedDelimiterTracker T(*this, tok::l_paren);
Alp Toker383d2c42014-01-01 03:08:43 +00003279 if (T.expectAndConsume())
John Wiegley6242b6a2011-04-28 00:16:57 +00003280 return ExprError();
3281
3282 TypeResult Ty = ParseTypeName();
3283 if (Ty.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003284 SkipUntil(tok::comma, StopAtSemi);
3285 SkipUntil(tok::r_paren, StopAtSemi);
John Wiegley6242b6a2011-04-28 00:16:57 +00003286 return ExprError();
3287 }
3288
3289 switch (ATT) {
3290 case ATT_ArrayRank: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003291 T.consumeClose();
Craig Topper161e4db2014-05-21 06:02:52 +00003292 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), nullptr,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003293 T.getCloseLocation());
John Wiegley6242b6a2011-04-28 00:16:57 +00003294 }
3295 case ATT_ArrayExtent: {
Alp Toker383d2c42014-01-01 03:08:43 +00003296 if (ExpectAndConsume(tok::comma)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003297 SkipUntil(tok::r_paren, StopAtSemi);
John Wiegley6242b6a2011-04-28 00:16:57 +00003298 return ExprError();
3299 }
3300
3301 ExprResult DimExpr = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003302 T.consumeClose();
John Wiegley6242b6a2011-04-28 00:16:57 +00003303
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003304 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(),
3305 T.getCloseLocation());
John Wiegley6242b6a2011-04-28 00:16:57 +00003306 }
John Wiegley6242b6a2011-04-28 00:16:57 +00003307 }
David Blaikiee4d798f2012-01-20 21:50:17 +00003308 llvm_unreachable("Invalid ArrayTypeTrait!");
John Wiegley6242b6a2011-04-28 00:16:57 +00003309}
3310
John Wiegleyf9f65842011-04-25 06:54:41 +00003311/// ParseExpressionTrait - Parse built-in expression-trait
3312/// pseudo-functions like __is_lvalue_expr( xxx ).
3313///
3314/// primary-expression:
3315/// [Embarcadero] expression-trait '(' expression ')'
3316///
3317ExprResult Parser::ParseExpressionTrait() {
3318 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
3319 SourceLocation Loc = ConsumeToken();
3320
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003321 BalancedDelimiterTracker T(*this, tok::l_paren);
Alp Toker383d2c42014-01-01 03:08:43 +00003322 if (T.expectAndConsume())
John Wiegleyf9f65842011-04-25 06:54:41 +00003323 return ExprError();
3324
3325 ExprResult Expr = ParseExpression();
3326
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003327 T.consumeClose();
John Wiegleyf9f65842011-04-25 06:54:41 +00003328
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003329 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(),
3330 T.getCloseLocation());
John Wiegleyf9f65842011-04-25 06:54:41 +00003331}
3332
3333
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003334/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
3335/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
3336/// based on the context past the parens.
John McCalldadc5752010-08-24 06:29:42 +00003337ExprResult
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003338Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallba7bf592010-08-24 05:47:05 +00003339 ParsedType &CastTy,
Richard Smith87e11a42014-05-15 02:43:47 +00003340 BalancedDelimiterTracker &Tracker,
3341 ColonProtectionRAIIObject &ColonProt) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003342 assert(getLangOpts().CPlusPlus && "Should only be called for C++!");
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003343 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
3344 assert(isTypeIdInParens() && "Not a type-id!");
3345
John McCalldadc5752010-08-24 06:29:42 +00003346 ExprResult Result(true);
David Blaikieefdccaa2016-01-15 23:43:34 +00003347 CastTy = nullptr;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003348
3349 // We need to disambiguate a very ugly part of the C++ syntax:
3350 //
3351 // (T())x; - type-id
3352 // (T())*x; - type-id
3353 // (T())/x; - expression
3354 // (T()); - expression
3355 //
3356 // The bad news is that we cannot use the specialized tentative parser, since
3357 // it can only verify that the thing inside the parens can be parsed as
3358 // type-id, it is not useful for determining the context past the parens.
3359 //
3360 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidis24ad6922009-05-22 15:12:46 +00003361 // making any unnecessary Action calls.
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003362 //
3363 // It uses a scheme similar to parsing inline methods. The parenthesized
3364 // tokens are cached, the context that follows is determined (possibly by
3365 // parsing a cast-expression), and then we re-introduce the cached tokens
3366 // into the token stream and parse them appropriately.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003367
Mike Stump11289f42009-09-09 15:08:12 +00003368 ParenParseOption ParseAs;
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003369 CachedTokens Toks;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003370
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003371 // Store the tokens of the parentheses. We will parse them after we determine
3372 // the context that follows them.
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00003373 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003374 // We didn't find the ')' we expected.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003375 Tracker.consumeClose();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003376 return ExprError();
3377 }
3378
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003379 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003380 ParseAs = CompoundLiteral;
3381 } else {
3382 bool NotCastExpr;
Eli Friedmancf7530f2009-05-25 19:41:42 +00003383 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
3384 NotCastExpr = true;
3385 } else {
3386 // Try parsing the cast-expression that may follow.
3387 // If it is not a cast-expression, NotCastExpr will be true and no token
3388 // will be consumed.
Richard Smith87e11a42014-05-15 02:43:47 +00003389 ColonProt.restore();
Eli Friedmancf7530f2009-05-25 19:41:42 +00003390 Result = ParseCastExpression(false/*isUnaryExpression*/,
3391 false/*isAddressofOperand*/,
John McCallba7bf592010-08-24 05:47:05 +00003392 NotCastExpr,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00003393 // type-id has priority.
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +00003394 IsTypeCast);
Eli Friedmancf7530f2009-05-25 19:41:42 +00003395 }
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003396
3397 // If we parsed a cast-expression, it's really a type-id, otherwise it's
3398 // an expression.
3399 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003400 }
3401
Alexey Bataev703a93c2016-02-04 04:22:09 +00003402 // Create a fake EOF to mark end of Toks buffer.
3403 Token AttrEnd;
3404 AttrEnd.startToken();
3405 AttrEnd.setKind(tok::eof);
3406 AttrEnd.setLocation(Tok.getLocation());
3407 AttrEnd.setEofData(Toks.data());
3408 Toks.push_back(AttrEnd);
3409
Mike Stump11289f42009-09-09 15:08:12 +00003410 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003411 Toks.push_back(Tok);
3412 // Re-enter the stored parenthesized tokens into the token stream, so we may
3413 // parse them now.
Ilya Biryukov929af672019-05-17 09:32:05 +00003414 PP.EnterTokenStream(Toks, /*DisableMacroExpansion*/ true,
3415 /*IsReinject*/ true);
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003416 // Drop the current token and bring the first cached one. It's the same token
3417 // as when we entered this function.
3418 ConsumeAnyToken();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003419
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003420 if (ParseAs >= CompoundLiteral) {
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00003421 // Parse the type declarator.
3422 DeclSpec DS(AttrFactory);
Faisal Vali421b2d12017-12-29 05:41:00 +00003423 Declarator DeclaratorInfo(DS, DeclaratorContext::TypeNameContext);
Richard Smith87e11a42014-05-15 02:43:47 +00003424 {
3425 ColonProtectionRAIIObject InnerColonProtection(*this);
3426 ParseSpecifierQualifierList(DS);
3427 ParseDeclarator(DeclaratorInfo);
3428 }
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003429
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003430 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003431 Tracker.consumeClose();
Richard Smith87e11a42014-05-15 02:43:47 +00003432 ColonProt.restore();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003433
Alexey Bataev703a93c2016-02-04 04:22:09 +00003434 // Consume EOF marker for Toks buffer.
3435 assert(Tok.is(tok::eof) && Tok.getEofData() == AttrEnd.getEofData());
3436 ConsumeAnyToken();
3437
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003438 if (ParseAs == CompoundLiteral) {
3439 ExprType = CompoundLiteral;
Richard Smithaba8b362014-05-15 02:51:15 +00003440 if (DeclaratorInfo.isInvalidType())
3441 return ExprError();
3442
3443 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Richard Smith87e11a42014-05-15 02:43:47 +00003444 return ParseCompoundLiteralExpression(Ty.get(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003445 Tracker.getOpenLocation(),
3446 Tracker.getCloseLocation());
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003447 }
Mike Stump11289f42009-09-09 15:08:12 +00003448
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003449 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
3450 assert(ParseAs == CastExpr);
3451
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00003452 if (DeclaratorInfo.isInvalidType())
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003453 return ExprError();
3454
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003455 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003456 if (!Result.isInvalid())
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003457 Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(),
3458 DeclaratorInfo, CastTy,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003459 Tracker.getCloseLocation(), Result.get());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003460 return Result;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003461 }
Mike Stump11289f42009-09-09 15:08:12 +00003462
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003463 // Not a compound literal, and not followed by a cast-expression.
3464 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003465
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003466 ExprType = SimpleExpr;
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003467 Result = ParseExpression();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003468 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Fangrui Song6907ce22018-07-30 19:24:48 +00003469 Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(),
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003470 Tok.getLocation(), Result.get());
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003471
3472 // Match the ')'.
3473 if (Result.isInvalid()) {
Alexey Bataev703a93c2016-02-04 04:22:09 +00003474 while (Tok.isNot(tok::eof))
3475 ConsumeAnyToken();
3476 assert(Tok.getEofData() == AttrEnd.getEofData());
3477 ConsumeAnyToken();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003478 return ExprError();
3479 }
Mike Stump11289f42009-09-09 15:08:12 +00003480
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003481 Tracker.consumeClose();
Alexey Bataev703a93c2016-02-04 04:22:09 +00003482 // Consume EOF marker for Toks buffer.
3483 assert(Tok.is(tok::eof) && Tok.getEofData() == AttrEnd.getEofData());
3484 ConsumeAnyToken();
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003485 return Result;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003486}