blob: 9489b4787e25a4b7d345776a4910edbd0266e5d9 [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//===----------------------------------------------------------------------===//
Faisal Vali2b391ab2013-09-26 19:54:12 +000013#include "clang/AST/DeclTemplate.h"
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
Faisal Vali2b391ab2013-09-26 19:54:12 +000024
Chris Lattner29375652006-12-04 18:06:35 +000025using namespace clang;
26
Richard Smith55858492011-04-14 21:45:45 +000027static int SelectDigraphErrorMessage(tok::TokenKind Kind) {
28 switch (Kind) {
29 case tok::kw_template: return 0;
30 case tok::kw_const_cast: return 1;
31 case tok::kw_dynamic_cast: return 2;
32 case tok::kw_reinterpret_cast: return 3;
33 case tok::kw_static_cast: return 4;
34 default:
David Blaikie83d382b2011-09-23 05:06:16 +000035 llvm_unreachable("Unknown type for digraph error message.");
Richard Smith55858492011-04-14 21:45:45 +000036 }
37}
38
39// Are the two tokens adjacent in the same source file?
Richard Smith7b3f3222012-06-18 06:11:04 +000040bool Parser::areTokensAdjacent(const Token &First, const Token &Second) {
Richard Smith55858492011-04-14 21:45:45 +000041 SourceManager &SM = PP.getSourceManager();
42 SourceLocation FirstLoc = SM.getSpellingLoc(First.getLocation());
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +000043 SourceLocation FirstEnd = FirstLoc.getLocWithOffset(First.getLength());
Richard Smith55858492011-04-14 21:45:45 +000044 return FirstEnd == SM.getSpellingLoc(Second.getLocation());
45}
46
47// Suggest fixit for "<::" after a cast.
48static void FixDigraph(Parser &P, Preprocessor &PP, Token &DigraphToken,
49 Token &ColonToken, tok::TokenKind Kind, bool AtDigraph) {
50 // Pull '<:' and ':' off token stream.
51 if (!AtDigraph)
52 PP.Lex(DigraphToken);
53 PP.Lex(ColonToken);
54
55 SourceRange Range;
56 Range.setBegin(DigraphToken.getLocation());
57 Range.setEnd(ColonToken.getLocation());
58 P.Diag(DigraphToken.getLocation(), diag::err_missing_whitespace_digraph)
59 << SelectDigraphErrorMessage(Kind)
60 << FixItHint::CreateReplacement(Range, "< ::");
61
62 // Update token information to reflect their change in token type.
63 ColonToken.setKind(tok::coloncolon);
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +000064 ColonToken.setLocation(ColonToken.getLocation().getLocWithOffset(-1));
Richard Smith55858492011-04-14 21:45:45 +000065 ColonToken.setLength(2);
66 DigraphToken.setKind(tok::less);
67 DigraphToken.setLength(1);
68
69 // Push new tokens back to token stream.
70 PP.EnterToken(ColonToken);
71 if (!AtDigraph)
72 PP.EnterToken(DigraphToken);
73}
74
Richard Trieu01fc0012011-09-19 19:01:00 +000075// Check for '<::' which should be '< ::' instead of '[:' when following
76// a template name.
77void Parser::CheckForTemplateAndDigraph(Token &Next, ParsedType ObjectType,
78 bool EnteringContext,
79 IdentifierInfo &II, CXXScopeSpec &SS) {
Richard Trieu02e25db2011-09-20 20:03:50 +000080 if (!Next.is(tok::l_square) || Next.getLength() != 2)
Richard Trieu01fc0012011-09-19 19:01:00 +000081 return;
82
83 Token SecondToken = GetLookAheadToken(2);
Richard Smith7b3f3222012-06-18 06:11:04 +000084 if (!SecondToken.is(tok::colon) || !areTokensAdjacent(Next, SecondToken))
Richard Trieu01fc0012011-09-19 19:01:00 +000085 return;
86
87 TemplateTy Template;
88 UnqualifiedId TemplateName;
89 TemplateName.setIdentifier(&II, Tok.getLocation());
90 bool MemberOfUnknownSpecialization;
91 if (!Actions.isTemplateName(getCurScope(), SS, /*hasTemplateKeyword=*/false,
92 TemplateName, ObjectType, EnteringContext,
93 Template, MemberOfUnknownSpecialization))
94 return;
95
96 FixDigraph(*this, PP, Next, SecondToken, tok::kw_template,
97 /*AtDigraph*/false);
98}
99
Richard Trieu1f3ea7b2012-11-02 01:08:58 +0000100/// \brief Emits an error for a left parentheses after a double colon.
101///
102/// When a '(' is found after a '::', emit an error. Attempt to fix the token
Nico Weber6be9b252012-11-29 05:29:23 +0000103/// stream by removing the '(', and the matching ')' if found.
Richard Trieu1f3ea7b2012-11-02 01:08:58 +0000104void Parser::CheckForLParenAfterColonColon() {
105 if (!Tok.is(tok::l_paren))
106 return;
107
108 SourceLocation l_parenLoc = ConsumeParen(), r_parenLoc;
109 Token Tok1 = getCurToken();
110 if (!Tok1.is(tok::identifier) && !Tok1.is(tok::star))
111 return;
112
113 if (Tok1.is(tok::identifier)) {
114 Token Tok2 = GetLookAheadToken(1);
115 if (Tok2.is(tok::r_paren)) {
116 ConsumeToken();
117 PP.EnterToken(Tok1);
118 r_parenLoc = ConsumeParen();
119 }
120 } else if (Tok1.is(tok::star)) {
121 Token Tok2 = GetLookAheadToken(1);
122 if (Tok2.is(tok::identifier)) {
123 Token Tok3 = GetLookAheadToken(2);
124 if (Tok3.is(tok::r_paren)) {
125 ConsumeToken();
126 ConsumeToken();
127 PP.EnterToken(Tok2);
128 PP.EnterToken(Tok1);
129 r_parenLoc = ConsumeParen();
130 }
131 }
132 }
133
134 Diag(l_parenLoc, diag::err_paren_after_colon_colon)
135 << FixItHint::CreateRemoval(l_parenLoc)
136 << FixItHint::CreateRemoval(r_parenLoc);
137}
138
Mike Stump11289f42009-09-09 15:08:12 +0000139/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000140///
141/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump11289f42009-09-09 15:08:12 +0000142/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000143/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000144///
145/// '::'[opt] nested-name-specifier
146/// '::'
147///
148/// nested-name-specifier:
149/// type-name '::'
150/// namespace-name '::'
151/// nested-name-specifier identifier '::'
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000152/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000153///
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000154///
Mike Stump11289f42009-09-09 15:08:12 +0000155/// \param SS the scope specifier that will be set to the parsed
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000156/// nested-name-specifier (or empty)
157///
Mike Stump11289f42009-09-09 15:08:12 +0000158/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000159/// the "." or "->" of a member access expression, this parameter provides the
160/// type of the object whose members are being accessed.
161///
162/// \param EnteringContext whether we will be entering into the context of
163/// the nested-name-specifier after parsing it.
164///
Douglas Gregore610ada2010-02-24 18:44:31 +0000165/// \param MayBePseudoDestructor When non-NULL, points to a flag that
166/// indicates whether this nested-name-specifier may be part of a
167/// pseudo-destructor name. In this case, the flag will be set false
168/// if we don't actually end up parsing a destructor name. Moreorover,
169/// if we do end up determining that we are parsing a destructor name,
170/// the last component of the nested-name-specifier is not parsed as
171/// part of the scope specifier.
Richard Smith7447af42013-03-26 01:15:19 +0000172///
173/// \param IsTypename If \c true, this nested-name-specifier is known to be
174/// part of a type name. This is used to improve error recovery.
175///
176/// \param LastII When non-NULL, points to an IdentifierInfo* that will be
177/// filled in with the leading identifier in the last component of the
178/// nested-name-specifier, if any.
Douglas Gregor90d554e2010-02-21 18:36:56 +0000179///
John McCall1f476a12010-02-26 08:45:28 +0000180/// \returns true if there was an error parsing a scope specifier
Douglas Gregore861bac2009-08-25 22:51:20 +0000181bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
John McCallba7bf592010-08-24 05:47:05 +0000182 ParsedType ObjectType,
Douglas Gregor90d554e2010-02-21 18:36:56 +0000183 bool EnteringContext,
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000184 bool *MayBePseudoDestructor,
Richard Smith7447af42013-03-26 01:15:19 +0000185 bool IsTypename,
186 IdentifierInfo **LastII) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000187 assert(getLangOpts().CPlusPlus &&
Chris Lattnerb5134c02009-01-05 01:24:05 +0000188 "Call sites of this function should be guarded by checking for C++");
Mike Stump11289f42009-09-09 15:08:12 +0000189
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000190 if (Tok.is(tok::annot_cxxscope)) {
Richard Smith7447af42013-03-26 01:15:19 +0000191 assert(!LastII && "want last identifier but have already annotated scope");
Douglas Gregor869ad452011-02-24 17:54:50 +0000192 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
193 Tok.getAnnotationRange(),
194 SS);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000195 ConsumeToken();
John McCall1f476a12010-02-26 08:45:28 +0000196 return false;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000197 }
Chris Lattnerf9b2cd42009-01-04 21:14:15 +0000198
Larisse Voufob959c3c2013-08-06 05:49:26 +0000199 if (Tok.is(tok::annot_template_id)) {
200 // If the current token is an annotated template id, it may already have
201 // a scope specifier. Restore it.
202 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
203 SS = TemplateId->SS;
204 }
205
Richard Smith7447af42013-03-26 01:15:19 +0000206 if (LastII)
207 *LastII = 0;
208
Douglas Gregor7f741122009-02-25 19:37:18 +0000209 bool HasScopeSpecifier = false;
210
Chris Lattner8a7d10d2009-01-05 03:55:46 +0000211 if (Tok.is(tok::coloncolon)) {
212 // ::new and ::delete aren't nested-name-specifiers.
213 tok::TokenKind NextKind = NextToken().getKind();
214 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
215 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000216
Chris Lattner45ddec32009-01-05 00:13:00 +0000217 // '::' - Global scope qualifier.
Douglas Gregor90c99722011-02-24 00:17:56 +0000218 if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
219 return true;
Richard Trieu1f3ea7b2012-11-02 01:08:58 +0000220
221 CheckForLParenAfterColonColon();
222
Douglas Gregor7f741122009-02-25 19:37:18 +0000223 HasScopeSpecifier = true;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000224 }
225
Douglas Gregore610ada2010-02-24 18:44:31 +0000226 bool CheckForDestructor = false;
227 if (MayBePseudoDestructor && *MayBePseudoDestructor) {
228 CheckForDestructor = true;
229 *MayBePseudoDestructor = false;
230 }
231
David Blaikie15a430a2011-12-04 05:04:18 +0000232 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
233 DeclSpec DS(AttrFactory);
234 SourceLocation DeclLoc = Tok.getLocation();
235 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
236 if (Tok.isNot(tok::coloncolon)) {
237 AnnotateExistingDecltypeSpecifier(DS, DeclLoc, EndLoc);
238 return false;
239 }
240
241 SourceLocation CCLoc = ConsumeToken();
242 if (Actions.ActOnCXXNestedNameSpecifierDecltype(SS, DS, CCLoc))
243 SS.SetInvalid(SourceRange(DeclLoc, CCLoc));
244
245 HasScopeSpecifier = true;
246 }
247
Douglas Gregor7f741122009-02-25 19:37:18 +0000248 while (true) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000249 if (HasScopeSpecifier) {
250 // C++ [basic.lookup.classref]p5:
251 // If the qualified-id has the form
Douglas Gregor308047d2009-09-09 00:23:06 +0000252 //
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000253 // ::class-name-or-namespace-name::...
Douglas Gregor308047d2009-09-09 00:23:06 +0000254 //
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000255 // the class-name-or-namespace-name is looked up in global scope as a
256 // class-name or namespace-name.
257 //
258 // To implement this, we clear out the object type as soon as we've
259 // seen a leading '::' or part of a nested-name-specifier.
John McCallba7bf592010-08-24 05:47:05 +0000260 ObjectType = ParsedType();
Douglas Gregor2436e712009-09-17 21:32:03 +0000261
262 if (Tok.is(tok::code_completion)) {
263 // Code completion for a nested-name-specifier, where the code
264 // code completion token follows the '::'.
Douglas Gregor0be31a22010-07-02 17:43:08 +0000265 Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
Argyrios Kyrtzidis7d94c922011-04-23 01:04:12 +0000266 // Include code completion token into the range of the scope otherwise
267 // when we try to annotate the scope tokens the dangling code completion
268 // token will cause assertion in
269 // Preprocessor::AnnotatePreviousCachedTokens.
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000270 SS.setEndLoc(Tok.getLocation());
271 cutOffParsing();
272 return true;
Douglas Gregor2436e712009-09-17 21:32:03 +0000273 }
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000274 }
Mike Stump11289f42009-09-09 15:08:12 +0000275
Douglas Gregor7f741122009-02-25 19:37:18 +0000276 // nested-name-specifier:
Chris Lattner0eed3a62009-06-26 03:47:46 +0000277 // nested-name-specifier 'template'[opt] simple-template-id '::'
278
279 // Parse the optional 'template' keyword, then make sure we have
280 // 'identifier <' after it.
281 if (Tok.is(tok::kw_template)) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000282 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedman2624be42009-08-29 04:08:08 +0000283 // nested-name-specifier, since they aren't allowed to start with
284 // 'template'.
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000285 if (!HasScopeSpecifier && !ObjectType)
Eli Friedman2624be42009-08-29 04:08:08 +0000286 break;
287
Douglas Gregor120635b2009-11-11 16:39:34 +0000288 TentativeParsingAction TPA(*this);
Chris Lattner0eed3a62009-06-26 03:47:46 +0000289 SourceLocation TemplateKWLoc = ConsumeToken();
Richard Smithd091dc12013-12-05 00:58:33 +0000290
Douglas Gregor71395fa2009-11-04 00:56:37 +0000291 UnqualifiedId TemplateName;
292 if (Tok.is(tok::identifier)) {
Douglas Gregor71395fa2009-11-04 00:56:37 +0000293 // Consume the identifier.
Douglas Gregor120635b2009-11-11 16:39:34 +0000294 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregor71395fa2009-11-04 00:56:37 +0000295 ConsumeToken();
296 } else if (Tok.is(tok::kw_operator)) {
Richard Smithd091dc12013-12-05 00:58:33 +0000297 // We don't need to actually parse the unqualified-id in this case,
298 // because a simple-template-id cannot start with 'operator', but
299 // go ahead and parse it anyway for consistency with the case where
300 // we already annotated the template-id.
301 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor120635b2009-11-11 16:39:34 +0000302 TemplateName)) {
303 TPA.Commit();
Douglas Gregor71395fa2009-11-04 00:56:37 +0000304 break;
Douglas Gregor120635b2009-11-11 16:39:34 +0000305 }
Richard Smithd091dc12013-12-05 00:58:33 +0000306
Alexis Hunted0530f2009-11-28 08:58:14 +0000307 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
308 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor71395fa2009-11-04 00:56:37 +0000309 Diag(TemplateName.getSourceRange().getBegin(),
310 diag::err_id_after_template_in_nested_name_spec)
311 << TemplateName.getSourceRange();
Douglas Gregor120635b2009-11-11 16:39:34 +0000312 TPA.Commit();
Douglas Gregor71395fa2009-11-04 00:56:37 +0000313 break;
314 }
315 } else {
Douglas Gregor120635b2009-11-11 16:39:34 +0000316 TPA.Revert();
Chris Lattner0eed3a62009-06-26 03:47:46 +0000317 break;
318 }
Mike Stump11289f42009-09-09 15:08:12 +0000319
Douglas Gregor120635b2009-11-11 16:39:34 +0000320 // If the next token is not '<', we have a qualified-id that refers
321 // to a template name, such as T::template apply, but is not a
322 // template-id.
323 if (Tok.isNot(tok::less)) {
324 TPA.Revert();
325 break;
326 }
327
328 // Commit to parsing the template-id.
329 TPA.Commit();
Douglas Gregorbb119652010-06-16 23:00:59 +0000330 TemplateTy Template;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000331 if (TemplateNameKind TNK
332 = Actions.ActOnDependentTemplateName(getCurScope(),
333 SS, TemplateKWLoc, TemplateName,
334 ObjectType, EnteringContext,
335 Template)) {
336 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateKWLoc,
337 TemplateName, false))
Douglas Gregorbb119652010-06-16 23:00:59 +0000338 return true;
339 } else
John McCall1f476a12010-02-26 08:45:28 +0000340 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000341
Chris Lattner0eed3a62009-06-26 03:47:46 +0000342 continue;
343 }
Mike Stump11289f42009-09-09 15:08:12 +0000344
Douglas Gregor7f741122009-02-25 19:37:18 +0000345 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump11289f42009-09-09 15:08:12 +0000346 // We have
Douglas Gregor7f741122009-02-25 19:37:18 +0000347 //
Richard Smith72bfbd82013-12-04 00:28:23 +0000348 // template-id '::'
Douglas Gregor7f741122009-02-25 19:37:18 +0000349 //
Richard Smith72bfbd82013-12-04 00:28:23 +0000350 // So we need to check whether the template-id is a simple-template-id of
351 // the right kind (it should name a type or be dependent), and then
Douglas Gregorb67535d2009-03-31 00:43:58 +0000352 // convert it into a type within the nested-name-specifier.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +0000353 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregore610ada2010-02-24 18:44:31 +0000354 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
355 *MayBePseudoDestructor = true;
John McCall1f476a12010-02-26 08:45:28 +0000356 return false;
Douglas Gregore610ada2010-02-24 18:44:31 +0000357 }
358
Richard Smith7447af42013-03-26 01:15:19 +0000359 if (LastII)
360 *LastII = TemplateId->Name;
361
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000362 // Consume the template-id token.
363 ConsumeToken();
364
365 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
366 SourceLocation CCLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000367
David Blaikie8c045bc2011-11-07 03:30:03 +0000368 HasScopeSpecifier = true;
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000369
Benjamin Kramercc4c49d2012-08-23 23:38:35 +0000370 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000371 TemplateId->NumArgs);
372
373 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000374 SS,
375 TemplateId->TemplateKWLoc,
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000376 TemplateId->Template,
377 TemplateId->TemplateNameLoc,
378 TemplateId->LAngleLoc,
379 TemplateArgsPtr,
380 TemplateId->RAngleLoc,
381 CCLoc,
382 EnteringContext)) {
383 SourceLocation StartLoc
384 = SS.getBeginLoc().isValid()? SS.getBeginLoc()
385 : TemplateId->TemplateNameLoc;
386 SS.SetInvalid(SourceRange(StartLoc, CCLoc));
Chris Lattner704edfb2009-06-26 03:45:46 +0000387 }
Argyrios Kyrtzidis13935672011-05-03 18:45:38 +0000388
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000389 continue;
Douglas Gregor7f741122009-02-25 19:37:18 +0000390 }
391
Chris Lattnere2355f72009-06-26 03:52:38 +0000392
393 // The rest of the nested-name-specifier possibilities start with
394 // tok::identifier.
395 if (Tok.isNot(tok::identifier))
396 break;
397
398 IdentifierInfo &II = *Tok.getIdentifierInfo();
399
400 // nested-name-specifier:
401 // type-name '::'
402 // namespace-name '::'
403 // nested-name-specifier identifier '::'
404 Token Next = NextToken();
Chris Lattner1c428032009-12-07 01:36:53 +0000405
406 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
407 // and emit a fixit hint for it.
Douglas Gregor90d554e2010-02-21 18:36:56 +0000408 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000409 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
410 Tok.getLocation(),
411 Next.getLocation(), ObjectType,
Douglas Gregor90d554e2010-02-21 18:36:56 +0000412 EnteringContext) &&
413 // If the token after the colon isn't an identifier, it's still an
414 // error, but they probably meant something else strange so don't
415 // recover like this.
416 PP.LookAhead(1).is(tok::identifier)) {
417 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
Douglas Gregora771f462010-03-31 17:46:05 +0000418 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregor90d554e2010-02-21 18:36:56 +0000419
420 // Recover as if the user wrote '::'.
421 Next.setKind(tok::coloncolon);
422 }
Chris Lattner1c428032009-12-07 01:36:53 +0000423 }
424
Chris Lattnere2355f72009-06-26 03:52:38 +0000425 if (Next.is(tok::coloncolon)) {
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000426 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +0000427 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000428 II, ObjectType)) {
Douglas Gregore610ada2010-02-24 18:44:31 +0000429 *MayBePseudoDestructor = true;
John McCall1f476a12010-02-26 08:45:28 +0000430 return false;
Douglas Gregore610ada2010-02-24 18:44:31 +0000431 }
432
Richard Smith7447af42013-03-26 01:15:19 +0000433 if (LastII)
434 *LastII = &II;
435
Chris Lattnere2355f72009-06-26 03:52:38 +0000436 // We have an identifier followed by a '::'. Lookup this name
437 // as the name in a nested-name-specifier.
438 SourceLocation IdLoc = ConsumeToken();
Chris Lattner1c428032009-12-07 01:36:53 +0000439 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
440 "NextToken() not working properly!");
Chris Lattnere2355f72009-06-26 03:52:38 +0000441 SourceLocation CCLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000442
Richard Trieu1f3ea7b2012-11-02 01:08:58 +0000443 CheckForLParenAfterColonColon();
444
Douglas Gregor90c99722011-02-24 00:17:56 +0000445 HasScopeSpecifier = true;
446 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
447 ObjectType, EnteringContext, SS))
448 SS.SetInvalid(SourceRange(IdLoc, CCLoc));
449
Chris Lattnere2355f72009-06-26 03:52:38 +0000450 continue;
451 }
Mike Stump11289f42009-09-09 15:08:12 +0000452
Richard Trieu01fc0012011-09-19 19:01:00 +0000453 CheckForTemplateAndDigraph(Next, ObjectType, EnteringContext, II, SS);
Richard Smith55858492011-04-14 21:45:45 +0000454
Chris Lattnere2355f72009-06-26 03:52:38 +0000455 // nested-name-specifier:
456 // type-name '<'
457 if (Next.is(tok::less)) {
458 TemplateTy Template;
Douglas Gregor3cf81312009-11-03 23:16:33 +0000459 UnqualifiedId TemplateName;
460 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor786123d2010-05-21 23:18:07 +0000461 bool MemberOfUnknownSpecialization;
Douglas Gregor0be31a22010-07-02 17:43:08 +0000462 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c5dee42010-08-06 12:11:11 +0000463 /*hasTemplateKeyword=*/false,
Douglas Gregor3cf81312009-11-03 23:16:33 +0000464 TemplateName,
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000465 ObjectType,
Douglas Gregore861bac2009-08-25 22:51:20 +0000466 EnteringContext,
Douglas Gregor786123d2010-05-21 23:18:07 +0000467 Template,
468 MemberOfUnknownSpecialization)) {
David Blaikie8c045bc2011-11-07 03:30:03 +0000469 // We have found a template name, so annotate this token
Chris Lattnere2355f72009-06-26 03:52:38 +0000470 // with a template-id annotation. We do not permit the
471 // template-id to be translated into a type annotation,
472 // because some clients (e.g., the parsing of class template
473 // specializations) still want to see the original template-id
474 // token.
Douglas Gregor71395fa2009-11-04 00:56:37 +0000475 ConsumeToken();
Abramo Bagnara7945c982012-01-27 09:46:47 +0000476 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
477 TemplateName, false))
John McCall1f476a12010-02-26 08:45:28 +0000478 return true;
Chris Lattnere2355f72009-06-26 03:52:38 +0000479 continue;
Larisse Voufo39a1e502013-08-06 01:03:05 +0000480 }
481
Douglas Gregor20c38a72010-05-21 23:43:39 +0000482 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000483 (IsTypename || IsTemplateArgumentList(1))) {
Douglas Gregor20c38a72010-05-21 23:43:39 +0000484 // We have something like t::getAs<T>, where getAs is a
485 // member of an unknown specialization. However, this will only
486 // parse correctly as a template, so suggest the keyword 'template'
487 // before 'getAs' and treat this as a dependent template name.
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000488 unsigned DiagID = diag::err_missing_dependent_template_keyword;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000489 if (getLangOpts().MicrosoftExt)
Francois Pichet93921652011-04-22 08:25:24 +0000490 DiagID = diag::warn_missing_dependent_template_keyword;
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000491
492 Diag(Tok.getLocation(), DiagID)
Douglas Gregor20c38a72010-05-21 23:43:39 +0000493 << II.getName()
494 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
495
Douglas Gregorbb119652010-06-16 23:00:59 +0000496 if (TemplateNameKind TNK
Douglas Gregor0be31a22010-07-02 17:43:08 +0000497 = Actions.ActOnDependentTemplateName(getCurScope(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000498 SS, SourceLocation(),
Douglas Gregorbb119652010-06-16 23:00:59 +0000499 TemplateName, ObjectType,
500 EnteringContext, Template)) {
501 // Consume the identifier.
502 ConsumeToken();
Abramo Bagnara7945c982012-01-27 09:46:47 +0000503 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
504 TemplateName, false))
505 return true;
Douglas Gregorbb119652010-06-16 23:00:59 +0000506 }
507 else
Douglas Gregor20c38a72010-05-21 23:43:39 +0000508 return true;
Douglas Gregorbb119652010-06-16 23:00:59 +0000509
Douglas Gregor20c38a72010-05-21 23:43:39 +0000510 continue;
Chris Lattnere2355f72009-06-26 03:52:38 +0000511 }
512 }
513
Douglas Gregor7f741122009-02-25 19:37:18 +0000514 // We don't have any tokens that form the beginning of a
515 // nested-name-specifier, so we're done.
516 break;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000517 }
Mike Stump11289f42009-09-09 15:08:12 +0000518
Douglas Gregore610ada2010-02-24 18:44:31 +0000519 // Even if we didn't see any pieces of a nested-name-specifier, we
520 // still check whether there is a tilde in this position, which
521 // indicates a potential pseudo-destructor.
522 if (CheckForDestructor && Tok.is(tok::tilde))
523 *MayBePseudoDestructor = true;
524
John McCall1f476a12010-02-26 08:45:28 +0000525 return false;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000526}
527
528/// ParseCXXIdExpression - Handle id-expression.
529///
530/// id-expression:
531/// unqualified-id
532/// qualified-id
533///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000534/// qualified-id:
535/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
536/// '::' identifier
537/// '::' operator-function-id
Douglas Gregora727cb92009-06-30 22:34:41 +0000538/// '::' template-id
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000539///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000540/// NOTE: The standard specifies that, for qualified-id, the parser does not
541/// expect:
542///
543/// '::' conversion-function-id
544/// '::' '~' class-name
545///
546/// This may cause a slight inconsistency on diagnostics:
547///
548/// class C {};
549/// namespace A {}
550/// void f() {
551/// :: A :: ~ C(); // Some Sema error about using destructor with a
552/// // namespace.
553/// :: ~ C(); // Some Parser error like 'unexpected ~'.
554/// }
555///
556/// We simplify the parser a bit and make it work like:
557///
558/// qualified-id:
559/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
560/// '::' unqualified-id
561///
562/// That way Sema can handle and report similar errors for namespaces and the
563/// global scope.
564///
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000565/// The isAddressOfOperand parameter indicates that this id-expression is a
566/// direct operand of the address-of operator. This is, besides member contexts,
567/// the only place where a qualified-id naming a non-static class member may
568/// appear.
569///
John McCalldadc5752010-08-24 06:29:42 +0000570ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000571 // qualified-id:
572 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
573 // '::' unqualified-id
574 //
575 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +0000576 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000577
578 SourceLocation TemplateKWLoc;
Douglas Gregora121b752009-11-03 16:56:39 +0000579 UnqualifiedId Name;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000580 if (ParseUnqualifiedId(SS,
581 /*EnteringContext=*/false,
582 /*AllowDestructorName=*/false,
583 /*AllowConstructorName=*/false,
John McCallba7bf592010-08-24 05:47:05 +0000584 /*ObjectType=*/ ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000585 TemplateKWLoc,
Douglas Gregora121b752009-11-03 16:56:39 +0000586 Name))
587 return ExprError();
John McCalla9ee3252009-11-22 02:49:43 +0000588
589 // This is only the direct operand of an & operator if it is not
590 // followed by a postfix-expression suffix.
John McCall8d08b9b2010-08-27 09:08:28 +0000591 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
592 isAddressOfOperand = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000593
594 return Actions.ActOnIdExpression(getCurScope(), SS, TemplateKWLoc, Name,
595 Tok.is(tok::l_paren), isAddressOfOperand);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000596}
597
Richard Smith21b3ab42013-05-09 21:36:41 +0000598/// ParseLambdaExpression - Parse a C++11 lambda expression.
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000599///
600/// lambda-expression:
601/// lambda-introducer lambda-declarator[opt] compound-statement
602///
603/// lambda-introducer:
604/// '[' lambda-capture[opt] ']'
605///
606/// lambda-capture:
607/// capture-default
608/// capture-list
609/// capture-default ',' capture-list
610///
611/// capture-default:
612/// '&'
613/// '='
614///
615/// capture-list:
616/// capture
617/// capture-list ',' capture
618///
619/// capture:
Richard Smith21b3ab42013-05-09 21:36:41 +0000620/// simple-capture
621/// init-capture [C++1y]
622///
623/// simple-capture:
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000624/// identifier
625/// '&' identifier
626/// 'this'
627///
Richard Smith21b3ab42013-05-09 21:36:41 +0000628/// init-capture: [C++1y]
629/// identifier initializer
630/// '&' identifier initializer
631///
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000632/// lambda-declarator:
633/// '(' parameter-declaration-clause ')' attribute-specifier[opt]
634/// 'mutable'[opt] exception-specification[opt]
635/// trailing-return-type[opt]
636///
637ExprResult Parser::ParseLambdaExpression() {
638 // Parse lambda-introducer.
639 LambdaIntroducer Intro;
640
David Blaikie05785d12013-02-20 22:23:23 +0000641 Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000642 if (DiagID) {
643 Diag(Tok, DiagID.getValue());
Alexey Bataevee6507d2013-11-18 08:17:37 +0000644 SkipUntil(tok::r_square, StopAtSemi);
645 SkipUntil(tok::l_brace, StopAtSemi);
646 SkipUntil(tok::r_brace, StopAtSemi);
Eli Friedmanc7c97142012-01-04 02:40:39 +0000647 return ExprError();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000648 }
649
650 return ParseLambdaExpressionAfterIntroducer(Intro);
651}
652
653/// TryParseLambdaExpression - Use lookahead and potentially tentative
654/// parsing to determine if we are looking at a C++0x lambda expression, and parse
655/// it if we are.
656///
657/// If we are not looking at a lambda expression, returns ExprError().
658ExprResult Parser::TryParseLambdaExpression() {
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000659 assert(getLangOpts().CPlusPlus11
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000660 && Tok.is(tok::l_square)
661 && "Not at the start of a possible lambda expression.");
662
663 const Token Next = NextToken(), After = GetLookAheadToken(2);
664
665 // If lookahead indicates this is a lambda...
666 if (Next.is(tok::r_square) || // []
667 Next.is(tok::equal) || // [=
668 (Next.is(tok::amp) && // [&] or [&,
669 (After.is(tok::r_square) ||
670 After.is(tok::comma))) ||
671 (Next.is(tok::identifier) && // [identifier]
672 After.is(tok::r_square))) {
673 return ParseLambdaExpression();
674 }
675
Eli Friedmanc7c97142012-01-04 02:40:39 +0000676 // If lookahead indicates an ObjC message send...
677 // [identifier identifier
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000678 if (Next.is(tok::identifier) && After.is(tok::identifier)) {
Eli Friedmanc7c97142012-01-04 02:40:39 +0000679 return ExprEmpty();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000680 }
681
Eli Friedmanc7c97142012-01-04 02:40:39 +0000682 // Here, we're stuck: lambda introducers and Objective-C message sends are
683 // unambiguous, but it requires arbitrary lookhead. [a,b,c,d,e,f,g] is a
684 // lambda, and [a,b,c,d,e,f,g h] is a Objective-C message send. Instead of
685 // writing two routines to parse a lambda introducer, just try to parse
686 // a lambda introducer first, and fall back if that fails.
687 // (TryParseLambdaIntroducer never produces any diagnostic output.)
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000688 LambdaIntroducer Intro;
689 if (TryParseLambdaIntroducer(Intro))
Eli Friedmanc7c97142012-01-04 02:40:39 +0000690 return ExprEmpty();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000691 return ParseLambdaExpressionAfterIntroducer(Intro);
692}
693
Richard Smithf44d2a82013-05-21 22:21:19 +0000694/// \brief Parse a lambda introducer.
695/// \param Intro A LambdaIntroducer filled in with information about the
696/// contents of the lambda-introducer.
697/// \param SkippedInits If non-null, we are disambiguating between an Obj-C
698/// message send and a lambda expression. In this mode, we will
699/// sometimes skip the initializers for init-captures and not fully
700/// populate \p Intro. This flag will be set to \c true if we do so.
701/// \return A DiagnosticID if it hit something unexpected. The location for
702/// for the diagnostic is that of the current token.
703Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro,
704 bool *SkippedInits) {
David Blaikie05785d12013-02-20 22:23:23 +0000705 typedef Optional<unsigned> DiagResult;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000706
707 assert(Tok.is(tok::l_square) && "Lambda expressions begin with '['.");
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000708 BalancedDelimiterTracker T(*this, tok::l_square);
709 T.consumeOpen();
710
711 Intro.Range.setBegin(T.getOpenLocation());
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000712
713 bool first = true;
714
715 // Parse capture-default.
716 if (Tok.is(tok::amp) &&
717 (NextToken().is(tok::comma) || NextToken().is(tok::r_square))) {
718 Intro.Default = LCD_ByRef;
Douglas Gregora1bffa22012-02-10 17:46:20 +0000719 Intro.DefaultLoc = ConsumeToken();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000720 first = false;
721 } else if (Tok.is(tok::equal)) {
722 Intro.Default = LCD_ByCopy;
Douglas Gregora1bffa22012-02-10 17:46:20 +0000723 Intro.DefaultLoc = ConsumeToken();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000724 first = false;
725 }
726
727 while (Tok.isNot(tok::r_square)) {
728 if (!first) {
Douglas Gregord8c61782012-02-15 15:34:24 +0000729 if (Tok.isNot(tok::comma)) {
Douglas Gregor721b14d2012-07-31 00:50:07 +0000730 // Provide a completion for a lambda introducer here. Except
731 // in Objective-C, where this is Almost Surely meant to be a message
732 // send. In that case, fail here and let the ObjC message
733 // expression parser perform the completion.
Douglas Gregor2d8db8f2012-07-31 15:27:48 +0000734 if (Tok.is(tok::code_completion) &&
735 !(getLangOpts().ObjC1 && Intro.Default == LCD_None &&
736 !Intro.Captures.empty())) {
Douglas Gregord8c61782012-02-15 15:34:24 +0000737 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
738 /*AfterAmpersand=*/false);
739 ConsumeCodeCompletionToken();
740 break;
741 }
742
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000743 return DiagResult(diag::err_expected_comma_or_rsquare);
Douglas Gregord8c61782012-02-15 15:34:24 +0000744 }
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000745 ConsumeToken();
746 }
747
Douglas Gregord8c61782012-02-15 15:34:24 +0000748 if (Tok.is(tok::code_completion)) {
749 // If we're in Objective-C++ and we have a bare '[', then this is more
750 // likely to be a message receiver.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000751 if (getLangOpts().ObjC1 && first)
Douglas Gregord8c61782012-02-15 15:34:24 +0000752 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
753 else
754 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
755 /*AfterAmpersand=*/false);
756 ConsumeCodeCompletionToken();
757 break;
758 }
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000759
Douglas Gregord8c61782012-02-15 15:34:24 +0000760 first = false;
761
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000762 // Parse capture.
763 LambdaCaptureKind Kind = LCK_ByCopy;
764 SourceLocation Loc;
765 IdentifierInfo* Id = 0;
Douglas Gregor3e308b12012-02-14 19:27:52 +0000766 SourceLocation EllipsisLoc;
Richard Smith21b3ab42013-05-09 21:36:41 +0000767 ExprResult Init;
Douglas Gregor3e308b12012-02-14 19:27:52 +0000768
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000769 if (Tok.is(tok::kw_this)) {
770 Kind = LCK_This;
771 Loc = ConsumeToken();
772 } else {
773 if (Tok.is(tok::amp)) {
774 Kind = LCK_ByRef;
775 ConsumeToken();
Douglas Gregord8c61782012-02-15 15:34:24 +0000776
777 if (Tok.is(tok::code_completion)) {
778 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
779 /*AfterAmpersand=*/true);
780 ConsumeCodeCompletionToken();
781 break;
782 }
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000783 }
784
785 if (Tok.is(tok::identifier)) {
786 Id = Tok.getIdentifierInfo();
787 Loc = ConsumeToken();
788 } else if (Tok.is(tok::kw_this)) {
789 // FIXME: If we want to suggest a fixit here, will need to return more
790 // than just DiagnosticID. Perhaps full DiagnosticBuilder that can be
791 // Clear()ed to prevent emission in case of tentative parsing?
792 return DiagResult(diag::err_this_captured_by_reference);
793 } else {
794 return DiagResult(diag::err_expected_capture);
795 }
Richard Smith21b3ab42013-05-09 21:36:41 +0000796
797 if (Tok.is(tok::l_paren)) {
798 BalancedDelimiterTracker Parens(*this, tok::l_paren);
799 Parens.consumeOpen();
800
801 ExprVector Exprs;
802 CommaLocsTy Commas;
Richard Smithf44d2a82013-05-21 22:21:19 +0000803 if (SkippedInits) {
804 Parens.skipToEnd();
805 *SkippedInits = true;
806 } else if (ParseExpressionList(Exprs, Commas)) {
Richard Smith21b3ab42013-05-09 21:36:41 +0000807 Parens.skipToEnd();
808 Init = ExprError();
809 } else {
810 Parens.consumeClose();
811 Init = Actions.ActOnParenListExpr(Parens.getOpenLocation(),
812 Parens.getCloseLocation(),
813 Exprs);
814 }
815 } else if (Tok.is(tok::l_brace) || Tok.is(tok::equal)) {
816 if (Tok.is(tok::equal))
817 ConsumeToken();
818
Richard Smithf44d2a82013-05-21 22:21:19 +0000819 if (!SkippedInits)
820 Init = ParseInitializer();
821 else if (Tok.is(tok::l_brace)) {
822 BalancedDelimiterTracker Braces(*this, tok::l_brace);
823 Braces.consumeOpen();
824 Braces.skipToEnd();
825 *SkippedInits = true;
826 } else {
827 // We're disambiguating this:
828 //
829 // [..., x = expr
830 //
831 // We need to find the end of the following expression in order to
832 // determine whether this is an Obj-C message send's receiver, or a
833 // lambda init-capture.
834 //
835 // Parse the expression to find where it ends, and annotate it back
836 // onto the tokens. We would have parsed this expression the same way
837 // in either case: both the RHS of an init-capture and the RHS of an
838 // assignment expression are parsed as an initializer-clause, and in
839 // neither case can anything be added to the scope between the '[' and
840 // here.
841 //
842 // FIXME: This is horrible. Adding a mechanism to skip an expression
843 // would be much cleaner.
844 // FIXME: If there is a ',' before the next ']' or ':', we can skip to
845 // that instead. (And if we see a ':' with no matching '?', we can
846 // classify this as an Obj-C message send.)
847 SourceLocation StartLoc = Tok.getLocation();
848 InMessageExpressionRAIIObject MaybeInMessageExpression(*this, true);
849 Init = ParseInitializer();
850
851 if (Tok.getLocation() != StartLoc) {
852 // Back out the lexing of the token after the initializer.
853 PP.RevertCachedTokens(1);
854
855 // Replace the consumed tokens with an appropriate annotation.
856 Tok.setLocation(StartLoc);
857 Tok.setKind(tok::annot_primary_expr);
858 setExprAnnotation(Tok, Init);
859 Tok.setAnnotationEndLoc(PP.getLastCachedTokenLocation());
860 PP.AnnotateCachedTokens(Tok);
861
862 // Consume the annotated initializer.
863 ConsumeToken();
864 }
865 }
Richard Smithba71c082013-05-16 06:20:58 +0000866 } else if (Tok.is(tok::ellipsis))
867 EllipsisLoc = ConsumeToken();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000868 }
869
Richard Smith21b3ab42013-05-09 21:36:41 +0000870 Intro.addCapture(Kind, Loc, Id, EllipsisLoc, Init);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000871 }
872
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000873 T.consumeClose();
874 Intro.Range.setEnd(T.getCloseLocation());
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000875
876 return DiagResult();
877}
878
Douglas Gregord8c61782012-02-15 15:34:24 +0000879/// TryParseLambdaIntroducer - Tentatively parse a lambda introducer.
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000880///
881/// Returns true if it hit something unexpected.
882bool Parser::TryParseLambdaIntroducer(LambdaIntroducer &Intro) {
883 TentativeParsingAction PA(*this);
884
Richard Smithf44d2a82013-05-21 22:21:19 +0000885 bool SkippedInits = false;
886 Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro, &SkippedInits));
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000887
888 if (DiagID) {
889 PA.Revert();
890 return true;
891 }
892
Richard Smithf44d2a82013-05-21 22:21:19 +0000893 if (SkippedInits) {
894 // Parse it again, but this time parse the init-captures too.
895 PA.Revert();
896 Intro = LambdaIntroducer();
897 DiagID = ParseLambdaIntroducer(Intro);
898 assert(!DiagID && "parsing lambda-introducer failed on reparse");
899 return false;
900 }
901
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000902 PA.Commit();
903 return false;
904}
905
906/// ParseLambdaExpressionAfterIntroducer - Parse the rest of a lambda
907/// expression.
908ExprResult Parser::ParseLambdaExpressionAfterIntroducer(
909 LambdaIntroducer &Intro) {
Eli Friedmanc7c97142012-01-04 02:40:39 +0000910 SourceLocation LambdaBeginLoc = Intro.Range.getBegin();
911 Diag(LambdaBeginLoc, diag::warn_cxx98_compat_lambda);
912
913 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), LambdaBeginLoc,
914 "lambda expression parsing");
915
Faisal Vali2b391ab2013-09-26 19:54:12 +0000916
917
Richard Smith21b3ab42013-05-09 21:36:41 +0000918 // FIXME: Call into Actions to add any init-capture declarations to the
919 // scope while parsing the lambda-declarator and compound-statement.
920
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000921 // Parse lambda-declarator[opt].
922 DeclSpec DS(AttrFactory);
Eli Friedman36d12942012-01-04 04:41:38 +0000923 Declarator D(DS, Declarator::LambdaExprContext);
Faisal Vali2b391ab2013-09-26 19:54:12 +0000924 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
925 Actions.PushLambdaScope();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000926
927 if (Tok.is(tok::l_paren)) {
928 ParseScope PrototypeScope(this,
929 Scope::FunctionPrototypeScope |
Richard Smithe233fbf2013-01-28 22:42:45 +0000930 Scope::FunctionDeclarationScope |
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000931 Scope::DeclScope);
932
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000933 SourceLocation DeclEndLoc;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000934 BalancedDelimiterTracker T(*this, tok::l_paren);
935 T.consumeOpen();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000936 SourceLocation LParenLoc = T.getOpenLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000937
938 // Parse parameter-declaration-clause.
939 ParsedAttributes Attr(AttrFactory);
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000940 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000941 SourceLocation EllipsisLoc;
942
Faisal Vali2b391ab2013-09-26 19:54:12 +0000943
944 if (Tok.isNot(tok::r_paren)) {
Faisal Vali2b391ab2013-09-26 19:54:12 +0000945 Actions.RecordParsingTemplateParameterDepth(TemplateParameterDepth);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000946 ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc);
Faisal Vali2b391ab2013-09-26 19:54:12 +0000947 // For a generic lambda, each 'auto' within the parameter declaration
948 // clause creates a template type parameter, so increment the depth.
949 if (Actions.getCurGenericLambda())
950 ++CurTemplateDepthTracker;
951 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000952 T.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000953 SourceLocation RParenLoc = T.getCloseLocation();
954 DeclEndLoc = RParenLoc;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000955
956 // Parse 'mutable'[opt].
957 SourceLocation MutableLoc;
958 if (Tok.is(tok::kw_mutable)) {
959 MutableLoc = ConsumeToken();
960 DeclEndLoc = MutableLoc;
961 }
962
963 // Parse exception-specification[opt].
964 ExceptionSpecificationType ESpecType = EST_None;
965 SourceRange ESpecRange;
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000966 SmallVector<ParsedType, 2> DynamicExceptions;
967 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000968 ExprResult NoexceptExpr;
Richard Smith2331bbf2012-05-02 22:22:32 +0000969 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +0000970 DynamicExceptions,
971 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +0000972 NoexceptExpr);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000973
974 if (ESpecType != EST_None)
975 DeclEndLoc = ESpecRange.getEnd();
976
977 // Parse attribute-specifier[opt].
Richard Smith89645bc2013-01-02 12:01:23 +0000978 MaybeParseCXX11Attributes(Attr, &DeclEndLoc);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000979
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000980 SourceLocation FunLocalRangeEnd = DeclEndLoc;
981
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000982 // Parse trailing-return-type[opt].
Richard Smith700537c2012-06-12 01:51:59 +0000983 TypeResult TrailingReturnType;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000984 if (Tok.is(tok::arrow)) {
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000985 FunLocalRangeEnd = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000986 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +0000987 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000988 if (Range.getEnd().isValid())
989 DeclEndLoc = Range.getEnd();
990 }
991
992 PrototypeScope.Exit();
993
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000994 SourceLocation NoLoc;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000995 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000996 /*isAmbiguous=*/false,
997 LParenLoc,
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000998 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000999 EllipsisLoc, RParenLoc,
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001000 DS.getTypeQualifiers(),
1001 /*RefQualifierIsLValueRef=*/true,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001002 /*RefQualifierLoc=*/NoLoc,
1003 /*ConstQualifierLoc=*/NoLoc,
1004 /*VolatileQualifierLoc=*/NoLoc,
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001005 MutableLoc,
1006 ESpecType, ESpecRange.getBegin(),
1007 DynamicExceptions.data(),
1008 DynamicExceptionRanges.data(),
1009 DynamicExceptions.size(),
1010 NoexceptExpr.isUsable() ?
1011 NoexceptExpr.get() : 0,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001012 LParenLoc, FunLocalRangeEnd, D,
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001013 TrailingReturnType),
1014 Attr, DeclEndLoc);
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001015 } else if (Tok.is(tok::kw_mutable) || Tok.is(tok::arrow)) {
1016 // It's common to forget that one needs '()' before 'mutable' or the
1017 // result type. Deal with this.
1018 Diag(Tok, diag::err_lambda_missing_parens)
1019 << Tok.is(tok::arrow)
1020 << FixItHint::CreateInsertion(Tok.getLocation(), "() ");
1021 SourceLocation DeclLoc = Tok.getLocation();
1022 SourceLocation DeclEndLoc = DeclLoc;
1023
1024 // Parse 'mutable', if it's there.
1025 SourceLocation MutableLoc;
1026 if (Tok.is(tok::kw_mutable)) {
1027 MutableLoc = ConsumeToken();
1028 DeclEndLoc = MutableLoc;
1029 }
1030
1031 // Parse the return type, if there is one.
Richard Smith700537c2012-06-12 01:51:59 +00001032 TypeResult TrailingReturnType;
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001033 if (Tok.is(tok::arrow)) {
1034 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00001035 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001036 if (Range.getEnd().isValid())
1037 DeclEndLoc = Range.getEnd();
1038 }
1039
1040 ParsedAttributes Attr(AttrFactory);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001041 SourceLocation NoLoc;
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001042 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001043 /*isAmbiguous=*/false,
1044 /*LParenLoc=*/NoLoc,
1045 /*Params=*/0,
1046 /*NumParams=*/0,
1047 /*EllipsisLoc=*/NoLoc,
1048 /*RParenLoc=*/NoLoc,
1049 /*TypeQuals=*/0,
1050 /*RefQualifierIsLValueRef=*/true,
1051 /*RefQualifierLoc=*/NoLoc,
1052 /*ConstQualifierLoc=*/NoLoc,
1053 /*VolatileQualifierLoc=*/NoLoc,
1054 MutableLoc,
1055 EST_None,
1056 /*ESpecLoc=*/NoLoc,
1057 /*Exceptions=*/0,
1058 /*ExceptionRanges=*/0,
1059 /*NumExceptions=*/0,
1060 /*NoexceptExpr=*/0,
1061 DeclLoc, DeclEndLoc, D,
1062 TrailingReturnType),
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001063 Attr, DeclEndLoc);
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001064 }
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001065
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001066
Eli Friedman4817cf72012-01-06 03:05:34 +00001067 // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
1068 // it.
Douglas Gregorb8389972012-02-21 22:51:27 +00001069 unsigned ScopeFlags = Scope::BlockScope | Scope::FnScope | Scope::DeclScope;
Douglas Gregorb8389972012-02-21 22:51:27 +00001070 ParseScope BodyScope(this, ScopeFlags);
Eli Friedman4817cf72012-01-06 03:05:34 +00001071
Eli Friedman71c80552012-01-05 03:35:19 +00001072 Actions.ActOnStartOfLambdaDefinition(Intro, D, getCurScope());
1073
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001074 // Parse compound-statement.
Eli Friedmanc7c97142012-01-04 02:40:39 +00001075 if (!Tok.is(tok::l_brace)) {
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001076 Diag(Tok, diag::err_expected_lambda_body);
Eli Friedmanc7c97142012-01-04 02:40:39 +00001077 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
1078 return ExprError();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001079 }
1080
Eli Friedmanc7c97142012-01-04 02:40:39 +00001081 StmtResult Stmt(ParseCompoundStatementBody());
1082 BodyScope.Exit();
1083
Eli Friedman898caf82012-01-04 02:46:53 +00001084 if (!Stmt.isInvalid())
Douglas Gregor63798542012-02-20 19:44:39 +00001085 return Actions.ActOnLambdaExpr(LambdaBeginLoc, Stmt.take(), getCurScope());
Eli Friedmanc7c97142012-01-04 02:40:39 +00001086
Eli Friedman898caf82012-01-04 02:46:53 +00001087 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
1088 return ExprError();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001089}
1090
Chris Lattner29375652006-12-04 18:06:35 +00001091/// ParseCXXCasts - This handles the various ways to cast expressions to another
1092/// type.
1093///
1094/// postfix-expression: [C++ 5.2p1]
1095/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
1096/// 'static_cast' '<' type-name '>' '(' expression ')'
1097/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
1098/// 'const_cast' '<' type-name '>' '(' expression ')'
1099///
John McCalldadc5752010-08-24 06:29:42 +00001100ExprResult Parser::ParseCXXCasts() {
Chris Lattner29375652006-12-04 18:06:35 +00001101 tok::TokenKind Kind = Tok.getKind();
1102 const char *CastName = 0; // For error messages
1103
1104 switch (Kind) {
David Blaikieaa347f92011-09-23 20:26:49 +00001105 default: llvm_unreachable("Unknown C++ cast!");
Chris Lattner29375652006-12-04 18:06:35 +00001106 case tok::kw_const_cast: CastName = "const_cast"; break;
1107 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
1108 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
1109 case tok::kw_static_cast: CastName = "static_cast"; break;
1110 }
1111
1112 SourceLocation OpLoc = ConsumeToken();
1113 SourceLocation LAngleBracketLoc = Tok.getLocation();
1114
Richard Smith55858492011-04-14 21:45:45 +00001115 // Check for "<::" which is parsed as "[:". If found, fix token stream,
1116 // diagnose error, suggest fix, and recover parsing.
Richard Smith62e66302012-08-20 17:37:52 +00001117 if (Tok.is(tok::l_square) && Tok.getLength() == 2) {
1118 Token Next = NextToken();
1119 if (Next.is(tok::colon) && areTokensAdjacent(Tok, Next))
1120 FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
1121 }
Richard Smith55858492011-04-14 21:45:45 +00001122
Chris Lattner29375652006-12-04 18:06:35 +00001123 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redld65cea82008-12-11 22:51:44 +00001124 return ExprError();
Chris Lattner29375652006-12-04 18:06:35 +00001125
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +00001126 // Parse the common declaration-specifiers piece.
1127 DeclSpec DS(AttrFactory);
1128 ParseSpecifierQualifierList(DS);
1129
1130 // Parse the abstract-declarator, if present.
1131 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1132 ParseDeclarator(DeclaratorInfo);
1133
Chris Lattner29375652006-12-04 18:06:35 +00001134 SourceLocation RAngleBracketLoc = Tok.getLocation();
1135
Chris Lattner6d29c102008-11-18 07:48:38 +00001136 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redld65cea82008-12-11 22:51:44 +00001137 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Chris Lattner29375652006-12-04 18:06:35 +00001138
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001139 SourceLocation LParenLoc, RParenLoc;
1140 BalancedDelimiterTracker T(*this, tok::l_paren);
Chris Lattner29375652006-12-04 18:06:35 +00001141
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001142 if (T.expectAndConsume(diag::err_expected_lparen_after, CastName))
Argyrios Kyrtzidis387a3342009-05-22 10:23:16 +00001143 return ExprError();
Chris Lattner29375652006-12-04 18:06:35 +00001144
John McCalldadc5752010-08-24 06:29:42 +00001145 ExprResult Result = ParseExpression();
Mike Stump11289f42009-09-09 15:08:12 +00001146
Argyrios Kyrtzidis387a3342009-05-22 10:23:16 +00001147 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001148 T.consumeClose();
Chris Lattner29375652006-12-04 18:06:35 +00001149
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +00001150 if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
Douglas Gregore200adc2008-10-27 19:41:14 +00001151 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +00001152 LAngleBracketLoc, DeclaratorInfo,
Douglas Gregor220cac52009-02-18 17:45:20 +00001153 RAngleBracketLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001154 T.getOpenLocation(), Result.take(),
1155 T.getCloseLocation());
Chris Lattner29375652006-12-04 18:06:35 +00001156
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001157 return Result;
Chris Lattner29375652006-12-04 18:06:35 +00001158}
Bill Wendling4073ed52007-02-13 01:51:42 +00001159
Sebastian Redlc4704762008-11-11 11:37:55 +00001160/// ParseCXXTypeid - This handles the C++ typeid expression.
1161///
1162/// postfix-expression: [C++ 5.2p1]
1163/// 'typeid' '(' expression ')'
1164/// 'typeid' '(' type-id ')'
1165///
John McCalldadc5752010-08-24 06:29:42 +00001166ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc4704762008-11-11 11:37:55 +00001167 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
1168
1169 SourceLocation OpLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001170 SourceLocation LParenLoc, RParenLoc;
1171 BalancedDelimiterTracker T(*this, tok::l_paren);
Sebastian Redlc4704762008-11-11 11:37:55 +00001172
1173 // typeid expressions are always parenthesized.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001174 if (T.expectAndConsume(diag::err_expected_lparen_after, "typeid"))
Sebastian Redld65cea82008-12-11 22:51:44 +00001175 return ExprError();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001176 LParenLoc = T.getOpenLocation();
Sebastian Redlc4704762008-11-11 11:37:55 +00001177
John McCalldadc5752010-08-24 06:29:42 +00001178 ExprResult Result;
Sebastian Redlc4704762008-11-11 11:37:55 +00001179
Richard Smith4f605af2012-08-18 00:55:03 +00001180 // C++0x [expr.typeid]p3:
1181 // When typeid is applied to an expression other than an lvalue of a
1182 // polymorphic class type [...] The expression is an unevaluated
1183 // operand (Clause 5).
1184 //
1185 // Note that we can't tell whether the expression is an lvalue of a
1186 // polymorphic class type until after we've parsed the expression; we
1187 // speculatively assume the subexpression is unevaluated, and fix it up
1188 // later.
1189 //
1190 // We enter the unevaluated context before trying to determine whether we
1191 // have a type-id, because the tentative parse logic will try to resolve
1192 // names, and must treat them as unevaluated.
Eli Friedman15681d62012-09-26 04:34:21 +00001193 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
1194 Sema::ReuseLambdaContextDecl);
Richard Smith4f605af2012-08-18 00:55:03 +00001195
Sebastian Redlc4704762008-11-11 11:37:55 +00001196 if (isTypeIdInParens()) {
Douglas Gregor220cac52009-02-18 17:45:20 +00001197 TypeResult Ty = ParseTypeName();
Sebastian Redlc4704762008-11-11 11:37:55 +00001198
1199 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001200 T.consumeClose();
1201 RParenLoc = T.getCloseLocation();
Douglas Gregor4c7c1092010-09-08 23:14:30 +00001202 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +00001203 return ExprError();
Sebastian Redlc4704762008-11-11 11:37:55 +00001204
1205 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallba7bf592010-08-24 05:47:05 +00001206 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc4704762008-11-11 11:37:55 +00001207 } else {
1208 Result = ParseExpression();
1209
1210 // Match the ')'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001211 if (Result.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00001212 SkipUntil(tok::r_paren, StopAtSemi);
Sebastian Redlc4704762008-11-11 11:37:55 +00001213 else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001214 T.consumeClose();
1215 RParenLoc = T.getCloseLocation();
Douglas Gregor4c7c1092010-09-08 23:14:30 +00001216 if (RParenLoc.isInvalid())
1217 return ExprError();
Douglas Gregor1beec452011-03-12 01:48:56 +00001218
Sebastian Redlc4704762008-11-11 11:37:55 +00001219 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001220 Result.release(), RParenLoc);
Sebastian Redlc4704762008-11-11 11:37:55 +00001221 }
1222 }
1223
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001224 return Result;
Sebastian Redlc4704762008-11-11 11:37:55 +00001225}
1226
Francois Pichet9f4f2072010-09-08 12:20:18 +00001227/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
1228///
1229/// '__uuidof' '(' expression ')'
1230/// '__uuidof' '(' type-id ')'
1231///
1232ExprResult Parser::ParseCXXUuidof() {
1233 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
1234
1235 SourceLocation OpLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001236 BalancedDelimiterTracker T(*this, tok::l_paren);
Francois Pichet9f4f2072010-09-08 12:20:18 +00001237
1238 // __uuidof expressions are always parenthesized.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001239 if (T.expectAndConsume(diag::err_expected_lparen_after, "__uuidof"))
Francois Pichet9f4f2072010-09-08 12:20:18 +00001240 return ExprError();
1241
1242 ExprResult Result;
1243
1244 if (isTypeIdInParens()) {
1245 TypeResult Ty = ParseTypeName();
1246
1247 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001248 T.consumeClose();
Francois Pichet9f4f2072010-09-08 12:20:18 +00001249
1250 if (Ty.isInvalid())
1251 return ExprError();
1252
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001253 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), /*isType=*/true,
1254 Ty.get().getAsOpaquePtr(),
1255 T.getCloseLocation());
Francois Pichet9f4f2072010-09-08 12:20:18 +00001256 } else {
1257 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1258 Result = ParseExpression();
1259
1260 // Match the ')'.
1261 if (Result.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00001262 SkipUntil(tok::r_paren, StopAtSemi);
Francois Pichet9f4f2072010-09-08 12:20:18 +00001263 else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001264 T.consumeClose();
Francois Pichet9f4f2072010-09-08 12:20:18 +00001265
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001266 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(),
1267 /*isType=*/false,
1268 Result.release(), T.getCloseLocation());
Francois Pichet9f4f2072010-09-08 12:20:18 +00001269 }
1270 }
1271
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001272 return Result;
Francois Pichet9f4f2072010-09-08 12:20:18 +00001273}
1274
Douglas Gregore610ada2010-02-24 18:44:31 +00001275/// \brief Parse a C++ pseudo-destructor expression after the base,
1276/// . or -> operator, and nested-name-specifier have already been
1277/// parsed.
1278///
1279/// postfix-expression: [C++ 5.2]
1280/// postfix-expression . pseudo-destructor-name
1281/// postfix-expression -> pseudo-destructor-name
1282///
1283/// pseudo-destructor-name:
1284/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
1285/// ::[opt] nested-name-specifier template simple-template-id ::
1286/// ~type-name
1287/// ::[opt] nested-name-specifier[opt] ~type-name
1288///
John McCalldadc5752010-08-24 06:29:42 +00001289ExprResult
Douglas Gregore610ada2010-02-24 18:44:31 +00001290Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
1291 tok::TokenKind OpKind,
1292 CXXScopeSpec &SS,
John McCallba7bf592010-08-24 05:47:05 +00001293 ParsedType ObjectType) {
Douglas Gregore610ada2010-02-24 18:44:31 +00001294 // We're parsing either a pseudo-destructor-name or a dependent
1295 // member access that has the same form as a
1296 // pseudo-destructor-name. We parse both in the same way and let
1297 // the action model sort them out.
1298 //
1299 // Note that the ::[opt] nested-name-specifier[opt] has already
1300 // been parsed, and if there was a simple-template-id, it has
1301 // been coalesced into a template-id annotation token.
1302 UnqualifiedId FirstTypeName;
1303 SourceLocation CCLoc;
1304 if (Tok.is(tok::identifier)) {
1305 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1306 ConsumeToken();
1307 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1308 CCLoc = ConsumeToken();
1309 } else if (Tok.is(tok::annot_template_id)) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001310 // FIXME: retrieve TemplateKWLoc from template-id annotation and
1311 // store it in the pseudo-dtor node (to be used when instantiating it).
Douglas Gregore610ada2010-02-24 18:44:31 +00001312 FirstTypeName.setTemplateId(
1313 (TemplateIdAnnotation *)Tok.getAnnotationValue());
1314 ConsumeToken();
1315 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1316 CCLoc = ConsumeToken();
1317 } else {
1318 FirstTypeName.setIdentifier(0, SourceLocation());
1319 }
1320
1321 // Parse the tilde.
1322 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
1323 SourceLocation TildeLoc = ConsumeToken();
David Blaikie1d578782011-12-16 16:03:09 +00001324
1325 if (Tok.is(tok::kw_decltype) && !FirstTypeName.isValid() && SS.isEmpty()) {
1326 DeclSpec DS(AttrFactory);
Benjamin Kramer198e0832011-12-18 12:18:02 +00001327 ParseDecltypeSpecifier(DS);
David Blaikie1d578782011-12-16 16:03:09 +00001328 if (DS.getTypeSpecType() == TST_error)
1329 return ExprError();
1330 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, OpLoc,
1331 OpKind, TildeLoc, DS,
1332 Tok.is(tok::l_paren));
1333 }
1334
Douglas Gregore610ada2010-02-24 18:44:31 +00001335 if (!Tok.is(tok::identifier)) {
1336 Diag(Tok, diag::err_destructor_tilde_identifier);
1337 return ExprError();
1338 }
1339
1340 // Parse the second type.
1341 UnqualifiedId SecondTypeName;
1342 IdentifierInfo *Name = Tok.getIdentifierInfo();
1343 SourceLocation NameLoc = ConsumeToken();
1344 SecondTypeName.setIdentifier(Name, NameLoc);
1345
1346 // If there is a '<', the second type name is a template-id. Parse
1347 // it as such.
1348 if (Tok.is(tok::less) &&
Abramo Bagnara7945c982012-01-27 09:46:47 +00001349 ParseUnqualifiedIdTemplateId(SS, SourceLocation(),
1350 Name, NameLoc,
1351 false, ObjectType, SecondTypeName,
1352 /*AssumeTemplateName=*/true))
Douglas Gregore610ada2010-02-24 18:44:31 +00001353 return ExprError();
1354
John McCallb268a282010-08-23 23:25:46 +00001355 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
1356 OpLoc, OpKind,
Douglas Gregore610ada2010-02-24 18:44:31 +00001357 SS, FirstTypeName, CCLoc,
1358 TildeLoc, SecondTypeName,
1359 Tok.is(tok::l_paren));
1360}
1361
Bill Wendling4073ed52007-02-13 01:51:42 +00001362/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
1363///
1364/// boolean-literal: [C++ 2.13.5]
1365/// 'true'
1366/// 'false'
John McCalldadc5752010-08-24 06:29:42 +00001367ExprResult Parser::ParseCXXBoolLiteral() {
Bill Wendling4073ed52007-02-13 01:51:42 +00001368 tok::TokenKind Kind = Tok.getKind();
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001369 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Bill Wendling4073ed52007-02-13 01:51:42 +00001370}
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001371
1372/// ParseThrowExpression - This handles the C++ throw expression.
1373///
1374/// throw-expression: [C++ 15]
1375/// 'throw' assignment-expression[opt]
John McCalldadc5752010-08-24 06:29:42 +00001376ExprResult Parser::ParseThrowExpression() {
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001377 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001378 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redld65cea82008-12-11 22:51:44 +00001379
Chris Lattner65dd8432008-04-06 06:02:23 +00001380 // If the current token isn't the start of an assignment-expression,
1381 // then the expression is not present. This handles things like:
1382 // "C ? throw : (void)42", which is crazy but legal.
1383 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
1384 case tok::semi:
1385 case tok::r_paren:
1386 case tok::r_square:
1387 case tok::r_brace:
1388 case tok::colon:
1389 case tok::comma:
Douglas Gregor53e191ed2011-07-06 22:04:06 +00001390 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0);
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001391
Chris Lattner65dd8432008-04-06 06:02:23 +00001392 default:
John McCalldadc5752010-08-24 06:29:42 +00001393 ExprResult Expr(ParseAssignmentExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001394 if (Expr.isInvalid()) return Expr;
Douglas Gregor53e191ed2011-07-06 22:04:06 +00001395 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take());
Chris Lattner65dd8432008-04-06 06:02:23 +00001396 }
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001397}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001398
1399/// ParseCXXThis - This handles the C++ 'this' pointer.
1400///
1401/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
1402/// a non-lvalue expression whose value is the address of the object for which
1403/// the function is called.
John McCalldadc5752010-08-24 06:29:42 +00001404ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001405 assert(Tok.is(tok::kw_this) && "Not 'this'!");
1406 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001407 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001408}
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001409
1410/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1411/// Can be interpreted either as function-style casting ("int(x)")
1412/// or class type construction ("ClassType(x,y,z)")
1413/// or creation of a value-initialized type ("int()").
Sebastian Redl3da34892011-06-05 12:23:16 +00001414/// See [C++ 5.2.3].
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001415///
1416/// postfix-expression: [C++ 5.2p1]
Sebastian Redl3da34892011-06-05 12:23:16 +00001417/// simple-type-specifier '(' expression-list[opt] ')'
1418/// [C++0x] simple-type-specifier braced-init-list
1419/// typename-specifier '(' expression-list[opt] ')'
1420/// [C++0x] typename-specifier braced-init-list
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001421///
John McCalldadc5752010-08-24 06:29:42 +00001422ExprResult
Sebastian Redld65cea82008-12-11 22:51:44 +00001423Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001424 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallba7bf592010-08-24 05:47:05 +00001425 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001426
Sebastian Redl3da34892011-06-05 12:23:16 +00001427 assert((Tok.is(tok::l_paren) ||
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001428 (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)))
Sebastian Redl3da34892011-06-05 12:23:16 +00001429 && "Expected '(' or '{'!");
Douglas Gregor94a32472011-01-11 00:33:19 +00001430
Sebastian Redl3da34892011-06-05 12:23:16 +00001431 if (Tok.is(tok::l_brace)) {
Sebastian Redld74dd492012-02-12 18:41:05 +00001432 ExprResult Init = ParseBraceInitializer();
1433 if (Init.isInvalid())
1434 return Init;
1435 Expr *InitList = Init.take();
1436 return Actions.ActOnCXXTypeConstructExpr(TypeRep, SourceLocation(),
1437 MultiExprArg(&InitList, 1),
1438 SourceLocation());
Sebastian Redl3da34892011-06-05 12:23:16 +00001439 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001440 BalancedDelimiterTracker T(*this, tok::l_paren);
1441 T.consumeOpen();
Sebastian Redl3da34892011-06-05 12:23:16 +00001442
Benjamin Kramerf0623432012-08-23 22:51:59 +00001443 ExprVector Exprs;
Sebastian Redl3da34892011-06-05 12:23:16 +00001444 CommaLocsTy CommaLocs;
1445
1446 if (Tok.isNot(tok::r_paren)) {
1447 if (ParseExpressionList(Exprs, CommaLocs)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001448 SkipUntil(tok::r_paren, StopAtSemi);
Sebastian Redl3da34892011-06-05 12:23:16 +00001449 return ExprError();
1450 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001451 }
Sebastian Redl3da34892011-06-05 12:23:16 +00001452
1453 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001454 T.consumeClose();
Sebastian Redl3da34892011-06-05 12:23:16 +00001455
1456 // TypeRep could be null, if it references an invalid typedef.
1457 if (!TypeRep)
1458 return ExprError();
1459
1460 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1461 "Unexpected number of commas!");
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001462 return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001463 Exprs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001464 T.getCloseLocation());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001465 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001466}
1467
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001468/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001469///
1470/// condition:
1471/// expression
1472/// type-specifier-seq declarator '=' assignment-expression
Richard Smith2a15b742012-02-22 06:49:09 +00001473/// [C++11] type-specifier-seq declarator '=' initializer-clause
1474/// [C++11] type-specifier-seq declarator braced-init-list
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001475/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
1476/// '=' assignment-expression
1477///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00001478/// \param ExprOut if the condition was parsed as an expression, the parsed
1479/// expression.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001480///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00001481/// \param DeclOut if the condition was parsed as a declaration, the parsed
1482/// declaration.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001483///
Douglas Gregore60e41a2010-05-06 17:25:47 +00001484/// \param Loc The location of the start of the statement that requires this
1485/// condition, e.g., the "for" in a for loop.
1486///
1487/// \param ConvertToBoolean Whether the condition expression should be
1488/// converted to a boolean value.
1489///
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001490/// \returns true if there was a parsing, false otherwise.
John McCalldadc5752010-08-24 06:29:42 +00001491bool Parser::ParseCXXCondition(ExprResult &ExprOut,
1492 Decl *&DeclOut,
Douglas Gregore60e41a2010-05-06 17:25:47 +00001493 SourceLocation Loc,
1494 bool ConvertToBoolean) {
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001495 if (Tok.is(tok::code_completion)) {
John McCallfaf5fb42010-08-26 23:41:50 +00001496 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001497 cutOffParsing();
1498 return true;
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001499 }
1500
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001501 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00001502 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001503
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001504 if (!isCXXConditionDeclaration()) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001505 ProhibitAttributes(attrs);
1506
Douglas Gregore60e41a2010-05-06 17:25:47 +00001507 // Parse the expression.
John McCalldadc5752010-08-24 06:29:42 +00001508 ExprOut = ParseExpression(); // expression
1509 DeclOut = 0;
1510 if (ExprOut.isInvalid())
Douglas Gregore60e41a2010-05-06 17:25:47 +00001511 return true;
1512
1513 // If required, convert to a boolean value.
1514 if (ConvertToBoolean)
John McCalldadc5752010-08-24 06:29:42 +00001515 ExprOut
1516 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
1517 return ExprOut.isInvalid();
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001518 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001519
1520 // type-specifier-seq
John McCall084e83d2011-03-24 11:26:52 +00001521 DeclSpec DS(AttrFactory);
Richard Smith54ecd982013-02-20 19:22:51 +00001522 DS.takeAttributesFrom(attrs);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001523 ParseSpecifierQualifierList(DS);
1524
1525 // declarator
1526 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
1527 ParseDeclarator(DeclaratorInfo);
1528
1529 // simple-asm-expr[opt]
1530 if (Tok.is(tok::kw_asm)) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001531 SourceLocation Loc;
John McCalldadc5752010-08-24 06:29:42 +00001532 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001533 if (AsmLabel.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001534 SkipUntil(tok::semi, StopAtSemi);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001535 return true;
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001536 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001537 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001538 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001539 }
1540
1541 // If attributes are present, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001542 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001543
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001544 // Type-check the declaration itself.
John McCalldadc5752010-08-24 06:29:42 +00001545 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall53fa7142010-12-24 02:08:15 +00001546 DeclaratorInfo);
John McCalldadc5752010-08-24 06:29:42 +00001547 DeclOut = Dcl.get();
1548 ExprOut = ExprError();
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +00001549
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001550 // '=' assignment-expression
Richard Trieuc64d3232012-01-18 22:54:52 +00001551 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Smith2a15b742012-02-22 06:49:09 +00001552 bool CopyInitialization = isTokenEqualOrEqualTypo();
1553 if (CopyInitialization)
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001554 ConsumeToken();
Richard Smith2a15b742012-02-22 06:49:09 +00001555
1556 ExprResult InitExpr = ExprError();
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001557 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith2a15b742012-02-22 06:49:09 +00001558 Diag(Tok.getLocation(),
1559 diag::warn_cxx98_compat_generalized_initializer_lists);
1560 InitExpr = ParseBraceInitializer();
1561 } else if (CopyInitialization) {
1562 InitExpr = ParseAssignmentExpression();
1563 } else if (Tok.is(tok::l_paren)) {
1564 // This was probably an attempt to initialize the variable.
1565 SourceLocation LParen = ConsumeParen(), RParen = LParen;
Alexey Bataevee6507d2013-11-18 08:17:37 +00001566 if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch))
Richard Smith2a15b742012-02-22 06:49:09 +00001567 RParen = ConsumeParen();
1568 Diag(DeclOut ? DeclOut->getLocation() : LParen,
1569 diag::err_expected_init_in_condition_lparen)
1570 << SourceRange(LParen, RParen);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001571 } else {
Richard Smith2a15b742012-02-22 06:49:09 +00001572 Diag(DeclOut ? DeclOut->getLocation() : Tok.getLocation(),
1573 diag::err_expected_init_in_condition);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001574 }
Richard Smith2a15b742012-02-22 06:49:09 +00001575
1576 if (!InitExpr.isInvalid())
1577 Actions.AddInitializerToDecl(DeclOut, InitExpr.take(), !CopyInitialization,
Richard Smith74aeef52013-04-26 16:15:35 +00001578 DS.containsPlaceholderType());
Richard Smith27d807c2013-04-30 13:56:41 +00001579 else
1580 Actions.ActOnInitializerError(DeclOut);
Richard Smith2a15b742012-02-22 06:49:09 +00001581
Douglas Gregore60e41a2010-05-06 17:25:47 +00001582 // FIXME: Build a reference to this declaration? Convert it to bool?
1583 // (This is currently handled by Sema).
Richard Smithb2bc2e62011-02-21 20:05:19 +00001584
1585 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregore60e41a2010-05-06 17:25:47 +00001586
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001587 return false;
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001588}
1589
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001590/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1591/// This should only be called when the current token is known to be part of
1592/// simple-type-specifier.
1593///
1594/// simple-type-specifier:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001595/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001596/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1597/// char
1598/// wchar_t
1599/// bool
1600/// short
1601/// int
1602/// long
1603/// signed
1604/// unsigned
1605/// float
1606/// double
1607/// void
1608/// [GNU] typeof-specifier
1609/// [C++0x] auto [TODO]
1610///
1611/// type-name:
1612/// class-name
1613/// enum-name
1614/// typedef-name
1615///
1616void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1617 DS.SetRangeStart(Tok.getLocation());
1618 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +00001619 unsigned DiagID;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001620 SourceLocation Loc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001621
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001622 switch (Tok.getKind()) {
Chris Lattner45ddec32009-01-05 00:13:00 +00001623 case tok::identifier: // foo::bar
1624 case tok::coloncolon: // ::foo::bar
David Blaikie83d382b2011-09-23 05:06:16 +00001625 llvm_unreachable("Annotation token should already be formed!");
Mike Stump11289f42009-09-09 15:08:12 +00001626 default:
David Blaikie83d382b2011-09-23 05:06:16 +00001627 llvm_unreachable("Not a simple-type-specifier token!");
Chris Lattner45ddec32009-01-05 00:13:00 +00001628
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001629 // type-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00001630 case tok::annot_typename: {
Douglas Gregor0231d8d2011-01-19 20:10:05 +00001631 if (getTypeAnnotation(Tok))
1632 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
1633 getTypeAnnotation(Tok));
1634 else
1635 DS.SetTypeSpecError();
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001636
1637 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1638 ConsumeToken();
1639
1640 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1641 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1642 // Objective-C interface. If we don't have Objective-C or a '<', this is
1643 // just a normal reference to a typedef name.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001644 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001645 ParseObjCProtocolQualifiers(DS);
1646
1647 DS.Finish(Diags, PP);
1648 return;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001649 }
Mike Stump11289f42009-09-09 15:08:12 +00001650
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001651 // builtin types
1652 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001653 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001654 break;
1655 case tok::kw_long:
John McCall49bfce42009-08-03 20:12:06 +00001656 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001657 break;
Francois Pichet84133e42011-04-28 01:59:37 +00001658 case tok::kw___int64:
1659 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1660 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001661 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001662 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001663 break;
1664 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001665 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001666 break;
1667 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001668 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001669 break;
1670 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001671 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001672 break;
1673 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001674 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001675 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00001676 case tok::kw___int128:
1677 DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, DiagID);
1678 break;
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00001679 case tok::kw_half:
1680 DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
1681 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001682 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001683 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001684 break;
1685 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001686 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001687 break;
1688 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001689 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001690 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001691 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001692 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001693 break;
1694 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001695 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001696 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001697 case tok::kw_bool:
John McCall49bfce42009-08-03 20:12:06 +00001698 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001699 break;
David Blaikie25896afb2012-01-24 05:47:35 +00001700 case tok::annot_decltype:
1701 case tok::kw_decltype:
1702 DS.SetRangeEnd(ParseDecltypeSpecifier(DS));
1703 return DS.Finish(Diags, PP);
Mike Stump11289f42009-09-09 15:08:12 +00001704
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001705 // GNU typeof support.
1706 case tok::kw_typeof:
1707 ParseTypeofSpecifier(DS);
Douglas Gregore3e01a22009-04-01 22:41:11 +00001708 DS.Finish(Diags, PP);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001709 return;
1710 }
Chris Lattnera8a3f732009-01-06 05:06:21 +00001711 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001712 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1713 else
1714 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001715 ConsumeToken();
Douglas Gregore3e01a22009-04-01 22:41:11 +00001716 DS.Finish(Diags, PP);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001717}
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001718
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001719/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1720/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1721/// e.g., "const short int". Note that the DeclSpec is *not* finished
1722/// by parsing the type-specifier-seq, because these sequences are
1723/// typically followed by some form of declarator. Returns true and
1724/// emits diagnostics if this is not a type-specifier-seq, false
1725/// otherwise.
1726///
1727/// type-specifier-seq: [C++ 8.1]
1728/// type-specifier type-specifier-seq[opt]
1729///
1730bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
Richard Smithc5b05522012-03-12 07:56:15 +00001731 ParseSpecifierQualifierList(DS, AS_none, DSC_type_specifier);
Douglas Gregor40d732f2010-02-24 23:13:13 +00001732 DS.Finish(Diags, PP);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001733 return false;
1734}
1735
Douglas Gregor7861a802009-11-03 01:35:08 +00001736/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1737/// some form.
1738///
1739/// This routine is invoked when a '<' is encountered after an identifier or
1740/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1741/// whether the unqualified-id is actually a template-id. This routine will
1742/// then parse the template arguments and form the appropriate template-id to
1743/// return to the caller.
1744///
1745/// \param SS the nested-name-specifier that precedes this template-id, if
1746/// we're actually parsing a qualified-id.
1747///
1748/// \param Name for constructor and destructor names, this is the actual
1749/// identifier that may be a template-name.
1750///
1751/// \param NameLoc the location of the class-name in a constructor or
1752/// destructor.
1753///
1754/// \param EnteringContext whether we're entering the scope of the
1755/// nested-name-specifier.
1756///
Douglas Gregor127ea592009-11-03 21:24:04 +00001757/// \param ObjectType if this unqualified-id occurs within a member access
1758/// expression, the type of the base object whose member is being accessed.
1759///
Douglas Gregor7861a802009-11-03 01:35:08 +00001760/// \param Id as input, describes the template-name or operator-function-id
1761/// that precedes the '<'. If template arguments were parsed successfully,
1762/// will be updated with the template-id.
1763///
Douglas Gregore610ada2010-02-24 18:44:31 +00001764/// \param AssumeTemplateId When true, this routine will assume that the name
1765/// refers to a template without performing name lookup to verify.
1766///
Douglas Gregor7861a802009-11-03 01:35:08 +00001767/// \returns true if a parse error occurred, false otherwise.
1768bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001769 SourceLocation TemplateKWLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00001770 IdentifierInfo *Name,
1771 SourceLocation NameLoc,
1772 bool EnteringContext,
John McCallba7bf592010-08-24 05:47:05 +00001773 ParsedType ObjectType,
Douglas Gregore610ada2010-02-24 18:44:31 +00001774 UnqualifiedId &Id,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001775 bool AssumeTemplateId) {
Douglas Gregorb22ee882010-05-05 05:58:24 +00001776 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1777 "Expected '<' to finish parsing a template-id");
Douglas Gregor7861a802009-11-03 01:35:08 +00001778
1779 TemplateTy Template;
1780 TemplateNameKind TNK = TNK_Non_template;
1781 switch (Id.getKind()) {
1782 case UnqualifiedId::IK_Identifier:
Douglas Gregor3cf81312009-11-03 23:16:33 +00001783 case UnqualifiedId::IK_OperatorFunctionId:
Alexis Hunted0530f2009-11-28 08:58:14 +00001784 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregore610ada2010-02-24 18:44:31 +00001785 if (AssumeTemplateId) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001786 TNK = Actions.ActOnDependentTemplateName(getCurScope(), SS, TemplateKWLoc,
Douglas Gregorbb119652010-06-16 23:00:59 +00001787 Id, ObjectType, EnteringContext,
1788 Template);
1789 if (TNK == TNK_Non_template)
1790 return true;
Douglas Gregor786123d2010-05-21 23:18:07 +00001791 } else {
1792 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001793 TNK = Actions.isTemplateName(getCurScope(), SS,
1794 TemplateKWLoc.isValid(), Id,
1795 ObjectType, EnteringContext, Template,
Douglas Gregor786123d2010-05-21 23:18:07 +00001796 MemberOfUnknownSpecialization);
1797
1798 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1799 ObjectType && IsTemplateArgumentList()) {
1800 // We have something like t->getAs<T>(), where getAs is a
1801 // member of an unknown specialization. However, this will only
1802 // parse correctly as a template, so suggest the keyword 'template'
1803 // before 'getAs' and treat this as a dependent template name.
1804 std::string Name;
1805 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1806 Name = Id.Identifier->getName();
1807 else {
1808 Name = "operator ";
1809 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1810 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1811 else
1812 Name += Id.Identifier->getName();
1813 }
1814 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1815 << Name
1816 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Abramo Bagnara7945c982012-01-27 09:46:47 +00001817 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1818 SS, TemplateKWLoc, Id,
1819 ObjectType, EnteringContext,
1820 Template);
Douglas Gregorbb119652010-06-16 23:00:59 +00001821 if (TNK == TNK_Non_template)
Douglas Gregor786123d2010-05-21 23:18:07 +00001822 return true;
1823 }
1824 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001825 break;
1826
Douglas Gregor3cf81312009-11-03 23:16:33 +00001827 case UnqualifiedId::IK_ConstructorName: {
1828 UnqualifiedId TemplateName;
Douglas Gregor786123d2010-05-21 23:18:07 +00001829 bool MemberOfUnknownSpecialization;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001830 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001831 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1832 TemplateName, ObjectType,
Douglas Gregor786123d2010-05-21 23:18:07 +00001833 EnteringContext, Template,
1834 MemberOfUnknownSpecialization);
Douglas Gregor7861a802009-11-03 01:35:08 +00001835 break;
1836 }
1837
Douglas Gregor3cf81312009-11-03 23:16:33 +00001838 case UnqualifiedId::IK_DestructorName: {
1839 UnqualifiedId TemplateName;
Douglas Gregor786123d2010-05-21 23:18:07 +00001840 bool MemberOfUnknownSpecialization;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001841 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001842 if (ObjectType) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001843 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1844 SS, TemplateKWLoc, TemplateName,
1845 ObjectType, EnteringContext,
1846 Template);
Douglas Gregorbb119652010-06-16 23:00:59 +00001847 if (TNK == TNK_Non_template)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001848 return true;
1849 } else {
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001850 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1851 TemplateName, ObjectType,
Douglas Gregor786123d2010-05-21 23:18:07 +00001852 EnteringContext, Template,
1853 MemberOfUnknownSpecialization);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001854
John McCallba7bf592010-08-24 05:47:05 +00001855 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00001856 Diag(NameLoc, diag::err_destructor_template_id)
1857 << Name << SS.getRange();
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001858 return true;
1859 }
1860 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001861 break;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001862 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001863
1864 default:
1865 return false;
1866 }
1867
1868 if (TNK == TNK_Non_template)
1869 return false;
1870
1871 // Parse the enclosed template argument list.
1872 SourceLocation LAngleLoc, RAngleLoc;
1873 TemplateArgList TemplateArgs;
Douglas Gregorb22ee882010-05-05 05:58:24 +00001874 if (Tok.is(tok::less) &&
1875 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregore7c20652011-03-02 00:47:37 +00001876 SS, true, LAngleLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00001877 TemplateArgs,
Douglas Gregor7861a802009-11-03 01:35:08 +00001878 RAngleLoc))
1879 return true;
1880
1881 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Alexis Hunted0530f2009-11-28 08:58:14 +00001882 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1883 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor7861a802009-11-03 01:35:08 +00001884 // Form a parsed representation of the template-id to be stored in the
1885 // UnqualifiedId.
1886 TemplateIdAnnotation *TemplateId
Benjamin Kramer1e6b6062012-04-14 12:14:03 +00001887 = TemplateIdAnnotation::Allocate(TemplateArgs.size(), TemplateIds);
Douglas Gregor7861a802009-11-03 01:35:08 +00001888
Richard Smith72bfbd82013-12-04 00:28:23 +00001889 // FIXME: Store name for literal operator too.
Douglas Gregor7861a802009-11-03 01:35:08 +00001890 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1891 TemplateId->Name = Id.Identifier;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001892 TemplateId->Operator = OO_None;
Douglas Gregor7861a802009-11-03 01:35:08 +00001893 TemplateId->TemplateNameLoc = Id.StartLocation;
1894 } else {
Douglas Gregor3cf81312009-11-03 23:16:33 +00001895 TemplateId->Name = 0;
1896 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1897 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor7861a802009-11-03 01:35:08 +00001898 }
1899
Douglas Gregore7c20652011-03-02 00:47:37 +00001900 TemplateId->SS = SS;
Benjamin Kramer807c2db2012-02-19 23:37:39 +00001901 TemplateId->TemplateKWLoc = TemplateKWLoc;
John McCall3e56fd42010-08-23 07:28:44 +00001902 TemplateId->Template = Template;
Douglas Gregor7861a802009-11-03 01:35:08 +00001903 TemplateId->Kind = TNK;
1904 TemplateId->LAngleLoc = LAngleLoc;
1905 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001906 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor7861a802009-11-03 01:35:08 +00001907 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001908 Arg != ArgEnd; ++Arg)
Douglas Gregor7861a802009-11-03 01:35:08 +00001909 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor7861a802009-11-03 01:35:08 +00001910
1911 Id.setTemplateId(TemplateId);
1912 return false;
1913 }
1914
1915 // Bundle the template arguments together.
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001916 ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs);
Abramo Bagnara4244b432012-01-27 08:46:19 +00001917
Douglas Gregor7861a802009-11-03 01:35:08 +00001918 // Constructor and destructor names.
John McCallfaf5fb42010-08-26 23:41:50 +00001919 TypeResult Type
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001920 = Actions.ActOnTemplateIdType(SS, TemplateKWLoc,
1921 Template, NameLoc,
Abramo Bagnara4244b432012-01-27 08:46:19 +00001922 LAngleLoc, TemplateArgsPtr, RAngleLoc,
1923 /*IsCtorOrDtorName=*/true);
Douglas Gregor7861a802009-11-03 01:35:08 +00001924 if (Type.isInvalid())
1925 return true;
1926
1927 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1928 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1929 else
1930 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1931
1932 return false;
1933}
1934
Douglas Gregor71395fa2009-11-04 00:56:37 +00001935/// \brief Parse an operator-function-id or conversion-function-id as part
1936/// of a C++ unqualified-id.
1937///
1938/// This routine is responsible only for parsing the operator-function-id or
1939/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor7861a802009-11-03 01:35:08 +00001940///
1941/// \code
Douglas Gregor7861a802009-11-03 01:35:08 +00001942/// operator-function-id: [C++ 13.5]
1943/// 'operator' operator
1944///
Douglas Gregor71395fa2009-11-04 00:56:37 +00001945/// operator: one of
Douglas Gregor7861a802009-11-03 01:35:08 +00001946/// new delete new[] delete[]
1947/// + - * / % ^ & | ~
1948/// ! = < > += -= *= /= %=
1949/// ^= &= |= << >> >>= <<= == !=
1950/// <= >= && || ++ -- , ->* ->
1951/// () []
1952///
1953/// conversion-function-id: [C++ 12.3.2]
1954/// operator conversion-type-id
1955///
1956/// conversion-type-id:
1957/// type-specifier-seq conversion-declarator[opt]
1958///
1959/// conversion-declarator:
1960/// ptr-operator conversion-declarator[opt]
1961/// \endcode
1962///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00001963/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregor7861a802009-11-03 01:35:08 +00001964/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1965///
1966/// \param EnteringContext whether we are entering the scope of the
1967/// nested-name-specifier.
1968///
Douglas Gregor71395fa2009-11-04 00:56:37 +00001969/// \param ObjectType if this unqualified-id occurs within a member access
1970/// expression, the type of the base object whose member is being accessed.
1971///
1972/// \param Result on a successful parse, contains the parsed unqualified-id.
1973///
1974/// \returns true if parsing fails, false otherwise.
1975bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallba7bf592010-08-24 05:47:05 +00001976 ParsedType ObjectType,
Douglas Gregor71395fa2009-11-04 00:56:37 +00001977 UnqualifiedId &Result) {
1978 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1979
1980 // Consume the 'operator' keyword.
1981 SourceLocation KeywordLoc = ConsumeToken();
1982
1983 // Determine what kind of operator name we have.
1984 unsigned SymbolIdx = 0;
1985 SourceLocation SymbolLocations[3];
1986 OverloadedOperatorKind Op = OO_None;
1987 switch (Tok.getKind()) {
1988 case tok::kw_new:
1989 case tok::kw_delete: {
1990 bool isNew = Tok.getKind() == tok::kw_new;
1991 // Consume the 'new' or 'delete'.
1992 SymbolLocations[SymbolIdx++] = ConsumeToken();
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001993 // Check for array new/delete.
1994 if (Tok.is(tok::l_square) &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001995 (!getLangOpts().CPlusPlus11 || NextToken().isNot(tok::l_square))) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001996 // Consume the '[' and ']'.
1997 BalancedDelimiterTracker T(*this, tok::l_square);
1998 T.consumeOpen();
1999 T.consumeClose();
2000 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00002001 return true;
2002
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002003 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
2004 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00002005 Op = isNew? OO_Array_New : OO_Array_Delete;
2006 } else {
2007 Op = isNew? OO_New : OO_Delete;
2008 }
2009 break;
2010 }
2011
2012#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
2013 case tok::Token: \
2014 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
2015 Op = OO_##Name; \
2016 break;
2017#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
2018#include "clang/Basic/OperatorKinds.def"
2019
2020 case tok::l_paren: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002021 // Consume the '(' and ')'.
2022 BalancedDelimiterTracker T(*this, tok::l_paren);
2023 T.consumeOpen();
2024 T.consumeClose();
2025 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00002026 return true;
2027
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002028 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
2029 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00002030 Op = OO_Call;
2031 break;
2032 }
2033
2034 case tok::l_square: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002035 // Consume the '[' and ']'.
2036 BalancedDelimiterTracker T(*this, tok::l_square);
2037 T.consumeOpen();
2038 T.consumeClose();
2039 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00002040 return true;
2041
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002042 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
2043 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00002044 Op = OO_Subscript;
2045 break;
2046 }
2047
2048 case tok::code_completion: {
2049 // Code completion for the operator name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002050 Actions.CodeCompleteOperatorName(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002051 cutOffParsing();
Douglas Gregor71395fa2009-11-04 00:56:37 +00002052 // Don't try to parse any further.
2053 return true;
2054 }
2055
2056 default:
2057 break;
2058 }
2059
2060 if (Op != OO_None) {
2061 // We have parsed an operator-function-id.
2062 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
2063 return false;
2064 }
Alexis Hunt34458502009-11-28 04:44:28 +00002065
2066 // Parse a literal-operator-id.
2067 //
Richard Smith6f212062012-10-20 08:41:10 +00002068 // literal-operator-id: C++11 [over.literal]
2069 // operator string-literal identifier
2070 // operator user-defined-string-literal
Alexis Hunt34458502009-11-28 04:44:28 +00002071
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002072 if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002073 Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator);
Alexis Hunt34458502009-11-28 04:44:28 +00002074
Richard Smith7d182a72012-03-08 23:06:02 +00002075 SourceLocation DiagLoc;
2076 unsigned DiagId = 0;
2077
2078 // We're past translation phase 6, so perform string literal concatenation
2079 // before checking for "".
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002080 SmallVector<Token, 4> Toks;
2081 SmallVector<SourceLocation, 4> TokLocs;
Richard Smith7d182a72012-03-08 23:06:02 +00002082 while (isTokenStringLiteral()) {
2083 if (!Tok.is(tok::string_literal) && !DiagId) {
Richard Smith6f212062012-10-20 08:41:10 +00002084 // C++11 [over.literal]p1:
2085 // The string-literal or user-defined-string-literal in a
2086 // literal-operator-id shall have no encoding-prefix [...].
Richard Smith7d182a72012-03-08 23:06:02 +00002087 DiagLoc = Tok.getLocation();
2088 DiagId = diag::err_literal_operator_string_prefix;
2089 }
2090 Toks.push_back(Tok);
2091 TokLocs.push_back(ConsumeStringToken());
2092 }
2093
2094 StringLiteralParser Literal(Toks.data(), Toks.size(), PP);
2095 if (Literal.hadError)
2096 return true;
2097
2098 // Grab the literal operator's suffix, which will be either the next token
2099 // or a ud-suffix from the string literal.
2100 IdentifierInfo *II = 0;
2101 SourceLocation SuffixLoc;
2102 if (!Literal.getUDSuffix().empty()) {
2103 II = &PP.getIdentifierTable().get(Literal.getUDSuffix());
2104 SuffixLoc =
2105 Lexer::AdvanceToTokenCharacter(TokLocs[Literal.getUDSuffixToken()],
2106 Literal.getUDSuffixOffset(),
David Blaikiebbafb8a2012-03-11 07:00:24 +00002107 PP.getSourceManager(), getLangOpts());
Richard Smith7d182a72012-03-08 23:06:02 +00002108 } else if (Tok.is(tok::identifier)) {
2109 II = Tok.getIdentifierInfo();
2110 SuffixLoc = ConsumeToken();
2111 TokLocs.push_back(SuffixLoc);
2112 } else {
Alexis Hunt34458502009-11-28 04:44:28 +00002113 Diag(Tok.getLocation(), diag::err_expected_ident);
2114 return true;
2115 }
2116
Richard Smith7d182a72012-03-08 23:06:02 +00002117 // The string literal must be empty.
2118 if (!Literal.GetString().empty() || Literal.Pascal) {
Richard Smith6f212062012-10-20 08:41:10 +00002119 // C++11 [over.literal]p1:
2120 // The string-literal or user-defined-string-literal in a
2121 // literal-operator-id shall [...] contain no characters
2122 // other than the implicit terminating '\0'.
Richard Smith7d182a72012-03-08 23:06:02 +00002123 DiagLoc = TokLocs.front();
2124 DiagId = diag::err_literal_operator_string_not_empty;
2125 }
2126
2127 if (DiagId) {
2128 // This isn't a valid literal-operator-id, but we think we know
2129 // what the user meant. Tell them what they should have written.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002130 SmallString<32> Str;
Richard Smith7d182a72012-03-08 23:06:02 +00002131 Str += "\"\" ";
2132 Str += II->getName();
2133 Diag(DiagLoc, DiagId) << FixItHint::CreateReplacement(
2134 SourceRange(TokLocs.front(), TokLocs.back()), Str);
2135 }
2136
2137 Result.setLiteralOperatorId(II, KeywordLoc, SuffixLoc);
Richard Smithd091dc12013-12-05 00:58:33 +00002138
2139 return Actions.checkLiteralOperatorId(SS, Result);
Alexis Hunt34458502009-11-28 04:44:28 +00002140 }
Richard Smithd091dc12013-12-05 00:58:33 +00002141
Douglas Gregor71395fa2009-11-04 00:56:37 +00002142 // Parse a conversion-function-id.
2143 //
2144 // conversion-function-id: [C++ 12.3.2]
2145 // operator conversion-type-id
2146 //
2147 // conversion-type-id:
2148 // type-specifier-seq conversion-declarator[opt]
2149 //
2150 // conversion-declarator:
2151 // ptr-operator conversion-declarator[opt]
2152
2153 // Parse the type-specifier-seq.
John McCall084e83d2011-03-24 11:26:52 +00002154 DeclSpec DS(AttrFactory);
Douglas Gregora25d65d2009-11-20 22:03:38 +00002155 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregor71395fa2009-11-04 00:56:37 +00002156 return true;
2157
2158 // Parse the conversion-declarator, which is merely a sequence of
2159 // ptr-operators.
Richard Smith01518fa2013-05-04 01:26:46 +00002160 Declarator D(DS, Declarator::ConversionIdContext);
Douglas Gregor71395fa2009-11-04 00:56:37 +00002161 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
2162
2163 // Finish up the type.
John McCallfaf5fb42010-08-26 23:41:50 +00002164 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregor71395fa2009-11-04 00:56:37 +00002165 if (Ty.isInvalid())
2166 return true;
2167
2168 // Note that this is a conversion-function-id.
2169 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
2170 D.getSourceRange().getEnd());
2171 return false;
2172}
2173
2174/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
2175/// name of an entity.
2176///
2177/// \code
2178/// unqualified-id: [C++ expr.prim.general]
2179/// identifier
2180/// operator-function-id
2181/// conversion-function-id
2182/// [C++0x] literal-operator-id [TODO]
2183/// ~ class-name
2184/// template-id
2185///
2186/// \endcode
2187///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00002188/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregor71395fa2009-11-04 00:56:37 +00002189/// non-empty, then we are parsing the unqualified-id of a qualified-id.
2190///
2191/// \param EnteringContext whether we are entering the scope of the
2192/// nested-name-specifier.
2193///
Douglas Gregor7861a802009-11-03 01:35:08 +00002194/// \param AllowDestructorName whether we allow parsing of a destructor name.
2195///
2196/// \param AllowConstructorName whether we allow parsing a constructor name.
2197///
Douglas Gregor127ea592009-11-03 21:24:04 +00002198/// \param ObjectType if this unqualified-id occurs within a member access
2199/// expression, the type of the base object whose member is being accessed.
2200///
Douglas Gregor7861a802009-11-03 01:35:08 +00002201/// \param Result on a successful parse, contains the parsed unqualified-id.
2202///
2203/// \returns true if parsing fails, false otherwise.
2204bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
2205 bool AllowDestructorName,
2206 bool AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00002207 ParsedType ObjectType,
Abramo Bagnara7945c982012-01-27 09:46:47 +00002208 SourceLocation& TemplateKWLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00002209 UnqualifiedId &Result) {
Douglas Gregorb22ee882010-05-05 05:58:24 +00002210
2211 // Handle 'A::template B'. This is for template-ids which have not
2212 // already been annotated by ParseOptionalCXXScopeSpecifier().
2213 bool TemplateSpecified = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002214 if (getLangOpts().CPlusPlus && Tok.is(tok::kw_template) &&
Douglas Gregorb22ee882010-05-05 05:58:24 +00002215 (ObjectType || SS.isSet())) {
2216 TemplateSpecified = true;
2217 TemplateKWLoc = ConsumeToken();
2218 }
2219
Douglas Gregor7861a802009-11-03 01:35:08 +00002220 // unqualified-id:
2221 // identifier
2222 // template-id (when it hasn't already been annotated)
2223 if (Tok.is(tok::identifier)) {
2224 // Consume the identifier.
2225 IdentifierInfo *Id = Tok.getIdentifierInfo();
2226 SourceLocation IdLoc = ConsumeToken();
2227
David Blaikiebbafb8a2012-03-11 07:00:24 +00002228 if (!getLangOpts().CPlusPlus) {
Douglas Gregor411e5ac2010-01-11 23:29:10 +00002229 // If we're not in C++, only identifiers matter. Record the
2230 // identifier and return.
2231 Result.setIdentifier(Id, IdLoc);
2232 return false;
2233 }
2234
Douglas Gregor7861a802009-11-03 01:35:08 +00002235 if (AllowConstructorName &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002236 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor7861a802009-11-03 01:35:08 +00002237 // We have parsed a constructor name.
Abramo Bagnara4244b432012-01-27 08:46:19 +00002238 ParsedType Ty = Actions.getTypeName(*Id, IdLoc, getCurScope(),
2239 &SS, false, false,
2240 ParsedType(),
2241 /*IsCtorOrDtorName=*/true,
2242 /*NonTrivialTypeSourceInfo=*/true);
2243 Result.setConstructorName(Ty, IdLoc, IdLoc);
Douglas Gregor7861a802009-11-03 01:35:08 +00002244 } else {
2245 // We have parsed an identifier.
2246 Result.setIdentifier(Id, IdLoc);
2247 }
2248
2249 // If the next token is a '<', we may have a template.
Douglas Gregorb22ee882010-05-05 05:58:24 +00002250 if (TemplateSpecified || Tok.is(tok::less))
Abramo Bagnara7945c982012-01-27 09:46:47 +00002251 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc, Id, IdLoc,
2252 EnteringContext, ObjectType,
2253 Result, TemplateSpecified);
Douglas Gregor7861a802009-11-03 01:35:08 +00002254
2255 return false;
2256 }
2257
2258 // unqualified-id:
2259 // template-id (already parsed and annotated)
2260 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002261 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002262
2263 // If the template-name names the current class, then this is a constructor
2264 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002265 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002266 if (SS.isSet()) {
2267 // C++ [class.qual]p2 specifies that a qualified template-name
2268 // is taken as the constructor name where a constructor can be
2269 // declared. Thus, the template arguments are extraneous, so
2270 // complain about them and remove them entirely.
2271 Diag(TemplateId->TemplateNameLoc,
2272 diag::err_out_of_line_constructor_template_id)
2273 << TemplateId->Name
Douglas Gregora771f462010-03-31 17:46:05 +00002274 << FixItHint::CreateRemoval(
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002275 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
Abramo Bagnara4244b432012-01-27 08:46:19 +00002276 ParsedType Ty = Actions.getTypeName(*TemplateId->Name,
2277 TemplateId->TemplateNameLoc,
2278 getCurScope(),
2279 &SS, false, false,
2280 ParsedType(),
2281 /*IsCtorOrDtorName=*/true,
2282 /*NontrivialTypeSourceInfo=*/true);
2283 Result.setConstructorName(Ty, TemplateId->TemplateNameLoc,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002284 TemplateId->RAngleLoc);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002285 ConsumeToken();
2286 return false;
2287 }
2288
2289 Result.setConstructorTemplateId(TemplateId);
2290 ConsumeToken();
2291 return false;
2292 }
2293
Douglas Gregor7861a802009-11-03 01:35:08 +00002294 // We have already parsed a template-id; consume the annotation token as
2295 // our unqualified-id.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002296 Result.setTemplateId(TemplateId);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002297 TemplateKWLoc = TemplateId->TemplateKWLoc;
Douglas Gregor7861a802009-11-03 01:35:08 +00002298 ConsumeToken();
2299 return false;
2300 }
2301
2302 // unqualified-id:
2303 // operator-function-id
2304 // conversion-function-id
2305 if (Tok.is(tok::kw_operator)) {
Douglas Gregor71395fa2009-11-04 00:56:37 +00002306 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor7861a802009-11-03 01:35:08 +00002307 return true;
2308
Alexis Hunted0530f2009-11-28 08:58:14 +00002309 // If we have an operator-function-id or a literal-operator-id and the next
2310 // token is a '<', we may have a
Douglas Gregor71395fa2009-11-04 00:56:37 +00002311 //
2312 // template-id:
2313 // operator-function-id < template-argument-list[opt] >
Alexis Hunted0530f2009-11-28 08:58:14 +00002314 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
2315 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregorb22ee882010-05-05 05:58:24 +00002316 (TemplateSpecified || Tok.is(tok::less)))
Abramo Bagnara7945c982012-01-27 09:46:47 +00002317 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2318 0, SourceLocation(),
2319 EnteringContext, ObjectType,
2320 Result, TemplateSpecified);
Douglas Gregor7861a802009-11-03 01:35:08 +00002321
Douglas Gregor7861a802009-11-03 01:35:08 +00002322 return false;
2323 }
2324
David Blaikiebbafb8a2012-03-11 07:00:24 +00002325 if (getLangOpts().CPlusPlus &&
Douglas Gregor411e5ac2010-01-11 23:29:10 +00002326 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor7861a802009-11-03 01:35:08 +00002327 // C++ [expr.unary.op]p10:
2328 // There is an ambiguity in the unary-expression ~X(), where X is a
2329 // class-name. The ambiguity is resolved in favor of treating ~ as a
2330 // unary complement rather than treating ~X as referring to a destructor.
2331
2332 // Parse the '~'.
2333 SourceLocation TildeLoc = ConsumeToken();
David Blaikieecd8a942011-12-08 16:13:53 +00002334
2335 if (SS.isEmpty() && Tok.is(tok::kw_decltype)) {
2336 DeclSpec DS(AttrFactory);
2337 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
2338 if (ParsedType Type = Actions.getDestructorType(DS, ObjectType)) {
2339 Result.setDestructorName(TildeLoc, Type, EndLoc);
2340 return false;
2341 }
2342 return true;
2343 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002344
2345 // Parse the class-name.
2346 if (Tok.isNot(tok::identifier)) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00002347 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor7861a802009-11-03 01:35:08 +00002348 return true;
2349 }
2350
2351 // Parse the class-name (or template-name in a simple-template-id).
2352 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
2353 SourceLocation ClassNameLoc = ConsumeToken();
2354
Douglas Gregorb22ee882010-05-05 05:58:24 +00002355 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallba7bf592010-08-24 05:47:05 +00002356 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002357 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2358 ClassName, ClassNameLoc,
2359 EnteringContext, ObjectType,
2360 Result, TemplateSpecified);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002361 }
2362
Douglas Gregor7861a802009-11-03 01:35:08 +00002363 // Note that this is a destructor name.
John McCallba7bf592010-08-24 05:47:05 +00002364 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
2365 ClassNameLoc, getCurScope(),
2366 SS, ObjectType,
2367 EnteringContext);
Douglas Gregorfe17d252010-02-16 19:09:40 +00002368 if (!Ty)
Douglas Gregor7861a802009-11-03 01:35:08 +00002369 return true;
Douglas Gregorfe17d252010-02-16 19:09:40 +00002370
Douglas Gregor7861a802009-11-03 01:35:08 +00002371 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor7861a802009-11-03 01:35:08 +00002372 return false;
2373 }
2374
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002375 Diag(Tok, diag::err_expected_unqualified_id)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002376 << getLangOpts().CPlusPlus;
Douglas Gregor7861a802009-11-03 01:35:08 +00002377 return true;
2378}
2379
Sebastian Redlbd150f42008-11-21 19:14:01 +00002380/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
2381/// memory in a typesafe manner and call constructors.
Mike Stump11289f42009-09-09 15:08:12 +00002382///
Chris Lattner109faf22009-01-04 21:25:24 +00002383/// This method is called to parse the new expression after the optional :: has
2384/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
2385/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002386///
2387/// new-expression:
2388/// '::'[opt] 'new' new-placement[opt] new-type-id
2389/// new-initializer[opt]
2390/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2391/// new-initializer[opt]
2392///
2393/// new-placement:
2394/// '(' expression-list ')'
2395///
Sebastian Redl351bb782008-12-02 14:43:59 +00002396/// new-type-id:
2397/// type-specifier-seq new-declarator[opt]
Douglas Gregora3a020a2011-04-15 19:40:02 +00002398/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redl351bb782008-12-02 14:43:59 +00002399///
2400/// new-declarator:
2401/// ptr-operator new-declarator[opt]
2402/// direct-new-declarator
2403///
Sebastian Redlbd150f42008-11-21 19:14:01 +00002404/// new-initializer:
2405/// '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00002406/// [C++0x] braced-init-list
Sebastian Redlbd150f42008-11-21 19:14:01 +00002407///
John McCalldadc5752010-08-24 06:29:42 +00002408ExprResult
Chris Lattner109faf22009-01-04 21:25:24 +00002409Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
2410 assert(Tok.is(tok::kw_new) && "expected 'new' token");
2411 ConsumeToken(); // Consume 'new'
Sebastian Redlbd150f42008-11-21 19:14:01 +00002412
2413 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
2414 // second form of new-expression. It can't be a new-type-id.
2415
Benjamin Kramerf0623432012-08-23 22:51:59 +00002416 ExprVector PlacementArgs;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002417 SourceLocation PlacementLParen, PlacementRParen;
2418
Douglas Gregorf2753b32010-07-13 15:54:32 +00002419 SourceRange TypeIdParens;
John McCall084e83d2011-03-24 11:26:52 +00002420 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis3ff13572011-06-28 03:01:23 +00002421 Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002422 if (Tok.is(tok::l_paren)) {
2423 // If it turns out to be a placement, we change the type location.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002424 BalancedDelimiterTracker T(*this, tok::l_paren);
2425 T.consumeOpen();
2426 PlacementLParen = T.getOpenLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002427 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002428 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redld65cea82008-12-11 22:51:44 +00002429 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002430 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002431
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002432 T.consumeClose();
2433 PlacementRParen = T.getCloseLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002434 if (PlacementRParen.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002435 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redld65cea82008-12-11 22:51:44 +00002436 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002437 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002438
Sebastian Redl351bb782008-12-02 14:43:59 +00002439 if (PlacementArgs.empty()) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002440 // Reset the placement locations. There was no placement.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002441 TypeIdParens = T.getRange();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002442 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002443 } else {
2444 // We still need the type.
2445 if (Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002446 BalancedDelimiterTracker T(*this, tok::l_paren);
2447 T.consumeOpen();
Douglas Gregora3a020a2011-04-15 19:40:02 +00002448 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002449 ParseSpecifierQualifierList(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002450 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002451 ParseDeclarator(DeclaratorInfo);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002452 T.consumeClose();
2453 TypeIdParens = T.getRange();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002454 } else {
Douglas Gregora3a020a2011-04-15 19:40:02 +00002455 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002456 if (ParseCXXTypeSpecifierSeq(DS))
2457 DeclaratorInfo.setInvalidType(true);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002458 else {
2459 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002460 ParseDeclaratorInternal(DeclaratorInfo,
2461 &Parser::ParseDirectNewDeclarator);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002462 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002463 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002464 }
2465 } else {
Sebastian Redl351bb782008-12-02 14:43:59 +00002466 // A new-type-id is a simplified type-id, where essentially the
2467 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregora3a020a2011-04-15 19:40:02 +00002468 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002469 if (ParseCXXTypeSpecifierSeq(DS))
2470 DeclaratorInfo.setInvalidType(true);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002471 else {
2472 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002473 ParseDeclaratorInternal(DeclaratorInfo,
2474 &Parser::ParseDirectNewDeclarator);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002475 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002476 }
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00002477 if (DeclaratorInfo.isInvalidType()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002478 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redld65cea82008-12-11 22:51:44 +00002479 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002480 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002481
Sebastian Redl6047f072012-02-16 12:22:20 +00002482 ExprResult Initializer;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002483
2484 if (Tok.is(tok::l_paren)) {
Sebastian Redl6047f072012-02-16 12:22:20 +00002485 SourceLocation ConstructorLParen, ConstructorRParen;
Benjamin Kramerf0623432012-08-23 22:51:59 +00002486 ExprVector ConstructorArgs;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002487 BalancedDelimiterTracker T(*this, tok::l_paren);
2488 T.consumeOpen();
2489 ConstructorLParen = T.getOpenLocation();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002490 if (Tok.isNot(tok::r_paren)) {
2491 CommaLocsTy CommaLocs;
Sebastian Redl351bb782008-12-02 14:43:59 +00002492 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002493 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redld65cea82008-12-11 22:51:44 +00002494 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002495 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002496 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002497 T.consumeClose();
2498 ConstructorRParen = T.getCloseLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002499 if (ConstructorRParen.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002500 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redld65cea82008-12-11 22:51:44 +00002501 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002502 }
Sebastian Redl6047f072012-02-16 12:22:20 +00002503 Initializer = Actions.ActOnParenListExpr(ConstructorLParen,
2504 ConstructorRParen,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002505 ConstructorArgs);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002506 } else if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus11) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002507 Diag(Tok.getLocation(),
2508 diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl6047f072012-02-16 12:22:20 +00002509 Initializer = ParseBraceInitializer();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002510 }
Sebastian Redl6047f072012-02-16 12:22:20 +00002511 if (Initializer.isInvalid())
2512 return Initializer;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002513
Sebastian Redl6d4256c2009-03-15 17:47:39 +00002514 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002515 PlacementArgs, PlacementRParen,
Sebastian Redl6047f072012-02-16 12:22:20 +00002516 TypeIdParens, DeclaratorInfo, Initializer.take());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002517}
2518
Sebastian Redlbd150f42008-11-21 19:14:01 +00002519/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
2520/// passed to ParseDeclaratorInternal.
2521///
2522/// direct-new-declarator:
2523/// '[' expression ']'
2524/// direct-new-declarator '[' constant-expression ']'
2525///
Chris Lattner109faf22009-01-04 21:25:24 +00002526void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002527 // Parse the array dimensions.
2528 bool first = true;
2529 while (Tok.is(tok::l_square)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002530 // An array-size expression can't start with a lambda.
2531 if (CheckProhibitedCXX11Attribute())
2532 continue;
2533
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002534 BalancedDelimiterTracker T(*this, tok::l_square);
2535 T.consumeOpen();
2536
John McCalldadc5752010-08-24 06:29:42 +00002537 ExprResult Size(first ? ParseExpression()
Sebastian Redl59b5e512008-12-11 21:36:32 +00002538 : ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002539 if (Size.isInvalid()) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002540 // Recover
Alexey Bataevee6507d2013-11-18 08:17:37 +00002541 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002542 return;
2543 }
2544 first = false;
2545
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002546 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00002547
Bill Wendling44426052012-12-20 19:22:21 +00002548 // Attributes here appertain to the array type. C++11 [expr.new]p5.
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002549 ParsedAttributes Attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002550 MaybeParseCXX11Attributes(Attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002551
John McCall084e83d2011-03-24 11:26:52 +00002552 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall53fa7142010-12-24 02:08:15 +00002553 /*static=*/false, /*star=*/false,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002554 Size.release(),
2555 T.getOpenLocation(),
2556 T.getCloseLocation()),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002557 Attrs, T.getCloseLocation());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002558
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002559 if (T.getCloseLocation().isInvalid())
Sebastian Redlbd150f42008-11-21 19:14:01 +00002560 return;
2561 }
2562}
2563
2564/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
2565/// This ambiguity appears in the syntax of the C++ new operator.
2566///
2567/// new-expression:
2568/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2569/// new-initializer[opt]
2570///
2571/// new-placement:
2572/// '(' expression-list ')'
2573///
John McCall37ad5512010-08-23 06:44:23 +00002574bool Parser::ParseExpressionListOrTypeId(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002575 SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner109faf22009-01-04 21:25:24 +00002576 Declarator &D) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002577 // The '(' was already consumed.
2578 if (isTypeIdInParens()) {
Sebastian Redl351bb782008-12-02 14:43:59 +00002579 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002580 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002581 ParseDeclarator(D);
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00002582 return D.isInvalidType();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002583 }
2584
2585 // It's not a type, it has to be an expression list.
2586 // Discard the comma locations - ActOnCXXNew has enough parameters.
2587 CommaLocsTy CommaLocs;
2588 return ParseExpressionList(PlacementArgs, CommaLocs);
2589}
2590
2591/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
2592/// to free memory allocated by new.
2593///
Chris Lattner109faf22009-01-04 21:25:24 +00002594/// This method is called to parse the 'delete' expression after the optional
2595/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
2596/// and "Start" is its location. Otherwise, "Start" is the location of the
2597/// 'delete' token.
2598///
Sebastian Redlbd150f42008-11-21 19:14:01 +00002599/// delete-expression:
2600/// '::'[opt] 'delete' cast-expression
2601/// '::'[opt] 'delete' '[' ']' cast-expression
John McCalldadc5752010-08-24 06:29:42 +00002602ExprResult
Chris Lattner109faf22009-01-04 21:25:24 +00002603Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
2604 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
2605 ConsumeToken(); // Consume 'delete'
Sebastian Redlbd150f42008-11-21 19:14:01 +00002606
2607 // Array delete?
2608 bool ArrayDelete = false;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002609 if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
Richard Smith10c60722012-08-09 19:01:51 +00002610 // C++11 [expr.delete]p1:
2611 // Whenever the delete keyword is followed by empty square brackets, it
2612 // shall be interpreted as [array delete].
2613 // [Footnote: A lambda expression with a lambda-introducer that consists
2614 // of empty square brackets can follow the delete keyword if
2615 // the lambda expression is enclosed in parentheses.]
2616 // FIXME: Produce a better diagnostic if the '[]' is unambiguously a
2617 // lambda-introducer.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002618 ArrayDelete = true;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002619 BalancedDelimiterTracker T(*this, tok::l_square);
2620
2621 T.consumeOpen();
2622 T.consumeClose();
2623 if (T.getCloseLocation().isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +00002624 return ExprError();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002625 }
2626
John McCalldadc5752010-08-24 06:29:42 +00002627 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002628 if (Operand.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002629 return Operand;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002630
John McCallb268a282010-08-23 23:25:46 +00002631 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002632}
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002633
Mike Stump11289f42009-09-09 15:08:12 +00002634static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002635 switch(kind) {
David Blaikie83d382b2011-09-23 05:06:16 +00002636 default: llvm_unreachable("Not a known unary type trait.");
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002637 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
Joao Matosc9523d42013-03-27 01:34:16 +00002638 case tok::kw___has_nothrow_move_assign: return UTT_HasNothrowMoveAssign;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002639 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
John Wiegley65497cc2011-04-27 23:09:49 +00002640 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002641 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
Joao Matosc9523d42013-03-27 01:34:16 +00002642 case tok::kw___has_trivial_move_assign: return UTT_HasTrivialMoveAssign;
Alexis Huntf479f1b2011-05-09 18:22:59 +00002643 case tok::kw___has_trivial_constructor:
2644 return UTT_HasTrivialDefaultConstructor;
Joao Matosc9523d42013-03-27 01:34:16 +00002645 case tok::kw___has_trivial_move_constructor:
2646 return UTT_HasTrivialMoveConstructor;
John Wiegley65497cc2011-04-27 23:09:49 +00002647 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002648 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
2649 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
2650 case tok::kw___is_abstract: return UTT_IsAbstract;
John Wiegley65497cc2011-04-27 23:09:49 +00002651 case tok::kw___is_arithmetic: return UTT_IsArithmetic;
2652 case tok::kw___is_array: return UTT_IsArray;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002653 case tok::kw___is_class: return UTT_IsClass;
John Wiegley65497cc2011-04-27 23:09:49 +00002654 case tok::kw___is_complete_type: return UTT_IsCompleteType;
2655 case tok::kw___is_compound: return UTT_IsCompound;
2656 case tok::kw___is_const: return UTT_IsConst;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002657 case tok::kw___is_empty: return UTT_IsEmpty;
2658 case tok::kw___is_enum: return UTT_IsEnum;
Douglas Gregordca70af2011-12-03 18:14:24 +00002659 case tok::kw___is_final: return UTT_IsFinal;
John Wiegley65497cc2011-04-27 23:09:49 +00002660 case tok::kw___is_floating_point: return UTT_IsFloatingPoint;
2661 case tok::kw___is_function: return UTT_IsFunction;
2662 case tok::kw___is_fundamental: return UTT_IsFundamental;
2663 case tok::kw___is_integral: return UTT_IsIntegral;
John McCallbf4a7d72012-09-25 07:32:49 +00002664 case tok::kw___is_interface_class: return UTT_IsInterfaceClass;
John Wiegley65497cc2011-04-27 23:09:49 +00002665 case tok::kw___is_lvalue_reference: return UTT_IsLvalueReference;
2666 case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
2667 case tok::kw___is_member_object_pointer: return UTT_IsMemberObjectPointer;
2668 case tok::kw___is_member_pointer: return UTT_IsMemberPointer;
2669 case tok::kw___is_object: return UTT_IsObject;
Chandler Carruth79803482011-04-23 10:47:20 +00002670 case tok::kw___is_literal: return UTT_IsLiteral;
Chandler Carruth65fa1fd2011-04-24 02:49:28 +00002671 case tok::kw___is_literal_type: return UTT_IsLiteral;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002672 case tok::kw___is_pod: return UTT_IsPOD;
John Wiegley65497cc2011-04-27 23:09:49 +00002673 case tok::kw___is_pointer: return UTT_IsPointer;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002674 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
John Wiegley65497cc2011-04-27 23:09:49 +00002675 case tok::kw___is_reference: return UTT_IsReference;
John Wiegley65497cc2011-04-27 23:09:49 +00002676 case tok::kw___is_rvalue_reference: return UTT_IsRvalueReference;
2677 case tok::kw___is_scalar: return UTT_IsScalar;
David Majnemera5433082013-10-18 00:33:31 +00002678 case tok::kw___is_sealed: return UTT_IsSealed;
John Wiegley65497cc2011-04-27 23:09:49 +00002679 case tok::kw___is_signed: return UTT_IsSigned;
2680 case tok::kw___is_standard_layout: return UTT_IsStandardLayout;
2681 case tok::kw___is_trivial: return UTT_IsTrivial;
Alexis Huntd9a5cc12011-05-13 00:31:07 +00002682 case tok::kw___is_trivially_copyable: return UTT_IsTriviallyCopyable;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002683 case tok::kw___is_union: return UTT_IsUnion;
John Wiegley65497cc2011-04-27 23:09:49 +00002684 case tok::kw___is_unsigned: return UTT_IsUnsigned;
2685 case tok::kw___is_void: return UTT_IsVoid;
2686 case tok::kw___is_volatile: return UTT_IsVolatile;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002687 }
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002688}
2689
2690static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
2691 switch(kind) {
Francois Pichet347c4c72010-12-07 00:55:57 +00002692 default: llvm_unreachable("Not a known binary type trait");
Francois Pichet34b21132010-12-08 22:35:30 +00002693 case tok::kw___is_base_of: return BTT_IsBaseOf;
John Wiegley65497cc2011-04-27 23:09:49 +00002694 case tok::kw___is_convertible: return BTT_IsConvertible;
2695 case tok::kw___is_same: return BTT_IsSame;
Francois Pichet34b21132010-12-08 22:35:30 +00002696 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor8006e762011-01-27 20:28:01 +00002697 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Douglas Gregor1be329d2012-02-23 07:33:15 +00002698 case tok::kw___is_trivially_assignable: return BTT_IsTriviallyAssignable;
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002699 }
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002700}
2701
Douglas Gregor29c42f22012-02-24 07:38:34 +00002702static TypeTrait TypeTraitFromTokKind(tok::TokenKind kind) {
2703 switch (kind) {
2704 default: llvm_unreachable("Not a known type trait");
2705 case tok::kw___is_trivially_constructible:
2706 return TT_IsTriviallyConstructible;
2707 }
2708}
2709
John Wiegley6242b6a2011-04-28 00:16:57 +00002710static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
2711 switch(kind) {
2712 default: llvm_unreachable("Not a known binary type trait");
2713 case tok::kw___array_rank: return ATT_ArrayRank;
2714 case tok::kw___array_extent: return ATT_ArrayExtent;
2715 }
2716}
2717
John Wiegleyf9f65842011-04-25 06:54:41 +00002718static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2719 switch(kind) {
David Blaikie83d382b2011-09-23 05:06:16 +00002720 default: llvm_unreachable("Not a known unary expression trait.");
John Wiegleyf9f65842011-04-25 06:54:41 +00002721 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
2722 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
2723 }
2724}
2725
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002726/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
2727/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2728/// templates.
2729///
2730/// primary-expression:
2731/// [GNU] unary-type-trait '(' type-id ')'
2732///
John McCalldadc5752010-08-24 06:29:42 +00002733ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002734 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
2735 SourceLocation Loc = ConsumeToken();
2736
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002737 BalancedDelimiterTracker T(*this, tok::l_paren);
2738 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002739 return ExprError();
2740
2741 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
2742 // there will be cryptic errors about mismatched parentheses and missing
2743 // specifiers.
Douglas Gregor220cac52009-02-18 17:45:20 +00002744 TypeResult Ty = ParseTypeName();
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002745
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002746 T.consumeClose();
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002747
Douglas Gregor220cac52009-02-18 17:45:20 +00002748 if (Ty.isInvalid())
2749 return ExprError();
2750
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002751 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), T.getCloseLocation());
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002752}
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002753
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002754/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
2755/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2756/// templates.
2757///
2758/// primary-expression:
2759/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
2760///
2761ExprResult Parser::ParseBinaryTypeTrait() {
2762 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
2763 SourceLocation Loc = ConsumeToken();
2764
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002765 BalancedDelimiterTracker T(*this, tok::l_paren);
2766 if (T.expectAndConsume(diag::err_expected_lparen))
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002767 return ExprError();
2768
2769 TypeResult LhsTy = ParseTypeName();
2770 if (LhsTy.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002771 SkipUntil(tok::r_paren, StopAtSemi);
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002772 return ExprError();
2773 }
2774
2775 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002776 SkipUntil(tok::r_paren, StopAtSemi);
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002777 return ExprError();
2778 }
2779
2780 TypeResult RhsTy = ParseTypeName();
2781 if (RhsTy.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002782 SkipUntil(tok::r_paren, StopAtSemi);
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002783 return ExprError();
2784 }
2785
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002786 T.consumeClose();
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002787
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002788 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(),
2789 T.getCloseLocation());
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002790}
2791
Douglas Gregor29c42f22012-02-24 07:38:34 +00002792/// \brief Parse the built-in type-trait pseudo-functions that allow
2793/// implementation of the TR1/C++11 type traits templates.
2794///
2795/// primary-expression:
2796/// type-trait '(' type-id-seq ')'
2797///
2798/// type-id-seq:
2799/// type-id ...[opt] type-id-seq[opt]
2800///
2801ExprResult Parser::ParseTypeTrait() {
2802 TypeTrait Kind = TypeTraitFromTokKind(Tok.getKind());
2803 SourceLocation Loc = ConsumeToken();
2804
2805 BalancedDelimiterTracker Parens(*this, tok::l_paren);
2806 if (Parens.expectAndConsume(diag::err_expected_lparen))
2807 return ExprError();
2808
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002809 SmallVector<ParsedType, 2> Args;
Douglas Gregor29c42f22012-02-24 07:38:34 +00002810 do {
2811 // Parse the next type.
2812 TypeResult Ty = ParseTypeName();
2813 if (Ty.isInvalid()) {
2814 Parens.skipToEnd();
2815 return ExprError();
2816 }
2817
2818 // Parse the ellipsis, if present.
2819 if (Tok.is(tok::ellipsis)) {
2820 Ty = Actions.ActOnPackExpansion(Ty.get(), ConsumeToken());
2821 if (Ty.isInvalid()) {
2822 Parens.skipToEnd();
2823 return ExprError();
2824 }
2825 }
2826
2827 // Add this type to the list of arguments.
2828 Args.push_back(Ty.get());
2829
2830 if (Tok.is(tok::comma)) {
2831 ConsumeToken();
2832 continue;
2833 }
2834
2835 break;
2836 } while (true);
2837
2838 if (Parens.consumeClose())
2839 return ExprError();
2840
2841 return Actions.ActOnTypeTrait(Kind, Loc, Args, Parens.getCloseLocation());
2842}
2843
John Wiegley6242b6a2011-04-28 00:16:57 +00002844/// ParseArrayTypeTrait - Parse the built-in array type-trait
2845/// pseudo-functions.
2846///
2847/// primary-expression:
2848/// [Embarcadero] '__array_rank' '(' type-id ')'
2849/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
2850///
2851ExprResult Parser::ParseArrayTypeTrait() {
2852 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
2853 SourceLocation Loc = ConsumeToken();
2854
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002855 BalancedDelimiterTracker T(*this, tok::l_paren);
2856 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley6242b6a2011-04-28 00:16:57 +00002857 return ExprError();
2858
2859 TypeResult Ty = ParseTypeName();
2860 if (Ty.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002861 SkipUntil(tok::comma, StopAtSemi);
2862 SkipUntil(tok::r_paren, StopAtSemi);
John Wiegley6242b6a2011-04-28 00:16:57 +00002863 return ExprError();
2864 }
2865
2866 switch (ATT) {
2867 case ATT_ArrayRank: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002868 T.consumeClose();
2869 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL,
2870 T.getCloseLocation());
John Wiegley6242b6a2011-04-28 00:16:57 +00002871 }
2872 case ATT_ArrayExtent: {
2873 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002874 SkipUntil(tok::r_paren, StopAtSemi);
John Wiegley6242b6a2011-04-28 00:16:57 +00002875 return ExprError();
2876 }
2877
2878 ExprResult DimExpr = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002879 T.consumeClose();
John Wiegley6242b6a2011-04-28 00:16:57 +00002880
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002881 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(),
2882 T.getCloseLocation());
John Wiegley6242b6a2011-04-28 00:16:57 +00002883 }
John Wiegley6242b6a2011-04-28 00:16:57 +00002884 }
David Blaikiee4d798f2012-01-20 21:50:17 +00002885 llvm_unreachable("Invalid ArrayTypeTrait!");
John Wiegley6242b6a2011-04-28 00:16:57 +00002886}
2887
John Wiegleyf9f65842011-04-25 06:54:41 +00002888/// ParseExpressionTrait - Parse built-in expression-trait
2889/// pseudo-functions like __is_lvalue_expr( xxx ).
2890///
2891/// primary-expression:
2892/// [Embarcadero] expression-trait '(' expression ')'
2893///
2894ExprResult Parser::ParseExpressionTrait() {
2895 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2896 SourceLocation Loc = ConsumeToken();
2897
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002898 BalancedDelimiterTracker T(*this, tok::l_paren);
2899 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegleyf9f65842011-04-25 06:54:41 +00002900 return ExprError();
2901
2902 ExprResult Expr = ParseExpression();
2903
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002904 T.consumeClose();
John Wiegleyf9f65842011-04-25 06:54:41 +00002905
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002906 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(),
2907 T.getCloseLocation());
John Wiegleyf9f65842011-04-25 06:54:41 +00002908}
2909
2910
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002911/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2912/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2913/// based on the context past the parens.
John McCalldadc5752010-08-24 06:29:42 +00002914ExprResult
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002915Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallba7bf592010-08-24 05:47:05 +00002916 ParsedType &CastTy,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002917 BalancedDelimiterTracker &Tracker) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002918 assert(getLangOpts().CPlusPlus && "Should only be called for C++!");
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002919 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2920 assert(isTypeIdInParens() && "Not a type-id!");
2921
John McCalldadc5752010-08-24 06:29:42 +00002922 ExprResult Result(true);
John McCallba7bf592010-08-24 05:47:05 +00002923 CastTy = ParsedType();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002924
2925 // We need to disambiguate a very ugly part of the C++ syntax:
2926 //
2927 // (T())x; - type-id
2928 // (T())*x; - type-id
2929 // (T())/x; - expression
2930 // (T()); - expression
2931 //
2932 // The bad news is that we cannot use the specialized tentative parser, since
2933 // it can only verify that the thing inside the parens can be parsed as
2934 // type-id, it is not useful for determining the context past the parens.
2935 //
2936 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidis24ad6922009-05-22 15:12:46 +00002937 // making any unnecessary Action calls.
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002938 //
2939 // It uses a scheme similar to parsing inline methods. The parenthesized
2940 // tokens are cached, the context that follows is determined (possibly by
2941 // parsing a cast-expression), and then we re-introduce the cached tokens
2942 // into the token stream and parse them appropriately.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002943
Mike Stump11289f42009-09-09 15:08:12 +00002944 ParenParseOption ParseAs;
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002945 CachedTokens Toks;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002946
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002947 // Store the tokens of the parentheses. We will parse them after we determine
2948 // the context that follows them.
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00002949 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002950 // We didn't find the ')' we expected.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002951 Tracker.consumeClose();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002952 return ExprError();
2953 }
2954
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002955 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002956 ParseAs = CompoundLiteral;
2957 } else {
2958 bool NotCastExpr;
Eli Friedmancf7530f2009-05-25 19:41:42 +00002959 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2960 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2961 NotCastExpr = true;
2962 } else {
2963 // Try parsing the cast-expression that may follow.
2964 // If it is not a cast-expression, NotCastExpr will be true and no token
2965 // will be consumed.
2966 Result = ParseCastExpression(false/*isUnaryExpression*/,
2967 false/*isAddressofOperand*/,
John McCallba7bf592010-08-24 05:47:05 +00002968 NotCastExpr,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002969 // type-id has priority.
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +00002970 IsTypeCast);
Eli Friedmancf7530f2009-05-25 19:41:42 +00002971 }
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002972
2973 // If we parsed a cast-expression, it's really a type-id, otherwise it's
2974 // an expression.
2975 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002976 }
2977
Mike Stump11289f42009-09-09 15:08:12 +00002978 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002979 Toks.push_back(Tok);
2980 // Re-enter the stored parenthesized tokens into the token stream, so we may
2981 // parse them now.
2982 PP.EnterTokenStream(Toks.data(), Toks.size(),
2983 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2984 // Drop the current token and bring the first cached one. It's the same token
2985 // as when we entered this function.
2986 ConsumeAnyToken();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002987
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002988 if (ParseAs >= CompoundLiteral) {
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002989 // Parse the type declarator.
2990 DeclSpec DS(AttrFactory);
2991 ParseSpecifierQualifierList(DS);
2992 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2993 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002994
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002995 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002996 Tracker.consumeClose();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002997
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002998 if (ParseAs == CompoundLiteral) {
2999 ExprType = CompoundLiteral;
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00003000 TypeResult Ty = ParseTypeName();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003001 return ParseCompoundLiteralExpression(Ty.get(),
3002 Tracker.getOpenLocation(),
3003 Tracker.getCloseLocation());
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003004 }
Mike Stump11289f42009-09-09 15:08:12 +00003005
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003006 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
3007 assert(ParseAs == CastExpr);
3008
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00003009 if (DeclaratorInfo.isInvalidType())
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003010 return ExprError();
3011
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003012 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003013 if (!Result.isInvalid())
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003014 Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(),
3015 DeclaratorInfo, CastTy,
3016 Tracker.getCloseLocation(), Result.take());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003017 return Result;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003018 }
Mike Stump11289f42009-09-09 15:08:12 +00003019
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003020 // Not a compound literal, and not followed by a cast-expression.
3021 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003022
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003023 ExprType = SimpleExpr;
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003024 Result = ParseExpression();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003025 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003026 Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(),
3027 Tok.getLocation(), Result.take());
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003028
3029 // Match the ')'.
3030 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003031 SkipUntil(tok::r_paren, StopAtSemi);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003032 return ExprError();
3033 }
Mike Stump11289f42009-09-09 15:08:12 +00003034
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003035 Tracker.consumeClose();
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003036 return Result;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003037}