blob: ab8be570c1a80a86410172161b4207b875accd32 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation for C++.
11//
12//===----------------------------------------------------------------------===//
13
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
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
Richard Smithea698b32011-04-14 21:45:45 +000026static int SelectDigraphErrorMessage(tok::TokenKind Kind) {
27 switch (Kind) {
28 case tok::kw_template: return 0;
29 case tok::kw_const_cast: return 1;
30 case tok::kw_dynamic_cast: return 2;
31 case tok::kw_reinterpret_cast: return 3;
32 case tok::kw_static_cast: return 4;
33 default:
David Blaikieb219cfc2011-09-23 05:06:16 +000034 llvm_unreachable("Unknown type for digraph error message.");
Richard Smithea698b32011-04-14 21:45:45 +000035 }
36}
37
38// Are the two tokens adjacent in the same source file?
Richard Smith19a27022012-06-18 06:11:04 +000039bool Parser::areTokensAdjacent(const Token &First, const Token &Second) {
Richard Smithea698b32011-04-14 21:45:45 +000040 SourceManager &SM = PP.getSourceManager();
41 SourceLocation FirstLoc = SM.getSpellingLoc(First.getLocation());
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +000042 SourceLocation FirstEnd = FirstLoc.getLocWithOffset(First.getLength());
Richard Smithea698b32011-04-14 21:45:45 +000043 return FirstEnd == SM.getSpellingLoc(Second.getLocation());
44}
45
46// Suggest fixit for "<::" after a cast.
47static void FixDigraph(Parser &P, Preprocessor &PP, Token &DigraphToken,
48 Token &ColonToken, tok::TokenKind Kind, bool AtDigraph) {
49 // Pull '<:' and ':' off token stream.
50 if (!AtDigraph)
51 PP.Lex(DigraphToken);
52 PP.Lex(ColonToken);
53
54 SourceRange Range;
55 Range.setBegin(DigraphToken.getLocation());
56 Range.setEnd(ColonToken.getLocation());
57 P.Diag(DigraphToken.getLocation(), diag::err_missing_whitespace_digraph)
58 << SelectDigraphErrorMessage(Kind)
59 << FixItHint::CreateReplacement(Range, "< ::");
60
61 // Update token information to reflect their change in token type.
62 ColonToken.setKind(tok::coloncolon);
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +000063 ColonToken.setLocation(ColonToken.getLocation().getLocWithOffset(-1));
Richard Smithea698b32011-04-14 21:45:45 +000064 ColonToken.setLength(2);
65 DigraphToken.setKind(tok::less);
66 DigraphToken.setLength(1);
67
68 // Push new tokens back to token stream.
69 PP.EnterToken(ColonToken);
70 if (!AtDigraph)
71 PP.EnterToken(DigraphToken);
72}
73
Richard Trieu950be712011-09-19 19:01:00 +000074// Check for '<::' which should be '< ::' instead of '[:' when following
75// a template name.
76void Parser::CheckForTemplateAndDigraph(Token &Next, ParsedType ObjectType,
77 bool EnteringContext,
78 IdentifierInfo &II, CXXScopeSpec &SS) {
Richard Trieuc11030e2011-09-20 20:03:50 +000079 if (!Next.is(tok::l_square) || Next.getLength() != 2)
Richard Trieu950be712011-09-19 19:01:00 +000080 return;
81
82 Token SecondToken = GetLookAheadToken(2);
Richard Smith19a27022012-06-18 06:11:04 +000083 if (!SecondToken.is(tok::colon) || !areTokensAdjacent(Next, SecondToken))
Richard Trieu950be712011-09-19 19:01:00 +000084 return;
85
86 TemplateTy Template;
87 UnqualifiedId TemplateName;
88 TemplateName.setIdentifier(&II, Tok.getLocation());
89 bool MemberOfUnknownSpecialization;
90 if (!Actions.isTemplateName(getCurScope(), SS, /*hasTemplateKeyword=*/false,
91 TemplateName, ObjectType, EnteringContext,
92 Template, MemberOfUnknownSpecialization))
93 return;
94
95 FixDigraph(*this, PP, Next, SecondToken, tok::kw_template,
96 /*AtDigraph*/false);
97}
98
Richard Trieu919b9552012-11-02 01:08:58 +000099/// \brief Emits an error for a left parentheses after a double colon.
100///
101/// When a '(' is found after a '::', emit an error. Attempt to fix the token
Nico Weberbba91b82012-11-29 05:29:23 +0000102/// stream by removing the '(', and the matching ')' if found.
Richard Trieu919b9552012-11-02 01:08:58 +0000103void Parser::CheckForLParenAfterColonColon() {
104 if (!Tok.is(tok::l_paren))
105 return;
106
107 SourceLocation l_parenLoc = ConsumeParen(), r_parenLoc;
108 Token Tok1 = getCurToken();
109 if (!Tok1.is(tok::identifier) && !Tok1.is(tok::star))
110 return;
111
112 if (Tok1.is(tok::identifier)) {
113 Token Tok2 = GetLookAheadToken(1);
114 if (Tok2.is(tok::r_paren)) {
115 ConsumeToken();
116 PP.EnterToken(Tok1);
117 r_parenLoc = ConsumeParen();
118 }
119 } else if (Tok1.is(tok::star)) {
120 Token Tok2 = GetLookAheadToken(1);
121 if (Tok2.is(tok::identifier)) {
122 Token Tok3 = GetLookAheadToken(2);
123 if (Tok3.is(tok::r_paren)) {
124 ConsumeToken();
125 ConsumeToken();
126 PP.EnterToken(Tok2);
127 PP.EnterToken(Tok1);
128 r_parenLoc = ConsumeParen();
129 }
130 }
131 }
132
133 Diag(l_parenLoc, diag::err_paren_after_colon_colon)
134 << FixItHint::CreateRemoval(l_parenLoc)
135 << FixItHint::CreateRemoval(r_parenLoc);
136}
137
Mike Stump1eb44332009-09-09 15:08:12 +0000138/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000139///
140/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump1eb44332009-09-09 15:08:12 +0000141/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000142/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000143///
144/// '::'[opt] nested-name-specifier
145/// '::'
146///
147/// nested-name-specifier:
148/// type-name '::'
149/// namespace-name '::'
150/// nested-name-specifier identifier '::'
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000151/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000152///
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000153///
Mike Stump1eb44332009-09-09 15:08:12 +0000154/// \param SS the scope specifier that will be set to the parsed
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000155/// nested-name-specifier (or empty)
156///
Mike Stump1eb44332009-09-09 15:08:12 +0000157/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000158/// the "." or "->" of a member access expression, this parameter provides the
159/// type of the object whose members are being accessed.
160///
161/// \param EnteringContext whether we will be entering into the context of
162/// the nested-name-specifier after parsing it.
163///
Douglas Gregord4dca082010-02-24 18:44:31 +0000164/// \param MayBePseudoDestructor When non-NULL, points to a flag that
165/// indicates whether this nested-name-specifier may be part of a
166/// pseudo-destructor name. In this case, the flag will be set false
167/// if we don't actually end up parsing a destructor name. Moreorover,
168/// if we do end up determining that we are parsing a destructor name,
169/// the last component of the nested-name-specifier is not parsed as
170/// part of the scope specifier.
Richard Smith2db075b2013-03-26 01:15:19 +0000171///
172/// \param IsTypename If \c true, this nested-name-specifier is known to be
173/// part of a type name. This is used to improve error recovery.
174///
175/// \param LastII When non-NULL, points to an IdentifierInfo* that will be
176/// filled in with the leading identifier in the last component of the
177/// nested-name-specifier, if any.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000178///
John McCall9ba61662010-02-26 08:45:28 +0000179/// \returns true if there was an error parsing a scope specifier
Douglas Gregor495c35d2009-08-25 22:51:20 +0000180bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +0000181 ParsedType ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000182 bool EnteringContext,
Francois Pichet4147d302011-03-27 19:41:34 +0000183 bool *MayBePseudoDestructor,
Richard Smith2db075b2013-03-26 01:15:19 +0000184 bool IsTypename,
185 IdentifierInfo **LastII) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000186 assert(getLangOpts().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +0000187 "Call sites of this function should be guarded by checking for C++");
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000189 if (Tok.is(tok::annot_cxxscope)) {
Richard Smith2db075b2013-03-26 01:15:19 +0000190 assert(!LastII && "want last identifier but have already annotated scope");
Douglas Gregorc34348a2011-02-24 17:54:50 +0000191 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
192 Tok.getAnnotationRange(),
193 SS);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000194 ConsumeToken();
John McCall9ba61662010-02-26 08:45:28 +0000195 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000196 }
Chris Lattnere607e802009-01-04 21:14:15 +0000197
Richard Smith2db075b2013-03-26 01:15:19 +0000198 if (LastII)
199 *LastII = 0;
200
Douglas Gregor39a8de12009-02-25 19:37:18 +0000201 bool HasScopeSpecifier = false;
202
Chris Lattner5b454732009-01-05 03:55:46 +0000203 if (Tok.is(tok::coloncolon)) {
204 // ::new and ::delete aren't nested-name-specifiers.
205 tok::TokenKind NextKind = NextToken().getKind();
206 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
207 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Chris Lattner55a7cef2009-01-05 00:13:00 +0000209 // '::' - Global scope qualifier.
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000210 if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
211 return true;
Richard Trieu919b9552012-11-02 01:08:58 +0000212
213 CheckForLParenAfterColonColon();
214
Douglas Gregor39a8de12009-02-25 19:37:18 +0000215 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000216 }
217
Douglas Gregord4dca082010-02-24 18:44:31 +0000218 bool CheckForDestructor = false;
219 if (MayBePseudoDestructor && *MayBePseudoDestructor) {
220 CheckForDestructor = true;
221 *MayBePseudoDestructor = false;
222 }
223
David Blaikie42d6d0c2011-12-04 05:04:18 +0000224 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
225 DeclSpec DS(AttrFactory);
226 SourceLocation DeclLoc = Tok.getLocation();
227 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
228 if (Tok.isNot(tok::coloncolon)) {
229 AnnotateExistingDecltypeSpecifier(DS, DeclLoc, EndLoc);
230 return false;
231 }
232
233 SourceLocation CCLoc = ConsumeToken();
234 if (Actions.ActOnCXXNestedNameSpecifierDecltype(SS, DS, CCLoc))
235 SS.SetInvalid(SourceRange(DeclLoc, CCLoc));
236
237 HasScopeSpecifier = true;
238 }
239
Douglas Gregor39a8de12009-02-25 19:37:18 +0000240 while (true) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000241 if (HasScopeSpecifier) {
242 // C++ [basic.lookup.classref]p5:
243 // If the qualified-id has the form
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000244 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000245 // ::class-name-or-namespace-name::...
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000246 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000247 // the class-name-or-namespace-name is looked up in global scope as a
248 // class-name or namespace-name.
249 //
250 // To implement this, we clear out the object type as soon as we've
251 // seen a leading '::' or part of a nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000252 ObjectType = ParsedType();
Douglas Gregor81b747b2009-09-17 21:32:03 +0000253
254 if (Tok.is(tok::code_completion)) {
255 // Code completion for a nested-name-specifier, where the code
256 // code completion token follows the '::'.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000257 Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
Argyrios Kyrtzidisb6b2b182011-04-23 01:04:12 +0000258 // Include code completion token into the range of the scope otherwise
259 // when we try to annotate the scope tokens the dangling code completion
260 // token will cause assertion in
261 // Preprocessor::AnnotatePreviousCachedTokens.
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000262 SS.setEndLoc(Tok.getLocation());
263 cutOffParsing();
264 return true;
Douglas Gregor81b747b2009-09-17 21:32:03 +0000265 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000266 }
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Douglas Gregor39a8de12009-02-25 19:37:18 +0000268 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +0000269 // nested-name-specifier 'template'[opt] simple-template-id '::'
270
271 // Parse the optional 'template' keyword, then make sure we have
272 // 'identifier <' after it.
273 if (Tok.is(tok::kw_template)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000274 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +0000275 // nested-name-specifier, since they aren't allowed to start with
276 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000277 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +0000278 break;
279
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000280 TentativeParsingAction TPA(*this);
Chris Lattner77cf72a2009-06-26 03:47:46 +0000281 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000282
283 UnqualifiedId TemplateName;
284 if (Tok.is(tok::identifier)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000285 // Consume the identifier.
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000286 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000287 ConsumeToken();
288 } else if (Tok.is(tok::kw_operator)) {
289 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000290 TemplateName)) {
291 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000292 break;
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000293 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000294
Sean Hunte6252d12009-11-28 08:58:14 +0000295 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
296 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000297 Diag(TemplateName.getSourceRange().getBegin(),
298 diag::err_id_after_template_in_nested_name_spec)
299 << TemplateName.getSourceRange();
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000300 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000301 break;
302 }
303 } else {
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000304 TPA.Revert();
Chris Lattner77cf72a2009-06-26 03:47:46 +0000305 break;
306 }
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000308 // If the next token is not '<', we have a qualified-id that refers
309 // to a template name, such as T::template apply, but is not a
310 // template-id.
311 if (Tok.isNot(tok::less)) {
312 TPA.Revert();
313 break;
314 }
315
316 // Commit to parsing the template-id.
317 TPA.Commit();
Douglas Gregord6ab2322010-06-16 23:00:59 +0000318 TemplateTy Template;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000319 if (TemplateNameKind TNK
320 = Actions.ActOnDependentTemplateName(getCurScope(),
321 SS, TemplateKWLoc, TemplateName,
322 ObjectType, EnteringContext,
323 Template)) {
324 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateKWLoc,
325 TemplateName, false))
Douglas Gregord6ab2322010-06-16 23:00:59 +0000326 return true;
327 } else
John McCall9ba61662010-02-26 08:45:28 +0000328 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Chris Lattner77cf72a2009-06-26 03:47:46 +0000330 continue;
331 }
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Douglas Gregor39a8de12009-02-25 19:37:18 +0000333 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000334 // We have
Douglas Gregor39a8de12009-02-25 19:37:18 +0000335 //
336 // simple-template-id '::'
337 //
338 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000339 // right kind (it should name a type or be dependent), and then
340 // convert it into a type within the nested-name-specifier.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000341 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord4dca082010-02-24 18:44:31 +0000342 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
343 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000344 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000345 }
346
Richard Smith2db075b2013-03-26 01:15:19 +0000347 if (LastII)
348 *LastII = TemplateId->Name;
349
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000350 // Consume the template-id token.
351 ConsumeToken();
352
353 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
354 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000355
David Blaikie6796fc12011-11-07 03:30:03 +0000356 HasScopeSpecifier = true;
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000357
Benjamin Kramer5354e772012-08-23 23:38:35 +0000358 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000359 TemplateId->NumArgs);
360
361 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000362 SS,
363 TemplateId->TemplateKWLoc,
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000364 TemplateId->Template,
365 TemplateId->TemplateNameLoc,
366 TemplateId->LAngleLoc,
367 TemplateArgsPtr,
368 TemplateId->RAngleLoc,
369 CCLoc,
370 EnteringContext)) {
371 SourceLocation StartLoc
372 = SS.getBeginLoc().isValid()? SS.getBeginLoc()
373 : TemplateId->TemplateNameLoc;
374 SS.SetInvalid(SourceRange(StartLoc, CCLoc));
Chris Lattner67b9e832009-06-26 03:45:46 +0000375 }
Argyrios Kyrtzidiseccce7e2011-05-03 18:45:38 +0000376
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000377 continue;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000378 }
379
Chris Lattner5c7f7862009-06-26 03:52:38 +0000380
381 // The rest of the nested-name-specifier possibilities start with
382 // tok::identifier.
383 if (Tok.isNot(tok::identifier))
384 break;
385
386 IdentifierInfo &II = *Tok.getIdentifierInfo();
387
388 // nested-name-specifier:
389 // type-name '::'
390 // namespace-name '::'
391 // nested-name-specifier identifier '::'
392 Token Next = NextToken();
Chris Lattner46646492009-12-07 01:36:53 +0000393
394 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
395 // and emit a fixit hint for it.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000396 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000397 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
398 Tok.getLocation(),
399 Next.getLocation(), ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000400 EnteringContext) &&
401 // If the token after the colon isn't an identifier, it's still an
402 // error, but they probably meant something else strange so don't
403 // recover like this.
404 PP.LookAhead(1).is(tok::identifier)) {
405 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
Douglas Gregor849b2432010-03-31 17:46:05 +0000406 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregorb10cd042010-02-21 18:36:56 +0000407
408 // Recover as if the user wrote '::'.
409 Next.setKind(tok::coloncolon);
410 }
Chris Lattner46646492009-12-07 01:36:53 +0000411 }
412
Chris Lattner5c7f7862009-06-26 03:52:38 +0000413 if (Next.is(tok::coloncolon)) {
Douglas Gregor77549082010-02-24 21:29:12 +0000414 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000415 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor77549082010-02-24 21:29:12 +0000416 II, ObjectType)) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000417 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000418 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000419 }
420
Richard Smith2db075b2013-03-26 01:15:19 +0000421 if (LastII)
422 *LastII = &II;
423
Chris Lattner5c7f7862009-06-26 03:52:38 +0000424 // We have an identifier followed by a '::'. Lookup this name
425 // as the name in a nested-name-specifier.
426 SourceLocation IdLoc = ConsumeToken();
Chris Lattner46646492009-12-07 01:36:53 +0000427 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
428 "NextToken() not working properly!");
Chris Lattner5c7f7862009-06-26 03:52:38 +0000429 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Richard Trieu919b9552012-11-02 01:08:58 +0000431 CheckForLParenAfterColonColon();
432
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000433 HasScopeSpecifier = true;
434 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
435 ObjectType, EnteringContext, SS))
436 SS.SetInvalid(SourceRange(IdLoc, CCLoc));
437
Chris Lattner5c7f7862009-06-26 03:52:38 +0000438 continue;
439 }
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Richard Trieu950be712011-09-19 19:01:00 +0000441 CheckForTemplateAndDigraph(Next, ObjectType, EnteringContext, II, SS);
Richard Smithea698b32011-04-14 21:45:45 +0000442
Chris Lattner5c7f7862009-06-26 03:52:38 +0000443 // nested-name-specifier:
444 // type-name '<'
445 if (Next.is(tok::less)) {
446 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000447 UnqualifiedId TemplateName;
448 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000449 bool MemberOfUnknownSpecialization;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000450 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000451 /*hasTemplateKeyword=*/false,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000452 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000453 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000454 EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000455 Template,
456 MemberOfUnknownSpecialization)) {
David Blaikie6796fc12011-11-07 03:30:03 +0000457 // We have found a template name, so annotate this token
Chris Lattner5c7f7862009-06-26 03:52:38 +0000458 // with a template-id annotation. We do not permit the
459 // template-id to be translated into a type annotation,
460 // because some clients (e.g., the parsing of class template
461 // specializations) still want to see the original template-id
462 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000463 ConsumeToken();
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000464 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
465 TemplateName, false))
John McCall9ba61662010-02-26 08:45:28 +0000466 return true;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000467 continue;
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000468 }
469
470 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
Francois Pichet4147d302011-03-27 19:41:34 +0000471 (IsTypename || IsTemplateArgumentList(1))) {
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000472 // We have something like t::getAs<T>, where getAs is a
473 // member of an unknown specialization. However, this will only
474 // parse correctly as a template, so suggest the keyword 'template'
475 // before 'getAs' and treat this as a dependent template name.
Francois Pichet4147d302011-03-27 19:41:34 +0000476 unsigned DiagID = diag::err_missing_dependent_template_keyword;
David Blaikie4e4d0842012-03-11 07:00:24 +0000477 if (getLangOpts().MicrosoftExt)
Francois Pichetcf320c62011-04-22 08:25:24 +0000478 DiagID = diag::warn_missing_dependent_template_keyword;
Francois Pichet4147d302011-03-27 19:41:34 +0000479
480 Diag(Tok.getLocation(), DiagID)
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000481 << II.getName()
482 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
483
Douglas Gregord6ab2322010-06-16 23:00:59 +0000484 if (TemplateNameKind TNK
Douglas Gregor23c94db2010-07-02 17:43:08 +0000485 = Actions.ActOnDependentTemplateName(getCurScope(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000486 SS, SourceLocation(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000487 TemplateName, ObjectType,
488 EnteringContext, Template)) {
489 // Consume the identifier.
490 ConsumeToken();
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000491 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
492 TemplateName, false))
493 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000494 }
495 else
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000496 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000497
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000498 continue;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000499 }
500 }
501
Douglas Gregor39a8de12009-02-25 19:37:18 +0000502 // We don't have any tokens that form the beginning of a
503 // nested-name-specifier, so we're done.
504 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000505 }
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Douglas Gregord4dca082010-02-24 18:44:31 +0000507 // Even if we didn't see any pieces of a nested-name-specifier, we
508 // still check whether there is a tilde in this position, which
509 // indicates a potential pseudo-destructor.
510 if (CheckForDestructor && Tok.is(tok::tilde))
511 *MayBePseudoDestructor = true;
512
John McCall9ba61662010-02-26 08:45:28 +0000513 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000514}
515
516/// ParseCXXIdExpression - Handle id-expression.
517///
518/// id-expression:
519/// unqualified-id
520/// qualified-id
521///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000522/// qualified-id:
523/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
524/// '::' identifier
525/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000526/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000527///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000528/// NOTE: The standard specifies that, for qualified-id, the parser does not
529/// expect:
530///
531/// '::' conversion-function-id
532/// '::' '~' class-name
533///
534/// This may cause a slight inconsistency on diagnostics:
535///
536/// class C {};
537/// namespace A {}
538/// void f() {
539/// :: A :: ~ C(); // Some Sema error about using destructor with a
540/// // namespace.
541/// :: ~ C(); // Some Parser error like 'unexpected ~'.
542/// }
543///
544/// We simplify the parser a bit and make it work like:
545///
546/// qualified-id:
547/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
548/// '::' unqualified-id
549///
550/// That way Sema can handle and report similar errors for namespaces and the
551/// global scope.
552///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000553/// The isAddressOfOperand parameter indicates that this id-expression is a
554/// direct operand of the address-of operator. This is, besides member contexts,
555/// the only place where a qualified-id naming a non-static class member may
556/// appear.
557///
John McCall60d7b3a2010-08-24 06:29:42 +0000558ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000559 // qualified-id:
560 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
561 // '::' unqualified-id
562 //
563 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000564 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000565
566 SourceLocation TemplateKWLoc;
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000567 UnqualifiedId Name;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000568 if (ParseUnqualifiedId(SS,
569 /*EnteringContext=*/false,
570 /*AllowDestructorName=*/false,
571 /*AllowConstructorName=*/false,
John McCallb3d87482010-08-24 05:47:05 +0000572 /*ObjectType=*/ ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000573 TemplateKWLoc,
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000574 Name))
575 return ExprError();
John McCallb681b612009-11-22 02:49:43 +0000576
577 // This is only the direct operand of an & operator if it is not
578 // followed by a postfix-expression suffix.
John McCall9c72c602010-08-27 09:08:28 +0000579 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
580 isAddressOfOperand = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000581
582 return Actions.ActOnIdExpression(getCurScope(), SS, TemplateKWLoc, Name,
583 Tok.is(tok::l_paren), isAddressOfOperand);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000584}
585
Douglas Gregorae7902c2011-08-04 15:30:47 +0000586/// ParseLambdaExpression - Parse a C++0x lambda expression.
587///
588/// lambda-expression:
589/// lambda-introducer lambda-declarator[opt] compound-statement
590///
591/// lambda-introducer:
592/// '[' lambda-capture[opt] ']'
593///
594/// lambda-capture:
595/// capture-default
596/// capture-list
597/// capture-default ',' capture-list
598///
599/// capture-default:
600/// '&'
601/// '='
602///
603/// capture-list:
604/// capture
605/// capture-list ',' capture
606///
607/// capture:
608/// identifier
609/// '&' identifier
610/// 'this'
611///
612/// lambda-declarator:
613/// '(' parameter-declaration-clause ')' attribute-specifier[opt]
614/// 'mutable'[opt] exception-specification[opt]
615/// trailing-return-type[opt]
616///
617ExprResult Parser::ParseLambdaExpression() {
618 // Parse lambda-introducer.
619 LambdaIntroducer Intro;
620
David Blaikiedc84cd52013-02-20 22:23:23 +0000621 Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
Douglas Gregorae7902c2011-08-04 15:30:47 +0000622 if (DiagID) {
623 Diag(Tok, DiagID.getValue());
624 SkipUntil(tok::r_square);
Eli Friedmandc3b7232012-01-04 02:40:39 +0000625 SkipUntil(tok::l_brace);
626 SkipUntil(tok::r_brace);
627 return ExprError();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000628 }
629
630 return ParseLambdaExpressionAfterIntroducer(Intro);
631}
632
633/// TryParseLambdaExpression - Use lookahead and potentially tentative
634/// parsing to determine if we are looking at a C++0x lambda expression, and parse
635/// it if we are.
636///
637/// If we are not looking at a lambda expression, returns ExprError().
638ExprResult Parser::TryParseLambdaExpression() {
Richard Smith80ad52f2013-01-02 11:42:31 +0000639 assert(getLangOpts().CPlusPlus11
Douglas Gregorae7902c2011-08-04 15:30:47 +0000640 && Tok.is(tok::l_square)
641 && "Not at the start of a possible lambda expression.");
642
643 const Token Next = NextToken(), After = GetLookAheadToken(2);
644
645 // If lookahead indicates this is a lambda...
646 if (Next.is(tok::r_square) || // []
647 Next.is(tok::equal) || // [=
648 (Next.is(tok::amp) && // [&] or [&,
649 (After.is(tok::r_square) ||
650 After.is(tok::comma))) ||
651 (Next.is(tok::identifier) && // [identifier]
652 After.is(tok::r_square))) {
653 return ParseLambdaExpression();
654 }
655
Eli Friedmandc3b7232012-01-04 02:40:39 +0000656 // If lookahead indicates an ObjC message send...
657 // [identifier identifier
Douglas Gregorae7902c2011-08-04 15:30:47 +0000658 if (Next.is(tok::identifier) && After.is(tok::identifier)) {
Eli Friedmandc3b7232012-01-04 02:40:39 +0000659 return ExprEmpty();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000660 }
661
Eli Friedmandc3b7232012-01-04 02:40:39 +0000662 // Here, we're stuck: lambda introducers and Objective-C message sends are
663 // unambiguous, but it requires arbitrary lookhead. [a,b,c,d,e,f,g] is a
664 // lambda, and [a,b,c,d,e,f,g h] is a Objective-C message send. Instead of
665 // writing two routines to parse a lambda introducer, just try to parse
666 // a lambda introducer first, and fall back if that fails.
667 // (TryParseLambdaIntroducer never produces any diagnostic output.)
Douglas Gregorae7902c2011-08-04 15:30:47 +0000668 LambdaIntroducer Intro;
669 if (TryParseLambdaIntroducer(Intro))
Eli Friedmandc3b7232012-01-04 02:40:39 +0000670 return ExprEmpty();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000671 return ParseLambdaExpressionAfterIntroducer(Intro);
672}
673
674/// ParseLambdaExpression - Parse a lambda introducer.
675///
676/// Returns a DiagnosticID if it hit something unexpected.
David Blaikiedc84cd52013-02-20 22:23:23 +0000677Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro) {
678 typedef Optional<unsigned> DiagResult;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000679
680 assert(Tok.is(tok::l_square) && "Lambda expressions begin with '['.");
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000681 BalancedDelimiterTracker T(*this, tok::l_square);
682 T.consumeOpen();
683
684 Intro.Range.setBegin(T.getOpenLocation());
Douglas Gregorae7902c2011-08-04 15:30:47 +0000685
686 bool first = true;
687
688 // Parse capture-default.
689 if (Tok.is(tok::amp) &&
690 (NextToken().is(tok::comma) || NextToken().is(tok::r_square))) {
691 Intro.Default = LCD_ByRef;
Douglas Gregor3ac109c2012-02-10 17:46:20 +0000692 Intro.DefaultLoc = ConsumeToken();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000693 first = false;
694 } else if (Tok.is(tok::equal)) {
695 Intro.Default = LCD_ByCopy;
Douglas Gregor3ac109c2012-02-10 17:46:20 +0000696 Intro.DefaultLoc = ConsumeToken();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000697 first = false;
698 }
699
700 while (Tok.isNot(tok::r_square)) {
701 if (!first) {
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000702 if (Tok.isNot(tok::comma)) {
Douglas Gregor437fbc52012-07-31 00:50:07 +0000703 // Provide a completion for a lambda introducer here. Except
704 // in Objective-C, where this is Almost Surely meant to be a message
705 // send. In that case, fail here and let the ObjC message
706 // expression parser perform the completion.
Douglas Gregord48ab062012-07-31 15:27:48 +0000707 if (Tok.is(tok::code_completion) &&
708 !(getLangOpts().ObjC1 && Intro.Default == LCD_None &&
709 !Intro.Captures.empty())) {
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000710 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
711 /*AfterAmpersand=*/false);
712 ConsumeCodeCompletionToken();
713 break;
714 }
715
Douglas Gregorae7902c2011-08-04 15:30:47 +0000716 return DiagResult(diag::err_expected_comma_or_rsquare);
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000717 }
Douglas Gregorae7902c2011-08-04 15:30:47 +0000718 ConsumeToken();
719 }
720
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000721 if (Tok.is(tok::code_completion)) {
722 // If we're in Objective-C++ and we have a bare '[', then this is more
723 // likely to be a message receiver.
David Blaikie4e4d0842012-03-11 07:00:24 +0000724 if (getLangOpts().ObjC1 && first)
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000725 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
726 else
727 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
728 /*AfterAmpersand=*/false);
729 ConsumeCodeCompletionToken();
730 break;
731 }
Douglas Gregorae7902c2011-08-04 15:30:47 +0000732
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000733 first = false;
734
Douglas Gregorae7902c2011-08-04 15:30:47 +0000735 // Parse capture.
736 LambdaCaptureKind Kind = LCK_ByCopy;
737 SourceLocation Loc;
738 IdentifierInfo* Id = 0;
Douglas Gregora7365242012-02-14 19:27:52 +0000739 SourceLocation EllipsisLoc;
740
Douglas Gregorae7902c2011-08-04 15:30:47 +0000741 if (Tok.is(tok::kw_this)) {
742 Kind = LCK_This;
743 Loc = ConsumeToken();
744 } else {
745 if (Tok.is(tok::amp)) {
746 Kind = LCK_ByRef;
747 ConsumeToken();
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000748
749 if (Tok.is(tok::code_completion)) {
750 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
751 /*AfterAmpersand=*/true);
752 ConsumeCodeCompletionToken();
753 break;
754 }
Douglas Gregorae7902c2011-08-04 15:30:47 +0000755 }
756
757 if (Tok.is(tok::identifier)) {
758 Id = Tok.getIdentifierInfo();
759 Loc = ConsumeToken();
Douglas Gregora7365242012-02-14 19:27:52 +0000760
761 if (Tok.is(tok::ellipsis))
762 EllipsisLoc = ConsumeToken();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000763 } else if (Tok.is(tok::kw_this)) {
764 // FIXME: If we want to suggest a fixit here, will need to return more
765 // than just DiagnosticID. Perhaps full DiagnosticBuilder that can be
766 // Clear()ed to prevent emission in case of tentative parsing?
767 return DiagResult(diag::err_this_captured_by_reference);
768 } else {
769 return DiagResult(diag::err_expected_capture);
770 }
771 }
772
Douglas Gregora7365242012-02-14 19:27:52 +0000773 Intro.addCapture(Kind, Loc, Id, EllipsisLoc);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000774 }
775
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000776 T.consumeClose();
777 Intro.Range.setEnd(T.getCloseLocation());
Douglas Gregorae7902c2011-08-04 15:30:47 +0000778
779 return DiagResult();
780}
781
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000782/// TryParseLambdaIntroducer - Tentatively parse a lambda introducer.
Douglas Gregorae7902c2011-08-04 15:30:47 +0000783///
784/// Returns true if it hit something unexpected.
785bool Parser::TryParseLambdaIntroducer(LambdaIntroducer &Intro) {
786 TentativeParsingAction PA(*this);
787
David Blaikiedc84cd52013-02-20 22:23:23 +0000788 Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
Douglas Gregorae7902c2011-08-04 15:30:47 +0000789
790 if (DiagID) {
791 PA.Revert();
792 return true;
793 }
794
795 PA.Commit();
796 return false;
797}
798
799/// ParseLambdaExpressionAfterIntroducer - Parse the rest of a lambda
800/// expression.
801ExprResult Parser::ParseLambdaExpressionAfterIntroducer(
802 LambdaIntroducer &Intro) {
Eli Friedmandc3b7232012-01-04 02:40:39 +0000803 SourceLocation LambdaBeginLoc = Intro.Range.getBegin();
804 Diag(LambdaBeginLoc, diag::warn_cxx98_compat_lambda);
805
806 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), LambdaBeginLoc,
807 "lambda expression parsing");
808
Douglas Gregorae7902c2011-08-04 15:30:47 +0000809 // Parse lambda-declarator[opt].
810 DeclSpec DS(AttrFactory);
Eli Friedmanf88c4002012-01-04 04:41:38 +0000811 Declarator D(DS, Declarator::LambdaExprContext);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000812
813 if (Tok.is(tok::l_paren)) {
814 ParseScope PrototypeScope(this,
815 Scope::FunctionPrototypeScope |
Richard Smith3a2b7a12013-01-28 22:42:45 +0000816 Scope::FunctionDeclarationScope |
Douglas Gregorae7902c2011-08-04 15:30:47 +0000817 Scope::DeclScope);
818
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000819 SourceLocation DeclEndLoc;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000820 BalancedDelimiterTracker T(*this, tok::l_paren);
821 T.consumeOpen();
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000822 SourceLocation LParenLoc = T.getOpenLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000823
824 // Parse parameter-declaration-clause.
825 ParsedAttributes Attr(AttrFactory);
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000826 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000827 SourceLocation EllipsisLoc;
828
829 if (Tok.isNot(tok::r_paren))
830 ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc);
831
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000832 T.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000833 SourceLocation RParenLoc = T.getCloseLocation();
834 DeclEndLoc = RParenLoc;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000835
836 // Parse 'mutable'[opt].
837 SourceLocation MutableLoc;
838 if (Tok.is(tok::kw_mutable)) {
839 MutableLoc = ConsumeToken();
840 DeclEndLoc = MutableLoc;
841 }
842
843 // Parse exception-specification[opt].
844 ExceptionSpecificationType ESpecType = EST_None;
845 SourceRange ESpecRange;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000846 SmallVector<ParsedType, 2> DynamicExceptions;
847 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000848 ExprResult NoexceptExpr;
Richard Smitha058fd42012-05-02 22:22:32 +0000849 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +0000850 DynamicExceptions,
851 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +0000852 NoexceptExpr);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000853
854 if (ESpecType != EST_None)
855 DeclEndLoc = ESpecRange.getEnd();
856
857 // Parse attribute-specifier[opt].
Richard Smith4e24f0f2013-01-02 12:01:23 +0000858 MaybeParseCXX11Attributes(Attr, &DeclEndLoc);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000859
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000860 SourceLocation FunLocalRangeEnd = DeclEndLoc;
861
Douglas Gregorae7902c2011-08-04 15:30:47 +0000862 // Parse trailing-return-type[opt].
Richard Smith54655be2012-06-12 01:51:59 +0000863 TypeResult TrailingReturnType;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000864 if (Tok.is(tok::arrow)) {
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000865 FunLocalRangeEnd = Tok.getLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000866 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +0000867 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000868 if (Range.getEnd().isValid())
869 DeclEndLoc = Range.getEnd();
870 }
871
872 PrototypeScope.Exit();
873
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000874 SourceLocation NoLoc;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000875 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000876 /*isAmbiguous=*/false,
877 LParenLoc,
Douglas Gregorae7902c2011-08-04 15:30:47 +0000878 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000879 EllipsisLoc, RParenLoc,
Douglas Gregorae7902c2011-08-04 15:30:47 +0000880 DS.getTypeQualifiers(),
881 /*RefQualifierIsLValueRef=*/true,
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000882 /*RefQualifierLoc=*/NoLoc,
883 /*ConstQualifierLoc=*/NoLoc,
884 /*VolatileQualifierLoc=*/NoLoc,
Douglas Gregorae7902c2011-08-04 15:30:47 +0000885 MutableLoc,
886 ESpecType, ESpecRange.getBegin(),
887 DynamicExceptions.data(),
888 DynamicExceptionRanges.data(),
889 DynamicExceptions.size(),
890 NoexceptExpr.isUsable() ?
891 NoexceptExpr.get() : 0,
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000892 LParenLoc, FunLocalRangeEnd, D,
Douglas Gregorae7902c2011-08-04 15:30:47 +0000893 TrailingReturnType),
894 Attr, DeclEndLoc);
Douglas Gregorc9ecec42012-02-16 21:53:36 +0000895 } else if (Tok.is(tok::kw_mutable) || Tok.is(tok::arrow)) {
896 // It's common to forget that one needs '()' before 'mutable' or the
897 // result type. Deal with this.
898 Diag(Tok, diag::err_lambda_missing_parens)
899 << Tok.is(tok::arrow)
900 << FixItHint::CreateInsertion(Tok.getLocation(), "() ");
901 SourceLocation DeclLoc = Tok.getLocation();
902 SourceLocation DeclEndLoc = DeclLoc;
903
904 // Parse 'mutable', if it's there.
905 SourceLocation MutableLoc;
906 if (Tok.is(tok::kw_mutable)) {
907 MutableLoc = ConsumeToken();
908 DeclEndLoc = MutableLoc;
909 }
910
911 // Parse the return type, if there is one.
Richard Smith54655be2012-06-12 01:51:59 +0000912 TypeResult TrailingReturnType;
Douglas Gregorc9ecec42012-02-16 21:53:36 +0000913 if (Tok.is(tok::arrow)) {
914 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +0000915 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregorc9ecec42012-02-16 21:53:36 +0000916 if (Range.getEnd().isValid())
917 DeclEndLoc = Range.getEnd();
918 }
919
920 ParsedAttributes Attr(AttrFactory);
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000921 SourceLocation NoLoc;
Douglas Gregorc9ecec42012-02-16 21:53:36 +0000922 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000923 /*isAmbiguous=*/false,
924 /*LParenLoc=*/NoLoc,
925 /*Params=*/0,
926 /*NumParams=*/0,
927 /*EllipsisLoc=*/NoLoc,
928 /*RParenLoc=*/NoLoc,
929 /*TypeQuals=*/0,
930 /*RefQualifierIsLValueRef=*/true,
931 /*RefQualifierLoc=*/NoLoc,
932 /*ConstQualifierLoc=*/NoLoc,
933 /*VolatileQualifierLoc=*/NoLoc,
934 MutableLoc,
935 EST_None,
936 /*ESpecLoc=*/NoLoc,
937 /*Exceptions=*/0,
938 /*ExceptionRanges=*/0,
939 /*NumExceptions=*/0,
940 /*NoexceptExpr=*/0,
941 DeclLoc, DeclEndLoc, D,
942 TrailingReturnType),
Douglas Gregorc9ecec42012-02-16 21:53:36 +0000943 Attr, DeclEndLoc);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000944 }
Douglas Gregorc9ecec42012-02-16 21:53:36 +0000945
Douglas Gregorae7902c2011-08-04 15:30:47 +0000946
Eli Friedman906a7e12012-01-06 03:05:34 +0000947 // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
948 // it.
Douglas Gregorfccfb622012-02-21 22:51:27 +0000949 unsigned ScopeFlags = Scope::BlockScope | Scope::FnScope | Scope::DeclScope;
Douglas Gregorfccfb622012-02-21 22:51:27 +0000950 ParseScope BodyScope(this, ScopeFlags);
Eli Friedman906a7e12012-01-06 03:05:34 +0000951
Eli Friedmanec9ea722012-01-05 03:35:19 +0000952 Actions.ActOnStartOfLambdaDefinition(Intro, D, getCurScope());
953
Douglas Gregorae7902c2011-08-04 15:30:47 +0000954 // Parse compound-statement.
Eli Friedmandc3b7232012-01-04 02:40:39 +0000955 if (!Tok.is(tok::l_brace)) {
Douglas Gregorae7902c2011-08-04 15:30:47 +0000956 Diag(Tok, diag::err_expected_lambda_body);
Eli Friedmandc3b7232012-01-04 02:40:39 +0000957 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
958 return ExprError();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000959 }
960
Eli Friedmandc3b7232012-01-04 02:40:39 +0000961 StmtResult Stmt(ParseCompoundStatementBody());
962 BodyScope.Exit();
963
Eli Friedmandeeab902012-01-04 02:46:53 +0000964 if (!Stmt.isInvalid())
Douglas Gregor9e8c92a2012-02-20 19:44:39 +0000965 return Actions.ActOnLambdaExpr(LambdaBeginLoc, Stmt.take(), getCurScope());
Eli Friedmandc3b7232012-01-04 02:40:39 +0000966
Eli Friedmandeeab902012-01-04 02:46:53 +0000967 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
968 return ExprError();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000969}
970
Reid Spencer5f016e22007-07-11 17:01:13 +0000971/// ParseCXXCasts - This handles the various ways to cast expressions to another
972/// type.
973///
974/// postfix-expression: [C++ 5.2p1]
975/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
976/// 'static_cast' '<' type-name '>' '(' expression ')'
977/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
978/// 'const_cast' '<' type-name '>' '(' expression ')'
979///
John McCall60d7b3a2010-08-24 06:29:42 +0000980ExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000981 tok::TokenKind Kind = Tok.getKind();
982 const char *CastName = 0; // For error messages
983
984 switch (Kind) {
David Blaikieeb2d1f12011-09-23 20:26:49 +0000985 default: llvm_unreachable("Unknown C++ cast!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000986 case tok::kw_const_cast: CastName = "const_cast"; break;
987 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
988 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
989 case tok::kw_static_cast: CastName = "static_cast"; break;
990 }
991
992 SourceLocation OpLoc = ConsumeToken();
993 SourceLocation LAngleBracketLoc = Tok.getLocation();
994
Richard Smithea698b32011-04-14 21:45:45 +0000995 // Check for "<::" which is parsed as "[:". If found, fix token stream,
996 // diagnose error, suggest fix, and recover parsing.
Richard Smith78fe3e02012-08-20 17:37:52 +0000997 if (Tok.is(tok::l_square) && Tok.getLength() == 2) {
998 Token Next = NextToken();
999 if (Next.is(tok::colon) && areTokensAdjacent(Tok, Next))
1000 FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
1001 }
Richard Smithea698b32011-04-14 21:45:45 +00001002
Reid Spencer5f016e22007-07-11 17:01:13 +00001003 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +00001004 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001005
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00001006 // Parse the common declaration-specifiers piece.
1007 DeclSpec DS(AttrFactory);
1008 ParseSpecifierQualifierList(DS);
1009
1010 // Parse the abstract-declarator, if present.
1011 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1012 ParseDeclarator(DeclaratorInfo);
1013
Reid Spencer5f016e22007-07-11 17:01:13 +00001014 SourceLocation RAngleBracketLoc = Tok.getLocation();
1015
Chris Lattner1ab3b962008-11-18 07:48:38 +00001016 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +00001017 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +00001018
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001019 SourceLocation LParenLoc, RParenLoc;
1020 BalancedDelimiterTracker T(*this, tok::l_paren);
Reid Spencer5f016e22007-07-11 17:01:13 +00001021
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001022 if (T.expectAndConsume(diag::err_expected_lparen_after, CastName))
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +00001023 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001024
John McCall60d7b3a2010-08-24 06:29:42 +00001025 ExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +00001026
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +00001027 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001028 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00001029
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00001030 if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
Douglas Gregor49badde2008-10-27 19:41:14 +00001031 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00001032 LAngleBracketLoc, DeclaratorInfo,
Douglas Gregor809070a2009-02-18 17:45:20 +00001033 RAngleBracketLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001034 T.getOpenLocation(), Result.take(),
1035 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001036
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001037 return Result;
Reid Spencer5f016e22007-07-11 17:01:13 +00001038}
1039
Sebastian Redlc42e1182008-11-11 11:37:55 +00001040/// ParseCXXTypeid - This handles the C++ typeid expression.
1041///
1042/// postfix-expression: [C++ 5.2p1]
1043/// 'typeid' '(' expression ')'
1044/// 'typeid' '(' type-id ')'
1045///
John McCall60d7b3a2010-08-24 06:29:42 +00001046ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +00001047 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
1048
1049 SourceLocation OpLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001050 SourceLocation LParenLoc, RParenLoc;
1051 BalancedDelimiterTracker T(*this, tok::l_paren);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001052
1053 // typeid expressions are always parenthesized.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001054 if (T.expectAndConsume(diag::err_expected_lparen_after, "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +00001055 return ExprError();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001056 LParenLoc = T.getOpenLocation();
Sebastian Redlc42e1182008-11-11 11:37:55 +00001057
John McCall60d7b3a2010-08-24 06:29:42 +00001058 ExprResult Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +00001059
Richard Smith05766812012-08-18 00:55:03 +00001060 // C++0x [expr.typeid]p3:
1061 // When typeid is applied to an expression other than an lvalue of a
1062 // polymorphic class type [...] The expression is an unevaluated
1063 // operand (Clause 5).
1064 //
1065 // Note that we can't tell whether the expression is an lvalue of a
1066 // polymorphic class type until after we've parsed the expression; we
1067 // speculatively assume the subexpression is unevaluated, and fix it up
1068 // later.
1069 //
1070 // We enter the unevaluated context before trying to determine whether we
1071 // have a type-id, because the tentative parse logic will try to resolve
1072 // names, and must treat them as unevaluated.
Eli Friedman80bfa3d2012-09-26 04:34:21 +00001073 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
1074 Sema::ReuseLambdaContextDecl);
Richard Smith05766812012-08-18 00:55:03 +00001075
Sebastian Redlc42e1182008-11-11 11:37:55 +00001076 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +00001077 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +00001078
1079 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001080 T.consumeClose();
1081 RParenLoc = T.getCloseLocation();
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +00001082 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001083 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +00001084
1085 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallb3d87482010-08-24 05:47:05 +00001086 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001087 } else {
1088 Result = ParseExpression();
1089
1090 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001091 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +00001092 SkipUntil(tok::r_paren);
1093 else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001094 T.consumeClose();
1095 RParenLoc = T.getCloseLocation();
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +00001096 if (RParenLoc.isInvalid())
1097 return ExprError();
Douglas Gregorfadb53b2011-03-12 01:48:56 +00001098
Sebastian Redlc42e1182008-11-11 11:37:55 +00001099 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +00001100 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001101 }
1102 }
1103
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001104 return Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +00001105}
1106
Francois Pichet01b7c302010-09-08 12:20:18 +00001107/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
1108///
1109/// '__uuidof' '(' expression ')'
1110/// '__uuidof' '(' type-id ')'
1111///
1112ExprResult Parser::ParseCXXUuidof() {
1113 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
1114
1115 SourceLocation OpLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001116 BalancedDelimiterTracker T(*this, tok::l_paren);
Francois Pichet01b7c302010-09-08 12:20:18 +00001117
1118 // __uuidof expressions are always parenthesized.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001119 if (T.expectAndConsume(diag::err_expected_lparen_after, "__uuidof"))
Francois Pichet01b7c302010-09-08 12:20:18 +00001120 return ExprError();
1121
1122 ExprResult Result;
1123
1124 if (isTypeIdInParens()) {
1125 TypeResult Ty = ParseTypeName();
1126
1127 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001128 T.consumeClose();
Francois Pichet01b7c302010-09-08 12:20:18 +00001129
1130 if (Ty.isInvalid())
1131 return ExprError();
1132
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001133 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), /*isType=*/true,
1134 Ty.get().getAsOpaquePtr(),
1135 T.getCloseLocation());
Francois Pichet01b7c302010-09-08 12:20:18 +00001136 } else {
1137 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1138 Result = ParseExpression();
1139
1140 // Match the ')'.
1141 if (Result.isInvalid())
1142 SkipUntil(tok::r_paren);
1143 else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001144 T.consumeClose();
Francois Pichet01b7c302010-09-08 12:20:18 +00001145
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001146 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(),
1147 /*isType=*/false,
1148 Result.release(), T.getCloseLocation());
Francois Pichet01b7c302010-09-08 12:20:18 +00001149 }
1150 }
1151
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001152 return Result;
Francois Pichet01b7c302010-09-08 12:20:18 +00001153}
1154
Douglas Gregord4dca082010-02-24 18:44:31 +00001155/// \brief Parse a C++ pseudo-destructor expression after the base,
1156/// . or -> operator, and nested-name-specifier have already been
1157/// parsed.
1158///
1159/// postfix-expression: [C++ 5.2]
1160/// postfix-expression . pseudo-destructor-name
1161/// postfix-expression -> pseudo-destructor-name
1162///
1163/// pseudo-destructor-name:
1164/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
1165/// ::[opt] nested-name-specifier template simple-template-id ::
1166/// ~type-name
1167/// ::[opt] nested-name-specifier[opt] ~type-name
1168///
John McCall60d7b3a2010-08-24 06:29:42 +00001169ExprResult
Douglas Gregord4dca082010-02-24 18:44:31 +00001170Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
1171 tok::TokenKind OpKind,
1172 CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +00001173 ParsedType ObjectType) {
Douglas Gregord4dca082010-02-24 18:44:31 +00001174 // We're parsing either a pseudo-destructor-name or a dependent
1175 // member access that has the same form as a
1176 // pseudo-destructor-name. We parse both in the same way and let
1177 // the action model sort them out.
1178 //
1179 // Note that the ::[opt] nested-name-specifier[opt] has already
1180 // been parsed, and if there was a simple-template-id, it has
1181 // been coalesced into a template-id annotation token.
1182 UnqualifiedId FirstTypeName;
1183 SourceLocation CCLoc;
1184 if (Tok.is(tok::identifier)) {
1185 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1186 ConsumeToken();
1187 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1188 CCLoc = ConsumeToken();
1189 } else if (Tok.is(tok::annot_template_id)) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001190 // FIXME: retrieve TemplateKWLoc from template-id annotation and
1191 // store it in the pseudo-dtor node (to be used when instantiating it).
Douglas Gregord4dca082010-02-24 18:44:31 +00001192 FirstTypeName.setTemplateId(
1193 (TemplateIdAnnotation *)Tok.getAnnotationValue());
1194 ConsumeToken();
1195 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1196 CCLoc = ConsumeToken();
1197 } else {
1198 FirstTypeName.setIdentifier(0, SourceLocation());
1199 }
1200
1201 // Parse the tilde.
1202 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
1203 SourceLocation TildeLoc = ConsumeToken();
David Blaikie91ec7892011-12-16 16:03:09 +00001204
1205 if (Tok.is(tok::kw_decltype) && !FirstTypeName.isValid() && SS.isEmpty()) {
1206 DeclSpec DS(AttrFactory);
Benjamin Kramer85c60db2011-12-18 12:18:02 +00001207 ParseDecltypeSpecifier(DS);
David Blaikie91ec7892011-12-16 16:03:09 +00001208 if (DS.getTypeSpecType() == TST_error)
1209 return ExprError();
1210 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, OpLoc,
1211 OpKind, TildeLoc, DS,
1212 Tok.is(tok::l_paren));
1213 }
1214
Douglas Gregord4dca082010-02-24 18:44:31 +00001215 if (!Tok.is(tok::identifier)) {
1216 Diag(Tok, diag::err_destructor_tilde_identifier);
1217 return ExprError();
1218 }
1219
1220 // Parse the second type.
1221 UnqualifiedId SecondTypeName;
1222 IdentifierInfo *Name = Tok.getIdentifierInfo();
1223 SourceLocation NameLoc = ConsumeToken();
1224 SecondTypeName.setIdentifier(Name, NameLoc);
1225
1226 // If there is a '<', the second type name is a template-id. Parse
1227 // it as such.
1228 if (Tok.is(tok::less) &&
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001229 ParseUnqualifiedIdTemplateId(SS, SourceLocation(),
1230 Name, NameLoc,
1231 false, ObjectType, SecondTypeName,
1232 /*AssumeTemplateName=*/true))
Douglas Gregord4dca082010-02-24 18:44:31 +00001233 return ExprError();
1234
John McCall9ae2f072010-08-23 23:25:46 +00001235 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
1236 OpLoc, OpKind,
Douglas Gregord4dca082010-02-24 18:44:31 +00001237 SS, FirstTypeName, CCLoc,
1238 TildeLoc, SecondTypeName,
1239 Tok.is(tok::l_paren));
1240}
1241
Reid Spencer5f016e22007-07-11 17:01:13 +00001242/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
1243///
1244/// boolean-literal: [C++ 2.13.5]
1245/// 'true'
1246/// 'false'
John McCall60d7b3a2010-08-24 06:29:42 +00001247ExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001248 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001249 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +00001250}
Chris Lattner50dd2892008-02-26 00:51:44 +00001251
1252/// ParseThrowExpression - This handles the C++ throw expression.
1253///
1254/// throw-expression: [C++ 15]
1255/// 'throw' assignment-expression[opt]
John McCall60d7b3a2010-08-24 06:29:42 +00001256ExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +00001257 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +00001258 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +00001259
Chris Lattner2a2819a2008-04-06 06:02:23 +00001260 // If the current token isn't the start of an assignment-expression,
1261 // then the expression is not present. This handles things like:
1262 // "C ? throw : (void)42", which is crazy but legal.
1263 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
1264 case tok::semi:
1265 case tok::r_paren:
1266 case tok::r_square:
1267 case tok::r_brace:
1268 case tok::colon:
1269 case tok::comma:
Douglas Gregorbca01b42011-07-06 22:04:06 +00001270 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0);
Chris Lattner50dd2892008-02-26 00:51:44 +00001271
Chris Lattner2a2819a2008-04-06 06:02:23 +00001272 default:
John McCall60d7b3a2010-08-24 06:29:42 +00001273 ExprResult Expr(ParseAssignmentExpression());
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001274 if (Expr.isInvalid()) return Expr;
Douglas Gregorbca01b42011-07-06 22:04:06 +00001275 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take());
Chris Lattner2a2819a2008-04-06 06:02:23 +00001276 }
Chris Lattner50dd2892008-02-26 00:51:44 +00001277}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001278
1279/// ParseCXXThis - This handles the C++ 'this' pointer.
1280///
1281/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
1282/// a non-lvalue expression whose value is the address of the object for which
1283/// the function is called.
John McCall60d7b3a2010-08-24 06:29:42 +00001284ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001285 assert(Tok.is(tok::kw_this) && "Not 'this'!");
1286 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001287 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001288}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001289
1290/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1291/// Can be interpreted either as function-style casting ("int(x)")
1292/// or class type construction ("ClassType(x,y,z)")
1293/// or creation of a value-initialized type ("int()").
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001294/// See [C++ 5.2.3].
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001295///
1296/// postfix-expression: [C++ 5.2p1]
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001297/// simple-type-specifier '(' expression-list[opt] ')'
1298/// [C++0x] simple-type-specifier braced-init-list
1299/// typename-specifier '(' expression-list[opt] ')'
1300/// [C++0x] typename-specifier braced-init-list
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001301///
John McCall60d7b3a2010-08-24 06:29:42 +00001302ExprResult
Sebastian Redl20df9b72008-12-11 22:51:44 +00001303Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001304 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallb3d87482010-08-24 05:47:05 +00001305 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001306
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001307 assert((Tok.is(tok::l_paren) ||
Richard Smith80ad52f2013-01-02 11:42:31 +00001308 (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)))
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001309 && "Expected '(' or '{'!");
Douglas Gregorbc61bd82011-01-11 00:33:19 +00001310
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001311 if (Tok.is(tok::l_brace)) {
Sebastian Redl6dc00f62012-02-12 18:41:05 +00001312 ExprResult Init = ParseBraceInitializer();
1313 if (Init.isInvalid())
1314 return Init;
1315 Expr *InitList = Init.take();
1316 return Actions.ActOnCXXTypeConstructExpr(TypeRep, SourceLocation(),
1317 MultiExprArg(&InitList, 1),
1318 SourceLocation());
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001319 } else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001320 BalancedDelimiterTracker T(*this, tok::l_paren);
1321 T.consumeOpen();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001322
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001323 ExprVector Exprs;
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001324 CommaLocsTy CommaLocs;
1325
1326 if (Tok.isNot(tok::r_paren)) {
1327 if (ParseExpressionList(Exprs, CommaLocs)) {
1328 SkipUntil(tok::r_paren);
1329 return ExprError();
1330 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001331 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001332
1333 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001334 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001335
1336 // TypeRep could be null, if it references an invalid typedef.
1337 if (!TypeRep)
1338 return ExprError();
1339
1340 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1341 "Unexpected number of commas!");
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001342 return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001343 Exprs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001344 T.getCloseLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001345 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001346}
1347
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001348/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001349///
1350/// condition:
1351/// expression
1352/// type-specifier-seq declarator '=' assignment-expression
Richard Smith0635aa72012-02-22 06:49:09 +00001353/// [C++11] type-specifier-seq declarator '=' initializer-clause
1354/// [C++11] type-specifier-seq declarator braced-init-list
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001355/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
1356/// '=' assignment-expression
1357///
Dmitri Gribenko1ddbd892012-08-24 00:01:24 +00001358/// \param ExprOut if the condition was parsed as an expression, the parsed
1359/// expression.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001360///
Dmitri Gribenko1ddbd892012-08-24 00:01:24 +00001361/// \param DeclOut if the condition was parsed as a declaration, the parsed
1362/// declaration.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001363///
Douglas Gregor586596f2010-05-06 17:25:47 +00001364/// \param Loc The location of the start of the statement that requires this
1365/// condition, e.g., the "for" in a for loop.
1366///
1367/// \param ConvertToBoolean Whether the condition expression should be
1368/// converted to a boolean value.
1369///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001370/// \returns true if there was a parsing, false otherwise.
John McCall60d7b3a2010-08-24 06:29:42 +00001371bool Parser::ParseCXXCondition(ExprResult &ExprOut,
1372 Decl *&DeclOut,
Douglas Gregor586596f2010-05-06 17:25:47 +00001373 SourceLocation Loc,
1374 bool ConvertToBoolean) {
Douglas Gregor01dfea02010-01-10 23:08:15 +00001375 if (Tok.is(tok::code_completion)) {
John McCallf312b1e2010-08-26 23:41:50 +00001376 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001377 cutOffParsing();
1378 return true;
Douglas Gregor01dfea02010-01-10 23:08:15 +00001379 }
1380
Sean Hunt2edf0a22012-06-23 05:07:58 +00001381 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00001382 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00001383
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001384 if (!isCXXConditionDeclaration()) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001385 ProhibitAttributes(attrs);
1386
Douglas Gregor586596f2010-05-06 17:25:47 +00001387 // Parse the expression.
John McCall60d7b3a2010-08-24 06:29:42 +00001388 ExprOut = ParseExpression(); // expression
1389 DeclOut = 0;
1390 if (ExprOut.isInvalid())
Douglas Gregor586596f2010-05-06 17:25:47 +00001391 return true;
1392
1393 // If required, convert to a boolean value.
1394 if (ConvertToBoolean)
John McCall60d7b3a2010-08-24 06:29:42 +00001395 ExprOut
1396 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
1397 return ExprOut.isInvalid();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001398 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001399
1400 // type-specifier-seq
John McCall0b7e6782011-03-24 11:26:52 +00001401 DeclSpec DS(AttrFactory);
Richard Smith6b3d3e52013-02-20 19:22:51 +00001402 DS.takeAttributesFrom(attrs);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001403 ParseSpecifierQualifierList(DS);
1404
1405 // declarator
1406 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
1407 ParseDeclarator(DeclaratorInfo);
1408
1409 // simple-asm-expr[opt]
1410 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00001411 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00001412 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001413 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001414 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001415 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001416 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001417 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001418 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001419 }
1420
1421 // If attributes are present, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001422 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001423
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001424 // Type-check the declaration itself.
John McCall60d7b3a2010-08-24 06:29:42 +00001425 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall7f040a92010-12-24 02:08:15 +00001426 DeclaratorInfo);
John McCall60d7b3a2010-08-24 06:29:42 +00001427 DeclOut = Dcl.get();
1428 ExprOut = ExprError();
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001429
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001430 // '=' assignment-expression
Richard Trieud6c7c672012-01-18 22:54:52 +00001431 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Smith0635aa72012-02-22 06:49:09 +00001432 bool CopyInitialization = isTokenEqualOrEqualTypo();
1433 if (CopyInitialization)
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001434 ConsumeToken();
Richard Smith0635aa72012-02-22 06:49:09 +00001435
1436 ExprResult InitExpr = ExprError();
Richard Smith80ad52f2013-01-02 11:42:31 +00001437 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith0635aa72012-02-22 06:49:09 +00001438 Diag(Tok.getLocation(),
1439 diag::warn_cxx98_compat_generalized_initializer_lists);
1440 InitExpr = ParseBraceInitializer();
1441 } else if (CopyInitialization) {
1442 InitExpr = ParseAssignmentExpression();
1443 } else if (Tok.is(tok::l_paren)) {
1444 // This was probably an attempt to initialize the variable.
1445 SourceLocation LParen = ConsumeParen(), RParen = LParen;
1446 if (SkipUntil(tok::r_paren, true, /*DontConsume=*/true))
1447 RParen = ConsumeParen();
1448 Diag(DeclOut ? DeclOut->getLocation() : LParen,
1449 diag::err_expected_init_in_condition_lparen)
1450 << SourceRange(LParen, RParen);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001451 } else {
Richard Smith0635aa72012-02-22 06:49:09 +00001452 Diag(DeclOut ? DeclOut->getLocation() : Tok.getLocation(),
1453 diag::err_expected_init_in_condition);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001454 }
Richard Smith0635aa72012-02-22 06:49:09 +00001455
1456 if (!InitExpr.isInvalid())
1457 Actions.AddInitializerToDecl(DeclOut, InitExpr.take(), !CopyInitialization,
Richard Smitha2c36462013-04-26 16:15:35 +00001458 DS.containsPlaceholderType());
Richard Smith0635aa72012-02-22 06:49:09 +00001459
Douglas Gregor586596f2010-05-06 17:25:47 +00001460 // FIXME: Build a reference to this declaration? Convert it to bool?
1461 // (This is currently handled by Sema).
Richard Smith483b9f32011-02-21 20:05:19 +00001462
1463 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregor586596f2010-05-06 17:25:47 +00001464
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001465 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001466}
1467
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001468/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1469/// This should only be called when the current token is known to be part of
1470/// simple-type-specifier.
1471///
1472/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001473/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001474/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1475/// char
1476/// wchar_t
1477/// bool
1478/// short
1479/// int
1480/// long
1481/// signed
1482/// unsigned
1483/// float
1484/// double
1485/// void
1486/// [GNU] typeof-specifier
1487/// [C++0x] auto [TODO]
1488///
1489/// type-name:
1490/// class-name
1491/// enum-name
1492/// typedef-name
1493///
1494void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1495 DS.SetRangeStart(Tok.getLocation());
1496 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001497 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001498 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001499
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001500 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +00001501 case tok::identifier: // foo::bar
1502 case tok::coloncolon: // ::foo::bar
David Blaikieb219cfc2011-09-23 05:06:16 +00001503 llvm_unreachable("Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +00001504 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00001505 llvm_unreachable("Not a simple-type-specifier token!");
Chris Lattner55a7cef2009-01-05 00:13:00 +00001506
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001507 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001508 case tok::annot_typename: {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001509 if (getTypeAnnotation(Tok))
1510 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
1511 getTypeAnnotation(Tok));
1512 else
1513 DS.SetTypeSpecError();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001514
1515 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1516 ConsumeToken();
1517
1518 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1519 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1520 // Objective-C interface. If we don't have Objective-C or a '<', this is
1521 // just a normal reference to a typedef name.
David Blaikie4e4d0842012-03-11 07:00:24 +00001522 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001523 ParseObjCProtocolQualifiers(DS);
1524
1525 DS.Finish(Diags, PP);
1526 return;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001527 }
Mike Stump1eb44332009-09-09 15:08:12 +00001528
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001529 // builtin types
1530 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001531 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001532 break;
1533 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +00001534 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001535 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00001536 case tok::kw___int64:
1537 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1538 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001539 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001540 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001541 break;
1542 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001543 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001544 break;
1545 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001546 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001547 break;
1548 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001549 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001550 break;
1551 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001552 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001553 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00001554 case tok::kw___int128:
1555 DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, DiagID);
1556 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001557 case tok::kw_half:
1558 DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
1559 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001560 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001561 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001562 break;
1563 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001564 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001565 break;
1566 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001567 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001568 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001569 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001570 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001571 break;
1572 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001573 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001574 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001575 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +00001576 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001577 break;
David Blaikie5e089fe2012-01-24 05:47:35 +00001578 case tok::annot_decltype:
1579 case tok::kw_decltype:
1580 DS.SetRangeEnd(ParseDecltypeSpecifier(DS));
1581 return DS.Finish(Diags, PP);
Mike Stump1eb44332009-09-09 15:08:12 +00001582
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001583 // GNU typeof support.
1584 case tok::kw_typeof:
1585 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001586 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001587 return;
1588 }
Chris Lattnerb31757b2009-01-06 05:06:21 +00001589 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001590 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1591 else
1592 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001593 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001594 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001595}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001596
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001597/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1598/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1599/// e.g., "const short int". Note that the DeclSpec is *not* finished
1600/// by parsing the type-specifier-seq, because these sequences are
1601/// typically followed by some form of declarator. Returns true and
1602/// emits diagnostics if this is not a type-specifier-seq, false
1603/// otherwise.
1604///
1605/// type-specifier-seq: [C++ 8.1]
1606/// type-specifier type-specifier-seq[opt]
1607///
1608bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
Richard Smith69730c12012-03-12 07:56:15 +00001609 ParseSpecifierQualifierList(DS, AS_none, DSC_type_specifier);
Douglas Gregor396a9f22010-02-24 23:13:13 +00001610 DS.Finish(Diags, PP);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001611 return false;
1612}
1613
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001614/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1615/// some form.
1616///
1617/// This routine is invoked when a '<' is encountered after an identifier or
1618/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1619/// whether the unqualified-id is actually a template-id. This routine will
1620/// then parse the template arguments and form the appropriate template-id to
1621/// return to the caller.
1622///
1623/// \param SS the nested-name-specifier that precedes this template-id, if
1624/// we're actually parsing a qualified-id.
1625///
1626/// \param Name for constructor and destructor names, this is the actual
1627/// identifier that may be a template-name.
1628///
1629/// \param NameLoc the location of the class-name in a constructor or
1630/// destructor.
1631///
1632/// \param EnteringContext whether we're entering the scope of the
1633/// nested-name-specifier.
1634///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001635/// \param ObjectType if this unqualified-id occurs within a member access
1636/// expression, the type of the base object whose member is being accessed.
1637///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001638/// \param Id as input, describes the template-name or operator-function-id
1639/// that precedes the '<'. If template arguments were parsed successfully,
1640/// will be updated with the template-id.
1641///
Douglas Gregord4dca082010-02-24 18:44:31 +00001642/// \param AssumeTemplateId When true, this routine will assume that the name
1643/// refers to a template without performing name lookup to verify.
1644///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001645/// \returns true if a parse error occurred, false otherwise.
1646bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001647 SourceLocation TemplateKWLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001648 IdentifierInfo *Name,
1649 SourceLocation NameLoc,
1650 bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001651 ParsedType ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +00001652 UnqualifiedId &Id,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001653 bool AssumeTemplateId) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001654 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1655 "Expected '<' to finish parsing a template-id");
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001656
1657 TemplateTy Template;
1658 TemplateNameKind TNK = TNK_Non_template;
1659 switch (Id.getKind()) {
1660 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +00001661 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +00001662 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +00001663 if (AssumeTemplateId) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001664 TNK = Actions.ActOnDependentTemplateName(getCurScope(), SS, TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001665 Id, ObjectType, EnteringContext,
1666 Template);
1667 if (TNK == TNK_Non_template)
1668 return true;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001669 } else {
1670 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001671 TNK = Actions.isTemplateName(getCurScope(), SS,
1672 TemplateKWLoc.isValid(), Id,
1673 ObjectType, EnteringContext, Template,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001674 MemberOfUnknownSpecialization);
1675
1676 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1677 ObjectType && IsTemplateArgumentList()) {
1678 // We have something like t->getAs<T>(), where getAs is a
1679 // member of an unknown specialization. However, this will only
1680 // parse correctly as a template, so suggest the keyword 'template'
1681 // before 'getAs' and treat this as a dependent template name.
1682 std::string Name;
1683 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1684 Name = Id.Identifier->getName();
1685 else {
1686 Name = "operator ";
1687 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1688 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1689 else
1690 Name += Id.Identifier->getName();
1691 }
1692 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1693 << Name
1694 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001695 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1696 SS, TemplateKWLoc, Id,
1697 ObjectType, EnteringContext,
1698 Template);
Douglas Gregord6ab2322010-06-16 23:00:59 +00001699 if (TNK == TNK_Non_template)
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001700 return true;
1701 }
1702 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001703 break;
1704
Douglas Gregor014e88d2009-11-03 23:16:33 +00001705 case UnqualifiedId::IK_ConstructorName: {
1706 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001707 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001708 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c153532010-08-06 12:11:11 +00001709 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1710 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001711 EnteringContext, Template,
1712 MemberOfUnknownSpecialization);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001713 break;
1714 }
1715
Douglas Gregor014e88d2009-11-03 23:16:33 +00001716 case UnqualifiedId::IK_DestructorName: {
1717 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001718 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001719 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001720 if (ObjectType) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001721 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1722 SS, TemplateKWLoc, TemplateName,
1723 ObjectType, EnteringContext,
1724 Template);
Douglas Gregord6ab2322010-06-16 23:00:59 +00001725 if (TNK == TNK_Non_template)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001726 return true;
1727 } else {
Abramo Bagnara7c153532010-08-06 12:11:11 +00001728 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1729 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001730 EnteringContext, Template,
1731 MemberOfUnknownSpecialization);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001732
John McCallb3d87482010-08-24 05:47:05 +00001733 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001734 Diag(NameLoc, diag::err_destructor_template_id)
1735 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001736 return true;
1737 }
1738 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001739 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001740 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001741
1742 default:
1743 return false;
1744 }
1745
1746 if (TNK == TNK_Non_template)
1747 return false;
1748
1749 // Parse the enclosed template argument list.
1750 SourceLocation LAngleLoc, RAngleLoc;
1751 TemplateArgList TemplateArgs;
Douglas Gregor0278e122010-05-05 05:58:24 +00001752 if (Tok.is(tok::less) &&
1753 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor059101f2011-03-02 00:47:37 +00001754 SS, true, LAngleLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001755 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001756 RAngleLoc))
1757 return true;
1758
1759 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001760 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1761 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001762 // Form a parsed representation of the template-id to be stored in the
1763 // UnqualifiedId.
1764 TemplateIdAnnotation *TemplateId
Benjamin Kramer13bb7012012-04-14 12:14:03 +00001765 = TemplateIdAnnotation::Allocate(TemplateArgs.size(), TemplateIds);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001766
1767 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1768 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001769 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001770 TemplateId->TemplateNameLoc = Id.StartLocation;
1771 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001772 TemplateId->Name = 0;
1773 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1774 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001775 }
1776
Douglas Gregor059101f2011-03-02 00:47:37 +00001777 TemplateId->SS = SS;
Benjamin Kramer2b28bf12012-02-19 23:37:39 +00001778 TemplateId->TemplateKWLoc = TemplateKWLoc;
John McCall2b5289b2010-08-23 07:28:44 +00001779 TemplateId->Template = Template;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001780 TemplateId->Kind = TNK;
1781 TemplateId->LAngleLoc = LAngleLoc;
1782 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001783 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001784 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001785 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001786 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001787
1788 Id.setTemplateId(TemplateId);
1789 return false;
1790 }
1791
1792 // Bundle the template arguments together.
Benjamin Kramer5354e772012-08-23 23:38:35 +00001793 ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs);
Abramo Bagnarafad03b72012-01-27 08:46:19 +00001794
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001795 // Constructor and destructor names.
John McCallf312b1e2010-08-26 23:41:50 +00001796 TypeResult Type
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001797 = Actions.ActOnTemplateIdType(SS, TemplateKWLoc,
1798 Template, NameLoc,
Abramo Bagnarafad03b72012-01-27 08:46:19 +00001799 LAngleLoc, TemplateArgsPtr, RAngleLoc,
1800 /*IsCtorOrDtorName=*/true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001801 if (Type.isInvalid())
1802 return true;
1803
1804 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1805 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1806 else
1807 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1808
1809 return false;
1810}
1811
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001812/// \brief Parse an operator-function-id or conversion-function-id as part
1813/// of a C++ unqualified-id.
1814///
1815/// This routine is responsible only for parsing the operator-function-id or
1816/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001817///
1818/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001819/// operator-function-id: [C++ 13.5]
1820/// 'operator' operator
1821///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001822/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001823/// new delete new[] delete[]
1824/// + - * / % ^ & | ~
1825/// ! = < > += -= *= /= %=
1826/// ^= &= |= << >> >>= <<= == !=
1827/// <= >= && || ++ -- , ->* ->
1828/// () []
1829///
1830/// conversion-function-id: [C++ 12.3.2]
1831/// operator conversion-type-id
1832///
1833/// conversion-type-id:
1834/// type-specifier-seq conversion-declarator[opt]
1835///
1836/// conversion-declarator:
1837/// ptr-operator conversion-declarator[opt]
1838/// \endcode
1839///
Dmitri Gribenko1ddbd892012-08-24 00:01:24 +00001840/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001841/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1842///
1843/// \param EnteringContext whether we are entering the scope of the
1844/// nested-name-specifier.
1845///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001846/// \param ObjectType if this unqualified-id occurs within a member access
1847/// expression, the type of the base object whose member is being accessed.
1848///
1849/// \param Result on a successful parse, contains the parsed unqualified-id.
1850///
1851/// \returns true if parsing fails, false otherwise.
1852bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001853 ParsedType ObjectType,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001854 UnqualifiedId &Result) {
1855 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1856
1857 // Consume the 'operator' keyword.
1858 SourceLocation KeywordLoc = ConsumeToken();
1859
1860 // Determine what kind of operator name we have.
1861 unsigned SymbolIdx = 0;
1862 SourceLocation SymbolLocations[3];
1863 OverloadedOperatorKind Op = OO_None;
1864 switch (Tok.getKind()) {
1865 case tok::kw_new:
1866 case tok::kw_delete: {
1867 bool isNew = Tok.getKind() == tok::kw_new;
1868 // Consume the 'new' or 'delete'.
1869 SymbolLocations[SymbolIdx++] = ConsumeToken();
Richard Smith6ee326a2012-04-10 01:32:12 +00001870 // Check for array new/delete.
1871 if (Tok.is(tok::l_square) &&
Richard Smith80ad52f2013-01-02 11:42:31 +00001872 (!getLangOpts().CPlusPlus11 || NextToken().isNot(tok::l_square))) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001873 // Consume the '[' and ']'.
1874 BalancedDelimiterTracker T(*this, tok::l_square);
1875 T.consumeOpen();
1876 T.consumeClose();
1877 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001878 return true;
1879
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001880 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1881 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001882 Op = isNew? OO_Array_New : OO_Array_Delete;
1883 } else {
1884 Op = isNew? OO_New : OO_Delete;
1885 }
1886 break;
1887 }
1888
1889#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1890 case tok::Token: \
1891 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1892 Op = OO_##Name; \
1893 break;
1894#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1895#include "clang/Basic/OperatorKinds.def"
1896
1897 case tok::l_paren: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001898 // Consume the '(' and ')'.
1899 BalancedDelimiterTracker T(*this, tok::l_paren);
1900 T.consumeOpen();
1901 T.consumeClose();
1902 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001903 return true;
1904
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001905 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1906 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001907 Op = OO_Call;
1908 break;
1909 }
1910
1911 case tok::l_square: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001912 // Consume the '[' and ']'.
1913 BalancedDelimiterTracker T(*this, tok::l_square);
1914 T.consumeOpen();
1915 T.consumeClose();
1916 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001917 return true;
1918
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001919 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1920 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001921 Op = OO_Subscript;
1922 break;
1923 }
1924
1925 case tok::code_completion: {
1926 // Code completion for the operator name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001927 Actions.CodeCompleteOperatorName(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001928 cutOffParsing();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001929 // Don't try to parse any further.
1930 return true;
1931 }
1932
1933 default:
1934 break;
1935 }
1936
1937 if (Op != OO_None) {
1938 // We have parsed an operator-function-id.
1939 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1940 return false;
1941 }
Sean Hunt0486d742009-11-28 04:44:28 +00001942
1943 // Parse a literal-operator-id.
1944 //
Richard Smithaa9a8ce2012-10-20 08:41:10 +00001945 // literal-operator-id: C++11 [over.literal]
1946 // operator string-literal identifier
1947 // operator user-defined-string-literal
Sean Hunt0486d742009-11-28 04:44:28 +00001948
Richard Smith80ad52f2013-01-02 11:42:31 +00001949 if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
Richard Smith7fe62082011-10-15 05:09:34 +00001950 Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator);
Sean Hunt0486d742009-11-28 04:44:28 +00001951
Richard Smith33762772012-03-08 23:06:02 +00001952 SourceLocation DiagLoc;
1953 unsigned DiagId = 0;
1954
1955 // We're past translation phase 6, so perform string literal concatenation
1956 // before checking for "".
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001957 SmallVector<Token, 4> Toks;
1958 SmallVector<SourceLocation, 4> TokLocs;
Richard Smith33762772012-03-08 23:06:02 +00001959 while (isTokenStringLiteral()) {
1960 if (!Tok.is(tok::string_literal) && !DiagId) {
Richard Smithaa9a8ce2012-10-20 08:41:10 +00001961 // C++11 [over.literal]p1:
1962 // The string-literal or user-defined-string-literal in a
1963 // literal-operator-id shall have no encoding-prefix [...].
Richard Smith33762772012-03-08 23:06:02 +00001964 DiagLoc = Tok.getLocation();
1965 DiagId = diag::err_literal_operator_string_prefix;
1966 }
1967 Toks.push_back(Tok);
1968 TokLocs.push_back(ConsumeStringToken());
1969 }
1970
1971 StringLiteralParser Literal(Toks.data(), Toks.size(), PP);
1972 if (Literal.hadError)
1973 return true;
1974
1975 // Grab the literal operator's suffix, which will be either the next token
1976 // or a ud-suffix from the string literal.
1977 IdentifierInfo *II = 0;
1978 SourceLocation SuffixLoc;
1979 if (!Literal.getUDSuffix().empty()) {
1980 II = &PP.getIdentifierTable().get(Literal.getUDSuffix());
1981 SuffixLoc =
1982 Lexer::AdvanceToTokenCharacter(TokLocs[Literal.getUDSuffixToken()],
1983 Literal.getUDSuffixOffset(),
David Blaikie4e4d0842012-03-11 07:00:24 +00001984 PP.getSourceManager(), getLangOpts());
Richard Smith33762772012-03-08 23:06:02 +00001985 } else if (Tok.is(tok::identifier)) {
1986 II = Tok.getIdentifierInfo();
1987 SuffixLoc = ConsumeToken();
1988 TokLocs.push_back(SuffixLoc);
1989 } else {
Sean Hunt0486d742009-11-28 04:44:28 +00001990 Diag(Tok.getLocation(), diag::err_expected_ident);
1991 return true;
1992 }
1993
Richard Smith33762772012-03-08 23:06:02 +00001994 // The string literal must be empty.
1995 if (!Literal.GetString().empty() || Literal.Pascal) {
Richard Smithaa9a8ce2012-10-20 08:41:10 +00001996 // C++11 [over.literal]p1:
1997 // The string-literal or user-defined-string-literal in a
1998 // literal-operator-id shall [...] contain no characters
1999 // other than the implicit terminating '\0'.
Richard Smith33762772012-03-08 23:06:02 +00002000 DiagLoc = TokLocs.front();
2001 DiagId = diag::err_literal_operator_string_not_empty;
2002 }
2003
2004 if (DiagId) {
2005 // This isn't a valid literal-operator-id, but we think we know
2006 // what the user meant. Tell them what they should have written.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002007 SmallString<32> Str;
Richard Smith33762772012-03-08 23:06:02 +00002008 Str += "\"\" ";
2009 Str += II->getName();
2010 Diag(DiagLoc, DiagId) << FixItHint::CreateReplacement(
2011 SourceRange(TokLocs.front(), TokLocs.back()), Str);
2012 }
2013
2014 Result.setLiteralOperatorId(II, KeywordLoc, SuffixLoc);
Sean Hunt3e518bd2009-11-29 07:34:05 +00002015 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00002016 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002017
2018 // Parse a conversion-function-id.
2019 //
2020 // conversion-function-id: [C++ 12.3.2]
2021 // operator conversion-type-id
2022 //
2023 // conversion-type-id:
2024 // type-specifier-seq conversion-declarator[opt]
2025 //
2026 // conversion-declarator:
2027 // ptr-operator conversion-declarator[opt]
2028
2029 // Parse the type-specifier-seq.
John McCall0b7e6782011-03-24 11:26:52 +00002030 DeclSpec DS(AttrFactory);
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00002031 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002032 return true;
2033
2034 // Parse the conversion-declarator, which is merely a sequence of
2035 // ptr-operators.
2036 Declarator D(DS, Declarator::TypeNameContext);
2037 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
2038
2039 // Finish up the type.
John McCallf312b1e2010-08-26 23:41:50 +00002040 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002041 if (Ty.isInvalid())
2042 return true;
2043
2044 // Note that this is a conversion-function-id.
2045 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
2046 D.getSourceRange().getEnd());
2047 return false;
2048}
2049
2050/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
2051/// name of an entity.
2052///
2053/// \code
2054/// unqualified-id: [C++ expr.prim.general]
2055/// identifier
2056/// operator-function-id
2057/// conversion-function-id
2058/// [C++0x] literal-operator-id [TODO]
2059/// ~ class-name
2060/// template-id
2061///
2062/// \endcode
2063///
Dmitri Gribenko1ddbd892012-08-24 00:01:24 +00002064/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002065/// non-empty, then we are parsing the unqualified-id of a qualified-id.
2066///
2067/// \param EnteringContext whether we are entering the scope of the
2068/// nested-name-specifier.
2069///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002070/// \param AllowDestructorName whether we allow parsing of a destructor name.
2071///
2072/// \param AllowConstructorName whether we allow parsing a constructor name.
2073///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00002074/// \param ObjectType if this unqualified-id occurs within a member access
2075/// expression, the type of the base object whose member is being accessed.
2076///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002077/// \param Result on a successful parse, contains the parsed unqualified-id.
2078///
2079/// \returns true if parsing fails, false otherwise.
2080bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
2081 bool AllowDestructorName,
2082 bool AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00002083 ParsedType ObjectType,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002084 SourceLocation& TemplateKWLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002085 UnqualifiedId &Result) {
Douglas Gregor0278e122010-05-05 05:58:24 +00002086
2087 // Handle 'A::template B'. This is for template-ids which have not
2088 // already been annotated by ParseOptionalCXXScopeSpecifier().
2089 bool TemplateSpecified = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00002090 if (getLangOpts().CPlusPlus && Tok.is(tok::kw_template) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00002091 (ObjectType || SS.isSet())) {
2092 TemplateSpecified = true;
2093 TemplateKWLoc = ConsumeToken();
2094 }
2095
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002096 // unqualified-id:
2097 // identifier
2098 // template-id (when it hasn't already been annotated)
2099 if (Tok.is(tok::identifier)) {
2100 // Consume the identifier.
2101 IdentifierInfo *Id = Tok.getIdentifierInfo();
2102 SourceLocation IdLoc = ConsumeToken();
2103
David Blaikie4e4d0842012-03-11 07:00:24 +00002104 if (!getLangOpts().CPlusPlus) {
Douglas Gregorb862b8f2010-01-11 23:29:10 +00002105 // If we're not in C++, only identifiers matter. Record the
2106 // identifier and return.
2107 Result.setIdentifier(Id, IdLoc);
2108 return false;
2109 }
2110
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002111 if (AllowConstructorName &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002112 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002113 // We have parsed a constructor name.
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002114 ParsedType Ty = Actions.getTypeName(*Id, IdLoc, getCurScope(),
2115 &SS, false, false,
2116 ParsedType(),
2117 /*IsCtorOrDtorName=*/true,
2118 /*NonTrivialTypeSourceInfo=*/true);
2119 Result.setConstructorName(Ty, IdLoc, IdLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002120 } else {
2121 // We have parsed an identifier.
2122 Result.setIdentifier(Id, IdLoc);
2123 }
2124
2125 // If the next token is a '<', we may have a template.
Douglas Gregor0278e122010-05-05 05:58:24 +00002126 if (TemplateSpecified || Tok.is(tok::less))
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002127 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc, Id, IdLoc,
2128 EnteringContext, ObjectType,
2129 Result, TemplateSpecified);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002130
2131 return false;
2132 }
2133
2134 // unqualified-id:
2135 // template-id (already parsed and annotated)
2136 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002137 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002138
2139 // If the template-name names the current class, then this is a constructor
2140 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002141 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002142 if (SS.isSet()) {
2143 // C++ [class.qual]p2 specifies that a qualified template-name
2144 // is taken as the constructor name where a constructor can be
2145 // declared. Thus, the template arguments are extraneous, so
2146 // complain about them and remove them entirely.
2147 Diag(TemplateId->TemplateNameLoc,
2148 diag::err_out_of_line_constructor_template_id)
2149 << TemplateId->Name
Douglas Gregor849b2432010-03-31 17:46:05 +00002150 << FixItHint::CreateRemoval(
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002151 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002152 ParsedType Ty = Actions.getTypeName(*TemplateId->Name,
2153 TemplateId->TemplateNameLoc,
2154 getCurScope(),
2155 &SS, false, false,
2156 ParsedType(),
2157 /*IsCtorOrDtorName=*/true,
2158 /*NontrivialTypeSourceInfo=*/true);
2159 Result.setConstructorName(Ty, TemplateId->TemplateNameLoc,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002160 TemplateId->RAngleLoc);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002161 ConsumeToken();
2162 return false;
2163 }
2164
2165 Result.setConstructorTemplateId(TemplateId);
2166 ConsumeToken();
2167 return false;
2168 }
2169
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002170 // We have already parsed a template-id; consume the annotation token as
2171 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002172 Result.setTemplateId(TemplateId);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002173 TemplateKWLoc = TemplateId->TemplateKWLoc;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002174 ConsumeToken();
2175 return false;
2176 }
2177
2178 // unqualified-id:
2179 // operator-function-id
2180 // conversion-function-id
2181 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002182 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002183 return true;
2184
Sean Hunte6252d12009-11-28 08:58:14 +00002185 // If we have an operator-function-id or a literal-operator-id and the next
2186 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002187 //
2188 // template-id:
2189 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00002190 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
2191 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00002192 (TemplateSpecified || Tok.is(tok::less)))
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002193 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2194 0, SourceLocation(),
2195 EnteringContext, ObjectType,
2196 Result, TemplateSpecified);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002197
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002198 return false;
2199 }
2200
David Blaikie4e4d0842012-03-11 07:00:24 +00002201 if (getLangOpts().CPlusPlus &&
Douglas Gregorb862b8f2010-01-11 23:29:10 +00002202 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002203 // C++ [expr.unary.op]p10:
2204 // There is an ambiguity in the unary-expression ~X(), where X is a
2205 // class-name. The ambiguity is resolved in favor of treating ~ as a
2206 // unary complement rather than treating ~X as referring to a destructor.
2207
2208 // Parse the '~'.
2209 SourceLocation TildeLoc = ConsumeToken();
David Blaikie53a75c02011-12-08 16:13:53 +00002210
2211 if (SS.isEmpty() && Tok.is(tok::kw_decltype)) {
2212 DeclSpec DS(AttrFactory);
2213 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
2214 if (ParsedType Type = Actions.getDestructorType(DS, ObjectType)) {
2215 Result.setDestructorName(TildeLoc, Type, EndLoc);
2216 return false;
2217 }
2218 return true;
2219 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002220
2221 // Parse the class-name.
2222 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00002223 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002224 return true;
2225 }
2226
2227 // Parse the class-name (or template-name in a simple-template-id).
2228 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
2229 SourceLocation ClassNameLoc = ConsumeToken();
2230
Douglas Gregor0278e122010-05-05 05:58:24 +00002231 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallb3d87482010-08-24 05:47:05 +00002232 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002233 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2234 ClassName, ClassNameLoc,
2235 EnteringContext, ObjectType,
2236 Result, TemplateSpecified);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002237 }
2238
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002239 // Note that this is a destructor name.
John McCallb3d87482010-08-24 05:47:05 +00002240 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
2241 ClassNameLoc, getCurScope(),
2242 SS, ObjectType,
2243 EnteringContext);
Douglas Gregor124b8782010-02-16 19:09:40 +00002244 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002245 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00002246
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002247 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002248 return false;
2249 }
2250
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002251 Diag(Tok, diag::err_expected_unqualified_id)
David Blaikie4e4d0842012-03-11 07:00:24 +00002252 << getLangOpts().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002253 return true;
2254}
2255
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002256/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
2257/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00002258///
Chris Lattner59232d32009-01-04 21:25:24 +00002259/// This method is called to parse the new expression after the optional :: has
2260/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
2261/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002262///
2263/// new-expression:
2264/// '::'[opt] 'new' new-placement[opt] new-type-id
2265/// new-initializer[opt]
2266/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2267/// new-initializer[opt]
2268///
2269/// new-placement:
2270/// '(' expression-list ')'
2271///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002272/// new-type-id:
2273/// type-specifier-seq new-declarator[opt]
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002274/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002275///
2276/// new-declarator:
2277/// ptr-operator new-declarator[opt]
2278/// direct-new-declarator
2279///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002280/// new-initializer:
2281/// '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002282/// [C++0x] braced-init-list
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002283///
John McCall60d7b3a2010-08-24 06:29:42 +00002284ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002285Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
2286 assert(Tok.is(tok::kw_new) && "expected 'new' token");
2287 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002288
2289 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
2290 // second form of new-expression. It can't be a new-type-id.
2291
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002292 ExprVector PlacementArgs;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002293 SourceLocation PlacementLParen, PlacementRParen;
2294
Douglas Gregor4bd40312010-07-13 15:54:32 +00002295 SourceRange TypeIdParens;
John McCall0b7e6782011-03-24 11:26:52 +00002296 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis0b8c98f2011-06-28 03:01:23 +00002297 Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002298 if (Tok.is(tok::l_paren)) {
2299 // If it turns out to be a placement, we change the type location.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002300 BalancedDelimiterTracker T(*this, tok::l_paren);
2301 T.consumeOpen();
2302 PlacementLParen = T.getOpenLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002303 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
2304 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002305 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002306 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002307
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002308 T.consumeClose();
2309 PlacementRParen = T.getCloseLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002310 if (PlacementRParen.isInvalid()) {
2311 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002312 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002313 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002314
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002315 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002316 // Reset the placement locations. There was no placement.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002317 TypeIdParens = T.getRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002318 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002319 } else {
2320 // We still need the type.
2321 if (Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002322 BalancedDelimiterTracker T(*this, tok::l_paren);
2323 T.consumeOpen();
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002324 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002325 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002326 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002327 ParseDeclarator(DeclaratorInfo);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002328 T.consumeClose();
2329 TypeIdParens = T.getRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002330 } else {
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002331 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002332 if (ParseCXXTypeSpecifierSeq(DS))
2333 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002334 else {
2335 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002336 ParseDeclaratorInternal(DeclaratorInfo,
2337 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002338 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002339 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002340 }
2341 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002342 // A new-type-id is a simplified type-id, where essentially the
2343 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002344 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002345 if (ParseCXXTypeSpecifierSeq(DS))
2346 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002347 else {
2348 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002349 ParseDeclaratorInternal(DeclaratorInfo,
2350 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002351 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002352 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00002353 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002354 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002355 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002356 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002357
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002358 ExprResult Initializer;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002359
2360 if (Tok.is(tok::l_paren)) {
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002361 SourceLocation ConstructorLParen, ConstructorRParen;
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002362 ExprVector ConstructorArgs;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002363 BalancedDelimiterTracker T(*this, tok::l_paren);
2364 T.consumeOpen();
2365 ConstructorLParen = T.getOpenLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002366 if (Tok.isNot(tok::r_paren)) {
2367 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002368 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
2369 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002370 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002371 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002372 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002373 T.consumeClose();
2374 ConstructorRParen = T.getCloseLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002375 if (ConstructorRParen.isInvalid()) {
2376 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002377 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002378 }
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002379 Initializer = Actions.ActOnParenListExpr(ConstructorLParen,
2380 ConstructorRParen,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002381 ConstructorArgs);
Richard Smith80ad52f2013-01-02 11:42:31 +00002382 } else if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus11) {
Richard Smith7fe62082011-10-15 05:09:34 +00002383 Diag(Tok.getLocation(),
2384 diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002385 Initializer = ParseBraceInitializer();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002386 }
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002387 if (Initializer.isInvalid())
2388 return Initializer;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002389
Sebastian Redlf53597f2009-03-15 17:47:39 +00002390 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002391 PlacementArgs, PlacementRParen,
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002392 TypeIdParens, DeclaratorInfo, Initializer.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002393}
2394
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002395/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
2396/// passed to ParseDeclaratorInternal.
2397///
2398/// direct-new-declarator:
2399/// '[' expression ']'
2400/// direct-new-declarator '[' constant-expression ']'
2401///
Chris Lattner59232d32009-01-04 21:25:24 +00002402void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002403 // Parse the array dimensions.
2404 bool first = true;
2405 while (Tok.is(tok::l_square)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00002406 // An array-size expression can't start with a lambda.
2407 if (CheckProhibitedCXX11Attribute())
2408 continue;
2409
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002410 BalancedDelimiterTracker T(*this, tok::l_square);
2411 T.consumeOpen();
2412
John McCall60d7b3a2010-08-24 06:29:42 +00002413 ExprResult Size(first ? ParseExpression()
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002414 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002415 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002416 // Recover
2417 SkipUntil(tok::r_square);
2418 return;
2419 }
2420 first = false;
2421
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002422 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00002423
Bill Wendlingad017fa2012-12-20 19:22:21 +00002424 // Attributes here appertain to the array type. C++11 [expr.new]p5.
Richard Smith6ee326a2012-04-10 01:32:12 +00002425 ParsedAttributes Attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00002426 MaybeParseCXX11Attributes(Attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00002427
John McCall0b7e6782011-03-24 11:26:52 +00002428 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall7f040a92010-12-24 02:08:15 +00002429 /*static=*/false, /*star=*/false,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002430 Size.release(),
2431 T.getOpenLocation(),
2432 T.getCloseLocation()),
Richard Smith6ee326a2012-04-10 01:32:12 +00002433 Attrs, T.getCloseLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002434
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002435 if (T.getCloseLocation().isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002436 return;
2437 }
2438}
2439
2440/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
2441/// This ambiguity appears in the syntax of the C++ new operator.
2442///
2443/// new-expression:
2444/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2445/// new-initializer[opt]
2446///
2447/// new-placement:
2448/// '(' expression-list ')'
2449///
John McCallca0408f2010-08-23 06:44:23 +00002450bool Parser::ParseExpressionListOrTypeId(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002451 SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00002452 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002453 // The '(' was already consumed.
2454 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002455 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00002456 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002457 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00002458 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002459 }
2460
2461 // It's not a type, it has to be an expression list.
2462 // Discard the comma locations - ActOnCXXNew has enough parameters.
2463 CommaLocsTy CommaLocs;
2464 return ParseExpressionList(PlacementArgs, CommaLocs);
2465}
2466
2467/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
2468/// to free memory allocated by new.
2469///
Chris Lattner59232d32009-01-04 21:25:24 +00002470/// This method is called to parse the 'delete' expression after the optional
2471/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
2472/// and "Start" is its location. Otherwise, "Start" is the location of the
2473/// 'delete' token.
2474///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002475/// delete-expression:
2476/// '::'[opt] 'delete' cast-expression
2477/// '::'[opt] 'delete' '[' ']' cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00002478ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002479Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
2480 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
2481 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002482
2483 // Array delete?
2484 bool ArrayDelete = false;
Richard Smith6ee326a2012-04-10 01:32:12 +00002485 if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
Richard Smith950435c2012-08-09 19:01:51 +00002486 // C++11 [expr.delete]p1:
2487 // Whenever the delete keyword is followed by empty square brackets, it
2488 // shall be interpreted as [array delete].
2489 // [Footnote: A lambda expression with a lambda-introducer that consists
2490 // of empty square brackets can follow the delete keyword if
2491 // the lambda expression is enclosed in parentheses.]
2492 // FIXME: Produce a better diagnostic if the '[]' is unambiguously a
2493 // lambda-introducer.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002494 ArrayDelete = true;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002495 BalancedDelimiterTracker T(*this, tok::l_square);
2496
2497 T.consumeOpen();
2498 T.consumeClose();
2499 if (T.getCloseLocation().isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00002500 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002501 }
2502
John McCall60d7b3a2010-08-24 06:29:42 +00002503 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002504 if (Operand.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002505 return Operand;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002506
John McCall9ae2f072010-08-23 23:25:46 +00002507 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002508}
Sebastian Redl64b45f72009-01-05 20:52:13 +00002509
Mike Stump1eb44332009-09-09 15:08:12 +00002510static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002511 switch(kind) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002512 default: llvm_unreachable("Not a known unary type trait.");
Sebastian Redl64b45f72009-01-05 20:52:13 +00002513 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
Joao Matos9ef98752013-03-27 01:34:16 +00002514 case tok::kw___has_nothrow_move_assign: return UTT_HasNothrowMoveAssign;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002515 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00002516 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002517 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
Joao Matos9ef98752013-03-27 01:34:16 +00002518 case tok::kw___has_trivial_move_assign: return UTT_HasTrivialMoveAssign;
Sean Hunt023df372011-05-09 18:22:59 +00002519 case tok::kw___has_trivial_constructor:
2520 return UTT_HasTrivialDefaultConstructor;
Joao Matos9ef98752013-03-27 01:34:16 +00002521 case tok::kw___has_trivial_move_constructor:
2522 return UTT_HasTrivialMoveConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00002523 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002524 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
2525 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
2526 case tok::kw___is_abstract: return UTT_IsAbstract;
John Wiegley20c0da72011-04-27 23:09:49 +00002527 case tok::kw___is_arithmetic: return UTT_IsArithmetic;
2528 case tok::kw___is_array: return UTT_IsArray;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002529 case tok::kw___is_class: return UTT_IsClass;
John Wiegley20c0da72011-04-27 23:09:49 +00002530 case tok::kw___is_complete_type: return UTT_IsCompleteType;
2531 case tok::kw___is_compound: return UTT_IsCompound;
2532 case tok::kw___is_const: return UTT_IsConst;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002533 case tok::kw___is_empty: return UTT_IsEmpty;
2534 case tok::kw___is_enum: return UTT_IsEnum;
Douglas Gregor5e9392b2011-12-03 18:14:24 +00002535 case tok::kw___is_final: return UTT_IsFinal;
John Wiegley20c0da72011-04-27 23:09:49 +00002536 case tok::kw___is_floating_point: return UTT_IsFloatingPoint;
2537 case tok::kw___is_function: return UTT_IsFunction;
2538 case tok::kw___is_fundamental: return UTT_IsFundamental;
2539 case tok::kw___is_integral: return UTT_IsIntegral;
John McCallea30e2f2012-09-25 07:32:49 +00002540 case tok::kw___is_interface_class: return UTT_IsInterfaceClass;
John Wiegley20c0da72011-04-27 23:09:49 +00002541 case tok::kw___is_lvalue_reference: return UTT_IsLvalueReference;
2542 case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
2543 case tok::kw___is_member_object_pointer: return UTT_IsMemberObjectPointer;
2544 case tok::kw___is_member_pointer: return UTT_IsMemberPointer;
2545 case tok::kw___is_object: return UTT_IsObject;
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00002546 case tok::kw___is_literal: return UTT_IsLiteral;
Chandler Carruth38402812011-04-24 02:49:28 +00002547 case tok::kw___is_literal_type: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002548 case tok::kw___is_pod: return UTT_IsPOD;
John Wiegley20c0da72011-04-27 23:09:49 +00002549 case tok::kw___is_pointer: return UTT_IsPointer;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002550 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
John Wiegley20c0da72011-04-27 23:09:49 +00002551 case tok::kw___is_reference: return UTT_IsReference;
John Wiegley20c0da72011-04-27 23:09:49 +00002552 case tok::kw___is_rvalue_reference: return UTT_IsRvalueReference;
2553 case tok::kw___is_scalar: return UTT_IsScalar;
2554 case tok::kw___is_signed: return UTT_IsSigned;
2555 case tok::kw___is_standard_layout: return UTT_IsStandardLayout;
2556 case tok::kw___is_trivial: return UTT_IsTrivial;
Sean Huntfeb375d2011-05-13 00:31:07 +00002557 case tok::kw___is_trivially_copyable: return UTT_IsTriviallyCopyable;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002558 case tok::kw___is_union: return UTT_IsUnion;
John Wiegley20c0da72011-04-27 23:09:49 +00002559 case tok::kw___is_unsigned: return UTT_IsUnsigned;
2560 case tok::kw___is_void: return UTT_IsVoid;
2561 case tok::kw___is_volatile: return UTT_IsVolatile;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002562 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00002563}
2564
2565static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
2566 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00002567 default: llvm_unreachable("Not a known binary type trait");
Francois Pichetf1872372010-12-08 22:35:30 +00002568 case tok::kw___is_base_of: return BTT_IsBaseOf;
John Wiegley20c0da72011-04-27 23:09:49 +00002569 case tok::kw___is_convertible: return BTT_IsConvertible;
2570 case tok::kw___is_same: return BTT_IsSame;
Francois Pichetf1872372010-12-08 22:35:30 +00002571 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor9f361132011-01-27 20:28:01 +00002572 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002573 case tok::kw___is_trivially_assignable: return BTT_IsTriviallyAssignable;
Francois Pichet6ad6f282010-12-07 00:08:36 +00002574 }
Sebastian Redl64b45f72009-01-05 20:52:13 +00002575}
2576
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002577static TypeTrait TypeTraitFromTokKind(tok::TokenKind kind) {
2578 switch (kind) {
2579 default: llvm_unreachable("Not a known type trait");
2580 case tok::kw___is_trivially_constructible:
2581 return TT_IsTriviallyConstructible;
2582 }
2583}
2584
John Wiegley21ff2e52011-04-28 00:16:57 +00002585static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
2586 switch(kind) {
2587 default: llvm_unreachable("Not a known binary type trait");
2588 case tok::kw___array_rank: return ATT_ArrayRank;
2589 case tok::kw___array_extent: return ATT_ArrayExtent;
2590 }
2591}
2592
John Wiegley55262202011-04-25 06:54:41 +00002593static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2594 switch(kind) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002595 default: llvm_unreachable("Not a known unary expression trait.");
John Wiegley55262202011-04-25 06:54:41 +00002596 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
2597 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
2598 }
2599}
2600
Sebastian Redl64b45f72009-01-05 20:52:13 +00002601/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
2602/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2603/// templates.
2604///
2605/// primary-expression:
2606/// [GNU] unary-type-trait '(' type-id ')'
2607///
John McCall60d7b3a2010-08-24 06:29:42 +00002608ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002609 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
2610 SourceLocation Loc = ConsumeToken();
2611
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002612 BalancedDelimiterTracker T(*this, tok::l_paren);
2613 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redl64b45f72009-01-05 20:52:13 +00002614 return ExprError();
2615
2616 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
2617 // there will be cryptic errors about mismatched parentheses and missing
2618 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00002619 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002620
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002621 T.consumeClose();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002622
Douglas Gregor809070a2009-02-18 17:45:20 +00002623 if (Ty.isInvalid())
2624 return ExprError();
2625
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002626 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), T.getCloseLocation());
Sebastian Redl64b45f72009-01-05 20:52:13 +00002627}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002628
Francois Pichet6ad6f282010-12-07 00:08:36 +00002629/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
2630/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2631/// templates.
2632///
2633/// primary-expression:
2634/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
2635///
2636ExprResult Parser::ParseBinaryTypeTrait() {
2637 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
2638 SourceLocation Loc = ConsumeToken();
2639
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002640 BalancedDelimiterTracker T(*this, tok::l_paren);
2641 if (T.expectAndConsume(diag::err_expected_lparen))
Francois Pichet6ad6f282010-12-07 00:08:36 +00002642 return ExprError();
2643
2644 TypeResult LhsTy = ParseTypeName();
2645 if (LhsTy.isInvalid()) {
2646 SkipUntil(tok::r_paren);
2647 return ExprError();
2648 }
2649
2650 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2651 SkipUntil(tok::r_paren);
2652 return ExprError();
2653 }
2654
2655 TypeResult RhsTy = ParseTypeName();
2656 if (RhsTy.isInvalid()) {
2657 SkipUntil(tok::r_paren);
2658 return ExprError();
2659 }
2660
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002661 T.consumeClose();
Francois Pichet6ad6f282010-12-07 00:08:36 +00002662
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002663 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(),
2664 T.getCloseLocation());
Francois Pichet6ad6f282010-12-07 00:08:36 +00002665}
2666
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002667/// \brief Parse the built-in type-trait pseudo-functions that allow
2668/// implementation of the TR1/C++11 type traits templates.
2669///
2670/// primary-expression:
2671/// type-trait '(' type-id-seq ')'
2672///
2673/// type-id-seq:
2674/// type-id ...[opt] type-id-seq[opt]
2675///
2676ExprResult Parser::ParseTypeTrait() {
2677 TypeTrait Kind = TypeTraitFromTokKind(Tok.getKind());
2678 SourceLocation Loc = ConsumeToken();
2679
2680 BalancedDelimiterTracker Parens(*this, tok::l_paren);
2681 if (Parens.expectAndConsume(diag::err_expected_lparen))
2682 return ExprError();
2683
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002684 SmallVector<ParsedType, 2> Args;
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002685 do {
2686 // Parse the next type.
2687 TypeResult Ty = ParseTypeName();
2688 if (Ty.isInvalid()) {
2689 Parens.skipToEnd();
2690 return ExprError();
2691 }
2692
2693 // Parse the ellipsis, if present.
2694 if (Tok.is(tok::ellipsis)) {
2695 Ty = Actions.ActOnPackExpansion(Ty.get(), ConsumeToken());
2696 if (Ty.isInvalid()) {
2697 Parens.skipToEnd();
2698 return ExprError();
2699 }
2700 }
2701
2702 // Add this type to the list of arguments.
2703 Args.push_back(Ty.get());
2704
2705 if (Tok.is(tok::comma)) {
2706 ConsumeToken();
2707 continue;
2708 }
2709
2710 break;
2711 } while (true);
2712
2713 if (Parens.consumeClose())
2714 return ExprError();
2715
2716 return Actions.ActOnTypeTrait(Kind, Loc, Args, Parens.getCloseLocation());
2717}
2718
John Wiegley21ff2e52011-04-28 00:16:57 +00002719/// ParseArrayTypeTrait - Parse the built-in array type-trait
2720/// pseudo-functions.
2721///
2722/// primary-expression:
2723/// [Embarcadero] '__array_rank' '(' type-id ')'
2724/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
2725///
2726ExprResult Parser::ParseArrayTypeTrait() {
2727 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
2728 SourceLocation Loc = ConsumeToken();
2729
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002730 BalancedDelimiterTracker T(*this, tok::l_paren);
2731 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley21ff2e52011-04-28 00:16:57 +00002732 return ExprError();
2733
2734 TypeResult Ty = ParseTypeName();
2735 if (Ty.isInvalid()) {
2736 SkipUntil(tok::comma);
2737 SkipUntil(tok::r_paren);
2738 return ExprError();
2739 }
2740
2741 switch (ATT) {
2742 case ATT_ArrayRank: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002743 T.consumeClose();
2744 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL,
2745 T.getCloseLocation());
John Wiegley21ff2e52011-04-28 00:16:57 +00002746 }
2747 case ATT_ArrayExtent: {
2748 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2749 SkipUntil(tok::r_paren);
2750 return ExprError();
2751 }
2752
2753 ExprResult DimExpr = ParseExpression();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002754 T.consumeClose();
John Wiegley21ff2e52011-04-28 00:16:57 +00002755
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002756 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(),
2757 T.getCloseLocation());
John Wiegley21ff2e52011-04-28 00:16:57 +00002758 }
John Wiegley21ff2e52011-04-28 00:16:57 +00002759 }
David Blaikie30263482012-01-20 21:50:17 +00002760 llvm_unreachable("Invalid ArrayTypeTrait!");
John Wiegley21ff2e52011-04-28 00:16:57 +00002761}
2762
John Wiegley55262202011-04-25 06:54:41 +00002763/// ParseExpressionTrait - Parse built-in expression-trait
2764/// pseudo-functions like __is_lvalue_expr( xxx ).
2765///
2766/// primary-expression:
2767/// [Embarcadero] expression-trait '(' expression ')'
2768///
2769ExprResult Parser::ParseExpressionTrait() {
2770 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2771 SourceLocation Loc = ConsumeToken();
2772
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002773 BalancedDelimiterTracker T(*this, tok::l_paren);
2774 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley55262202011-04-25 06:54:41 +00002775 return ExprError();
2776
2777 ExprResult Expr = ParseExpression();
2778
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002779 T.consumeClose();
John Wiegley55262202011-04-25 06:54:41 +00002780
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002781 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(),
2782 T.getCloseLocation());
John Wiegley55262202011-04-25 06:54:41 +00002783}
2784
2785
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002786/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2787/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2788/// based on the context past the parens.
John McCall60d7b3a2010-08-24 06:29:42 +00002789ExprResult
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002790Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallb3d87482010-08-24 05:47:05 +00002791 ParsedType &CastTy,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002792 BalancedDelimiterTracker &Tracker) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002793 assert(getLangOpts().CPlusPlus && "Should only be called for C++!");
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002794 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2795 assert(isTypeIdInParens() && "Not a type-id!");
2796
John McCall60d7b3a2010-08-24 06:29:42 +00002797 ExprResult Result(true);
John McCallb3d87482010-08-24 05:47:05 +00002798 CastTy = ParsedType();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002799
2800 // We need to disambiguate a very ugly part of the C++ syntax:
2801 //
2802 // (T())x; - type-id
2803 // (T())*x; - type-id
2804 // (T())/x; - expression
2805 // (T()); - expression
2806 //
2807 // The bad news is that we cannot use the specialized tentative parser, since
2808 // it can only verify that the thing inside the parens can be parsed as
2809 // type-id, it is not useful for determining the context past the parens.
2810 //
2811 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00002812 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002813 //
2814 // It uses a scheme similar to parsing inline methods. The parenthesized
2815 // tokens are cached, the context that follows is determined (possibly by
2816 // parsing a cast-expression), and then we re-introduce the cached tokens
2817 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002818
Mike Stump1eb44332009-09-09 15:08:12 +00002819 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002820 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002821
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002822 // Store the tokens of the parentheses. We will parse them after we determine
2823 // the context that follows them.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00002824 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002825 // We didn't find the ')' we expected.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002826 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002827 return ExprError();
2828 }
2829
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002830 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002831 ParseAs = CompoundLiteral;
2832 } else {
2833 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002834 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2835 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2836 NotCastExpr = true;
2837 } else {
2838 // Try parsing the cast-expression that may follow.
2839 // If it is not a cast-expression, NotCastExpr will be true and no token
2840 // will be consumed.
2841 Result = ParseCastExpression(false/*isUnaryExpression*/,
2842 false/*isAddressofOperand*/,
John McCallb3d87482010-08-24 05:47:05 +00002843 NotCastExpr,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002844 // type-id has priority.
Kaelyn Uhraincd78e612012-01-25 20:49:08 +00002845 IsTypeCast);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002846 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002847
2848 // If we parsed a cast-expression, it's really a type-id, otherwise it's
2849 // an expression.
2850 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002851 }
2852
Mike Stump1eb44332009-09-09 15:08:12 +00002853 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002854 Toks.push_back(Tok);
2855 // Re-enter the stored parenthesized tokens into the token stream, so we may
2856 // parse them now.
2857 PP.EnterTokenStream(Toks.data(), Toks.size(),
2858 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2859 // Drop the current token and bring the first cached one. It's the same token
2860 // as when we entered this function.
2861 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002862
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002863 if (ParseAs >= CompoundLiteral) {
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002864 // Parse the type declarator.
2865 DeclSpec DS(AttrFactory);
2866 ParseSpecifierQualifierList(DS);
2867 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2868 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002869
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002870 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002871 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002872
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002873 if (ParseAs == CompoundLiteral) {
2874 ExprType = CompoundLiteral;
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002875 TypeResult Ty = ParseTypeName();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002876 return ParseCompoundLiteralExpression(Ty.get(),
2877 Tracker.getOpenLocation(),
2878 Tracker.getCloseLocation());
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002879 }
Mike Stump1eb44332009-09-09 15:08:12 +00002880
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002881 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2882 assert(ParseAs == CastExpr);
2883
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002884 if (DeclaratorInfo.isInvalidType())
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002885 return ExprError();
2886
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002887 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002888 if (!Result.isInvalid())
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002889 Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(),
2890 DeclaratorInfo, CastTy,
2891 Tracker.getCloseLocation(), Result.take());
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002892 return Result;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002893 }
Mike Stump1eb44332009-09-09 15:08:12 +00002894
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002895 // Not a compound literal, and not followed by a cast-expression.
2896 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002897
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002898 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002899 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002900 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002901 Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(),
2902 Tok.getLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002903
2904 // Match the ')'.
2905 if (Result.isInvalid()) {
2906 SkipUntil(tok::r_paren);
2907 return ExprError();
2908 }
Mike Stump1eb44332009-09-09 15:08:12 +00002909
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002910 Tracker.consumeClose();
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002911 return Result;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002912}