blob: 86e2e187d3e7c30743355dc1f6caeb46d64a0754 [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
Richard Smith21b3ab42013-05-09 21:36:41 +0000586/// ParseLambdaExpression - Parse a C++11 lambda expression.
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000587///
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:
Richard Smith21b3ab42013-05-09 21:36:41 +0000608/// simple-capture
609/// init-capture [C++1y]
610///
611/// simple-capture:
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000612/// identifier
613/// '&' identifier
614/// 'this'
615///
Richard Smith21b3ab42013-05-09 21:36:41 +0000616/// init-capture: [C++1y]
617/// identifier initializer
618/// '&' identifier initializer
619///
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000620/// lambda-declarator:
621/// '(' parameter-declaration-clause ')' attribute-specifier[opt]
622/// 'mutable'[opt] exception-specification[opt]
623/// trailing-return-type[opt]
624///
625ExprResult Parser::ParseLambdaExpression() {
626 // Parse lambda-introducer.
627 LambdaIntroducer Intro;
628
David Blaikie05785d12013-02-20 22:23:23 +0000629 Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000630 if (DiagID) {
631 Diag(Tok, DiagID.getValue());
632 SkipUntil(tok::r_square);
Eli Friedmanc7c97142012-01-04 02:40:39 +0000633 SkipUntil(tok::l_brace);
634 SkipUntil(tok::r_brace);
635 return ExprError();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000636 }
637
638 return ParseLambdaExpressionAfterIntroducer(Intro);
639}
640
641/// TryParseLambdaExpression - Use lookahead and potentially tentative
642/// parsing to determine if we are looking at a C++0x lambda expression, and parse
643/// it if we are.
644///
645/// If we are not looking at a lambda expression, returns ExprError().
646ExprResult Parser::TryParseLambdaExpression() {
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000647 assert(getLangOpts().CPlusPlus11
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000648 && Tok.is(tok::l_square)
649 && "Not at the start of a possible lambda expression.");
650
651 const Token Next = NextToken(), After = GetLookAheadToken(2);
652
653 // If lookahead indicates this is a lambda...
654 if (Next.is(tok::r_square) || // []
655 Next.is(tok::equal) || // [=
656 (Next.is(tok::amp) && // [&] or [&,
657 (After.is(tok::r_square) ||
658 After.is(tok::comma))) ||
659 (Next.is(tok::identifier) && // [identifier]
660 After.is(tok::r_square))) {
661 return ParseLambdaExpression();
662 }
663
Eli Friedmanc7c97142012-01-04 02:40:39 +0000664 // If lookahead indicates an ObjC message send...
665 // [identifier identifier
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000666 if (Next.is(tok::identifier) && After.is(tok::identifier)) {
Eli Friedmanc7c97142012-01-04 02:40:39 +0000667 return ExprEmpty();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000668 }
669
Eli Friedmanc7c97142012-01-04 02:40:39 +0000670 // Here, we're stuck: lambda introducers and Objective-C message sends are
671 // unambiguous, but it requires arbitrary lookhead. [a,b,c,d,e,f,g] is a
672 // lambda, and [a,b,c,d,e,f,g h] is a Objective-C message send. Instead of
673 // writing two routines to parse a lambda introducer, just try to parse
674 // a lambda introducer first, and fall back if that fails.
675 // (TryParseLambdaIntroducer never produces any diagnostic output.)
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000676 LambdaIntroducer Intro;
677 if (TryParseLambdaIntroducer(Intro))
Eli Friedmanc7c97142012-01-04 02:40:39 +0000678 return ExprEmpty();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000679 return ParseLambdaExpressionAfterIntroducer(Intro);
680}
681
682/// ParseLambdaExpression - Parse a lambda introducer.
683///
684/// Returns a DiagnosticID if it hit something unexpected.
David Blaikie05785d12013-02-20 22:23:23 +0000685Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro) {
686 typedef Optional<unsigned> DiagResult;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000687
688 assert(Tok.is(tok::l_square) && "Lambda expressions begin with '['.");
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000689 BalancedDelimiterTracker T(*this, tok::l_square);
690 T.consumeOpen();
691
692 Intro.Range.setBegin(T.getOpenLocation());
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000693
694 bool first = true;
695
696 // Parse capture-default.
697 if (Tok.is(tok::amp) &&
698 (NextToken().is(tok::comma) || NextToken().is(tok::r_square))) {
699 Intro.Default = LCD_ByRef;
Douglas Gregora1bffa22012-02-10 17:46:20 +0000700 Intro.DefaultLoc = ConsumeToken();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000701 first = false;
702 } else if (Tok.is(tok::equal)) {
703 Intro.Default = LCD_ByCopy;
Douglas Gregora1bffa22012-02-10 17:46:20 +0000704 Intro.DefaultLoc = ConsumeToken();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000705 first = false;
706 }
707
708 while (Tok.isNot(tok::r_square)) {
709 if (!first) {
Douglas Gregord8c61782012-02-15 15:34:24 +0000710 if (Tok.isNot(tok::comma)) {
Douglas Gregor721b14d2012-07-31 00:50:07 +0000711 // Provide a completion for a lambda introducer here. Except
712 // in Objective-C, where this is Almost Surely meant to be a message
713 // send. In that case, fail here and let the ObjC message
714 // expression parser perform the completion.
Douglas Gregor2d8db8f2012-07-31 15:27:48 +0000715 if (Tok.is(tok::code_completion) &&
716 !(getLangOpts().ObjC1 && Intro.Default == LCD_None &&
717 !Intro.Captures.empty())) {
Douglas Gregord8c61782012-02-15 15:34:24 +0000718 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
719 /*AfterAmpersand=*/false);
720 ConsumeCodeCompletionToken();
721 break;
722 }
723
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000724 return DiagResult(diag::err_expected_comma_or_rsquare);
Douglas Gregord8c61782012-02-15 15:34:24 +0000725 }
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000726 ConsumeToken();
727 }
728
Douglas Gregord8c61782012-02-15 15:34:24 +0000729 if (Tok.is(tok::code_completion)) {
730 // If we're in Objective-C++ and we have a bare '[', then this is more
731 // likely to be a message receiver.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000732 if (getLangOpts().ObjC1 && first)
Douglas Gregord8c61782012-02-15 15:34:24 +0000733 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
734 else
735 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
736 /*AfterAmpersand=*/false);
737 ConsumeCodeCompletionToken();
738 break;
739 }
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000740
Douglas Gregord8c61782012-02-15 15:34:24 +0000741 first = false;
742
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000743 // Parse capture.
744 LambdaCaptureKind Kind = LCK_ByCopy;
745 SourceLocation Loc;
746 IdentifierInfo* Id = 0;
Douglas Gregor3e308b12012-02-14 19:27:52 +0000747 SourceLocation EllipsisLoc;
Richard Smith21b3ab42013-05-09 21:36:41 +0000748 ExprResult Init;
Douglas Gregor3e308b12012-02-14 19:27:52 +0000749
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000750 if (Tok.is(tok::kw_this)) {
751 Kind = LCK_This;
752 Loc = ConsumeToken();
753 } else {
754 if (Tok.is(tok::amp)) {
755 Kind = LCK_ByRef;
756 ConsumeToken();
Douglas Gregord8c61782012-02-15 15:34:24 +0000757
758 if (Tok.is(tok::code_completion)) {
759 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
760 /*AfterAmpersand=*/true);
761 ConsumeCodeCompletionToken();
762 break;
763 }
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000764 }
765
766 if (Tok.is(tok::identifier)) {
767 Id = Tok.getIdentifierInfo();
768 Loc = ConsumeToken();
Douglas Gregor3e308b12012-02-14 19:27:52 +0000769
770 if (Tok.is(tok::ellipsis))
771 EllipsisLoc = ConsumeToken();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000772 } else if (Tok.is(tok::kw_this)) {
773 // FIXME: If we want to suggest a fixit here, will need to return more
774 // than just DiagnosticID. Perhaps full DiagnosticBuilder that can be
775 // Clear()ed to prevent emission in case of tentative parsing?
776 return DiagResult(diag::err_this_captured_by_reference);
777 } else {
778 return DiagResult(diag::err_expected_capture);
779 }
Richard Smith21b3ab42013-05-09 21:36:41 +0000780
781 if (Tok.is(tok::l_paren)) {
782 BalancedDelimiterTracker Parens(*this, tok::l_paren);
783 Parens.consumeOpen();
784
785 ExprVector Exprs;
786 CommaLocsTy Commas;
787 if (ParseExpressionList(Exprs, Commas)) {
788 Parens.skipToEnd();
789 Init = ExprError();
790 } else {
791 Parens.consumeClose();
792 Init = Actions.ActOnParenListExpr(Parens.getOpenLocation(),
793 Parens.getCloseLocation(),
794 Exprs);
795 }
796 } else if (Tok.is(tok::l_brace) || Tok.is(tok::equal)) {
797 if (Tok.is(tok::equal))
798 ConsumeToken();
799
800 Init = ParseInitializer();
801 }
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000802 }
803
Richard Smith21b3ab42013-05-09 21:36:41 +0000804 Intro.addCapture(Kind, Loc, Id, EllipsisLoc, Init);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000805 }
806
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000807 T.consumeClose();
808 Intro.Range.setEnd(T.getCloseLocation());
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000809
810 return DiagResult();
811}
812
Douglas Gregord8c61782012-02-15 15:34:24 +0000813/// TryParseLambdaIntroducer - Tentatively parse a lambda introducer.
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000814///
815/// Returns true if it hit something unexpected.
816bool Parser::TryParseLambdaIntroducer(LambdaIntroducer &Intro) {
817 TentativeParsingAction PA(*this);
818
David Blaikie05785d12013-02-20 22:23:23 +0000819 Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000820
821 if (DiagID) {
822 PA.Revert();
823 return true;
824 }
825
826 PA.Commit();
827 return false;
828}
829
830/// ParseLambdaExpressionAfterIntroducer - Parse the rest of a lambda
831/// expression.
832ExprResult Parser::ParseLambdaExpressionAfterIntroducer(
833 LambdaIntroducer &Intro) {
Eli Friedmanc7c97142012-01-04 02:40:39 +0000834 SourceLocation LambdaBeginLoc = Intro.Range.getBegin();
835 Diag(LambdaBeginLoc, diag::warn_cxx98_compat_lambda);
836
837 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), LambdaBeginLoc,
838 "lambda expression parsing");
839
Richard Smith21b3ab42013-05-09 21:36:41 +0000840 // FIXME: Call into Actions to add any init-capture declarations to the
841 // scope while parsing the lambda-declarator and compound-statement.
842
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000843 // Parse lambda-declarator[opt].
844 DeclSpec DS(AttrFactory);
Eli Friedman36d12942012-01-04 04:41:38 +0000845 Declarator D(DS, Declarator::LambdaExprContext);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000846
847 if (Tok.is(tok::l_paren)) {
848 ParseScope PrototypeScope(this,
849 Scope::FunctionPrototypeScope |
Richard Smithe233fbf2013-01-28 22:42:45 +0000850 Scope::FunctionDeclarationScope |
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000851 Scope::DeclScope);
852
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000853 SourceLocation DeclEndLoc;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000854 BalancedDelimiterTracker T(*this, tok::l_paren);
855 T.consumeOpen();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000856 SourceLocation LParenLoc = T.getOpenLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000857
858 // Parse parameter-declaration-clause.
859 ParsedAttributes Attr(AttrFactory);
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000860 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000861 SourceLocation EllipsisLoc;
862
863 if (Tok.isNot(tok::r_paren))
864 ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc);
865
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000866 T.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000867 SourceLocation RParenLoc = T.getCloseLocation();
868 DeclEndLoc = RParenLoc;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000869
870 // Parse 'mutable'[opt].
871 SourceLocation MutableLoc;
872 if (Tok.is(tok::kw_mutable)) {
873 MutableLoc = ConsumeToken();
874 DeclEndLoc = MutableLoc;
875 }
876
877 // Parse exception-specification[opt].
878 ExceptionSpecificationType ESpecType = EST_None;
879 SourceRange ESpecRange;
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000880 SmallVector<ParsedType, 2> DynamicExceptions;
881 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000882 ExprResult NoexceptExpr;
Richard Smith2331bbf2012-05-02 22:22:32 +0000883 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +0000884 DynamicExceptions,
885 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +0000886 NoexceptExpr);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000887
888 if (ESpecType != EST_None)
889 DeclEndLoc = ESpecRange.getEnd();
890
891 // Parse attribute-specifier[opt].
Richard Smith89645bc2013-01-02 12:01:23 +0000892 MaybeParseCXX11Attributes(Attr, &DeclEndLoc);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000893
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000894 SourceLocation FunLocalRangeEnd = DeclEndLoc;
895
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000896 // Parse trailing-return-type[opt].
Richard Smith700537c2012-06-12 01:51:59 +0000897 TypeResult TrailingReturnType;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000898 if (Tok.is(tok::arrow)) {
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000899 FunLocalRangeEnd = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000900 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +0000901 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000902 if (Range.getEnd().isValid())
903 DeclEndLoc = Range.getEnd();
904 }
905
906 PrototypeScope.Exit();
907
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000908 SourceLocation NoLoc;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000909 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000910 /*isAmbiguous=*/false,
911 LParenLoc,
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000912 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000913 EllipsisLoc, RParenLoc,
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000914 DS.getTypeQualifiers(),
915 /*RefQualifierIsLValueRef=*/true,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000916 /*RefQualifierLoc=*/NoLoc,
917 /*ConstQualifierLoc=*/NoLoc,
918 /*VolatileQualifierLoc=*/NoLoc,
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000919 MutableLoc,
920 ESpecType, ESpecRange.getBegin(),
921 DynamicExceptions.data(),
922 DynamicExceptionRanges.data(),
923 DynamicExceptions.size(),
924 NoexceptExpr.isUsable() ?
925 NoexceptExpr.get() : 0,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000926 LParenLoc, FunLocalRangeEnd, D,
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000927 TrailingReturnType),
928 Attr, DeclEndLoc);
Douglas Gregor6746c5d2012-02-16 21:53:36 +0000929 } else if (Tok.is(tok::kw_mutable) || Tok.is(tok::arrow)) {
930 // It's common to forget that one needs '()' before 'mutable' or the
931 // result type. Deal with this.
932 Diag(Tok, diag::err_lambda_missing_parens)
933 << Tok.is(tok::arrow)
934 << FixItHint::CreateInsertion(Tok.getLocation(), "() ");
935 SourceLocation DeclLoc = Tok.getLocation();
936 SourceLocation DeclEndLoc = DeclLoc;
937
938 // Parse 'mutable', if it's there.
939 SourceLocation MutableLoc;
940 if (Tok.is(tok::kw_mutable)) {
941 MutableLoc = ConsumeToken();
942 DeclEndLoc = MutableLoc;
943 }
944
945 // Parse the return type, if there is one.
Richard Smith700537c2012-06-12 01:51:59 +0000946 TypeResult TrailingReturnType;
Douglas Gregor6746c5d2012-02-16 21:53:36 +0000947 if (Tok.is(tok::arrow)) {
948 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +0000949 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregor6746c5d2012-02-16 21:53:36 +0000950 if (Range.getEnd().isValid())
951 DeclEndLoc = Range.getEnd();
952 }
953
954 ParsedAttributes Attr(AttrFactory);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000955 SourceLocation NoLoc;
Douglas Gregor6746c5d2012-02-16 21:53:36 +0000956 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000957 /*isAmbiguous=*/false,
958 /*LParenLoc=*/NoLoc,
959 /*Params=*/0,
960 /*NumParams=*/0,
961 /*EllipsisLoc=*/NoLoc,
962 /*RParenLoc=*/NoLoc,
963 /*TypeQuals=*/0,
964 /*RefQualifierIsLValueRef=*/true,
965 /*RefQualifierLoc=*/NoLoc,
966 /*ConstQualifierLoc=*/NoLoc,
967 /*VolatileQualifierLoc=*/NoLoc,
968 MutableLoc,
969 EST_None,
970 /*ESpecLoc=*/NoLoc,
971 /*Exceptions=*/0,
972 /*ExceptionRanges=*/0,
973 /*NumExceptions=*/0,
974 /*NoexceptExpr=*/0,
975 DeclLoc, DeclEndLoc, D,
976 TrailingReturnType),
Douglas Gregor6746c5d2012-02-16 21:53:36 +0000977 Attr, DeclEndLoc);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000978 }
Douglas Gregor6746c5d2012-02-16 21:53:36 +0000979
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000980
Eli Friedman4817cf72012-01-06 03:05:34 +0000981 // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
982 // it.
Douglas Gregorb8389972012-02-21 22:51:27 +0000983 unsigned ScopeFlags = Scope::BlockScope | Scope::FnScope | Scope::DeclScope;
Douglas Gregorb8389972012-02-21 22:51:27 +0000984 ParseScope BodyScope(this, ScopeFlags);
Eli Friedman4817cf72012-01-06 03:05:34 +0000985
Eli Friedman71c80552012-01-05 03:35:19 +0000986 Actions.ActOnStartOfLambdaDefinition(Intro, D, getCurScope());
987
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000988 // Parse compound-statement.
Eli Friedmanc7c97142012-01-04 02:40:39 +0000989 if (!Tok.is(tok::l_brace)) {
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000990 Diag(Tok, diag::err_expected_lambda_body);
Eli Friedmanc7c97142012-01-04 02:40:39 +0000991 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
992 return ExprError();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000993 }
994
Eli Friedmanc7c97142012-01-04 02:40:39 +0000995 StmtResult Stmt(ParseCompoundStatementBody());
996 BodyScope.Exit();
997
Eli Friedman898caf82012-01-04 02:46:53 +0000998 if (!Stmt.isInvalid())
Douglas Gregor63798542012-02-20 19:44:39 +0000999 return Actions.ActOnLambdaExpr(LambdaBeginLoc, Stmt.take(), getCurScope());
Eli Friedmanc7c97142012-01-04 02:40:39 +00001000
Eli Friedman898caf82012-01-04 02:46:53 +00001001 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
1002 return ExprError();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001003}
1004
Chris Lattner29375652006-12-04 18:06:35 +00001005/// ParseCXXCasts - This handles the various ways to cast expressions to another
1006/// type.
1007///
1008/// postfix-expression: [C++ 5.2p1]
1009/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
1010/// 'static_cast' '<' type-name '>' '(' expression ')'
1011/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
1012/// 'const_cast' '<' type-name '>' '(' expression ')'
1013///
John McCalldadc5752010-08-24 06:29:42 +00001014ExprResult Parser::ParseCXXCasts() {
Chris Lattner29375652006-12-04 18:06:35 +00001015 tok::TokenKind Kind = Tok.getKind();
1016 const char *CastName = 0; // For error messages
1017
1018 switch (Kind) {
David Blaikieaa347f92011-09-23 20:26:49 +00001019 default: llvm_unreachable("Unknown C++ cast!");
Chris Lattner29375652006-12-04 18:06:35 +00001020 case tok::kw_const_cast: CastName = "const_cast"; break;
1021 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
1022 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
1023 case tok::kw_static_cast: CastName = "static_cast"; break;
1024 }
1025
1026 SourceLocation OpLoc = ConsumeToken();
1027 SourceLocation LAngleBracketLoc = Tok.getLocation();
1028
Richard Smith55858492011-04-14 21:45:45 +00001029 // Check for "<::" which is parsed as "[:". If found, fix token stream,
1030 // diagnose error, suggest fix, and recover parsing.
Richard Smith62e66302012-08-20 17:37:52 +00001031 if (Tok.is(tok::l_square) && Tok.getLength() == 2) {
1032 Token Next = NextToken();
1033 if (Next.is(tok::colon) && areTokensAdjacent(Tok, Next))
1034 FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
1035 }
Richard Smith55858492011-04-14 21:45:45 +00001036
Chris Lattner29375652006-12-04 18:06:35 +00001037 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redld65cea82008-12-11 22:51:44 +00001038 return ExprError();
Chris Lattner29375652006-12-04 18:06:35 +00001039
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +00001040 // Parse the common declaration-specifiers piece.
1041 DeclSpec DS(AttrFactory);
1042 ParseSpecifierQualifierList(DS);
1043
1044 // Parse the abstract-declarator, if present.
1045 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1046 ParseDeclarator(DeclaratorInfo);
1047
Chris Lattner29375652006-12-04 18:06:35 +00001048 SourceLocation RAngleBracketLoc = Tok.getLocation();
1049
Chris Lattner6d29c102008-11-18 07:48:38 +00001050 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redld65cea82008-12-11 22:51:44 +00001051 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Chris Lattner29375652006-12-04 18:06:35 +00001052
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001053 SourceLocation LParenLoc, RParenLoc;
1054 BalancedDelimiterTracker T(*this, tok::l_paren);
Chris Lattner29375652006-12-04 18:06:35 +00001055
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001056 if (T.expectAndConsume(diag::err_expected_lparen_after, CastName))
Argyrios Kyrtzidis387a3342009-05-22 10:23:16 +00001057 return ExprError();
Chris Lattner29375652006-12-04 18:06:35 +00001058
John McCalldadc5752010-08-24 06:29:42 +00001059 ExprResult Result = ParseExpression();
Mike Stump11289f42009-09-09 15:08:12 +00001060
Argyrios Kyrtzidis387a3342009-05-22 10:23:16 +00001061 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001062 T.consumeClose();
Chris Lattner29375652006-12-04 18:06:35 +00001063
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +00001064 if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
Douglas Gregore200adc2008-10-27 19:41:14 +00001065 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +00001066 LAngleBracketLoc, DeclaratorInfo,
Douglas Gregor220cac52009-02-18 17:45:20 +00001067 RAngleBracketLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001068 T.getOpenLocation(), Result.take(),
1069 T.getCloseLocation());
Chris Lattner29375652006-12-04 18:06:35 +00001070
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001071 return Result;
Chris Lattner29375652006-12-04 18:06:35 +00001072}
Bill Wendling4073ed52007-02-13 01:51:42 +00001073
Sebastian Redlc4704762008-11-11 11:37:55 +00001074/// ParseCXXTypeid - This handles the C++ typeid expression.
1075///
1076/// postfix-expression: [C++ 5.2p1]
1077/// 'typeid' '(' expression ')'
1078/// 'typeid' '(' type-id ')'
1079///
John McCalldadc5752010-08-24 06:29:42 +00001080ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc4704762008-11-11 11:37:55 +00001081 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
1082
1083 SourceLocation OpLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001084 SourceLocation LParenLoc, RParenLoc;
1085 BalancedDelimiterTracker T(*this, tok::l_paren);
Sebastian Redlc4704762008-11-11 11:37:55 +00001086
1087 // typeid expressions are always parenthesized.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001088 if (T.expectAndConsume(diag::err_expected_lparen_after, "typeid"))
Sebastian Redld65cea82008-12-11 22:51:44 +00001089 return ExprError();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001090 LParenLoc = T.getOpenLocation();
Sebastian Redlc4704762008-11-11 11:37:55 +00001091
John McCalldadc5752010-08-24 06:29:42 +00001092 ExprResult Result;
Sebastian Redlc4704762008-11-11 11:37:55 +00001093
Richard Smith4f605af2012-08-18 00:55:03 +00001094 // C++0x [expr.typeid]p3:
1095 // When typeid is applied to an expression other than an lvalue of a
1096 // polymorphic class type [...] The expression is an unevaluated
1097 // operand (Clause 5).
1098 //
1099 // Note that we can't tell whether the expression is an lvalue of a
1100 // polymorphic class type until after we've parsed the expression; we
1101 // speculatively assume the subexpression is unevaluated, and fix it up
1102 // later.
1103 //
1104 // We enter the unevaluated context before trying to determine whether we
1105 // have a type-id, because the tentative parse logic will try to resolve
1106 // names, and must treat them as unevaluated.
Eli Friedman15681d62012-09-26 04:34:21 +00001107 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
1108 Sema::ReuseLambdaContextDecl);
Richard Smith4f605af2012-08-18 00:55:03 +00001109
Sebastian Redlc4704762008-11-11 11:37:55 +00001110 if (isTypeIdInParens()) {
Douglas Gregor220cac52009-02-18 17:45:20 +00001111 TypeResult Ty = ParseTypeName();
Sebastian Redlc4704762008-11-11 11:37:55 +00001112
1113 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001114 T.consumeClose();
1115 RParenLoc = T.getCloseLocation();
Douglas Gregor4c7c1092010-09-08 23:14:30 +00001116 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +00001117 return ExprError();
Sebastian Redlc4704762008-11-11 11:37:55 +00001118
1119 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallba7bf592010-08-24 05:47:05 +00001120 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc4704762008-11-11 11:37:55 +00001121 } else {
1122 Result = ParseExpression();
1123
1124 // Match the ')'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001125 if (Result.isInvalid())
Sebastian Redlc4704762008-11-11 11:37:55 +00001126 SkipUntil(tok::r_paren);
1127 else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001128 T.consumeClose();
1129 RParenLoc = T.getCloseLocation();
Douglas Gregor4c7c1092010-09-08 23:14:30 +00001130 if (RParenLoc.isInvalid())
1131 return ExprError();
Douglas Gregor1beec452011-03-12 01:48:56 +00001132
Sebastian Redlc4704762008-11-11 11:37:55 +00001133 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001134 Result.release(), RParenLoc);
Sebastian Redlc4704762008-11-11 11:37:55 +00001135 }
1136 }
1137
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001138 return Result;
Sebastian Redlc4704762008-11-11 11:37:55 +00001139}
1140
Francois Pichet9f4f2072010-09-08 12:20:18 +00001141/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
1142///
1143/// '__uuidof' '(' expression ')'
1144/// '__uuidof' '(' type-id ')'
1145///
1146ExprResult Parser::ParseCXXUuidof() {
1147 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
1148
1149 SourceLocation OpLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001150 BalancedDelimiterTracker T(*this, tok::l_paren);
Francois Pichet9f4f2072010-09-08 12:20:18 +00001151
1152 // __uuidof expressions are always parenthesized.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001153 if (T.expectAndConsume(diag::err_expected_lparen_after, "__uuidof"))
Francois Pichet9f4f2072010-09-08 12:20:18 +00001154 return ExprError();
1155
1156 ExprResult Result;
1157
1158 if (isTypeIdInParens()) {
1159 TypeResult Ty = ParseTypeName();
1160
1161 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001162 T.consumeClose();
Francois Pichet9f4f2072010-09-08 12:20:18 +00001163
1164 if (Ty.isInvalid())
1165 return ExprError();
1166
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001167 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), /*isType=*/true,
1168 Ty.get().getAsOpaquePtr(),
1169 T.getCloseLocation());
Francois Pichet9f4f2072010-09-08 12:20:18 +00001170 } else {
1171 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1172 Result = ParseExpression();
1173
1174 // Match the ')'.
1175 if (Result.isInvalid())
1176 SkipUntil(tok::r_paren);
1177 else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001178 T.consumeClose();
Francois Pichet9f4f2072010-09-08 12:20:18 +00001179
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001180 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(),
1181 /*isType=*/false,
1182 Result.release(), T.getCloseLocation());
Francois Pichet9f4f2072010-09-08 12:20:18 +00001183 }
1184 }
1185
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001186 return Result;
Francois Pichet9f4f2072010-09-08 12:20:18 +00001187}
1188
Douglas Gregore610ada2010-02-24 18:44:31 +00001189/// \brief Parse a C++ pseudo-destructor expression after the base,
1190/// . or -> operator, and nested-name-specifier have already been
1191/// parsed.
1192///
1193/// postfix-expression: [C++ 5.2]
1194/// postfix-expression . pseudo-destructor-name
1195/// postfix-expression -> pseudo-destructor-name
1196///
1197/// pseudo-destructor-name:
1198/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
1199/// ::[opt] nested-name-specifier template simple-template-id ::
1200/// ~type-name
1201/// ::[opt] nested-name-specifier[opt] ~type-name
1202///
John McCalldadc5752010-08-24 06:29:42 +00001203ExprResult
Douglas Gregore610ada2010-02-24 18:44:31 +00001204Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
1205 tok::TokenKind OpKind,
1206 CXXScopeSpec &SS,
John McCallba7bf592010-08-24 05:47:05 +00001207 ParsedType ObjectType) {
Douglas Gregore610ada2010-02-24 18:44:31 +00001208 // We're parsing either a pseudo-destructor-name or a dependent
1209 // member access that has the same form as a
1210 // pseudo-destructor-name. We parse both in the same way and let
1211 // the action model sort them out.
1212 //
1213 // Note that the ::[opt] nested-name-specifier[opt] has already
1214 // been parsed, and if there was a simple-template-id, it has
1215 // been coalesced into a template-id annotation token.
1216 UnqualifiedId FirstTypeName;
1217 SourceLocation CCLoc;
1218 if (Tok.is(tok::identifier)) {
1219 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1220 ConsumeToken();
1221 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1222 CCLoc = ConsumeToken();
1223 } else if (Tok.is(tok::annot_template_id)) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001224 // FIXME: retrieve TemplateKWLoc from template-id annotation and
1225 // store it in the pseudo-dtor node (to be used when instantiating it).
Douglas Gregore610ada2010-02-24 18:44:31 +00001226 FirstTypeName.setTemplateId(
1227 (TemplateIdAnnotation *)Tok.getAnnotationValue());
1228 ConsumeToken();
1229 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1230 CCLoc = ConsumeToken();
1231 } else {
1232 FirstTypeName.setIdentifier(0, SourceLocation());
1233 }
1234
1235 // Parse the tilde.
1236 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
1237 SourceLocation TildeLoc = ConsumeToken();
David Blaikie1d578782011-12-16 16:03:09 +00001238
1239 if (Tok.is(tok::kw_decltype) && !FirstTypeName.isValid() && SS.isEmpty()) {
1240 DeclSpec DS(AttrFactory);
Benjamin Kramer198e0832011-12-18 12:18:02 +00001241 ParseDecltypeSpecifier(DS);
David Blaikie1d578782011-12-16 16:03:09 +00001242 if (DS.getTypeSpecType() == TST_error)
1243 return ExprError();
1244 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, OpLoc,
1245 OpKind, TildeLoc, DS,
1246 Tok.is(tok::l_paren));
1247 }
1248
Douglas Gregore610ada2010-02-24 18:44:31 +00001249 if (!Tok.is(tok::identifier)) {
1250 Diag(Tok, diag::err_destructor_tilde_identifier);
1251 return ExprError();
1252 }
1253
1254 // Parse the second type.
1255 UnqualifiedId SecondTypeName;
1256 IdentifierInfo *Name = Tok.getIdentifierInfo();
1257 SourceLocation NameLoc = ConsumeToken();
1258 SecondTypeName.setIdentifier(Name, NameLoc);
1259
1260 // If there is a '<', the second type name is a template-id. Parse
1261 // it as such.
1262 if (Tok.is(tok::less) &&
Abramo Bagnara7945c982012-01-27 09:46:47 +00001263 ParseUnqualifiedIdTemplateId(SS, SourceLocation(),
1264 Name, NameLoc,
1265 false, ObjectType, SecondTypeName,
1266 /*AssumeTemplateName=*/true))
Douglas Gregore610ada2010-02-24 18:44:31 +00001267 return ExprError();
1268
John McCallb268a282010-08-23 23:25:46 +00001269 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
1270 OpLoc, OpKind,
Douglas Gregore610ada2010-02-24 18:44:31 +00001271 SS, FirstTypeName, CCLoc,
1272 TildeLoc, SecondTypeName,
1273 Tok.is(tok::l_paren));
1274}
1275
Bill Wendling4073ed52007-02-13 01:51:42 +00001276/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
1277///
1278/// boolean-literal: [C++ 2.13.5]
1279/// 'true'
1280/// 'false'
John McCalldadc5752010-08-24 06:29:42 +00001281ExprResult Parser::ParseCXXBoolLiteral() {
Bill Wendling4073ed52007-02-13 01:51:42 +00001282 tok::TokenKind Kind = Tok.getKind();
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001283 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Bill Wendling4073ed52007-02-13 01:51:42 +00001284}
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001285
1286/// ParseThrowExpression - This handles the C++ throw expression.
1287///
1288/// throw-expression: [C++ 15]
1289/// 'throw' assignment-expression[opt]
John McCalldadc5752010-08-24 06:29:42 +00001290ExprResult Parser::ParseThrowExpression() {
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001291 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001292 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redld65cea82008-12-11 22:51:44 +00001293
Chris Lattner65dd8432008-04-06 06:02:23 +00001294 // If the current token isn't the start of an assignment-expression,
1295 // then the expression is not present. This handles things like:
1296 // "C ? throw : (void)42", which is crazy but legal.
1297 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
1298 case tok::semi:
1299 case tok::r_paren:
1300 case tok::r_square:
1301 case tok::r_brace:
1302 case tok::colon:
1303 case tok::comma:
Douglas Gregor53e191ed2011-07-06 22:04:06 +00001304 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0);
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001305
Chris Lattner65dd8432008-04-06 06:02:23 +00001306 default:
John McCalldadc5752010-08-24 06:29:42 +00001307 ExprResult Expr(ParseAssignmentExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001308 if (Expr.isInvalid()) return Expr;
Douglas Gregor53e191ed2011-07-06 22:04:06 +00001309 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take());
Chris Lattner65dd8432008-04-06 06:02:23 +00001310 }
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001311}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001312
1313/// ParseCXXThis - This handles the C++ 'this' pointer.
1314///
1315/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
1316/// a non-lvalue expression whose value is the address of the object for which
1317/// the function is called.
John McCalldadc5752010-08-24 06:29:42 +00001318ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001319 assert(Tok.is(tok::kw_this) && "Not 'this'!");
1320 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001321 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001322}
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001323
1324/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1325/// Can be interpreted either as function-style casting ("int(x)")
1326/// or class type construction ("ClassType(x,y,z)")
1327/// or creation of a value-initialized type ("int()").
Sebastian Redl3da34892011-06-05 12:23:16 +00001328/// See [C++ 5.2.3].
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001329///
1330/// postfix-expression: [C++ 5.2p1]
Sebastian Redl3da34892011-06-05 12:23:16 +00001331/// simple-type-specifier '(' expression-list[opt] ')'
1332/// [C++0x] simple-type-specifier braced-init-list
1333/// typename-specifier '(' expression-list[opt] ')'
1334/// [C++0x] typename-specifier braced-init-list
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001335///
John McCalldadc5752010-08-24 06:29:42 +00001336ExprResult
Sebastian Redld65cea82008-12-11 22:51:44 +00001337Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001338 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallba7bf592010-08-24 05:47:05 +00001339 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001340
Sebastian Redl3da34892011-06-05 12:23:16 +00001341 assert((Tok.is(tok::l_paren) ||
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001342 (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)))
Sebastian Redl3da34892011-06-05 12:23:16 +00001343 && "Expected '(' or '{'!");
Douglas Gregor94a32472011-01-11 00:33:19 +00001344
Sebastian Redl3da34892011-06-05 12:23:16 +00001345 if (Tok.is(tok::l_brace)) {
Sebastian Redld74dd492012-02-12 18:41:05 +00001346 ExprResult Init = ParseBraceInitializer();
1347 if (Init.isInvalid())
1348 return Init;
1349 Expr *InitList = Init.take();
1350 return Actions.ActOnCXXTypeConstructExpr(TypeRep, SourceLocation(),
1351 MultiExprArg(&InitList, 1),
1352 SourceLocation());
Sebastian Redl3da34892011-06-05 12:23:16 +00001353 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001354 BalancedDelimiterTracker T(*this, tok::l_paren);
1355 T.consumeOpen();
Sebastian Redl3da34892011-06-05 12:23:16 +00001356
Benjamin Kramerf0623432012-08-23 22:51:59 +00001357 ExprVector Exprs;
Sebastian Redl3da34892011-06-05 12:23:16 +00001358 CommaLocsTy CommaLocs;
1359
1360 if (Tok.isNot(tok::r_paren)) {
1361 if (ParseExpressionList(Exprs, CommaLocs)) {
1362 SkipUntil(tok::r_paren);
1363 return ExprError();
1364 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001365 }
Sebastian Redl3da34892011-06-05 12:23:16 +00001366
1367 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001368 T.consumeClose();
Sebastian Redl3da34892011-06-05 12:23:16 +00001369
1370 // TypeRep could be null, if it references an invalid typedef.
1371 if (!TypeRep)
1372 return ExprError();
1373
1374 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1375 "Unexpected number of commas!");
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001376 return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001377 Exprs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001378 T.getCloseLocation());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001379 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001380}
1381
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001382/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001383///
1384/// condition:
1385/// expression
1386/// type-specifier-seq declarator '=' assignment-expression
Richard Smith2a15b742012-02-22 06:49:09 +00001387/// [C++11] type-specifier-seq declarator '=' initializer-clause
1388/// [C++11] type-specifier-seq declarator braced-init-list
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001389/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
1390/// '=' assignment-expression
1391///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00001392/// \param ExprOut if the condition was parsed as an expression, the parsed
1393/// expression.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001394///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00001395/// \param DeclOut if the condition was parsed as a declaration, the parsed
1396/// declaration.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001397///
Douglas Gregore60e41a2010-05-06 17:25:47 +00001398/// \param Loc The location of the start of the statement that requires this
1399/// condition, e.g., the "for" in a for loop.
1400///
1401/// \param ConvertToBoolean Whether the condition expression should be
1402/// converted to a boolean value.
1403///
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001404/// \returns true if there was a parsing, false otherwise.
John McCalldadc5752010-08-24 06:29:42 +00001405bool Parser::ParseCXXCondition(ExprResult &ExprOut,
1406 Decl *&DeclOut,
Douglas Gregore60e41a2010-05-06 17:25:47 +00001407 SourceLocation Loc,
1408 bool ConvertToBoolean) {
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001409 if (Tok.is(tok::code_completion)) {
John McCallfaf5fb42010-08-26 23:41:50 +00001410 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001411 cutOffParsing();
1412 return true;
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001413 }
1414
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001415 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00001416 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001417
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001418 if (!isCXXConditionDeclaration()) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001419 ProhibitAttributes(attrs);
1420
Douglas Gregore60e41a2010-05-06 17:25:47 +00001421 // Parse the expression.
John McCalldadc5752010-08-24 06:29:42 +00001422 ExprOut = ParseExpression(); // expression
1423 DeclOut = 0;
1424 if (ExprOut.isInvalid())
Douglas Gregore60e41a2010-05-06 17:25:47 +00001425 return true;
1426
1427 // If required, convert to a boolean value.
1428 if (ConvertToBoolean)
John McCalldadc5752010-08-24 06:29:42 +00001429 ExprOut
1430 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
1431 return ExprOut.isInvalid();
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001432 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001433
1434 // type-specifier-seq
John McCall084e83d2011-03-24 11:26:52 +00001435 DeclSpec DS(AttrFactory);
Richard Smith54ecd982013-02-20 19:22:51 +00001436 DS.takeAttributesFrom(attrs);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001437 ParseSpecifierQualifierList(DS);
1438
1439 // declarator
1440 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
1441 ParseDeclarator(DeclaratorInfo);
1442
1443 // simple-asm-expr[opt]
1444 if (Tok.is(tok::kw_asm)) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001445 SourceLocation Loc;
John McCalldadc5752010-08-24 06:29:42 +00001446 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001447 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001448 SkipUntil(tok::semi);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001449 return true;
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001450 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001451 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001452 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001453 }
1454
1455 // If attributes are present, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001456 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001457
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001458 // Type-check the declaration itself.
John McCalldadc5752010-08-24 06:29:42 +00001459 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall53fa7142010-12-24 02:08:15 +00001460 DeclaratorInfo);
John McCalldadc5752010-08-24 06:29:42 +00001461 DeclOut = Dcl.get();
1462 ExprOut = ExprError();
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +00001463
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001464 // '=' assignment-expression
Richard Trieuc64d3232012-01-18 22:54:52 +00001465 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Smith2a15b742012-02-22 06:49:09 +00001466 bool CopyInitialization = isTokenEqualOrEqualTypo();
1467 if (CopyInitialization)
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001468 ConsumeToken();
Richard Smith2a15b742012-02-22 06:49:09 +00001469
1470 ExprResult InitExpr = ExprError();
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001471 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith2a15b742012-02-22 06:49:09 +00001472 Diag(Tok.getLocation(),
1473 diag::warn_cxx98_compat_generalized_initializer_lists);
1474 InitExpr = ParseBraceInitializer();
1475 } else if (CopyInitialization) {
1476 InitExpr = ParseAssignmentExpression();
1477 } else if (Tok.is(tok::l_paren)) {
1478 // This was probably an attempt to initialize the variable.
1479 SourceLocation LParen = ConsumeParen(), RParen = LParen;
1480 if (SkipUntil(tok::r_paren, true, /*DontConsume=*/true))
1481 RParen = ConsumeParen();
1482 Diag(DeclOut ? DeclOut->getLocation() : LParen,
1483 diag::err_expected_init_in_condition_lparen)
1484 << SourceRange(LParen, RParen);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001485 } else {
Richard Smith2a15b742012-02-22 06:49:09 +00001486 Diag(DeclOut ? DeclOut->getLocation() : Tok.getLocation(),
1487 diag::err_expected_init_in_condition);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001488 }
Richard Smith2a15b742012-02-22 06:49:09 +00001489
1490 if (!InitExpr.isInvalid())
1491 Actions.AddInitializerToDecl(DeclOut, InitExpr.take(), !CopyInitialization,
Richard Smith74aeef52013-04-26 16:15:35 +00001492 DS.containsPlaceholderType());
Richard Smith27d807c2013-04-30 13:56:41 +00001493 else
1494 Actions.ActOnInitializerError(DeclOut);
Richard Smith2a15b742012-02-22 06:49:09 +00001495
Douglas Gregore60e41a2010-05-06 17:25:47 +00001496 // FIXME: Build a reference to this declaration? Convert it to bool?
1497 // (This is currently handled by Sema).
Richard Smithb2bc2e62011-02-21 20:05:19 +00001498
1499 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregore60e41a2010-05-06 17:25:47 +00001500
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001501 return false;
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001502}
1503
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001504/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1505/// This should only be called when the current token is known to be part of
1506/// simple-type-specifier.
1507///
1508/// simple-type-specifier:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001509/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001510/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1511/// char
1512/// wchar_t
1513/// bool
1514/// short
1515/// int
1516/// long
1517/// signed
1518/// unsigned
1519/// float
1520/// double
1521/// void
1522/// [GNU] typeof-specifier
1523/// [C++0x] auto [TODO]
1524///
1525/// type-name:
1526/// class-name
1527/// enum-name
1528/// typedef-name
1529///
1530void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1531 DS.SetRangeStart(Tok.getLocation());
1532 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +00001533 unsigned DiagID;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001534 SourceLocation Loc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001535
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001536 switch (Tok.getKind()) {
Chris Lattner45ddec32009-01-05 00:13:00 +00001537 case tok::identifier: // foo::bar
1538 case tok::coloncolon: // ::foo::bar
David Blaikie83d382b2011-09-23 05:06:16 +00001539 llvm_unreachable("Annotation token should already be formed!");
Mike Stump11289f42009-09-09 15:08:12 +00001540 default:
David Blaikie83d382b2011-09-23 05:06:16 +00001541 llvm_unreachable("Not a simple-type-specifier token!");
Chris Lattner45ddec32009-01-05 00:13:00 +00001542
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001543 // type-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00001544 case tok::annot_typename: {
Douglas Gregor0231d8d2011-01-19 20:10:05 +00001545 if (getTypeAnnotation(Tok))
1546 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
1547 getTypeAnnotation(Tok));
1548 else
1549 DS.SetTypeSpecError();
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001550
1551 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1552 ConsumeToken();
1553
1554 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1555 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1556 // Objective-C interface. If we don't have Objective-C or a '<', this is
1557 // just a normal reference to a typedef name.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001558 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001559 ParseObjCProtocolQualifiers(DS);
1560
1561 DS.Finish(Diags, PP);
1562 return;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001563 }
Mike Stump11289f42009-09-09 15:08:12 +00001564
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001565 // builtin types
1566 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001567 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001568 break;
1569 case tok::kw_long:
John McCall49bfce42009-08-03 20:12:06 +00001570 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001571 break;
Francois Pichet84133e42011-04-28 01:59:37 +00001572 case tok::kw___int64:
1573 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1574 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001575 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001576 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001577 break;
1578 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001579 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001580 break;
1581 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001582 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001583 break;
1584 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001585 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001586 break;
1587 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001588 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001589 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00001590 case tok::kw___int128:
1591 DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, DiagID);
1592 break;
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00001593 case tok::kw_half:
1594 DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
1595 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001596 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001597 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001598 break;
1599 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001600 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001601 break;
1602 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001603 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001604 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001605 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001606 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001607 break;
1608 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001609 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001610 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001611 case tok::kw_bool:
John McCall49bfce42009-08-03 20:12:06 +00001612 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001613 break;
David Blaikie25896afb2012-01-24 05:47:35 +00001614 case tok::annot_decltype:
1615 case tok::kw_decltype:
1616 DS.SetRangeEnd(ParseDecltypeSpecifier(DS));
1617 return DS.Finish(Diags, PP);
Mike Stump11289f42009-09-09 15:08:12 +00001618
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001619 // GNU typeof support.
1620 case tok::kw_typeof:
1621 ParseTypeofSpecifier(DS);
Douglas Gregore3e01a22009-04-01 22:41:11 +00001622 DS.Finish(Diags, PP);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001623 return;
1624 }
Chris Lattnera8a3f732009-01-06 05:06:21 +00001625 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001626 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1627 else
1628 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001629 ConsumeToken();
Douglas Gregore3e01a22009-04-01 22:41:11 +00001630 DS.Finish(Diags, PP);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001631}
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001632
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001633/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1634/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1635/// e.g., "const short int". Note that the DeclSpec is *not* finished
1636/// by parsing the type-specifier-seq, because these sequences are
1637/// typically followed by some form of declarator. Returns true and
1638/// emits diagnostics if this is not a type-specifier-seq, false
1639/// otherwise.
1640///
1641/// type-specifier-seq: [C++ 8.1]
1642/// type-specifier type-specifier-seq[opt]
1643///
1644bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
Richard Smithc5b05522012-03-12 07:56:15 +00001645 ParseSpecifierQualifierList(DS, AS_none, DSC_type_specifier);
Douglas Gregor40d732f2010-02-24 23:13:13 +00001646 DS.Finish(Diags, PP);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001647 return false;
1648}
1649
Douglas Gregor7861a802009-11-03 01:35:08 +00001650/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1651/// some form.
1652///
1653/// This routine is invoked when a '<' is encountered after an identifier or
1654/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1655/// whether the unqualified-id is actually a template-id. This routine will
1656/// then parse the template arguments and form the appropriate template-id to
1657/// return to the caller.
1658///
1659/// \param SS the nested-name-specifier that precedes this template-id, if
1660/// we're actually parsing a qualified-id.
1661///
1662/// \param Name for constructor and destructor names, this is the actual
1663/// identifier that may be a template-name.
1664///
1665/// \param NameLoc the location of the class-name in a constructor or
1666/// destructor.
1667///
1668/// \param EnteringContext whether we're entering the scope of the
1669/// nested-name-specifier.
1670///
Douglas Gregor127ea592009-11-03 21:24:04 +00001671/// \param ObjectType if this unqualified-id occurs within a member access
1672/// expression, the type of the base object whose member is being accessed.
1673///
Douglas Gregor7861a802009-11-03 01:35:08 +00001674/// \param Id as input, describes the template-name or operator-function-id
1675/// that precedes the '<'. If template arguments were parsed successfully,
1676/// will be updated with the template-id.
1677///
Douglas Gregore610ada2010-02-24 18:44:31 +00001678/// \param AssumeTemplateId When true, this routine will assume that the name
1679/// refers to a template without performing name lookup to verify.
1680///
Douglas Gregor7861a802009-11-03 01:35:08 +00001681/// \returns true if a parse error occurred, false otherwise.
1682bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001683 SourceLocation TemplateKWLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00001684 IdentifierInfo *Name,
1685 SourceLocation NameLoc,
1686 bool EnteringContext,
John McCallba7bf592010-08-24 05:47:05 +00001687 ParsedType ObjectType,
Douglas Gregore610ada2010-02-24 18:44:31 +00001688 UnqualifiedId &Id,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001689 bool AssumeTemplateId) {
Douglas Gregorb22ee882010-05-05 05:58:24 +00001690 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1691 "Expected '<' to finish parsing a template-id");
Douglas Gregor7861a802009-11-03 01:35:08 +00001692
1693 TemplateTy Template;
1694 TemplateNameKind TNK = TNK_Non_template;
1695 switch (Id.getKind()) {
1696 case UnqualifiedId::IK_Identifier:
Douglas Gregor3cf81312009-11-03 23:16:33 +00001697 case UnqualifiedId::IK_OperatorFunctionId:
Alexis Hunted0530f2009-11-28 08:58:14 +00001698 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregore610ada2010-02-24 18:44:31 +00001699 if (AssumeTemplateId) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001700 TNK = Actions.ActOnDependentTemplateName(getCurScope(), SS, TemplateKWLoc,
Douglas Gregorbb119652010-06-16 23:00:59 +00001701 Id, ObjectType, EnteringContext,
1702 Template);
1703 if (TNK == TNK_Non_template)
1704 return true;
Douglas Gregor786123d2010-05-21 23:18:07 +00001705 } else {
1706 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001707 TNK = Actions.isTemplateName(getCurScope(), SS,
1708 TemplateKWLoc.isValid(), Id,
1709 ObjectType, EnteringContext, Template,
Douglas Gregor786123d2010-05-21 23:18:07 +00001710 MemberOfUnknownSpecialization);
1711
1712 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1713 ObjectType && IsTemplateArgumentList()) {
1714 // We have something like t->getAs<T>(), where getAs is a
1715 // member of an unknown specialization. However, this will only
1716 // parse correctly as a template, so suggest the keyword 'template'
1717 // before 'getAs' and treat this as a dependent template name.
1718 std::string Name;
1719 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1720 Name = Id.Identifier->getName();
1721 else {
1722 Name = "operator ";
1723 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1724 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1725 else
1726 Name += Id.Identifier->getName();
1727 }
1728 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1729 << Name
1730 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Abramo Bagnara7945c982012-01-27 09:46:47 +00001731 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1732 SS, TemplateKWLoc, Id,
1733 ObjectType, EnteringContext,
1734 Template);
Douglas Gregorbb119652010-06-16 23:00:59 +00001735 if (TNK == TNK_Non_template)
Douglas Gregor786123d2010-05-21 23:18:07 +00001736 return true;
1737 }
1738 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001739 break;
1740
Douglas Gregor3cf81312009-11-03 23:16:33 +00001741 case UnqualifiedId::IK_ConstructorName: {
1742 UnqualifiedId TemplateName;
Douglas Gregor786123d2010-05-21 23:18:07 +00001743 bool MemberOfUnknownSpecialization;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001744 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001745 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1746 TemplateName, ObjectType,
Douglas Gregor786123d2010-05-21 23:18:07 +00001747 EnteringContext, Template,
1748 MemberOfUnknownSpecialization);
Douglas Gregor7861a802009-11-03 01:35:08 +00001749 break;
1750 }
1751
Douglas Gregor3cf81312009-11-03 23:16:33 +00001752 case UnqualifiedId::IK_DestructorName: {
1753 UnqualifiedId TemplateName;
Douglas Gregor786123d2010-05-21 23:18:07 +00001754 bool MemberOfUnknownSpecialization;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001755 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001756 if (ObjectType) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001757 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1758 SS, TemplateKWLoc, TemplateName,
1759 ObjectType, EnteringContext,
1760 Template);
Douglas Gregorbb119652010-06-16 23:00:59 +00001761 if (TNK == TNK_Non_template)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001762 return true;
1763 } else {
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001764 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1765 TemplateName, ObjectType,
Douglas Gregor786123d2010-05-21 23:18:07 +00001766 EnteringContext, Template,
1767 MemberOfUnknownSpecialization);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001768
John McCallba7bf592010-08-24 05:47:05 +00001769 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00001770 Diag(NameLoc, diag::err_destructor_template_id)
1771 << Name << SS.getRange();
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001772 return true;
1773 }
1774 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001775 break;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001776 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001777
1778 default:
1779 return false;
1780 }
1781
1782 if (TNK == TNK_Non_template)
1783 return false;
1784
1785 // Parse the enclosed template argument list.
1786 SourceLocation LAngleLoc, RAngleLoc;
1787 TemplateArgList TemplateArgs;
Douglas Gregorb22ee882010-05-05 05:58:24 +00001788 if (Tok.is(tok::less) &&
1789 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregore7c20652011-03-02 00:47:37 +00001790 SS, true, LAngleLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00001791 TemplateArgs,
Douglas Gregor7861a802009-11-03 01:35:08 +00001792 RAngleLoc))
1793 return true;
1794
1795 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Alexis Hunted0530f2009-11-28 08:58:14 +00001796 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1797 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor7861a802009-11-03 01:35:08 +00001798 // Form a parsed representation of the template-id to be stored in the
1799 // UnqualifiedId.
1800 TemplateIdAnnotation *TemplateId
Benjamin Kramer1e6b6062012-04-14 12:14:03 +00001801 = TemplateIdAnnotation::Allocate(TemplateArgs.size(), TemplateIds);
Douglas Gregor7861a802009-11-03 01:35:08 +00001802
1803 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1804 TemplateId->Name = Id.Identifier;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001805 TemplateId->Operator = OO_None;
Douglas Gregor7861a802009-11-03 01:35:08 +00001806 TemplateId->TemplateNameLoc = Id.StartLocation;
1807 } else {
Douglas Gregor3cf81312009-11-03 23:16:33 +00001808 TemplateId->Name = 0;
1809 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1810 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor7861a802009-11-03 01:35:08 +00001811 }
1812
Douglas Gregore7c20652011-03-02 00:47:37 +00001813 TemplateId->SS = SS;
Benjamin Kramer807c2db2012-02-19 23:37:39 +00001814 TemplateId->TemplateKWLoc = TemplateKWLoc;
John McCall3e56fd42010-08-23 07:28:44 +00001815 TemplateId->Template = Template;
Douglas Gregor7861a802009-11-03 01:35:08 +00001816 TemplateId->Kind = TNK;
1817 TemplateId->LAngleLoc = LAngleLoc;
1818 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001819 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor7861a802009-11-03 01:35:08 +00001820 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001821 Arg != ArgEnd; ++Arg)
Douglas Gregor7861a802009-11-03 01:35:08 +00001822 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor7861a802009-11-03 01:35:08 +00001823
1824 Id.setTemplateId(TemplateId);
1825 return false;
1826 }
1827
1828 // Bundle the template arguments together.
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001829 ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs);
Abramo Bagnara4244b432012-01-27 08:46:19 +00001830
Douglas Gregor7861a802009-11-03 01:35:08 +00001831 // Constructor and destructor names.
John McCallfaf5fb42010-08-26 23:41:50 +00001832 TypeResult Type
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001833 = Actions.ActOnTemplateIdType(SS, TemplateKWLoc,
1834 Template, NameLoc,
Abramo Bagnara4244b432012-01-27 08:46:19 +00001835 LAngleLoc, TemplateArgsPtr, RAngleLoc,
1836 /*IsCtorOrDtorName=*/true);
Douglas Gregor7861a802009-11-03 01:35:08 +00001837 if (Type.isInvalid())
1838 return true;
1839
1840 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1841 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1842 else
1843 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1844
1845 return false;
1846}
1847
Douglas Gregor71395fa2009-11-04 00:56:37 +00001848/// \brief Parse an operator-function-id or conversion-function-id as part
1849/// of a C++ unqualified-id.
1850///
1851/// This routine is responsible only for parsing the operator-function-id or
1852/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor7861a802009-11-03 01:35:08 +00001853///
1854/// \code
Douglas Gregor7861a802009-11-03 01:35:08 +00001855/// operator-function-id: [C++ 13.5]
1856/// 'operator' operator
1857///
Douglas Gregor71395fa2009-11-04 00:56:37 +00001858/// operator: one of
Douglas Gregor7861a802009-11-03 01:35:08 +00001859/// new delete new[] delete[]
1860/// + - * / % ^ & | ~
1861/// ! = < > += -= *= /= %=
1862/// ^= &= |= << >> >>= <<= == !=
1863/// <= >= && || ++ -- , ->* ->
1864/// () []
1865///
1866/// conversion-function-id: [C++ 12.3.2]
1867/// operator conversion-type-id
1868///
1869/// conversion-type-id:
1870/// type-specifier-seq conversion-declarator[opt]
1871///
1872/// conversion-declarator:
1873/// ptr-operator conversion-declarator[opt]
1874/// \endcode
1875///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00001876/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregor7861a802009-11-03 01:35:08 +00001877/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1878///
1879/// \param EnteringContext whether we are entering the scope of the
1880/// nested-name-specifier.
1881///
Douglas Gregor71395fa2009-11-04 00:56:37 +00001882/// \param ObjectType if this unqualified-id occurs within a member access
1883/// expression, the type of the base object whose member is being accessed.
1884///
1885/// \param Result on a successful parse, contains the parsed unqualified-id.
1886///
1887/// \returns true if parsing fails, false otherwise.
1888bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallba7bf592010-08-24 05:47:05 +00001889 ParsedType ObjectType,
Douglas Gregor71395fa2009-11-04 00:56:37 +00001890 UnqualifiedId &Result) {
1891 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1892
1893 // Consume the 'operator' keyword.
1894 SourceLocation KeywordLoc = ConsumeToken();
1895
1896 // Determine what kind of operator name we have.
1897 unsigned SymbolIdx = 0;
1898 SourceLocation SymbolLocations[3];
1899 OverloadedOperatorKind Op = OO_None;
1900 switch (Tok.getKind()) {
1901 case tok::kw_new:
1902 case tok::kw_delete: {
1903 bool isNew = Tok.getKind() == tok::kw_new;
1904 // Consume the 'new' or 'delete'.
1905 SymbolLocations[SymbolIdx++] = ConsumeToken();
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001906 // Check for array new/delete.
1907 if (Tok.is(tok::l_square) &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001908 (!getLangOpts().CPlusPlus11 || NextToken().isNot(tok::l_square))) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001909 // Consume the '[' and ']'.
1910 BalancedDelimiterTracker T(*this, tok::l_square);
1911 T.consumeOpen();
1912 T.consumeClose();
1913 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00001914 return true;
1915
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001916 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1917 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00001918 Op = isNew? OO_Array_New : OO_Array_Delete;
1919 } else {
1920 Op = isNew? OO_New : OO_Delete;
1921 }
1922 break;
1923 }
1924
1925#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1926 case tok::Token: \
1927 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1928 Op = OO_##Name; \
1929 break;
1930#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
1931#include "clang/Basic/OperatorKinds.def"
1932
1933 case tok::l_paren: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001934 // Consume the '(' and ')'.
1935 BalancedDelimiterTracker T(*this, tok::l_paren);
1936 T.consumeOpen();
1937 T.consumeClose();
1938 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00001939 return true;
1940
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001941 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1942 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00001943 Op = OO_Call;
1944 break;
1945 }
1946
1947 case tok::l_square: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001948 // Consume the '[' and ']'.
1949 BalancedDelimiterTracker T(*this, tok::l_square);
1950 T.consumeOpen();
1951 T.consumeClose();
1952 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00001953 return true;
1954
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001955 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1956 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00001957 Op = OO_Subscript;
1958 break;
1959 }
1960
1961 case tok::code_completion: {
1962 // Code completion for the operator name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001963 Actions.CodeCompleteOperatorName(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001964 cutOffParsing();
Douglas Gregor71395fa2009-11-04 00:56:37 +00001965 // Don't try to parse any further.
1966 return true;
1967 }
1968
1969 default:
1970 break;
1971 }
1972
1973 if (Op != OO_None) {
1974 // We have parsed an operator-function-id.
1975 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
1976 return false;
1977 }
Alexis Hunt34458502009-11-28 04:44:28 +00001978
1979 // Parse a literal-operator-id.
1980 //
Richard Smith6f212062012-10-20 08:41:10 +00001981 // literal-operator-id: C++11 [over.literal]
1982 // operator string-literal identifier
1983 // operator user-defined-string-literal
Alexis Hunt34458502009-11-28 04:44:28 +00001984
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001985 if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
Richard Smith5d164bc2011-10-15 05:09:34 +00001986 Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator);
Alexis Hunt34458502009-11-28 04:44:28 +00001987
Richard Smith7d182a72012-03-08 23:06:02 +00001988 SourceLocation DiagLoc;
1989 unsigned DiagId = 0;
1990
1991 // We're past translation phase 6, so perform string literal concatenation
1992 // before checking for "".
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001993 SmallVector<Token, 4> Toks;
1994 SmallVector<SourceLocation, 4> TokLocs;
Richard Smith7d182a72012-03-08 23:06:02 +00001995 while (isTokenStringLiteral()) {
1996 if (!Tok.is(tok::string_literal) && !DiagId) {
Richard Smith6f212062012-10-20 08:41:10 +00001997 // C++11 [over.literal]p1:
1998 // The string-literal or user-defined-string-literal in a
1999 // literal-operator-id shall have no encoding-prefix [...].
Richard Smith7d182a72012-03-08 23:06:02 +00002000 DiagLoc = Tok.getLocation();
2001 DiagId = diag::err_literal_operator_string_prefix;
2002 }
2003 Toks.push_back(Tok);
2004 TokLocs.push_back(ConsumeStringToken());
2005 }
2006
2007 StringLiteralParser Literal(Toks.data(), Toks.size(), PP);
2008 if (Literal.hadError)
2009 return true;
2010
2011 // Grab the literal operator's suffix, which will be either the next token
2012 // or a ud-suffix from the string literal.
2013 IdentifierInfo *II = 0;
2014 SourceLocation SuffixLoc;
2015 if (!Literal.getUDSuffix().empty()) {
2016 II = &PP.getIdentifierTable().get(Literal.getUDSuffix());
2017 SuffixLoc =
2018 Lexer::AdvanceToTokenCharacter(TokLocs[Literal.getUDSuffixToken()],
2019 Literal.getUDSuffixOffset(),
David Blaikiebbafb8a2012-03-11 07:00:24 +00002020 PP.getSourceManager(), getLangOpts());
Richard Smith7d182a72012-03-08 23:06:02 +00002021 } else if (Tok.is(tok::identifier)) {
2022 II = Tok.getIdentifierInfo();
2023 SuffixLoc = ConsumeToken();
2024 TokLocs.push_back(SuffixLoc);
2025 } else {
Alexis Hunt34458502009-11-28 04:44:28 +00002026 Diag(Tok.getLocation(), diag::err_expected_ident);
2027 return true;
2028 }
2029
Richard Smith7d182a72012-03-08 23:06:02 +00002030 // The string literal must be empty.
2031 if (!Literal.GetString().empty() || Literal.Pascal) {
Richard Smith6f212062012-10-20 08:41:10 +00002032 // C++11 [over.literal]p1:
2033 // The string-literal or user-defined-string-literal in a
2034 // literal-operator-id shall [...] contain no characters
2035 // other than the implicit terminating '\0'.
Richard Smith7d182a72012-03-08 23:06:02 +00002036 DiagLoc = TokLocs.front();
2037 DiagId = diag::err_literal_operator_string_not_empty;
2038 }
2039
2040 if (DiagId) {
2041 // This isn't a valid literal-operator-id, but we think we know
2042 // what the user meant. Tell them what they should have written.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002043 SmallString<32> Str;
Richard Smith7d182a72012-03-08 23:06:02 +00002044 Str += "\"\" ";
2045 Str += II->getName();
2046 Diag(DiagLoc, DiagId) << FixItHint::CreateReplacement(
2047 SourceRange(TokLocs.front(), TokLocs.back()), Str);
2048 }
2049
2050 Result.setLiteralOperatorId(II, KeywordLoc, SuffixLoc);
Alexis Hunt3d221f22009-11-29 07:34:05 +00002051 return false;
Alexis Hunt34458502009-11-28 04:44:28 +00002052 }
Douglas Gregor71395fa2009-11-04 00:56:37 +00002053
2054 // Parse a conversion-function-id.
2055 //
2056 // conversion-function-id: [C++ 12.3.2]
2057 // operator conversion-type-id
2058 //
2059 // conversion-type-id:
2060 // type-specifier-seq conversion-declarator[opt]
2061 //
2062 // conversion-declarator:
2063 // ptr-operator conversion-declarator[opt]
2064
2065 // Parse the type-specifier-seq.
John McCall084e83d2011-03-24 11:26:52 +00002066 DeclSpec DS(AttrFactory);
Douglas Gregora25d65d2009-11-20 22:03:38 +00002067 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregor71395fa2009-11-04 00:56:37 +00002068 return true;
2069
2070 // Parse the conversion-declarator, which is merely a sequence of
2071 // ptr-operators.
Richard Smith01518fa2013-05-04 01:26:46 +00002072 Declarator D(DS, Declarator::ConversionIdContext);
Douglas Gregor71395fa2009-11-04 00:56:37 +00002073 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
2074
2075 // Finish up the type.
John McCallfaf5fb42010-08-26 23:41:50 +00002076 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregor71395fa2009-11-04 00:56:37 +00002077 if (Ty.isInvalid())
2078 return true;
2079
2080 // Note that this is a conversion-function-id.
2081 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
2082 D.getSourceRange().getEnd());
2083 return false;
2084}
2085
2086/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
2087/// name of an entity.
2088///
2089/// \code
2090/// unqualified-id: [C++ expr.prim.general]
2091/// identifier
2092/// operator-function-id
2093/// conversion-function-id
2094/// [C++0x] literal-operator-id [TODO]
2095/// ~ class-name
2096/// template-id
2097///
2098/// \endcode
2099///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00002100/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregor71395fa2009-11-04 00:56:37 +00002101/// non-empty, then we are parsing the unqualified-id of a qualified-id.
2102///
2103/// \param EnteringContext whether we are entering the scope of the
2104/// nested-name-specifier.
2105///
Douglas Gregor7861a802009-11-03 01:35:08 +00002106/// \param AllowDestructorName whether we allow parsing of a destructor name.
2107///
2108/// \param AllowConstructorName whether we allow parsing a constructor name.
2109///
Douglas Gregor127ea592009-11-03 21:24:04 +00002110/// \param ObjectType if this unqualified-id occurs within a member access
2111/// expression, the type of the base object whose member is being accessed.
2112///
Douglas Gregor7861a802009-11-03 01:35:08 +00002113/// \param Result on a successful parse, contains the parsed unqualified-id.
2114///
2115/// \returns true if parsing fails, false otherwise.
2116bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
2117 bool AllowDestructorName,
2118 bool AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00002119 ParsedType ObjectType,
Abramo Bagnara7945c982012-01-27 09:46:47 +00002120 SourceLocation& TemplateKWLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00002121 UnqualifiedId &Result) {
Douglas Gregorb22ee882010-05-05 05:58:24 +00002122
2123 // Handle 'A::template B'. This is for template-ids which have not
2124 // already been annotated by ParseOptionalCXXScopeSpecifier().
2125 bool TemplateSpecified = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002126 if (getLangOpts().CPlusPlus && Tok.is(tok::kw_template) &&
Douglas Gregorb22ee882010-05-05 05:58:24 +00002127 (ObjectType || SS.isSet())) {
2128 TemplateSpecified = true;
2129 TemplateKWLoc = ConsumeToken();
2130 }
2131
Douglas Gregor7861a802009-11-03 01:35:08 +00002132 // unqualified-id:
2133 // identifier
2134 // template-id (when it hasn't already been annotated)
2135 if (Tok.is(tok::identifier)) {
2136 // Consume the identifier.
2137 IdentifierInfo *Id = Tok.getIdentifierInfo();
2138 SourceLocation IdLoc = ConsumeToken();
2139
David Blaikiebbafb8a2012-03-11 07:00:24 +00002140 if (!getLangOpts().CPlusPlus) {
Douglas Gregor411e5ac2010-01-11 23:29:10 +00002141 // If we're not in C++, only identifiers matter. Record the
2142 // identifier and return.
2143 Result.setIdentifier(Id, IdLoc);
2144 return false;
2145 }
2146
Douglas Gregor7861a802009-11-03 01:35:08 +00002147 if (AllowConstructorName &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002148 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor7861a802009-11-03 01:35:08 +00002149 // We have parsed a constructor name.
Abramo Bagnara4244b432012-01-27 08:46:19 +00002150 ParsedType Ty = Actions.getTypeName(*Id, IdLoc, getCurScope(),
2151 &SS, false, false,
2152 ParsedType(),
2153 /*IsCtorOrDtorName=*/true,
2154 /*NonTrivialTypeSourceInfo=*/true);
2155 Result.setConstructorName(Ty, IdLoc, IdLoc);
Douglas Gregor7861a802009-11-03 01:35:08 +00002156 } else {
2157 // We have parsed an identifier.
2158 Result.setIdentifier(Id, IdLoc);
2159 }
2160
2161 // If the next token is a '<', we may have a template.
Douglas Gregorb22ee882010-05-05 05:58:24 +00002162 if (TemplateSpecified || Tok.is(tok::less))
Abramo Bagnara7945c982012-01-27 09:46:47 +00002163 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc, Id, IdLoc,
2164 EnteringContext, ObjectType,
2165 Result, TemplateSpecified);
Douglas Gregor7861a802009-11-03 01:35:08 +00002166
2167 return false;
2168 }
2169
2170 // unqualified-id:
2171 // template-id (already parsed and annotated)
2172 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002173 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002174
2175 // If the template-name names the current class, then this is a constructor
2176 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002177 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002178 if (SS.isSet()) {
2179 // C++ [class.qual]p2 specifies that a qualified template-name
2180 // is taken as the constructor name where a constructor can be
2181 // declared. Thus, the template arguments are extraneous, so
2182 // complain about them and remove them entirely.
2183 Diag(TemplateId->TemplateNameLoc,
2184 diag::err_out_of_line_constructor_template_id)
2185 << TemplateId->Name
Douglas Gregora771f462010-03-31 17:46:05 +00002186 << FixItHint::CreateRemoval(
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002187 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
Abramo Bagnara4244b432012-01-27 08:46:19 +00002188 ParsedType Ty = Actions.getTypeName(*TemplateId->Name,
2189 TemplateId->TemplateNameLoc,
2190 getCurScope(),
2191 &SS, false, false,
2192 ParsedType(),
2193 /*IsCtorOrDtorName=*/true,
2194 /*NontrivialTypeSourceInfo=*/true);
2195 Result.setConstructorName(Ty, TemplateId->TemplateNameLoc,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002196 TemplateId->RAngleLoc);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002197 ConsumeToken();
2198 return false;
2199 }
2200
2201 Result.setConstructorTemplateId(TemplateId);
2202 ConsumeToken();
2203 return false;
2204 }
2205
Douglas Gregor7861a802009-11-03 01:35:08 +00002206 // We have already parsed a template-id; consume the annotation token as
2207 // our unqualified-id.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002208 Result.setTemplateId(TemplateId);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002209 TemplateKWLoc = TemplateId->TemplateKWLoc;
Douglas Gregor7861a802009-11-03 01:35:08 +00002210 ConsumeToken();
2211 return false;
2212 }
2213
2214 // unqualified-id:
2215 // operator-function-id
2216 // conversion-function-id
2217 if (Tok.is(tok::kw_operator)) {
Douglas Gregor71395fa2009-11-04 00:56:37 +00002218 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor7861a802009-11-03 01:35:08 +00002219 return true;
2220
Alexis Hunted0530f2009-11-28 08:58:14 +00002221 // If we have an operator-function-id or a literal-operator-id and the next
2222 // token is a '<', we may have a
Douglas Gregor71395fa2009-11-04 00:56:37 +00002223 //
2224 // template-id:
2225 // operator-function-id < template-argument-list[opt] >
Alexis Hunted0530f2009-11-28 08:58:14 +00002226 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
2227 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregorb22ee882010-05-05 05:58:24 +00002228 (TemplateSpecified || Tok.is(tok::less)))
Abramo Bagnara7945c982012-01-27 09:46:47 +00002229 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2230 0, SourceLocation(),
2231 EnteringContext, ObjectType,
2232 Result, TemplateSpecified);
Douglas Gregor7861a802009-11-03 01:35:08 +00002233
Douglas Gregor7861a802009-11-03 01:35:08 +00002234 return false;
2235 }
2236
David Blaikiebbafb8a2012-03-11 07:00:24 +00002237 if (getLangOpts().CPlusPlus &&
Douglas Gregor411e5ac2010-01-11 23:29:10 +00002238 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor7861a802009-11-03 01:35:08 +00002239 // C++ [expr.unary.op]p10:
2240 // There is an ambiguity in the unary-expression ~X(), where X is a
2241 // class-name. The ambiguity is resolved in favor of treating ~ as a
2242 // unary complement rather than treating ~X as referring to a destructor.
2243
2244 // Parse the '~'.
2245 SourceLocation TildeLoc = ConsumeToken();
David Blaikieecd8a942011-12-08 16:13:53 +00002246
2247 if (SS.isEmpty() && Tok.is(tok::kw_decltype)) {
2248 DeclSpec DS(AttrFactory);
2249 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
2250 if (ParsedType Type = Actions.getDestructorType(DS, ObjectType)) {
2251 Result.setDestructorName(TildeLoc, Type, EndLoc);
2252 return false;
2253 }
2254 return true;
2255 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002256
2257 // Parse the class-name.
2258 if (Tok.isNot(tok::identifier)) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00002259 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor7861a802009-11-03 01:35:08 +00002260 return true;
2261 }
2262
2263 // Parse the class-name (or template-name in a simple-template-id).
2264 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
2265 SourceLocation ClassNameLoc = ConsumeToken();
2266
Douglas Gregorb22ee882010-05-05 05:58:24 +00002267 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallba7bf592010-08-24 05:47:05 +00002268 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002269 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2270 ClassName, ClassNameLoc,
2271 EnteringContext, ObjectType,
2272 Result, TemplateSpecified);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002273 }
2274
Douglas Gregor7861a802009-11-03 01:35:08 +00002275 // Note that this is a destructor name.
John McCallba7bf592010-08-24 05:47:05 +00002276 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
2277 ClassNameLoc, getCurScope(),
2278 SS, ObjectType,
2279 EnteringContext);
Douglas Gregorfe17d252010-02-16 19:09:40 +00002280 if (!Ty)
Douglas Gregor7861a802009-11-03 01:35:08 +00002281 return true;
Douglas Gregorfe17d252010-02-16 19:09:40 +00002282
Douglas Gregor7861a802009-11-03 01:35:08 +00002283 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor7861a802009-11-03 01:35:08 +00002284 return false;
2285 }
2286
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002287 Diag(Tok, diag::err_expected_unqualified_id)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002288 << getLangOpts().CPlusPlus;
Douglas Gregor7861a802009-11-03 01:35:08 +00002289 return true;
2290}
2291
Sebastian Redlbd150f42008-11-21 19:14:01 +00002292/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
2293/// memory in a typesafe manner and call constructors.
Mike Stump11289f42009-09-09 15:08:12 +00002294///
Chris Lattner109faf22009-01-04 21:25:24 +00002295/// This method is called to parse the new expression after the optional :: has
2296/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
2297/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002298///
2299/// new-expression:
2300/// '::'[opt] 'new' new-placement[opt] new-type-id
2301/// new-initializer[opt]
2302/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2303/// new-initializer[opt]
2304///
2305/// new-placement:
2306/// '(' expression-list ')'
2307///
Sebastian Redl351bb782008-12-02 14:43:59 +00002308/// new-type-id:
2309/// type-specifier-seq new-declarator[opt]
Douglas Gregora3a020a2011-04-15 19:40:02 +00002310/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redl351bb782008-12-02 14:43:59 +00002311///
2312/// new-declarator:
2313/// ptr-operator new-declarator[opt]
2314/// direct-new-declarator
2315///
Sebastian Redlbd150f42008-11-21 19:14:01 +00002316/// new-initializer:
2317/// '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00002318/// [C++0x] braced-init-list
Sebastian Redlbd150f42008-11-21 19:14:01 +00002319///
John McCalldadc5752010-08-24 06:29:42 +00002320ExprResult
Chris Lattner109faf22009-01-04 21:25:24 +00002321Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
2322 assert(Tok.is(tok::kw_new) && "expected 'new' token");
2323 ConsumeToken(); // Consume 'new'
Sebastian Redlbd150f42008-11-21 19:14:01 +00002324
2325 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
2326 // second form of new-expression. It can't be a new-type-id.
2327
Benjamin Kramerf0623432012-08-23 22:51:59 +00002328 ExprVector PlacementArgs;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002329 SourceLocation PlacementLParen, PlacementRParen;
2330
Douglas Gregorf2753b32010-07-13 15:54:32 +00002331 SourceRange TypeIdParens;
John McCall084e83d2011-03-24 11:26:52 +00002332 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis3ff13572011-06-28 03:01:23 +00002333 Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002334 if (Tok.is(tok::l_paren)) {
2335 // If it turns out to be a placement, we change the type location.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002336 BalancedDelimiterTracker T(*this, tok::l_paren);
2337 T.consumeOpen();
2338 PlacementLParen = T.getOpenLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002339 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
2340 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002341 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002342 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002343
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002344 T.consumeClose();
2345 PlacementRParen = T.getCloseLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002346 if (PlacementRParen.isInvalid()) {
2347 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002348 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002349 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002350
Sebastian Redl351bb782008-12-02 14:43:59 +00002351 if (PlacementArgs.empty()) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002352 // Reset the placement locations. There was no placement.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002353 TypeIdParens = T.getRange();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002354 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002355 } else {
2356 // We still need the type.
2357 if (Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002358 BalancedDelimiterTracker T(*this, tok::l_paren);
2359 T.consumeOpen();
Douglas Gregora3a020a2011-04-15 19:40:02 +00002360 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002361 ParseSpecifierQualifierList(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002362 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002363 ParseDeclarator(DeclaratorInfo);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002364 T.consumeClose();
2365 TypeIdParens = T.getRange();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002366 } else {
Douglas Gregora3a020a2011-04-15 19:40:02 +00002367 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002368 if (ParseCXXTypeSpecifierSeq(DS))
2369 DeclaratorInfo.setInvalidType(true);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002370 else {
2371 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002372 ParseDeclaratorInternal(DeclaratorInfo,
2373 &Parser::ParseDirectNewDeclarator);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002374 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002375 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002376 }
2377 } else {
Sebastian Redl351bb782008-12-02 14:43:59 +00002378 // A new-type-id is a simplified type-id, where essentially the
2379 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregora3a020a2011-04-15 19:40:02 +00002380 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002381 if (ParseCXXTypeSpecifierSeq(DS))
2382 DeclaratorInfo.setInvalidType(true);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002383 else {
2384 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002385 ParseDeclaratorInternal(DeclaratorInfo,
2386 &Parser::ParseDirectNewDeclarator);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002387 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002388 }
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00002389 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redl351bb782008-12-02 14:43:59 +00002390 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002391 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002392 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002393
Sebastian Redl6047f072012-02-16 12:22:20 +00002394 ExprResult Initializer;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002395
2396 if (Tok.is(tok::l_paren)) {
Sebastian Redl6047f072012-02-16 12:22:20 +00002397 SourceLocation ConstructorLParen, ConstructorRParen;
Benjamin Kramerf0623432012-08-23 22:51:59 +00002398 ExprVector ConstructorArgs;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002399 BalancedDelimiterTracker T(*this, tok::l_paren);
2400 T.consumeOpen();
2401 ConstructorLParen = T.getOpenLocation();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002402 if (Tok.isNot(tok::r_paren)) {
2403 CommaLocsTy CommaLocs;
Sebastian Redl351bb782008-12-02 14:43:59 +00002404 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
2405 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002406 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002407 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002408 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002409 T.consumeClose();
2410 ConstructorRParen = T.getCloseLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002411 if (ConstructorRParen.isInvalid()) {
2412 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redld65cea82008-12-11 22:51:44 +00002413 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002414 }
Sebastian Redl6047f072012-02-16 12:22:20 +00002415 Initializer = Actions.ActOnParenListExpr(ConstructorLParen,
2416 ConstructorRParen,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002417 ConstructorArgs);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002418 } else if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus11) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002419 Diag(Tok.getLocation(),
2420 diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl6047f072012-02-16 12:22:20 +00002421 Initializer = ParseBraceInitializer();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002422 }
Sebastian Redl6047f072012-02-16 12:22:20 +00002423 if (Initializer.isInvalid())
2424 return Initializer;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002425
Sebastian Redl6d4256c2009-03-15 17:47:39 +00002426 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002427 PlacementArgs, PlacementRParen,
Sebastian Redl6047f072012-02-16 12:22:20 +00002428 TypeIdParens, DeclaratorInfo, Initializer.take());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002429}
2430
Sebastian Redlbd150f42008-11-21 19:14:01 +00002431/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
2432/// passed to ParseDeclaratorInternal.
2433///
2434/// direct-new-declarator:
2435/// '[' expression ']'
2436/// direct-new-declarator '[' constant-expression ']'
2437///
Chris Lattner109faf22009-01-04 21:25:24 +00002438void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002439 // Parse the array dimensions.
2440 bool first = true;
2441 while (Tok.is(tok::l_square)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002442 // An array-size expression can't start with a lambda.
2443 if (CheckProhibitedCXX11Attribute())
2444 continue;
2445
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002446 BalancedDelimiterTracker T(*this, tok::l_square);
2447 T.consumeOpen();
2448
John McCalldadc5752010-08-24 06:29:42 +00002449 ExprResult Size(first ? ParseExpression()
Sebastian Redl59b5e512008-12-11 21:36:32 +00002450 : ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002451 if (Size.isInvalid()) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002452 // Recover
2453 SkipUntil(tok::r_square);
2454 return;
2455 }
2456 first = false;
2457
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002458 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00002459
Bill Wendling44426052012-12-20 19:22:21 +00002460 // Attributes here appertain to the array type. C++11 [expr.new]p5.
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002461 ParsedAttributes Attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002462 MaybeParseCXX11Attributes(Attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002463
John McCall084e83d2011-03-24 11:26:52 +00002464 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall53fa7142010-12-24 02:08:15 +00002465 /*static=*/false, /*star=*/false,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002466 Size.release(),
2467 T.getOpenLocation(),
2468 T.getCloseLocation()),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002469 Attrs, T.getCloseLocation());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002470
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002471 if (T.getCloseLocation().isInvalid())
Sebastian Redlbd150f42008-11-21 19:14:01 +00002472 return;
2473 }
2474}
2475
2476/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
2477/// This ambiguity appears in the syntax of the C++ new operator.
2478///
2479/// new-expression:
2480/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2481/// new-initializer[opt]
2482///
2483/// new-placement:
2484/// '(' expression-list ')'
2485///
John McCall37ad5512010-08-23 06:44:23 +00002486bool Parser::ParseExpressionListOrTypeId(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002487 SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner109faf22009-01-04 21:25:24 +00002488 Declarator &D) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002489 // The '(' was already consumed.
2490 if (isTypeIdInParens()) {
Sebastian Redl351bb782008-12-02 14:43:59 +00002491 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002492 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002493 ParseDeclarator(D);
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00002494 return D.isInvalidType();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002495 }
2496
2497 // It's not a type, it has to be an expression list.
2498 // Discard the comma locations - ActOnCXXNew has enough parameters.
2499 CommaLocsTy CommaLocs;
2500 return ParseExpressionList(PlacementArgs, CommaLocs);
2501}
2502
2503/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
2504/// to free memory allocated by new.
2505///
Chris Lattner109faf22009-01-04 21:25:24 +00002506/// This method is called to parse the 'delete' expression after the optional
2507/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
2508/// and "Start" is its location. Otherwise, "Start" is the location of the
2509/// 'delete' token.
2510///
Sebastian Redlbd150f42008-11-21 19:14:01 +00002511/// delete-expression:
2512/// '::'[opt] 'delete' cast-expression
2513/// '::'[opt] 'delete' '[' ']' cast-expression
John McCalldadc5752010-08-24 06:29:42 +00002514ExprResult
Chris Lattner109faf22009-01-04 21:25:24 +00002515Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
2516 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
2517 ConsumeToken(); // Consume 'delete'
Sebastian Redlbd150f42008-11-21 19:14:01 +00002518
2519 // Array delete?
2520 bool ArrayDelete = false;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002521 if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
Richard Smith10c60722012-08-09 19:01:51 +00002522 // C++11 [expr.delete]p1:
2523 // Whenever the delete keyword is followed by empty square brackets, it
2524 // shall be interpreted as [array delete].
2525 // [Footnote: A lambda expression with a lambda-introducer that consists
2526 // of empty square brackets can follow the delete keyword if
2527 // the lambda expression is enclosed in parentheses.]
2528 // FIXME: Produce a better diagnostic if the '[]' is unambiguously a
2529 // lambda-introducer.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002530 ArrayDelete = true;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002531 BalancedDelimiterTracker T(*this, tok::l_square);
2532
2533 T.consumeOpen();
2534 T.consumeClose();
2535 if (T.getCloseLocation().isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +00002536 return ExprError();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002537 }
2538
John McCalldadc5752010-08-24 06:29:42 +00002539 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002540 if (Operand.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002541 return Operand;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002542
John McCallb268a282010-08-23 23:25:46 +00002543 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002544}
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002545
Mike Stump11289f42009-09-09 15:08:12 +00002546static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002547 switch(kind) {
David Blaikie83d382b2011-09-23 05:06:16 +00002548 default: llvm_unreachable("Not a known unary type trait.");
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002549 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
Joao Matosc9523d42013-03-27 01:34:16 +00002550 case tok::kw___has_nothrow_move_assign: return UTT_HasNothrowMoveAssign;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002551 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
John Wiegley65497cc2011-04-27 23:09:49 +00002552 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002553 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
Joao Matosc9523d42013-03-27 01:34:16 +00002554 case tok::kw___has_trivial_move_assign: return UTT_HasTrivialMoveAssign;
Alexis Huntf479f1b2011-05-09 18:22:59 +00002555 case tok::kw___has_trivial_constructor:
2556 return UTT_HasTrivialDefaultConstructor;
Joao Matosc9523d42013-03-27 01:34:16 +00002557 case tok::kw___has_trivial_move_constructor:
2558 return UTT_HasTrivialMoveConstructor;
John Wiegley65497cc2011-04-27 23:09:49 +00002559 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002560 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
2561 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
2562 case tok::kw___is_abstract: return UTT_IsAbstract;
John Wiegley65497cc2011-04-27 23:09:49 +00002563 case tok::kw___is_arithmetic: return UTT_IsArithmetic;
2564 case tok::kw___is_array: return UTT_IsArray;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002565 case tok::kw___is_class: return UTT_IsClass;
John Wiegley65497cc2011-04-27 23:09:49 +00002566 case tok::kw___is_complete_type: return UTT_IsCompleteType;
2567 case tok::kw___is_compound: return UTT_IsCompound;
2568 case tok::kw___is_const: return UTT_IsConst;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002569 case tok::kw___is_empty: return UTT_IsEmpty;
2570 case tok::kw___is_enum: return UTT_IsEnum;
Douglas Gregordca70af2011-12-03 18:14:24 +00002571 case tok::kw___is_final: return UTT_IsFinal;
John Wiegley65497cc2011-04-27 23:09:49 +00002572 case tok::kw___is_floating_point: return UTT_IsFloatingPoint;
2573 case tok::kw___is_function: return UTT_IsFunction;
2574 case tok::kw___is_fundamental: return UTT_IsFundamental;
2575 case tok::kw___is_integral: return UTT_IsIntegral;
John McCallbf4a7d72012-09-25 07:32:49 +00002576 case tok::kw___is_interface_class: return UTT_IsInterfaceClass;
John Wiegley65497cc2011-04-27 23:09:49 +00002577 case tok::kw___is_lvalue_reference: return UTT_IsLvalueReference;
2578 case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
2579 case tok::kw___is_member_object_pointer: return UTT_IsMemberObjectPointer;
2580 case tok::kw___is_member_pointer: return UTT_IsMemberPointer;
2581 case tok::kw___is_object: return UTT_IsObject;
Chandler Carruth79803482011-04-23 10:47:20 +00002582 case tok::kw___is_literal: return UTT_IsLiteral;
Chandler Carruth65fa1fd2011-04-24 02:49:28 +00002583 case tok::kw___is_literal_type: return UTT_IsLiteral;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002584 case tok::kw___is_pod: return UTT_IsPOD;
John Wiegley65497cc2011-04-27 23:09:49 +00002585 case tok::kw___is_pointer: return UTT_IsPointer;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002586 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
John Wiegley65497cc2011-04-27 23:09:49 +00002587 case tok::kw___is_reference: return UTT_IsReference;
John Wiegley65497cc2011-04-27 23:09:49 +00002588 case tok::kw___is_rvalue_reference: return UTT_IsRvalueReference;
2589 case tok::kw___is_scalar: return UTT_IsScalar;
2590 case tok::kw___is_signed: return UTT_IsSigned;
2591 case tok::kw___is_standard_layout: return UTT_IsStandardLayout;
2592 case tok::kw___is_trivial: return UTT_IsTrivial;
Alexis Huntd9a5cc12011-05-13 00:31:07 +00002593 case tok::kw___is_trivially_copyable: return UTT_IsTriviallyCopyable;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002594 case tok::kw___is_union: return UTT_IsUnion;
John Wiegley65497cc2011-04-27 23:09:49 +00002595 case tok::kw___is_unsigned: return UTT_IsUnsigned;
2596 case tok::kw___is_void: return UTT_IsVoid;
2597 case tok::kw___is_volatile: return UTT_IsVolatile;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002598 }
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002599}
2600
2601static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
2602 switch(kind) {
Francois Pichet347c4c72010-12-07 00:55:57 +00002603 default: llvm_unreachable("Not a known binary type trait");
Francois Pichet34b21132010-12-08 22:35:30 +00002604 case tok::kw___is_base_of: return BTT_IsBaseOf;
John Wiegley65497cc2011-04-27 23:09:49 +00002605 case tok::kw___is_convertible: return BTT_IsConvertible;
2606 case tok::kw___is_same: return BTT_IsSame;
Francois Pichet34b21132010-12-08 22:35:30 +00002607 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor8006e762011-01-27 20:28:01 +00002608 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Douglas Gregor1be329d2012-02-23 07:33:15 +00002609 case tok::kw___is_trivially_assignable: return BTT_IsTriviallyAssignable;
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002610 }
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002611}
2612
Douglas Gregor29c42f22012-02-24 07:38:34 +00002613static TypeTrait TypeTraitFromTokKind(tok::TokenKind kind) {
2614 switch (kind) {
2615 default: llvm_unreachable("Not a known type trait");
2616 case tok::kw___is_trivially_constructible:
2617 return TT_IsTriviallyConstructible;
2618 }
2619}
2620
John Wiegley6242b6a2011-04-28 00:16:57 +00002621static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
2622 switch(kind) {
2623 default: llvm_unreachable("Not a known binary type trait");
2624 case tok::kw___array_rank: return ATT_ArrayRank;
2625 case tok::kw___array_extent: return ATT_ArrayExtent;
2626 }
2627}
2628
John Wiegleyf9f65842011-04-25 06:54:41 +00002629static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2630 switch(kind) {
David Blaikie83d382b2011-09-23 05:06:16 +00002631 default: llvm_unreachable("Not a known unary expression trait.");
John Wiegleyf9f65842011-04-25 06:54:41 +00002632 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
2633 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
2634 }
2635}
2636
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002637/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
2638/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2639/// templates.
2640///
2641/// primary-expression:
2642/// [GNU] unary-type-trait '(' type-id ')'
2643///
John McCalldadc5752010-08-24 06:29:42 +00002644ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002645 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
2646 SourceLocation Loc = ConsumeToken();
2647
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002648 BalancedDelimiterTracker T(*this, tok::l_paren);
2649 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002650 return ExprError();
2651
2652 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
2653 // there will be cryptic errors about mismatched parentheses and missing
2654 // specifiers.
Douglas Gregor220cac52009-02-18 17:45:20 +00002655 TypeResult Ty = ParseTypeName();
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002656
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002657 T.consumeClose();
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002658
Douglas Gregor220cac52009-02-18 17:45:20 +00002659 if (Ty.isInvalid())
2660 return ExprError();
2661
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002662 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), T.getCloseLocation());
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002663}
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002664
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002665/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
2666/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2667/// templates.
2668///
2669/// primary-expression:
2670/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
2671///
2672ExprResult Parser::ParseBinaryTypeTrait() {
2673 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
2674 SourceLocation Loc = ConsumeToken();
2675
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002676 BalancedDelimiterTracker T(*this, tok::l_paren);
2677 if (T.expectAndConsume(diag::err_expected_lparen))
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002678 return ExprError();
2679
2680 TypeResult LhsTy = ParseTypeName();
2681 if (LhsTy.isInvalid()) {
2682 SkipUntil(tok::r_paren);
2683 return ExprError();
2684 }
2685
2686 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2687 SkipUntil(tok::r_paren);
2688 return ExprError();
2689 }
2690
2691 TypeResult RhsTy = ParseTypeName();
2692 if (RhsTy.isInvalid()) {
2693 SkipUntil(tok::r_paren);
2694 return ExprError();
2695 }
2696
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002697 T.consumeClose();
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002698
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002699 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(),
2700 T.getCloseLocation());
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002701}
2702
Douglas Gregor29c42f22012-02-24 07:38:34 +00002703/// \brief Parse the built-in type-trait pseudo-functions that allow
2704/// implementation of the TR1/C++11 type traits templates.
2705///
2706/// primary-expression:
2707/// type-trait '(' type-id-seq ')'
2708///
2709/// type-id-seq:
2710/// type-id ...[opt] type-id-seq[opt]
2711///
2712ExprResult Parser::ParseTypeTrait() {
2713 TypeTrait Kind = TypeTraitFromTokKind(Tok.getKind());
2714 SourceLocation Loc = ConsumeToken();
2715
2716 BalancedDelimiterTracker Parens(*this, tok::l_paren);
2717 if (Parens.expectAndConsume(diag::err_expected_lparen))
2718 return ExprError();
2719
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002720 SmallVector<ParsedType, 2> Args;
Douglas Gregor29c42f22012-02-24 07:38:34 +00002721 do {
2722 // Parse the next type.
2723 TypeResult Ty = ParseTypeName();
2724 if (Ty.isInvalid()) {
2725 Parens.skipToEnd();
2726 return ExprError();
2727 }
2728
2729 // Parse the ellipsis, if present.
2730 if (Tok.is(tok::ellipsis)) {
2731 Ty = Actions.ActOnPackExpansion(Ty.get(), ConsumeToken());
2732 if (Ty.isInvalid()) {
2733 Parens.skipToEnd();
2734 return ExprError();
2735 }
2736 }
2737
2738 // Add this type to the list of arguments.
2739 Args.push_back(Ty.get());
2740
2741 if (Tok.is(tok::comma)) {
2742 ConsumeToken();
2743 continue;
2744 }
2745
2746 break;
2747 } while (true);
2748
2749 if (Parens.consumeClose())
2750 return ExprError();
2751
2752 return Actions.ActOnTypeTrait(Kind, Loc, Args, Parens.getCloseLocation());
2753}
2754
John Wiegley6242b6a2011-04-28 00:16:57 +00002755/// ParseArrayTypeTrait - Parse the built-in array type-trait
2756/// pseudo-functions.
2757///
2758/// primary-expression:
2759/// [Embarcadero] '__array_rank' '(' type-id ')'
2760/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
2761///
2762ExprResult Parser::ParseArrayTypeTrait() {
2763 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
2764 SourceLocation Loc = ConsumeToken();
2765
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002766 BalancedDelimiterTracker T(*this, tok::l_paren);
2767 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley6242b6a2011-04-28 00:16:57 +00002768 return ExprError();
2769
2770 TypeResult Ty = ParseTypeName();
2771 if (Ty.isInvalid()) {
2772 SkipUntil(tok::comma);
2773 SkipUntil(tok::r_paren);
2774 return ExprError();
2775 }
2776
2777 switch (ATT) {
2778 case ATT_ArrayRank: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002779 T.consumeClose();
2780 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL,
2781 T.getCloseLocation());
John Wiegley6242b6a2011-04-28 00:16:57 +00002782 }
2783 case ATT_ArrayExtent: {
2784 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2785 SkipUntil(tok::r_paren);
2786 return ExprError();
2787 }
2788
2789 ExprResult DimExpr = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002790 T.consumeClose();
John Wiegley6242b6a2011-04-28 00:16:57 +00002791
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002792 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(),
2793 T.getCloseLocation());
John Wiegley6242b6a2011-04-28 00:16:57 +00002794 }
John Wiegley6242b6a2011-04-28 00:16:57 +00002795 }
David Blaikiee4d798f2012-01-20 21:50:17 +00002796 llvm_unreachable("Invalid ArrayTypeTrait!");
John Wiegley6242b6a2011-04-28 00:16:57 +00002797}
2798
John Wiegleyf9f65842011-04-25 06:54:41 +00002799/// ParseExpressionTrait - Parse built-in expression-trait
2800/// pseudo-functions like __is_lvalue_expr( xxx ).
2801///
2802/// primary-expression:
2803/// [Embarcadero] expression-trait '(' expression ')'
2804///
2805ExprResult Parser::ParseExpressionTrait() {
2806 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2807 SourceLocation Loc = ConsumeToken();
2808
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002809 BalancedDelimiterTracker T(*this, tok::l_paren);
2810 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegleyf9f65842011-04-25 06:54:41 +00002811 return ExprError();
2812
2813 ExprResult Expr = ParseExpression();
2814
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002815 T.consumeClose();
John Wiegleyf9f65842011-04-25 06:54:41 +00002816
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002817 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(),
2818 T.getCloseLocation());
John Wiegleyf9f65842011-04-25 06:54:41 +00002819}
2820
2821
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002822/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2823/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2824/// based on the context past the parens.
John McCalldadc5752010-08-24 06:29:42 +00002825ExprResult
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002826Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallba7bf592010-08-24 05:47:05 +00002827 ParsedType &CastTy,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002828 BalancedDelimiterTracker &Tracker) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002829 assert(getLangOpts().CPlusPlus && "Should only be called for C++!");
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002830 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2831 assert(isTypeIdInParens() && "Not a type-id!");
2832
John McCalldadc5752010-08-24 06:29:42 +00002833 ExprResult Result(true);
John McCallba7bf592010-08-24 05:47:05 +00002834 CastTy = ParsedType();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002835
2836 // We need to disambiguate a very ugly part of the C++ syntax:
2837 //
2838 // (T())x; - type-id
2839 // (T())*x; - type-id
2840 // (T())/x; - expression
2841 // (T()); - expression
2842 //
2843 // The bad news is that we cannot use the specialized tentative parser, since
2844 // it can only verify that the thing inside the parens can be parsed as
2845 // type-id, it is not useful for determining the context past the parens.
2846 //
2847 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidis24ad6922009-05-22 15:12:46 +00002848 // making any unnecessary Action calls.
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002849 //
2850 // It uses a scheme similar to parsing inline methods. The parenthesized
2851 // tokens are cached, the context that follows is determined (possibly by
2852 // parsing a cast-expression), and then we re-introduce the cached tokens
2853 // into the token stream and parse them appropriately.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002854
Mike Stump11289f42009-09-09 15:08:12 +00002855 ParenParseOption ParseAs;
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002856 CachedTokens Toks;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002857
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002858 // Store the tokens of the parentheses. We will parse them after we determine
2859 // the context that follows them.
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00002860 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002861 // We didn't find the ')' we expected.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002862 Tracker.consumeClose();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002863 return ExprError();
2864 }
2865
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002866 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002867 ParseAs = CompoundLiteral;
2868 } else {
2869 bool NotCastExpr;
Eli Friedmancf7530f2009-05-25 19:41:42 +00002870 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2871 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2872 NotCastExpr = true;
2873 } else {
2874 // Try parsing the cast-expression that may follow.
2875 // If it is not a cast-expression, NotCastExpr will be true and no token
2876 // will be consumed.
2877 Result = ParseCastExpression(false/*isUnaryExpression*/,
2878 false/*isAddressofOperand*/,
John McCallba7bf592010-08-24 05:47:05 +00002879 NotCastExpr,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002880 // type-id has priority.
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +00002881 IsTypeCast);
Eli Friedmancf7530f2009-05-25 19:41:42 +00002882 }
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002883
2884 // If we parsed a cast-expression, it's really a type-id, otherwise it's
2885 // an expression.
2886 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002887 }
2888
Mike Stump11289f42009-09-09 15:08:12 +00002889 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002890 Toks.push_back(Tok);
2891 // Re-enter the stored parenthesized tokens into the token stream, so we may
2892 // parse them now.
2893 PP.EnterTokenStream(Toks.data(), Toks.size(),
2894 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2895 // Drop the current token and bring the first cached one. It's the same token
2896 // as when we entered this function.
2897 ConsumeAnyToken();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002898
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002899 if (ParseAs >= CompoundLiteral) {
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002900 // Parse the type declarator.
2901 DeclSpec DS(AttrFactory);
2902 ParseSpecifierQualifierList(DS);
2903 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2904 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002905
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002906 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002907 Tracker.consumeClose();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002908
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002909 if (ParseAs == CompoundLiteral) {
2910 ExprType = CompoundLiteral;
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002911 TypeResult Ty = ParseTypeName();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002912 return ParseCompoundLiteralExpression(Ty.get(),
2913 Tracker.getOpenLocation(),
2914 Tracker.getCloseLocation());
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002915 }
Mike Stump11289f42009-09-09 15:08:12 +00002916
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002917 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2918 assert(ParseAs == CastExpr);
2919
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002920 if (DeclaratorInfo.isInvalidType())
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002921 return ExprError();
2922
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002923 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002924 if (!Result.isInvalid())
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002925 Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(),
2926 DeclaratorInfo, CastTy,
2927 Tracker.getCloseLocation(), Result.take());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002928 return Result;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002929 }
Mike Stump11289f42009-09-09 15:08:12 +00002930
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002931 // Not a compound literal, and not followed by a cast-expression.
2932 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002933
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002934 ExprType = SimpleExpr;
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002935 Result = ParseExpression();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002936 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002937 Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(),
2938 Tok.getLocation(), Result.take());
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002939
2940 // Match the ')'.
2941 if (Result.isInvalid()) {
2942 SkipUntil(tok::r_paren);
2943 return ExprError();
2944 }
Mike Stump11289f42009-09-09 15:08:12 +00002945
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002946 Tracker.consumeClose();
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002947 return Result;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002948}