blob: 83e6ae4ef53e069cae378c5629998b0fd29be814 [file] [log] [blame]
Chris Lattner29375652006-12-04 18:06:35 +00001//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner29375652006-12-04 18:06:35 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation for C++.
11//
12//===----------------------------------------------------------------------===//
Erik Verbruggen888d52a2014-01-15 09:15:43 +000013#include "clang/AST/ASTContext.h"
Douglas Gregor94a32472011-01-11 00:33:19 +000014#include "RAIIObjectsForParser.h"
Chandler Carruth757fcd62014-03-04 10:05:20 +000015#include "clang/AST/DeclTemplate.h"
Eli Friedmanc7c97142012-01-04 02:40:39 +000016#include "clang/Basic/PrettyStackTrace.h"
Richard Smith7d182a72012-03-08 23:06:02 +000017#include "clang/Lex/LiteralSupport.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/Parse/ParseDiagnostic.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000019#include "clang/Parse/Parser.h"
John McCall8b0666c2010-08-20 18:27:03 +000020#include "clang/Sema/DeclSpec.h"
21#include "clang/Sema/ParsedTemplate.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/Sema/Scope.h"
Douglas Gregor7861a802009-11-03 01:35:08 +000023#include "llvm/Support/ErrorHandling.h"
24
Faisal Vali2b391ab2013-09-26 19:54:12 +000025
Chris Lattner29375652006-12-04 18:06:35 +000026using namespace clang;
27
Alp Tokerf990cef2014-01-07 02:35:33 +000028static int SelectDigraphErrorMessage(tok::TokenKind Kind) {
29 switch (Kind) {
30 // template name
31 case tok::unknown: return 0;
32 // casts
33 case tok::kw_const_cast: return 1;
34 case tok::kw_dynamic_cast: return 2;
35 case tok::kw_reinterpret_cast: return 3;
36 case tok::kw_static_cast: return 4;
37 default:
38 llvm_unreachable("Unknown type for digraph error message.");
39 }
40}
41
Richard Smith55858492011-04-14 21:45:45 +000042// Are the two tokens adjacent in the same source file?
Richard Smith7b3f3222012-06-18 06:11:04 +000043bool Parser::areTokensAdjacent(const Token &First, const Token &Second) {
Richard Smith55858492011-04-14 21:45:45 +000044 SourceManager &SM = PP.getSourceManager();
45 SourceLocation FirstLoc = SM.getSpellingLoc(First.getLocation());
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +000046 SourceLocation FirstEnd = FirstLoc.getLocWithOffset(First.getLength());
Richard Smith55858492011-04-14 21:45:45 +000047 return FirstEnd == SM.getSpellingLoc(Second.getLocation());
48}
49
50// Suggest fixit for "<::" after a cast.
51static void FixDigraph(Parser &P, Preprocessor &PP, Token &DigraphToken,
52 Token &ColonToken, tok::TokenKind Kind, bool AtDigraph) {
53 // Pull '<:' and ':' off token stream.
54 if (!AtDigraph)
55 PP.Lex(DigraphToken);
56 PP.Lex(ColonToken);
57
58 SourceRange Range;
59 Range.setBegin(DigraphToken.getLocation());
60 Range.setEnd(ColonToken.getLocation());
61 P.Diag(DigraphToken.getLocation(), diag::err_missing_whitespace_digraph)
Alp Tokerf990cef2014-01-07 02:35:33 +000062 << SelectDigraphErrorMessage(Kind)
63 << FixItHint::CreateReplacement(Range, "< ::");
Richard Smith55858492011-04-14 21:45:45 +000064
65 // Update token information to reflect their change in token type.
66 ColonToken.setKind(tok::coloncolon);
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +000067 ColonToken.setLocation(ColonToken.getLocation().getLocWithOffset(-1));
Richard Smith55858492011-04-14 21:45:45 +000068 ColonToken.setLength(2);
69 DigraphToken.setKind(tok::less);
70 DigraphToken.setLength(1);
71
72 // Push new tokens back to token stream.
73 PP.EnterToken(ColonToken);
74 if (!AtDigraph)
75 PP.EnterToken(DigraphToken);
76}
77
Richard Trieu01fc0012011-09-19 19:01:00 +000078// Check for '<::' which should be '< ::' instead of '[:' when following
79// a template name.
80void Parser::CheckForTemplateAndDigraph(Token &Next, ParsedType ObjectType,
81 bool EnteringContext,
82 IdentifierInfo &II, CXXScopeSpec &SS) {
Richard Trieu02e25db2011-09-20 20:03:50 +000083 if (!Next.is(tok::l_square) || Next.getLength() != 2)
Richard Trieu01fc0012011-09-19 19:01:00 +000084 return;
85
86 Token SecondToken = GetLookAheadToken(2);
Richard Smith7b3f3222012-06-18 06:11:04 +000087 if (!SecondToken.is(tok::colon) || !areTokensAdjacent(Next, SecondToken))
Richard Trieu01fc0012011-09-19 19:01:00 +000088 return;
89
90 TemplateTy Template;
91 UnqualifiedId TemplateName;
92 TemplateName.setIdentifier(&II, Tok.getLocation());
93 bool MemberOfUnknownSpecialization;
94 if (!Actions.isTemplateName(getCurScope(), SS, /*hasTemplateKeyword=*/false,
95 TemplateName, ObjectType, EnteringContext,
96 Template, MemberOfUnknownSpecialization))
97 return;
98
Alp Tokerf990cef2014-01-07 02:35:33 +000099 FixDigraph(*this, PP, Next, SecondToken, tok::unknown,
100 /*AtDigraph*/false);
Richard Trieu01fc0012011-09-19 19:01:00 +0000101}
102
Richard Trieu1f3ea7b2012-11-02 01:08:58 +0000103/// \brief Emits an error for a left parentheses after a double colon.
104///
105/// When a '(' is found after a '::', emit an error. Attempt to fix the token
Nico Weber6be9b252012-11-29 05:29:23 +0000106/// stream by removing the '(', and the matching ')' if found.
Richard Trieu1f3ea7b2012-11-02 01:08:58 +0000107void Parser::CheckForLParenAfterColonColon() {
108 if (!Tok.is(tok::l_paren))
109 return;
110
David Majnemer6ca445e2014-12-17 01:39:22 +0000111 Token LParen = Tok;
112 Token NextTok = GetLookAheadToken(1);
113 Token StarTok = NextTok;
114 // Check for (identifier or (*identifier
115 Token IdentifierTok = StarTok.is(tok::star) ? GetLookAheadToken(2) : StarTok;
116 if (IdentifierTok.isNot(tok::identifier))
Richard Trieu1f3ea7b2012-11-02 01:08:58 +0000117 return;
David Majnemer6ca445e2014-12-17 01:39:22 +0000118 // Eat the '('.
119 ConsumeParen();
120 Token RParen;
Benjamin Kramerd503c1c2015-03-08 18:11:59 +0000121 RParen.setLocation(SourceLocation());
David Majnemer6ca445e2014-12-17 01:39:22 +0000122 // Do we have a ')' ?
123 NextTok = StarTok.is(tok::star) ? GetLookAheadToken(2) : GetLookAheadToken(1);
124 if (NextTok.is(tok::r_paren)) {
125 RParen = NextTok;
126 // Eat the '*' if it is present.
127 if (StarTok.is(tok::star))
Richard Trieu1f3ea7b2012-11-02 01:08:58 +0000128 ConsumeToken();
David Majnemer6ca445e2014-12-17 01:39:22 +0000129 // Eat the identifier.
130 ConsumeToken();
131 // Add the identifier token back.
132 PP.EnterToken(IdentifierTok);
133 // Add the '*' back if it was present.
134 if (StarTok.is(tok::star))
135 PP.EnterToken(StarTok);
136 // Eat the ')'.
137 ConsumeParen();
Richard Trieu1f3ea7b2012-11-02 01:08:58 +0000138 }
139
David Majnemer6ca445e2014-12-17 01:39:22 +0000140 Diag(LParen.getLocation(), diag::err_paren_after_colon_colon)
141 << FixItHint::CreateRemoval(LParen.getLocation())
142 << FixItHint::CreateRemoval(RParen.getLocation());
Richard Trieu1f3ea7b2012-11-02 01:08:58 +0000143}
144
Mike Stump11289f42009-09-09 15:08:12 +0000145/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000146///
147/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump11289f42009-09-09 15:08:12 +0000148/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000149/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000150///
151/// '::'[opt] nested-name-specifier
152/// '::'
153///
154/// nested-name-specifier:
155/// type-name '::'
156/// namespace-name '::'
157/// nested-name-specifier identifier '::'
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000158/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000159///
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000160///
Mike Stump11289f42009-09-09 15:08:12 +0000161/// \param SS the scope specifier that will be set to the parsed
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000162/// nested-name-specifier (or empty)
163///
Mike Stump11289f42009-09-09 15:08:12 +0000164/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000165/// the "." or "->" of a member access expression, this parameter provides the
166/// type of the object whose members are being accessed.
167///
168/// \param EnteringContext whether we will be entering into the context of
169/// the nested-name-specifier after parsing it.
170///
Douglas Gregore610ada2010-02-24 18:44:31 +0000171/// \param MayBePseudoDestructor When non-NULL, points to a flag that
172/// indicates whether this nested-name-specifier may be part of a
173/// pseudo-destructor name. In this case, the flag will be set false
174/// if we don't actually end up parsing a destructor name. Moreorover,
175/// if we do end up determining that we are parsing a destructor name,
176/// the last component of the nested-name-specifier is not parsed as
177/// part of the scope specifier.
Richard Smith7447af42013-03-26 01:15:19 +0000178///
179/// \param IsTypename If \c true, this nested-name-specifier is known to be
180/// part of a type name. This is used to improve error recovery.
181///
182/// \param LastII When non-NULL, points to an IdentifierInfo* that will be
183/// filled in with the leading identifier in the last component of the
184/// nested-name-specifier, if any.
Douglas Gregor90d554e2010-02-21 18:36:56 +0000185///
John McCall1f476a12010-02-26 08:45:28 +0000186/// \returns true if there was an error parsing a scope specifier
Douglas Gregore861bac2009-08-25 22:51:20 +0000187bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
John McCallba7bf592010-08-24 05:47:05 +0000188 ParsedType ObjectType,
Douglas Gregor90d554e2010-02-21 18:36:56 +0000189 bool EnteringContext,
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000190 bool *MayBePseudoDestructor,
Richard Smith7447af42013-03-26 01:15:19 +0000191 bool IsTypename,
192 IdentifierInfo **LastII) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000193 assert(getLangOpts().CPlusPlus &&
Chris Lattnerb5134c02009-01-05 01:24:05 +0000194 "Call sites of this function should be guarded by checking for C++");
Mike Stump11289f42009-09-09 15:08:12 +0000195
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000196 if (Tok.is(tok::annot_cxxscope)) {
Richard Smith7447af42013-03-26 01:15:19 +0000197 assert(!LastII && "want last identifier but have already annotated scope");
Nico Weberc60aa712015-02-16 22:32:46 +0000198 assert(!MayBePseudoDestructor && "unexpected annot_cxxscope");
Douglas Gregor869ad452011-02-24 17:54:50 +0000199 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
200 Tok.getAnnotationRange(),
201 SS);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000202 ConsumeToken();
John McCall1f476a12010-02-26 08:45:28 +0000203 return false;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000204 }
Chris Lattnerf9b2cd42009-01-04 21:14:15 +0000205
Larisse Voufob959c3c2013-08-06 05:49:26 +0000206 if (Tok.is(tok::annot_template_id)) {
207 // If the current token is an annotated template id, it may already have
208 // a scope specifier. Restore it.
209 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
210 SS = TemplateId->SS;
211 }
212
Nico Weberc60aa712015-02-16 22:32:46 +0000213 // Has to happen before any "return false"s in this function.
214 bool CheckForDestructor = false;
215 if (MayBePseudoDestructor && *MayBePseudoDestructor) {
216 CheckForDestructor = true;
217 *MayBePseudoDestructor = false;
218 }
219
Richard Smith7447af42013-03-26 01:15:19 +0000220 if (LastII)
Craig Topper161e4db2014-05-21 06:02:52 +0000221 *LastII = nullptr;
Richard Smith7447af42013-03-26 01:15:19 +0000222
Douglas Gregor7f741122009-02-25 19:37:18 +0000223 bool HasScopeSpecifier = false;
224
Chris Lattner8a7d10d2009-01-05 03:55:46 +0000225 if (Tok.is(tok::coloncolon)) {
226 // ::new and ::delete aren't nested-name-specifiers.
227 tok::TokenKind NextKind = NextToken().getKind();
228 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
229 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000230
David Majnemere8fb28f2014-12-29 19:19:18 +0000231 if (NextKind == tok::l_brace) {
232 // It is invalid to have :: {, consume the scope qualifier and pretend
233 // like we never saw it.
234 Diag(ConsumeToken(), diag::err_expected) << tok::identifier;
235 } else {
236 // '::' - Global scope qualifier.
237 if (Actions.ActOnCXXGlobalScopeSpecifier(ConsumeToken(), SS))
238 return true;
Richard Trieu1f3ea7b2012-11-02 01:08:58 +0000239
David Majnemere8fb28f2014-12-29 19:19:18 +0000240 CheckForLParenAfterColonColon();
Richard Trieu1f3ea7b2012-11-02 01:08:58 +0000241
David Majnemere8fb28f2014-12-29 19:19:18 +0000242 HasScopeSpecifier = true;
243 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000244 }
245
Nikola Smiljanic67860242014-09-26 00:28:20 +0000246 if (Tok.is(tok::kw___super)) {
247 SourceLocation SuperLoc = ConsumeToken();
248 if (!Tok.is(tok::coloncolon)) {
249 Diag(Tok.getLocation(), diag::err_expected_coloncolon_after_super);
250 return true;
251 }
252
253 return Actions.ActOnSuperScopeSpecifier(SuperLoc, ConsumeToken(), SS);
254 }
255
Richard Smitha9d10012014-10-04 01:57:39 +0000256 if (!HasScopeSpecifier &&
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000257 Tok.isOneOf(tok::kw_decltype, tok::annot_decltype)) {
David Blaikie15a430a2011-12-04 05:04:18 +0000258 DeclSpec DS(AttrFactory);
259 SourceLocation DeclLoc = Tok.getLocation();
260 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
Alp Tokera3ebe6e2013-12-17 14:12:37 +0000261
262 SourceLocation CCLoc;
263 if (!TryConsumeToken(tok::coloncolon, CCLoc)) {
David Blaikie15a430a2011-12-04 05:04:18 +0000264 AnnotateExistingDecltypeSpecifier(DS, DeclLoc, EndLoc);
265 return false;
266 }
Alp Tokera3ebe6e2013-12-17 14:12:37 +0000267
David Blaikie15a430a2011-12-04 05:04:18 +0000268 if (Actions.ActOnCXXNestedNameSpecifierDecltype(SS, DS, CCLoc))
269 SS.SetInvalid(SourceRange(DeclLoc, CCLoc));
270
271 HasScopeSpecifier = true;
272 }
273
Douglas Gregor7f741122009-02-25 19:37:18 +0000274 while (true) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000275 if (HasScopeSpecifier) {
276 // C++ [basic.lookup.classref]p5:
277 // If the qualified-id has the form
Douglas Gregor308047d2009-09-09 00:23:06 +0000278 //
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000279 // ::class-name-or-namespace-name::...
Douglas Gregor308047d2009-09-09 00:23:06 +0000280 //
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000281 // the class-name-or-namespace-name is looked up in global scope as a
282 // class-name or namespace-name.
283 //
284 // To implement this, we clear out the object type as soon as we've
285 // seen a leading '::' or part of a nested-name-specifier.
David Blaikieefdccaa2016-01-15 23:43:34 +0000286 ObjectType = nullptr;
287
Douglas Gregor2436e712009-09-17 21:32:03 +0000288 if (Tok.is(tok::code_completion)) {
289 // Code completion for a nested-name-specifier, where the code
290 // code completion token follows the '::'.
Douglas Gregor0be31a22010-07-02 17:43:08 +0000291 Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
Argyrios Kyrtzidis7d94c922011-04-23 01:04:12 +0000292 // Include code completion token into the range of the scope otherwise
293 // when we try to annotate the scope tokens the dangling code completion
294 // token will cause assertion in
295 // Preprocessor::AnnotatePreviousCachedTokens.
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000296 SS.setEndLoc(Tok.getLocation());
297 cutOffParsing();
298 return true;
Douglas Gregor2436e712009-09-17 21:32:03 +0000299 }
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000300 }
Mike Stump11289f42009-09-09 15:08:12 +0000301
Douglas Gregor7f741122009-02-25 19:37:18 +0000302 // nested-name-specifier:
Chris Lattner0eed3a62009-06-26 03:47:46 +0000303 // nested-name-specifier 'template'[opt] simple-template-id '::'
304
305 // Parse the optional 'template' keyword, then make sure we have
306 // 'identifier <' after it.
307 if (Tok.is(tok::kw_template)) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000308 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedman2624be42009-08-29 04:08:08 +0000309 // nested-name-specifier, since they aren't allowed to start with
310 // 'template'.
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000311 if (!HasScopeSpecifier && !ObjectType)
Eli Friedman2624be42009-08-29 04:08:08 +0000312 break;
313
Douglas Gregor120635b2009-11-11 16:39:34 +0000314 TentativeParsingAction TPA(*this);
Chris Lattner0eed3a62009-06-26 03:47:46 +0000315 SourceLocation TemplateKWLoc = ConsumeToken();
Richard Smithd091dc12013-12-05 00:58:33 +0000316
Douglas Gregor71395fa2009-11-04 00:56:37 +0000317 UnqualifiedId TemplateName;
318 if (Tok.is(tok::identifier)) {
Douglas Gregor71395fa2009-11-04 00:56:37 +0000319 // Consume the identifier.
Douglas Gregor120635b2009-11-11 16:39:34 +0000320 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregor71395fa2009-11-04 00:56:37 +0000321 ConsumeToken();
322 } else if (Tok.is(tok::kw_operator)) {
Richard Smithd091dc12013-12-05 00:58:33 +0000323 // We don't need to actually parse the unqualified-id in this case,
324 // because a simple-template-id cannot start with 'operator', but
325 // go ahead and parse it anyway for consistency with the case where
326 // we already annotated the template-id.
327 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor120635b2009-11-11 16:39:34 +0000328 TemplateName)) {
329 TPA.Commit();
Douglas Gregor71395fa2009-11-04 00:56:37 +0000330 break;
Douglas Gregor120635b2009-11-11 16:39:34 +0000331 }
Richard Smithd091dc12013-12-05 00:58:33 +0000332
Alexis Hunted0530f2009-11-28 08:58:14 +0000333 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
334 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor71395fa2009-11-04 00:56:37 +0000335 Diag(TemplateName.getSourceRange().getBegin(),
336 diag::err_id_after_template_in_nested_name_spec)
337 << TemplateName.getSourceRange();
Douglas Gregor120635b2009-11-11 16:39:34 +0000338 TPA.Commit();
Douglas Gregor71395fa2009-11-04 00:56:37 +0000339 break;
340 }
341 } else {
Douglas Gregor120635b2009-11-11 16:39:34 +0000342 TPA.Revert();
Chris Lattner0eed3a62009-06-26 03:47:46 +0000343 break;
344 }
Mike Stump11289f42009-09-09 15:08:12 +0000345
Douglas Gregor120635b2009-11-11 16:39:34 +0000346 // If the next token is not '<', we have a qualified-id that refers
347 // to a template name, such as T::template apply, but is not a
348 // template-id.
349 if (Tok.isNot(tok::less)) {
350 TPA.Revert();
351 break;
352 }
353
354 // Commit to parsing the template-id.
355 TPA.Commit();
Douglas Gregorbb119652010-06-16 23:00:59 +0000356 TemplateTy Template;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000357 if (TemplateNameKind TNK
358 = Actions.ActOnDependentTemplateName(getCurScope(),
359 SS, TemplateKWLoc, TemplateName,
360 ObjectType, EnteringContext,
361 Template)) {
362 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateKWLoc,
363 TemplateName, false))
Douglas Gregorbb119652010-06-16 23:00:59 +0000364 return true;
365 } else
John McCall1f476a12010-02-26 08:45:28 +0000366 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000367
Chris Lattner0eed3a62009-06-26 03:47:46 +0000368 continue;
369 }
Mike Stump11289f42009-09-09 15:08:12 +0000370
Douglas Gregor7f741122009-02-25 19:37:18 +0000371 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump11289f42009-09-09 15:08:12 +0000372 // We have
Douglas Gregor7f741122009-02-25 19:37:18 +0000373 //
Richard Smith72bfbd82013-12-04 00:28:23 +0000374 // template-id '::'
Douglas Gregor7f741122009-02-25 19:37:18 +0000375 //
Richard Smith72bfbd82013-12-04 00:28:23 +0000376 // So we need to check whether the template-id is a simple-template-id of
377 // the right kind (it should name a type or be dependent), and then
Douglas Gregorb67535d2009-03-31 00:43:58 +0000378 // convert it into a type within the nested-name-specifier.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +0000379 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregore610ada2010-02-24 18:44:31 +0000380 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
381 *MayBePseudoDestructor = true;
John McCall1f476a12010-02-26 08:45:28 +0000382 return false;
Douglas Gregore610ada2010-02-24 18:44:31 +0000383 }
384
Richard Smith7447af42013-03-26 01:15:19 +0000385 if (LastII)
386 *LastII = TemplateId->Name;
387
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000388 // Consume the template-id token.
389 ConsumeToken();
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000390
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000391 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
392 SourceLocation CCLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000393
David Blaikie8c045bc2011-11-07 03:30:03 +0000394 HasScopeSpecifier = true;
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000395
Benjamin Kramercc4c49d2012-08-23 23:38:35 +0000396 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000397 TemplateId->NumArgs);
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000398
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000399 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000400 SS,
401 TemplateId->TemplateKWLoc,
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000402 TemplateId->Template,
403 TemplateId->TemplateNameLoc,
404 TemplateId->LAngleLoc,
405 TemplateArgsPtr,
406 TemplateId->RAngleLoc,
407 CCLoc,
408 EnteringContext)) {
409 SourceLocation StartLoc
410 = SS.getBeginLoc().isValid()? SS.getBeginLoc()
411 : TemplateId->TemplateNameLoc;
412 SS.SetInvalid(SourceRange(StartLoc, CCLoc));
Chris Lattner704edfb2009-06-26 03:45:46 +0000413 }
Argyrios Kyrtzidis13935672011-05-03 18:45:38 +0000414
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000415 continue;
Douglas Gregor7f741122009-02-25 19:37:18 +0000416 }
417
Chris Lattnere2355f72009-06-26 03:52:38 +0000418 // The rest of the nested-name-specifier possibilities start with
419 // tok::identifier.
420 if (Tok.isNot(tok::identifier))
421 break;
422
423 IdentifierInfo &II = *Tok.getIdentifierInfo();
424
425 // nested-name-specifier:
426 // type-name '::'
427 // namespace-name '::'
428 // nested-name-specifier identifier '::'
429 Token Next = NextToken();
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000430 Sema::NestedNameSpecInfo IdInfo(&II, Tok.getLocation(), Next.getLocation(),
431 ObjectType);
432
Chris Lattner1c428032009-12-07 01:36:53 +0000433 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
434 // and emit a fixit hint for it.
Douglas Gregor90d554e2010-02-21 18:36:56 +0000435 if (Next.is(tok::colon) && !ColonIsSacred) {
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000436 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, IdInfo,
Douglas Gregor90d554e2010-02-21 18:36:56 +0000437 EnteringContext) &&
438 // If the token after the colon isn't an identifier, it's still an
439 // error, but they probably meant something else strange so don't
440 // recover like this.
441 PP.LookAhead(1).is(tok::identifier)) {
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000442 Diag(Next, diag::err_unexpected_colon_in_nested_name_spec)
Douglas Gregora771f462010-03-31 17:46:05 +0000443 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregor90d554e2010-02-21 18:36:56 +0000444 // Recover as if the user wrote '::'.
445 Next.setKind(tok::coloncolon);
446 }
Chris Lattner1c428032009-12-07 01:36:53 +0000447 }
David Majnemerf58efd92014-12-29 23:12:23 +0000448
449 if (Next.is(tok::coloncolon) && GetLookAheadToken(2).is(tok::l_brace)) {
450 // It is invalid to have :: {, consume the scope qualifier and pretend
451 // like we never saw it.
452 Token Identifier = Tok; // Stash away the identifier.
453 ConsumeToken(); // Eat the identifier, current token is now '::'.
David Majnemerec3f49d2014-12-29 23:24:27 +0000454 Diag(PP.getLocForEndOfToken(ConsumeToken()), diag::err_expected)
455 << tok::identifier;
David Majnemerf58efd92014-12-29 23:12:23 +0000456 UnconsumeToken(Identifier); // Stick the identifier back.
457 Next = NextToken(); // Point Next at the '{' token.
458 }
459
Chris Lattnere2355f72009-06-26 03:52:38 +0000460 if (Next.is(tok::coloncolon)) {
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000461 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000462 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, IdInfo)) {
Douglas Gregore610ada2010-02-24 18:44:31 +0000463 *MayBePseudoDestructor = true;
John McCall1f476a12010-02-26 08:45:28 +0000464 return false;
Douglas Gregore610ada2010-02-24 18:44:31 +0000465 }
466
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000467 if (ColonIsSacred) {
468 const Token &Next2 = GetLookAheadToken(2);
469 if (Next2.is(tok::kw_private) || Next2.is(tok::kw_protected) ||
470 Next2.is(tok::kw_public) || Next2.is(tok::kw_virtual)) {
471 Diag(Next2, diag::err_unexpected_token_in_nested_name_spec)
472 << Next2.getName()
473 << FixItHint::CreateReplacement(Next.getLocation(), ":");
474 Token ColonColon;
475 PP.Lex(ColonColon);
476 ColonColon.setKind(tok::colon);
477 PP.EnterToken(ColonColon);
478 break;
479 }
480 }
481
Richard Smith7447af42013-03-26 01:15:19 +0000482 if (LastII)
483 *LastII = &II;
484
Chris Lattnere2355f72009-06-26 03:52:38 +0000485 // We have an identifier followed by a '::'. Lookup this name
486 // as the name in a nested-name-specifier.
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000487 Token Identifier = Tok;
Chris Lattnere2355f72009-06-26 03:52:38 +0000488 SourceLocation IdLoc = ConsumeToken();
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000489 assert(Tok.isOneOf(tok::coloncolon, tok::colon) &&
Chris Lattner1c428032009-12-07 01:36:53 +0000490 "NextToken() not working properly!");
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000491 Token ColonColon = Tok;
Chris Lattnere2355f72009-06-26 03:52:38 +0000492 SourceLocation CCLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000493
Richard Trieu1f3ea7b2012-11-02 01:08:58 +0000494 CheckForLParenAfterColonColon();
495
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000496 bool IsCorrectedToColon = false;
Craig Topper161e4db2014-05-21 06:02:52 +0000497 bool *CorrectionFlagPtr = ColonIsSacred ? &IsCorrectedToColon : nullptr;
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000498 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), IdInfo,
499 EnteringContext, SS,
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000500 false, CorrectionFlagPtr)) {
501 // Identifier is not recognized as a nested name, but we can have
502 // mistyped '::' instead of ':'.
503 if (CorrectionFlagPtr && IsCorrectedToColon) {
504 ColonColon.setKind(tok::colon);
505 PP.EnterToken(Tok);
506 PP.EnterToken(ColonColon);
507 Tok = Identifier;
508 break;
509 }
Douglas Gregor90c99722011-02-24 00:17:56 +0000510 SS.SetInvalid(SourceRange(IdLoc, CCLoc));
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000511 }
512 HasScopeSpecifier = true;
Chris Lattnere2355f72009-06-26 03:52:38 +0000513 continue;
514 }
Mike Stump11289f42009-09-09 15:08:12 +0000515
Richard Trieu01fc0012011-09-19 19:01:00 +0000516 CheckForTemplateAndDigraph(Next, ObjectType, EnteringContext, II, SS);
Richard Smith55858492011-04-14 21:45:45 +0000517
Chris Lattnere2355f72009-06-26 03:52:38 +0000518 // nested-name-specifier:
519 // type-name '<'
520 if (Next.is(tok::less)) {
521 TemplateTy Template;
Douglas Gregor3cf81312009-11-03 23:16:33 +0000522 UnqualifiedId TemplateName;
523 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor786123d2010-05-21 23:18:07 +0000524 bool MemberOfUnknownSpecialization;
Douglas Gregor0be31a22010-07-02 17:43:08 +0000525 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c5dee42010-08-06 12:11:11 +0000526 /*hasTemplateKeyword=*/false,
Douglas Gregor3cf81312009-11-03 23:16:33 +0000527 TemplateName,
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000528 ObjectType,
Douglas Gregore861bac2009-08-25 22:51:20 +0000529 EnteringContext,
Douglas Gregor786123d2010-05-21 23:18:07 +0000530 Template,
531 MemberOfUnknownSpecialization)) {
David Blaikie8c045bc2011-11-07 03:30:03 +0000532 // We have found a template name, so annotate this token
Chris Lattnere2355f72009-06-26 03:52:38 +0000533 // with a template-id annotation. We do not permit the
534 // template-id to be translated into a type annotation,
535 // because some clients (e.g., the parsing of class template
536 // specializations) still want to see the original template-id
537 // token.
Douglas Gregor71395fa2009-11-04 00:56:37 +0000538 ConsumeToken();
Abramo Bagnara7945c982012-01-27 09:46:47 +0000539 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
540 TemplateName, false))
John McCall1f476a12010-02-26 08:45:28 +0000541 return true;
Chris Lattnere2355f72009-06-26 03:52:38 +0000542 continue;
Larisse Voufo39a1e502013-08-06 01:03:05 +0000543 }
544
Douglas Gregor20c38a72010-05-21 23:43:39 +0000545 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000546 (IsTypename || IsTemplateArgumentList(1))) {
Douglas Gregor20c38a72010-05-21 23:43:39 +0000547 // We have something like t::getAs<T>, where getAs is a
548 // member of an unknown specialization. However, this will only
549 // parse correctly as a template, so suggest the keyword 'template'
550 // before 'getAs' and treat this as a dependent template name.
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000551 unsigned DiagID = diag::err_missing_dependent_template_keyword;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000552 if (getLangOpts().MicrosoftExt)
Francois Pichet93921652011-04-22 08:25:24 +0000553 DiagID = diag::warn_missing_dependent_template_keyword;
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000554
555 Diag(Tok.getLocation(), DiagID)
Douglas Gregor20c38a72010-05-21 23:43:39 +0000556 << II.getName()
557 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
558
Douglas Gregorbb119652010-06-16 23:00:59 +0000559 if (TemplateNameKind TNK
Douglas Gregor0be31a22010-07-02 17:43:08 +0000560 = Actions.ActOnDependentTemplateName(getCurScope(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000561 SS, SourceLocation(),
Douglas Gregorbb119652010-06-16 23:00:59 +0000562 TemplateName, ObjectType,
563 EnteringContext, Template)) {
564 // Consume the identifier.
565 ConsumeToken();
Abramo Bagnara7945c982012-01-27 09:46:47 +0000566 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
567 TemplateName, false))
568 return true;
Douglas Gregorbb119652010-06-16 23:00:59 +0000569 }
570 else
Douglas Gregor20c38a72010-05-21 23:43:39 +0000571 return true;
Douglas Gregorbb119652010-06-16 23:00:59 +0000572
Douglas Gregor20c38a72010-05-21 23:43:39 +0000573 continue;
Chris Lattnere2355f72009-06-26 03:52:38 +0000574 }
575 }
576
Douglas Gregor7f741122009-02-25 19:37:18 +0000577 // We don't have any tokens that form the beginning of a
578 // nested-name-specifier, so we're done.
579 break;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000580 }
Mike Stump11289f42009-09-09 15:08:12 +0000581
Douglas Gregore610ada2010-02-24 18:44:31 +0000582 // Even if we didn't see any pieces of a nested-name-specifier, we
583 // still check whether there is a tilde in this position, which
584 // indicates a potential pseudo-destructor.
585 if (CheckForDestructor && Tok.is(tok::tilde))
586 *MayBePseudoDestructor = true;
587
John McCall1f476a12010-02-26 08:45:28 +0000588 return false;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000589}
590
Kaelyn Takatab16e6322014-11-20 22:06:40 +0000591ExprResult Parser::tryParseCXXIdExpression(CXXScopeSpec &SS, bool isAddressOfOperand,
592 Token &Replacement) {
593 SourceLocation TemplateKWLoc;
594 UnqualifiedId Name;
595 if (ParseUnqualifiedId(SS,
596 /*EnteringContext=*/false,
597 /*AllowDestructorName=*/false,
598 /*AllowConstructorName=*/false,
David Blaikieefdccaa2016-01-15 23:43:34 +0000599 /*ObjectType=*/nullptr, TemplateKWLoc, Name))
Kaelyn Takatab16e6322014-11-20 22:06:40 +0000600 return ExprError();
601
602 // This is only the direct operand of an & operator if it is not
603 // followed by a postfix-expression suffix.
604 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
605 isAddressOfOperand = false;
606
607 return Actions.ActOnIdExpression(getCurScope(), SS, TemplateKWLoc, Name,
608 Tok.is(tok::l_paren), isAddressOfOperand,
609 nullptr, /*IsInlineAsmIdentifier=*/false,
610 &Replacement);
611}
612
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000613/// ParseCXXIdExpression - Handle id-expression.
614///
615/// id-expression:
616/// unqualified-id
617/// qualified-id
618///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000619/// qualified-id:
620/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
621/// '::' identifier
622/// '::' operator-function-id
Douglas Gregora727cb92009-06-30 22:34:41 +0000623/// '::' template-id
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000624///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000625/// NOTE: The standard specifies that, for qualified-id, the parser does not
626/// expect:
627///
628/// '::' conversion-function-id
629/// '::' '~' class-name
630///
631/// This may cause a slight inconsistency on diagnostics:
632///
633/// class C {};
634/// namespace A {}
635/// void f() {
636/// :: A :: ~ C(); // Some Sema error about using destructor with a
637/// // namespace.
638/// :: ~ C(); // Some Parser error like 'unexpected ~'.
639/// }
640///
641/// We simplify the parser a bit and make it work like:
642///
643/// qualified-id:
644/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
645/// '::' unqualified-id
646///
647/// That way Sema can handle and report similar errors for namespaces and the
648/// global scope.
649///
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000650/// The isAddressOfOperand parameter indicates that this id-expression is a
651/// direct operand of the address-of operator. This is, besides member contexts,
652/// the only place where a qualified-id naming a non-static class member may
653/// appear.
654///
John McCalldadc5752010-08-24 06:29:42 +0000655ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000656 // qualified-id:
657 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
658 // '::' unqualified-id
659 //
660 CXXScopeSpec SS;
David Blaikieefdccaa2016-01-15 23:43:34 +0000661 ParseOptionalCXXScopeSpecifier(SS, nullptr, /*EnteringContext=*/false);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000662
Kaelyn Takatab16e6322014-11-20 22:06:40 +0000663 Token Replacement;
Nico Weber01a46ad2015-02-15 06:15:40 +0000664 ExprResult Result =
665 tryParseCXXIdExpression(SS, isAddressOfOperand, Replacement);
Kaelyn Takatab16e6322014-11-20 22:06:40 +0000666 if (Result.isUnset()) {
667 // If the ExprResult is valid but null, then typo correction suggested a
668 // keyword replacement that needs to be reparsed.
669 UnconsumeToken(Replacement);
670 Result = tryParseCXXIdExpression(SS, isAddressOfOperand, Replacement);
671 }
672 assert(!Result.isUnset() && "Typo correction suggested a keyword replacement "
673 "for a previous keyword suggestion");
674 return Result;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000675}
676
Richard Smith21b3ab42013-05-09 21:36:41 +0000677/// ParseLambdaExpression - Parse a C++11 lambda expression.
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000678///
679/// lambda-expression:
680/// lambda-introducer lambda-declarator[opt] compound-statement
681///
682/// lambda-introducer:
683/// '[' lambda-capture[opt] ']'
684///
685/// lambda-capture:
686/// capture-default
687/// capture-list
688/// capture-default ',' capture-list
689///
690/// capture-default:
691/// '&'
692/// '='
693///
694/// capture-list:
695/// capture
696/// capture-list ',' capture
697///
698/// capture:
Richard Smith21b3ab42013-05-09 21:36:41 +0000699/// simple-capture
700/// init-capture [C++1y]
701///
702/// simple-capture:
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000703/// identifier
704/// '&' identifier
705/// 'this'
706///
Richard Smith21b3ab42013-05-09 21:36:41 +0000707/// init-capture: [C++1y]
708/// identifier initializer
709/// '&' identifier initializer
710///
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000711/// lambda-declarator:
712/// '(' parameter-declaration-clause ')' attribute-specifier[opt]
713/// 'mutable'[opt] exception-specification[opt]
714/// trailing-return-type[opt]
715///
716ExprResult Parser::ParseLambdaExpression() {
717 // Parse lambda-introducer.
718 LambdaIntroducer Intro;
Faisal Vali5fb7c3c2013-12-05 01:40:41 +0000719 Optional<unsigned> DiagID = ParseLambdaIntroducer(Intro);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000720 if (DiagID) {
721 Diag(Tok, DiagID.getValue());
David Majnemer234b8182015-01-12 03:36:37 +0000722 SkipUntil(tok::r_square, StopAtSemi);
723 SkipUntil(tok::l_brace, StopAtSemi);
724 SkipUntil(tok::r_brace, StopAtSemi);
Eli Friedmanc7c97142012-01-04 02:40:39 +0000725 return ExprError();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000726 }
727
728 return ParseLambdaExpressionAfterIntroducer(Intro);
729}
730
731/// TryParseLambdaExpression - Use lookahead and potentially tentative
732/// parsing to determine if we are looking at a C++0x lambda expression, and parse
733/// it if we are.
734///
735/// If we are not looking at a lambda expression, returns ExprError().
736ExprResult Parser::TryParseLambdaExpression() {
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000737 assert(getLangOpts().CPlusPlus11
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000738 && Tok.is(tok::l_square)
739 && "Not at the start of a possible lambda expression.");
740
Bruno Cardoso Lopes29b34232016-05-31 18:46:31 +0000741 const Token Next = NextToken();
742 if (Next.is(tok::eof)) // Nothing else to lookup here...
743 return ExprEmpty();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000744
Bruno Cardoso Lopes29b34232016-05-31 18:46:31 +0000745 const Token After = GetLookAheadToken(2);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000746 // If lookahead indicates this is a lambda...
747 if (Next.is(tok::r_square) || // []
748 Next.is(tok::equal) || // [=
749 (Next.is(tok::amp) && // [&] or [&,
750 (After.is(tok::r_square) ||
751 After.is(tok::comma))) ||
752 (Next.is(tok::identifier) && // [identifier]
753 After.is(tok::r_square))) {
754 return ParseLambdaExpression();
755 }
756
Eli Friedmanc7c97142012-01-04 02:40:39 +0000757 // If lookahead indicates an ObjC message send...
758 // [identifier identifier
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000759 if (Next.is(tok::identifier) && After.is(tok::identifier)) {
Eli Friedmanc7c97142012-01-04 02:40:39 +0000760 return ExprEmpty();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000761 }
Faisal Vali5fb7c3c2013-12-05 01:40:41 +0000762
Eli Friedmanc7c97142012-01-04 02:40:39 +0000763 // Here, we're stuck: lambda introducers and Objective-C message sends are
764 // unambiguous, but it requires arbitrary lookhead. [a,b,c,d,e,f,g] is a
765 // lambda, and [a,b,c,d,e,f,g h] is a Objective-C message send. Instead of
766 // writing two routines to parse a lambda introducer, just try to parse
767 // a lambda introducer first, and fall back if that fails.
768 // (TryParseLambdaIntroducer never produces any diagnostic output.)
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000769 LambdaIntroducer Intro;
770 if (TryParseLambdaIntroducer(Intro))
Eli Friedmanc7c97142012-01-04 02:40:39 +0000771 return ExprEmpty();
Faisal Vali5fb7c3c2013-12-05 01:40:41 +0000772
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000773 return ParseLambdaExpressionAfterIntroducer(Intro);
774}
775
Richard Smithf44d2a82013-05-21 22:21:19 +0000776/// \brief Parse a lambda introducer.
777/// \param Intro A LambdaIntroducer filled in with information about the
778/// contents of the lambda-introducer.
779/// \param SkippedInits If non-null, we are disambiguating between an Obj-C
780/// message send and a lambda expression. In this mode, we will
781/// sometimes skip the initializers for init-captures and not fully
782/// populate \p Intro. This flag will be set to \c true if we do so.
783/// \return A DiagnosticID if it hit something unexpected. The location for
784/// for the diagnostic is that of the current token.
785Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro,
786 bool *SkippedInits) {
David Blaikie05785d12013-02-20 22:23:23 +0000787 typedef Optional<unsigned> DiagResult;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000788
789 assert(Tok.is(tok::l_square) && "Lambda expressions begin with '['.");
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000790 BalancedDelimiterTracker T(*this, tok::l_square);
791 T.consumeOpen();
792
793 Intro.Range.setBegin(T.getOpenLocation());
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000794
795 bool first = true;
796
797 // Parse capture-default.
798 if (Tok.is(tok::amp) &&
799 (NextToken().is(tok::comma) || NextToken().is(tok::r_square))) {
800 Intro.Default = LCD_ByRef;
Douglas Gregora1bffa22012-02-10 17:46:20 +0000801 Intro.DefaultLoc = ConsumeToken();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000802 first = false;
803 } else if (Tok.is(tok::equal)) {
804 Intro.Default = LCD_ByCopy;
Douglas Gregora1bffa22012-02-10 17:46:20 +0000805 Intro.DefaultLoc = ConsumeToken();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000806 first = false;
807 }
808
809 while (Tok.isNot(tok::r_square)) {
810 if (!first) {
Douglas Gregord8c61782012-02-15 15:34:24 +0000811 if (Tok.isNot(tok::comma)) {
Douglas Gregor721b14d2012-07-31 00:50:07 +0000812 // Provide a completion for a lambda introducer here. Except
813 // in Objective-C, where this is Almost Surely meant to be a message
814 // send. In that case, fail here and let the ObjC message
815 // expression parser perform the completion.
Douglas Gregor2d8db8f2012-07-31 15:27:48 +0000816 if (Tok.is(tok::code_completion) &&
817 !(getLangOpts().ObjC1 && Intro.Default == LCD_None &&
818 !Intro.Captures.empty())) {
Douglas Gregord8c61782012-02-15 15:34:24 +0000819 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
820 /*AfterAmpersand=*/false);
Alp Toker1c583cc2014-05-02 03:43:14 +0000821 cutOffParsing();
Douglas Gregord8c61782012-02-15 15:34:24 +0000822 break;
823 }
824
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000825 return DiagResult(diag::err_expected_comma_or_rsquare);
Douglas Gregord8c61782012-02-15 15:34:24 +0000826 }
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000827 ConsumeToken();
828 }
829
Douglas Gregord8c61782012-02-15 15:34:24 +0000830 if (Tok.is(tok::code_completion)) {
831 // If we're in Objective-C++ and we have a bare '[', then this is more
832 // likely to be a message receiver.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000833 if (getLangOpts().ObjC1 && first)
Douglas Gregord8c61782012-02-15 15:34:24 +0000834 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
835 else
836 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
837 /*AfterAmpersand=*/false);
Alp Toker1c583cc2014-05-02 03:43:14 +0000838 cutOffParsing();
Douglas Gregord8c61782012-02-15 15:34:24 +0000839 break;
840 }
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000841
Douglas Gregord8c61782012-02-15 15:34:24 +0000842 first = false;
843
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000844 // Parse capture.
845 LambdaCaptureKind Kind = LCK_ByCopy;
Richard Smith42b10572015-11-11 01:36:17 +0000846 LambdaCaptureInitKind InitKind = LambdaCaptureInitKind::NoInit;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000847 SourceLocation Loc;
Craig Topper161e4db2014-05-21 06:02:52 +0000848 IdentifierInfo *Id = nullptr;
Douglas Gregor3e308b12012-02-14 19:27:52 +0000849 SourceLocation EllipsisLoc;
Richard Smith21b3ab42013-05-09 21:36:41 +0000850 ExprResult Init;
Faisal Validc6b5962016-03-21 09:25:37 +0000851
852 if (Tok.is(tok::star)) {
853 Loc = ConsumeToken();
854 if (Tok.is(tok::kw_this)) {
855 ConsumeToken();
856 Kind = LCK_StarThis;
857 } else {
858 return DiagResult(diag::err_expected_star_this_capture);
859 }
860 } else if (Tok.is(tok::kw_this)) {
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000861 Kind = LCK_This;
862 Loc = ConsumeToken();
863 } else {
864 if (Tok.is(tok::amp)) {
865 Kind = LCK_ByRef;
866 ConsumeToken();
Douglas Gregord8c61782012-02-15 15:34:24 +0000867
868 if (Tok.is(tok::code_completion)) {
869 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
870 /*AfterAmpersand=*/true);
Alp Toker1c583cc2014-05-02 03:43:14 +0000871 cutOffParsing();
Douglas Gregord8c61782012-02-15 15:34:24 +0000872 break;
873 }
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000874 }
875
876 if (Tok.is(tok::identifier)) {
877 Id = Tok.getIdentifierInfo();
878 Loc = ConsumeToken();
879 } else if (Tok.is(tok::kw_this)) {
880 // FIXME: If we want to suggest a fixit here, will need to return more
881 // than just DiagnosticID. Perhaps full DiagnosticBuilder that can be
882 // Clear()ed to prevent emission in case of tentative parsing?
883 return DiagResult(diag::err_this_captured_by_reference);
884 } else {
885 return DiagResult(diag::err_expected_capture);
886 }
Richard Smith21b3ab42013-05-09 21:36:41 +0000887
888 if (Tok.is(tok::l_paren)) {
889 BalancedDelimiterTracker Parens(*this, tok::l_paren);
890 Parens.consumeOpen();
891
Richard Smith42b10572015-11-11 01:36:17 +0000892 InitKind = LambdaCaptureInitKind::DirectInit;
893
Richard Smith21b3ab42013-05-09 21:36:41 +0000894 ExprVector Exprs;
895 CommaLocsTy Commas;
Richard Smithf44d2a82013-05-21 22:21:19 +0000896 if (SkippedInits) {
897 Parens.skipToEnd();
898 *SkippedInits = true;
899 } else if (ParseExpressionList(Exprs, Commas)) {
Richard Smith21b3ab42013-05-09 21:36:41 +0000900 Parens.skipToEnd();
901 Init = ExprError();
902 } else {
903 Parens.consumeClose();
904 Init = Actions.ActOnParenListExpr(Parens.getOpenLocation(),
905 Parens.getCloseLocation(),
906 Exprs);
907 }
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000908 } else if (Tok.isOneOf(tok::l_brace, tok::equal)) {
Faisal Vali5fb7c3c2013-12-05 01:40:41 +0000909 // Each lambda init-capture forms its own full expression, which clears
910 // Actions.MaybeODRUseExprs. So create an expression evaluation context
911 // to save the necessary state, and restore it later.
912 EnterExpressionEvaluationContext EC(Actions,
913 Sema::PotentiallyEvaluated);
Richard Smith42b10572015-11-11 01:36:17 +0000914
915 if (TryConsumeToken(tok::equal))
916 InitKind = LambdaCaptureInitKind::CopyInit;
917 else
918 InitKind = LambdaCaptureInitKind::ListInit;
Richard Smith21b3ab42013-05-09 21:36:41 +0000919
Richard Smith215f4232015-02-11 02:41:33 +0000920 if (!SkippedInits) {
Richard Smithf44d2a82013-05-21 22:21:19 +0000921 Init = ParseInitializer();
Richard Smith215f4232015-02-11 02:41:33 +0000922 } else if (Tok.is(tok::l_brace)) {
Richard Smithf44d2a82013-05-21 22:21:19 +0000923 BalancedDelimiterTracker Braces(*this, tok::l_brace);
924 Braces.consumeOpen();
925 Braces.skipToEnd();
926 *SkippedInits = true;
927 } else {
928 // We're disambiguating this:
929 //
930 // [..., x = expr
931 //
932 // We need to find the end of the following expression in order to
Richard Smith9e2f0a42014-04-13 04:31:48 +0000933 // determine whether this is an Obj-C message send's receiver, a
934 // C99 designator, or a lambda init-capture.
Richard Smithf44d2a82013-05-21 22:21:19 +0000935 //
936 // Parse the expression to find where it ends, and annotate it back
937 // onto the tokens. We would have parsed this expression the same way
938 // in either case: both the RHS of an init-capture and the RHS of an
939 // assignment expression are parsed as an initializer-clause, and in
940 // neither case can anything be added to the scope between the '[' and
941 // here.
942 //
943 // FIXME: This is horrible. Adding a mechanism to skip an expression
944 // would be much cleaner.
945 // FIXME: If there is a ',' before the next ']' or ':', we can skip to
946 // that instead. (And if we see a ':' with no matching '?', we can
947 // classify this as an Obj-C message send.)
948 SourceLocation StartLoc = Tok.getLocation();
949 InMessageExpressionRAIIObject MaybeInMessageExpression(*this, true);
950 Init = ParseInitializer();
951
952 if (Tok.getLocation() != StartLoc) {
953 // Back out the lexing of the token after the initializer.
954 PP.RevertCachedTokens(1);
955
956 // Replace the consumed tokens with an appropriate annotation.
957 Tok.setLocation(StartLoc);
958 Tok.setKind(tok::annot_primary_expr);
959 setExprAnnotation(Tok, Init);
960 Tok.setAnnotationEndLoc(PP.getLastCachedTokenLocation());
961 PP.AnnotateCachedTokens(Tok);
962
963 // Consume the annotated initializer.
964 ConsumeToken();
965 }
966 }
Alp Tokera3ebe6e2013-12-17 14:12:37 +0000967 } else
968 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000969 }
Faisal Vali5fb7c3c2013-12-05 01:40:41 +0000970 // If this is an init capture, process the initialization expression
971 // right away. For lambda init-captures such as the following:
972 // const int x = 10;
973 // auto L = [i = x+1](int a) {
974 // return [j = x+2,
975 // &k = x](char b) { };
976 // };
977 // keep in mind that each lambda init-capture has to have:
978 // - its initialization expression executed in the context
979 // of the enclosing/parent decl-context.
980 // - but the variable itself has to be 'injected' into the
981 // decl-context of its lambda's call-operator (which has
982 // not yet been created).
983 // Each init-expression is a full-expression that has to get
984 // Sema-analyzed (for capturing etc.) before its lambda's
985 // call-operator's decl-context, scope & scopeinfo are pushed on their
986 // respective stacks. Thus if any variable is odr-used in the init-capture
987 // it will correctly get captured in the enclosing lambda, if one exists.
988 // The init-variables above are created later once the lambdascope and
989 // call-operators decl-context is pushed onto its respective stack.
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000990
Faisal Vali5fb7c3c2013-12-05 01:40:41 +0000991 // Since the lambda init-capture's initializer expression occurs in the
992 // context of the enclosing function or lambda, therefore we can not wait
993 // till a lambda scope has been pushed on before deciding whether the
994 // variable needs to be captured. We also need to process all
995 // lvalue-to-rvalue conversions and discarded-value conversions,
996 // so that we can avoid capturing certain constant variables.
997 // For e.g.,
998 // void test() {
999 // const int x = 10;
1000 // auto L = [&z = x](char a) { <-- don't capture by the current lambda
1001 // return [y = x](int i) { <-- don't capture by enclosing lambda
1002 // return y;
1003 // }
1004 // };
Richard Smithbdb84f32016-07-22 23:36:59 +00001005 // }
Faisal Vali5fb7c3c2013-12-05 01:40:41 +00001006 // If x was not const, the second use would require 'L' to capture, and
1007 // that would be an error.
1008
Richard Smith42b10572015-11-11 01:36:17 +00001009 ParsedType InitCaptureType;
Faisal Vali5fb7c3c2013-12-05 01:40:41 +00001010 if (Init.isUsable()) {
1011 // Get the pointer and store it in an lvalue, so we can use it as an
1012 // out argument.
1013 Expr *InitExpr = Init.get();
1014 // This performs any lvalue-to-rvalue conversions if necessary, which
1015 // can affect what gets captured in the containing decl-context.
Richard Smith42b10572015-11-11 01:36:17 +00001016 InitCaptureType = Actions.actOnLambdaInitCaptureInitialization(
1017 Loc, Kind == LCK_ByRef, Id, InitKind, InitExpr);
Faisal Vali5fb7c3c2013-12-05 01:40:41 +00001018 Init = InitExpr;
Faisal Vali5fb7c3c2013-12-05 01:40:41 +00001019 }
Richard Smith42b10572015-11-11 01:36:17 +00001020 Intro.addCapture(Kind, Loc, Id, EllipsisLoc, InitKind, Init,
1021 InitCaptureType);
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001022 }
1023
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001024 T.consumeClose();
1025 Intro.Range.setEnd(T.getCloseLocation());
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001026 return DiagResult();
1027}
1028
Douglas Gregord8c61782012-02-15 15:34:24 +00001029/// TryParseLambdaIntroducer - Tentatively parse a lambda introducer.
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001030///
1031/// Returns true if it hit something unexpected.
1032bool Parser::TryParseLambdaIntroducer(LambdaIntroducer &Intro) {
1033 TentativeParsingAction PA(*this);
1034
Richard Smithf44d2a82013-05-21 22:21:19 +00001035 bool SkippedInits = false;
1036 Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro, &SkippedInits));
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001037
1038 if (DiagID) {
1039 PA.Revert();
1040 return true;
1041 }
1042
Richard Smithf44d2a82013-05-21 22:21:19 +00001043 if (SkippedInits) {
1044 // Parse it again, but this time parse the init-captures too.
1045 PA.Revert();
1046 Intro = LambdaIntroducer();
1047 DiagID = ParseLambdaIntroducer(Intro);
1048 assert(!DiagID && "parsing lambda-introducer failed on reparse");
1049 return false;
1050 }
1051
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001052 PA.Commit();
1053 return false;
1054}
1055
Faisal Valia734ab92016-03-26 16:11:37 +00001056static void
1057tryConsumeMutableOrConstexprToken(Parser &P, SourceLocation &MutableLoc,
1058 SourceLocation &ConstexprLoc,
1059 SourceLocation &DeclEndLoc) {
1060 assert(MutableLoc.isInvalid());
1061 assert(ConstexprLoc.isInvalid());
1062 // Consume constexpr-opt mutable-opt in any sequence, and set the DeclEndLoc
1063 // to the final of those locations. Emit an error if we have multiple
1064 // copies of those keywords and recover.
1065
1066 while (true) {
1067 switch (P.getCurToken().getKind()) {
1068 case tok::kw_mutable: {
1069 if (MutableLoc.isValid()) {
1070 P.Diag(P.getCurToken().getLocation(),
1071 diag::err_lambda_decl_specifier_repeated)
1072 << 0 << FixItHint::CreateRemoval(P.getCurToken().getLocation());
1073 }
1074 MutableLoc = P.ConsumeToken();
1075 DeclEndLoc = MutableLoc;
1076 break /*switch*/;
1077 }
1078 case tok::kw_constexpr:
1079 if (ConstexprLoc.isValid()) {
1080 P.Diag(P.getCurToken().getLocation(),
1081 diag::err_lambda_decl_specifier_repeated)
1082 << 1 << FixItHint::CreateRemoval(P.getCurToken().getLocation());
1083 }
1084 ConstexprLoc = P.ConsumeToken();
1085 DeclEndLoc = ConstexprLoc;
1086 break /*switch*/;
1087 default:
1088 return;
1089 }
1090 }
1091}
1092
1093static void
1094addConstexprToLambdaDeclSpecifier(Parser &P, SourceLocation ConstexprLoc,
1095 DeclSpec &DS) {
1096 if (ConstexprLoc.isValid()) {
1097 P.Diag(ConstexprLoc, !P.getLangOpts().CPlusPlus1z
1098 ? diag::ext_constexpr_on_lambda_cxx1z
1099 : diag::warn_cxx14_compat_constexpr_on_lambda);
1100 const char *PrevSpec = nullptr;
1101 unsigned DiagID = 0;
1102 DS.SetConstexprSpec(ConstexprLoc, PrevSpec, DiagID);
1103 assert(PrevSpec == nullptr && DiagID == 0 &&
1104 "Constexpr cannot have been set previously!");
1105 }
1106}
1107
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001108/// ParseLambdaExpressionAfterIntroducer - Parse the rest of a lambda
1109/// expression.
1110ExprResult Parser::ParseLambdaExpressionAfterIntroducer(
1111 LambdaIntroducer &Intro) {
Eli Friedmanc7c97142012-01-04 02:40:39 +00001112 SourceLocation LambdaBeginLoc = Intro.Range.getBegin();
1113 Diag(LambdaBeginLoc, diag::warn_cxx98_compat_lambda);
1114
1115 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), LambdaBeginLoc,
1116 "lambda expression parsing");
1117
Faisal Vali2b391ab2013-09-26 19:54:12 +00001118
1119
Richard Smith21b3ab42013-05-09 21:36:41 +00001120 // FIXME: Call into Actions to add any init-capture declarations to the
1121 // scope while parsing the lambda-declarator and compound-statement.
1122
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001123 // Parse lambda-declarator[opt].
1124 DeclSpec DS(AttrFactory);
Eli Friedman36d12942012-01-04 04:41:38 +00001125 Declarator D(DS, Declarator::LambdaExprContext);
Faisal Vali2b391ab2013-09-26 19:54:12 +00001126 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
Justin Lebar0fad0ba2016-09-30 17:14:48 +00001127 Actions.PushLambdaScope();
1128
1129 ParsedAttributes Attr(AttrFactory);
1130 SourceLocation DeclLoc = Tok.getLocation();
Justin Lebar0fad0ba2016-09-30 17:14:48 +00001131 if (getLangOpts().CUDA) {
1132 // In CUDA code, GNU attributes are allowed to appear immediately after the
1133 // "[...]", even if there is no "(...)" before the lambda body.
Justin Lebar0139a5d2016-09-30 19:55:48 +00001134 MaybeParseGNUAttributes(D);
Justin Lebar0fad0ba2016-09-30 17:14:48 +00001135 }
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001136
Justin Lebare46ea722016-09-30 19:55:55 +00001137 // Helper to emit a warning if we see a CUDA host/device/global attribute
1138 // after '(...)'. nvcc doesn't accept this.
1139 auto WarnIfHasCUDATargetAttr = [&] {
1140 if (getLangOpts().CUDA)
1141 for (auto *A = Attr.getList(); A != nullptr; A = A->getNext())
1142 if (A->getKind() == AttributeList::AT_CUDADevice ||
1143 A->getKind() == AttributeList::AT_CUDAHost ||
1144 A->getKind() == AttributeList::AT_CUDAGlobal)
1145 Diag(A->getLoc(), diag::warn_cuda_attr_lambda_position)
1146 << A->getName()->getName();
1147 };
1148
David Majnemere01c4662015-01-09 05:10:55 +00001149 TypeResult TrailingReturnType;
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001150 if (Tok.is(tok::l_paren)) {
1151 ParseScope PrototypeScope(this,
1152 Scope::FunctionPrototypeScope |
Richard Smithe233fbf2013-01-28 22:42:45 +00001153 Scope::FunctionDeclarationScope |
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001154 Scope::DeclScope);
1155
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001156 BalancedDelimiterTracker T(*this, tok::l_paren);
1157 T.consumeOpen();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001158 SourceLocation LParenLoc = T.getOpenLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001159
1160 // Parse parameter-declaration-clause.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001161 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001162 SourceLocation EllipsisLoc;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001163
1164 if (Tok.isNot(tok::r_paren)) {
Faisal Vali2b391ab2013-09-26 19:54:12 +00001165 Actions.RecordParsingTemplateParameterDepth(TemplateParameterDepth);
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001166 ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc);
Faisal Vali2b391ab2013-09-26 19:54:12 +00001167 // For a generic lambda, each 'auto' within the parameter declaration
1168 // clause creates a template type parameter, so increment the depth.
1169 if (Actions.getCurGenericLambda())
1170 ++CurTemplateDepthTracker;
1171 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001172 T.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001173 SourceLocation RParenLoc = T.getCloseLocation();
Justin Lebar0139a5d2016-09-30 19:55:48 +00001174 SourceLocation DeclEndLoc = RParenLoc;
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001175
Aaron Ballmane8d69b72014-03-12 00:01:07 +00001176 // GNU-style attributes must be parsed before the mutable specifier to be
1177 // compatible with GCC.
1178 MaybeParseGNUAttributes(Attr, &DeclEndLoc);
1179
David Majnemerbda86322015-02-04 08:22:46 +00001180 // MSVC-style attributes must be parsed before the mutable specifier to be
1181 // compatible with MSVC.
Aaron Ballman068aa512015-05-20 20:58:33 +00001182 MaybeParseMicrosoftDeclSpecs(Attr, &DeclEndLoc);
David Majnemerbda86322015-02-04 08:22:46 +00001183
Faisal Valia734ab92016-03-26 16:11:37 +00001184 // Parse mutable-opt and/or constexpr-opt, and update the DeclEndLoc.
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001185 SourceLocation MutableLoc;
Faisal Valia734ab92016-03-26 16:11:37 +00001186 SourceLocation ConstexprLoc;
1187 tryConsumeMutableOrConstexprToken(*this, MutableLoc, ConstexprLoc,
1188 DeclEndLoc);
1189
1190 addConstexprToLambdaDeclSpecifier(*this, ConstexprLoc, DS);
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001191
1192 // Parse exception-specification[opt].
1193 ExceptionSpecificationType ESpecType = EST_None;
1194 SourceRange ESpecRange;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001195 SmallVector<ParsedType, 2> DynamicExceptions;
1196 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001197 ExprResult NoexceptExpr;
Richard Smith0b3a4622014-11-13 20:01:57 +00001198 CachedTokens *ExceptionSpecTokens;
1199 ESpecType = tryParseExceptionSpecification(/*Delayed=*/false,
1200 ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00001201 DynamicExceptions,
1202 DynamicExceptionRanges,
Richard Smith0b3a4622014-11-13 20:01:57 +00001203 NoexceptExpr,
1204 ExceptionSpecTokens);
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001205
1206 if (ESpecType != EST_None)
1207 DeclEndLoc = ESpecRange.getEnd();
1208
1209 // Parse attribute-specifier[opt].
Richard Smith89645bc2013-01-02 12:01:23 +00001210 MaybeParseCXX11Attributes(Attr, &DeclEndLoc);
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001211
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001212 SourceLocation FunLocalRangeEnd = DeclEndLoc;
1213
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001214 // Parse trailing-return-type[opt].
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001215 if (Tok.is(tok::arrow)) {
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001216 FunLocalRangeEnd = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001217 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00001218 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001219 if (Range.getEnd().isValid())
1220 DeclEndLoc = Range.getEnd();
1221 }
1222
1223 PrototypeScope.Exit();
1224
Justin Lebare46ea722016-09-30 19:55:55 +00001225 WarnIfHasCUDATargetAttr();
1226
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001227 SourceLocation NoLoc;
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001228 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001229 /*isAmbiguous=*/false,
1230 LParenLoc,
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001231 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001232 EllipsisLoc, RParenLoc,
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001233 DS.getTypeQualifiers(),
1234 /*RefQualifierIsLValueRef=*/true,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001235 /*RefQualifierLoc=*/NoLoc,
1236 /*ConstQualifierLoc=*/NoLoc,
1237 /*VolatileQualifierLoc=*/NoLoc,
Hal Finkel23a07392014-10-20 17:32:04 +00001238 /*RestrictQualifierLoc=*/NoLoc,
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001239 MutableLoc,
Nathan Wilsone23a9a42015-08-26 04:19:36 +00001240 ESpecType, ESpecRange,
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001241 DynamicExceptions.data(),
1242 DynamicExceptionRanges.data(),
1243 DynamicExceptions.size(),
1244 NoexceptExpr.isUsable() ?
Craig Topper161e4db2014-05-21 06:02:52 +00001245 NoexceptExpr.get() : nullptr,
Richard Smith0b3a4622014-11-13 20:01:57 +00001246 /*ExceptionSpecTokens*/nullptr,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001247 LParenLoc, FunLocalRangeEnd, D,
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001248 TrailingReturnType),
1249 Attr, DeclEndLoc);
Faisal Valia734ab92016-03-26 16:11:37 +00001250 } else if (Tok.isOneOf(tok::kw_mutable, tok::arrow, tok::kw___attribute,
1251 tok::kw_constexpr) ||
Aaron Ballmanb5c59f52014-03-11 13:03:15 +00001252 (Tok.is(tok::l_square) && NextToken().is(tok::l_square))) {
1253 // It's common to forget that one needs '()' before 'mutable', an attribute
1254 // specifier, or the result type. Deal with this.
1255 unsigned TokKind = 0;
1256 switch (Tok.getKind()) {
1257 case tok::kw_mutable: TokKind = 0; break;
1258 case tok::arrow: TokKind = 1; break;
Aaron Ballmane8d69b72014-03-12 00:01:07 +00001259 case tok::kw___attribute:
Aaron Ballmanb5c59f52014-03-11 13:03:15 +00001260 case tok::l_square: TokKind = 2; break;
Faisal Valia734ab92016-03-26 16:11:37 +00001261 case tok::kw_constexpr: TokKind = 3; break;
Aaron Ballmanb5c59f52014-03-11 13:03:15 +00001262 default: llvm_unreachable("Unknown token kind");
1263 }
1264
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001265 Diag(Tok, diag::err_lambda_missing_parens)
Aaron Ballmanb5c59f52014-03-11 13:03:15 +00001266 << TokKind
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001267 << FixItHint::CreateInsertion(Tok.getLocation(), "() ");
Justin Lebar0139a5d2016-09-30 19:55:48 +00001268 SourceLocation DeclEndLoc = DeclLoc;
Aaron Ballmane8d69b72014-03-12 00:01:07 +00001269
1270 // GNU-style attributes must be parsed before the mutable specifier to be
1271 // compatible with GCC.
Aaron Ballmane8d69b72014-03-12 00:01:07 +00001272 MaybeParseGNUAttributes(Attr, &DeclEndLoc);
1273
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001274 // Parse 'mutable', if it's there.
1275 SourceLocation MutableLoc;
1276 if (Tok.is(tok::kw_mutable)) {
1277 MutableLoc = ConsumeToken();
1278 DeclEndLoc = MutableLoc;
1279 }
Aaron Ballmanb5c59f52014-03-11 13:03:15 +00001280
1281 // Parse attribute-specifier[opt].
Aaron Ballmanb5c59f52014-03-11 13:03:15 +00001282 MaybeParseCXX11Attributes(Attr, &DeclEndLoc);
1283
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001284 // Parse the return type, if there is one.
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001285 if (Tok.is(tok::arrow)) {
1286 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00001287 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001288 if (Range.getEnd().isValid())
David Majnemere01c4662015-01-09 05:10:55 +00001289 DeclEndLoc = Range.getEnd();
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001290 }
1291
Justin Lebare46ea722016-09-30 19:55:55 +00001292 WarnIfHasCUDATargetAttr();
1293
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001294 SourceLocation NoLoc;
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001295 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001296 /*isAmbiguous=*/false,
1297 /*LParenLoc=*/NoLoc,
Craig Topper161e4db2014-05-21 06:02:52 +00001298 /*Params=*/nullptr,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001299 /*NumParams=*/0,
1300 /*EllipsisLoc=*/NoLoc,
1301 /*RParenLoc=*/NoLoc,
1302 /*TypeQuals=*/0,
1303 /*RefQualifierIsLValueRef=*/true,
1304 /*RefQualifierLoc=*/NoLoc,
1305 /*ConstQualifierLoc=*/NoLoc,
1306 /*VolatileQualifierLoc=*/NoLoc,
Hal Finkel23a07392014-10-20 17:32:04 +00001307 /*RestrictQualifierLoc=*/NoLoc,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001308 MutableLoc,
1309 EST_None,
Nathan Wilsone23a9a42015-08-26 04:19:36 +00001310 /*ESpecRange=*/SourceRange(),
Craig Topper161e4db2014-05-21 06:02:52 +00001311 /*Exceptions=*/nullptr,
1312 /*ExceptionRanges=*/nullptr,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001313 /*NumExceptions=*/0,
Craig Topper161e4db2014-05-21 06:02:52 +00001314 /*NoexceptExpr=*/nullptr,
Richard Smith0b3a4622014-11-13 20:01:57 +00001315 /*ExceptionSpecTokens=*/nullptr,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001316 DeclLoc, DeclEndLoc, D,
1317 TrailingReturnType),
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001318 Attr, DeclEndLoc);
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001319 }
1320
Eli Friedman4817cf72012-01-06 03:05:34 +00001321 // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
1322 // it.
Douglas Gregorb8389972012-02-21 22:51:27 +00001323 unsigned ScopeFlags = Scope::BlockScope | Scope::FnScope | Scope::DeclScope;
Douglas Gregorb8389972012-02-21 22:51:27 +00001324 ParseScope BodyScope(this, ScopeFlags);
Eli Friedman4817cf72012-01-06 03:05:34 +00001325
Eli Friedman71c80552012-01-05 03:35:19 +00001326 Actions.ActOnStartOfLambdaDefinition(Intro, D, getCurScope());
1327
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001328 // Parse compound-statement.
Eli Friedmanc7c97142012-01-04 02:40:39 +00001329 if (!Tok.is(tok::l_brace)) {
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001330 Diag(Tok, diag::err_expected_lambda_body);
Eli Friedmanc7c97142012-01-04 02:40:39 +00001331 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
1332 return ExprError();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001333 }
1334
Eli Friedmanc7c97142012-01-04 02:40:39 +00001335 StmtResult Stmt(ParseCompoundStatementBody());
1336 BodyScope.Exit();
1337
David Majnemere01c4662015-01-09 05:10:55 +00001338 if (!Stmt.isInvalid() && !TrailingReturnType.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001339 return Actions.ActOnLambdaExpr(LambdaBeginLoc, Stmt.get(), getCurScope());
David Majnemere01c4662015-01-09 05:10:55 +00001340
Eli Friedman898caf82012-01-04 02:46:53 +00001341 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
1342 return ExprError();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001343}
1344
Chris Lattner29375652006-12-04 18:06:35 +00001345/// ParseCXXCasts - This handles the various ways to cast expressions to another
1346/// type.
1347///
1348/// postfix-expression: [C++ 5.2p1]
1349/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
1350/// 'static_cast' '<' type-name '>' '(' expression ')'
1351/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
1352/// 'const_cast' '<' type-name '>' '(' expression ')'
1353///
John McCalldadc5752010-08-24 06:29:42 +00001354ExprResult Parser::ParseCXXCasts() {
Chris Lattner29375652006-12-04 18:06:35 +00001355 tok::TokenKind Kind = Tok.getKind();
Craig Topper161e4db2014-05-21 06:02:52 +00001356 const char *CastName = nullptr; // For error messages
Chris Lattner29375652006-12-04 18:06:35 +00001357
1358 switch (Kind) {
David Blaikieaa347f92011-09-23 20:26:49 +00001359 default: llvm_unreachable("Unknown C++ cast!");
Chris Lattner29375652006-12-04 18:06:35 +00001360 case tok::kw_const_cast: CastName = "const_cast"; break;
1361 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
1362 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
1363 case tok::kw_static_cast: CastName = "static_cast"; break;
1364 }
1365
1366 SourceLocation OpLoc = ConsumeToken();
1367 SourceLocation LAngleBracketLoc = Tok.getLocation();
1368
Richard Smith55858492011-04-14 21:45:45 +00001369 // Check for "<::" which is parsed as "[:". If found, fix token stream,
1370 // diagnose error, suggest fix, and recover parsing.
Richard Smith62e66302012-08-20 17:37:52 +00001371 if (Tok.is(tok::l_square) && Tok.getLength() == 2) {
1372 Token Next = NextToken();
1373 if (Next.is(tok::colon) && areTokensAdjacent(Tok, Next))
1374 FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
1375 }
Richard Smith55858492011-04-14 21:45:45 +00001376
Chris Lattner29375652006-12-04 18:06:35 +00001377 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redld65cea82008-12-11 22:51:44 +00001378 return ExprError();
Chris Lattner29375652006-12-04 18:06:35 +00001379
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +00001380 // Parse the common declaration-specifiers piece.
1381 DeclSpec DS(AttrFactory);
1382 ParseSpecifierQualifierList(DS);
1383
1384 // Parse the abstract-declarator, if present.
1385 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1386 ParseDeclarator(DeclaratorInfo);
1387
Chris Lattner29375652006-12-04 18:06:35 +00001388 SourceLocation RAngleBracketLoc = Tok.getLocation();
1389
Alp Toker383d2c42014-01-01 03:08:43 +00001390 if (ExpectAndConsume(tok::greater))
Alp Tokerec543272013-12-24 09:48:30 +00001391 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << tok::less);
Chris Lattner29375652006-12-04 18:06:35 +00001392
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001393 SourceLocation LParenLoc, RParenLoc;
1394 BalancedDelimiterTracker T(*this, tok::l_paren);
Chris Lattner29375652006-12-04 18:06:35 +00001395
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001396 if (T.expectAndConsume(diag::err_expected_lparen_after, CastName))
Argyrios Kyrtzidis387a3342009-05-22 10:23:16 +00001397 return ExprError();
Chris Lattner29375652006-12-04 18:06:35 +00001398
John McCalldadc5752010-08-24 06:29:42 +00001399 ExprResult Result = ParseExpression();
Mike Stump11289f42009-09-09 15:08:12 +00001400
Argyrios Kyrtzidis387a3342009-05-22 10:23:16 +00001401 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001402 T.consumeClose();
Chris Lattner29375652006-12-04 18:06:35 +00001403
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +00001404 if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
Douglas Gregore200adc2008-10-27 19:41:14 +00001405 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +00001406 LAngleBracketLoc, DeclaratorInfo,
Douglas Gregor220cac52009-02-18 17:45:20 +00001407 RAngleBracketLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001408 T.getOpenLocation(), Result.get(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001409 T.getCloseLocation());
Chris Lattner29375652006-12-04 18:06:35 +00001410
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001411 return Result;
Chris Lattner29375652006-12-04 18:06:35 +00001412}
Bill Wendling4073ed52007-02-13 01:51:42 +00001413
Sebastian Redlc4704762008-11-11 11:37:55 +00001414/// ParseCXXTypeid - This handles the C++ typeid expression.
1415///
1416/// postfix-expression: [C++ 5.2p1]
1417/// 'typeid' '(' expression ')'
1418/// 'typeid' '(' type-id ')'
1419///
John McCalldadc5752010-08-24 06:29:42 +00001420ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc4704762008-11-11 11:37:55 +00001421 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
1422
1423 SourceLocation OpLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001424 SourceLocation LParenLoc, RParenLoc;
1425 BalancedDelimiterTracker T(*this, tok::l_paren);
Sebastian Redlc4704762008-11-11 11:37:55 +00001426
1427 // typeid expressions are always parenthesized.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001428 if (T.expectAndConsume(diag::err_expected_lparen_after, "typeid"))
Sebastian Redld65cea82008-12-11 22:51:44 +00001429 return ExprError();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001430 LParenLoc = T.getOpenLocation();
Sebastian Redlc4704762008-11-11 11:37:55 +00001431
John McCalldadc5752010-08-24 06:29:42 +00001432 ExprResult Result;
Sebastian Redlc4704762008-11-11 11:37:55 +00001433
Richard Smith4f605af2012-08-18 00:55:03 +00001434 // C++0x [expr.typeid]p3:
1435 // When typeid is applied to an expression other than an lvalue of a
1436 // polymorphic class type [...] The expression is an unevaluated
1437 // operand (Clause 5).
1438 //
1439 // Note that we can't tell whether the expression is an lvalue of a
1440 // polymorphic class type until after we've parsed the expression; we
1441 // speculatively assume the subexpression is unevaluated, and fix it up
1442 // later.
1443 //
1444 // We enter the unevaluated context before trying to determine whether we
1445 // have a type-id, because the tentative parse logic will try to resolve
1446 // names, and must treat them as unevaluated.
Eli Friedman15681d62012-09-26 04:34:21 +00001447 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
1448 Sema::ReuseLambdaContextDecl);
Richard Smith4f605af2012-08-18 00:55:03 +00001449
Sebastian Redlc4704762008-11-11 11:37:55 +00001450 if (isTypeIdInParens()) {
Douglas Gregor220cac52009-02-18 17:45:20 +00001451 TypeResult Ty = ParseTypeName();
Sebastian Redlc4704762008-11-11 11:37:55 +00001452
1453 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001454 T.consumeClose();
1455 RParenLoc = T.getCloseLocation();
Douglas Gregor4c7c1092010-09-08 23:14:30 +00001456 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +00001457 return ExprError();
Sebastian Redlc4704762008-11-11 11:37:55 +00001458
1459 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallba7bf592010-08-24 05:47:05 +00001460 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc4704762008-11-11 11:37:55 +00001461 } else {
1462 Result = ParseExpression();
1463
1464 // Match the ')'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001465 if (Result.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00001466 SkipUntil(tok::r_paren, StopAtSemi);
Sebastian Redlc4704762008-11-11 11:37:55 +00001467 else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001468 T.consumeClose();
1469 RParenLoc = T.getCloseLocation();
Douglas Gregor4c7c1092010-09-08 23:14:30 +00001470 if (RParenLoc.isInvalid())
1471 return ExprError();
Douglas Gregor1beec452011-03-12 01:48:56 +00001472
Sebastian Redlc4704762008-11-11 11:37:55 +00001473 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001474 Result.get(), RParenLoc);
Sebastian Redlc4704762008-11-11 11:37:55 +00001475 }
1476 }
1477
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001478 return Result;
Sebastian Redlc4704762008-11-11 11:37:55 +00001479}
1480
Francois Pichet9f4f2072010-09-08 12:20:18 +00001481/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
1482///
1483/// '__uuidof' '(' expression ')'
1484/// '__uuidof' '(' type-id ')'
1485///
1486ExprResult Parser::ParseCXXUuidof() {
1487 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
1488
1489 SourceLocation OpLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001490 BalancedDelimiterTracker T(*this, tok::l_paren);
Francois Pichet9f4f2072010-09-08 12:20:18 +00001491
1492 // __uuidof expressions are always parenthesized.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001493 if (T.expectAndConsume(diag::err_expected_lparen_after, "__uuidof"))
Francois Pichet9f4f2072010-09-08 12:20:18 +00001494 return ExprError();
1495
1496 ExprResult Result;
1497
1498 if (isTypeIdInParens()) {
1499 TypeResult Ty = ParseTypeName();
1500
1501 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001502 T.consumeClose();
Francois Pichet9f4f2072010-09-08 12:20:18 +00001503
1504 if (Ty.isInvalid())
1505 return ExprError();
1506
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001507 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), /*isType=*/true,
1508 Ty.get().getAsOpaquePtr(),
1509 T.getCloseLocation());
Francois Pichet9f4f2072010-09-08 12:20:18 +00001510 } else {
1511 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1512 Result = ParseExpression();
1513
1514 // Match the ')'.
1515 if (Result.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00001516 SkipUntil(tok::r_paren, StopAtSemi);
Francois Pichet9f4f2072010-09-08 12:20:18 +00001517 else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001518 T.consumeClose();
Francois Pichet9f4f2072010-09-08 12:20:18 +00001519
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001520 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(),
1521 /*isType=*/false,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001522 Result.get(), T.getCloseLocation());
Francois Pichet9f4f2072010-09-08 12:20:18 +00001523 }
1524 }
1525
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001526 return Result;
Francois Pichet9f4f2072010-09-08 12:20:18 +00001527}
1528
Douglas Gregore610ada2010-02-24 18:44:31 +00001529/// \brief Parse a C++ pseudo-destructor expression after the base,
1530/// . or -> operator, and nested-name-specifier have already been
1531/// parsed.
1532///
1533/// postfix-expression: [C++ 5.2]
1534/// postfix-expression . pseudo-destructor-name
1535/// postfix-expression -> pseudo-destructor-name
1536///
1537/// pseudo-destructor-name:
1538/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
1539/// ::[opt] nested-name-specifier template simple-template-id ::
1540/// ~type-name
1541/// ::[opt] nested-name-specifier[opt] ~type-name
1542///
John McCalldadc5752010-08-24 06:29:42 +00001543ExprResult
Craig Toppera2c51532014-10-30 05:30:05 +00001544Parser::ParseCXXPseudoDestructor(Expr *Base, SourceLocation OpLoc,
Douglas Gregore610ada2010-02-24 18:44:31 +00001545 tok::TokenKind OpKind,
1546 CXXScopeSpec &SS,
John McCallba7bf592010-08-24 05:47:05 +00001547 ParsedType ObjectType) {
Douglas Gregore610ada2010-02-24 18:44:31 +00001548 // We're parsing either a pseudo-destructor-name or a dependent
1549 // member access that has the same form as a
1550 // pseudo-destructor-name. We parse both in the same way and let
1551 // the action model sort them out.
1552 //
1553 // Note that the ::[opt] nested-name-specifier[opt] has already
1554 // been parsed, and if there was a simple-template-id, it has
1555 // been coalesced into a template-id annotation token.
1556 UnqualifiedId FirstTypeName;
1557 SourceLocation CCLoc;
1558 if (Tok.is(tok::identifier)) {
1559 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1560 ConsumeToken();
1561 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1562 CCLoc = ConsumeToken();
1563 } else if (Tok.is(tok::annot_template_id)) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001564 // FIXME: retrieve TemplateKWLoc from template-id annotation and
1565 // store it in the pseudo-dtor node (to be used when instantiating it).
Douglas Gregore610ada2010-02-24 18:44:31 +00001566 FirstTypeName.setTemplateId(
1567 (TemplateIdAnnotation *)Tok.getAnnotationValue());
1568 ConsumeToken();
1569 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1570 CCLoc = ConsumeToken();
1571 } else {
Craig Topper161e4db2014-05-21 06:02:52 +00001572 FirstTypeName.setIdentifier(nullptr, SourceLocation());
Douglas Gregore610ada2010-02-24 18:44:31 +00001573 }
1574
1575 // Parse the tilde.
1576 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
1577 SourceLocation TildeLoc = ConsumeToken();
David Blaikie1d578782011-12-16 16:03:09 +00001578
1579 if (Tok.is(tok::kw_decltype) && !FirstTypeName.isValid() && SS.isEmpty()) {
1580 DeclSpec DS(AttrFactory);
Benjamin Kramer198e0832011-12-18 12:18:02 +00001581 ParseDecltypeSpecifier(DS);
David Blaikie1d578782011-12-16 16:03:09 +00001582 if (DS.getTypeSpecType() == TST_error)
1583 return ExprError();
David Majnemerced8bdf2015-02-25 17:36:15 +00001584 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, OpLoc, OpKind,
1585 TildeLoc, DS);
David Blaikie1d578782011-12-16 16:03:09 +00001586 }
1587
Douglas Gregore610ada2010-02-24 18:44:31 +00001588 if (!Tok.is(tok::identifier)) {
1589 Diag(Tok, diag::err_destructor_tilde_identifier);
1590 return ExprError();
1591 }
1592
1593 // Parse the second type.
1594 UnqualifiedId SecondTypeName;
1595 IdentifierInfo *Name = Tok.getIdentifierInfo();
1596 SourceLocation NameLoc = ConsumeToken();
1597 SecondTypeName.setIdentifier(Name, NameLoc);
1598
1599 // If there is a '<', the second type name is a template-id. Parse
1600 // it as such.
1601 if (Tok.is(tok::less) &&
Abramo Bagnara7945c982012-01-27 09:46:47 +00001602 ParseUnqualifiedIdTemplateId(SS, SourceLocation(),
1603 Name, NameLoc,
1604 false, ObjectType, SecondTypeName,
1605 /*AssumeTemplateName=*/true))
Douglas Gregore610ada2010-02-24 18:44:31 +00001606 return ExprError();
1607
David Majnemerced8bdf2015-02-25 17:36:15 +00001608 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, OpLoc, OpKind,
1609 SS, FirstTypeName, CCLoc, TildeLoc,
1610 SecondTypeName);
Douglas Gregore610ada2010-02-24 18:44:31 +00001611}
1612
Bill Wendling4073ed52007-02-13 01:51:42 +00001613/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
1614///
1615/// boolean-literal: [C++ 2.13.5]
1616/// 'true'
1617/// 'false'
John McCalldadc5752010-08-24 06:29:42 +00001618ExprResult Parser::ParseCXXBoolLiteral() {
Bill Wendling4073ed52007-02-13 01:51:42 +00001619 tok::TokenKind Kind = Tok.getKind();
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001620 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Bill Wendling4073ed52007-02-13 01:51:42 +00001621}
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001622
1623/// ParseThrowExpression - This handles the C++ throw expression.
1624///
1625/// throw-expression: [C++ 15]
1626/// 'throw' assignment-expression[opt]
John McCalldadc5752010-08-24 06:29:42 +00001627ExprResult Parser::ParseThrowExpression() {
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001628 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001629 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redld65cea82008-12-11 22:51:44 +00001630
Chris Lattner65dd8432008-04-06 06:02:23 +00001631 // If the current token isn't the start of an assignment-expression,
1632 // then the expression is not present. This handles things like:
1633 // "C ? throw : (void)42", which is crazy but legal.
1634 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
1635 case tok::semi:
1636 case tok::r_paren:
1637 case tok::r_square:
1638 case tok::r_brace:
1639 case tok::colon:
1640 case tok::comma:
Craig Topper161e4db2014-05-21 06:02:52 +00001641 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, nullptr);
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001642
Chris Lattner65dd8432008-04-06 06:02:23 +00001643 default:
John McCalldadc5752010-08-24 06:29:42 +00001644 ExprResult Expr(ParseAssignmentExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001645 if (Expr.isInvalid()) return Expr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001646 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.get());
Chris Lattner65dd8432008-04-06 06:02:23 +00001647 }
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001648}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001649
Richard Smith0e304ea2015-10-22 04:46:14 +00001650/// \brief Parse the C++ Coroutines co_yield expression.
1651///
1652/// co_yield-expression:
1653/// 'co_yield' assignment-expression[opt]
1654ExprResult Parser::ParseCoyieldExpression() {
1655 assert(Tok.is(tok::kw_co_yield) && "Not co_yield!");
1656
1657 SourceLocation Loc = ConsumeToken();
Richard Smithae3d1472015-11-20 22:47:10 +00001658 ExprResult Expr = Tok.is(tok::l_brace) ? ParseBraceInitializer()
1659 : ParseAssignmentExpression();
Richard Smithcfd53b42015-10-22 06:13:50 +00001660 if (!Expr.isInvalid())
Richard Smith9f690bd2015-10-27 06:02:45 +00001661 Expr = Actions.ActOnCoyieldExpr(getCurScope(), Loc, Expr.get());
Richard Smith0e304ea2015-10-22 04:46:14 +00001662 return Expr;
1663}
1664
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001665/// ParseCXXThis - This handles the C++ 'this' pointer.
1666///
1667/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
1668/// a non-lvalue expression whose value is the address of the object for which
1669/// the function is called.
John McCalldadc5752010-08-24 06:29:42 +00001670ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001671 assert(Tok.is(tok::kw_this) && "Not 'this'!");
1672 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001673 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001674}
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001675
1676/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1677/// Can be interpreted either as function-style casting ("int(x)")
1678/// or class type construction ("ClassType(x,y,z)")
1679/// or creation of a value-initialized type ("int()").
Sebastian Redl3da34892011-06-05 12:23:16 +00001680/// See [C++ 5.2.3].
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001681///
1682/// postfix-expression: [C++ 5.2p1]
Sebastian Redl3da34892011-06-05 12:23:16 +00001683/// simple-type-specifier '(' expression-list[opt] ')'
1684/// [C++0x] simple-type-specifier braced-init-list
1685/// typename-specifier '(' expression-list[opt] ')'
1686/// [C++0x] typename-specifier braced-init-list
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001687///
John McCalldadc5752010-08-24 06:29:42 +00001688ExprResult
Sebastian Redld65cea82008-12-11 22:51:44 +00001689Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001690 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallba7bf592010-08-24 05:47:05 +00001691 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001692
Sebastian Redl3da34892011-06-05 12:23:16 +00001693 assert((Tok.is(tok::l_paren) ||
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001694 (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)))
Sebastian Redl3da34892011-06-05 12:23:16 +00001695 && "Expected '(' or '{'!");
Douglas Gregor94a32472011-01-11 00:33:19 +00001696
Sebastian Redl3da34892011-06-05 12:23:16 +00001697 if (Tok.is(tok::l_brace)) {
Sebastian Redld74dd492012-02-12 18:41:05 +00001698 ExprResult Init = ParseBraceInitializer();
1699 if (Init.isInvalid())
1700 return Init;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001701 Expr *InitList = Init.get();
Sebastian Redld74dd492012-02-12 18:41:05 +00001702 return Actions.ActOnCXXTypeConstructExpr(TypeRep, SourceLocation(),
1703 MultiExprArg(&InitList, 1),
1704 SourceLocation());
Sebastian Redl3da34892011-06-05 12:23:16 +00001705 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001706 BalancedDelimiterTracker T(*this, tok::l_paren);
1707 T.consumeOpen();
Sebastian Redl3da34892011-06-05 12:23:16 +00001708
Benjamin Kramerf0623432012-08-23 22:51:59 +00001709 ExprVector Exprs;
Sebastian Redl3da34892011-06-05 12:23:16 +00001710 CommaLocsTy CommaLocs;
1711
1712 if (Tok.isNot(tok::r_paren)) {
Francisco Lopes da Silva975a9f62015-01-21 16:24:11 +00001713 if (ParseExpressionList(Exprs, CommaLocs, [&] {
1714 Actions.CodeCompleteConstructor(getCurScope(),
1715 TypeRep.get()->getCanonicalTypeInternal(),
1716 DS.getLocEnd(), Exprs);
1717 })) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001718 SkipUntil(tok::r_paren, StopAtSemi);
Sebastian Redl3da34892011-06-05 12:23:16 +00001719 return ExprError();
1720 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001721 }
Sebastian Redl3da34892011-06-05 12:23:16 +00001722
1723 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001724 T.consumeClose();
Sebastian Redl3da34892011-06-05 12:23:16 +00001725
1726 // TypeRep could be null, if it references an invalid typedef.
1727 if (!TypeRep)
1728 return ExprError();
1729
1730 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1731 "Unexpected number of commas!");
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001732 return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001733 Exprs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001734 T.getCloseLocation());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001735 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001736}
1737
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001738/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001739///
1740/// condition:
1741/// expression
1742/// type-specifier-seq declarator '=' assignment-expression
Richard Smith2a15b742012-02-22 06:49:09 +00001743/// [C++11] type-specifier-seq declarator '=' initializer-clause
1744/// [C++11] type-specifier-seq declarator braced-init-list
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001745/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
1746/// '=' assignment-expression
1747///
Richard Smithc7a05a92016-06-29 21:17:59 +00001748/// In C++1z, a condition may in some contexts be preceded by an
1749/// optional init-statement. This function will parse that too.
1750///
1751/// \param InitStmt If non-null, an init-statement is permitted, and if present
1752/// will be parsed and stored here.
1753///
Douglas Gregore60e41a2010-05-06 17:25:47 +00001754/// \param Loc The location of the start of the statement that requires this
1755/// condition, e.g., the "for" in a for loop.
1756///
Richard Smith03a4aa32016-06-23 19:02:52 +00001757/// \returns The parsed condition.
Richard Smithc7a05a92016-06-29 21:17:59 +00001758Sema::ConditionResult Parser::ParseCXXCondition(StmtResult *InitStmt,
1759 SourceLocation Loc,
Richard Smith03a4aa32016-06-23 19:02:52 +00001760 Sema::ConditionKind CK) {
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001761 if (Tok.is(tok::code_completion)) {
John McCallfaf5fb42010-08-26 23:41:50 +00001762 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001763 cutOffParsing();
Richard Smith03a4aa32016-06-23 19:02:52 +00001764 return Sema::ConditionError();
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001765 }
1766
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001767 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00001768 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001769
Richard Smithc7a05a92016-06-29 21:17:59 +00001770 // Determine what kind of thing we have.
1771 switch (isCXXConditionDeclarationOrInitStatement(InitStmt)) {
1772 case ConditionOrInitStatement::Expression: {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001773 ProhibitAttributes(attrs);
1774
Douglas Gregore60e41a2010-05-06 17:25:47 +00001775 // Parse the expression.
Richard Smith03a4aa32016-06-23 19:02:52 +00001776 ExprResult Expr = ParseExpression(); // expression
1777 if (Expr.isInvalid())
1778 return Sema::ConditionError();
Douglas Gregore60e41a2010-05-06 17:25:47 +00001779
Richard Smithc7a05a92016-06-29 21:17:59 +00001780 if (InitStmt && Tok.is(tok::semi)) {
1781 *InitStmt = Actions.ActOnExprStmt(Expr.get());
1782 ConsumeToken();
1783 return ParseCXXCondition(nullptr, Loc, CK);
1784 }
1785
Richard Smith03a4aa32016-06-23 19:02:52 +00001786 return Actions.ActOnCondition(getCurScope(), Loc, Expr.get(), CK);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001787 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001788
Richard Smithc7a05a92016-06-29 21:17:59 +00001789 case ConditionOrInitStatement::InitStmtDecl: {
Richard Smithfccb5122016-10-18 20:27:16 +00001790 Diag(Tok.getLocation(), getLangOpts().CPlusPlus1z
1791 ? diag::warn_cxx14_compat_init_statement
1792 : diag::ext_init_statement)
1793 << (CK == Sema::ConditionKind::Switch);
Richard Smithc7a05a92016-06-29 21:17:59 +00001794 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
1795 DeclGroupPtrTy DG = ParseSimpleDeclaration(
1796 Declarator::InitStmtContext, DeclEnd, attrs, /*RequireSemi=*/true);
1797 *InitStmt = Actions.ActOnDeclStmt(DG, DeclStart, DeclEnd);
1798 return ParseCXXCondition(nullptr, Loc, CK);
1799 }
1800
1801 case ConditionOrInitStatement::ConditionDecl:
1802 case ConditionOrInitStatement::Error:
1803 break;
1804 }
1805
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001806 // type-specifier-seq
John McCall084e83d2011-03-24 11:26:52 +00001807 DeclSpec DS(AttrFactory);
Richard Smith54ecd982013-02-20 19:22:51 +00001808 DS.takeAttributesFrom(attrs);
Meador Ingef0af05c2015-06-25 22:06:40 +00001809 ParseSpecifierQualifierList(DS, AS_none, DSC_condition);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001810
1811 // declarator
1812 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
1813 ParseDeclarator(DeclaratorInfo);
1814
1815 // simple-asm-expr[opt]
1816 if (Tok.is(tok::kw_asm)) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001817 SourceLocation Loc;
John McCalldadc5752010-08-24 06:29:42 +00001818 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001819 if (AsmLabel.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001820 SkipUntil(tok::semi, StopAtSemi);
Richard Smith03a4aa32016-06-23 19:02:52 +00001821 return Sema::ConditionError();
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001822 }
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001823 DeclaratorInfo.setAsmLabel(AsmLabel.get());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001824 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001825 }
1826
1827 // If attributes are present, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001828 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001829
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001830 // Type-check the declaration itself.
John McCalldadc5752010-08-24 06:29:42 +00001831 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall53fa7142010-12-24 02:08:15 +00001832 DeclaratorInfo);
Richard Smith03a4aa32016-06-23 19:02:52 +00001833 if (Dcl.isInvalid())
1834 return Sema::ConditionError();
1835 Decl *DeclOut = Dcl.get();
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +00001836
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001837 // '=' assignment-expression
Richard Trieuc64d3232012-01-18 22:54:52 +00001838 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Smith2a15b742012-02-22 06:49:09 +00001839 bool CopyInitialization = isTokenEqualOrEqualTypo();
1840 if (CopyInitialization)
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001841 ConsumeToken();
Richard Smith2a15b742012-02-22 06:49:09 +00001842
1843 ExprResult InitExpr = ExprError();
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001844 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith2a15b742012-02-22 06:49:09 +00001845 Diag(Tok.getLocation(),
1846 diag::warn_cxx98_compat_generalized_initializer_lists);
1847 InitExpr = ParseBraceInitializer();
1848 } else if (CopyInitialization) {
1849 InitExpr = ParseAssignmentExpression();
1850 } else if (Tok.is(tok::l_paren)) {
1851 // This was probably an attempt to initialize the variable.
1852 SourceLocation LParen = ConsumeParen(), RParen = LParen;
Alexey Bataevee6507d2013-11-18 08:17:37 +00001853 if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch))
Richard Smith2a15b742012-02-22 06:49:09 +00001854 RParen = ConsumeParen();
Richard Smith03a4aa32016-06-23 19:02:52 +00001855 Diag(DeclOut->getLocation(),
Richard Smith2a15b742012-02-22 06:49:09 +00001856 diag::err_expected_init_in_condition_lparen)
1857 << SourceRange(LParen, RParen);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001858 } else {
Richard Smith03a4aa32016-06-23 19:02:52 +00001859 Diag(DeclOut->getLocation(), diag::err_expected_init_in_condition);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001860 }
Richard Smith2a15b742012-02-22 06:49:09 +00001861
1862 if (!InitExpr.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001863 Actions.AddInitializerToDecl(DeclOut, InitExpr.get(), !CopyInitialization,
Richard Smith74aeef52013-04-26 16:15:35 +00001864 DS.containsPlaceholderType());
Richard Smith27d807c2013-04-30 13:56:41 +00001865 else
1866 Actions.ActOnInitializerError(DeclOut);
Richard Smith2a15b742012-02-22 06:49:09 +00001867
Richard Smithb2bc2e62011-02-21 20:05:19 +00001868 Actions.FinalizeDeclaration(DeclOut);
Richard Smith03a4aa32016-06-23 19:02:52 +00001869 return Actions.ActOnConditionVariable(DeclOut, Loc, CK);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001870}
1871
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001872/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1873/// This should only be called when the current token is known to be part of
1874/// simple-type-specifier.
1875///
1876/// simple-type-specifier:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001877/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001878/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1879/// char
1880/// wchar_t
1881/// bool
1882/// short
1883/// int
1884/// long
1885/// signed
1886/// unsigned
1887/// float
1888/// double
1889/// void
1890/// [GNU] typeof-specifier
1891/// [C++0x] auto [TODO]
1892///
1893/// type-name:
1894/// class-name
1895/// enum-name
1896/// typedef-name
1897///
1898void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1899 DS.SetRangeStart(Tok.getLocation());
1900 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +00001901 unsigned DiagID;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001902 SourceLocation Loc = Tok.getLocation();
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001903 const clang::PrintingPolicy &Policy =
1904 Actions.getASTContext().getPrintingPolicy();
Mike Stump11289f42009-09-09 15:08:12 +00001905
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001906 switch (Tok.getKind()) {
Chris Lattner45ddec32009-01-05 00:13:00 +00001907 case tok::identifier: // foo::bar
1908 case tok::coloncolon: // ::foo::bar
David Blaikie83d382b2011-09-23 05:06:16 +00001909 llvm_unreachable("Annotation token should already be formed!");
Mike Stump11289f42009-09-09 15:08:12 +00001910 default:
David Blaikie83d382b2011-09-23 05:06:16 +00001911 llvm_unreachable("Not a simple-type-specifier token!");
Chris Lattner45ddec32009-01-05 00:13:00 +00001912
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001913 // type-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00001914 case tok::annot_typename: {
Douglas Gregor0231d8d2011-01-19 20:10:05 +00001915 if (getTypeAnnotation(Tok))
1916 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001917 getTypeAnnotation(Tok), Policy);
Douglas Gregor0231d8d2011-01-19 20:10:05 +00001918 else
1919 DS.SetTypeSpecError();
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001920
1921 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1922 ConsumeToken();
1923
Craig Topper25122412015-11-15 03:32:11 +00001924 DS.Finish(Actions, Policy);
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001925 return;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001926 }
Mike Stump11289f42009-09-09 15:08:12 +00001927
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001928 // builtin types
1929 case tok::kw_short:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001930 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001931 break;
1932 case tok::kw_long:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001933 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001934 break;
Francois Pichet84133e42011-04-28 01:59:37 +00001935 case tok::kw___int64:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001936 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID, Policy);
Francois Pichet84133e42011-04-28 01:59:37 +00001937 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001938 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001939 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001940 break;
1941 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001942 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001943 break;
1944 case tok::kw_void:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001945 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001946 break;
1947 case tok::kw_char:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001948 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001949 break;
1950 case tok::kw_int:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001951 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001952 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00001953 case tok::kw___int128:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001954 DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, DiagID, Policy);
Richard Smithf016bbc2012-04-04 06:24:32 +00001955 break;
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00001956 case tok::kw_half:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001957 DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID, Policy);
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00001958 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001959 case tok::kw_float:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001960 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001961 break;
1962 case tok::kw_double:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001963 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001964 break;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00001965 case tok::kw___float128:
1966 DS.SetTypeSpecType(DeclSpec::TST_float128, Loc, PrevSpec, DiagID, Policy);
1967 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001968 case tok::kw_wchar_t:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001969 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001970 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001971 case tok::kw_char16_t:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001972 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID, Policy);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001973 break;
1974 case tok::kw_char32_t:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001975 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID, Policy);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001976 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001977 case tok::kw_bool:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001978 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001979 break;
David Blaikie25896afb2012-01-24 05:47:35 +00001980 case tok::annot_decltype:
1981 case tok::kw_decltype:
1982 DS.SetRangeEnd(ParseDecltypeSpecifier(DS));
Craig Topper25122412015-11-15 03:32:11 +00001983 return DS.Finish(Actions, Policy);
Mike Stump11289f42009-09-09 15:08:12 +00001984
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001985 // GNU typeof support.
1986 case tok::kw_typeof:
1987 ParseTypeofSpecifier(DS);
Craig Topper25122412015-11-15 03:32:11 +00001988 DS.Finish(Actions, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001989 return;
1990 }
Chris Lattnera8a3f732009-01-06 05:06:21 +00001991 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001992 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1993 else
1994 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001995 ConsumeToken();
Craig Topper25122412015-11-15 03:32:11 +00001996 DS.Finish(Actions, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001997}
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001998
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001999/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
2000/// [dcl.name]), which is a non-empty sequence of type-specifiers,
2001/// e.g., "const short int". Note that the DeclSpec is *not* finished
2002/// by parsing the type-specifier-seq, because these sequences are
2003/// typically followed by some form of declarator. Returns true and
2004/// emits diagnostics if this is not a type-specifier-seq, false
2005/// otherwise.
2006///
2007/// type-specifier-seq: [C++ 8.1]
2008/// type-specifier type-specifier-seq[opt]
2009///
2010bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
Richard Smithc5b05522012-03-12 07:56:15 +00002011 ParseSpecifierQualifierList(DS, AS_none, DSC_type_specifier);
Craig Topper25122412015-11-15 03:32:11 +00002012 DS.Finish(Actions, Actions.getASTContext().getPrintingPolicy());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002013 return false;
2014}
2015
Douglas Gregor7861a802009-11-03 01:35:08 +00002016/// \brief Finish parsing a C++ unqualified-id that is a template-id of
2017/// some form.
2018///
2019/// This routine is invoked when a '<' is encountered after an identifier or
2020/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
2021/// whether the unqualified-id is actually a template-id. This routine will
2022/// then parse the template arguments and form the appropriate template-id to
2023/// return to the caller.
2024///
2025/// \param SS the nested-name-specifier that precedes this template-id, if
2026/// we're actually parsing a qualified-id.
2027///
2028/// \param Name for constructor and destructor names, this is the actual
2029/// identifier that may be a template-name.
2030///
2031/// \param NameLoc the location of the class-name in a constructor or
2032/// destructor.
2033///
2034/// \param EnteringContext whether we're entering the scope of the
2035/// nested-name-specifier.
2036///
Douglas Gregor127ea592009-11-03 21:24:04 +00002037/// \param ObjectType if this unqualified-id occurs within a member access
2038/// expression, the type of the base object whose member is being accessed.
2039///
Douglas Gregor7861a802009-11-03 01:35:08 +00002040/// \param Id as input, describes the template-name or operator-function-id
2041/// that precedes the '<'. If template arguments were parsed successfully,
2042/// will be updated with the template-id.
2043///
Douglas Gregore610ada2010-02-24 18:44:31 +00002044/// \param AssumeTemplateId When true, this routine will assume that the name
2045/// refers to a template without performing name lookup to verify.
2046///
Douglas Gregor7861a802009-11-03 01:35:08 +00002047/// \returns true if a parse error occurred, false otherwise.
2048bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
Abramo Bagnara7945c982012-01-27 09:46:47 +00002049 SourceLocation TemplateKWLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00002050 IdentifierInfo *Name,
2051 SourceLocation NameLoc,
2052 bool EnteringContext,
John McCallba7bf592010-08-24 05:47:05 +00002053 ParsedType ObjectType,
Douglas Gregore610ada2010-02-24 18:44:31 +00002054 UnqualifiedId &Id,
Abramo Bagnara7945c982012-01-27 09:46:47 +00002055 bool AssumeTemplateId) {
Douglas Gregorb22ee882010-05-05 05:58:24 +00002056 assert((AssumeTemplateId || Tok.is(tok::less)) &&
2057 "Expected '<' to finish parsing a template-id");
Douglas Gregor7861a802009-11-03 01:35:08 +00002058
2059 TemplateTy Template;
2060 TemplateNameKind TNK = TNK_Non_template;
2061 switch (Id.getKind()) {
2062 case UnqualifiedId::IK_Identifier:
Douglas Gregor3cf81312009-11-03 23:16:33 +00002063 case UnqualifiedId::IK_OperatorFunctionId:
Alexis Hunted0530f2009-11-28 08:58:14 +00002064 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregore610ada2010-02-24 18:44:31 +00002065 if (AssumeTemplateId) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00002066 TNK = Actions.ActOnDependentTemplateName(getCurScope(), SS, TemplateKWLoc,
Douglas Gregorbb119652010-06-16 23:00:59 +00002067 Id, ObjectType, EnteringContext,
2068 Template);
2069 if (TNK == TNK_Non_template)
2070 return true;
Douglas Gregor786123d2010-05-21 23:18:07 +00002071 } else {
2072 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00002073 TNK = Actions.isTemplateName(getCurScope(), SS,
2074 TemplateKWLoc.isValid(), Id,
2075 ObjectType, EnteringContext, Template,
Douglas Gregor786123d2010-05-21 23:18:07 +00002076 MemberOfUnknownSpecialization);
2077
2078 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
2079 ObjectType && IsTemplateArgumentList()) {
2080 // We have something like t->getAs<T>(), where getAs is a
2081 // member of an unknown specialization. However, this will only
2082 // parse correctly as a template, so suggest the keyword 'template'
2083 // before 'getAs' and treat this as a dependent template name.
2084 std::string Name;
2085 if (Id.getKind() == UnqualifiedId::IK_Identifier)
2086 Name = Id.Identifier->getName();
2087 else {
2088 Name = "operator ";
2089 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
2090 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
2091 else
2092 Name += Id.Identifier->getName();
2093 }
2094 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
2095 << Name
2096 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Abramo Bagnara7945c982012-01-27 09:46:47 +00002097 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
2098 SS, TemplateKWLoc, Id,
2099 ObjectType, EnteringContext,
2100 Template);
Douglas Gregorbb119652010-06-16 23:00:59 +00002101 if (TNK == TNK_Non_template)
Douglas Gregor786123d2010-05-21 23:18:07 +00002102 return true;
2103 }
2104 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002105 break;
2106
Douglas Gregor3cf81312009-11-03 23:16:33 +00002107 case UnqualifiedId::IK_ConstructorName: {
2108 UnqualifiedId TemplateName;
Douglas Gregor786123d2010-05-21 23:18:07 +00002109 bool MemberOfUnknownSpecialization;
Douglas Gregor3cf81312009-11-03 23:16:33 +00002110 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00002111 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
2112 TemplateName, ObjectType,
Douglas Gregor786123d2010-05-21 23:18:07 +00002113 EnteringContext, Template,
2114 MemberOfUnknownSpecialization);
Douglas Gregor7861a802009-11-03 01:35:08 +00002115 break;
2116 }
2117
Douglas Gregor3cf81312009-11-03 23:16:33 +00002118 case UnqualifiedId::IK_DestructorName: {
2119 UnqualifiedId TemplateName;
Douglas Gregor786123d2010-05-21 23:18:07 +00002120 bool MemberOfUnknownSpecialization;
Douglas Gregor3cf81312009-11-03 23:16:33 +00002121 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002122 if (ObjectType) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00002123 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
2124 SS, TemplateKWLoc, TemplateName,
2125 ObjectType, EnteringContext,
2126 Template);
Douglas Gregorbb119652010-06-16 23:00:59 +00002127 if (TNK == TNK_Non_template)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002128 return true;
2129 } else {
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00002130 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
2131 TemplateName, ObjectType,
Douglas Gregor786123d2010-05-21 23:18:07 +00002132 EnteringContext, Template,
2133 MemberOfUnknownSpecialization);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002134
John McCallba7bf592010-08-24 05:47:05 +00002135 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00002136 Diag(NameLoc, diag::err_destructor_template_id)
2137 << Name << SS.getRange();
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002138 return true;
2139 }
2140 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002141 break;
Douglas Gregor3cf81312009-11-03 23:16:33 +00002142 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002143
2144 default:
2145 return false;
2146 }
2147
2148 if (TNK == TNK_Non_template)
2149 return false;
2150
2151 // Parse the enclosed template argument list.
2152 SourceLocation LAngleLoc, RAngleLoc;
2153 TemplateArgList TemplateArgs;
Douglas Gregorb22ee882010-05-05 05:58:24 +00002154 if (Tok.is(tok::less) &&
2155 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregore7c20652011-03-02 00:47:37 +00002156 SS, true, LAngleLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00002157 TemplateArgs,
Douglas Gregor7861a802009-11-03 01:35:08 +00002158 RAngleLoc))
2159 return true;
2160
2161 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Alexis Hunted0530f2009-11-28 08:58:14 +00002162 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
2163 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor7861a802009-11-03 01:35:08 +00002164 // Form a parsed representation of the template-id to be stored in the
2165 // UnqualifiedId.
2166 TemplateIdAnnotation *TemplateId
Benjamin Kramer1e6b6062012-04-14 12:14:03 +00002167 = TemplateIdAnnotation::Allocate(TemplateArgs.size(), TemplateIds);
Douglas Gregor7861a802009-11-03 01:35:08 +00002168
Richard Smith72bfbd82013-12-04 00:28:23 +00002169 // FIXME: Store name for literal operator too.
Douglas Gregor7861a802009-11-03 01:35:08 +00002170 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
2171 TemplateId->Name = Id.Identifier;
Douglas Gregor3cf81312009-11-03 23:16:33 +00002172 TemplateId->Operator = OO_None;
Douglas Gregor7861a802009-11-03 01:35:08 +00002173 TemplateId->TemplateNameLoc = Id.StartLocation;
2174 } else {
Craig Topper161e4db2014-05-21 06:02:52 +00002175 TemplateId->Name = nullptr;
Douglas Gregor3cf81312009-11-03 23:16:33 +00002176 TemplateId->Operator = Id.OperatorFunctionId.Operator;
2177 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor7861a802009-11-03 01:35:08 +00002178 }
2179
Douglas Gregore7c20652011-03-02 00:47:37 +00002180 TemplateId->SS = SS;
Benjamin Kramer807c2db2012-02-19 23:37:39 +00002181 TemplateId->TemplateKWLoc = TemplateKWLoc;
John McCall3e56fd42010-08-23 07:28:44 +00002182 TemplateId->Template = Template;
Douglas Gregor7861a802009-11-03 01:35:08 +00002183 TemplateId->Kind = TNK;
2184 TemplateId->LAngleLoc = LAngleLoc;
2185 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregorb53edfb2009-11-10 19:49:08 +00002186 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor7861a802009-11-03 01:35:08 +00002187 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregorb53edfb2009-11-10 19:49:08 +00002188 Arg != ArgEnd; ++Arg)
Douglas Gregor7861a802009-11-03 01:35:08 +00002189 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor7861a802009-11-03 01:35:08 +00002190
2191 Id.setTemplateId(TemplateId);
2192 return false;
2193 }
2194
2195 // Bundle the template arguments together.
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00002196 ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs);
Abramo Bagnara4244b432012-01-27 08:46:19 +00002197
Douglas Gregor7861a802009-11-03 01:35:08 +00002198 // Constructor and destructor names.
John McCallfaf5fb42010-08-26 23:41:50 +00002199 TypeResult Type
Abramo Bagnara48c05be2012-02-06 14:41:24 +00002200 = Actions.ActOnTemplateIdType(SS, TemplateKWLoc,
2201 Template, NameLoc,
Abramo Bagnara4244b432012-01-27 08:46:19 +00002202 LAngleLoc, TemplateArgsPtr, RAngleLoc,
2203 /*IsCtorOrDtorName=*/true);
Douglas Gregor7861a802009-11-03 01:35:08 +00002204 if (Type.isInvalid())
2205 return true;
2206
2207 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
2208 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
2209 else
2210 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
2211
2212 return false;
2213}
2214
Douglas Gregor71395fa2009-11-04 00:56:37 +00002215/// \brief Parse an operator-function-id or conversion-function-id as part
2216/// of a C++ unqualified-id.
2217///
2218/// This routine is responsible only for parsing the operator-function-id or
2219/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor7861a802009-11-03 01:35:08 +00002220///
2221/// \code
Douglas Gregor7861a802009-11-03 01:35:08 +00002222/// operator-function-id: [C++ 13.5]
2223/// 'operator' operator
2224///
Douglas Gregor71395fa2009-11-04 00:56:37 +00002225/// operator: one of
Douglas Gregor7861a802009-11-03 01:35:08 +00002226/// new delete new[] delete[]
2227/// + - * / % ^ & | ~
2228/// ! = < > += -= *= /= %=
2229/// ^= &= |= << >> >>= <<= == !=
2230/// <= >= && || ++ -- , ->* ->
2231/// () []
2232///
2233/// conversion-function-id: [C++ 12.3.2]
2234/// operator conversion-type-id
2235///
2236/// conversion-type-id:
2237/// type-specifier-seq conversion-declarator[opt]
2238///
2239/// conversion-declarator:
2240/// ptr-operator conversion-declarator[opt]
2241/// \endcode
2242///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00002243/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregor7861a802009-11-03 01:35:08 +00002244/// non-empty, then we are parsing the unqualified-id of a qualified-id.
2245///
2246/// \param EnteringContext whether we are entering the scope of the
2247/// nested-name-specifier.
2248///
Douglas Gregor71395fa2009-11-04 00:56:37 +00002249/// \param ObjectType if this unqualified-id occurs within a member access
2250/// expression, the type of the base object whose member is being accessed.
2251///
2252/// \param Result on a successful parse, contains the parsed unqualified-id.
2253///
2254/// \returns true if parsing fails, false otherwise.
2255bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallba7bf592010-08-24 05:47:05 +00002256 ParsedType ObjectType,
Douglas Gregor71395fa2009-11-04 00:56:37 +00002257 UnqualifiedId &Result) {
2258 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
2259
2260 // Consume the 'operator' keyword.
2261 SourceLocation KeywordLoc = ConsumeToken();
2262
2263 // Determine what kind of operator name we have.
2264 unsigned SymbolIdx = 0;
2265 SourceLocation SymbolLocations[3];
2266 OverloadedOperatorKind Op = OO_None;
2267 switch (Tok.getKind()) {
2268 case tok::kw_new:
2269 case tok::kw_delete: {
2270 bool isNew = Tok.getKind() == tok::kw_new;
2271 // Consume the 'new' or 'delete'.
2272 SymbolLocations[SymbolIdx++] = ConsumeToken();
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002273 // Check for array new/delete.
2274 if (Tok.is(tok::l_square) &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002275 (!getLangOpts().CPlusPlus11 || NextToken().isNot(tok::l_square))) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002276 // Consume the '[' and ']'.
2277 BalancedDelimiterTracker T(*this, tok::l_square);
2278 T.consumeOpen();
2279 T.consumeClose();
2280 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00002281 return true;
2282
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002283 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
2284 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00002285 Op = isNew? OO_Array_New : OO_Array_Delete;
2286 } else {
2287 Op = isNew? OO_New : OO_Delete;
2288 }
2289 break;
2290 }
2291
2292#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
2293 case tok::Token: \
2294 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
2295 Op = OO_##Name; \
2296 break;
2297#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
2298#include "clang/Basic/OperatorKinds.def"
2299
2300 case tok::l_paren: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002301 // Consume the '(' and ')'.
2302 BalancedDelimiterTracker T(*this, tok::l_paren);
2303 T.consumeOpen();
2304 T.consumeClose();
2305 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00002306 return true;
2307
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002308 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
2309 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00002310 Op = OO_Call;
2311 break;
2312 }
2313
2314 case tok::l_square: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002315 // Consume the '[' and ']'.
2316 BalancedDelimiterTracker T(*this, tok::l_square);
2317 T.consumeOpen();
2318 T.consumeClose();
2319 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00002320 return true;
2321
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002322 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
2323 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00002324 Op = OO_Subscript;
2325 break;
2326 }
2327
2328 case tok::code_completion: {
2329 // Code completion for the operator name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002330 Actions.CodeCompleteOperatorName(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002331 cutOffParsing();
Douglas Gregor71395fa2009-11-04 00:56:37 +00002332 // Don't try to parse any further.
2333 return true;
2334 }
2335
2336 default:
2337 break;
2338 }
2339
2340 if (Op != OO_None) {
2341 // We have parsed an operator-function-id.
2342 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
2343 return false;
2344 }
Alexis Hunt34458502009-11-28 04:44:28 +00002345
2346 // Parse a literal-operator-id.
2347 //
Richard Smith6f212062012-10-20 08:41:10 +00002348 // literal-operator-id: C++11 [over.literal]
2349 // operator string-literal identifier
2350 // operator user-defined-string-literal
Alexis Hunt34458502009-11-28 04:44:28 +00002351
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002352 if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002353 Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator);
Alexis Hunt34458502009-11-28 04:44:28 +00002354
Richard Smith7d182a72012-03-08 23:06:02 +00002355 SourceLocation DiagLoc;
2356 unsigned DiagId = 0;
2357
2358 // We're past translation phase 6, so perform string literal concatenation
2359 // before checking for "".
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002360 SmallVector<Token, 4> Toks;
2361 SmallVector<SourceLocation, 4> TokLocs;
Richard Smith7d182a72012-03-08 23:06:02 +00002362 while (isTokenStringLiteral()) {
2363 if (!Tok.is(tok::string_literal) && !DiagId) {
Richard Smith6f212062012-10-20 08:41:10 +00002364 // C++11 [over.literal]p1:
2365 // The string-literal or user-defined-string-literal in a
2366 // literal-operator-id shall have no encoding-prefix [...].
Richard Smith7d182a72012-03-08 23:06:02 +00002367 DiagLoc = Tok.getLocation();
2368 DiagId = diag::err_literal_operator_string_prefix;
2369 }
2370 Toks.push_back(Tok);
2371 TokLocs.push_back(ConsumeStringToken());
2372 }
2373
Craig Topper9d5583e2014-06-26 04:58:39 +00002374 StringLiteralParser Literal(Toks, PP);
Richard Smith7d182a72012-03-08 23:06:02 +00002375 if (Literal.hadError)
2376 return true;
2377
2378 // Grab the literal operator's suffix, which will be either the next token
2379 // or a ud-suffix from the string literal.
Craig Topper161e4db2014-05-21 06:02:52 +00002380 IdentifierInfo *II = nullptr;
Richard Smith7d182a72012-03-08 23:06:02 +00002381 SourceLocation SuffixLoc;
2382 if (!Literal.getUDSuffix().empty()) {
2383 II = &PP.getIdentifierTable().get(Literal.getUDSuffix());
2384 SuffixLoc =
2385 Lexer::AdvanceToTokenCharacter(TokLocs[Literal.getUDSuffixToken()],
2386 Literal.getUDSuffixOffset(),
David Blaikiebbafb8a2012-03-11 07:00:24 +00002387 PP.getSourceManager(), getLangOpts());
Richard Smith7d182a72012-03-08 23:06:02 +00002388 } else if (Tok.is(tok::identifier)) {
2389 II = Tok.getIdentifierInfo();
2390 SuffixLoc = ConsumeToken();
2391 TokLocs.push_back(SuffixLoc);
2392 } else {
Alp Tokerec543272013-12-24 09:48:30 +00002393 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
Alexis Hunt34458502009-11-28 04:44:28 +00002394 return true;
2395 }
2396
Richard Smith7d182a72012-03-08 23:06:02 +00002397 // The string literal must be empty.
2398 if (!Literal.GetString().empty() || Literal.Pascal) {
Richard Smith6f212062012-10-20 08:41:10 +00002399 // C++11 [over.literal]p1:
2400 // The string-literal or user-defined-string-literal in a
2401 // literal-operator-id shall [...] contain no characters
2402 // other than the implicit terminating '\0'.
Richard Smith7d182a72012-03-08 23:06:02 +00002403 DiagLoc = TokLocs.front();
2404 DiagId = diag::err_literal_operator_string_not_empty;
2405 }
2406
2407 if (DiagId) {
2408 // This isn't a valid literal-operator-id, but we think we know
2409 // what the user meant. Tell them what they should have written.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002410 SmallString<32> Str;
Richard Smithe87aeb32015-10-08 00:17:59 +00002411 Str += "\"\"";
Richard Smith7d182a72012-03-08 23:06:02 +00002412 Str += II->getName();
2413 Diag(DiagLoc, DiagId) << FixItHint::CreateReplacement(
2414 SourceRange(TokLocs.front(), TokLocs.back()), Str);
2415 }
2416
2417 Result.setLiteralOperatorId(II, KeywordLoc, SuffixLoc);
Richard Smithd091dc12013-12-05 00:58:33 +00002418
2419 return Actions.checkLiteralOperatorId(SS, Result);
Alexis Hunt34458502009-11-28 04:44:28 +00002420 }
Richard Smithd091dc12013-12-05 00:58:33 +00002421
Douglas Gregor71395fa2009-11-04 00:56:37 +00002422 // Parse a conversion-function-id.
2423 //
2424 // conversion-function-id: [C++ 12.3.2]
2425 // operator conversion-type-id
2426 //
2427 // conversion-type-id:
2428 // type-specifier-seq conversion-declarator[opt]
2429 //
2430 // conversion-declarator:
2431 // ptr-operator conversion-declarator[opt]
2432
2433 // Parse the type-specifier-seq.
John McCall084e83d2011-03-24 11:26:52 +00002434 DeclSpec DS(AttrFactory);
Douglas Gregora25d65d2009-11-20 22:03:38 +00002435 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregor71395fa2009-11-04 00:56:37 +00002436 return true;
2437
2438 // Parse the conversion-declarator, which is merely a sequence of
2439 // ptr-operators.
Richard Smith01518fa2013-05-04 01:26:46 +00002440 Declarator D(DS, Declarator::ConversionIdContext);
Craig Topper161e4db2014-05-21 06:02:52 +00002441 ParseDeclaratorInternal(D, /*DirectDeclParser=*/nullptr);
2442
Douglas Gregor71395fa2009-11-04 00:56:37 +00002443 // Finish up the type.
John McCallfaf5fb42010-08-26 23:41:50 +00002444 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregor71395fa2009-11-04 00:56:37 +00002445 if (Ty.isInvalid())
2446 return true;
2447
2448 // Note that this is a conversion-function-id.
2449 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
2450 D.getSourceRange().getEnd());
2451 return false;
2452}
2453
2454/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
2455/// name of an entity.
2456///
2457/// \code
2458/// unqualified-id: [C++ expr.prim.general]
2459/// identifier
2460/// operator-function-id
2461/// conversion-function-id
2462/// [C++0x] literal-operator-id [TODO]
2463/// ~ class-name
2464/// template-id
2465///
2466/// \endcode
2467///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00002468/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregor71395fa2009-11-04 00:56:37 +00002469/// non-empty, then we are parsing the unqualified-id of a qualified-id.
2470///
2471/// \param EnteringContext whether we are entering the scope of the
2472/// nested-name-specifier.
2473///
Douglas Gregor7861a802009-11-03 01:35:08 +00002474/// \param AllowDestructorName whether we allow parsing of a destructor name.
2475///
2476/// \param AllowConstructorName whether we allow parsing a constructor name.
2477///
Douglas Gregor127ea592009-11-03 21:24:04 +00002478/// \param ObjectType if this unqualified-id occurs within a member access
2479/// expression, the type of the base object whose member is being accessed.
2480///
Douglas Gregor7861a802009-11-03 01:35:08 +00002481/// \param Result on a successful parse, contains the parsed unqualified-id.
2482///
2483/// \returns true if parsing fails, false otherwise.
2484bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
2485 bool AllowDestructorName,
2486 bool AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00002487 ParsedType ObjectType,
Abramo Bagnara7945c982012-01-27 09:46:47 +00002488 SourceLocation& TemplateKWLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00002489 UnqualifiedId &Result) {
Douglas Gregorb22ee882010-05-05 05:58:24 +00002490
2491 // Handle 'A::template B'. This is for template-ids which have not
2492 // already been annotated by ParseOptionalCXXScopeSpecifier().
2493 bool TemplateSpecified = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002494 if (getLangOpts().CPlusPlus && Tok.is(tok::kw_template) &&
Douglas Gregorb22ee882010-05-05 05:58:24 +00002495 (ObjectType || SS.isSet())) {
2496 TemplateSpecified = true;
2497 TemplateKWLoc = ConsumeToken();
2498 }
2499
Douglas Gregor7861a802009-11-03 01:35:08 +00002500 // unqualified-id:
2501 // identifier
2502 // template-id (when it hasn't already been annotated)
2503 if (Tok.is(tok::identifier)) {
2504 // Consume the identifier.
2505 IdentifierInfo *Id = Tok.getIdentifierInfo();
2506 SourceLocation IdLoc = ConsumeToken();
2507
David Blaikiebbafb8a2012-03-11 07:00:24 +00002508 if (!getLangOpts().CPlusPlus) {
Douglas Gregor411e5ac2010-01-11 23:29:10 +00002509 // If we're not in C++, only identifiers matter. Record the
2510 // identifier and return.
2511 Result.setIdentifier(Id, IdLoc);
2512 return false;
2513 }
2514
Douglas Gregor7861a802009-11-03 01:35:08 +00002515 if (AllowConstructorName &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002516 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor7861a802009-11-03 01:35:08 +00002517 // We have parsed a constructor name.
David Blaikieefdccaa2016-01-15 23:43:34 +00002518 ParsedType Ty = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, false,
2519 false, nullptr,
Abramo Bagnara4244b432012-01-27 08:46:19 +00002520 /*IsCtorOrDtorName=*/true,
2521 /*NonTrivialTypeSourceInfo=*/true);
2522 Result.setConstructorName(Ty, IdLoc, IdLoc);
Douglas Gregor7861a802009-11-03 01:35:08 +00002523 } else {
2524 // We have parsed an identifier.
2525 Result.setIdentifier(Id, IdLoc);
2526 }
2527
2528 // If the next token is a '<', we may have a template.
Douglas Gregorb22ee882010-05-05 05:58:24 +00002529 if (TemplateSpecified || Tok.is(tok::less))
Abramo Bagnara7945c982012-01-27 09:46:47 +00002530 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc, Id, IdLoc,
2531 EnteringContext, ObjectType,
2532 Result, TemplateSpecified);
Douglas Gregor7861a802009-11-03 01:35:08 +00002533
2534 return false;
2535 }
2536
2537 // unqualified-id:
2538 // template-id (already parsed and annotated)
2539 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002540 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002541
2542 // If the template-name names the current class, then this is a constructor
2543 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002544 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002545 if (SS.isSet()) {
2546 // C++ [class.qual]p2 specifies that a qualified template-name
2547 // is taken as the constructor name where a constructor can be
2548 // declared. Thus, the template arguments are extraneous, so
2549 // complain about them and remove them entirely.
2550 Diag(TemplateId->TemplateNameLoc,
2551 diag::err_out_of_line_constructor_template_id)
2552 << TemplateId->Name
Douglas Gregora771f462010-03-31 17:46:05 +00002553 << FixItHint::CreateRemoval(
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002554 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
David Blaikieefdccaa2016-01-15 23:43:34 +00002555 ParsedType Ty =
2556 Actions.getTypeName(*TemplateId->Name, TemplateId->TemplateNameLoc,
2557 getCurScope(), &SS, false, false, nullptr,
2558 /*IsCtorOrDtorName=*/true,
2559 /*NontrivialTypeSourceInfo=*/true);
Abramo Bagnara4244b432012-01-27 08:46:19 +00002560 Result.setConstructorName(Ty, TemplateId->TemplateNameLoc,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002561 TemplateId->RAngleLoc);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002562 ConsumeToken();
2563 return false;
2564 }
2565
2566 Result.setConstructorTemplateId(TemplateId);
2567 ConsumeToken();
2568 return false;
2569 }
2570
Douglas Gregor7861a802009-11-03 01:35:08 +00002571 // We have already parsed a template-id; consume the annotation token as
2572 // our unqualified-id.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002573 Result.setTemplateId(TemplateId);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002574 TemplateKWLoc = TemplateId->TemplateKWLoc;
Douglas Gregor7861a802009-11-03 01:35:08 +00002575 ConsumeToken();
2576 return false;
2577 }
2578
2579 // unqualified-id:
2580 // operator-function-id
2581 // conversion-function-id
2582 if (Tok.is(tok::kw_operator)) {
Douglas Gregor71395fa2009-11-04 00:56:37 +00002583 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor7861a802009-11-03 01:35:08 +00002584 return true;
2585
Alexis Hunted0530f2009-11-28 08:58:14 +00002586 // If we have an operator-function-id or a literal-operator-id and the next
2587 // token is a '<', we may have a
Douglas Gregor71395fa2009-11-04 00:56:37 +00002588 //
2589 // template-id:
2590 // operator-function-id < template-argument-list[opt] >
Alexis Hunted0530f2009-11-28 08:58:14 +00002591 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
2592 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregorb22ee882010-05-05 05:58:24 +00002593 (TemplateSpecified || Tok.is(tok::less)))
Abramo Bagnara7945c982012-01-27 09:46:47 +00002594 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
Craig Topper161e4db2014-05-21 06:02:52 +00002595 nullptr, SourceLocation(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00002596 EnteringContext, ObjectType,
2597 Result, TemplateSpecified);
Craig Topper161e4db2014-05-21 06:02:52 +00002598
Douglas Gregor7861a802009-11-03 01:35:08 +00002599 return false;
2600 }
2601
David Blaikiebbafb8a2012-03-11 07:00:24 +00002602 if (getLangOpts().CPlusPlus &&
Douglas Gregor411e5ac2010-01-11 23:29:10 +00002603 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor7861a802009-11-03 01:35:08 +00002604 // C++ [expr.unary.op]p10:
2605 // There is an ambiguity in the unary-expression ~X(), where X is a
2606 // class-name. The ambiguity is resolved in favor of treating ~ as a
2607 // unary complement rather than treating ~X as referring to a destructor.
2608
2609 // Parse the '~'.
2610 SourceLocation TildeLoc = ConsumeToken();
David Blaikieecd8a942011-12-08 16:13:53 +00002611
2612 if (SS.isEmpty() && Tok.is(tok::kw_decltype)) {
2613 DeclSpec DS(AttrFactory);
2614 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
2615 if (ParsedType Type = Actions.getDestructorType(DS, ObjectType)) {
2616 Result.setDestructorName(TildeLoc, Type, EndLoc);
2617 return false;
2618 }
2619 return true;
2620 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002621
2622 // Parse the class-name.
2623 if (Tok.isNot(tok::identifier)) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00002624 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor7861a802009-11-03 01:35:08 +00002625 return true;
2626 }
2627
Richard Smithefa6f732014-09-06 02:06:12 +00002628 // If the user wrote ~T::T, correct it to T::~T.
Richard Smith64e033f2015-01-15 00:48:52 +00002629 DeclaratorScopeObj DeclScopeObj(*this, SS);
Nico Weber10d02b52015-02-02 04:18:38 +00002630 if (!TemplateSpecified && NextToken().is(tok::coloncolon)) {
Nico Weberf9e37be2015-01-30 16:53:11 +00002631 // Don't let ParseOptionalCXXScopeSpecifier() "correct"
2632 // `int A; struct { ~A::A(); };` to `int A; struct { ~A:A(); };`,
2633 // it will confuse this recovery logic.
2634 ColonProtectionRAIIObject ColonRAII(*this, false);
2635
Richard Smithefa6f732014-09-06 02:06:12 +00002636 if (SS.isSet()) {
2637 AnnotateScopeToken(SS, /*NewAnnotation*/true);
2638 SS.clear();
2639 }
2640 if (ParseOptionalCXXScopeSpecifier(SS, ObjectType, EnteringContext))
2641 return true;
Nico Weber7f8ec522015-02-02 05:33:50 +00002642 if (SS.isNotEmpty())
David Blaikieefdccaa2016-01-15 23:43:34 +00002643 ObjectType = nullptr;
Nico Weberd0045862015-01-30 04:05:15 +00002644 if (Tok.isNot(tok::identifier) || NextToken().is(tok::coloncolon) ||
Benjamin Kramer3012e592015-03-29 14:35:39 +00002645 !SS.isSet()) {
Richard Smithefa6f732014-09-06 02:06:12 +00002646 Diag(TildeLoc, diag::err_destructor_tilde_scope);
2647 return true;
2648 }
2649
2650 // Recover as if the tilde had been written before the identifier.
2651 Diag(TildeLoc, diag::err_destructor_tilde_scope)
2652 << FixItHint::CreateRemoval(TildeLoc)
2653 << FixItHint::CreateInsertion(Tok.getLocation(), "~");
Richard Smith64e033f2015-01-15 00:48:52 +00002654
2655 // Temporarily enter the scope for the rest of this function.
2656 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
2657 DeclScopeObj.EnterDeclaratorScope();
Richard Smithefa6f732014-09-06 02:06:12 +00002658 }
2659
Douglas Gregor7861a802009-11-03 01:35:08 +00002660 // Parse the class-name (or template-name in a simple-template-id).
2661 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
2662 SourceLocation ClassNameLoc = ConsumeToken();
Richard Smithefa6f732014-09-06 02:06:12 +00002663
Douglas Gregorb22ee882010-05-05 05:58:24 +00002664 if (TemplateSpecified || Tok.is(tok::less)) {
David Blaikieefdccaa2016-01-15 23:43:34 +00002665 Result.setDestructorName(TildeLoc, nullptr, ClassNameLoc);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002666 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2667 ClassName, ClassNameLoc,
2668 EnteringContext, ObjectType,
2669 Result, TemplateSpecified);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002670 }
Richard Smithefa6f732014-09-06 02:06:12 +00002671
Douglas Gregor7861a802009-11-03 01:35:08 +00002672 // Note that this is a destructor name.
John McCallba7bf592010-08-24 05:47:05 +00002673 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
2674 ClassNameLoc, getCurScope(),
2675 SS, ObjectType,
2676 EnteringContext);
Douglas Gregorfe17d252010-02-16 19:09:40 +00002677 if (!Ty)
Douglas Gregor7861a802009-11-03 01:35:08 +00002678 return true;
Douglas Gregorfe17d252010-02-16 19:09:40 +00002679
Douglas Gregor7861a802009-11-03 01:35:08 +00002680 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor7861a802009-11-03 01:35:08 +00002681 return false;
2682 }
2683
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002684 Diag(Tok, diag::err_expected_unqualified_id)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002685 << getLangOpts().CPlusPlus;
Douglas Gregor7861a802009-11-03 01:35:08 +00002686 return true;
2687}
2688
Sebastian Redlbd150f42008-11-21 19:14:01 +00002689/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
2690/// memory in a typesafe manner and call constructors.
Mike Stump11289f42009-09-09 15:08:12 +00002691///
Chris Lattner109faf22009-01-04 21:25:24 +00002692/// This method is called to parse the new expression after the optional :: has
2693/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
2694/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002695///
2696/// new-expression:
2697/// '::'[opt] 'new' new-placement[opt] new-type-id
2698/// new-initializer[opt]
2699/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2700/// new-initializer[opt]
2701///
2702/// new-placement:
2703/// '(' expression-list ')'
2704///
Sebastian Redl351bb782008-12-02 14:43:59 +00002705/// new-type-id:
2706/// type-specifier-seq new-declarator[opt]
Douglas Gregora3a020a2011-04-15 19:40:02 +00002707/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redl351bb782008-12-02 14:43:59 +00002708///
2709/// new-declarator:
2710/// ptr-operator new-declarator[opt]
2711/// direct-new-declarator
2712///
Sebastian Redlbd150f42008-11-21 19:14:01 +00002713/// new-initializer:
2714/// '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00002715/// [C++0x] braced-init-list
Sebastian Redlbd150f42008-11-21 19:14:01 +00002716///
John McCalldadc5752010-08-24 06:29:42 +00002717ExprResult
Chris Lattner109faf22009-01-04 21:25:24 +00002718Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
2719 assert(Tok.is(tok::kw_new) && "expected 'new' token");
2720 ConsumeToken(); // Consume 'new'
Sebastian Redlbd150f42008-11-21 19:14:01 +00002721
2722 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
2723 // second form of new-expression. It can't be a new-type-id.
2724
Benjamin Kramerf0623432012-08-23 22:51:59 +00002725 ExprVector PlacementArgs;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002726 SourceLocation PlacementLParen, PlacementRParen;
2727
Douglas Gregorf2753b32010-07-13 15:54:32 +00002728 SourceRange TypeIdParens;
John McCall084e83d2011-03-24 11:26:52 +00002729 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis3ff13572011-06-28 03:01:23 +00002730 Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002731 if (Tok.is(tok::l_paren)) {
2732 // If it turns out to be a placement, we change the type location.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002733 BalancedDelimiterTracker T(*this, tok::l_paren);
2734 T.consumeOpen();
2735 PlacementLParen = T.getOpenLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002736 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002737 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redld65cea82008-12-11 22:51:44 +00002738 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002739 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002740
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002741 T.consumeClose();
2742 PlacementRParen = T.getCloseLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002743 if (PlacementRParen.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002744 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redld65cea82008-12-11 22:51:44 +00002745 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002746 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002747
Sebastian Redl351bb782008-12-02 14:43:59 +00002748 if (PlacementArgs.empty()) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002749 // Reset the placement locations. There was no placement.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002750 TypeIdParens = T.getRange();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002751 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002752 } else {
2753 // We still need the type.
2754 if (Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002755 BalancedDelimiterTracker T(*this, tok::l_paren);
2756 T.consumeOpen();
Douglas Gregora3a020a2011-04-15 19:40:02 +00002757 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002758 ParseSpecifierQualifierList(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002759 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002760 ParseDeclarator(DeclaratorInfo);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002761 T.consumeClose();
2762 TypeIdParens = T.getRange();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002763 } else {
Douglas Gregora3a020a2011-04-15 19:40:02 +00002764 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002765 if (ParseCXXTypeSpecifierSeq(DS))
2766 DeclaratorInfo.setInvalidType(true);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002767 else {
2768 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002769 ParseDeclaratorInternal(DeclaratorInfo,
2770 &Parser::ParseDirectNewDeclarator);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002771 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002772 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002773 }
2774 } else {
Sebastian Redl351bb782008-12-02 14:43:59 +00002775 // A new-type-id is a simplified type-id, where essentially the
2776 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregora3a020a2011-04-15 19:40:02 +00002777 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002778 if (ParseCXXTypeSpecifierSeq(DS))
2779 DeclaratorInfo.setInvalidType(true);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002780 else {
2781 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002782 ParseDeclaratorInternal(DeclaratorInfo,
2783 &Parser::ParseDirectNewDeclarator);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002784 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002785 }
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00002786 if (DeclaratorInfo.isInvalidType()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002787 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redld65cea82008-12-11 22:51:44 +00002788 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002789 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002790
Sebastian Redl6047f072012-02-16 12:22:20 +00002791 ExprResult Initializer;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002792
2793 if (Tok.is(tok::l_paren)) {
Sebastian Redl6047f072012-02-16 12:22:20 +00002794 SourceLocation ConstructorLParen, ConstructorRParen;
Benjamin Kramerf0623432012-08-23 22:51:59 +00002795 ExprVector ConstructorArgs;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002796 BalancedDelimiterTracker T(*this, tok::l_paren);
2797 T.consumeOpen();
2798 ConstructorLParen = T.getOpenLocation();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002799 if (Tok.isNot(tok::r_paren)) {
2800 CommaLocsTy CommaLocs;
Francisco Lopes da Silva975a9f62015-01-21 16:24:11 +00002801 if (ParseExpressionList(ConstructorArgs, CommaLocs, [&] {
2802 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(),
2803 DeclaratorInfo).get();
2804 Actions.CodeCompleteConstructor(getCurScope(),
2805 TypeRep.get()->getCanonicalTypeInternal(),
2806 DeclaratorInfo.getLocEnd(),
2807 ConstructorArgs);
2808 })) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002809 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redld65cea82008-12-11 22:51:44 +00002810 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002811 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002812 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002813 T.consumeClose();
2814 ConstructorRParen = T.getCloseLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002815 if (ConstructorRParen.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002816 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redld65cea82008-12-11 22:51:44 +00002817 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002818 }
Sebastian Redl6047f072012-02-16 12:22:20 +00002819 Initializer = Actions.ActOnParenListExpr(ConstructorLParen,
2820 ConstructorRParen,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002821 ConstructorArgs);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002822 } else if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus11) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002823 Diag(Tok.getLocation(),
2824 diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl6047f072012-02-16 12:22:20 +00002825 Initializer = ParseBraceInitializer();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002826 }
Sebastian Redl6047f072012-02-16 12:22:20 +00002827 if (Initializer.isInvalid())
2828 return Initializer;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002829
Sebastian Redl6d4256c2009-03-15 17:47:39 +00002830 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002831 PlacementArgs, PlacementRParen,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002832 TypeIdParens, DeclaratorInfo, Initializer.get());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002833}
2834
Sebastian Redlbd150f42008-11-21 19:14:01 +00002835/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
2836/// passed to ParseDeclaratorInternal.
2837///
2838/// direct-new-declarator:
2839/// '[' expression ']'
2840/// direct-new-declarator '[' constant-expression ']'
2841///
Chris Lattner109faf22009-01-04 21:25:24 +00002842void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002843 // Parse the array dimensions.
2844 bool first = true;
2845 while (Tok.is(tok::l_square)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002846 // An array-size expression can't start with a lambda.
2847 if (CheckProhibitedCXX11Attribute())
2848 continue;
2849
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002850 BalancedDelimiterTracker T(*this, tok::l_square);
2851 T.consumeOpen();
2852
John McCalldadc5752010-08-24 06:29:42 +00002853 ExprResult Size(first ? ParseExpression()
Sebastian Redl59b5e512008-12-11 21:36:32 +00002854 : ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002855 if (Size.isInvalid()) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002856 // Recover
Alexey Bataevee6507d2013-11-18 08:17:37 +00002857 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002858 return;
2859 }
2860 first = false;
2861
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002862 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00002863
Bill Wendling44426052012-12-20 19:22:21 +00002864 // Attributes here appertain to the array type. C++11 [expr.new]p5.
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002865 ParsedAttributes Attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002866 MaybeParseCXX11Attributes(Attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002867
John McCall084e83d2011-03-24 11:26:52 +00002868 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall53fa7142010-12-24 02:08:15 +00002869 /*static=*/false, /*star=*/false,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002870 Size.get(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002871 T.getOpenLocation(),
2872 T.getCloseLocation()),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002873 Attrs, T.getCloseLocation());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002874
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002875 if (T.getCloseLocation().isInvalid())
Sebastian Redlbd150f42008-11-21 19:14:01 +00002876 return;
2877 }
2878}
2879
2880/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
2881/// This ambiguity appears in the syntax of the C++ new operator.
2882///
2883/// new-expression:
2884/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2885/// new-initializer[opt]
2886///
2887/// new-placement:
2888/// '(' expression-list ')'
2889///
John McCall37ad5512010-08-23 06:44:23 +00002890bool Parser::ParseExpressionListOrTypeId(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002891 SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner109faf22009-01-04 21:25:24 +00002892 Declarator &D) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002893 // The '(' was already consumed.
2894 if (isTypeIdInParens()) {
Sebastian Redl351bb782008-12-02 14:43:59 +00002895 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002896 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002897 ParseDeclarator(D);
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00002898 return D.isInvalidType();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002899 }
2900
2901 // It's not a type, it has to be an expression list.
2902 // Discard the comma locations - ActOnCXXNew has enough parameters.
2903 CommaLocsTy CommaLocs;
2904 return ParseExpressionList(PlacementArgs, CommaLocs);
2905}
2906
2907/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
2908/// to free memory allocated by new.
2909///
Chris Lattner109faf22009-01-04 21:25:24 +00002910/// This method is called to parse the 'delete' expression after the optional
2911/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
2912/// and "Start" is its location. Otherwise, "Start" is the location of the
2913/// 'delete' token.
2914///
Sebastian Redlbd150f42008-11-21 19:14:01 +00002915/// delete-expression:
2916/// '::'[opt] 'delete' cast-expression
2917/// '::'[opt] 'delete' '[' ']' cast-expression
John McCalldadc5752010-08-24 06:29:42 +00002918ExprResult
Chris Lattner109faf22009-01-04 21:25:24 +00002919Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
2920 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
2921 ConsumeToken(); // Consume 'delete'
Sebastian Redlbd150f42008-11-21 19:14:01 +00002922
2923 // Array delete?
2924 bool ArrayDelete = false;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002925 if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
Richard Smith10c60722012-08-09 19:01:51 +00002926 // C++11 [expr.delete]p1:
2927 // Whenever the delete keyword is followed by empty square brackets, it
2928 // shall be interpreted as [array delete].
2929 // [Footnote: A lambda expression with a lambda-introducer that consists
2930 // of empty square brackets can follow the delete keyword if
2931 // the lambda expression is enclosed in parentheses.]
2932 // FIXME: Produce a better diagnostic if the '[]' is unambiguously a
2933 // lambda-introducer.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002934 ArrayDelete = true;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002935 BalancedDelimiterTracker T(*this, tok::l_square);
2936
2937 T.consumeOpen();
2938 T.consumeClose();
2939 if (T.getCloseLocation().isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +00002940 return ExprError();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002941 }
2942
John McCalldadc5752010-08-24 06:29:42 +00002943 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002944 if (Operand.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002945 return Operand;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002946
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002947 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.get());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002948}
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002949
Douglas Gregor29c42f22012-02-24 07:38:34 +00002950static TypeTrait TypeTraitFromTokKind(tok::TokenKind kind) {
2951 switch (kind) {
2952 default: llvm_unreachable("Not a known type trait");
Alp Toker95e7ff22014-01-01 05:57:51 +00002953#define TYPE_TRAIT_1(Spelling, Name, Key) \
2954case tok::kw_ ## Spelling: return UTT_ ## Name;
Alp Tokercbb90342013-12-13 20:49:58 +00002955#define TYPE_TRAIT_2(Spelling, Name, Key) \
2956case tok::kw_ ## Spelling: return BTT_ ## Name;
2957#include "clang/Basic/TokenKinds.def"
Alp Toker40f9b1c2013-12-12 21:23:03 +00002958#define TYPE_TRAIT_N(Spelling, Name, Key) \
2959 case tok::kw_ ## Spelling: return TT_ ## Name;
2960#include "clang/Basic/TokenKinds.def"
Douglas Gregor29c42f22012-02-24 07:38:34 +00002961 }
2962}
2963
John Wiegley6242b6a2011-04-28 00:16:57 +00002964static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
2965 switch(kind) {
2966 default: llvm_unreachable("Not a known binary type trait");
2967 case tok::kw___array_rank: return ATT_ArrayRank;
2968 case tok::kw___array_extent: return ATT_ArrayExtent;
2969 }
2970}
2971
John Wiegleyf9f65842011-04-25 06:54:41 +00002972static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2973 switch(kind) {
David Blaikie83d382b2011-09-23 05:06:16 +00002974 default: llvm_unreachable("Not a known unary expression trait.");
John Wiegleyf9f65842011-04-25 06:54:41 +00002975 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
2976 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
2977 }
2978}
2979
Alp Toker40f9b1c2013-12-12 21:23:03 +00002980static unsigned TypeTraitArity(tok::TokenKind kind) {
2981 switch (kind) {
2982 default: llvm_unreachable("Not a known type trait");
2983#define TYPE_TRAIT(N,Spelling,K) case tok::kw_##Spelling: return N;
2984#include "clang/Basic/TokenKinds.def"
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002985 }
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002986}
2987
Douglas Gregor29c42f22012-02-24 07:38:34 +00002988/// \brief Parse the built-in type-trait pseudo-functions that allow
2989/// implementation of the TR1/C++11 type traits templates.
2990///
2991/// primary-expression:
Alp Toker40f9b1c2013-12-12 21:23:03 +00002992/// unary-type-trait '(' type-id ')'
2993/// binary-type-trait '(' type-id ',' type-id ')'
Douglas Gregor29c42f22012-02-24 07:38:34 +00002994/// type-trait '(' type-id-seq ')'
2995///
2996/// type-id-seq:
2997/// type-id ...[opt] type-id-seq[opt]
2998///
2999ExprResult Parser::ParseTypeTrait() {
Alp Toker40f9b1c2013-12-12 21:23:03 +00003000 tok::TokenKind Kind = Tok.getKind();
3001 unsigned Arity = TypeTraitArity(Kind);
3002
Douglas Gregor29c42f22012-02-24 07:38:34 +00003003 SourceLocation Loc = ConsumeToken();
3004
3005 BalancedDelimiterTracker Parens(*this, tok::l_paren);
Alp Toker383d2c42014-01-01 03:08:43 +00003006 if (Parens.expectAndConsume())
Douglas Gregor29c42f22012-02-24 07:38:34 +00003007 return ExprError();
3008
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003009 SmallVector<ParsedType, 2> Args;
Douglas Gregor29c42f22012-02-24 07:38:34 +00003010 do {
3011 // Parse the next type.
3012 TypeResult Ty = ParseTypeName();
3013 if (Ty.isInvalid()) {
3014 Parens.skipToEnd();
3015 return ExprError();
3016 }
3017
3018 // Parse the ellipsis, if present.
3019 if (Tok.is(tok::ellipsis)) {
3020 Ty = Actions.ActOnPackExpansion(Ty.get(), ConsumeToken());
3021 if (Ty.isInvalid()) {
3022 Parens.skipToEnd();
3023 return ExprError();
3024 }
3025 }
3026
3027 // Add this type to the list of arguments.
3028 Args.push_back(Ty.get());
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003029 } while (TryConsumeToken(tok::comma));
3030
Douglas Gregor29c42f22012-02-24 07:38:34 +00003031 if (Parens.consumeClose())
3032 return ExprError();
Alp Toker40f9b1c2013-12-12 21:23:03 +00003033
3034 SourceLocation EndLoc = Parens.getCloseLocation();
3035
3036 if (Arity && Args.size() != Arity) {
3037 Diag(EndLoc, diag::err_type_trait_arity)
3038 << Arity << 0 << (Arity > 1) << (int)Args.size() << SourceRange(Loc);
3039 return ExprError();
3040 }
3041
3042 if (!Arity && Args.empty()) {
3043 Diag(EndLoc, diag::err_type_trait_arity)
3044 << 1 << 1 << 1 << (int)Args.size() << SourceRange(Loc);
3045 return ExprError();
3046 }
3047
Alp Toker88f64e62013-12-13 21:19:30 +00003048 return Actions.ActOnTypeTrait(TypeTraitFromTokKind(Kind), Loc, Args, EndLoc);
Douglas Gregor29c42f22012-02-24 07:38:34 +00003049}
3050
John Wiegley6242b6a2011-04-28 00:16:57 +00003051/// ParseArrayTypeTrait - Parse the built-in array type-trait
3052/// pseudo-functions.
3053///
3054/// primary-expression:
3055/// [Embarcadero] '__array_rank' '(' type-id ')'
3056/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
3057///
3058ExprResult Parser::ParseArrayTypeTrait() {
3059 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
3060 SourceLocation Loc = ConsumeToken();
3061
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003062 BalancedDelimiterTracker T(*this, tok::l_paren);
Alp Toker383d2c42014-01-01 03:08:43 +00003063 if (T.expectAndConsume())
John Wiegley6242b6a2011-04-28 00:16:57 +00003064 return ExprError();
3065
3066 TypeResult Ty = ParseTypeName();
3067 if (Ty.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003068 SkipUntil(tok::comma, StopAtSemi);
3069 SkipUntil(tok::r_paren, StopAtSemi);
John Wiegley6242b6a2011-04-28 00:16:57 +00003070 return ExprError();
3071 }
3072
3073 switch (ATT) {
3074 case ATT_ArrayRank: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003075 T.consumeClose();
Craig Topper161e4db2014-05-21 06:02:52 +00003076 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), nullptr,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003077 T.getCloseLocation());
John Wiegley6242b6a2011-04-28 00:16:57 +00003078 }
3079 case ATT_ArrayExtent: {
Alp Toker383d2c42014-01-01 03:08:43 +00003080 if (ExpectAndConsume(tok::comma)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003081 SkipUntil(tok::r_paren, StopAtSemi);
John Wiegley6242b6a2011-04-28 00:16:57 +00003082 return ExprError();
3083 }
3084
3085 ExprResult DimExpr = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003086 T.consumeClose();
John Wiegley6242b6a2011-04-28 00:16:57 +00003087
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003088 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(),
3089 T.getCloseLocation());
John Wiegley6242b6a2011-04-28 00:16:57 +00003090 }
John Wiegley6242b6a2011-04-28 00:16:57 +00003091 }
David Blaikiee4d798f2012-01-20 21:50:17 +00003092 llvm_unreachable("Invalid ArrayTypeTrait!");
John Wiegley6242b6a2011-04-28 00:16:57 +00003093}
3094
John Wiegleyf9f65842011-04-25 06:54:41 +00003095/// ParseExpressionTrait - Parse built-in expression-trait
3096/// pseudo-functions like __is_lvalue_expr( xxx ).
3097///
3098/// primary-expression:
3099/// [Embarcadero] expression-trait '(' expression ')'
3100///
3101ExprResult Parser::ParseExpressionTrait() {
3102 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
3103 SourceLocation Loc = ConsumeToken();
3104
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003105 BalancedDelimiterTracker T(*this, tok::l_paren);
Alp Toker383d2c42014-01-01 03:08:43 +00003106 if (T.expectAndConsume())
John Wiegleyf9f65842011-04-25 06:54:41 +00003107 return ExprError();
3108
3109 ExprResult Expr = ParseExpression();
3110
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003111 T.consumeClose();
John Wiegleyf9f65842011-04-25 06:54:41 +00003112
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003113 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(),
3114 T.getCloseLocation());
John Wiegleyf9f65842011-04-25 06:54:41 +00003115}
3116
3117
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003118/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
3119/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
3120/// based on the context past the parens.
John McCalldadc5752010-08-24 06:29:42 +00003121ExprResult
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003122Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallba7bf592010-08-24 05:47:05 +00003123 ParsedType &CastTy,
Richard Smith87e11a42014-05-15 02:43:47 +00003124 BalancedDelimiterTracker &Tracker,
3125 ColonProtectionRAIIObject &ColonProt) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003126 assert(getLangOpts().CPlusPlus && "Should only be called for C++!");
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003127 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
3128 assert(isTypeIdInParens() && "Not a type-id!");
3129
John McCalldadc5752010-08-24 06:29:42 +00003130 ExprResult Result(true);
David Blaikieefdccaa2016-01-15 23:43:34 +00003131 CastTy = nullptr;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003132
3133 // We need to disambiguate a very ugly part of the C++ syntax:
3134 //
3135 // (T())x; - type-id
3136 // (T())*x; - type-id
3137 // (T())/x; - expression
3138 // (T()); - expression
3139 //
3140 // The bad news is that we cannot use the specialized tentative parser, since
3141 // it can only verify that the thing inside the parens can be parsed as
3142 // type-id, it is not useful for determining the context past the parens.
3143 //
3144 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidis24ad6922009-05-22 15:12:46 +00003145 // making any unnecessary Action calls.
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003146 //
3147 // It uses a scheme similar to parsing inline methods. The parenthesized
3148 // tokens are cached, the context that follows is determined (possibly by
3149 // parsing a cast-expression), and then we re-introduce the cached tokens
3150 // into the token stream and parse them appropriately.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003151
Mike Stump11289f42009-09-09 15:08:12 +00003152 ParenParseOption ParseAs;
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003153 CachedTokens Toks;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003154
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003155 // Store the tokens of the parentheses. We will parse them after we determine
3156 // the context that follows them.
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00003157 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003158 // We didn't find the ')' we expected.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003159 Tracker.consumeClose();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003160 return ExprError();
3161 }
3162
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003163 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003164 ParseAs = CompoundLiteral;
3165 } else {
3166 bool NotCastExpr;
Eli Friedmancf7530f2009-05-25 19:41:42 +00003167 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
3168 NotCastExpr = true;
3169 } else {
3170 // Try parsing the cast-expression that may follow.
3171 // If it is not a cast-expression, NotCastExpr will be true and no token
3172 // will be consumed.
Richard Smith87e11a42014-05-15 02:43:47 +00003173 ColonProt.restore();
Eli Friedmancf7530f2009-05-25 19:41:42 +00003174 Result = ParseCastExpression(false/*isUnaryExpression*/,
3175 false/*isAddressofOperand*/,
John McCallba7bf592010-08-24 05:47:05 +00003176 NotCastExpr,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00003177 // type-id has priority.
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +00003178 IsTypeCast);
Eli Friedmancf7530f2009-05-25 19:41:42 +00003179 }
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003180
3181 // If we parsed a cast-expression, it's really a type-id, otherwise it's
3182 // an expression.
3183 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003184 }
3185
Alexey Bataev703a93c2016-02-04 04:22:09 +00003186 // Create a fake EOF to mark end of Toks buffer.
3187 Token AttrEnd;
3188 AttrEnd.startToken();
3189 AttrEnd.setKind(tok::eof);
3190 AttrEnd.setLocation(Tok.getLocation());
3191 AttrEnd.setEofData(Toks.data());
3192 Toks.push_back(AttrEnd);
3193
Mike Stump11289f42009-09-09 15:08:12 +00003194 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003195 Toks.push_back(Tok);
3196 // Re-enter the stored parenthesized tokens into the token stream, so we may
3197 // parse them now.
David Blaikie2eabcc92016-02-09 18:52:09 +00003198 PP.EnterTokenStream(Toks, true /*DisableMacroExpansion*/);
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003199 // Drop the current token and bring the first cached one. It's the same token
3200 // as when we entered this function.
3201 ConsumeAnyToken();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003202
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003203 if (ParseAs >= CompoundLiteral) {
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00003204 // Parse the type declarator.
3205 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00003206 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Richard Smith87e11a42014-05-15 02:43:47 +00003207 {
3208 ColonProtectionRAIIObject InnerColonProtection(*this);
3209 ParseSpecifierQualifierList(DS);
3210 ParseDeclarator(DeclaratorInfo);
3211 }
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003212
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003213 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003214 Tracker.consumeClose();
Richard Smith87e11a42014-05-15 02:43:47 +00003215 ColonProt.restore();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003216
Alexey Bataev703a93c2016-02-04 04:22:09 +00003217 // Consume EOF marker for Toks buffer.
3218 assert(Tok.is(tok::eof) && Tok.getEofData() == AttrEnd.getEofData());
3219 ConsumeAnyToken();
3220
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003221 if (ParseAs == CompoundLiteral) {
3222 ExprType = CompoundLiteral;
Richard Smithaba8b362014-05-15 02:51:15 +00003223 if (DeclaratorInfo.isInvalidType())
3224 return ExprError();
3225
3226 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Richard Smith87e11a42014-05-15 02:43:47 +00003227 return ParseCompoundLiteralExpression(Ty.get(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003228 Tracker.getOpenLocation(),
3229 Tracker.getCloseLocation());
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003230 }
Mike Stump11289f42009-09-09 15:08:12 +00003231
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003232 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
3233 assert(ParseAs == CastExpr);
3234
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00003235 if (DeclaratorInfo.isInvalidType())
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003236 return ExprError();
3237
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003238 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003239 if (!Result.isInvalid())
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003240 Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(),
3241 DeclaratorInfo, CastTy,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003242 Tracker.getCloseLocation(), Result.get());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003243 return Result;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003244 }
Mike Stump11289f42009-09-09 15:08:12 +00003245
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003246 // Not a compound literal, and not followed by a cast-expression.
3247 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003248
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003249 ExprType = SimpleExpr;
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003250 Result = ParseExpression();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003251 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003252 Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(),
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003253 Tok.getLocation(), Result.get());
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003254
3255 // Match the ')'.
3256 if (Result.isInvalid()) {
Alexey Bataev703a93c2016-02-04 04:22:09 +00003257 while (Tok.isNot(tok::eof))
3258 ConsumeAnyToken();
3259 assert(Tok.getEofData() == AttrEnd.getEofData());
3260 ConsumeAnyToken();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003261 return ExprError();
3262 }
Mike Stump11289f42009-09-09 15:08:12 +00003263
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003264 Tracker.consumeClose();
Alexey Bataev703a93c2016-02-04 04:22:09 +00003265 // Consume EOF marker for Toks buffer.
3266 assert(Tok.is(tok::eof) && Tok.getEofData() == AttrEnd.getEofData());
3267 ConsumeAnyToken();
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003268 return Result;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003269}