blob: f07428a0f3f41ab20b5b14ac2cd259116af6428a [file] [log] [blame]
Chris Lattner29375652006-12-04 18:06:35 +00001//===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner29375652006-12-04 18:06:35 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation for C++.
11//
12//===----------------------------------------------------------------------===//
Faisal Vali2b391ab2013-09-26 19:54:12 +000013#include "clang/AST/DeclTemplate.h"
Chris Lattner29375652006-12-04 18:06:35 +000014#include "clang/Parse/Parser.h"
Douglas Gregor94a32472011-01-11 00:33:19 +000015#include "RAIIObjectsForParser.h"
Eli Friedmanc7c97142012-01-04 02:40:39 +000016#include "clang/Basic/PrettyStackTrace.h"
Richard Smith7d182a72012-03-08 23:06:02 +000017#include "clang/Lex/LiteralSupport.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/Parse/ParseDiagnostic.h"
John McCall8b0666c2010-08-20 18:27:03 +000019#include "clang/Sema/DeclSpec.h"
20#include "clang/Sema/ParsedTemplate.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/Sema/Scope.h"
Douglas Gregor7861a802009-11-03 01:35:08 +000022#include "llvm/Support/ErrorHandling.h"
23
Faisal Vali2b391ab2013-09-26 19:54:12 +000024
Chris Lattner29375652006-12-04 18:06:35 +000025using namespace clang;
26
Richard Smith55858492011-04-14 21:45:45 +000027static int SelectDigraphErrorMessage(tok::TokenKind Kind) {
28 switch (Kind) {
29 case tok::kw_template: return 0;
30 case tok::kw_const_cast: return 1;
31 case tok::kw_dynamic_cast: return 2;
32 case tok::kw_reinterpret_cast: return 3;
33 case tok::kw_static_cast: return 4;
34 default:
David Blaikie83d382b2011-09-23 05:06:16 +000035 llvm_unreachable("Unknown type for digraph error message.");
Richard Smith55858492011-04-14 21:45:45 +000036 }
37}
38
39// Are the two tokens adjacent in the same source file?
Richard Smith7b3f3222012-06-18 06:11:04 +000040bool Parser::areTokensAdjacent(const Token &First, const Token &Second) {
Richard Smith55858492011-04-14 21:45:45 +000041 SourceManager &SM = PP.getSourceManager();
42 SourceLocation FirstLoc = SM.getSpellingLoc(First.getLocation());
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +000043 SourceLocation FirstEnd = FirstLoc.getLocWithOffset(First.getLength());
Richard Smith55858492011-04-14 21:45:45 +000044 return FirstEnd == SM.getSpellingLoc(Second.getLocation());
45}
46
47// Suggest fixit for "<::" after a cast.
48static void FixDigraph(Parser &P, Preprocessor &PP, Token &DigraphToken,
49 Token &ColonToken, tok::TokenKind Kind, bool AtDigraph) {
50 // Pull '<:' and ':' off token stream.
51 if (!AtDigraph)
52 PP.Lex(DigraphToken);
53 PP.Lex(ColonToken);
54
55 SourceRange Range;
56 Range.setBegin(DigraphToken.getLocation());
57 Range.setEnd(ColonToken.getLocation());
58 P.Diag(DigraphToken.getLocation(), diag::err_missing_whitespace_digraph)
59 << SelectDigraphErrorMessage(Kind)
60 << FixItHint::CreateReplacement(Range, "< ::");
61
62 // Update token information to reflect their change in token type.
63 ColonToken.setKind(tok::coloncolon);
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +000064 ColonToken.setLocation(ColonToken.getLocation().getLocWithOffset(-1));
Richard Smith55858492011-04-14 21:45:45 +000065 ColonToken.setLength(2);
66 DigraphToken.setKind(tok::less);
67 DigraphToken.setLength(1);
68
69 // Push new tokens back to token stream.
70 PP.EnterToken(ColonToken);
71 if (!AtDigraph)
72 PP.EnterToken(DigraphToken);
73}
74
Richard Trieu01fc0012011-09-19 19:01:00 +000075// Check for '<::' which should be '< ::' instead of '[:' when following
76// a template name.
77void Parser::CheckForTemplateAndDigraph(Token &Next, ParsedType ObjectType,
78 bool EnteringContext,
79 IdentifierInfo &II, CXXScopeSpec &SS) {
Richard Trieu02e25db2011-09-20 20:03:50 +000080 if (!Next.is(tok::l_square) || Next.getLength() != 2)
Richard Trieu01fc0012011-09-19 19:01:00 +000081 return;
82
83 Token SecondToken = GetLookAheadToken(2);
Richard Smith7b3f3222012-06-18 06:11:04 +000084 if (!SecondToken.is(tok::colon) || !areTokensAdjacent(Next, SecondToken))
Richard Trieu01fc0012011-09-19 19:01:00 +000085 return;
86
87 TemplateTy Template;
88 UnqualifiedId TemplateName;
89 TemplateName.setIdentifier(&II, Tok.getLocation());
90 bool MemberOfUnknownSpecialization;
91 if (!Actions.isTemplateName(getCurScope(), SS, /*hasTemplateKeyword=*/false,
92 TemplateName, ObjectType, EnteringContext,
93 Template, MemberOfUnknownSpecialization))
94 return;
95
96 FixDigraph(*this, PP, Next, SecondToken, tok::kw_template,
97 /*AtDigraph*/false);
98}
99
Richard Trieu1f3ea7b2012-11-02 01:08:58 +0000100/// \brief Emits an error for a left parentheses after a double colon.
101///
102/// When a '(' is found after a '::', emit an error. Attempt to fix the token
Nico Weber6be9b252012-11-29 05:29:23 +0000103/// stream by removing the '(', and the matching ')' if found.
Richard Trieu1f3ea7b2012-11-02 01:08:58 +0000104void Parser::CheckForLParenAfterColonColon() {
105 if (!Tok.is(tok::l_paren))
106 return;
107
108 SourceLocation l_parenLoc = ConsumeParen(), r_parenLoc;
109 Token Tok1 = getCurToken();
110 if (!Tok1.is(tok::identifier) && !Tok1.is(tok::star))
111 return;
112
113 if (Tok1.is(tok::identifier)) {
114 Token Tok2 = GetLookAheadToken(1);
115 if (Tok2.is(tok::r_paren)) {
116 ConsumeToken();
117 PP.EnterToken(Tok1);
118 r_parenLoc = ConsumeParen();
119 }
120 } else if (Tok1.is(tok::star)) {
121 Token Tok2 = GetLookAheadToken(1);
122 if (Tok2.is(tok::identifier)) {
123 Token Tok3 = GetLookAheadToken(2);
124 if (Tok3.is(tok::r_paren)) {
125 ConsumeToken();
126 ConsumeToken();
127 PP.EnterToken(Tok2);
128 PP.EnterToken(Tok1);
129 r_parenLoc = ConsumeParen();
130 }
131 }
132 }
133
134 Diag(l_parenLoc, diag::err_paren_after_colon_colon)
135 << FixItHint::CreateRemoval(l_parenLoc)
136 << FixItHint::CreateRemoval(r_parenLoc);
137}
138
Mike Stump11289f42009-09-09 15:08:12 +0000139/// \brief Parse global scope or nested-name-specifier if present.
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000140///
141/// Parses a C++ global scope specifier ('::') or nested-name-specifier (which
Mike Stump11289f42009-09-09 15:08:12 +0000142/// may be preceded by '::'). Note that this routine will not parse ::new or
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000143/// ::delete; it will just leave them in the token stream.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000144///
145/// '::'[opt] nested-name-specifier
146/// '::'
147///
148/// nested-name-specifier:
149/// type-name '::'
150/// namespace-name '::'
151/// nested-name-specifier identifier '::'
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000152/// nested-name-specifier 'template'[opt] simple-template-id '::'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000153///
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000154///
Mike Stump11289f42009-09-09 15:08:12 +0000155/// \param SS the scope specifier that will be set to the parsed
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000156/// nested-name-specifier (or empty)
157///
Mike Stump11289f42009-09-09 15:08:12 +0000158/// \param ObjectType if this nested-name-specifier is being parsed following
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000159/// the "." or "->" of a member access expression, this parameter provides the
160/// type of the object whose members are being accessed.
161///
162/// \param EnteringContext whether we will be entering into the context of
163/// the nested-name-specifier after parsing it.
164///
Douglas Gregore610ada2010-02-24 18:44:31 +0000165/// \param MayBePseudoDestructor When non-NULL, points to a flag that
166/// indicates whether this nested-name-specifier may be part of a
167/// pseudo-destructor name. In this case, the flag will be set false
168/// if we don't actually end up parsing a destructor name. Moreorover,
169/// if we do end up determining that we are parsing a destructor name,
170/// the last component of the nested-name-specifier is not parsed as
171/// part of the scope specifier.
Richard Smith7447af42013-03-26 01:15:19 +0000172///
173/// \param IsTypename If \c true, this nested-name-specifier is known to be
174/// part of a type name. This is used to improve error recovery.
175///
176/// \param LastII When non-NULL, points to an IdentifierInfo* that will be
177/// filled in with the leading identifier in the last component of the
178/// nested-name-specifier, if any.
Douglas Gregor90d554e2010-02-21 18:36:56 +0000179///
John McCall1f476a12010-02-26 08:45:28 +0000180/// \returns true if there was an error parsing a scope specifier
Douglas Gregore861bac2009-08-25 22:51:20 +0000181bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
John McCallba7bf592010-08-24 05:47:05 +0000182 ParsedType ObjectType,
Douglas Gregor90d554e2010-02-21 18:36:56 +0000183 bool EnteringContext,
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000184 bool *MayBePseudoDestructor,
Richard Smith7447af42013-03-26 01:15:19 +0000185 bool IsTypename,
186 IdentifierInfo **LastII) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000187 assert(getLangOpts().CPlusPlus &&
Chris Lattnerb5134c02009-01-05 01:24:05 +0000188 "Call sites of this function should be guarded by checking for C++");
Mike Stump11289f42009-09-09 15:08:12 +0000189
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000190 if (Tok.is(tok::annot_cxxscope)) {
Richard Smith7447af42013-03-26 01:15:19 +0000191 assert(!LastII && "want last identifier but have already annotated scope");
Douglas Gregor869ad452011-02-24 17:54:50 +0000192 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
193 Tok.getAnnotationRange(),
194 SS);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000195 ConsumeToken();
John McCall1f476a12010-02-26 08:45:28 +0000196 return false;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000197 }
Chris Lattnerf9b2cd42009-01-04 21:14:15 +0000198
Larisse Voufob959c3c2013-08-06 05:49:26 +0000199 if (Tok.is(tok::annot_template_id)) {
200 // If the current token is an annotated template id, it may already have
201 // a scope specifier. Restore it.
202 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
203 SS = TemplateId->SS;
204 }
205
Richard Smith7447af42013-03-26 01:15:19 +0000206 if (LastII)
207 *LastII = 0;
208
Douglas Gregor7f741122009-02-25 19:37:18 +0000209 bool HasScopeSpecifier = false;
210
Chris Lattner8a7d10d2009-01-05 03:55:46 +0000211 if (Tok.is(tok::coloncolon)) {
212 // ::new and ::delete aren't nested-name-specifiers.
213 tok::TokenKind NextKind = NextToken().getKind();
214 if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
215 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000216
Chris Lattner45ddec32009-01-05 00:13:00 +0000217 // '::' - Global scope qualifier.
Douglas Gregor90c99722011-02-24 00:17:56 +0000218 if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
219 return true;
Richard Trieu1f3ea7b2012-11-02 01:08:58 +0000220
221 CheckForLParenAfterColonColon();
222
Douglas Gregor7f741122009-02-25 19:37:18 +0000223 HasScopeSpecifier = true;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000224 }
225
Douglas Gregore610ada2010-02-24 18:44:31 +0000226 bool CheckForDestructor = false;
227 if (MayBePseudoDestructor && *MayBePseudoDestructor) {
228 CheckForDestructor = true;
229 *MayBePseudoDestructor = false;
230 }
231
David Blaikie15a430a2011-12-04 05:04:18 +0000232 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
233 DeclSpec DS(AttrFactory);
234 SourceLocation DeclLoc = Tok.getLocation();
235 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
236 if (Tok.isNot(tok::coloncolon)) {
237 AnnotateExistingDecltypeSpecifier(DS, DeclLoc, EndLoc);
238 return false;
239 }
240
241 SourceLocation CCLoc = ConsumeToken();
242 if (Actions.ActOnCXXNestedNameSpecifierDecltype(SS, DS, CCLoc))
243 SS.SetInvalid(SourceRange(DeclLoc, CCLoc));
244
245 HasScopeSpecifier = true;
246 }
247
Douglas Gregor7f741122009-02-25 19:37:18 +0000248 while (true) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000249 if (HasScopeSpecifier) {
250 // C++ [basic.lookup.classref]p5:
251 // If the qualified-id has the form
Douglas Gregor308047d2009-09-09 00:23:06 +0000252 //
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000253 // ::class-name-or-namespace-name::...
Douglas Gregor308047d2009-09-09 00:23:06 +0000254 //
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000255 // the class-name-or-namespace-name is looked up in global scope as a
256 // class-name or namespace-name.
257 //
258 // To implement this, we clear out the object type as soon as we've
259 // seen a leading '::' or part of a nested-name-specifier.
John McCallba7bf592010-08-24 05:47:05 +0000260 ObjectType = ParsedType();
Douglas Gregor2436e712009-09-17 21:32:03 +0000261
262 if (Tok.is(tok::code_completion)) {
263 // Code completion for a nested-name-specifier, where the code
264 // code completion token follows the '::'.
Douglas Gregor0be31a22010-07-02 17:43:08 +0000265 Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
Argyrios Kyrtzidis7d94c922011-04-23 01:04:12 +0000266 // Include code completion token into the range of the scope otherwise
267 // when we try to annotate the scope tokens the dangling code completion
268 // token will cause assertion in
269 // Preprocessor::AnnotatePreviousCachedTokens.
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000270 SS.setEndLoc(Tok.getLocation());
271 cutOffParsing();
272 return true;
Douglas Gregor2436e712009-09-17 21:32:03 +0000273 }
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000274 }
Mike Stump11289f42009-09-09 15:08:12 +0000275
Douglas Gregor7f741122009-02-25 19:37:18 +0000276 // nested-name-specifier:
Chris Lattner0eed3a62009-06-26 03:47:46 +0000277 // nested-name-specifier 'template'[opt] simple-template-id '::'
278
279 // Parse the optional 'template' keyword, then make sure we have
280 // 'identifier <' after it.
281 if (Tok.is(tok::kw_template)) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000282 // If we don't have a scope specifier or an object type, this isn't a
Eli Friedman2624be42009-08-29 04:08:08 +0000283 // nested-name-specifier, since they aren't allowed to start with
284 // 'template'.
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000285 if (!HasScopeSpecifier && !ObjectType)
Eli Friedman2624be42009-08-29 04:08:08 +0000286 break;
287
Douglas Gregor120635b2009-11-11 16:39:34 +0000288 TentativeParsingAction TPA(*this);
Chris Lattner0eed3a62009-06-26 03:47:46 +0000289 SourceLocation TemplateKWLoc = ConsumeToken();
Douglas Gregor71395fa2009-11-04 00:56:37 +0000290
291 UnqualifiedId TemplateName;
292 if (Tok.is(tok::identifier)) {
Douglas Gregor71395fa2009-11-04 00:56:37 +0000293 // Consume the identifier.
Douglas Gregor120635b2009-11-11 16:39:34 +0000294 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregor71395fa2009-11-04 00:56:37 +0000295 ConsumeToken();
296 } else if (Tok.is(tok::kw_operator)) {
297 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
Douglas Gregor120635b2009-11-11 16:39:34 +0000298 TemplateName)) {
299 TPA.Commit();
Douglas Gregor71395fa2009-11-04 00:56:37 +0000300 break;
Douglas Gregor120635b2009-11-11 16:39:34 +0000301 }
Douglas Gregor71395fa2009-11-04 00:56:37 +0000302
Alexis Hunted0530f2009-11-28 08:58:14 +0000303 if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
304 TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor71395fa2009-11-04 00:56:37 +0000305 Diag(TemplateName.getSourceRange().getBegin(),
306 diag::err_id_after_template_in_nested_name_spec)
307 << TemplateName.getSourceRange();
Douglas Gregor120635b2009-11-11 16:39:34 +0000308 TPA.Commit();
Douglas Gregor71395fa2009-11-04 00:56:37 +0000309 break;
310 }
311 } else {
Douglas Gregor120635b2009-11-11 16:39:34 +0000312 TPA.Revert();
Chris Lattner0eed3a62009-06-26 03:47:46 +0000313 break;
314 }
Mike Stump11289f42009-09-09 15:08:12 +0000315
Douglas Gregor120635b2009-11-11 16:39:34 +0000316 // If the next token is not '<', we have a qualified-id that refers
317 // to a template name, such as T::template apply, but is not a
318 // template-id.
319 if (Tok.isNot(tok::less)) {
320 TPA.Revert();
321 break;
322 }
323
324 // Commit to parsing the template-id.
325 TPA.Commit();
Douglas Gregorbb119652010-06-16 23:00:59 +0000326 TemplateTy Template;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000327 if (TemplateNameKind TNK
328 = Actions.ActOnDependentTemplateName(getCurScope(),
329 SS, TemplateKWLoc, TemplateName,
330 ObjectType, EnteringContext,
331 Template)) {
332 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateKWLoc,
333 TemplateName, false))
Douglas Gregorbb119652010-06-16 23:00:59 +0000334 return true;
335 } else
John McCall1f476a12010-02-26 08:45:28 +0000336 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000337
Chris Lattner0eed3a62009-06-26 03:47:46 +0000338 continue;
339 }
Mike Stump11289f42009-09-09 15:08:12 +0000340
Douglas Gregor7f741122009-02-25 19:37:18 +0000341 if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
Mike Stump11289f42009-09-09 15:08:12 +0000342 // We have
Douglas Gregor7f741122009-02-25 19:37:18 +0000343 //
Richard Smith72bfbd82013-12-04 00:28:23 +0000344 // template-id '::'
Douglas Gregor7f741122009-02-25 19:37:18 +0000345 //
Richard Smith72bfbd82013-12-04 00:28:23 +0000346 // So we need to check whether the template-id is a simple-template-id of
347 // the right kind (it should name a type or be dependent), and then
Douglas Gregorb67535d2009-03-31 00:43:58 +0000348 // convert it into a type within the nested-name-specifier.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +0000349 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregore610ada2010-02-24 18:44:31 +0000350 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
351 *MayBePseudoDestructor = true;
John McCall1f476a12010-02-26 08:45:28 +0000352 return false;
Douglas Gregore610ada2010-02-24 18:44:31 +0000353 }
354
Richard Smith7447af42013-03-26 01:15:19 +0000355 if (LastII)
356 *LastII = TemplateId->Name;
357
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000358 // Consume the template-id token.
359 ConsumeToken();
360
361 assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
362 SourceLocation CCLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000363
David Blaikie8c045bc2011-11-07 03:30:03 +0000364 HasScopeSpecifier = true;
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000365
Benjamin Kramercc4c49d2012-08-23 23:38:35 +0000366 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000367 TemplateId->NumArgs);
368
369 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000370 SS,
371 TemplateId->TemplateKWLoc,
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000372 TemplateId->Template,
373 TemplateId->TemplateNameLoc,
374 TemplateId->LAngleLoc,
375 TemplateArgsPtr,
376 TemplateId->RAngleLoc,
377 CCLoc,
378 EnteringContext)) {
379 SourceLocation StartLoc
380 = SS.getBeginLoc().isValid()? SS.getBeginLoc()
381 : TemplateId->TemplateNameLoc;
382 SS.SetInvalid(SourceRange(StartLoc, CCLoc));
Chris Lattner704edfb2009-06-26 03:45:46 +0000383 }
Argyrios Kyrtzidis13935672011-05-03 18:45:38 +0000384
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000385 continue;
Douglas Gregor7f741122009-02-25 19:37:18 +0000386 }
387
Chris Lattnere2355f72009-06-26 03:52:38 +0000388
389 // The rest of the nested-name-specifier possibilities start with
390 // tok::identifier.
391 if (Tok.isNot(tok::identifier))
392 break;
393
394 IdentifierInfo &II = *Tok.getIdentifierInfo();
395
396 // nested-name-specifier:
397 // type-name '::'
398 // namespace-name '::'
399 // nested-name-specifier identifier '::'
400 Token Next = NextToken();
Chris Lattner1c428032009-12-07 01:36:53 +0000401
402 // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover
403 // and emit a fixit hint for it.
Douglas Gregor90d554e2010-02-21 18:36:56 +0000404 if (Next.is(tok::colon) && !ColonIsSacred) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000405 if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
406 Tok.getLocation(),
407 Next.getLocation(), ObjectType,
Douglas Gregor90d554e2010-02-21 18:36:56 +0000408 EnteringContext) &&
409 // If the token after the colon isn't an identifier, it's still an
410 // error, but they probably meant something else strange so don't
411 // recover like this.
412 PP.LookAhead(1).is(tok::identifier)) {
413 Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
Douglas Gregora771f462010-03-31 17:46:05 +0000414 << FixItHint::CreateReplacement(Next.getLocation(), "::");
Douglas Gregor90d554e2010-02-21 18:36:56 +0000415
416 // Recover as if the user wrote '::'.
417 Next.setKind(tok::coloncolon);
418 }
Chris Lattner1c428032009-12-07 01:36:53 +0000419 }
420
Chris Lattnere2355f72009-06-26 03:52:38 +0000421 if (Next.is(tok::coloncolon)) {
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000422 if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +0000423 !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000424 II, ObjectType)) {
Douglas Gregore610ada2010-02-24 18:44:31 +0000425 *MayBePseudoDestructor = true;
John McCall1f476a12010-02-26 08:45:28 +0000426 return false;
Douglas Gregore610ada2010-02-24 18:44:31 +0000427 }
428
Richard Smith7447af42013-03-26 01:15:19 +0000429 if (LastII)
430 *LastII = &II;
431
Chris Lattnere2355f72009-06-26 03:52:38 +0000432 // We have an identifier followed by a '::'. Lookup this name
433 // as the name in a nested-name-specifier.
434 SourceLocation IdLoc = ConsumeToken();
Chris Lattner1c428032009-12-07 01:36:53 +0000435 assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
436 "NextToken() not working properly!");
Chris Lattnere2355f72009-06-26 03:52:38 +0000437 SourceLocation CCLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000438
Richard Trieu1f3ea7b2012-11-02 01:08:58 +0000439 CheckForLParenAfterColonColon();
440
Douglas Gregor90c99722011-02-24 00:17:56 +0000441 HasScopeSpecifier = true;
442 if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
443 ObjectType, EnteringContext, SS))
444 SS.SetInvalid(SourceRange(IdLoc, CCLoc));
445
Chris Lattnere2355f72009-06-26 03:52:38 +0000446 continue;
447 }
Mike Stump11289f42009-09-09 15:08:12 +0000448
Richard Trieu01fc0012011-09-19 19:01:00 +0000449 CheckForTemplateAndDigraph(Next, ObjectType, EnteringContext, II, SS);
Richard Smith55858492011-04-14 21:45:45 +0000450
Chris Lattnere2355f72009-06-26 03:52:38 +0000451 // nested-name-specifier:
452 // type-name '<'
453 if (Next.is(tok::less)) {
454 TemplateTy Template;
Douglas Gregor3cf81312009-11-03 23:16:33 +0000455 UnqualifiedId TemplateName;
456 TemplateName.setIdentifier(&II, Tok.getLocation());
Douglas Gregor786123d2010-05-21 23:18:07 +0000457 bool MemberOfUnknownSpecialization;
Douglas Gregor0be31a22010-07-02 17:43:08 +0000458 if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
Abramo Bagnara7c5dee42010-08-06 12:11:11 +0000459 /*hasTemplateKeyword=*/false,
Douglas Gregor3cf81312009-11-03 23:16:33 +0000460 TemplateName,
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000461 ObjectType,
Douglas Gregore861bac2009-08-25 22:51:20 +0000462 EnteringContext,
Douglas Gregor786123d2010-05-21 23:18:07 +0000463 Template,
464 MemberOfUnknownSpecialization)) {
David Blaikie8c045bc2011-11-07 03:30:03 +0000465 // We have found a template name, so annotate this token
Chris Lattnere2355f72009-06-26 03:52:38 +0000466 // with a template-id annotation. We do not permit the
467 // template-id to be translated into a type annotation,
468 // because some clients (e.g., the parsing of class template
469 // specializations) still want to see the original template-id
470 // token.
Douglas Gregor71395fa2009-11-04 00:56:37 +0000471 ConsumeToken();
Abramo Bagnara7945c982012-01-27 09:46:47 +0000472 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
473 TemplateName, false))
John McCall1f476a12010-02-26 08:45:28 +0000474 return true;
Chris Lattnere2355f72009-06-26 03:52:38 +0000475 continue;
Larisse Voufo39a1e502013-08-06 01:03:05 +0000476 }
477
Douglas Gregor20c38a72010-05-21 23:43:39 +0000478 if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000479 (IsTypename || IsTemplateArgumentList(1))) {
Douglas Gregor20c38a72010-05-21 23:43:39 +0000480 // We have something like t::getAs<T>, where getAs is a
481 // member of an unknown specialization. However, this will only
482 // parse correctly as a template, so suggest the keyword 'template'
483 // before 'getAs' and treat this as a dependent template name.
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000484 unsigned DiagID = diag::err_missing_dependent_template_keyword;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000485 if (getLangOpts().MicrosoftExt)
Francois Pichet93921652011-04-22 08:25:24 +0000486 DiagID = diag::warn_missing_dependent_template_keyword;
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000487
488 Diag(Tok.getLocation(), DiagID)
Douglas Gregor20c38a72010-05-21 23:43:39 +0000489 << II.getName()
490 << FixItHint::CreateInsertion(Tok.getLocation(), "template ");
491
Douglas Gregorbb119652010-06-16 23:00:59 +0000492 if (TemplateNameKind TNK
Douglas Gregor0be31a22010-07-02 17:43:08 +0000493 = Actions.ActOnDependentTemplateName(getCurScope(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000494 SS, SourceLocation(),
Douglas Gregorbb119652010-06-16 23:00:59 +0000495 TemplateName, ObjectType,
496 EnteringContext, Template)) {
497 // Consume the identifier.
498 ConsumeToken();
Abramo Bagnara7945c982012-01-27 09:46:47 +0000499 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
500 TemplateName, false))
501 return true;
Douglas Gregorbb119652010-06-16 23:00:59 +0000502 }
503 else
Douglas Gregor20c38a72010-05-21 23:43:39 +0000504 return true;
Douglas Gregorbb119652010-06-16 23:00:59 +0000505
Douglas Gregor20c38a72010-05-21 23:43:39 +0000506 continue;
Chris Lattnere2355f72009-06-26 03:52:38 +0000507 }
508 }
509
Douglas Gregor7f741122009-02-25 19:37:18 +0000510 // We don't have any tokens that form the beginning of a
511 // nested-name-specifier, so we're done.
512 break;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000513 }
Mike Stump11289f42009-09-09 15:08:12 +0000514
Douglas Gregore610ada2010-02-24 18:44:31 +0000515 // Even if we didn't see any pieces of a nested-name-specifier, we
516 // still check whether there is a tilde in this position, which
517 // indicates a potential pseudo-destructor.
518 if (CheckForDestructor && Tok.is(tok::tilde))
519 *MayBePseudoDestructor = true;
520
John McCall1f476a12010-02-26 08:45:28 +0000521 return false;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000522}
523
524/// ParseCXXIdExpression - Handle id-expression.
525///
526/// id-expression:
527/// unqualified-id
528/// qualified-id
529///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000530/// qualified-id:
531/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
532/// '::' identifier
533/// '::' operator-function-id
Douglas Gregora727cb92009-06-30 22:34:41 +0000534/// '::' template-id
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000535///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000536/// NOTE: The standard specifies that, for qualified-id, the parser does not
537/// expect:
538///
539/// '::' conversion-function-id
540/// '::' '~' class-name
541///
542/// This may cause a slight inconsistency on diagnostics:
543///
544/// class C {};
545/// namespace A {}
546/// void f() {
547/// :: A :: ~ C(); // Some Sema error about using destructor with a
548/// // namespace.
549/// :: ~ C(); // Some Parser error like 'unexpected ~'.
550/// }
551///
552/// We simplify the parser a bit and make it work like:
553///
554/// qualified-id:
555/// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
556/// '::' unqualified-id
557///
558/// That way Sema can handle and report similar errors for namespaces and the
559/// global scope.
560///
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000561/// The isAddressOfOperand parameter indicates that this id-expression is a
562/// direct operand of the address-of operator. This is, besides member contexts,
563/// the only place where a qualified-id naming a non-static class member may
564/// appear.
565///
John McCalldadc5752010-08-24 06:29:42 +0000566ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000567 // qualified-id:
568 // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id
569 // '::' unqualified-id
570 //
571 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +0000572 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000573
574 SourceLocation TemplateKWLoc;
Douglas Gregora121b752009-11-03 16:56:39 +0000575 UnqualifiedId Name;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000576 if (ParseUnqualifiedId(SS,
577 /*EnteringContext=*/false,
578 /*AllowDestructorName=*/false,
579 /*AllowConstructorName=*/false,
John McCallba7bf592010-08-24 05:47:05 +0000580 /*ObjectType=*/ ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000581 TemplateKWLoc,
Douglas Gregora121b752009-11-03 16:56:39 +0000582 Name))
583 return ExprError();
John McCalla9ee3252009-11-22 02:49:43 +0000584
585 // This is only the direct operand of an & operator if it is not
586 // followed by a postfix-expression suffix.
John McCall8d08b9b2010-08-27 09:08:28 +0000587 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
588 isAddressOfOperand = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000589
590 return Actions.ActOnIdExpression(getCurScope(), SS, TemplateKWLoc, Name,
591 Tok.is(tok::l_paren), isAddressOfOperand);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000592}
593
Richard Smith21b3ab42013-05-09 21:36:41 +0000594/// ParseLambdaExpression - Parse a C++11 lambda expression.
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000595///
596/// lambda-expression:
597/// lambda-introducer lambda-declarator[opt] compound-statement
598///
599/// lambda-introducer:
600/// '[' lambda-capture[opt] ']'
601///
602/// lambda-capture:
603/// capture-default
604/// capture-list
605/// capture-default ',' capture-list
606///
607/// capture-default:
608/// '&'
609/// '='
610///
611/// capture-list:
612/// capture
613/// capture-list ',' capture
614///
615/// capture:
Richard Smith21b3ab42013-05-09 21:36:41 +0000616/// simple-capture
617/// init-capture [C++1y]
618///
619/// simple-capture:
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000620/// identifier
621/// '&' identifier
622/// 'this'
623///
Richard Smith21b3ab42013-05-09 21:36:41 +0000624/// init-capture: [C++1y]
625/// identifier initializer
626/// '&' identifier initializer
627///
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000628/// lambda-declarator:
629/// '(' parameter-declaration-clause ')' attribute-specifier[opt]
630/// 'mutable'[opt] exception-specification[opt]
631/// trailing-return-type[opt]
632///
633ExprResult Parser::ParseLambdaExpression() {
634 // Parse lambda-introducer.
635 LambdaIntroducer Intro;
636
David Blaikie05785d12013-02-20 22:23:23 +0000637 Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000638 if (DiagID) {
639 Diag(Tok, DiagID.getValue());
Alexey Bataevee6507d2013-11-18 08:17:37 +0000640 SkipUntil(tok::r_square, StopAtSemi);
641 SkipUntil(tok::l_brace, StopAtSemi);
642 SkipUntil(tok::r_brace, StopAtSemi);
Eli Friedmanc7c97142012-01-04 02:40:39 +0000643 return ExprError();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000644 }
645
646 return ParseLambdaExpressionAfterIntroducer(Intro);
647}
648
649/// TryParseLambdaExpression - Use lookahead and potentially tentative
650/// parsing to determine if we are looking at a C++0x lambda expression, and parse
651/// it if we are.
652///
653/// If we are not looking at a lambda expression, returns ExprError().
654ExprResult Parser::TryParseLambdaExpression() {
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000655 assert(getLangOpts().CPlusPlus11
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000656 && Tok.is(tok::l_square)
657 && "Not at the start of a possible lambda expression.");
658
659 const Token Next = NextToken(), After = GetLookAheadToken(2);
660
661 // If lookahead indicates this is a lambda...
662 if (Next.is(tok::r_square) || // []
663 Next.is(tok::equal) || // [=
664 (Next.is(tok::amp) && // [&] or [&,
665 (After.is(tok::r_square) ||
666 After.is(tok::comma))) ||
667 (Next.is(tok::identifier) && // [identifier]
668 After.is(tok::r_square))) {
669 return ParseLambdaExpression();
670 }
671
Eli Friedmanc7c97142012-01-04 02:40:39 +0000672 // If lookahead indicates an ObjC message send...
673 // [identifier identifier
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000674 if (Next.is(tok::identifier) && After.is(tok::identifier)) {
Eli Friedmanc7c97142012-01-04 02:40:39 +0000675 return ExprEmpty();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000676 }
677
Eli Friedmanc7c97142012-01-04 02:40:39 +0000678 // Here, we're stuck: lambda introducers and Objective-C message sends are
679 // unambiguous, but it requires arbitrary lookhead. [a,b,c,d,e,f,g] is a
680 // lambda, and [a,b,c,d,e,f,g h] is a Objective-C message send. Instead of
681 // writing two routines to parse a lambda introducer, just try to parse
682 // a lambda introducer first, and fall back if that fails.
683 // (TryParseLambdaIntroducer never produces any diagnostic output.)
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000684 LambdaIntroducer Intro;
685 if (TryParseLambdaIntroducer(Intro))
Eli Friedmanc7c97142012-01-04 02:40:39 +0000686 return ExprEmpty();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000687 return ParseLambdaExpressionAfterIntroducer(Intro);
688}
689
Richard Smithf44d2a82013-05-21 22:21:19 +0000690/// \brief Parse a lambda introducer.
691/// \param Intro A LambdaIntroducer filled in with information about the
692/// contents of the lambda-introducer.
693/// \param SkippedInits If non-null, we are disambiguating between an Obj-C
694/// message send and a lambda expression. In this mode, we will
695/// sometimes skip the initializers for init-captures and not fully
696/// populate \p Intro. This flag will be set to \c true if we do so.
697/// \return A DiagnosticID if it hit something unexpected. The location for
698/// for the diagnostic is that of the current token.
699Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro,
700 bool *SkippedInits) {
David Blaikie05785d12013-02-20 22:23:23 +0000701 typedef Optional<unsigned> DiagResult;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000702
703 assert(Tok.is(tok::l_square) && "Lambda expressions begin with '['.");
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000704 BalancedDelimiterTracker T(*this, tok::l_square);
705 T.consumeOpen();
706
707 Intro.Range.setBegin(T.getOpenLocation());
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000708
709 bool first = true;
710
711 // Parse capture-default.
712 if (Tok.is(tok::amp) &&
713 (NextToken().is(tok::comma) || NextToken().is(tok::r_square))) {
714 Intro.Default = LCD_ByRef;
Douglas Gregora1bffa22012-02-10 17:46:20 +0000715 Intro.DefaultLoc = ConsumeToken();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000716 first = false;
717 } else if (Tok.is(tok::equal)) {
718 Intro.Default = LCD_ByCopy;
Douglas Gregora1bffa22012-02-10 17:46:20 +0000719 Intro.DefaultLoc = ConsumeToken();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000720 first = false;
721 }
722
723 while (Tok.isNot(tok::r_square)) {
724 if (!first) {
Douglas Gregord8c61782012-02-15 15:34:24 +0000725 if (Tok.isNot(tok::comma)) {
Douglas Gregor721b14d2012-07-31 00:50:07 +0000726 // Provide a completion for a lambda introducer here. Except
727 // in Objective-C, where this is Almost Surely meant to be a message
728 // send. In that case, fail here and let the ObjC message
729 // expression parser perform the completion.
Douglas Gregor2d8db8f2012-07-31 15:27:48 +0000730 if (Tok.is(tok::code_completion) &&
731 !(getLangOpts().ObjC1 && Intro.Default == LCD_None &&
732 !Intro.Captures.empty())) {
Douglas Gregord8c61782012-02-15 15:34:24 +0000733 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
734 /*AfterAmpersand=*/false);
735 ConsumeCodeCompletionToken();
736 break;
737 }
738
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000739 return DiagResult(diag::err_expected_comma_or_rsquare);
Douglas Gregord8c61782012-02-15 15:34:24 +0000740 }
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000741 ConsumeToken();
742 }
743
Douglas Gregord8c61782012-02-15 15:34:24 +0000744 if (Tok.is(tok::code_completion)) {
745 // If we're in Objective-C++ and we have a bare '[', then this is more
746 // likely to be a message receiver.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000747 if (getLangOpts().ObjC1 && first)
Douglas Gregord8c61782012-02-15 15:34:24 +0000748 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
749 else
750 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
751 /*AfterAmpersand=*/false);
752 ConsumeCodeCompletionToken();
753 break;
754 }
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000755
Douglas Gregord8c61782012-02-15 15:34:24 +0000756 first = false;
757
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000758 // Parse capture.
759 LambdaCaptureKind Kind = LCK_ByCopy;
760 SourceLocation Loc;
761 IdentifierInfo* Id = 0;
Douglas Gregor3e308b12012-02-14 19:27:52 +0000762 SourceLocation EllipsisLoc;
Richard Smith21b3ab42013-05-09 21:36:41 +0000763 ExprResult Init;
Douglas Gregor3e308b12012-02-14 19:27:52 +0000764
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000765 if (Tok.is(tok::kw_this)) {
766 Kind = LCK_This;
767 Loc = ConsumeToken();
768 } else {
769 if (Tok.is(tok::amp)) {
770 Kind = LCK_ByRef;
771 ConsumeToken();
Douglas Gregord8c61782012-02-15 15:34:24 +0000772
773 if (Tok.is(tok::code_completion)) {
774 Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
775 /*AfterAmpersand=*/true);
776 ConsumeCodeCompletionToken();
777 break;
778 }
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000779 }
780
781 if (Tok.is(tok::identifier)) {
782 Id = Tok.getIdentifierInfo();
783 Loc = ConsumeToken();
784 } else if (Tok.is(tok::kw_this)) {
785 // FIXME: If we want to suggest a fixit here, will need to return more
786 // than just DiagnosticID. Perhaps full DiagnosticBuilder that can be
787 // Clear()ed to prevent emission in case of tentative parsing?
788 return DiagResult(diag::err_this_captured_by_reference);
789 } else {
790 return DiagResult(diag::err_expected_capture);
791 }
Richard Smith21b3ab42013-05-09 21:36:41 +0000792
793 if (Tok.is(tok::l_paren)) {
794 BalancedDelimiterTracker Parens(*this, tok::l_paren);
795 Parens.consumeOpen();
796
797 ExprVector Exprs;
798 CommaLocsTy Commas;
Richard Smithf44d2a82013-05-21 22:21:19 +0000799 if (SkippedInits) {
800 Parens.skipToEnd();
801 *SkippedInits = true;
802 } else if (ParseExpressionList(Exprs, Commas)) {
Richard Smith21b3ab42013-05-09 21:36:41 +0000803 Parens.skipToEnd();
804 Init = ExprError();
805 } else {
806 Parens.consumeClose();
807 Init = Actions.ActOnParenListExpr(Parens.getOpenLocation(),
808 Parens.getCloseLocation(),
809 Exprs);
810 }
811 } else if (Tok.is(tok::l_brace) || Tok.is(tok::equal)) {
812 if (Tok.is(tok::equal))
813 ConsumeToken();
814
Richard Smithf44d2a82013-05-21 22:21:19 +0000815 if (!SkippedInits)
816 Init = ParseInitializer();
817 else if (Tok.is(tok::l_brace)) {
818 BalancedDelimiterTracker Braces(*this, tok::l_brace);
819 Braces.consumeOpen();
820 Braces.skipToEnd();
821 *SkippedInits = true;
822 } else {
823 // We're disambiguating this:
824 //
825 // [..., x = expr
826 //
827 // We need to find the end of the following expression in order to
828 // determine whether this is an Obj-C message send's receiver, or a
829 // lambda init-capture.
830 //
831 // Parse the expression to find where it ends, and annotate it back
832 // onto the tokens. We would have parsed this expression the same way
833 // in either case: both the RHS of an init-capture and the RHS of an
834 // assignment expression are parsed as an initializer-clause, and in
835 // neither case can anything be added to the scope between the '[' and
836 // here.
837 //
838 // FIXME: This is horrible. Adding a mechanism to skip an expression
839 // would be much cleaner.
840 // FIXME: If there is a ',' before the next ']' or ':', we can skip to
841 // that instead. (And if we see a ':' with no matching '?', we can
842 // classify this as an Obj-C message send.)
843 SourceLocation StartLoc = Tok.getLocation();
844 InMessageExpressionRAIIObject MaybeInMessageExpression(*this, true);
845 Init = ParseInitializer();
846
847 if (Tok.getLocation() != StartLoc) {
848 // Back out the lexing of the token after the initializer.
849 PP.RevertCachedTokens(1);
850
851 // Replace the consumed tokens with an appropriate annotation.
852 Tok.setLocation(StartLoc);
853 Tok.setKind(tok::annot_primary_expr);
854 setExprAnnotation(Tok, Init);
855 Tok.setAnnotationEndLoc(PP.getLastCachedTokenLocation());
856 PP.AnnotateCachedTokens(Tok);
857
858 // Consume the annotated initializer.
859 ConsumeToken();
860 }
861 }
Richard Smithba71c082013-05-16 06:20:58 +0000862 } else if (Tok.is(tok::ellipsis))
863 EllipsisLoc = ConsumeToken();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000864 }
865
Richard Smith21b3ab42013-05-09 21:36:41 +0000866 Intro.addCapture(Kind, Loc, Id, EllipsisLoc, Init);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000867 }
868
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000869 T.consumeClose();
870 Intro.Range.setEnd(T.getCloseLocation());
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000871
872 return DiagResult();
873}
874
Douglas Gregord8c61782012-02-15 15:34:24 +0000875/// TryParseLambdaIntroducer - Tentatively parse a lambda introducer.
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000876///
877/// Returns true if it hit something unexpected.
878bool Parser::TryParseLambdaIntroducer(LambdaIntroducer &Intro) {
879 TentativeParsingAction PA(*this);
880
Richard Smithf44d2a82013-05-21 22:21:19 +0000881 bool SkippedInits = false;
882 Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro, &SkippedInits));
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000883
884 if (DiagID) {
885 PA.Revert();
886 return true;
887 }
888
Richard Smithf44d2a82013-05-21 22:21:19 +0000889 if (SkippedInits) {
890 // Parse it again, but this time parse the init-captures too.
891 PA.Revert();
892 Intro = LambdaIntroducer();
893 DiagID = ParseLambdaIntroducer(Intro);
894 assert(!DiagID && "parsing lambda-introducer failed on reparse");
895 return false;
896 }
897
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000898 PA.Commit();
899 return false;
900}
901
902/// ParseLambdaExpressionAfterIntroducer - Parse the rest of a lambda
903/// expression.
904ExprResult Parser::ParseLambdaExpressionAfterIntroducer(
905 LambdaIntroducer &Intro) {
Eli Friedmanc7c97142012-01-04 02:40:39 +0000906 SourceLocation LambdaBeginLoc = Intro.Range.getBegin();
907 Diag(LambdaBeginLoc, diag::warn_cxx98_compat_lambda);
908
909 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), LambdaBeginLoc,
910 "lambda expression parsing");
911
Faisal Vali2b391ab2013-09-26 19:54:12 +0000912
913
Richard Smith21b3ab42013-05-09 21:36:41 +0000914 // FIXME: Call into Actions to add any init-capture declarations to the
915 // scope while parsing the lambda-declarator and compound-statement.
916
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000917 // Parse lambda-declarator[opt].
918 DeclSpec DS(AttrFactory);
Eli Friedman36d12942012-01-04 04:41:38 +0000919 Declarator D(DS, Declarator::LambdaExprContext);
Faisal Vali2b391ab2013-09-26 19:54:12 +0000920 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
921 Actions.PushLambdaScope();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000922
923 if (Tok.is(tok::l_paren)) {
924 ParseScope PrototypeScope(this,
925 Scope::FunctionPrototypeScope |
Richard Smithe233fbf2013-01-28 22:42:45 +0000926 Scope::FunctionDeclarationScope |
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000927 Scope::DeclScope);
928
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000929 SourceLocation DeclEndLoc;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000930 BalancedDelimiterTracker T(*this, tok::l_paren);
931 T.consumeOpen();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000932 SourceLocation LParenLoc = T.getOpenLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000933
934 // Parse parameter-declaration-clause.
935 ParsedAttributes Attr(AttrFactory);
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000936 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000937 SourceLocation EllipsisLoc;
938
Faisal Vali2b391ab2013-09-26 19:54:12 +0000939
940 if (Tok.isNot(tok::r_paren)) {
Faisal Vali2b391ab2013-09-26 19:54:12 +0000941 Actions.RecordParsingTemplateParameterDepth(TemplateParameterDepth);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000942 ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc);
Faisal Vali2b391ab2013-09-26 19:54:12 +0000943 // For a generic lambda, each 'auto' within the parameter declaration
944 // clause creates a template type parameter, so increment the depth.
945 if (Actions.getCurGenericLambda())
946 ++CurTemplateDepthTracker;
947 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000948 T.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000949 SourceLocation RParenLoc = T.getCloseLocation();
950 DeclEndLoc = RParenLoc;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000951
952 // Parse 'mutable'[opt].
953 SourceLocation MutableLoc;
954 if (Tok.is(tok::kw_mutable)) {
955 MutableLoc = ConsumeToken();
956 DeclEndLoc = MutableLoc;
957 }
958
959 // Parse exception-specification[opt].
960 ExceptionSpecificationType ESpecType = EST_None;
961 SourceRange ESpecRange;
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000962 SmallVector<ParsedType, 2> DynamicExceptions;
963 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000964 ExprResult NoexceptExpr;
Richard Smith2331bbf2012-05-02 22:22:32 +0000965 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +0000966 DynamicExceptions,
967 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +0000968 NoexceptExpr);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000969
970 if (ESpecType != EST_None)
971 DeclEndLoc = ESpecRange.getEnd();
972
973 // Parse attribute-specifier[opt].
Richard Smith89645bc2013-01-02 12:01:23 +0000974 MaybeParseCXX11Attributes(Attr, &DeclEndLoc);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000975
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000976 SourceLocation FunLocalRangeEnd = DeclEndLoc;
977
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000978 // Parse trailing-return-type[opt].
Richard Smith700537c2012-06-12 01:51:59 +0000979 TypeResult TrailingReturnType;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000980 if (Tok.is(tok::arrow)) {
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000981 FunLocalRangeEnd = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000982 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +0000983 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000984 if (Range.getEnd().isValid())
985 DeclEndLoc = Range.getEnd();
986 }
987
988 PrototypeScope.Exit();
989
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000990 SourceLocation NoLoc;
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000991 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000992 /*isAmbiguous=*/false,
993 LParenLoc,
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000994 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000995 EllipsisLoc, RParenLoc,
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000996 DS.getTypeQualifiers(),
997 /*RefQualifierIsLValueRef=*/true,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000998 /*RefQualifierLoc=*/NoLoc,
999 /*ConstQualifierLoc=*/NoLoc,
1000 /*VolatileQualifierLoc=*/NoLoc,
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001001 MutableLoc,
1002 ESpecType, ESpecRange.getBegin(),
1003 DynamicExceptions.data(),
1004 DynamicExceptionRanges.data(),
1005 DynamicExceptions.size(),
1006 NoexceptExpr.isUsable() ?
1007 NoexceptExpr.get() : 0,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001008 LParenLoc, FunLocalRangeEnd, D,
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001009 TrailingReturnType),
1010 Attr, DeclEndLoc);
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001011 } else if (Tok.is(tok::kw_mutable) || Tok.is(tok::arrow)) {
1012 // It's common to forget that one needs '()' before 'mutable' or the
1013 // result type. Deal with this.
1014 Diag(Tok, diag::err_lambda_missing_parens)
1015 << Tok.is(tok::arrow)
1016 << FixItHint::CreateInsertion(Tok.getLocation(), "() ");
1017 SourceLocation DeclLoc = Tok.getLocation();
1018 SourceLocation DeclEndLoc = DeclLoc;
1019
1020 // Parse 'mutable', if it's there.
1021 SourceLocation MutableLoc;
1022 if (Tok.is(tok::kw_mutable)) {
1023 MutableLoc = ConsumeToken();
1024 DeclEndLoc = MutableLoc;
1025 }
1026
1027 // Parse the return type, if there is one.
Richard Smith700537c2012-06-12 01:51:59 +00001028 TypeResult TrailingReturnType;
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001029 if (Tok.is(tok::arrow)) {
1030 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00001031 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001032 if (Range.getEnd().isValid())
1033 DeclEndLoc = Range.getEnd();
1034 }
1035
1036 ParsedAttributes Attr(AttrFactory);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001037 SourceLocation NoLoc;
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001038 D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00001039 /*isAmbiguous=*/false,
1040 /*LParenLoc=*/NoLoc,
1041 /*Params=*/0,
1042 /*NumParams=*/0,
1043 /*EllipsisLoc=*/NoLoc,
1044 /*RParenLoc=*/NoLoc,
1045 /*TypeQuals=*/0,
1046 /*RefQualifierIsLValueRef=*/true,
1047 /*RefQualifierLoc=*/NoLoc,
1048 /*ConstQualifierLoc=*/NoLoc,
1049 /*VolatileQualifierLoc=*/NoLoc,
1050 MutableLoc,
1051 EST_None,
1052 /*ESpecLoc=*/NoLoc,
1053 /*Exceptions=*/0,
1054 /*ExceptionRanges=*/0,
1055 /*NumExceptions=*/0,
1056 /*NoexceptExpr=*/0,
1057 DeclLoc, DeclEndLoc, D,
1058 TrailingReturnType),
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001059 Attr, DeclEndLoc);
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001060 }
Douglas Gregor6746c5d2012-02-16 21:53:36 +00001061
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001062
Eli Friedman4817cf72012-01-06 03:05:34 +00001063 // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using
1064 // it.
Douglas Gregorb8389972012-02-21 22:51:27 +00001065 unsigned ScopeFlags = Scope::BlockScope | Scope::FnScope | Scope::DeclScope;
Douglas Gregorb8389972012-02-21 22:51:27 +00001066 ParseScope BodyScope(this, ScopeFlags);
Eli Friedman4817cf72012-01-06 03:05:34 +00001067
Eli Friedman71c80552012-01-05 03:35:19 +00001068 Actions.ActOnStartOfLambdaDefinition(Intro, D, getCurScope());
1069
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001070 // Parse compound-statement.
Eli Friedmanc7c97142012-01-04 02:40:39 +00001071 if (!Tok.is(tok::l_brace)) {
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001072 Diag(Tok, diag::err_expected_lambda_body);
Eli Friedmanc7c97142012-01-04 02:40:39 +00001073 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
1074 return ExprError();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001075 }
1076
Eli Friedmanc7c97142012-01-04 02:40:39 +00001077 StmtResult Stmt(ParseCompoundStatementBody());
1078 BodyScope.Exit();
1079
Eli Friedman898caf82012-01-04 02:46:53 +00001080 if (!Stmt.isInvalid())
Douglas Gregor63798542012-02-20 19:44:39 +00001081 return Actions.ActOnLambdaExpr(LambdaBeginLoc, Stmt.take(), getCurScope());
Eli Friedmanc7c97142012-01-04 02:40:39 +00001082
Eli Friedman898caf82012-01-04 02:46:53 +00001083 Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
1084 return ExprError();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001085}
1086
Chris Lattner29375652006-12-04 18:06:35 +00001087/// ParseCXXCasts - This handles the various ways to cast expressions to another
1088/// type.
1089///
1090/// postfix-expression: [C++ 5.2p1]
1091/// 'dynamic_cast' '<' type-name '>' '(' expression ')'
1092/// 'static_cast' '<' type-name '>' '(' expression ')'
1093/// 'reinterpret_cast' '<' type-name '>' '(' expression ')'
1094/// 'const_cast' '<' type-name '>' '(' expression ')'
1095///
John McCalldadc5752010-08-24 06:29:42 +00001096ExprResult Parser::ParseCXXCasts() {
Chris Lattner29375652006-12-04 18:06:35 +00001097 tok::TokenKind Kind = Tok.getKind();
1098 const char *CastName = 0; // For error messages
1099
1100 switch (Kind) {
David Blaikieaa347f92011-09-23 20:26:49 +00001101 default: llvm_unreachable("Unknown C++ cast!");
Chris Lattner29375652006-12-04 18:06:35 +00001102 case tok::kw_const_cast: CastName = "const_cast"; break;
1103 case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
1104 case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
1105 case tok::kw_static_cast: CastName = "static_cast"; break;
1106 }
1107
1108 SourceLocation OpLoc = ConsumeToken();
1109 SourceLocation LAngleBracketLoc = Tok.getLocation();
1110
Richard Smith55858492011-04-14 21:45:45 +00001111 // Check for "<::" which is parsed as "[:". If found, fix token stream,
1112 // diagnose error, suggest fix, and recover parsing.
Richard Smith62e66302012-08-20 17:37:52 +00001113 if (Tok.is(tok::l_square) && Tok.getLength() == 2) {
1114 Token Next = NextToken();
1115 if (Next.is(tok::colon) && areTokensAdjacent(Tok, Next))
1116 FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true);
1117 }
Richard Smith55858492011-04-14 21:45:45 +00001118
Chris Lattner29375652006-12-04 18:06:35 +00001119 if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
Sebastian Redld65cea82008-12-11 22:51:44 +00001120 return ExprError();
Chris Lattner29375652006-12-04 18:06:35 +00001121
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +00001122 // Parse the common declaration-specifiers piece.
1123 DeclSpec DS(AttrFactory);
1124 ParseSpecifierQualifierList(DS);
1125
1126 // Parse the abstract-declarator, if present.
1127 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1128 ParseDeclarator(DeclaratorInfo);
1129
Chris Lattner29375652006-12-04 18:06:35 +00001130 SourceLocation RAngleBracketLoc = Tok.getLocation();
1131
Chris Lattner6d29c102008-11-18 07:48:38 +00001132 if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
Sebastian Redld65cea82008-12-11 22:51:44 +00001133 return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
Chris Lattner29375652006-12-04 18:06:35 +00001134
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001135 SourceLocation LParenLoc, RParenLoc;
1136 BalancedDelimiterTracker T(*this, tok::l_paren);
Chris Lattner29375652006-12-04 18:06:35 +00001137
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001138 if (T.expectAndConsume(diag::err_expected_lparen_after, CastName))
Argyrios Kyrtzidis387a3342009-05-22 10:23:16 +00001139 return ExprError();
Chris Lattner29375652006-12-04 18:06:35 +00001140
John McCalldadc5752010-08-24 06:29:42 +00001141 ExprResult Result = ParseExpression();
Mike Stump11289f42009-09-09 15:08:12 +00001142
Argyrios Kyrtzidis387a3342009-05-22 10:23:16 +00001143 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001144 T.consumeClose();
Chris Lattner29375652006-12-04 18:06:35 +00001145
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +00001146 if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
Douglas Gregore200adc2008-10-27 19:41:14 +00001147 Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
Argyrios Kyrtzidis7451d1c2011-07-01 22:22:50 +00001148 LAngleBracketLoc, DeclaratorInfo,
Douglas Gregor220cac52009-02-18 17:45:20 +00001149 RAngleBracketLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001150 T.getOpenLocation(), Result.take(),
1151 T.getCloseLocation());
Chris Lattner29375652006-12-04 18:06:35 +00001152
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001153 return Result;
Chris Lattner29375652006-12-04 18:06:35 +00001154}
Bill Wendling4073ed52007-02-13 01:51:42 +00001155
Sebastian Redlc4704762008-11-11 11:37:55 +00001156/// ParseCXXTypeid - This handles the C++ typeid expression.
1157///
1158/// postfix-expression: [C++ 5.2p1]
1159/// 'typeid' '(' expression ')'
1160/// 'typeid' '(' type-id ')'
1161///
John McCalldadc5752010-08-24 06:29:42 +00001162ExprResult Parser::ParseCXXTypeid() {
Sebastian Redlc4704762008-11-11 11:37:55 +00001163 assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
1164
1165 SourceLocation OpLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001166 SourceLocation LParenLoc, RParenLoc;
1167 BalancedDelimiterTracker T(*this, tok::l_paren);
Sebastian Redlc4704762008-11-11 11:37:55 +00001168
1169 // typeid expressions are always parenthesized.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001170 if (T.expectAndConsume(diag::err_expected_lparen_after, "typeid"))
Sebastian Redld65cea82008-12-11 22:51:44 +00001171 return ExprError();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001172 LParenLoc = T.getOpenLocation();
Sebastian Redlc4704762008-11-11 11:37:55 +00001173
John McCalldadc5752010-08-24 06:29:42 +00001174 ExprResult Result;
Sebastian Redlc4704762008-11-11 11:37:55 +00001175
Richard Smith4f605af2012-08-18 00:55:03 +00001176 // C++0x [expr.typeid]p3:
1177 // When typeid is applied to an expression other than an lvalue of a
1178 // polymorphic class type [...] The expression is an unevaluated
1179 // operand (Clause 5).
1180 //
1181 // Note that we can't tell whether the expression is an lvalue of a
1182 // polymorphic class type until after we've parsed the expression; we
1183 // speculatively assume the subexpression is unevaluated, and fix it up
1184 // later.
1185 //
1186 // We enter the unevaluated context before trying to determine whether we
1187 // have a type-id, because the tentative parse logic will try to resolve
1188 // names, and must treat them as unevaluated.
Eli Friedman15681d62012-09-26 04:34:21 +00001189 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
1190 Sema::ReuseLambdaContextDecl);
Richard Smith4f605af2012-08-18 00:55:03 +00001191
Sebastian Redlc4704762008-11-11 11:37:55 +00001192 if (isTypeIdInParens()) {
Douglas Gregor220cac52009-02-18 17:45:20 +00001193 TypeResult Ty = ParseTypeName();
Sebastian Redlc4704762008-11-11 11:37:55 +00001194
1195 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001196 T.consumeClose();
1197 RParenLoc = T.getCloseLocation();
Douglas Gregor4c7c1092010-09-08 23:14:30 +00001198 if (Ty.isInvalid() || RParenLoc.isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +00001199 return ExprError();
Sebastian Redlc4704762008-11-11 11:37:55 +00001200
1201 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true,
John McCallba7bf592010-08-24 05:47:05 +00001202 Ty.get().getAsOpaquePtr(), RParenLoc);
Sebastian Redlc4704762008-11-11 11:37:55 +00001203 } else {
1204 Result = ParseExpression();
1205
1206 // Match the ')'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001207 if (Result.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00001208 SkipUntil(tok::r_paren, StopAtSemi);
Sebastian Redlc4704762008-11-11 11:37:55 +00001209 else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001210 T.consumeClose();
1211 RParenLoc = T.getCloseLocation();
Douglas Gregor4c7c1092010-09-08 23:14:30 +00001212 if (RParenLoc.isInvalid())
1213 return ExprError();
Douglas Gregor1beec452011-03-12 01:48:56 +00001214
Sebastian Redlc4704762008-11-11 11:37:55 +00001215 Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false,
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001216 Result.release(), RParenLoc);
Sebastian Redlc4704762008-11-11 11:37:55 +00001217 }
1218 }
1219
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001220 return Result;
Sebastian Redlc4704762008-11-11 11:37:55 +00001221}
1222
Francois Pichet9f4f2072010-09-08 12:20:18 +00001223/// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression.
1224///
1225/// '__uuidof' '(' expression ')'
1226/// '__uuidof' '(' type-id ')'
1227///
1228ExprResult Parser::ParseCXXUuidof() {
1229 assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
1230
1231 SourceLocation OpLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001232 BalancedDelimiterTracker T(*this, tok::l_paren);
Francois Pichet9f4f2072010-09-08 12:20:18 +00001233
1234 // __uuidof expressions are always parenthesized.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001235 if (T.expectAndConsume(diag::err_expected_lparen_after, "__uuidof"))
Francois Pichet9f4f2072010-09-08 12:20:18 +00001236 return ExprError();
1237
1238 ExprResult Result;
1239
1240 if (isTypeIdInParens()) {
1241 TypeResult Ty = ParseTypeName();
1242
1243 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001244 T.consumeClose();
Francois Pichet9f4f2072010-09-08 12:20:18 +00001245
1246 if (Ty.isInvalid())
1247 return ExprError();
1248
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001249 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), /*isType=*/true,
1250 Ty.get().getAsOpaquePtr(),
1251 T.getCloseLocation());
Francois Pichet9f4f2072010-09-08 12:20:18 +00001252 } else {
1253 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1254 Result = ParseExpression();
1255
1256 // Match the ')'.
1257 if (Result.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00001258 SkipUntil(tok::r_paren, StopAtSemi);
Francois Pichet9f4f2072010-09-08 12:20:18 +00001259 else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001260 T.consumeClose();
Francois Pichet9f4f2072010-09-08 12:20:18 +00001261
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001262 Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(),
1263 /*isType=*/false,
1264 Result.release(), T.getCloseLocation());
Francois Pichet9f4f2072010-09-08 12:20:18 +00001265 }
1266 }
1267
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001268 return Result;
Francois Pichet9f4f2072010-09-08 12:20:18 +00001269}
1270
Douglas Gregore610ada2010-02-24 18:44:31 +00001271/// \brief Parse a C++ pseudo-destructor expression after the base,
1272/// . or -> operator, and nested-name-specifier have already been
1273/// parsed.
1274///
1275/// postfix-expression: [C++ 5.2]
1276/// postfix-expression . pseudo-destructor-name
1277/// postfix-expression -> pseudo-destructor-name
1278///
1279/// pseudo-destructor-name:
1280/// ::[opt] nested-name-specifier[opt] type-name :: ~type-name
1281/// ::[opt] nested-name-specifier template simple-template-id ::
1282/// ~type-name
1283/// ::[opt] nested-name-specifier[opt] ~type-name
1284///
John McCalldadc5752010-08-24 06:29:42 +00001285ExprResult
Douglas Gregore610ada2010-02-24 18:44:31 +00001286Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
1287 tok::TokenKind OpKind,
1288 CXXScopeSpec &SS,
John McCallba7bf592010-08-24 05:47:05 +00001289 ParsedType ObjectType) {
Douglas Gregore610ada2010-02-24 18:44:31 +00001290 // We're parsing either a pseudo-destructor-name or a dependent
1291 // member access that has the same form as a
1292 // pseudo-destructor-name. We parse both in the same way and let
1293 // the action model sort them out.
1294 //
1295 // Note that the ::[opt] nested-name-specifier[opt] has already
1296 // been parsed, and if there was a simple-template-id, it has
1297 // been coalesced into a template-id annotation token.
1298 UnqualifiedId FirstTypeName;
1299 SourceLocation CCLoc;
1300 if (Tok.is(tok::identifier)) {
1301 FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1302 ConsumeToken();
1303 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1304 CCLoc = ConsumeToken();
1305 } else if (Tok.is(tok::annot_template_id)) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001306 // FIXME: retrieve TemplateKWLoc from template-id annotation and
1307 // store it in the pseudo-dtor node (to be used when instantiating it).
Douglas Gregore610ada2010-02-24 18:44:31 +00001308 FirstTypeName.setTemplateId(
1309 (TemplateIdAnnotation *)Tok.getAnnotationValue());
1310 ConsumeToken();
1311 assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
1312 CCLoc = ConsumeToken();
1313 } else {
1314 FirstTypeName.setIdentifier(0, SourceLocation());
1315 }
1316
1317 // Parse the tilde.
1318 assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
1319 SourceLocation TildeLoc = ConsumeToken();
David Blaikie1d578782011-12-16 16:03:09 +00001320
1321 if (Tok.is(tok::kw_decltype) && !FirstTypeName.isValid() && SS.isEmpty()) {
1322 DeclSpec DS(AttrFactory);
Benjamin Kramer198e0832011-12-18 12:18:02 +00001323 ParseDecltypeSpecifier(DS);
David Blaikie1d578782011-12-16 16:03:09 +00001324 if (DS.getTypeSpecType() == TST_error)
1325 return ExprError();
1326 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, OpLoc,
1327 OpKind, TildeLoc, DS,
1328 Tok.is(tok::l_paren));
1329 }
1330
Douglas Gregore610ada2010-02-24 18:44:31 +00001331 if (!Tok.is(tok::identifier)) {
1332 Diag(Tok, diag::err_destructor_tilde_identifier);
1333 return ExprError();
1334 }
1335
1336 // Parse the second type.
1337 UnqualifiedId SecondTypeName;
1338 IdentifierInfo *Name = Tok.getIdentifierInfo();
1339 SourceLocation NameLoc = ConsumeToken();
1340 SecondTypeName.setIdentifier(Name, NameLoc);
1341
1342 // If there is a '<', the second type name is a template-id. Parse
1343 // it as such.
1344 if (Tok.is(tok::less) &&
Abramo Bagnara7945c982012-01-27 09:46:47 +00001345 ParseUnqualifiedIdTemplateId(SS, SourceLocation(),
1346 Name, NameLoc,
1347 false, ObjectType, SecondTypeName,
1348 /*AssumeTemplateName=*/true))
Douglas Gregore610ada2010-02-24 18:44:31 +00001349 return ExprError();
1350
John McCallb268a282010-08-23 23:25:46 +00001351 return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
1352 OpLoc, OpKind,
Douglas Gregore610ada2010-02-24 18:44:31 +00001353 SS, FirstTypeName, CCLoc,
1354 TildeLoc, SecondTypeName,
1355 Tok.is(tok::l_paren));
1356}
1357
Bill Wendling4073ed52007-02-13 01:51:42 +00001358/// ParseCXXBoolLiteral - This handles the C++ Boolean literals.
1359///
1360/// boolean-literal: [C++ 2.13.5]
1361/// 'true'
1362/// 'false'
John McCalldadc5752010-08-24 06:29:42 +00001363ExprResult Parser::ParseCXXBoolLiteral() {
Bill Wendling4073ed52007-02-13 01:51:42 +00001364 tok::TokenKind Kind = Tok.getKind();
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001365 return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
Bill Wendling4073ed52007-02-13 01:51:42 +00001366}
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001367
1368/// ParseThrowExpression - This handles the C++ throw expression.
1369///
1370/// throw-expression: [C++ 15]
1371/// 'throw' assignment-expression[opt]
John McCalldadc5752010-08-24 06:29:42 +00001372ExprResult Parser::ParseThrowExpression() {
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001373 assert(Tok.is(tok::kw_throw) && "Not throw!");
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001374 SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token.
Sebastian Redld65cea82008-12-11 22:51:44 +00001375
Chris Lattner65dd8432008-04-06 06:02:23 +00001376 // If the current token isn't the start of an assignment-expression,
1377 // then the expression is not present. This handles things like:
1378 // "C ? throw : (void)42", which is crazy but legal.
1379 switch (Tok.getKind()) { // FIXME: move this predicate somewhere common.
1380 case tok::semi:
1381 case tok::r_paren:
1382 case tok::r_square:
1383 case tok::r_brace:
1384 case tok::colon:
1385 case tok::comma:
Douglas Gregor53e191ed2011-07-06 22:04:06 +00001386 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0);
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001387
Chris Lattner65dd8432008-04-06 06:02:23 +00001388 default:
John McCalldadc5752010-08-24 06:29:42 +00001389 ExprResult Expr(ParseAssignmentExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001390 if (Expr.isInvalid()) return Expr;
Douglas Gregor53e191ed2011-07-06 22:04:06 +00001391 return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take());
Chris Lattner65dd8432008-04-06 06:02:23 +00001392 }
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001393}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001394
1395/// ParseCXXThis - This handles the C++ 'this' pointer.
1396///
1397/// C++ 9.3.2: In the body of a non-static member function, the keyword this is
1398/// a non-lvalue expression whose value is the address of the object for which
1399/// the function is called.
John McCalldadc5752010-08-24 06:29:42 +00001400ExprResult Parser::ParseCXXThis() {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001401 assert(Tok.is(tok::kw_this) && "Not 'this'!");
1402 SourceLocation ThisLoc = ConsumeToken();
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001403 return Actions.ActOnCXXThis(ThisLoc);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001404}
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001405
1406/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
1407/// Can be interpreted either as function-style casting ("int(x)")
1408/// or class type construction ("ClassType(x,y,z)")
1409/// or creation of a value-initialized type ("int()").
Sebastian Redl3da34892011-06-05 12:23:16 +00001410/// See [C++ 5.2.3].
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001411///
1412/// postfix-expression: [C++ 5.2p1]
Sebastian Redl3da34892011-06-05 12:23:16 +00001413/// simple-type-specifier '(' expression-list[opt] ')'
1414/// [C++0x] simple-type-specifier braced-init-list
1415/// typename-specifier '(' expression-list[opt] ')'
1416/// [C++0x] typename-specifier braced-init-list
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001417///
John McCalldadc5752010-08-24 06:29:42 +00001418ExprResult
Sebastian Redld65cea82008-12-11 22:51:44 +00001419Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001420 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
John McCallba7bf592010-08-24 05:47:05 +00001421 ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001422
Sebastian Redl3da34892011-06-05 12:23:16 +00001423 assert((Tok.is(tok::l_paren) ||
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001424 (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)))
Sebastian Redl3da34892011-06-05 12:23:16 +00001425 && "Expected '(' or '{'!");
Douglas Gregor94a32472011-01-11 00:33:19 +00001426
Sebastian Redl3da34892011-06-05 12:23:16 +00001427 if (Tok.is(tok::l_brace)) {
Sebastian Redld74dd492012-02-12 18:41:05 +00001428 ExprResult Init = ParseBraceInitializer();
1429 if (Init.isInvalid())
1430 return Init;
1431 Expr *InitList = Init.take();
1432 return Actions.ActOnCXXTypeConstructExpr(TypeRep, SourceLocation(),
1433 MultiExprArg(&InitList, 1),
1434 SourceLocation());
Sebastian Redl3da34892011-06-05 12:23:16 +00001435 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001436 BalancedDelimiterTracker T(*this, tok::l_paren);
1437 T.consumeOpen();
Sebastian Redl3da34892011-06-05 12:23:16 +00001438
Benjamin Kramerf0623432012-08-23 22:51:59 +00001439 ExprVector Exprs;
Sebastian Redl3da34892011-06-05 12:23:16 +00001440 CommaLocsTy CommaLocs;
1441
1442 if (Tok.isNot(tok::r_paren)) {
1443 if (ParseExpressionList(Exprs, CommaLocs)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001444 SkipUntil(tok::r_paren, StopAtSemi);
Sebastian Redl3da34892011-06-05 12:23:16 +00001445 return ExprError();
1446 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001447 }
Sebastian Redl3da34892011-06-05 12:23:16 +00001448
1449 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001450 T.consumeClose();
Sebastian Redl3da34892011-06-05 12:23:16 +00001451
1452 // TypeRep could be null, if it references an invalid typedef.
1453 if (!TypeRep)
1454 return ExprError();
1455
1456 assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
1457 "Unexpected number of commas!");
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001458 return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001459 Exprs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001460 T.getCloseLocation());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001461 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001462}
1463
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001464/// ParseCXXCondition - if/switch/while condition expression.
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001465///
1466/// condition:
1467/// expression
1468/// type-specifier-seq declarator '=' assignment-expression
Richard Smith2a15b742012-02-22 06:49:09 +00001469/// [C++11] type-specifier-seq declarator '=' initializer-clause
1470/// [C++11] type-specifier-seq declarator braced-init-list
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001471/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
1472/// '=' assignment-expression
1473///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00001474/// \param ExprOut if the condition was parsed as an expression, the parsed
1475/// expression.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001476///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00001477/// \param DeclOut if the condition was parsed as a declaration, the parsed
1478/// declaration.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001479///
Douglas Gregore60e41a2010-05-06 17:25:47 +00001480/// \param Loc The location of the start of the statement that requires this
1481/// condition, e.g., the "for" in a for loop.
1482///
1483/// \param ConvertToBoolean Whether the condition expression should be
1484/// converted to a boolean value.
1485///
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001486/// \returns true if there was a parsing, false otherwise.
John McCalldadc5752010-08-24 06:29:42 +00001487bool Parser::ParseCXXCondition(ExprResult &ExprOut,
1488 Decl *&DeclOut,
Douglas Gregore60e41a2010-05-06 17:25:47 +00001489 SourceLocation Loc,
1490 bool ConvertToBoolean) {
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001491 if (Tok.is(tok::code_completion)) {
John McCallfaf5fb42010-08-26 23:41:50 +00001492 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001493 cutOffParsing();
1494 return true;
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001495 }
1496
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001497 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00001498 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001499
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001500 if (!isCXXConditionDeclaration()) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001501 ProhibitAttributes(attrs);
1502
Douglas Gregore60e41a2010-05-06 17:25:47 +00001503 // Parse the expression.
John McCalldadc5752010-08-24 06:29:42 +00001504 ExprOut = ParseExpression(); // expression
1505 DeclOut = 0;
1506 if (ExprOut.isInvalid())
Douglas Gregore60e41a2010-05-06 17:25:47 +00001507 return true;
1508
1509 // If required, convert to a boolean value.
1510 if (ConvertToBoolean)
John McCalldadc5752010-08-24 06:29:42 +00001511 ExprOut
1512 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
1513 return ExprOut.isInvalid();
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001514 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001515
1516 // type-specifier-seq
John McCall084e83d2011-03-24 11:26:52 +00001517 DeclSpec DS(AttrFactory);
Richard Smith54ecd982013-02-20 19:22:51 +00001518 DS.takeAttributesFrom(attrs);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001519 ParseSpecifierQualifierList(DS);
1520
1521 // declarator
1522 Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
1523 ParseDeclarator(DeclaratorInfo);
1524
1525 // simple-asm-expr[opt]
1526 if (Tok.is(tok::kw_asm)) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001527 SourceLocation Loc;
John McCalldadc5752010-08-24 06:29:42 +00001528 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001529 if (AsmLabel.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001530 SkipUntil(tok::semi, StopAtSemi);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001531 return true;
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001532 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001533 DeclaratorInfo.setAsmLabel(AsmLabel.release());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001534 DeclaratorInfo.SetRangeEnd(Loc);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001535 }
1536
1537 // If attributes are present, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001538 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001539
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001540 // Type-check the declaration itself.
John McCalldadc5752010-08-24 06:29:42 +00001541 DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
John McCall53fa7142010-12-24 02:08:15 +00001542 DeclaratorInfo);
John McCalldadc5752010-08-24 06:29:42 +00001543 DeclOut = Dcl.get();
1544 ExprOut = ExprError();
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +00001545
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001546 // '=' assignment-expression
Richard Trieuc64d3232012-01-18 22:54:52 +00001547 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Smith2a15b742012-02-22 06:49:09 +00001548 bool CopyInitialization = isTokenEqualOrEqualTypo();
1549 if (CopyInitialization)
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001550 ConsumeToken();
Richard Smith2a15b742012-02-22 06:49:09 +00001551
1552 ExprResult InitExpr = ExprError();
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001553 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith2a15b742012-02-22 06:49:09 +00001554 Diag(Tok.getLocation(),
1555 diag::warn_cxx98_compat_generalized_initializer_lists);
1556 InitExpr = ParseBraceInitializer();
1557 } else if (CopyInitialization) {
1558 InitExpr = ParseAssignmentExpression();
1559 } else if (Tok.is(tok::l_paren)) {
1560 // This was probably an attempt to initialize the variable.
1561 SourceLocation LParen = ConsumeParen(), RParen = LParen;
Alexey Bataevee6507d2013-11-18 08:17:37 +00001562 if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch))
Richard Smith2a15b742012-02-22 06:49:09 +00001563 RParen = ConsumeParen();
1564 Diag(DeclOut ? DeclOut->getLocation() : LParen,
1565 diag::err_expected_init_in_condition_lparen)
1566 << SourceRange(LParen, RParen);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001567 } else {
Richard Smith2a15b742012-02-22 06:49:09 +00001568 Diag(DeclOut ? DeclOut->getLocation() : Tok.getLocation(),
1569 diag::err_expected_init_in_condition);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001570 }
Richard Smith2a15b742012-02-22 06:49:09 +00001571
1572 if (!InitExpr.isInvalid())
1573 Actions.AddInitializerToDecl(DeclOut, InitExpr.take(), !CopyInitialization,
Richard Smith74aeef52013-04-26 16:15:35 +00001574 DS.containsPlaceholderType());
Richard Smith27d807c2013-04-30 13:56:41 +00001575 else
1576 Actions.ActOnInitializerError(DeclOut);
Richard Smith2a15b742012-02-22 06:49:09 +00001577
Douglas Gregore60e41a2010-05-06 17:25:47 +00001578 // FIXME: Build a reference to this declaration? Convert it to bool?
1579 // (This is currently handled by Sema).
Richard Smithb2bc2e62011-02-21 20:05:19 +00001580
1581 Actions.FinalizeDeclaration(DeclOut);
Douglas Gregore60e41a2010-05-06 17:25:47 +00001582
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001583 return false;
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001584}
1585
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001586/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
1587/// This should only be called when the current token is known to be part of
1588/// simple-type-specifier.
1589///
1590/// simple-type-specifier:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001591/// '::'[opt] nested-name-specifier[opt] type-name
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001592/// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
1593/// char
1594/// wchar_t
1595/// bool
1596/// short
1597/// int
1598/// long
1599/// signed
1600/// unsigned
1601/// float
1602/// double
1603/// void
1604/// [GNU] typeof-specifier
1605/// [C++0x] auto [TODO]
1606///
1607/// type-name:
1608/// class-name
1609/// enum-name
1610/// typedef-name
1611///
1612void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
1613 DS.SetRangeStart(Tok.getLocation());
1614 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +00001615 unsigned DiagID;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001616 SourceLocation Loc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001617
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001618 switch (Tok.getKind()) {
Chris Lattner45ddec32009-01-05 00:13:00 +00001619 case tok::identifier: // foo::bar
1620 case tok::coloncolon: // ::foo::bar
David Blaikie83d382b2011-09-23 05:06:16 +00001621 llvm_unreachable("Annotation token should already be formed!");
Mike Stump11289f42009-09-09 15:08:12 +00001622 default:
David Blaikie83d382b2011-09-23 05:06:16 +00001623 llvm_unreachable("Not a simple-type-specifier token!");
Chris Lattner45ddec32009-01-05 00:13:00 +00001624
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001625 // type-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00001626 case tok::annot_typename: {
Douglas Gregor0231d8d2011-01-19 20:10:05 +00001627 if (getTypeAnnotation(Tok))
1628 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
1629 getTypeAnnotation(Tok));
1630 else
1631 DS.SetTypeSpecError();
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001632
1633 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1634 ConsumeToken();
1635
1636 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1637 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1638 // Objective-C interface. If we don't have Objective-C or a '<', this is
1639 // just a normal reference to a typedef name.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001640 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001641 ParseObjCProtocolQualifiers(DS);
1642
1643 DS.Finish(Diags, PP);
1644 return;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001645 }
Mike Stump11289f42009-09-09 15:08:12 +00001646
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001647 // builtin types
1648 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001649 DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001650 break;
1651 case tok::kw_long:
John McCall49bfce42009-08-03 20:12:06 +00001652 DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001653 break;
Francois Pichet84133e42011-04-28 01:59:37 +00001654 case tok::kw___int64:
1655 DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
1656 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001657 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001658 DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001659 break;
1660 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001661 DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001662 break;
1663 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001664 DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001665 break;
1666 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001667 DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001668 break;
1669 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001670 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001671 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00001672 case tok::kw___int128:
1673 DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, DiagID);
1674 break;
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00001675 case tok::kw_half:
1676 DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
1677 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001678 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001679 DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001680 break;
1681 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001682 DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001683 break;
1684 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001685 DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001686 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001687 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001688 DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001689 break;
1690 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001691 DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001692 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001693 case tok::kw_bool:
John McCall49bfce42009-08-03 20:12:06 +00001694 DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001695 break;
David Blaikie25896afb2012-01-24 05:47:35 +00001696 case tok::annot_decltype:
1697 case tok::kw_decltype:
1698 DS.SetRangeEnd(ParseDecltypeSpecifier(DS));
1699 return DS.Finish(Diags, PP);
Mike Stump11289f42009-09-09 15:08:12 +00001700
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001701 // GNU typeof support.
1702 case tok::kw_typeof:
1703 ParseTypeofSpecifier(DS);
Douglas Gregore3e01a22009-04-01 22:41:11 +00001704 DS.Finish(Diags, PP);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001705 return;
1706 }
Chris Lattnera8a3f732009-01-06 05:06:21 +00001707 if (Tok.is(tok::annot_typename))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001708 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1709 else
1710 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001711 ConsumeToken();
Douglas Gregore3e01a22009-04-01 22:41:11 +00001712 DS.Finish(Diags, PP);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001713}
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001714
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001715/// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++
1716/// [dcl.name]), which is a non-empty sequence of type-specifiers,
1717/// e.g., "const short int". Note that the DeclSpec is *not* finished
1718/// by parsing the type-specifier-seq, because these sequences are
1719/// typically followed by some form of declarator. Returns true and
1720/// emits diagnostics if this is not a type-specifier-seq, false
1721/// otherwise.
1722///
1723/// type-specifier-seq: [C++ 8.1]
1724/// type-specifier type-specifier-seq[opt]
1725///
1726bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
Richard Smithc5b05522012-03-12 07:56:15 +00001727 ParseSpecifierQualifierList(DS, AS_none, DSC_type_specifier);
Douglas Gregor40d732f2010-02-24 23:13:13 +00001728 DS.Finish(Diags, PP);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001729 return false;
1730}
1731
Douglas Gregor7861a802009-11-03 01:35:08 +00001732/// \brief Finish parsing a C++ unqualified-id that is a template-id of
1733/// some form.
1734///
1735/// This routine is invoked when a '<' is encountered after an identifier or
1736/// operator-function-id is parsed by \c ParseUnqualifiedId() to determine
1737/// whether the unqualified-id is actually a template-id. This routine will
1738/// then parse the template arguments and form the appropriate template-id to
1739/// return to the caller.
1740///
1741/// \param SS the nested-name-specifier that precedes this template-id, if
1742/// we're actually parsing a qualified-id.
1743///
1744/// \param Name for constructor and destructor names, this is the actual
1745/// identifier that may be a template-name.
1746///
1747/// \param NameLoc the location of the class-name in a constructor or
1748/// destructor.
1749///
1750/// \param EnteringContext whether we're entering the scope of the
1751/// nested-name-specifier.
1752///
Douglas Gregor127ea592009-11-03 21:24:04 +00001753/// \param ObjectType if this unqualified-id occurs within a member access
1754/// expression, the type of the base object whose member is being accessed.
1755///
Douglas Gregor7861a802009-11-03 01:35:08 +00001756/// \param Id as input, describes the template-name or operator-function-id
1757/// that precedes the '<'. If template arguments were parsed successfully,
1758/// will be updated with the template-id.
1759///
Douglas Gregore610ada2010-02-24 18:44:31 +00001760/// \param AssumeTemplateId When true, this routine will assume that the name
1761/// refers to a template without performing name lookup to verify.
1762///
Douglas Gregor7861a802009-11-03 01:35:08 +00001763/// \returns true if a parse error occurred, false otherwise.
1764bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001765 SourceLocation TemplateKWLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00001766 IdentifierInfo *Name,
1767 SourceLocation NameLoc,
1768 bool EnteringContext,
John McCallba7bf592010-08-24 05:47:05 +00001769 ParsedType ObjectType,
Douglas Gregore610ada2010-02-24 18:44:31 +00001770 UnqualifiedId &Id,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001771 bool AssumeTemplateId) {
Douglas Gregorb22ee882010-05-05 05:58:24 +00001772 assert((AssumeTemplateId || Tok.is(tok::less)) &&
1773 "Expected '<' to finish parsing a template-id");
Douglas Gregor7861a802009-11-03 01:35:08 +00001774
1775 TemplateTy Template;
1776 TemplateNameKind TNK = TNK_Non_template;
1777 switch (Id.getKind()) {
1778 case UnqualifiedId::IK_Identifier:
Douglas Gregor3cf81312009-11-03 23:16:33 +00001779 case UnqualifiedId::IK_OperatorFunctionId:
Alexis Hunted0530f2009-11-28 08:58:14 +00001780 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregore610ada2010-02-24 18:44:31 +00001781 if (AssumeTemplateId) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001782 TNK = Actions.ActOnDependentTemplateName(getCurScope(), SS, TemplateKWLoc,
Douglas Gregorbb119652010-06-16 23:00:59 +00001783 Id, ObjectType, EnteringContext,
1784 Template);
1785 if (TNK == TNK_Non_template)
1786 return true;
Douglas Gregor786123d2010-05-21 23:18:07 +00001787 } else {
1788 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001789 TNK = Actions.isTemplateName(getCurScope(), SS,
1790 TemplateKWLoc.isValid(), Id,
1791 ObjectType, EnteringContext, Template,
Douglas Gregor786123d2010-05-21 23:18:07 +00001792 MemberOfUnknownSpecialization);
1793
1794 if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
1795 ObjectType && IsTemplateArgumentList()) {
1796 // We have something like t->getAs<T>(), where getAs is a
1797 // member of an unknown specialization. However, this will only
1798 // parse correctly as a template, so suggest the keyword 'template'
1799 // before 'getAs' and treat this as a dependent template name.
1800 std::string Name;
1801 if (Id.getKind() == UnqualifiedId::IK_Identifier)
1802 Name = Id.Identifier->getName();
1803 else {
1804 Name = "operator ";
1805 if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
1806 Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
1807 else
1808 Name += Id.Identifier->getName();
1809 }
1810 Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
1811 << Name
1812 << FixItHint::CreateInsertion(Id.StartLocation, "template ");
Abramo Bagnara7945c982012-01-27 09:46:47 +00001813 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1814 SS, TemplateKWLoc, Id,
1815 ObjectType, EnteringContext,
1816 Template);
Douglas Gregorbb119652010-06-16 23:00:59 +00001817 if (TNK == TNK_Non_template)
Douglas Gregor786123d2010-05-21 23:18:07 +00001818 return true;
1819 }
1820 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001821 break;
1822
Douglas Gregor3cf81312009-11-03 23:16:33 +00001823 case UnqualifiedId::IK_ConstructorName: {
1824 UnqualifiedId TemplateName;
Douglas Gregor786123d2010-05-21 23:18:07 +00001825 bool MemberOfUnknownSpecialization;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001826 TemplateName.setIdentifier(Name, NameLoc);
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001827 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1828 TemplateName, ObjectType,
Douglas Gregor786123d2010-05-21 23:18:07 +00001829 EnteringContext, Template,
1830 MemberOfUnknownSpecialization);
Douglas Gregor7861a802009-11-03 01:35:08 +00001831 break;
1832 }
1833
Douglas Gregor3cf81312009-11-03 23:16:33 +00001834 case UnqualifiedId::IK_DestructorName: {
1835 UnqualifiedId TemplateName;
Douglas Gregor786123d2010-05-21 23:18:07 +00001836 bool MemberOfUnknownSpecialization;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001837 TemplateName.setIdentifier(Name, NameLoc);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001838 if (ObjectType) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001839 TNK = Actions.ActOnDependentTemplateName(getCurScope(),
1840 SS, TemplateKWLoc, TemplateName,
1841 ObjectType, EnteringContext,
1842 Template);
Douglas Gregorbb119652010-06-16 23:00:59 +00001843 if (TNK == TNK_Non_template)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001844 return true;
1845 } else {
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001846 TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
1847 TemplateName, ObjectType,
Douglas Gregor786123d2010-05-21 23:18:07 +00001848 EnteringContext, Template,
1849 MemberOfUnknownSpecialization);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001850
John McCallba7bf592010-08-24 05:47:05 +00001851 if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00001852 Diag(NameLoc, diag::err_destructor_template_id)
1853 << Name << SS.getRange();
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001854 return true;
1855 }
1856 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001857 break;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001858 }
Douglas Gregor7861a802009-11-03 01:35:08 +00001859
1860 default:
1861 return false;
1862 }
1863
1864 if (TNK == TNK_Non_template)
1865 return false;
1866
1867 // Parse the enclosed template argument list.
1868 SourceLocation LAngleLoc, RAngleLoc;
1869 TemplateArgList TemplateArgs;
Douglas Gregorb22ee882010-05-05 05:58:24 +00001870 if (Tok.is(tok::less) &&
1871 ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
Douglas Gregore7c20652011-03-02 00:47:37 +00001872 SS, true, LAngleLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00001873 TemplateArgs,
Douglas Gregor7861a802009-11-03 01:35:08 +00001874 RAngleLoc))
1875 return true;
1876
1877 if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Alexis Hunted0530f2009-11-28 08:58:14 +00001878 Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
1879 Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
Douglas Gregor7861a802009-11-03 01:35:08 +00001880 // Form a parsed representation of the template-id to be stored in the
1881 // UnqualifiedId.
1882 TemplateIdAnnotation *TemplateId
Benjamin Kramer1e6b6062012-04-14 12:14:03 +00001883 = TemplateIdAnnotation::Allocate(TemplateArgs.size(), TemplateIds);
Douglas Gregor7861a802009-11-03 01:35:08 +00001884
Richard Smith72bfbd82013-12-04 00:28:23 +00001885 // FIXME: Store name for literal operator too.
Douglas Gregor7861a802009-11-03 01:35:08 +00001886 if (Id.getKind() == UnqualifiedId::IK_Identifier) {
1887 TemplateId->Name = Id.Identifier;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001888 TemplateId->Operator = OO_None;
Douglas Gregor7861a802009-11-03 01:35:08 +00001889 TemplateId->TemplateNameLoc = Id.StartLocation;
1890 } else {
Douglas Gregor3cf81312009-11-03 23:16:33 +00001891 TemplateId->Name = 0;
1892 TemplateId->Operator = Id.OperatorFunctionId.Operator;
1893 TemplateId->TemplateNameLoc = Id.StartLocation;
Douglas Gregor7861a802009-11-03 01:35:08 +00001894 }
1895
Douglas Gregore7c20652011-03-02 00:47:37 +00001896 TemplateId->SS = SS;
Benjamin Kramer807c2db2012-02-19 23:37:39 +00001897 TemplateId->TemplateKWLoc = TemplateKWLoc;
John McCall3e56fd42010-08-23 07:28:44 +00001898 TemplateId->Template = Template;
Douglas Gregor7861a802009-11-03 01:35:08 +00001899 TemplateId->Kind = TNK;
1900 TemplateId->LAngleLoc = LAngleLoc;
1901 TemplateId->RAngleLoc = RAngleLoc;
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001902 ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
Douglas Gregor7861a802009-11-03 01:35:08 +00001903 for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001904 Arg != ArgEnd; ++Arg)
Douglas Gregor7861a802009-11-03 01:35:08 +00001905 Args[Arg] = TemplateArgs[Arg];
Douglas Gregor7861a802009-11-03 01:35:08 +00001906
1907 Id.setTemplateId(TemplateId);
1908 return false;
1909 }
1910
1911 // Bundle the template arguments together.
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001912 ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs);
Abramo Bagnara4244b432012-01-27 08:46:19 +00001913
Douglas Gregor7861a802009-11-03 01:35:08 +00001914 // Constructor and destructor names.
John McCallfaf5fb42010-08-26 23:41:50 +00001915 TypeResult Type
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001916 = Actions.ActOnTemplateIdType(SS, TemplateKWLoc,
1917 Template, NameLoc,
Abramo Bagnara4244b432012-01-27 08:46:19 +00001918 LAngleLoc, TemplateArgsPtr, RAngleLoc,
1919 /*IsCtorOrDtorName=*/true);
Douglas Gregor7861a802009-11-03 01:35:08 +00001920 if (Type.isInvalid())
1921 return true;
1922
1923 if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
1924 Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
1925 else
1926 Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
1927
1928 return false;
1929}
1930
Douglas Gregor71395fa2009-11-04 00:56:37 +00001931/// \brief Parse an operator-function-id or conversion-function-id as part
1932/// of a C++ unqualified-id.
1933///
1934/// This routine is responsible only for parsing the operator-function-id or
1935/// conversion-function-id; it does not handle template arguments in any way.
Douglas Gregor7861a802009-11-03 01:35:08 +00001936///
1937/// \code
Douglas Gregor7861a802009-11-03 01:35:08 +00001938/// operator-function-id: [C++ 13.5]
1939/// 'operator' operator
1940///
Douglas Gregor71395fa2009-11-04 00:56:37 +00001941/// operator: one of
Douglas Gregor7861a802009-11-03 01:35:08 +00001942/// new delete new[] delete[]
1943/// + - * / % ^ & | ~
1944/// ! = < > += -= *= /= %=
1945/// ^= &= |= << >> >>= <<= == !=
1946/// <= >= && || ++ -- , ->* ->
1947/// () []
1948///
1949/// conversion-function-id: [C++ 12.3.2]
1950/// operator conversion-type-id
1951///
1952/// conversion-type-id:
1953/// type-specifier-seq conversion-declarator[opt]
1954///
1955/// conversion-declarator:
1956/// ptr-operator conversion-declarator[opt]
1957/// \endcode
1958///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00001959/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregor7861a802009-11-03 01:35:08 +00001960/// non-empty, then we are parsing the unqualified-id of a qualified-id.
1961///
1962/// \param EnteringContext whether we are entering the scope of the
1963/// nested-name-specifier.
1964///
Douglas Gregor71395fa2009-11-04 00:56:37 +00001965/// \param ObjectType if this unqualified-id occurs within a member access
1966/// expression, the type of the base object whose member is being accessed.
1967///
1968/// \param Result on a successful parse, contains the parsed unqualified-id.
1969///
1970/// \returns true if parsing fails, false otherwise.
1971bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
John McCallba7bf592010-08-24 05:47:05 +00001972 ParsedType ObjectType,
Douglas Gregor71395fa2009-11-04 00:56:37 +00001973 UnqualifiedId &Result) {
1974 assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
1975
1976 // Consume the 'operator' keyword.
1977 SourceLocation KeywordLoc = ConsumeToken();
1978
1979 // Determine what kind of operator name we have.
1980 unsigned SymbolIdx = 0;
1981 SourceLocation SymbolLocations[3];
1982 OverloadedOperatorKind Op = OO_None;
1983 switch (Tok.getKind()) {
1984 case tok::kw_new:
1985 case tok::kw_delete: {
1986 bool isNew = Tok.getKind() == tok::kw_new;
1987 // Consume the 'new' or 'delete'.
1988 SymbolLocations[SymbolIdx++] = ConsumeToken();
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001989 // Check for array new/delete.
1990 if (Tok.is(tok::l_square) &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001991 (!getLangOpts().CPlusPlus11 || NextToken().isNot(tok::l_square))) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001992 // Consume the '[' and ']'.
1993 BalancedDelimiterTracker T(*this, tok::l_square);
1994 T.consumeOpen();
1995 T.consumeClose();
1996 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00001997 return true;
1998
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001999 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
2000 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00002001 Op = isNew? OO_Array_New : OO_Array_Delete;
2002 } else {
2003 Op = isNew? OO_New : OO_Delete;
2004 }
2005 break;
2006 }
2007
2008#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
2009 case tok::Token: \
2010 SymbolLocations[SymbolIdx++] = ConsumeToken(); \
2011 Op = OO_##Name; \
2012 break;
2013#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
2014#include "clang/Basic/OperatorKinds.def"
2015
2016 case tok::l_paren: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002017 // Consume the '(' and ')'.
2018 BalancedDelimiterTracker T(*this, tok::l_paren);
2019 T.consumeOpen();
2020 T.consumeClose();
2021 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00002022 return true;
2023
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002024 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
2025 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00002026 Op = OO_Call;
2027 break;
2028 }
2029
2030 case tok::l_square: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002031 // Consume the '[' and ']'.
2032 BalancedDelimiterTracker T(*this, tok::l_square);
2033 T.consumeOpen();
2034 T.consumeClose();
2035 if (T.getCloseLocation().isInvalid())
Douglas Gregor71395fa2009-11-04 00:56:37 +00002036 return true;
2037
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002038 SymbolLocations[SymbolIdx++] = T.getOpenLocation();
2039 SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Douglas Gregor71395fa2009-11-04 00:56:37 +00002040 Op = OO_Subscript;
2041 break;
2042 }
2043
2044 case tok::code_completion: {
2045 // Code completion for the operator name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002046 Actions.CodeCompleteOperatorName(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002047 cutOffParsing();
Douglas Gregor71395fa2009-11-04 00:56:37 +00002048 // Don't try to parse any further.
2049 return true;
2050 }
2051
2052 default:
2053 break;
2054 }
2055
2056 if (Op != OO_None) {
2057 // We have parsed an operator-function-id.
2058 Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
2059 return false;
2060 }
Alexis Hunt34458502009-11-28 04:44:28 +00002061
2062 // Parse a literal-operator-id.
2063 //
Richard Smith6f212062012-10-20 08:41:10 +00002064 // literal-operator-id: C++11 [over.literal]
2065 // operator string-literal identifier
2066 // operator user-defined-string-literal
Alexis Hunt34458502009-11-28 04:44:28 +00002067
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002068 if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002069 Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator);
Alexis Hunt34458502009-11-28 04:44:28 +00002070
Richard Smith7d182a72012-03-08 23:06:02 +00002071 SourceLocation DiagLoc;
2072 unsigned DiagId = 0;
2073
2074 // We're past translation phase 6, so perform string literal concatenation
2075 // before checking for "".
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002076 SmallVector<Token, 4> Toks;
2077 SmallVector<SourceLocation, 4> TokLocs;
Richard Smith7d182a72012-03-08 23:06:02 +00002078 while (isTokenStringLiteral()) {
2079 if (!Tok.is(tok::string_literal) && !DiagId) {
Richard Smith6f212062012-10-20 08:41:10 +00002080 // C++11 [over.literal]p1:
2081 // The string-literal or user-defined-string-literal in a
2082 // literal-operator-id shall have no encoding-prefix [...].
Richard Smith7d182a72012-03-08 23:06:02 +00002083 DiagLoc = Tok.getLocation();
2084 DiagId = diag::err_literal_operator_string_prefix;
2085 }
2086 Toks.push_back(Tok);
2087 TokLocs.push_back(ConsumeStringToken());
2088 }
2089
2090 StringLiteralParser Literal(Toks.data(), Toks.size(), PP);
2091 if (Literal.hadError)
2092 return true;
2093
2094 // Grab the literal operator's suffix, which will be either the next token
2095 // or a ud-suffix from the string literal.
2096 IdentifierInfo *II = 0;
2097 SourceLocation SuffixLoc;
2098 if (!Literal.getUDSuffix().empty()) {
2099 II = &PP.getIdentifierTable().get(Literal.getUDSuffix());
2100 SuffixLoc =
2101 Lexer::AdvanceToTokenCharacter(TokLocs[Literal.getUDSuffixToken()],
2102 Literal.getUDSuffixOffset(),
David Blaikiebbafb8a2012-03-11 07:00:24 +00002103 PP.getSourceManager(), getLangOpts());
Richard Smith7d182a72012-03-08 23:06:02 +00002104 } else if (Tok.is(tok::identifier)) {
2105 II = Tok.getIdentifierInfo();
2106 SuffixLoc = ConsumeToken();
2107 TokLocs.push_back(SuffixLoc);
2108 } else {
Alexis Hunt34458502009-11-28 04:44:28 +00002109 Diag(Tok.getLocation(), diag::err_expected_ident);
2110 return true;
2111 }
2112
Richard Smith7d182a72012-03-08 23:06:02 +00002113 // The string literal must be empty.
2114 if (!Literal.GetString().empty() || Literal.Pascal) {
Richard Smith6f212062012-10-20 08:41:10 +00002115 // C++11 [over.literal]p1:
2116 // The string-literal or user-defined-string-literal in a
2117 // literal-operator-id shall [...] contain no characters
2118 // other than the implicit terminating '\0'.
Richard Smith7d182a72012-03-08 23:06:02 +00002119 DiagLoc = TokLocs.front();
2120 DiagId = diag::err_literal_operator_string_not_empty;
2121 }
2122
2123 if (DiagId) {
2124 // This isn't a valid literal-operator-id, but we think we know
2125 // what the user meant. Tell them what they should have written.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002126 SmallString<32> Str;
Richard Smith7d182a72012-03-08 23:06:02 +00002127 Str += "\"\" ";
2128 Str += II->getName();
2129 Diag(DiagLoc, DiagId) << FixItHint::CreateReplacement(
2130 SourceRange(TokLocs.front(), TokLocs.back()), Str);
2131 }
2132
2133 Result.setLiteralOperatorId(II, KeywordLoc, SuffixLoc);
Alexis Hunt3d221f22009-11-29 07:34:05 +00002134 return false;
Alexis Hunt34458502009-11-28 04:44:28 +00002135 }
Douglas Gregor71395fa2009-11-04 00:56:37 +00002136
2137 // Parse a conversion-function-id.
2138 //
2139 // conversion-function-id: [C++ 12.3.2]
2140 // operator conversion-type-id
2141 //
2142 // conversion-type-id:
2143 // type-specifier-seq conversion-declarator[opt]
2144 //
2145 // conversion-declarator:
2146 // ptr-operator conversion-declarator[opt]
2147
2148 // Parse the type-specifier-seq.
John McCall084e83d2011-03-24 11:26:52 +00002149 DeclSpec DS(AttrFactory);
Douglas Gregora25d65d2009-11-20 22:03:38 +00002150 if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType?
Douglas Gregor71395fa2009-11-04 00:56:37 +00002151 return true;
2152
2153 // Parse the conversion-declarator, which is merely a sequence of
2154 // ptr-operators.
Richard Smith01518fa2013-05-04 01:26:46 +00002155 Declarator D(DS, Declarator::ConversionIdContext);
Douglas Gregor71395fa2009-11-04 00:56:37 +00002156 ParseDeclaratorInternal(D, /*DirectDeclParser=*/0);
2157
2158 // Finish up the type.
John McCallfaf5fb42010-08-26 23:41:50 +00002159 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
Douglas Gregor71395fa2009-11-04 00:56:37 +00002160 if (Ty.isInvalid())
2161 return true;
2162
2163 // Note that this is a conversion-function-id.
2164 Result.setConversionFunctionId(KeywordLoc, Ty.get(),
2165 D.getSourceRange().getEnd());
2166 return false;
2167}
2168
2169/// \brief Parse a C++ unqualified-id (or a C identifier), which describes the
2170/// name of an entity.
2171///
2172/// \code
2173/// unqualified-id: [C++ expr.prim.general]
2174/// identifier
2175/// operator-function-id
2176/// conversion-function-id
2177/// [C++0x] literal-operator-id [TODO]
2178/// ~ class-name
2179/// template-id
2180///
2181/// \endcode
2182///
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00002183/// \param SS The nested-name-specifier that preceded this unqualified-id. If
Douglas Gregor71395fa2009-11-04 00:56:37 +00002184/// non-empty, then we are parsing the unqualified-id of a qualified-id.
2185///
2186/// \param EnteringContext whether we are entering the scope of the
2187/// nested-name-specifier.
2188///
Douglas Gregor7861a802009-11-03 01:35:08 +00002189/// \param AllowDestructorName whether we allow parsing of a destructor name.
2190///
2191/// \param AllowConstructorName whether we allow parsing a constructor name.
2192///
Douglas Gregor127ea592009-11-03 21:24:04 +00002193/// \param ObjectType if this unqualified-id occurs within a member access
2194/// expression, the type of the base object whose member is being accessed.
2195///
Douglas Gregor7861a802009-11-03 01:35:08 +00002196/// \param Result on a successful parse, contains the parsed unqualified-id.
2197///
2198/// \returns true if parsing fails, false otherwise.
2199bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
2200 bool AllowDestructorName,
2201 bool AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00002202 ParsedType ObjectType,
Abramo Bagnara7945c982012-01-27 09:46:47 +00002203 SourceLocation& TemplateKWLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00002204 UnqualifiedId &Result) {
Douglas Gregorb22ee882010-05-05 05:58:24 +00002205
2206 // Handle 'A::template B'. This is for template-ids which have not
2207 // already been annotated by ParseOptionalCXXScopeSpecifier().
2208 bool TemplateSpecified = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002209 if (getLangOpts().CPlusPlus && Tok.is(tok::kw_template) &&
Douglas Gregorb22ee882010-05-05 05:58:24 +00002210 (ObjectType || SS.isSet())) {
2211 TemplateSpecified = true;
2212 TemplateKWLoc = ConsumeToken();
2213 }
2214
Douglas Gregor7861a802009-11-03 01:35:08 +00002215 // unqualified-id:
2216 // identifier
2217 // template-id (when it hasn't already been annotated)
2218 if (Tok.is(tok::identifier)) {
2219 // Consume the identifier.
2220 IdentifierInfo *Id = Tok.getIdentifierInfo();
2221 SourceLocation IdLoc = ConsumeToken();
2222
David Blaikiebbafb8a2012-03-11 07:00:24 +00002223 if (!getLangOpts().CPlusPlus) {
Douglas Gregor411e5ac2010-01-11 23:29:10 +00002224 // If we're not in C++, only identifiers matter. Record the
2225 // identifier and return.
2226 Result.setIdentifier(Id, IdLoc);
2227 return false;
2228 }
2229
Douglas Gregor7861a802009-11-03 01:35:08 +00002230 if (AllowConstructorName &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002231 Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
Douglas Gregor7861a802009-11-03 01:35:08 +00002232 // We have parsed a constructor name.
Abramo Bagnara4244b432012-01-27 08:46:19 +00002233 ParsedType Ty = Actions.getTypeName(*Id, IdLoc, getCurScope(),
2234 &SS, false, false,
2235 ParsedType(),
2236 /*IsCtorOrDtorName=*/true,
2237 /*NonTrivialTypeSourceInfo=*/true);
2238 Result.setConstructorName(Ty, IdLoc, IdLoc);
Douglas Gregor7861a802009-11-03 01:35:08 +00002239 } else {
2240 // We have parsed an identifier.
2241 Result.setIdentifier(Id, IdLoc);
2242 }
2243
2244 // If the next token is a '<', we may have a template.
Douglas Gregorb22ee882010-05-05 05:58:24 +00002245 if (TemplateSpecified || Tok.is(tok::less))
Abramo Bagnara7945c982012-01-27 09:46:47 +00002246 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc, Id, IdLoc,
2247 EnteringContext, ObjectType,
2248 Result, TemplateSpecified);
Douglas Gregor7861a802009-11-03 01:35:08 +00002249
2250 return false;
2251 }
2252
2253 // unqualified-id:
2254 // template-id (already parsed and annotated)
2255 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002256 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002257
2258 // If the template-name names the current class, then this is a constructor
2259 if (AllowConstructorName && TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002260 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002261 if (SS.isSet()) {
2262 // C++ [class.qual]p2 specifies that a qualified template-name
2263 // is taken as the constructor name where a constructor can be
2264 // declared. Thus, the template arguments are extraneous, so
2265 // complain about them and remove them entirely.
2266 Diag(TemplateId->TemplateNameLoc,
2267 diag::err_out_of_line_constructor_template_id)
2268 << TemplateId->Name
Douglas Gregora771f462010-03-31 17:46:05 +00002269 << FixItHint::CreateRemoval(
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002270 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
Abramo Bagnara4244b432012-01-27 08:46:19 +00002271 ParsedType Ty = Actions.getTypeName(*TemplateId->Name,
2272 TemplateId->TemplateNameLoc,
2273 getCurScope(),
2274 &SS, false, false,
2275 ParsedType(),
2276 /*IsCtorOrDtorName=*/true,
2277 /*NontrivialTypeSourceInfo=*/true);
2278 Result.setConstructorName(Ty, TemplateId->TemplateNameLoc,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002279 TemplateId->RAngleLoc);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002280 ConsumeToken();
2281 return false;
2282 }
2283
2284 Result.setConstructorTemplateId(TemplateId);
2285 ConsumeToken();
2286 return false;
2287 }
2288
Douglas Gregor7861a802009-11-03 01:35:08 +00002289 // We have already parsed a template-id; consume the annotation token as
2290 // our unqualified-id.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002291 Result.setTemplateId(TemplateId);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002292 TemplateKWLoc = TemplateId->TemplateKWLoc;
Douglas Gregor7861a802009-11-03 01:35:08 +00002293 ConsumeToken();
2294 return false;
2295 }
2296
2297 // unqualified-id:
2298 // operator-function-id
2299 // conversion-function-id
2300 if (Tok.is(tok::kw_operator)) {
Douglas Gregor71395fa2009-11-04 00:56:37 +00002301 if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
Douglas Gregor7861a802009-11-03 01:35:08 +00002302 return true;
2303
Alexis Hunted0530f2009-11-28 08:58:14 +00002304 // If we have an operator-function-id or a literal-operator-id and the next
2305 // token is a '<', we may have a
Douglas Gregor71395fa2009-11-04 00:56:37 +00002306 //
2307 // template-id:
2308 // operator-function-id < template-argument-list[opt] >
Alexis Hunted0530f2009-11-28 08:58:14 +00002309 if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
2310 Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
Douglas Gregorb22ee882010-05-05 05:58:24 +00002311 (TemplateSpecified || Tok.is(tok::less)))
Abramo Bagnara7945c982012-01-27 09:46:47 +00002312 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2313 0, SourceLocation(),
2314 EnteringContext, ObjectType,
2315 Result, TemplateSpecified);
Douglas Gregor7861a802009-11-03 01:35:08 +00002316
Douglas Gregor7861a802009-11-03 01:35:08 +00002317 return false;
2318 }
2319
David Blaikiebbafb8a2012-03-11 07:00:24 +00002320 if (getLangOpts().CPlusPlus &&
Douglas Gregor411e5ac2010-01-11 23:29:10 +00002321 (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
Douglas Gregor7861a802009-11-03 01:35:08 +00002322 // C++ [expr.unary.op]p10:
2323 // There is an ambiguity in the unary-expression ~X(), where X is a
2324 // class-name. The ambiguity is resolved in favor of treating ~ as a
2325 // unary complement rather than treating ~X as referring to a destructor.
2326
2327 // Parse the '~'.
2328 SourceLocation TildeLoc = ConsumeToken();
David Blaikieecd8a942011-12-08 16:13:53 +00002329
2330 if (SS.isEmpty() && Tok.is(tok::kw_decltype)) {
2331 DeclSpec DS(AttrFactory);
2332 SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
2333 if (ParsedType Type = Actions.getDestructorType(DS, ObjectType)) {
2334 Result.setDestructorName(TildeLoc, Type, EndLoc);
2335 return false;
2336 }
2337 return true;
2338 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002339
2340 // Parse the class-name.
2341 if (Tok.isNot(tok::identifier)) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00002342 Diag(Tok, diag::err_destructor_tilde_identifier);
Douglas Gregor7861a802009-11-03 01:35:08 +00002343 return true;
2344 }
2345
2346 // Parse the class-name (or template-name in a simple-template-id).
2347 IdentifierInfo *ClassName = Tok.getIdentifierInfo();
2348 SourceLocation ClassNameLoc = ConsumeToken();
2349
Douglas Gregorb22ee882010-05-05 05:58:24 +00002350 if (TemplateSpecified || Tok.is(tok::less)) {
John McCallba7bf592010-08-24 05:47:05 +00002351 Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002352 return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
2353 ClassName, ClassNameLoc,
2354 EnteringContext, ObjectType,
2355 Result, TemplateSpecified);
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002356 }
2357
Douglas Gregor7861a802009-11-03 01:35:08 +00002358 // Note that this is a destructor name.
John McCallba7bf592010-08-24 05:47:05 +00002359 ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
2360 ClassNameLoc, getCurScope(),
2361 SS, ObjectType,
2362 EnteringContext);
Douglas Gregorfe17d252010-02-16 19:09:40 +00002363 if (!Ty)
Douglas Gregor7861a802009-11-03 01:35:08 +00002364 return true;
Douglas Gregorfe17d252010-02-16 19:09:40 +00002365
Douglas Gregor7861a802009-11-03 01:35:08 +00002366 Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
Douglas Gregor7861a802009-11-03 01:35:08 +00002367 return false;
2368 }
2369
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002370 Diag(Tok, diag::err_expected_unqualified_id)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002371 << getLangOpts().CPlusPlus;
Douglas Gregor7861a802009-11-03 01:35:08 +00002372 return true;
2373}
2374
Sebastian Redlbd150f42008-11-21 19:14:01 +00002375/// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate
2376/// memory in a typesafe manner and call constructors.
Mike Stump11289f42009-09-09 15:08:12 +00002377///
Chris Lattner109faf22009-01-04 21:25:24 +00002378/// This method is called to parse the new expression after the optional :: has
2379/// been already parsed. If the :: was present, "UseGlobal" is true and "Start"
2380/// is its location. Otherwise, "Start" is the location of the 'new' token.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002381///
2382/// new-expression:
2383/// '::'[opt] 'new' new-placement[opt] new-type-id
2384/// new-initializer[opt]
2385/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2386/// new-initializer[opt]
2387///
2388/// new-placement:
2389/// '(' expression-list ')'
2390///
Sebastian Redl351bb782008-12-02 14:43:59 +00002391/// new-type-id:
2392/// type-specifier-seq new-declarator[opt]
Douglas Gregora3a020a2011-04-15 19:40:02 +00002393/// [GNU] attributes type-specifier-seq new-declarator[opt]
Sebastian Redl351bb782008-12-02 14:43:59 +00002394///
2395/// new-declarator:
2396/// ptr-operator new-declarator[opt]
2397/// direct-new-declarator
2398///
Sebastian Redlbd150f42008-11-21 19:14:01 +00002399/// new-initializer:
2400/// '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00002401/// [C++0x] braced-init-list
Sebastian Redlbd150f42008-11-21 19:14:01 +00002402///
John McCalldadc5752010-08-24 06:29:42 +00002403ExprResult
Chris Lattner109faf22009-01-04 21:25:24 +00002404Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
2405 assert(Tok.is(tok::kw_new) && "expected 'new' token");
2406 ConsumeToken(); // Consume 'new'
Sebastian Redlbd150f42008-11-21 19:14:01 +00002407
2408 // A '(' now can be a new-placement or the '(' wrapping the type-id in the
2409 // second form of new-expression. It can't be a new-type-id.
2410
Benjamin Kramerf0623432012-08-23 22:51:59 +00002411 ExprVector PlacementArgs;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002412 SourceLocation PlacementLParen, PlacementRParen;
2413
Douglas Gregorf2753b32010-07-13 15:54:32 +00002414 SourceRange TypeIdParens;
John McCall084e83d2011-03-24 11:26:52 +00002415 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis3ff13572011-06-28 03:01:23 +00002416 Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002417 if (Tok.is(tok::l_paren)) {
2418 // If it turns out to be a placement, we change the type location.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002419 BalancedDelimiterTracker T(*this, tok::l_paren);
2420 T.consumeOpen();
2421 PlacementLParen = T.getOpenLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002422 if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002423 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redld65cea82008-12-11 22:51:44 +00002424 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002425 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002426
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002427 T.consumeClose();
2428 PlacementRParen = T.getCloseLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002429 if (PlacementRParen.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002430 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redld65cea82008-12-11 22:51:44 +00002431 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002432 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002433
Sebastian Redl351bb782008-12-02 14:43:59 +00002434 if (PlacementArgs.empty()) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002435 // Reset the placement locations. There was no placement.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002436 TypeIdParens = T.getRange();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002437 PlacementLParen = PlacementRParen = SourceLocation();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002438 } else {
2439 // We still need the type.
2440 if (Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002441 BalancedDelimiterTracker T(*this, tok::l_paren);
2442 T.consumeOpen();
Douglas Gregora3a020a2011-04-15 19:40:02 +00002443 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002444 ParseSpecifierQualifierList(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002445 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002446 ParseDeclarator(DeclaratorInfo);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002447 T.consumeClose();
2448 TypeIdParens = T.getRange();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002449 } else {
Douglas Gregora3a020a2011-04-15 19:40:02 +00002450 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002451 if (ParseCXXTypeSpecifierSeq(DS))
2452 DeclaratorInfo.setInvalidType(true);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002453 else {
2454 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002455 ParseDeclaratorInternal(DeclaratorInfo,
2456 &Parser::ParseDirectNewDeclarator);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002457 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002458 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002459 }
2460 } else {
Sebastian Redl351bb782008-12-02 14:43:59 +00002461 // A new-type-id is a simplified type-id, where essentially the
2462 // direct-declarator is replaced by a direct-new-declarator.
Douglas Gregora3a020a2011-04-15 19:40:02 +00002463 MaybeParseGNUAttributes(DeclaratorInfo);
Sebastian Redl351bb782008-12-02 14:43:59 +00002464 if (ParseCXXTypeSpecifierSeq(DS))
2465 DeclaratorInfo.setInvalidType(true);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002466 else {
2467 DeclaratorInfo.SetSourceRange(DS.getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002468 ParseDeclaratorInternal(DeclaratorInfo,
2469 &Parser::ParseDirectNewDeclarator);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002470 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002471 }
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00002472 if (DeclaratorInfo.isInvalidType()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002473 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redld65cea82008-12-11 22:51:44 +00002474 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002475 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002476
Sebastian Redl6047f072012-02-16 12:22:20 +00002477 ExprResult Initializer;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002478
2479 if (Tok.is(tok::l_paren)) {
Sebastian Redl6047f072012-02-16 12:22:20 +00002480 SourceLocation ConstructorLParen, ConstructorRParen;
Benjamin Kramerf0623432012-08-23 22:51:59 +00002481 ExprVector ConstructorArgs;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002482 BalancedDelimiterTracker T(*this, tok::l_paren);
2483 T.consumeOpen();
2484 ConstructorLParen = T.getOpenLocation();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002485 if (Tok.isNot(tok::r_paren)) {
2486 CommaLocsTy CommaLocs;
Sebastian Redl351bb782008-12-02 14:43:59 +00002487 if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002488 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redld65cea82008-12-11 22:51:44 +00002489 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002490 }
Sebastian Redlbd150f42008-11-21 19:14:01 +00002491 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002492 T.consumeClose();
2493 ConstructorRParen = T.getCloseLocation();
Sebastian Redl351bb782008-12-02 14:43:59 +00002494 if (ConstructorRParen.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002495 SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
Sebastian Redld65cea82008-12-11 22:51:44 +00002496 return ExprError();
Sebastian Redl351bb782008-12-02 14:43:59 +00002497 }
Sebastian Redl6047f072012-02-16 12:22:20 +00002498 Initializer = Actions.ActOnParenListExpr(ConstructorLParen,
2499 ConstructorRParen,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002500 ConstructorArgs);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002501 } else if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus11) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002502 Diag(Tok.getLocation(),
2503 diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl6047f072012-02-16 12:22:20 +00002504 Initializer = ParseBraceInitializer();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002505 }
Sebastian Redl6047f072012-02-16 12:22:20 +00002506 if (Initializer.isInvalid())
2507 return Initializer;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002508
Sebastian Redl6d4256c2009-03-15 17:47:39 +00002509 return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002510 PlacementArgs, PlacementRParen,
Sebastian Redl6047f072012-02-16 12:22:20 +00002511 TypeIdParens, DeclaratorInfo, Initializer.take());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002512}
2513
Sebastian Redlbd150f42008-11-21 19:14:01 +00002514/// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be
2515/// passed to ParseDeclaratorInternal.
2516///
2517/// direct-new-declarator:
2518/// '[' expression ']'
2519/// direct-new-declarator '[' constant-expression ']'
2520///
Chris Lattner109faf22009-01-04 21:25:24 +00002521void Parser::ParseDirectNewDeclarator(Declarator &D) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002522 // Parse the array dimensions.
2523 bool first = true;
2524 while (Tok.is(tok::l_square)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002525 // An array-size expression can't start with a lambda.
2526 if (CheckProhibitedCXX11Attribute())
2527 continue;
2528
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002529 BalancedDelimiterTracker T(*this, tok::l_square);
2530 T.consumeOpen();
2531
John McCalldadc5752010-08-24 06:29:42 +00002532 ExprResult Size(first ? ParseExpression()
Sebastian Redl59b5e512008-12-11 21:36:32 +00002533 : ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002534 if (Size.isInvalid()) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002535 // Recover
Alexey Bataevee6507d2013-11-18 08:17:37 +00002536 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002537 return;
2538 }
2539 first = false;
2540
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002541 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00002542
Bill Wendling44426052012-12-20 19:22:21 +00002543 // Attributes here appertain to the array type. C++11 [expr.new]p5.
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002544 ParsedAttributes Attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002545 MaybeParseCXX11Attributes(Attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002546
John McCall084e83d2011-03-24 11:26:52 +00002547 D.AddTypeInfo(DeclaratorChunk::getArray(0,
John McCall53fa7142010-12-24 02:08:15 +00002548 /*static=*/false, /*star=*/false,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002549 Size.release(),
2550 T.getOpenLocation(),
2551 T.getCloseLocation()),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002552 Attrs, T.getCloseLocation());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002553
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002554 if (T.getCloseLocation().isInvalid())
Sebastian Redlbd150f42008-11-21 19:14:01 +00002555 return;
2556 }
2557}
2558
2559/// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id.
2560/// This ambiguity appears in the syntax of the C++ new operator.
2561///
2562/// new-expression:
2563/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
2564/// new-initializer[opt]
2565///
2566/// new-placement:
2567/// '(' expression-list ')'
2568///
John McCall37ad5512010-08-23 06:44:23 +00002569bool Parser::ParseExpressionListOrTypeId(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002570 SmallVectorImpl<Expr*> &PlacementArgs,
Chris Lattner109faf22009-01-04 21:25:24 +00002571 Declarator &D) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002572 // The '(' was already consumed.
2573 if (isTypeIdInParens()) {
Sebastian Redl351bb782008-12-02 14:43:59 +00002574 ParseSpecifierQualifierList(D.getMutableDeclSpec());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002575 D.SetSourceRange(D.getDeclSpec().getSourceRange());
Sebastian Redl351bb782008-12-02 14:43:59 +00002576 ParseDeclarator(D);
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00002577 return D.isInvalidType();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002578 }
2579
2580 // It's not a type, it has to be an expression list.
2581 // Discard the comma locations - ActOnCXXNew has enough parameters.
2582 CommaLocsTy CommaLocs;
2583 return ParseExpressionList(PlacementArgs, CommaLocs);
2584}
2585
2586/// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used
2587/// to free memory allocated by new.
2588///
Chris Lattner109faf22009-01-04 21:25:24 +00002589/// This method is called to parse the 'delete' expression after the optional
2590/// '::' has been already parsed. If the '::' was present, "UseGlobal" is true
2591/// and "Start" is its location. Otherwise, "Start" is the location of the
2592/// 'delete' token.
2593///
Sebastian Redlbd150f42008-11-21 19:14:01 +00002594/// delete-expression:
2595/// '::'[opt] 'delete' cast-expression
2596/// '::'[opt] 'delete' '[' ']' cast-expression
John McCalldadc5752010-08-24 06:29:42 +00002597ExprResult
Chris Lattner109faf22009-01-04 21:25:24 +00002598Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
2599 assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
2600 ConsumeToken(); // Consume 'delete'
Sebastian Redlbd150f42008-11-21 19:14:01 +00002601
2602 // Array delete?
2603 bool ArrayDelete = false;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00002604 if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
Richard Smith10c60722012-08-09 19:01:51 +00002605 // C++11 [expr.delete]p1:
2606 // Whenever the delete keyword is followed by empty square brackets, it
2607 // shall be interpreted as [array delete].
2608 // [Footnote: A lambda expression with a lambda-introducer that consists
2609 // of empty square brackets can follow the delete keyword if
2610 // the lambda expression is enclosed in parentheses.]
2611 // FIXME: Produce a better diagnostic if the '[]' is unambiguously a
2612 // lambda-introducer.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002613 ArrayDelete = true;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002614 BalancedDelimiterTracker T(*this, tok::l_square);
2615
2616 T.consumeOpen();
2617 T.consumeClose();
2618 if (T.getCloseLocation().isInvalid())
Sebastian Redld65cea82008-12-11 22:51:44 +00002619 return ExprError();
Sebastian Redlbd150f42008-11-21 19:14:01 +00002620 }
2621
John McCalldadc5752010-08-24 06:29:42 +00002622 ExprResult Operand(ParseCastExpression(false));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002623 if (Operand.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002624 return Operand;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002625
John McCallb268a282010-08-23 23:25:46 +00002626 return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
Sebastian Redlbd150f42008-11-21 19:14:01 +00002627}
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002628
Mike Stump11289f42009-09-09 15:08:12 +00002629static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002630 switch(kind) {
David Blaikie83d382b2011-09-23 05:06:16 +00002631 default: llvm_unreachable("Not a known unary type trait.");
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002632 case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
Joao Matosc9523d42013-03-27 01:34:16 +00002633 case tok::kw___has_nothrow_move_assign: return UTT_HasNothrowMoveAssign;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002634 case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
John Wiegley65497cc2011-04-27 23:09:49 +00002635 case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002636 case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
Joao Matosc9523d42013-03-27 01:34:16 +00002637 case tok::kw___has_trivial_move_assign: return UTT_HasTrivialMoveAssign;
Alexis Huntf479f1b2011-05-09 18:22:59 +00002638 case tok::kw___has_trivial_constructor:
2639 return UTT_HasTrivialDefaultConstructor;
Joao Matosc9523d42013-03-27 01:34:16 +00002640 case tok::kw___has_trivial_move_constructor:
2641 return UTT_HasTrivialMoveConstructor;
John Wiegley65497cc2011-04-27 23:09:49 +00002642 case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002643 case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
2644 case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
2645 case tok::kw___is_abstract: return UTT_IsAbstract;
John Wiegley65497cc2011-04-27 23:09:49 +00002646 case tok::kw___is_arithmetic: return UTT_IsArithmetic;
2647 case tok::kw___is_array: return UTT_IsArray;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002648 case tok::kw___is_class: return UTT_IsClass;
John Wiegley65497cc2011-04-27 23:09:49 +00002649 case tok::kw___is_complete_type: return UTT_IsCompleteType;
2650 case tok::kw___is_compound: return UTT_IsCompound;
2651 case tok::kw___is_const: return UTT_IsConst;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002652 case tok::kw___is_empty: return UTT_IsEmpty;
2653 case tok::kw___is_enum: return UTT_IsEnum;
Douglas Gregordca70af2011-12-03 18:14:24 +00002654 case tok::kw___is_final: return UTT_IsFinal;
John Wiegley65497cc2011-04-27 23:09:49 +00002655 case tok::kw___is_floating_point: return UTT_IsFloatingPoint;
2656 case tok::kw___is_function: return UTT_IsFunction;
2657 case tok::kw___is_fundamental: return UTT_IsFundamental;
2658 case tok::kw___is_integral: return UTT_IsIntegral;
John McCallbf4a7d72012-09-25 07:32:49 +00002659 case tok::kw___is_interface_class: return UTT_IsInterfaceClass;
John Wiegley65497cc2011-04-27 23:09:49 +00002660 case tok::kw___is_lvalue_reference: return UTT_IsLvalueReference;
2661 case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
2662 case tok::kw___is_member_object_pointer: return UTT_IsMemberObjectPointer;
2663 case tok::kw___is_member_pointer: return UTT_IsMemberPointer;
2664 case tok::kw___is_object: return UTT_IsObject;
Chandler Carruth79803482011-04-23 10:47:20 +00002665 case tok::kw___is_literal: return UTT_IsLiteral;
Chandler Carruth65fa1fd2011-04-24 02:49:28 +00002666 case tok::kw___is_literal_type: return UTT_IsLiteral;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002667 case tok::kw___is_pod: return UTT_IsPOD;
John Wiegley65497cc2011-04-27 23:09:49 +00002668 case tok::kw___is_pointer: return UTT_IsPointer;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002669 case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
John Wiegley65497cc2011-04-27 23:09:49 +00002670 case tok::kw___is_reference: return UTT_IsReference;
John Wiegley65497cc2011-04-27 23:09:49 +00002671 case tok::kw___is_rvalue_reference: return UTT_IsRvalueReference;
2672 case tok::kw___is_scalar: return UTT_IsScalar;
David Majnemera5433082013-10-18 00:33:31 +00002673 case tok::kw___is_sealed: return UTT_IsSealed;
John Wiegley65497cc2011-04-27 23:09:49 +00002674 case tok::kw___is_signed: return UTT_IsSigned;
2675 case tok::kw___is_standard_layout: return UTT_IsStandardLayout;
2676 case tok::kw___is_trivial: return UTT_IsTrivial;
Alexis Huntd9a5cc12011-05-13 00:31:07 +00002677 case tok::kw___is_trivially_copyable: return UTT_IsTriviallyCopyable;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002678 case tok::kw___is_union: return UTT_IsUnion;
John Wiegley65497cc2011-04-27 23:09:49 +00002679 case tok::kw___is_unsigned: return UTT_IsUnsigned;
2680 case tok::kw___is_void: return UTT_IsVoid;
2681 case tok::kw___is_volatile: return UTT_IsVolatile;
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002682 }
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002683}
2684
2685static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
2686 switch(kind) {
Francois Pichet347c4c72010-12-07 00:55:57 +00002687 default: llvm_unreachable("Not a known binary type trait");
Francois Pichet34b21132010-12-08 22:35:30 +00002688 case tok::kw___is_base_of: return BTT_IsBaseOf;
John Wiegley65497cc2011-04-27 23:09:49 +00002689 case tok::kw___is_convertible: return BTT_IsConvertible;
2690 case tok::kw___is_same: return BTT_IsSame;
Francois Pichet34b21132010-12-08 22:35:30 +00002691 case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
Douglas Gregor8006e762011-01-27 20:28:01 +00002692 case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
Douglas Gregor1be329d2012-02-23 07:33:15 +00002693 case tok::kw___is_trivially_assignable: return BTT_IsTriviallyAssignable;
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002694 }
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002695}
2696
Douglas Gregor29c42f22012-02-24 07:38:34 +00002697static TypeTrait TypeTraitFromTokKind(tok::TokenKind kind) {
2698 switch (kind) {
2699 default: llvm_unreachable("Not a known type trait");
2700 case tok::kw___is_trivially_constructible:
2701 return TT_IsTriviallyConstructible;
2702 }
2703}
2704
John Wiegley6242b6a2011-04-28 00:16:57 +00002705static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
2706 switch(kind) {
2707 default: llvm_unreachable("Not a known binary type trait");
2708 case tok::kw___array_rank: return ATT_ArrayRank;
2709 case tok::kw___array_extent: return ATT_ArrayExtent;
2710 }
2711}
2712
John Wiegleyf9f65842011-04-25 06:54:41 +00002713static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
2714 switch(kind) {
David Blaikie83d382b2011-09-23 05:06:16 +00002715 default: llvm_unreachable("Not a known unary expression trait.");
John Wiegleyf9f65842011-04-25 06:54:41 +00002716 case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
2717 case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
2718 }
2719}
2720
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002721/// ParseUnaryTypeTrait - Parse the built-in unary type-trait
2722/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2723/// templates.
2724///
2725/// primary-expression:
2726/// [GNU] unary-type-trait '(' type-id ')'
2727///
John McCalldadc5752010-08-24 06:29:42 +00002728ExprResult Parser::ParseUnaryTypeTrait() {
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002729 UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
2730 SourceLocation Loc = ConsumeToken();
2731
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002732 BalancedDelimiterTracker T(*this, tok::l_paren);
2733 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002734 return ExprError();
2735
2736 // FIXME: Error reporting absolutely sucks! If the this fails to parse a type
2737 // there will be cryptic errors about mismatched parentheses and missing
2738 // specifiers.
Douglas Gregor220cac52009-02-18 17:45:20 +00002739 TypeResult Ty = ParseTypeName();
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002740
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002741 T.consumeClose();
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002742
Douglas Gregor220cac52009-02-18 17:45:20 +00002743 if (Ty.isInvalid())
2744 return ExprError();
2745
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002746 return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), T.getCloseLocation());
Sebastian Redlbaad4e72009-01-05 20:52:13 +00002747}
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002748
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002749/// ParseBinaryTypeTrait - Parse the built-in binary type-trait
2750/// pseudo-functions that allow implementation of the TR1/C++0x type traits
2751/// templates.
2752///
2753/// primary-expression:
2754/// [GNU] binary-type-trait '(' type-id ',' type-id ')'
2755///
2756ExprResult Parser::ParseBinaryTypeTrait() {
2757 BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
2758 SourceLocation Loc = ConsumeToken();
2759
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002760 BalancedDelimiterTracker T(*this, tok::l_paren);
2761 if (T.expectAndConsume(diag::err_expected_lparen))
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002762 return ExprError();
2763
2764 TypeResult LhsTy = ParseTypeName();
2765 if (LhsTy.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002766 SkipUntil(tok::r_paren, StopAtSemi);
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002767 return ExprError();
2768 }
2769
2770 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002771 SkipUntil(tok::r_paren, StopAtSemi);
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002772 return ExprError();
2773 }
2774
2775 TypeResult RhsTy = ParseTypeName();
2776 if (RhsTy.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002777 SkipUntil(tok::r_paren, StopAtSemi);
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002778 return ExprError();
2779 }
2780
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002781 T.consumeClose();
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002782
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002783 return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(),
2784 T.getCloseLocation());
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002785}
2786
Douglas Gregor29c42f22012-02-24 07:38:34 +00002787/// \brief Parse the built-in type-trait pseudo-functions that allow
2788/// implementation of the TR1/C++11 type traits templates.
2789///
2790/// primary-expression:
2791/// type-trait '(' type-id-seq ')'
2792///
2793/// type-id-seq:
2794/// type-id ...[opt] type-id-seq[opt]
2795///
2796ExprResult Parser::ParseTypeTrait() {
2797 TypeTrait Kind = TypeTraitFromTokKind(Tok.getKind());
2798 SourceLocation Loc = ConsumeToken();
2799
2800 BalancedDelimiterTracker Parens(*this, tok::l_paren);
2801 if (Parens.expectAndConsume(diag::err_expected_lparen))
2802 return ExprError();
2803
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002804 SmallVector<ParsedType, 2> Args;
Douglas Gregor29c42f22012-02-24 07:38:34 +00002805 do {
2806 // Parse the next type.
2807 TypeResult Ty = ParseTypeName();
2808 if (Ty.isInvalid()) {
2809 Parens.skipToEnd();
2810 return ExprError();
2811 }
2812
2813 // Parse the ellipsis, if present.
2814 if (Tok.is(tok::ellipsis)) {
2815 Ty = Actions.ActOnPackExpansion(Ty.get(), ConsumeToken());
2816 if (Ty.isInvalid()) {
2817 Parens.skipToEnd();
2818 return ExprError();
2819 }
2820 }
2821
2822 // Add this type to the list of arguments.
2823 Args.push_back(Ty.get());
2824
2825 if (Tok.is(tok::comma)) {
2826 ConsumeToken();
2827 continue;
2828 }
2829
2830 break;
2831 } while (true);
2832
2833 if (Parens.consumeClose())
2834 return ExprError();
2835
2836 return Actions.ActOnTypeTrait(Kind, Loc, Args, Parens.getCloseLocation());
2837}
2838
John Wiegley6242b6a2011-04-28 00:16:57 +00002839/// ParseArrayTypeTrait - Parse the built-in array type-trait
2840/// pseudo-functions.
2841///
2842/// primary-expression:
2843/// [Embarcadero] '__array_rank' '(' type-id ')'
2844/// [Embarcadero] '__array_extent' '(' type-id ',' expression ')'
2845///
2846ExprResult Parser::ParseArrayTypeTrait() {
2847 ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
2848 SourceLocation Loc = ConsumeToken();
2849
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002850 BalancedDelimiterTracker T(*this, tok::l_paren);
2851 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegley6242b6a2011-04-28 00:16:57 +00002852 return ExprError();
2853
2854 TypeResult Ty = ParseTypeName();
2855 if (Ty.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002856 SkipUntil(tok::comma, StopAtSemi);
2857 SkipUntil(tok::r_paren, StopAtSemi);
John Wiegley6242b6a2011-04-28 00:16:57 +00002858 return ExprError();
2859 }
2860
2861 switch (ATT) {
2862 case ATT_ArrayRank: {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002863 T.consumeClose();
2864 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL,
2865 T.getCloseLocation());
John Wiegley6242b6a2011-04-28 00:16:57 +00002866 }
2867 case ATT_ArrayExtent: {
2868 if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002869 SkipUntil(tok::r_paren, StopAtSemi);
John Wiegley6242b6a2011-04-28 00:16:57 +00002870 return ExprError();
2871 }
2872
2873 ExprResult DimExpr = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002874 T.consumeClose();
John Wiegley6242b6a2011-04-28 00:16:57 +00002875
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002876 return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(),
2877 T.getCloseLocation());
John Wiegley6242b6a2011-04-28 00:16:57 +00002878 }
John Wiegley6242b6a2011-04-28 00:16:57 +00002879 }
David Blaikiee4d798f2012-01-20 21:50:17 +00002880 llvm_unreachable("Invalid ArrayTypeTrait!");
John Wiegley6242b6a2011-04-28 00:16:57 +00002881}
2882
John Wiegleyf9f65842011-04-25 06:54:41 +00002883/// ParseExpressionTrait - Parse built-in expression-trait
2884/// pseudo-functions like __is_lvalue_expr( xxx ).
2885///
2886/// primary-expression:
2887/// [Embarcadero] expression-trait '(' expression ')'
2888///
2889ExprResult Parser::ParseExpressionTrait() {
2890 ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
2891 SourceLocation Loc = ConsumeToken();
2892
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002893 BalancedDelimiterTracker T(*this, tok::l_paren);
2894 if (T.expectAndConsume(diag::err_expected_lparen))
John Wiegleyf9f65842011-04-25 06:54:41 +00002895 return ExprError();
2896
2897 ExprResult Expr = ParseExpression();
2898
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002899 T.consumeClose();
John Wiegleyf9f65842011-04-25 06:54:41 +00002900
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002901 return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(),
2902 T.getCloseLocation());
John Wiegleyf9f65842011-04-25 06:54:41 +00002903}
2904
2905
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002906/// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a
2907/// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate
2908/// based on the context past the parens.
John McCalldadc5752010-08-24 06:29:42 +00002909ExprResult
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002910Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
John McCallba7bf592010-08-24 05:47:05 +00002911 ParsedType &CastTy,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002912 BalancedDelimiterTracker &Tracker) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002913 assert(getLangOpts().CPlusPlus && "Should only be called for C++!");
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002914 assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
2915 assert(isTypeIdInParens() && "Not a type-id!");
2916
John McCalldadc5752010-08-24 06:29:42 +00002917 ExprResult Result(true);
John McCallba7bf592010-08-24 05:47:05 +00002918 CastTy = ParsedType();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002919
2920 // We need to disambiguate a very ugly part of the C++ syntax:
2921 //
2922 // (T())x; - type-id
2923 // (T())*x; - type-id
2924 // (T())/x; - expression
2925 // (T()); - expression
2926 //
2927 // The bad news is that we cannot use the specialized tentative parser, since
2928 // it can only verify that the thing inside the parens can be parsed as
2929 // type-id, it is not useful for determining the context past the parens.
2930 //
2931 // The good news is that the parser can disambiguate this part without
Argyrios Kyrtzidis24ad6922009-05-22 15:12:46 +00002932 // making any unnecessary Action calls.
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002933 //
2934 // It uses a scheme similar to parsing inline methods. The parenthesized
2935 // tokens are cached, the context that follows is determined (possibly by
2936 // parsing a cast-expression), and then we re-introduce the cached tokens
2937 // into the token stream and parse them appropriately.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002938
Mike Stump11289f42009-09-09 15:08:12 +00002939 ParenParseOption ParseAs;
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002940 CachedTokens Toks;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002941
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002942 // Store the tokens of the parentheses. We will parse them after we determine
2943 // the context that follows them.
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00002944 if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002945 // We didn't find the ')' we expected.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002946 Tracker.consumeClose();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002947 return ExprError();
2948 }
2949
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002950 if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002951 ParseAs = CompoundLiteral;
2952 } else {
2953 bool NotCastExpr;
Eli Friedmancf7530f2009-05-25 19:41:42 +00002954 // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression
2955 if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
2956 NotCastExpr = true;
2957 } else {
2958 // Try parsing the cast-expression that may follow.
2959 // If it is not a cast-expression, NotCastExpr will be true and no token
2960 // will be consumed.
2961 Result = ParseCastExpression(false/*isUnaryExpression*/,
2962 false/*isAddressofOperand*/,
John McCallba7bf592010-08-24 05:47:05 +00002963 NotCastExpr,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002964 // type-id has priority.
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +00002965 IsTypeCast);
Eli Friedmancf7530f2009-05-25 19:41:42 +00002966 }
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002967
2968 // If we parsed a cast-expression, it's really a type-id, otherwise it's
2969 // an expression.
2970 ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002971 }
2972
Mike Stump11289f42009-09-09 15:08:12 +00002973 // The current token should go after the cached tokens.
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002974 Toks.push_back(Tok);
2975 // Re-enter the stored parenthesized tokens into the token stream, so we may
2976 // parse them now.
2977 PP.EnterTokenStream(Toks.data(), Toks.size(),
2978 true/*DisableMacroExpansion*/, false/*OwnsTokens*/);
2979 // Drop the current token and bring the first cached one. It's the same token
2980 // as when we entered this function.
2981 ConsumeAnyToken();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002982
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002983 if (ParseAs >= CompoundLiteral) {
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002984 // Parse the type declarator.
2985 DeclSpec DS(AttrFactory);
2986 ParseSpecifierQualifierList(DS);
2987 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2988 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002989
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002990 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002991 Tracker.consumeClose();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00002992
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002993 if (ParseAs == CompoundLiteral) {
2994 ExprType = CompoundLiteral;
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002995 TypeResult Ty = ParseTypeName();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002996 return ParseCompoundLiteralExpression(Ty.get(),
2997 Tracker.getOpenLocation(),
2998 Tracker.getCloseLocation());
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00002999 }
Mike Stump11289f42009-09-09 15:08:12 +00003000
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003001 // We parsed '(' type-id ')' and the thing after it wasn't a '{'.
3002 assert(ParseAs == CastExpr);
3003
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00003004 if (DeclaratorInfo.isInvalidType())
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003005 return ExprError();
3006
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003007 // Result is what ParseCastExpression returned earlier.
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003008 if (!Result.isInvalid())
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003009 Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(),
3010 DeclaratorInfo, CastTy,
3011 Tracker.getCloseLocation(), Result.take());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003012 return Result;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003013 }
Mike Stump11289f42009-09-09 15:08:12 +00003014
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003015 // Not a compound literal, and not followed by a cast-expression.
3016 assert(ParseAs == SimpleExpr);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003017
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003018 ExprType = SimpleExpr;
Argyrios Kyrtzidisf73f2d22009-05-22 21:09:47 +00003019 Result = ParseExpression();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003020 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003021 Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(),
3022 Tok.getLocation(), Result.take());
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003023
3024 // Match the ')'.
3025 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003026 SkipUntil(tok::r_paren, StopAtSemi);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003027 return ExprError();
3028 }
Mike Stump11289f42009-09-09 15:08:12 +00003029
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003030 Tracker.consumeClose();
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003031 return Result;
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00003032}