blob: 181deb6cfc42a25f28ad2ca36fc0371b70bd6f0d [file] [log] [blame]
Chris Lattner29375652006-12-04 18:06:35 +00001//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner29375652006-12-04 18:06:35 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation for C++.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner29375652006-12-04 18:06:35 +000014#include "clang/Parse/Parser.h"
Douglas Gregor94a32472011-01-11 00:33:19 +000015#include "RAIIObjectsForParser.h"
Eli Friedmanc7c97142012-01-04 02:40:39 +000016#include "clang/Basic/PrettyStackTrace.h"
Richard Smith7d182a72012-03-08 23:06:02 +000017#include "clang/Lex/LiteralSupport.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/Parse/ParseDiagnostic.h"
John McCall8b0666c2010-08-20 18:27:03 +000019#include "clang/Sema/DeclSpec.h"
20#include "clang/Sema/ParsedTemplate.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/Sema/Scope.h"
Douglas Gregor7861a802009-11-03 01:35:08 +000022#include "llvm/Support/ErrorHandling.h"
23
Chris Lattner29375652006-12-04 18:06:35 +000024using namespace clang;
25
Richard Smith55858492011-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 Blaikie83d382b2011-09-23 05:06:16 +000034 llvm_unreachable("Unknown type for digraph error message.");
Richard Smith55858492011-04-14 21:45:45 +000035 }
36}
37
38// Are the two tokens adjacent in the same source file?
Richard Smith7b3f3222012-06-18 06:11:04 +000039bool Parser::areTokensAdjacent(const Token &First, const Token &Second) {
Richard Smith55858492011-04-14 21:45:45 +000040 SourceManager &SM = PP.getSourceManager();
41 SourceLocation FirstLoc = SM.getSpellingLoc(First.getLocation());
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +000042 SourceLocation FirstEnd = FirstLoc.getLocWithOffset(First.getLength());
Richard Smith55858492011-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 Kyrtzidise6e67de2011-09-19 20:40:19 +000063 ColonToken.setLocation(ColonToken.getLocation().getLocWithOffset(-1));
Richard Smith55858492011-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 Trieu01fc0012011-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 Trieu02e25db2011-09-20 20:03:50 +000079 if (!Next.is(tok::l_square) || Next.getLength() != 2)
Richard Trieu01fc0012011-09-19 19:01:00 +000080 return;
81
82 Token SecondToken = GetLookAheadToken(2);
Richard Smith7b3f3222012-06-18 06:11:04 +000083 if (!SecondToken.is(tok::colon) || !areTokensAdjacent(Next, SecondToken))
Richard Trieu01fc0012011-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 Trieu1f3ea7b2012-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 Weber6be9b252012-11-29 05:29:23 +0000102/// stream by removing the '(', and the matching ')' if found.
Richard Trieu1f3ea7b2012-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 Stump11289f42009-09-09 15:08:12 +0000138/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000139///
140/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump11289f42009-09-09 15:08:12 +0000141/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000142/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidis32a03792008-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 Gregorb7bfe792009-09-02 22:59:36 +0000151/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000152///
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000153///
Mike Stump11289f42009-09-09 15:08:12 +0000154/// \param SS the scope specifier that will be set to the parsed
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000155/// nested-name-specifier (or empty)
156///
Mike Stump11289f42009-09-09 15:08:12 +0000157/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregorb7bfe792009-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 Gregore610ada2010-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 Smith7447af42013-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 Gregor90d554e2010-02-21 18:36:56 +0000178///
John McCall1f476a12010-02-26 08:45:28 +0000179/// \returns true if there was an error parsing a scope specifier
Douglas Gregore861bac2009-08-25 22:51:20 +0000180bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
John McCallba7bf592010-08-24 05:47:05 +0000181 ParsedType ObjectType,
Douglas Gregor90d554e2010-02-21 18:36:56 +0000182 bool EnteringContext,
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000183 bool *MayBePseudoDestructor,
Richard Smith7447af42013-03-26 01:15:19 +0000184 bool IsTypename,
185 IdentifierInfo **LastII) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000186 assert(getLangOpts().CPlusPlus &&
Chris Lattnerb5134c02009-01-05 01:24:05 +0000187 "Call sites of this function should be guarded by checking for C++");
Mike Stump11289f42009-09-09 15:08:12 +0000188
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000189 if (Tok.is(tok::annot_cxxscope)) {
Richard Smith7447af42013-03-26 01:15:19 +0000190 assert(!LastII && "want last identifier but have already annotated scope");
Douglas Gregor869ad452011-02-24 17:54:50 +0000191 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
192 Tok.getAnnotationRange(),
193 SS);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000194 ConsumeToken();
John McCall1f476a12010-02-26 08:45:28 +0000195 return false;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000196 }
Chris Lattnerf9b2cd42009-01-04 21:14:15 +0000197
Richard Smith7447af42013-03-26 01:15:19 +0000198 if (LastII)
199 *LastII = 0;
200
Douglas Gregor7f741122009-02-25 19:37:18 +0000201 bool HasScopeSpecifier = false;
202
Chris Lattner8a7d10d2009-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 Stump11289f42009-09-09 15:08:12 +0000208
Chris Lattner45ddec32009-01-05 00:13:00 +0000209 // '::' - Global scope qualifier.
Douglas Gregor90c99722011-02-24 00:17:56 +0000210 if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
211 return true;
Richard Trieu1f3ea7b2012-11-02 01:08:58 +0000212
213 CheckForLParenAfterColonColon();
214
Douglas Gregor7f741122009-02-25 19:37:18 +0000215 HasScopeSpecifier = true;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000216 }
217
Douglas Gregore610ada2010-02-24 18:44:31 +0000218 bool CheckForDestructor = false;
219 if (MayBePseudoDestructor && *MayBePseudoDestructor) {
220 CheckForDestructor = true;
221 *MayBePseudoDestructor = false;
222 }
223
David Blaikie15a430a2011-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 Gregor7f741122009-02-25 19:37:18 +0000240 while (true) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000241 if (HasScopeSpecifier) {
242 // C++ [basic.lookup.classref]p5:
243 // If the qualified-id has the form
Douglas Gregor308047d2009-09-09 00:23:06 +0000244 //
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000245 // ::class-name-or-namespace-name::...
Douglas Gregor308047d2009-09-09 00:23:06 +0000246 //
Douglas Gregorb7bfe792009-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 McCallba7bf592010-08-24 05:47:05 +0000252 ObjectType = ParsedType();
Douglas Gregor2436e712009-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 Gregor0be31a22010-07-02 17:43:08 +0000257 Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
Argyrios Kyrtzidis7d94c922011-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 Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000262 SS.setEndLoc(Tok.getLocation());
263 cutOffParsing();
264 return true;
Douglas Gregor2436e712009-09-17 21:32:03 +0000265 }
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000266 }
Mike Stump11289f42009-09-09 15:08:12 +0000267
Douglas Gregor7f741122009-02-25 19:37:18 +0000268 // nested-name-specifier:
Chris Lattner0eed3a62009-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 Gregorb7bfe792009-09-02 22:59:36 +0000274 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedman2624be42009-08-29 04:08:08 +0000275 // nested-name-specifier, since they aren't allowed to start with
276 // 'template'.
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000277 if (!HasScopeSpecifier && !ObjectType)
Eli Friedman2624be42009-08-29 04:08:08 +0000278 break;
279
Douglas Gregor120635b2009-11-11 16:39:34 +0000280 TentativeParsingAction TPA(*this);
Chris Lattner0eed3a62009-06-26 03:47:46 +0000281 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregor71395fa2009-11-04 00:56:37 +0000282
283 UnqualifiedId TemplateName;
284 if (Tok.is(tok::identifier)) {
Douglas Gregor71395fa2009-11-04 00:56:37 +0000285 // Consume the identifier.
Douglas Gregor120635b2009-11-11 16:39:34 +0000286 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregor71395fa2009-11-04 00:56:37 +0000287 ConsumeToken();
288 } else if (Tok.is(tok::kw_operator)) {
289 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor120635b2009-11-11 16:39:34 +0000290 TemplateName)) {
291 TPA.Commit();
Douglas Gregor71395fa2009-11-04 00:56:37 +0000292 break;
Douglas Gregor120635b2009-11-11 16:39:34 +0000293 }
Douglas Gregor71395fa2009-11-04 00:56:37 +0000294
Alexis Hunted0530f2009-11-28 08:58:14 +0000295 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
296 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor71395fa2009-11-04 00:56:37 +0000297 Diag(TemplateName.getSourceRange().getBegin(),
298 diag::err_id_after_template_in_nested_name_spec)
299 << TemplateName.getSourceRange();
Douglas Gregor120635b2009-11-11 16:39:34 +0000300 TPA.Commit();
Douglas Gregor71395fa2009-11-04 00:56:37 +0000301 break;
302 }
303 } else {
Douglas Gregor120635b2009-11-11 16:39:34 +0000304 TPA.Revert();
Chris Lattner0eed3a62009-06-26 03:47:46 +0000305 break;
306 }
Mike Stump11289f42009-09-09 15:08:12 +0000307
Douglas Gregor120635b2009-11-11 16:39:34 +0000308 // If the next token is not '<', we have a qualified-id that refers
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 Gregorbb119652010-06-16 23:00:59 +0000318 TemplateTy Template;
Abramo Bagnara7945c982012-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 Gregorbb119652010-06-16 23:00:59 +0000326 return true;
327 } else
John McCall1f476a12010-02-26 08:45:28 +0000328 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000329
Chris Lattner0eed3a62009-06-26 03:47:46 +0000330 continue;
331 }
Mike Stump11289f42009-09-09 15:08:12 +0000332
Douglas Gregor7f741122009-02-25 19:37:18 +0000333 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump11289f42009-09-09 15:08:12 +0000334 // We have
Douglas Gregor7f741122009-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 Gregorb67535d2009-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 Kyrtzidisc0c5dd22011-06-22 06:09:49 +0000341 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregore610ada2010-02-24 18:44:31 +0000342 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
343 *MayBePseudoDestructor = true;
John McCall1f476a12010-02-26 08:45:28 +0000344 return false;
Douglas Gregore610ada2010-02-24 18:44:31 +0000345 }
346
Richard Smith7447af42013-03-26 01:15:19 +0000347 if (LastII)
348 *LastII = TemplateId->Name;
349
Douglas Gregor8b6070b2011-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 Stump11289f42009-09-09 15:08:12 +0000355
David Blaikie8c045bc2011-11-07 03:30:03 +0000356 HasScopeSpecifier = true;
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000357
Benjamin Kramercc4c49d2012-08-23 23:38:35 +0000358 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000359 TemplateId->NumArgs);
360
361 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000362 SS,
363 TemplateId->TemplateKWLoc,
Douglas Gregor8b6070b2011-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 Lattner704edfb2009-06-26 03:45:46 +0000375 }
Argyrios Kyrtzidis13935672011-05-03 18:45:38 +0000376
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000377 continue;
Douglas Gregor7f741122009-02-25 19:37:18 +0000378 }
379
Chris Lattnere2355f72009-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 Lattner1c428032009-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 Gregor90d554e2010-02-21 18:36:56 +0000396 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000397 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
398 Tok.getLocation(),
399 Next.getLocation(), ObjectType,
Douglas Gregor90d554e2010-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 Gregora771f462010-03-31 17:46:05 +0000406 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregor90d554e2010-02-21 18:36:56 +0000407
408 // Recover as if the user wrote '::'.
409 Next.setKind(tok::coloncolon);
410 }
Chris Lattner1c428032009-12-07 01:36:53 +0000411 }
412
Chris Lattnere2355f72009-06-26 03:52:38 +0000413 if (Next.is(tok::coloncolon)) {
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000414 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +0000415 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000416 II, ObjectType)) {
Douglas Gregore610ada2010-02-24 18:44:31 +0000417 *MayBePseudoDestructor = true;
John McCall1f476a12010-02-26 08:45:28 +0000418 return false;
Douglas Gregore610ada2010-02-24 18:44:31 +0000419 }
420
Richard Smith7447af42013-03-26 01:15:19 +0000421 if (LastII)
422 *LastII = &II;
423
Chris Lattnere2355f72009-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 Lattner1c428032009-12-07 01:36:53 +0000427 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
428 "NextToken() not working properly!");
Chris Lattnere2355f72009-06-26 03:52:38 +0000429 SourceLocation CCLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000430
Richard Trieu1f3ea7b2012-11-02 01:08:58 +0000431 CheckForLParenAfterColonColon();
432
Douglas Gregor90c99722011-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 Lattnere2355f72009-06-26 03:52:38 +0000438 continue;
439 }
Mike Stump11289f42009-09-09 15:08:12 +0000440
Richard Trieu01fc0012011-09-19 19:01:00 +0000441 CheckForTemplateAndDigraph(Next, ObjectType, EnteringContext, II, SS);
Richard Smith55858492011-04-14 21:45:45 +0000442
Chris Lattnere2355f72009-06-26 03:52:38 +0000443 // nested-name-specifier:
444 // type-name '<'
445 if (Next.is(tok::less)) {
446 TemplateTy Template;
Douglas Gregor3cf81312009-11-03 23:16:33 +0000447 UnqualifiedId TemplateName;
448 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor786123d2010-05-21 23:18:07 +0000449 bool MemberOfUnknownSpecialization;
Douglas Gregor0be31a22010-07-02 17:43:08 +0000450 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c5dee42010-08-06 12:11:11 +0000451 /*hasTemplateKeyword=*/false,
Douglas Gregor3cf81312009-11-03 23:16:33 +0000452 TemplateName,
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000453 ObjectType,
Douglas Gregore861bac2009-08-25 22:51:20 +0000454 EnteringContext,
Douglas Gregor786123d2010-05-21 23:18:07 +0000455 Template,
456 MemberOfUnknownSpecialization)) {
David Blaikie8c045bc2011-11-07 03:30:03 +0000457 // We have found a template name, so annotate this token
Chris Lattnere2355f72009-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 Gregor71395fa2009-11-04 00:56:37 +0000463 ConsumeToken();
Abramo Bagnara7945c982012-01-27 09:46:47 +0000464 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
465 TemplateName, false))
John McCall1f476a12010-02-26 08:45:28 +0000466 return true;
Chris Lattnere2355f72009-06-26 03:52:38 +0000467 continue;
Douglas Gregor20c38a72010-05-21 23:43:39 +0000468 }
469
470 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000471 (IsTypename || IsTemplateArgumentList(1))) {
Douglas Gregor20c38a72010-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 Pichet4e7a2c02011-03-27 19:41:34 +0000476 unsigned DiagID = diag::err_missing_dependent_template_keyword;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000477 if (getLangOpts().MicrosoftExt)
Francois Pichet93921652011-04-22 08:25:24 +0000478 DiagID = diag::warn_missing_dependent_template_keyword;
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000479
480 Diag(Tok.getLocation(), DiagID)
Douglas Gregor20c38a72010-05-21 23:43:39 +0000481 << II.getName()
482 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
483
Douglas Gregorbb119652010-06-16 23:00:59 +0000484 if (TemplateNameKind TNK
Douglas Gregor0be31a22010-07-02 17:43:08 +0000485 = Actions.ActOnDependentTemplateName(getCurScope(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000486 SS, SourceLocation(),
Douglas Gregorbb119652010-06-16 23:00:59 +0000487 TemplateName, ObjectType,
488 EnteringContext, Template)) {
489 // Consume the identifier.
490 ConsumeToken();
Abramo Bagnara7945c982012-01-27 09:46:47 +0000491 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
492 TemplateName, false))
493 return true;
Douglas Gregorbb119652010-06-16 23:00:59 +0000494 }
495 else
Douglas Gregor20c38a72010-05-21 23:43:39 +0000496 return true;
Douglas Gregorbb119652010-06-16 23:00:59 +0000497
Douglas Gregor20c38a72010-05-21 23:43:39 +0000498 continue;
Chris Lattnere2355f72009-06-26 03:52:38 +0000499 }
500 }
501
Douglas Gregor7f741122009-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 Kyrtzidis32a03792008-11-08 16:45:02 +0000505 }
Mike Stump11289f42009-09-09 15:08:12 +0000506
Douglas Gregore610ada2010-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 McCall1f476a12010-02-26 08:45:28 +0000513 return false;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000514}
515
516/// ParseCXXIdExpression - Handle id-expression.
517///
518/// id-expression:
519/// unqualified-id
520/// qualified-id
521///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000522/// qualified-id:
523/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
524/// '::' identifier
525/// '::' operator-function-id
Douglas Gregora727cb92009-06-30 22:34:41 +0000526/// '::' template-id
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000527///
Argyrios Kyrtzidis32a03792008-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 Redl3d3f75a2009-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 McCalldadc5752010-08-24 06:29:42 +0000558ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidis32a03792008-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 Gregordf593fb2011-11-07 17:33:42 +0000564 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000565
566 SourceLocation TemplateKWLoc;
Douglas Gregora121b752009-11-03 16:56:39 +0000567 UnqualifiedId Name;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000568 if (ParseUnqualifiedId(SS,
569 /*EnteringContext=*/false,
570 /*AllowDestructorName=*/false,
571 /*AllowConstructorName=*/false,
John McCallba7bf592010-08-24 05:47:05 +0000572 /*ObjectType=*/ ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000573 TemplateKWLoc,
Douglas Gregora121b752009-11-03 16:56:39 +0000574 Name))
575 return ExprError();
John McCalla9ee3252009-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 McCall8d08b9b2010-08-27 09:08:28 +0000579 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
580 isAddressOfOperand = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000581
582 return Actions.ActOnIdExpression(getCurScope(), SS, TemplateKWLoc, Name,
583 Tok.is(tok::l_paren), isAddressOfOperand);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000584}
585
Douglas Gregordb0b9f12011-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 Blaikie05785d12013-02-20 22:23:23 +0000621 Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000622 if (DiagID) {
623 Diag(Tok, DiagID.getValue());
624 SkipUntil(tok::r_square);
Eli Friedmanc7c97142012-01-04 02:40:39 +0000625 SkipUntil(tok::l_brace);
626 SkipUntil(tok::r_brace);
627 return ExprError();
Douglas Gregordb0b9f12011-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 Smith2bf7fdb2013-01-02 11:42:31 +0000639 assert(getLangOpts().CPlusPlus11
Douglas Gregordb0b9f12011-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 Friedmanc7c97142012-01-04 02:40:39 +0000656 // If lookahead indicates an ObjC message send...
657 // [identifier identifier
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000658 if (Next.is(tok::identifier) && After.is(tok::identifier)) {
Eli Friedmanc7c97142012-01-04 02:40:39 +0000659 return ExprEmpty();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000660 }
661
Eli Friedmanc7c97142012-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 Gregordb0b9f12011-08-04 15:30:47 +0000668 LambdaIntroducer Intro;
669 if (TryParseLambdaIntroducer(Intro))
Eli Friedmanc7c97142012-01-04 02:40:39 +0000670 return ExprEmpty();
Douglas Gregordb0b9f12011-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 Blaikie05785d12013-02-20 22:23:23 +0000677Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro) {
678 typedef Optional<unsigned> DiagResult;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000679
680 assert(Tok.is(tok::l_square) && "Lambda expressions begin with '['.");
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000681 BalancedDelimiterTracker T(*this, tok::l_square);
682 T.consumeOpen();
683
684 Intro.Range.setBegin(T.getOpenLocation());
Douglas Gregordb0b9f12011-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 Gregora1bffa22012-02-10 17:46:20 +0000692 Intro.DefaultLoc = ConsumeToken();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000693 first = false;
694 } else if (Tok.is(tok::equal)) {
695 Intro.Default = LCD_ByCopy;
Douglas Gregora1bffa22012-02-10 17:46:20 +0000696 Intro.DefaultLoc = ConsumeToken();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000697 first = false;
698 }
699
700 while (Tok.isNot(tok::r_square)) {
701 if (!first) {
Douglas Gregord8c61782012-02-15 15:34:24 +0000702 if (Tok.isNot(tok::comma)) {
Douglas Gregor721b14d2012-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 Gregor2d8db8f2012-07-31 15:27:48 +0000707 if (Tok.is(tok::code_completion) &&
708 !(getLangOpts().ObjC1 && Intro.Default == LCD_None &&
709 !Intro.Captures.empty())) {
Douglas Gregord8c61782012-02-15 15:34:24 +0000710 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
711 /*AfterAmpersand=*/false);
712 ConsumeCodeCompletionToken();
713 break;
714 }
715
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000716 return DiagResult(diag::err_expected_comma_or_rsquare);
Douglas Gregord8c61782012-02-15 15:34:24 +0000717 }
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000718 ConsumeToken();
719 }
720
Douglas Gregord8c61782012-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 Blaikiebbafb8a2012-03-11 07:00:24 +0000724 if (getLangOpts().ObjC1 && first)
Douglas Gregord8c61782012-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 Gregordb0b9f12011-08-04 15:30:47 +0000732
Douglas Gregord8c61782012-02-15 15:34:24 +0000733 first = false;
734
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000735 // Parse capture.
736 LambdaCaptureKind Kind = LCK_ByCopy;
737 SourceLocation Loc;
738 IdentifierInfo* Id = 0;
Douglas Gregor3e308b12012-02-14 19:27:52 +0000739 SourceLocation EllipsisLoc;
740
Douglas Gregordb0b9f12011-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 Gregord8c61782012-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 Gregordb0b9f12011-08-04 15:30:47 +0000755 }
756
757 if (Tok.is(tok::identifier)) {
758 Id = Tok.getIdentifierInfo();
759 Loc = ConsumeToken();
Douglas Gregor3e308b12012-02-14 19:27:52 +0000760
761 if (Tok.is(tok::ellipsis))
762 EllipsisLoc = ConsumeToken();
Douglas Gregordb0b9f12011-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 Gregor3e308b12012-02-14 19:27:52 +0000773 Intro.addCapture(Kind, Loc, Id, EllipsisLoc);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000774 }
775
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000776 T.consumeClose();
777 Intro.Range.setEnd(T.getCloseLocation());
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000778
779 return DiagResult();
780}
781
Douglas Gregord8c61782012-02-15 15:34:24 +0000782/// TryParseLambdaIntroducer - Tentatively parse a lambda introducer.
Douglas Gregordb0b9f12011-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 Blaikie05785d12013-02-20 22:23:23 +0000788 Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
Douglas Gregordb0b9f12011-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 Friedmanc7c97142012-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 Gregordb0b9f12011-08-04 15:30:47 +0000809 // Parse lambda-declarator[opt].
810 DeclSpec DS(AttrFactory);
Eli Friedman36d12942012-01-04 04:41:38 +0000811 Declarator D(DS, Declarator::LambdaExprContext);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000812
813 if (Tok.is(tok::l_paren)) {
814 ParseScope PrototypeScope(this,
815 Scope::FunctionPrototypeScope |
Richard Smithe233fbf2013-01-28 22:42:45 +0000816 Scope::FunctionDeclarationScope |
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000817 Scope::DeclScope);
818
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000819 SourceLocation DeclEndLoc;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000820 BalancedDelimiterTracker T(*this, tok::l_paren);
821 T.consumeOpen();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000822 SourceLocation LParenLoc = T.getOpenLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000823
824 // Parse parameter-declaration-clause.
825 ParsedAttributes Attr(AttrFactory);
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000826 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000827 SourceLocation EllipsisLoc;
828
829 if (Tok.isNot(tok::r_paren))
830 ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc);
831
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000832 T.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000833 SourceLocation RParenLoc = T.getCloseLocation();
834 DeclEndLoc = RParenLoc;
Douglas Gregordb0b9f12011-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 Gribenkof8579502013-01-12 19:30:44 +0000846 SmallVector<ParsedType, 2> DynamicExceptions;
847 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000848 ExprResult NoexceptExpr;
Richard Smith2331bbf2012-05-02 22:22:32 +0000849 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +0000850 DynamicExceptions,
851 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +0000852 NoexceptExpr);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000853
854 if (ESpecType != EST_None)
855 DeclEndLoc = ESpecRange.getEnd();
856
857 // Parse attribute-specifier[opt].
Richard Smith89645bc2013-01-02 12:01:23 +0000858 MaybeParseCXX11Attributes(Attr, &DeclEndLoc);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000859
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000860 SourceLocation FunLocalRangeEnd = DeclEndLoc;
861
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000862 // Parse trailing-return-type[opt].
Richard Smith700537c2012-06-12 01:51:59 +0000863 TypeResult TrailingReturnType;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000864 if (Tok.is(tok::arrow)) {
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000865 FunLocalRangeEnd = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000866 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +0000867 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000868 if (Range.getEnd().isValid())
869 DeclEndLoc = Range.getEnd();
870 }
871
872 PrototypeScope.Exit();
873
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000874 SourceLocation NoLoc;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000875 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000876 /*isAmbiguous=*/false,
877 LParenLoc,
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000878 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000879 EllipsisLoc, RParenLoc,
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000880 DS.getTypeQualifiers(),
881 /*RefQualifierIsLValueRef=*/true,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000882 /*RefQualifierLoc=*/NoLoc,
883 /*ConstQualifierLoc=*/NoLoc,
884 /*VolatileQualifierLoc=*/NoLoc,
Douglas Gregordb0b9f12011-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 Bagnaraaeeb9892012-10-04 21:42:10 +0000892 LParenLoc, FunLocalRangeEnd, D,
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000893 TrailingReturnType),
894 Attr, DeclEndLoc);
Douglas Gregor6746c5d2012-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 Smith700537c2012-06-12 01:51:59 +0000912 TypeResult TrailingReturnType;
Douglas Gregor6746c5d2012-02-16 21:53:36 +0000913 if (Tok.is(tok::arrow)) {
914 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +0000915 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregor6746c5d2012-02-16 21:53:36 +0000916 if (Range.getEnd().isValid())
917 DeclEndLoc = Range.getEnd();
918 }
919
920 ParsedAttributes Attr(AttrFactory);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000921 SourceLocation NoLoc;
Douglas Gregor6746c5d2012-02-16 21:53:36 +0000922 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
Abramo Bagnaraaeeb9892012-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 Gregor6746c5d2012-02-16 21:53:36 +0000943 Attr, DeclEndLoc);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000944 }
Douglas Gregor6746c5d2012-02-16 21:53:36 +0000945
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000946
Eli Friedman4817cf72012-01-06 03:05:34 +0000947 // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
948 // it.
Douglas Gregorb8389972012-02-21 22:51:27 +0000949 unsigned ScopeFlags = Scope::BlockScope | Scope::FnScope | Scope::DeclScope;
Douglas Gregorb8389972012-02-21 22:51:27 +0000950 ParseScope BodyScope(this, ScopeFlags);
Eli Friedman4817cf72012-01-06 03:05:34 +0000951
Eli Friedman71c80552012-01-05 03:35:19 +0000952 Actions.ActOnStartOfLambdaDefinition(Intro, D, getCurScope());
953
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000954 // Parse compound-statement.
Eli Friedmanc7c97142012-01-04 02:40:39 +0000955 if (!Tok.is(tok::l_brace)) {
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000956 Diag(Tok, diag::err_expected_lambda_body);
Eli Friedmanc7c97142012-01-04 02:40:39 +0000957 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
958 return ExprError();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000959 }
960
Eli Friedmanc7c97142012-01-04 02:40:39 +0000961 StmtResult Stmt(ParseCompoundStatementBody());
962 BodyScope.Exit();
963
Eli Friedman898caf82012-01-04 02:46:53 +0000964 if (!Stmt.isInvalid())
Douglas Gregor63798542012-02-20 19:44:39 +0000965 return Actions.ActOnLambdaExpr(LambdaBeginLoc, Stmt.take(), getCurScope());
Eli Friedmanc7c97142012-01-04 02:40:39 +0000966
Eli Friedman898caf82012-01-04 02:46:53 +0000967 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
968 return ExprError();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000969}
970
Chris Lattner29375652006-12-04 18:06:35 +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 McCalldadc5752010-08-24 06:29:42 +0000980ExprResult Parser::ParseCXXCasts() {
Chris Lattner29375652006-12-04 18:06:35 +0000981 tok::TokenKind Kind = Tok.getKind();
982 const char *CastName = 0; // For error messages
983
984 switch (Kind) {
David Blaikieaa347f92011-09-23 20:26:49 +0000985 default: llvm_unreachable("Unknown C++ cast!");
Chris Lattner29375652006-12-04 18:06:35 +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 Smith55858492011-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 Smith62e66302012-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 Smith55858492011-04-14 21:45:45 +00001002
Chris Lattner29375652006-12-04 18:06:35 +00001003 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redld65cea82008-12-11 22:51:44 +00001004 return ExprError();
Chris Lattner29375652006-12-04 18:06:35 +00001005
Argyrios Kyrtzidis7451d1c2011-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
Chris Lattner29375652006-12-04 18:06:35 +00001014 SourceLocation RAngleBracketLoc = Tok.getLocation();
1015
Chris Lattner6d29c102008-11-18 07:48:38 +00001016 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redld65cea82008-12-11 22:51:44 +00001017 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Chris Lattner29375652006-12-04 18:06:35 +00001018
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001019 SourceLocation LParenLoc, RParenLoc;
1020 BalancedDelimiterTracker T(*this, tok::l_paren);
Chris Lattner29375652006-12-04 18:06:35 +00001021
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001022 if (T.expectAndConsume(diag::err_expected_lparen_after, CastName))
Argyrios Kyrtzidis387a3342009-05-22 10:23:16 +00001023 return ExprError();
Chris Lattner29375652006-12-04 18:06:35 +00001024
John McCalldadc5752010-08-24 06:29:42 +00001025 ExprResult Result = ParseExpression();
Mike Stump11289f42009-09-09 15:08:12 +00001026
Argyrios Kyrtzidis387a3342009-05-22 10:23:16 +00001027 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001028 T.consumeClose();
Chris Lattner29375652006-12-04 18:06:35 +00001029
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +00001030 if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
Douglas Gregore200adc2008-10-27 19:41:14 +00001031 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +00001032 LAngleBracketLoc, DeclaratorInfo,
Douglas Gregor220cac52009-02-18 17:45:20 +00001033 RAngleBracketLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001034 T.getOpenLocation(), Result.take(),
1035 T.getCloseLocation());
Chris Lattner29375652006-12-04 18:06:35 +00001036
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001037 return Result;
Chris Lattner29375652006-12-04 18:06:35 +00001038}
Bill Wendling4073ed52007-02-13 01:51:42 +00001039
Sebastian Redlc4704762008-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 McCalldadc5752010-08-24 06:29:42 +00001046ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc4704762008-11-11 11:37:55 +00001047 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
1048
1049 SourceLocation OpLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001050 SourceLocation LParenLoc, RParenLoc;
1051 BalancedDelimiterTracker T(*this, tok::l_paren);
Sebastian Redlc4704762008-11-11 11:37:55 +00001052
1053 // typeid expressions are always parenthesized.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001054 if (T.expectAndConsume(diag::err_expected_lparen_after, "typeid"))
Sebastian Redld65cea82008-12-11 22:51:44 +00001055 return ExprError();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001056 LParenLoc = T.getOpenLocation();
Sebastian Redlc4704762008-11-11 11:37:55 +00001057
John McCalldadc5752010-08-24 06:29:42 +00001058 ExprResult Result;
Sebastian Redlc4704762008-11-11 11:37:55 +00001059
Richard Smith4f605af2012-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 Friedman15681d62012-09-26 04:34:21 +00001073 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
1074 Sema::ReuseLambdaContextDecl);
Richard Smith4f605af2012-08-18 00:55:03 +00001075
Sebastian Redlc4704762008-11-11 11:37:55 +00001076 if (isTypeIdInParens()) {
Douglas Gregor220cac52009-02-18 17:45:20 +00001077 TypeResult Ty = ParseTypeName();
Sebastian Redlc4704762008-11-11 11:37:55 +00001078
1079 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001080 T.consumeClose();
1081 RParenLoc = T.getCloseLocation();
Douglas Gregor4c7c1092010-09-08 23:14:30 +00001082 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +00001083 return ExprError();
Sebastian Redlc4704762008-11-11 11:37:55 +00001084
1085 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallba7bf592010-08-24 05:47:05 +00001086 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc4704762008-11-11 11:37:55 +00001087 } else {
1088 Result = ParseExpression();
1089
1090 // Match the ')'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001091 if (Result.isInvalid())
Sebastian Redlc4704762008-11-11 11:37:55 +00001092 SkipUntil(tok::r_paren);
1093 else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001094 T.consumeClose();
1095 RParenLoc = T.getCloseLocation();
Douglas Gregor4c7c1092010-09-08 23:14:30 +00001096 if (RParenLoc.isInvalid())
1097 return ExprError();
Douglas Gregor1beec452011-03-12 01:48:56 +00001098
Sebastian Redlc4704762008-11-11 11:37:55 +00001099 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001100 Result.release(), RParenLoc);
Sebastian Redlc4704762008-11-11 11:37:55 +00001101 }
1102 }
1103
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001104 return Result;
Sebastian Redlc4704762008-11-11 11:37:55 +00001105}
1106
Francois Pichet9f4f2072010-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 Gregore7a8e3b2011-10-12 16:37:45 +00001116 BalancedDelimiterTracker T(*this, tok::l_paren);
Francois Pichet9f4f2072010-09-08 12:20:18 +00001117
1118 // __uuidof expressions are always parenthesized.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001119 if (T.expectAndConsume(diag::err_expected_lparen_after, "__uuidof"))
Francois Pichet9f4f2072010-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 Gregore7a8e3b2011-10-12 16:37:45 +00001128 T.consumeClose();
Francois Pichet9f4f2072010-09-08 12:20:18 +00001129
1130 if (Ty.isInvalid())
1131 return ExprError();
1132
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001133 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), /*isType=*/true,
1134 Ty.get().getAsOpaquePtr(),
1135 T.getCloseLocation());
Francois Pichet9f4f2072010-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 Gregore7a8e3b2011-10-12 16:37:45 +00001144 T.consumeClose();
Francois Pichet9f4f2072010-09-08 12:20:18 +00001145
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001146 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(),
1147 /*isType=*/false,
1148 Result.release(), T.getCloseLocation());
Francois Pichet9f4f2072010-09-08 12:20:18 +00001149 }
1150 }
1151
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001152 return Result;
Francois Pichet9f4f2072010-09-08 12:20:18 +00001153}
1154
Douglas Gregore610ada2010-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 McCalldadc5752010-08-24 06:29:42 +00001169ExprResult
Douglas Gregore610ada2010-02-24 18:44:31 +00001170Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
1171 tok::TokenKind OpKind,
1172 CXXScopeSpec &SS,
John McCallba7bf592010-08-24 05:47:05 +00001173 ParsedType ObjectType) {
Douglas Gregore610ada2010-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 Bagnara7945c982012-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 Gregore610ada2010-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 Blaikie1d578782011-12-16 16:03:09 +00001204
1205 if (Tok.is(tok::kw_decltype) && !FirstTypeName.isValid() && SS.isEmpty()) {
1206 DeclSpec DS(AttrFactory);
Benjamin Kramer198e0832011-12-18 12:18:02 +00001207 ParseDecltypeSpecifier(DS);
David Blaikie1d578782011-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 Gregore610ada2010-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 Bagnara7945c982012-01-27 09:46:47 +00001229 ParseUnqualifiedIdTemplateId(SS, SourceLocation(),
1230 Name, NameLoc,
1231 false, ObjectType, SecondTypeName,
1232 /*AssumeTemplateName=*/true))
Douglas Gregore610ada2010-02-24 18:44:31 +00001233 return ExprError();
1234
John McCallb268a282010-08-23 23:25:46 +00001235 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
1236 OpLoc, OpKind,
Douglas Gregore610ada2010-02-24 18:44:31 +00001237 SS, FirstTypeName, CCLoc,
1238 TildeLoc, SecondTypeName,
1239 Tok.is(tok::l_paren));
1240}
1241
Bill Wendling4073ed52007-02-13 01:51:42 +00001242/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
1243///
1244/// boolean-literal: [C++ 2.13.5]
1245/// 'true'
1246/// 'false'
John McCalldadc5752010-08-24 06:29:42 +00001247ExprResult Parser::ParseCXXBoolLiteral() {
Bill Wendling4073ed52007-02-13 01:51:42 +00001248 tok::TokenKind Kind = Tok.getKind();
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001249 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Bill Wendling4073ed52007-02-13 01:51:42 +00001250}
Chris Lattnerb7e656b2008-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 McCalldadc5752010-08-24 06:29:42 +00001256ExprResult Parser::ParseThrowExpression() {
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001257 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001258 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redld65cea82008-12-11 22:51:44 +00001259
Chris Lattner65dd8432008-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 Gregor53e191ed2011-07-06 22:04:06 +00001270 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0);
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001271
Chris Lattner65dd8432008-04-06 06:02:23 +00001272 default:
John McCalldadc5752010-08-24 06:29:42 +00001273 ExprResult Expr(ParseAssignmentExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001274 if (Expr.isInvalid()) return Expr;
Douglas Gregor53e191ed2011-07-06 22:04:06 +00001275 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take());
Chris Lattner65dd8432008-04-06 06:02:23 +00001276 }
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001277}
Argyrios Kyrtzidis7bbb20e2008-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 McCalldadc5752010-08-24 06:29:42 +00001284ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001285 assert(Tok.is(tok::kw_this) && "Not 'this'!");
1286 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001287 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001288}
Argyrios Kyrtzidis857fcc22008-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 Redl3da34892011-06-05 12:23:16 +00001294/// See [C++ 5.2.3].
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001295///
1296/// postfix-expression: [C++ 5.2p1]
Sebastian Redl3da34892011-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 Kyrtzidis857fcc22008-08-22 15:38:55 +00001301///
John McCalldadc5752010-08-24 06:29:42 +00001302ExprResult
Sebastian Redld65cea82008-12-11 22:51:44 +00001303Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001304 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallba7bf592010-08-24 05:47:05 +00001305 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001306
Sebastian Redl3da34892011-06-05 12:23:16 +00001307 assert((Tok.is(tok::l_paren) ||
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001308 (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)))
Sebastian Redl3da34892011-06-05 12:23:16 +00001309 && "Expected '(' or '{'!");
Douglas Gregor94a32472011-01-11 00:33:19 +00001310
Sebastian Redl3da34892011-06-05 12:23:16 +00001311 if (Tok.is(tok::l_brace)) {
Sebastian Redld74dd492012-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 Redl3da34892011-06-05 12:23:16 +00001319 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001320 BalancedDelimiterTracker T(*this, tok::l_paren);
1321 T.consumeOpen();
Sebastian Redl3da34892011-06-05 12:23:16 +00001322
Benjamin Kramerf0623432012-08-23 22:51:59 +00001323 ExprVector Exprs;
Sebastian Redl3da34892011-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 Kyrtzidis857fcc22008-08-22 15:38:55 +00001331 }
Sebastian Redl3da34892011-06-05 12:23:16 +00001332
1333 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001334 T.consumeClose();
Sebastian Redl3da34892011-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 Gregore7a8e3b2011-10-12 16:37:45 +00001342 return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001343 Exprs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001344 T.getCloseLocation());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001345 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001346}
1347
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001348/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001349///
1350/// condition:
1351/// expression
1352/// type-specifier-seq declarator '=' assignment-expression
Richard Smith2a15b742012-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 Kyrtzidis2b4072f2008-09-09 20:38:47 +00001355/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
1356/// '=' assignment-expression
1357///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00001358/// \param ExprOut if the condition was parsed as an expression, the parsed
1359/// expression.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001360///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00001361/// \param DeclOut if the condition was parsed as a declaration, the parsed
1362/// declaration.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001363///
Douglas Gregore60e41a2010-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 Gregor7bab5ff2009-11-25 00:27:52 +00001370/// \returns true if there was a parsing, false otherwise.
John McCalldadc5752010-08-24 06:29:42 +00001371bool Parser::ParseCXXCondition(ExprResult &ExprOut,
1372 Decl *&DeclOut,
Douglas Gregore60e41a2010-05-06 17:25:47 +00001373 SourceLocation Loc,
1374 bool ConvertToBoolean) {
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001375 if (Tok.is(tok::code_completion)) {
John McCallfaf5fb42010-08-26 23:41:50 +00001376 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001377 cutOffParsing();
1378 return true;
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001379 }
1380
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001381 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00001382 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001383
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001384 if (!isCXXConditionDeclaration()) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001385 ProhibitAttributes(attrs);
1386
Douglas Gregore60e41a2010-05-06 17:25:47 +00001387 // Parse the expression.
John McCalldadc5752010-08-24 06:29:42 +00001388 ExprOut = ParseExpression(); // expression
1389 DeclOut = 0;
1390 if (ExprOut.isInvalid())
Douglas Gregore60e41a2010-05-06 17:25:47 +00001391 return true;
1392
1393 // If required, convert to a boolean value.
1394 if (ConvertToBoolean)
John McCalldadc5752010-08-24 06:29:42 +00001395 ExprOut
1396 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
1397 return ExprOut.isInvalid();
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001398 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001399
1400 // type-specifier-seq
John McCall084e83d2011-03-24 11:26:52 +00001401 DeclSpec DS(AttrFactory);
Richard Smith54ecd982013-02-20 19:22:51 +00001402 DS.takeAttributesFrom(attrs);
Argyrios Kyrtzidis2b4072f2008-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 Redlf6591ca2009-02-09 18:23:29 +00001411 SourceLocation Loc;
John McCalldadc5752010-08-24 06:29:42 +00001412 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001413 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001414 SkipUntil(tok::semi);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001415 return true;
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001416 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001417 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001418 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001419 }
1420
1421 // If attributes are present, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001422 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001423
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001424 // Type-check the declaration itself.
John McCalldadc5752010-08-24 06:29:42 +00001425 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall53fa7142010-12-24 02:08:15 +00001426 DeclaratorInfo);
John McCalldadc5752010-08-24 06:29:42 +00001427 DeclOut = Dcl.get();
1428 ExprOut = ExprError();
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +00001429
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001430 // '=' assignment-expression
Richard Trieuc64d3232012-01-18 22:54:52 +00001431 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Smith2a15b742012-02-22 06:49:09 +00001432 bool CopyInitialization = isTokenEqualOrEqualTypo();
1433 if (CopyInitialization)
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001434 ConsumeToken();
Richard Smith2a15b742012-02-22 06:49:09 +00001435
1436 ExprResult InitExpr = ExprError();
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001437 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith2a15b742012-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 Gregor7bab5ff2009-11-25 00:27:52 +00001451 } else {
Richard Smith2a15b742012-02-22 06:49:09 +00001452 Diag(DeclOut ? DeclOut->getLocation() : Tok.getLocation(),
1453 diag::err_expected_init_in_condition);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001454 }
Richard Smith2a15b742012-02-22 06:49:09 +00001455
1456 if (!InitExpr.isInvalid())
1457 Actions.AddInitializerToDecl(DeclOut, InitExpr.take(), !CopyInitialization,
Richard Smith74aeef52013-04-26 16:15:35 +00001458 DS.containsPlaceholderType());
Richard Smith27d807c2013-04-30 13:56:41 +00001459 else
1460 Actions.ActOnInitializerError(DeclOut);
Richard Smith2a15b742012-02-22 06:49:09 +00001461
Douglas Gregore60e41a2010-05-06 17:25:47 +00001462 // FIXME: Build a reference to this declaration? Convert it to bool?
1463 // (This is currently handled by Sema).
Richard Smithb2bc2e62011-02-21 20:05:19 +00001464
1465 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregore60e41a2010-05-06 17:25:47 +00001466
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001467 return false;
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001468}
1469
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001470/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1471/// This should only be called when the current token is known to be part of
1472/// simple-type-specifier.
1473///
1474/// simple-type-specifier:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001475/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001476/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1477/// char
1478/// wchar_t
1479/// bool
1480/// short
1481/// int
1482/// long
1483/// signed
1484/// unsigned
1485/// float
1486/// double
1487/// void
1488/// [GNU] typeof-specifier
1489/// [C++0x] auto [TODO]
1490///
1491/// type-name:
1492/// class-name
1493/// enum-name
1494/// typedef-name
1495///
1496void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1497 DS.SetRangeStart(Tok.getLocation());
1498 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +00001499 unsigned DiagID;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001500 SourceLocation Loc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001501
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001502 switch (Tok.getKind()) {
Chris Lattner45ddec32009-01-05 00:13:00 +00001503 case tok::identifier: // foo::bar
1504 case tok::coloncolon: // ::foo::bar
David Blaikie83d382b2011-09-23 05:06:16 +00001505 llvm_unreachable("Annotation token should already be formed!");
Mike Stump11289f42009-09-09 15:08:12 +00001506 default:
David Blaikie83d382b2011-09-23 05:06:16 +00001507 llvm_unreachable("Not a simple-type-specifier token!");
Chris Lattner45ddec32009-01-05 00:13:00 +00001508
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001509 // type-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00001510 case tok::annot_typename: {
Douglas Gregor0231d8d2011-01-19 20:10:05 +00001511 if (getTypeAnnotation(Tok))
1512 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
1513 getTypeAnnotation(Tok));
1514 else
1515 DS.SetTypeSpecError();
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001516
1517 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1518 ConsumeToken();
1519
1520 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1521 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1522 // Objective-C interface. If we don't have Objective-C or a '<', this is
1523 // just a normal reference to a typedef name.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001524 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001525 ParseObjCProtocolQualifiers(DS);
1526
1527 DS.Finish(Diags, PP);
1528 return;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001529 }
Mike Stump11289f42009-09-09 15:08:12 +00001530
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001531 // builtin types
1532 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001533 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001534 break;
1535 case tok::kw_long:
John McCall49bfce42009-08-03 20:12:06 +00001536 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001537 break;
Francois Pichet84133e42011-04-28 01:59:37 +00001538 case tok::kw___int64:
1539 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1540 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001541 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001542 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001543 break;
1544 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001545 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001546 break;
1547 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001548 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001549 break;
1550 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001551 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001552 break;
1553 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001554 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001555 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00001556 case tok::kw___int128:
1557 DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, DiagID);
1558 break;
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00001559 case tok::kw_half:
1560 DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
1561 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001562 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001563 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001564 break;
1565 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001566 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001567 break;
1568 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001569 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001570 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001571 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001572 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001573 break;
1574 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001575 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001576 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001577 case tok::kw_bool:
John McCall49bfce42009-08-03 20:12:06 +00001578 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001579 break;
David Blaikie25896afb2012-01-24 05:47:35 +00001580 case tok::annot_decltype:
1581 case tok::kw_decltype:
1582 DS.SetRangeEnd(ParseDecltypeSpecifier(DS));
1583 return DS.Finish(Diags, PP);
Mike Stump11289f42009-09-09 15:08:12 +00001584
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001585 // GNU typeof support.
1586 case tok::kw_typeof:
1587 ParseTypeofSpecifier(DS);
Douglas Gregore3e01a22009-04-01 22:41:11 +00001588 DS.Finish(Diags, PP);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001589 return;
1590 }
Chris Lattnera8a3f732009-01-06 05:06:21 +00001591 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001592 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1593 else
1594 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001595 ConsumeToken();
Douglas Gregore3e01a22009-04-01 22:41:11 +00001596 DS.Finish(Diags, PP);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001597}
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001598
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001599/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1600/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1601/// e.g., "const short int". Note that the DeclSpec is *not* finished
1602/// by parsing the type-specifier-seq, because these sequences are
1603/// typically followed by some form of declarator. Returns true and
1604/// emits diagnostics if this is not a type-specifier-seq, false
1605/// otherwise.
1606///
1607/// type-specifier-seq: [C++ 8.1]
1608/// type-specifier type-specifier-seq[opt]
1609///
1610bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
Richard Smithc5b05522012-03-12 07:56:15 +00001611 ParseSpecifierQualifierList(DS, AS_none, DSC_type_specifier);
Douglas Gregor40d732f2010-02-24 23:13:13 +00001612 DS.Finish(Diags, PP);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001613 return false;
1614}
1615
Douglas Gregor7861a802009-11-03 01:35:08 +00001616/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1617/// some form.
1618///
1619/// This routine is invoked when a '<' is encountered after an identifier or
1620/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1621/// whether the unqualified-id is actually a template-id. This routine will
1622/// then parse the template arguments and form the appropriate template-id to
1623/// return to the caller.
1624///
1625/// \param SS the nested-name-specifier that precedes this template-id, if
1626/// we're actually parsing a qualified-id.
1627///
1628/// \param Name for constructor and destructor names, this is the actual
1629/// identifier that may be a template-name.
1630///
1631/// \param NameLoc the location of the class-name in a constructor or
1632/// destructor.
1633///
1634/// \param EnteringContext whether we're entering the scope of the
1635/// nested-name-specifier.
1636///
Douglas Gregor127ea592009-11-03 21:24:04 +00001637/// \param ObjectType if this unqualified-id occurs within a member access
1638/// expression, the type of the base object whose member is being accessed.
1639///
Douglas Gregor7861a802009-11-03 01:35:08 +00001640/// \param Id as input, describes the template-name or operator-function-id
1641/// that precedes the '<'. If template arguments were parsed successfully,
1642/// will be updated with the template-id.
1643///
Douglas Gregore610ada2010-02-24 18:44:31 +00001644/// \param AssumeTemplateId When true, this routine will assume that the name
1645/// refers to a template without performing name lookup to verify.
1646///
Douglas Gregor7861a802009-11-03 01:35:08 +00001647/// \returns true if a parse error occurred, false otherwise.
1648bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001649 SourceLocation TemplateKWLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00001650 IdentifierInfo *Name,
1651 SourceLocation NameLoc,
1652 bool EnteringContext,
John McCallba7bf592010-08-24 05:47:05 +00001653 ParsedType ObjectType,
Douglas Gregore610ada2010-02-24 18:44:31 +00001654 UnqualifiedId &Id,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001655 bool AssumeTemplateId) {
Douglas Gregorb22ee882010-05-05 05:58:24 +00001656 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1657 "Expected '<' to finish parsing a template-id");
Douglas Gregor7861a802009-11-03 01:35:08 +00001658
1659 TemplateTy Template;
1660 TemplateNameKind TNK = TNK_Non_template;
1661 switch (Id.getKind()) {
1662 case UnqualifiedId::IK_Identifier:
Douglas Gregor3cf81312009-11-03 23:16:33 +00001663 case UnqualifiedId::IK_OperatorFunctionId:
Alexis Hunted0530f2009-11-28 08:58:14 +00001664 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregore610ada2010-02-24 18:44:31 +00001665 if (AssumeTemplateId) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001666 TNK = Actions.ActOnDependentTemplateName(getCurScope(), SS, TemplateKWLoc,
Douglas Gregorbb119652010-06-16 23:00:59 +00001667 Id, ObjectType, EnteringContext,
1668 Template);
1669 if (TNK == TNK_Non_template)
1670 return true;
Douglas Gregor786123d2010-05-21 23:18:07 +00001671 } else {
1672 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001673 TNK = Actions.isTemplateName(getCurScope(), SS,
1674 TemplateKWLoc.isValid(), Id,
1675 ObjectType, EnteringContext, Template,
Douglas Gregor786123d2010-05-21 23:18:07 +00001676 MemberOfUnknownSpecialization);
1677
1678 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1679 ObjectType && IsTemplateArgumentList()) {
1680 // We have something like t->getAs<T>(), where getAs is a
1681 // member of an unknown specialization. However, this will only
1682 // parse correctly as a template, so suggest the keyword 'template'
1683 // before 'getAs' and treat this as a dependent template name.
1684 std::string Name;
1685 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1686 Name = Id.Identifier->getName();
1687 else {
1688 Name = "operator ";
1689 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1690 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1691 else
1692 Name += Id.Identifier->getName();
1693 }
1694 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1695 << Name
1696 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Abramo Bagnara7945c982012-01-27 09:46:47 +00001697 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1698 SS, TemplateKWLoc, Id,
1699 ObjectType, EnteringContext,
1700 Template);
Douglas Gregorbb119652010-06-16 23:00:59 +00001701 if (TNK == TNK_Non_template)
Douglas Gregor786123d2010-05-21 23:18:07 +00001702 return true;
1703 }
1704 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001705 break;
1706
Douglas Gregor3cf81312009-11-03 23:16:33 +00001707 case UnqualifiedId::IK_ConstructorName: {
1708 UnqualifiedId TemplateName;
Douglas Gregor786123d2010-05-21 23:18:07 +00001709 bool MemberOfUnknownSpecialization;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001710 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001711 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1712 TemplateName, ObjectType,
Douglas Gregor786123d2010-05-21 23:18:07 +00001713 EnteringContext, Template,
1714 MemberOfUnknownSpecialization);
Douglas Gregor7861a802009-11-03 01:35:08 +00001715 break;
1716 }
1717
Douglas Gregor3cf81312009-11-03 23:16:33 +00001718 case UnqualifiedId::IK_DestructorName: {
1719 UnqualifiedId TemplateName;
Douglas Gregor786123d2010-05-21 23:18:07 +00001720 bool MemberOfUnknownSpecialization;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001721 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001722 if (ObjectType) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001723 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1724 SS, TemplateKWLoc, TemplateName,
1725 ObjectType, EnteringContext,
1726 Template);
Douglas Gregorbb119652010-06-16 23:00:59 +00001727 if (TNK == TNK_Non_template)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001728 return true;
1729 } else {
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001730 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1731 TemplateName, ObjectType,
Douglas Gregor786123d2010-05-21 23:18:07 +00001732 EnteringContext, Template,
1733 MemberOfUnknownSpecialization);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001734
John McCallba7bf592010-08-24 05:47:05 +00001735 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00001736 Diag(NameLoc, diag::err_destructor_template_id)
1737 << Name << SS.getRange();
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001738 return true;
1739 }
1740 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001741 break;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001742 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001743
1744 default:
1745 return false;
1746 }
1747
1748 if (TNK == TNK_Non_template)
1749 return false;
1750
1751 // Parse the enclosed template argument list.
1752 SourceLocation LAngleLoc, RAngleLoc;
1753 TemplateArgList TemplateArgs;
Douglas Gregorb22ee882010-05-05 05:58:24 +00001754 if (Tok.is(tok::less) &&
1755 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregore7c20652011-03-02 00:47:37 +00001756 SS, true, LAngleLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00001757 TemplateArgs,
Douglas Gregor7861a802009-11-03 01:35:08 +00001758 RAngleLoc))
1759 return true;
1760
1761 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Alexis Hunted0530f2009-11-28 08:58:14 +00001762 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1763 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor7861a802009-11-03 01:35:08 +00001764 // Form a parsed representation of the template-id to be stored in the
1765 // UnqualifiedId.
1766 TemplateIdAnnotation *TemplateId
Benjamin Kramer1e6b6062012-04-14 12:14:03 +00001767 = TemplateIdAnnotation::Allocate(TemplateArgs.size(), TemplateIds);
Douglas Gregor7861a802009-11-03 01:35:08 +00001768
1769 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1770 TemplateId->Name = Id.Identifier;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001771 TemplateId->Operator = OO_None;
Douglas Gregor7861a802009-11-03 01:35:08 +00001772 TemplateId->TemplateNameLoc = Id.StartLocation;
1773 } else {
Douglas Gregor3cf81312009-11-03 23:16:33 +00001774 TemplateId->Name = 0;
1775 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1776 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor7861a802009-11-03 01:35:08 +00001777 }
1778
Douglas Gregore7c20652011-03-02 00:47:37 +00001779 TemplateId->SS = SS;
Benjamin Kramer807c2db2012-02-19 23:37:39 +00001780 TemplateId->TemplateKWLoc = TemplateKWLoc;
John McCall3e56fd42010-08-23 07:28:44 +00001781 TemplateId->Template = Template;
Douglas Gregor7861a802009-11-03 01:35:08 +00001782 TemplateId->Kind = TNK;
1783 TemplateId->LAngleLoc = LAngleLoc;
1784 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001785 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor7861a802009-11-03 01:35:08 +00001786 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001787 Arg != ArgEnd; ++Arg)
Douglas Gregor7861a802009-11-03 01:35:08 +00001788 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor7861a802009-11-03 01:35:08 +00001789
1790 Id.setTemplateId(TemplateId);
1791 return false;
1792 }
1793
1794 // Bundle the template arguments together.
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001795 ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs);
Abramo Bagnara4244b432012-01-27 08:46:19 +00001796
Douglas Gregor7861a802009-11-03 01:35:08 +00001797 // Constructor and destructor names.
John McCallfaf5fb42010-08-26 23:41:50 +00001798 TypeResult Type
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001799 = Actions.ActOnTemplateIdType(SS, TemplateKWLoc,
1800 Template, NameLoc,
Abramo Bagnara4244b432012-01-27 08:46:19 +00001801 LAngleLoc, TemplateArgsPtr, RAngleLoc,
1802 /*IsCtorOrDtorName=*/true);
Douglas Gregor7861a802009-11-03 01:35:08 +00001803 if (Type.isInvalid())
1804 return true;
1805
1806 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1807 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1808 else
1809 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1810
1811 return false;
1812}
1813
Douglas Gregor71395fa2009-11-04 00:56:37 +00001814/// \brief Parse an operator-function-id or conversion-function-id as part
1815/// of a C++ unqualified-id.
1816///
1817/// This routine is responsible only for parsing the operator-function-id or
1818/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor7861a802009-11-03 01:35:08 +00001819///
1820/// \code
Douglas Gregor7861a802009-11-03 01:35:08 +00001821/// operator-function-id: [C++ 13.5]
1822/// 'operator' operator
1823///
Douglas Gregor71395fa2009-11-04 00:56:37 +00001824/// operator: one of
Douglas Gregor7861a802009-11-03 01:35:08 +00001825/// new delete new[] delete[]
1826/// + - * / % ^ & | ~
1827/// ! = < > += -= *= /= %=
1828/// ^= &= |= << >> >>= <<= == !=
1829/// <= >= && || ++ -- , ->* ->
1830/// () []
1831///
1832/// conversion-function-id: [C++ 12.3.2]
1833/// operator conversion-type-id
1834///
1835/// conversion-type-id:
1836/// type-specifier-seq conversion-declarator[opt]
1837///
1838/// conversion-declarator:
1839/// ptr-operator conversion-declarator[opt]
1840/// \endcode
1841///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00001842/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregor7861a802009-11-03 01:35:08 +00001843/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1844///
1845/// \param EnteringContext whether we are entering the scope of the
1846/// nested-name-specifier.
1847///
Douglas Gregor71395fa2009-11-04 00:56:37 +00001848/// \param ObjectType if this unqualified-id occurs within a member access
1849/// expression, the type of the base object whose member is being accessed.
1850///
1851/// \param Result on a successful parse, contains the parsed unqualified-id.
1852///
1853/// \returns true if parsing fails, false otherwise.
1854bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallba7bf592010-08-24 05:47:05 +00001855 ParsedType ObjectType,
Douglas Gregor71395fa2009-11-04 00:56:37 +00001856 UnqualifiedId &Result) {
1857 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1858
1859 // Consume the 'operator' keyword.
1860 SourceLocation KeywordLoc = ConsumeToken();
1861
1862 // Determine what kind of operator name we have.
1863 unsigned SymbolIdx = 0;
1864 SourceLocation SymbolLocations[3];
1865 OverloadedOperatorKind Op = OO_None;
1866 switch (Tok.getKind()) {
1867 case tok::kw_new:
1868 case tok::kw_delete: {
1869 bool isNew = Tok.getKind() == tok::kw_new;
1870 // Consume the 'new' or 'delete'.
1871 SymbolLocations[SymbolIdx++] = ConsumeToken();
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001872 // Check for array new/delete.
1873 if (Tok.is(tok::l_square) &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001874 (!getLangOpts().CPlusPlus11 || NextToken().isNot(tok::l_square))) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001875 // Consume the '[' and ']'.
1876 BalancedDelimiterTracker T(*this, tok::l_square);
1877 T.consumeOpen();
1878 T.consumeClose();
1879 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00001880 return true;
1881
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001882 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1883 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00001884 Op = isNew? OO_Array_New : OO_Array_Delete;
1885 } else {
1886 Op = isNew? OO_New : OO_Delete;
1887 }
1888 break;
1889 }
1890
1891#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1892 case tok::Token: \
1893 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1894 Op = OO_##Name; \
1895 break;
1896#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1897#include "clang/Basic/OperatorKinds.def"
1898
1899 case tok::l_paren: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001900 // Consume the '(' and ')'.
1901 BalancedDelimiterTracker T(*this, tok::l_paren);
1902 T.consumeOpen();
1903 T.consumeClose();
1904 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00001905 return true;
1906
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001907 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1908 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00001909 Op = OO_Call;
1910 break;
1911 }
1912
1913 case tok::l_square: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001914 // Consume the '[' and ']'.
1915 BalancedDelimiterTracker T(*this, tok::l_square);
1916 T.consumeOpen();
1917 T.consumeClose();
1918 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00001919 return true;
1920
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001921 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1922 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00001923 Op = OO_Subscript;
1924 break;
1925 }
1926
1927 case tok::code_completion: {
1928 // Code completion for the operator name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001929 Actions.CodeCompleteOperatorName(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001930 cutOffParsing();
Douglas Gregor71395fa2009-11-04 00:56:37 +00001931 // Don't try to parse any further.
1932 return true;
1933 }
1934
1935 default:
1936 break;
1937 }
1938
1939 if (Op != OO_None) {
1940 // We have parsed an operator-function-id.
1941 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1942 return false;
1943 }
Alexis Hunt34458502009-11-28 04:44:28 +00001944
1945 // Parse a literal-operator-id.
1946 //
Richard Smith6f212062012-10-20 08:41:10 +00001947 // literal-operator-id: C++11 [over.literal]
1948 // operator string-literal identifier
1949 // operator user-defined-string-literal
Alexis Hunt34458502009-11-28 04:44:28 +00001950
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001951 if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
Richard Smith5d164bc2011-10-15 05:09:34 +00001952 Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator);
Alexis Hunt34458502009-11-28 04:44:28 +00001953
Richard Smith7d182a72012-03-08 23:06:02 +00001954 SourceLocation DiagLoc;
1955 unsigned DiagId = 0;
1956
1957 // We're past translation phase 6, so perform string literal concatenation
1958 // before checking for "".
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001959 SmallVector<Token, 4> Toks;
1960 SmallVector<SourceLocation, 4> TokLocs;
Richard Smith7d182a72012-03-08 23:06:02 +00001961 while (isTokenStringLiteral()) {
1962 if (!Tok.is(tok::string_literal) && !DiagId) {
Richard Smith6f212062012-10-20 08:41:10 +00001963 // C++11 [over.literal]p1:
1964 // The string-literal or user-defined-string-literal in a
1965 // literal-operator-id shall have no encoding-prefix [...].
Richard Smith7d182a72012-03-08 23:06:02 +00001966 DiagLoc = Tok.getLocation();
1967 DiagId = diag::err_literal_operator_string_prefix;
1968 }
1969 Toks.push_back(Tok);
1970 TokLocs.push_back(ConsumeStringToken());
1971 }
1972
1973 StringLiteralParser Literal(Toks.data(), Toks.size(), PP);
1974 if (Literal.hadError)
1975 return true;
1976
1977 // Grab the literal operator's suffix, which will be either the next token
1978 // or a ud-suffix from the string literal.
1979 IdentifierInfo *II = 0;
1980 SourceLocation SuffixLoc;
1981 if (!Literal.getUDSuffix().empty()) {
1982 II = &PP.getIdentifierTable().get(Literal.getUDSuffix());
1983 SuffixLoc =
1984 Lexer::AdvanceToTokenCharacter(TokLocs[Literal.getUDSuffixToken()],
1985 Literal.getUDSuffixOffset(),
David Blaikiebbafb8a2012-03-11 07:00:24 +00001986 PP.getSourceManager(), getLangOpts());
Richard Smith7d182a72012-03-08 23:06:02 +00001987 } else if (Tok.is(tok::identifier)) {
1988 II = Tok.getIdentifierInfo();
1989 SuffixLoc = ConsumeToken();
1990 TokLocs.push_back(SuffixLoc);
1991 } else {
Alexis Hunt34458502009-11-28 04:44:28 +00001992 Diag(Tok.getLocation(), diag::err_expected_ident);
1993 return true;
1994 }
1995
Richard Smith7d182a72012-03-08 23:06:02 +00001996 // The string literal must be empty.
1997 if (!Literal.GetString().empty() || Literal.Pascal) {
Richard Smith6f212062012-10-20 08:41:10 +00001998 // C++11 [over.literal]p1:
1999 // The string-literal or user-defined-string-literal in a
2000 // literal-operator-id shall [...] contain no characters
2001 // other than the implicit terminating '\0'.
Richard Smith7d182a72012-03-08 23:06:02 +00002002 DiagLoc = TokLocs.front();
2003 DiagId = diag::err_literal_operator_string_not_empty;
2004 }
2005
2006 if (DiagId) {
2007 // This isn't a valid literal-operator-id, but we think we know
2008 // what the user meant. Tell them what they should have written.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002009 SmallString<32> Str;
Richard Smith7d182a72012-03-08 23:06:02 +00002010 Str += "\"\" ";
2011 Str += II->getName();
2012 Diag(DiagLoc, DiagId) << FixItHint::CreateReplacement(
2013 SourceRange(TokLocs.front(), TokLocs.back()), Str);
2014 }
2015
2016 Result.setLiteralOperatorId(II, KeywordLoc, SuffixLoc);
Alexis Hunt3d221f22009-11-29 07:34:05 +00002017 return false;
Alexis Hunt34458502009-11-28 04:44:28 +00002018 }
Douglas Gregor71395fa2009-11-04 00:56:37 +00002019
2020 // Parse a conversion-function-id.
2021 //
2022 // conversion-function-id: [C++ 12.3.2]
2023 // operator conversion-type-id
2024 //
2025 // conversion-type-id:
2026 // type-specifier-seq conversion-declarator[opt]
2027 //
2028 // conversion-declarator:
2029 // ptr-operator conversion-declarator[opt]
2030
2031 // Parse the type-specifier-seq.
John McCall084e83d2011-03-24 11:26:52 +00002032 DeclSpec DS(AttrFactory);
Douglas Gregora25d65d2009-11-20 22:03:38 +00002033 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregor71395fa2009-11-04 00:56:37 +00002034 return true;
2035
2036 // Parse the conversion-declarator, which is merely a sequence of
2037 // ptr-operators.
2038 Declarator D(DS, Declarator::TypeNameContext);
2039 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
2040
2041 // Finish up the type.
John McCallfaf5fb42010-08-26 23:41:50 +00002042 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregor71395fa2009-11-04 00:56:37 +00002043 if (Ty.isInvalid())
2044 return true;
2045
2046 // Note that this is a conversion-function-id.
2047 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
2048 D.getSourceRange().getEnd());
2049 return false;
2050}
2051
2052/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
2053/// name of an entity.
2054///
2055/// \code
2056/// unqualified-id: [C++ expr.prim.general]
2057/// identifier
2058/// operator-function-id
2059/// conversion-function-id
2060/// [C++0x] literal-operator-id [TODO]
2061/// ~ class-name
2062/// template-id
2063///
2064/// \endcode
2065///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00002066/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregor71395fa2009-11-04 00:56:37 +00002067/// non-empty, then we are parsing the unqualified-id of a qualified-id.
2068///
2069/// \param EnteringContext whether we are entering the scope of the
2070/// nested-name-specifier.
2071///
Douglas Gregor7861a802009-11-03 01:35:08 +00002072/// \param AllowDestructorName whether we allow parsing of a destructor name.
2073///
2074/// \param AllowConstructorName whether we allow parsing a constructor name.
2075///
Douglas Gregor127ea592009-11-03 21:24:04 +00002076/// \param ObjectType if this unqualified-id occurs within a member access
2077/// expression, the type of the base object whose member is being accessed.
2078///
Douglas Gregor7861a802009-11-03 01:35:08 +00002079/// \param Result on a successful parse, contains the parsed unqualified-id.
2080///
2081/// \returns true if parsing fails, false otherwise.
2082bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
2083 bool AllowDestructorName,
2084 bool AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00002085 ParsedType ObjectType,
Abramo Bagnara7945c982012-01-27 09:46:47 +00002086 SourceLocation& TemplateKWLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00002087 UnqualifiedId &Result) {
Douglas Gregorb22ee882010-05-05 05:58:24 +00002088
2089 // Handle 'A::template B'. This is for template-ids which have not
2090 // already been annotated by ParseOptionalCXXScopeSpecifier().
2091 bool TemplateSpecified = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002092 if (getLangOpts().CPlusPlus && Tok.is(tok::kw_template) &&
Douglas Gregorb22ee882010-05-05 05:58:24 +00002093 (ObjectType || SS.isSet())) {
2094 TemplateSpecified = true;
2095 TemplateKWLoc = ConsumeToken();
2096 }
2097
Douglas Gregor7861a802009-11-03 01:35:08 +00002098 // unqualified-id:
2099 // identifier
2100 // template-id (when it hasn't already been annotated)
2101 if (Tok.is(tok::identifier)) {
2102 // Consume the identifier.
2103 IdentifierInfo *Id = Tok.getIdentifierInfo();
2104 SourceLocation IdLoc = ConsumeToken();
2105
David Blaikiebbafb8a2012-03-11 07:00:24 +00002106 if (!getLangOpts().CPlusPlus) {
Douglas Gregor411e5ac2010-01-11 23:29:10 +00002107 // If we're not in C++, only identifiers matter. Record the
2108 // identifier and return.
2109 Result.setIdentifier(Id, IdLoc);
2110 return false;
2111 }
2112
Douglas Gregor7861a802009-11-03 01:35:08 +00002113 if (AllowConstructorName &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002114 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor7861a802009-11-03 01:35:08 +00002115 // We have parsed a constructor name.
Abramo Bagnara4244b432012-01-27 08:46:19 +00002116 ParsedType Ty = Actions.getTypeName(*Id, IdLoc, getCurScope(),
2117 &SS, false, false,
2118 ParsedType(),
2119 /*IsCtorOrDtorName=*/true,
2120 /*NonTrivialTypeSourceInfo=*/true);
2121 Result.setConstructorName(Ty, IdLoc, IdLoc);
Douglas Gregor7861a802009-11-03 01:35:08 +00002122 } else {
2123 // We have parsed an identifier.
2124 Result.setIdentifier(Id, IdLoc);
2125 }
2126
2127 // If the next token is a '<', we may have a template.
Douglas Gregorb22ee882010-05-05 05:58:24 +00002128 if (TemplateSpecified || Tok.is(tok::less))
Abramo Bagnara7945c982012-01-27 09:46:47 +00002129 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc, Id, IdLoc,
2130 EnteringContext, ObjectType,
2131 Result, TemplateSpecified);
Douglas Gregor7861a802009-11-03 01:35:08 +00002132
2133 return false;
2134 }
2135
2136 // unqualified-id:
2137 // template-id (already parsed and annotated)
2138 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002139 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002140
2141 // If the template-name names the current class, then this is a constructor
2142 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002143 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002144 if (SS.isSet()) {
2145 // C++ [class.qual]p2 specifies that a qualified template-name
2146 // is taken as the constructor name where a constructor can be
2147 // declared. Thus, the template arguments are extraneous, so
2148 // complain about them and remove them entirely.
2149 Diag(TemplateId->TemplateNameLoc,
2150 diag::err_out_of_line_constructor_template_id)
2151 << TemplateId->Name
Douglas Gregora771f462010-03-31 17:46:05 +00002152 << FixItHint::CreateRemoval(
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002153 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
Abramo Bagnara4244b432012-01-27 08:46:19 +00002154 ParsedType Ty = Actions.getTypeName(*TemplateId->Name,
2155 TemplateId->TemplateNameLoc,
2156 getCurScope(),
2157 &SS, false, false,
2158 ParsedType(),
2159 /*IsCtorOrDtorName=*/true,
2160 /*NontrivialTypeSourceInfo=*/true);
2161 Result.setConstructorName(Ty, TemplateId->TemplateNameLoc,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002162 TemplateId->RAngleLoc);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002163 ConsumeToken();
2164 return false;
2165 }
2166
2167 Result.setConstructorTemplateId(TemplateId);
2168 ConsumeToken();
2169 return false;
2170 }
2171
Douglas Gregor7861a802009-11-03 01:35:08 +00002172 // We have already parsed a template-id; consume the annotation token as
2173 // our unqualified-id.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002174 Result.setTemplateId(TemplateId);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002175 TemplateKWLoc = TemplateId->TemplateKWLoc;
Douglas Gregor7861a802009-11-03 01:35:08 +00002176 ConsumeToken();
2177 return false;
2178 }
2179
2180 // unqualified-id:
2181 // operator-function-id
2182 // conversion-function-id
2183 if (Tok.is(tok::kw_operator)) {
Douglas Gregor71395fa2009-11-04 00:56:37 +00002184 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor7861a802009-11-03 01:35:08 +00002185 return true;
2186
Alexis Hunted0530f2009-11-28 08:58:14 +00002187 // If we have an operator-function-id or a literal-operator-id and the next
2188 // token is a '<', we may have a
Douglas Gregor71395fa2009-11-04 00:56:37 +00002189 //
2190 // template-id:
2191 // operator-function-id < template-argument-list[opt] >
Alexis Hunted0530f2009-11-28 08:58:14 +00002192 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
2193 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregorb22ee882010-05-05 05:58:24 +00002194 (TemplateSpecified || Tok.is(tok::less)))
Abramo Bagnara7945c982012-01-27 09:46:47 +00002195 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2196 0, SourceLocation(),
2197 EnteringContext, ObjectType,
2198 Result, TemplateSpecified);
Douglas Gregor7861a802009-11-03 01:35:08 +00002199
Douglas Gregor7861a802009-11-03 01:35:08 +00002200 return false;
2201 }
2202
David Blaikiebbafb8a2012-03-11 07:00:24 +00002203 if (getLangOpts().CPlusPlus &&
Douglas Gregor411e5ac2010-01-11 23:29:10 +00002204 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor7861a802009-11-03 01:35:08 +00002205 // C++ [expr.unary.op]p10:
2206 // There is an ambiguity in the unary-expression ~X(), where X is a
2207 // class-name. The ambiguity is resolved in favor of treating ~ as a
2208 // unary complement rather than treating ~X as referring to a destructor.
2209
2210 // Parse the '~'.
2211 SourceLocation TildeLoc = ConsumeToken();
David Blaikieecd8a942011-12-08 16:13:53 +00002212
2213 if (SS.isEmpty() && Tok.is(tok::kw_decltype)) {
2214 DeclSpec DS(AttrFactory);
2215 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
2216 if (ParsedType Type = Actions.getDestructorType(DS, ObjectType)) {
2217 Result.setDestructorName(TildeLoc, Type, EndLoc);
2218 return false;
2219 }
2220 return true;
2221 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002222
2223 // Parse the class-name.
2224 if (Tok.isNot(tok::identifier)) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00002225 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor7861a802009-11-03 01:35:08 +00002226 return true;
2227 }
2228
2229 // Parse the class-name (or template-name in a simple-template-id).
2230 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
2231 SourceLocation ClassNameLoc = ConsumeToken();
2232
Douglas Gregorb22ee882010-05-05 05:58:24 +00002233 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallba7bf592010-08-24 05:47:05 +00002234 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002235 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2236 ClassName, ClassNameLoc,
2237 EnteringContext, ObjectType,
2238 Result, TemplateSpecified);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002239 }
2240
Douglas Gregor7861a802009-11-03 01:35:08 +00002241 // Note that this is a destructor name.
John McCallba7bf592010-08-24 05:47:05 +00002242 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
2243 ClassNameLoc, getCurScope(),
2244 SS, ObjectType,
2245 EnteringContext);
Douglas Gregorfe17d252010-02-16 19:09:40 +00002246 if (!Ty)
Douglas Gregor7861a802009-11-03 01:35:08 +00002247 return true;
Douglas Gregorfe17d252010-02-16 19:09:40 +00002248
Douglas Gregor7861a802009-11-03 01:35:08 +00002249 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor7861a802009-11-03 01:35:08 +00002250 return false;
2251 }
2252
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002253 Diag(Tok, diag::err_expected_unqualified_id)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002254 << getLangOpts().CPlusPlus;
Douglas Gregor7861a802009-11-03 01:35:08 +00002255 return true;
2256}
2257
Sebastian Redlbd150f42008-11-21 19:14:01 +00002258/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
2259/// memory in a typesafe manner and call constructors.
Mike Stump11289f42009-09-09 15:08:12 +00002260///
Chris Lattner109faf22009-01-04 21:25:24 +00002261/// This method is called to parse the new expression after the optional :: has
2262/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
2263/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002264///
2265/// new-expression:
2266/// '::'[opt] 'new' new-placement[opt] new-type-id
2267/// new-initializer[opt]
2268/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2269/// new-initializer[opt]
2270///
2271/// new-placement:
2272/// '(' expression-list ')'
2273///
Sebastian Redl351bb782008-12-02 14:43:59 +00002274/// new-type-id:
2275/// type-specifier-seq new-declarator[opt]
Douglas Gregora3a020a2011-04-15 19:40:02 +00002276/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redl351bb782008-12-02 14:43:59 +00002277///
2278/// new-declarator:
2279/// ptr-operator new-declarator[opt]
2280/// direct-new-declarator
2281///
Sebastian Redlbd150f42008-11-21 19:14:01 +00002282/// new-initializer:
2283/// '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00002284/// [C++0x] braced-init-list
Sebastian Redlbd150f42008-11-21 19:14:01 +00002285///
John McCalldadc5752010-08-24 06:29:42 +00002286ExprResult
Chris Lattner109faf22009-01-04 21:25:24 +00002287Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
2288 assert(Tok.is(tok::kw_new) && "expected 'new' token");
2289 ConsumeToken(); // Consume 'new'
Sebastian Redlbd150f42008-11-21 19:14:01 +00002290
2291 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
2292 // second form of new-expression. It can't be a new-type-id.
2293
Benjamin Kramerf0623432012-08-23 22:51:59 +00002294 ExprVector PlacementArgs;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002295 SourceLocation PlacementLParen, PlacementRParen;
2296
Douglas Gregorf2753b32010-07-13 15:54:32 +00002297 SourceRange TypeIdParens;
John McCall084e83d2011-03-24 11:26:52 +00002298 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis3ff13572011-06-28 03:01:23 +00002299 Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002300 if (Tok.is(tok::l_paren)) {
2301 // If it turns out to be a placement, we change the type location.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002302 BalancedDelimiterTracker T(*this, tok::l_paren);
2303 T.consumeOpen();
2304 PlacementLParen = T.getOpenLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002305 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
2306 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002307 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002308 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002309
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002310 T.consumeClose();
2311 PlacementRParen = T.getCloseLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002312 if (PlacementRParen.isInvalid()) {
2313 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002314 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002315 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002316
Sebastian Redl351bb782008-12-02 14:43:59 +00002317 if (PlacementArgs.empty()) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002318 // Reset the placement locations. There was no placement.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002319 TypeIdParens = T.getRange();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002320 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002321 } else {
2322 // We still need the type.
2323 if (Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002324 BalancedDelimiterTracker T(*this, tok::l_paren);
2325 T.consumeOpen();
Douglas Gregora3a020a2011-04-15 19:40:02 +00002326 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002327 ParseSpecifierQualifierList(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002328 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002329 ParseDeclarator(DeclaratorInfo);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002330 T.consumeClose();
2331 TypeIdParens = T.getRange();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002332 } else {
Douglas Gregora3a020a2011-04-15 19:40:02 +00002333 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002334 if (ParseCXXTypeSpecifierSeq(DS))
2335 DeclaratorInfo.setInvalidType(true);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002336 else {
2337 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002338 ParseDeclaratorInternal(DeclaratorInfo,
2339 &Parser::ParseDirectNewDeclarator);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002340 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002341 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002342 }
2343 } else {
Sebastian Redl351bb782008-12-02 14:43:59 +00002344 // A new-type-id is a simplified type-id, where essentially the
2345 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregora3a020a2011-04-15 19:40:02 +00002346 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002347 if (ParseCXXTypeSpecifierSeq(DS))
2348 DeclaratorInfo.setInvalidType(true);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002349 else {
2350 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002351 ParseDeclaratorInternal(DeclaratorInfo,
2352 &Parser::ParseDirectNewDeclarator);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002353 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002354 }
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00002355 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redl351bb782008-12-02 14:43:59 +00002356 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002357 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002358 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002359
Sebastian Redl6047f072012-02-16 12:22:20 +00002360 ExprResult Initializer;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002361
2362 if (Tok.is(tok::l_paren)) {
Sebastian Redl6047f072012-02-16 12:22:20 +00002363 SourceLocation ConstructorLParen, ConstructorRParen;
Benjamin Kramerf0623432012-08-23 22:51:59 +00002364 ExprVector ConstructorArgs;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002365 BalancedDelimiterTracker T(*this, tok::l_paren);
2366 T.consumeOpen();
2367 ConstructorLParen = T.getOpenLocation();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002368 if (Tok.isNot(tok::r_paren)) {
2369 CommaLocsTy CommaLocs;
Sebastian Redl351bb782008-12-02 14:43:59 +00002370 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
2371 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002372 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002373 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002374 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002375 T.consumeClose();
2376 ConstructorRParen = T.getCloseLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002377 if (ConstructorRParen.isInvalid()) {
2378 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002379 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002380 }
Sebastian Redl6047f072012-02-16 12:22:20 +00002381 Initializer = Actions.ActOnParenListExpr(ConstructorLParen,
2382 ConstructorRParen,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002383 ConstructorArgs);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002384 } else if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus11) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002385 Diag(Tok.getLocation(),
2386 diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl6047f072012-02-16 12:22:20 +00002387 Initializer = ParseBraceInitializer();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002388 }
Sebastian Redl6047f072012-02-16 12:22:20 +00002389 if (Initializer.isInvalid())
2390 return Initializer;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002391
Sebastian Redl6d4256c2009-03-15 17:47:39 +00002392 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002393 PlacementArgs, PlacementRParen,
Sebastian Redl6047f072012-02-16 12:22:20 +00002394 TypeIdParens, DeclaratorInfo, Initializer.take());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002395}
2396
Sebastian Redlbd150f42008-11-21 19:14:01 +00002397/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
2398/// passed to ParseDeclaratorInternal.
2399///
2400/// direct-new-declarator:
2401/// '[' expression ']'
2402/// direct-new-declarator '[' constant-expression ']'
2403///
Chris Lattner109faf22009-01-04 21:25:24 +00002404void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002405 // Parse the array dimensions.
2406 bool first = true;
2407 while (Tok.is(tok::l_square)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002408 // An array-size expression can't start with a lambda.
2409 if (CheckProhibitedCXX11Attribute())
2410 continue;
2411
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002412 BalancedDelimiterTracker T(*this, tok::l_square);
2413 T.consumeOpen();
2414
John McCalldadc5752010-08-24 06:29:42 +00002415 ExprResult Size(first ? ParseExpression()
Sebastian Redl59b5e512008-12-11 21:36:32 +00002416 : ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002417 if (Size.isInvalid()) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002418 // Recover
2419 SkipUntil(tok::r_square);
2420 return;
2421 }
2422 first = false;
2423
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002424 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00002425
Bill Wendling44426052012-12-20 19:22:21 +00002426 // Attributes here appertain to the array type. C++11 [expr.new]p5.
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002427 ParsedAttributes Attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002428 MaybeParseCXX11Attributes(Attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002429
John McCall084e83d2011-03-24 11:26:52 +00002430 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall53fa7142010-12-24 02:08:15 +00002431 /*static=*/false, /*star=*/false,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002432 Size.release(),
2433 T.getOpenLocation(),
2434 T.getCloseLocation()),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002435 Attrs, T.getCloseLocation());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002436
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002437 if (T.getCloseLocation().isInvalid())
Sebastian Redlbd150f42008-11-21 19:14:01 +00002438 return;
2439 }
2440}
2441
2442/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
2443/// This ambiguity appears in the syntax of the C++ new operator.
2444///
2445/// new-expression:
2446/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2447/// new-initializer[opt]
2448///
2449/// new-placement:
2450/// '(' expression-list ')'
2451///
John McCall37ad5512010-08-23 06:44:23 +00002452bool Parser::ParseExpressionListOrTypeId(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002453 SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner109faf22009-01-04 21:25:24 +00002454 Declarator &D) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002455 // The '(' was already consumed.
2456 if (isTypeIdInParens()) {
Sebastian Redl351bb782008-12-02 14:43:59 +00002457 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002458 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002459 ParseDeclarator(D);
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00002460 return D.isInvalidType();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002461 }
2462
2463 // It's not a type, it has to be an expression list.
2464 // Discard the comma locations - ActOnCXXNew has enough parameters.
2465 CommaLocsTy CommaLocs;
2466 return ParseExpressionList(PlacementArgs, CommaLocs);
2467}
2468
2469/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
2470/// to free memory allocated by new.
2471///
Chris Lattner109faf22009-01-04 21:25:24 +00002472/// This method is called to parse the 'delete' expression after the optional
2473/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
2474/// and "Start" is its location. Otherwise, "Start" is the location of the
2475/// 'delete' token.
2476///
Sebastian Redlbd150f42008-11-21 19:14:01 +00002477/// delete-expression:
2478/// '::'[opt] 'delete' cast-expression
2479/// '::'[opt] 'delete' '[' ']' cast-expression
John McCalldadc5752010-08-24 06:29:42 +00002480ExprResult
Chris Lattner109faf22009-01-04 21:25:24 +00002481Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
2482 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
2483 ConsumeToken(); // Consume 'delete'
Sebastian Redlbd150f42008-11-21 19:14:01 +00002484
2485 // Array delete?
2486 bool ArrayDelete = false;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002487 if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
Richard Smith10c60722012-08-09 19:01:51 +00002488 // C++11 [expr.delete]p1:
2489 // Whenever the delete keyword is followed by empty square brackets, it
2490 // shall be interpreted as [array delete].
2491 // [Footnote: A lambda expression with a lambda-introducer that consists
2492 // of empty square brackets can follow the delete keyword if
2493 // the lambda expression is enclosed in parentheses.]
2494 // FIXME: Produce a better diagnostic if the '[]' is unambiguously a
2495 // lambda-introducer.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002496 ArrayDelete = true;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002497 BalancedDelimiterTracker T(*this, tok::l_square);
2498
2499 T.consumeOpen();
2500 T.consumeClose();
2501 if (T.getCloseLocation().isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +00002502 return ExprError();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002503 }
2504
John McCalldadc5752010-08-24 06:29:42 +00002505 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002506 if (Operand.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002507 return Operand;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002508
John McCallb268a282010-08-23 23:25:46 +00002509 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002510}
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002511
Mike Stump11289f42009-09-09 15:08:12 +00002512static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002513 switch(kind) {
David Blaikie83d382b2011-09-23 05:06:16 +00002514 default: llvm_unreachable("Not a known unary type trait.");
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002515 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
Joao Matosc9523d42013-03-27 01:34:16 +00002516 case tok::kw___has_nothrow_move_assign: return UTT_HasNothrowMoveAssign;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002517 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
John Wiegley65497cc2011-04-27 23:09:49 +00002518 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002519 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
Joao Matosc9523d42013-03-27 01:34:16 +00002520 case tok::kw___has_trivial_move_assign: return UTT_HasTrivialMoveAssign;
Alexis Huntf479f1b2011-05-09 18:22:59 +00002521 case tok::kw___has_trivial_constructor:
2522 return UTT_HasTrivialDefaultConstructor;
Joao Matosc9523d42013-03-27 01:34:16 +00002523 case tok::kw___has_trivial_move_constructor:
2524 return UTT_HasTrivialMoveConstructor;
John Wiegley65497cc2011-04-27 23:09:49 +00002525 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002526 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
2527 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
2528 case tok::kw___is_abstract: return UTT_IsAbstract;
John Wiegley65497cc2011-04-27 23:09:49 +00002529 case tok::kw___is_arithmetic: return UTT_IsArithmetic;
2530 case tok::kw___is_array: return UTT_IsArray;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002531 case tok::kw___is_class: return UTT_IsClass;
John Wiegley65497cc2011-04-27 23:09:49 +00002532 case tok::kw___is_complete_type: return UTT_IsCompleteType;
2533 case tok::kw___is_compound: return UTT_IsCompound;
2534 case tok::kw___is_const: return UTT_IsConst;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002535 case tok::kw___is_empty: return UTT_IsEmpty;
2536 case tok::kw___is_enum: return UTT_IsEnum;
Douglas Gregordca70af2011-12-03 18:14:24 +00002537 case tok::kw___is_final: return UTT_IsFinal;
John Wiegley65497cc2011-04-27 23:09:49 +00002538 case tok::kw___is_floating_point: return UTT_IsFloatingPoint;
2539 case tok::kw___is_function: return UTT_IsFunction;
2540 case tok::kw___is_fundamental: return UTT_IsFundamental;
2541 case tok::kw___is_integral: return UTT_IsIntegral;
John McCallbf4a7d72012-09-25 07:32:49 +00002542 case tok::kw___is_interface_class: return UTT_IsInterfaceClass;
John Wiegley65497cc2011-04-27 23:09:49 +00002543 case tok::kw___is_lvalue_reference: return UTT_IsLvalueReference;
2544 case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
2545 case tok::kw___is_member_object_pointer: return UTT_IsMemberObjectPointer;
2546 case tok::kw___is_member_pointer: return UTT_IsMemberPointer;
2547 case tok::kw___is_object: return UTT_IsObject;
Chandler Carruth79803482011-04-23 10:47:20 +00002548 case tok::kw___is_literal: return UTT_IsLiteral;
Chandler Carruth65fa1fd2011-04-24 02:49:28 +00002549 case tok::kw___is_literal_type: return UTT_IsLiteral;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002550 case tok::kw___is_pod: return UTT_IsPOD;
John Wiegley65497cc2011-04-27 23:09:49 +00002551 case tok::kw___is_pointer: return UTT_IsPointer;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002552 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
John Wiegley65497cc2011-04-27 23:09:49 +00002553 case tok::kw___is_reference: return UTT_IsReference;
John Wiegley65497cc2011-04-27 23:09:49 +00002554 case tok::kw___is_rvalue_reference: return UTT_IsRvalueReference;
2555 case tok::kw___is_scalar: return UTT_IsScalar;
2556 case tok::kw___is_signed: return UTT_IsSigned;
2557 case tok::kw___is_standard_layout: return UTT_IsStandardLayout;
2558 case tok::kw___is_trivial: return UTT_IsTrivial;
Alexis Huntd9a5cc12011-05-13 00:31:07 +00002559 case tok::kw___is_trivially_copyable: return UTT_IsTriviallyCopyable;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002560 case tok::kw___is_union: return UTT_IsUnion;
John Wiegley65497cc2011-04-27 23:09:49 +00002561 case tok::kw___is_unsigned: return UTT_IsUnsigned;
2562 case tok::kw___is_void: return UTT_IsVoid;
2563 case tok::kw___is_volatile: return UTT_IsVolatile;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002564 }
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002565}
2566
2567static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
2568 switch(kind) {
Francois Pichet347c4c72010-12-07 00:55:57 +00002569 default: llvm_unreachable("Not a known binary type trait");
Francois Pichet34b21132010-12-08 22:35:30 +00002570 case tok::kw___is_base_of: return BTT_IsBaseOf;
John Wiegley65497cc2011-04-27 23:09:49 +00002571 case tok::kw___is_convertible: return BTT_IsConvertible;
2572 case tok::kw___is_same: return BTT_IsSame;
Francois Pichet34b21132010-12-08 22:35:30 +00002573 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor8006e762011-01-27 20:28:01 +00002574 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Douglas Gregor1be329d2012-02-23 07:33:15 +00002575 case tok::kw___is_trivially_assignable: return BTT_IsTriviallyAssignable;
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002576 }
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002577}
2578
Douglas Gregor29c42f22012-02-24 07:38:34 +00002579static TypeTrait TypeTraitFromTokKind(tok::TokenKind kind) {
2580 switch (kind) {
2581 default: llvm_unreachable("Not a known type trait");
2582 case tok::kw___is_trivially_constructible:
2583 return TT_IsTriviallyConstructible;
2584 }
2585}
2586
John Wiegley6242b6a2011-04-28 00:16:57 +00002587static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
2588 switch(kind) {
2589 default: llvm_unreachable("Not a known binary type trait");
2590 case tok::kw___array_rank: return ATT_ArrayRank;
2591 case tok::kw___array_extent: return ATT_ArrayExtent;
2592 }
2593}
2594
John Wiegleyf9f65842011-04-25 06:54:41 +00002595static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2596 switch(kind) {
David Blaikie83d382b2011-09-23 05:06:16 +00002597 default: llvm_unreachable("Not a known unary expression trait.");
John Wiegleyf9f65842011-04-25 06:54:41 +00002598 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
2599 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
2600 }
2601}
2602
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002603/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
2604/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2605/// templates.
2606///
2607/// primary-expression:
2608/// [GNU] unary-type-trait '(' type-id ')'
2609///
John McCalldadc5752010-08-24 06:29:42 +00002610ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002611 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
2612 SourceLocation Loc = ConsumeToken();
2613
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002614 BalancedDelimiterTracker T(*this, tok::l_paren);
2615 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002616 return ExprError();
2617
2618 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
2619 // there will be cryptic errors about mismatched parentheses and missing
2620 // specifiers.
Douglas Gregor220cac52009-02-18 17:45:20 +00002621 TypeResult Ty = ParseTypeName();
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002622
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002623 T.consumeClose();
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002624
Douglas Gregor220cac52009-02-18 17:45:20 +00002625 if (Ty.isInvalid())
2626 return ExprError();
2627
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002628 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), T.getCloseLocation());
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002629}
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002630
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002631/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
2632/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2633/// templates.
2634///
2635/// primary-expression:
2636/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
2637///
2638ExprResult Parser::ParseBinaryTypeTrait() {
2639 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
2640 SourceLocation Loc = ConsumeToken();
2641
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002642 BalancedDelimiterTracker T(*this, tok::l_paren);
2643 if (T.expectAndConsume(diag::err_expected_lparen))
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002644 return ExprError();
2645
2646 TypeResult LhsTy = ParseTypeName();
2647 if (LhsTy.isInvalid()) {
2648 SkipUntil(tok::r_paren);
2649 return ExprError();
2650 }
2651
2652 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2653 SkipUntil(tok::r_paren);
2654 return ExprError();
2655 }
2656
2657 TypeResult RhsTy = ParseTypeName();
2658 if (RhsTy.isInvalid()) {
2659 SkipUntil(tok::r_paren);
2660 return ExprError();
2661 }
2662
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002663 T.consumeClose();
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002664
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002665 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(),
2666 T.getCloseLocation());
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002667}
2668
Douglas Gregor29c42f22012-02-24 07:38:34 +00002669/// \brief Parse the built-in type-trait pseudo-functions that allow
2670/// implementation of the TR1/C++11 type traits templates.
2671///
2672/// primary-expression:
2673/// type-trait '(' type-id-seq ')'
2674///
2675/// type-id-seq:
2676/// type-id ...[opt] type-id-seq[opt]
2677///
2678ExprResult Parser::ParseTypeTrait() {
2679 TypeTrait Kind = TypeTraitFromTokKind(Tok.getKind());
2680 SourceLocation Loc = ConsumeToken();
2681
2682 BalancedDelimiterTracker Parens(*this, tok::l_paren);
2683 if (Parens.expectAndConsume(diag::err_expected_lparen))
2684 return ExprError();
2685
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002686 SmallVector<ParsedType, 2> Args;
Douglas Gregor29c42f22012-02-24 07:38:34 +00002687 do {
2688 // Parse the next type.
2689 TypeResult Ty = ParseTypeName();
2690 if (Ty.isInvalid()) {
2691 Parens.skipToEnd();
2692 return ExprError();
2693 }
2694
2695 // Parse the ellipsis, if present.
2696 if (Tok.is(tok::ellipsis)) {
2697 Ty = Actions.ActOnPackExpansion(Ty.get(), ConsumeToken());
2698 if (Ty.isInvalid()) {
2699 Parens.skipToEnd();
2700 return ExprError();
2701 }
2702 }
2703
2704 // Add this type to the list of arguments.
2705 Args.push_back(Ty.get());
2706
2707 if (Tok.is(tok::comma)) {
2708 ConsumeToken();
2709 continue;
2710 }
2711
2712 break;
2713 } while (true);
2714
2715 if (Parens.consumeClose())
2716 return ExprError();
2717
2718 return Actions.ActOnTypeTrait(Kind, Loc, Args, Parens.getCloseLocation());
2719}
2720
John Wiegley6242b6a2011-04-28 00:16:57 +00002721/// ParseArrayTypeTrait - Parse the built-in array type-trait
2722/// pseudo-functions.
2723///
2724/// primary-expression:
2725/// [Embarcadero] '__array_rank' '(' type-id ')'
2726/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
2727///
2728ExprResult Parser::ParseArrayTypeTrait() {
2729 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
2730 SourceLocation Loc = ConsumeToken();
2731
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002732 BalancedDelimiterTracker T(*this, tok::l_paren);
2733 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley6242b6a2011-04-28 00:16:57 +00002734 return ExprError();
2735
2736 TypeResult Ty = ParseTypeName();
2737 if (Ty.isInvalid()) {
2738 SkipUntil(tok::comma);
2739 SkipUntil(tok::r_paren);
2740 return ExprError();
2741 }
2742
2743 switch (ATT) {
2744 case ATT_ArrayRank: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002745 T.consumeClose();
2746 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL,
2747 T.getCloseLocation());
John Wiegley6242b6a2011-04-28 00:16:57 +00002748 }
2749 case ATT_ArrayExtent: {
2750 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2751 SkipUntil(tok::r_paren);
2752 return ExprError();
2753 }
2754
2755 ExprResult DimExpr = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002756 T.consumeClose();
John Wiegley6242b6a2011-04-28 00:16:57 +00002757
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002758 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(),
2759 T.getCloseLocation());
John Wiegley6242b6a2011-04-28 00:16:57 +00002760 }
John Wiegley6242b6a2011-04-28 00:16:57 +00002761 }
David Blaikiee4d798f2012-01-20 21:50:17 +00002762 llvm_unreachable("Invalid ArrayTypeTrait!");
John Wiegley6242b6a2011-04-28 00:16:57 +00002763}
2764
John Wiegleyf9f65842011-04-25 06:54:41 +00002765/// ParseExpressionTrait - Parse built-in expression-trait
2766/// pseudo-functions like __is_lvalue_expr( xxx ).
2767///
2768/// primary-expression:
2769/// [Embarcadero] expression-trait '(' expression ')'
2770///
2771ExprResult Parser::ParseExpressionTrait() {
2772 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2773 SourceLocation Loc = ConsumeToken();
2774
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002775 BalancedDelimiterTracker T(*this, tok::l_paren);
2776 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegleyf9f65842011-04-25 06:54:41 +00002777 return ExprError();
2778
2779 ExprResult Expr = ParseExpression();
2780
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002781 T.consumeClose();
John Wiegleyf9f65842011-04-25 06:54:41 +00002782
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002783 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(),
2784 T.getCloseLocation());
John Wiegleyf9f65842011-04-25 06:54:41 +00002785}
2786
2787
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002788/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2789/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2790/// based on the context past the parens.
John McCalldadc5752010-08-24 06:29:42 +00002791ExprResult
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002792Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallba7bf592010-08-24 05:47:05 +00002793 ParsedType &CastTy,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002794 BalancedDelimiterTracker &Tracker) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002795 assert(getLangOpts().CPlusPlus && "Should only be called for C++!");
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002796 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2797 assert(isTypeIdInParens() && "Not a type-id!");
2798
John McCalldadc5752010-08-24 06:29:42 +00002799 ExprResult Result(true);
John McCallba7bf592010-08-24 05:47:05 +00002800 CastTy = ParsedType();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002801
2802 // We need to disambiguate a very ugly part of the C++ syntax:
2803 //
2804 // (T())x; - type-id
2805 // (T())*x; - type-id
2806 // (T())/x; - expression
2807 // (T()); - expression
2808 //
2809 // The bad news is that we cannot use the specialized tentative parser, since
2810 // it can only verify that the thing inside the parens can be parsed as
2811 // type-id, it is not useful for determining the context past the parens.
2812 //
2813 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidis24ad6922009-05-22 15:12:46 +00002814 // making any unnecessary Action calls.
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002815 //
2816 // It uses a scheme similar to parsing inline methods. The parenthesized
2817 // tokens are cached, the context that follows is determined (possibly by
2818 // parsing a cast-expression), and then we re-introduce the cached tokens
2819 // into the token stream and parse them appropriately.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002820
Mike Stump11289f42009-09-09 15:08:12 +00002821 ParenParseOption ParseAs;
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002822 CachedTokens Toks;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002823
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002824 // Store the tokens of the parentheses. We will parse them after we determine
2825 // the context that follows them.
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00002826 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002827 // We didn't find the ')' we expected.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002828 Tracker.consumeClose();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002829 return ExprError();
2830 }
2831
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002832 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002833 ParseAs = CompoundLiteral;
2834 } else {
2835 bool NotCastExpr;
Eli Friedmancf7530f2009-05-25 19:41:42 +00002836 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2837 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2838 NotCastExpr = true;
2839 } else {
2840 // Try parsing the cast-expression that may follow.
2841 // If it is not a cast-expression, NotCastExpr will be true and no token
2842 // will be consumed.
2843 Result = ParseCastExpression(false/*isUnaryExpression*/,
2844 false/*isAddressofOperand*/,
John McCallba7bf592010-08-24 05:47:05 +00002845 NotCastExpr,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002846 // type-id has priority.
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +00002847 IsTypeCast);
Eli Friedmancf7530f2009-05-25 19:41:42 +00002848 }
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002849
2850 // If we parsed a cast-expression, it's really a type-id, otherwise it's
2851 // an expression.
2852 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002853 }
2854
Mike Stump11289f42009-09-09 15:08:12 +00002855 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002856 Toks.push_back(Tok);
2857 // Re-enter the stored parenthesized tokens into the token stream, so we may
2858 // parse them now.
2859 PP.EnterTokenStream(Toks.data(), Toks.size(),
2860 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2861 // Drop the current token and bring the first cached one. It's the same token
2862 // as when we entered this function.
2863 ConsumeAnyToken();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002864
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002865 if (ParseAs >= CompoundLiteral) {
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002866 // Parse the type declarator.
2867 DeclSpec DS(AttrFactory);
2868 ParseSpecifierQualifierList(DS);
2869 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2870 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002871
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002872 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002873 Tracker.consumeClose();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002874
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002875 if (ParseAs == CompoundLiteral) {
2876 ExprType = CompoundLiteral;
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002877 TypeResult Ty = ParseTypeName();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002878 return ParseCompoundLiteralExpression(Ty.get(),
2879 Tracker.getOpenLocation(),
2880 Tracker.getCloseLocation());
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002881 }
Mike Stump11289f42009-09-09 15:08:12 +00002882
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002883 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2884 assert(ParseAs == CastExpr);
2885
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002886 if (DeclaratorInfo.isInvalidType())
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002887 return ExprError();
2888
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002889 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002890 if (!Result.isInvalid())
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002891 Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(),
2892 DeclaratorInfo, CastTy,
2893 Tracker.getCloseLocation(), Result.take());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002894 return Result;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002895 }
Mike Stump11289f42009-09-09 15:08:12 +00002896
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002897 // Not a compound literal, and not followed by a cast-expression.
2898 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002899
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002900 ExprType = SimpleExpr;
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002901 Result = ParseExpression();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002902 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002903 Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(),
2904 Tok.getLocation(), Result.take());
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002905
2906 // Match the ')'.
2907 if (Result.isInvalid()) {
2908 SkipUntil(tok::r_paren);
2909 return ExprError();
2910 }
Mike Stump11289f42009-09-09 15:08:12 +00002911
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002912 Tracker.consumeClose();
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002913 return Result;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002914}