blob: 9ac6d435067566749fdf9194e4d32e0ba6028c29 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation for C++.
11//
12//===----------------------------------------------------------------------===//
13
Reid Spencer5f016e22007-07-11 17:01:13 +000014#include "clang/Parse/Parser.h"
Douglas Gregorbc61bd82011-01-11 00:33:19 +000015#include "RAIIObjectsForParser.h"
Eli Friedmandc3b7232012-01-04 02:40:39 +000016#include "clang/Basic/PrettyStackTrace.h"
Richard Smith33762772012-03-08 23:06:02 +000017#include "clang/Lex/LiteralSupport.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000018#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000019#include "clang/Sema/DeclSpec.h"
20#include "clang/Sema/ParsedTemplate.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000021#include "clang/Sema/Scope.h"
Douglas Gregor3f9a0562009-11-03 01:35:08 +000022#include "llvm/Support/ErrorHandling.h"
23
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
Richard Smithea698b32011-04-14 21:45:45 +000026static int SelectDigraphErrorMessage(tok::TokenKind Kind) {
27 switch (Kind) {
28 case tok::kw_template: return 0;
29 case tok::kw_const_cast: return 1;
30 case tok::kw_dynamic_cast: return 2;
31 case tok::kw_reinterpret_cast: return 3;
32 case tok::kw_static_cast: return 4;
33 default:
David Blaikieb219cfc2011-09-23 05:06:16 +000034 llvm_unreachable("Unknown type for digraph error message.");
Richard Smithea698b32011-04-14 21:45:45 +000035 }
36}
37
38// Are the two tokens adjacent in the same source file?
Richard Smith19a27022012-06-18 06:11:04 +000039bool Parser::areTokensAdjacent(const Token &First, const Token &Second) {
Richard Smithea698b32011-04-14 21:45:45 +000040 SourceManager &SM = PP.getSourceManager();
41 SourceLocation FirstLoc = SM.getSpellingLoc(First.getLocation());
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +000042 SourceLocation FirstEnd = FirstLoc.getLocWithOffset(First.getLength());
Richard Smithea698b32011-04-14 21:45:45 +000043 return FirstEnd == SM.getSpellingLoc(Second.getLocation());
44}
45
46// Suggest fixit for "<::" after a cast.
47static void FixDigraph(Parser &P, Preprocessor &PP, Token &DigraphToken,
48 Token &ColonToken, tok::TokenKind Kind, bool AtDigraph) {
49 // Pull '<:' and ':' off token stream.
50 if (!AtDigraph)
51 PP.Lex(DigraphToken);
52 PP.Lex(ColonToken);
53
54 SourceRange Range;
55 Range.setBegin(DigraphToken.getLocation());
56 Range.setEnd(ColonToken.getLocation());
57 P.Diag(DigraphToken.getLocation(), diag::err_missing_whitespace_digraph)
58 << SelectDigraphErrorMessage(Kind)
59 << FixItHint::CreateReplacement(Range, "< ::");
60
61 // Update token information to reflect their change in token type.
62 ColonToken.setKind(tok::coloncolon);
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +000063 ColonToken.setLocation(ColonToken.getLocation().getLocWithOffset(-1));
Richard Smithea698b32011-04-14 21:45:45 +000064 ColonToken.setLength(2);
65 DigraphToken.setKind(tok::less);
66 DigraphToken.setLength(1);
67
68 // Push new tokens back to token stream.
69 PP.EnterToken(ColonToken);
70 if (!AtDigraph)
71 PP.EnterToken(DigraphToken);
72}
73
Richard Trieu950be712011-09-19 19:01:00 +000074// Check for '<::' which should be '< ::' instead of '[:' when following
75// a template name.
76void Parser::CheckForTemplateAndDigraph(Token &Next, ParsedType ObjectType,
77 bool EnteringContext,
78 IdentifierInfo &II, CXXScopeSpec &SS) {
Richard Trieuc11030e2011-09-20 20:03:50 +000079 if (!Next.is(tok::l_square) || Next.getLength() != 2)
Richard Trieu950be712011-09-19 19:01:00 +000080 return;
81
82 Token SecondToken = GetLookAheadToken(2);
Richard Smith19a27022012-06-18 06:11:04 +000083 if (!SecondToken.is(tok::colon) || !areTokensAdjacent(Next, SecondToken))
Richard Trieu950be712011-09-19 19:01:00 +000084 return;
85
86 TemplateTy Template;
87 UnqualifiedId TemplateName;
88 TemplateName.setIdentifier(&II, Tok.getLocation());
89 bool MemberOfUnknownSpecialization;
90 if (!Actions.isTemplateName(getCurScope(), SS, /*hasTemplateKeyword=*/false,
91 TemplateName, ObjectType, EnteringContext,
92 Template, MemberOfUnknownSpecialization))
93 return;
94
95 FixDigraph(*this, PP, Next, SecondToken, tok::kw_template,
96 /*AtDigraph*/false);
97}
98
Richard Trieu919b9552012-11-02 01:08:58 +000099/// \brief Emits an error for a left parentheses after a double colon.
100///
101/// When a '(' is found after a '::', emit an error. Attempt to fix the token
Nico Weberbba91b82012-11-29 05:29:23 +0000102/// stream by removing the '(', and the matching ')' if found.
Richard Trieu919b9552012-11-02 01:08:58 +0000103void Parser::CheckForLParenAfterColonColon() {
104 if (!Tok.is(tok::l_paren))
105 return;
106
107 SourceLocation l_parenLoc = ConsumeParen(), r_parenLoc;
108 Token Tok1 = getCurToken();
109 if (!Tok1.is(tok::identifier) && !Tok1.is(tok::star))
110 return;
111
112 if (Tok1.is(tok::identifier)) {
113 Token Tok2 = GetLookAheadToken(1);
114 if (Tok2.is(tok::r_paren)) {
115 ConsumeToken();
116 PP.EnterToken(Tok1);
117 r_parenLoc = ConsumeParen();
118 }
119 } else if (Tok1.is(tok::star)) {
120 Token Tok2 = GetLookAheadToken(1);
121 if (Tok2.is(tok::identifier)) {
122 Token Tok3 = GetLookAheadToken(2);
123 if (Tok3.is(tok::r_paren)) {
124 ConsumeToken();
125 ConsumeToken();
126 PP.EnterToken(Tok2);
127 PP.EnterToken(Tok1);
128 r_parenLoc = ConsumeParen();
129 }
130 }
131 }
132
133 Diag(l_parenLoc, diag::err_paren_after_colon_colon)
134 << FixItHint::CreateRemoval(l_parenLoc)
135 << FixItHint::CreateRemoval(r_parenLoc);
136}
137
Mike Stump1eb44332009-09-09 15:08:12 +0000138/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000139///
140/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump1eb44332009-09-09 15:08:12 +0000141/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000142/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000143///
144/// '::'[opt] nested-name-specifier
145/// '::'
146///
147/// nested-name-specifier:
148/// type-name '::'
149/// namespace-name '::'
150/// nested-name-specifier identifier '::'
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000151/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000152///
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000153///
Mike Stump1eb44332009-09-09 15:08:12 +0000154/// \param SS the scope specifier that will be set to the parsed
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000155/// nested-name-specifier (or empty)
156///
Mike Stump1eb44332009-09-09 15:08:12 +0000157/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000158/// the "." or "->" of a member access expression, this parameter provides the
159/// type of the object whose members are being accessed.
160///
161/// \param EnteringContext whether we will be entering into the context of
162/// the nested-name-specifier after parsing it.
163///
Douglas Gregord4dca082010-02-24 18:44:31 +0000164/// \param MayBePseudoDestructor When non-NULL, points to a flag that
165/// indicates whether this nested-name-specifier may be part of a
166/// pseudo-destructor name. In this case, the flag will be set false
167/// if we don't actually end up parsing a destructor name. Moreorover,
168/// if we do end up determining that we are parsing a destructor name,
169/// the last component of the nested-name-specifier is not parsed as
170/// part of the scope specifier.
Richard Smith2db075b2013-03-26 01:15:19 +0000171///
172/// \param IsTypename If \c true, this nested-name-specifier is known to be
173/// part of a type name. This is used to improve error recovery.
174///
175/// \param LastII When non-NULL, points to an IdentifierInfo* that will be
176/// filled in with the leading identifier in the last component of the
177/// nested-name-specifier, if any.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000178///
John McCall9ba61662010-02-26 08:45:28 +0000179/// \returns true if there was an error parsing a scope specifier
Douglas Gregor495c35d2009-08-25 22:51:20 +0000180bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +0000181 ParsedType ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000182 bool EnteringContext,
Francois Pichet4147d302011-03-27 19:41:34 +0000183 bool *MayBePseudoDestructor,
Richard Smith2db075b2013-03-26 01:15:19 +0000184 bool IsTypename,
185 IdentifierInfo **LastII) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000186 assert(getLangOpts().CPlusPlus &&
Chris Lattner7452c6f2009-01-05 01:24:05 +0000187 "Call sites of this function should be guarded by checking for C++");
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000189 if (Tok.is(tok::annot_cxxscope)) {
Richard Smith2db075b2013-03-26 01:15:19 +0000190 assert(!LastII && "want last identifier but have already annotated scope");
Douglas Gregorc34348a2011-02-24 17:54:50 +0000191 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
192 Tok.getAnnotationRange(),
193 SS);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000194 ConsumeToken();
John McCall9ba61662010-02-26 08:45:28 +0000195 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000196 }
Chris Lattnere607e802009-01-04 21:14:15 +0000197
Larisse Voufo9c90f7f2013-08-06 05:49:26 +0000198 if (Tok.is(tok::annot_template_id)) {
199 // If the current token is an annotated template id, it may already have
200 // a scope specifier. Restore it.
201 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
202 SS = TemplateId->SS;
203 }
204
Richard Smith2db075b2013-03-26 01:15:19 +0000205 if (LastII)
206 *LastII = 0;
207
Douglas Gregor39a8de12009-02-25 19:37:18 +0000208 bool HasScopeSpecifier = false;
209
Chris Lattner5b454732009-01-05 03:55:46 +0000210 if (Tok.is(tok::coloncolon)) {
211 // ::new and ::delete aren't nested-name-specifiers.
212 tok::TokenKind NextKind = NextToken().getKind();
213 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
214 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Chris Lattner55a7cef2009-01-05 00:13:00 +0000216 // '::' - Global scope qualifier.
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000217 if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
218 return true;
Richard Trieu919b9552012-11-02 01:08:58 +0000219
220 CheckForLParenAfterColonColon();
221
Douglas Gregor39a8de12009-02-25 19:37:18 +0000222 HasScopeSpecifier = true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000223 }
224
Douglas Gregord4dca082010-02-24 18:44:31 +0000225 bool CheckForDestructor = false;
226 if (MayBePseudoDestructor && *MayBePseudoDestructor) {
227 CheckForDestructor = true;
228 *MayBePseudoDestructor = false;
229 }
230
David Blaikie42d6d0c2011-12-04 05:04:18 +0000231 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
232 DeclSpec DS(AttrFactory);
233 SourceLocation DeclLoc = Tok.getLocation();
234 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
235 if (Tok.isNot(tok::coloncolon)) {
236 AnnotateExistingDecltypeSpecifier(DS, DeclLoc, EndLoc);
237 return false;
238 }
239
240 SourceLocation CCLoc = ConsumeToken();
241 if (Actions.ActOnCXXNestedNameSpecifierDecltype(SS, DS, CCLoc))
242 SS.SetInvalid(SourceRange(DeclLoc, CCLoc));
243
244 HasScopeSpecifier = true;
245 }
246
Douglas Gregor39a8de12009-02-25 19:37:18 +0000247 while (true) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000248 if (HasScopeSpecifier) {
249 // C++ [basic.lookup.classref]p5:
250 // If the qualified-id has the form
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000251 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000252 // ::class-name-or-namespace-name::...
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000253 //
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000254 // the class-name-or-namespace-name is looked up in global scope as a
255 // class-name or namespace-name.
256 //
257 // To implement this, we clear out the object type as soon as we've
258 // seen a leading '::' or part of a nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000259 ObjectType = ParsedType();
Douglas Gregor81b747b2009-09-17 21:32:03 +0000260
261 if (Tok.is(tok::code_completion)) {
262 // Code completion for a nested-name-specifier, where the code
263 // code completion token follows the '::'.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000264 Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
Argyrios Kyrtzidisb6b2b182011-04-23 01:04:12 +0000265 // Include code completion token into the range of the scope otherwise
266 // when we try to annotate the scope tokens the dangling code completion
267 // token will cause assertion in
268 // Preprocessor::AnnotatePreviousCachedTokens.
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000269 SS.setEndLoc(Tok.getLocation());
270 cutOffParsing();
271 return true;
Douglas Gregor81b747b2009-09-17 21:32:03 +0000272 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000273 }
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Douglas Gregor39a8de12009-02-25 19:37:18 +0000275 // nested-name-specifier:
Chris Lattner77cf72a2009-06-26 03:47:46 +0000276 // nested-name-specifier 'template'[opt] simple-template-id '::'
277
278 // Parse the optional 'template' keyword, then make sure we have
279 // 'identifier <' after it.
280 if (Tok.is(tok::kw_template)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000281 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedmaneab975d2009-08-29 04:08:08 +0000282 // nested-name-specifier, since they aren't allowed to start with
283 // 'template'.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000284 if (!HasScopeSpecifier && !ObjectType)
Eli Friedmaneab975d2009-08-29 04:08:08 +0000285 break;
286
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000287 TentativeParsingAction TPA(*this);
Chris Lattner77cf72a2009-06-26 03:47:46 +0000288 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000289
290 UnqualifiedId TemplateName;
291 if (Tok.is(tok::identifier)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000292 // Consume the identifier.
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000293 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000294 ConsumeToken();
295 } else if (Tok.is(tok::kw_operator)) {
296 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000297 TemplateName)) {
298 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000299 break;
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000300 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000301
Sean Hunte6252d12009-11-28 08:58:14 +0000302 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
303 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000304 Diag(TemplateName.getSourceRange().getBegin(),
305 diag::err_id_after_template_in_nested_name_spec)
306 << TemplateName.getSourceRange();
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000307 TPA.Commit();
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000308 break;
309 }
310 } else {
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000311 TPA.Revert();
Chris Lattner77cf72a2009-06-26 03:47:46 +0000312 break;
313 }
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Douglas Gregor7bb87fc2009-11-11 16:39:34 +0000315 // If the next token is not '<', we have a qualified-id that refers
316 // to a template name, such as T::template apply, but is not a
317 // template-id.
318 if (Tok.isNot(tok::less)) {
319 TPA.Revert();
320 break;
321 }
322
323 // Commit to parsing the template-id.
324 TPA.Commit();
Douglas Gregord6ab2322010-06-16 23:00:59 +0000325 TemplateTy Template;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000326 if (TemplateNameKind TNK
327 = Actions.ActOnDependentTemplateName(getCurScope(),
328 SS, TemplateKWLoc, TemplateName,
329 ObjectType, EnteringContext,
330 Template)) {
331 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateKWLoc,
332 TemplateName, false))
Douglas Gregord6ab2322010-06-16 23:00:59 +0000333 return true;
334 } else
John McCall9ba61662010-02-26 08:45:28 +0000335 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Chris Lattner77cf72a2009-06-26 03:47:46 +0000337 continue;
338 }
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Douglas Gregor39a8de12009-02-25 19:37:18 +0000340 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000341 // We have
Douglas Gregor39a8de12009-02-25 19:37:18 +0000342 //
343 // simple-template-id '::'
344 //
345 // So we need to check whether the simple-template-id is of the
Douglas Gregorc45c2322009-03-31 00:43:58 +0000346 // right kind (it should name a type or be dependent), and then
347 // convert it into a type within the nested-name-specifier.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000348 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord4dca082010-02-24 18:44:31 +0000349 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
350 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000351 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000352 }
353
Richard Smith2db075b2013-03-26 01:15:19 +0000354 if (LastII)
355 *LastII = TemplateId->Name;
356
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000357 // Consume the template-id token.
358 ConsumeToken();
359
360 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
361 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000362
David Blaikie6796fc12011-11-07 03:30:03 +0000363 HasScopeSpecifier = true;
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000364
Benjamin Kramer5354e772012-08-23 23:38:35 +0000365 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000366 TemplateId->NumArgs);
367
368 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000369 SS,
370 TemplateId->TemplateKWLoc,
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000371 TemplateId->Template,
372 TemplateId->TemplateNameLoc,
373 TemplateId->LAngleLoc,
374 TemplateArgsPtr,
375 TemplateId->RAngleLoc,
376 CCLoc,
377 EnteringContext)) {
378 SourceLocation StartLoc
379 = SS.getBeginLoc().isValid()? SS.getBeginLoc()
380 : TemplateId->TemplateNameLoc;
381 SS.SetInvalid(SourceRange(StartLoc, CCLoc));
Chris Lattner67b9e832009-06-26 03:45:46 +0000382 }
Argyrios Kyrtzidiseccce7e2011-05-03 18:45:38 +0000383
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000384 continue;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000385 }
386
Chris Lattner5c7f7862009-06-26 03:52:38 +0000387
388 // The rest of the nested-name-specifier possibilities start with
389 // tok::identifier.
390 if (Tok.isNot(tok::identifier))
391 break;
392
393 IdentifierInfo &II = *Tok.getIdentifierInfo();
394
395 // nested-name-specifier:
396 // type-name '::'
397 // namespace-name '::'
398 // nested-name-specifier identifier '::'
399 Token Next = NextToken();
Chris Lattner46646492009-12-07 01:36:53 +0000400
401 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
402 // and emit a fixit hint for it.
Douglas Gregorb10cd042010-02-21 18:36:56 +0000403 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000404 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
405 Tok.getLocation(),
406 Next.getLocation(), ObjectType,
Douglas Gregorb10cd042010-02-21 18:36:56 +0000407 EnteringContext) &&
408 // If the token after the colon isn't an identifier, it's still an
409 // error, but they probably meant something else strange so don't
410 // recover like this.
411 PP.LookAhead(1).is(tok::identifier)) {
412 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
Douglas Gregor849b2432010-03-31 17:46:05 +0000413 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregorb10cd042010-02-21 18:36:56 +0000414
415 // Recover as if the user wrote '::'.
416 Next.setKind(tok::coloncolon);
417 }
Chris Lattner46646492009-12-07 01:36:53 +0000418 }
419
Chris Lattner5c7f7862009-06-26 03:52:38 +0000420 if (Next.is(tok::coloncolon)) {
Douglas Gregor77549082010-02-24 21:29:12 +0000421 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000422 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor77549082010-02-24 21:29:12 +0000423 II, ObjectType)) {
Douglas Gregord4dca082010-02-24 18:44:31 +0000424 *MayBePseudoDestructor = true;
John McCall9ba61662010-02-26 08:45:28 +0000425 return false;
Douglas Gregord4dca082010-02-24 18:44:31 +0000426 }
427
Richard Smith2db075b2013-03-26 01:15:19 +0000428 if (LastII)
429 *LastII = &II;
430
Chris Lattner5c7f7862009-06-26 03:52:38 +0000431 // We have an identifier followed by a '::'. Lookup this name
432 // as the name in a nested-name-specifier.
433 SourceLocation IdLoc = ConsumeToken();
Chris Lattner46646492009-12-07 01:36:53 +0000434 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
435 "NextToken() not working properly!");
Chris Lattner5c7f7862009-06-26 03:52:38 +0000436 SourceLocation CCLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Richard Trieu919b9552012-11-02 01:08:58 +0000438 CheckForLParenAfterColonColon();
439
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000440 HasScopeSpecifier = true;
441 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
442 ObjectType, EnteringContext, SS))
443 SS.SetInvalid(SourceRange(IdLoc, CCLoc));
444
Chris Lattner5c7f7862009-06-26 03:52:38 +0000445 continue;
446 }
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Richard Trieu950be712011-09-19 19:01:00 +0000448 CheckForTemplateAndDigraph(Next, ObjectType, EnteringContext, II, SS);
Richard Smithea698b32011-04-14 21:45:45 +0000449
Chris Lattner5c7f7862009-06-26 03:52:38 +0000450 // nested-name-specifier:
451 // type-name '<'
452 if (Next.is(tok::less)) {
453 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000454 UnqualifiedId TemplateName;
455 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000456 bool MemberOfUnknownSpecialization;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000457 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000458 /*hasTemplateKeyword=*/false,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000459 TemplateName,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000460 ObjectType,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000461 EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000462 Template,
463 MemberOfUnknownSpecialization)) {
David Blaikie6796fc12011-11-07 03:30:03 +0000464 // We have found a template name, so annotate this token
Chris Lattner5c7f7862009-06-26 03:52:38 +0000465 // with a template-id annotation. We do not permit the
466 // template-id to be translated into a type annotation,
467 // because some clients (e.g., the parsing of class template
468 // specializations) still want to see the original template-id
469 // token.
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000470 ConsumeToken();
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000471 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
472 TemplateName, false))
John McCall9ba61662010-02-26 08:45:28 +0000473 return true;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000474 continue;
Larisse Voufoef4579c2013-08-06 01:03:05 +0000475 }
476
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000477 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
Francois Pichet4147d302011-03-27 19:41:34 +0000478 (IsTypename || IsTemplateArgumentList(1))) {
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000479 // We have something like t::getAs<T>, where getAs is a
480 // member of an unknown specialization. However, this will only
481 // parse correctly as a template, so suggest the keyword 'template'
482 // before 'getAs' and treat this as a dependent template name.
Francois Pichet4147d302011-03-27 19:41:34 +0000483 unsigned DiagID = diag::err_missing_dependent_template_keyword;
David Blaikie4e4d0842012-03-11 07:00:24 +0000484 if (getLangOpts().MicrosoftExt)
Francois Pichetcf320c62011-04-22 08:25:24 +0000485 DiagID = diag::warn_missing_dependent_template_keyword;
Francois Pichet4147d302011-03-27 19:41:34 +0000486
487 Diag(Tok.getLocation(), DiagID)
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000488 << II.getName()
489 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
490
Douglas Gregord6ab2322010-06-16 23:00:59 +0000491 if (TemplateNameKind TNK
Douglas Gregor23c94db2010-07-02 17:43:08 +0000492 = Actions.ActOnDependentTemplateName(getCurScope(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000493 SS, SourceLocation(),
Douglas Gregord6ab2322010-06-16 23:00:59 +0000494 TemplateName, ObjectType,
495 EnteringContext, Template)) {
496 // Consume the identifier.
497 ConsumeToken();
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000498 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
499 TemplateName, false))
500 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000501 }
502 else
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000503 return true;
Douglas Gregord6ab2322010-06-16 23:00:59 +0000504
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000505 continue;
Chris Lattner5c7f7862009-06-26 03:52:38 +0000506 }
507 }
508
Douglas Gregor39a8de12009-02-25 19:37:18 +0000509 // We don't have any tokens that form the beginning of a
510 // nested-name-specifier, so we're done.
511 break;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000512 }
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Douglas Gregord4dca082010-02-24 18:44:31 +0000514 // Even if we didn't see any pieces of a nested-name-specifier, we
515 // still check whether there is a tilde in this position, which
516 // indicates a potential pseudo-destructor.
517 if (CheckForDestructor && Tok.is(tok::tilde))
518 *MayBePseudoDestructor = true;
519
John McCall9ba61662010-02-26 08:45:28 +0000520 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000521}
522
523/// ParseCXXIdExpression - Handle id-expression.
524///
525/// id-expression:
526/// unqualified-id
527/// qualified-id
528///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000529/// qualified-id:
530/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
531/// '::' identifier
532/// '::' operator-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000533/// '::' template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000534///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000535/// NOTE: The standard specifies that, for qualified-id, the parser does not
536/// expect:
537///
538/// '::' conversion-function-id
539/// '::' '~' class-name
540///
541/// This may cause a slight inconsistency on diagnostics:
542///
543/// class C {};
544/// namespace A {}
545/// void f() {
546/// :: A :: ~ C(); // Some Sema error about using destructor with a
547/// // namespace.
548/// :: ~ C(); // Some Parser error like 'unexpected ~'.
549/// }
550///
551/// We simplify the parser a bit and make it work like:
552///
553/// qualified-id:
554/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
555/// '::' unqualified-id
556///
557/// That way Sema can handle and report similar errors for namespaces and the
558/// global scope.
559///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000560/// The isAddressOfOperand parameter indicates that this id-expression is a
561/// direct operand of the address-of operator. This is, besides member contexts,
562/// the only place where a qualified-id naming a non-static class member may
563/// appear.
564///
John McCall60d7b3a2010-08-24 06:29:42 +0000565ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000566 // qualified-id:
567 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
568 // '::' unqualified-id
569 //
570 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000571 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000572
573 SourceLocation TemplateKWLoc;
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000574 UnqualifiedId Name;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000575 if (ParseUnqualifiedId(SS,
576 /*EnteringContext=*/false,
577 /*AllowDestructorName=*/false,
578 /*AllowConstructorName=*/false,
John McCallb3d87482010-08-24 05:47:05 +0000579 /*ObjectType=*/ ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000580 TemplateKWLoc,
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000581 Name))
582 return ExprError();
John McCallb681b612009-11-22 02:49:43 +0000583
584 // This is only the direct operand of an & operator if it is not
585 // followed by a postfix-expression suffix.
John McCall9c72c602010-08-27 09:08:28 +0000586 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
587 isAddressOfOperand = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000588
589 return Actions.ActOnIdExpression(getCurScope(), SS, TemplateKWLoc, Name,
590 Tok.is(tok::l_paren), isAddressOfOperand);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000591}
592
Richard Smith0a664b82013-05-09 21:36:41 +0000593/// ParseLambdaExpression - Parse a C++11 lambda expression.
Douglas Gregorae7902c2011-08-04 15:30:47 +0000594///
595/// lambda-expression:
596/// lambda-introducer lambda-declarator[opt] compound-statement
597///
598/// lambda-introducer:
599/// '[' lambda-capture[opt] ']'
600///
601/// lambda-capture:
602/// capture-default
603/// capture-list
604/// capture-default ',' capture-list
605///
606/// capture-default:
607/// '&'
608/// '='
609///
610/// capture-list:
611/// capture
612/// capture-list ',' capture
613///
614/// capture:
Richard Smith0a664b82013-05-09 21:36:41 +0000615/// simple-capture
616/// init-capture [C++1y]
617///
618/// simple-capture:
Douglas Gregorae7902c2011-08-04 15:30:47 +0000619/// identifier
620/// '&' identifier
621/// 'this'
622///
Richard Smith0a664b82013-05-09 21:36:41 +0000623/// init-capture: [C++1y]
624/// identifier initializer
625/// '&' identifier initializer
626///
Douglas Gregorae7902c2011-08-04 15:30:47 +0000627/// lambda-declarator:
628/// '(' parameter-declaration-clause ')' attribute-specifier[opt]
629/// 'mutable'[opt] exception-specification[opt]
630/// trailing-return-type[opt]
631///
632ExprResult Parser::ParseLambdaExpression() {
633 // Parse lambda-introducer.
634 LambdaIntroducer Intro;
635
David Blaikiedc84cd52013-02-20 22:23:23 +0000636 Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
Douglas Gregorae7902c2011-08-04 15:30:47 +0000637 if (DiagID) {
638 Diag(Tok, DiagID.getValue());
639 SkipUntil(tok::r_square);
Eli Friedmandc3b7232012-01-04 02:40:39 +0000640 SkipUntil(tok::l_brace);
641 SkipUntil(tok::r_brace);
642 return ExprError();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000643 }
644
645 return ParseLambdaExpressionAfterIntroducer(Intro);
646}
647
648/// TryParseLambdaExpression - Use lookahead and potentially tentative
649/// parsing to determine if we are looking at a C++0x lambda expression, and parse
650/// it if we are.
651///
652/// If we are not looking at a lambda expression, returns ExprError().
653ExprResult Parser::TryParseLambdaExpression() {
Richard Smith80ad52f2013-01-02 11:42:31 +0000654 assert(getLangOpts().CPlusPlus11
Douglas Gregorae7902c2011-08-04 15:30:47 +0000655 && Tok.is(tok::l_square)
656 && "Not at the start of a possible lambda expression.");
657
658 const Token Next = NextToken(), After = GetLookAheadToken(2);
659
660 // If lookahead indicates this is a lambda...
661 if (Next.is(tok::r_square) || // []
662 Next.is(tok::equal) || // [=
663 (Next.is(tok::amp) && // [&] or [&,
664 (After.is(tok::r_square) ||
665 After.is(tok::comma))) ||
666 (Next.is(tok::identifier) && // [identifier]
667 After.is(tok::r_square))) {
668 return ParseLambdaExpression();
669 }
670
Eli Friedmandc3b7232012-01-04 02:40:39 +0000671 // If lookahead indicates an ObjC message send...
672 // [identifier identifier
Douglas Gregorae7902c2011-08-04 15:30:47 +0000673 if (Next.is(tok::identifier) && After.is(tok::identifier)) {
Eli Friedmandc3b7232012-01-04 02:40:39 +0000674 return ExprEmpty();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000675 }
676
Eli Friedmandc3b7232012-01-04 02:40:39 +0000677 // Here, we're stuck: lambda introducers and Objective-C message sends are
678 // unambiguous, but it requires arbitrary lookhead. [a,b,c,d,e,f,g] is a
679 // lambda, and [a,b,c,d,e,f,g h] is a Objective-C message send. Instead of
680 // writing two routines to parse a lambda introducer, just try to parse
681 // a lambda introducer first, and fall back if that fails.
682 // (TryParseLambdaIntroducer never produces any diagnostic output.)
Douglas Gregorae7902c2011-08-04 15:30:47 +0000683 LambdaIntroducer Intro;
684 if (TryParseLambdaIntroducer(Intro))
Eli Friedmandc3b7232012-01-04 02:40:39 +0000685 return ExprEmpty();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000686 return ParseLambdaExpressionAfterIntroducer(Intro);
687}
688
Richard Smith440d4562013-05-21 22:21:19 +0000689/// \brief Parse a lambda introducer.
690/// \param Intro A LambdaIntroducer filled in with information about the
691/// contents of the lambda-introducer.
692/// \param SkippedInits If non-null, we are disambiguating between an Obj-C
693/// message send and a lambda expression. In this mode, we will
694/// sometimes skip the initializers for init-captures and not fully
695/// populate \p Intro. This flag will be set to \c true if we do so.
696/// \return A DiagnosticID if it hit something unexpected. The location for
697/// for the diagnostic is that of the current token.
698Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro,
699 bool *SkippedInits) {
David Blaikiedc84cd52013-02-20 22:23:23 +0000700 typedef Optional<unsigned> DiagResult;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000701
702 assert(Tok.is(tok::l_square) && "Lambda expressions begin with '['.");
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000703 BalancedDelimiterTracker T(*this, tok::l_square);
704 T.consumeOpen();
705
706 Intro.Range.setBegin(T.getOpenLocation());
Douglas Gregorae7902c2011-08-04 15:30:47 +0000707
708 bool first = true;
709
710 // Parse capture-default.
711 if (Tok.is(tok::amp) &&
712 (NextToken().is(tok::comma) || NextToken().is(tok::r_square))) {
713 Intro.Default = LCD_ByRef;
Douglas Gregor3ac109c2012-02-10 17:46:20 +0000714 Intro.DefaultLoc = ConsumeToken();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000715 first = false;
716 } else if (Tok.is(tok::equal)) {
717 Intro.Default = LCD_ByCopy;
Douglas Gregor3ac109c2012-02-10 17:46:20 +0000718 Intro.DefaultLoc = ConsumeToken();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000719 first = false;
720 }
721
722 while (Tok.isNot(tok::r_square)) {
723 if (!first) {
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000724 if (Tok.isNot(tok::comma)) {
Douglas Gregor437fbc52012-07-31 00:50:07 +0000725 // Provide a completion for a lambda introducer here. Except
726 // in Objective-C, where this is Almost Surely meant to be a message
727 // send. In that case, fail here and let the ObjC message
728 // expression parser perform the completion.
Douglas Gregord48ab062012-07-31 15:27:48 +0000729 if (Tok.is(tok::code_completion) &&
730 !(getLangOpts().ObjC1 && Intro.Default == LCD_None &&
731 !Intro.Captures.empty())) {
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000732 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
733 /*AfterAmpersand=*/false);
734 ConsumeCodeCompletionToken();
735 break;
736 }
737
Douglas Gregorae7902c2011-08-04 15:30:47 +0000738 return DiagResult(diag::err_expected_comma_or_rsquare);
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000739 }
Douglas Gregorae7902c2011-08-04 15:30:47 +0000740 ConsumeToken();
741 }
742
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000743 if (Tok.is(tok::code_completion)) {
744 // If we're in Objective-C++ and we have a bare '[', then this is more
745 // likely to be a message receiver.
David Blaikie4e4d0842012-03-11 07:00:24 +0000746 if (getLangOpts().ObjC1 && first)
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000747 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
748 else
749 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
750 /*AfterAmpersand=*/false);
751 ConsumeCodeCompletionToken();
752 break;
753 }
Douglas Gregorae7902c2011-08-04 15:30:47 +0000754
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000755 first = false;
756
Douglas Gregorae7902c2011-08-04 15:30:47 +0000757 // Parse capture.
758 LambdaCaptureKind Kind = LCK_ByCopy;
759 SourceLocation Loc;
760 IdentifierInfo* Id = 0;
Douglas Gregora7365242012-02-14 19:27:52 +0000761 SourceLocation EllipsisLoc;
Richard Smith0a664b82013-05-09 21:36:41 +0000762 ExprResult Init;
Douglas Gregora7365242012-02-14 19:27:52 +0000763
Douglas Gregorae7902c2011-08-04 15:30:47 +0000764 if (Tok.is(tok::kw_this)) {
765 Kind = LCK_This;
766 Loc = ConsumeToken();
767 } else {
768 if (Tok.is(tok::amp)) {
769 Kind = LCK_ByRef;
770 ConsumeToken();
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000771
772 if (Tok.is(tok::code_completion)) {
773 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
774 /*AfterAmpersand=*/true);
775 ConsumeCodeCompletionToken();
776 break;
777 }
Douglas Gregorae7902c2011-08-04 15:30:47 +0000778 }
779
780 if (Tok.is(tok::identifier)) {
781 Id = Tok.getIdentifierInfo();
782 Loc = ConsumeToken();
783 } else if (Tok.is(tok::kw_this)) {
784 // FIXME: If we want to suggest a fixit here, will need to return more
785 // than just DiagnosticID. Perhaps full DiagnosticBuilder that can be
786 // Clear()ed to prevent emission in case of tentative parsing?
787 return DiagResult(diag::err_this_captured_by_reference);
788 } else {
789 return DiagResult(diag::err_expected_capture);
790 }
Richard Smith0a664b82013-05-09 21:36:41 +0000791
792 if (Tok.is(tok::l_paren)) {
793 BalancedDelimiterTracker Parens(*this, tok::l_paren);
794 Parens.consumeOpen();
795
796 ExprVector Exprs;
797 CommaLocsTy Commas;
Richard Smith440d4562013-05-21 22:21:19 +0000798 if (SkippedInits) {
799 Parens.skipToEnd();
800 *SkippedInits = true;
801 } else if (ParseExpressionList(Exprs, Commas)) {
Richard Smith0a664b82013-05-09 21:36:41 +0000802 Parens.skipToEnd();
803 Init = ExprError();
804 } else {
805 Parens.consumeClose();
806 Init = Actions.ActOnParenListExpr(Parens.getOpenLocation(),
807 Parens.getCloseLocation(),
808 Exprs);
809 }
810 } else if (Tok.is(tok::l_brace) || Tok.is(tok::equal)) {
811 if (Tok.is(tok::equal))
812 ConsumeToken();
813
Richard Smith440d4562013-05-21 22:21:19 +0000814 if (!SkippedInits)
815 Init = ParseInitializer();
816 else if (Tok.is(tok::l_brace)) {
817 BalancedDelimiterTracker Braces(*this, tok::l_brace);
818 Braces.consumeOpen();
819 Braces.skipToEnd();
820 *SkippedInits = true;
821 } else {
822 // We're disambiguating this:
823 //
824 // [..., x = expr
825 //
826 // We need to find the end of the following expression in order to
827 // determine whether this is an Obj-C message send's receiver, or a
828 // lambda init-capture.
829 //
830 // Parse the expression to find where it ends, and annotate it back
831 // onto the tokens. We would have parsed this expression the same way
832 // in either case: both the RHS of an init-capture and the RHS of an
833 // assignment expression are parsed as an initializer-clause, and in
834 // neither case can anything be added to the scope between the '[' and
835 // here.
836 //
837 // FIXME: This is horrible. Adding a mechanism to skip an expression
838 // would be much cleaner.
839 // FIXME: If there is a ',' before the next ']' or ':', we can skip to
840 // that instead. (And if we see a ':' with no matching '?', we can
841 // classify this as an Obj-C message send.)
842 SourceLocation StartLoc = Tok.getLocation();
843 InMessageExpressionRAIIObject MaybeInMessageExpression(*this, true);
844 Init = ParseInitializer();
845
846 if (Tok.getLocation() != StartLoc) {
847 // Back out the lexing of the token after the initializer.
848 PP.RevertCachedTokens(1);
849
850 // Replace the consumed tokens with an appropriate annotation.
851 Tok.setLocation(StartLoc);
852 Tok.setKind(tok::annot_primary_expr);
853 setExprAnnotation(Tok, Init);
854 Tok.setAnnotationEndLoc(PP.getLastCachedTokenLocation());
855 PP.AnnotateCachedTokens(Tok);
856
857 // Consume the annotated initializer.
858 ConsumeToken();
859 }
860 }
Richard Smith0d8e9642013-05-16 06:20:58 +0000861 } else if (Tok.is(tok::ellipsis))
862 EllipsisLoc = ConsumeToken();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000863 }
864
Richard Smith0a664b82013-05-09 21:36:41 +0000865 Intro.addCapture(Kind, Loc, Id, EllipsisLoc, Init);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000866 }
867
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000868 T.consumeClose();
869 Intro.Range.setEnd(T.getCloseLocation());
Douglas Gregorae7902c2011-08-04 15:30:47 +0000870
871 return DiagResult();
872}
873
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000874/// TryParseLambdaIntroducer - Tentatively parse a lambda introducer.
Douglas Gregorae7902c2011-08-04 15:30:47 +0000875///
876/// Returns true if it hit something unexpected.
877bool Parser::TryParseLambdaIntroducer(LambdaIntroducer &Intro) {
878 TentativeParsingAction PA(*this);
879
Richard Smith440d4562013-05-21 22:21:19 +0000880 bool SkippedInits = false;
881 Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro, &SkippedInits));
Douglas Gregorae7902c2011-08-04 15:30:47 +0000882
883 if (DiagID) {
884 PA.Revert();
885 return true;
886 }
887
Richard Smith440d4562013-05-21 22:21:19 +0000888 if (SkippedInits) {
889 // Parse it again, but this time parse the init-captures too.
890 PA.Revert();
891 Intro = LambdaIntroducer();
892 DiagID = ParseLambdaIntroducer(Intro);
893 assert(!DiagID && "parsing lambda-introducer failed on reparse");
894 return false;
895 }
896
Douglas Gregorae7902c2011-08-04 15:30:47 +0000897 PA.Commit();
898 return false;
899}
900
901/// ParseLambdaExpressionAfterIntroducer - Parse the rest of a lambda
902/// expression.
903ExprResult Parser::ParseLambdaExpressionAfterIntroducer(
904 LambdaIntroducer &Intro) {
Eli Friedmandc3b7232012-01-04 02:40:39 +0000905 SourceLocation LambdaBeginLoc = Intro.Range.getBegin();
906 Diag(LambdaBeginLoc, diag::warn_cxx98_compat_lambda);
907
908 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), LambdaBeginLoc,
909 "lambda expression parsing");
910
Richard Smith0a664b82013-05-09 21:36:41 +0000911 // FIXME: Call into Actions to add any init-capture declarations to the
912 // scope while parsing the lambda-declarator and compound-statement.
913
Douglas Gregorae7902c2011-08-04 15:30:47 +0000914 // Parse lambda-declarator[opt].
915 DeclSpec DS(AttrFactory);
Eli Friedmanf88c4002012-01-04 04:41:38 +0000916 Declarator D(DS, Declarator::LambdaExprContext);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000917
918 if (Tok.is(tok::l_paren)) {
919 ParseScope PrototypeScope(this,
920 Scope::FunctionPrototypeScope |
Richard Smith3a2b7a12013-01-28 22:42:45 +0000921 Scope::FunctionDeclarationScope |
Douglas Gregorae7902c2011-08-04 15:30:47 +0000922 Scope::DeclScope);
923
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000924 SourceLocation DeclEndLoc;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000925 BalancedDelimiterTracker T(*this, tok::l_paren);
926 T.consumeOpen();
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000927 SourceLocation LParenLoc = T.getOpenLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000928
929 // Parse parameter-declaration-clause.
930 ParsedAttributes Attr(AttrFactory);
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000931 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000932 SourceLocation EllipsisLoc;
933
Manuel Klimek152b4e42013-08-22 12:12:24 +0000934 if (Tok.isNot(tok::r_paren))
Douglas Gregorae7902c2011-08-04 15:30:47 +0000935 ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc);
Manuel Klimek152b4e42013-08-22 12:12:24 +0000936
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000937 T.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000938 SourceLocation RParenLoc = T.getCloseLocation();
939 DeclEndLoc = RParenLoc;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000940
941 // Parse 'mutable'[opt].
942 SourceLocation MutableLoc;
943 if (Tok.is(tok::kw_mutable)) {
944 MutableLoc = ConsumeToken();
945 DeclEndLoc = MutableLoc;
946 }
947
948 // Parse exception-specification[opt].
949 ExceptionSpecificationType ESpecType = EST_None;
950 SourceRange ESpecRange;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000951 SmallVector<ParsedType, 2> DynamicExceptions;
952 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000953 ExprResult NoexceptExpr;
Richard Smitha058fd42012-05-02 22:22:32 +0000954 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +0000955 DynamicExceptions,
956 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +0000957 NoexceptExpr);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000958
959 if (ESpecType != EST_None)
960 DeclEndLoc = ESpecRange.getEnd();
961
962 // Parse attribute-specifier[opt].
Richard Smith4e24f0f2013-01-02 12:01:23 +0000963 MaybeParseCXX11Attributes(Attr, &DeclEndLoc);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000964
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000965 SourceLocation FunLocalRangeEnd = DeclEndLoc;
966
Douglas Gregorae7902c2011-08-04 15:30:47 +0000967 // Parse trailing-return-type[opt].
Richard Smith54655be2012-06-12 01:51:59 +0000968 TypeResult TrailingReturnType;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000969 if (Tok.is(tok::arrow)) {
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000970 FunLocalRangeEnd = Tok.getLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +0000971 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +0000972 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregorae7902c2011-08-04 15:30:47 +0000973 if (Range.getEnd().isValid())
974 DeclEndLoc = Range.getEnd();
975 }
976
977 PrototypeScope.Exit();
978
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000979 SourceLocation NoLoc;
Douglas Gregorae7902c2011-08-04 15:30:47 +0000980 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000981 /*isAmbiguous=*/false,
982 LParenLoc,
Douglas Gregorae7902c2011-08-04 15:30:47 +0000983 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000984 EllipsisLoc, RParenLoc,
Douglas Gregorae7902c2011-08-04 15:30:47 +0000985 DS.getTypeQualifiers(),
986 /*RefQualifierIsLValueRef=*/true,
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000987 /*RefQualifierLoc=*/NoLoc,
988 /*ConstQualifierLoc=*/NoLoc,
989 /*VolatileQualifierLoc=*/NoLoc,
Douglas Gregorae7902c2011-08-04 15:30:47 +0000990 MutableLoc,
991 ESpecType, ESpecRange.getBegin(),
992 DynamicExceptions.data(),
993 DynamicExceptionRanges.data(),
994 DynamicExceptions.size(),
995 NoexceptExpr.isUsable() ?
996 NoexceptExpr.get() : 0,
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000997 LParenLoc, FunLocalRangeEnd, D,
Douglas Gregorae7902c2011-08-04 15:30:47 +0000998 TrailingReturnType),
999 Attr, DeclEndLoc);
Douglas Gregorc9ecec42012-02-16 21:53:36 +00001000 } else if (Tok.is(tok::kw_mutable) || Tok.is(tok::arrow)) {
1001 // It's common to forget that one needs '()' before 'mutable' or the
1002 // result type. Deal with this.
1003 Diag(Tok, diag::err_lambda_missing_parens)
1004 << Tok.is(tok::arrow)
1005 << FixItHint::CreateInsertion(Tok.getLocation(), "() ");
1006 SourceLocation DeclLoc = Tok.getLocation();
1007 SourceLocation DeclEndLoc = DeclLoc;
1008
1009 // Parse 'mutable', if it's there.
1010 SourceLocation MutableLoc;
1011 if (Tok.is(tok::kw_mutable)) {
1012 MutableLoc = ConsumeToken();
1013 DeclEndLoc = MutableLoc;
1014 }
1015
1016 // Parse the return type, if there is one.
Richard Smith54655be2012-06-12 01:51:59 +00001017 TypeResult TrailingReturnType;
Douglas Gregorc9ecec42012-02-16 21:53:36 +00001018 if (Tok.is(tok::arrow)) {
1019 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00001020 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregorc9ecec42012-02-16 21:53:36 +00001021 if (Range.getEnd().isValid())
1022 DeclEndLoc = Range.getEnd();
1023 }
1024
1025 ParsedAttributes Attr(AttrFactory);
Abramo Bagnara59c0a812012-10-04 21:42:10 +00001026 SourceLocation NoLoc;
Douglas Gregorc9ecec42012-02-16 21:53:36 +00001027 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
Abramo Bagnara59c0a812012-10-04 21:42:10 +00001028 /*isAmbiguous=*/false,
1029 /*LParenLoc=*/NoLoc,
1030 /*Params=*/0,
1031 /*NumParams=*/0,
1032 /*EllipsisLoc=*/NoLoc,
1033 /*RParenLoc=*/NoLoc,
1034 /*TypeQuals=*/0,
1035 /*RefQualifierIsLValueRef=*/true,
1036 /*RefQualifierLoc=*/NoLoc,
1037 /*ConstQualifierLoc=*/NoLoc,
1038 /*VolatileQualifierLoc=*/NoLoc,
1039 MutableLoc,
1040 EST_None,
1041 /*ESpecLoc=*/NoLoc,
1042 /*Exceptions=*/0,
1043 /*ExceptionRanges=*/0,
1044 /*NumExceptions=*/0,
1045 /*NoexceptExpr=*/0,
1046 DeclLoc, DeclEndLoc, D,
1047 TrailingReturnType),
Douglas Gregorc9ecec42012-02-16 21:53:36 +00001048 Attr, DeclEndLoc);
Douglas Gregorae7902c2011-08-04 15:30:47 +00001049 }
Douglas Gregorc9ecec42012-02-16 21:53:36 +00001050
Douglas Gregorae7902c2011-08-04 15:30:47 +00001051
Eli Friedman906a7e12012-01-06 03:05:34 +00001052 // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
1053 // it.
Douglas Gregorfccfb622012-02-21 22:51:27 +00001054 unsigned ScopeFlags = Scope::BlockScope | Scope::FnScope | Scope::DeclScope;
Douglas Gregorfccfb622012-02-21 22:51:27 +00001055 ParseScope BodyScope(this, ScopeFlags);
Eli Friedman906a7e12012-01-06 03:05:34 +00001056
Eli Friedmanec9ea722012-01-05 03:35:19 +00001057 Actions.ActOnStartOfLambdaDefinition(Intro, D, getCurScope());
1058
Douglas Gregorae7902c2011-08-04 15:30:47 +00001059 // Parse compound-statement.
Eli Friedmandc3b7232012-01-04 02:40:39 +00001060 if (!Tok.is(tok::l_brace)) {
Douglas Gregorae7902c2011-08-04 15:30:47 +00001061 Diag(Tok, diag::err_expected_lambda_body);
Eli Friedmandc3b7232012-01-04 02:40:39 +00001062 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
1063 return ExprError();
Douglas Gregorae7902c2011-08-04 15:30:47 +00001064 }
1065
Eli Friedmandc3b7232012-01-04 02:40:39 +00001066 StmtResult Stmt(ParseCompoundStatementBody());
1067 BodyScope.Exit();
1068
Eli Friedmandeeab902012-01-04 02:46:53 +00001069 if (!Stmt.isInvalid())
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00001070 return Actions.ActOnLambdaExpr(LambdaBeginLoc, Stmt.take(), getCurScope());
Eli Friedmandc3b7232012-01-04 02:40:39 +00001071
Eli Friedmandeeab902012-01-04 02:46:53 +00001072 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
1073 return ExprError();
Douglas Gregorae7902c2011-08-04 15:30:47 +00001074}
1075
Reid Spencer5f016e22007-07-11 17:01:13 +00001076/// ParseCXXCasts - This handles the various ways to cast expressions to another
1077/// type.
1078///
1079/// postfix-expression: [C++ 5.2p1]
1080/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
1081/// 'static_cast' '<' type-name '>' '(' expression ')'
1082/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
1083/// 'const_cast' '<' type-name '>' '(' expression ')'
1084///
John McCall60d7b3a2010-08-24 06:29:42 +00001085ExprResult Parser::ParseCXXCasts() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001086 tok::TokenKind Kind = Tok.getKind();
1087 const char *CastName = 0; // For error messages
1088
1089 switch (Kind) {
David Blaikieeb2d1f12011-09-23 20:26:49 +00001090 default: llvm_unreachable("Unknown C++ cast!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001091 case tok::kw_const_cast: CastName = "const_cast"; break;
1092 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
1093 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
1094 case tok::kw_static_cast: CastName = "static_cast"; break;
1095 }
1096
1097 SourceLocation OpLoc = ConsumeToken();
1098 SourceLocation LAngleBracketLoc = Tok.getLocation();
1099
Richard Smithea698b32011-04-14 21:45:45 +00001100 // Check for "<::" which is parsed as "[:". If found, fix token stream,
1101 // diagnose error, suggest fix, and recover parsing.
Richard Smith78fe3e02012-08-20 17:37:52 +00001102 if (Tok.is(tok::l_square) && Tok.getLength() == 2) {
1103 Token Next = NextToken();
1104 if (Next.is(tok::colon) && areTokensAdjacent(Tok, Next))
1105 FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
1106 }
Richard Smithea698b32011-04-14 21:45:45 +00001107
Reid Spencer5f016e22007-07-11 17:01:13 +00001108 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redl20df9b72008-12-11 22:51:44 +00001109 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001110
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00001111 // Parse the common declaration-specifiers piece.
1112 DeclSpec DS(AttrFactory);
1113 ParseSpecifierQualifierList(DS);
1114
1115 // Parse the abstract-declarator, if present.
1116 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1117 ParseDeclarator(DeclaratorInfo);
1118
Reid Spencer5f016e22007-07-11 17:01:13 +00001119 SourceLocation RAngleBracketLoc = Tok.getLocation();
1120
Chris Lattner1ab3b962008-11-18 07:48:38 +00001121 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redl20df9b72008-12-11 22:51:44 +00001122 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Reid Spencer5f016e22007-07-11 17:01:13 +00001123
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001124 SourceLocation LParenLoc, RParenLoc;
1125 BalancedDelimiterTracker T(*this, tok::l_paren);
Reid Spencer5f016e22007-07-11 17:01:13 +00001126
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001127 if (T.expectAndConsume(diag::err_expected_lparen_after, CastName))
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +00001128 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001129
John McCall60d7b3a2010-08-24 06:29:42 +00001130 ExprResult Result = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +00001131
Argyrios Kyrtzidis21e7ad22009-05-22 10:23:16 +00001132 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001133 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00001134
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00001135 if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
Douglas Gregor49badde2008-10-27 19:41:14 +00001136 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00001137 LAngleBracketLoc, DeclaratorInfo,
Douglas Gregor809070a2009-02-18 17:45:20 +00001138 RAngleBracketLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001139 T.getOpenLocation(), Result.take(),
1140 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001141
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001142 return Result;
Reid Spencer5f016e22007-07-11 17:01:13 +00001143}
1144
Sebastian Redlc42e1182008-11-11 11:37:55 +00001145/// ParseCXXTypeid - This handles the C++ typeid expression.
1146///
1147/// postfix-expression: [C++ 5.2p1]
1148/// 'typeid' '(' expression ')'
1149/// 'typeid' '(' type-id ')'
1150///
John McCall60d7b3a2010-08-24 06:29:42 +00001151ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc42e1182008-11-11 11:37:55 +00001152 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
1153
1154 SourceLocation OpLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001155 SourceLocation LParenLoc, RParenLoc;
1156 BalancedDelimiterTracker T(*this, tok::l_paren);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001157
1158 // typeid expressions are always parenthesized.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001159 if (T.expectAndConsume(diag::err_expected_lparen_after, "typeid"))
Sebastian Redl20df9b72008-12-11 22:51:44 +00001160 return ExprError();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001161 LParenLoc = T.getOpenLocation();
Sebastian Redlc42e1182008-11-11 11:37:55 +00001162
John McCall60d7b3a2010-08-24 06:29:42 +00001163 ExprResult Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +00001164
Richard Smith05766812012-08-18 00:55:03 +00001165 // C++0x [expr.typeid]p3:
1166 // When typeid is applied to an expression other than an lvalue of a
1167 // polymorphic class type [...] The expression is an unevaluated
1168 // operand (Clause 5).
1169 //
1170 // Note that we can't tell whether the expression is an lvalue of a
1171 // polymorphic class type until after we've parsed the expression; we
1172 // speculatively assume the subexpression is unevaluated, and fix it up
1173 // later.
1174 //
1175 // We enter the unevaluated context before trying to determine whether we
1176 // have a type-id, because the tentative parse logic will try to resolve
1177 // names, and must treat them as unevaluated.
Eli Friedman80bfa3d2012-09-26 04:34:21 +00001178 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
1179 Sema::ReuseLambdaContextDecl);
Richard Smith05766812012-08-18 00:55:03 +00001180
Sebastian Redlc42e1182008-11-11 11:37:55 +00001181 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +00001182 TypeResult Ty = ParseTypeName();
Sebastian Redlc42e1182008-11-11 11:37:55 +00001183
1184 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001185 T.consumeClose();
1186 RParenLoc = T.getCloseLocation();
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +00001187 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00001188 return ExprError();
Sebastian Redlc42e1182008-11-11 11:37:55 +00001189
1190 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallb3d87482010-08-24 05:47:05 +00001191 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001192 } else {
1193 Result = ParseExpression();
1194
1195 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001196 if (Result.isInvalid())
Sebastian Redlc42e1182008-11-11 11:37:55 +00001197 SkipUntil(tok::r_paren);
1198 else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001199 T.consumeClose();
1200 RParenLoc = T.getCloseLocation();
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +00001201 if (RParenLoc.isInvalid())
1202 return ExprError();
Douglas Gregorfadb53b2011-03-12 01:48:56 +00001203
Sebastian Redlc42e1182008-11-11 11:37:55 +00001204 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redleffa8d12008-12-10 00:02:53 +00001205 Result.release(), RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001206 }
1207 }
1208
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001209 return Result;
Sebastian Redlc42e1182008-11-11 11:37:55 +00001210}
1211
Francois Pichet01b7c302010-09-08 12:20:18 +00001212/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
1213///
1214/// '__uuidof' '(' expression ')'
1215/// '__uuidof' '(' type-id ')'
1216///
1217ExprResult Parser::ParseCXXUuidof() {
1218 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
1219
1220 SourceLocation OpLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001221 BalancedDelimiterTracker T(*this, tok::l_paren);
Francois Pichet01b7c302010-09-08 12:20:18 +00001222
1223 // __uuidof expressions are always parenthesized.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001224 if (T.expectAndConsume(diag::err_expected_lparen_after, "__uuidof"))
Francois Pichet01b7c302010-09-08 12:20:18 +00001225 return ExprError();
1226
1227 ExprResult Result;
1228
1229 if (isTypeIdInParens()) {
1230 TypeResult Ty = ParseTypeName();
1231
1232 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001233 T.consumeClose();
Francois Pichet01b7c302010-09-08 12:20:18 +00001234
1235 if (Ty.isInvalid())
1236 return ExprError();
1237
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001238 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), /*isType=*/true,
1239 Ty.get().getAsOpaquePtr(),
1240 T.getCloseLocation());
Francois Pichet01b7c302010-09-08 12:20:18 +00001241 } else {
1242 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1243 Result = ParseExpression();
1244
1245 // Match the ')'.
1246 if (Result.isInvalid())
1247 SkipUntil(tok::r_paren);
1248 else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001249 T.consumeClose();
Francois Pichet01b7c302010-09-08 12:20:18 +00001250
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001251 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(),
1252 /*isType=*/false,
1253 Result.release(), T.getCloseLocation());
Francois Pichet01b7c302010-09-08 12:20:18 +00001254 }
1255 }
1256
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001257 return Result;
Francois Pichet01b7c302010-09-08 12:20:18 +00001258}
1259
Douglas Gregord4dca082010-02-24 18:44:31 +00001260/// \brief Parse a C++ pseudo-destructor expression after the base,
1261/// . or -> operator, and nested-name-specifier have already been
1262/// parsed.
1263///
1264/// postfix-expression: [C++ 5.2]
1265/// postfix-expression . pseudo-destructor-name
1266/// postfix-expression -> pseudo-destructor-name
1267///
1268/// pseudo-destructor-name:
1269/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
1270/// ::[opt] nested-name-specifier template simple-template-id ::
1271/// ~type-name
1272/// ::[opt] nested-name-specifier[opt] ~type-name
1273///
John McCall60d7b3a2010-08-24 06:29:42 +00001274ExprResult
Douglas Gregord4dca082010-02-24 18:44:31 +00001275Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
1276 tok::TokenKind OpKind,
1277 CXXScopeSpec &SS,
John McCallb3d87482010-08-24 05:47:05 +00001278 ParsedType ObjectType) {
Douglas Gregord4dca082010-02-24 18:44:31 +00001279 // We're parsing either a pseudo-destructor-name or a dependent
1280 // member access that has the same form as a
1281 // pseudo-destructor-name. We parse both in the same way and let
1282 // the action model sort them out.
1283 //
1284 // Note that the ::[opt] nested-name-specifier[opt] has already
1285 // been parsed, and if there was a simple-template-id, it has
1286 // been coalesced into a template-id annotation token.
1287 UnqualifiedId FirstTypeName;
1288 SourceLocation CCLoc;
1289 if (Tok.is(tok::identifier)) {
1290 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1291 ConsumeToken();
1292 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1293 CCLoc = ConsumeToken();
1294 } else if (Tok.is(tok::annot_template_id)) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001295 // FIXME: retrieve TemplateKWLoc from template-id annotation and
1296 // store it in the pseudo-dtor node (to be used when instantiating it).
Douglas Gregord4dca082010-02-24 18:44:31 +00001297 FirstTypeName.setTemplateId(
1298 (TemplateIdAnnotation *)Tok.getAnnotationValue());
1299 ConsumeToken();
1300 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1301 CCLoc = ConsumeToken();
1302 } else {
1303 FirstTypeName.setIdentifier(0, SourceLocation());
1304 }
1305
1306 // Parse the tilde.
1307 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
1308 SourceLocation TildeLoc = ConsumeToken();
David Blaikie91ec7892011-12-16 16:03:09 +00001309
1310 if (Tok.is(tok::kw_decltype) && !FirstTypeName.isValid() && SS.isEmpty()) {
1311 DeclSpec DS(AttrFactory);
Benjamin Kramer85c60db2011-12-18 12:18:02 +00001312 ParseDecltypeSpecifier(DS);
David Blaikie91ec7892011-12-16 16:03:09 +00001313 if (DS.getTypeSpecType() == TST_error)
1314 return ExprError();
1315 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, OpLoc,
1316 OpKind, TildeLoc, DS,
1317 Tok.is(tok::l_paren));
1318 }
1319
Douglas Gregord4dca082010-02-24 18:44:31 +00001320 if (!Tok.is(tok::identifier)) {
1321 Diag(Tok, diag::err_destructor_tilde_identifier);
1322 return ExprError();
1323 }
1324
1325 // Parse the second type.
1326 UnqualifiedId SecondTypeName;
1327 IdentifierInfo *Name = Tok.getIdentifierInfo();
1328 SourceLocation NameLoc = ConsumeToken();
1329 SecondTypeName.setIdentifier(Name, NameLoc);
1330
1331 // If there is a '<', the second type name is a template-id. Parse
1332 // it as such.
1333 if (Tok.is(tok::less) &&
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001334 ParseUnqualifiedIdTemplateId(SS, SourceLocation(),
1335 Name, NameLoc,
1336 false, ObjectType, SecondTypeName,
1337 /*AssumeTemplateName=*/true))
Douglas Gregord4dca082010-02-24 18:44:31 +00001338 return ExprError();
1339
John McCall9ae2f072010-08-23 23:25:46 +00001340 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
1341 OpLoc, OpKind,
Douglas Gregord4dca082010-02-24 18:44:31 +00001342 SS, FirstTypeName, CCLoc,
1343 TildeLoc, SecondTypeName,
1344 Tok.is(tok::l_paren));
1345}
1346
Reid Spencer5f016e22007-07-11 17:01:13 +00001347/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
1348///
1349/// boolean-literal: [C++ 2.13.5]
1350/// 'true'
1351/// 'false'
John McCall60d7b3a2010-08-24 06:29:42 +00001352ExprResult Parser::ParseCXXBoolLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001353 tok::TokenKind Kind = Tok.getKind();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001354 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +00001355}
Chris Lattner50dd2892008-02-26 00:51:44 +00001356
1357/// ParseThrowExpression - This handles the C++ throw expression.
1358///
1359/// throw-expression: [C++ 15]
1360/// 'throw' assignment-expression[opt]
John McCall60d7b3a2010-08-24 06:29:42 +00001361ExprResult Parser::ParseThrowExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +00001362 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattner50dd2892008-02-26 00:51:44 +00001363 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redl20df9b72008-12-11 22:51:44 +00001364
Chris Lattner2a2819a2008-04-06 06:02:23 +00001365 // If the current token isn't the start of an assignment-expression,
1366 // then the expression is not present. This handles things like:
1367 // "C ? throw : (void)42", which is crazy but legal.
1368 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
1369 case tok::semi:
1370 case tok::r_paren:
1371 case tok::r_square:
1372 case tok::r_brace:
1373 case tok::colon:
1374 case tok::comma:
Douglas Gregorbca01b42011-07-06 22:04:06 +00001375 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0);
Chris Lattner50dd2892008-02-26 00:51:44 +00001376
Chris Lattner2a2819a2008-04-06 06:02:23 +00001377 default:
John McCall60d7b3a2010-08-24 06:29:42 +00001378 ExprResult Expr(ParseAssignmentExpression());
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001379 if (Expr.isInvalid()) return Expr;
Douglas Gregorbca01b42011-07-06 22:04:06 +00001380 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take());
Chris Lattner2a2819a2008-04-06 06:02:23 +00001381 }
Chris Lattner50dd2892008-02-26 00:51:44 +00001382}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001383
1384/// ParseCXXThis - This handles the C++ 'this' pointer.
1385///
1386/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
1387/// a non-lvalue expression whose value is the address of the object for which
1388/// the function is called.
John McCall60d7b3a2010-08-24 06:29:42 +00001389ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001390 assert(Tok.is(tok::kw_this) && "Not 'this'!");
1391 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redlf53597f2009-03-15 17:47:39 +00001392 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001393}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001394
1395/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1396/// Can be interpreted either as function-style casting ("int(x)")
1397/// or class type construction ("ClassType(x,y,z)")
1398/// or creation of a value-initialized type ("int()").
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001399/// See [C++ 5.2.3].
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001400///
1401/// postfix-expression: [C++ 5.2p1]
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001402/// simple-type-specifier '(' expression-list[opt] ')'
1403/// [C++0x] simple-type-specifier braced-init-list
1404/// typename-specifier '(' expression-list[opt] ')'
1405/// [C++0x] typename-specifier braced-init-list
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001406///
John McCall60d7b3a2010-08-24 06:29:42 +00001407ExprResult
Sebastian Redl20df9b72008-12-11 22:51:44 +00001408Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001409 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallb3d87482010-08-24 05:47:05 +00001410 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001411
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001412 assert((Tok.is(tok::l_paren) ||
Richard Smith80ad52f2013-01-02 11:42:31 +00001413 (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)))
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001414 && "Expected '(' or '{'!");
Douglas Gregorbc61bd82011-01-11 00:33:19 +00001415
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001416 if (Tok.is(tok::l_brace)) {
Sebastian Redl6dc00f62012-02-12 18:41:05 +00001417 ExprResult Init = ParseBraceInitializer();
1418 if (Init.isInvalid())
1419 return Init;
1420 Expr *InitList = Init.take();
1421 return Actions.ActOnCXXTypeConstructExpr(TypeRep, SourceLocation(),
1422 MultiExprArg(&InitList, 1),
1423 SourceLocation());
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001424 } else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001425 BalancedDelimiterTracker T(*this, tok::l_paren);
1426 T.consumeOpen();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001427
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001428 ExprVector Exprs;
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001429 CommaLocsTy CommaLocs;
1430
1431 if (Tok.isNot(tok::r_paren)) {
1432 if (ParseExpressionList(Exprs, CommaLocs)) {
1433 SkipUntil(tok::r_paren);
1434 return ExprError();
1435 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001436 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001437
1438 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001439 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001440
1441 // TypeRep could be null, if it references an invalid typedef.
1442 if (!TypeRep)
1443 return ExprError();
1444
1445 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1446 "Unexpected number of commas!");
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001447 return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001448 Exprs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001449 T.getCloseLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001450 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001451}
1452
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001453/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001454///
1455/// condition:
1456/// expression
1457/// type-specifier-seq declarator '=' assignment-expression
Richard Smith0635aa72012-02-22 06:49:09 +00001458/// [C++11] type-specifier-seq declarator '=' initializer-clause
1459/// [C++11] type-specifier-seq declarator braced-init-list
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001460/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
1461/// '=' assignment-expression
1462///
Dmitri Gribenko1ddbd892012-08-24 00:01:24 +00001463/// \param ExprOut if the condition was parsed as an expression, the parsed
1464/// expression.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001465///
Dmitri Gribenko1ddbd892012-08-24 00:01:24 +00001466/// \param DeclOut if the condition was parsed as a declaration, the parsed
1467/// declaration.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001468///
Douglas Gregor586596f2010-05-06 17:25:47 +00001469/// \param Loc The location of the start of the statement that requires this
1470/// condition, e.g., the "for" in a for loop.
1471///
1472/// \param ConvertToBoolean Whether the condition expression should be
1473/// converted to a boolean value.
1474///
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001475/// \returns true if there was a parsing, false otherwise.
John McCall60d7b3a2010-08-24 06:29:42 +00001476bool Parser::ParseCXXCondition(ExprResult &ExprOut,
1477 Decl *&DeclOut,
Douglas Gregor586596f2010-05-06 17:25:47 +00001478 SourceLocation Loc,
1479 bool ConvertToBoolean) {
Douglas Gregor01dfea02010-01-10 23:08:15 +00001480 if (Tok.is(tok::code_completion)) {
John McCallf312b1e2010-08-26 23:41:50 +00001481 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001482 cutOffParsing();
1483 return true;
Douglas Gregor01dfea02010-01-10 23:08:15 +00001484 }
1485
Sean Hunt2edf0a22012-06-23 05:07:58 +00001486 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00001487 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00001488
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001489 if (!isCXXConditionDeclaration()) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001490 ProhibitAttributes(attrs);
1491
Douglas Gregor586596f2010-05-06 17:25:47 +00001492 // Parse the expression.
John McCall60d7b3a2010-08-24 06:29:42 +00001493 ExprOut = ParseExpression(); // expression
1494 DeclOut = 0;
1495 if (ExprOut.isInvalid())
Douglas Gregor586596f2010-05-06 17:25:47 +00001496 return true;
1497
1498 // If required, convert to a boolean value.
1499 if (ConvertToBoolean)
John McCall60d7b3a2010-08-24 06:29:42 +00001500 ExprOut
1501 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
1502 return ExprOut.isInvalid();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001503 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001504
1505 // type-specifier-seq
John McCall0b7e6782011-03-24 11:26:52 +00001506 DeclSpec DS(AttrFactory);
Richard Smith6b3d3e52013-02-20 19:22:51 +00001507 DS.takeAttributesFrom(attrs);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001508 ParseSpecifierQualifierList(DS);
1509
1510 // declarator
1511 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
1512 ParseDeclarator(DeclaratorInfo);
1513
1514 // simple-asm-expr[opt]
1515 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00001516 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00001517 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001518 if (AsmLabel.isInvalid()) {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001519 SkipUntil(tok::semi);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001520 return true;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001521 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001522 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001523 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001524 }
1525
1526 // If attributes are present, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001527 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001528
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001529 // Type-check the declaration itself.
John McCall60d7b3a2010-08-24 06:29:42 +00001530 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall7f040a92010-12-24 02:08:15 +00001531 DeclaratorInfo);
John McCall60d7b3a2010-08-24 06:29:42 +00001532 DeclOut = Dcl.get();
1533 ExprOut = ExprError();
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001534
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001535 // '=' assignment-expression
Richard Trieud6c7c672012-01-18 22:54:52 +00001536 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Smith0635aa72012-02-22 06:49:09 +00001537 bool CopyInitialization = isTokenEqualOrEqualTypo();
1538 if (CopyInitialization)
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001539 ConsumeToken();
Richard Smith0635aa72012-02-22 06:49:09 +00001540
1541 ExprResult InitExpr = ExprError();
Richard Smith80ad52f2013-01-02 11:42:31 +00001542 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith0635aa72012-02-22 06:49:09 +00001543 Diag(Tok.getLocation(),
1544 diag::warn_cxx98_compat_generalized_initializer_lists);
1545 InitExpr = ParseBraceInitializer();
1546 } else if (CopyInitialization) {
1547 InitExpr = ParseAssignmentExpression();
1548 } else if (Tok.is(tok::l_paren)) {
1549 // This was probably an attempt to initialize the variable.
1550 SourceLocation LParen = ConsumeParen(), RParen = LParen;
1551 if (SkipUntil(tok::r_paren, true, /*DontConsume=*/true))
1552 RParen = ConsumeParen();
1553 Diag(DeclOut ? DeclOut->getLocation() : LParen,
1554 diag::err_expected_init_in_condition_lparen)
1555 << SourceRange(LParen, RParen);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001556 } else {
Richard Smith0635aa72012-02-22 06:49:09 +00001557 Diag(DeclOut ? DeclOut->getLocation() : Tok.getLocation(),
1558 diag::err_expected_init_in_condition);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001559 }
Richard Smith0635aa72012-02-22 06:49:09 +00001560
1561 if (!InitExpr.isInvalid())
1562 Actions.AddInitializerToDecl(DeclOut, InitExpr.take(), !CopyInitialization,
Richard Smitha2c36462013-04-26 16:15:35 +00001563 DS.containsPlaceholderType());
Richard Smithdc7a4f52013-04-30 13:56:41 +00001564 else
1565 Actions.ActOnInitializerError(DeclOut);
Richard Smith0635aa72012-02-22 06:49:09 +00001566
Douglas Gregor586596f2010-05-06 17:25:47 +00001567 // FIXME: Build a reference to this declaration? Convert it to bool?
1568 // (This is currently handled by Sema).
Richard Smith483b9f32011-02-21 20:05:19 +00001569
1570 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregor586596f2010-05-06 17:25:47 +00001571
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001572 return false;
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001573}
1574
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001575/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1576/// This should only be called when the current token is known to be part of
1577/// simple-type-specifier.
1578///
1579/// simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001580/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001581/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1582/// char
1583/// wchar_t
1584/// bool
1585/// short
1586/// int
1587/// long
1588/// signed
1589/// unsigned
1590/// float
1591/// double
1592/// void
1593/// [GNU] typeof-specifier
1594/// [C++0x] auto [TODO]
1595///
1596/// type-name:
1597/// class-name
1598/// enum-name
1599/// typedef-name
1600///
1601void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1602 DS.SetRangeStart(Tok.getLocation());
1603 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001604 unsigned DiagID;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001605 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001606
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001607 switch (Tok.getKind()) {
Chris Lattner55a7cef2009-01-05 00:13:00 +00001608 case tok::identifier: // foo::bar
1609 case tok::coloncolon: // ::foo::bar
David Blaikieb219cfc2011-09-23 05:06:16 +00001610 llvm_unreachable("Annotation token should already be formed!");
Mike Stump1eb44332009-09-09 15:08:12 +00001611 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00001612 llvm_unreachable("Not a simple-type-specifier token!");
Chris Lattner55a7cef2009-01-05 00:13:00 +00001613
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001614 // type-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001615 case tok::annot_typename: {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001616 if (getTypeAnnotation(Tok))
1617 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
1618 getTypeAnnotation(Tok));
1619 else
1620 DS.SetTypeSpecError();
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001621
1622 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1623 ConsumeToken();
1624
1625 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1626 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1627 // Objective-C interface. If we don't have Objective-C or a '<', this is
1628 // just a normal reference to a typedef name.
David Blaikie4e4d0842012-03-11 07:00:24 +00001629 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001630 ParseObjCProtocolQualifiers(DS);
1631
1632 DS.Finish(Diags, PP);
1633 return;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001634 }
Mike Stump1eb44332009-09-09 15:08:12 +00001635
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001636 // builtin types
1637 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001638 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001639 break;
1640 case tok::kw_long:
John McCallfec54012009-08-03 20:12:06 +00001641 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001642 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00001643 case tok::kw___int64:
1644 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1645 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001646 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001647 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001648 break;
1649 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001650 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001651 break;
1652 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001653 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001654 break;
1655 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001656 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001657 break;
1658 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001659 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001660 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00001661 case tok::kw___int128:
1662 DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, DiagID);
1663 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001664 case tok::kw_half:
1665 DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
1666 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001667 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001668 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001669 break;
1670 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001671 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001672 break;
1673 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001674 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001675 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001676 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001677 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001678 break;
1679 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001680 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001681 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001682 case tok::kw_bool:
John McCallfec54012009-08-03 20:12:06 +00001683 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001684 break;
David Blaikie5e089fe2012-01-24 05:47:35 +00001685 case tok::annot_decltype:
1686 case tok::kw_decltype:
1687 DS.SetRangeEnd(ParseDecltypeSpecifier(DS));
1688 return DS.Finish(Diags, PP);
Mike Stump1eb44332009-09-09 15:08:12 +00001689
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001690 // GNU typeof support.
1691 case tok::kw_typeof:
1692 ParseTypeofSpecifier(DS);
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001693 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001694 return;
1695 }
Chris Lattnerb31757b2009-01-06 05:06:21 +00001696 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001697 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1698 else
1699 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001700 ConsumeToken();
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001701 DS.Finish(Diags, PP);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001702}
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001703
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001704/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1705/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1706/// e.g., "const short int". Note that the DeclSpec is *not* finished
1707/// by parsing the type-specifier-seq, because these sequences are
1708/// typically followed by some form of declarator. Returns true and
1709/// emits diagnostics if this is not a type-specifier-seq, false
1710/// otherwise.
1711///
1712/// type-specifier-seq: [C++ 8.1]
1713/// type-specifier type-specifier-seq[opt]
1714///
1715bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
Richard Smith69730c12012-03-12 07:56:15 +00001716 ParseSpecifierQualifierList(DS, AS_none, DSC_type_specifier);
Douglas Gregor396a9f22010-02-24 23:13:13 +00001717 DS.Finish(Diags, PP);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001718 return false;
1719}
1720
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001721/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1722/// some form.
1723///
1724/// This routine is invoked when a '<' is encountered after an identifier or
1725/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1726/// whether the unqualified-id is actually a template-id. This routine will
1727/// then parse the template arguments and form the appropriate template-id to
1728/// return to the caller.
1729///
1730/// \param SS the nested-name-specifier that precedes this template-id, if
1731/// we're actually parsing a qualified-id.
1732///
1733/// \param Name for constructor and destructor names, this is the actual
1734/// identifier that may be a template-name.
1735///
1736/// \param NameLoc the location of the class-name in a constructor or
1737/// destructor.
1738///
1739/// \param EnteringContext whether we're entering the scope of the
1740/// nested-name-specifier.
1741///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00001742/// \param ObjectType if this unqualified-id occurs within a member access
1743/// expression, the type of the base object whose member is being accessed.
1744///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001745/// \param Id as input, describes the template-name or operator-function-id
1746/// that precedes the '<'. If template arguments were parsed successfully,
1747/// will be updated with the template-id.
1748///
Douglas Gregord4dca082010-02-24 18:44:31 +00001749/// \param AssumeTemplateId When true, this routine will assume that the name
1750/// refers to a template without performing name lookup to verify.
1751///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001752/// \returns true if a parse error occurred, false otherwise.
1753bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001754 SourceLocation TemplateKWLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001755 IdentifierInfo *Name,
1756 SourceLocation NameLoc,
1757 bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001758 ParsedType ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +00001759 UnqualifiedId &Id,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001760 bool AssumeTemplateId) {
Douglas Gregor0278e122010-05-05 05:58:24 +00001761 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1762 "Expected '<' to finish parsing a template-id");
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001763
1764 TemplateTy Template;
1765 TemplateNameKind TNK = TNK_Non_template;
1766 switch (Id.getKind()) {
1767 case UnqualifiedId::IK_Identifier:
Douglas Gregor014e88d2009-11-03 23:16:33 +00001768 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunte6252d12009-11-28 08:58:14 +00001769 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregord4dca082010-02-24 18:44:31 +00001770 if (AssumeTemplateId) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001771 TNK = Actions.ActOnDependentTemplateName(getCurScope(), SS, TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001772 Id, ObjectType, EnteringContext,
1773 Template);
1774 if (TNK == TNK_Non_template)
1775 return true;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001776 } else {
1777 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001778 TNK = Actions.isTemplateName(getCurScope(), SS,
1779 TemplateKWLoc.isValid(), Id,
1780 ObjectType, EnteringContext, Template,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001781 MemberOfUnknownSpecialization);
1782
1783 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1784 ObjectType && IsTemplateArgumentList()) {
1785 // We have something like t->getAs<T>(), where getAs is a
1786 // member of an unknown specialization. However, this will only
1787 // parse correctly as a template, so suggest the keyword 'template'
1788 // before 'getAs' and treat this as a dependent template name.
1789 std::string Name;
1790 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1791 Name = Id.Identifier->getName();
1792 else {
1793 Name = "operator ";
1794 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1795 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1796 else
1797 Name += Id.Identifier->getName();
1798 }
1799 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1800 << Name
1801 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001802 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1803 SS, TemplateKWLoc, Id,
1804 ObjectType, EnteringContext,
1805 Template);
Douglas Gregord6ab2322010-06-16 23:00:59 +00001806 if (TNK == TNK_Non_template)
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001807 return true;
1808 }
1809 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001810 break;
1811
Douglas Gregor014e88d2009-11-03 23:16:33 +00001812 case UnqualifiedId::IK_ConstructorName: {
1813 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001814 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001815 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c153532010-08-06 12:11:11 +00001816 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1817 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001818 EnteringContext, Template,
1819 MemberOfUnknownSpecialization);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001820 break;
1821 }
1822
Douglas Gregor014e88d2009-11-03 23:16:33 +00001823 case UnqualifiedId::IK_DestructorName: {
1824 UnqualifiedId TemplateName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001825 bool MemberOfUnknownSpecialization;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001826 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001827 if (ObjectType) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001828 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1829 SS, TemplateKWLoc, TemplateName,
1830 ObjectType, EnteringContext,
1831 Template);
Douglas Gregord6ab2322010-06-16 23:00:59 +00001832 if (TNK == TNK_Non_template)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001833 return true;
1834 } else {
Abramo Bagnara7c153532010-08-06 12:11:11 +00001835 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1836 TemplateName, ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001837 EnteringContext, Template,
1838 MemberOfUnknownSpecialization);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001839
John McCallb3d87482010-08-24 05:47:05 +00001840 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001841 Diag(NameLoc, diag::err_destructor_template_id)
1842 << Name << SS.getRange();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001843 return true;
1844 }
1845 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001846 break;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001847 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001848
1849 default:
1850 return false;
1851 }
1852
1853 if (TNK == TNK_Non_template)
1854 return false;
1855
1856 // Parse the enclosed template argument list.
1857 SourceLocation LAngleLoc, RAngleLoc;
1858 TemplateArgList TemplateArgs;
Douglas Gregor0278e122010-05-05 05:58:24 +00001859 if (Tok.is(tok::less) &&
1860 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregor059101f2011-03-02 00:47:37 +00001861 SS, true, LAngleLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001862 TemplateArgs,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001863 RAngleLoc))
1864 return true;
1865
1866 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Sean Hunte6252d12009-11-28 08:58:14 +00001867 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1868 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001869 // Form a parsed representation of the template-id to be stored in the
1870 // UnqualifiedId.
1871 TemplateIdAnnotation *TemplateId
Benjamin Kramer13bb7012012-04-14 12:14:03 +00001872 = TemplateIdAnnotation::Allocate(TemplateArgs.size(), TemplateIds);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001873
1874 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1875 TemplateId->Name = Id.Identifier;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001876 TemplateId->Operator = OO_None;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001877 TemplateId->TemplateNameLoc = Id.StartLocation;
1878 } else {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001879 TemplateId->Name = 0;
1880 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1881 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001882 }
1883
Douglas Gregor059101f2011-03-02 00:47:37 +00001884 TemplateId->SS = SS;
Benjamin Kramer2b28bf12012-02-19 23:37:39 +00001885 TemplateId->TemplateKWLoc = TemplateKWLoc;
John McCall2b5289b2010-08-23 07:28:44 +00001886 TemplateId->Template = Template;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001887 TemplateId->Kind = TNK;
1888 TemplateId->LAngleLoc = LAngleLoc;
1889 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregor314b97f2009-11-10 19:49:08 +00001890 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001891 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregor314b97f2009-11-10 19:49:08 +00001892 Arg != ArgEnd; ++Arg)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001893 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001894
1895 Id.setTemplateId(TemplateId);
1896 return false;
1897 }
1898
1899 // Bundle the template arguments together.
Benjamin Kramer5354e772012-08-23 23:38:35 +00001900 ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs);
Abramo Bagnarafad03b72012-01-27 08:46:19 +00001901
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001902 // Constructor and destructor names.
John McCallf312b1e2010-08-26 23:41:50 +00001903 TypeResult Type
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001904 = Actions.ActOnTemplateIdType(SS, TemplateKWLoc,
1905 Template, NameLoc,
Abramo Bagnarafad03b72012-01-27 08:46:19 +00001906 LAngleLoc, TemplateArgsPtr, RAngleLoc,
1907 /*IsCtorOrDtorName=*/true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001908 if (Type.isInvalid())
1909 return true;
1910
1911 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1912 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1913 else
1914 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1915
1916 return false;
1917}
1918
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001919/// \brief Parse an operator-function-id or conversion-function-id as part
1920/// of a C++ unqualified-id.
1921///
1922/// This routine is responsible only for parsing the operator-function-id or
1923/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001924///
1925/// \code
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001926/// operator-function-id: [C++ 13.5]
1927/// 'operator' operator
1928///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001929/// operator: one of
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001930/// new delete new[] delete[]
1931/// + - * / % ^ & | ~
1932/// ! = < > += -= *= /= %=
1933/// ^= &= |= << >> >>= <<= == !=
1934/// <= >= && || ++ -- , ->* ->
1935/// () []
1936///
1937/// conversion-function-id: [C++ 12.3.2]
1938/// operator conversion-type-id
1939///
1940/// conversion-type-id:
1941/// type-specifier-seq conversion-declarator[opt]
1942///
1943/// conversion-declarator:
1944/// ptr-operator conversion-declarator[opt]
1945/// \endcode
1946///
Dmitri Gribenko1ddbd892012-08-24 00:01:24 +00001947/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001948/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1949///
1950/// \param EnteringContext whether we are entering the scope of the
1951/// nested-name-specifier.
1952///
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001953/// \param ObjectType if this unqualified-id occurs within a member access
1954/// expression, the type of the base object whose member is being accessed.
1955///
1956/// \param Result on a successful parse, contains the parsed unqualified-id.
1957///
1958/// \returns true if parsing fails, false otherwise.
1959bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallb3d87482010-08-24 05:47:05 +00001960 ParsedType ObjectType,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001961 UnqualifiedId &Result) {
1962 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1963
1964 // Consume the 'operator' keyword.
1965 SourceLocation KeywordLoc = ConsumeToken();
1966
1967 // Determine what kind of operator name we have.
1968 unsigned SymbolIdx = 0;
1969 SourceLocation SymbolLocations[3];
1970 OverloadedOperatorKind Op = OO_None;
1971 switch (Tok.getKind()) {
1972 case tok::kw_new:
1973 case tok::kw_delete: {
1974 bool isNew = Tok.getKind() == tok::kw_new;
1975 // Consume the 'new' or 'delete'.
1976 SymbolLocations[SymbolIdx++] = ConsumeToken();
Richard Smith6ee326a2012-04-10 01:32:12 +00001977 // Check for array new/delete.
1978 if (Tok.is(tok::l_square) &&
Richard Smith80ad52f2013-01-02 11:42:31 +00001979 (!getLangOpts().CPlusPlus11 || NextToken().isNot(tok::l_square))) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001980 // Consume the '[' and ']'.
1981 BalancedDelimiterTracker T(*this, tok::l_square);
1982 T.consumeOpen();
1983 T.consumeClose();
1984 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001985 return true;
1986
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001987 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
1988 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001989 Op = isNew? OO_Array_New : OO_Array_Delete;
1990 } else {
1991 Op = isNew? OO_New : OO_Delete;
1992 }
1993 break;
1994 }
1995
1996#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1997 case tok::Token: \
1998 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
1999 Op = OO_##Name; \
2000 break;
2001#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
2002#include "clang/Basic/OperatorKinds.def"
2003
2004 case tok::l_paren: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002005 // Consume the '(' and ')'.
2006 BalancedDelimiterTracker T(*this, tok::l_paren);
2007 T.consumeOpen();
2008 T.consumeClose();
2009 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002010 return true;
2011
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002012 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
2013 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002014 Op = OO_Call;
2015 break;
2016 }
2017
2018 case tok::l_square: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002019 // Consume the '[' and ']'.
2020 BalancedDelimiterTracker T(*this, tok::l_square);
2021 T.consumeOpen();
2022 T.consumeClose();
2023 if (T.getCloseLocation().isInvalid())
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002024 return true;
2025
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002026 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
2027 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002028 Op = OO_Subscript;
2029 break;
2030 }
2031
2032 case tok::code_completion: {
2033 // Code completion for the operator name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002034 Actions.CodeCompleteOperatorName(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002035 cutOffParsing();
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002036 // Don't try to parse any further.
2037 return true;
2038 }
2039
2040 default:
2041 break;
2042 }
2043
2044 if (Op != OO_None) {
2045 // We have parsed an operator-function-id.
2046 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
2047 return false;
2048 }
Sean Hunt0486d742009-11-28 04:44:28 +00002049
2050 // Parse a literal-operator-id.
2051 //
Richard Smithaa9a8ce2012-10-20 08:41:10 +00002052 // literal-operator-id: C++11 [over.literal]
2053 // operator string-literal identifier
2054 // operator user-defined-string-literal
Sean Hunt0486d742009-11-28 04:44:28 +00002055
Richard Smith80ad52f2013-01-02 11:42:31 +00002056 if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
Richard Smith7fe62082011-10-15 05:09:34 +00002057 Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator);
Sean Hunt0486d742009-11-28 04:44:28 +00002058
Richard Smith33762772012-03-08 23:06:02 +00002059 SourceLocation DiagLoc;
2060 unsigned DiagId = 0;
2061
2062 // We're past translation phase 6, so perform string literal concatenation
2063 // before checking for "".
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002064 SmallVector<Token, 4> Toks;
2065 SmallVector<SourceLocation, 4> TokLocs;
Richard Smith33762772012-03-08 23:06:02 +00002066 while (isTokenStringLiteral()) {
2067 if (!Tok.is(tok::string_literal) && !DiagId) {
Richard Smithaa9a8ce2012-10-20 08:41:10 +00002068 // C++11 [over.literal]p1:
2069 // The string-literal or user-defined-string-literal in a
2070 // literal-operator-id shall have no encoding-prefix [...].
Richard Smith33762772012-03-08 23:06:02 +00002071 DiagLoc = Tok.getLocation();
2072 DiagId = diag::err_literal_operator_string_prefix;
2073 }
2074 Toks.push_back(Tok);
2075 TokLocs.push_back(ConsumeStringToken());
2076 }
2077
2078 StringLiteralParser Literal(Toks.data(), Toks.size(), PP);
2079 if (Literal.hadError)
2080 return true;
2081
2082 // Grab the literal operator's suffix, which will be either the next token
2083 // or a ud-suffix from the string literal.
2084 IdentifierInfo *II = 0;
2085 SourceLocation SuffixLoc;
2086 if (!Literal.getUDSuffix().empty()) {
2087 II = &PP.getIdentifierTable().get(Literal.getUDSuffix());
2088 SuffixLoc =
2089 Lexer::AdvanceToTokenCharacter(TokLocs[Literal.getUDSuffixToken()],
2090 Literal.getUDSuffixOffset(),
David Blaikie4e4d0842012-03-11 07:00:24 +00002091 PP.getSourceManager(), getLangOpts());
Richard Smith33762772012-03-08 23:06:02 +00002092 } else if (Tok.is(tok::identifier)) {
2093 II = Tok.getIdentifierInfo();
2094 SuffixLoc = ConsumeToken();
2095 TokLocs.push_back(SuffixLoc);
2096 } else {
Sean Hunt0486d742009-11-28 04:44:28 +00002097 Diag(Tok.getLocation(), diag::err_expected_ident);
2098 return true;
2099 }
2100
Richard Smith33762772012-03-08 23:06:02 +00002101 // The string literal must be empty.
2102 if (!Literal.GetString().empty() || Literal.Pascal) {
Richard Smithaa9a8ce2012-10-20 08:41:10 +00002103 // C++11 [over.literal]p1:
2104 // The string-literal or user-defined-string-literal in a
2105 // literal-operator-id shall [...] contain no characters
2106 // other than the implicit terminating '\0'.
Richard Smith33762772012-03-08 23:06:02 +00002107 DiagLoc = TokLocs.front();
2108 DiagId = diag::err_literal_operator_string_not_empty;
2109 }
2110
2111 if (DiagId) {
2112 // This isn't a valid literal-operator-id, but we think we know
2113 // what the user meant. Tell them what they should have written.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002114 SmallString<32> Str;
Richard Smith33762772012-03-08 23:06:02 +00002115 Str += "\"\" ";
2116 Str += II->getName();
2117 Diag(DiagLoc, DiagId) << FixItHint::CreateReplacement(
2118 SourceRange(TokLocs.front(), TokLocs.back()), Str);
2119 }
2120
2121 Result.setLiteralOperatorId(II, KeywordLoc, SuffixLoc);
Sean Hunt3e518bd2009-11-29 07:34:05 +00002122 return false;
Sean Hunt0486d742009-11-28 04:44:28 +00002123 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002124
2125 // Parse a conversion-function-id.
2126 //
2127 // conversion-function-id: [C++ 12.3.2]
2128 // operator conversion-type-id
2129 //
2130 // conversion-type-id:
2131 // type-specifier-seq conversion-declarator[opt]
2132 //
2133 // conversion-declarator:
2134 // ptr-operator conversion-declarator[opt]
2135
2136 // Parse the type-specifier-seq.
John McCall0b7e6782011-03-24 11:26:52 +00002137 DeclSpec DS(AttrFactory);
Douglas Gregorf6e6fc82009-11-20 22:03:38 +00002138 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002139 return true;
2140
2141 // Parse the conversion-declarator, which is merely a sequence of
2142 // ptr-operators.
Richard Smith14f78f42013-05-04 01:26:46 +00002143 Declarator D(DS, Declarator::ConversionIdContext);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002144 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
2145
2146 // Finish up the type.
John McCallf312b1e2010-08-26 23:41:50 +00002147 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002148 if (Ty.isInvalid())
2149 return true;
2150
2151 // Note that this is a conversion-function-id.
2152 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
2153 D.getSourceRange().getEnd());
2154 return false;
2155}
2156
2157/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
2158/// name of an entity.
2159///
2160/// \code
2161/// unqualified-id: [C++ expr.prim.general]
2162/// identifier
2163/// operator-function-id
2164/// conversion-function-id
2165/// [C++0x] literal-operator-id [TODO]
2166/// ~ class-name
2167/// template-id
2168///
2169/// \endcode
2170///
Dmitri Gribenko1ddbd892012-08-24 00:01:24 +00002171/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002172/// non-empty, then we are parsing the unqualified-id of a qualified-id.
2173///
2174/// \param EnteringContext whether we are entering the scope of the
2175/// nested-name-specifier.
2176///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002177/// \param AllowDestructorName whether we allow parsing of a destructor name.
2178///
2179/// \param AllowConstructorName whether we allow parsing a constructor name.
2180///
Douglas Gregor46df8cc2009-11-03 21:24:04 +00002181/// \param ObjectType if this unqualified-id occurs within a member access
2182/// expression, the type of the base object whose member is being accessed.
2183///
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002184/// \param Result on a successful parse, contains the parsed unqualified-id.
2185///
2186/// \returns true if parsing fails, false otherwise.
2187bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
2188 bool AllowDestructorName,
2189 bool AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00002190 ParsedType ObjectType,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002191 SourceLocation& TemplateKWLoc,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002192 UnqualifiedId &Result) {
Douglas Gregor0278e122010-05-05 05:58:24 +00002193
2194 // Handle 'A::template B'. This is for template-ids which have not
2195 // already been annotated by ParseOptionalCXXScopeSpecifier().
2196 bool TemplateSpecified = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00002197 if (getLangOpts().CPlusPlus && Tok.is(tok::kw_template) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00002198 (ObjectType || SS.isSet())) {
2199 TemplateSpecified = true;
2200 TemplateKWLoc = ConsumeToken();
2201 }
2202
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002203 // unqualified-id:
2204 // identifier
2205 // template-id (when it hasn't already been annotated)
2206 if (Tok.is(tok::identifier)) {
2207 // Consume the identifier.
2208 IdentifierInfo *Id = Tok.getIdentifierInfo();
2209 SourceLocation IdLoc = ConsumeToken();
2210
David Blaikie4e4d0842012-03-11 07:00:24 +00002211 if (!getLangOpts().CPlusPlus) {
Douglas Gregorb862b8f2010-01-11 23:29:10 +00002212 // If we're not in C++, only identifiers matter. Record the
2213 // identifier and return.
2214 Result.setIdentifier(Id, IdLoc);
2215 return false;
2216 }
2217
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002218 if (AllowConstructorName &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002219 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002220 // We have parsed a constructor name.
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002221 ParsedType Ty = Actions.getTypeName(*Id, IdLoc, getCurScope(),
2222 &SS, false, false,
2223 ParsedType(),
2224 /*IsCtorOrDtorName=*/true,
2225 /*NonTrivialTypeSourceInfo=*/true);
2226 Result.setConstructorName(Ty, IdLoc, IdLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002227 } else {
2228 // We have parsed an identifier.
2229 Result.setIdentifier(Id, IdLoc);
2230 }
2231
2232 // If the next token is a '<', we may have a template.
Douglas Gregor0278e122010-05-05 05:58:24 +00002233 if (TemplateSpecified || Tok.is(tok::less))
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002234 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc, Id, IdLoc,
2235 EnteringContext, ObjectType,
2236 Result, TemplateSpecified);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002237
2238 return false;
2239 }
2240
2241 // unqualified-id:
2242 // template-id (already parsed and annotated)
2243 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002244 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002245
2246 // If the template-name names the current class, then this is a constructor
2247 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002248 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002249 if (SS.isSet()) {
2250 // C++ [class.qual]p2 specifies that a qualified template-name
2251 // is taken as the constructor name where a constructor can be
2252 // declared. Thus, the template arguments are extraneous, so
2253 // complain about them and remove them entirely.
2254 Diag(TemplateId->TemplateNameLoc,
2255 diag::err_out_of_line_constructor_template_id)
2256 << TemplateId->Name
Douglas Gregor849b2432010-03-31 17:46:05 +00002257 << FixItHint::CreateRemoval(
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002258 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002259 ParsedType Ty = Actions.getTypeName(*TemplateId->Name,
2260 TemplateId->TemplateNameLoc,
2261 getCurScope(),
2262 &SS, false, false,
2263 ParsedType(),
2264 /*IsCtorOrDtorName=*/true,
2265 /*NontrivialTypeSourceInfo=*/true);
2266 Result.setConstructorName(Ty, TemplateId->TemplateNameLoc,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002267 TemplateId->RAngleLoc);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002268 ConsumeToken();
2269 return false;
2270 }
2271
2272 Result.setConstructorTemplateId(TemplateId);
2273 ConsumeToken();
2274 return false;
2275 }
2276
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002277 // We have already parsed a template-id; consume the annotation token as
2278 // our unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002279 Result.setTemplateId(TemplateId);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002280 TemplateKWLoc = TemplateId->TemplateKWLoc;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002281 ConsumeToken();
2282 return false;
2283 }
2284
2285 // unqualified-id:
2286 // operator-function-id
2287 // conversion-function-id
2288 if (Tok.is(tok::kw_operator)) {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002289 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002290 return true;
2291
Sean Hunte6252d12009-11-28 08:58:14 +00002292 // If we have an operator-function-id or a literal-operator-id and the next
2293 // token is a '<', we may have a
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002294 //
2295 // template-id:
2296 // operator-function-id < template-argument-list[opt] >
Sean Hunte6252d12009-11-28 08:58:14 +00002297 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
2298 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregor0278e122010-05-05 05:58:24 +00002299 (TemplateSpecified || Tok.is(tok::less)))
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002300 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2301 0, SourceLocation(),
2302 EnteringContext, ObjectType,
2303 Result, TemplateSpecified);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002304
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002305 return false;
2306 }
2307
David Blaikie4e4d0842012-03-11 07:00:24 +00002308 if (getLangOpts().CPlusPlus &&
Douglas Gregorb862b8f2010-01-11 23:29:10 +00002309 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002310 // C++ [expr.unary.op]p10:
2311 // There is an ambiguity in the unary-expression ~X(), where X is a
2312 // class-name. The ambiguity is resolved in favor of treating ~ as a
2313 // unary complement rather than treating ~X as referring to a destructor.
2314
2315 // Parse the '~'.
2316 SourceLocation TildeLoc = ConsumeToken();
David Blaikie53a75c02011-12-08 16:13:53 +00002317
2318 if (SS.isEmpty() && Tok.is(tok::kw_decltype)) {
2319 DeclSpec DS(AttrFactory);
2320 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
2321 if (ParsedType Type = Actions.getDestructorType(DS, ObjectType)) {
2322 Result.setDestructorName(TildeLoc, Type, EndLoc);
2323 return false;
2324 }
2325 return true;
2326 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002327
2328 // Parse the class-name.
2329 if (Tok.isNot(tok::identifier)) {
Douglas Gregor124b8782010-02-16 19:09:40 +00002330 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002331 return true;
2332 }
2333
2334 // Parse the class-name (or template-name in a simple-template-id).
2335 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
2336 SourceLocation ClassNameLoc = ConsumeToken();
2337
Douglas Gregor0278e122010-05-05 05:58:24 +00002338 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallb3d87482010-08-24 05:47:05 +00002339 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002340 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2341 ClassName, ClassNameLoc,
2342 EnteringContext, ObjectType,
2343 Result, TemplateSpecified);
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002344 }
2345
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002346 // Note that this is a destructor name.
John McCallb3d87482010-08-24 05:47:05 +00002347 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
2348 ClassNameLoc, getCurScope(),
2349 SS, ObjectType,
2350 EnteringContext);
Douglas Gregor124b8782010-02-16 19:09:40 +00002351 if (!Ty)
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002352 return true;
Douglas Gregor124b8782010-02-16 19:09:40 +00002353
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002354 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002355 return false;
2356 }
2357
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002358 Diag(Tok, diag::err_expected_unqualified_id)
David Blaikie4e4d0842012-03-11 07:00:24 +00002359 << getLangOpts().CPlusPlus;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002360 return true;
2361}
2362
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002363/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
2364/// memory in a typesafe manner and call constructors.
Mike Stump1eb44332009-09-09 15:08:12 +00002365///
Chris Lattner59232d32009-01-04 21:25:24 +00002366/// This method is called to parse the new expression after the optional :: has
2367/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
2368/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002369///
2370/// new-expression:
2371/// '::'[opt] 'new' new-placement[opt] new-type-id
2372/// new-initializer[opt]
2373/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2374/// new-initializer[opt]
2375///
2376/// new-placement:
2377/// '(' expression-list ')'
2378///
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002379/// new-type-id:
2380/// type-specifier-seq new-declarator[opt]
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002381/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002382///
2383/// new-declarator:
2384/// ptr-operator new-declarator[opt]
2385/// direct-new-declarator
2386///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002387/// new-initializer:
2388/// '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002389/// [C++0x] braced-init-list
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002390///
John McCall60d7b3a2010-08-24 06:29:42 +00002391ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002392Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
2393 assert(Tok.is(tok::kw_new) && "expected 'new' token");
2394 ConsumeToken(); // Consume 'new'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002395
2396 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
2397 // second form of new-expression. It can't be a new-type-id.
2398
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002399 ExprVector PlacementArgs;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002400 SourceLocation PlacementLParen, PlacementRParen;
2401
Douglas Gregor4bd40312010-07-13 15:54:32 +00002402 SourceRange TypeIdParens;
John McCall0b7e6782011-03-24 11:26:52 +00002403 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis0b8c98f2011-06-28 03:01:23 +00002404 Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002405 if (Tok.is(tok::l_paren)) {
2406 // If it turns out to be a placement, we change the type location.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002407 BalancedDelimiterTracker T(*this, tok::l_paren);
2408 T.consumeOpen();
2409 PlacementLParen = T.getOpenLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002410 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
2411 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002412 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002413 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002414
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002415 T.consumeClose();
2416 PlacementRParen = T.getCloseLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002417 if (PlacementRParen.isInvalid()) {
2418 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002419 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002420 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002421
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002422 if (PlacementArgs.empty()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002423 // Reset the placement locations. There was no placement.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002424 TypeIdParens = T.getRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002425 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002426 } else {
2427 // We still need the type.
2428 if (Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002429 BalancedDelimiterTracker T(*this, tok::l_paren);
2430 T.consumeOpen();
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002431 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002432 ParseSpecifierQualifierList(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002433 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002434 ParseDeclarator(DeclaratorInfo);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002435 T.consumeClose();
2436 TypeIdParens = T.getRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002437 } else {
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002438 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002439 if (ParseCXXTypeSpecifierSeq(DS))
2440 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002441 else {
2442 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002443 ParseDeclaratorInternal(DeclaratorInfo,
2444 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002445 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002446 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002447 }
2448 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002449 // A new-type-id is a simplified type-id, where essentially the
2450 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregor893e1cc2011-04-15 19:40:02 +00002451 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002452 if (ParseCXXTypeSpecifierSeq(DS))
2453 DeclaratorInfo.setInvalidType(true);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002454 else {
2455 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002456 ParseDeclaratorInternal(DeclaratorInfo,
2457 &Parser::ParseDirectNewDeclarator);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002458 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002459 }
Chris Lattnereaaebc72009-04-25 08:06:05 +00002460 if (DeclaratorInfo.isInvalidType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002461 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002462 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002463 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002464
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002465 ExprResult Initializer;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002466
2467 if (Tok.is(tok::l_paren)) {
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002468 SourceLocation ConstructorLParen, ConstructorRParen;
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002469 ExprVector ConstructorArgs;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002470 BalancedDelimiterTracker T(*this, tok::l_paren);
2471 T.consumeOpen();
2472 ConstructorLParen = T.getOpenLocation();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002473 if (Tok.isNot(tok::r_paren)) {
2474 CommaLocsTy CommaLocs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002475 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
2476 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002477 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002478 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002479 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002480 T.consumeClose();
2481 ConstructorRParen = T.getCloseLocation();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002482 if (ConstructorRParen.isInvalid()) {
2483 SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true);
Sebastian Redl20df9b72008-12-11 22:51:44 +00002484 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002485 }
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002486 Initializer = Actions.ActOnParenListExpr(ConstructorLParen,
2487 ConstructorRParen,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002488 ConstructorArgs);
Richard Smith80ad52f2013-01-02 11:42:31 +00002489 } else if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus11) {
Richard Smith7fe62082011-10-15 05:09:34 +00002490 Diag(Tok.getLocation(),
2491 diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002492 Initializer = ParseBraceInitializer();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002493 }
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002494 if (Initializer.isInvalid())
2495 return Initializer;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002496
Sebastian Redlf53597f2009-03-15 17:47:39 +00002497 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002498 PlacementArgs, PlacementRParen,
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002499 TypeIdParens, DeclaratorInfo, Initializer.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002500}
2501
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002502/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
2503/// passed to ParseDeclaratorInternal.
2504///
2505/// direct-new-declarator:
2506/// '[' expression ']'
2507/// direct-new-declarator '[' constant-expression ']'
2508///
Chris Lattner59232d32009-01-04 21:25:24 +00002509void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002510 // Parse the array dimensions.
2511 bool first = true;
2512 while (Tok.is(tok::l_square)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00002513 // An array-size expression can't start with a lambda.
2514 if (CheckProhibitedCXX11Attribute())
2515 continue;
2516
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002517 BalancedDelimiterTracker T(*this, tok::l_square);
2518 T.consumeOpen();
2519
John McCall60d7b3a2010-08-24 06:29:42 +00002520 ExprResult Size(first ? ParseExpression()
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002521 : ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002522 if (Size.isInvalid()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002523 // Recover
2524 SkipUntil(tok::r_square);
2525 return;
2526 }
2527 first = false;
2528
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002529 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00002530
Bill Wendlingad017fa2012-12-20 19:22:21 +00002531 // Attributes here appertain to the array type. C++11 [expr.new]p5.
Richard Smith6ee326a2012-04-10 01:32:12 +00002532 ParsedAttributes Attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00002533 MaybeParseCXX11Attributes(Attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00002534
John McCall0b7e6782011-03-24 11:26:52 +00002535 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall7f040a92010-12-24 02:08:15 +00002536 /*static=*/false, /*star=*/false,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002537 Size.release(),
2538 T.getOpenLocation(),
2539 T.getCloseLocation()),
Richard Smith6ee326a2012-04-10 01:32:12 +00002540 Attrs, T.getCloseLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002541
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002542 if (T.getCloseLocation().isInvalid())
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002543 return;
2544 }
2545}
2546
2547/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
2548/// This ambiguity appears in the syntax of the C++ new operator.
2549///
2550/// new-expression:
2551/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2552/// new-initializer[opt]
2553///
2554/// new-placement:
2555/// '(' expression-list ')'
2556///
John McCallca0408f2010-08-23 06:44:23 +00002557bool Parser::ParseExpressionListOrTypeId(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002558 SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner59232d32009-01-04 21:25:24 +00002559 Declarator &D) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002560 // The '(' was already consumed.
2561 if (isTypeIdInParens()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002562 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlab197ba2009-02-09 18:23:29 +00002563 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +00002564 ParseDeclarator(D);
Chris Lattnereaaebc72009-04-25 08:06:05 +00002565 return D.isInvalidType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002566 }
2567
2568 // It's not a type, it has to be an expression list.
2569 // Discard the comma locations - ActOnCXXNew has enough parameters.
2570 CommaLocsTy CommaLocs;
2571 return ParseExpressionList(PlacementArgs, CommaLocs);
2572}
2573
2574/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
2575/// to free memory allocated by new.
2576///
Chris Lattner59232d32009-01-04 21:25:24 +00002577/// This method is called to parse the 'delete' expression after the optional
2578/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
2579/// and "Start" is its location. Otherwise, "Start" is the location of the
2580/// 'delete' token.
2581///
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002582/// delete-expression:
2583/// '::'[opt] 'delete' cast-expression
2584/// '::'[opt] 'delete' '[' ']' cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00002585ExprResult
Chris Lattner59232d32009-01-04 21:25:24 +00002586Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
2587 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
2588 ConsumeToken(); // Consume 'delete'
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002589
2590 // Array delete?
2591 bool ArrayDelete = false;
Richard Smith6ee326a2012-04-10 01:32:12 +00002592 if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
Richard Smith950435c2012-08-09 19:01:51 +00002593 // C++11 [expr.delete]p1:
2594 // Whenever the delete keyword is followed by empty square brackets, it
2595 // shall be interpreted as [array delete].
2596 // [Footnote: A lambda expression with a lambda-introducer that consists
2597 // of empty square brackets can follow the delete keyword if
2598 // the lambda expression is enclosed in parentheses.]
2599 // FIXME: Produce a better diagnostic if the '[]' is unambiguously a
2600 // lambda-introducer.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002601 ArrayDelete = true;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002602 BalancedDelimiterTracker T(*this, tok::l_square);
2603
2604 T.consumeOpen();
2605 T.consumeClose();
2606 if (T.getCloseLocation().isInvalid())
Sebastian Redl20df9b72008-12-11 22:51:44 +00002607 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002608 }
2609
John McCall60d7b3a2010-08-24 06:29:42 +00002610 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002611 if (Operand.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002612 return Operand;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002613
John McCall9ae2f072010-08-23 23:25:46 +00002614 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002615}
Sebastian Redl64b45f72009-01-05 20:52:13 +00002616
Mike Stump1eb44332009-09-09 15:08:12 +00002617static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002618 switch(kind) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002619 default: llvm_unreachable("Not a known unary type trait.");
Sebastian Redl64b45f72009-01-05 20:52:13 +00002620 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
Joao Matos9ef98752013-03-27 01:34:16 +00002621 case tok::kw___has_nothrow_move_assign: return UTT_HasNothrowMoveAssign;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002622 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00002623 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002624 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
Joao Matos9ef98752013-03-27 01:34:16 +00002625 case tok::kw___has_trivial_move_assign: return UTT_HasTrivialMoveAssign;
Sean Hunt023df372011-05-09 18:22:59 +00002626 case tok::kw___has_trivial_constructor:
2627 return UTT_HasTrivialDefaultConstructor;
Joao Matos9ef98752013-03-27 01:34:16 +00002628 case tok::kw___has_trivial_move_constructor:
2629 return UTT_HasTrivialMoveConstructor;
John Wiegley20c0da72011-04-27 23:09:49 +00002630 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002631 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
2632 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
2633 case tok::kw___is_abstract: return UTT_IsAbstract;
John Wiegley20c0da72011-04-27 23:09:49 +00002634 case tok::kw___is_arithmetic: return UTT_IsArithmetic;
2635 case tok::kw___is_array: return UTT_IsArray;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002636 case tok::kw___is_class: return UTT_IsClass;
John Wiegley20c0da72011-04-27 23:09:49 +00002637 case tok::kw___is_complete_type: return UTT_IsCompleteType;
2638 case tok::kw___is_compound: return UTT_IsCompound;
2639 case tok::kw___is_const: return UTT_IsConst;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002640 case tok::kw___is_empty: return UTT_IsEmpty;
2641 case tok::kw___is_enum: return UTT_IsEnum;
Douglas Gregor5e9392b2011-12-03 18:14:24 +00002642 case tok::kw___is_final: return UTT_IsFinal;
John Wiegley20c0da72011-04-27 23:09:49 +00002643 case tok::kw___is_floating_point: return UTT_IsFloatingPoint;
2644 case tok::kw___is_function: return UTT_IsFunction;
2645 case tok::kw___is_fundamental: return UTT_IsFundamental;
2646 case tok::kw___is_integral: return UTT_IsIntegral;
John McCallea30e2f2012-09-25 07:32:49 +00002647 case tok::kw___is_interface_class: return UTT_IsInterfaceClass;
John Wiegley20c0da72011-04-27 23:09:49 +00002648 case tok::kw___is_lvalue_reference: return UTT_IsLvalueReference;
2649 case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
2650 case tok::kw___is_member_object_pointer: return UTT_IsMemberObjectPointer;
2651 case tok::kw___is_member_pointer: return UTT_IsMemberPointer;
2652 case tok::kw___is_object: return UTT_IsObject;
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00002653 case tok::kw___is_literal: return UTT_IsLiteral;
Chandler Carruth38402812011-04-24 02:49:28 +00002654 case tok::kw___is_literal_type: return UTT_IsLiteral;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002655 case tok::kw___is_pod: return UTT_IsPOD;
John Wiegley20c0da72011-04-27 23:09:49 +00002656 case tok::kw___is_pointer: return UTT_IsPointer;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002657 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
John Wiegley20c0da72011-04-27 23:09:49 +00002658 case tok::kw___is_reference: return UTT_IsReference;
John Wiegley20c0da72011-04-27 23:09:49 +00002659 case tok::kw___is_rvalue_reference: return UTT_IsRvalueReference;
2660 case tok::kw___is_scalar: return UTT_IsScalar;
2661 case tok::kw___is_signed: return UTT_IsSigned;
2662 case tok::kw___is_standard_layout: return UTT_IsStandardLayout;
2663 case tok::kw___is_trivial: return UTT_IsTrivial;
Sean Huntfeb375d2011-05-13 00:31:07 +00002664 case tok::kw___is_trivially_copyable: return UTT_IsTriviallyCopyable;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002665 case tok::kw___is_union: return UTT_IsUnion;
John Wiegley20c0da72011-04-27 23:09:49 +00002666 case tok::kw___is_unsigned: return UTT_IsUnsigned;
2667 case tok::kw___is_void: return UTT_IsVoid;
2668 case tok::kw___is_volatile: return UTT_IsVolatile;
Sebastian Redl64b45f72009-01-05 20:52:13 +00002669 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00002670}
2671
2672static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
2673 switch(kind) {
Francois Pichet38c2b732010-12-07 00:55:57 +00002674 default: llvm_unreachable("Not a known binary type trait");
Francois Pichetf1872372010-12-08 22:35:30 +00002675 case tok::kw___is_base_of: return BTT_IsBaseOf;
John Wiegley20c0da72011-04-27 23:09:49 +00002676 case tok::kw___is_convertible: return BTT_IsConvertible;
2677 case tok::kw___is_same: return BTT_IsSame;
Francois Pichetf1872372010-12-08 22:35:30 +00002678 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor9f361132011-01-27 20:28:01 +00002679 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002680 case tok::kw___is_trivially_assignable: return BTT_IsTriviallyAssignable;
Francois Pichet6ad6f282010-12-07 00:08:36 +00002681 }
Sebastian Redl64b45f72009-01-05 20:52:13 +00002682}
2683
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002684static TypeTrait TypeTraitFromTokKind(tok::TokenKind kind) {
2685 switch (kind) {
2686 default: llvm_unreachable("Not a known type trait");
2687 case tok::kw___is_trivially_constructible:
2688 return TT_IsTriviallyConstructible;
2689 }
2690}
2691
John Wiegley21ff2e52011-04-28 00:16:57 +00002692static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
2693 switch(kind) {
2694 default: llvm_unreachable("Not a known binary type trait");
2695 case tok::kw___array_rank: return ATT_ArrayRank;
2696 case tok::kw___array_extent: return ATT_ArrayExtent;
2697 }
2698}
2699
John Wiegley55262202011-04-25 06:54:41 +00002700static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2701 switch(kind) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002702 default: llvm_unreachable("Not a known unary expression trait.");
John Wiegley55262202011-04-25 06:54:41 +00002703 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
2704 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
2705 }
2706}
2707
Sebastian Redl64b45f72009-01-05 20:52:13 +00002708/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
2709/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2710/// templates.
2711///
2712/// primary-expression:
2713/// [GNU] unary-type-trait '(' type-id ')'
2714///
John McCall60d7b3a2010-08-24 06:29:42 +00002715ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redl64b45f72009-01-05 20:52:13 +00002716 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
2717 SourceLocation Loc = ConsumeToken();
2718
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002719 BalancedDelimiterTracker T(*this, tok::l_paren);
2720 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redl64b45f72009-01-05 20:52:13 +00002721 return ExprError();
2722
2723 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
2724 // there will be cryptic errors about mismatched parentheses and missing
2725 // specifiers.
Douglas Gregor809070a2009-02-18 17:45:20 +00002726 TypeResult Ty = ParseTypeName();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002727
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002728 T.consumeClose();
Sebastian Redl64b45f72009-01-05 20:52:13 +00002729
Douglas Gregor809070a2009-02-18 17:45:20 +00002730 if (Ty.isInvalid())
2731 return ExprError();
2732
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002733 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), T.getCloseLocation());
Sebastian Redl64b45f72009-01-05 20:52:13 +00002734}
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002735
Francois Pichet6ad6f282010-12-07 00:08:36 +00002736/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
2737/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2738/// templates.
2739///
2740/// primary-expression:
2741/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
2742///
2743ExprResult Parser::ParseBinaryTypeTrait() {
2744 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
2745 SourceLocation Loc = ConsumeToken();
2746
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002747 BalancedDelimiterTracker T(*this, tok::l_paren);
2748 if (T.expectAndConsume(diag::err_expected_lparen))
Francois Pichet6ad6f282010-12-07 00:08:36 +00002749 return ExprError();
2750
2751 TypeResult LhsTy = ParseTypeName();
2752 if (LhsTy.isInvalid()) {
2753 SkipUntil(tok::r_paren);
2754 return ExprError();
2755 }
2756
2757 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2758 SkipUntil(tok::r_paren);
2759 return ExprError();
2760 }
2761
2762 TypeResult RhsTy = ParseTypeName();
2763 if (RhsTy.isInvalid()) {
2764 SkipUntil(tok::r_paren);
2765 return ExprError();
2766 }
2767
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002768 T.consumeClose();
Francois Pichet6ad6f282010-12-07 00:08:36 +00002769
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002770 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(),
2771 T.getCloseLocation());
Francois Pichet6ad6f282010-12-07 00:08:36 +00002772}
2773
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002774/// \brief Parse the built-in type-trait pseudo-functions that allow
2775/// implementation of the TR1/C++11 type traits templates.
2776///
2777/// primary-expression:
2778/// type-trait '(' type-id-seq ')'
2779///
2780/// type-id-seq:
2781/// type-id ...[opt] type-id-seq[opt]
2782///
2783ExprResult Parser::ParseTypeTrait() {
2784 TypeTrait Kind = TypeTraitFromTokKind(Tok.getKind());
2785 SourceLocation Loc = ConsumeToken();
2786
2787 BalancedDelimiterTracker Parens(*this, tok::l_paren);
2788 if (Parens.expectAndConsume(diag::err_expected_lparen))
2789 return ExprError();
2790
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002791 SmallVector<ParsedType, 2> Args;
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002792 do {
2793 // Parse the next type.
2794 TypeResult Ty = ParseTypeName();
2795 if (Ty.isInvalid()) {
2796 Parens.skipToEnd();
2797 return ExprError();
2798 }
2799
2800 // Parse the ellipsis, if present.
2801 if (Tok.is(tok::ellipsis)) {
2802 Ty = Actions.ActOnPackExpansion(Ty.get(), ConsumeToken());
2803 if (Ty.isInvalid()) {
2804 Parens.skipToEnd();
2805 return ExprError();
2806 }
2807 }
2808
2809 // Add this type to the list of arguments.
2810 Args.push_back(Ty.get());
2811
2812 if (Tok.is(tok::comma)) {
2813 ConsumeToken();
2814 continue;
2815 }
2816
2817 break;
2818 } while (true);
2819
2820 if (Parens.consumeClose())
2821 return ExprError();
2822
2823 return Actions.ActOnTypeTrait(Kind, Loc, Args, Parens.getCloseLocation());
2824}
2825
John Wiegley21ff2e52011-04-28 00:16:57 +00002826/// ParseArrayTypeTrait - Parse the built-in array type-trait
2827/// pseudo-functions.
2828///
2829/// primary-expression:
2830/// [Embarcadero] '__array_rank' '(' type-id ')'
2831/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
2832///
2833ExprResult Parser::ParseArrayTypeTrait() {
2834 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
2835 SourceLocation Loc = ConsumeToken();
2836
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002837 BalancedDelimiterTracker T(*this, tok::l_paren);
2838 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley21ff2e52011-04-28 00:16:57 +00002839 return ExprError();
2840
2841 TypeResult Ty = ParseTypeName();
2842 if (Ty.isInvalid()) {
2843 SkipUntil(tok::comma);
2844 SkipUntil(tok::r_paren);
2845 return ExprError();
2846 }
2847
2848 switch (ATT) {
2849 case ATT_ArrayRank: {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002850 T.consumeClose();
2851 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL,
2852 T.getCloseLocation());
John Wiegley21ff2e52011-04-28 00:16:57 +00002853 }
2854 case ATT_ArrayExtent: {
2855 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
2856 SkipUntil(tok::r_paren);
2857 return ExprError();
2858 }
2859
2860 ExprResult DimExpr = ParseExpression();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002861 T.consumeClose();
John Wiegley21ff2e52011-04-28 00:16:57 +00002862
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002863 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(),
2864 T.getCloseLocation());
John Wiegley21ff2e52011-04-28 00:16:57 +00002865 }
John Wiegley21ff2e52011-04-28 00:16:57 +00002866 }
David Blaikie30263482012-01-20 21:50:17 +00002867 llvm_unreachable("Invalid ArrayTypeTrait!");
John Wiegley21ff2e52011-04-28 00:16:57 +00002868}
2869
John Wiegley55262202011-04-25 06:54:41 +00002870/// ParseExpressionTrait - Parse built-in expression-trait
2871/// pseudo-functions like __is_lvalue_expr( xxx ).
2872///
2873/// primary-expression:
2874/// [Embarcadero] expression-trait '(' expression ')'
2875///
2876ExprResult Parser::ParseExpressionTrait() {
2877 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2878 SourceLocation Loc = ConsumeToken();
2879
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002880 BalancedDelimiterTracker T(*this, tok::l_paren);
2881 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley55262202011-04-25 06:54:41 +00002882 return ExprError();
2883
2884 ExprResult Expr = ParseExpression();
2885
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002886 T.consumeClose();
John Wiegley55262202011-04-25 06:54:41 +00002887
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002888 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(),
2889 T.getCloseLocation());
John Wiegley55262202011-04-25 06:54:41 +00002890}
2891
2892
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002893/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2894/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2895/// based on the context past the parens.
John McCall60d7b3a2010-08-24 06:29:42 +00002896ExprResult
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002897Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallb3d87482010-08-24 05:47:05 +00002898 ParsedType &CastTy,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002899 BalancedDelimiterTracker &Tracker) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002900 assert(getLangOpts().CPlusPlus && "Should only be called for C++!");
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002901 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2902 assert(isTypeIdInParens() && "Not a type-id!");
2903
John McCall60d7b3a2010-08-24 06:29:42 +00002904 ExprResult Result(true);
John McCallb3d87482010-08-24 05:47:05 +00002905 CastTy = ParsedType();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002906
2907 // We need to disambiguate a very ugly part of the C++ syntax:
2908 //
2909 // (T())x; - type-id
2910 // (T())*x; - type-id
2911 // (T())/x; - expression
2912 // (T()); - expression
2913 //
2914 // The bad news is that we cannot use the specialized tentative parser, since
2915 // it can only verify that the thing inside the parens can be parsed as
2916 // type-id, it is not useful for determining the context past the parens.
2917 //
2918 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidisa558a892009-05-22 15:12:46 +00002919 // making any unnecessary Action calls.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002920 //
2921 // It uses a scheme similar to parsing inline methods. The parenthesized
2922 // tokens are cached, the context that follows is determined (possibly by
2923 // parsing a cast-expression), and then we re-introduce the cached tokens
2924 // into the token stream and parse them appropriately.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002925
Mike Stump1eb44332009-09-09 15:08:12 +00002926 ParenParseOption ParseAs;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002927 CachedTokens Toks;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002928
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002929 // Store the tokens of the parentheses. We will parse them after we determine
2930 // the context that follows them.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00002931 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002932 // We didn't find the ')' we expected.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002933 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002934 return ExprError();
2935 }
2936
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002937 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002938 ParseAs = CompoundLiteral;
2939 } else {
2940 bool NotCastExpr;
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002941 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2942 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2943 NotCastExpr = true;
2944 } else {
2945 // Try parsing the cast-expression that may follow.
2946 // If it is not a cast-expression, NotCastExpr will be true and no token
2947 // will be consumed.
2948 Result = ParseCastExpression(false/*isUnaryExpression*/,
2949 false/*isAddressofOperand*/,
John McCallb3d87482010-08-24 05:47:05 +00002950 NotCastExpr,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002951 // type-id has priority.
Kaelyn Uhraincd78e612012-01-25 20:49:08 +00002952 IsTypeCast);
Eli Friedmanb53f08a2009-05-25 19:41:42 +00002953 }
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002954
2955 // If we parsed a cast-expression, it's really a type-id, otherwise it's
2956 // an expression.
2957 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002958 }
2959
Mike Stump1eb44332009-09-09 15:08:12 +00002960 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002961 Toks.push_back(Tok);
2962 // Re-enter the stored parenthesized tokens into the token stream, so we may
2963 // parse them now.
2964 PP.EnterTokenStream(Toks.data(), Toks.size(),
2965 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2966 // Drop the current token and bring the first cached one. It's the same token
2967 // as when we entered this function.
2968 ConsumeAnyToken();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002969
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002970 if (ParseAs >= CompoundLiteral) {
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002971 // Parse the type declarator.
2972 DeclSpec DS(AttrFactory);
2973 ParseSpecifierQualifierList(DS);
2974 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2975 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002976
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002977 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002978 Tracker.consumeClose();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002979
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002980 if (ParseAs == CompoundLiteral) {
2981 ExprType = CompoundLiteral;
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002982 TypeResult Ty = ParseTypeName();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002983 return ParseCompoundLiteralExpression(Ty.get(),
2984 Tracker.getOpenLocation(),
2985 Tracker.getCloseLocation());
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002986 }
Mike Stump1eb44332009-09-09 15:08:12 +00002987
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002988 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
2989 assert(ParseAs == CastExpr);
2990
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002991 if (DeclaratorInfo.isInvalidType())
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002992 return ExprError();
2993
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00002994 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00002995 if (!Result.isInvalid())
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002996 Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(),
2997 DeclaratorInfo, CastTy,
2998 Tracker.getCloseLocation(), Result.take());
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002999 return Result;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00003000 }
Mike Stump1eb44332009-09-09 15:08:12 +00003001
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00003002 // Not a compound literal, and not followed by a cast-expression.
3003 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00003004
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00003005 ExprType = SimpleExpr;
Argyrios Kyrtzidisf40882a2009-05-22 21:09:47 +00003006 Result = ParseExpression();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00003007 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003008 Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(),
3009 Tok.getLocation(), Result.take());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00003010
3011 // Match the ')'.
3012 if (Result.isInvalid()) {
3013 SkipUntil(tok::r_paren);
3014 return ExprError();
3015 }
Mike Stump1eb44332009-09-09 15:08:12 +00003016
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003017 Tracker.consumeClose();
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00003018 return Result;
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00003019}