blob: f10ca6ab784ed0ef8727daa4063623a5e6c17884 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation for C++.
11//
12//===----------------------------------------------------------------------===//
Stephen Hines651f13c2014-04-23 16:59:28 -070013#include "clang/AST/ASTContext.h"
Douglas Gregorbc61bd82011-01-11 00:33:19 +000014#include "RAIIObjectsForParser.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070015#include "clang/AST/DeclTemplate.h"
Eli Friedmandc3b7232012-01-04 02:40:39 +000016#include "clang/Basic/PrettyStackTrace.h"
Richard Smith33762772012-03-08 23:06:02 +000017#include "clang/Lex/LiteralSupport.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000018#include "clang/Parse/ParseDiagnostic.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070019#include "clang/Parse/Parser.h"
John McCall19510852010-08-20 18:27:03 +000020#include "clang/Sema/DeclSpec.h"
21#include "clang/Sema/ParsedTemplate.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000022#include "clang/Sema/Scope.h"
Douglas Gregor3f9a0562009-11-03 01:35:08 +000023#include "llvm/Support/ErrorHandling.h"
24
Faisal Valifad9e132013-09-26 19:54:12 +000025
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
Richard Smithea698b32011-04-14 21:45:45 +000028static int SelectDigraphErrorMessage(tok::TokenKind Kind) {
29 switch (Kind) {
Stephen Hines651f13c2014-04-23 16:59:28 -070030 // template name
31 case tok::unknown: return 0;
32 // casts
Richard Smithea698b32011-04-14 21:45:45 +000033 case tok::kw_const_cast: return 1;
34 case tok::kw_dynamic_cast: return 2;
35 case tok::kw_reinterpret_cast: return 3;
36 case tok::kw_static_cast: return 4;
37 default:
David Blaikieb219cfc2011-09-23 05:06:16 +000038 llvm_unreachable("Unknown type for digraph error message.");
Richard Smithea698b32011-04-14 21:45:45 +000039 }
40}
41
42// Are the two tokens adjacent in the same source file?
Richard Smith19a27022012-06-18 06:11:04 +000043bool Parser::areTokensAdjacent(const Token &First, const Token &Second) {
Richard Smithea698b32011-04-14 21:45:45 +000044 SourceManager &SM = PP.getSourceManager();
45 SourceLocation FirstLoc = SM.getSpellingLoc(First.getLocation());
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +000046 SourceLocation FirstEnd = FirstLoc.getLocWithOffset(First.getLength());
Richard Smithea698b32011-04-14 21:45:45 +000047 return FirstEnd == SM.getSpellingLoc(Second.getLocation());
48}
49
50// Suggest fixit for "<::" after a cast.
51static void FixDigraph(Parser &P, Preprocessor &PP, Token &DigraphToken,
52 Token &ColonToken, tok::TokenKind Kind, bool AtDigraph) {
53 // Pull '<:' and ':' off token stream.
54 if (!AtDigraph)
55 PP.Lex(DigraphToken);
56 PP.Lex(ColonToken);
57
58 SourceRange Range;
59 Range.setBegin(DigraphToken.getLocation());
60 Range.setEnd(ColonToken.getLocation());
61 P.Diag(DigraphToken.getLocation(), diag::err_missing_whitespace_digraph)
62 << SelectDigraphErrorMessage(Kind)
63 << FixItHint::CreateReplacement(Range, "< ::");
64
65 // Update token information to reflect their change in token type.
66 ColonToken.setKind(tok::coloncolon);
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +000067 ColonToken.setLocation(ColonToken.getLocation().getLocWithOffset(-1));
Richard Smithea698b32011-04-14 21:45:45 +000068 ColonToken.setLength(2);
69 DigraphToken.setKind(tok::less);
70 DigraphToken.setLength(1);
71
72 // Push new tokens back to token stream.
73 PP.EnterToken(ColonToken);
74 if (!AtDigraph)
75 PP.EnterToken(DigraphToken);
76}
77
Richard Trieu950be712011-09-19 19:01:00 +000078// Check for '<::' which should be '< ::' instead of '[:' when following
79// a template name.
80void Parser::CheckForTemplateAndDigraph(Token &Next, ParsedType ObjectType,
81 bool EnteringContext,
82 IdentifierInfo &II, CXXScopeSpec &SS) {
Richard Trieuc11030e2011-09-20 20:03:50 +000083 if (!Next.is(tok::l_square) || Next.getLength() != 2)
Richard Trieu950be712011-09-19 19:01:00 +000084 return;
85
86 Token SecondToken = GetLookAheadToken(2);
Richard Smith19a27022012-06-18 06:11:04 +000087 if (!SecondToken.is(tok::colon) || !areTokensAdjacent(Next, SecondToken))
Richard Trieu950be712011-09-19 19:01:00 +000088 return;
89
90 TemplateTy Template;
91 UnqualifiedId TemplateName;
92 TemplateName.setIdentifier(&II, Tok.getLocation());
93 bool MemberOfUnknownSpecialization;
94 if (!Actions.isTemplateName(getCurScope(), SS, /*hasTemplateKeyword=*/false,
95 TemplateName, ObjectType, EnteringContext,
96 Template, MemberOfUnknownSpecialization))
97 return;
98
Stephen Hines651f13c2014-04-23 16:59:28 -070099 FixDigraph(*this, PP, Next, SecondToken, tok::unknown,
Richard Trieu950be712011-09-19 19:01:00 +0000100 /*AtDigraph*/false);
101}
102
Richard Trieu919b9552012-11-02 01:08:58 +0000103/// \brief Emits an error for a left parentheses after a double colon.
104///
105/// When a '(' is found after a '::', emit an error. Attempt to fix the token
Nico Weberbba91b82012-11-29 05:29:23 +0000106/// stream by removing the '(', and the matching ')' if found.
Richard Trieu919b9552012-11-02 01:08:58 +0000107void Parser::CheckForLParenAfterColonColon() {
108 if (!Tok.is(tok::l_paren))
109 return;
110
111 SourceLocation l_parenLoc = ConsumeParen(), r_parenLoc;
112 Token Tok1 = getCurToken();
113 if (!Tok1.is(tok::identifier) && !Tok1.is(tok::star))
114 return;
115
116 if (Tok1.is(tok::identifier)) {
117 Token Tok2 = GetLookAheadToken(1);
118 if (Tok2.is(tok::r_paren)) {
119 ConsumeToken();
120 PP.EnterToken(Tok1);
121 r_parenLoc = ConsumeParen();
122 }
123 } else if (Tok1.is(tok::star)) {
124 Token Tok2 = GetLookAheadToken(1);
125 if (Tok2.is(tok::identifier)) {
126 Token Tok3 = GetLookAheadToken(2);
127 if (Tok3.is(tok::r_paren)) {
128 ConsumeToken();
129 ConsumeToken();
130 PP.EnterToken(Tok2);
131 PP.EnterToken(Tok1);
132 r_parenLoc = ConsumeParen();
133 }
134 }
135 }
136
137 Diag(l_parenLoc, diag::err_paren_after_colon_colon)
138 << FixItHint::CreateRemoval(l_parenLoc)
139 << FixItHint::CreateRemoval(r_parenLoc);
140}
141
Mike Stump1eb44332009-09-09 15:08:12 +0000142/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000143///
144/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump1eb44332009-09-09 15:08:12 +0000145/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000146/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000147///
148/// '::'[opt] nested-name-specifier
149/// '::'
150///
151/// nested-name-specifier:
152/// type-name '::'
153/// namespace-name '::'
154/// nested-name-specifier identifier '::'
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000155/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000156///
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000157///
Mike Stump1eb44332009-09-09 15:08:12 +0000158/// \param SS the scope specifier that will be set to the parsed
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000159/// nested-name-specifier (or empty)
160///
Mike Stump1eb44332009-09-09 15:08:12 +0000161/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000162/// the "." or "->" of a member access expression, this parameter provides the
163/// type of the object whose members are being accessed.
164///
165/// \param EnteringContext whether we will be entering into the context of
166/// the nested-name-specifier after parsing it.
167///
Douglas Gregord4dca082010-02-24 18:44:31 +0000168/// \param MayBePseudoDestructor When non-NULL, points to a flag that
169/// indicates whether this nested-name-specifier may be part of a
170/// pseudo-destructor name. In this case, the flag will be set false
171/// if we don't actually end up parsing a destructor name. Moreorover,
172/// if we do end up determining that we are parsing a destructor name,
173/// the last component of the nested-name-specifier is not parsed as
174/// part of the scope specifier.
Richard Smith2db075b2013-03-26 01:15:19 +0000175///
176/// \param IsTypename If \c true, this nested-name-specifier is known to be
177/// part of a type name. This is used to improve error recovery.
178///
179/// \param LastII When non-NULL, points to an IdentifierInfo* that will be
180/// filled in with the leading identifier in the last component of the
181/// nested-name-specifier, if any.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000182///
John McCall9ba61662010-02-26 08:45:28 +0000183/// \returns true if there was an error parsing a scope specifier
Douglas Gregor495c35d2009-08-25 22:51:20 +0000184bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +0000185 ParsedType ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000186 bool EnteringContext,
Francois Pichet4147d302011-03-27 19:41:34 +0000187 bool *MayBePseudoDestructor,
Richard Smith2db075b2013-03-26 01:15:19 +0000188 bool IsTypename,
189 IdentifierInfo **LastII) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000190 assert(getLangOpts().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +0000191 "Call sites of this function should be guarded by checking for C++");
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000193 if (Tok.is(tok::annot_cxxscope)) {
Richard Smith2db075b2013-03-26 01:15:19 +0000194 assert(!LastII && "want last identifier but have already annotated scope");
Douglas Gregorc34348a2011-02-24 17:54:50 +0000195 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
196 Tok.getAnnotationRange(),
197 SS);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000198 ConsumeToken();
John McCall9ba61662010-02-26 08:45:28 +0000199 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000200 }
Chris Lattnere607e802009-01-04 21:14:15 +0000201
Larisse Voufo9c90f7f2013-08-06 05:49:26 +0000202 if (Tok.is(tok::annot_template_id)) {
203 // If the current token is an annotated template id, it may already have
204 // a scope specifier. Restore it.
205 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
206 SS = TemplateId->SS;
207 }
208
Richard Smith2db075b2013-03-26 01:15:19 +0000209 if (LastII)
210 *LastII = 0;
211
Douglas Gregor39a8de12009-02-25 19:37:18 +0000212 bool HasScopeSpecifier = false;
213
Chris Lattner5b454732009-01-05 03:55:46 +0000214 if (Tok.is(tok::coloncolon)) {
215 // ::new and ::delete aren't nested-name-specifiers.
216 tok::TokenKind NextKind = NextToken().getKind();
217 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
218 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000219
Chris Lattner55a7cef2009-01-05 00:13:00 +0000220 // '::' - Global scope qualifier.
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000221 if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
222 return true;
Richard Trieu919b9552012-11-02 01:08:58 +0000223
224 CheckForLParenAfterColonColon();
225
Douglas Gregor39a8de12009-02-25 19:37:18 +0000226 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000227 }
228
Douglas Gregord4dca082010-02-24 18:44:31 +0000229 bool CheckForDestructor = false;
230 if (MayBePseudoDestructor && *MayBePseudoDestructor) {
231 CheckForDestructor = true;
232 *MayBePseudoDestructor = false;
233 }
234
David Blaikie42d6d0c2011-12-04 05:04:18 +0000235 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
236 DeclSpec DS(AttrFactory);
237 SourceLocation DeclLoc = Tok.getLocation();
238 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
Stephen Hines651f13c2014-04-23 16:59:28 -0700239
240 SourceLocation CCLoc;
241 if (!TryConsumeToken(tok::coloncolon, CCLoc)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +0000242 AnnotateExistingDecltypeSpecifier(DS, DeclLoc, EndLoc);
243 return false;
244 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700245
David Blaikie42d6d0c2011-12-04 05:04:18 +0000246 if (Actions.ActOnCXXNestedNameSpecifierDecltype(SS, DS, CCLoc))
247 SS.SetInvalid(SourceRange(DeclLoc, CCLoc));
248
249 HasScopeSpecifier = true;
250 }
251
Douglas Gregor39a8de12009-02-25 19:37:18 +0000252 while (true) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000253 if (HasScopeSpecifier) {
254 // C++ [basic.lookup.classref]p5:
255 // If the qualified-id has the form
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000256 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000257 // ::class-name-or-namespace-name::...
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000258 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000259 // the class-name-or-namespace-name is looked up in global scope as a
260 // class-name or namespace-name.
261 //
262 // To implement this, we clear out the object type as soon as we've
263 // seen a leading '::' or part of a nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000264 ObjectType = ParsedType();
Douglas Gregor81b747b2009-09-17 21:32:03 +0000265
266 if (Tok.is(tok::code_completion)) {
267 // Code completion for a nested-name-specifier, where the code
268 // code completion token follows the '::'.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000269 Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
Argyrios Kyrtzidisb6b2b182011-04-23 01:04:12 +0000270 // Include code completion token into the range of the scope otherwise
271 // when we try to annotate the scope tokens the dangling code completion
272 // token will cause assertion in
273 // Preprocessor::AnnotatePreviousCachedTokens.
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000274 SS.setEndLoc(Tok.getLocation());
275 cutOffParsing();
276 return true;
Douglas Gregor81b747b2009-09-17 21:32:03 +0000277 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000278 }
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Douglas Gregor39a8de12009-02-25 19:37:18 +0000280 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +0000281 // nested-name-specifier 'template'[opt] simple-template-id '::'
282
283 // Parse the optional 'template' keyword, then make sure we have
284 // 'identifier <' after it.
285 if (Tok.is(tok::kw_template)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000286 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +0000287 // nested-name-specifier, since they aren't allowed to start with
288 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000289 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +0000290 break;
291
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000292 TentativeParsingAction TPA(*this);
Chris Lattner77cf72a2009-06-26 03:47:46 +0000293 SourceLocation TemplateKWLoc = ConsumeToken();
Stephen Hines651f13c2014-04-23 16:59:28 -0700294
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000295 UnqualifiedId TemplateName;
296 if (Tok.is(tok::identifier)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000297 // Consume the identifier.
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000298 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000299 ConsumeToken();
300 } else if (Tok.is(tok::kw_operator)) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700301 // We don't need to actually parse the unqualified-id in this case,
302 // because a simple-template-id cannot start with 'operator', but
303 // go ahead and parse it anyway for consistency with the case where
304 // we already annotated the template-id.
305 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000306 TemplateName)) {
307 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000308 break;
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000309 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700310
Sean Hunte6252d12009-11-28 08:58:14 +0000311 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
312 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000313 Diag(TemplateName.getSourceRange().getBegin(),
314 diag::err_id_after_template_in_nested_name_spec)
315 << TemplateName.getSourceRange();
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000316 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000317 break;
318 }
319 } else {
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000320 TPA.Revert();
Chris Lattner77cf72a2009-06-26 03:47:46 +0000321 break;
322 }
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000324 // If the next token is not '<', we have a qualified-id that refers
325 // to a template name, such as T::template apply, but is not a
326 // template-id.
327 if (Tok.isNot(tok::less)) {
328 TPA.Revert();
329 break;
330 }
331
332 // Commit to parsing the template-id.
333 TPA.Commit();
Douglas Gregord6ab2322010-06-16 23:00:59 +0000334 TemplateTy Template;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000335 if (TemplateNameKind TNK
336 = Actions.ActOnDependentTemplateName(getCurScope(),
337 SS, TemplateKWLoc, TemplateName,
338 ObjectType, EnteringContext,
339 Template)) {
340 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateKWLoc,
341 TemplateName, false))
Douglas Gregord6ab2322010-06-16 23:00:59 +0000342 return true;
343 } else
John McCall9ba61662010-02-26 08:45:28 +0000344 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Chris Lattner77cf72a2009-06-26 03:47:46 +0000346 continue;
347 }
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Douglas Gregor39a8de12009-02-25 19:37:18 +0000349 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000350 // We have
Douglas Gregor39a8de12009-02-25 19:37:18 +0000351 //
Stephen Hines651f13c2014-04-23 16:59:28 -0700352 // template-id '::'
Douglas Gregor39a8de12009-02-25 19:37:18 +0000353 //
Stephen Hines651f13c2014-04-23 16:59:28 -0700354 // So we need to check whether the template-id is a simple-template-id of
355 // the right kind (it should name a type or be dependent), and then
Douglas Gregorc45c2322009-03-31 00:43:58 +0000356 // convert it into a type within the nested-name-specifier.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000357 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord4dca082010-02-24 18:44:31 +0000358 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
359 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000360 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000361 }
362
Richard Smith2db075b2013-03-26 01:15:19 +0000363 if (LastII)
364 *LastII = TemplateId->Name;
365
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000366 // Consume the template-id token.
367 ConsumeToken();
368
369 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
370 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000371
David Blaikie6796fc12011-11-07 03:30:03 +0000372 HasScopeSpecifier = true;
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000373
Benjamin Kramer5354e772012-08-23 23:38:35 +0000374 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000375 TemplateId->NumArgs);
376
377 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000378 SS,
379 TemplateId->TemplateKWLoc,
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000380 TemplateId->Template,
381 TemplateId->TemplateNameLoc,
382 TemplateId->LAngleLoc,
383 TemplateArgsPtr,
384 TemplateId->RAngleLoc,
385 CCLoc,
386 EnteringContext)) {
387 SourceLocation StartLoc
388 = SS.getBeginLoc().isValid()? SS.getBeginLoc()
389 : TemplateId->TemplateNameLoc;
390 SS.SetInvalid(SourceRange(StartLoc, CCLoc));
Chris Lattner67b9e832009-06-26 03:45:46 +0000391 }
Argyrios Kyrtzidiseccce7e2011-05-03 18:45:38 +0000392
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000393 continue;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000394 }
395
Chris Lattner5c7f7862009-06-26 03:52:38 +0000396
397 // The rest of the nested-name-specifier possibilities start with
398 // tok::identifier.
399 if (Tok.isNot(tok::identifier))
400 break;
401
402 IdentifierInfo &II = *Tok.getIdentifierInfo();
403
404 // nested-name-specifier:
405 // type-name '::'
406 // namespace-name '::'
407 // nested-name-specifier identifier '::'
408 Token Next = NextToken();
Chris Lattner46646492009-12-07 01:36:53 +0000409
410 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
411 // and emit a fixit hint for it.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000412 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000413 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
414 Tok.getLocation(),
415 Next.getLocation(), ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000416 EnteringContext) &&
417 // If the token after the colon isn't an identifier, it's still an
418 // error, but they probably meant something else strange so don't
419 // recover like this.
420 PP.LookAhead(1).is(tok::identifier)) {
421 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
Douglas Gregor849b2432010-03-31 17:46:05 +0000422 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregorb10cd042010-02-21 18:36:56 +0000423
424 // Recover as if the user wrote '::'.
425 Next.setKind(tok::coloncolon);
426 }
Chris Lattner46646492009-12-07 01:36:53 +0000427 }
428
Chris Lattner5c7f7862009-06-26 03:52:38 +0000429 if (Next.is(tok::coloncolon)) {
Douglas Gregor77549082010-02-24 21:29:12 +0000430 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000431 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor77549082010-02-24 21:29:12 +0000432 II, ObjectType)) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000433 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000434 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000435 }
436
Richard Smith2db075b2013-03-26 01:15:19 +0000437 if (LastII)
438 *LastII = &II;
439
Chris Lattner5c7f7862009-06-26 03:52:38 +0000440 // We have an identifier followed by a '::'. Lookup this name
441 // as the name in a nested-name-specifier.
442 SourceLocation IdLoc = ConsumeToken();
Chris Lattner46646492009-12-07 01:36:53 +0000443 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
444 "NextToken() not working properly!");
Chris Lattner5c7f7862009-06-26 03:52:38 +0000445 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Richard Trieu919b9552012-11-02 01:08:58 +0000447 CheckForLParenAfterColonColon();
448
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000449 HasScopeSpecifier = true;
450 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
451 ObjectType, EnteringContext, SS))
452 SS.SetInvalid(SourceRange(IdLoc, CCLoc));
453
Chris Lattner5c7f7862009-06-26 03:52:38 +0000454 continue;
455 }
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Richard Trieu950be712011-09-19 19:01:00 +0000457 CheckForTemplateAndDigraph(Next, ObjectType, EnteringContext, II, SS);
Richard Smithea698b32011-04-14 21:45:45 +0000458
Chris Lattner5c7f7862009-06-26 03:52:38 +0000459 // nested-name-specifier:
460 // type-name '<'
461 if (Next.is(tok::less)) {
462 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000463 UnqualifiedId TemplateName;
464 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000465 bool MemberOfUnknownSpecialization;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000466 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000467 /*hasTemplateKeyword=*/false,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000468 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000469 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000470 EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000471 Template,
472 MemberOfUnknownSpecialization)) {
David Blaikie6796fc12011-11-07 03:30:03 +0000473 // We have found a template name, so annotate this token
Chris Lattner5c7f7862009-06-26 03:52:38 +0000474 // with a template-id annotation. We do not permit the
475 // template-id to be translated into a type annotation,
476 // because some clients (e.g., the parsing of class template
477 // specializations) still want to see the original template-id
478 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000479 ConsumeToken();
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000480 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
481 TemplateName, false))
John McCall9ba61662010-02-26 08:45:28 +0000482 return true;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000483 continue;
Larisse Voufoef4579c2013-08-06 01:03:05 +0000484 }
485
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000486 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
Francois Pichet4147d302011-03-27 19:41:34 +0000487 (IsTypename || IsTemplateArgumentList(1))) {
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000488 // We have something like t::getAs<T>, where getAs is a
489 // member of an unknown specialization. However, this will only
490 // parse correctly as a template, so suggest the keyword 'template'
491 // before 'getAs' and treat this as a dependent template name.
Francois Pichet4147d302011-03-27 19:41:34 +0000492 unsigned DiagID = diag::err_missing_dependent_template_keyword;
David Blaikie4e4d0842012-03-11 07:00:24 +0000493 if (getLangOpts().MicrosoftExt)
Francois Pichetcf320c62011-04-22 08:25:24 +0000494 DiagID = diag::warn_missing_dependent_template_keyword;
Francois Pichet4147d302011-03-27 19:41:34 +0000495
496 Diag(Tok.getLocation(), DiagID)
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000497 << II.getName()
498 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
499
Douglas Gregord6ab2322010-06-16 23:00:59 +0000500 if (TemplateNameKind TNK
Douglas Gregor23c94db2010-07-02 17:43:08 +0000501 = Actions.ActOnDependentTemplateName(getCurScope(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000502 SS, SourceLocation(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000503 TemplateName, ObjectType,
504 EnteringContext, Template)) {
505 // Consume the identifier.
506 ConsumeToken();
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000507 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
508 TemplateName, false))
509 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000510 }
511 else
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000512 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000513
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000514 continue;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000515 }
516 }
517
Douglas Gregor39a8de12009-02-25 19:37:18 +0000518 // We don't have any tokens that form the beginning of a
519 // nested-name-specifier, so we're done.
520 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000521 }
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Douglas Gregord4dca082010-02-24 18:44:31 +0000523 // Even if we didn't see any pieces of a nested-name-specifier, we
524 // still check whether there is a tilde in this position, which
525 // indicates a potential pseudo-destructor.
526 if (CheckForDestructor && Tok.is(tok::tilde))
527 *MayBePseudoDestructor = true;
528
John McCall9ba61662010-02-26 08:45:28 +0000529 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000530}
531
532/// ParseCXXIdExpression - Handle id-expression.
533///
534/// id-expression:
535/// unqualified-id
536/// qualified-id
537///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000538/// qualified-id:
539/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
540/// '::' identifier
541/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000542/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000543///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000544/// NOTE: The standard specifies that, for qualified-id, the parser does not
545/// expect:
546///
547/// '::' conversion-function-id
548/// '::' '~' class-name
549///
550/// This may cause a slight inconsistency on diagnostics:
551///
552/// class C {};
553/// namespace A {}
554/// void f() {
555/// :: A :: ~ C(); // Some Sema error about using destructor with a
556/// // namespace.
557/// :: ~ C(); // Some Parser error like 'unexpected ~'.
558/// }
559///
560/// We simplify the parser a bit and make it work like:
561///
562/// qualified-id:
563/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
564/// '::' unqualified-id
565///
566/// That way Sema can handle and report similar errors for namespaces and the
567/// global scope.
568///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000569/// The isAddressOfOperand parameter indicates that this id-expression is a
570/// direct operand of the address-of operator. This is, besides member contexts,
571/// the only place where a qualified-id naming a non-static class member may
572/// appear.
573///
John McCall60d7b3a2010-08-24 06:29:42 +0000574ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000575 // qualified-id:
576 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
577 // '::' unqualified-id
578 //
579 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000580 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000581
582 SourceLocation TemplateKWLoc;
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000583 UnqualifiedId Name;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000584 if (ParseUnqualifiedId(SS,
585 /*EnteringContext=*/false,
586 /*AllowDestructorName=*/false,
587 /*AllowConstructorName=*/false,
John McCallb3d87482010-08-24 05:47:05 +0000588 /*ObjectType=*/ ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000589 TemplateKWLoc,
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000590 Name))
591 return ExprError();
John McCallb681b612009-11-22 02:49:43 +0000592
593 // This is only the direct operand of an & operator if it is not
594 // followed by a postfix-expression suffix.
John McCall9c72c602010-08-27 09:08:28 +0000595 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
596 isAddressOfOperand = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000597
598 return Actions.ActOnIdExpression(getCurScope(), SS, TemplateKWLoc, Name,
599 Tok.is(tok::l_paren), isAddressOfOperand);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000600}
601
Richard Smith0a664b82013-05-09 21:36:41 +0000602/// ParseLambdaExpression - Parse a C++11 lambda expression.
Douglas Gregorae7902c2011-08-04 15:30:47 +0000603///
604/// lambda-expression:
605/// lambda-introducer lambda-declarator[opt] compound-statement
606///
607/// lambda-introducer:
608/// '[' lambda-capture[opt] ']'
609///
610/// lambda-capture:
611/// capture-default
612/// capture-list
613/// capture-default ',' capture-list
614///
615/// capture-default:
616/// '&'
617/// '='
618///
619/// capture-list:
620/// capture
621/// capture-list ',' capture
622///
623/// capture:
Richard Smith0a664b82013-05-09 21:36:41 +0000624/// simple-capture
625/// init-capture [C++1y]
626///
627/// simple-capture:
Douglas Gregorae7902c2011-08-04 15:30:47 +0000628/// identifier
629/// '&' identifier
630/// 'this'
631///
Richard Smith0a664b82013-05-09 21:36:41 +0000632/// init-capture: [C++1y]
633/// identifier initializer
634/// '&' identifier initializer
635///
Douglas Gregorae7902c2011-08-04 15:30:47 +0000636/// lambda-declarator:
637/// '(' parameter-declaration-clause ')' attribute-specifier[opt]
638/// 'mutable'[opt] exception-specification[opt]
639/// trailing-return-type[opt]
640///
641ExprResult Parser::ParseLambdaExpression() {
642 // Parse lambda-introducer.
643 LambdaIntroducer Intro;
Bill Wendling2434dcf2013-12-05 05:25:04 +0000644 Optional<unsigned> DiagID = ParseLambdaIntroducer(Intro);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000645 if (DiagID) {
646 Diag(Tok, DiagID.getValue());
Alexey Bataev8fe24752013-11-18 08:17:37 +0000647 SkipUntil(tok::r_square, StopAtSemi);
648 SkipUntil(tok::l_brace, StopAtSemi);
649 SkipUntil(tok::r_brace, StopAtSemi);
Eli Friedmandc3b7232012-01-04 02:40:39 +0000650 return ExprError();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000651 }
652
653 return ParseLambdaExpressionAfterIntroducer(Intro);
654}
655
656/// TryParseLambdaExpression - Use lookahead and potentially tentative
657/// parsing to determine if we are looking at a C++0x lambda expression, and parse
658/// it if we are.
659///
660/// If we are not looking at a lambda expression, returns ExprError().
661ExprResult Parser::TryParseLambdaExpression() {
Richard Smith80ad52f2013-01-02 11:42:31 +0000662 assert(getLangOpts().CPlusPlus11
Douglas Gregorae7902c2011-08-04 15:30:47 +0000663 && Tok.is(tok::l_square)
664 && "Not at the start of a possible lambda expression.");
665
666 const Token Next = NextToken(), After = GetLookAheadToken(2);
667
668 // If lookahead indicates this is a lambda...
669 if (Next.is(tok::r_square) || // []
670 Next.is(tok::equal) || // [=
671 (Next.is(tok::amp) && // [&] or [&,
672 (After.is(tok::r_square) ||
673 After.is(tok::comma))) ||
674 (Next.is(tok::identifier) && // [identifier]
675 After.is(tok::r_square))) {
676 return ParseLambdaExpression();
677 }
678
Eli Friedmandc3b7232012-01-04 02:40:39 +0000679 // If lookahead indicates an ObjC message send...
680 // [identifier identifier
Douglas Gregorae7902c2011-08-04 15:30:47 +0000681 if (Next.is(tok::identifier) && After.is(tok::identifier)) {
Eli Friedmandc3b7232012-01-04 02:40:39 +0000682 return ExprEmpty();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000683 }
Bill Wendling2434dcf2013-12-05 05:25:04 +0000684
Eli Friedmandc3b7232012-01-04 02:40:39 +0000685 // Here, we're stuck: lambda introducers and Objective-C message sends are
686 // unambiguous, but it requires arbitrary lookhead. [a,b,c,d,e,f,g] is a
687 // lambda, and [a,b,c,d,e,f,g h] is a Objective-C message send. Instead of
688 // writing two routines to parse a lambda introducer, just try to parse
689 // a lambda introducer first, and fall back if that fails.
690 // (TryParseLambdaIntroducer never produces any diagnostic output.)
Douglas Gregorae7902c2011-08-04 15:30:47 +0000691 LambdaIntroducer Intro;
692 if (TryParseLambdaIntroducer(Intro))
Eli Friedmandc3b7232012-01-04 02:40:39 +0000693 return ExprEmpty();
Bill Wendling2434dcf2013-12-05 05:25:04 +0000694
Douglas Gregorae7902c2011-08-04 15:30:47 +0000695 return ParseLambdaExpressionAfterIntroducer(Intro);
696}
697
Richard Smith440d4562013-05-21 22:21:19 +0000698/// \brief Parse a lambda introducer.
699/// \param Intro A LambdaIntroducer filled in with information about the
700/// contents of the lambda-introducer.
701/// \param SkippedInits If non-null, we are disambiguating between an Obj-C
702/// message send and a lambda expression. In this mode, we will
703/// sometimes skip the initializers for init-captures and not fully
704/// populate \p Intro. This flag will be set to \c true if we do so.
705/// \return A DiagnosticID if it hit something unexpected. The location for
706/// for the diagnostic is that of the current token.
707Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro,
708 bool *SkippedInits) {
David Blaikiedc84cd52013-02-20 22:23:23 +0000709 typedef Optional<unsigned> DiagResult;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000710
711 assert(Tok.is(tok::l_square) && "Lambda expressions begin with '['.");
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000712 BalancedDelimiterTracker T(*this, tok::l_square);
713 T.consumeOpen();
714
715 Intro.Range.setBegin(T.getOpenLocation());
Douglas Gregorae7902c2011-08-04 15:30:47 +0000716
717 bool first = true;
718
719 // Parse capture-default.
720 if (Tok.is(tok::amp) &&
721 (NextToken().is(tok::comma) || NextToken().is(tok::r_square))) {
722 Intro.Default = LCD_ByRef;
Douglas Gregor3ac109c2012-02-10 17:46:20 +0000723 Intro.DefaultLoc = ConsumeToken();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000724 first = false;
725 } else if (Tok.is(tok::equal)) {
726 Intro.Default = LCD_ByCopy;
Douglas Gregor3ac109c2012-02-10 17:46:20 +0000727 Intro.DefaultLoc = ConsumeToken();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000728 first = false;
729 }
730
731 while (Tok.isNot(tok::r_square)) {
732 if (!first) {
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000733 if (Tok.isNot(tok::comma)) {
Douglas Gregor437fbc52012-07-31 00:50:07 +0000734 // Provide a completion for a lambda introducer here. Except
735 // in Objective-C, where this is Almost Surely meant to be a message
736 // send. In that case, fail here and let the ObjC message
737 // expression parser perform the completion.
Douglas Gregord48ab062012-07-31 15:27:48 +0000738 if (Tok.is(tok::code_completion) &&
739 !(getLangOpts().ObjC1 && Intro.Default == LCD_None &&
740 !Intro.Captures.empty())) {
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000741 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
742 /*AfterAmpersand=*/false);
743 ConsumeCodeCompletionToken();
744 break;
745 }
746
Douglas Gregorae7902c2011-08-04 15:30:47 +0000747 return DiagResult(diag::err_expected_comma_or_rsquare);
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000748 }
Douglas Gregorae7902c2011-08-04 15:30:47 +0000749 ConsumeToken();
750 }
751
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000752 if (Tok.is(tok::code_completion)) {
753 // If we're in Objective-C++ and we have a bare '[', then this is more
754 // likely to be a message receiver.
David Blaikie4e4d0842012-03-11 07:00:24 +0000755 if (getLangOpts().ObjC1 && first)
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000756 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
757 else
758 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
759 /*AfterAmpersand=*/false);
760 ConsumeCodeCompletionToken();
761 break;
762 }
Douglas Gregorae7902c2011-08-04 15:30:47 +0000763
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000764 first = false;
765
Douglas Gregorae7902c2011-08-04 15:30:47 +0000766 // Parse capture.
767 LambdaCaptureKind Kind = LCK_ByCopy;
768 SourceLocation Loc;
769 IdentifierInfo* Id = 0;
Douglas Gregora7365242012-02-14 19:27:52 +0000770 SourceLocation EllipsisLoc;
Richard Smith0a664b82013-05-09 21:36:41 +0000771 ExprResult Init;
Douglas Gregora7365242012-02-14 19:27:52 +0000772
Douglas Gregorae7902c2011-08-04 15:30:47 +0000773 if (Tok.is(tok::kw_this)) {
774 Kind = LCK_This;
775 Loc = ConsumeToken();
776 } else {
777 if (Tok.is(tok::amp)) {
778 Kind = LCK_ByRef;
779 ConsumeToken();
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000780
781 if (Tok.is(tok::code_completion)) {
782 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
783 /*AfterAmpersand=*/true);
784 ConsumeCodeCompletionToken();
785 break;
786 }
Douglas Gregorae7902c2011-08-04 15:30:47 +0000787 }
788
789 if (Tok.is(tok::identifier)) {
790 Id = Tok.getIdentifierInfo();
791 Loc = ConsumeToken();
792 } else if (Tok.is(tok::kw_this)) {
793 // FIXME: If we want to suggest a fixit here, will need to return more
794 // than just DiagnosticID. Perhaps full DiagnosticBuilder that can be
795 // Clear()ed to prevent emission in case of tentative parsing?
796 return DiagResult(diag::err_this_captured_by_reference);
797 } else {
798 return DiagResult(diag::err_expected_capture);
799 }
Richard Smith0a664b82013-05-09 21:36:41 +0000800
801 if (Tok.is(tok::l_paren)) {
802 BalancedDelimiterTracker Parens(*this, tok::l_paren);
803 Parens.consumeOpen();
804
805 ExprVector Exprs;
806 CommaLocsTy Commas;
Richard Smith440d4562013-05-21 22:21:19 +0000807 if (SkippedInits) {
808 Parens.skipToEnd();
809 *SkippedInits = true;
810 } else if (ParseExpressionList(Exprs, Commas)) {
Richard Smith0a664b82013-05-09 21:36:41 +0000811 Parens.skipToEnd();
812 Init = ExprError();
813 } else {
814 Parens.consumeClose();
815 Init = Actions.ActOnParenListExpr(Parens.getOpenLocation(),
816 Parens.getCloseLocation(),
817 Exprs);
818 }
819 } else if (Tok.is(tok::l_brace) || Tok.is(tok::equal)) {
Bill Wendling2434dcf2013-12-05 05:25:04 +0000820 // Each lambda init-capture forms its own full expression, which clears
821 // Actions.MaybeODRUseExprs. So create an expression evaluation context
822 // to save the necessary state, and restore it later.
823 EnterExpressionEvaluationContext EC(Actions,
824 Sema::PotentiallyEvaluated);
Stephen Hines651f13c2014-04-23 16:59:28 -0700825 TryConsumeToken(tok::equal);
Richard Smith0a664b82013-05-09 21:36:41 +0000826
Richard Smith440d4562013-05-21 22:21:19 +0000827 if (!SkippedInits)
828 Init = ParseInitializer();
829 else if (Tok.is(tok::l_brace)) {
830 BalancedDelimiterTracker Braces(*this, tok::l_brace);
831 Braces.consumeOpen();
832 Braces.skipToEnd();
833 *SkippedInits = true;
834 } else {
835 // We're disambiguating this:
836 //
837 // [..., x = expr
838 //
839 // We need to find the end of the following expression in order to
840 // determine whether this is an Obj-C message send's receiver, or a
841 // lambda init-capture.
842 //
843 // Parse the expression to find where it ends, and annotate it back
844 // onto the tokens. We would have parsed this expression the same way
845 // in either case: both the RHS of an init-capture and the RHS of an
846 // assignment expression are parsed as an initializer-clause, and in
847 // neither case can anything be added to the scope between the '[' and
848 // here.
849 //
850 // FIXME: This is horrible. Adding a mechanism to skip an expression
851 // would be much cleaner.
852 // FIXME: If there is a ',' before the next ']' or ':', we can skip to
853 // that instead. (And if we see a ':' with no matching '?', we can
854 // classify this as an Obj-C message send.)
855 SourceLocation StartLoc = Tok.getLocation();
856 InMessageExpressionRAIIObject MaybeInMessageExpression(*this, true);
857 Init = ParseInitializer();
858
859 if (Tok.getLocation() != StartLoc) {
860 // Back out the lexing of the token after the initializer.
861 PP.RevertCachedTokens(1);
862
863 // Replace the consumed tokens with an appropriate annotation.
864 Tok.setLocation(StartLoc);
865 Tok.setKind(tok::annot_primary_expr);
866 setExprAnnotation(Tok, Init);
867 Tok.setAnnotationEndLoc(PP.getLastCachedTokenLocation());
868 PP.AnnotateCachedTokens(Tok);
869
870 // Consume the annotated initializer.
871 ConsumeToken();
872 }
873 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700874 } else
875 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000876 }
Bill Wendling2434dcf2013-12-05 05:25:04 +0000877 // If this is an init capture, process the initialization expression
878 // right away. For lambda init-captures such as the following:
879 // const int x = 10;
880 // auto L = [i = x+1](int a) {
881 // return [j = x+2,
882 // &k = x](char b) { };
883 // };
884 // keep in mind that each lambda init-capture has to have:
885 // - its initialization expression executed in the context
886 // of the enclosing/parent decl-context.
887 // - but the variable itself has to be 'injected' into the
888 // decl-context of its lambda's call-operator (which has
889 // not yet been created).
890 // Each init-expression is a full-expression that has to get
891 // Sema-analyzed (for capturing etc.) before its lambda's
892 // call-operator's decl-context, scope & scopeinfo are pushed on their
893 // respective stacks. Thus if any variable is odr-used in the init-capture
894 // it will correctly get captured in the enclosing lambda, if one exists.
895 // The init-variables above are created later once the lambdascope and
896 // call-operators decl-context is pushed onto its respective stack.
Douglas Gregorae7902c2011-08-04 15:30:47 +0000897
Bill Wendling2434dcf2013-12-05 05:25:04 +0000898 // Since the lambda init-capture's initializer expression occurs in the
899 // context of the enclosing function or lambda, therefore we can not wait
900 // till a lambda scope has been pushed on before deciding whether the
901 // variable needs to be captured. We also need to process all
902 // lvalue-to-rvalue conversions and discarded-value conversions,
903 // so that we can avoid capturing certain constant variables.
904 // For e.g.,
905 // void test() {
906 // const int x = 10;
907 // auto L = [&z = x](char a) { <-- don't capture by the current lambda
908 // return [y = x](int i) { <-- don't capture by enclosing lambda
909 // return y;
910 // }
911 // };
912 // If x was not const, the second use would require 'L' to capture, and
913 // that would be an error.
914
915 ParsedType InitCaptureParsedType;
916 if (Init.isUsable()) {
917 // Get the pointer and store it in an lvalue, so we can use it as an
918 // out argument.
919 Expr *InitExpr = Init.get();
920 // This performs any lvalue-to-rvalue conversions if necessary, which
921 // can affect what gets captured in the containing decl-context.
922 QualType InitCaptureType = Actions.performLambdaInitCaptureInitialization(
923 Loc, Kind == LCK_ByRef, Id, InitExpr);
924 Init = InitExpr;
925 InitCaptureParsedType.set(InitCaptureType);
926 }
927 Intro.addCapture(Kind, Loc, Id, EllipsisLoc, Init, InitCaptureParsedType);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000928 }
929
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000930 T.consumeClose();
931 Intro.Range.setEnd(T.getCloseLocation());
Douglas Gregorae7902c2011-08-04 15:30:47 +0000932 return DiagResult();
933}
934
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000935/// TryParseLambdaIntroducer - Tentatively parse a lambda introducer.
Douglas Gregorae7902c2011-08-04 15:30:47 +0000936///
937/// Returns true if it hit something unexpected.
938bool Parser::TryParseLambdaIntroducer(LambdaIntroducer &Intro) {
939 TentativeParsingAction PA(*this);
940
Richard Smith440d4562013-05-21 22:21:19 +0000941 bool SkippedInits = false;
942 Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro, &SkippedInits));
Douglas Gregorae7902c2011-08-04 15:30:47 +0000943
944 if (DiagID) {
945 PA.Revert();
946 return true;
947 }
948
Richard Smith440d4562013-05-21 22:21:19 +0000949 if (SkippedInits) {
950 // Parse it again, but this time parse the init-captures too.
951 PA.Revert();
952 Intro = LambdaIntroducer();
953 DiagID = ParseLambdaIntroducer(Intro);
954 assert(!DiagID && "parsing lambda-introducer failed on reparse");
955 return false;
956 }
957
Douglas Gregorae7902c2011-08-04 15:30:47 +0000958 PA.Commit();
959 return false;
960}
961
962/// ParseLambdaExpressionAfterIntroducer - Parse the rest of a lambda
963/// expression.
964ExprResult Parser::ParseLambdaExpressionAfterIntroducer(
965 LambdaIntroducer &Intro) {
Eli Friedmandc3b7232012-01-04 02:40:39 +0000966 SourceLocation LambdaBeginLoc = Intro.Range.getBegin();
967 Diag(LambdaBeginLoc, diag::warn_cxx98_compat_lambda);
968
969 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), LambdaBeginLoc,
970 "lambda expression parsing");
971
Faisal Valifad9e132013-09-26 19:54:12 +0000972
973
Richard Smith0a664b82013-05-09 21:36:41 +0000974 // FIXME: Call into Actions to add any init-capture declarations to the
975 // scope while parsing the lambda-declarator and compound-statement.
976
Douglas Gregorae7902c2011-08-04 15:30:47 +0000977 // Parse lambda-declarator[opt].
978 DeclSpec DS(AttrFactory);
Eli Friedmanf88c4002012-01-04 04:41:38 +0000979 Declarator D(DS, Declarator::LambdaExprContext);
Faisal Valifad9e132013-09-26 19:54:12 +0000980 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
981 Actions.PushLambdaScope();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000982
983 if (Tok.is(tok::l_paren)) {
984 ParseScope PrototypeScope(this,
985 Scope::FunctionPrototypeScope |
Richard Smith3a2b7a12013-01-28 22:42:45 +0000986 Scope::FunctionDeclarationScope |
Douglas Gregorae7902c2011-08-04 15:30:47 +0000987 Scope::DeclScope);
988
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000989 SourceLocation DeclEndLoc;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000990 BalancedDelimiterTracker T(*this, tok::l_paren);
991 T.consumeOpen();
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000992 SourceLocation LParenLoc = T.getOpenLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000993
994 // Parse parameter-declaration-clause.
995 ParsedAttributes Attr(AttrFactory);
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000996 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000997 SourceLocation EllipsisLoc;
Faisal Valifad9e132013-09-26 19:54:12 +0000998
999 if (Tok.isNot(tok::r_paren)) {
Faisal Valifad9e132013-09-26 19:54:12 +00001000 Actions.RecordParsingTemplateParameterDepth(TemplateParameterDepth);
Douglas Gregorae7902c2011-08-04 15:30:47 +00001001 ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc);
Faisal Valifad9e132013-09-26 19:54:12 +00001002 // For a generic lambda, each 'auto' within the parameter declaration
1003 // clause creates a template type parameter, so increment the depth.
1004 if (Actions.getCurGenericLambda())
1005 ++CurTemplateDepthTracker;
1006 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001007 T.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00001008 SourceLocation RParenLoc = T.getCloseLocation();
1009 DeclEndLoc = RParenLoc;
Douglas Gregorae7902c2011-08-04 15:30:47 +00001010
Stephen Hines651f13c2014-04-23 16:59:28 -07001011 // GNU-style attributes must be parsed before the mutable specifier to be
1012 // compatible with GCC.
1013 MaybeParseGNUAttributes(Attr, &DeclEndLoc);
1014
Douglas Gregorae7902c2011-08-04 15:30:47 +00001015 // Parse 'mutable'[opt].
1016 SourceLocation MutableLoc;
Stephen Hines651f13c2014-04-23 16:59:28 -07001017 if (TryConsumeToken(tok::kw_mutable, MutableLoc))
Douglas Gregorae7902c2011-08-04 15:30:47 +00001018 DeclEndLoc = MutableLoc;
Douglas Gregorae7902c2011-08-04 15:30:47 +00001019
1020 // Parse exception-specification[opt].
1021 ExceptionSpecificationType ESpecType = EST_None;
1022 SourceRange ESpecRange;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001023 SmallVector<ParsedType, 2> DynamicExceptions;
1024 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregorae7902c2011-08-04 15:30:47 +00001025 ExprResult NoexceptExpr;
Richard Smitha058fd42012-05-02 22:22:32 +00001026 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001027 DynamicExceptions,
1028 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00001029 NoexceptExpr);
Douglas Gregorae7902c2011-08-04 15:30:47 +00001030
1031 if (ESpecType != EST_None)
1032 DeclEndLoc = ESpecRange.getEnd();
1033
1034 // Parse attribute-specifier[opt].
Richard Smith4e24f0f2013-01-02 12:01:23 +00001035 MaybeParseCXX11Attributes(Attr, &DeclEndLoc);
Douglas Gregorae7902c2011-08-04 15:30:47 +00001036
Abramo Bagnara59c0a812012-10-04 21:42:10 +00001037 SourceLocation FunLocalRangeEnd = DeclEndLoc;
1038
Douglas Gregorae7902c2011-08-04 15:30:47 +00001039 // Parse trailing-return-type[opt].
Richard Smith54655be2012-06-12 01:51:59 +00001040 TypeResult TrailingReturnType;
Douglas Gregorae7902c2011-08-04 15:30:47 +00001041 if (Tok.is(tok::arrow)) {
Abramo Bagnara59c0a812012-10-04 21:42:10 +00001042 FunLocalRangeEnd = Tok.getLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +00001043 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00001044 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregorae7902c2011-08-04 15:30:47 +00001045 if (Range.getEnd().isValid())
1046 DeclEndLoc = Range.getEnd();
1047 }
1048
1049 PrototypeScope.Exit();
1050
Abramo Bagnara59c0a812012-10-04 21:42:10 +00001051 SourceLocation NoLoc;
Douglas Gregorae7902c2011-08-04 15:30:47 +00001052 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
Abramo Bagnara59c0a812012-10-04 21:42:10 +00001053 /*isAmbiguous=*/false,
1054 LParenLoc,
Douglas Gregorae7902c2011-08-04 15:30:47 +00001055 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnara59c0a812012-10-04 21:42:10 +00001056 EllipsisLoc, RParenLoc,
Douglas Gregorae7902c2011-08-04 15:30:47 +00001057 DS.getTypeQualifiers(),
1058 /*RefQualifierIsLValueRef=*/true,
Abramo Bagnara59c0a812012-10-04 21:42:10 +00001059 /*RefQualifierLoc=*/NoLoc,
1060 /*ConstQualifierLoc=*/NoLoc,
1061 /*VolatileQualifierLoc=*/NoLoc,
Douglas Gregorae7902c2011-08-04 15:30:47 +00001062 MutableLoc,
1063 ESpecType, ESpecRange.getBegin(),
1064 DynamicExceptions.data(),
1065 DynamicExceptionRanges.data(),
1066 DynamicExceptions.size(),
1067 NoexceptExpr.isUsable() ?
1068 NoexceptExpr.get() : 0,
Abramo Bagnara59c0a812012-10-04 21:42:10 +00001069 LParenLoc, FunLocalRangeEnd, D,
Douglas Gregorae7902c2011-08-04 15:30:47 +00001070 TrailingReturnType),
1071 Attr, DeclEndLoc);
Stephen Hines651f13c2014-04-23 16:59:28 -07001072 } else if (Tok.is(tok::kw_mutable) || Tok.is(tok::arrow) ||
1073 Tok.is(tok::kw___attribute) ||
1074 (Tok.is(tok::l_square) && NextToken().is(tok::l_square))) {
1075 // It's common to forget that one needs '()' before 'mutable', an attribute
1076 // specifier, or the result type. Deal with this.
1077 unsigned TokKind = 0;
1078 switch (Tok.getKind()) {
1079 case tok::kw_mutable: TokKind = 0; break;
1080 case tok::arrow: TokKind = 1; break;
1081 case tok::kw___attribute:
1082 case tok::l_square: TokKind = 2; break;
1083 default: llvm_unreachable("Unknown token kind");
1084 }
1085
Douglas Gregorc9ecec42012-02-16 21:53:36 +00001086 Diag(Tok, diag::err_lambda_missing_parens)
Stephen Hines651f13c2014-04-23 16:59:28 -07001087 << TokKind
Douglas Gregorc9ecec42012-02-16 21:53:36 +00001088 << FixItHint::CreateInsertion(Tok.getLocation(), "() ");
1089 SourceLocation DeclLoc = Tok.getLocation();
1090 SourceLocation DeclEndLoc = DeclLoc;
Stephen Hines651f13c2014-04-23 16:59:28 -07001091
1092 // GNU-style attributes must be parsed before the mutable specifier to be
1093 // compatible with GCC.
1094 ParsedAttributes Attr(AttrFactory);
1095 MaybeParseGNUAttributes(Attr, &DeclEndLoc);
1096
Douglas Gregorc9ecec42012-02-16 21:53:36 +00001097 // Parse 'mutable', if it's there.
1098 SourceLocation MutableLoc;
1099 if (Tok.is(tok::kw_mutable)) {
1100 MutableLoc = ConsumeToken();
1101 DeclEndLoc = MutableLoc;
1102 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001103
1104 // Parse attribute-specifier[opt].
1105 MaybeParseCXX11Attributes(Attr, &DeclEndLoc);
1106
Douglas Gregorc9ecec42012-02-16 21:53:36 +00001107 // Parse the return type, if there is one.
Richard Smith54655be2012-06-12 01:51:59 +00001108 TypeResult TrailingReturnType;
Douglas Gregorc9ecec42012-02-16 21:53:36 +00001109 if (Tok.is(tok::arrow)) {
1110 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00001111 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregorc9ecec42012-02-16 21:53:36 +00001112 if (Range.getEnd().isValid())
1113 DeclEndLoc = Range.getEnd();
1114 }
1115
Abramo Bagnara59c0a812012-10-04 21:42:10 +00001116 SourceLocation NoLoc;
Douglas Gregorc9ecec42012-02-16 21:53:36 +00001117 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
Abramo Bagnara59c0a812012-10-04 21:42:10 +00001118 /*isAmbiguous=*/false,
1119 /*LParenLoc=*/NoLoc,
1120 /*Params=*/0,
1121 /*NumParams=*/0,
1122 /*EllipsisLoc=*/NoLoc,
1123 /*RParenLoc=*/NoLoc,
1124 /*TypeQuals=*/0,
1125 /*RefQualifierIsLValueRef=*/true,
1126 /*RefQualifierLoc=*/NoLoc,
1127 /*ConstQualifierLoc=*/NoLoc,
1128 /*VolatileQualifierLoc=*/NoLoc,
1129 MutableLoc,
1130 EST_None,
1131 /*ESpecLoc=*/NoLoc,
1132 /*Exceptions=*/0,
1133 /*ExceptionRanges=*/0,
1134 /*NumExceptions=*/0,
1135 /*NoexceptExpr=*/0,
1136 DeclLoc, DeclEndLoc, D,
1137 TrailingReturnType),
Douglas Gregorc9ecec42012-02-16 21:53:36 +00001138 Attr, DeclEndLoc);
Douglas Gregorae7902c2011-08-04 15:30:47 +00001139 }
Douglas Gregorc9ecec42012-02-16 21:53:36 +00001140
Douglas Gregorae7902c2011-08-04 15:30:47 +00001141
Eli Friedman906a7e12012-01-06 03:05:34 +00001142 // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
1143 // it.
Douglas Gregorfccfb622012-02-21 22:51:27 +00001144 unsigned ScopeFlags = Scope::BlockScope | Scope::FnScope | Scope::DeclScope;
Douglas Gregorfccfb622012-02-21 22:51:27 +00001145 ParseScope BodyScope(this, ScopeFlags);
Eli Friedman906a7e12012-01-06 03:05:34 +00001146
Eli Friedmanec9ea722012-01-05 03:35:19 +00001147 Actions.ActOnStartOfLambdaDefinition(Intro, D, getCurScope());
1148
Douglas Gregorae7902c2011-08-04 15:30:47 +00001149 // Parse compound-statement.
Eli Friedmandc3b7232012-01-04 02:40:39 +00001150 if (!Tok.is(tok::l_brace)) {
Douglas Gregorae7902c2011-08-04 15:30:47 +00001151 Diag(Tok, diag::err_expected_lambda_body);
Eli Friedmandc3b7232012-01-04 02:40:39 +00001152 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
1153 return ExprError();
Douglas Gregorae7902c2011-08-04 15:30:47 +00001154 }
1155
Eli Friedmandc3b7232012-01-04 02:40:39 +00001156 StmtResult Stmt(ParseCompoundStatementBody());
1157 BodyScope.Exit();
1158
Eli Friedmandeeab902012-01-04 02:46:53 +00001159 if (!Stmt.isInvalid())
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00001160 return Actions.ActOnLambdaExpr(LambdaBeginLoc, Stmt.take(), getCurScope());
Eli Friedmandc3b7232012-01-04 02:40:39 +00001161
Eli Friedmandeeab902012-01-04 02:46:53 +00001162 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
1163 return ExprError();
Douglas Gregorae7902c2011-08-04 15:30:47 +00001164}
1165
Reid Spencer5f016e22007-07-11 17:01:13 +00001166/// ParseCXXCasts - This handles the various ways to cast expressions to another
1167/// type.
1168///
1169/// postfix-expression: [C++ 5.2p1]
1170/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
1171/// 'static_cast' '<' type-name '>' '(' expression ')'
1172/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
1173/// 'const_cast' '<' type-name '>' '(' expression ')'
1174///
John McCall60d7b3a2010-08-24 06:29:42 +00001175ExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001176 tok::TokenKind Kind = Tok.getKind();
1177 const char *CastName = 0; // For error messages
1178
1179 switch (Kind) {
David Blaikieeb2d1f12011-09-23 20:26:49 +00001180 default: llvm_unreachable("Unknown C++ cast!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001181 case tok::kw_const_cast: CastName = "const_cast"; break;
1182 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
1183 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
1184 case tok::kw_static_cast: CastName = "static_cast"; break;
1185 }
1186
1187 SourceLocation OpLoc = ConsumeToken();
1188 SourceLocation LAngleBracketLoc = Tok.getLocation();
1189
Richard Smithea698b32011-04-14 21:45:45 +00001190 // Check for "<::" which is parsed as "[:". If found, fix token stream,
1191 // diagnose error, suggest fix, and recover parsing.
Richard Smith78fe3e02012-08-20 17:37:52 +00001192 if (Tok.is(tok::l_square) && Tok.getLength() == 2) {
1193 Token Next = NextToken();
1194 if (Next.is(tok::colon) && areTokensAdjacent(Tok, Next))
1195 FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
1196 }
Richard Smithea698b32011-04-14 21:45:45 +00001197
Reid Spencer5f016e22007-07-11 17:01:13 +00001198 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +00001199 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001200
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00001201 // Parse the common declaration-specifiers piece.
1202 DeclSpec DS(AttrFactory);
1203 ParseSpecifierQualifierList(DS);
1204
1205 // Parse the abstract-declarator, if present.
1206 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1207 ParseDeclarator(DeclaratorInfo);
1208
Reid Spencer5f016e22007-07-11 17:01:13 +00001209 SourceLocation RAngleBracketLoc = Tok.getLocation();
1210
Stephen Hines651f13c2014-04-23 16:59:28 -07001211 if (ExpectAndConsume(tok::greater))
1212 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << tok::less);
Reid Spencer5f016e22007-07-11 17:01:13 +00001213
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001214 SourceLocation LParenLoc, RParenLoc;
1215 BalancedDelimiterTracker T(*this, tok::l_paren);
Reid Spencer5f016e22007-07-11 17:01:13 +00001216
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001217 if (T.expectAndConsume(diag::err_expected_lparen_after, CastName))
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +00001218 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001219
John McCall60d7b3a2010-08-24 06:29:42 +00001220 ExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +00001221
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +00001222 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001223 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00001224
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00001225 if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
Douglas Gregor49badde2008-10-27 19:41:14 +00001226 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00001227 LAngleBracketLoc, DeclaratorInfo,
Douglas Gregor809070a2009-02-18 17:45:20 +00001228 RAngleBracketLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001229 T.getOpenLocation(), Result.take(),
1230 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001231
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001232 return Result;
Reid Spencer5f016e22007-07-11 17:01:13 +00001233}
1234
Sebastian Redlc42e1182008-11-11 11:37:55 +00001235/// ParseCXXTypeid - This handles the C++ typeid expression.
1236///
1237/// postfix-expression: [C++ 5.2p1]
1238/// 'typeid' '(' expression ')'
1239/// 'typeid' '(' type-id ')'
1240///
John McCall60d7b3a2010-08-24 06:29:42 +00001241ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +00001242 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
1243
1244 SourceLocation OpLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001245 SourceLocation LParenLoc, RParenLoc;
1246 BalancedDelimiterTracker T(*this, tok::l_paren);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001247
1248 // typeid expressions are always parenthesized.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001249 if (T.expectAndConsume(diag::err_expected_lparen_after, "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +00001250 return ExprError();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001251 LParenLoc = T.getOpenLocation();
Sebastian Redlc42e1182008-11-11 11:37:55 +00001252
John McCall60d7b3a2010-08-24 06:29:42 +00001253 ExprResult Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +00001254
Richard Smith05766812012-08-18 00:55:03 +00001255 // C++0x [expr.typeid]p3:
1256 // When typeid is applied to an expression other than an lvalue of a
1257 // polymorphic class type [...] The expression is an unevaluated
1258 // operand (Clause 5).
1259 //
1260 // Note that we can't tell whether the expression is an lvalue of a
1261 // polymorphic class type until after we've parsed the expression; we
1262 // speculatively assume the subexpression is unevaluated, and fix it up
1263 // later.
1264 //
1265 // We enter the unevaluated context before trying to determine whether we
1266 // have a type-id, because the tentative parse logic will try to resolve
1267 // names, and must treat them as unevaluated.
Eli Friedman80bfa3d2012-09-26 04:34:21 +00001268 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
1269 Sema::ReuseLambdaContextDecl);
Richard Smith05766812012-08-18 00:55:03 +00001270
Sebastian Redlc42e1182008-11-11 11:37:55 +00001271 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +00001272 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +00001273
1274 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001275 T.consumeClose();
1276 RParenLoc = T.getCloseLocation();
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +00001277 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001278 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +00001279
1280 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallb3d87482010-08-24 05:47:05 +00001281 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001282 } else {
1283 Result = ParseExpression();
1284
1285 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001286 if (Result.isInvalid())
Alexey Bataev8fe24752013-11-18 08:17:37 +00001287 SkipUntil(tok::r_paren, StopAtSemi);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001288 else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001289 T.consumeClose();
1290 RParenLoc = T.getCloseLocation();
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +00001291 if (RParenLoc.isInvalid())
1292 return ExprError();
Douglas Gregorfadb53b2011-03-12 01:48:56 +00001293
Sebastian Redlc42e1182008-11-11 11:37:55 +00001294 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +00001295 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001296 }
1297 }
1298
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001299 return Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +00001300}
1301
Francois Pichet01b7c302010-09-08 12:20:18 +00001302/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
1303///
1304/// '__uuidof' '(' expression ')'
1305/// '__uuidof' '(' type-id ')'
1306///
1307ExprResult Parser::ParseCXXUuidof() {
1308 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
1309
1310 SourceLocation OpLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001311 BalancedDelimiterTracker T(*this, tok::l_paren);
Francois Pichet01b7c302010-09-08 12:20:18 +00001312
1313 // __uuidof expressions are always parenthesized.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001314 if (T.expectAndConsume(diag::err_expected_lparen_after, "__uuidof"))
Francois Pichet01b7c302010-09-08 12:20:18 +00001315 return ExprError();
1316
1317 ExprResult Result;
1318
1319 if (isTypeIdInParens()) {
1320 TypeResult Ty = ParseTypeName();
1321
1322 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001323 T.consumeClose();
Francois Pichet01b7c302010-09-08 12:20:18 +00001324
1325 if (Ty.isInvalid())
1326 return ExprError();
1327
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001328 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), /*isType=*/true,
1329 Ty.get().getAsOpaquePtr(),
1330 T.getCloseLocation());
Francois Pichet01b7c302010-09-08 12:20:18 +00001331 } else {
1332 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1333 Result = ParseExpression();
1334
1335 // Match the ')'.
1336 if (Result.isInvalid())
Alexey Bataev8fe24752013-11-18 08:17:37 +00001337 SkipUntil(tok::r_paren, StopAtSemi);
Francois Pichet01b7c302010-09-08 12:20:18 +00001338 else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001339 T.consumeClose();
Francois Pichet01b7c302010-09-08 12:20:18 +00001340
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001341 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(),
1342 /*isType=*/false,
1343 Result.release(), T.getCloseLocation());
Francois Pichet01b7c302010-09-08 12:20:18 +00001344 }
1345 }
1346
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001347 return Result;
Francois Pichet01b7c302010-09-08 12:20:18 +00001348}
1349
Douglas Gregord4dca082010-02-24 18:44:31 +00001350/// \brief Parse a C++ pseudo-destructor expression after the base,
1351/// . or -> operator, and nested-name-specifier have already been
1352/// parsed.
1353///
1354/// postfix-expression: [C++ 5.2]
1355/// postfix-expression . pseudo-destructor-name
1356/// postfix-expression -> pseudo-destructor-name
1357///
1358/// pseudo-destructor-name:
1359/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
1360/// ::[opt] nested-name-specifier template simple-template-id ::
1361/// ~type-name
1362/// ::[opt] nested-name-specifier[opt] ~type-name
1363///
John McCall60d7b3a2010-08-24 06:29:42 +00001364ExprResult
Douglas Gregord4dca082010-02-24 18:44:31 +00001365Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
1366 tok::TokenKind OpKind,
1367 CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +00001368 ParsedType ObjectType) {
Douglas Gregord4dca082010-02-24 18:44:31 +00001369 // We're parsing either a pseudo-destructor-name or a dependent
1370 // member access that has the same form as a
1371 // pseudo-destructor-name. We parse both in the same way and let
1372 // the action model sort them out.
1373 //
1374 // Note that the ::[opt] nested-name-specifier[opt] has already
1375 // been parsed, and if there was a simple-template-id, it has
1376 // been coalesced into a template-id annotation token.
1377 UnqualifiedId FirstTypeName;
1378 SourceLocation CCLoc;
1379 if (Tok.is(tok::identifier)) {
1380 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1381 ConsumeToken();
1382 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1383 CCLoc = ConsumeToken();
1384 } else if (Tok.is(tok::annot_template_id)) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001385 // FIXME: retrieve TemplateKWLoc from template-id annotation and
1386 // store it in the pseudo-dtor node (to be used when instantiating it).
Douglas Gregord4dca082010-02-24 18:44:31 +00001387 FirstTypeName.setTemplateId(
1388 (TemplateIdAnnotation *)Tok.getAnnotationValue());
1389 ConsumeToken();
1390 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1391 CCLoc = ConsumeToken();
1392 } else {
1393 FirstTypeName.setIdentifier(0, SourceLocation());
1394 }
1395
1396 // Parse the tilde.
1397 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
1398 SourceLocation TildeLoc = ConsumeToken();
David Blaikie91ec7892011-12-16 16:03:09 +00001399
1400 if (Tok.is(tok::kw_decltype) && !FirstTypeName.isValid() && SS.isEmpty()) {
1401 DeclSpec DS(AttrFactory);
Benjamin Kramer85c60db2011-12-18 12:18:02 +00001402 ParseDecltypeSpecifier(DS);
David Blaikie91ec7892011-12-16 16:03:09 +00001403 if (DS.getTypeSpecType() == TST_error)
1404 return ExprError();
1405 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, OpLoc,
1406 OpKind, TildeLoc, DS,
1407 Tok.is(tok::l_paren));
1408 }
1409
Douglas Gregord4dca082010-02-24 18:44:31 +00001410 if (!Tok.is(tok::identifier)) {
1411 Diag(Tok, diag::err_destructor_tilde_identifier);
1412 return ExprError();
1413 }
1414
1415 // Parse the second type.
1416 UnqualifiedId SecondTypeName;
1417 IdentifierInfo *Name = Tok.getIdentifierInfo();
1418 SourceLocation NameLoc = ConsumeToken();
1419 SecondTypeName.setIdentifier(Name, NameLoc);
1420
1421 // If there is a '<', the second type name is a template-id. Parse
1422 // it as such.
1423 if (Tok.is(tok::less) &&
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001424 ParseUnqualifiedIdTemplateId(SS, SourceLocation(),
1425 Name, NameLoc,
1426 false, ObjectType, SecondTypeName,
1427 /*AssumeTemplateName=*/true))
Douglas Gregord4dca082010-02-24 18:44:31 +00001428 return ExprError();
1429
John McCall9ae2f072010-08-23 23:25:46 +00001430 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
1431 OpLoc, OpKind,
Douglas Gregord4dca082010-02-24 18:44:31 +00001432 SS, FirstTypeName, CCLoc,
1433 TildeLoc, SecondTypeName,
1434 Tok.is(tok::l_paren));
1435}
1436
Reid Spencer5f016e22007-07-11 17:01:13 +00001437/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
1438///
1439/// boolean-literal: [C++ 2.13.5]
1440/// 'true'
1441/// 'false'
John McCall60d7b3a2010-08-24 06:29:42 +00001442ExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001443 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001444 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +00001445}
Chris Lattner50dd2892008-02-26 00:51:44 +00001446
1447/// ParseThrowExpression - This handles the C++ throw expression.
1448///
1449/// throw-expression: [C++ 15]
1450/// 'throw' assignment-expression[opt]
John McCall60d7b3a2010-08-24 06:29:42 +00001451ExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +00001452 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +00001453 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +00001454
Chris Lattner2a2819a2008-04-06 06:02:23 +00001455 // If the current token isn't the start of an assignment-expression,
1456 // then the expression is not present. This handles things like:
1457 // "C ? throw : (void)42", which is crazy but legal.
1458 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
1459 case tok::semi:
1460 case tok::r_paren:
1461 case tok::r_square:
1462 case tok::r_brace:
1463 case tok::colon:
1464 case tok::comma:
Douglas Gregorbca01b42011-07-06 22:04:06 +00001465 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0);
Chris Lattner50dd2892008-02-26 00:51:44 +00001466
Chris Lattner2a2819a2008-04-06 06:02:23 +00001467 default:
John McCall60d7b3a2010-08-24 06:29:42 +00001468 ExprResult Expr(ParseAssignmentExpression());
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001469 if (Expr.isInvalid()) return Expr;
Douglas Gregorbca01b42011-07-06 22:04:06 +00001470 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take());
Chris Lattner2a2819a2008-04-06 06:02:23 +00001471 }
Chris Lattner50dd2892008-02-26 00:51:44 +00001472}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001473
1474/// ParseCXXThis - This handles the C++ 'this' pointer.
1475///
1476/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
1477/// a non-lvalue expression whose value is the address of the object for which
1478/// the function is called.
John McCall60d7b3a2010-08-24 06:29:42 +00001479ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001480 assert(Tok.is(tok::kw_this) && "Not 'this'!");
1481 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001482 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001483}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001484
1485/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1486/// Can be interpreted either as function-style casting ("int(x)")
1487/// or class type construction ("ClassType(x,y,z)")
1488/// or creation of a value-initialized type ("int()").
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001489/// See [C++ 5.2.3].
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001490///
1491/// postfix-expression: [C++ 5.2p1]
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001492/// simple-type-specifier '(' expression-list[opt] ')'
1493/// [C++0x] simple-type-specifier braced-init-list
1494/// typename-specifier '(' expression-list[opt] ')'
1495/// [C++0x] typename-specifier braced-init-list
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001496///
John McCall60d7b3a2010-08-24 06:29:42 +00001497ExprResult
Sebastian Redl20df9b72008-12-11 22:51:44 +00001498Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001499 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallb3d87482010-08-24 05:47:05 +00001500 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001501
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001502 assert((Tok.is(tok::l_paren) ||
Richard Smith80ad52f2013-01-02 11:42:31 +00001503 (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)))
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001504 && "Expected '(' or '{'!");
Douglas Gregorbc61bd82011-01-11 00:33:19 +00001505
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001506 if (Tok.is(tok::l_brace)) {
Sebastian Redl6dc00f62012-02-12 18:41:05 +00001507 ExprResult Init = ParseBraceInitializer();
1508 if (Init.isInvalid())
1509 return Init;
1510 Expr *InitList = Init.take();
1511 return Actions.ActOnCXXTypeConstructExpr(TypeRep, SourceLocation(),
1512 MultiExprArg(&InitList, 1),
1513 SourceLocation());
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001514 } else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001515 BalancedDelimiterTracker T(*this, tok::l_paren);
1516 T.consumeOpen();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001517
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001518 ExprVector Exprs;
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001519 CommaLocsTy CommaLocs;
1520
1521 if (Tok.isNot(tok::r_paren)) {
1522 if (ParseExpressionList(Exprs, CommaLocs)) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00001523 SkipUntil(tok::r_paren, StopAtSemi);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001524 return ExprError();
1525 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001526 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001527
1528 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001529 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001530
1531 // TypeRep could be null, if it references an invalid typedef.
1532 if (!TypeRep)
1533 return ExprError();
1534
1535 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1536 "Unexpected number of commas!");
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001537 return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001538 Exprs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001539 T.getCloseLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001540 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001541}
1542
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001543/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001544///
1545/// condition:
1546/// expression
1547/// type-specifier-seq declarator '=' assignment-expression
Richard Smith0635aa72012-02-22 06:49:09 +00001548/// [C++11] type-specifier-seq declarator '=' initializer-clause
1549/// [C++11] type-specifier-seq declarator braced-init-list
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001550/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
1551/// '=' assignment-expression
1552///
Dmitri Gribenko1ddbd892012-08-24 00:01:24 +00001553/// \param ExprOut if the condition was parsed as an expression, the parsed
1554/// expression.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001555///
Dmitri Gribenko1ddbd892012-08-24 00:01:24 +00001556/// \param DeclOut if the condition was parsed as a declaration, the parsed
1557/// declaration.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001558///
Douglas Gregor586596f2010-05-06 17:25:47 +00001559/// \param Loc The location of the start of the statement that requires this
1560/// condition, e.g., the "for" in a for loop.
1561///
1562/// \param ConvertToBoolean Whether the condition expression should be
1563/// converted to a boolean value.
1564///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001565/// \returns true if there was a parsing, false otherwise.
John McCall60d7b3a2010-08-24 06:29:42 +00001566bool Parser::ParseCXXCondition(ExprResult &ExprOut,
1567 Decl *&DeclOut,
Douglas Gregor586596f2010-05-06 17:25:47 +00001568 SourceLocation Loc,
1569 bool ConvertToBoolean) {
Douglas Gregor01dfea02010-01-10 23:08:15 +00001570 if (Tok.is(tok::code_completion)) {
John McCallf312b1e2010-08-26 23:41:50 +00001571 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001572 cutOffParsing();
1573 return true;
Douglas Gregor01dfea02010-01-10 23:08:15 +00001574 }
1575
Sean Hunt2edf0a22012-06-23 05:07:58 +00001576 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00001577 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00001578
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001579 if (!isCXXConditionDeclaration()) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001580 ProhibitAttributes(attrs);
1581
Douglas Gregor586596f2010-05-06 17:25:47 +00001582 // Parse the expression.
John McCall60d7b3a2010-08-24 06:29:42 +00001583 ExprOut = ParseExpression(); // expression
1584 DeclOut = 0;
1585 if (ExprOut.isInvalid())
Douglas Gregor586596f2010-05-06 17:25:47 +00001586 return true;
1587
1588 // If required, convert to a boolean value.
1589 if (ConvertToBoolean)
John McCall60d7b3a2010-08-24 06:29:42 +00001590 ExprOut
1591 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
1592 return ExprOut.isInvalid();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001593 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001594
1595 // type-specifier-seq
John McCall0b7e6782011-03-24 11:26:52 +00001596 DeclSpec DS(AttrFactory);
Richard Smith6b3d3e52013-02-20 19:22:51 +00001597 DS.takeAttributesFrom(attrs);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001598 ParseSpecifierQualifierList(DS);
1599
1600 // declarator
1601 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
1602 ParseDeclarator(DeclaratorInfo);
1603
1604 // simple-asm-expr[opt]
1605 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00001606 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00001607 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001608 if (AsmLabel.isInvalid()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00001609 SkipUntil(tok::semi, StopAtSemi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001610 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001611 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001612 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001613 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001614 }
1615
1616 // If attributes are present, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001617 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001618
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001619 // Type-check the declaration itself.
John McCall60d7b3a2010-08-24 06:29:42 +00001620 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall7f040a92010-12-24 02:08:15 +00001621 DeclaratorInfo);
John McCall60d7b3a2010-08-24 06:29:42 +00001622 DeclOut = Dcl.get();
1623 ExprOut = ExprError();
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001624
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001625 // '=' assignment-expression
Richard Trieud6c7c672012-01-18 22:54:52 +00001626 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Smith0635aa72012-02-22 06:49:09 +00001627 bool CopyInitialization = isTokenEqualOrEqualTypo();
1628 if (CopyInitialization)
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001629 ConsumeToken();
Richard Smith0635aa72012-02-22 06:49:09 +00001630
1631 ExprResult InitExpr = ExprError();
Richard Smith80ad52f2013-01-02 11:42:31 +00001632 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith0635aa72012-02-22 06:49:09 +00001633 Diag(Tok.getLocation(),
1634 diag::warn_cxx98_compat_generalized_initializer_lists);
1635 InitExpr = ParseBraceInitializer();
1636 } else if (CopyInitialization) {
1637 InitExpr = ParseAssignmentExpression();
1638 } else if (Tok.is(tok::l_paren)) {
1639 // This was probably an attempt to initialize the variable.
1640 SourceLocation LParen = ConsumeParen(), RParen = LParen;
Alexey Bataev8fe24752013-11-18 08:17:37 +00001641 if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch))
Richard Smith0635aa72012-02-22 06:49:09 +00001642 RParen = ConsumeParen();
1643 Diag(DeclOut ? DeclOut->getLocation() : LParen,
1644 diag::err_expected_init_in_condition_lparen)
1645 << SourceRange(LParen, RParen);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001646 } else {
Richard Smith0635aa72012-02-22 06:49:09 +00001647 Diag(DeclOut ? DeclOut->getLocation() : Tok.getLocation(),
1648 diag::err_expected_init_in_condition);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001649 }
Richard Smith0635aa72012-02-22 06:49:09 +00001650
1651 if (!InitExpr.isInvalid())
1652 Actions.AddInitializerToDecl(DeclOut, InitExpr.take(), !CopyInitialization,
Richard Smitha2c36462013-04-26 16:15:35 +00001653 DS.containsPlaceholderType());
Richard Smithdc7a4f52013-04-30 13:56:41 +00001654 else
1655 Actions.ActOnInitializerError(DeclOut);
Richard Smith0635aa72012-02-22 06:49:09 +00001656
Douglas Gregor586596f2010-05-06 17:25:47 +00001657 // FIXME: Build a reference to this declaration? Convert it to bool?
1658 // (This is currently handled by Sema).
Richard Smith483b9f32011-02-21 20:05:19 +00001659
1660 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregor586596f2010-05-06 17:25:47 +00001661
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001662 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001663}
1664
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001665/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1666/// This should only be called when the current token is known to be part of
1667/// simple-type-specifier.
1668///
1669/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001670/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001671/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1672/// char
1673/// wchar_t
1674/// bool
1675/// short
1676/// int
1677/// long
1678/// signed
1679/// unsigned
1680/// float
1681/// double
1682/// void
1683/// [GNU] typeof-specifier
1684/// [C++0x] auto [TODO]
1685///
1686/// type-name:
1687/// class-name
1688/// enum-name
1689/// typedef-name
1690///
1691void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1692 DS.SetRangeStart(Tok.getLocation());
1693 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001694 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001695 SourceLocation Loc = Tok.getLocation();
Stephen Hines651f13c2014-04-23 16:59:28 -07001696 const clang::PrintingPolicy &Policy =
1697 Actions.getASTContext().getPrintingPolicy();
Mike Stump1eb44332009-09-09 15:08:12 +00001698
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001699 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +00001700 case tok::identifier: // foo::bar
1701 case tok::coloncolon: // ::foo::bar
David Blaikieb219cfc2011-09-23 05:06:16 +00001702 llvm_unreachable("Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +00001703 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00001704 llvm_unreachable("Not a simple-type-specifier token!");
Chris Lattner55a7cef2009-01-05 00:13:00 +00001705
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001706 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001707 case tok::annot_typename: {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001708 if (getTypeAnnotation(Tok))
1709 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
Stephen Hines651f13c2014-04-23 16:59:28 -07001710 getTypeAnnotation(Tok), Policy);
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001711 else
1712 DS.SetTypeSpecError();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001713
1714 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1715 ConsumeToken();
1716
1717 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1718 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1719 // Objective-C interface. If we don't have Objective-C or a '<', this is
1720 // just a normal reference to a typedef name.
David Blaikie4e4d0842012-03-11 07:00:24 +00001721 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001722 ParseObjCProtocolQualifiers(DS);
1723
Stephen Hines651f13c2014-04-23 16:59:28 -07001724 DS.Finish(Diags, PP, Policy);
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001725 return;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001726 }
Mike Stump1eb44332009-09-09 15:08:12 +00001727
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001728 // builtin types
1729 case tok::kw_short:
Stephen Hines651f13c2014-04-23 16:59:28 -07001730 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001731 break;
1732 case tok::kw_long:
Stephen Hines651f13c2014-04-23 16:59:28 -07001733 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001734 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00001735 case tok::kw___int64:
Stephen Hines651f13c2014-04-23 16:59:28 -07001736 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID, Policy);
Francois Pichet338d7f72011-04-28 01:59:37 +00001737 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001738 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001739 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001740 break;
1741 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001742 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001743 break;
1744 case tok::kw_void:
Stephen Hines651f13c2014-04-23 16:59:28 -07001745 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001746 break;
1747 case tok::kw_char:
Stephen Hines651f13c2014-04-23 16:59:28 -07001748 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001749 break;
1750 case tok::kw_int:
Stephen Hines651f13c2014-04-23 16:59:28 -07001751 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001752 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00001753 case tok::kw___int128:
Stephen Hines651f13c2014-04-23 16:59:28 -07001754 DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, DiagID, Policy);
Richard Smith5a5a9712012-04-04 06:24:32 +00001755 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001756 case tok::kw_half:
Stephen Hines651f13c2014-04-23 16:59:28 -07001757 DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID, Policy);
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001758 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001759 case tok::kw_float:
Stephen Hines651f13c2014-04-23 16:59:28 -07001760 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001761 break;
1762 case tok::kw_double:
Stephen Hines651f13c2014-04-23 16:59:28 -07001763 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001764 break;
1765 case tok::kw_wchar_t:
Stephen Hines651f13c2014-04-23 16:59:28 -07001766 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001767 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001768 case tok::kw_char16_t:
Stephen Hines651f13c2014-04-23 16:59:28 -07001769 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID, Policy);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001770 break;
1771 case tok::kw_char32_t:
Stephen Hines651f13c2014-04-23 16:59:28 -07001772 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID, Policy);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001773 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001774 case tok::kw_bool:
Stephen Hines651f13c2014-04-23 16:59:28 -07001775 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID, Policy);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001776 break;
David Blaikie5e089fe2012-01-24 05:47:35 +00001777 case tok::annot_decltype:
1778 case tok::kw_decltype:
1779 DS.SetRangeEnd(ParseDecltypeSpecifier(DS));
Stephen Hines651f13c2014-04-23 16:59:28 -07001780 return DS.Finish(Diags, PP, Policy);
Mike Stump1eb44332009-09-09 15:08:12 +00001781
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001782 // GNU typeof support.
1783 case tok::kw_typeof:
1784 ParseTypeofSpecifier(DS);
Stephen Hines651f13c2014-04-23 16:59:28 -07001785 DS.Finish(Diags, PP, Policy);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001786 return;
1787 }
Chris Lattnerb31757b2009-01-06 05:06:21 +00001788 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001789 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1790 else
1791 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001792 ConsumeToken();
Stephen Hines651f13c2014-04-23 16:59:28 -07001793 DS.Finish(Diags, PP, Policy);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001794}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001795
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001796/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1797/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1798/// e.g., "const short int". Note that the DeclSpec is *not* finished
1799/// by parsing the type-specifier-seq, because these sequences are
1800/// typically followed by some form of declarator. Returns true and
1801/// emits diagnostics if this is not a type-specifier-seq, false
1802/// otherwise.
1803///
1804/// type-specifier-seq: [C++ 8.1]
1805/// type-specifier type-specifier-seq[opt]
1806///
1807bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
Richard Smith69730c12012-03-12 07:56:15 +00001808 ParseSpecifierQualifierList(DS, AS_none, DSC_type_specifier);
Stephen Hines651f13c2014-04-23 16:59:28 -07001809 DS.Finish(Diags, PP, Actions.getASTContext().getPrintingPolicy());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001810 return false;
1811}
1812
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001813/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1814/// some form.
1815///
1816/// This routine is invoked when a '<' is encountered after an identifier or
1817/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1818/// whether the unqualified-id is actually a template-id. This routine will
1819/// then parse the template arguments and form the appropriate template-id to
1820/// return to the caller.
1821///
1822/// \param SS the nested-name-specifier that precedes this template-id, if
1823/// we're actually parsing a qualified-id.
1824///
1825/// \param Name for constructor and destructor names, this is the actual
1826/// identifier that may be a template-name.
1827///
1828/// \param NameLoc the location of the class-name in a constructor or
1829/// destructor.
1830///
1831/// \param EnteringContext whether we're entering the scope of the
1832/// nested-name-specifier.
1833///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001834/// \param ObjectType if this unqualified-id occurs within a member access
1835/// expression, the type of the base object whose member is being accessed.
1836///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001837/// \param Id as input, describes the template-name or operator-function-id
1838/// that precedes the '<'. If template arguments were parsed successfully,
1839/// will be updated with the template-id.
1840///
Douglas Gregord4dca082010-02-24 18:44:31 +00001841/// \param AssumeTemplateId When true, this routine will assume that the name
1842/// refers to a template without performing name lookup to verify.
1843///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001844/// \returns true if a parse error occurred, false otherwise.
1845bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001846 SourceLocation TemplateKWLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001847 IdentifierInfo *Name,
1848 SourceLocation NameLoc,
1849 bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001850 ParsedType ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +00001851 UnqualifiedId &Id,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001852 bool AssumeTemplateId) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001853 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1854 "Expected '<' to finish parsing a template-id");
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001855
1856 TemplateTy Template;
1857 TemplateNameKind TNK = TNK_Non_template;
1858 switch (Id.getKind()) {
1859 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +00001860 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +00001861 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +00001862 if (AssumeTemplateId) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001863 TNK = Actions.ActOnDependentTemplateName(getCurScope(), SS, TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001864 Id, ObjectType, EnteringContext,
1865 Template);
1866 if (TNK == TNK_Non_template)
1867 return true;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001868 } else {
1869 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001870 TNK = Actions.isTemplateName(getCurScope(), SS,
1871 TemplateKWLoc.isValid(), Id,
1872 ObjectType, EnteringContext, Template,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001873 MemberOfUnknownSpecialization);
1874
1875 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1876 ObjectType && IsTemplateArgumentList()) {
1877 // We have something like t->getAs<T>(), where getAs is a
1878 // member of an unknown specialization. However, this will only
1879 // parse correctly as a template, so suggest the keyword 'template'
1880 // before 'getAs' and treat this as a dependent template name.
1881 std::string Name;
1882 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1883 Name = Id.Identifier->getName();
1884 else {
1885 Name = "operator ";
1886 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1887 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1888 else
1889 Name += Id.Identifier->getName();
1890 }
1891 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1892 << Name
1893 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001894 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1895 SS, TemplateKWLoc, Id,
1896 ObjectType, EnteringContext,
1897 Template);
Douglas Gregord6ab2322010-06-16 23:00:59 +00001898 if (TNK == TNK_Non_template)
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001899 return true;
1900 }
1901 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001902 break;
1903
Douglas Gregor014e88d2009-11-03 23:16:33 +00001904 case UnqualifiedId::IK_ConstructorName: {
1905 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001906 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001907 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c153532010-08-06 12:11:11 +00001908 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1909 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001910 EnteringContext, Template,
1911 MemberOfUnknownSpecialization);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001912 break;
1913 }
1914
Douglas Gregor014e88d2009-11-03 23:16:33 +00001915 case UnqualifiedId::IK_DestructorName: {
1916 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001917 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001918 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001919 if (ObjectType) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001920 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1921 SS, TemplateKWLoc, TemplateName,
1922 ObjectType, EnteringContext,
1923 Template);
Douglas Gregord6ab2322010-06-16 23:00:59 +00001924 if (TNK == TNK_Non_template)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001925 return true;
1926 } else {
Abramo Bagnara7c153532010-08-06 12:11:11 +00001927 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1928 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001929 EnteringContext, Template,
1930 MemberOfUnknownSpecialization);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001931
John McCallb3d87482010-08-24 05:47:05 +00001932 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001933 Diag(NameLoc, diag::err_destructor_template_id)
1934 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001935 return true;
1936 }
1937 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001938 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001939 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001940
1941 default:
1942 return false;
1943 }
1944
1945 if (TNK == TNK_Non_template)
1946 return false;
1947
1948 // Parse the enclosed template argument list.
1949 SourceLocation LAngleLoc, RAngleLoc;
1950 TemplateArgList TemplateArgs;
Douglas Gregor0278e122010-05-05 05:58:24 +00001951 if (Tok.is(tok::less) &&
1952 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor059101f2011-03-02 00:47:37 +00001953 SS, true, LAngleLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001954 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001955 RAngleLoc))
1956 return true;
1957
1958 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001959 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1960 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001961 // Form a parsed representation of the template-id to be stored in the
1962 // UnqualifiedId.
1963 TemplateIdAnnotation *TemplateId
Benjamin Kramer13bb7012012-04-14 12:14:03 +00001964 = TemplateIdAnnotation::Allocate(TemplateArgs.size(), TemplateIds);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001965
Stephen Hines651f13c2014-04-23 16:59:28 -07001966 // FIXME: Store name for literal operator too.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001967 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1968 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001969 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001970 TemplateId->TemplateNameLoc = Id.StartLocation;
1971 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001972 TemplateId->Name = 0;
1973 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1974 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001975 }
1976
Douglas Gregor059101f2011-03-02 00:47:37 +00001977 TemplateId->SS = SS;
Benjamin Kramer2b28bf12012-02-19 23:37:39 +00001978 TemplateId->TemplateKWLoc = TemplateKWLoc;
John McCall2b5289b2010-08-23 07:28:44 +00001979 TemplateId->Template = Template;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001980 TemplateId->Kind = TNK;
1981 TemplateId->LAngleLoc = LAngleLoc;
1982 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001983 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001984 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001985 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001986 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001987
1988 Id.setTemplateId(TemplateId);
1989 return false;
1990 }
1991
1992 // Bundle the template arguments together.
Benjamin Kramer5354e772012-08-23 23:38:35 +00001993 ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs);
Abramo Bagnarafad03b72012-01-27 08:46:19 +00001994
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001995 // Constructor and destructor names.
John McCallf312b1e2010-08-26 23:41:50 +00001996 TypeResult Type
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001997 = Actions.ActOnTemplateIdType(SS, TemplateKWLoc,
1998 Template, NameLoc,
Abramo Bagnarafad03b72012-01-27 08:46:19 +00001999 LAngleLoc, TemplateArgsPtr, RAngleLoc,
2000 /*IsCtorOrDtorName=*/true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002001 if (Type.isInvalid())
2002 return true;
2003
2004 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
2005 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
2006 else
2007 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
2008
2009 return false;
2010}
2011
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002012/// \brief Parse an operator-function-id or conversion-function-id as part
2013/// of a C++ unqualified-id.
2014///
2015/// This routine is responsible only for parsing the operator-function-id or
2016/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002017///
2018/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002019/// operator-function-id: [C++ 13.5]
2020/// 'operator' operator
2021///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002022/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002023/// new delete new[] delete[]
2024/// + - * / % ^ & | ~
2025/// ! = < > += -= *= /= %=
2026/// ^= &= |= << >> >>= <<= == !=
2027/// <= >= && || ++ -- , ->* ->
2028/// () []
2029///
2030/// conversion-function-id: [C++ 12.3.2]
2031/// operator conversion-type-id
2032///
2033/// conversion-type-id:
2034/// type-specifier-seq conversion-declarator[opt]
2035///
2036/// conversion-declarator:
2037/// ptr-operator conversion-declarator[opt]
2038/// \endcode
2039///
Dmitri Gribenko1ddbd892012-08-24 00:01:24 +00002040/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002041/// non-empty, then we are parsing the unqualified-id of a qualified-id.
2042///
2043/// \param EnteringContext whether we are entering the scope of the
2044/// nested-name-specifier.
2045///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002046/// \param ObjectType if this unqualified-id occurs within a member access
2047/// expression, the type of the base object whose member is being accessed.
2048///
2049/// \param Result on a successful parse, contains the parsed unqualified-id.
2050///
2051/// \returns true if parsing fails, false otherwise.
2052bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00002053 ParsedType ObjectType,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002054 UnqualifiedId &Result) {
2055 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
2056
2057 // Consume the 'operator' keyword.
2058 SourceLocation KeywordLoc = ConsumeToken();
2059
2060 // Determine what kind of operator name we have.
2061 unsigned SymbolIdx = 0;
2062 SourceLocation SymbolLocations[3];
2063 OverloadedOperatorKind Op = OO_None;
2064 switch (Tok.getKind()) {
2065 case tok::kw_new:
2066 case tok::kw_delete: {
2067 bool isNew = Tok.getKind() == tok::kw_new;
2068 // Consume the 'new' or 'delete'.
2069 SymbolLocations[SymbolIdx++] = ConsumeToken();
Richard Smith6ee326a2012-04-10 01:32:12 +00002070 // Check for array new/delete.
2071 if (Tok.is(tok::l_square) &&
Richard Smith80ad52f2013-01-02 11:42:31 +00002072 (!getLangOpts().CPlusPlus11 || NextToken().isNot(tok::l_square))) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002073 // Consume the '[' and ']'.
2074 BalancedDelimiterTracker T(*this, tok::l_square);
2075 T.consumeOpen();
2076 T.consumeClose();
2077 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002078 return true;
2079
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002080 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
2081 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002082 Op = isNew? OO_Array_New : OO_Array_Delete;
2083 } else {
2084 Op = isNew? OO_New : OO_Delete;
2085 }
2086 break;
2087 }
2088
2089#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
2090 case tok::Token: \
2091 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
2092 Op = OO_##Name; \
2093 break;
2094#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
2095#include "clang/Basic/OperatorKinds.def"
2096
2097 case tok::l_paren: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002098 // Consume the '(' and ')'.
2099 BalancedDelimiterTracker T(*this, tok::l_paren);
2100 T.consumeOpen();
2101 T.consumeClose();
2102 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002103 return true;
2104
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002105 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
2106 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002107 Op = OO_Call;
2108 break;
2109 }
2110
2111 case tok::l_square: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002112 // Consume the '[' and ']'.
2113 BalancedDelimiterTracker T(*this, tok::l_square);
2114 T.consumeOpen();
2115 T.consumeClose();
2116 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002117 return true;
2118
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002119 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
2120 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002121 Op = OO_Subscript;
2122 break;
2123 }
2124
2125 case tok::code_completion: {
2126 // Code completion for the operator name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002127 Actions.CodeCompleteOperatorName(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002128 cutOffParsing();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002129 // Don't try to parse any further.
2130 return true;
2131 }
2132
2133 default:
2134 break;
2135 }
2136
2137 if (Op != OO_None) {
2138 // We have parsed an operator-function-id.
2139 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
2140 return false;
2141 }
Sean Hunt0486d742009-11-28 04:44:28 +00002142
2143 // Parse a literal-operator-id.
2144 //
Richard Smithaa9a8ce2012-10-20 08:41:10 +00002145 // literal-operator-id: C++11 [over.literal]
2146 // operator string-literal identifier
2147 // operator user-defined-string-literal
Sean Hunt0486d742009-11-28 04:44:28 +00002148
Richard Smith80ad52f2013-01-02 11:42:31 +00002149 if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
Richard Smith7fe62082011-10-15 05:09:34 +00002150 Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator);
Sean Hunt0486d742009-11-28 04:44:28 +00002151
Richard Smith33762772012-03-08 23:06:02 +00002152 SourceLocation DiagLoc;
2153 unsigned DiagId = 0;
2154
2155 // We're past translation phase 6, so perform string literal concatenation
2156 // before checking for "".
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002157 SmallVector<Token, 4> Toks;
2158 SmallVector<SourceLocation, 4> TokLocs;
Richard Smith33762772012-03-08 23:06:02 +00002159 while (isTokenStringLiteral()) {
2160 if (!Tok.is(tok::string_literal) && !DiagId) {
Richard Smithaa9a8ce2012-10-20 08:41:10 +00002161 // C++11 [over.literal]p1:
2162 // The string-literal or user-defined-string-literal in a
2163 // literal-operator-id shall have no encoding-prefix [...].
Richard Smith33762772012-03-08 23:06:02 +00002164 DiagLoc = Tok.getLocation();
2165 DiagId = diag::err_literal_operator_string_prefix;
2166 }
2167 Toks.push_back(Tok);
2168 TokLocs.push_back(ConsumeStringToken());
2169 }
2170
2171 StringLiteralParser Literal(Toks.data(), Toks.size(), PP);
2172 if (Literal.hadError)
2173 return true;
2174
2175 // Grab the literal operator's suffix, which will be either the next token
2176 // or a ud-suffix from the string literal.
2177 IdentifierInfo *II = 0;
2178 SourceLocation SuffixLoc;
2179 if (!Literal.getUDSuffix().empty()) {
2180 II = &PP.getIdentifierTable().get(Literal.getUDSuffix());
2181 SuffixLoc =
2182 Lexer::AdvanceToTokenCharacter(TokLocs[Literal.getUDSuffixToken()],
2183 Literal.getUDSuffixOffset(),
David Blaikie4e4d0842012-03-11 07:00:24 +00002184 PP.getSourceManager(), getLangOpts());
Richard Smith33762772012-03-08 23:06:02 +00002185 } else if (Tok.is(tok::identifier)) {
2186 II = Tok.getIdentifierInfo();
2187 SuffixLoc = ConsumeToken();
2188 TokLocs.push_back(SuffixLoc);
2189 } else {
Stephen Hines651f13c2014-04-23 16:59:28 -07002190 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
Sean Hunt0486d742009-11-28 04:44:28 +00002191 return true;
2192 }
2193
Richard Smith33762772012-03-08 23:06:02 +00002194 // The string literal must be empty.
2195 if (!Literal.GetString().empty() || Literal.Pascal) {
Richard Smithaa9a8ce2012-10-20 08:41:10 +00002196 // C++11 [over.literal]p1:
2197 // The string-literal or user-defined-string-literal in a
2198 // literal-operator-id shall [...] contain no characters
2199 // other than the implicit terminating '\0'.
Richard Smith33762772012-03-08 23:06:02 +00002200 DiagLoc = TokLocs.front();
2201 DiagId = diag::err_literal_operator_string_not_empty;
2202 }
2203
2204 if (DiagId) {
2205 // This isn't a valid literal-operator-id, but we think we know
2206 // what the user meant. Tell them what they should have written.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002207 SmallString<32> Str;
Richard Smith33762772012-03-08 23:06:02 +00002208 Str += "\"\" ";
2209 Str += II->getName();
2210 Diag(DiagLoc, DiagId) << FixItHint::CreateReplacement(
2211 SourceRange(TokLocs.front(), TokLocs.back()), Str);
2212 }
2213
2214 Result.setLiteralOperatorId(II, KeywordLoc, SuffixLoc);
Stephen Hines651f13c2014-04-23 16:59:28 -07002215
2216 return Actions.checkLiteralOperatorId(SS, Result);
Sean Hunt0486d742009-11-28 04:44:28 +00002217 }
Stephen Hines651f13c2014-04-23 16:59:28 -07002218
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002219 // Parse a conversion-function-id.
2220 //
2221 // conversion-function-id: [C++ 12.3.2]
2222 // operator conversion-type-id
2223 //
2224 // conversion-type-id:
2225 // type-specifier-seq conversion-declarator[opt]
2226 //
2227 // conversion-declarator:
2228 // ptr-operator conversion-declarator[opt]
2229
2230 // Parse the type-specifier-seq.
John McCall0b7e6782011-03-24 11:26:52 +00002231 DeclSpec DS(AttrFactory);
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00002232 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002233 return true;
2234
2235 // Parse the conversion-declarator, which is merely a sequence of
2236 // ptr-operators.
Richard Smith14f78f42013-05-04 01:26:46 +00002237 Declarator D(DS, Declarator::ConversionIdContext);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002238 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
2239
2240 // Finish up the type.
John McCallf312b1e2010-08-26 23:41:50 +00002241 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002242 if (Ty.isInvalid())
2243 return true;
2244
2245 // Note that this is a conversion-function-id.
2246 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
2247 D.getSourceRange().getEnd());
2248 return false;
2249}
2250
2251/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
2252/// name of an entity.
2253///
2254/// \code
2255/// unqualified-id: [C++ expr.prim.general]
2256/// identifier
2257/// operator-function-id
2258/// conversion-function-id
2259/// [C++0x] literal-operator-id [TODO]
2260/// ~ class-name
2261/// template-id
2262///
2263/// \endcode
2264///
Dmitri Gribenko1ddbd892012-08-24 00:01:24 +00002265/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002266/// non-empty, then we are parsing the unqualified-id of a qualified-id.
2267///
2268/// \param EnteringContext whether we are entering the scope of the
2269/// nested-name-specifier.
2270///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002271/// \param AllowDestructorName whether we allow parsing of a destructor name.
2272///
2273/// \param AllowConstructorName whether we allow parsing a constructor name.
2274///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00002275/// \param ObjectType if this unqualified-id occurs within a member access
2276/// expression, the type of the base object whose member is being accessed.
2277///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002278/// \param Result on a successful parse, contains the parsed unqualified-id.
2279///
2280/// \returns true if parsing fails, false otherwise.
2281bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
2282 bool AllowDestructorName,
2283 bool AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00002284 ParsedType ObjectType,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002285 SourceLocation& TemplateKWLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002286 UnqualifiedId &Result) {
Douglas Gregor0278e122010-05-05 05:58:24 +00002287
2288 // Handle 'A::template B'. This is for template-ids which have not
2289 // already been annotated by ParseOptionalCXXScopeSpecifier().
2290 bool TemplateSpecified = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00002291 if (getLangOpts().CPlusPlus && Tok.is(tok::kw_template) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00002292 (ObjectType || SS.isSet())) {
2293 TemplateSpecified = true;
2294 TemplateKWLoc = ConsumeToken();
2295 }
2296
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002297 // unqualified-id:
2298 // identifier
2299 // template-id (when it hasn't already been annotated)
2300 if (Tok.is(tok::identifier)) {
2301 // Consume the identifier.
2302 IdentifierInfo *Id = Tok.getIdentifierInfo();
2303 SourceLocation IdLoc = ConsumeToken();
2304
David Blaikie4e4d0842012-03-11 07:00:24 +00002305 if (!getLangOpts().CPlusPlus) {
Douglas Gregorb862b8f2010-01-11 23:29:10 +00002306 // If we're not in C++, only identifiers matter. Record the
2307 // identifier and return.
2308 Result.setIdentifier(Id, IdLoc);
2309 return false;
2310 }
2311
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002312 if (AllowConstructorName &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002313 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002314 // We have parsed a constructor name.
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002315 ParsedType Ty = Actions.getTypeName(*Id, IdLoc, getCurScope(),
2316 &SS, false, false,
2317 ParsedType(),
2318 /*IsCtorOrDtorName=*/true,
2319 /*NonTrivialTypeSourceInfo=*/true);
2320 Result.setConstructorName(Ty, IdLoc, IdLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002321 } else {
2322 // We have parsed an identifier.
2323 Result.setIdentifier(Id, IdLoc);
2324 }
2325
2326 // If the next token is a '<', we may have a template.
Douglas Gregor0278e122010-05-05 05:58:24 +00002327 if (TemplateSpecified || Tok.is(tok::less))
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002328 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc, Id, IdLoc,
2329 EnteringContext, ObjectType,
2330 Result, TemplateSpecified);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002331
2332 return false;
2333 }
2334
2335 // unqualified-id:
2336 // template-id (already parsed and annotated)
2337 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002338 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002339
2340 // If the template-name names the current class, then this is a constructor
2341 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002342 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002343 if (SS.isSet()) {
2344 // C++ [class.qual]p2 specifies that a qualified template-name
2345 // is taken as the constructor name where a constructor can be
2346 // declared. Thus, the template arguments are extraneous, so
2347 // complain about them and remove them entirely.
2348 Diag(TemplateId->TemplateNameLoc,
2349 diag::err_out_of_line_constructor_template_id)
2350 << TemplateId->Name
Douglas Gregor849b2432010-03-31 17:46:05 +00002351 << FixItHint::CreateRemoval(
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002352 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002353 ParsedType Ty = Actions.getTypeName(*TemplateId->Name,
2354 TemplateId->TemplateNameLoc,
2355 getCurScope(),
2356 &SS, false, false,
2357 ParsedType(),
2358 /*IsCtorOrDtorName=*/true,
2359 /*NontrivialTypeSourceInfo=*/true);
2360 Result.setConstructorName(Ty, TemplateId->TemplateNameLoc,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002361 TemplateId->RAngleLoc);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002362 ConsumeToken();
2363 return false;
2364 }
2365
2366 Result.setConstructorTemplateId(TemplateId);
2367 ConsumeToken();
2368 return false;
2369 }
2370
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002371 // We have already parsed a template-id; consume the annotation token as
2372 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002373 Result.setTemplateId(TemplateId);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002374 TemplateKWLoc = TemplateId->TemplateKWLoc;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002375 ConsumeToken();
2376 return false;
2377 }
2378
2379 // unqualified-id:
2380 // operator-function-id
2381 // conversion-function-id
2382 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002383 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002384 return true;
2385
Sean Hunte6252d12009-11-28 08:58:14 +00002386 // If we have an operator-function-id or a literal-operator-id and the next
2387 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002388 //
2389 // template-id:
2390 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00002391 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
2392 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00002393 (TemplateSpecified || Tok.is(tok::less)))
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002394 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2395 0, SourceLocation(),
2396 EnteringContext, ObjectType,
2397 Result, TemplateSpecified);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002398
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002399 return false;
2400 }
2401
David Blaikie4e4d0842012-03-11 07:00:24 +00002402 if (getLangOpts().CPlusPlus &&
Douglas Gregorb862b8f2010-01-11 23:29:10 +00002403 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002404 // C++ [expr.unary.op]p10:
2405 // There is an ambiguity in the unary-expression ~X(), where X is a
2406 // class-name. The ambiguity is resolved in favor of treating ~ as a
2407 // unary complement rather than treating ~X as referring to a destructor.
2408
2409 // Parse the '~'.
2410 SourceLocation TildeLoc = ConsumeToken();
David Blaikie53a75c02011-12-08 16:13:53 +00002411
2412 if (SS.isEmpty() && Tok.is(tok::kw_decltype)) {
2413 DeclSpec DS(AttrFactory);
2414 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
2415 if (ParsedType Type = Actions.getDestructorType(DS, ObjectType)) {
2416 Result.setDestructorName(TildeLoc, Type, EndLoc);
2417 return false;
2418 }
2419 return true;
2420 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002421
2422 // Parse the class-name.
2423 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00002424 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002425 return true;
2426 }
2427
2428 // Parse the class-name (or template-name in a simple-template-id).
2429 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
2430 SourceLocation ClassNameLoc = ConsumeToken();
2431
Douglas Gregor0278e122010-05-05 05:58:24 +00002432 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallb3d87482010-08-24 05:47:05 +00002433 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002434 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2435 ClassName, ClassNameLoc,
2436 EnteringContext, ObjectType,
2437 Result, TemplateSpecified);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002438 }
2439
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002440 // Note that this is a destructor name.
John McCallb3d87482010-08-24 05:47:05 +00002441 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
2442 ClassNameLoc, getCurScope(),
2443 SS, ObjectType,
2444 EnteringContext);
Douglas Gregor124b8782010-02-16 19:09:40 +00002445 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002446 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00002447
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002448 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002449 return false;
2450 }
2451
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002452 Diag(Tok, diag::err_expected_unqualified_id)
David Blaikie4e4d0842012-03-11 07:00:24 +00002453 << getLangOpts().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002454 return true;
2455}
2456
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002457/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
2458/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00002459///
Chris Lattner59232d32009-01-04 21:25:24 +00002460/// This method is called to parse the new expression after the optional :: has
2461/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
2462/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002463///
2464/// new-expression:
2465/// '::'[opt] 'new' new-placement[opt] new-type-id
2466/// new-initializer[opt]
2467/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2468/// new-initializer[opt]
2469///
2470/// new-placement:
2471/// '(' expression-list ')'
2472///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002473/// new-type-id:
2474/// type-specifier-seq new-declarator[opt]
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002475/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002476///
2477/// new-declarator:
2478/// ptr-operator new-declarator[opt]
2479/// direct-new-declarator
2480///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002481/// new-initializer:
2482/// '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002483/// [C++0x] braced-init-list
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002484///
John McCall60d7b3a2010-08-24 06:29:42 +00002485ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002486Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
2487 assert(Tok.is(tok::kw_new) && "expected 'new' token");
2488 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002489
2490 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
2491 // second form of new-expression. It can't be a new-type-id.
2492
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002493 ExprVector PlacementArgs;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002494 SourceLocation PlacementLParen, PlacementRParen;
2495
Douglas Gregor4bd40312010-07-13 15:54:32 +00002496 SourceRange TypeIdParens;
John McCall0b7e6782011-03-24 11:26:52 +00002497 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis0b8c98f2011-06-28 03:01:23 +00002498 Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002499 if (Tok.is(tok::l_paren)) {
2500 // If it turns out to be a placement, we change the type location.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002501 BalancedDelimiterTracker T(*this, tok::l_paren);
2502 T.consumeOpen();
2503 PlacementLParen = T.getOpenLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002504 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00002505 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002506 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002507 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002508
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002509 T.consumeClose();
2510 PlacementRParen = T.getCloseLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002511 if (PlacementRParen.isInvalid()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00002512 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002513 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002514 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002515
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002516 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002517 // Reset the placement locations. There was no placement.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002518 TypeIdParens = T.getRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002519 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002520 } else {
2521 // We still need the type.
2522 if (Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002523 BalancedDelimiterTracker T(*this, tok::l_paren);
2524 T.consumeOpen();
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002525 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002526 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002527 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002528 ParseDeclarator(DeclaratorInfo);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002529 T.consumeClose();
2530 TypeIdParens = T.getRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002531 } else {
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002532 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002533 if (ParseCXXTypeSpecifierSeq(DS))
2534 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002535 else {
2536 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002537 ParseDeclaratorInternal(DeclaratorInfo,
2538 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002539 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002540 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002541 }
2542 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002543 // A new-type-id is a simplified type-id, where essentially the
2544 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002545 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002546 if (ParseCXXTypeSpecifierSeq(DS))
2547 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002548 else {
2549 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002550 ParseDeclaratorInternal(DeclaratorInfo,
2551 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002552 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002553 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00002554 if (DeclaratorInfo.isInvalidType()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00002555 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002556 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002557 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002558
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002559 ExprResult Initializer;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002560
2561 if (Tok.is(tok::l_paren)) {
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002562 SourceLocation ConstructorLParen, ConstructorRParen;
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002563 ExprVector ConstructorArgs;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002564 BalancedDelimiterTracker T(*this, tok::l_paren);
2565 T.consumeOpen();
2566 ConstructorLParen = T.getOpenLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002567 if (Tok.isNot(tok::r_paren)) {
2568 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002569 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00002570 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002571 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002572 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002573 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002574 T.consumeClose();
2575 ConstructorRParen = T.getCloseLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002576 if (ConstructorRParen.isInvalid()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00002577 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002578 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002579 }
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002580 Initializer = Actions.ActOnParenListExpr(ConstructorLParen,
2581 ConstructorRParen,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002582 ConstructorArgs);
Richard Smith80ad52f2013-01-02 11:42:31 +00002583 } else if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus11) {
Richard Smith7fe62082011-10-15 05:09:34 +00002584 Diag(Tok.getLocation(),
2585 diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002586 Initializer = ParseBraceInitializer();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002587 }
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002588 if (Initializer.isInvalid())
2589 return Initializer;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002590
Sebastian Redlf53597f2009-03-15 17:47:39 +00002591 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002592 PlacementArgs, PlacementRParen,
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002593 TypeIdParens, DeclaratorInfo, Initializer.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002594}
2595
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002596/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
2597/// passed to ParseDeclaratorInternal.
2598///
2599/// direct-new-declarator:
2600/// '[' expression ']'
2601/// direct-new-declarator '[' constant-expression ']'
2602///
Chris Lattner59232d32009-01-04 21:25:24 +00002603void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002604 // Parse the array dimensions.
2605 bool first = true;
2606 while (Tok.is(tok::l_square)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00002607 // An array-size expression can't start with a lambda.
2608 if (CheckProhibitedCXX11Attribute())
2609 continue;
2610
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002611 BalancedDelimiterTracker T(*this, tok::l_square);
2612 T.consumeOpen();
2613
John McCall60d7b3a2010-08-24 06:29:42 +00002614 ExprResult Size(first ? ParseExpression()
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002615 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002616 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002617 // Recover
Alexey Bataev8fe24752013-11-18 08:17:37 +00002618 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002619 return;
2620 }
2621 first = false;
2622
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002623 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00002624
Bill Wendlingad017fa2012-12-20 19:22:21 +00002625 // Attributes here appertain to the array type. C++11 [expr.new]p5.
Richard Smith6ee326a2012-04-10 01:32:12 +00002626 ParsedAttributes Attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00002627 MaybeParseCXX11Attributes(Attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00002628
John McCall0b7e6782011-03-24 11:26:52 +00002629 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall7f040a92010-12-24 02:08:15 +00002630 /*static=*/false, /*star=*/false,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002631 Size.release(),
2632 T.getOpenLocation(),
2633 T.getCloseLocation()),
Richard Smith6ee326a2012-04-10 01:32:12 +00002634 Attrs, T.getCloseLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002635
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002636 if (T.getCloseLocation().isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002637 return;
2638 }
2639}
2640
2641/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
2642/// This ambiguity appears in the syntax of the C++ new operator.
2643///
2644/// new-expression:
2645/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2646/// new-initializer[opt]
2647///
2648/// new-placement:
2649/// '(' expression-list ')'
2650///
John McCallca0408f2010-08-23 06:44:23 +00002651bool Parser::ParseExpressionListOrTypeId(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002652 SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00002653 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002654 // The '(' was already consumed.
2655 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002656 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00002657 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002658 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00002659 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002660 }
2661
2662 // It's not a type, it has to be an expression list.
2663 // Discard the comma locations - ActOnCXXNew has enough parameters.
2664 CommaLocsTy CommaLocs;
2665 return ParseExpressionList(PlacementArgs, CommaLocs);
2666}
2667
2668/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
2669/// to free memory allocated by new.
2670///
Chris Lattner59232d32009-01-04 21:25:24 +00002671/// This method is called to parse the 'delete' expression after the optional
2672/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
2673/// and "Start" is its location. Otherwise, "Start" is the location of the
2674/// 'delete' token.
2675///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002676/// delete-expression:
2677/// '::'[opt] 'delete' cast-expression
2678/// '::'[opt] 'delete' '[' ']' cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00002679ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002680Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
2681 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
2682 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002683
2684 // Array delete?
2685 bool ArrayDelete = false;
Richard Smith6ee326a2012-04-10 01:32:12 +00002686 if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
Richard Smith950435c2012-08-09 19:01:51 +00002687 // C++11 [expr.delete]p1:
2688 // Whenever the delete keyword is followed by empty square brackets, it
2689 // shall be interpreted as [array delete].
2690 // [Footnote: A lambda expression with a lambda-introducer that consists
2691 // of empty square brackets can follow the delete keyword if
2692 // the lambda expression is enclosed in parentheses.]
2693 // FIXME: Produce a better diagnostic if the '[]' is unambiguously a
2694 // lambda-introducer.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002695 ArrayDelete = true;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002696 BalancedDelimiterTracker T(*this, tok::l_square);
2697
2698 T.consumeOpen();
2699 T.consumeClose();
2700 if (T.getCloseLocation().isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00002701 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002702 }
2703
John McCall60d7b3a2010-08-24 06:29:42 +00002704 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002705 if (Operand.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002706 return Operand;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002707
John McCall9ae2f072010-08-23 23:25:46 +00002708 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002709}
Sebastian Redl64b45f72009-01-05 20:52:13 +00002710
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002711static TypeTrait TypeTraitFromTokKind(tok::TokenKind kind) {
2712 switch (kind) {
2713 default: llvm_unreachable("Not a known type trait");
Stephen Hines651f13c2014-04-23 16:59:28 -07002714#define TYPE_TRAIT_1(Spelling, Name, Key) \
2715case tok::kw_ ## Spelling: return UTT_ ## Name;
2716#define TYPE_TRAIT_2(Spelling, Name, Key) \
2717case tok::kw_ ## Spelling: return BTT_ ## Name;
2718#include "clang/Basic/TokenKinds.def"
2719#define TYPE_TRAIT_N(Spelling, Name, Key) \
2720 case tok::kw_ ## Spelling: return TT_ ## Name;
2721#include "clang/Basic/TokenKinds.def"
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002722 }
2723}
2724
John Wiegley21ff2e52011-04-28 00:16:57 +00002725static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
2726 switch(kind) {
2727 default: llvm_unreachable("Not a known binary type trait");
2728 case tok::kw___array_rank: return ATT_ArrayRank;
2729 case tok::kw___array_extent: return ATT_ArrayExtent;
2730 }
2731}
2732
John Wiegley55262202011-04-25 06:54:41 +00002733static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2734 switch(kind) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002735 default: llvm_unreachable("Not a known unary expression trait.");
John Wiegley55262202011-04-25 06:54:41 +00002736 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
2737 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
2738 }
2739}
2740
Stephen Hines651f13c2014-04-23 16:59:28 -07002741static unsigned TypeTraitArity(tok::TokenKind kind) {
2742 switch (kind) {
2743 default: llvm_unreachable("Not a known type trait");
2744#define TYPE_TRAIT(N,Spelling,K) case tok::kw_##Spelling: return N;
2745#include "clang/Basic/TokenKinds.def"
Francois Pichet6ad6f282010-12-07 00:08:36 +00002746 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00002747}
2748
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002749/// \brief Parse the built-in type-trait pseudo-functions that allow
2750/// implementation of the TR1/C++11 type traits templates.
2751///
2752/// primary-expression:
Stephen Hines651f13c2014-04-23 16:59:28 -07002753/// unary-type-trait '(' type-id ')'
2754/// binary-type-trait '(' type-id ',' type-id ')'
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002755/// type-trait '(' type-id-seq ')'
2756///
2757/// type-id-seq:
2758/// type-id ...[opt] type-id-seq[opt]
2759///
2760ExprResult Parser::ParseTypeTrait() {
Stephen Hines651f13c2014-04-23 16:59:28 -07002761 tok::TokenKind Kind = Tok.getKind();
2762 unsigned Arity = TypeTraitArity(Kind);
2763
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002764 SourceLocation Loc = ConsumeToken();
2765
2766 BalancedDelimiterTracker Parens(*this, tok::l_paren);
Stephen Hines651f13c2014-04-23 16:59:28 -07002767 if (Parens.expectAndConsume())
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002768 return ExprError();
2769
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002770 SmallVector<ParsedType, 2> Args;
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002771 do {
2772 // Parse the next type.
2773 TypeResult Ty = ParseTypeName();
2774 if (Ty.isInvalid()) {
2775 Parens.skipToEnd();
2776 return ExprError();
2777 }
2778
2779 // Parse the ellipsis, if present.
2780 if (Tok.is(tok::ellipsis)) {
2781 Ty = Actions.ActOnPackExpansion(Ty.get(), ConsumeToken());
2782 if (Ty.isInvalid()) {
2783 Parens.skipToEnd();
2784 return ExprError();
2785 }
2786 }
2787
2788 // Add this type to the list of arguments.
2789 Args.push_back(Ty.get());
Stephen Hines651f13c2014-04-23 16:59:28 -07002790 } while (TryConsumeToken(tok::comma));
2791
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002792 if (Parens.consumeClose())
2793 return ExprError();
Stephen Hines651f13c2014-04-23 16:59:28 -07002794
2795 SourceLocation EndLoc = Parens.getCloseLocation();
2796
2797 if (Arity && Args.size() != Arity) {
2798 Diag(EndLoc, diag::err_type_trait_arity)
2799 << Arity << 0 << (Arity > 1) << (int)Args.size() << SourceRange(Loc);
2800 return ExprError();
2801 }
2802
2803 if (!Arity && Args.empty()) {
2804 Diag(EndLoc, diag::err_type_trait_arity)
2805 << 1 << 1 << 1 << (int)Args.size() << SourceRange(Loc);
2806 return ExprError();
2807 }
2808
2809 return Actions.ActOnTypeTrait(TypeTraitFromTokKind(Kind), Loc, Args, EndLoc);
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002810}
2811
John Wiegley21ff2e52011-04-28 00:16:57 +00002812/// ParseArrayTypeTrait - Parse the built-in array type-trait
2813/// pseudo-functions.
2814///
2815/// primary-expression:
2816/// [Embarcadero] '__array_rank' '(' type-id ')'
2817/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
2818///
2819ExprResult Parser::ParseArrayTypeTrait() {
2820 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
2821 SourceLocation Loc = ConsumeToken();
2822
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002823 BalancedDelimiterTracker T(*this, tok::l_paren);
Stephen Hines651f13c2014-04-23 16:59:28 -07002824 if (T.expectAndConsume())
John Wiegley21ff2e52011-04-28 00:16:57 +00002825 return ExprError();
2826
2827 TypeResult Ty = ParseTypeName();
2828 if (Ty.isInvalid()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00002829 SkipUntil(tok::comma, StopAtSemi);
2830 SkipUntil(tok::r_paren, StopAtSemi);
John Wiegley21ff2e52011-04-28 00:16:57 +00002831 return ExprError();
2832 }
2833
2834 switch (ATT) {
2835 case ATT_ArrayRank: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002836 T.consumeClose();
2837 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL,
2838 T.getCloseLocation());
John Wiegley21ff2e52011-04-28 00:16:57 +00002839 }
2840 case ATT_ArrayExtent: {
Stephen Hines651f13c2014-04-23 16:59:28 -07002841 if (ExpectAndConsume(tok::comma)) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00002842 SkipUntil(tok::r_paren, StopAtSemi);
John Wiegley21ff2e52011-04-28 00:16:57 +00002843 return ExprError();
2844 }
2845
2846 ExprResult DimExpr = ParseExpression();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002847 T.consumeClose();
John Wiegley21ff2e52011-04-28 00:16:57 +00002848
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002849 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(),
2850 T.getCloseLocation());
John Wiegley21ff2e52011-04-28 00:16:57 +00002851 }
John Wiegley21ff2e52011-04-28 00:16:57 +00002852 }
David Blaikie30263482012-01-20 21:50:17 +00002853 llvm_unreachable("Invalid ArrayTypeTrait!");
John Wiegley21ff2e52011-04-28 00:16:57 +00002854}
2855
John Wiegley55262202011-04-25 06:54:41 +00002856/// ParseExpressionTrait - Parse built-in expression-trait
2857/// pseudo-functions like __is_lvalue_expr( xxx ).
2858///
2859/// primary-expression:
2860/// [Embarcadero] expression-trait '(' expression ')'
2861///
2862ExprResult Parser::ParseExpressionTrait() {
2863 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2864 SourceLocation Loc = ConsumeToken();
2865
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002866 BalancedDelimiterTracker T(*this, tok::l_paren);
Stephen Hines651f13c2014-04-23 16:59:28 -07002867 if (T.expectAndConsume())
John Wiegley55262202011-04-25 06:54:41 +00002868 return ExprError();
2869
2870 ExprResult Expr = ParseExpression();
2871
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002872 T.consumeClose();
John Wiegley55262202011-04-25 06:54:41 +00002873
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002874 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(),
2875 T.getCloseLocation());
John Wiegley55262202011-04-25 06:54:41 +00002876}
2877
2878
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002879/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2880/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2881/// based on the context past the parens.
John McCall60d7b3a2010-08-24 06:29:42 +00002882ExprResult
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002883Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallb3d87482010-08-24 05:47:05 +00002884 ParsedType &CastTy,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002885 BalancedDelimiterTracker &Tracker) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002886 assert(getLangOpts().CPlusPlus && "Should only be called for C++!");
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002887 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2888 assert(isTypeIdInParens() && "Not a type-id!");
2889
John McCall60d7b3a2010-08-24 06:29:42 +00002890 ExprResult Result(true);
John McCallb3d87482010-08-24 05:47:05 +00002891 CastTy = ParsedType();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002892
2893 // We need to disambiguate a very ugly part of the C++ syntax:
2894 //
2895 // (T())x; - type-id
2896 // (T())*x; - type-id
2897 // (T())/x; - expression
2898 // (T()); - expression
2899 //
2900 // The bad news is that we cannot use the specialized tentative parser, since
2901 // it can only verify that the thing inside the parens can be parsed as
2902 // type-id, it is not useful for determining the context past the parens.
2903 //
2904 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00002905 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002906 //
2907 // It uses a scheme similar to parsing inline methods. The parenthesized
2908 // tokens are cached, the context that follows is determined (possibly by
2909 // parsing a cast-expression), and then we re-introduce the cached tokens
2910 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002911
Mike Stump1eb44332009-09-09 15:08:12 +00002912 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002913 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002914
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002915 // Store the tokens of the parentheses. We will parse them after we determine
2916 // the context that follows them.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00002917 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002918 // We didn't find the ')' we expected.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002919 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002920 return ExprError();
2921 }
2922
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002923 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002924 ParseAs = CompoundLiteral;
2925 } else {
2926 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002927 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2928 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2929 NotCastExpr = true;
2930 } else {
2931 // Try parsing the cast-expression that may follow.
2932 // If it is not a cast-expression, NotCastExpr will be true and no token
2933 // will be consumed.
2934 Result = ParseCastExpression(false/*isUnaryExpression*/,
2935 false/*isAddressofOperand*/,
John McCallb3d87482010-08-24 05:47:05 +00002936 NotCastExpr,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002937 // type-id has priority.
Kaelyn Uhraincd78e612012-01-25 20:49:08 +00002938 IsTypeCast);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002939 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002940
2941 // If we parsed a cast-expression, it's really a type-id, otherwise it's
2942 // an expression.
2943 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002944 }
2945
Mike Stump1eb44332009-09-09 15:08:12 +00002946 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002947 Toks.push_back(Tok);
2948 // Re-enter the stored parenthesized tokens into the token stream, so we may
2949 // parse them now.
2950 PP.EnterTokenStream(Toks.data(), Toks.size(),
2951 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2952 // Drop the current token and bring the first cached one. It's the same token
2953 // as when we entered this function.
2954 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002955
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002956 if (ParseAs >= CompoundLiteral) {
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002957 // Parse the type declarator.
2958 DeclSpec DS(AttrFactory);
2959 ParseSpecifierQualifierList(DS);
2960 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2961 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002962
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002963 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002964 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002965
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002966 if (ParseAs == CompoundLiteral) {
2967 ExprType = CompoundLiteral;
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002968 TypeResult Ty = ParseTypeName();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002969 return ParseCompoundLiteralExpression(Ty.get(),
2970 Tracker.getOpenLocation(),
2971 Tracker.getCloseLocation());
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002972 }
Mike Stump1eb44332009-09-09 15:08:12 +00002973
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002974 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2975 assert(ParseAs == CastExpr);
2976
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002977 if (DeclaratorInfo.isInvalidType())
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002978 return ExprError();
2979
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002980 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002981 if (!Result.isInvalid())
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002982 Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(),
2983 DeclaratorInfo, CastTy,
2984 Tracker.getCloseLocation(), Result.take());
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002985 return Result;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002986 }
Mike Stump1eb44332009-09-09 15:08:12 +00002987
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002988 // Not a compound literal, and not followed by a cast-expression.
2989 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002990
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002991 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002992 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002993 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002994 Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(),
2995 Tok.getLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002996
2997 // Match the ')'.
2998 if (Result.isInvalid()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00002999 SkipUntil(tok::r_paren, StopAtSemi);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00003000 return ExprError();
3001 }
Mike Stump1eb44332009-09-09 15:08:12 +00003002
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003003 Tracker.consumeClose();
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00003004 return Result;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00003005}