blob: 5fe47fc9711c7f3472507549afdf29b2e8b14e97 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation for C++.
11//
12//===----------------------------------------------------------------------===//
Faisal Valifad9e132013-09-26 19:54:12 +000013#include "clang/AST/DeclTemplate.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000014#include "clang/Parse/Parser.h"
Douglas Gregorbc61bd82011-01-11 00:33:19 +000015#include "RAIIObjectsForParser.h"
Eli Friedmandc3b7232012-01-04 02:40:39 +000016#include "clang/Basic/PrettyStackTrace.h"
Richard Smith33762772012-03-08 23:06:02 +000017#include "clang/Lex/LiteralSupport.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000018#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000019#include "clang/Sema/DeclSpec.h"
20#include "clang/Sema/ParsedTemplate.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000021#include "clang/Sema/Scope.h"
Douglas Gregor3f9a0562009-11-03 01:35:08 +000022#include "llvm/Support/ErrorHandling.h"
23
Faisal Valifad9e132013-09-26 19:54:12 +000024
Reid Spencer5f016e22007-07-11 17:01:13 +000025using namespace clang;
26
Richard Smithea698b32011-04-14 21:45:45 +000027static int SelectDigraphErrorMessage(tok::TokenKind Kind) {
28 switch (Kind) {
29 case tok::kw_template: return 0;
30 case tok::kw_const_cast: return 1;
31 case tok::kw_dynamic_cast: return 2;
32 case tok::kw_reinterpret_cast: return 3;
33 case tok::kw_static_cast: return 4;
34 default:
David Blaikieb219cfc2011-09-23 05:06:16 +000035 llvm_unreachable("Unknown type for digraph error message.");
Richard Smithea698b32011-04-14 21:45:45 +000036 }
37}
38
39// Are the two tokens adjacent in the same source file?
Richard Smith19a27022012-06-18 06:11:04 +000040bool Parser::areTokensAdjacent(const Token &First, const Token &Second) {
Richard Smithea698b32011-04-14 21:45:45 +000041 SourceManager &SM = PP.getSourceManager();
42 SourceLocation FirstLoc = SM.getSpellingLoc(First.getLocation());
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +000043 SourceLocation FirstEnd = FirstLoc.getLocWithOffset(First.getLength());
Richard Smithea698b32011-04-14 21:45:45 +000044 return FirstEnd == SM.getSpellingLoc(Second.getLocation());
45}
46
47// Suggest fixit for "<::" after a cast.
48static void FixDigraph(Parser &P, Preprocessor &PP, Token &DigraphToken,
49 Token &ColonToken, tok::TokenKind Kind, bool AtDigraph) {
50 // Pull '<:' and ':' off token stream.
51 if (!AtDigraph)
52 PP.Lex(DigraphToken);
53 PP.Lex(ColonToken);
54
55 SourceRange Range;
56 Range.setBegin(DigraphToken.getLocation());
57 Range.setEnd(ColonToken.getLocation());
58 P.Diag(DigraphToken.getLocation(), diag::err_missing_whitespace_digraph)
59 << SelectDigraphErrorMessage(Kind)
60 << FixItHint::CreateReplacement(Range, "< ::");
61
62 // Update token information to reflect their change in token type.
63 ColonToken.setKind(tok::coloncolon);
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +000064 ColonToken.setLocation(ColonToken.getLocation().getLocWithOffset(-1));
Richard Smithea698b32011-04-14 21:45:45 +000065 ColonToken.setLength(2);
66 DigraphToken.setKind(tok::less);
67 DigraphToken.setLength(1);
68
69 // Push new tokens back to token stream.
70 PP.EnterToken(ColonToken);
71 if (!AtDigraph)
72 PP.EnterToken(DigraphToken);
73}
74
Richard Trieu950be712011-09-19 19:01:00 +000075// Check for '<::' which should be '< ::' instead of '[:' when following
76// a template name.
77void Parser::CheckForTemplateAndDigraph(Token &Next, ParsedType ObjectType,
78 bool EnteringContext,
79 IdentifierInfo &II, CXXScopeSpec &SS) {
Richard Trieuc11030e2011-09-20 20:03:50 +000080 if (!Next.is(tok::l_square) || Next.getLength() != 2)
Richard Trieu950be712011-09-19 19:01:00 +000081 return;
82
83 Token SecondToken = GetLookAheadToken(2);
Richard Smith19a27022012-06-18 06:11:04 +000084 if (!SecondToken.is(tok::colon) || !areTokensAdjacent(Next, SecondToken))
Richard Trieu950be712011-09-19 19:01:00 +000085 return;
86
87 TemplateTy Template;
88 UnqualifiedId TemplateName;
89 TemplateName.setIdentifier(&II, Tok.getLocation());
90 bool MemberOfUnknownSpecialization;
91 if (!Actions.isTemplateName(getCurScope(), SS, /*hasTemplateKeyword=*/false,
92 TemplateName, ObjectType, EnteringContext,
93 Template, MemberOfUnknownSpecialization))
94 return;
95
96 FixDigraph(*this, PP, Next, SecondToken, tok::kw_template,
97 /*AtDigraph*/false);
98}
99
Richard Trieu919b9552012-11-02 01:08:58 +0000100/// \brief Emits an error for a left parentheses after a double colon.
101///
102/// When a '(' is found after a '::', emit an error. Attempt to fix the token
Nico Weberbba91b82012-11-29 05:29:23 +0000103/// stream by removing the '(', and the matching ')' if found.
Richard Trieu919b9552012-11-02 01:08:58 +0000104void Parser::CheckForLParenAfterColonColon() {
105 if (!Tok.is(tok::l_paren))
106 return;
107
108 SourceLocation l_parenLoc = ConsumeParen(), r_parenLoc;
109 Token Tok1 = getCurToken();
110 if (!Tok1.is(tok::identifier) && !Tok1.is(tok::star))
111 return;
112
113 if (Tok1.is(tok::identifier)) {
114 Token Tok2 = GetLookAheadToken(1);
115 if (Tok2.is(tok::r_paren)) {
116 ConsumeToken();
117 PP.EnterToken(Tok1);
118 r_parenLoc = ConsumeParen();
119 }
120 } else if (Tok1.is(tok::star)) {
121 Token Tok2 = GetLookAheadToken(1);
122 if (Tok2.is(tok::identifier)) {
123 Token Tok3 = GetLookAheadToken(2);
124 if (Tok3.is(tok::r_paren)) {
125 ConsumeToken();
126 ConsumeToken();
127 PP.EnterToken(Tok2);
128 PP.EnterToken(Tok1);
129 r_parenLoc = ConsumeParen();
130 }
131 }
132 }
133
134 Diag(l_parenLoc, diag::err_paren_after_colon_colon)
135 << FixItHint::CreateRemoval(l_parenLoc)
136 << FixItHint::CreateRemoval(r_parenLoc);
137}
138
Mike Stump1eb44332009-09-09 15:08:12 +0000139/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000140///
141/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump1eb44332009-09-09 15:08:12 +0000142/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000143/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000144///
145/// '::'[opt] nested-name-specifier
146/// '::'
147///
148/// nested-name-specifier:
149/// type-name '::'
150/// namespace-name '::'
151/// nested-name-specifier identifier '::'
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000152/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000153///
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000154///
Mike Stump1eb44332009-09-09 15:08:12 +0000155/// \param SS the scope specifier that will be set to the parsed
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000156/// nested-name-specifier (or empty)
157///
Mike Stump1eb44332009-09-09 15:08:12 +0000158/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000159/// the "." or "->" of a member access expression, this parameter provides the
160/// type of the object whose members are being accessed.
161///
162/// \param EnteringContext whether we will be entering into the context of
163/// the nested-name-specifier after parsing it.
164///
Douglas Gregord4dca082010-02-24 18:44:31 +0000165/// \param MayBePseudoDestructor When non-NULL, points to a flag that
166/// indicates whether this nested-name-specifier may be part of a
167/// pseudo-destructor name. In this case, the flag will be set false
168/// if we don't actually end up parsing a destructor name. Moreorover,
169/// if we do end up determining that we are parsing a destructor name,
170/// the last component of the nested-name-specifier is not parsed as
171/// part of the scope specifier.
Richard Smith2db075b2013-03-26 01:15:19 +0000172///
173/// \param IsTypename If \c true, this nested-name-specifier is known to be
174/// part of a type name. This is used to improve error recovery.
175///
176/// \param LastII When non-NULL, points to an IdentifierInfo* that will be
177/// filled in with the leading identifier in the last component of the
178/// nested-name-specifier, if any.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000179///
John McCall9ba61662010-02-26 08:45:28 +0000180/// \returns true if there was an error parsing a scope specifier
Douglas Gregor495c35d2009-08-25 22:51:20 +0000181bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +0000182 ParsedType ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000183 bool EnteringContext,
Francois Pichet4147d302011-03-27 19:41:34 +0000184 bool *MayBePseudoDestructor,
Richard Smith2db075b2013-03-26 01:15:19 +0000185 bool IsTypename,
186 IdentifierInfo **LastII) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000187 assert(getLangOpts().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +0000188 "Call sites of this function should be guarded by checking for C++");
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000190 if (Tok.is(tok::annot_cxxscope)) {
Richard Smith2db075b2013-03-26 01:15:19 +0000191 assert(!LastII && "want last identifier but have already annotated scope");
Douglas Gregorc34348a2011-02-24 17:54:50 +0000192 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
193 Tok.getAnnotationRange(),
194 SS);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000195 ConsumeToken();
John McCall9ba61662010-02-26 08:45:28 +0000196 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000197 }
Chris Lattnere607e802009-01-04 21:14:15 +0000198
Larisse Voufo9c90f7f2013-08-06 05:49:26 +0000199 if (Tok.is(tok::annot_template_id)) {
200 // If the current token is an annotated template id, it may already have
201 // a scope specifier. Restore it.
202 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
203 SS = TemplateId->SS;
204 }
205
Richard Smith2db075b2013-03-26 01:15:19 +0000206 if (LastII)
207 *LastII = 0;
208
Douglas Gregor39a8de12009-02-25 19:37:18 +0000209 bool HasScopeSpecifier = false;
210
Chris Lattner5b454732009-01-05 03:55:46 +0000211 if (Tok.is(tok::coloncolon)) {
212 // ::new and ::delete aren't nested-name-specifiers.
213 tok::TokenKind NextKind = NextToken().getKind();
214 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
215 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000216
Chris Lattner55a7cef2009-01-05 00:13:00 +0000217 // '::' - Global scope qualifier.
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000218 if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
219 return true;
Richard Trieu919b9552012-11-02 01:08:58 +0000220
221 CheckForLParenAfterColonColon();
222
Douglas Gregor39a8de12009-02-25 19:37:18 +0000223 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000224 }
225
Douglas Gregord4dca082010-02-24 18:44:31 +0000226 bool CheckForDestructor = false;
227 if (MayBePseudoDestructor && *MayBePseudoDestructor) {
228 CheckForDestructor = true;
229 *MayBePseudoDestructor = false;
230 }
231
David Blaikie42d6d0c2011-12-04 05:04:18 +0000232 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
233 DeclSpec DS(AttrFactory);
234 SourceLocation DeclLoc = Tok.getLocation();
235 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
236 if (Tok.isNot(tok::coloncolon)) {
237 AnnotateExistingDecltypeSpecifier(DS, DeclLoc, EndLoc);
238 return false;
239 }
240
241 SourceLocation CCLoc = ConsumeToken();
242 if (Actions.ActOnCXXNestedNameSpecifierDecltype(SS, DS, CCLoc))
243 SS.SetInvalid(SourceRange(DeclLoc, CCLoc));
244
245 HasScopeSpecifier = true;
246 }
247
Douglas Gregor39a8de12009-02-25 19:37:18 +0000248 while (true) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000249 if (HasScopeSpecifier) {
250 // C++ [basic.lookup.classref]p5:
251 // If the qualified-id has the form
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000252 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000253 // ::class-name-or-namespace-name::...
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000254 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000255 // the class-name-or-namespace-name is looked up in global scope as a
256 // class-name or namespace-name.
257 //
258 // To implement this, we clear out the object type as soon as we've
259 // seen a leading '::' or part of a nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000260 ObjectType = ParsedType();
Douglas Gregor81b747b2009-09-17 21:32:03 +0000261
262 if (Tok.is(tok::code_completion)) {
263 // Code completion for a nested-name-specifier, where the code
264 // code completion token follows the '::'.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000265 Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
Argyrios Kyrtzidisb6b2b182011-04-23 01:04:12 +0000266 // Include code completion token into the range of the scope otherwise
267 // when we try to annotate the scope tokens the dangling code completion
268 // token will cause assertion in
269 // Preprocessor::AnnotatePreviousCachedTokens.
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000270 SS.setEndLoc(Tok.getLocation());
271 cutOffParsing();
272 return true;
Douglas Gregor81b747b2009-09-17 21:32:03 +0000273 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000274 }
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Douglas Gregor39a8de12009-02-25 19:37:18 +0000276 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +0000277 // nested-name-specifier 'template'[opt] simple-template-id '::'
278
279 // Parse the optional 'template' keyword, then make sure we have
280 // 'identifier <' after it.
281 if (Tok.is(tok::kw_template)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000282 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +0000283 // nested-name-specifier, since they aren't allowed to start with
284 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000285 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +0000286 break;
287
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000288 TentativeParsingAction TPA(*this);
Chris Lattner77cf72a2009-06-26 03:47:46 +0000289 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000290
291 UnqualifiedId TemplateName;
292 if (Tok.is(tok::identifier)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000293 // Consume the identifier.
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000294 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000295 ConsumeToken();
296 } else if (Tok.is(tok::kw_operator)) {
297 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000298 TemplateName)) {
299 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000300 break;
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000301 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000302
Sean Hunte6252d12009-11-28 08:58:14 +0000303 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
304 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000305 Diag(TemplateName.getSourceRange().getBegin(),
306 diag::err_id_after_template_in_nested_name_spec)
307 << TemplateName.getSourceRange();
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000308 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000309 break;
310 }
311 } else {
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000312 TPA.Revert();
Chris Lattner77cf72a2009-06-26 03:47:46 +0000313 break;
314 }
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000316 // If the next token is not '<', we have a qualified-id that refers
317 // to a template name, such as T::template apply, but is not a
318 // template-id.
319 if (Tok.isNot(tok::less)) {
320 TPA.Revert();
321 break;
322 }
323
324 // Commit to parsing the template-id.
325 TPA.Commit();
Douglas Gregord6ab2322010-06-16 23:00:59 +0000326 TemplateTy Template;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000327 if (TemplateNameKind TNK
328 = Actions.ActOnDependentTemplateName(getCurScope(),
329 SS, TemplateKWLoc, TemplateName,
330 ObjectType, EnteringContext,
331 Template)) {
332 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateKWLoc,
333 TemplateName, false))
Douglas Gregord6ab2322010-06-16 23:00:59 +0000334 return true;
335 } else
John McCall9ba61662010-02-26 08:45:28 +0000336 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Chris Lattner77cf72a2009-06-26 03:47:46 +0000338 continue;
339 }
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Douglas Gregor39a8de12009-02-25 19:37:18 +0000341 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000342 // We have
Douglas Gregor39a8de12009-02-25 19:37:18 +0000343 //
344 // simple-template-id '::'
345 //
346 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000347 // right kind (it should name a type or be dependent), and then
348 // convert it into a type within the nested-name-specifier.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000349 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord4dca082010-02-24 18:44:31 +0000350 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
351 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000352 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000353 }
354
Richard Smith2db075b2013-03-26 01:15:19 +0000355 if (LastII)
356 *LastII = TemplateId->Name;
357
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000358 // Consume the template-id token.
359 ConsumeToken();
360
361 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
362 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000363
David Blaikie6796fc12011-11-07 03:30:03 +0000364 HasScopeSpecifier = true;
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000365
Benjamin Kramer5354e772012-08-23 23:38:35 +0000366 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000367 TemplateId->NumArgs);
368
369 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000370 SS,
371 TemplateId->TemplateKWLoc,
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000372 TemplateId->Template,
373 TemplateId->TemplateNameLoc,
374 TemplateId->LAngleLoc,
375 TemplateArgsPtr,
376 TemplateId->RAngleLoc,
377 CCLoc,
378 EnteringContext)) {
379 SourceLocation StartLoc
380 = SS.getBeginLoc().isValid()? SS.getBeginLoc()
381 : TemplateId->TemplateNameLoc;
382 SS.SetInvalid(SourceRange(StartLoc, CCLoc));
Chris Lattner67b9e832009-06-26 03:45:46 +0000383 }
Argyrios Kyrtzidiseccce7e2011-05-03 18:45:38 +0000384
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000385 continue;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000386 }
387
Chris Lattner5c7f7862009-06-26 03:52:38 +0000388
389 // The rest of the nested-name-specifier possibilities start with
390 // tok::identifier.
391 if (Tok.isNot(tok::identifier))
392 break;
393
394 IdentifierInfo &II = *Tok.getIdentifierInfo();
395
396 // nested-name-specifier:
397 // type-name '::'
398 // namespace-name '::'
399 // nested-name-specifier identifier '::'
400 Token Next = NextToken();
Chris Lattner46646492009-12-07 01:36:53 +0000401
402 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
403 // and emit a fixit hint for it.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000404 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000405 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
406 Tok.getLocation(),
407 Next.getLocation(), ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000408 EnteringContext) &&
409 // If the token after the colon isn't an identifier, it's still an
410 // error, but they probably meant something else strange so don't
411 // recover like this.
412 PP.LookAhead(1).is(tok::identifier)) {
413 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
Douglas Gregor849b2432010-03-31 17:46:05 +0000414 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregorb10cd042010-02-21 18:36:56 +0000415
416 // Recover as if the user wrote '::'.
417 Next.setKind(tok::coloncolon);
418 }
Chris Lattner46646492009-12-07 01:36:53 +0000419 }
420
Chris Lattner5c7f7862009-06-26 03:52:38 +0000421 if (Next.is(tok::coloncolon)) {
Douglas Gregor77549082010-02-24 21:29:12 +0000422 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000423 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor77549082010-02-24 21:29:12 +0000424 II, ObjectType)) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000425 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000426 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000427 }
428
Richard Smith2db075b2013-03-26 01:15:19 +0000429 if (LastII)
430 *LastII = &II;
431
Chris Lattner5c7f7862009-06-26 03:52:38 +0000432 // We have an identifier followed by a '::'. Lookup this name
433 // as the name in a nested-name-specifier.
434 SourceLocation IdLoc = ConsumeToken();
Chris Lattner46646492009-12-07 01:36:53 +0000435 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
436 "NextToken() not working properly!");
Chris Lattner5c7f7862009-06-26 03:52:38 +0000437 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Richard Trieu919b9552012-11-02 01:08:58 +0000439 CheckForLParenAfterColonColon();
440
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000441 HasScopeSpecifier = true;
442 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
443 ObjectType, EnteringContext, SS))
444 SS.SetInvalid(SourceRange(IdLoc, CCLoc));
445
Chris Lattner5c7f7862009-06-26 03:52:38 +0000446 continue;
447 }
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Richard Trieu950be712011-09-19 19:01:00 +0000449 CheckForTemplateAndDigraph(Next, ObjectType, EnteringContext, II, SS);
Richard Smithea698b32011-04-14 21:45:45 +0000450
Chris Lattner5c7f7862009-06-26 03:52:38 +0000451 // nested-name-specifier:
452 // type-name '<'
453 if (Next.is(tok::less)) {
454 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000455 UnqualifiedId TemplateName;
456 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000457 bool MemberOfUnknownSpecialization;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000458 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000459 /*hasTemplateKeyword=*/false,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000460 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000461 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000462 EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000463 Template,
464 MemberOfUnknownSpecialization)) {
David Blaikie6796fc12011-11-07 03:30:03 +0000465 // We have found a template name, so annotate this token
Chris Lattner5c7f7862009-06-26 03:52:38 +0000466 // with a template-id annotation. We do not permit the
467 // template-id to be translated into a type annotation,
468 // because some clients (e.g., the parsing of class template
469 // specializations) still want to see the original template-id
470 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000471 ConsumeToken();
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000472 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
473 TemplateName, false))
John McCall9ba61662010-02-26 08:45:28 +0000474 return true;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000475 continue;
Larisse Voufoef4579c2013-08-06 01:03:05 +0000476 }
477
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000478 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
Francois Pichet4147d302011-03-27 19:41:34 +0000479 (IsTypename || IsTemplateArgumentList(1))) {
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000480 // We have something like t::getAs<T>, where getAs is a
481 // member of an unknown specialization. However, this will only
482 // parse correctly as a template, so suggest the keyword 'template'
483 // before 'getAs' and treat this as a dependent template name.
Francois Pichet4147d302011-03-27 19:41:34 +0000484 unsigned DiagID = diag::err_missing_dependent_template_keyword;
David Blaikie4e4d0842012-03-11 07:00:24 +0000485 if (getLangOpts().MicrosoftExt)
Francois Pichetcf320c62011-04-22 08:25:24 +0000486 DiagID = diag::warn_missing_dependent_template_keyword;
Francois Pichet4147d302011-03-27 19:41:34 +0000487
488 Diag(Tok.getLocation(), DiagID)
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000489 << II.getName()
490 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
491
Douglas Gregord6ab2322010-06-16 23:00:59 +0000492 if (TemplateNameKind TNK
Douglas Gregor23c94db2010-07-02 17:43:08 +0000493 = Actions.ActOnDependentTemplateName(getCurScope(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000494 SS, SourceLocation(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000495 TemplateName, ObjectType,
496 EnteringContext, Template)) {
497 // Consume the identifier.
498 ConsumeToken();
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000499 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
500 TemplateName, false))
501 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000502 }
503 else
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000504 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000505
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000506 continue;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000507 }
508 }
509
Douglas Gregor39a8de12009-02-25 19:37:18 +0000510 // We don't have any tokens that form the beginning of a
511 // nested-name-specifier, so we're done.
512 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000513 }
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Douglas Gregord4dca082010-02-24 18:44:31 +0000515 // Even if we didn't see any pieces of a nested-name-specifier, we
516 // still check whether there is a tilde in this position, which
517 // indicates a potential pseudo-destructor.
518 if (CheckForDestructor && Tok.is(tok::tilde))
519 *MayBePseudoDestructor = true;
520
John McCall9ba61662010-02-26 08:45:28 +0000521 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000522}
523
524/// ParseCXXIdExpression - Handle id-expression.
525///
526/// id-expression:
527/// unqualified-id
528/// qualified-id
529///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000530/// qualified-id:
531/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
532/// '::' identifier
533/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000534/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000535///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000536/// NOTE: The standard specifies that, for qualified-id, the parser does not
537/// expect:
538///
539/// '::' conversion-function-id
540/// '::' '~' class-name
541///
542/// This may cause a slight inconsistency on diagnostics:
543///
544/// class C {};
545/// namespace A {}
546/// void f() {
547/// :: A :: ~ C(); // Some Sema error about using destructor with a
548/// // namespace.
549/// :: ~ C(); // Some Parser error like 'unexpected ~'.
550/// }
551///
552/// We simplify the parser a bit and make it work like:
553///
554/// qualified-id:
555/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
556/// '::' unqualified-id
557///
558/// That way Sema can handle and report similar errors for namespaces and the
559/// global scope.
560///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000561/// The isAddressOfOperand parameter indicates that this id-expression is a
562/// direct operand of the address-of operator. This is, besides member contexts,
563/// the only place where a qualified-id naming a non-static class member may
564/// appear.
565///
John McCall60d7b3a2010-08-24 06:29:42 +0000566ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000567 // qualified-id:
568 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
569 // '::' unqualified-id
570 //
571 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000572 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000573
574 SourceLocation TemplateKWLoc;
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000575 UnqualifiedId Name;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000576 if (ParseUnqualifiedId(SS,
577 /*EnteringContext=*/false,
578 /*AllowDestructorName=*/false,
579 /*AllowConstructorName=*/false,
John McCallb3d87482010-08-24 05:47:05 +0000580 /*ObjectType=*/ ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000581 TemplateKWLoc,
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000582 Name))
583 return ExprError();
John McCallb681b612009-11-22 02:49:43 +0000584
585 // This is only the direct operand of an & operator if it is not
586 // followed by a postfix-expression suffix.
John McCall9c72c602010-08-27 09:08:28 +0000587 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
588 isAddressOfOperand = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000589
590 return Actions.ActOnIdExpression(getCurScope(), SS, TemplateKWLoc, Name,
591 Tok.is(tok::l_paren), isAddressOfOperand);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000592}
593
Richard Smith0a664b82013-05-09 21:36:41 +0000594/// ParseLambdaExpression - Parse a C++11 lambda expression.
Douglas Gregorae7902c2011-08-04 15:30:47 +0000595///
596/// lambda-expression:
597/// lambda-introducer lambda-declarator[opt] compound-statement
598///
599/// lambda-introducer:
600/// '[' lambda-capture[opt] ']'
601///
602/// lambda-capture:
603/// capture-default
604/// capture-list
605/// capture-default ',' capture-list
606///
607/// capture-default:
608/// '&'
609/// '='
610///
611/// capture-list:
612/// capture
613/// capture-list ',' capture
614///
615/// capture:
Richard Smith0a664b82013-05-09 21:36:41 +0000616/// simple-capture
617/// init-capture [C++1y]
618///
619/// simple-capture:
Douglas Gregorae7902c2011-08-04 15:30:47 +0000620/// identifier
621/// '&' identifier
622/// 'this'
623///
Richard Smith0a664b82013-05-09 21:36:41 +0000624/// init-capture: [C++1y]
625/// identifier initializer
626/// '&' identifier initializer
627///
Douglas Gregorae7902c2011-08-04 15:30:47 +0000628/// lambda-declarator:
629/// '(' parameter-declaration-clause ')' attribute-specifier[opt]
630/// 'mutable'[opt] exception-specification[opt]
631/// trailing-return-type[opt]
632///
633ExprResult Parser::ParseLambdaExpression() {
634 // Parse lambda-introducer.
635 LambdaIntroducer Intro;
Bill Wendling2434dcf2013-12-05 05:25:04 +0000636 Optional<unsigned> DiagID = ParseLambdaIntroducer(Intro);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000637 if (DiagID) {
638 Diag(Tok, DiagID.getValue());
Alexey Bataev8fe24752013-11-18 08:17:37 +0000639 SkipUntil(tok::r_square, StopAtSemi);
640 SkipUntil(tok::l_brace, StopAtSemi);
641 SkipUntil(tok::r_brace, StopAtSemi);
Eli Friedmandc3b7232012-01-04 02:40:39 +0000642 return ExprError();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000643 }
644
645 return ParseLambdaExpressionAfterIntroducer(Intro);
646}
647
648/// TryParseLambdaExpression - Use lookahead and potentially tentative
649/// parsing to determine if we are looking at a C++0x lambda expression, and parse
650/// it if we are.
651///
652/// If we are not looking at a lambda expression, returns ExprError().
653ExprResult Parser::TryParseLambdaExpression() {
Richard Smith80ad52f2013-01-02 11:42:31 +0000654 assert(getLangOpts().CPlusPlus11
Douglas Gregorae7902c2011-08-04 15:30:47 +0000655 && Tok.is(tok::l_square)
656 && "Not at the start of a possible lambda expression.");
657
658 const Token Next = NextToken(), After = GetLookAheadToken(2);
659
660 // If lookahead indicates this is a lambda...
661 if (Next.is(tok::r_square) || // []
662 Next.is(tok::equal) || // [=
663 (Next.is(tok::amp) && // [&] or [&,
664 (After.is(tok::r_square) ||
665 After.is(tok::comma))) ||
666 (Next.is(tok::identifier) && // [identifier]
667 After.is(tok::r_square))) {
668 return ParseLambdaExpression();
669 }
670
Eli Friedmandc3b7232012-01-04 02:40:39 +0000671 // If lookahead indicates an ObjC message send...
672 // [identifier identifier
Douglas Gregorae7902c2011-08-04 15:30:47 +0000673 if (Next.is(tok::identifier) && After.is(tok::identifier)) {
Eli Friedmandc3b7232012-01-04 02:40:39 +0000674 return ExprEmpty();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000675 }
Bill Wendling2434dcf2013-12-05 05:25:04 +0000676
Eli Friedmandc3b7232012-01-04 02:40:39 +0000677 // Here, we're stuck: lambda introducers and Objective-C message sends are
678 // unambiguous, but it requires arbitrary lookhead. [a,b,c,d,e,f,g] is a
679 // lambda, and [a,b,c,d,e,f,g h] is a Objective-C message send. Instead of
680 // writing two routines to parse a lambda introducer, just try to parse
681 // a lambda introducer first, and fall back if that fails.
682 // (TryParseLambdaIntroducer never produces any diagnostic output.)
Douglas Gregorae7902c2011-08-04 15:30:47 +0000683 LambdaIntroducer Intro;
684 if (TryParseLambdaIntroducer(Intro))
Eli Friedmandc3b7232012-01-04 02:40:39 +0000685 return ExprEmpty();
Bill Wendling2434dcf2013-12-05 05:25:04 +0000686
Douglas Gregorae7902c2011-08-04 15:30:47 +0000687 return ParseLambdaExpressionAfterIntroducer(Intro);
688}
689
Richard Smith440d4562013-05-21 22:21:19 +0000690/// \brief Parse a lambda introducer.
691/// \param Intro A LambdaIntroducer filled in with information about the
692/// contents of the lambda-introducer.
693/// \param SkippedInits If non-null, we are disambiguating between an Obj-C
694/// message send and a lambda expression. In this mode, we will
695/// sometimes skip the initializers for init-captures and not fully
696/// populate \p Intro. This flag will be set to \c true if we do so.
697/// \return A DiagnosticID if it hit something unexpected. The location for
698/// for the diagnostic is that of the current token.
699Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro,
700 bool *SkippedInits) {
David Blaikiedc84cd52013-02-20 22:23:23 +0000701 typedef Optional<unsigned> DiagResult;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000702
703 assert(Tok.is(tok::l_square) && "Lambda expressions begin with '['.");
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000704 BalancedDelimiterTracker T(*this, tok::l_square);
705 T.consumeOpen();
706
707 Intro.Range.setBegin(T.getOpenLocation());
Douglas Gregorae7902c2011-08-04 15:30:47 +0000708
709 bool first = true;
710
711 // Parse capture-default.
712 if (Tok.is(tok::amp) &&
713 (NextToken().is(tok::comma) || NextToken().is(tok::r_square))) {
714 Intro.Default = LCD_ByRef;
Douglas Gregor3ac109c2012-02-10 17:46:20 +0000715 Intro.DefaultLoc = ConsumeToken();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000716 first = false;
717 } else if (Tok.is(tok::equal)) {
718 Intro.Default = LCD_ByCopy;
Douglas Gregor3ac109c2012-02-10 17:46:20 +0000719 Intro.DefaultLoc = ConsumeToken();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000720 first = false;
721 }
722
723 while (Tok.isNot(tok::r_square)) {
724 if (!first) {
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000725 if (Tok.isNot(tok::comma)) {
Douglas Gregor437fbc52012-07-31 00:50:07 +0000726 // Provide a completion for a lambda introducer here. Except
727 // in Objective-C, where this is Almost Surely meant to be a message
728 // send. In that case, fail here and let the ObjC message
729 // expression parser perform the completion.
Douglas Gregord48ab062012-07-31 15:27:48 +0000730 if (Tok.is(tok::code_completion) &&
731 !(getLangOpts().ObjC1 && Intro.Default == LCD_None &&
732 !Intro.Captures.empty())) {
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000733 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
734 /*AfterAmpersand=*/false);
735 ConsumeCodeCompletionToken();
736 break;
737 }
738
Douglas Gregorae7902c2011-08-04 15:30:47 +0000739 return DiagResult(diag::err_expected_comma_or_rsquare);
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000740 }
Douglas Gregorae7902c2011-08-04 15:30:47 +0000741 ConsumeToken();
742 }
743
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000744 if (Tok.is(tok::code_completion)) {
745 // If we're in Objective-C++ and we have a bare '[', then this is more
746 // likely to be a message receiver.
David Blaikie4e4d0842012-03-11 07:00:24 +0000747 if (getLangOpts().ObjC1 && first)
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000748 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
749 else
750 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
751 /*AfterAmpersand=*/false);
752 ConsumeCodeCompletionToken();
753 break;
754 }
Douglas Gregorae7902c2011-08-04 15:30:47 +0000755
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000756 first = false;
757
Douglas Gregorae7902c2011-08-04 15:30:47 +0000758 // Parse capture.
759 LambdaCaptureKind Kind = LCK_ByCopy;
760 SourceLocation Loc;
761 IdentifierInfo* Id = 0;
Douglas Gregora7365242012-02-14 19:27:52 +0000762 SourceLocation EllipsisLoc;
Richard Smith0a664b82013-05-09 21:36:41 +0000763 ExprResult Init;
Douglas Gregora7365242012-02-14 19:27:52 +0000764
Douglas Gregorae7902c2011-08-04 15:30:47 +0000765 if (Tok.is(tok::kw_this)) {
766 Kind = LCK_This;
767 Loc = ConsumeToken();
768 } else {
769 if (Tok.is(tok::amp)) {
770 Kind = LCK_ByRef;
771 ConsumeToken();
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000772
773 if (Tok.is(tok::code_completion)) {
774 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
775 /*AfterAmpersand=*/true);
776 ConsumeCodeCompletionToken();
777 break;
778 }
Douglas Gregorae7902c2011-08-04 15:30:47 +0000779 }
780
781 if (Tok.is(tok::identifier)) {
782 Id = Tok.getIdentifierInfo();
783 Loc = ConsumeToken();
784 } else if (Tok.is(tok::kw_this)) {
785 // FIXME: If we want to suggest a fixit here, will need to return more
786 // than just DiagnosticID. Perhaps full DiagnosticBuilder that can be
787 // Clear()ed to prevent emission in case of tentative parsing?
788 return DiagResult(diag::err_this_captured_by_reference);
789 } else {
790 return DiagResult(diag::err_expected_capture);
791 }
Richard Smith0a664b82013-05-09 21:36:41 +0000792
793 if (Tok.is(tok::l_paren)) {
794 BalancedDelimiterTracker Parens(*this, tok::l_paren);
795 Parens.consumeOpen();
796
797 ExprVector Exprs;
798 CommaLocsTy Commas;
Richard Smith440d4562013-05-21 22:21:19 +0000799 if (SkippedInits) {
800 Parens.skipToEnd();
801 *SkippedInits = true;
802 } else if (ParseExpressionList(Exprs, Commas)) {
Richard Smith0a664b82013-05-09 21:36:41 +0000803 Parens.skipToEnd();
804 Init = ExprError();
805 } else {
806 Parens.consumeClose();
807 Init = Actions.ActOnParenListExpr(Parens.getOpenLocation(),
808 Parens.getCloseLocation(),
809 Exprs);
810 }
811 } else if (Tok.is(tok::l_brace) || Tok.is(tok::equal)) {
Bill Wendling2434dcf2013-12-05 05:25:04 +0000812 // Each lambda init-capture forms its own full expression, which clears
813 // Actions.MaybeODRUseExprs. So create an expression evaluation context
814 // to save the necessary state, and restore it later.
815 EnterExpressionEvaluationContext EC(Actions,
816 Sema::PotentiallyEvaluated);
Richard Smith0a664b82013-05-09 21:36:41 +0000817 if (Tok.is(tok::equal))
818 ConsumeToken();
819
Richard Smith440d4562013-05-21 22:21:19 +0000820 if (!SkippedInits)
821 Init = ParseInitializer();
822 else if (Tok.is(tok::l_brace)) {
823 BalancedDelimiterTracker Braces(*this, tok::l_brace);
824 Braces.consumeOpen();
825 Braces.skipToEnd();
826 *SkippedInits = true;
827 } else {
828 // We're disambiguating this:
829 //
830 // [..., x = expr
831 //
832 // We need to find the end of the following expression in order to
833 // determine whether this is an Obj-C message send's receiver, or a
834 // lambda init-capture.
835 //
836 // Parse the expression to find where it ends, and annotate it back
837 // onto the tokens. We would have parsed this expression the same way
838 // in either case: both the RHS of an init-capture and the RHS of an
839 // assignment expression are parsed as an initializer-clause, and in
840 // neither case can anything be added to the scope between the '[' and
841 // here.
842 //
843 // FIXME: This is horrible. Adding a mechanism to skip an expression
844 // would be much cleaner.
845 // FIXME: If there is a ',' before the next ']' or ':', we can skip to
846 // that instead. (And if we see a ':' with no matching '?', we can
847 // classify this as an Obj-C message send.)
848 SourceLocation StartLoc = Tok.getLocation();
849 InMessageExpressionRAIIObject MaybeInMessageExpression(*this, true);
850 Init = ParseInitializer();
851
852 if (Tok.getLocation() != StartLoc) {
853 // Back out the lexing of the token after the initializer.
854 PP.RevertCachedTokens(1);
855
856 // Replace the consumed tokens with an appropriate annotation.
857 Tok.setLocation(StartLoc);
858 Tok.setKind(tok::annot_primary_expr);
859 setExprAnnotation(Tok, Init);
860 Tok.setAnnotationEndLoc(PP.getLastCachedTokenLocation());
861 PP.AnnotateCachedTokens(Tok);
862
863 // Consume the annotated initializer.
864 ConsumeToken();
865 }
866 }
Richard Smith0d8e9642013-05-16 06:20:58 +0000867 } else if (Tok.is(tok::ellipsis))
868 EllipsisLoc = ConsumeToken();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000869 }
Bill Wendling2434dcf2013-12-05 05:25:04 +0000870 // If this is an init capture, process the initialization expression
871 // right away. For lambda init-captures such as the following:
872 // const int x = 10;
873 // auto L = [i = x+1](int a) {
874 // return [j = x+2,
875 // &k = x](char b) { };
876 // };
877 // keep in mind that each lambda init-capture has to have:
878 // - its initialization expression executed in the context
879 // of the enclosing/parent decl-context.
880 // - but the variable itself has to be 'injected' into the
881 // decl-context of its lambda's call-operator (which has
882 // not yet been created).
883 // Each init-expression is a full-expression that has to get
884 // Sema-analyzed (for capturing etc.) before its lambda's
885 // call-operator's decl-context, scope & scopeinfo are pushed on their
886 // respective stacks. Thus if any variable is odr-used in the init-capture
887 // it will correctly get captured in the enclosing lambda, if one exists.
888 // The init-variables above are created later once the lambdascope and
889 // call-operators decl-context is pushed onto its respective stack.
Douglas Gregorae7902c2011-08-04 15:30:47 +0000890
Bill Wendling2434dcf2013-12-05 05:25:04 +0000891 // Since the lambda init-capture's initializer expression occurs in the
892 // context of the enclosing function or lambda, therefore we can not wait
893 // till a lambda scope has been pushed on before deciding whether the
894 // variable needs to be captured. We also need to process all
895 // lvalue-to-rvalue conversions and discarded-value conversions,
896 // so that we can avoid capturing certain constant variables.
897 // For e.g.,
898 // void test() {
899 // const int x = 10;
900 // auto L = [&z = x](char a) { <-- don't capture by the current lambda
901 // return [y = x](int i) { <-- don't capture by enclosing lambda
902 // return y;
903 // }
904 // };
905 // If x was not const, the second use would require 'L' to capture, and
906 // that would be an error.
907
908 ParsedType InitCaptureParsedType;
909 if (Init.isUsable()) {
910 // Get the pointer and store it in an lvalue, so we can use it as an
911 // out argument.
912 Expr *InitExpr = Init.get();
913 // This performs any lvalue-to-rvalue conversions if necessary, which
914 // can affect what gets captured in the containing decl-context.
915 QualType InitCaptureType = Actions.performLambdaInitCaptureInitialization(
916 Loc, Kind == LCK_ByRef, Id, InitExpr);
917 Init = InitExpr;
918 InitCaptureParsedType.set(InitCaptureType);
919 }
920 Intro.addCapture(Kind, Loc, Id, EllipsisLoc, Init, InitCaptureParsedType);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000921 }
922
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000923 T.consumeClose();
924 Intro.Range.setEnd(T.getCloseLocation());
Douglas Gregorae7902c2011-08-04 15:30:47 +0000925 return DiagResult();
926}
927
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000928/// TryParseLambdaIntroducer - Tentatively parse a lambda introducer.
Douglas Gregorae7902c2011-08-04 15:30:47 +0000929///
930/// Returns true if it hit something unexpected.
931bool Parser::TryParseLambdaIntroducer(LambdaIntroducer &Intro) {
932 TentativeParsingAction PA(*this);
933
Richard Smith440d4562013-05-21 22:21:19 +0000934 bool SkippedInits = false;
935 Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro, &SkippedInits));
Douglas Gregorae7902c2011-08-04 15:30:47 +0000936
937 if (DiagID) {
938 PA.Revert();
939 return true;
940 }
941
Richard Smith440d4562013-05-21 22:21:19 +0000942 if (SkippedInits) {
943 // Parse it again, but this time parse the init-captures too.
944 PA.Revert();
945 Intro = LambdaIntroducer();
946 DiagID = ParseLambdaIntroducer(Intro);
947 assert(!DiagID && "parsing lambda-introducer failed on reparse");
948 return false;
949 }
950
Douglas Gregorae7902c2011-08-04 15:30:47 +0000951 PA.Commit();
952 return false;
953}
954
955/// ParseLambdaExpressionAfterIntroducer - Parse the rest of a lambda
956/// expression.
957ExprResult Parser::ParseLambdaExpressionAfterIntroducer(
958 LambdaIntroducer &Intro) {
Eli Friedmandc3b7232012-01-04 02:40:39 +0000959 SourceLocation LambdaBeginLoc = Intro.Range.getBegin();
960 Diag(LambdaBeginLoc, diag::warn_cxx98_compat_lambda);
961
962 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), LambdaBeginLoc,
963 "lambda expression parsing");
964
Faisal Valifad9e132013-09-26 19:54:12 +0000965
966
Richard Smith0a664b82013-05-09 21:36:41 +0000967 // FIXME: Call into Actions to add any init-capture declarations to the
968 // scope while parsing the lambda-declarator and compound-statement.
969
Douglas Gregorae7902c2011-08-04 15:30:47 +0000970 // Parse lambda-declarator[opt].
971 DeclSpec DS(AttrFactory);
Eli Friedmanf88c4002012-01-04 04:41:38 +0000972 Declarator D(DS, Declarator::LambdaExprContext);
Faisal Valifad9e132013-09-26 19:54:12 +0000973 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
974 Actions.PushLambdaScope();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000975
976 if (Tok.is(tok::l_paren)) {
977 ParseScope PrototypeScope(this,
978 Scope::FunctionPrototypeScope |
Richard Smith3a2b7a12013-01-28 22:42:45 +0000979 Scope::FunctionDeclarationScope |
Douglas Gregorae7902c2011-08-04 15:30:47 +0000980 Scope::DeclScope);
981
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000982 SourceLocation DeclEndLoc;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000983 BalancedDelimiterTracker T(*this, tok::l_paren);
984 T.consumeOpen();
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000985 SourceLocation LParenLoc = T.getOpenLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000986
987 // Parse parameter-declaration-clause.
988 ParsedAttributes Attr(AttrFactory);
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000989 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000990 SourceLocation EllipsisLoc;
991
Faisal Valifad9e132013-09-26 19:54:12 +0000992
993 if (Tok.isNot(tok::r_paren)) {
Faisal Valifad9e132013-09-26 19:54:12 +0000994 Actions.RecordParsingTemplateParameterDepth(TemplateParameterDepth);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000995 ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc);
Faisal Valifad9e132013-09-26 19:54:12 +0000996 // For a generic lambda, each 'auto' within the parameter declaration
997 // clause creates a template type parameter, so increment the depth.
998 if (Actions.getCurGenericLambda())
999 ++CurTemplateDepthTracker;
1000 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001001 T.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00001002 SourceLocation RParenLoc = T.getCloseLocation();
1003 DeclEndLoc = RParenLoc;
Douglas Gregorae7902c2011-08-04 15:30:47 +00001004
1005 // Parse 'mutable'[opt].
1006 SourceLocation MutableLoc;
1007 if (Tok.is(tok::kw_mutable)) {
1008 MutableLoc = ConsumeToken();
1009 DeclEndLoc = MutableLoc;
1010 }
1011
1012 // Parse exception-specification[opt].
1013 ExceptionSpecificationType ESpecType = EST_None;
1014 SourceRange ESpecRange;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001015 SmallVector<ParsedType, 2> DynamicExceptions;
1016 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregorae7902c2011-08-04 15:30:47 +00001017 ExprResult NoexceptExpr;
Richard Smitha058fd42012-05-02 22:22:32 +00001018 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001019 DynamicExceptions,
1020 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00001021 NoexceptExpr);
Douglas Gregorae7902c2011-08-04 15:30:47 +00001022
1023 if (ESpecType != EST_None)
1024 DeclEndLoc = ESpecRange.getEnd();
1025
1026 // Parse attribute-specifier[opt].
Richard Smith4e24f0f2013-01-02 12:01:23 +00001027 MaybeParseCXX11Attributes(Attr, &DeclEndLoc);
Douglas Gregorae7902c2011-08-04 15:30:47 +00001028
Abramo Bagnara59c0a812012-10-04 21:42:10 +00001029 SourceLocation FunLocalRangeEnd = DeclEndLoc;
1030
Douglas Gregorae7902c2011-08-04 15:30:47 +00001031 // Parse trailing-return-type[opt].
Richard Smith54655be2012-06-12 01:51:59 +00001032 TypeResult TrailingReturnType;
Douglas Gregorae7902c2011-08-04 15:30:47 +00001033 if (Tok.is(tok::arrow)) {
Abramo Bagnara59c0a812012-10-04 21:42:10 +00001034 FunLocalRangeEnd = Tok.getLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +00001035 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00001036 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregorae7902c2011-08-04 15:30:47 +00001037 if (Range.getEnd().isValid())
1038 DeclEndLoc = Range.getEnd();
1039 }
1040
1041 PrototypeScope.Exit();
1042
Abramo Bagnara59c0a812012-10-04 21:42:10 +00001043 SourceLocation NoLoc;
Douglas Gregorae7902c2011-08-04 15:30:47 +00001044 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
Abramo Bagnara59c0a812012-10-04 21:42:10 +00001045 /*isAmbiguous=*/false,
1046 LParenLoc,
Douglas Gregorae7902c2011-08-04 15:30:47 +00001047 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnara59c0a812012-10-04 21:42:10 +00001048 EllipsisLoc, RParenLoc,
Douglas Gregorae7902c2011-08-04 15:30:47 +00001049 DS.getTypeQualifiers(),
1050 /*RefQualifierIsLValueRef=*/true,
Abramo Bagnara59c0a812012-10-04 21:42:10 +00001051 /*RefQualifierLoc=*/NoLoc,
1052 /*ConstQualifierLoc=*/NoLoc,
1053 /*VolatileQualifierLoc=*/NoLoc,
Douglas Gregorae7902c2011-08-04 15:30:47 +00001054 MutableLoc,
1055 ESpecType, ESpecRange.getBegin(),
1056 DynamicExceptions.data(),
1057 DynamicExceptionRanges.data(),
1058 DynamicExceptions.size(),
1059 NoexceptExpr.isUsable() ?
1060 NoexceptExpr.get() : 0,
Abramo Bagnara59c0a812012-10-04 21:42:10 +00001061 LParenLoc, FunLocalRangeEnd, D,
Douglas Gregorae7902c2011-08-04 15:30:47 +00001062 TrailingReturnType),
1063 Attr, DeclEndLoc);
Douglas Gregorc9ecec42012-02-16 21:53:36 +00001064 } else if (Tok.is(tok::kw_mutable) || Tok.is(tok::arrow)) {
1065 // It's common to forget that one needs '()' before 'mutable' or the
1066 // result type. Deal with this.
1067 Diag(Tok, diag::err_lambda_missing_parens)
1068 << Tok.is(tok::arrow)
1069 << FixItHint::CreateInsertion(Tok.getLocation(), "() ");
1070 SourceLocation DeclLoc = Tok.getLocation();
1071 SourceLocation DeclEndLoc = DeclLoc;
1072
1073 // Parse 'mutable', if it's there.
1074 SourceLocation MutableLoc;
1075 if (Tok.is(tok::kw_mutable)) {
1076 MutableLoc = ConsumeToken();
1077 DeclEndLoc = MutableLoc;
1078 }
1079
1080 // Parse the return type, if there is one.
Richard Smith54655be2012-06-12 01:51:59 +00001081 TypeResult TrailingReturnType;
Douglas Gregorc9ecec42012-02-16 21:53:36 +00001082 if (Tok.is(tok::arrow)) {
1083 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00001084 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregorc9ecec42012-02-16 21:53:36 +00001085 if (Range.getEnd().isValid())
1086 DeclEndLoc = Range.getEnd();
1087 }
1088
1089 ParsedAttributes Attr(AttrFactory);
Abramo Bagnara59c0a812012-10-04 21:42:10 +00001090 SourceLocation NoLoc;
Douglas Gregorc9ecec42012-02-16 21:53:36 +00001091 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
Abramo Bagnara59c0a812012-10-04 21:42:10 +00001092 /*isAmbiguous=*/false,
1093 /*LParenLoc=*/NoLoc,
1094 /*Params=*/0,
1095 /*NumParams=*/0,
1096 /*EllipsisLoc=*/NoLoc,
1097 /*RParenLoc=*/NoLoc,
1098 /*TypeQuals=*/0,
1099 /*RefQualifierIsLValueRef=*/true,
1100 /*RefQualifierLoc=*/NoLoc,
1101 /*ConstQualifierLoc=*/NoLoc,
1102 /*VolatileQualifierLoc=*/NoLoc,
1103 MutableLoc,
1104 EST_None,
1105 /*ESpecLoc=*/NoLoc,
1106 /*Exceptions=*/0,
1107 /*ExceptionRanges=*/0,
1108 /*NumExceptions=*/0,
1109 /*NoexceptExpr=*/0,
1110 DeclLoc, DeclEndLoc, D,
1111 TrailingReturnType),
Douglas Gregorc9ecec42012-02-16 21:53:36 +00001112 Attr, DeclEndLoc);
Douglas Gregorae7902c2011-08-04 15:30:47 +00001113 }
Douglas Gregorc9ecec42012-02-16 21:53:36 +00001114
Douglas Gregorae7902c2011-08-04 15:30:47 +00001115
Eli Friedman906a7e12012-01-06 03:05:34 +00001116 // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
1117 // it.
Douglas Gregorfccfb622012-02-21 22:51:27 +00001118 unsigned ScopeFlags = Scope::BlockScope | Scope::FnScope | Scope::DeclScope;
Douglas Gregorfccfb622012-02-21 22:51:27 +00001119 ParseScope BodyScope(this, ScopeFlags);
Eli Friedman906a7e12012-01-06 03:05:34 +00001120
Eli Friedmanec9ea722012-01-05 03:35:19 +00001121 Actions.ActOnStartOfLambdaDefinition(Intro, D, getCurScope());
1122
Douglas Gregorae7902c2011-08-04 15:30:47 +00001123 // Parse compound-statement.
Eli Friedmandc3b7232012-01-04 02:40:39 +00001124 if (!Tok.is(tok::l_brace)) {
Douglas Gregorae7902c2011-08-04 15:30:47 +00001125 Diag(Tok, diag::err_expected_lambda_body);
Eli Friedmandc3b7232012-01-04 02:40:39 +00001126 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
1127 return ExprError();
Douglas Gregorae7902c2011-08-04 15:30:47 +00001128 }
1129
Eli Friedmandc3b7232012-01-04 02:40:39 +00001130 StmtResult Stmt(ParseCompoundStatementBody());
1131 BodyScope.Exit();
1132
Eli Friedmandeeab902012-01-04 02:46:53 +00001133 if (!Stmt.isInvalid())
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00001134 return Actions.ActOnLambdaExpr(LambdaBeginLoc, Stmt.take(), getCurScope());
Eli Friedmandc3b7232012-01-04 02:40:39 +00001135
Eli Friedmandeeab902012-01-04 02:46:53 +00001136 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
1137 return ExprError();
Douglas Gregorae7902c2011-08-04 15:30:47 +00001138}
1139
Reid Spencer5f016e22007-07-11 17:01:13 +00001140/// ParseCXXCasts - This handles the various ways to cast expressions to another
1141/// type.
1142///
1143/// postfix-expression: [C++ 5.2p1]
1144/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
1145/// 'static_cast' '<' type-name '>' '(' expression ')'
1146/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
1147/// 'const_cast' '<' type-name '>' '(' expression ')'
1148///
John McCall60d7b3a2010-08-24 06:29:42 +00001149ExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001150 tok::TokenKind Kind = Tok.getKind();
1151 const char *CastName = 0; // For error messages
1152
1153 switch (Kind) {
David Blaikieeb2d1f12011-09-23 20:26:49 +00001154 default: llvm_unreachable("Unknown C++ cast!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001155 case tok::kw_const_cast: CastName = "const_cast"; break;
1156 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
1157 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
1158 case tok::kw_static_cast: CastName = "static_cast"; break;
1159 }
1160
1161 SourceLocation OpLoc = ConsumeToken();
1162 SourceLocation LAngleBracketLoc = Tok.getLocation();
1163
Richard Smithea698b32011-04-14 21:45:45 +00001164 // Check for "<::" which is parsed as "[:". If found, fix token stream,
1165 // diagnose error, suggest fix, and recover parsing.
Richard Smith78fe3e02012-08-20 17:37:52 +00001166 if (Tok.is(tok::l_square) && Tok.getLength() == 2) {
1167 Token Next = NextToken();
1168 if (Next.is(tok::colon) && areTokensAdjacent(Tok, Next))
1169 FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
1170 }
Richard Smithea698b32011-04-14 21:45:45 +00001171
Reid Spencer5f016e22007-07-11 17:01:13 +00001172 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +00001173 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001174
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00001175 // Parse the common declaration-specifiers piece.
1176 DeclSpec DS(AttrFactory);
1177 ParseSpecifierQualifierList(DS);
1178
1179 // Parse the abstract-declarator, if present.
1180 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1181 ParseDeclarator(DeclaratorInfo);
1182
Reid Spencer5f016e22007-07-11 17:01:13 +00001183 SourceLocation RAngleBracketLoc = Tok.getLocation();
1184
Chris Lattner1ab3b962008-11-18 07:48:38 +00001185 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +00001186 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +00001187
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001188 SourceLocation LParenLoc, RParenLoc;
1189 BalancedDelimiterTracker T(*this, tok::l_paren);
Reid Spencer5f016e22007-07-11 17:01:13 +00001190
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001191 if (T.expectAndConsume(diag::err_expected_lparen_after, CastName))
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +00001192 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001193
John McCall60d7b3a2010-08-24 06:29:42 +00001194 ExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +00001195
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +00001196 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001197 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00001198
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00001199 if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
Douglas Gregor49badde2008-10-27 19:41:14 +00001200 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00001201 LAngleBracketLoc, DeclaratorInfo,
Douglas Gregor809070a2009-02-18 17:45:20 +00001202 RAngleBracketLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001203 T.getOpenLocation(), Result.take(),
1204 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001205
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001206 return Result;
Reid Spencer5f016e22007-07-11 17:01:13 +00001207}
1208
Sebastian Redlc42e1182008-11-11 11:37:55 +00001209/// ParseCXXTypeid - This handles the C++ typeid expression.
1210///
1211/// postfix-expression: [C++ 5.2p1]
1212/// 'typeid' '(' expression ')'
1213/// 'typeid' '(' type-id ')'
1214///
John McCall60d7b3a2010-08-24 06:29:42 +00001215ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +00001216 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
1217
1218 SourceLocation OpLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001219 SourceLocation LParenLoc, RParenLoc;
1220 BalancedDelimiterTracker T(*this, tok::l_paren);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001221
1222 // typeid expressions are always parenthesized.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001223 if (T.expectAndConsume(diag::err_expected_lparen_after, "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +00001224 return ExprError();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001225 LParenLoc = T.getOpenLocation();
Sebastian Redlc42e1182008-11-11 11:37:55 +00001226
John McCall60d7b3a2010-08-24 06:29:42 +00001227 ExprResult Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +00001228
Richard Smith05766812012-08-18 00:55:03 +00001229 // C++0x [expr.typeid]p3:
1230 // When typeid is applied to an expression other than an lvalue of a
1231 // polymorphic class type [...] The expression is an unevaluated
1232 // operand (Clause 5).
1233 //
1234 // Note that we can't tell whether the expression is an lvalue of a
1235 // polymorphic class type until after we've parsed the expression; we
1236 // speculatively assume the subexpression is unevaluated, and fix it up
1237 // later.
1238 //
1239 // We enter the unevaluated context before trying to determine whether we
1240 // have a type-id, because the tentative parse logic will try to resolve
1241 // names, and must treat them as unevaluated.
Eli Friedman80bfa3d2012-09-26 04:34:21 +00001242 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
1243 Sema::ReuseLambdaContextDecl);
Richard Smith05766812012-08-18 00:55:03 +00001244
Sebastian Redlc42e1182008-11-11 11:37:55 +00001245 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +00001246 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +00001247
1248 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001249 T.consumeClose();
1250 RParenLoc = T.getCloseLocation();
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +00001251 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001252 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +00001253
1254 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallb3d87482010-08-24 05:47:05 +00001255 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001256 } else {
1257 Result = ParseExpression();
1258
1259 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001260 if (Result.isInvalid())
Alexey Bataev8fe24752013-11-18 08:17:37 +00001261 SkipUntil(tok::r_paren, StopAtSemi);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001262 else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001263 T.consumeClose();
1264 RParenLoc = T.getCloseLocation();
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +00001265 if (RParenLoc.isInvalid())
1266 return ExprError();
Douglas Gregorfadb53b2011-03-12 01:48:56 +00001267
Sebastian Redlc42e1182008-11-11 11:37:55 +00001268 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +00001269 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001270 }
1271 }
1272
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001273 return Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +00001274}
1275
Francois Pichet01b7c302010-09-08 12:20:18 +00001276/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
1277///
1278/// '__uuidof' '(' expression ')'
1279/// '__uuidof' '(' type-id ')'
1280///
1281ExprResult Parser::ParseCXXUuidof() {
1282 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
1283
1284 SourceLocation OpLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001285 BalancedDelimiterTracker T(*this, tok::l_paren);
Francois Pichet01b7c302010-09-08 12:20:18 +00001286
1287 // __uuidof expressions are always parenthesized.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001288 if (T.expectAndConsume(diag::err_expected_lparen_after, "__uuidof"))
Francois Pichet01b7c302010-09-08 12:20:18 +00001289 return ExprError();
1290
1291 ExprResult Result;
1292
1293 if (isTypeIdInParens()) {
1294 TypeResult Ty = ParseTypeName();
1295
1296 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001297 T.consumeClose();
Francois Pichet01b7c302010-09-08 12:20:18 +00001298
1299 if (Ty.isInvalid())
1300 return ExprError();
1301
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001302 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), /*isType=*/true,
1303 Ty.get().getAsOpaquePtr(),
1304 T.getCloseLocation());
Francois Pichet01b7c302010-09-08 12:20:18 +00001305 } else {
1306 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1307 Result = ParseExpression();
1308
1309 // Match the ')'.
1310 if (Result.isInvalid())
Alexey Bataev8fe24752013-11-18 08:17:37 +00001311 SkipUntil(tok::r_paren, StopAtSemi);
Francois Pichet01b7c302010-09-08 12:20:18 +00001312 else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001313 T.consumeClose();
Francois Pichet01b7c302010-09-08 12:20:18 +00001314
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001315 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(),
1316 /*isType=*/false,
1317 Result.release(), T.getCloseLocation());
Francois Pichet01b7c302010-09-08 12:20:18 +00001318 }
1319 }
1320
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001321 return Result;
Francois Pichet01b7c302010-09-08 12:20:18 +00001322}
1323
Douglas Gregord4dca082010-02-24 18:44:31 +00001324/// \brief Parse a C++ pseudo-destructor expression after the base,
1325/// . or -> operator, and nested-name-specifier have already been
1326/// parsed.
1327///
1328/// postfix-expression: [C++ 5.2]
1329/// postfix-expression . pseudo-destructor-name
1330/// postfix-expression -> pseudo-destructor-name
1331///
1332/// pseudo-destructor-name:
1333/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
1334/// ::[opt] nested-name-specifier template simple-template-id ::
1335/// ~type-name
1336/// ::[opt] nested-name-specifier[opt] ~type-name
1337///
John McCall60d7b3a2010-08-24 06:29:42 +00001338ExprResult
Douglas Gregord4dca082010-02-24 18:44:31 +00001339Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
1340 tok::TokenKind OpKind,
1341 CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +00001342 ParsedType ObjectType) {
Douglas Gregord4dca082010-02-24 18:44:31 +00001343 // We're parsing either a pseudo-destructor-name or a dependent
1344 // member access that has the same form as a
1345 // pseudo-destructor-name. We parse both in the same way and let
1346 // the action model sort them out.
1347 //
1348 // Note that the ::[opt] nested-name-specifier[opt] has already
1349 // been parsed, and if there was a simple-template-id, it has
1350 // been coalesced into a template-id annotation token.
1351 UnqualifiedId FirstTypeName;
1352 SourceLocation CCLoc;
1353 if (Tok.is(tok::identifier)) {
1354 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1355 ConsumeToken();
1356 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1357 CCLoc = ConsumeToken();
1358 } else if (Tok.is(tok::annot_template_id)) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001359 // FIXME: retrieve TemplateKWLoc from template-id annotation and
1360 // store it in the pseudo-dtor node (to be used when instantiating it).
Douglas Gregord4dca082010-02-24 18:44:31 +00001361 FirstTypeName.setTemplateId(
1362 (TemplateIdAnnotation *)Tok.getAnnotationValue());
1363 ConsumeToken();
1364 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1365 CCLoc = ConsumeToken();
1366 } else {
1367 FirstTypeName.setIdentifier(0, SourceLocation());
1368 }
1369
1370 // Parse the tilde.
1371 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
1372 SourceLocation TildeLoc = ConsumeToken();
David Blaikie91ec7892011-12-16 16:03:09 +00001373
1374 if (Tok.is(tok::kw_decltype) && !FirstTypeName.isValid() && SS.isEmpty()) {
1375 DeclSpec DS(AttrFactory);
Benjamin Kramer85c60db2011-12-18 12:18:02 +00001376 ParseDecltypeSpecifier(DS);
David Blaikie91ec7892011-12-16 16:03:09 +00001377 if (DS.getTypeSpecType() == TST_error)
1378 return ExprError();
1379 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, OpLoc,
1380 OpKind, TildeLoc, DS,
1381 Tok.is(tok::l_paren));
1382 }
1383
Douglas Gregord4dca082010-02-24 18:44:31 +00001384 if (!Tok.is(tok::identifier)) {
1385 Diag(Tok, diag::err_destructor_tilde_identifier);
1386 return ExprError();
1387 }
1388
1389 // Parse the second type.
1390 UnqualifiedId SecondTypeName;
1391 IdentifierInfo *Name = Tok.getIdentifierInfo();
1392 SourceLocation NameLoc = ConsumeToken();
1393 SecondTypeName.setIdentifier(Name, NameLoc);
1394
1395 // If there is a '<', the second type name is a template-id. Parse
1396 // it as such.
1397 if (Tok.is(tok::less) &&
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001398 ParseUnqualifiedIdTemplateId(SS, SourceLocation(),
1399 Name, NameLoc,
1400 false, ObjectType, SecondTypeName,
1401 /*AssumeTemplateName=*/true))
Douglas Gregord4dca082010-02-24 18:44:31 +00001402 return ExprError();
1403
John McCall9ae2f072010-08-23 23:25:46 +00001404 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
1405 OpLoc, OpKind,
Douglas Gregord4dca082010-02-24 18:44:31 +00001406 SS, FirstTypeName, CCLoc,
1407 TildeLoc, SecondTypeName,
1408 Tok.is(tok::l_paren));
1409}
1410
Reid Spencer5f016e22007-07-11 17:01:13 +00001411/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
1412///
1413/// boolean-literal: [C++ 2.13.5]
1414/// 'true'
1415/// 'false'
John McCall60d7b3a2010-08-24 06:29:42 +00001416ExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001417 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001418 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +00001419}
Chris Lattner50dd2892008-02-26 00:51:44 +00001420
1421/// ParseThrowExpression - This handles the C++ throw expression.
1422///
1423/// throw-expression: [C++ 15]
1424/// 'throw' assignment-expression[opt]
John McCall60d7b3a2010-08-24 06:29:42 +00001425ExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +00001426 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +00001427 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +00001428
Chris Lattner2a2819a2008-04-06 06:02:23 +00001429 // If the current token isn't the start of an assignment-expression,
1430 // then the expression is not present. This handles things like:
1431 // "C ? throw : (void)42", which is crazy but legal.
1432 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
1433 case tok::semi:
1434 case tok::r_paren:
1435 case tok::r_square:
1436 case tok::r_brace:
1437 case tok::colon:
1438 case tok::comma:
Douglas Gregorbca01b42011-07-06 22:04:06 +00001439 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0);
Chris Lattner50dd2892008-02-26 00:51:44 +00001440
Chris Lattner2a2819a2008-04-06 06:02:23 +00001441 default:
John McCall60d7b3a2010-08-24 06:29:42 +00001442 ExprResult Expr(ParseAssignmentExpression());
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001443 if (Expr.isInvalid()) return Expr;
Douglas Gregorbca01b42011-07-06 22:04:06 +00001444 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take());
Chris Lattner2a2819a2008-04-06 06:02:23 +00001445 }
Chris Lattner50dd2892008-02-26 00:51:44 +00001446}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001447
1448/// ParseCXXThis - This handles the C++ 'this' pointer.
1449///
1450/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
1451/// a non-lvalue expression whose value is the address of the object for which
1452/// the function is called.
John McCall60d7b3a2010-08-24 06:29:42 +00001453ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001454 assert(Tok.is(tok::kw_this) && "Not 'this'!");
1455 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001456 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001457}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001458
1459/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1460/// Can be interpreted either as function-style casting ("int(x)")
1461/// or class type construction ("ClassType(x,y,z)")
1462/// or creation of a value-initialized type ("int()").
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001463/// See [C++ 5.2.3].
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001464///
1465/// postfix-expression: [C++ 5.2p1]
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001466/// simple-type-specifier '(' expression-list[opt] ')'
1467/// [C++0x] simple-type-specifier braced-init-list
1468/// typename-specifier '(' expression-list[opt] ')'
1469/// [C++0x] typename-specifier braced-init-list
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001470///
John McCall60d7b3a2010-08-24 06:29:42 +00001471ExprResult
Sebastian Redl20df9b72008-12-11 22:51:44 +00001472Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001473 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallb3d87482010-08-24 05:47:05 +00001474 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001475
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001476 assert((Tok.is(tok::l_paren) ||
Richard Smith80ad52f2013-01-02 11:42:31 +00001477 (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)))
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001478 && "Expected '(' or '{'!");
Douglas Gregorbc61bd82011-01-11 00:33:19 +00001479
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001480 if (Tok.is(tok::l_brace)) {
Sebastian Redl6dc00f62012-02-12 18:41:05 +00001481 ExprResult Init = ParseBraceInitializer();
1482 if (Init.isInvalid())
1483 return Init;
1484 Expr *InitList = Init.take();
1485 return Actions.ActOnCXXTypeConstructExpr(TypeRep, SourceLocation(),
1486 MultiExprArg(&InitList, 1),
1487 SourceLocation());
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001488 } else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001489 BalancedDelimiterTracker T(*this, tok::l_paren);
1490 T.consumeOpen();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001491
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001492 ExprVector Exprs;
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001493 CommaLocsTy CommaLocs;
1494
1495 if (Tok.isNot(tok::r_paren)) {
1496 if (ParseExpressionList(Exprs, CommaLocs)) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00001497 SkipUntil(tok::r_paren, StopAtSemi);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001498 return ExprError();
1499 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001500 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001501
1502 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001503 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001504
1505 // TypeRep could be null, if it references an invalid typedef.
1506 if (!TypeRep)
1507 return ExprError();
1508
1509 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1510 "Unexpected number of commas!");
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001511 return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001512 Exprs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001513 T.getCloseLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001514 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001515}
1516
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001517/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001518///
1519/// condition:
1520/// expression
1521/// type-specifier-seq declarator '=' assignment-expression
Richard Smith0635aa72012-02-22 06:49:09 +00001522/// [C++11] type-specifier-seq declarator '=' initializer-clause
1523/// [C++11] type-specifier-seq declarator braced-init-list
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001524/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
1525/// '=' assignment-expression
1526///
Dmitri Gribenko1ddbd892012-08-24 00:01:24 +00001527/// \param ExprOut if the condition was parsed as an expression, the parsed
1528/// expression.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001529///
Dmitri Gribenko1ddbd892012-08-24 00:01:24 +00001530/// \param DeclOut if the condition was parsed as a declaration, the parsed
1531/// declaration.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001532///
Douglas Gregor586596f2010-05-06 17:25:47 +00001533/// \param Loc The location of the start of the statement that requires this
1534/// condition, e.g., the "for" in a for loop.
1535///
1536/// \param ConvertToBoolean Whether the condition expression should be
1537/// converted to a boolean value.
1538///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001539/// \returns true if there was a parsing, false otherwise.
John McCall60d7b3a2010-08-24 06:29:42 +00001540bool Parser::ParseCXXCondition(ExprResult &ExprOut,
1541 Decl *&DeclOut,
Douglas Gregor586596f2010-05-06 17:25:47 +00001542 SourceLocation Loc,
1543 bool ConvertToBoolean) {
Douglas Gregor01dfea02010-01-10 23:08:15 +00001544 if (Tok.is(tok::code_completion)) {
John McCallf312b1e2010-08-26 23:41:50 +00001545 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001546 cutOffParsing();
1547 return true;
Douglas Gregor01dfea02010-01-10 23:08:15 +00001548 }
1549
Sean Hunt2edf0a22012-06-23 05:07:58 +00001550 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00001551 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00001552
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001553 if (!isCXXConditionDeclaration()) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001554 ProhibitAttributes(attrs);
1555
Douglas Gregor586596f2010-05-06 17:25:47 +00001556 // Parse the expression.
John McCall60d7b3a2010-08-24 06:29:42 +00001557 ExprOut = ParseExpression(); // expression
1558 DeclOut = 0;
1559 if (ExprOut.isInvalid())
Douglas Gregor586596f2010-05-06 17:25:47 +00001560 return true;
1561
1562 // If required, convert to a boolean value.
1563 if (ConvertToBoolean)
John McCall60d7b3a2010-08-24 06:29:42 +00001564 ExprOut
1565 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
1566 return ExprOut.isInvalid();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001567 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001568
1569 // type-specifier-seq
John McCall0b7e6782011-03-24 11:26:52 +00001570 DeclSpec DS(AttrFactory);
Richard Smith6b3d3e52013-02-20 19:22:51 +00001571 DS.takeAttributesFrom(attrs);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001572 ParseSpecifierQualifierList(DS);
1573
1574 // declarator
1575 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
1576 ParseDeclarator(DeclaratorInfo);
1577
1578 // simple-asm-expr[opt]
1579 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00001580 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00001581 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001582 if (AsmLabel.isInvalid()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00001583 SkipUntil(tok::semi, StopAtSemi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001584 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001585 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001586 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001587 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001588 }
1589
1590 // If attributes are present, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001591 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001592
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001593 // Type-check the declaration itself.
John McCall60d7b3a2010-08-24 06:29:42 +00001594 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall7f040a92010-12-24 02:08:15 +00001595 DeclaratorInfo);
John McCall60d7b3a2010-08-24 06:29:42 +00001596 DeclOut = Dcl.get();
1597 ExprOut = ExprError();
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001598
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001599 // '=' assignment-expression
Richard Trieud6c7c672012-01-18 22:54:52 +00001600 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Smith0635aa72012-02-22 06:49:09 +00001601 bool CopyInitialization = isTokenEqualOrEqualTypo();
1602 if (CopyInitialization)
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001603 ConsumeToken();
Richard Smith0635aa72012-02-22 06:49:09 +00001604
1605 ExprResult InitExpr = ExprError();
Richard Smith80ad52f2013-01-02 11:42:31 +00001606 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith0635aa72012-02-22 06:49:09 +00001607 Diag(Tok.getLocation(),
1608 diag::warn_cxx98_compat_generalized_initializer_lists);
1609 InitExpr = ParseBraceInitializer();
1610 } else if (CopyInitialization) {
1611 InitExpr = ParseAssignmentExpression();
1612 } else if (Tok.is(tok::l_paren)) {
1613 // This was probably an attempt to initialize the variable.
1614 SourceLocation LParen = ConsumeParen(), RParen = LParen;
Alexey Bataev8fe24752013-11-18 08:17:37 +00001615 if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch))
Richard Smith0635aa72012-02-22 06:49:09 +00001616 RParen = ConsumeParen();
1617 Diag(DeclOut ? DeclOut->getLocation() : LParen,
1618 diag::err_expected_init_in_condition_lparen)
1619 << SourceRange(LParen, RParen);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001620 } else {
Richard Smith0635aa72012-02-22 06:49:09 +00001621 Diag(DeclOut ? DeclOut->getLocation() : Tok.getLocation(),
1622 diag::err_expected_init_in_condition);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001623 }
Richard Smith0635aa72012-02-22 06:49:09 +00001624
1625 if (!InitExpr.isInvalid())
1626 Actions.AddInitializerToDecl(DeclOut, InitExpr.take(), !CopyInitialization,
Richard Smitha2c36462013-04-26 16:15:35 +00001627 DS.containsPlaceholderType());
Richard Smithdc7a4f52013-04-30 13:56:41 +00001628 else
1629 Actions.ActOnInitializerError(DeclOut);
Richard Smith0635aa72012-02-22 06:49:09 +00001630
Douglas Gregor586596f2010-05-06 17:25:47 +00001631 // FIXME: Build a reference to this declaration? Convert it to bool?
1632 // (This is currently handled by Sema).
Richard Smith483b9f32011-02-21 20:05:19 +00001633
1634 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregor586596f2010-05-06 17:25:47 +00001635
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001636 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001637}
1638
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001639/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1640/// This should only be called when the current token is known to be part of
1641/// simple-type-specifier.
1642///
1643/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001644/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001645/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1646/// char
1647/// wchar_t
1648/// bool
1649/// short
1650/// int
1651/// long
1652/// signed
1653/// unsigned
1654/// float
1655/// double
1656/// void
1657/// [GNU] typeof-specifier
1658/// [C++0x] auto [TODO]
1659///
1660/// type-name:
1661/// class-name
1662/// enum-name
1663/// typedef-name
1664///
1665void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1666 DS.SetRangeStart(Tok.getLocation());
1667 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001668 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001669 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001670
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001671 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +00001672 case tok::identifier: // foo::bar
1673 case tok::coloncolon: // ::foo::bar
David Blaikieb219cfc2011-09-23 05:06:16 +00001674 llvm_unreachable("Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +00001675 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00001676 llvm_unreachable("Not a simple-type-specifier token!");
Chris Lattner55a7cef2009-01-05 00:13:00 +00001677
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001678 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001679 case tok::annot_typename: {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001680 if (getTypeAnnotation(Tok))
1681 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
1682 getTypeAnnotation(Tok));
1683 else
1684 DS.SetTypeSpecError();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001685
1686 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1687 ConsumeToken();
1688
1689 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1690 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1691 // Objective-C interface. If we don't have Objective-C or a '<', this is
1692 // just a normal reference to a typedef name.
David Blaikie4e4d0842012-03-11 07:00:24 +00001693 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001694 ParseObjCProtocolQualifiers(DS);
1695
1696 DS.Finish(Diags, PP);
1697 return;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001698 }
Mike Stump1eb44332009-09-09 15:08:12 +00001699
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001700 // builtin types
1701 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001702 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001703 break;
1704 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +00001705 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001706 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00001707 case tok::kw___int64:
1708 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1709 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001710 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001711 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001712 break;
1713 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001714 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001715 break;
1716 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001717 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001718 break;
1719 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001720 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001721 break;
1722 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001723 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001724 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00001725 case tok::kw___int128:
1726 DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, DiagID);
1727 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001728 case tok::kw_half:
1729 DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
1730 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001731 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001732 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001733 break;
1734 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001735 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001736 break;
1737 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001738 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001739 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001740 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001741 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001742 break;
1743 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001744 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001745 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001746 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +00001747 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001748 break;
David Blaikie5e089fe2012-01-24 05:47:35 +00001749 case tok::annot_decltype:
1750 case tok::kw_decltype:
1751 DS.SetRangeEnd(ParseDecltypeSpecifier(DS));
1752 return DS.Finish(Diags, PP);
Mike Stump1eb44332009-09-09 15:08:12 +00001753
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001754 // GNU typeof support.
1755 case tok::kw_typeof:
1756 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001757 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001758 return;
1759 }
Chris Lattnerb31757b2009-01-06 05:06:21 +00001760 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001761 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1762 else
1763 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001764 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001765 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001766}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001767
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001768/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1769/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1770/// e.g., "const short int". Note that the DeclSpec is *not* finished
1771/// by parsing the type-specifier-seq, because these sequences are
1772/// typically followed by some form of declarator. Returns true and
1773/// emits diagnostics if this is not a type-specifier-seq, false
1774/// otherwise.
1775///
1776/// type-specifier-seq: [C++ 8.1]
1777/// type-specifier type-specifier-seq[opt]
1778///
1779bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
Richard Smith69730c12012-03-12 07:56:15 +00001780 ParseSpecifierQualifierList(DS, AS_none, DSC_type_specifier);
Douglas Gregor396a9f22010-02-24 23:13:13 +00001781 DS.Finish(Diags, PP);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001782 return false;
1783}
1784
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001785/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1786/// some form.
1787///
1788/// This routine is invoked when a '<' is encountered after an identifier or
1789/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1790/// whether the unqualified-id is actually a template-id. This routine will
1791/// then parse the template arguments and form the appropriate template-id to
1792/// return to the caller.
1793///
1794/// \param SS the nested-name-specifier that precedes this template-id, if
1795/// we're actually parsing a qualified-id.
1796///
1797/// \param Name for constructor and destructor names, this is the actual
1798/// identifier that may be a template-name.
1799///
1800/// \param NameLoc the location of the class-name in a constructor or
1801/// destructor.
1802///
1803/// \param EnteringContext whether we're entering the scope of the
1804/// nested-name-specifier.
1805///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001806/// \param ObjectType if this unqualified-id occurs within a member access
1807/// expression, the type of the base object whose member is being accessed.
1808///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001809/// \param Id as input, describes the template-name or operator-function-id
1810/// that precedes the '<'. If template arguments were parsed successfully,
1811/// will be updated with the template-id.
1812///
Douglas Gregord4dca082010-02-24 18:44:31 +00001813/// \param AssumeTemplateId When true, this routine will assume that the name
1814/// refers to a template without performing name lookup to verify.
1815///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001816/// \returns true if a parse error occurred, false otherwise.
1817bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001818 SourceLocation TemplateKWLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001819 IdentifierInfo *Name,
1820 SourceLocation NameLoc,
1821 bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001822 ParsedType ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +00001823 UnqualifiedId &Id,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001824 bool AssumeTemplateId) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001825 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1826 "Expected '<' to finish parsing a template-id");
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001827
1828 TemplateTy Template;
1829 TemplateNameKind TNK = TNK_Non_template;
1830 switch (Id.getKind()) {
1831 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +00001832 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +00001833 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +00001834 if (AssumeTemplateId) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001835 TNK = Actions.ActOnDependentTemplateName(getCurScope(), SS, TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001836 Id, ObjectType, EnteringContext,
1837 Template);
1838 if (TNK == TNK_Non_template)
1839 return true;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001840 } else {
1841 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001842 TNK = Actions.isTemplateName(getCurScope(), SS,
1843 TemplateKWLoc.isValid(), Id,
1844 ObjectType, EnteringContext, Template,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001845 MemberOfUnknownSpecialization);
1846
1847 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1848 ObjectType && IsTemplateArgumentList()) {
1849 // We have something like t->getAs<T>(), where getAs is a
1850 // member of an unknown specialization. However, this will only
1851 // parse correctly as a template, so suggest the keyword 'template'
1852 // before 'getAs' and treat this as a dependent template name.
1853 std::string Name;
1854 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1855 Name = Id.Identifier->getName();
1856 else {
1857 Name = "operator ";
1858 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1859 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1860 else
1861 Name += Id.Identifier->getName();
1862 }
1863 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1864 << Name
1865 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001866 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1867 SS, TemplateKWLoc, Id,
1868 ObjectType, EnteringContext,
1869 Template);
Douglas Gregord6ab2322010-06-16 23:00:59 +00001870 if (TNK == TNK_Non_template)
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001871 return true;
1872 }
1873 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001874 break;
1875
Douglas Gregor014e88d2009-11-03 23:16:33 +00001876 case UnqualifiedId::IK_ConstructorName: {
1877 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001878 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001879 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c153532010-08-06 12:11:11 +00001880 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1881 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001882 EnteringContext, Template,
1883 MemberOfUnknownSpecialization);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001884 break;
1885 }
1886
Douglas Gregor014e88d2009-11-03 23:16:33 +00001887 case UnqualifiedId::IK_DestructorName: {
1888 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001889 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001890 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001891 if (ObjectType) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001892 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1893 SS, TemplateKWLoc, TemplateName,
1894 ObjectType, EnteringContext,
1895 Template);
Douglas Gregord6ab2322010-06-16 23:00:59 +00001896 if (TNK == TNK_Non_template)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001897 return true;
1898 } else {
Abramo Bagnara7c153532010-08-06 12:11:11 +00001899 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1900 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001901 EnteringContext, Template,
1902 MemberOfUnknownSpecialization);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001903
John McCallb3d87482010-08-24 05:47:05 +00001904 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001905 Diag(NameLoc, diag::err_destructor_template_id)
1906 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001907 return true;
1908 }
1909 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001910 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001911 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001912
1913 default:
1914 return false;
1915 }
1916
1917 if (TNK == TNK_Non_template)
1918 return false;
1919
1920 // Parse the enclosed template argument list.
1921 SourceLocation LAngleLoc, RAngleLoc;
1922 TemplateArgList TemplateArgs;
Douglas Gregor0278e122010-05-05 05:58:24 +00001923 if (Tok.is(tok::less) &&
1924 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor059101f2011-03-02 00:47:37 +00001925 SS, true, LAngleLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001926 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001927 RAngleLoc))
1928 return true;
1929
1930 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001931 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1932 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001933 // Form a parsed representation of the template-id to be stored in the
1934 // UnqualifiedId.
1935 TemplateIdAnnotation *TemplateId
Benjamin Kramer13bb7012012-04-14 12:14:03 +00001936 = TemplateIdAnnotation::Allocate(TemplateArgs.size(), TemplateIds);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001937
1938 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1939 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001940 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001941 TemplateId->TemplateNameLoc = Id.StartLocation;
1942 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001943 TemplateId->Name = 0;
1944 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1945 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001946 }
1947
Douglas Gregor059101f2011-03-02 00:47:37 +00001948 TemplateId->SS = SS;
Benjamin Kramer2b28bf12012-02-19 23:37:39 +00001949 TemplateId->TemplateKWLoc = TemplateKWLoc;
John McCall2b5289b2010-08-23 07:28:44 +00001950 TemplateId->Template = Template;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001951 TemplateId->Kind = TNK;
1952 TemplateId->LAngleLoc = LAngleLoc;
1953 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001954 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001955 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001956 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001957 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001958
1959 Id.setTemplateId(TemplateId);
1960 return false;
1961 }
1962
1963 // Bundle the template arguments together.
Benjamin Kramer5354e772012-08-23 23:38:35 +00001964 ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs);
Abramo Bagnarafad03b72012-01-27 08:46:19 +00001965
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001966 // Constructor and destructor names.
John McCallf312b1e2010-08-26 23:41:50 +00001967 TypeResult Type
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001968 = Actions.ActOnTemplateIdType(SS, TemplateKWLoc,
1969 Template, NameLoc,
Abramo Bagnarafad03b72012-01-27 08:46:19 +00001970 LAngleLoc, TemplateArgsPtr, RAngleLoc,
1971 /*IsCtorOrDtorName=*/true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001972 if (Type.isInvalid())
1973 return true;
1974
1975 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1976 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1977 else
1978 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1979
1980 return false;
1981}
1982
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001983/// \brief Parse an operator-function-id or conversion-function-id as part
1984/// of a C++ unqualified-id.
1985///
1986/// This routine is responsible only for parsing the operator-function-id or
1987/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001988///
1989/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001990/// operator-function-id: [C++ 13.5]
1991/// 'operator' operator
1992///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001993/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001994/// new delete new[] delete[]
1995/// + - * / % ^ & | ~
1996/// ! = < > += -= *= /= %=
1997/// ^= &= |= << >> >>= <<= == !=
1998/// <= >= && || ++ -- , ->* ->
1999/// () []
2000///
2001/// conversion-function-id: [C++ 12.3.2]
2002/// operator conversion-type-id
2003///
2004/// conversion-type-id:
2005/// type-specifier-seq conversion-declarator[opt]
2006///
2007/// conversion-declarator:
2008/// ptr-operator conversion-declarator[opt]
2009/// \endcode
2010///
Dmitri Gribenko1ddbd892012-08-24 00:01:24 +00002011/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002012/// non-empty, then we are parsing the unqualified-id of a qualified-id.
2013///
2014/// \param EnteringContext whether we are entering the scope of the
2015/// nested-name-specifier.
2016///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002017/// \param ObjectType if this unqualified-id occurs within a member access
2018/// expression, the type of the base object whose member is being accessed.
2019///
2020/// \param Result on a successful parse, contains the parsed unqualified-id.
2021///
2022/// \returns true if parsing fails, false otherwise.
2023bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00002024 ParsedType ObjectType,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002025 UnqualifiedId &Result) {
2026 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
2027
2028 // Consume the 'operator' keyword.
2029 SourceLocation KeywordLoc = ConsumeToken();
2030
2031 // Determine what kind of operator name we have.
2032 unsigned SymbolIdx = 0;
2033 SourceLocation SymbolLocations[3];
2034 OverloadedOperatorKind Op = OO_None;
2035 switch (Tok.getKind()) {
2036 case tok::kw_new:
2037 case tok::kw_delete: {
2038 bool isNew = Tok.getKind() == tok::kw_new;
2039 // Consume the 'new' or 'delete'.
2040 SymbolLocations[SymbolIdx++] = ConsumeToken();
Richard Smith6ee326a2012-04-10 01:32:12 +00002041 // Check for array new/delete.
2042 if (Tok.is(tok::l_square) &&
Richard Smith80ad52f2013-01-02 11:42:31 +00002043 (!getLangOpts().CPlusPlus11 || NextToken().isNot(tok::l_square))) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002044 // Consume the '[' and ']'.
2045 BalancedDelimiterTracker T(*this, tok::l_square);
2046 T.consumeOpen();
2047 T.consumeClose();
2048 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002049 return true;
2050
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002051 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
2052 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002053 Op = isNew? OO_Array_New : OO_Array_Delete;
2054 } else {
2055 Op = isNew? OO_New : OO_Delete;
2056 }
2057 break;
2058 }
2059
2060#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
2061 case tok::Token: \
2062 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
2063 Op = OO_##Name; \
2064 break;
2065#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
2066#include "clang/Basic/OperatorKinds.def"
2067
2068 case tok::l_paren: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002069 // Consume the '(' and ')'.
2070 BalancedDelimiterTracker T(*this, tok::l_paren);
2071 T.consumeOpen();
2072 T.consumeClose();
2073 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002074 return true;
2075
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002076 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
2077 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002078 Op = OO_Call;
2079 break;
2080 }
2081
2082 case tok::l_square: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002083 // Consume the '[' and ']'.
2084 BalancedDelimiterTracker T(*this, tok::l_square);
2085 T.consumeOpen();
2086 T.consumeClose();
2087 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002088 return true;
2089
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002090 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
2091 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002092 Op = OO_Subscript;
2093 break;
2094 }
2095
2096 case tok::code_completion: {
2097 // Code completion for the operator name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002098 Actions.CodeCompleteOperatorName(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002099 cutOffParsing();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002100 // Don't try to parse any further.
2101 return true;
2102 }
2103
2104 default:
2105 break;
2106 }
2107
2108 if (Op != OO_None) {
2109 // We have parsed an operator-function-id.
2110 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
2111 return false;
2112 }
Sean Hunt0486d742009-11-28 04:44:28 +00002113
2114 // Parse a literal-operator-id.
2115 //
Richard Smithaa9a8ce2012-10-20 08:41:10 +00002116 // literal-operator-id: C++11 [over.literal]
2117 // operator string-literal identifier
2118 // operator user-defined-string-literal
Sean Hunt0486d742009-11-28 04:44:28 +00002119
Richard Smith80ad52f2013-01-02 11:42:31 +00002120 if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
Richard Smith7fe62082011-10-15 05:09:34 +00002121 Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator);
Sean Hunt0486d742009-11-28 04:44:28 +00002122
Richard Smith33762772012-03-08 23:06:02 +00002123 SourceLocation DiagLoc;
2124 unsigned DiagId = 0;
2125
2126 // We're past translation phase 6, so perform string literal concatenation
2127 // before checking for "".
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002128 SmallVector<Token, 4> Toks;
2129 SmallVector<SourceLocation, 4> TokLocs;
Richard Smith33762772012-03-08 23:06:02 +00002130 while (isTokenStringLiteral()) {
2131 if (!Tok.is(tok::string_literal) && !DiagId) {
Richard Smithaa9a8ce2012-10-20 08:41:10 +00002132 // C++11 [over.literal]p1:
2133 // The string-literal or user-defined-string-literal in a
2134 // literal-operator-id shall have no encoding-prefix [...].
Richard Smith33762772012-03-08 23:06:02 +00002135 DiagLoc = Tok.getLocation();
2136 DiagId = diag::err_literal_operator_string_prefix;
2137 }
2138 Toks.push_back(Tok);
2139 TokLocs.push_back(ConsumeStringToken());
2140 }
2141
2142 StringLiteralParser Literal(Toks.data(), Toks.size(), PP);
2143 if (Literal.hadError)
2144 return true;
2145
2146 // Grab the literal operator's suffix, which will be either the next token
2147 // or a ud-suffix from the string literal.
2148 IdentifierInfo *II = 0;
2149 SourceLocation SuffixLoc;
2150 if (!Literal.getUDSuffix().empty()) {
2151 II = &PP.getIdentifierTable().get(Literal.getUDSuffix());
2152 SuffixLoc =
2153 Lexer::AdvanceToTokenCharacter(TokLocs[Literal.getUDSuffixToken()],
2154 Literal.getUDSuffixOffset(),
David Blaikie4e4d0842012-03-11 07:00:24 +00002155 PP.getSourceManager(), getLangOpts());
Richard Smith33762772012-03-08 23:06:02 +00002156 } else if (Tok.is(tok::identifier)) {
2157 II = Tok.getIdentifierInfo();
2158 SuffixLoc = ConsumeToken();
2159 TokLocs.push_back(SuffixLoc);
2160 } else {
Sean Hunt0486d742009-11-28 04:44:28 +00002161 Diag(Tok.getLocation(), diag::err_expected_ident);
2162 return true;
2163 }
2164
Richard Smith33762772012-03-08 23:06:02 +00002165 // The string literal must be empty.
2166 if (!Literal.GetString().empty() || Literal.Pascal) {
Richard Smithaa9a8ce2012-10-20 08:41:10 +00002167 // C++11 [over.literal]p1:
2168 // The string-literal or user-defined-string-literal in a
2169 // literal-operator-id shall [...] contain no characters
2170 // other than the implicit terminating '\0'.
Richard Smith33762772012-03-08 23:06:02 +00002171 DiagLoc = TokLocs.front();
2172 DiagId = diag::err_literal_operator_string_not_empty;
2173 }
2174
2175 if (DiagId) {
2176 // This isn't a valid literal-operator-id, but we think we know
2177 // what the user meant. Tell them what they should have written.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002178 SmallString<32> Str;
Richard Smith33762772012-03-08 23:06:02 +00002179 Str += "\"\" ";
2180 Str += II->getName();
2181 Diag(DiagLoc, DiagId) << FixItHint::CreateReplacement(
2182 SourceRange(TokLocs.front(), TokLocs.back()), Str);
2183 }
2184
2185 Result.setLiteralOperatorId(II, KeywordLoc, SuffixLoc);
Sean Hunt3e518bd2009-11-29 07:34:05 +00002186 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00002187 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002188
2189 // Parse a conversion-function-id.
2190 //
2191 // conversion-function-id: [C++ 12.3.2]
2192 // operator conversion-type-id
2193 //
2194 // conversion-type-id:
2195 // type-specifier-seq conversion-declarator[opt]
2196 //
2197 // conversion-declarator:
2198 // ptr-operator conversion-declarator[opt]
2199
2200 // Parse the type-specifier-seq.
John McCall0b7e6782011-03-24 11:26:52 +00002201 DeclSpec DS(AttrFactory);
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00002202 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002203 return true;
2204
2205 // Parse the conversion-declarator, which is merely a sequence of
2206 // ptr-operators.
Richard Smith14f78f42013-05-04 01:26:46 +00002207 Declarator D(DS, Declarator::ConversionIdContext);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002208 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
2209
2210 // Finish up the type.
John McCallf312b1e2010-08-26 23:41:50 +00002211 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002212 if (Ty.isInvalid())
2213 return true;
2214
2215 // Note that this is a conversion-function-id.
2216 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
2217 D.getSourceRange().getEnd());
2218 return false;
2219}
2220
2221/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
2222/// name of an entity.
2223///
2224/// \code
2225/// unqualified-id: [C++ expr.prim.general]
2226/// identifier
2227/// operator-function-id
2228/// conversion-function-id
2229/// [C++0x] literal-operator-id [TODO]
2230/// ~ class-name
2231/// template-id
2232///
2233/// \endcode
2234///
Dmitri Gribenko1ddbd892012-08-24 00:01:24 +00002235/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002236/// non-empty, then we are parsing the unqualified-id of a qualified-id.
2237///
2238/// \param EnteringContext whether we are entering the scope of the
2239/// nested-name-specifier.
2240///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002241/// \param AllowDestructorName whether we allow parsing of a destructor name.
2242///
2243/// \param AllowConstructorName whether we allow parsing a constructor name.
2244///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00002245/// \param ObjectType if this unqualified-id occurs within a member access
2246/// expression, the type of the base object whose member is being accessed.
2247///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002248/// \param Result on a successful parse, contains the parsed unqualified-id.
2249///
2250/// \returns true if parsing fails, false otherwise.
2251bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
2252 bool AllowDestructorName,
2253 bool AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00002254 ParsedType ObjectType,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002255 SourceLocation& TemplateKWLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002256 UnqualifiedId &Result) {
Douglas Gregor0278e122010-05-05 05:58:24 +00002257
2258 // Handle 'A::template B'. This is for template-ids which have not
2259 // already been annotated by ParseOptionalCXXScopeSpecifier().
2260 bool TemplateSpecified = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00002261 if (getLangOpts().CPlusPlus && Tok.is(tok::kw_template) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00002262 (ObjectType || SS.isSet())) {
2263 TemplateSpecified = true;
2264 TemplateKWLoc = ConsumeToken();
2265 }
2266
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002267 // unqualified-id:
2268 // identifier
2269 // template-id (when it hasn't already been annotated)
2270 if (Tok.is(tok::identifier)) {
2271 // Consume the identifier.
2272 IdentifierInfo *Id = Tok.getIdentifierInfo();
2273 SourceLocation IdLoc = ConsumeToken();
2274
David Blaikie4e4d0842012-03-11 07:00:24 +00002275 if (!getLangOpts().CPlusPlus) {
Douglas Gregorb862b8f2010-01-11 23:29:10 +00002276 // If we're not in C++, only identifiers matter. Record the
2277 // identifier and return.
2278 Result.setIdentifier(Id, IdLoc);
2279 return false;
2280 }
2281
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002282 if (AllowConstructorName &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002283 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002284 // We have parsed a constructor name.
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002285 ParsedType Ty = Actions.getTypeName(*Id, IdLoc, getCurScope(),
2286 &SS, false, false,
2287 ParsedType(),
2288 /*IsCtorOrDtorName=*/true,
2289 /*NonTrivialTypeSourceInfo=*/true);
2290 Result.setConstructorName(Ty, IdLoc, IdLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002291 } else {
2292 // We have parsed an identifier.
2293 Result.setIdentifier(Id, IdLoc);
2294 }
2295
2296 // If the next token is a '<', we may have a template.
Douglas Gregor0278e122010-05-05 05:58:24 +00002297 if (TemplateSpecified || Tok.is(tok::less))
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002298 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc, Id, IdLoc,
2299 EnteringContext, ObjectType,
2300 Result, TemplateSpecified);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002301
2302 return false;
2303 }
2304
2305 // unqualified-id:
2306 // template-id (already parsed and annotated)
2307 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002308 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002309
2310 // If the template-name names the current class, then this is a constructor
2311 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002312 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002313 if (SS.isSet()) {
2314 // C++ [class.qual]p2 specifies that a qualified template-name
2315 // is taken as the constructor name where a constructor can be
2316 // declared. Thus, the template arguments are extraneous, so
2317 // complain about them and remove them entirely.
2318 Diag(TemplateId->TemplateNameLoc,
2319 diag::err_out_of_line_constructor_template_id)
2320 << TemplateId->Name
Douglas Gregor849b2432010-03-31 17:46:05 +00002321 << FixItHint::CreateRemoval(
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002322 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002323 ParsedType Ty = Actions.getTypeName(*TemplateId->Name,
2324 TemplateId->TemplateNameLoc,
2325 getCurScope(),
2326 &SS, false, false,
2327 ParsedType(),
2328 /*IsCtorOrDtorName=*/true,
2329 /*NontrivialTypeSourceInfo=*/true);
2330 Result.setConstructorName(Ty, TemplateId->TemplateNameLoc,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002331 TemplateId->RAngleLoc);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002332 ConsumeToken();
2333 return false;
2334 }
2335
2336 Result.setConstructorTemplateId(TemplateId);
2337 ConsumeToken();
2338 return false;
2339 }
2340
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002341 // We have already parsed a template-id; consume the annotation token as
2342 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002343 Result.setTemplateId(TemplateId);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002344 TemplateKWLoc = TemplateId->TemplateKWLoc;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002345 ConsumeToken();
2346 return false;
2347 }
2348
2349 // unqualified-id:
2350 // operator-function-id
2351 // conversion-function-id
2352 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002353 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002354 return true;
2355
Sean Hunte6252d12009-11-28 08:58:14 +00002356 // If we have an operator-function-id or a literal-operator-id and the next
2357 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002358 //
2359 // template-id:
2360 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00002361 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
2362 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00002363 (TemplateSpecified || Tok.is(tok::less)))
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002364 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2365 0, SourceLocation(),
2366 EnteringContext, ObjectType,
2367 Result, TemplateSpecified);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002368
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002369 return false;
2370 }
2371
David Blaikie4e4d0842012-03-11 07:00:24 +00002372 if (getLangOpts().CPlusPlus &&
Douglas Gregorb862b8f2010-01-11 23:29:10 +00002373 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002374 // C++ [expr.unary.op]p10:
2375 // There is an ambiguity in the unary-expression ~X(), where X is a
2376 // class-name. The ambiguity is resolved in favor of treating ~ as a
2377 // unary complement rather than treating ~X as referring to a destructor.
2378
2379 // Parse the '~'.
2380 SourceLocation TildeLoc = ConsumeToken();
David Blaikie53a75c02011-12-08 16:13:53 +00002381
2382 if (SS.isEmpty() && Tok.is(tok::kw_decltype)) {
2383 DeclSpec DS(AttrFactory);
2384 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
2385 if (ParsedType Type = Actions.getDestructorType(DS, ObjectType)) {
2386 Result.setDestructorName(TildeLoc, Type, EndLoc);
2387 return false;
2388 }
2389 return true;
2390 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002391
2392 // Parse the class-name.
2393 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00002394 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002395 return true;
2396 }
2397
2398 // Parse the class-name (or template-name in a simple-template-id).
2399 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
2400 SourceLocation ClassNameLoc = ConsumeToken();
2401
Douglas Gregor0278e122010-05-05 05:58:24 +00002402 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallb3d87482010-08-24 05:47:05 +00002403 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002404 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2405 ClassName, ClassNameLoc,
2406 EnteringContext, ObjectType,
2407 Result, TemplateSpecified);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002408 }
2409
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002410 // Note that this is a destructor name.
John McCallb3d87482010-08-24 05:47:05 +00002411 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
2412 ClassNameLoc, getCurScope(),
2413 SS, ObjectType,
2414 EnteringContext);
Douglas Gregor124b8782010-02-16 19:09:40 +00002415 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002416 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00002417
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002418 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002419 return false;
2420 }
2421
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002422 Diag(Tok, diag::err_expected_unqualified_id)
David Blaikie4e4d0842012-03-11 07:00:24 +00002423 << getLangOpts().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002424 return true;
2425}
2426
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002427/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
2428/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00002429///
Chris Lattner59232d32009-01-04 21:25:24 +00002430/// This method is called to parse the new expression after the optional :: has
2431/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
2432/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002433///
2434/// new-expression:
2435/// '::'[opt] 'new' new-placement[opt] new-type-id
2436/// new-initializer[opt]
2437/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2438/// new-initializer[opt]
2439///
2440/// new-placement:
2441/// '(' expression-list ')'
2442///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002443/// new-type-id:
2444/// type-specifier-seq new-declarator[opt]
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002445/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002446///
2447/// new-declarator:
2448/// ptr-operator new-declarator[opt]
2449/// direct-new-declarator
2450///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002451/// new-initializer:
2452/// '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002453/// [C++0x] braced-init-list
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002454///
John McCall60d7b3a2010-08-24 06:29:42 +00002455ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002456Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
2457 assert(Tok.is(tok::kw_new) && "expected 'new' token");
2458 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002459
2460 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
2461 // second form of new-expression. It can't be a new-type-id.
2462
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002463 ExprVector PlacementArgs;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002464 SourceLocation PlacementLParen, PlacementRParen;
2465
Douglas Gregor4bd40312010-07-13 15:54:32 +00002466 SourceRange TypeIdParens;
John McCall0b7e6782011-03-24 11:26:52 +00002467 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis0b8c98f2011-06-28 03:01:23 +00002468 Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002469 if (Tok.is(tok::l_paren)) {
2470 // If it turns out to be a placement, we change the type location.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002471 BalancedDelimiterTracker T(*this, tok::l_paren);
2472 T.consumeOpen();
2473 PlacementLParen = T.getOpenLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002474 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00002475 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002476 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002477 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002478
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002479 T.consumeClose();
2480 PlacementRParen = T.getCloseLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002481 if (PlacementRParen.isInvalid()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00002482 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002483 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002484 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002485
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002486 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002487 // Reset the placement locations. There was no placement.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002488 TypeIdParens = T.getRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002489 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002490 } else {
2491 // We still need the type.
2492 if (Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002493 BalancedDelimiterTracker T(*this, tok::l_paren);
2494 T.consumeOpen();
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002495 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002496 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002497 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002498 ParseDeclarator(DeclaratorInfo);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002499 T.consumeClose();
2500 TypeIdParens = T.getRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002501 } else {
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002502 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002503 if (ParseCXXTypeSpecifierSeq(DS))
2504 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002505 else {
2506 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002507 ParseDeclaratorInternal(DeclaratorInfo,
2508 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002509 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002510 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002511 }
2512 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002513 // A new-type-id is a simplified type-id, where essentially the
2514 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002515 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002516 if (ParseCXXTypeSpecifierSeq(DS))
2517 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002518 else {
2519 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002520 ParseDeclaratorInternal(DeclaratorInfo,
2521 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002522 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002523 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00002524 if (DeclaratorInfo.isInvalidType()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00002525 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002526 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002527 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002528
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002529 ExprResult Initializer;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002530
2531 if (Tok.is(tok::l_paren)) {
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002532 SourceLocation ConstructorLParen, ConstructorRParen;
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002533 ExprVector ConstructorArgs;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002534 BalancedDelimiterTracker T(*this, tok::l_paren);
2535 T.consumeOpen();
2536 ConstructorLParen = T.getOpenLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002537 if (Tok.isNot(tok::r_paren)) {
2538 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002539 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00002540 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002541 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002542 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002543 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002544 T.consumeClose();
2545 ConstructorRParen = T.getCloseLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002546 if (ConstructorRParen.isInvalid()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00002547 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002548 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002549 }
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002550 Initializer = Actions.ActOnParenListExpr(ConstructorLParen,
2551 ConstructorRParen,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002552 ConstructorArgs);
Richard Smith80ad52f2013-01-02 11:42:31 +00002553 } else if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus11) {
Richard Smith7fe62082011-10-15 05:09:34 +00002554 Diag(Tok.getLocation(),
2555 diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002556 Initializer = ParseBraceInitializer();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002557 }
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002558 if (Initializer.isInvalid())
2559 return Initializer;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002560
Sebastian Redlf53597f2009-03-15 17:47:39 +00002561 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002562 PlacementArgs, PlacementRParen,
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002563 TypeIdParens, DeclaratorInfo, Initializer.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002564}
2565
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002566/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
2567/// passed to ParseDeclaratorInternal.
2568///
2569/// direct-new-declarator:
2570/// '[' expression ']'
2571/// direct-new-declarator '[' constant-expression ']'
2572///
Chris Lattner59232d32009-01-04 21:25:24 +00002573void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002574 // Parse the array dimensions.
2575 bool first = true;
2576 while (Tok.is(tok::l_square)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00002577 // An array-size expression can't start with a lambda.
2578 if (CheckProhibitedCXX11Attribute())
2579 continue;
2580
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002581 BalancedDelimiterTracker T(*this, tok::l_square);
2582 T.consumeOpen();
2583
John McCall60d7b3a2010-08-24 06:29:42 +00002584 ExprResult Size(first ? ParseExpression()
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002585 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002586 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002587 // Recover
Alexey Bataev8fe24752013-11-18 08:17:37 +00002588 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002589 return;
2590 }
2591 first = false;
2592
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002593 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00002594
Bill Wendlingad017fa2012-12-20 19:22:21 +00002595 // Attributes here appertain to the array type. C++11 [expr.new]p5.
Richard Smith6ee326a2012-04-10 01:32:12 +00002596 ParsedAttributes Attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00002597 MaybeParseCXX11Attributes(Attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00002598
John McCall0b7e6782011-03-24 11:26:52 +00002599 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall7f040a92010-12-24 02:08:15 +00002600 /*static=*/false, /*star=*/false,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002601 Size.release(),
2602 T.getOpenLocation(),
2603 T.getCloseLocation()),
Richard Smith6ee326a2012-04-10 01:32:12 +00002604 Attrs, T.getCloseLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002605
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002606 if (T.getCloseLocation().isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002607 return;
2608 }
2609}
2610
2611/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
2612/// This ambiguity appears in the syntax of the C++ new operator.
2613///
2614/// new-expression:
2615/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2616/// new-initializer[opt]
2617///
2618/// new-placement:
2619/// '(' expression-list ')'
2620///
John McCallca0408f2010-08-23 06:44:23 +00002621bool Parser::ParseExpressionListOrTypeId(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002622 SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00002623 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002624 // The '(' was already consumed.
2625 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002626 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00002627 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002628 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00002629 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002630 }
2631
2632 // It's not a type, it has to be an expression list.
2633 // Discard the comma locations - ActOnCXXNew has enough parameters.
2634 CommaLocsTy CommaLocs;
2635 return ParseExpressionList(PlacementArgs, CommaLocs);
2636}
2637
2638/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
2639/// to free memory allocated by new.
2640///
Chris Lattner59232d32009-01-04 21:25:24 +00002641/// This method is called to parse the 'delete' expression after the optional
2642/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
2643/// and "Start" is its location. Otherwise, "Start" is the location of the
2644/// 'delete' token.
2645///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002646/// delete-expression:
2647/// '::'[opt] 'delete' cast-expression
2648/// '::'[opt] 'delete' '[' ']' cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00002649ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002650Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
2651 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
2652 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002653
2654 // Array delete?
2655 bool ArrayDelete = false;
Richard Smith6ee326a2012-04-10 01:32:12 +00002656 if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
Richard Smith950435c2012-08-09 19:01:51 +00002657 // C++11 [expr.delete]p1:
2658 // Whenever the delete keyword is followed by empty square brackets, it
2659 // shall be interpreted as [array delete].
2660 // [Footnote: A lambda expression with a lambda-introducer that consists
2661 // of empty square brackets can follow the delete keyword if
2662 // the lambda expression is enclosed in parentheses.]
2663 // FIXME: Produce a better diagnostic if the '[]' is unambiguously a
2664 // lambda-introducer.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002665 ArrayDelete = true;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002666 BalancedDelimiterTracker T(*this, tok::l_square);
2667
2668 T.consumeOpen();
2669 T.consumeClose();
2670 if (T.getCloseLocation().isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00002671 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002672 }
2673
John McCall60d7b3a2010-08-24 06:29:42 +00002674 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002675 if (Operand.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002676 return Operand;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002677
John McCall9ae2f072010-08-23 23:25:46 +00002678 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002679}
Sebastian Redl64b45f72009-01-05 20:52:13 +00002680
Mike Stump1eb44332009-09-09 15:08:12 +00002681static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002682 switch(kind) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002683 default: llvm_unreachable("Not a known unary type trait.");
Sebastian Redl64b45f72009-01-05 20:52:13 +00002684 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
Joao Matos9ef98752013-03-27 01:34:16 +00002685 case tok::kw___has_nothrow_move_assign: return UTT_HasNothrowMoveAssign;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002686 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00002687 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002688 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
Joao Matos9ef98752013-03-27 01:34:16 +00002689 case tok::kw___has_trivial_move_assign: return UTT_HasTrivialMoveAssign;
Sean Hunt023df372011-05-09 18:22:59 +00002690 case tok::kw___has_trivial_constructor:
2691 return UTT_HasTrivialDefaultConstructor;
Joao Matos9ef98752013-03-27 01:34:16 +00002692 case tok::kw___has_trivial_move_constructor:
2693 return UTT_HasTrivialMoveConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00002694 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002695 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
2696 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
2697 case tok::kw___is_abstract: return UTT_IsAbstract;
John Wiegley20c0da72011-04-27 23:09:49 +00002698 case tok::kw___is_arithmetic: return UTT_IsArithmetic;
2699 case tok::kw___is_array: return UTT_IsArray;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002700 case tok::kw___is_class: return UTT_IsClass;
John Wiegley20c0da72011-04-27 23:09:49 +00002701 case tok::kw___is_complete_type: return UTT_IsCompleteType;
2702 case tok::kw___is_compound: return UTT_IsCompound;
2703 case tok::kw___is_const: return UTT_IsConst;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002704 case tok::kw___is_empty: return UTT_IsEmpty;
2705 case tok::kw___is_enum: return UTT_IsEnum;
Douglas Gregor5e9392b2011-12-03 18:14:24 +00002706 case tok::kw___is_final: return UTT_IsFinal;
John Wiegley20c0da72011-04-27 23:09:49 +00002707 case tok::kw___is_floating_point: return UTT_IsFloatingPoint;
2708 case tok::kw___is_function: return UTT_IsFunction;
2709 case tok::kw___is_fundamental: return UTT_IsFundamental;
2710 case tok::kw___is_integral: return UTT_IsIntegral;
John McCallea30e2f2012-09-25 07:32:49 +00002711 case tok::kw___is_interface_class: return UTT_IsInterfaceClass;
John Wiegley20c0da72011-04-27 23:09:49 +00002712 case tok::kw___is_lvalue_reference: return UTT_IsLvalueReference;
2713 case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
2714 case tok::kw___is_member_object_pointer: return UTT_IsMemberObjectPointer;
2715 case tok::kw___is_member_pointer: return UTT_IsMemberPointer;
2716 case tok::kw___is_object: return UTT_IsObject;
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00002717 case tok::kw___is_literal: return UTT_IsLiteral;
Chandler Carruth38402812011-04-24 02:49:28 +00002718 case tok::kw___is_literal_type: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002719 case tok::kw___is_pod: return UTT_IsPOD;
John Wiegley20c0da72011-04-27 23:09:49 +00002720 case tok::kw___is_pointer: return UTT_IsPointer;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002721 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
John Wiegley20c0da72011-04-27 23:09:49 +00002722 case tok::kw___is_reference: return UTT_IsReference;
John Wiegley20c0da72011-04-27 23:09:49 +00002723 case tok::kw___is_rvalue_reference: return UTT_IsRvalueReference;
2724 case tok::kw___is_scalar: return UTT_IsScalar;
David Majnemer7121bdb2013-10-18 00:33:31 +00002725 case tok::kw___is_sealed: return UTT_IsSealed;
John Wiegley20c0da72011-04-27 23:09:49 +00002726 case tok::kw___is_signed: return UTT_IsSigned;
2727 case tok::kw___is_standard_layout: return UTT_IsStandardLayout;
2728 case tok::kw___is_trivial: return UTT_IsTrivial;
Sean Huntfeb375d2011-05-13 00:31:07 +00002729 case tok::kw___is_trivially_copyable: return UTT_IsTriviallyCopyable;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002730 case tok::kw___is_union: return UTT_IsUnion;
John Wiegley20c0da72011-04-27 23:09:49 +00002731 case tok::kw___is_unsigned: return UTT_IsUnsigned;
2732 case tok::kw___is_void: return UTT_IsVoid;
2733 case tok::kw___is_volatile: return UTT_IsVolatile;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002734 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00002735}
2736
2737static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
2738 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00002739 default: llvm_unreachable("Not a known binary type trait");
Francois Pichetf1872372010-12-08 22:35:30 +00002740 case tok::kw___is_base_of: return BTT_IsBaseOf;
John Wiegley20c0da72011-04-27 23:09:49 +00002741 case tok::kw___is_convertible: return BTT_IsConvertible;
2742 case tok::kw___is_same: return BTT_IsSame;
Francois Pichetf1872372010-12-08 22:35:30 +00002743 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor9f361132011-01-27 20:28:01 +00002744 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002745 case tok::kw___is_trivially_assignable: return BTT_IsTriviallyAssignable;
Francois Pichet6ad6f282010-12-07 00:08:36 +00002746 }
Sebastian Redl64b45f72009-01-05 20:52:13 +00002747}
2748
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002749static TypeTrait TypeTraitFromTokKind(tok::TokenKind kind) {
2750 switch (kind) {
2751 default: llvm_unreachable("Not a known type trait");
2752 case tok::kw___is_trivially_constructible:
2753 return TT_IsTriviallyConstructible;
2754 }
2755}
2756
John Wiegley21ff2e52011-04-28 00:16:57 +00002757static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
2758 switch(kind) {
2759 default: llvm_unreachable("Not a known binary type trait");
2760 case tok::kw___array_rank: return ATT_ArrayRank;
2761 case tok::kw___array_extent: return ATT_ArrayExtent;
2762 }
2763}
2764
John Wiegley55262202011-04-25 06:54:41 +00002765static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2766 switch(kind) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002767 default: llvm_unreachable("Not a known unary expression trait.");
John Wiegley55262202011-04-25 06:54:41 +00002768 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
2769 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
2770 }
2771}
2772
Sebastian Redl64b45f72009-01-05 20:52:13 +00002773/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
2774/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2775/// templates.
2776///
2777/// primary-expression:
2778/// [GNU] unary-type-trait '(' type-id ')'
2779///
John McCall60d7b3a2010-08-24 06:29:42 +00002780ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002781 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
2782 SourceLocation Loc = ConsumeToken();
2783
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002784 BalancedDelimiterTracker T(*this, tok::l_paren);
2785 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redl64b45f72009-01-05 20:52:13 +00002786 return ExprError();
2787
2788 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
2789 // there will be cryptic errors about mismatched parentheses and missing
2790 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00002791 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002792
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002793 T.consumeClose();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002794
Douglas Gregor809070a2009-02-18 17:45:20 +00002795 if (Ty.isInvalid())
2796 return ExprError();
2797
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002798 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), T.getCloseLocation());
Sebastian Redl64b45f72009-01-05 20:52:13 +00002799}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002800
Francois Pichet6ad6f282010-12-07 00:08:36 +00002801/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
2802/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2803/// templates.
2804///
2805/// primary-expression:
2806/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
2807///
2808ExprResult Parser::ParseBinaryTypeTrait() {
2809 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
2810 SourceLocation Loc = ConsumeToken();
2811
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002812 BalancedDelimiterTracker T(*this, tok::l_paren);
2813 if (T.expectAndConsume(diag::err_expected_lparen))
Francois Pichet6ad6f282010-12-07 00:08:36 +00002814 return ExprError();
2815
2816 TypeResult LhsTy = ParseTypeName();
2817 if (LhsTy.isInvalid()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00002818 SkipUntil(tok::r_paren, StopAtSemi);
Francois Pichet6ad6f282010-12-07 00:08:36 +00002819 return ExprError();
2820 }
2821
2822 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00002823 SkipUntil(tok::r_paren, StopAtSemi);
Francois Pichet6ad6f282010-12-07 00:08:36 +00002824 return ExprError();
2825 }
2826
2827 TypeResult RhsTy = ParseTypeName();
2828 if (RhsTy.isInvalid()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00002829 SkipUntil(tok::r_paren, StopAtSemi);
Francois Pichet6ad6f282010-12-07 00:08:36 +00002830 return ExprError();
2831 }
2832
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002833 T.consumeClose();
Francois Pichet6ad6f282010-12-07 00:08:36 +00002834
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002835 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(),
2836 T.getCloseLocation());
Francois Pichet6ad6f282010-12-07 00:08:36 +00002837}
2838
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002839/// \brief Parse the built-in type-trait pseudo-functions that allow
2840/// implementation of the TR1/C++11 type traits templates.
2841///
2842/// primary-expression:
2843/// type-trait '(' type-id-seq ')'
2844///
2845/// type-id-seq:
2846/// type-id ...[opt] type-id-seq[opt]
2847///
2848ExprResult Parser::ParseTypeTrait() {
2849 TypeTrait Kind = TypeTraitFromTokKind(Tok.getKind());
2850 SourceLocation Loc = ConsumeToken();
2851
2852 BalancedDelimiterTracker Parens(*this, tok::l_paren);
2853 if (Parens.expectAndConsume(diag::err_expected_lparen))
2854 return ExprError();
2855
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002856 SmallVector<ParsedType, 2> Args;
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002857 do {
2858 // Parse the next type.
2859 TypeResult Ty = ParseTypeName();
2860 if (Ty.isInvalid()) {
2861 Parens.skipToEnd();
2862 return ExprError();
2863 }
2864
2865 // Parse the ellipsis, if present.
2866 if (Tok.is(tok::ellipsis)) {
2867 Ty = Actions.ActOnPackExpansion(Ty.get(), ConsumeToken());
2868 if (Ty.isInvalid()) {
2869 Parens.skipToEnd();
2870 return ExprError();
2871 }
2872 }
2873
2874 // Add this type to the list of arguments.
2875 Args.push_back(Ty.get());
2876
2877 if (Tok.is(tok::comma)) {
2878 ConsumeToken();
2879 continue;
2880 }
2881
2882 break;
2883 } while (true);
2884
2885 if (Parens.consumeClose())
2886 return ExprError();
2887
2888 return Actions.ActOnTypeTrait(Kind, Loc, Args, Parens.getCloseLocation());
2889}
2890
John Wiegley21ff2e52011-04-28 00:16:57 +00002891/// ParseArrayTypeTrait - Parse the built-in array type-trait
2892/// pseudo-functions.
2893///
2894/// primary-expression:
2895/// [Embarcadero] '__array_rank' '(' type-id ')'
2896/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
2897///
2898ExprResult Parser::ParseArrayTypeTrait() {
2899 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
2900 SourceLocation Loc = ConsumeToken();
2901
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002902 BalancedDelimiterTracker T(*this, tok::l_paren);
2903 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley21ff2e52011-04-28 00:16:57 +00002904 return ExprError();
2905
2906 TypeResult Ty = ParseTypeName();
2907 if (Ty.isInvalid()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00002908 SkipUntil(tok::comma, StopAtSemi);
2909 SkipUntil(tok::r_paren, StopAtSemi);
John Wiegley21ff2e52011-04-28 00:16:57 +00002910 return ExprError();
2911 }
2912
2913 switch (ATT) {
2914 case ATT_ArrayRank: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002915 T.consumeClose();
2916 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL,
2917 T.getCloseLocation());
John Wiegley21ff2e52011-04-28 00:16:57 +00002918 }
2919 case ATT_ArrayExtent: {
2920 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00002921 SkipUntil(tok::r_paren, StopAtSemi);
John Wiegley21ff2e52011-04-28 00:16:57 +00002922 return ExprError();
2923 }
2924
2925 ExprResult DimExpr = ParseExpression();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002926 T.consumeClose();
John Wiegley21ff2e52011-04-28 00:16:57 +00002927
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002928 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(),
2929 T.getCloseLocation());
John Wiegley21ff2e52011-04-28 00:16:57 +00002930 }
John Wiegley21ff2e52011-04-28 00:16:57 +00002931 }
David Blaikie30263482012-01-20 21:50:17 +00002932 llvm_unreachable("Invalid ArrayTypeTrait!");
John Wiegley21ff2e52011-04-28 00:16:57 +00002933}
2934
John Wiegley55262202011-04-25 06:54:41 +00002935/// ParseExpressionTrait - Parse built-in expression-trait
2936/// pseudo-functions like __is_lvalue_expr( xxx ).
2937///
2938/// primary-expression:
2939/// [Embarcadero] expression-trait '(' expression ')'
2940///
2941ExprResult Parser::ParseExpressionTrait() {
2942 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2943 SourceLocation Loc = ConsumeToken();
2944
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002945 BalancedDelimiterTracker T(*this, tok::l_paren);
2946 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley55262202011-04-25 06:54:41 +00002947 return ExprError();
2948
2949 ExprResult Expr = ParseExpression();
2950
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002951 T.consumeClose();
John Wiegley55262202011-04-25 06:54:41 +00002952
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002953 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(),
2954 T.getCloseLocation());
John Wiegley55262202011-04-25 06:54:41 +00002955}
2956
2957
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002958/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2959/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2960/// based on the context past the parens.
John McCall60d7b3a2010-08-24 06:29:42 +00002961ExprResult
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002962Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallb3d87482010-08-24 05:47:05 +00002963 ParsedType &CastTy,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002964 BalancedDelimiterTracker &Tracker) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002965 assert(getLangOpts().CPlusPlus && "Should only be called for C++!");
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002966 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2967 assert(isTypeIdInParens() && "Not a type-id!");
2968
John McCall60d7b3a2010-08-24 06:29:42 +00002969 ExprResult Result(true);
John McCallb3d87482010-08-24 05:47:05 +00002970 CastTy = ParsedType();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002971
2972 // We need to disambiguate a very ugly part of the C++ syntax:
2973 //
2974 // (T())x; - type-id
2975 // (T())*x; - type-id
2976 // (T())/x; - expression
2977 // (T()); - expression
2978 //
2979 // The bad news is that we cannot use the specialized tentative parser, since
2980 // it can only verify that the thing inside the parens can be parsed as
2981 // type-id, it is not useful for determining the context past the parens.
2982 //
2983 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00002984 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002985 //
2986 // It uses a scheme similar to parsing inline methods. The parenthesized
2987 // tokens are cached, the context that follows is determined (possibly by
2988 // parsing a cast-expression), and then we re-introduce the cached tokens
2989 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002990
Mike Stump1eb44332009-09-09 15:08:12 +00002991 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002992 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002993
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002994 // Store the tokens of the parentheses. We will parse them after we determine
2995 // the context that follows them.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00002996 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002997 // We didn't find the ')' we expected.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002998 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002999 return ExprError();
3000 }
3001
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00003002 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00003003 ParseAs = CompoundLiteral;
3004 } else {
3005 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00003006 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
3007 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
3008 NotCastExpr = true;
3009 } else {
3010 // Try parsing the cast-expression that may follow.
3011 // If it is not a cast-expression, NotCastExpr will be true and no token
3012 // will be consumed.
3013 Result = ParseCastExpression(false/*isUnaryExpression*/,
3014 false/*isAddressofOperand*/,
John McCallb3d87482010-08-24 05:47:05 +00003015 NotCastExpr,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00003016 // type-id has priority.
Kaelyn Uhraincd78e612012-01-25 20:49:08 +00003017 IsTypeCast);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00003018 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00003019
3020 // If we parsed a cast-expression, it's really a type-id, otherwise it's
3021 // an expression.
3022 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00003023 }
3024
Mike Stump1eb44332009-09-09 15:08:12 +00003025 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00003026 Toks.push_back(Tok);
3027 // Re-enter the stored parenthesized tokens into the token stream, so we may
3028 // parse them now.
3029 PP.EnterTokenStream(Toks.data(), Toks.size(),
3030 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
3031 // Drop the current token and bring the first cached one. It's the same token
3032 // as when we entered this function.
3033 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00003034
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00003035 if (ParseAs >= CompoundLiteral) {
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00003036 // Parse the type declarator.
3037 DeclSpec DS(AttrFactory);
3038 ParseSpecifierQualifierList(DS);
3039 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
3040 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00003041
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00003042 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003043 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00003044
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00003045 if (ParseAs == CompoundLiteral) {
3046 ExprType = CompoundLiteral;
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00003047 TypeResult Ty = ParseTypeName();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003048 return ParseCompoundLiteralExpression(Ty.get(),
3049 Tracker.getOpenLocation(),
3050 Tracker.getCloseLocation());
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00003051 }
Mike Stump1eb44332009-09-09 15:08:12 +00003052
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00003053 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
3054 assert(ParseAs == CastExpr);
3055
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00003056 if (DeclaratorInfo.isInvalidType())
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00003057 return ExprError();
3058
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00003059 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00003060 if (!Result.isInvalid())
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003061 Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(),
3062 DeclaratorInfo, CastTy,
3063 Tracker.getCloseLocation(), Result.take());
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00003064 return Result;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00003065 }
Mike Stump1eb44332009-09-09 15:08:12 +00003066
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00003067 // Not a compound literal, and not followed by a cast-expression.
3068 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00003069
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00003070 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00003071 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00003072 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003073 Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(),
3074 Tok.getLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00003075
3076 // Match the ')'.
3077 if (Result.isInvalid()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00003078 SkipUntil(tok::r_paren, StopAtSemi);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00003079 return ExprError();
3080 }
Mike Stump1eb44332009-09-09 15:08:12 +00003081
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003082 Tracker.consumeClose();
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00003083 return Result;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00003084}